-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsum_barcodes.py
55 lines (43 loc) · 1.49 KB
/
sum_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
import anndata as ad
import sys
import argparse
def get_options():
parser = argparse.ArgumentParser(prog='sum_barcodes.py')
parser.add_argument('-s', '--sample_name', help='Sample name', required=True)
parser.add_argument('-i', '--input_files', help='Count files to be considered', nargs='+')
parser.add_argument('-b', '--barcodes', help='Non-default barcode table')
parser.add_argument('-X', '--Xdata', help='Layer to be used as anndata.X', default='tn5')
options = parser.parse_args()
return options
def main():
options = get_options()
barcodes = {'CGTACTAG':'tn5',
'TCCTGAGC':'tn5',
'TCATGAGC':'tn5',
'CCTGAGAT':'tn5',
'TAAGGCGA':'tnH',
'GCTACGCT':'tnH',
'AGGCTCCG':'tnH',
'CTGCGCAT':'tnH'
}
if options.barcodes:
barcodes = {}
for line in open(options.barcodes):
t = line.split()
barcodes[t[0]] = t[1]
ad_l = dict.fromkeys(barcodes.values())
for l in ad_l:
bc_l = [x for x in barcodes if barcodes[x] == l]
layer_files = [y for x in bc_l for y in options.input_files if x in y]
ad_tmp = ad.read(layer_files[0])
for f in layer_files[1:]:
_X = ad.read(f)
_X = _X[ad_tmp.obs_names]
ad_tmp.X = ad_tmp.X + _X.X
ad_l[l] = ad_tmp.copy()
adata = ad_l[options.Xdata].copy()
for l in ad_l:
adata.layers[l] = ad_l[l].X
adata.write(f'{options.sample_name}.h5ad')
if __name__ == '__main__':
main()