-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSplit_ReadsV2.codon
More file actions
executable file
·756 lines (614 loc) · 19.7 KB
/
Copy pathSplit_ReadsV2.codon
File metadata and controls
executable file
·756 lines (614 loc) · 19.7 KB
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
import sys
import bio
from bio import *
from typing import Dict, Set, List, Tuple
CANONICAL_CELL_TAG = "XI"
# -----------------------------
# Normalize SB: by design SB in comment includes injected base as FIRST base.
# Key is "all bases after the first one".
# -----------------------------
def normalize_sb_drop_first(sb: str) -> str:
if len(sb) < 2:
sys.stderr.write("ERROR: SB tag length < 2: " + sb + "\n")
sys.exit(1)
return sb[1:]
# -----------------------------
# Hot-path tag extraction from FASTQ comment
# Tokens like:
# CB:Z:AAAA SB:Z:AGCAT ...
# or
# CB:AAAA SB:AGCAT ...
# -----------------------------
def get_cb_mo_sb(comment: str) -> Tuple[str, str, str]:
cb = ""
mo = ""
sb = ""
i = 0
n = len(comment)
while i < n:
while i < n and (comment[i] == ' ' or comment[i] == '\t'):
i += 1
if i >= n:
break
j = i
while j < n and (comment[j] != ' ' and comment[j] != '\t'):
j += 1
if (j - i) >= 3 and comment[i] == 'C' and comment[i + 1] == 'B' and comment[i + 2] == ':':
k = comment.rfind(':', i, j)
if k >= 0 and (k + 1) < j:
cb = comment[k + 1:j]
elif (j - i) >= 3 and comment[i] == 'M' and comment[i + 1] == 'O' and comment[i + 2] == ':':
k = comment.rfind(':', i, j)
if k >= 0 and (k + 1) < j:
mo = comment[k + 1:j]
elif (j - i) >= 3 and comment[i] == 'S' and comment[i + 1] == 'B' and comment[i + 2] == ':':
k = comment.rfind(':', i, j)
if k >= 0 and (k + 1) < j:
sb = comment[k + 1:j]
i = j + 1
return cb, mo, sb
def get_cb_sb(comment: str) -> Tuple[str, str]:
cb = ""
sb = ""
i = 0
n = len(comment)
while i < n:
while i < n and (comment[i] == ' ' or comment[i] == '\t'):
i += 1
if i >= n:
break
j = i
while j < n and (comment[j] != ' ' and comment[j] != '\t'):
j += 1
if (j - i) >= 3 and comment[i] == 'C' and comment[i + 1] == 'B' and comment[i + 2] == ':':
k = comment.rfind(':', i, j)
if k >= 0 and (k + 1) < j:
cb = comment[k + 1:j]
elif (j - i) >= 3 and comment[i] == 'S' and comment[i + 1] == 'B' and comment[i + 2] == ':':
k = comment.rfind(':', i, j)
if k >= 0 and (k + 1) < j:
sb = comment[k + 1:j]
i = j + 1
return cb, sb
def canonical_cell_id(sample: str, group_name: str, cell_barcode: str) -> str:
return sample + "_" + group_name + "_" + cell_barcode
def cell_barcode_without_sb(cb: str, sb: str, sample: str, group_name: str) -> str:
if cb.startswith(sb):
cell = cb[len(sb):]
if cell:
return cell
if len(sb) > 1:
key = normalize_sb_drop_first(sb)
if cb.startswith(key):
cell = cb[len(key):]
if cell:
return cell
sys.stderr.write(
"ERROR: Cannot derive canonical cell barcode for sample " + sample +
" group " + group_name + ": CB tag '" + cb + "' does not start with SB tag '" + sb + "'\n"
)
sys.exit(1)
return ""
def canonicalize_fastq_comment(sample: str, group_name: str, comment: str) -> str:
cb, mo, sb = get_cb_mo_sb(comment)
if not cb:
cb, sb = get_cb_sb(comment)
if (not cb) or (not sb):
sys.stderr.write(
"ERROR: Missing CB or SB tag while canonicalizing cell ID for sample " +
sample + " group " + group_name + "\n"
)
sys.exit(1)
technical_cell = cell_barcode_without_sb(cb, sb, sample, group_name)
canonical = canonical_cell_id(sample, group_name, technical_cell)
out: List[str] = []
has_rg = False
has_canonical = False
for token in comment.split():
if token.startswith("CB:"):
out.append("CB:Z:" + technical_cell)
elif token.startswith("RG:"):
out.append("RG:Z:" + technical_cell)
has_rg = True
elif token.startswith(CANONICAL_CELL_TAG + ":"):
out.append(CANONICAL_CELL_TAG + ":Z:" + canonical)
has_canonical = True
else:
out.append(token)
if not has_rg:
out.append("RG:Z:" + technical_cell)
if not has_canonical:
out.append(CANONICAL_CELL_TAG + ":Z:" + canonical)
return "\t".join(out)
# -----------------------------
# SB group map loading
# TSV columns: sample<TAB>sb_group<TAB>sb_bc
# Returns:
# sb_to_gid: maps SB key -> group index
# idx_to_group: list of group names by index
# -----------------------------
def load_sb_group_map_tsv(path: str, sample: str) -> Tuple[Dict[str, int], List[str]]:
sb_to_gid: Dict[str, int] = {}
group_to_gid: Dict[str, int] = {}
idx_to_group: List[str] = []
with open(path, "r") as f:
for line in f:
line = line.rstrip()
if not line:
continue
if line[0] == '#':
continue
parts = line.split()
if len(parts) < 3:
continue
s = parts[0]
if s != sample:
continue
gname = parts[1]
sb_bc = parts[2]
if gname in group_to_gid:
gid = group_to_gid[gname]
else:
gid = len(idx_to_group)
group_to_gid[gname] = gid
idx_to_group.append(gname)
if sb_bc in sb_to_gid:
if sb_to_gid[sb_bc] != gid:
sys.stderr.write("ERROR: SB group conflict for sample " + sample + " SB " + sb_bc + "\n")
sys.exit(1)
else:
sb_to_gid[sb_bc] = gid
if len(sb_to_gid) == 0:
sys.stderr.write("ERROR: No SB group mapping found for sample " + sample + " in " + path + "\n")
sys.exit(1)
# sanity: ensure all groups have at least one SB
grp_counts: List[int] = []
for _ in range(len(idx_to_group)):
grp_counts.append(0)
for k in sb_to_gid:
grp_counts[sb_to_gid[k]] += 1
for g in range(len(idx_to_group)):
if grp_counts[g] == 0:
sys.stderr.write("ERROR: SB group has zero barcodes for sample " + sample + " group " + idx_to_group[g] + "\n")
sys.exit(1)
return sb_to_gid, idx_to_group
# -----------------------------
# MO map loading (unchanged behavior)
# Supports BOTH:
# (A) 3-col: sample mark mo_bc
# (B) 4-col: sample sb_group mark mo_bc
# -----------------------------
def load_mo_map_tsv(map_path: str, sample: str, group_to_gid: Dict[str, int], use_sb: bool) -> Tuple[List[str], List[int], List[int], List[str]]:
mark_to_idx: Dict[str, int] = {}
idx_to_mark: List[str] = []
mo_list: List[str] = []
mo_gid: List[int] = []
mo_mark_idx: List[int] = []
pair_to_mark: Dict[str, int] = {}
seen_format = 0
with open(map_path, "r") as f:
for line in f:
line = line.rstrip()
if not line:
continue
if line[0] == '#':
continue
parts = line.split()
if len(parts) < 3:
continue
s = parts[0]
if s != sample:
continue
if len(parts) >= 4:
if seen_format == 0:
seen_format = 4
elif seen_format != 4:
sys.stderr.write("ERROR: mixed 3-col and 4-col MO map lines for sample " + sample + "\n")
sys.exit(1)
gname = parts[1]
mark = parts[2]
mo = parts[3]
gid = 0
if use_sb:
if gname in group_to_gid:
gid = group_to_gid[gname]
else:
sys.stderr.write("ERROR: MO map uses sb_group '" + gname + "' not present in SB group map for sample " + sample + "\n")
sys.exit(1)
else:
gid = 0
else:
if seen_format == 0:
seen_format = 3
elif seen_format != 3:
sys.stderr.write("ERROR: mixed 3-col and 4-col MO map lines for sample " + sample + "\n")
sys.exit(1)
mark = parts[1]
mo = parts[2]
gid = -1
if mark in mark_to_idx:
midx = mark_to_idx[mark]
else:
midx = len(idx_to_mark)
mark_to_idx[mark] = midx
idx_to_mark.append(mark)
key = str(gid) + "|" + mo
if key in pair_to_mark:
if pair_to_mark[key] != midx:
sys.stderr.write("ERROR: mapping conflict for sample " + sample + " gid " + str(gid) + " MO " + mo + "\n")
sys.exit(1)
else:
pair_to_mark[key] = midx
mo_list.append(mo)
mo_gid.append(gid)
mo_mark_idx.append(midx)
if len(mo_list) == 0:
sys.stderr.write("ERROR: No MO mapping found for sample " + sample + " in " + map_path + "\n")
sys.exit(1)
return mo_list, mo_gid, mo_mark_idx, idx_to_mark
def write_SAM_RG_Header(sample: str, lib: str, out_path: str, passing_BCs: Set[str]) -> None:
with open(out_path, "w") as fh:
for bc in passing_BCs:
fh.write("@RG\tID:")
fh.write(bc)
fh.write("\tSM:")
fh.write(sample)
fh.write("\tLB:")
fh.write(lib)
fh.write("\tPL:ELEMENT\tPM:AVITI_500MIO\n")
def write_fastq_record(fh, name: str, comment: str, seq_s: str, qual_s: str) -> None:
fh.write("@")
fh.write(name)
if comment:
fh.write(" ")
fh.write(comment)
fh.write("\n")
fh.write(seq_s)
fh.write("\n+\n")
fh.write(qual_s)
fh.write("\n")
def find_mark_for_mo(mo: str, gid: int, mo_list: List[str], mo_gid: List[int], mo_mark_idx: List[int]) -> int:
mo_n = len(mo_list)
for k in range(mo_n):
if mo == mo_list[k] and mo_gid[k] == gid:
return mo_mark_idx[k]
for k in range(mo_n):
if mo == mo_list[k] and mo_gid[k] == -1:
return mo_mark_idx[k]
return -1
# -----------------------------
# Compute gid from SB raw:
# - primary key: drop injected base (sb_raw[1:])
# - fallback: sb_raw itself (defensive)
# - NEVER silently route to gid=0 on mismatch
# -----------------------------
def gid_from_sb_raw(sample: str, sb_raw: str, sb_to_gid: Dict[str, int]) -> int:
# defensive: if some inputs ever already have injected base removed
if sb_raw in sb_to_gid:
return sb_to_gid[sb_raw]
key1 = normalize_sb_drop_first(sb_raw)
if key1 in sb_to_gid:
return sb_to_gid[key1]
sys.stderr.write("ERROR: SB not found in SB group map for sample " + sample + ": raw=" + sb_raw + " key=" + key1 + "\n")
sys.exit(1)
return 0
def MakeRecordNameList_DNA(
sample: str,
out_folder: str,
R1_path: str,
R2_path: str,
lib_name: str,
mo_map_path: str,
sb_group_map_path: str = "",
unknown_policy: str = "fail"
) -> int:
use_sb = False
if sb_group_map_path and sb_group_map_path != "-" and sb_group_map_path != "none":
use_sb = True
sys.stdout.write("Initialization\n")
R1_gen = bio.FASTQ(R1_path, gzip=True, validate=False, copy=True)
R2_gen = bio.FASTQ(R2_path, gzip=True, validate=False, copy=True)
R2_it = iter(R2_gen)
sb_to_gid: Dict[str, int] = {}
idx_to_group: List[str] = []
group_to_gid: Dict[str, int] = {}
if use_sb:
sb_to_gid, idx_to_group = load_sb_group_map_tsv(sb_group_map_path, sample)
# build group_to_gid
for g in range(len(idx_to_group)):
group_to_gid[idx_to_group[g]] = g
mo_list, mo_gid, mo_mark_idx, idx_to_mark = load_mo_map_tsv(mo_map_path, sample, group_to_gid, use_sb)
n_marks = len(idx_to_mark)
n_groups = len(idx_to_group) if use_sb else 1
total_slots = n_groups * n_marks
unknown_mode = 0
if unknown_policy == "skip":
unknown_mode = 1
elif unknown_policy == "unknown":
unknown_mode = 2
elif unknown_policy != "fail":
sys.stderr.write("ERROR: unknown_policy must be fail|skip|unknown\n")
sys.exit(1)
used: List[int] = []
for _ in range(total_slots):
used.append(0)
for k in range(len(mo_list)):
midx = mo_mark_idx[k]
gid = mo_gid[k]
if gid == -1:
for g in range(n_groups):
used[g * n_marks + midx] = 1
else:
if gid >= 0 and gid < n_groups:
used[gid * n_marks + midx] = 1
r1_fhs = []
r2_fhs = []
bc_sets: List[Set[str]] = []
for i in range(total_slots):
bc_sets.append(Set[str]())
if used[i] == 1:
gid = i // n_marks
midx = i - gid * n_marks
mark = idx_to_mark[midx]
if use_sb:
gname = idx_to_group[gid]
prefix = out_folder + "/" + sample + "_" + gname + "_" + mark
else:
prefix = out_folder + "/" + sample + "_" + mark
r1_fhs.append(open(prefix + "_R1.fastq", "w"))
r2_fhs.append(open(prefix + "_R2.fastq", "w"))
else:
r1_fhs.append(open("/dev/null", "w"))
r2_fhs.append(open("/dev/null", "w"))
unknown_r1 = open("/dev/null", "w")
unknown_r2 = open("/dev/null", "w")
unknown_bcs: Set[str] = Set[str]()
unknown_count = 0
unknown_mo_count = 0
if unknown_mode == 2:
unknown_r1.close()
unknown_r2.close()
unknown_r1 = open(out_folder + "/" + sample + "_Unknown_R1.fastq", "w")
unknown_r2 = open(out_folder + "/" + sample + "_Unknown_R2.fastq", "w")
sys.stdout.write("Read Splitting\n")
processed = 0
next_report = 20000
for rec1 in R1_gen:
rec2 = next(R2_it)
processed += 1
if processed >= next_report:
sys.stdout.write("\r")
sys.stdout.write(str(processed))
sys.stdout.write(" reads processed ...")
sys.stdout.flush()
next_report += 20000
comment = rec1.comment
if comment.find("NoMatch") >= 0:
continue
cb, mo, sb_raw = get_cb_mo_sb(comment)
if (not cb) or (not mo) or (not sb_raw):
sys.stderr.write("ERROR: Missing CB or MO or SB tag in FASTQ comment\n")
sys.exit(1)
gid = 0
if use_sb:
gid = gid_from_sb_raw(sample, sb_raw, sb_to_gid)
midx = find_mark_for_mo(mo, gid, mo_list, mo_gid, mo_mark_idx)
if midx < 0:
unknown_mo_count += 1
unknown_count += 1
if unknown_mode == 1:
continue
elif unknown_mode == 2:
seq1 = str(rec1.seq); qual1 = str(rec1.qual)
seq2 = str(rec2.seq); qual2 = str(rec2.qual)
write_fastq_record(unknown_r1, rec1.name, rec1.comment, seq1, qual1)
write_fastq_record(unknown_r2, rec2.name, rec2.comment, seq2, qual2)
unknown_bcs.add(cb)
continue
else:
sys.stderr.write("ERROR: MO barcode not found for sample " + sample + ": " + mo + "\n")
sys.exit(1)
slot = gid * n_marks + midx
if used[slot] != 1:
sys.stderr.write("ERROR: internal: slot not marked used but read mapped to it\n")
sys.exit(1)
group_name = idx_to_group[gid] if use_sb else sample
comment1 = canonicalize_fastq_comment(sample, group_name, rec1.comment)
comment2 = canonicalize_fastq_comment(sample, group_name, rec2.comment)
canonical_cb, _, _ = get_cb_mo_sb(comment1)
seq1 = str(rec1.seq); qual1 = str(rec1.qual)
seq2 = str(rec2.seq); qual2 = str(rec2.qual)
write_fastq_record(r1_fhs[slot], rec1.name, comment1, seq1, qual1)
write_fastq_record(r2_fhs[slot], rec2.name, comment2, seq2, qual2)
bc_sets[slot].add(canonical_cb)
sys.stdout.write("\rFinished processing ")
sys.stdout.write(str(processed))
sys.stdout.write(" reads...\n")
if unknown_count > 0:
sys.stdout.write("Unknown reads total: ")
sys.stdout.write(str(unknown_count))
sys.stdout.write(" (MO_unknown=")
sys.stdout.write(str(unknown_mo_count))
sys.stdout.write(", policy=")
sys.stdout.write(unknown_policy)
sys.stdout.write(")\n")
sys.stdout.write("SAM_RG_Header Writing\n")
for i in range(total_slots):
r1_fhs[i].close()
r2_fhs[i].close()
if used[i] != 1:
continue
gid2 = i // n_marks
midx2 = i - gid2 * n_marks
mark = idx_to_mark[midx2]
if use_sb:
gname2 = idx_to_group[gid2]
hpath = out_folder + "/SAM_RG_Header_" + sample + "_" + gname2 + "_" + mark + ".tsv"
else:
hpath = out_folder + "/SAM_RG_Header_" + sample + "_" + mark + ".tsv"
write_SAM_RG_Header(sample, lib_name, hpath, bc_sets[i])
unknown_r1.close()
unknown_r2.close()
if unknown_mode == 2:
write_SAM_RG_Header(sample, lib_name, out_folder + "/SAM_RG_Header_" + sample + "_Unknown.tsv", unknown_bcs)
sys.stdout.write("DONE\n")
return 0
def MakeRecordNameList_RNA(
sample: str,
out_folder: str,
R1_path: str,
R2_path: str,
lib_name: str,
sb_group_map_path: str = "",
unknown_policy: str = "fail"
) -> int:
use_sb = False
if sb_group_map_path and sb_group_map_path != "-" and sb_group_map_path != "none":
use_sb = True
sys.stdout.write("Initialization\n")
R1_gen = bio.FASTQ(R1_path, gzip=True, validate=False, copy=True)
R2_gen = bio.FASTQ(R2_path, gzip=True, validate=False, copy=True)
R2_it = iter(R2_gen)
sb_to_gid: Dict[str, int] = {}
idx_to_group: List[str] = []
if use_sb:
sb_to_gid, idx_to_group = load_sb_group_map_tsv(sb_group_map_path, sample)
n_groups = len(idx_to_group) if use_sb else 1
unknown_mode = 0
if unknown_policy == "skip":
unknown_mode = 1
elif unknown_policy == "unknown":
unknown_mode = 2
elif unknown_policy != "fail":
sys.stderr.write("ERROR: unknown_policy must be fail|skip|unknown\n")
sys.exit(1)
r1_fhs = []
r2_fhs = []
bc_sets: List[Set[str]] = []
for g in range(n_groups):
bc_sets.append(Set[str]())
if use_sb:
gname = idx_to_group[g]
prefix = out_folder + "/" + sample + "_" + gname
else:
prefix = out_folder + "/" + sample
r1_fhs.append(open(prefix + "_R1.fastq", "w"))
r2_fhs.append(open(prefix + "_R2.fastq", "w"))
unknown_r1 = open("/dev/null", "w")
unknown_r2 = open("/dev/null", "w")
unknown_bcs: Set[str] = Set[str]()
unknown_count = 0
if unknown_mode == 2:
unknown_r1.close()
unknown_r2.close()
unknown_r1 = open(out_folder + "/" + sample + "_Unknown_R1.fastq", "w")
unknown_r2 = open(out_folder + "/" + sample + "_Unknown_R2.fastq", "w")
sys.stdout.write("Read Splitting\n")
processed = 0
next_report = 20000
for rec1 in R1_gen:
rec2 = next(R2_it)
processed += 1
if processed >= next_report:
sys.stdout.write("\r")
sys.stdout.write(str(processed))
sys.stdout.write(" reads processed ...")
sys.stdout.flush()
next_report += 20000
comment = rec1.comment
if comment.find("NoMatch") >= 0:
continue
cb, sb_raw = get_cb_sb(comment)
if (not cb) or (not sb_raw):
sys.stderr.write("ERROR: Missing CB or SB tag in FASTQ comment\n")
sys.exit(1)
gid = 0
if use_sb:
# This is the critical part: no fallback to TP3_A on mismatch.
gid = gid_from_sb_raw(sample, sb_raw, sb_to_gid)
group_name = idx_to_group[gid] if use_sb else sample
comment1 = canonicalize_fastq_comment(sample, group_name, rec1.comment)
comment2 = canonicalize_fastq_comment(sample, group_name, rec2.comment)
canonical_cb, _ = get_cb_sb(comment1)
seq1 = str(rec1.seq); qual1 = str(rec1.qual)
seq2 = str(rec2.seq); qual2 = str(rec2.qual)
write_fastq_record(r1_fhs[gid], rec1.name, comment1, seq1, qual1)
write_fastq_record(r2_fhs[gid], rec2.name, comment2, seq2, qual2)
bc_sets[gid].add(canonical_cb)
sys.stdout.write("\rFinished processing ")
sys.stdout.write(str(processed))
sys.stdout.write(" reads...\n")
if unknown_count > 0:
sys.stdout.write("Unknown reads total: ")
sys.stdout.write(str(unknown_count))
sys.stdout.write(" (policy=")
sys.stdout.write(unknown_policy)
sys.stdout.write(")\n")
sys.stdout.write("SAM_RG_Header Writing\n")
for g in range(n_groups):
r1_fhs[g].close()
r2_fhs[g].close()
if use_sb:
gname = idx_to_group[g]
hpath = out_folder + "/SAM_RG_Header_" + sample + "_" + gname + ".tsv"
else:
hpath = out_folder + "/SAM_RG_Header_" + sample + ".tsv"
write_SAM_RG_Header(sample, lib_name, hpath, bc_sets[g])
unknown_r1.close()
unknown_r2.close()
if unknown_mode == 2:
write_SAM_RG_Header(sample, lib_name, out_folder + "/SAM_RG_Header_" + sample + "_Unknown.tsv", unknown_bcs)
sys.stdout.write("DONE\n")
return 0
def main():
# Unified interface (unchanged):
# DNA:
# codon run -plugin seq -release Split_ReadsV2.codon <Sample> <OutFolder> <LibName> dna <mo_map.tsv> <R1.fq.gz> <R2.fq.gz> [sb_group_map.tsv]
# RNA:
# codon run -plugin seq -release Split_ReadsV2.codon <Sample> <OutFolder> <LibName> rna - <R1.fq.gz> <R2.fq.gz> [sb_group_map.tsv]
if len(sys.argv) != 8 and len(sys.argv) != 9:
sys.stderr.write(
"Wrong call, exiting...\n"
"Usage:\n"
" DNA: codon run -plugin seq -release " + sys.argv[0] +
" <Sample> <OutFolder> <LibName> dna <mo_map.tsv> <R1> <R2> [sb_group_map.tsv]\n"
" RNA: codon run -plugin seq -release " + sys.argv[0] +
" <Sample> <OutFolder> <LibName> rna - <R1> <R2> [sb_group_map.tsv]\n\n"
)
sys.exit(1)
sample_name = sys.argv[1]
out = sys.argv[2]
library_name = sys.argv[3]
mode = sys.argv[4]
mo_map_path = sys.argv[5]
R1_path_arg = sys.argv[6]
R2_path_arg = sys.argv[7]
sb_map_path = ""
if len(sys.argv) == 9:
sb_map_path = sys.argv[8]
if mode == "dna":
MakeRecordNameList_DNA(
sample=sample_name,
out_folder=out,
R1_path=R1_path_arg,
R2_path=R2_path_arg,
lib_name=library_name,
mo_map_path=mo_map_path,
sb_group_map_path=sb_map_path,
unknown_policy="fail"
)
elif mode == "rna":
MakeRecordNameList_RNA(
sample=sample_name,
out_folder=out,
R1_path=R1_path_arg,
R2_path=R2_path_arg,
lib_name=library_name,
sb_group_map_path=sb_map_path,
unknown_policy="fail"
)
else:
sys.stderr.write("ERROR: mode must be dna|rna\n")
sys.exit(1)
if __name__ == "__main__":
main()