-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathx_statistic.py
291 lines (256 loc) · 10.9 KB
/
x_statistic.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
##10/16/2018
##@@author: Simon (Chong) Chu, DBMI, Harvard Medical School
##@@contact: chong_chu@hms.harvard.edu
import os
import sys
import pysam
from subprocess import *
from multiprocessing import Pool
AVE_MOLECULE_SIZE=100000 #suppose average molecule size if 100K
def unwrap_self_molecule_statistic_by_MI(arg, **kwarg):
return MoleculeInfo.run_molecule_statistic_for_MIs(*arg, **kwarg)
def unwrap_self_molecule_statistic_by_BX_MI(arg, **kwarg):
return MoleculeInfo.run_molecule_statistic_for_BX_MI(*arg, **kwarg)
#
class MoleculeInfo():
def __init__(self, sf_bam, working_folder, n_jobs):
self._sf_bam = sf_bam
self._working_folder = working_folder
self._n_jobs = n_jobs
def run_molecule_statistic_for_MIs(self, record):
istart=int(record[0][0])
iend=int(record[0][1])
sf_bam=record[1]
sf_tmp_out=record[2]
samfile = pysam.AlignmentFile(sf_bam, "rb")
barcodes = samfile.references
m_statistic={}
for s_mi in barcodes[istart:iend+1]:
m_chrs={}
l_algnmts=[]
for alignmt in samfile.fetch(s_mi):
ch = "*"
if alignmt.has_tag("ch"):
ch = alignmt.get_tag("ch")
if ch not in m_chrs:
m_chrs[ch]=1
else:
m_chrs[ch] += 1
cp = 0
if alignmt.has_tag("cp"):
cp = alignmt.get_tag("cp")
l_algnmts.append((ch, cp))
max_chrm, i_left_pos, i_right_pos=self.statistic_one_molecule(m_chrs, l_algnmts)
if max_chrm=="*":
continue
m_statistic[s_mi]=(i_left_pos, i_right_pos)
samfile.close()
with open(sf_tmp_out, "w") as fout_tmp:
for s_mi in m_statistic:
i_size=m_statistic[s_mi][1]-m_statistic[s_mi][0]
s_rcd="{0}\t{1}\t{2}\t{3}\n".format(s_mi, m_statistic[s_mi][0], m_statistic[s_mi][1], i_size)
fout_tmp.write(s_rcd)
def run_molecule_statistic_for_BX_MI(self, record):
istart=int(record[0][0])
iend=int(record[0][1])
sf_bam=record[1]
sf_tmp_out=record[2]
samfile = pysam.AlignmentFile(sf_bam, "rb")
barcodes = samfile.references
#print barcodes[0], istart, iend ###################################################
with open(sf_tmp_out, "w") as fout_tmp:
for s_bx in barcodes[istart:iend+1]:
m_chrs={}
m_algnmts={}
#m_statistic = {} #for each BX barcode
for alignmt in samfile.fetch(s_bx):#all the reads of the same BX barcode
####each BX should have several MI barcode
#
s_mi="null"
if alignmt.has_tag("MI"):
s_mi=str(alignmt.get_tag("MI"))
if s_mi=="null":#skip the alignments without a "MI" tag
continue
#
if s_mi not in m_chrs:
m_chrs[s_mi]={}
if s_mi not in m_algnmts:
m_algnmts[s_mi]=[]
ch = "*"
if alignmt.has_tag("ch"):
ch = alignmt.get_tag("ch")
if ch not in m_chrs[s_mi]:
m_chrs[s_mi][ch]=1
else:
m_chrs[s_mi][ch] += 1
cp = 0
if alignmt.has_tag("cp"):
cp = alignmt.get_tag("cp")
m_algnmts[s_mi].append((ch, cp))
for s_mi in m_algnmts:
if s_mi not in m_chrs:
continue
max_chrm, i_left_pos, i_right_pos=self.statistic_one_molecule(m_chrs[s_mi], m_algnmts[s_mi])
if max_chrm=="*":
continue
# if s_mi not in m_statistic:
# m_statistic[s_mi]=[]
#m_statistic[s_mi].append((s_bx, i_left_pos, i_right_pos))
i_size=i_right_pos-i_left_pos
n_reads=len(m_algnmts[s_mi])
s_rcd = "{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\n".format(s_mi, s_bx, max_chrm, i_left_pos,
i_right_pos, i_size, n_reads)
fout_tmp.write(s_rcd)
#write to file
samfile.close()
# with open(sf_tmp_out, "w") as fout_tmp:
# for s_mi in m_statistic:
# for rcd in m_statistic[s_mi]:
# s_bx=rcd[0]
# i_left=rcd[1]
# i_right=rcd[2]
# i_size=i_right-i_left
# s_rcd="{0}\t{1}\t{2}\t{3}\t{4}\n".format(s_mi, s_bx, i_left, i_right, i_size)
# fout_tmp.write(s_rcd)
#
def statistic_one_molecule(self, m_chrs, l_algnmts):
i_max = 0
max_chrm = ""
for tmp_chrm in m_chrs:
if m_chrs[tmp_chrm] > i_max:
i_max = m_chrs[tmp_chrm]
max_chrm = tmp_chrm
if max_chrm == "*":
#continue
return max_chrm, -1, -1
# find cluster, and then the start and end position of the cluster
l_tmp_pos = []
for tmp_rcd in l_algnmts:
if tmp_rcd[0] == max_chrm:
l_tmp_pos.append(int(tmp_rcd[1]))
l_tmp_pos.sort()
n_size = len(l_tmp_pos)
i_medium = n_size / 2
medium_pos = l_tmp_pos[i_medium]
i_left_pos = l_tmp_pos[0]
i_right_pos = l_tmp_pos[-1]
for i in range(n_size):
if abs(l_tmp_pos[i] - medium_pos) < AVE_MOLECULE_SIZE:
i_left_pos = int(l_tmp_pos[i])
break
for tmp_pos in reversed(l_tmp_pos):
if abs(tmp_pos - medium_pos) < AVE_MOLECULE_SIZE:
i_right_pos = tmp_pos
break
#m_statistic[s_mi] = (i_left_pos, i_right_pos)
return max_chrm, i_left_pos, i_right_pos
def get_molecule_size(self, sf_out):
#first get all the MI from the head
samfile = pysam.AlignmentFile(self._sf_bam, "rb")
barcodes = samfile.references
n_barcodes=len(barcodes)
l_regions = []
n_ave = n_barcodes / self._n_jobs
istart = 0
iend = 0
while istart < (self._n_jobs * n_ave):
iend = istart + n_ave
l_regions.append((istart, iend - 1))
istart = iend
if iend < (n_barcodes - 1):
l_regions.append((iend, n_barcodes - 1))
samfile.close()
#
l_records=[]
for rcd in l_regions:
sf_tmp_out = self._working_folder + "{0}_{1}.molecule_stat.txt".format(rcd[0], rcd[1])
l_records.append((rcd, self._sf_bam, sf_tmp_out))
pool = Pool(self._n_jobs)
pool.map(unwrap_self_molecule_statistic_by_MI, zip([self] * len(l_records), l_records), 1)
pool.close()
pool.join()
#merge the tmp results:
with open(sf_out, "w") as fout_rslt:
fout_rslt.write("#MI\tChrom\tMolecule-Start\tMolecule-End\tMolecule-length\n")
for rcd in l_regions:
sf_tmp_out = self._working_folder + "{0}_{1}.molecule_stat.txt".format(rcd[0], rcd[1])
if os.path.isfile(sf_tmp_out)==True:
with open(sf_tmp_out) as fin_tmp:
for line in fin_tmp:
fout_rslt.write(line)
####
#This is assume the bam is barcode (BX) based, and already sorted
def get_molecule_size_BX_bam(self, sf_out):
# first get all the MI from the head
samfile = pysam.AlignmentFile(self._sf_bam, "rb")
barcodes = samfile.references
n_barcodes = len(barcodes)
l_regions = []
n_ave = n_barcodes / self._n_jobs
istart = 0
iend = 0
while istart < (self._n_jobs * n_ave):
iend = istart + n_ave
l_regions.append((istart, iend - 1))
istart = iend
if iend < (n_barcodes - 1):
l_regions.append((iend, n_barcodes - 1))
samfile.close()
#
l_records = []
for rcd in l_regions:
sf_tmp_out = self._working_folder + "{0}_{1}.molecule_stat.txt".format(rcd[0], rcd[1])
l_records.append((rcd, self._sf_bam, sf_tmp_out))
#record=(rcd, self._sf_bam, sf_tmp_out)
#self.run_molecule_statistic_for_BX_MI(record)
pool = Pool(self._n_jobs)
pool.map(unwrap_self_molecule_statistic_by_BX_MI, zip([self] * len(l_records), l_records), 1)
pool.close()
pool.join()
# merge the tmp results:
with open(sf_out, "w") as fout_rslt:
fout_rslt.write("#MI\tBX\tChrm\tMolecule-Start\tMolecule-End\tMolecule-length\tNum-of-reads\n")
for rcd in l_regions:
sf_tmp_out = self._working_folder + "{0}_{1}.molecule_stat.txt".format(rcd[0], rcd[1])
if os.path.isfile(sf_tmp_out) == True:
with open(sf_tmp_out) as fin_tmp:
for line in fin_tmp:
fout_rslt.write(line)
def get_molecule_size_single_core(self, sf_out):
samfile = pysam.AlignmentFile(self._sf_bam, "rb")
m_chrs = {}
l_algnmts = []
pre_mi=None
with open(sf_out,"w") as fout_rslt:
fout_rslt.write("#MI\tMolecule-Start\tMolecule-End\tMolecule-length\n")
for alignmt in samfile.fetch(until_eof=True):#this is retrieve all the reads, and no need to be sorted
cur_mi=alignmt.reference_id
if pre_mi!=None and pre_mi!=cur_mi:
max_chrm, i_left_pos, i_right_pos = self.statistic_one_molecule(m_chrs, l_algnmts)
if max_chrm!="*":
i_size = i_right_pos - i_left_pos
s_rcd = "{0}\t{1}\t{2}\t{3}\n".format(pre_mi, i_left_pos, i_right_pos, i_size)
fout_rslt.write(s_rcd)
m_chrs.clear()
del l_algnmts[:]
pre_mi=cur_mi
ch = "*"
if alignmt.has_tag("ch"):
ch = alignmt.get_tag("ch")
if ch not in m_chrs:
m_chrs[ch] = 1
else:
m_chrs[ch] += 1
cp = 0
if alignmt.has_tag("cp"):
cp = alignmt.get_tag("cp")
l_algnmts.append((ch, cp))
#save the last one
max_chrm, i_left_pos, i_right_pos = self.statistic_one_molecule(m_chrs, l_algnmts)
if max_chrm != "*":
i_size = i_right_pos - i_left_pos
s_rcd = "{0}\t{1}\t{2}\t{3}\n".format(pre_mi, i_left_pos, i_right_pos, i_size)
fout_rslt.write(s_rcd)
samfile.close()
####
####