-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSplitVDJbam.py
76 lines (54 loc) · 2.1 KB
/
SplitVDJbam.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
import pysam
import json
import argparse
import os
import logging
# Usage:
# python SplitVDJbam.py --bam /home/chensy/Dual/2.Result/scTCR-Seq/vdj_v1_cd8/outs/all_contig.bam --list /home/chensy/Dual/2.Result/scTCR-Seq/vdj_v1_cd8/outs/cell_barcodes.json --out ./
def CommandLineParser():
parser = argparse.ArgumentParser()
parser.add_argument("--bam", required=True, help="BAM file")
parser.add_argument("--list", required=True, help="Barcode list json")
parser.add_argument("--out", required=True, help="Output folder")
parser.add_argument("--file", required=True, help="Generated Manifest file")
return parser.parse_args()
if __name__ == "__main__":
args = CommandLineParser()
args = { arg:getattr(args, arg) for arg in vars(args) }
LOG_FORMAT = "%(asctime)s - %(levelname)s - %(message)s"
logging.basicConfig(level=logging.DEBUG, format=LOG_FORMAT)
bam = args['bam']
bc_liust = args['list']
out_folder = os.path.abspath(args['out'])
if not os.path.exists(out_folder):
os.system(f"mkdir -p {out_folder}")
logging.info("Loading Barcode list")
with open(bc_liust) as handle:
bc_list = set(json.load(handle))
logging.info("Loading BAM file")
rds = [
(rd.get_tag('CB'), rd.seq, rd.qual) for rd in pysam.AlignmentFile(bam) if rd.get_tag('CB') in bc_list
]
logging.info("Sorting Cell Barcode")
rds.sort(key= lambda x: x[0])
logging.info("Writing to files")
cur = 'START'
manifest = []
for idx, (bc, seq, qual) in enumerate(rds):
if seq == 'None':
continue
if cur == 'START':
cur = bc
res = []
if bc != cur:
with open(f"{out_folder}/{cur}.fastq", 'w') as handle:
handle.write("".join(res))
manifest.append(f"{cur}\t{out_folder}/{cur}.fastq\tNone\n")
res = []
cur = bc
res.append(f"@{bc}_{idx}\n{seq}\n+\n{qual}\n")
else:
#TODO(chens) there is a miss cell
pass
with open(args['file'], 'w') as handle:
handle.write("".join(manifest))