comparison literal.py @ 5:58160195728e

Change color setting UI, allow background color setting, limit image size.
author Nick Stoler <nstoler@psu.edu>
date Sun, 02 Mar 2014 22:01:44 -0500
parents 5257ce9d9184
children
comparison
equal deleted inserted replaced
4:16772d7246e0 5:58160195728e
4 import sys 4 import sys
5 import Image 5 import Image
6 import argparse 6 import argparse
7 import fastareader 7 import fastareader
8 8
9 OPT_DEFAULTS = {'size':'512x512', 'verbose':True, 9 OPT_DEFAULTS = {'size':'512x512', 'verbose':True, 'background':'255,255,255',
10 'A':'0,255,0', 'T':'255,0,0', 'G':'255,255,255', 'C':'0,0,255'} 10 'A':'0,255,0', 'T':'255,0,0', 'G':'255,255,255', 'C':'0,0,255'}
11 USAGE = "%(prog)s [options] genome.fasta" 11 USAGE = "%(prog)s [options] genome.fasta"
12 DESCRIPTION = """Convert DNA sequence into a PNG image by representing each base 12 DESCRIPTION = """Convert DNA sequence into a PNG image by representing each base
13 with one colored pixel.""" 13 with one colored pixel."""
14 EPILOG = """""" 14 EPILOG = """"""
38 parser.add_argument('-v', '--verbose', action='store_true', 38 parser.add_argument('-v', '--verbose', action='store_true',
39 help="""Verbose mode. On by default.""") 39 help="""Verbose mode. On by default.""")
40 parser.add_argument('-q', '--quiet', action='store_false', dest='verbose', 40 parser.add_argument('-q', '--quiet', action='store_false', dest='verbose',
41 help="""Quiet mode.""") 41 help="""Quiet mode.""")
42 group = parser.add_argument_group('Color customization', """Use these options 42 group = parser.add_argument_group('Color customization', """Use these options
43 to use custom colors for bases. Specify with a comma-delimited RGB value 43 to use custom colors for the background and the bases. Specify with a
44 like "100,150,10".""") 44 comma-delimited RGB value like "100,150,10".""")
45 group.add_argument('-b', '--background', metavar='R,G,B',
46 help="""default: %(default)s""")
45 group.add_argument('-A', metavar='R,G,B', 47 group.add_argument('-A', metavar='R,G,B',
46 help="""default: %(default)s""") 48 help="""default: %(default)s""")
47 group.add_argument('-T', metavar='R,G,B', 49 group.add_argument('-T', metavar='R,G,B',
48 help="""default: %(default)s""") 50 help="""default: %(default)s""")
49 group.add_argument('-G', metavar='R,G,B', 51 group.add_argument('-G', metavar='R,G,B',
52 help="""default: %(default)s""") 54 help="""default: %(default)s""")
53 55
54 args = parser.parse_args() 56 args = parser.parse_args()
55 57
56 try: 58 try:
57 size = parse_size(args.size) 59 size = parse_int_str(args.size, delim='x', num_ints=2)
58 except ValueError: 60 except ValueError:
59 parser.print_help() 61 parser.print_help()
60 fail('\nError: Invalid size string "%s".' % args.size) 62 fail('\nError: Invalid size string "%s".' % args.size)
61 63
62 fasta = fastareader.FastaLineGenerator(args.fasta) 64 fasta = fastareader.FastaLineGenerator(args.fasta)
65 if not args.display: 67 if not args.display:
66 outfile = args.outfile if args.outfile else outfile_name(args.fasta) 68 outfile = args.outfile if args.outfile else outfile_name(args.fasta)
67 if os.path.exists(outfile) and not args.clobber: 69 if os.path.exists(outfile) and not args.clobber:
68 fail('Error: Output filename already taken: "%s"' % outfile) 70 fail('Error: Output filename already taken: "%s"' % outfile)
69 71
72 background = parse_int_str(args.background)
73
70 colors = {} 74 colors = {}
71 colors['A'] = parse_rgb(args.A) 75 colors['A'] = parse_int_str(args.A)
72 colors['T'] = parse_rgb(args.T) 76 colors['T'] = parse_int_str(args.T)
73 colors['G'] = parse_rgb(args.G) 77 colors['G'] = parse_int_str(args.G)
74 colors['C'] = parse_rgb(args.C) 78 colors['C'] = parse_int_str(args.C)
75 79
76 image = Image.new('RGB', size, 'white') 80 image = Image.new('RGB', size, background)
77 pixels = image.load() 81 pixels = image.load()
78 82
79 done = False 83 done = False
80 for i in range(image.size[1]): 84 for i in range(image.size[1]):
81 for j in range(image.size[0]): 85 for j in range(image.size[0]):
93 image.show() 97 image.show()
94 else: 98 else:
95 image.save(outfile, 'PNG') 99 image.save(outfile, 'PNG')
96 100
97 101
98 102 def parse_int_str(string, delim=',', num_ints=3):
99 def parse_size(size_str): 103 """Parse string of delimited ints, return them in a tuple.
100 """Parse size string, return a tuple of (width, height). 104 Checks that they are ints, they are delimited by "delim", and there are
101 Accepts size strings in the format "640x480". 105 "num_ints" of them.
102 If not valid, raises ValueError.""" 106 If not valid, raises ValueError."""
103 size = map(int, size_str.split('x')) 107 ints = map(int, string.split(delim))
104 if len(size) != 2: 108 if len(ints) != num_ints:
105 raise ValueError 109 raise ValueError
106 else: 110 else:
107 return tuple(size) 111 return tuple(ints)
108
109
110 def parse_rgb(rgb_str):
111 """Parse RGB string, return a tuple of (R, G, B).
112 If not valid, raises ValueError."""
113 rgb = map(int, rgb_str.split(','))
114 if len(rgb) != 3:
115 raise ValueError
116 else:
117 return tuple(rgb)
118 112
119 113
120 def outfile_name(infilename): 114 def outfile_name(infilename):
121 base = infilename.split('.')[0] 115 base = infilename.split('.')[0]
122 if not base: 116 if not base: