annotate tools/regVariation/microsats_alignment_level.py @ 0:9071e359b9a3

Uploaded
author xuebing
date Fri, 09 Mar 2012 19:37:19 -0500
parents
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 Uses SPUTNIK to fetch microsatellites and extracts orthologous repeats from the sputnik output.
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, os, tempfile, string, math, re
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
8
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
9 def reverse_complement(text):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
10 DNA_COMP = string.maketrans( "ACGTacgt", "TGCAtgca" )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
11 comp = [ch for ch in text.translate(DNA_COMP)]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
12 comp.reverse()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
13 return "".join(comp)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
14
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
15 def main():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
16 if len(sys.argv) != 8:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
17 print >>sys.stderr, "Insufficient number of arguments."
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
18 sys.exit()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
19
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
20 infile = open(sys.argv[1],'r')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
21 separation = int(sys.argv[2])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
22 outfile = sys.argv[3]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
23 align_type = sys.argv[4]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
24 if align_type == "2way":
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
25 align_type_len = 2
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
26 elif align_type == "3way":
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
27 align_type_len = 3
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
28 mono_threshold = int(sys.argv[5])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
29 non_mono_threshold = int(sys.argv[6])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
30 allow_different_units = int(sys.argv[7])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
31
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
32 print "Min distance = %d bp; Min threshold for mono repeats = %d; Min threshold for non-mono repeats = %d; Allow different motifs = %s" %(separation, mono_threshold, non_mono_threshold, allow_different_units==1)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
33 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
34 fout = open(outfile, "w")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
35 print >>fout, "#Block\tSeq1_Name\tSeq1_Start\tSeq1_End\tSeq1_Type\tSeq1_Length\tSeq1_RepeatNumber\tSeq1_Unit\tSeq2_Name\tSeq2_Start\tSeq2_End\tSeq2_Type\tSeq2_Length\tSeq2_RepeatNumber\tSeq2_Unit"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
36 #sputnik_cmd = os.path.join(os.path.split(sys.argv[0])[0], "sputnik")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
37 sputnik_cmd = "sputnik"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
38 input = infile.read()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
39 skipped = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
40 block_num = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
41 input = input.replace('\r','\n')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
42 for block in input.split('\n\n'):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
43 block_num += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
44 tmpin = tempfile.NamedTemporaryFile()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
45 tmpout = tempfile.NamedTemporaryFile()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
46 tmpin.write(block.strip())
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
47 blk = tmpin.read()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
48 cmdline = sputnik_cmd + " " + tmpin.name + " > /dev/null 2>&1 >> " + tmpout.name
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
49 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
50 os.system(cmdline)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
51 except Exception, es:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
52 continue
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
53 sputnik_out = tmpout.read()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
54 tmpin.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
55 tmpout.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
56 if sputnik_out != "":
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
57 if len(block.split('>')[1:]) != 2: #len(sputnik_out.split('>')):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
58 skipped += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
59 continue
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
60 align_block = block.strip().split('>')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
61
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
62 lendict = {'mononucleotide':1, 'dinucleotide':2, 'trinucleotide':3, 'tetranucleotide':4, 'pentanucleotide':5, 'hexanucleotide':6}
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
63 blockdict={}
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
64 r=0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
65 namelist=[]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
66 for k,sput_block in enumerate(sputnik_out.split('>')[1:]):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
67 whole_seq = ''.join(align_block[k+1].split('\n')[1:]).replace('\n','').strip()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
68 p = re.compile('\n(\S*nucleotide)')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
69 repeats = p.split(sput_block.strip())
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
70 repeats_count = len(repeats)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
71 j = 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
72 name = repeats[0].strip()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
73 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
74 coords = re.search('\d+[-_:]\d+',name).group()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
75 coords = coords.replace('_','-').replace(':','-')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
76 except Exception, e:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
77 coords = '0-0'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
78 pass
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
79 r += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
80 blockdict[r]={}
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
81 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
82 sp_name = name[:name.index('.')]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
83 chr_name = name[name.index('.'):name.index('(')]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
84 namelist.append(sp_name + chr_name)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
85 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
86 namelist.append(name[:20])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
87 while j < repeats_count:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
88 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
89 if repeats[j].strip() not in lendict:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
90 j += 2
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
91 continue
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
92
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
93 if blockdict[r].has_key('types'):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
94 blockdict[r]['types'].append(repeats[j].strip()) #type of microsat
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
95 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
96 blockdict[r]['types'] = [repeats[j].strip()] #type of microsat
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
97
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
98 sequence = ''.join(align_block[r].split('\n')[1:]).replace('\n','').strip()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
99 start = int(repeats[j+1].split('--')[0].split(':')[0].strip())
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
100 #check to see if there are gaps before the start of the repeat, and change the start accordingly
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
101 sgaps = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
102 ch_pos = start - 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
103 while ch_pos >= 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
104 if whole_seq[ch_pos] == '-':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
105 sgaps += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
106 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
107 break #break at the 1st non-gap character
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
108 ch_pos -= 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
109 if blockdict[r].has_key('starts'):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
110 blockdict[r]['starts'].append(start+sgaps) #start co-ords adjusted with alignment co-ords to include GAPS
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
111 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
112 blockdict[r]['starts'] = [start+sgaps]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
113
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
114 end = int(repeats[j+1].split('--')[0].split(':')[1].strip())
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
115 #check to see if there are gaps after the end of the repeat, and change the end accordingly
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
116 egaps = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
117 for ch in whole_seq[end:]:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
118 if ch == '-':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
119 egaps += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
120 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
121 break #break at the 1st non-gap character
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
122 if blockdict[r].has_key('ends'):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
123 blockdict[r]['ends'].append(end+egaps) #end co-ords adjusted with alignment co-ords to include GAPS
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
124 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
125 blockdict[r]['ends'] = [end+egaps]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
126
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
127 repeat_seq = ''.join(repeats[j+1].replace('\r','\n').split('\n')[1:]).strip() #Repeat Sequence
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
128 repeat_len = repeats[j+1].split('--')[1].split()[1].strip()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
129 gap_count = repeat_seq.count('-')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
130 #print repeats[j+1].split('--')[1], len(repeat_seq), repeat_len, gap_count
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
131 repeat_len = str(int(repeat_len) - gap_count)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
132
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
133 rel_start = blockdict[r]['starts'][-1]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
134 gaps_before_start = whole_seq[:rel_start].count('-')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
135
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
136 if blockdict[r].has_key('gaps_before_start'):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
137 blockdict[r]['gaps_before_start'].append(gaps_before_start) #lengths
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
138 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
139 blockdict[r]['gaps_before_start'] = [gaps_before_start] #lengths
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
140
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
141 whole_seq_start= int(coords.split('-')[0])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
142 if blockdict[r].has_key('whole_seq_start'):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
143 blockdict[r]['whole_seq_start'].append(whole_seq_start) #lengths
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
144 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
145 blockdict[r]['whole_seq_start'] = [whole_seq_start] #lengths
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
146
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
147 if blockdict[r].has_key('lengths'):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
148 blockdict[r]['lengths'].append(repeat_len) #lengths
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
149 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
150 blockdict[r]['lengths'] = [repeat_len] #lengths
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
151
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
152 if blockdict[r].has_key('counts'):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
153 blockdict[r]['counts'].append(str(int(repeat_len)/lendict[repeats[j].strip()])) #Repeat Unit
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
154 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
155 blockdict[r]['counts'] = [str(int(repeat_len)/lendict[repeats[j].strip()])] #Repeat Unit
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
156
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
157 if blockdict[r].has_key('units'):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
158 blockdict[r]['units'].append(repeat_seq[:lendict[repeats[j].strip()]]) #Repeat Unit
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
159 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
160 blockdict[r]['units'] = [repeat_seq[:lendict[repeats[j].strip()]]] #Repeat Unit
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
161
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
162 except Exception, eh:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
163 pass
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
164 j+=2
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
165 #check the co-ords of all repeats corresponding to a sequence and remove adjacent repeats separated by less than the user-specified 'separation'.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
166 delete_index_list = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
167 for ind, item in enumerate(blockdict[r]['ends']):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
168 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
169 if blockdict[r]['starts'][ind+1]-item < separation:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
170 if ind not in delete_index_list:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
171 delete_index_list.append(ind)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
172 if ind+1 not in delete_index_list:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
173 delete_index_list.append(ind+1)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
174 except Exception, ek:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
175 pass
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
176 for index in delete_index_list: #mark them for deletion
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
177 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
178 blockdict[r]['starts'][index] = 'marked'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
179 blockdict[r]['ends'][index] = 'marked'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
180 blockdict[r]['types'][index] = 'marked'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
181 blockdict[r]['gaps_before_start'][index] = 'marked'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
182 blockdict[r]['whole_seq_start'][index] = 'marked'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
183 blockdict[r]['lengths'][index] = 'marked'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
184 blockdict[r]['counts'][index] = 'marked'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
185 blockdict[r]['units'][index] = 'marked'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
186 except Exception, ej:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
187 pass
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
188 #remove 'marked' elements from all the lists
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
189 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
190 for key in blockdict[r].keys():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
191 for elem in blockdict[r][key]:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
192 if elem == 'marked':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
193 blockdict[r][key].remove(elem)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
194 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
195 #print blockdict
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
196
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
197 #make sure that the blockdict has keys for both the species
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
198 if (1 not in blockdict) or (2 not in blockdict):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
199 continue
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
200
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
201 visited_2 = [0 for x in range(len(blockdict[2]['starts']))]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
202 for ind1,coord_s1 in enumerate(blockdict[1]['starts']):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
203 if coord_s1 == 'marked':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
204 continue
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
205 coord_e1 = blockdict[1]['ends'][ind1]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
206 out = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
207 for ind2,coord_s2 in enumerate(blockdict[2]['starts']):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
208 if coord_s2 == 'marked':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
209 visited_2[ind2] = 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
210 continue
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
211 coord_e2 = blockdict[2]['ends'][ind2]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
212 #skip if the 2 repeats are not of the same type or don't have the same repeating unit.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
213 if allow_different_units == 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
214 if (blockdict[1]['types'][ind1] != blockdict[2]['types'][ind2]):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
215 continue
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
216 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
217 if (blockdict[1]['units'][ind1] not in blockdict[2]['units'][ind2]*2) and (reverse_complement(blockdict[1]['units'][ind1]) not in blockdict[2]['units'][ind2]*2):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
218 continue
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
219 #print >>sys.stderr, (reverse_complement(blockdict[1]['units'][ind1]) not in blockdict[2]['units'][ind2]*2)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
220 #skip if the repeat number thresholds are not met
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
221 if blockdict[1]['types'][ind1] == 'mononucleotide':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
222 if (int(blockdict[1]['counts'][ind1]) < mono_threshold):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
223 continue
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
224 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
225 if (int(blockdict[1]['counts'][ind1]) < non_mono_threshold):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
226 continue
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
227
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
228 if blockdict[2]['types'][ind2] == 'mononucleotide':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
229 if (int(blockdict[2]['counts'][ind2]) < mono_threshold):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
230 continue
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
231 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
232 if (int(blockdict[2]['counts'][ind2]) < non_mono_threshold):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
233 continue
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
234 #print "s1,e1=%s,%s; s2,e2=%s,%s" %(coord_s1,coord_e1,coord_s2,coord_e2)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
235 if (coord_s1 in range(coord_s2,coord_e2)) or (coord_e1 in range(coord_s2,coord_e2)):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
236 out.append(str(block_num))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
237 out.append(namelist[0])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
238 rel_start = blockdict[1]['whole_seq_start'][ind1] + coord_s1 - blockdict[1]['gaps_before_start'][ind1]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
239 rel_end = rel_start + int(blockdict[1]['lengths'][ind1])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
240 out.append(str(rel_start))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
241 out.append(str(rel_end))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
242 out.append(blockdict[1]['types'][ind1])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
243 out.append(blockdict[1]['lengths'][ind1])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
244 out.append(blockdict[1]['counts'][ind1])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
245 out.append(blockdict[1]['units'][ind1])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
246 out.append(namelist[1])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
247 rel_start = blockdict[2]['whole_seq_start'][ind2] + coord_s2 - blockdict[2]['gaps_before_start'][ind2]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
248 rel_end = rel_start + int(blockdict[2]['lengths'][ind2])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
249 out.append(str(rel_start))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
250 out.append(str(rel_end))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
251 out.append(blockdict[2]['types'][ind2])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
252 out.append(blockdict[2]['lengths'][ind2])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
253 out.append(blockdict[2]['counts'][ind2])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
254 out.append(blockdict[2]['units'][ind2])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
255 print >>fout, '\t'.join(out)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
256 visited_2[ind2] = 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
257 out=[]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
258
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
259 if 0 in visited_2: #there are still some elements in 2nd set which haven't found orthologs yet.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
260 for ind2, coord_s2 in enumerate(blockdict[2]['starts']):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
261 if coord_s2 == 'marked':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
262 continue
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
263 if visited_2[ind] != 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
264 continue
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
265 coord_e2 = blockdict[2]['ends'][ind2]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
266 out = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
267 for ind1,coord_s1 in enumerate(blockdict[1]['starts']):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
268 if coord_s1 == 'marked':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
269 continue
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
270 coord_e1 = blockdict[1]['ends'][ind1]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
271 #skip if the 2 repeats are not of the same type or don't have the same repeating unit.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
272 if allow_different_units == 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
273 if (blockdict[1]['types'][ind1] != blockdict[2]['types'][ind2]):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
274 continue
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
275 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
276 if (blockdict[1]['units'][ind1] not in blockdict[2]['units'][ind2]*2):# and reverse_complement(blockdict[1]['units'][ind1]) not in blockdict[2]['units'][ind2]*2:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
277 continue
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
278 #skip if the repeat number thresholds are not met
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
279 if blockdict[1]['types'][ind1] == 'mononucleotide':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
280 if (int(blockdict[1]['counts'][ind1]) < mono_threshold):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
281 continue
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
282 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
283 if (int(blockdict[1]['counts'][ind1]) < non_mono_threshold):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
284 continue
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
285
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
286 if blockdict[2]['types'][ind2] == 'mononucleotide':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
287 if (int(blockdict[2]['counts'][ind2]) < mono_threshold):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
288 continue
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
289 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
290 if (int(blockdict[2]['counts'][ind2]) < non_mono_threshold):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
291 continue
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
292
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
293 if (coord_s2 in range(coord_s1,coord_e1)) or (coord_e2 in range(coord_s1,coord_e1)):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
294 out.append(str(block_num))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
295 out.append(namelist[0])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
296 rel_start = blockdict[1]['whole_seq_start'][ind1] + coord_s1 - blockdict[1]['gaps_before_start'][ind1]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
297 rel_end = rel_start + int(blockdict[1]['lengths'][ind1])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
298 out.append(str(rel_start))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
299 out.append(str(rel_end))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
300 out.append(blockdict[1]['types'][ind1])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
301 out.append(blockdict[1]['lengths'][ind1])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
302 out.append(blockdict[1]['counts'][ind1])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
303 out.append(blockdict[1]['units'][ind1])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
304 out.append(namelist[1])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
305 rel_start = blockdict[2]['whole_seq_start'][ind2] + coord_s2 - blockdict[2]['gaps_before_start'][ind2]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
306 rel_end = rel_start + int(blockdict[2]['lengths'][ind2])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
307 out.append(str(rel_start))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
308 out.append(str(rel_end))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
309 out.append(blockdict[2]['types'][ind2])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
310 out.append(blockdict[2]['lengths'][ind2])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
311 out.append(blockdict[2]['counts'][ind2])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
312 out.append(blockdict[2]['units'][ind2])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
313 print >>fout, '\t'.join(out)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
314 visited_2[ind2] = 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
315 out=[]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
316
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
317 #print >>fout, blockdict
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
318 except Exception, exc:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
319 print >>sys.stderr, "type(exc),args,exc: %s, %s, %s" %(type(exc), exc.args, exc)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
320
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
321 if __name__ == "__main__":
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
322 main()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
323