annotate dpmix_plot.py @ 24:248b06e86022

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