annotate tools/regVariation/microsats_mutability.py @ 2:c2a356708570

Uploaded
author xuebing
date Fri, 09 Mar 2012 19:45:42 -0500
parents 9071e359b9a3
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
1 #!/usr/bin/env python
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
2 #Guruprasad Ananda
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
3 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
4 This tool computes microsatellite mutability for the orthologous microsatellites fetched from 'Extract Orthologous Microsatellites from pair-wise alignments' tool.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
5 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
6 from galaxy import eggs
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
7 import sys, string, re, commands, tempfile, os, fileinput
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
8 from galaxy.tools.util.galaxyops import *
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
9 from bx.intervals.io import *
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
10 from bx.intervals.operations import quicksect
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
11
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
12 fout = open(sys.argv[2],'w')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
13 p_group = int(sys.argv[3]) #primary "group-by" feature
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
14 p_bin_size = int(sys.argv[4])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
15 s_group = int(sys.argv[5]) #sub-group by feature
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
16 s_bin_size = int(sys.argv[6])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
17 mono_threshold = 9
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
18 non_mono_threshold = 4
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
19 p_group_cols = [p_group, p_group+7]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
20 s_group_cols = [s_group, s_group+7]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
21 num_generations = int(sys.argv[7])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
22 region = sys.argv[8]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
23 int_file = sys.argv[9]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
24 if int_file != "None": #User has specified an interval file
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
25 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
26 fint = open(int_file, 'r')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
27 dbkey_i = sys.argv[10]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
28 chr_col_i, start_col_i, end_col_i, strand_col_i = parse_cols_arg( sys.argv[11] )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
29 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
30 stop_err("Unable to open input Interval file")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
31
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
32 def stop_err(msg):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
33 sys.stderr.write(msg)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
34 sys.exit()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
35
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
36 def reverse_complement(text):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
37 DNA_COMP = string.maketrans( "ACGTacgt", "TGCAtgca" )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
38 comp = [ch for ch in text.translate(DNA_COMP)]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
39 comp.reverse()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
40 return "".join(comp)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
41
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
42 def get_unique_elems(elems):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
43 seen=set()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
44 return[x for x in elems if x not in seen and not seen.add(x)]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
45
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
46 def get_binned_lists(uniqlist, binsize):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
47 binnedlist=[]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
48 uniqlist.sort()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
49 start = int(uniqlist[0])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
50 bin_ind=0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
51 l_ind=0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
52 binnedlist.append([])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
53 while l_ind < len(uniqlist):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
54 elem = int(uniqlist[l_ind])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
55 if elem in range(start,start+binsize):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
56 binnedlist[bin_ind].append(elem)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
57 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
58 start += binsize
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
59 bin_ind += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
60 binnedlist.append([])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
61 binnedlist[bin_ind].append(elem)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
62 l_ind += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
63 return binnedlist
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
64
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
65 def fetch_weight(H,C,t):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
66 if (H-(C-H)) < t:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
67 return 2.0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
68 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
69 return 1.0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
70
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
71 def mutabilityEstimator(repeats1,repeats2,thresholds):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
72 mut_num = 0.0 #Mutability Numerator
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
73 mut_den = 0.0 #Mutability denominator
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
74 for ind,H in enumerate(repeats1):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
75 C = repeats2[ind]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
76 t = thresholds[ind]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
77 w = fetch_weight(H,C,t)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
78 mut_num += ((H-C)*(H-C)*w)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
79 mut_den += w
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
80 return [mut_num, mut_den]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
81
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
82 def output_writer(blk, blk_lines):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
83 global winspecies, speciesind
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
84 all_elems_1=[]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
85 all_elems_2=[]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
86 all_s_elems_1=[]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
87 all_s_elems_2=[]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
88 for bline in blk_lines:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
89 if not(bline):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
90 continue
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
91 items = bline.split('\t')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
92 seq1 = items[1]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
93 start1 = items[2]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
94 end1 = items[3]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
95 seq2 = items[8]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
96 start2 = items[9]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
97 end2 = items[10]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
98 if p_group_cols[0] == 6:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
99 items[p_group_cols[0]] = int(items[p_group_cols[0]])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
100 items[p_group_cols[1]] = int(items[p_group_cols[1]])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
101 if s_group_cols[0] == 6:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
102 items[s_group_cols[0]] = int(items[s_group_cols[0]])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
103 items[s_group_cols[1]] = int(items[s_group_cols[1]])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
104 all_elems_1.append(items[p_group_cols[0]]) #primary col elements for species 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
105 all_elems_2.append(items[p_group_cols[1]]) #primary col elements for species 2
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
106 if s_group_cols[0] != -1: #sub-group is not None
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
107 all_s_elems_1.append(items[s_group_cols[0]]) #secondary col elements for species 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
108 all_s_elems_2.append(items[s_group_cols[1]]) #secondary col elements for species 2
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
109 uniq_elems_1 = get_unique_elems(all_elems_1)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
110 uniq_elems_2 = get_unique_elems(all_elems_2)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
111 if s_group_cols[0] != -1:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
112 uniq_s_elems_1 = get_unique_elems(all_s_elems_1)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
113 uniq_s_elems_2 = get_unique_elems(all_s_elems_2)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
114 mut1={}
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
115 mut2={}
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
116 count1 = {}
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
117 count2 = {}
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
118 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
119 if p_group_cols[0] == 7: #i.e. the option chosen is group-by unit(AG, GTC, etc)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
120 uniq_elems_1 = get_unique_units(j.sort(lambda x, y: len(x)-len(y)))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
121 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
122 if p_group_cols[0] == 6: #i.e. the option chosen is group-by repeat number.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
123 uniq_elems_1 = get_binned_lists(uniq_elems_1,p_bin_size)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
124 uniq_elems_2 = get_binned_lists(uniq_elems_2,p_bin_size)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
125
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
126 if s_group_cols[0] == 6: #i.e. the option chosen is subgroup-by repeat number.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
127 uniq_s_elems_1 = get_binned_lists(uniq_s_elems_1,s_bin_size)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
128 uniq_s_elems_2 = get_binned_lists(uniq_s_elems_2,s_bin_size)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
129
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
130 for pitem1 in uniq_elems_1:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
131 #repeats1 = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
132 #repeats2 = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
133 thresholds = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
134 if s_group_cols[0] != -1: #Sub-group by feature is not None
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
135 for sitem1 in uniq_s_elems_1:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
136 repeats1 = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
137 repeats2 = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
138 if type(sitem1) == type(''):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
139 sitem1 = sitem1.strip()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
140 for bline in blk_lines:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
141 belems = bline.split('\t')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
142 if type(pitem1) == list:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
143 if p_group_cols[0] == 6:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
144 belems[p_group_cols[0]] = int(belems[p_group_cols[0]])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
145 if belems[p_group_cols[0]] in pitem1:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
146 if belems[s_group_cols[0]]==sitem1:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
147 repeats1.append(int(belems[6]))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
148 repeats2.append(int(belems[13]))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
149 if belems[4] == 'mononucleotide':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
150 thresholds.append(mono_threshold)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
151 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
152 thresholds.append(non_mono_threshold)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
153 mut1[str(pitem1)+'\t'+str(sitem1)]=mutabilityEstimator(repeats1,repeats2,thresholds)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
154 if region == 'align':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
155 count1[str(pitem1)+'\t'+str(sitem1)]=min(sum(repeats1),sum(repeats2))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
156 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
157 if winspecies == 1:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
158 count1["%s\t%s" %(pitem1,sitem1)]=sum(repeats1)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
159 elif winspecies == 2:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
160 count1["%s\t%s" %(pitem1,sitem1)]=sum(repeats2)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
161 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
162 if type(sitem1) == list:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
163 if s_group_cols[0] == 6:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
164 belems[s_group_cols[0]] = int(belems[s_group_cols[0]])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
165 if belems[p_group_cols[0]]==pitem1 and belems[s_group_cols[0]] in sitem1:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
166 repeats1.append(int(belems[6]))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
167 repeats2.append(int(belems[13]))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
168 if belems[4] == 'mononucleotide':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
169 thresholds.append(mono_threshold)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
170 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
171 thresholds.append(non_mono_threshold)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
172 mut1["%s\t%s" %(pitem1,sitem1)]=mutabilityEstimator(repeats1,repeats2,thresholds)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
173 if region == 'align':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
174 count1[str(pitem1)+'\t'+str(sitem1)]=min(sum(repeats1),sum(repeats2))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
175 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
176 if winspecies == 1:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
177 count1[str(pitem1)+'\t'+str(sitem1)]=sum(repeats1)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
178 elif winspecies == 2:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
179 count1[str(pitem1)+'\t'+str(sitem1)]=sum(repeats2)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
180 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
181 if belems[p_group_cols[0]]==pitem1 and belems[s_group_cols[0]]==sitem1:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
182 repeats1.append(int(belems[6]))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
183 repeats2.append(int(belems[13]))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
184 if belems[4] == 'mononucleotide':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
185 thresholds.append(mono_threshold)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
186 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
187 thresholds.append(non_mono_threshold)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
188 mut1["%s\t%s" %(pitem1,sitem1)]=mutabilityEstimator(repeats1,repeats2,thresholds)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
189 if region == 'align':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
190 count1[str(pitem1)+'\t'+str(sitem1)]=min(sum(repeats1),sum(repeats2))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
191 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
192 if winspecies == 1:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
193 count1["%s\t%s" %(pitem1,sitem1)]=sum(repeats1)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
194 elif winspecies == 2:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
195 count1["%s\t%s" %(pitem1,sitem1)]=sum(repeats2)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
196 else: #Sub-group by feature is None
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
197 for bline in blk_lines:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
198 belems = bline.split('\t')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
199 if type(pitem1) == list:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
200 #print >>sys.stderr, "item: " + str(item1)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
201 if p_group_cols[0] == 6:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
202 belems[p_group_cols[0]] = int(belems[p_group_cols[0]])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
203 if belems[p_group_cols[0]] in pitem1:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
204 repeats1.append(int(belems[6]))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
205 repeats2.append(int(belems[13]))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
206 if belems[4] == 'mononucleotide':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
207 thresholds.append(mono_threshold)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
208 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
209 thresholds.append(non_mono_threshold)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
210 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
211 if belems[p_group_cols[0]]==pitem1:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
212 repeats1.append(int(belems[6]))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
213 repeats2.append(int(belems[13]))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
214 if belems[4] == 'mononucleotide':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
215 thresholds.append(mono_threshold)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
216 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
217 thresholds.append(non_mono_threshold)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
218 mut1["%s" %(pitem1)]=mutabilityEstimator(repeats1,repeats2,thresholds)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
219 if region == 'align':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
220 count1["%s" %(pitem1)]=min(sum(repeats1),sum(repeats2))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
221 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
222 if winspecies == 1:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
223 count1[str(pitem1)]=sum(repeats1)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
224 elif winspecies == 2:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
225 count1[str(pitem1)]=sum(repeats2)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
226
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
227 for pitem2 in uniq_elems_2:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
228 #repeats1 = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
229 #repeats2 = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
230 thresholds = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
231 if s_group_cols[0] != -1: #Sub-group by feature is not None
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
232 for sitem2 in uniq_s_elems_2:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
233 repeats1 = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
234 repeats2 = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
235 if type(sitem2)==type(''):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
236 sitem2 = sitem2.strip()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
237 for bline in blk_lines:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
238 belems = bline.split('\t')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
239 if type(pitem2) == list:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
240 if p_group_cols[0] == 6:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
241 belems[p_group_cols[1]] = int(belems[p_group_cols[1]])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
242 if belems[p_group_cols[1]] in pitem2 and belems[s_group_cols[1]]==sitem2:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
243 repeats2.append(int(belems[13]))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
244 repeats1.append(int(belems[6]))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
245 if belems[4] == 'mononucleotide':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
246 thresholds.append(mono_threshold)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
247 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
248 thresholds.append(non_mono_threshold)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
249 mut2["%s\t%s" %(pitem2,sitem2)]=mutabilityEstimator(repeats2,repeats1,thresholds)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
250 #count2[str(pitem2)+'\t'+str(sitem2)]=len(repeats2)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
251 if region == 'align':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
252 count2["%s\t%s" %(pitem2,sitem2)]=min(sum(repeats1),sum(repeats2))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
253 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
254 if winspecies == 1:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
255 count2["%s\t%s" %(pitem2,sitem2)]=len(repeats2)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
256 elif winspecies == 2:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
257 count2["%s\t%s" %(pitem2,sitem2)]=len(repeats1)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
258 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
259 if type(sitem2) == list:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
260 if s_group_cols[0] == 6:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
261 belems[s_group_cols[1]] = int(belems[s_group_cols[1]])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
262 if belems[p_group_cols[1]]==pitem2 and belems[s_group_cols[1]] in sitem2:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
263 repeats2.append(int(belems[13]))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
264 repeats1.append(int(belems[6]))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
265 if belems[4] == 'mononucleotide':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
266 thresholds.append(mono_threshold)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
267 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
268 thresholds.append(non_mono_threshold)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
269 mut2["%s\t%s" %(pitem2,sitem2)]=mutabilityEstimator(repeats2,repeats1,thresholds)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
270 if region == 'align':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
271 count2["%s\t%s" %(pitem2,sitem2)]=min(sum(repeats1),sum(repeats2))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
272 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
273 if winspecies == 1:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
274 count2["%s\t%s" %(pitem2,sitem2)]=len(repeats2)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
275 elif winspecies == 2:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
276 count2["%s\t%s" %(pitem2,sitem2)]=len(repeats1)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
277 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
278 if belems[p_group_cols[1]]==pitem2 and belems[s_group_cols[1]]==sitem2:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
279 repeats1.append(int(belems[13]))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
280 repeats2.append(int(belems[6]))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
281 if belems[4] == 'mononucleotide':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
282 thresholds.append(mono_threshold)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
283 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
284 thresholds.append(non_mono_threshold)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
285 mut2["%s\t%s" %(pitem2,sitem2)]=mutabilityEstimator(repeats2,repeats1,thresholds)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
286 if region == 'align':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
287 count2["%s\t%s" %(pitem2,sitem2)]=min(sum(repeats1),sum(repeats2))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
288 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
289 if winspecies == 1:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
290 count2["%s\t%s" %(pitem2,sitem2)]=len(repeats2)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
291 elif winspecies == 2:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
292 count2["%s\t%s" %(pitem2,sitem2)]=len(repeats1)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
293 else: #Sub-group by feature is None
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
294 for bline in blk_lines:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
295 belems = bline.split('\t')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
296 if type(pitem2) == list:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
297 if p_group_cols[0] == 6:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
298 belems[p_group_cols[1]] = int(belems[p_group_cols[1]])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
299 if belems[p_group_cols[1]] in pitem2:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
300 repeats2.append(int(belems[13]))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
301 repeats1.append(int(belems[6]))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
302 if belems[4] == 'mononucleotide':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
303 thresholds.append(mono_threshold)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
304 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
305 thresholds.append(non_mono_threshold)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
306 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
307 if belems[p_group_cols[1]]==pitem2:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
308 repeats2.append(int(belems[13]))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
309 repeats1.append(int(belems[6]))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
310 if belems[4] == 'mononucleotide':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
311 thresholds.append(mono_threshold)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
312 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
313 thresholds.append(non_mono_threshold)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
314 mut2["%s" %(pitem2)]=mutabilityEstimator(repeats2,repeats1,thresholds)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
315 if region == 'align':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
316 count2["%s" %(pitem2)]=min(sum(repeats1),sum(repeats2))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
317 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
318 if winspecies == 1:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
319 count2["%s" %(pitem2)]=sum(repeats2)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
320 elif winspecies == 2:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
321 count2["%s" %(pitem2)]=sum(repeats1)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
322 for key in mut1.keys():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
323 if key in mut2.keys():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
324 mut = (mut1[key][0]+mut2[key][0])/(mut1[key][1]+mut2[key][1])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
325 count = count1[key]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
326 del mut2[key]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
327 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
328 unit_found = False
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
329 if p_group_cols[0] == 7 or s_group_cols[0] == 7: #if it is Repeat Unit (AG, GCT etc.) check for reverse-complements too
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
330 if p_group_cols[0] == 7:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
331 this,other = 0,1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
332 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
333 this,other = 1,0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
334 groups1 = key.split('\t')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
335 mutn = mut1[key][0]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
336 mutd = mut1[key][1]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
337 count = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
338 for key2 in mut2.keys():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
339 groups2 = key2.split('\t')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
340 if groups1[other] == groups2[other]:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
341 if groups1[this] in groups2[this]*2 or reverse_complement(groups1[this]) in groups2[this]*2:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
342 #mut = (mut1[key][0]+mut2[key2][0])/(mut1[key][1]+mut2[key2][1])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
343 mutn += mut2[key2][0]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
344 mutd += mut2[key2][1]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
345 count += int(count2[key2])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
346 unit_found = True
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
347 del mut2[key2]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
348 #break
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
349 if unit_found:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
350 mut = mutn/mutd
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
351 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
352 mut = mut1[key][0]/mut1[key][1]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
353 count = count1[key]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
354 mut = "%.2e" %(mut/num_generations)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
355 if region == 'align':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
356 print >>fout, str(blk) + '\t'+seq1 + '\t' + seq2 + '\t' +key.strip()+ '\t'+str(mut) + '\t'+ str(count)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
357 elif region == 'win':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
358 fout.write("%s\t%s\t%s\t%s\n" %(blk,key.strip(),mut,count))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
359 fout.flush()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
360
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
361 #catch any remaining repeats, for instance if the orthologous position contained different repeat units
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
362 for remaining_key in mut2.keys():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
363 mut = mut2[remaining_key][0]/mut2[remaining_key][1]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
364 mut = "%.2e" %(mut/num_generations)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
365 count = count2[remaining_key]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
366 if region == 'align':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
367 print >>fout, str(blk) + '\t'+seq1 + '\t'+seq2 + '\t'+remaining_key.strip()+ '\t'+str(mut)+ '\t'+ str(count)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
368 elif region == 'win':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
369 fout.write("%s\t%s\t%s\t%s\n" %(blk,remaining_key.strip(),mut,count))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
370 fout.flush()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
371 #print >>fout, blk + '\t'+remaining_key.strip()+ '\t'+str(mut)+ '\t'+ str(count)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
372
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
373 def counter(node, start, end, report_func):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
374 if start <= node.start < end and start < node.end <= end:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
375 report_func(node)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
376 if node.right:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
377 counter(node.right, start, end, report_func)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
378 if node.left:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
379 counter(node.left, start, end, report_func)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
380 elif node.start < start and node.right:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
381 counter(node.right, start, end, report_func)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
382 elif node.start >= end and node.left and node.left.maxend > start:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
383 counter(node.left, start, end, report_func)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
384
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
385
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
386 def main():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
387 infile = sys.argv[1]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
388
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
389 for i, line in enumerate( file ( infile )):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
390 line = line.rstrip('\r\n')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
391 if len( line )>0 and not line.startswith( '#' ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
392 elems = line.split( '\t' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
393 break
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
394 if i == 30:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
395 break # Hopefully we'll never get here...
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
396
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
397 if len( elems ) != 15:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
398 stop_err( "This tool only works on tabular data output by 'Extract Orthologous Microsatellites from pair-wise alignments' tool. The data in your input dataset is either missing or not formatted properly." )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
399 global winspecies, speciesind
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
400 if region == 'win':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
401 if dbkey_i in elems[1]:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
402 winspecies = 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
403 speciesind = 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
404 elif dbkey_i in elems[8]:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
405 winspecies = 2
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
406 speciesind = 8
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
407 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
408 stop_err("The species build corresponding to your interval file is not present in the Microsatellite file.")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
409
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
410 fin = open(infile, 'r')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
411 skipped = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
412 blk=0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
413 win=0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
414 linestr=""
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
415
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
416 if region == 'win':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
417
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
418 msats = NiceReaderWrapper( fileinput.FileInput( infile ),
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
419 chrom_col = speciesind,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
420 start_col = speciesind+1,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
421 end_col = speciesind+2,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
422 strand_col = -1,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
423 fix_strand = True)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
424 msatTree = quicksect.IntervalTree()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
425 for item in msats:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
426 if type( item ) is GenomicInterval:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
427 msatTree.insert( item, msats.linenum, item.fields )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
428
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
429 for iline in fint:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
430 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
431 iline = iline.rstrip('\r\n')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
432 if not(iline) or iline == "":
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
433 continue
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
434 ielems = iline.strip("\r\n").split('\t')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
435 ichr = ielems[chr_col_i]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
436 istart = int(ielems[start_col_i])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
437 iend = int(ielems[end_col_i])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
438 isrc = "%s.%s" %(dbkey_i,ichr)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
439 if isrc not in msatTree.chroms:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
440 continue
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
441 result = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
442 root = msatTree.chroms[isrc] #root node for the chrom
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
443 counter(root, istart, iend, lambda node: result.append( node ))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
444 if not(result):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
445 continue
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
446 tmpfile1 = tempfile.NamedTemporaryFile('wb+')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
447 for node in result:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
448 tmpfile1.write("%s\n" % "\t".join( node.other ))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
449
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
450 tmpfile1.seek(0)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
451 output_writer(iline, tmpfile1.readlines())
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
452 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
453 skipped+=1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
454 if skipped:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
455 print "Skipped %d intervals as invalid." %(skipped)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
456 elif region == 'align':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
457 if s_group_cols[0] != -1:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
458 print >>fout, "#Window\tSpecies_1\tSpecies_2\tGroupby_Feature\tSubGroupby_Feature\tMutability\tCount"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
459 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
460 print >>fout, "#Window\tSpecies_1\tWindow_Start\tWindow_End\tSpecies_2\tGroupby_Feature\tMutability\tCount"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
461 prev_bnum = -1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
462 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
463 for line in fin:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
464 line = line.strip("\r\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
465 if not(line) or line == "":
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
466 continue
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
467 elems = line.split('\t')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
468 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
469 assert int(elems[0])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
470 assert len(elems) == 15
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
471 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
472 continue
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
473 new_bnum = int(elems[0])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
474 if new_bnum != prev_bnum:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
475 if prev_bnum != -1:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
476 output_writer(prev_bnum, linestr.strip().replace('\r','\n').split('\n'))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
477 linestr = line + "\n"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
478 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
479 linestr += line
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
480 linestr += "\n"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
481 prev_bnum = new_bnum
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
482 output_writer(prev_bnum, linestr.strip().replace('\r','\n').split('\n'))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
483 except Exception, ea:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
484 print >>sys.stderr, ea
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
485 skipped += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
486 if skipped:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
487 print "Skipped %d lines as invalid." %(skipped)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
488 if __name__ == "__main__":
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
489 main()