-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPAMProcessor.py
More file actions
97 lines (78 loc) · 3.33 KB
/
Copy pathPAMProcessor.py
File metadata and controls
97 lines (78 loc) · 3.33 KB
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
import re
class PAMProcessor:
def __init__(self, records, pam, direction):
self.records = records
self.pam = pam.replace("N", "[ATCG]")
self.direction = direction
def get_sequence(self, row):
sequence = self.records[row.Chromosome].seq[row.Start : row.End]
if row.Strand == "-":
sequence = sequence.reverse_complement()
return sequence
def get_strand(self, strand_symbol):
# Normalize the input to handle common variations
normalized_strand = str(strand_symbol).lower().strip()
if normalized_strand in ("+", "1", "+1", "fwd", "forward"):
return 1
elif normalized_strand in ("-", "-1", "rev", "reverse"):
return -1
else:
raise ValueError(f"Unrecognized strand symbol: {strand_symbol}")
class GuideFinder:
def __init__(self, records, pam, direction, length):
self.records = records
self.pam = pam.replace("N", "[ATCG]")
self.direction = direction
self.length = length
def find_guides_from_pam(self):
guide_sequences = []
for record_id, record in self.records.items():
for sequence in [record.seq, record.seq.reverse_complement()]:
sequence_str = str(sequence)
for match in re.finditer(self.pam, sequence_str):
start = match.start()
end = match.end()
if self.direction == "downstream":
guide_sequence = sequence_str[
max(0, start - self.length) : start
]
elif self.direction == "upstream":
guide_sequence = sequence_str[
end : min(end + self.length, len(sequence_str))
]
else:
raise ValueError("Direction must be 'upstream' or 'downstream'")
guide_sequences.append(guide_sequence)
return guide_sequences
class PAMFinder(PAMProcessor):
def __init__(self, records, pam, direction):
super().__init__(records, pam, direction)
self.pam_length = len(pam)
def get_pam_seq(self, row):
sequence = self.get_sequence(row)
strand = self.get_strand(row.Strand)
if self.direction == "upstream":
pam_sequence = (
self.records[row.Chromosome].seq[row.End : row.End + self.pam_length]
if strand == 1
else self.records[row.Chromosome].seq[
row.Start - self.pam_length : row.Start
]
# .reverse_complement()
)
elif self.direction == "downstream":
pam_sequence = (
self.records[row.Chromosome].seq[row.End : row.End + self.pam_length]
if strand == 1
else self.records[row.Chromosome].seq[
row.Start - self.pam_length : row.Start
]
# .reverse_complement()
)
else:
raise ValueError("direction must be 'upstream' or 'downstream'")
if strand == -1:
pam_sequence = pam_sequence.reverse_complement()
return str(pam_sequence)
def pam_matches(self, sequence):
return bool(re.search(self.pam, sequence))