-
Notifications
You must be signed in to change notification settings - Fork 0
/
PySamParser.py
52 lines (41 loc) · 1.36 KB
/
PySamParser.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
import pandas as pd
import pyranges as pr
import pysam
from functools import lru_cache
from Bio.Seq import Seq
def rev_comp(seq):
return str(Seq(seq).reverse_complement())
class PySamParser:
def __init__(self, filename):
self.filename = filename
def read_sam(self):
with pysam.AlignmentFile(self.filename, "r") as samfile:
for read in samfile:
yield read
@property
@lru_cache(maxsize=None)
def ranges(self):
data = []
for read in self.read_sam():
if read.is_reverse == 0:
strand = "+"
sequence = read.query_sequence
elif read.is_reverse == 1:
strand = "-"
sequence = rev_comp(read.query_sequence)
else:
strand = "."
sequence = None
interval_dict = {
"Chromosome": read.reference_name,
"Start": read.reference_start,
"End": read.reference_end,
"Mapped": True if not read.is_unmapped else False,
"Strand": strand,
"Barcode": sequence,
"Mismatches": (read.get_tag("NM") if read.has_tag("NM") else "0"),
}
data.append(interval_dict)
df = pd.DataFrame(data)
ranges = pr.PyRanges(df)
return ranges