annotate literal.py @ 4:16772d7246e0

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