Mercurial > repos > nick > dna_visualizer
annotate literal.py @ 10:54223991334b default tip
tool_dependencies.xml: Defined PIL dependency
author | Nick Stoler <nstoler@psu.edu> |
---|---|
date | Mon, 03 Mar 2014 12:54:02 -0500 |
parents | 58160195728e |
children |
rev | line source |
---|---|
0 | 1 #!/usr/bin/env python |
2 from __future__ import division | |
3 import os | |
4 import sys | |
5 import Image | |
6 import argparse | |
7 import fastareader | |
8 | |
5
58160195728e
Change color setting UI, allow background color setting, limit image size.
Nick Stoler <nstoler@psu.edu>
parents:
0
diff
changeset
|
9 OPT_DEFAULTS = {'size':'512x512', 'verbose':True, 'background':'255,255,255', |
0 | 10 'A':'0,255,0', 'T':'255,0,0', 'G':'255,255,255', 'C':'0,0,255'} |
11 USAGE = "%(prog)s [options] genome.fasta" | |
12 DESCRIPTION = """Convert DNA sequence into a PNG image by representing each base | |
13 with one colored pixel.""" | |
14 EPILOG = """""" | |
15 | |
16 def main(): | |
17 | |
18 parser = argparse.ArgumentParser( | |
19 description=DESCRIPTION, usage=USAGE, epilog=EPILOG) | |
20 parser.set_defaults(**OPT_DEFAULTS) | |
21 | |
22 parser.add_argument('fasta', metavar='genome.fasta', | |
23 help="""Input sequence. Can be in FASTA format or a plain text file | |
24 containing only the sequence. Any non-ATGC characters (case-insensitive) | |
25 will be skipped.""") | |
26 parser.add_argument('-s', '--size', | |
27 help="""The output image size, in pixels, in the format "widthxheight", e.g. | |
28 "640x480". If the sequence is larger than the number of pixels in the | |
29 image, it will be cut off. Default size: %(default)s""") | |
30 parser.add_argument('-o', '--outfile', metavar='image.png', | |
31 help="""Output filename. Overrides the default, which is | |
32 to use the input filename base plus .png.""") | |
33 parser.add_argument('-d', '--display', action='store_true', | |
34 help="""Display the image instead of saving it.""") | |
35 parser.add_argument('-c', '--clobber', action='store_true', | |
36 help="""If the output filename already exists, overwrite it instead of | |
37 throwing an error (the default).""") | |
38 parser.add_argument('-v', '--verbose', action='store_true', | |
39 help="""Verbose mode. On by default.""") | |
40 parser.add_argument('-q', '--quiet', action='store_false', dest='verbose', | |
41 help="""Quiet mode.""") | |
42 group = parser.add_argument_group('Color customization', """Use these options | |
5
58160195728e
Change color setting UI, allow background color setting, limit image size.
Nick Stoler <nstoler@psu.edu>
parents:
0
diff
changeset
|
43 to use custom colors for the background and the bases. Specify with a |
58160195728e
Change color setting UI, allow background color setting, limit image size.
Nick Stoler <nstoler@psu.edu>
parents:
0
diff
changeset
|
44 comma-delimited RGB value like "100,150,10".""") |
58160195728e
Change color setting UI, allow background color setting, limit image size.
Nick Stoler <nstoler@psu.edu>
parents:
0
diff
changeset
|
45 group.add_argument('-b', '--background', metavar='R,G,B', |
58160195728e
Change color setting UI, allow background color setting, limit image size.
Nick Stoler <nstoler@psu.edu>
parents:
0
diff
changeset
|
46 help="""default: %(default)s""") |
0 | 47 group.add_argument('-A', metavar='R,G,B', |
48 help="""default: %(default)s""") | |
49 group.add_argument('-T', metavar='R,G,B', | |
50 help="""default: %(default)s""") | |
51 group.add_argument('-G', metavar='R,G,B', | |
52 help="""default: %(default)s""") | |
53 group.add_argument('-C', metavar='R,G,B', | |
54 help="""default: %(default)s""") | |
55 | |
56 args = parser.parse_args() | |
57 | |
58 try: | |
5
58160195728e
Change color setting UI, allow background color setting, limit image size.
Nick Stoler <nstoler@psu.edu>
parents:
0
diff
changeset
|
59 size = parse_int_str(args.size, delim='x', num_ints=2) |
0 | 60 except ValueError: |
61 parser.print_help() | |
62 fail('\nError: Invalid size string "%s".' % args.size) | |
63 | |
64 fasta = fastareader.FastaLineGenerator(args.fasta) | |
65 bases = fasta.bases() | |
66 | |
67 if not args.display: | |
68 outfile = args.outfile if args.outfile else outfile_name(args.fasta) | |
69 if os.path.exists(outfile) and not args.clobber: | |
70 fail('Error: Output filename already taken: "%s"' % outfile) | |
71 | |
5
58160195728e
Change color setting UI, allow background color setting, limit image size.
Nick Stoler <nstoler@psu.edu>
parents:
0
diff
changeset
|
72 background = parse_int_str(args.background) |
58160195728e
Change color setting UI, allow background color setting, limit image size.
Nick Stoler <nstoler@psu.edu>
parents:
0
diff
changeset
|
73 |
0 | 74 colors = {} |
5
58160195728e
Change color setting UI, allow background color setting, limit image size.
Nick Stoler <nstoler@psu.edu>
parents:
0
diff
changeset
|
75 colors['A'] = parse_int_str(args.A) |
58160195728e
Change color setting UI, allow background color setting, limit image size.
Nick Stoler <nstoler@psu.edu>
parents:
0
diff
changeset
|
76 colors['T'] = parse_int_str(args.T) |
58160195728e
Change color setting UI, allow background color setting, limit image size.
Nick Stoler <nstoler@psu.edu>
parents:
0
diff
changeset
|
77 colors['G'] = parse_int_str(args.G) |
58160195728e
Change color setting UI, allow background color setting, limit image size.
Nick Stoler <nstoler@psu.edu>
parents:
0
diff
changeset
|
78 colors['C'] = parse_int_str(args.C) |
0 | 79 |
5
58160195728e
Change color setting UI, allow background color setting, limit image size.
Nick Stoler <nstoler@psu.edu>
parents:
0
diff
changeset
|
80 image = Image.new('RGB', size, background) |
0 | 81 pixels = image.load() |
82 | |
83 done = False | |
84 for i in range(image.size[1]): | |
85 for j in range(image.size[0]): | |
86 try: | |
87 base = next(bases).upper() | |
88 except StopIteration: | |
89 done = True | |
90 break | |
91 if base in colors: | |
92 pixels[j,i] = colors[base] | |
93 if done: | |
94 break | |
95 | |
96 if args.display: | |
97 image.show() | |
98 else: | |
99 image.save(outfile, 'PNG') | |
100 | |
101 | |
5
58160195728e
Change color setting UI, allow background color setting, limit image size.
Nick Stoler <nstoler@psu.edu>
parents:
0
diff
changeset
|
102 def parse_int_str(string, delim=',', num_ints=3): |
58160195728e
Change color setting UI, allow background color setting, limit image size.
Nick Stoler <nstoler@psu.edu>
parents:
0
diff
changeset
|
103 """Parse string of delimited ints, return them in a tuple. |
58160195728e
Change color setting UI, allow background color setting, limit image size.
Nick Stoler <nstoler@psu.edu>
parents:
0
diff
changeset
|
104 Checks that they are ints, they are delimited by "delim", and there are |
58160195728e
Change color setting UI, allow background color setting, limit image size.
Nick Stoler <nstoler@psu.edu>
parents:
0
diff
changeset
|
105 "num_ints" of them. |
0 | 106 If not valid, raises ValueError.""" |
5
58160195728e
Change color setting UI, allow background color setting, limit image size.
Nick Stoler <nstoler@psu.edu>
parents:
0
diff
changeset
|
107 ints = map(int, string.split(delim)) |
58160195728e
Change color setting UI, allow background color setting, limit image size.
Nick Stoler <nstoler@psu.edu>
parents:
0
diff
changeset
|
108 if len(ints) != num_ints: |
0 | 109 raise ValueError |
110 else: | |
5
58160195728e
Change color setting UI, allow background color setting, limit image size.
Nick Stoler <nstoler@psu.edu>
parents:
0
diff
changeset
|
111 return tuple(ints) |
0 | 112 |
113 | |
114 def outfile_name(infilename): | |
115 base = infilename.split('.')[0] | |
116 if not base: | |
117 base = infilename | |
118 return base+'.png' | |
119 | |
120 | |
121 def fail(message): | |
122 sys.stderr.write(message+"\n") | |
123 sys.exit(1) | |
124 | |
125 if __name__ == '__main__': | |
126 main() |