-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter_bam_by_barcode.py
59 lines (45 loc) · 1.48 KB
/
filter_bam_by_barcode.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
import os
import pysam
import pandas as pd
import argparse
import logging
import warnings
warnings.filterwarnings("ignore")
logging.basicConfig(
format="%(asctime)s %(levelname)-8s %(message)s",
level=logging.INFO,
datefmt="%Y-%m-%d %H:%M:%S",
)
def parse_args():
parser = argparse.ArgumentParser(
description="This script filters bam file by a list of barcodes",
formatter_class=argparse.RawTextHelpFormatter,
)
# Required parameters
parser.add_argument("--bam_file", type=str, default=None)
parser.add_argument("--barcode_file", type=str, default=None)
parser.add_argument("--bc_tag", type=str, default="CB")
parser.add_argument("--out_dir", type=str, default=None)
parser.add_argument("--out_name", type=str, default=None)
return parser.parse_args()
def main():
args = parse_args()
infile = pysam.AlignmentFile(args.bam_file, "rb")
outfile = pysam.AlignmentFile(
f"{args.out_dir}/{args.out_name}.bam", "wb",
template=infile
)
logging.info("Reading barcode file")
df = pd.read_csv(args.barcode_file)
sel_barcodes = set(df['barcode'].tolist())
logging.info(f"Number of valid barcodes: {len(sel_barcodes)}")
iter = infile.fetch(until_eof=True)
for read in iter:
barcode = read.get_tag(args.bc_tag)
if barcode in sel_barcodes:
outfile.write(read)
infile.close()
outfile.close()
logging.info(f"Done!")
if __name__ == "__main__":
main()