annotate dpmix_plot.py @ 0:879cbc69a0ee default tip

Uploaded
author rico
date Thu, 05 Apr 2012 15:28:27 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
879cbc69a0ee Uploaded
rico
parents:
diff changeset
1 #!/usr/bin/env python
879cbc69a0ee Uploaded
rico
parents:
diff changeset
2
879cbc69a0ee Uploaded
rico
parents:
diff changeset
3 import os
879cbc69a0ee Uploaded
rico
parents:
diff changeset
4 import sys
879cbc69a0ee Uploaded
rico
parents:
diff changeset
5 import math
879cbc69a0ee Uploaded
rico
parents:
diff changeset
6 import matplotlib as mpl
879cbc69a0ee Uploaded
rico
parents:
diff changeset
7 mpl.use('PDF')
879cbc69a0ee Uploaded
rico
parents:
diff changeset
8 import matplotlib.pyplot as plt
879cbc69a0ee Uploaded
rico
parents:
diff changeset
9 from matplotlib.path import Path
879cbc69a0ee Uploaded
rico
parents:
diff changeset
10 import matplotlib.patches as patches
879cbc69a0ee Uploaded
rico
parents:
diff changeset
11
879cbc69a0ee Uploaded
rico
parents:
diff changeset
12 ################################################################################
879cbc69a0ee Uploaded
rico
parents:
diff changeset
13
879cbc69a0ee Uploaded
rico
parents:
diff changeset
14 def build_chrom_len_dict(dbkey, galaxy_data_index_dir):
879cbc69a0ee Uploaded
rico
parents:
diff changeset
15 chrom_len_root = os.path.join(galaxy_data_index_dir, 'shared/ucsc/chrom')
879cbc69a0ee Uploaded
rico
parents:
diff changeset
16 chrom_len_file = '{0}.len'.format(dbkey)
879cbc69a0ee Uploaded
rico
parents:
diff changeset
17 chrom_len_path = os.path.join(chrom_len_root, chrom_len_file)
879cbc69a0ee Uploaded
rico
parents:
diff changeset
18
879cbc69a0ee Uploaded
rico
parents:
diff changeset
19 chrom_len = {}
879cbc69a0ee Uploaded
rico
parents:
diff changeset
20
879cbc69a0ee Uploaded
rico
parents:
diff changeset
21 try:
879cbc69a0ee Uploaded
rico
parents:
diff changeset
22 with open(chrom_len_path) as fh:
879cbc69a0ee Uploaded
rico
parents:
diff changeset
23 for line in fh:
879cbc69a0ee Uploaded
rico
parents:
diff changeset
24 line = line.rstrip('\r\n')
879cbc69a0ee Uploaded
rico
parents:
diff changeset
25 elems = line.split()
879cbc69a0ee Uploaded
rico
parents:
diff changeset
26 if len(elems) == 2:
879cbc69a0ee Uploaded
rico
parents:
diff changeset
27 chrom = elems[0]
879cbc69a0ee Uploaded
rico
parents:
diff changeset
28 length = int(elems[1])
879cbc69a0ee Uploaded
rico
parents:
diff changeset
29 chrom_len[chrom] = length
879cbc69a0ee Uploaded
rico
parents:
diff changeset
30 except:
879cbc69a0ee Uploaded
rico
parents:
diff changeset
31 pass
879cbc69a0ee Uploaded
rico
parents:
diff changeset
32
879cbc69a0ee Uploaded
rico
parents:
diff changeset
33 return chrom_len
879cbc69a0ee Uploaded
rico
parents:
diff changeset
34
879cbc69a0ee Uploaded
rico
parents:
diff changeset
35 def parse_input_file(input_file):
879cbc69a0ee Uploaded
rico
parents:
diff changeset
36 chroms = []
879cbc69a0ee Uploaded
rico
parents:
diff changeset
37 individuals = []
879cbc69a0ee Uploaded
rico
parents:
diff changeset
38 data = {}
879cbc69a0ee Uploaded
rico
parents:
diff changeset
39 chrom_len = {}
879cbc69a0ee Uploaded
rico
parents:
diff changeset
40
879cbc69a0ee Uploaded
rico
parents:
diff changeset
41 with open(input_file) as fh:
879cbc69a0ee Uploaded
rico
parents:
diff changeset
42 for line in fh:
879cbc69a0ee Uploaded
rico
parents:
diff changeset
43 line = line.strip()
879cbc69a0ee Uploaded
rico
parents:
diff changeset
44 if line:
879cbc69a0ee Uploaded
rico
parents:
diff changeset
45 elems = line.split()
879cbc69a0ee Uploaded
rico
parents:
diff changeset
46 chrom = elems[0]
879cbc69a0ee Uploaded
rico
parents:
diff changeset
47 p1, p2, state = map(int, elems[1:4])
879cbc69a0ee Uploaded
rico
parents:
diff changeset
48 id = elems[4]
879cbc69a0ee Uploaded
rico
parents:
diff changeset
49
879cbc69a0ee Uploaded
rico
parents:
diff changeset
50 if chrom not in chroms:
879cbc69a0ee Uploaded
rico
parents:
diff changeset
51 chroms.append(chrom)
879cbc69a0ee Uploaded
rico
parents:
diff changeset
52
879cbc69a0ee Uploaded
rico
parents:
diff changeset
53 if id not in individuals:
879cbc69a0ee Uploaded
rico
parents:
diff changeset
54 individuals.append(id)
879cbc69a0ee Uploaded
rico
parents:
diff changeset
55
879cbc69a0ee Uploaded
rico
parents:
diff changeset
56 data.setdefault(chrom, {})
879cbc69a0ee Uploaded
rico
parents:
diff changeset
57 data[chrom].setdefault(id, [])
879cbc69a0ee Uploaded
rico
parents:
diff changeset
58 data[chrom][id].append((p1, p2, state))
879cbc69a0ee Uploaded
rico
parents:
diff changeset
59
879cbc69a0ee Uploaded
rico
parents:
diff changeset
60 if p2 > chrom_len.setdefault(chrom, 0):
879cbc69a0ee Uploaded
rico
parents:
diff changeset
61 chrom_len[chrom] = p2
879cbc69a0ee Uploaded
rico
parents:
diff changeset
62
879cbc69a0ee Uploaded
rico
parents:
diff changeset
63 return chroms, individuals, data, chrom_len
879cbc69a0ee Uploaded
rico
parents:
diff changeset
64
879cbc69a0ee Uploaded
rico
parents:
diff changeset
65 def check_chroms(chroms, chrom_len, dbkey):
879cbc69a0ee Uploaded
rico
parents:
diff changeset
66 error = 0
879cbc69a0ee Uploaded
rico
parents:
diff changeset
67 for chrom in chroms:
879cbc69a0ee Uploaded
rico
parents:
diff changeset
68 if chrom not in chrom_len:
879cbc69a0ee Uploaded
rico
parents:
diff changeset
69 print >> sys.stderr, "Can't find length for {0} chromosome {1}".format(dbkey, chrom)
879cbc69a0ee Uploaded
rico
parents:
diff changeset
70 error = 1
879cbc69a0ee Uploaded
rico
parents:
diff changeset
71 if error:
879cbc69a0ee Uploaded
rico
parents:
diff changeset
72 sys.exit(1)
879cbc69a0ee Uploaded
rico
parents:
diff changeset
73
879cbc69a0ee Uploaded
rico
parents:
diff changeset
74 def check_data(data, chrom_len, dbkey):
879cbc69a0ee Uploaded
rico
parents:
diff changeset
75 error = 0
879cbc69a0ee Uploaded
rico
parents:
diff changeset
76 for chrom in data:
879cbc69a0ee Uploaded
rico
parents:
diff changeset
77 chrom_beg = 0
879cbc69a0ee Uploaded
rico
parents:
diff changeset
78 chrom_end = chrom_len[chrom]
879cbc69a0ee Uploaded
rico
parents:
diff changeset
79 for individual in data[chrom]:
879cbc69a0ee Uploaded
rico
parents:
diff changeset
80 for p1, p2, state in data[chrom][individual]:
879cbc69a0ee Uploaded
rico
parents:
diff changeset
81 if p1 >= p2:
879cbc69a0ee Uploaded
rico
parents:
diff changeset
82 print >> sys.stderr, "Bad data line: begin >= end: {0} {1} {2} {3}".format(chrom, p1, p2, state, individual)
879cbc69a0ee Uploaded
rico
parents:
diff changeset
83 error = 1
879cbc69a0ee Uploaded
rico
parents:
diff changeset
84 if p1 < chrom_beg or p2 > chrom_end:
879cbc69a0ee Uploaded
rico
parents:
diff changeset
85 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)
879cbc69a0ee Uploaded
rico
parents:
diff changeset
86 error = 1
879cbc69a0ee Uploaded
rico
parents:
diff changeset
87 if error:
879cbc69a0ee Uploaded
rico
parents:
diff changeset
88 sys.exit(1)
879cbc69a0ee Uploaded
rico
parents:
diff changeset
89
879cbc69a0ee Uploaded
rico
parents:
diff changeset
90 def make_rectangle(p1, p2, color, bottom=0.0, top=1.0):
879cbc69a0ee Uploaded
rico
parents:
diff changeset
91 verts = [
879cbc69a0ee Uploaded
rico
parents:
diff changeset
92 (p1, bottom), # left, bottom
879cbc69a0ee Uploaded
rico
parents:
diff changeset
93 (p1, top), # left, top
879cbc69a0ee Uploaded
rico
parents:
diff changeset
94 (p2, top), # right, top
879cbc69a0ee Uploaded
rico
parents:
diff changeset
95 (p2, bottom), # right, bottom
879cbc69a0ee Uploaded
rico
parents:
diff changeset
96 (0.0, 0.0) # ignored
879cbc69a0ee Uploaded
rico
parents:
diff changeset
97 ]
879cbc69a0ee Uploaded
rico
parents:
diff changeset
98
879cbc69a0ee Uploaded
rico
parents:
diff changeset
99 codes = [
879cbc69a0ee Uploaded
rico
parents:
diff changeset
100 Path.MOVETO,
879cbc69a0ee Uploaded
rico
parents:
diff changeset
101 Path.LINETO,
879cbc69a0ee Uploaded
rico
parents:
diff changeset
102 Path.LINETO,
879cbc69a0ee Uploaded
rico
parents:
diff changeset
103 Path.LINETO,
879cbc69a0ee Uploaded
rico
parents:
diff changeset
104 Path.CLOSEPOLY
879cbc69a0ee Uploaded
rico
parents:
diff changeset
105 ]
879cbc69a0ee Uploaded
rico
parents:
diff changeset
106
879cbc69a0ee Uploaded
rico
parents:
diff changeset
107 path = Path(verts, codes)
879cbc69a0ee Uploaded
rico
parents:
diff changeset
108 return patches.PathPatch(path, facecolor=color, lw=0)
879cbc69a0ee Uploaded
rico
parents:
diff changeset
109
879cbc69a0ee Uploaded
rico
parents:
diff changeset
110 def make_split_rectangle(p1, p2, top_color, bottom_color):
879cbc69a0ee Uploaded
rico
parents:
diff changeset
111 patch1 = make_rectangle(p1, p2, bottom_color, top=0.5)
879cbc69a0ee Uploaded
rico
parents:
diff changeset
112 patch2 = make_rectangle(p1, p2, top_color, bottom=0.5)
879cbc69a0ee Uploaded
rico
parents:
diff changeset
113 return [patch1, patch2]
879cbc69a0ee Uploaded
rico
parents:
diff changeset
114
879cbc69a0ee Uploaded
rico
parents:
diff changeset
115 def make_state_rectangle(p1, p2, state, chrom, individual):
879cbc69a0ee Uploaded
rico
parents:
diff changeset
116 if state == 0:
879cbc69a0ee Uploaded
rico
parents:
diff changeset
117 return [ make_rectangle(p1, p2, 'r') ]
879cbc69a0ee Uploaded
rico
parents:
diff changeset
118 elif state == 1:
879cbc69a0ee Uploaded
rico
parents:
diff changeset
119 return make_split_rectangle(p1, p2, 'r', 'g')
879cbc69a0ee Uploaded
rico
parents:
diff changeset
120 elif state == 2:
879cbc69a0ee Uploaded
rico
parents:
diff changeset
121 return [ make_rectangle(p1, p2, 'g') ]
879cbc69a0ee Uploaded
rico
parents:
diff changeset
122 else:
879cbc69a0ee Uploaded
rico
parents:
diff changeset
123 print >> sys.stderr, "Unknown state: {0}: {1} {2} {3} {4}".format(state, chrom, p1, p2, state, individual)
879cbc69a0ee Uploaded
rico
parents:
diff changeset
124 sys.exit(1)
879cbc69a0ee Uploaded
rico
parents:
diff changeset
125
879cbc69a0ee Uploaded
rico
parents:
diff changeset
126 def nicenum(num, round=False):
879cbc69a0ee Uploaded
rico
parents:
diff changeset
127 if num == 0:
879cbc69a0ee Uploaded
rico
parents:
diff changeset
128 return 0.0
879cbc69a0ee Uploaded
rico
parents:
diff changeset
129
879cbc69a0ee Uploaded
rico
parents:
diff changeset
130 exp = int(math.floor(math.log10(num)))
879cbc69a0ee Uploaded
rico
parents:
diff changeset
131 f = num / math.pow(10, exp)
879cbc69a0ee Uploaded
rico
parents:
diff changeset
132
879cbc69a0ee Uploaded
rico
parents:
diff changeset
133 if round:
879cbc69a0ee Uploaded
rico
parents:
diff changeset
134 if f < 1.5:
879cbc69a0ee Uploaded
rico
parents:
diff changeset
135 nf = 1.0
879cbc69a0ee Uploaded
rico
parents:
diff changeset
136 elif f < 3.0:
879cbc69a0ee Uploaded
rico
parents:
diff changeset
137 nf = 2.0
879cbc69a0ee Uploaded
rico
parents:
diff changeset
138 elif f < 7.0:
879cbc69a0ee Uploaded
rico
parents:
diff changeset
139 nf = 5.0
879cbc69a0ee Uploaded
rico
parents:
diff changeset
140 else:
879cbc69a0ee Uploaded
rico
parents:
diff changeset
141 nf = 10.0
879cbc69a0ee Uploaded
rico
parents:
diff changeset
142 else:
879cbc69a0ee Uploaded
rico
parents:
diff changeset
143 if f <= 1.0:
879cbc69a0ee Uploaded
rico
parents:
diff changeset
144 nf = 1.0
879cbc69a0ee Uploaded
rico
parents:
diff changeset
145 elif f <= 2.0:
879cbc69a0ee Uploaded
rico
parents:
diff changeset
146 nf = 2.0
879cbc69a0ee Uploaded
rico
parents:
diff changeset
147 elif f <= 5.0:
879cbc69a0ee Uploaded
rico
parents:
diff changeset
148 nf = 5.0
879cbc69a0ee Uploaded
rico
parents:
diff changeset
149 else:
879cbc69a0ee Uploaded
rico
parents:
diff changeset
150 nf = 10.0
879cbc69a0ee Uploaded
rico
parents:
diff changeset
151
879cbc69a0ee Uploaded
rico
parents:
diff changeset
152 return nf * pow(10, exp)
879cbc69a0ee Uploaded
rico
parents:
diff changeset
153
879cbc69a0ee Uploaded
rico
parents:
diff changeset
154 def tick_foo(beg, end, loose=False):
879cbc69a0ee Uploaded
rico
parents:
diff changeset
155 ntick = 10
879cbc69a0ee Uploaded
rico
parents:
diff changeset
156
879cbc69a0ee Uploaded
rico
parents:
diff changeset
157 range = nicenum(end - beg, round=False)
879cbc69a0ee Uploaded
rico
parents:
diff changeset
158 d = nicenum(range/(ntick - 1), round=True)
879cbc69a0ee Uploaded
rico
parents:
diff changeset
159 digits = int(math.floor(math.log10(d)))
879cbc69a0ee Uploaded
rico
parents:
diff changeset
160
879cbc69a0ee Uploaded
rico
parents:
diff changeset
161 if loose:
879cbc69a0ee Uploaded
rico
parents:
diff changeset
162 graph_min = math.floor(beg/d) * d
879cbc69a0ee Uploaded
rico
parents:
diff changeset
163 graph_max = math.ceil(end/d) * d
879cbc69a0ee Uploaded
rico
parents:
diff changeset
164 else:
879cbc69a0ee Uploaded
rico
parents:
diff changeset
165 graph_min = beg
879cbc69a0ee Uploaded
rico
parents:
diff changeset
166 graph_max = end
879cbc69a0ee Uploaded
rico
parents:
diff changeset
167
879cbc69a0ee Uploaded
rico
parents:
diff changeset
168 nfrac = max([-1 * digits, 0])
879cbc69a0ee Uploaded
rico
parents:
diff changeset
169 vals = []
879cbc69a0ee Uploaded
rico
parents:
diff changeset
170
879cbc69a0ee Uploaded
rico
parents:
diff changeset
171 stop = graph_max
879cbc69a0ee Uploaded
rico
parents:
diff changeset
172 if loose:
879cbc69a0ee Uploaded
rico
parents:
diff changeset
173 stop = graph_max + (0.5 * d)
879cbc69a0ee Uploaded
rico
parents:
diff changeset
174
879cbc69a0ee Uploaded
rico
parents:
diff changeset
175 x = graph_min
879cbc69a0ee Uploaded
rico
parents:
diff changeset
176 while x <= stop:
879cbc69a0ee Uploaded
rico
parents:
diff changeset
177 vals.append(int(x))
879cbc69a0ee Uploaded
rico
parents:
diff changeset
178 x += d
879cbc69a0ee Uploaded
rico
parents:
diff changeset
179
879cbc69a0ee Uploaded
rico
parents:
diff changeset
180 vals = vals[1:]
879cbc69a0ee Uploaded
rico
parents:
diff changeset
181
879cbc69a0ee Uploaded
rico
parents:
diff changeset
182 # if not loose:
879cbc69a0ee Uploaded
rico
parents:
diff changeset
183 # if vals[-1] < graph_max:
879cbc69a0ee Uploaded
rico
parents:
diff changeset
184 # vals.append(int(graph_max))
879cbc69a0ee Uploaded
rico
parents:
diff changeset
185
879cbc69a0ee Uploaded
rico
parents:
diff changeset
186 labels = []
879cbc69a0ee Uploaded
rico
parents:
diff changeset
187 for val in vals:
879cbc69a0ee Uploaded
rico
parents:
diff changeset
188 labels.append('{0}'.format(int(val/math.pow(10, digits))))
879cbc69a0ee Uploaded
rico
parents:
diff changeset
189
879cbc69a0ee Uploaded
rico
parents:
diff changeset
190 # labels.append('{0:.1f}'.format(vals[-1]/math.pow(10, digits)))
879cbc69a0ee Uploaded
rico
parents:
diff changeset
191
879cbc69a0ee Uploaded
rico
parents:
diff changeset
192 return vals, labels
879cbc69a0ee Uploaded
rico
parents:
diff changeset
193
879cbc69a0ee Uploaded
rico
parents:
diff changeset
194 ################################################################################
879cbc69a0ee Uploaded
rico
parents:
diff changeset
195
879cbc69a0ee Uploaded
rico
parents:
diff changeset
196 def make_dpmix_plot(input_dbkey, input_file, output_file, galaxy_data_index_dir):
879cbc69a0ee Uploaded
rico
parents:
diff changeset
197 fs_chrom_len = build_chrom_len_dict(input_dbkey, galaxy_data_index_dir)
879cbc69a0ee Uploaded
rico
parents:
diff changeset
198 chroms, individuals, data, chrom_len = parse_input_file(input_file)
879cbc69a0ee Uploaded
rico
parents:
diff changeset
199
879cbc69a0ee Uploaded
rico
parents:
diff changeset
200 for chrom in chrom_len.keys():
879cbc69a0ee Uploaded
rico
parents:
diff changeset
201 if chrom in fs_chrom_len:
879cbc69a0ee Uploaded
rico
parents:
diff changeset
202 chrom_len[chrom] = fs_chrom_len[chrom]
879cbc69a0ee Uploaded
rico
parents:
diff changeset
203
879cbc69a0ee Uploaded
rico
parents:
diff changeset
204 #check_chroms(chroms, chrom_len, input_dbkey)
879cbc69a0ee Uploaded
rico
parents:
diff changeset
205 check_data(data, chrom_len, input_dbkey)
879cbc69a0ee Uploaded
rico
parents:
diff changeset
206
879cbc69a0ee Uploaded
rico
parents:
diff changeset
207 ## units below are inches
879cbc69a0ee Uploaded
rico
parents:
diff changeset
208 top_space = 0.10
879cbc69a0ee Uploaded
rico
parents:
diff changeset
209 chrom_space = 0.25
879cbc69a0ee Uploaded
rico
parents:
diff changeset
210 chrom_height = 0.25
879cbc69a0ee Uploaded
rico
parents:
diff changeset
211 ind_space = 0.10
879cbc69a0ee Uploaded
rico
parents:
diff changeset
212 ind_height = 0.25
879cbc69a0ee Uploaded
rico
parents:
diff changeset
213
879cbc69a0ee Uploaded
rico
parents:
diff changeset
214 total_height = 0.0
879cbc69a0ee Uploaded
rico
parents:
diff changeset
215 at_top = True
879cbc69a0ee Uploaded
rico
parents:
diff changeset
216 for chrom in chroms:
879cbc69a0ee Uploaded
rico
parents:
diff changeset
217 if at_top:
879cbc69a0ee Uploaded
rico
parents:
diff changeset
218 total_height += (top_space + chrom_height)
879cbc69a0ee Uploaded
rico
parents:
diff changeset
219 at_top = False
879cbc69a0ee Uploaded
rico
parents:
diff changeset
220 else:
879cbc69a0ee Uploaded
rico
parents:
diff changeset
221 total_height += (top_space + chrom_space + chrom_height)
879cbc69a0ee Uploaded
rico
parents:
diff changeset
222
879cbc69a0ee Uploaded
rico
parents:
diff changeset
223 individual_count = 0
879cbc69a0ee Uploaded
rico
parents:
diff changeset
224 for individual in individuals:
879cbc69a0ee Uploaded
rico
parents:
diff changeset
225 if individual in data[chrom]:
879cbc69a0ee Uploaded
rico
parents:
diff changeset
226 individual_count += 1
879cbc69a0ee Uploaded
rico
parents:
diff changeset
227 total_height += individual_count * (ind_space + ind_height)
879cbc69a0ee Uploaded
rico
parents:
diff changeset
228
879cbc69a0ee Uploaded
rico
parents:
diff changeset
229 width = 7.5
879cbc69a0ee Uploaded
rico
parents:
diff changeset
230 height = math.ceil(total_height)
879cbc69a0ee Uploaded
rico
parents:
diff changeset
231
879cbc69a0ee Uploaded
rico
parents:
diff changeset
232 bottom = 1.0
879cbc69a0ee Uploaded
rico
parents:
diff changeset
233
879cbc69a0ee Uploaded
rico
parents:
diff changeset
234 fig = plt.figure(figsize=(width, height))
879cbc69a0ee Uploaded
rico
parents:
diff changeset
235
879cbc69a0ee Uploaded
rico
parents:
diff changeset
236 at_top = True
879cbc69a0ee Uploaded
rico
parents:
diff changeset
237 for_webb = False
879cbc69a0ee Uploaded
rico
parents:
diff changeset
238
879cbc69a0ee Uploaded
rico
parents:
diff changeset
239 for chrom in chroms:
879cbc69a0ee Uploaded
rico
parents:
diff changeset
240 length = chrom_len[chrom]
879cbc69a0ee Uploaded
rico
parents:
diff changeset
241 vals, labels = tick_foo(0, length)
879cbc69a0ee Uploaded
rico
parents:
diff changeset
242
879cbc69a0ee Uploaded
rico
parents:
diff changeset
243 if at_top:
879cbc69a0ee Uploaded
rico
parents:
diff changeset
244 bottom -= (top_space + chrom_height)/height
879cbc69a0ee Uploaded
rico
parents:
diff changeset
245 at_top = False
879cbc69a0ee Uploaded
rico
parents:
diff changeset
246 else:
879cbc69a0ee Uploaded
rico
parents:
diff changeset
247 bottom -= (top_space + chrom_space + chrom_height)/height
879cbc69a0ee Uploaded
rico
parents:
diff changeset
248
879cbc69a0ee Uploaded
rico
parents:
diff changeset
249 if not for_webb:
879cbc69a0ee Uploaded
rico
parents:
diff changeset
250 ax = fig.add_axes([0.0, bottom, 1.0, chrom_height/height])
879cbc69a0ee Uploaded
rico
parents:
diff changeset
251 plt.axis('off')
879cbc69a0ee Uploaded
rico
parents:
diff changeset
252 plt.text(0.5, 0.5, chrom, fontsize=14, ha='center')
879cbc69a0ee Uploaded
rico
parents:
diff changeset
253
879cbc69a0ee Uploaded
rico
parents:
diff changeset
254 individual_count = 0
879cbc69a0ee Uploaded
rico
parents:
diff changeset
255 for individual in individuals:
879cbc69a0ee Uploaded
rico
parents:
diff changeset
256 if individual in data[chrom]:
879cbc69a0ee Uploaded
rico
parents:
diff changeset
257 individual_count += 1
879cbc69a0ee Uploaded
rico
parents:
diff changeset
258
879cbc69a0ee Uploaded
rico
parents:
diff changeset
259 i = 0
879cbc69a0ee Uploaded
rico
parents:
diff changeset
260 for individual in individuals:
879cbc69a0ee Uploaded
rico
parents:
diff changeset
261 if individual in data[chrom]:
879cbc69a0ee Uploaded
rico
parents:
diff changeset
262 i += 1
879cbc69a0ee Uploaded
rico
parents:
diff changeset
263
879cbc69a0ee Uploaded
rico
parents:
diff changeset
264 bottom -= (ind_space + ind_height)/height
879cbc69a0ee Uploaded
rico
parents:
diff changeset
265 if not for_webb:
879cbc69a0ee Uploaded
rico
parents:
diff changeset
266 # [left, bottom, width, height]
879cbc69a0ee Uploaded
rico
parents:
diff changeset
267 ax1 = fig.add_axes([0.0, bottom, 0.09, ind_height/height])
879cbc69a0ee Uploaded
rico
parents:
diff changeset
268 plt.axis('off')
879cbc69a0ee Uploaded
rico
parents:
diff changeset
269 plt.text(1.0, 0.5, individual, fontsize=10, ha='right', va='center')
879cbc69a0ee Uploaded
rico
parents:
diff changeset
270 # [left, bottom, width, height]
879cbc69a0ee Uploaded
rico
parents:
diff changeset
271 ax2 = fig.add_axes([0.10, bottom, 0.88, ind_height/height], frame_on=False)
879cbc69a0ee Uploaded
rico
parents:
diff changeset
272 ax2.set_xlim(0, length)
879cbc69a0ee Uploaded
rico
parents:
diff changeset
273 ax2.set_ylim(0, 1)
879cbc69a0ee Uploaded
rico
parents:
diff changeset
274 if i != individual_count:
879cbc69a0ee Uploaded
rico
parents:
diff changeset
275 plt.axis('off')
879cbc69a0ee Uploaded
rico
parents:
diff changeset
276 else:
879cbc69a0ee Uploaded
rico
parents:
diff changeset
277 if not for_webb:
879cbc69a0ee Uploaded
rico
parents:
diff changeset
278 ax2.tick_params(top=False, left=False, right=False, labelleft=False)
879cbc69a0ee Uploaded
rico
parents:
diff changeset
279 ax2.set_xticks(vals)
879cbc69a0ee Uploaded
rico
parents:
diff changeset
280 ax2.set_xticklabels(labels)
879cbc69a0ee Uploaded
rico
parents:
diff changeset
281 else:
879cbc69a0ee Uploaded
rico
parents:
diff changeset
282 plt.axis('off')
879cbc69a0ee Uploaded
rico
parents:
diff changeset
283 for p1, p2, state in sorted(data[chrom][individual]):
879cbc69a0ee Uploaded
rico
parents:
diff changeset
284 for patch in make_state_rectangle(p1, p2, state, chrom, individual):
879cbc69a0ee Uploaded
rico
parents:
diff changeset
285 ax2.add_patch(patch)
879cbc69a0ee Uploaded
rico
parents:
diff changeset
286
879cbc69a0ee Uploaded
rico
parents:
diff changeset
287 plt.savefig(output_file)
879cbc69a0ee Uploaded
rico
parents:
diff changeset
288
879cbc69a0ee Uploaded
rico
parents:
diff changeset
289 ################################################################################
879cbc69a0ee Uploaded
rico
parents:
diff changeset
290
879cbc69a0ee Uploaded
rico
parents:
diff changeset
291 if __name__ == '__main__':
879cbc69a0ee Uploaded
rico
parents:
diff changeset
292 input_dbkey, input_file, output_file, galaxy_data_index_dir = sys.argv[1:5]
879cbc69a0ee Uploaded
rico
parents:
diff changeset
293 make_dpmix_plot(input_dbkey, input_file, output_file, galaxy_data_index_dir)
879cbc69a0ee Uploaded
rico
parents:
diff changeset
294 sys.exit(0)
879cbc69a0ee Uploaded
rico
parents:
diff changeset
295