Mercurial > repos > nick > dna_visualizer
annotate visualize.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 | 534516105ca9 |
children |
rev | line source |
---|---|
8
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
1 #!/usr/bin/python |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
2 import sys |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
3 import random |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
4 import hashlib |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
5 import Image |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
6 import ImageDraw |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
7 |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
8 def main(): |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
9 |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
10 if len(sys.argv) == 1: |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
11 print """USAGE: |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
12 $ ./visualize.py genome.fa outfile.png pxsize |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
13 The last two arguments are optional, but must be in those positions. |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
14 If no outfile name is given, it will attempt to display the image directly. |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
15 If no pxsize is given, the default is 512. A power of 2 is highly |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
16 recommended as the resulting image will be much better laid out.""" |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
17 sys.exit(0) |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
18 |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
19 DEFAULT_SIZE = (512,512) |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
20 size = DEFAULT_SIZE |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
21 if len(sys.argv) > 3: |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
22 user_size = int(sys.argv[3]) |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
23 size = (user_size,user_size) |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
24 |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
25 # can skip hashing and specify the seed as an option (for testing) |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
26 if len(sys.argv) > 4: |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
27 seed = sys.argv[4] |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
28 else: |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
29 seed = get_hash(sys.argv[1]) |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
30 print "seed: "+seed |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
31 random.seed(seed) |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
32 level = 1 |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
33 layers = [] |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
34 image = draw_layer(size, level) |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
35 while 1: |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
36 level+=1 |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
37 layers.append(draw_layer(size, level)) |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
38 if layers[-1] == False: |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
39 break |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
40 # opacity = 256/2**level # opacity #1 |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
41 opacity = 256/2**(level-1) # opacity #2 |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
42 # opacity = 256*(level/2)/2**(level-1) # opacity #3 |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
43 # opacity = 256*(level/float(2**level)) # opacity #4 |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
44 print "opacity: "+str(int(opacity/2.56))+"%" |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
45 mask = Image.new("L", size, color=opacity) |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
46 image.paste(layers[-1], (0,0), mask) |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
47 |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
48 if len(sys.argv) > 2: |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
49 image.save(sys.argv[2], 'PNG') |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
50 else: |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
51 image.show() |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
52 |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
53 def draw_layer(image_size, level): |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
54 """Draw every block for a particular layer |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
55 (blocks of a particular pixel size). |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
56 Returns an image of the finished layer.""" |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
57 (image_width,image_height) = image_size |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
58 (width, height) = block_size(image_size, level) |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
59 if width < 1 or height < 1: |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
60 return False |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
61 print "width, height: "+str(width)+", "+str(height) |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
62 layer = Image.new("RGB", image_size, (0,0,0)) |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
63 draw = ImageDraw.Draw(layer) |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
64 (x,y) = (0,0) |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
65 while y < image_height: |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
66 while x < image_width: |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
67 draw.rectangle([(x,y), (x+width-1, y+height-1)], fill=randcolor()) |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
68 x += width |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
69 y += height |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
70 x = 0 |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
71 return layer |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
72 |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
73 def get_hash(filepath): |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
74 """Compute hash of the file""" |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
75 hashed = hashlib.sha256() |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
76 with open(filepath, 'rb') as filehandle: |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
77 for chunk in iter(lambda: filehandle.read(65536), b''): |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
78 hashed.update(chunk) |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
79 return hashed.hexdigest() |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
80 |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
81 def randcolor(): |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
82 """Return a tuple of random color values""" |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
83 color = [] |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
84 for i in range(3): |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
85 color.append(random.randrange(0,255)) |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
86 return tuple(color) |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
87 |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
88 def block_size(image_size, level): |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
89 """Compute the block width and height for this layer.""" |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
90 (width,height) = image_size |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
91 while level > 0: |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
92 width = width/2 |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
93 height = height/4 |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
94 level-=1 |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
95 if level < 1: |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
96 break |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
97 width = width/4 |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
98 height = height/2 |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
99 level-=1 |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
100 return (width,height) |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
101 |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
102 def format_genome(): |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
103 """Eventually I'd like to attempt to process the genome file into a standard |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
104 format, stripping out details of representation that can change the output, |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
105 such as: |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
106 - upper/lowercase |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
107 - line endings |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
108 - chromosome naming |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
109 - chromosome order |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
110 - noncanonical chromosomes""" |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
111 |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
112 def check_genome(): |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
113 """Eventually I'd like this to check assumptions I might make about the |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
114 genome file. A first check would be that it contains all the chromosomes, in |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
115 the format ''>chr1' or ''>1'. If these assumptions (which format_genome() |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
116 depends on) aren't met, and the format isn't recognized, then default to a |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
117 straight digest of the unmodified file.""" |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
118 |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
119 if __name__ == "__main__": |
534516105ca9
visualize.py: The second tool, for a more aesthetic, abstract visualization.
Nick Stoler <nstoler@psu.edu>
parents:
diff
changeset
|
120 main() |