-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert_junction_sam.py
executable file
·253 lines (211 loc) · 7.7 KB
/
convert_junction_sam.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#!/usr/bin/env python
# Co-written by Angadhjot Hundal.
# Copyright (c) 2011, Angela Brooks. [email protected]
# All rights reserved.
import time
import re
import os
import sys
import fileinput
import pdb
KNOWN_TAGS = ["XA", "XM", "MD", "NM", "NH"]
JUNC_DELIM = "|"
def JUNCTION( w2 ):
junc_read = w2.split("|")
return junc_read
def READ( junc ):
t = junc[-1]
genes = junc[-3]
chr = junc[0]
s = junc[-2]
intron_start = int(junc[2]) + 1
intron_end = int(junc[3]) - 1
return t, genes, chr, s, intron_start, intron_end
def INTRON( w2 ):
temp = re.findall('\:\:\:\d+', w2)
intron_end = temp[-1][3:]
return intron_end
def STRAND_DIR( junc ):
if junc == '-':
direction = 16
else:
direction = 0
return direction
def DIR( w1, dir ):
if dir == 16:
# Bitwise exclusive or when the junction is on the opposite
# strand
return repr(int(w1)^16)
return w1
def CIGAR( w3 , w5, dir , intron_start , intron_end, sub ):
total = int(w5[:-1])
length = total - int(sub)
intron_len = intron_end_pos - intron_start_pos + 1
if dir == 0:
first_M = length - int(w3) + 1
sec_M = total - first_M
else:
sec_M = length - int(w3) + 1
first_M = total - sec_M
exon_start_pos1 = int(intron_start) - first_M
exon_end_pos1 = int(intron_start) - 1
exon_start_pos2 = int(intron_end) + 1
exon_end_pos2 = int(exon_start_pos2) + sec_M
w3 = str(exon_start_pos1)
w5 = str(first_M)+"M"+str(intron_len)+"N"+str(sec_M)+"M"
return w3, w5
def SEQ( w9 ):
reverse = []
for base in w9:
if base == "C":
reverse.append("G")
elif base == "G":
reverse.append("C")
elif base == "A":
reverse.append("T")
elif base == "T":
reverse.append("A")
reverse_compliment = reverse[::-1]
final_seq = ''.join( reverse_compliment )
return final_seq
def formatLine(line):
line = line.replace("\r","")
line = line.replace("\n","")
return line
def reverseQual(old_qual):
qual_list = list(old_qual)
qual_list.reverse()
return "".join(qual_list)
def reverseMD(value):
md_list = re.split("(\d+)", value)
md_list.reverse()
return "".join(md_list)
def diffJunc(other_junc_format):
(chr, upstr_start, upstr_end, downstr_start, downstr_end, type, strand,
gene1, gene2) = other_junc_format.split(":::")
# THIS IS FOR A REALLY WEIRD FORMAT
intron_start = int(upstr_end) - 1
new_format = [type, gene1, gene2, chr.lstrip("chr"),strand,
repr(intron_start), downstr_start]
return ":::".join(new_format)
usage = "usage: %s infile outfile overhang [--watsonJunctions]" % os.path.basename(sys.argv[0])
if len( sys.argv ) < 4:
print usage
sys.exit(1)
else:
# print "There are %s args " %len( sys.argv )
fileToSearch = open( sys.argv[1], 'r' )
fileToOutput = sys.argv[2]
oldFileName = 'old_' + fileToOutput
tempFileName = 'temp_' + fileToOutput
# If there's already an 'old_' prefixed backup file there from
# a previous run, remove it...
if os.path.isfile( tempFileName ):
os.remove( tempFileName )
if os.path.isfile( oldFileName ):
os.remove( oldFileName )
if os.path.isfile( fileToOutput ):
os.remove( fileToOutput )
sub = sys.argv[3]
# Junction sequences can be given in the watson strand only, which will
# affect the way the junction is converted.
isWatsonJunctions = False
if len(sys.argv) == 5:
if sys.argv[4] == "--watsonJunctions":
isWatsonJunctions = True
# Remove unaligned reads
fTemp = open(tempFileName , 'a')
# fTemp.writelines("This is a temporary file: \n")
for lines in fileToSearch:
words = lines.split()
if words[1] != '4':
fTemp.write(lines)
fileToSearch.close()
fTemp.close()
fin = open(tempFileName , 'r')
fout = open(oldFileName , 'a')
insertionFlag = False
deletionFlag = False
softFlag = False
hardFlag = False
for lines in fin:
lines = formatLine(lines)
words = lines.split()
if "I" in words[5]:
if not insertionFlag:
print "Not handling insertion alignments yet..."
insertionFlag = True
continue
if "D" in words[5]:
if not deletionFlag:
print "Not handling deletion alignments yet..."
deletionFlag = True
continue
if "S" in words[5]:
if not softFlag:
print "Not handling soft clipping alignments yet..."
softFlag = True
continue
if "H" in words[5]:
if not hardFlag:
print "Not handling hard clipping alignments yet..."
hardFlag = True
continue
if len(words) >= 11:
#### if words[2].startswith("chr"):
#### words[2] = diffJunc(words[2])
junc_read = JUNCTION( words[2] )
if len(junc_read) > 1: # It is a junction read.
# type, gene1, gene2, read, strand, intron_start_pos = READ( junc_read )
type, genes, chr, strand, intron_start_pos, intron_end_pos = READ( junc_read )
# direction = STRAND_DIR( strand )
direction = int(words[1])
new14 = "Y0:Z:"+words[2]
new15 = "XS:A:"+strand
if not isWatsonJunctions:
words[1] = DIR( words[1], direction )
if strand == "-" and direction == 0:
words[9] = SEQ( words[9] )
words[10] = reverseQual(words[10])
words[2] = chr
words[3], words[5] = CIGAR( words[3] , words[5], int(words[1]) , intron_start_pos , intron_end_pos , sub)
remaining_fields = words[11:]
new_fields = []
for field in remaining_fields:
tag, type, val = field.split(":")
if tag not in KNOWN_TAGS:
print "ERROR: Unknown tag: %s" % tag
sys.exit(1)
if tag == "XA" or tag == "XM" or tag == "NM" or tag == "NH":
new_fields.append(field)
if tag == "MD":
if strand == "-":
new_field = "%s:%s:%s" % (tag,
type,
reverseMD(val))
new_fields.append(field)
else:
new_fields.append(field)
# t1 = words[11]
# words[11] = words[13]
# words[13] = "X"+t1[-1:]+":i:1"
new_fields.append(new14)
new_fields.append(new15)
new_line = [ words[0], words[1], words[2], words[3], words[4], words[5], words[6], words[7],
words[8], words[9], words[10]]
new_line.extend(new_fields)
new_string = '\t'.join(new_line)
fout.write(new_string+'\n')
else: # Is a genome read.
fout.write(lines + "\n")
break
else:
print "SAM file needs at least 11 fields."
sys.exit(1)
fin.close()
fout.close()
# Rename the original file by prefixing it with 'old_'
os.rename( oldFileName, fileToOutput )
# Remove temporary file
if os.path.isfile( tempFileName ):
os.remove( tempFileName )