-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcount_coverage_btp.py
129 lines (95 loc) · 3.3 KB
/
count_coverage_btp.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
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
import os, sys
import pysam, argparse
import numpy as np
import copy
bam_dir = '/data/allbam'
btp_dir = bam_dir+'/190916_work'
#bam_list = os.listdir(bam_dir)
#bam_list = [file for file in bam_list if file.endswith(".bam")]
btp_list = os.listdir(btp_dir)
btp_list = [file for file in btp_list if file.endswith(".bed")]
output_50x = []
output_100x = []
for btp in btp_list :
btpname = btp[:btp.find('.')]
print(btpname+' start!')
output_50x.append({})
output_100x.append({})
btp_path = btp_dir+'/'+btp
slfile = open(btp_dir+'/sample.'+btpname, 'r')
sl_ls = slfile.readlines()
bam_list = []
for sl_l in sl_ls :
bam_list.append(sl_l[:-1]+'.bwamem.sorted.dedup.realn.recal.dedup.bam')
for bnum, bam in enumerate(bam_list) :
# if bnum > 2 :
# break
sam_path = bam_dir+'/'+bam
if not os.path.isfile(sam_path) :
continue
samfile = pysam.AlignmentFile(sam_path, "rb")
btpfile = open(btp_path, 'r')
lines = btpfile.readlines()
bamname = bam[:bam.find('.')]
output_50x[-1][bamname] = {}
output_100x[-1][bamname] = {}
for lnum, line in enumerate(lines) :
splited = line.split('\t')
contig = splited[0]
start = int(splited[1])
stop = int(splited[2])
gene = splited[3]
gene = gene[:-1]
print('\r', bamname+', '+gene+' : '+str(bnum+1)+' / '+str(len(bam_list))+', '+str(lnum+1)+' / '+str(len(lines)), end='\r')
sys.stdout.write("\033[K")
cv_original = np.array(samfile.count_coverage(contig, start=start, stop=stop))
cv_50x = cv_original.sum(axis=0)
cv_100x = copy.deepcopy(cv_50x)
cv_50x = [1 if ce > 50 else 0 for ce in cv_50x]
cv_100x = [1 if ce > 100 else 0 for ce in cv_100x]
num_50x = sum(cv_50x)
num_100x = sum(cv_100x)
if gene in output_50x[-1][bamname] :
output_50x[-1][bamname][gene]['num'] += num_50x
output_50x[-1][bamname][gene]['len'] += len(cv_50x)
else :
output_50x[-1][bamname][gene] = {}
output_50x[-1][bamname][gene]['num'] = num_50x
output_50x[-1][bamname][gene]['len'] = len(cv_50x)
if gene in output_100x[-1][bamname] :
output_100x[-1][bamname][gene]['num'] += num_100x
output_100x[-1][bamname][gene]['len'] += len(cv_100x)
else :
output_100x[-1][bamname][gene] = {}
output_100x[-1][bamname][gene]['num'] = num_100x
output_100x[-1][bamname][gene]['len'] = len(cv_100x)
btpfile.close()
samfile.close()
outfile_50x = open(btp_dir+'/'+btpname+'.50x.tsv', 'w')
outfile_100x = open(btp_dir+'/'+btpname+'.100x.tsv', 'w')
key_50x = output_50x[-1].keys()
key_100x = output_50x[-1].keys()
gene_50x = output_50x[-1][list(key_50x)[0]].keys()
gene_100x = output_100x[-1][list(key_100x)[0]].keys()
outfile_50x.write('GENE')
for bamn in key_50x :
outfile_50x.write('\t'+bamn)
outfile_50x.write('\n')
for genn in gene_50x :
outfile_50x.write(genn)
for bamn in key_50x :
cnum = output_50x[-1][bamn][genn]['num'] / output_50x[-1][bamn][genn]['len']
outfile_50x.write('\t'+str(cnum))
outfile_50x.write('\n')
outfile_50x.close()
outfile_100x.write('GENE')
for bamn in key_100x :
outfile_100x.write('\t'+bamn)
outfile_100x.write('\n')
for genn in gene_100x :
outfile_100x.write(genn)
for bamn in key_100x :
cnum = output_100x[-1][bamn][genn]['num'] / output_100x[-1][bamn][genn]['len']
outfile_100x.write('\t'+str(cnum))
outfile_100x.write('\n')
outfile_100x.close()