annotate microsats_alignment_level.py @ 1:433660af5d5e

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