-
Notifications
You must be signed in to change notification settings - Fork 0
/
separate_barcodes.py
executable file
·72 lines (52 loc) · 2.14 KB
/
separate_barcodes.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
import pandas as pd
import pysam
import argparse
import logging
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='Saves reads below a alignment threshold and discards all others')
parser.add_argument('--human_reads', type=str, default=None)
parser.add_argument('--mouse_reads', type=str, default=None)
parser.add_argument('--ambiguous_reads', type=str, default=None)
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()
df_human = pd.read_csv(args.human_reads, sep="\t", header=None)
df_mouse = pd.read_csv(args.mouse_reads, sep="\t", header=None)
df_ambiguous = pd.read_csv(args.ambiguous_reads, sep="\t", header=None)
df_human.columns = ['read']
df_mouse.columns = ['read']
df_ambiguous.columns = ['read']
barcodes, species = [], []
for read in df_human['read']:
names = read.split('_')[1].split(',')
barcode = names[0] + names[1] + names[2]
barcodes.append(barcode)
species.append('human')
for read in df_mouse['read']:
names = read.split('_')[1].split(',')
barcode = names[0] + names[1] + names[2]
barcodes.append(barcode)
species.append('mouse')
for read in df_ambiguous['read']:
names = read.split('_')[1].split(',')
barcode = names[0] + names[1] + names[2]
barcodes.append(barcode)
species.append('ambiguous')
df_barcode = pd.DataFrame({'barcode': barcodes, 'species': species})
df_count = df_barcode.groupby(
['barcode', 'species']).size().reset_index(name='count')
df_count = df_count.pivot(index='barcode', columns='species',
values='count').fillna(0)
df_count = df_count.astype({'ambiguous': 'int', 'human': 'int', 'mouse': 'int'})
df_count.to_csv(f'{args.out_dir}/{args.out_name}.csv')
logging.info('Done!')
if __name__ == "__main__":
main()