-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemultiplex.py
347 lines (296 loc) · 13.2 KB
/
demultiplex.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#!/usr/bin/env python
import sys, argparse, os, itertools
from collections import defaultdict
def readfq(fp):#,barcode_length): # this is a generator function
last = None # this is a buffer keeping the last unprocessed line
while True: # mimic closure; is it a bad idea?
if not last: # the first record or a record following a fastq
for l in fp: # search for the start of the next record
if l[0] in '>@': # fasta/q header line
last = l[:-1] # save this line
break
if not last: break
#name, seqs, last = last[1:].partition(" ")[0], [], None
name, seqs, last = last[1:], [], None
for l in fp: # read the sequence
if l[0] in '@+>':
last = l[:-1]
break
seqs.append(l[:-1])
#seqs.append(l[:barcode_length])
if not last or last[0] != '+': # this is a fasta record
yield name, ''.join(seqs), None # yield a fasta record
if not last: break
else: # this is a fastq record
seq, leng, seqs = ''.join(seqs), 0, []
for l in fp: # read the quality
seqs.append(l[:-1])
leng += len(l) - 1
if leng >= len(seq): # have read enough quality
last = None
yield name, seq, ''.join(seqs); # yield a fastq record
break
if last: # reach EOF before reading enough quality
yield name, seq, None # yield a fasta record instead
break
def readfq_full(f1):
reads = []
name = ""
seq = ""
qual = ""
for i,l in enumerate(f1):
l = l.strip()
#if i % 4 == 2:# and l == "+":
# continue
if i % 4 == 0:# and l == "@":
name = l
elif i % 4 == 1:
seq = l
elif i % 4 == 3:
qual = l
reads.append([name,seq,qual])
return reads
parser = argparse.ArgumentParser(description="Demultiplex sequencing run.")
parser.add_argument('barcodes', help="Barcodes")
parser.add_argument('--r1', help="Read1")
parser.add_argument('--r2', help="Read2")
parser.add_argument('--out', default="out", help="Output dir")
parser.add_argument('--force_pair_bc', default=False, action="store_true", help="Force same barcodes on mates")
parser.add_argument('--no_out', default=False, action="store_true", help="Do not output reads")
parser.add_argument('--flush', default="all", help="Reads to flush at a time")
parser.add_argument('--pos', default=False, action="store_true", help="Check for positional effects")
args = parser.parse_args()
barcodes = {}
barcodes_list = []
bar_max = 0
bar_min = 10
with open(args.barcodes) as f:
for l in f:
l = l.strip()
barcodes[l.split()[1]] = l.split()[0]
barcodes_list.append(l.split()[1])
bar_max=max(bar_max,len(l.split()[1]))
bar_min=min(bar_min,len(l.split()[1]))
barcode_r1r2_r1 = defaultdict(list)
barcode_r1r2_r1_ct = defaultdict(int)
barcode_r1r2_r2 = defaultdict(list)
barcode_r1r2_r2_ct = defaultdict(int)
barcode_r1n_r1 = defaultdict(list)
barcode_r1n_r1_ct = defaultdict(int)
barcode_r1n_r2 = defaultdict(list)
barcode_r1n_r2_ct = defaultdict(int)
barcode_r2n_r1 = defaultdict(list)
barcode_r2n_r1_ct = defaultdict(int)
barcode_r2n_r2 = defaultdict(list)
barcode_r2n_r2_ct = defaultdict(int)
barcode_mix_r1 = []
barcode_mix_r1_ct = 0
barcode_mix_r2 = []
barcode_mix_r2_ct = 0
barcode_none_r1 = []
barcode_none_r1_ct = 0
barcode_none_r2 = []
barcode_none_r2_ct = 0
f1 = open(args.r1)
f2 = open(args.r2)
#PREP OUTPUT FILES
if args.no_out == False:
if os.path.exists(os.getcwd()+"/"+args.out) == False:
os.makedirs(os.getcwd()+"/"+args.out)
open(os.getcwd()+"/"+args.out+"/none_R1.fq", 'w').close()
open(os.getcwd()+"/"+args.out+"/none_R2.fq", 'w').close()
open(os.getcwd()+"/"+args.out+"/mix_R1.fq", 'w').close()
open(os.getcwd()+"/"+args.out+"/mix_R2.fq", 'w').close()
for b in barcodes.keys():
open(os.getcwd()+"/"+args.out+"/"+barcodes[b]+"_R1.fq", 'w').close()
open(os.getcwd()+"/"+args.out+"/"+barcodes[b]+"_R2.fq", 'w').close()
if args.force_pair_bc == False:
open(os.getcwd()+"/"+args.out+"/"+barcodes[b]+"_R1nR1.fq", 'w').close()
open(os.getcwd()+"/"+args.out+"/"+barcodes[b]+"_R1nR2.fq", 'w').close()
open(os.getcwd()+"/"+args.out+"/"+barcodes[b]+"_R2nR1.fq", 'w').close()
open(os.getcwd()+"/"+args.out+"/"+barcodes[b]+"_R2nR2.fq", 'w').close()
i = 0
if args.force_pair_bc == True:
for read1,read2 in itertools.izip(readfq(f1),readfq(f2)):
#~ for read1,read2 in itertools.izip(readfq_full(f1),readfq_full(f2)):
seq1 = read1[1]
seq2 = read2[1]
r1_hit_bar = ""
r2_hit_bar = "_"
i += 1
if args.flush != "all" and i % int(args.flush) == 0:
for b in barcodes.keys():
if args.no_out == False:
with open(os.getcwd()+"/"+args.out+"/"+barcodes[b]+"_R1.fq","a") as f1:
f1.write("\n".join(["\n".join(["@"+x[0],x[1],"+",x[2]]) for x in barcode_r1r2_r1[b]]))
with open(os.getcwd()+"/"+args.out+"/"+barcodes[b]+"_R2.fq","a") as f2:
f2.write("\n".join(["\n".join(["@"+x[0],x[1],"+",x[2]]) for x in barcode_r1r2_r2[b]]))
with open(os.getcwd()+"/"+args.out+"/none_R1.fq","a") as n1:
n1.write("\n".join(["\n".join(["@"+x[0],x[1],"+",x[2]]) for x in barcode_none_r1]))
with open(os.getcwd()+"/"+args.out+"/none_R2.fq","a") as n2:
n2.write("\n".join(["\n".join(["@"+x[0],x[1],"+",x[2]]) for x in barcode_none_r2]))
with open(os.getcwd()+"/"+args.out+"/mix_R1.fq","a") as n1:
n1.write("\n".join(["\n".join(["@"+x[0],x[1],"+",x[2]]) for x in barcode_mix_r1]))
with open(os.getcwd()+"/"+args.out+"/mix_R2.fq","a") as n2:
n2.write("\n".join(["\n".join(["@"+x[0],x[1],"+",x[2]]) for x in barcode_mix_r2]))
barcode_r1r2_r1_ct[b] += len(barcode_r1r2_r1[b])
barcode_r1r2_r2_ct[b] += len(barcode_r1r2_r2[b])
barcode_r1r2_r1[b] = []
barcode_r1r2_r2[b] = []
barcode_none_r1_ct += len(barcode_none_r1)
barcode_none_r2_ct += len(barcode_none_r2)
barcode_none_r1 = []
barcode_none_r2 = []
barcode_mix_r1_ct += len(barcode_mix_r1)
barcode_mix_r2_ct += len(barcode_mix_r2)
barcode_mix_r1 = []
barcode_mix_r2 = []
if seq1[:bar_min] not in {seq2[:bar_min]:1}:
barcode_none_r1.append(read1)
barcode_none_r2.append(read2)
continue
else:
for bartest in range(bar_min,bar_max+1):
if seq1[:bartest] in barcodes:
r1_hit_bar = seq1[:bartest]
barcode_r1r2_r1[r1_hit_bar].append(read1)
barcode_r1r2_r2[r1_hit_bar].append(read2)
break
else:
for read1,read2 in itertools.izip(readfq(f1),readfq(f2)):
#~ for read1,read2 in itertools.izip(readfq_full(f1),readfq_full(f2)):
name1 = read1[0]
name2 = read2[0]
tile1 = name1.split(":")[4]
x1 = name1.split(":")[5]
y1 = name1.split(":")[6]
tile2 = name2.split(":")[4]
x2 = name2.split(":")[5]
y2 = name2.split(":")[6]
seq1 = read1[1]
seq2 = read2[1]
r1_hit_bar = ""
r2_hit_bar = ""
i += 1
#PRINT OUT READS
if args.flush != "all" and i % int(args.flush) == 0:
for b in barcodes.keys():
if args.no_out == False:
with open(os.getcwd()+"/"+args.out+"/"+barcodes[b]+"_R1.fq","a") as f1:
f1.write("\n".join(["\n".join(["@"+x[0],x[1],"+",x[2]]) for x in barcode_r1r2_r1[b]]))
with open(os.getcwd()+"/"+args.out+"/"+barcodes[b]+"_R2.fq","a") as f2:
f2.write("\n".join(["\n".join(["@"+x[0],x[1],"+",x[2]]) for x in barcode_r1r2_r2[b]]))
with open(os.getcwd()+"/"+args.out+"/"+barcodes[b]+"_R1nR1.fq","a") as f1:
f1.write("\n".join(["\n".join(["@"+x[0],x[1],"+",x[2]]) for x in barcode_r1n_r1[b]]))
with open(os.getcwd()+"/"+args.out+"/"+barcodes[b]+"_R1nR2.fq","a") as f2:
f2.write("\n".join(["\n".join(["@"+x[0],x[1],"+",x[2]]) for x in barcode_r1n_r2[b]]))
with open(os.getcwd()+"/"+args.out+"/"+barcodes[b]+"_R2nR1.fq","a") as f1:
f1.write("\n".join(["\n".join(["@"+x[0],x[1],"+",x[2]]) for x in barcode_r2n_r1[b]]))
with open(os.getcwd()+"/"+args.out+"/"+barcodes[b]+"_R2nR2.fq","a") as f2:
f2.write("\n".join(["\n".join(["@"+x[0],x[1],"+",x[2]]) for x in barcode_r2n_r2[b]]))
with open(os.getcwd()+"/"+args.out+"/none_R1.fq","a") as n1:
n1.write("\n".join(["\n".join(["@"+x[0],x[1],"+",x[2]]) for x in barcode_none_r1]))
with open(os.getcwd()+"/"+args.out+"/none_R2.fq","a") as n2:
n2.write("\n".join(["\n".join(["@"+x[0],x[1],"+",x[2]]) for x in barcode_none_r2]))
with open(os.getcwd()+"/"+args.out+"/mix_R1.fq","a") as n1:
n1.write("\n".join(["\n".join(["@"+x[0],x[1],"+",x[2]]) for x in barcode_mix_r1]))
with open(os.getcwd()+"/"+args.out+"/mix_R2.fq","a") as n2:
n2.write("\n".join(["\n".join(["@"+x[0],x[1],"+",x[2]]) for x in barcode_mix_r2]))
barcode_r1r2_r1_ct[b] += len(barcode_r1r2_r1[b])
barcode_r1r2_r2_ct[b] += len(barcode_r1r2_r2[b])
barcode_r1r2_r1[b] = []
barcode_r1r2_r2[b] = []
barcode_r1n_r1_ct[b] += len(barcode_r1n_r1[b])
barcode_r1n_r2_ct[b] += len(barcode_r1n_r2[b])
barcode_r1n_r1[b] = []
barcode_r1n_r2[b] = []
barcode_r2n_r1_ct[b] += len(barcode_r2n_r1[b])
barcode_r2n_r2_ct[b] += len(barcode_r2n_r2[b])
barcode_r2n_r1[b] = []
barcode_r2n_r2[b] = []
barcode_none_r1_ct += len(barcode_none_r1)
barcode_none_r2_ct += len(barcode_none_r2)
barcode_none_r1 = []
barcode_none_r2 = []
barcode_mix_r1_ct += len(barcode_mix_r1)
barcode_mix_r2_ct += len(barcode_mix_r2)
barcode_mix_r1 = []
barcode_mix_r2 = []
#CHECK BARCODES
for bartest in range(bar_min,bar_max+1):
if seq1[:bartest] == seq2[:bartest] and seq1[:bartest] in barcodes:
r1_hit_bar = seq1[:bartest]
r2_hit_bar = seq1[:bartest]
break
elif seq1[:bartest] in barcodes:
r1_hit_bar = seq1[:bartest]
elif seq2[:bartest] in barcodes:
r2_hit_bar = seq2[:bartest]
if r1_hit_bar == r2_hit_bar and r1_hit_bar != "":
barcode_r1r2_r1[r1_hit_bar].append(read1)
barcode_r1r2_r2[r1_hit_bar].append(read2)
if r1_hit_bar != "" and r2_hit_bar == "":
barcode_r1n_r1[r1_hit_bar].append(read1)
barcode_r1n_r2[r1_hit_bar].append(read2)
elif r1_hit_bar == "" and r2_hit_bar != "":
barcode_r2n_r1[r2_hit_bar].append(read1)
barcode_r2n_r2[r2_hit_bar].append(read2)
elif r1_hit_bar != "" and r2_hit_bar != "" and r1_hit_bar != r2_hit_bar:
barcode_mix_r1.append(read1)
barcode_mix_r2.append(read2)
elif r1_hit_bar == "" and r2_hit_bar == "":
barcode_none_r1.append(read1)
barcode_none_r2.append(read2)
f1.close()
f2.close()
total_r1r2 = 0
total_r1n = 0
total_r2n = 0
for b in sorted(barcodes):
if args.flush != "all":
if args.no_out == False:
with open(os.getcwd()+"/"+args.out+"/"+barcodes[b]+"_R1.fq","a") as f1:
f1.write("\n".join(["\n".join(["@"+x[0],x[1],"+",x[2]]) for x in barcode_r1r2_r1[b]]))
with open(os.getcwd()+"/"+args.out+"/"+barcodes[b]+"_R2.fq","a") as f2:
f2.write("\n".join(["\n".join(["@"+x[0],x[1],"+",x[2]]) for x in barcode_r1r2_r2[b]]))
with open(os.getcwd()+"/"+args.out+"/"+barcodes[b]+"_R1nR1.fq","a") as f1:
f1.write("\n".join(["\n".join(["@"+x[0],x[1],"+",x[2]]) for x in barcode_r1n_r1[b]]))
with open(os.getcwd()+"/"+args.out+"/"+barcodes[b]+"_R1nR2.fq","a") as f2:
f2.write("\n".join(["\n".join(["@"+x[0],x[1],"+",x[2]]) for x in barcode_r1n_r2[b]]))
with open(os.getcwd()+"/"+args.out+"/"+barcodes[b]+"_R2nR1.fq","a") as f1:
f1.write("\n".join(["\n".join(["@"+x[0],x[1],"+",x[2]]) for x in barcode_r2n_r1[b]]))
with open(os.getcwd()+"/"+args.out+"/"+barcodes[b]+"_R2nR2.fq","a") as f2:
f2.write("\n".join(["\n".join(["@"+x[0],x[1],"+",x[2]]) for x in barcode_r2n_r2[b]]))
barcode_r1r2_r1_ct[b] += len(barcode_r1r2_r1[b])
barcode_r1r2_r2_ct[b] += len(barcode_r1r2_r2[b])
if args.force_pair_bc == False:
barcode_r1n_r2_ct[b] += len(barcode_r1n_r2[b])
barcode_r1n_r1_ct[b] += len(barcode_r1n_r1[b])
barcode_r2n_r1_ct[b] += len(barcode_r2n_r1[b])
barcode_r2n_r2_ct[b] += len(barcode_r2n_r2[b])
total_r1r2 += barcode_r1r2_r1_ct[b]
total_r1n += barcode_r1n_r1_ct[b]
total_r2n += barcode_r2n_r1_ct[b]
if args.force_pair_bc == False and args.flush != "all" and args.no_out == False:
print barcodes[b],b,barcode_r1r2_r1_ct[b],barcode_r1n_r1_ct[b],barcode_r2n_r1_ct[b]
else:
print barcodes[b],b,barcode_r1r2_r1_ct[b],barcode_r1n_r1_ct[b],barcode_r2n_r1_ct[b]
if args.no_out == False:
with open(os.getcwd()+"/"+args.out+"/none_R1.fq","a") as n1:
n1.write("\n".join(["\n".join(["@"+x[0],x[1],"+",x[2]]) for x in barcode_none_r1]))
with open(os.getcwd()+"/"+args.out+"/none_R2.fq","a") as n2:
n2.write("\n".join(["\n".join(["@"+x[0],x[1],"+",x[2]]) for x in barcode_none_r2]))
with open(os.getcwd()+"/"+args.out+"/mix_R1.fq","a") as n1:
n1.write("\n".join(["\n".join(["@"+x[0],x[1],"+",x[2]]) for x in barcode_mix_r1]))
with open(os.getcwd()+"/"+args.out+"/mix_R2.fq","a") as n2:
n2.write("\n".join(["\n".join(["@"+x[0],x[1],"+",x[2]]) for x in barcode_mix_r2]))
barcode_none_r1_ct += len(barcode_none_r1)
barcode_none_r2_ct += len(barcode_none_r2)
barcode_mix_r1_ct += len(barcode_mix_r1)
barcode_mix_r2_ct += len(barcode_mix_r2)
print "Total_R1R2",total_r1r2
print "Total_R1n",total_r1n
print "Total_R2n",total_r2n
print "No_barcode",barcode_none_r1_ct
print "Mixed_barcodes",barcode_mix_r1_ct
print "Grand_total",str(total_r1r2+total_r1n+total_r2n+barcode_none_r1_ct+barcode_mix_r1_ct)