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
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
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
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
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
5257ce9d9184 Initial literal.py tool
Nick Stoler <nstoler@psu.edu>
parents:
diff changeset
47 group.add_argument('-A', 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('-T', 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('-G', 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 group.add_argument('-C', metavar='R,G,B',
5257ce9d9184 Initial literal.py tool
Nick Stoler <nstoler@psu.edu>
parents:
diff changeset
54 help="""default: %(default)s""")
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 args = parser.parse_args()
5257ce9d9184 Initial literal.py tool
Nick Stoler <nstoler@psu.edu>
parents:
diff changeset
57
5257ce9d9184 Initial literal.py tool
Nick Stoler <nstoler@psu.edu>
parents:
diff changeset
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
5257ce9d9184 Initial literal.py tool
Nick Stoler <nstoler@psu.edu>
parents:
diff changeset
60 except ValueError:
5257ce9d9184 Initial literal.py tool
Nick Stoler <nstoler@psu.edu>
parents:
diff changeset
61 parser.print_help()
5257ce9d9184 Initial literal.py tool
Nick Stoler <nstoler@psu.edu>
parents:
diff changeset
62 fail('\nError: Invalid size string "%s".' % args.size)
5257ce9d9184 Initial literal.py tool
Nick Stoler <nstoler@psu.edu>
parents:
diff changeset
63
5257ce9d9184 Initial literal.py tool
Nick Stoler <nstoler@psu.edu>
parents:
diff changeset
64 fasta = fastareader.FastaLineGenerator(args.fasta)
5257ce9d9184 Initial literal.py tool
Nick Stoler <nstoler@psu.edu>
parents:
diff changeset
65 bases = fasta.bases()
5257ce9d9184 Initial literal.py tool
Nick Stoler <nstoler@psu.edu>
parents:
diff changeset
66
5257ce9d9184 Initial literal.py tool
Nick Stoler <nstoler@psu.edu>
parents:
diff changeset
67 if not args.display:
5257ce9d9184 Initial literal.py tool
Nick Stoler <nstoler@psu.edu>
parents:
diff changeset
68 outfile = args.outfile if args.outfile else outfile_name(args.fasta)
5257ce9d9184 Initial literal.py tool
Nick Stoler <nstoler@psu.edu>
parents:
diff changeset
69 if os.path.exists(outfile) and not args.clobber:
5257ce9d9184 Initial literal.py tool
Nick Stoler <nstoler@psu.edu>
parents:
diff changeset
70 fail('Error: Output filename already taken: "%s"' % outfile)
5257ce9d9184 Initial literal.py tool
Nick Stoler <nstoler@psu.edu>
parents:
diff changeset
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
5257ce9d9184 Initial literal.py tool
Nick Stoler <nstoler@psu.edu>
parents:
diff changeset
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
5257ce9d9184 Initial literal.py tool
Nick Stoler <nstoler@psu.edu>
parents:
diff changeset
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
5257ce9d9184 Initial literal.py tool
Nick Stoler <nstoler@psu.edu>
parents:
diff changeset
81 pixels = image.load()
5257ce9d9184 Initial literal.py tool
Nick Stoler <nstoler@psu.edu>
parents:
diff changeset
82
5257ce9d9184 Initial literal.py tool
Nick Stoler <nstoler@psu.edu>
parents:
diff changeset
83 done = False
5257ce9d9184 Initial literal.py tool
Nick Stoler <nstoler@psu.edu>
parents:
diff changeset
84 for i in range(image.size[1]):
5257ce9d9184 Initial literal.py tool
Nick Stoler <nstoler@psu.edu>
parents:
diff changeset
85 for j in range(image.size[0]):
5257ce9d9184 Initial literal.py tool
Nick Stoler <nstoler@psu.edu>
parents:
diff changeset
86 try:
5257ce9d9184 Initial literal.py tool
Nick Stoler <nstoler@psu.edu>
parents:
diff changeset
87 base = next(bases).upper()
5257ce9d9184 Initial literal.py tool
Nick Stoler <nstoler@psu.edu>
parents:
diff changeset
88 except StopIteration:
5257ce9d9184 Initial literal.py tool
Nick Stoler <nstoler@psu.edu>
parents:
diff changeset
89 done = True
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 if base in colors:
5257ce9d9184 Initial literal.py tool
Nick Stoler <nstoler@psu.edu>
parents:
diff changeset
92 pixels[j,i] = colors[base]
5257ce9d9184 Initial literal.py tool
Nick Stoler <nstoler@psu.edu>
parents:
diff changeset
93 if done:
5257ce9d9184 Initial literal.py tool
Nick Stoler <nstoler@psu.edu>
parents:
diff changeset
94 break
5257ce9d9184 Initial literal.py tool
Nick Stoler <nstoler@psu.edu>
parents:
diff changeset
95
5257ce9d9184 Initial literal.py tool
Nick Stoler <nstoler@psu.edu>
parents:
diff changeset
96 if args.display:
5257ce9d9184 Initial literal.py tool
Nick Stoler <nstoler@psu.edu>
parents:
diff changeset
97 image.show()
5257ce9d9184 Initial literal.py tool
Nick Stoler <nstoler@psu.edu>
parents:
diff changeset
98 else:
5257ce9d9184 Initial literal.py tool
Nick Stoler <nstoler@psu.edu>
parents:
diff changeset
99 image.save(outfile, 'PNG')
5257ce9d9184 Initial literal.py tool
Nick Stoler <nstoler@psu.edu>
parents:
diff changeset
100
5257ce9d9184 Initial literal.py tool
Nick Stoler <nstoler@psu.edu>
parents:
diff changeset
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
5257ce9d9184 Initial literal.py tool
Nick Stoler <nstoler@psu.edu>
parents:
diff changeset
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
5257ce9d9184 Initial literal.py tool
Nick Stoler <nstoler@psu.edu>
parents:
diff changeset
109 raise ValueError
5257ce9d9184 Initial literal.py tool
Nick Stoler <nstoler@psu.edu>
parents:
diff changeset
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
5257ce9d9184 Initial literal.py tool
Nick Stoler <nstoler@psu.edu>
parents:
diff changeset
112
5257ce9d9184 Initial literal.py tool
Nick Stoler <nstoler@psu.edu>
parents:
diff changeset
113
5257ce9d9184 Initial literal.py tool
Nick Stoler <nstoler@psu.edu>
parents:
diff changeset
114 def outfile_name(infilename):
5257ce9d9184 Initial literal.py tool
Nick Stoler <nstoler@psu.edu>
parents:
diff changeset
115 base = infilename.split('.')[0]
5257ce9d9184 Initial literal.py tool
Nick Stoler <nstoler@psu.edu>
parents:
diff changeset
116 if not base:
5257ce9d9184 Initial literal.py tool
Nick Stoler <nstoler@psu.edu>
parents:
diff changeset
117 base = infilename
5257ce9d9184 Initial literal.py tool
Nick Stoler <nstoler@psu.edu>
parents:
diff changeset
118 return base+'.png'
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
5257ce9d9184 Initial literal.py tool
Nick Stoler <nstoler@psu.edu>
parents:
diff changeset
121 def fail(message):
5257ce9d9184 Initial literal.py tool
Nick Stoler <nstoler@psu.edu>
parents:
diff changeset
122 sys.stderr.write(message+"\n")
5257ce9d9184 Initial literal.py tool
Nick Stoler <nstoler@psu.edu>
parents:
diff changeset
123 sys.exit(1)
5257ce9d9184 Initial literal.py tool
Nick Stoler <nstoler@psu.edu>
parents:
diff changeset
124
5257ce9d9184 Initial literal.py tool
Nick Stoler <nstoler@psu.edu>
parents:
diff changeset
125 if __name__ == '__main__':
5257ce9d9184 Initial literal.py tool
Nick Stoler <nstoler@psu.edu>
parents:
diff changeset
126 main()