-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmgSNP_sam-filter.py
executable file
·146 lines (123 loc) · 3.96 KB
/
mgSNP_sam-filter.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
#! /usr/bin/python
#Note I noticed that some program modify the column number of MD data. please check if MD column used here is 11 (starting from 0). Otherwise change line 121
import os, sys, getopt, re
# Here tab5 is the CIGAR string
def getCIGARstats(tab5):
readLength = 0
clipLength = 0
indelLength = 0
#print tab9,
#seqlength = len(tab9)
#print seqlength,
#print tab5,
tab5 = tab5.strip()
match_all = re.findall( r"([0-9]*[MIDNSHP=X]*)", tab5, re.I)
# ignoring tag except H,S,M
for item in match_all:
#print item,
if re.match('.*H|.*S',item):
tmp_item = item.strip('HS')
clipLength += int(tmp_item)
readLength += int(tmp_item)
#print "-H/S-",
elif re.match('.*M|.*=|.*X',item):
tmp_item = item.strip('MX=')
readLength += int(tmp_item)
#print "-M/=/X-",
elif re.match('.*I|.*D',item):
tmp_item = item.strip('ID')
indelLength += int(tmp_item)
#print "-I/D-",
else:
pass
#print "*", readLength, "*",clipLength,"*", indelLength,"*"
return readLength, clipLength, indelLength
def getMDstats(tab11):
#MD = "0G24A1T22^CTA72T53^A87A76"
#print "MD:" + MD
MD_tag = tab11.split(':')
MD = MD_tag[2]
matchLength = 0
mismatchLength = 0
deletionLength = 0
match_all = re.findall( "(\^[A-Z]*)", MD, re.I)
for item in match_all:
if item != '':
#print item,
tmp_item = item.strip('^')
deletionLength += len(tmp_item)
#print
match_all = re.findall( "([A-Z]*)", MD, re.I)
for item in match_all:
if item != '':
#print item,
mismatchLength += len(item)
#print
match_all = re.findall( "([0-9]*)", MD, re.I)
for item in match_all:
if item != '':
#print item,
matchLength += int(item)
#print
mismatchLength = mismatchLength - deletionLength
#print "Match-" + str(matchLength) + "\tMisMatch-" + str(mismatchLength) + "\tDeletion-" + str(deletionLength)
return matchLength, mismatchLength, deletionLength
# defines arguments and how to process them
myopts, args = getopt.getopt(sys.argv[1:],"i:o:")
for o, a in myopts:
if o == '-i':
infile=a
elif o == '-o':
outfile=a
else:
print("Usage: %s -i input -o output" % sys.argv[0])
#print ("Input file : %s and output file: %s" % (infile,outfile) )
#Open input file
INFILE = ''
OUTFILE = ''
if os.path.isfile(infile):
try:
INFILE = open(infile, "r")
print "\nINFO: Reading input file " + infile
except IOError:
print 'ERROR: Cannot open file ', + infile
sys.exit()
else:
print "ERROR: Input file " + infile + " not found"
sys.exit()
try:
OUTFILE = open(outfile, "w")
print "INFO: Opened output file " + outfile + " for writing"
except IOError:
print 'ERROR: Cannot open file ', + outfile
sys.exit()
MIN_ALIGN_LENGTH = 80
MIN_PERCENT_MATCH = 95
for line in INFILE:
#print line
# Skipping the lines starting with '#'
if not line.startswith('@'):
#print line,
line = line.rstrip('\r\n|\n') # to remove endlines if any
tab = line.split()
tab2 = tab[2]
tab5 = tab[5]
tab9 = tab[9]
tab12 = tab[12]
if tab2 == '*' or tab5 == '*':
#OUTFILE.write(line + "\n")
pass
else:
readLength, clipLength, indelLength = getCIGARstats(tab5)
alignLength = readLength - clipLength
matchLength, mismatchLength, deletionLength = getMDstats(tab12)
percentMatch = 100 * (float(matchLength) / float(matchLength + mismatchLength))
#print "*", readLength, "*",clipLength,"*", indelLength,"*"
if alignLength > MIN_ALIGN_LENGTH and percentMatch >= MIN_PERCENT_MATCH:
#print line
OUTFILE.write(line + "\n")
#print tab5,tab12 , matchLength, mismatchLength, deletionLength, alignLength , percentMatch
else:
OUTFILE.write(line)
#pass
print "INFO: Program completed. Output file " + outfile + " generated.\n"