annotate dpmix_plot.py @ 0:2c498d40ecde

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