forked from ibest/GRC_Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Illumina_demultiplexer.py
executable file
·250 lines (208 loc) · 7.84 KB
/
Illumina_demultiplexer.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
#!/usr/bin/env python
"""
Demultiplex reads when they come with a "barcode" read and haven't been split previously.
"""
import sys
from Bio import SeqIO
import os
import time
#------------------- classes -------------------------------
class fastqIter:
" A simple file iterator that returns 4 lines for fast fastq iteration. "
def __init__(self, handle):
self.inf = handle
def __iter__(self):
return self
def next(self):
with self.inf as inf:
lines = {'id': inf.readline().strip(),
'seq': inf.readline().strip(),
'+': inf.readline().strip(),
'qual': inf.readline().strip()}
assert(len(lines['seq']) == len(lines['qual']))
if lines['id'] == '' or lines['seq'] == '' or lines['+'] == '' or lines['qual'] == '':
return False
else:
return lines
@staticmethod
def parse(handle):
return fastqIter(handle)
def close(self):
self.inf.close()
"""
def writePE(outf1, outf2, r1, r2):
#read1
outf1.write(r1['id'])
outf1.write(r1['seq'])
outf1.write(r1['+'])
outf1.write(r1['qual'])
#read2
outf2.write(r2['id'])
outf2.write(r2['seq'])
outf2.write(r2['+'])
outf2.write(r2['qual'])
outPE2.write("@" + ID + "#0/2" '\n')
outPE2.write(r2[0] + '\n')
outPE2.write('+\n' + r2[1] + '\n')
"""
#------------------- functions ------------------------------
def strdist(s1, s2):
if len(s1) == len(s2) and len(s1) > 0:
return sum(map(lambda x: x[0] != x[1], zip(s1, s2))) # python is bad-ass
else:
print "ERROR lengths of barcodes and index read do not match!"
print "Target", s1
print "Index read:", s2
sys.exit()
#-------------------- Testing --------------------------------
## Testing stuff:
# sys.argv.append("targets.tsv")
# sys.argv.append("DoubleBarcodeRun_140407_NoIndex_L001_R1_001.fastq.gz")
# sys.argv.append("DoubleBarcodeRun_140407_NoIndex_L001_R4_001.fastq.gz")
# sys.argv.append("DoubleBarcodeRun_140407_NoIndex_L001_R2_001.fastq.gz")
# sys.argv.append("DoubleBarcodeRun_140407_NoIndex_L001_R3_001.fastq.gz")
#----------- introductory stuff to check input-----------------
if len(sys.argv) not in [4, 5, 6]:
print "Usage:"
print "\tFor Dual index PE: Illumina_demultiplexer.py targets.tsv read1.fastq<.gz> read2.fastq<.gz> barcode1.fastq<.gz> barcode2.fastq<.gz>"
print "\tFor PE: Illumina_demultiplexer.py targets.tsv read1.fastq<.gz> read2.fastq<.gz> barcode.fastq<.gz>"
print "\tFor SE: Illumina_demultiplexer.py targets.tsv read1.fastq<.gz> barcode.fastq<.gz>"
print "\t Where: "
print "\t\tfastq files can be gzipped or not but must end in .gz if they are gzipped."
print "\t\ttargets.tsv must be TAB seperated, and have column names sample_id<tab>barcode"
print "\t\tor for dual index: sample_id<tab>barcode1<tab>barcode2"
sys.exit()
#--------Get file names--------------
dualbc = False
PE = False
targetsf = sys.argv[1]
PE1f = sys.argv[2]
if len(sys.argv) == 4:
barcodef1 = sys.argv[3]
elif len(sys.argv) == 5:
PE2f = sys.argv[3]
barcodef1 = sys.argv[4]
PE = True
elif len(sys.argv) == 6:
PE2f = sys.argv[3]
barcodef1 = sys.argv[4]
barcodef2 = sys.argv[5]
dualbc = True
PE = True
#--------- Check that all inputs exist, report error and exit if not -----------
if not os.path.isfile(targetsf):
print targetsf, " file cannot be found, please check input parameters and try again."
sys.exit()
if not os.path.isfile(PE1f):
print PE1f, " file cannot be found, please check input parameters and try again."
sys.exit()
if not os.path.isfile(barcodef1):
print barcodef1, " file cannot be found, please check input parameters and try again."
sys.exit()
if dualbc and not os.path.isfile(barcodef2):
print barcodef2, " file cannot be found, please check input parameters and try again."
sys.exit()
if PE and not os.path.isfile(PE2f):
print PE2f, " file cannot be found, please check input parameters and try again."
sys.exit()
#-----------Create file handles for Fastq/gz files -------------------
if PE1f.split(".")[-1] == "gz":
print "Input appears to be gzipped."
import gzip
iter1 = SeqIO.parse(gzip.open(PE1f, 'rb'), "fastq")
if PE:
iter2 = SeqIO.parse(gzip.open(PE2f, 'rb'), "fastq")
iteridx1 = SeqIO.parse(gzip.open(barcodef1, 'rb'), "fastq")
if dualbc:
iteridx2 = SeqIO.parse(gzip.open(barcodef2, 'rb'), "fastq")
else:
print "Files appear to be uncompressed fastq."
iter1 = SeqIO.parse(open(PE1f, 'r'), "fastq")
if PE:
iter2 = SeqIO.parse(open(PE2f, 'r'), "fastq")
iteridx = SeqIO.parse(open(barcodef1, 'r'), "fastq")
if dualbc:
iteridx2 = SeqIO.parse(barcodef2, "fastaq")
#---------Open and parse the targets file -------------
targets = {} # barcode is key, sample_id is value
with open(targetsf, 'r') as inf:
l = inf.next()
l = l.strip().split('\t')
if dualbc:
if l[0] != 'sample_id' or l[1] != 'barcode1' or l[2] != 'barcode2':
print "Targets file is not properly formatted, first line must be: sample_id<tab>barcode1<tab>barcode2<additional field optional>"
sys.exit()
else:
if l[0] != 'sample_id' or l[1] != 'barcode':
print "Targets file is not properly formatted, first line must be: sample_id<tab>barcode<additional field optional>"
sys.exit()
for l in inf:
if l == '\n':
break
l = l.strip().split("\t")
if dualbc:
k = l[1] + l[2]
else:
k = l[1]
targets[k] = l[0]
# ------- Open output files -----------
outfiles = {}
for k in targets:
if PE:
outfiles[k] = [gzip.open("%s_%s_R1.fastq.gz" % (targets[k], k), 'wb'), gzip.open("%s_%s_R2.fastq.gz" % (targets[k], k), 'wb')]
else:
outfiles[k] = [gzip.open("%s_%s_R2.fastq.gz" % (targets[k], k), 'wb')]
# ------- make some counters for reporting ------------
counters = {}
for k in targets:
counters[k] = [0, 0] # first value is perfect matches, second is 1bp mismatch
counters['undetermined'] = 0
reads = 0
# Now iterate through both input files, split and write out
try:
startt = time.time()
while 1:
read1 = iter1.next()
if PE:
read2 = iter2.next()
idx1 = iteridx1.next()
if dualbc:
idx2 = iteridx2.next()
reads += 1
key = None
k = idx1.seq.tostring()
if dualbc:
k += idx2.seq.tostring()
if k in outfiles:
key = k
counters[key][0] += 1
else: # handle 1 base mismatches gracefully
for bc in outfiles:
if strdist(bc, k) <= 1:
key = bc
counters[key][1] += 1
if key is not None:
read1.description += key
SeqIO.write(read1, outfiles[key][0], "fastq")
if PE:
read2.description += key
SeqIO.write(read2, outfiles[key][1], "fastq")
else:
counters['undetermined'] += 1
if reads % 10000 == 0:
print"-------------------"
print "%s reads processed in %s seconds, %s reads/sec" % (reads, (time.time() - startt), reads/(time.time() - startt))
for k in targets:
print "%s_%s: perfect match:%s, mismatch:%s" % (targets[k], k, counters[k][0], counters[k][1])
print "undetermined: %s" % counters['undetermined']
print"-------------------"
except StopIteration:
pass
finally:
print "Finished processing "
print"-------------------"
print "%s reads processed."
for k in targets:
print "%s_%s: perfect match:%s, mismatch:%s" % (targets[k], k, counters[k][0], counters[k][1])
print "undetermind: %s" % counters['undetermined']
print"-------------------"