-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter_wgs_reads.py
81 lines (67 loc) · 2.09 KB
/
filter_wgs_reads.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
import sys, re
from time import time
if len(sys.argv) < 2:
print 'usage: python', sys.argv[0], '<FILENAME>'
print 'Script to filter pyrosequenced fasta files according to paper specifications'
exit(0)
fileName = sys.argv[1]
filteredFileName = fileName.rsplit('.')[0] + '_filtered.fna'
# TODO: check file existence
inFile = open(fileName, 'r')
outFile = open(filteredFileName, 'w+')
seq = ''
head = ''
nFiltered = 0
nReads = 0
seqDict = {}
t0 = time()
def passesFilter(read):
if len(read) < 60: return False
degenerate = read.count('N')
if degenerate > 2: return False
if re.search('NN', read) != None: return False
return True
# filter short and degenerate reads
for line in inFile:
if line.startswith('>'):
nReads += 1
if passesFilter(seq):
identifier = seq[0:20]
seqDict.setdefault(identifier, []).append((head, seq))
else:
print 'filtered bad sequence'
nFiltered += 1
head = line
seq = ''
else:
seq += line
print time() - t0, 'seconds'
print 'scanning', len(seqDict), 'possible duplicates'
# remove duplicates
def areDuplicates(readA, readB):
sharedLength = min(len(readA), len(readB))
identical = 0.0
for i in xrange(sharedLength):
if readA[i] == readB[i]:
identical += 1.0
return 1.0 * identical / sharedLength > 0.97
n = 0
for key in seqDict.keys():
print 'step', n, time() - t0, 'seconds'
n += 1
candidates = seqDict[key]
duplicates = set()
if len(candidates) > 1:
for i in xrange(len(candidates)-1):
for j in xrange(i+1, len(candidates)):
if areDuplicates(candidates[i][1], candidates[j][1]):
duplicates.add(candidates[j])
nFiltered += len(duplicates)
for dupe in duplicates:
candidates.remove(dupe)
# print filtered fasta-file
for group in seqDict:
for header, sequence in group:
outFile.write(header + '\n')
outFile.write(sequence + '\n')
print 'filtered', nFiltered, 'of', nReads, '=>', 100.0*(nReads-nFiltered)/nReads, 'percent kept'