# HG changeset patch # User Nick Stoler # Date 1393815704 18000 # Node ID 58160195728e323d6126c0c40341a20ce09960a7 # Parent 16772d7246e0df91617ffc1e1fd27c99650cadf1 Change color setting UI, allow background color setting, limit image size. diff -r 16772d7246e0 -r 58160195728e literal.py --- a/literal.py Sun Mar 02 15:02:00 2014 -0500 +++ b/literal.py Sun Mar 02 22:01:44 2014 -0500 @@ -6,7 +6,7 @@ import argparse import fastareader -OPT_DEFAULTS = {'size':'512x512', 'verbose':True, +OPT_DEFAULTS = {'size':'512x512', 'verbose':True, 'background':'255,255,255', 'A':'0,255,0', 'T':'255,0,0', 'G':'255,255,255', 'C':'0,0,255'} USAGE = "%(prog)s [options] genome.fasta" DESCRIPTION = """Convert DNA sequence into a PNG image by representing each base @@ -40,8 +40,10 @@ parser.add_argument('-q', '--quiet', action='store_false', dest='verbose', help="""Quiet mode.""") group = parser.add_argument_group('Color customization', """Use these options - to use custom colors for bases. Specify with a comma-delimited RGB value - like "100,150,10".""") + to use custom colors for the background and the bases. Specify with a + comma-delimited RGB value like "100,150,10".""") + group.add_argument('-b', '--background', metavar='R,G,B', + help="""default: %(default)s""") group.add_argument('-A', metavar='R,G,B', help="""default: %(default)s""") group.add_argument('-T', metavar='R,G,B', @@ -54,7 +56,7 @@ args = parser.parse_args() try: - size = parse_size(args.size) + size = parse_int_str(args.size, delim='x', num_ints=2) except ValueError: parser.print_help() fail('\nError: Invalid size string "%s".' % args.size) @@ -67,13 +69,15 @@ if os.path.exists(outfile) and not args.clobber: fail('Error: Output filename already taken: "%s"' % outfile) + background = parse_int_str(args.background) + colors = {} - colors['A'] = parse_rgb(args.A) - colors['T'] = parse_rgb(args.T) - colors['G'] = parse_rgb(args.G) - colors['C'] = parse_rgb(args.C) + colors['A'] = parse_int_str(args.A) + colors['T'] = parse_int_str(args.T) + colors['G'] = parse_int_str(args.G) + colors['C'] = parse_int_str(args.C) - image = Image.new('RGB', size, 'white') + image = Image.new('RGB', size, background) pixels = image.load() done = False @@ -95,26 +99,16 @@ image.save(outfile, 'PNG') - -def parse_size(size_str): - """Parse size string, return a tuple of (width, height). - Accepts size strings in the format "640x480". +def parse_int_str(string, delim=',', num_ints=3): + """Parse string of delimited ints, return them in a tuple. + Checks that they are ints, they are delimited by "delim", and there are + "num_ints" of them. If not valid, raises ValueError.""" - size = map(int, size_str.split('x')) - if len(size) != 2: + ints = map(int, string.split(delim)) + if len(ints) != num_ints: raise ValueError else: - return tuple(size) - - -def parse_rgb(rgb_str): - """Parse RGB string, return a tuple of (R, G, B). - If not valid, raises ValueError.""" - rgb = map(int, rgb_str.split(',')) - if len(rgb) != 3: - raise ValueError - else: - return tuple(rgb) + return tuple(ints) def outfile_name(infilename): diff -r 16772d7246e0 -r 58160195728e literal.xml --- a/literal.xml Sun Mar 02 15:02:00 2014 -0500 +++ b/literal.xml Sun Mar 02 22:01:44 2014 -0500 @@ -2,13 +2,13 @@ Visualize DNA with colored pixels. literal.py $input -o $output -c -s ${width}x${height} #if $colors.custom: - -A $colors.AR,$colors.AG,$colors.AB -T $colors.TR,$colors.TG,$colors.TB -G $colors.GR,$colors.GG,$colors.GB -C $colors.CR,$colors.CG,$colors.CB + -b $colors.background -G $colors.G -C $colors.C -A $colors.A -T $colors.T #end if - - + + @@ -18,29 +18,28 @@ - - - - - - - - - - - - - + + + ^[0-9]+,[0-9]+,[0-9]+$ + + + ^[0-9]+,[0-9]+,[0-9]+$ + + + ^[0-9]+,[0-9]+,[0-9]+$ + + + ^[0-9]+,[0-9]+,[0-9]+$ + + + ^[0-9]+,[0-9]+,[0-9]+$ + - - - -