annotate dpmix_plot.py @ 39:e56023008e36 default tip

Changed revision of package_fisher_0_1_4 to be2fc454d121 Changed revision of package_matplotlib_1_2 to a03ee94316b5
author miller-lab
date Mon, 06 Jul 2015 10:32:24 -0400
parents a631c2f6d913
children
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
31
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
6
12
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
7 import matplotlib as mpl
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
8 mpl.use('PDF')
31
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
9 from matplotlib.backends.backend_pdf import PdfPages
12
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
10 import matplotlib.pyplot as plt
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
11 from matplotlib.path import Path
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
12 import matplotlib.patches as patches
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
13
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
14 ################################################################################
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
15
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
16 def build_chrom_len_dict(dbkey, galaxy_data_index_dir):
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
17 chrom_len_root = os.path.join(galaxy_data_index_dir, 'shared/ucsc/chrom')
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
18 chrom_len_file = '{0}.len'.format(dbkey)
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
19 chrom_len_path = os.path.join(chrom_len_root, chrom_len_file)
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
20
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
21 chrom_len = {}
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
22
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
23 try:
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
24 with open(chrom_len_path) as fh:
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
25 for line in fh:
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
26 line = line.rstrip('\r\n')
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
27 elems = line.split()
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
28 if len(elems) == 2:
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
29 chrom = elems[0]
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
30 length = int(elems[1])
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
31 chrom_len[chrom] = length
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
32 except:
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
33 pass
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
34
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
35 return chrom_len
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
36
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
37 def parse_input_file(input_file):
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
38 chroms = []
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
39 individuals = []
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
40 data = {}
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
41 chrom_len = {}
27
8997f2ca8c7a Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents: 12
diff changeset
42 used_states = []
12
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
43
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
44 with open(input_file) as fh:
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
45 for line in fh:
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
46 line = line.strip()
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
47 if line:
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
48 elems = line.split()
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
49 chrom = elems[0]
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
50 p1, p2, state = map(int, elems[1:4])
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
51 id = elems[4]
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
52
27
8997f2ca8c7a Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents: 12
diff changeset
53 if state not in used_states:
8997f2ca8c7a Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents: 12
diff changeset
54 used_states.append(state)
8997f2ca8c7a Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents: 12
diff changeset
55
12
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
56 if chrom not in chroms:
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
57 chroms.append(chrom)
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
58
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
59 if id not in individuals:
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
60 individuals.append(id)
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
61
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
62 data.setdefault(chrom, {})
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
63 data[chrom].setdefault(id, [])
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
64 data[chrom][id].append((p1, p2, state))
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
65
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
66 if p2 > chrom_len.setdefault(chrom, 0):
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
67 chrom_len[chrom] = p2
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
68
27
8997f2ca8c7a Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents: 12
diff changeset
69 return chroms, individuals, data, chrom_len, used_states
12
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
70
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
71 def check_chroms(chroms, chrom_len, dbkey):
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
72 error = 0
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
73 for chrom in chroms:
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
74 if chrom not in chrom_len:
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
75 print >> sys.stderr, "Can't find length for {0} chromosome {1}".format(dbkey, chrom)
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
76 error = 1
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
77 if error:
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
78 sys.exit(1)
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
79
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
80 def check_data(data, chrom_len, dbkey):
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
81 error = 0
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
82 for chrom in data:
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
83 chrom_beg = 0
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
84 chrom_end = chrom_len[chrom]
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
85 for individual in data[chrom]:
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
86 for p1, p2, state in data[chrom][individual]:
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
87 if p1 >= p2:
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
88 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
89 error = 1
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
90 if p1 < chrom_beg or p2 > chrom_end:
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
91 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
92 error = 1
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
93 if error:
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
94 sys.exit(1)
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
95
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
96 def make_rectangle(p1, p2, color, bottom=0.0, top=1.0):
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
97 verts = [
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
98 (p1, bottom), # left, bottom
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
99 (p1, top), # left, top
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
100 (p2, top), # right, top
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
101 (p2, bottom), # right, bottom
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
102 (0.0, 0.0) # ignored
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
103 ]
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
104
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
105 codes = [
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
106 Path.MOVETO,
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
107 Path.LINETO,
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
108 Path.LINETO,
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
109 Path.LINETO,
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
110 Path.CLOSEPOLY
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
111 ]
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
112
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
113 path = Path(verts, codes)
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
114 return patches.PathPatch(path, facecolor=color, lw=0)
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
115
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
116 def make_split_rectangle(p1, p2, top_color, bottom_color):
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
117 patch1 = make_rectangle(p1, p2, bottom_color, top=0.5)
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
118 patch2 = make_rectangle(p1, p2, top_color, bottom=0.5)
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
119 return [patch1, patch2]
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
120
27
8997f2ca8c7a Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents: 12
diff changeset
121 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
122 p1_color = 'r'
8997f2ca8c7a Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents: 12
diff changeset
123 p2_color = 'g'
8997f2ca8c7a Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents: 12
diff changeset
124 heterochromatin_color = '#c7c7c7'
8997f2ca8c7a Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents: 12
diff changeset
125
12
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
126 if state == 0:
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, heterochromatin_color) ]
12
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
128 elif state == 1:
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, p1_color) ]
12
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
130 elif state == 2:
27
8997f2ca8c7a Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents: 12
diff changeset
131 return [ make_rectangle(p1, p2, p2_color) ]
12
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
132 elif state == 3:
27
8997f2ca8c7a Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents: 12
diff changeset
133 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
134 else:
8997f2ca8c7a Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents: 12
diff changeset
135 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
136 sys.exit(1)
8997f2ca8c7a Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents: 12
diff changeset
137
8997f2ca8c7a Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents: 12
diff changeset
138 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
139 p1_color = 'r'
8997f2ca8c7a Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents: 12
diff changeset
140 p2_color = 'g'
8997f2ca8c7a Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents: 12
diff changeset
141 p3_color = 'b'
8997f2ca8c7a Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents: 12
diff changeset
142 heterochromatin_color = '#c7c7c7'
8997f2ca8c7a Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents: 12
diff changeset
143
8997f2ca8c7a Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents: 12
diff changeset
144 if state == 0:
8997f2ca8c7a Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents: 12
diff changeset
145 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
146 if state == 1:
8997f2ca8c7a Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents: 12
diff changeset
147 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
148 if state == 2:
8997f2ca8c7a Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents: 12
diff changeset
149 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
150 if state == 3:
8997f2ca8c7a Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents: 12
diff changeset
151 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
152 if state == 4:
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, p2_color)
8997f2ca8c7a Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents: 12
diff changeset
154 if state == 5:
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, p1_color, p3_color)
8997f2ca8c7a Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents: 12
diff changeset
156 if state == 6:
8997f2ca8c7a Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents: 12
diff changeset
157 return make_split_rectangle(p1, p2, p2_color, p3_color)
12
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
158 else:
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
159 print >> sys.stderr, "Unknown state: {0}: {1} {2} {3} {4}".format(state, chrom, p1, p2, state, individual)
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
160 sys.exit(1)
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
161
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
162 def nicenum(num, round=False):
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
163 if num == 0:
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
164 return 0.0
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
165
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
166 exp = int(math.floor(math.log10(num)))
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
167 f = num / math.pow(10, exp)
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
168
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
169 if round:
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
170 if f < 1.5:
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
171 nf = 1.0
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
172 elif f < 3.0:
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
173 nf = 2.0
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
174 elif f < 7.0:
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
175 nf = 5.0
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
176 else:
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
177 nf = 10.0
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
178 else:
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
179 if f <= 1.0:
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
180 nf = 1.0
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
181 elif f <= 2.0:
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
182 nf = 2.0
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
183 elif f <= 5.0:
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
184 nf = 5.0
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
185 else:
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
186 nf = 10.0
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
187
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
188 return nf * pow(10, exp)
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
189
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
190 def tick_foo(beg, end, loose=False):
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
191 ntick = 10
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
192
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
193 range = nicenum(end - beg, round=False)
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
194 d = nicenum(range/(ntick - 1), round=True)
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
195 digits = int(math.floor(math.log10(d)))
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
196
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
197 if loose:
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
198 graph_min = math.floor(beg/d) * d
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
199 graph_max = math.ceil(end/d) * d
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
200 else:
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
201 graph_min = beg
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
202 graph_max = end
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
203
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
204 nfrac = max([-1 * digits, 0])
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
205 vals = []
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
206
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
207 stop = graph_max
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
208 if loose:
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
209 stop = graph_max + (0.5 * d)
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
210
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
211 x = graph_min
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
212 while x <= stop:
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
213 vals.append(int(x))
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
214 x += d
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
215
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
216 vals = vals[1:]
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
217
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
218 # if not loose:
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
219 # if vals[-1] < graph_max:
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
220 # vals.append(int(graph_max))
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
221
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
222 labels = []
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
223 for val in vals:
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
224 labels.append('{0}'.format(int(val/math.pow(10, digits))))
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
225
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
226 # labels.append('{0:.1f}'.format(vals[-1]/math.pow(10, digits)))
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
227
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
228 return vals, labels
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
229
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
230 ################################################################################
31
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
231 ################################################################################
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
232 ################################################################################
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
233 ################################################################################
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
234
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
235 def space_for_legend(plot_params):
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
236 space = 0.0
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
237
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
238 legend_states = plot_params['legend_states']
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
239 if legend_states:
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
240 ind_space = plot_params['ind_space']
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
241 ind_height = plot_params['ind_height']
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
242 space += len(legend_states) * (ind_space + ind_height) - ind_space
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
243
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
244 return space
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
245
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
246 ################################################################################
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
247
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
248 def space_for_chroms(plot_params, chroms, individuals, data):
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
249 space_dict = {}
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
250
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
251 chrom_height = plot_params['chrom_height']
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
252 ind_space = plot_params['ind_space']
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
253 ind_height = plot_params['ind_height']
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
254
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
255 for chrom in chroms:
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
256 space_dict[chrom] = chrom_height
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
257
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
258 individual_count = 0
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
259 for individual in individuals:
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
260 if individual in data[chrom]:
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
261 individual_count += 1
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
262
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
263 space_dict[chrom] += individual_count * (ind_space + ind_height)
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
264
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
265 return space_dict
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
266
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
267 ################################################################################
12
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
268
27
8997f2ca8c7a Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents: 12
diff changeset
269 def make_dpmix_plot(input_dbkey, input_file, output_file, galaxy_data_index_dir, state2name=None, populations=3):
12
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
270 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
271 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
272
31
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
273 ## populate chrom_len
12
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
274 for chrom in chrom_len.keys():
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
275 if chrom in fs_chrom_len:
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
276 chrom_len[chrom] = fs_chrom_len[chrom]
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
277
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
278 #check_chroms(chroms, chrom_len, input_dbkey)
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
279 check_data(data, chrom_len, input_dbkey)
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
280
31
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
281 ## plot parameters
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
282 plot_params = {
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
283 'plot_dpi': 300,
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
284 'page_width': 8.50,
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
285 'page_height': 11.00,
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
286 'top_margin': 0.10,
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
287 'bottom_margin': 0.10,
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
288 'chrom_space': 0.25,
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
289 'chrom_height': 0.25,
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
290 'ind_space': 0.10,
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
291 'ind_height': 0.25,
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
292 'legend_space': 0.10
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
293 }
12
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
294
31
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
295 ## in the legend, only print out states that are
27
8997f2ca8c7a Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents: 12
diff changeset
296 ## 1) in the data
8997f2ca8c7a Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents: 12
diff changeset
297 ## - AND -
8997f2ca8c7a Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents: 12
diff changeset
298 ## 2) in the state2name map
8997f2ca8c7a Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents: 12
diff changeset
299 legend_states = []
8997f2ca8c7a Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents: 12
diff changeset
300 if state2name is not None:
8997f2ca8c7a Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents: 12
diff changeset
301 for state in used_states:
8997f2ca8c7a Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents: 12
diff changeset
302 if state in state2name:
8997f2ca8c7a Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents: 12
diff changeset
303 legend_states.append(state)
8997f2ca8c7a Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents: 12
diff changeset
304
31
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
305 plot_params['legend_states'] = legend_states
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
306
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
307 ## choose the correct make_state_rectangle method
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
308 if populations == 3:
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
309 plot_params['rectangle_method'] = make_state_rectangle_3pop
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
310 elif populations == 2:
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
311 plot_params['rectangle_method'] = make_state_rectangle_2pop
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
312
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
313 pdf_pages = PdfPages(output_file)
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
314
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
315 ## generate a list of chroms for each page
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
316
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
317 needed_for_legend = space_for_legend(plot_params)
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
318 needed_for_chroms = space_for_chroms(plot_params, chroms, individuals, data)
27
8997f2ca8c7a Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents: 12
diff changeset
319
31
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
320 chrom_space_per_page = plot_params['page_height']
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
321 chrom_space_per_page -= plot_params['top_margin'] + plot_params['bottom_margin']
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
322 chrom_space_per_page -= needed_for_legend + plot_params['legend_space']
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
323 chrom_space_per_page -= plot_params['chrom_space']
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
324
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
325 chroms_left = chroms[:]
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
326 pages = []
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
327
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
328 space_left = chrom_space_per_page
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
329 chrom_list = []
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
330
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
331 while chroms_left:
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
332 chrom = chroms_left.pop(0)
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
333 space_needed = needed_for_chroms[chrom] + plot_params['chrom_space']
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
334 if (space_needed > chrom_space_per_page):
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
335 print >> sys.stderr, 'Multipage chroms not yet supported'
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
336 sys.exit(1)
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
337
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
338 ## sometimes 1.9 - 1.9 < 0 (-4.4408920985e-16)
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
339 ## so, we make sure it's not more than a millimeter over
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
340 if space_left - space_needed > -0.04:
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
341 chrom_list.append(chrom)
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
342 space_left -= space_needed
12
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
343 else:
31
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
344 pages.append(chrom_list[:])
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
345 chrom_list = []
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
346 chroms_left.insert(0, chrom)
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
347 space_left = chrom_space_per_page
12
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
348
31
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
349 ############################################################################
12
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
350
31
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
351 plot_dpi = plot_params['plot_dpi']
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
352 page_width = plot_params['page_width']
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
353 page_height = plot_params['page_height']
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
354 top_margin = plot_params['top_margin']
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
355 ind_space = plot_params['ind_space']
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
356 ind_height = plot_params['ind_height']
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
357 make_state_rectangle = plot_params['rectangle_method']
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
358 legend_space = plot_params['legend_space']
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
359 chrom_space = plot_params['chrom_space']
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
360 chrom_height = plot_params['chrom_height']
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
361
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
362 for page in pages:
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
363 fig = plt.figure(figsize=(page_width, page_height), dpi=plot_dpi)
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
364 bottom = 1.0 - (top_margin/page_height)
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
365
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
366 # print legend
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
367 if legend_states:
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
368 top = True
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
369 for state in sorted(legend_states):
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
370 if top:
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
371 bottom -= ind_height/page_height
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
372 top = False
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
373 else:
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
374 bottom -= (ind_space + ind_height)/page_height
27
8997f2ca8c7a Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents: 12
diff changeset
375
31
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
376 ax1 = fig.add_axes([0.0, bottom, 0.09, ind_height/page_height])
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
377 plt.axis('off')
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
378 ax1.set_xlim(0, 1)
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
379 ax1.set_ylim(0, 1)
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
380 for patch in make_state_rectangle(0, 1, state, 'legend', state2name[state]):
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
381 ax1.add_patch(patch)
27
8997f2ca8c7a Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents: 12
diff changeset
382
31
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
383 ax2 = fig.add_axes([0.10, bottom, 0.88, ind_height/page_height], frame_on=False)
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
384 plt.axis('off')
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
385 plt.text(0.0, 0.5, state2name[state], fontsize=10, ha='left', va='center')
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
386
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
387 bottom -= legend_space/page_height
12
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
388
31
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
389 # print chroms
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
390 top = True
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
391 for chrom in page:
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
392 length = chrom_len[chrom]
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
393 vals, labels = tick_foo(0, length)
12
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
394
31
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
395 if top:
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
396 bottom -= chrom_height/page_height
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
397 top = False
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
398 else:
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
399 bottom -= (chrom_space + chrom_height)/page_height
12
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
400
31
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
401 ax = fig.add_axes([0.0, bottom, 1.0, chrom_height/page_height])
12
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
402 plt.axis('off')
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
403 plt.text(0.5, 0.5, chrom, fontsize=14, ha='center')
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
404
31
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
405 individual_count = 0
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
406 for individual in individuals:
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
407 if individual in data[chrom]:
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
408 individual_count += 1
12
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
409
31
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
410 i = 0
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
411 for individual in individuals:
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
412 if individual in data[chrom]:
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
413 i += 1
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
414 bottom -= (ind_space + ind_height)/page_height
12
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
415
31
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
416 ax1 = fig.add_axes([0.0, bottom, 0.09, ind_height/page_height])
12
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
417 plt.axis('off')
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
418 plt.text(1.0, 0.5, individual, fontsize=10, ha='right', va='center')
31
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
419
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
420 ax2 = fig.add_axes([0.10, bottom, 0.88, ind_height/page_height], frame_on=False)
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
421 ax2.set_xlim(0, length)
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
422 ax2.set_ylim(0, 1)
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
423
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
424 if i != individual_count:
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
425 plt.axis('off')
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
426 else:
12
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
427 ax2.tick_params(top=False, left=False, right=False, labelleft=False)
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
428 ax2.set_xticks(vals)
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
429 ax2.set_xticklabels(labels)
31
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
430
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
431 for p1, p2, state in sorted(data[chrom][individual]):
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
432 for patch in make_state_rectangle(p1, p2, state, chrom, individual):
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
433 ax2.add_patch(patch)
12
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
434
31
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
435 # extend last state to end of chrom
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
436 if p2 < length:
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
437 for patch in make_state_rectangle(p2, length, state, chrom, individual):
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
438 ax2.add_patch(patch)
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
439
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
440
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
441 pdf_pages.savefig(fig)
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
442 plt.close(fig)
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
443
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
444 pdf_pages.close()
12
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
445
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
446 ################################################################################
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
447
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
448 if __name__ == '__main__':
31
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
449 make_dpmix_plot('loxAfr3', 'output.dat', 'output2_files/picture.pdf', '/scratch/galaxy/home/oocyte/galaxy_oocyte/tool-data', state2name={0: 'heterochromatin', 1: 'reference', 2: 'asian'}, populations=2)
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
450 # input_dbkey, input_file, output_file, galaxy_data_index_dir = sys.argv[1:5]
a631c2f6d913 Update to Miller Lab devshed revision 3c4110ffacc3
Richard Burhans <burhans@bx.psu.edu>
parents: 27
diff changeset
451 # make_dpmix_plot(input_dbkey, input_file, output_file, galaxy_data_index_dir)
12
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
452 sys.exit(0)
4b6590dd7250 Uploaded
miller-lab
parents:
diff changeset
453
27
8997f2ca8c7a Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents: 12
diff changeset
454 ## notes
8997f2ca8c7a Update to Miller Lab devshed revision bae0d3306d3b
Richard Burhans <burhans@bx.psu.edu>
parents: 12
diff changeset
455 # 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
456 # 2) only print out names for states which exist in the data, and are in the state to name mapping