Mercurial > repos > miller-lab > genome_diversity
annotate dpmix_plot.py @ 28:184d14e4270d
Update to Miller Lab devshed revision 4ede22dd5500
author | Richard Burhans <burhans@bx.psu.edu> |
---|---|
date | Wed, 17 Jul 2013 12:46:46 -0400 |
parents | 8997f2ca8c7a |
children | a631c2f6d913 |
rev | line source |
---|---|
12 | 1 #!/usr/bin/env python |
2 | |
3 import os | |
4 import sys | |
5 import math | |
6 import matplotlib as mpl | |
7 mpl.use('PDF') | |
8 import matplotlib.pyplot as plt | |
9 from matplotlib.path import Path | |
10 import matplotlib.patches as patches | |
11 | |
12 ################################################################################ | |
13 | |
14 def build_chrom_len_dict(dbkey, galaxy_data_index_dir): | |
15 chrom_len_root = os.path.join(galaxy_data_index_dir, 'shared/ucsc/chrom') | |
16 chrom_len_file = '{0}.len'.format(dbkey) | |
17 chrom_len_path = os.path.join(chrom_len_root, chrom_len_file) | |
18 | |
19 chrom_len = {} | |
20 | |
21 try: | |
22 with open(chrom_len_path) as fh: | |
23 for line in fh: | |
24 line = line.rstrip('\r\n') | |
25 elems = line.split() | |
26 if len(elems) == 2: | |
27 chrom = elems[0] | |
28 length = int(elems[1]) | |
29 chrom_len[chrom] = length | |
30 except: | |
31 pass | |
32 | |
33 return chrom_len | |
34 | |
35 def parse_input_file(input_file): | |
36 chroms = [] | |
37 individuals = [] | |
38 data = {} | |
39 chrom_len = {} | |
27
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
40 used_states = [] |
12 | 41 |
42 with open(input_file) as fh: | |
43 for line in fh: | |
44 line = line.strip() | |
45 if line: | |
46 elems = line.split() | |
47 chrom = elems[0] | |
48 p1, p2, state = map(int, elems[1:4]) | |
49 id = elems[4] | |
50 | |
27
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
51 if state not in used_states: |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
52 used_states.append(state) |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
53 |
12 | 54 if chrom not in chroms: |
55 chroms.append(chrom) | |
56 | |
57 if id not in individuals: | |
58 individuals.append(id) | |
59 | |
60 data.setdefault(chrom, {}) | |
61 data[chrom].setdefault(id, []) | |
62 data[chrom][id].append((p1, p2, state)) | |
63 | |
64 if p2 > chrom_len.setdefault(chrom, 0): | |
65 chrom_len[chrom] = p2 | |
66 | |
27
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
67 return chroms, individuals, data, chrom_len, used_states |
12 | 68 |
69 def check_chroms(chroms, chrom_len, dbkey): | |
70 error = 0 | |
71 for chrom in chroms: | |
72 if chrom not in chrom_len: | |
73 print >> sys.stderr, "Can't find length for {0} chromosome {1}".format(dbkey, chrom) | |
74 error = 1 | |
75 if error: | |
76 sys.exit(1) | |
77 | |
78 def check_data(data, chrom_len, dbkey): | |
79 error = 0 | |
80 for chrom in data: | |
81 chrom_beg = 0 | |
82 chrom_end = chrom_len[chrom] | |
83 for individual in data[chrom]: | |
84 for p1, p2, state in data[chrom][individual]: | |
85 if p1 >= p2: | |
86 print >> sys.stderr, "Bad data line: begin >= end: {0} {1} {2} {3}".format(chrom, p1, p2, state, individual) | |
87 error = 1 | |
88 if p1 < chrom_beg or p2 > chrom_end: | |
89 print >> sys.stderr, "Bad data line: outside {0} boundaries[{1} - {2}]: {3} {4} {5} {6}".format(dbkey, chrom_beg, chrom_end, chrom, p1, p2, state, individual) | |
90 error = 1 | |
91 if error: | |
92 sys.exit(1) | |
93 | |
94 def make_rectangle(p1, p2, color, bottom=0.0, top=1.0): | |
95 verts = [ | |
96 (p1, bottom), # left, bottom | |
97 (p1, top), # left, top | |
98 (p2, top), # right, top | |
99 (p2, bottom), # right, bottom | |
100 (0.0, 0.0) # ignored | |
101 ] | |
102 | |
103 codes = [ | |
104 Path.MOVETO, | |
105 Path.LINETO, | |
106 Path.LINETO, | |
107 Path.LINETO, | |
108 Path.CLOSEPOLY | |
109 ] | |
110 | |
111 path = Path(verts, codes) | |
112 return patches.PathPatch(path, facecolor=color, lw=0) | |
113 | |
114 def make_split_rectangle(p1, p2, top_color, bottom_color): | |
115 patch1 = make_rectangle(p1, p2, bottom_color, top=0.5) | |
116 patch2 = make_rectangle(p1, p2, top_color, bottom=0.5) | |
117 return [patch1, patch2] | |
118 | |
27
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
119 def make_state_rectangle_2pop(p1, p2, state, chrom, individual): |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
120 p1_color = 'r' |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
121 p2_color = 'g' |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
122 heterochromatin_color = '#c7c7c7' |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
123 |
12 | 124 if state == 0: |
27
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
125 return [ make_rectangle(p1, p2, heterochromatin_color) ] |
12 | 126 elif state == 1: |
27
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
127 return [ make_rectangle(p1, p2, p1_color) ] |
12 | 128 elif state == 2: |
27
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
129 return [ make_rectangle(p1, p2, p2_color) ] |
12 | 130 elif state == 3: |
27
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
131 return make_split_rectangle(p1, p2, p1_color, p2_color) |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
132 else: |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
133 print >> sys.stderr, "Unknown state: {0}: {1} {2} {3} {4}".format(state, chrom, p1, p2, state, individual) |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
134 sys.exit(1) |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
135 |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
136 def make_state_rectangle_3pop(p1, p2, state, chrom, individual): |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
137 p1_color = 'r' |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
138 p2_color = 'g' |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
139 p3_color = 'b' |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
140 heterochromatin_color = '#c7c7c7' |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
141 |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
142 if state == 0: |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
143 return [ make_rectangle(p1, p2, heterochromatin_color) ] |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
144 if state == 1: |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
145 return [ make_rectangle(p1, p2, p1_color) ] |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
146 if state == 2: |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
147 return [ make_rectangle(p1, p2, p2_color) ] |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
148 if state == 3: |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
149 return [ make_rectangle(p1, p2, p3_color) ] |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
150 if state == 4: |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
151 return make_split_rectangle(p1, p2, p1_color, p2_color) |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
152 if state == 5: |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
153 return make_split_rectangle(p1, p2, p1_color, p3_color) |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
154 if state == 6: |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
155 return make_split_rectangle(p1, p2, p2_color, p3_color) |
12 | 156 else: |
157 print >> sys.stderr, "Unknown state: {0}: {1} {2} {3} {4}".format(state, chrom, p1, p2, state, individual) | |
158 sys.exit(1) | |
159 | |
160 def nicenum(num, round=False): | |
161 if num == 0: | |
162 return 0.0 | |
163 | |
164 exp = int(math.floor(math.log10(num))) | |
165 f = num / math.pow(10, exp) | |
166 | |
167 if round: | |
168 if f < 1.5: | |
169 nf = 1.0 | |
170 elif f < 3.0: | |
171 nf = 2.0 | |
172 elif f < 7.0: | |
173 nf = 5.0 | |
174 else: | |
175 nf = 10.0 | |
176 else: | |
177 if f <= 1.0: | |
178 nf = 1.0 | |
179 elif f <= 2.0: | |
180 nf = 2.0 | |
181 elif f <= 5.0: | |
182 nf = 5.0 | |
183 else: | |
184 nf = 10.0 | |
185 | |
186 return nf * pow(10, exp) | |
187 | |
188 def tick_foo(beg, end, loose=False): | |
189 ntick = 10 | |
190 | |
191 range = nicenum(end - beg, round=False) | |
192 d = nicenum(range/(ntick - 1), round=True) | |
193 digits = int(math.floor(math.log10(d))) | |
194 | |
195 if loose: | |
196 graph_min = math.floor(beg/d) * d | |
197 graph_max = math.ceil(end/d) * d | |
198 else: | |
199 graph_min = beg | |
200 graph_max = end | |
201 | |
202 nfrac = max([-1 * digits, 0]) | |
203 vals = [] | |
204 | |
205 stop = graph_max | |
206 if loose: | |
207 stop = graph_max + (0.5 * d) | |
208 | |
209 x = graph_min | |
210 while x <= stop: | |
211 vals.append(int(x)) | |
212 x += d | |
213 | |
214 vals = vals[1:] | |
215 | |
216 # if not loose: | |
217 # if vals[-1] < graph_max: | |
218 # vals.append(int(graph_max)) | |
219 | |
220 labels = [] | |
221 for val in vals: | |
222 labels.append('{0}'.format(int(val/math.pow(10, digits)))) | |
223 | |
224 # labels.append('{0:.1f}'.format(vals[-1]/math.pow(10, digits))) | |
225 | |
226 return vals, labels | |
227 | |
228 ################################################################################ | |
229 | |
27
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
230 def make_dpmix_plot(input_dbkey, input_file, output_file, galaxy_data_index_dir, state2name=None, populations=3): |
12 | 231 fs_chrom_len = build_chrom_len_dict(input_dbkey, galaxy_data_index_dir) |
27
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
232 chroms, individuals, data, chrom_len, used_states = parse_input_file(input_file) |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
233 |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
234 if populations == 3: |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
235 make_state_rectangle = make_state_rectangle_3pop |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
236 elif populations == 2: |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
237 make_state_rectangle = make_state_rectangle_2pop |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
238 else: |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
239 pass |
12 | 240 |
241 for chrom in chrom_len.keys(): | |
242 if chrom in fs_chrom_len: | |
243 chrom_len[chrom] = fs_chrom_len[chrom] | |
244 | |
245 #check_chroms(chroms, chrom_len, input_dbkey) | |
246 check_data(data, chrom_len, input_dbkey) | |
247 | |
248 ## units below are inches | |
249 top_space = 0.10 | |
250 chrom_space = 0.25 | |
251 chrom_height = 0.25 | |
252 ind_space = 0.10 | |
253 ind_height = 0.25 | |
254 | |
255 total_height = 0.0 | |
27
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
256 |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
257 ## make a legend |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
258 ## only print out states that are |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
259 ## 1) in the data |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
260 ## - AND - |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
261 ## 2) in the state2name map |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
262 ## here, we only calculate the space needed |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
263 legend_states = [] |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
264 if state2name is not None: |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
265 for state in used_states: |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
266 if state in state2name: |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
267 legend_states.append(state) |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
268 |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
269 if legend_states: |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
270 total_height += len(legend_states) * (ind_space + ind_height) |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
271 total_height += (top_space - ind_space) |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
272 at_top = False |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
273 else: |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
274 at_top = True |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
275 |
12 | 276 for chrom in chroms: |
277 if at_top: | |
278 total_height += (top_space + chrom_height) | |
279 at_top = False | |
280 else: | |
281 total_height += (top_space + chrom_space + chrom_height) | |
282 | |
283 individual_count = 0 | |
284 for individual in individuals: | |
285 if individual in data[chrom]: | |
286 individual_count += 1 | |
287 total_height += individual_count * (ind_space + ind_height) | |
288 | |
289 width = 7.5 | |
290 height = math.ceil(total_height) | |
291 | |
292 bottom = 1.0 | |
293 | |
294 fig = plt.figure(figsize=(width, height)) | |
295 | |
27
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
296 if legend_states: |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
297 at_top = True |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
298 for state in sorted(legend_states): |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
299 if at_top: |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
300 bottom -= (top_space + ind_height)/height |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
301 at_top = False |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
302 else: |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
303 bottom -= (ind_space + ind_height)/height |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
304 ## add code here to draw legend |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
305 # [left, bottom, width, height] |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
306 ax1 = fig.add_axes([0.0, bottom, 0.09, ind_height/height]) |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
307 plt.axis('off') |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
308 ax1.set_xlim(0, 1) |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
309 ax1.set_ylim(0, 1) |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
310 for patch in make_state_rectangle(0, 1, state, 'legend', state2name[state]): |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
311 ax1.add_patch(patch) |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
312 |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
313 ax2 = fig.add_axes([0.10, bottom, 0.88, ind_height/height], frame_on=False) |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
314 plt.axis('off') |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
315 plt.text(0.0, 0.5, state2name[state], fontsize=10, ha='left', va='center') |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
316 else: |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
317 at_top = True |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
318 |
12 | 319 for_webb = False |
320 | |
321 for chrom in chroms: | |
322 length = chrom_len[chrom] | |
323 vals, labels = tick_foo(0, length) | |
324 | |
325 if at_top: | |
326 bottom -= (top_space + chrom_height)/height | |
327 at_top = False | |
328 else: | |
329 bottom -= (top_space + chrom_space + chrom_height)/height | |
330 | |
331 if not for_webb: | |
332 ax = fig.add_axes([0.0, bottom, 1.0, chrom_height/height]) | |
333 plt.axis('off') | |
334 plt.text(0.5, 0.5, chrom, fontsize=14, ha='center') | |
335 | |
336 individual_count = 0 | |
337 for individual in individuals: | |
338 if individual in data[chrom]: | |
339 individual_count += 1 | |
340 | |
341 i = 0 | |
342 for individual in individuals: | |
343 if individual in data[chrom]: | |
344 i += 1 | |
345 | |
346 bottom -= (ind_space + ind_height)/height | |
347 if not for_webb: | |
348 # [left, bottom, width, height] | |
349 ax1 = fig.add_axes([0.0, bottom, 0.09, ind_height/height]) | |
350 plt.axis('off') | |
351 plt.text(1.0, 0.5, individual, fontsize=10, ha='right', va='center') | |
352 # [left, bottom, width, height] | |
353 ax2 = fig.add_axes([0.10, bottom, 0.88, ind_height/height], frame_on=False) | |
354 ax2.set_xlim(0, length) | |
355 ax2.set_ylim(0, 1) | |
356 if i != individual_count: | |
357 plt.axis('off') | |
358 else: | |
359 if not for_webb: | |
360 ax2.tick_params(top=False, left=False, right=False, labelleft=False) | |
361 ax2.set_xticks(vals) | |
362 ax2.set_xticklabels(labels) | |
363 else: | |
364 plt.axis('off') | |
365 for p1, p2, state in sorted(data[chrom][individual]): | |
27
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
366 #for patch in make_state_rectangle(p1, p2, state, chrom, individual): |
12 | 367 for patch in make_state_rectangle(p1, p2, state, chrom, individual): |
368 ax2.add_patch(patch) | |
369 | |
370 plt.savefig(output_file) | |
371 | |
372 ################################################################################ | |
373 | |
374 if __name__ == '__main__': | |
375 input_dbkey, input_file, output_file, galaxy_data_index_dir = sys.argv[1:5] | |
376 make_dpmix_plot(input_dbkey, input_file, output_file, galaxy_data_index_dir) | |
377 sys.exit(0) | |
378 | |
27
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
379 ## notes |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
380 # 1) pass in a state to name mapping |
8997f2ca8c7a
Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents:
12
diff
changeset
|
381 # 2) only print out names for states which exist in the data, and are in the state to name mapping |