-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelper.py
376 lines (310 loc) · 11.5 KB
/
helper.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#!/usr/bin/env python
# =========================================================================
# Copyright (C) Xue Zou ([email protected])
# =========================================================================
import logging
import random
import os
import subprocess
import tempfile
from misc_tools.Pipe import Pipe
from misc_tools.Rex import Rex
from misc_tools.Translation import Translation
rex = Rex()
#######################################
class Variant:
def __init__(self, fields):
self.Chr = fields[0]
self.genomicPos = int(fields[1]) - 1 # convert to 0-based
self.ref = fields[3]
self.alt = fields[4]
genotype = fields[9]
self.genotype = None
if rex.find("(\d)[\|\/](\d)", genotype):
self.genotype = (int(rex[1]), int(rex[2]))
if self.genotype[0] not in {0, 1} or self.genotype[1] not in {0, 1}:
self.genotype = None
# cat HG00096.no_chr.content.SNPs.filtered.vcf.gz | gunzip | awk '{print $10}' | sort | uniq -c
# 74304603 0|0
# 1071282 0|1
# 1065885 1|0
# 1376362 1|1
# cat 123375.no_chr.content.SNPs.filtered.vcf.gz | gunzip | awk '{print $10}' | sort | uniq -c
# 21194 ./.
# 2917072 0/0
# 2354362 0/1
# cat NA12878.no_chr.content.SNPs.filtered.vcf.gz | gunzip | awk '{print $10}' | sort | uniq -c
# 5947 0/1
# 976007 0|1
# 986082 1|0
# 1289007 1|1
def isOK(self):
return self.genotype is not None
def isHet(self):
return self.genotype[0] != self.genotype[1]
def isHomozygousAlt(self):
return self.genotype[0] > 0 and self.genotype[1] > 0
def printRead(header, seq, qual, FH):
print(header + "\n" + seq + "\n+\n" + qual, file=FH)
def tabix_regions(
regions, line_processor, target_file_path, comment_char="#", region_prefix=""
):
logging.info(
f"Start tabix extraction of {len(regions)} regions from file {target_file_path}"
)
if region_prefix != "":
regions = list(map(lambda x: region_prefix + x, regions))
region_to_results = {}
with tempfile.NamedTemporaryFile(mode="w") as file:
for region in regions:
chr, rest = region.split(":")
start, end = rest.split("-")
file.write(f"{chr}\t{start}\t{end}\n")
file.flush()
command = f"tabix --separate-regions {target_file_path} --regions {file.name}"
process = subprocess.Popen(command, stdout=subprocess.PIPE, shell=True)
records = []
for line in iter(process.stdout.readline, b""):
line = line.decode("utf-8").rstrip()
if len(line) == 0:
continue
# start accumulating new region
if line.startswith(comment_char):
region_str = line[1:].strip(region_prefix)
records = []
region_to_results[region_str] = records
continue
result = line_processor(line)
if result is not None:
records.append(result)
logging.info(f"Got {len(region_to_results)} / {len(regions)} regions with data")
return region_to_results
def variant_processor_hets(line):
#########
# this function is used to fetch variants from .vcf
#########
fields = line.rstrip().split()
if len(fields) != 10:
raise Exception("Expecting 10 fields in VCF file")
variant = Variant(fields)
if not variant.isOK():
return None
# not het and not homoalt
if not (variant.isHet() and not variant.isHomozygousAlt()):
return None
if not (len(variant.ref) == 1 and len(variant.alt) == 1):
return None
return variant
def variant_processor_SNPs(line):
#########
# this function is used to fetch variants from .vcf
#########
fields = line.rstrip().split()
if len(fields) != 10:
raise Exception("Expecting 10 fields in VCF file")
variant = Variant(fields)
if not variant.isOK():
return None
# not het and not homoalt
if not (len(variant.ref) == 1 and len(variant.alt) == 1):
return None
return variant
def sam_data_processor(line):
#########
# this function is used to fetch data from .sam.gz
# returns ( (start_pos, template_len), quality_string )
# reference: https://samtools.github.io/hts-specs/SAMv1.pdf
# fields[3]: start POS
# fields[8]: template length
# fields[10]: quality string
#########
fields = line.rstrip().split()
if len(fields) < 11:
return None
pos1 = int(fields[3])
tlen = int(fields[8])
quality_string = fields[10]
if tlen <= 0:
return None
result = ((pos1, tlen), quality_string)
return result
def simRead_patmat(
input_ref_seq,
input_alt_seq,
input_ref_seq_rev_comp,
input_alt_seq_rev_comp,
qual1,
qual2,
fragLen,
):
#####################################################################
# transcript length
L = len(input_ref_seq)
qual1_len = len(qual1)
qual2_len = len(qual2)
# transcript seq index start/end
L_end = L - 1
L_start = 0
# fragLen: actual read length drawn from SAM
if L_end < fragLen or L_end < qual1_len or L_end < qual2_len:
return None
# transcript coord
lastStart = min(L_end - fragLen, L_end - qual1_len, L_end - qual2_len) # 20
start1 = random.randrange(lastStart + 1) # 10
end1 = start1 + qual1_len # rec1.readLen # 10+75 = 85
LEN1 = abs(end1 - start1)
end2 = start1 + fragLen # 10+80 = 90
start2 = end2 - qual2_len # rec2.readLen # 90-75 = 15
LEN2 = abs(end2 - start2)
assert start1 >= L_start
assert end1 <= L_end
assert start2 >= L_start
assert end2 <= L_end
assert len(qual1) == LEN1
assert len(qual2) == LEN2
######## forward strand, same sequence pos for mat/aptf fov
simulated_ref_seq = input_ref_seq[start1:end1]
simulated_alt_seq = input_alt_seq[start1:end1]
######## reverse strand, same sequence pos for mat/apt rev
simulated_ref_seq_rev = input_ref_seq_rev_comp[L - end2 : L - start2]
simulated_alt_seq_rev = input_alt_seq_rev_comp[L - end2 : L - start2]
assert len(qual1) == len(simulated_ref_seq)
assert len(qual2) == len(simulated_ref_seq_rev)
return (
simulated_ref_seq,
simulated_ref_seq_rev,
simulated_alt_seq,
simulated_alt_seq_rev,
)
def posTlen_to_fragLen(gene, pos1_tlen_to_count, readLen):
transcript_id_to_mapped_lengths = {}
# logging.debug(
# ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> filtering : valid start/end pos of SAM records map to transcript"
# )
for i in range(gene.getNumTranscripts()):
transcript = gene.getIthTranscript(i)
mapped_lengths = []
pos_cache = {}
def mapPos(pos):
nonlocal transcript
nonlocal pos_cache
if pos not in pos_cache:
pos_cache[pos] = transcript.mapToTranscript(pos)
return pos_cache[pos]
for pos1_tlen, count in pos1_tlen_to_count.items():
pos1, tlen = pos1_tlen
# get transcript coordiante position
begin = mapPos(pos1)
# filter out sites whose both start/end could not map to transcript
if begin < 0:
continue
end = mapPos(pos1 + tlen)
if end < 0:
continue
mapped_length = abs(end - begin)
# logging.debug(
# f"transcript {i+1} - {transcript.getID()}:{pos1_tlen} mapped start,end: ({begin},{end}) fragLen {mapped_length}"
# )
if mapped_length < readLen:
continue
mapped_lengths.extend([mapped_length for i in range(count)])
if len(mapped_lengths) > 0:
mapped_lengths.sort()
transcript_id_to_mapped_lengths[transcript.getID()] = mapped_lengths
return transcript_id_to_mapped_lengths
def twoBitID(chr, begin, end):
return f"{chr}:{begin}-{end}"
def load_twoBit_TranscriptSeqs(gene, genome, path):
Chr = gene.getSubstrate()
strand = gene.getStrand()
twobitReads = []
for transcript in gene.transcripts:
transcript.exons = transcript.getRawExons()
for exon in transcript.rawExons:
twobitReads.append(f"{twoBitID(Chr, exon.begin, exon.end)}\n")
with open("/tmp/2bitinput", "w") as f:
f.writelines(twobitReads)
id_to_seq = run2bitBatch(path, "/tmp/2bitinput", genome)
for transcript in gene.transcripts:
transcript.sequence = ""
transcript.exons = transcript.getRawExons()
for exon in transcript.rawExons:
exon.sequence = id_to_seq[twoBitID(Chr, exon.begin, exon.end)]
exon.sequence = exon.sequence.upper()
if strand == "-":
exon.sequence = Translation.reverseComplement(exon.sequence)
if "N" in exon.sequence:
raise Exception(
"N FOUND: Chr="
+ Chr
+ "begin="
+ str(exon.begin)
+ "end="
+ str(exon.end)
)
transcript.sequence += exon.sequence
def run2bitBatch(path, inputFile, genome):
cmd = f"{os.path.join(path, 'twoBitToFa')} -seqList={inputFile} {genome} stdout"
logging.debug(f"twobit command {cmd}")
output = Pipe.run(cmd)
lines = output.rstrip().split("\n")
id_to_seq = {}
id = None
seq = None
for line in lines:
if line.startswith(">"):
if id is not None:
id_to_seq[id] = seq
id = line.strip(">")
seq = ""
continue
seq += line
if id is not None:
id_to_seq[id] = seq
return id_to_seq
def constructTwoBitInput(genes, file):
count = 0
with open(file, "w") as f:
for gene in genes:
chr = gene.getSubstrate()
for transcript in gene.transcripts:
transcript.exons = transcript.getRawExons()
for exon in transcript.rawExons:
f.write(f"{twoBitID(chr, exon.begin, exon.end)}\n")
count += 1
logging.debug(f"generated two bit input of {count} lines")
def annotate_transcripts(genes, id_to_seq):
for gene in genes:
Chr = gene.getSubstrate()
strand = gene.getStrand()
for transcript in gene.transcripts:
transcript.sequence = ""
transcript.exons = transcript.getRawExons()
for exon in transcript.rawExons:
exon.sequence = id_to_seq[twoBitID(Chr, exon.begin, exon.end)]
exon.sequence = exon.sequence.upper()
if strand == "-":
exon.sequence = Translation.reverseComplement(exon.sequence)
if "N" in exon.sequence:
raise Exception(
"N FOUND: Chr="
+ Chr
+ "begin="
+ str(exon.begin)
+ "end="
+ str(exon.end)
)
transcript.sequence += exon.sequence
def chunk_iter(iter, n):
"""Yield successive n-sized chunks from iter."""
res = []
try:
while True:
while len(res) < n:
v = next(iter)
res.append(v)
yield res
res = []
except StopIteration:
if len(res) > 0:
yield res