-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpydna_helpers.py
More file actions
360 lines (319 loc) · 11.9 KB
/
pydna_helpers.py
File metadata and controls
360 lines (319 loc) · 11.9 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
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
from Bio import SeqIO
from Bio.Seq import Seq
from Bio.SeqFeature import FeatureLocation, SeqFeature
from Bio.SeqRecord import SeqRecord
from pydna.assembly2 import homologous_recombination_integration
from pydna.dseqrecord import Dseqrecord
from pydna.genbank import Genbank
def fetch_genome(accession, email):
"Fetches genome from GenBank"
gb = Genbank(email)
genome = gb.nucleotide(accession) # Returns Dseqrecord (pydna)
return genome
def get_plus_strand_cds_stops(genome):
"Defines non-disruptive insertion positions by identifying CDS sequences and stop codons (for +strand CDS features) - potential for upgrade with (-) strands too"
stops = []
for seq in genome.features:
if seq.type == "CDS" and int(getattr(seq.location, "strand", 0) or 0) == 1:
stops.append(int(seq.location.end))
return stops
def extract_homology_arms(genome, insert_pos, left_len, right_len):
"Extracts left and right homology arms around insertion position"
if insert_pos < left_len or insert_pos + right_len > len(genome.seq):
return None
left_h_arm = str(genome.seq[insert_pos - left_len : insert_pos])
right_h_arm = str(genome.seq[insert_pos : insert_pos + right_len])
return left_h_arm, right_h_arm
def count_exact_matches(haystack, needle):
""" "Counts overlapping occurrences using a 'needle in haystack' approach:
haystack = genome; needle = left/right_h_arm
"""
count = 0
start = 0
while True:
pos = haystack.find(needle, start)
if pos == -1:
break
count += 1
start = pos + 1
return count
def score_homology_arm_pydna(
genome_dsr, arm_str, use_subsets: bool, subset_k: int, left_len: int, right_len: int
):
#### Scores a homology arm using pydna's dsDNA functions
total = 0
# Create Dseqrecord from arm string for pydna operations
arm_dsr = Dseqrecord(arm_str, circular=False)
genome_str = str(genome_dsr.seq)
# Count full-length matches
# Forward strand
total += count_exact_matches(genome_str, arm_str)
# Reverse complementary sequence counts
# A recombination event could use the plus strand or the minus strand as template
# So we must check both directions to know if the homology arm is unique
arm_rc_str = str(arm_dsr.rc().seq)
total += count_exact_matches(genome_str, arm_rc_str)
# But if there are many shorter perfect matches scattered across the genome,
# there’s a chance of unintended recombination or micro-homology-mediated events.
# Count subset matches if "True"
if use_subsets and 0 < subset_k <= len(arm_str):
for seq in (arm_str, arm_rc_str):
for i in range(0, len(seq) - subset_k + 1):
total += (subset_k / min(left_len, right_len)) * count_exact_matches(
str(genome_dsr.seq), seq[i : i + subset_k]
)
# multiply by a coefficient because finding a subset match is better than finding an exact one
return total
def score_candidate(
genome_dsr,
left_h,
right_h,
count_subsets: bool,
subset_k: int,
left_len: int,
right_len: int,
):
### Score a candidate by summing scores of both homology arms using pydna
return score_homology_arm_pydna(
genome_dsr=genome_dsr,
arm_str=left_h,
use_subsets=count_subsets,
subset_k=subset_k,
left_len=left_len,
right_len=right_len,
) + score_homology_arm_pydna(
genome_dsr=genome_dsr,
arm_str=right_h,
use_subsets=count_subsets,
subset_k=subset_k,
left_len=left_len,
right_len=right_len,
)
def create_candidate_dseqrecord(insert_seq, insert_pos, left, right, score, rank):
### Create pydna Dseqrecord for a candidate with full insert_w_homologies sequence
insert_w_homologies_str = left + insert_seq + right
# Create Dseqrecord
candidate = Dseqrecord(insert_w_homologies_str, circular=False)
candidate.id = "candidate_%03d_pos_%d" % (rank, insert_pos)
candidate.name = "cand_%03d" % rank
candidate.description = "Candidate %d at position %d, score=%d" % (
rank,
insert_pos,
score,
)
left_len = len(left)
ins_len = len(insert_seq)
total_len = len(insert_w_homologies_str)
# Add features using pydna format
candidate.features = [
SeqFeature(
FeatureLocation(0, left_len),
type="misc_feature",
qualifiers={
"note": [
"homology_left (pos %d-%d)" % (insert_pos - left_len, insert_pos)
]
},
),
SeqFeature(
FeatureLocation(left_len, left_len + ins_len),
type="misc_feature",
qualifiers={"note": ["insert"]},
),
SeqFeature(
FeatureLocation(left_len + ins_len, total_len),
type="misc_feature",
qualifiers={
"note": [
"homology_right (pos %d-%d)" % (insert_pos, insert_pos + len(right))
]
},
),
SeqFeature(
FeatureLocation(0, 0),
type="misc_feature",
qualifiers={
"note": [
"score=%d" % score,
"rank=%d" % rank,
"insertion_pos=%d" % insert_pos,
]
},
),
]
return candidate
def create_insert_with_homologies(insert_seq, left, right, rank, pos):
"""Create clean pydna Dseqrecord for insert with homology arms"""
insert_w_homologies_str = left + insert_seq + right
insert_dsr = Dseqrecord(insert_w_homologies_str, circular=False)
insert_dsr.id = "insert_w_homologies_candidate_%03d" % rank
insert_dsr.name = "insert_%03d" % rank
insert_dsr.description = (
"Insert with homologies for candidate %d at position %d" % (rank, pos)
)
insert_dsr.features = [
SeqFeature(
FeatureLocation(0, len(left)),
type="misc_feature",
qualifiers={"note": ["homology_left"]},
),
SeqFeature(
FeatureLocation(len(left), len(left) + len(insert_seq)),
type="misc_feature",
qualifiers={"note": ["insert"]},
),
SeqFeature(
FeatureLocation(len(left) + len(insert_seq), len(insert_w_homologies_str)),
type="misc_feature",
qualifiers={"note": ["homology_right"]},
),
]
return insert_dsr
def integrate_features_pydna(genome, insert_pos: int, insert_len: int):
"""Create transformed genome with features shifted around insertion site using pydna"""
# Create new Dseqrecord with same sequence (will be updated after integration)
new_genome = Dseqrecord(str(genome.seq), circular=genome.circular)
new_genome.id = genome.id
new_genome.name = genome.name
new_genome.description = genome.description
new_genome.annotations = dict(genome.annotations)
new_feats = []
for feat in genome.features:
s = int(feat.location.start)
e = int(feat.location.end)
strand = int(getattr(feat.location, "strand", 0) or 0)
# direction of new features is unknown
if e <= insert_pos: # feature entirely upstream of insertion site
loc = FeatureLocation(s, e, strand=strand) # so we do nothing
elif (
s >= insert_pos
): # feature is after the insertion site, so everything is moved
loc = FeatureLocation(s + insert_len, e + insert_len, strand=strand)
else:
loc = FeatureLocation(s, e, strand=strand)
new_feats.append(
SeqFeature(
location=loc,
type=feat.type,
id=feat.id,
qualifiers=dict(feat.qualifiers),
)
)
# Add insertion site markers, adding note in "qualifiers" for a genome positions of insertion
new_feats.append(
SeqFeature(
FeatureLocation(insert_pos, insert_pos),
type="misc_feature",
qualifiers={"note": ["insertion_site_after_stop_codon"]},
)
)
new_feats.append(
SeqFeature(
FeatureLocation(insert_pos, insert_pos + insert_len),
type="misc_feature",
qualifiers={"note": ["insert"]},
)
)
new_genome.features = new_feats
return new_genome
def write_gbk_pydna(dseqrecord, path):
"""Write a pydna Dseqrecord to a GenBank file.
Parameters
----------
dseqrecord : Dseqrecord
The Dseqrecord to write.
path : str
The file path to write the GenBank file to.
"""
# Convert Dseqrecord to BioPython SeqRecord for writing
sr = SeqRecord(
Seq(str(dseqrecord.seq)),
id=dseqrecord.id,
name=dseqrecord.name,
description=dseqrecord.description,
annotations=dseqrecord.annotations,
)
sr.features = dseqrecord.features
sr.annotations["molecule_type"] = sr.annotations.get("molecule_type", "DNA")
if not sr.id:
sr.id = "record"
if not sr.name:
sr.name = sr.id[:16]
SeqIO.write(sr, path, "genbank")
def build_and_score_candidates(
genome,
insert_seq,
stops,
left_len,
right_len,
count_subsets: bool = True,
subset_k: int = 50,
):
# Getting the list of all arms candidates with their scores
# the list of stops is a result of function get_plus_strand_cds_stops()
candidates = [] # a list for storing potential arms
for pos in stops:
arms = extract_homology_arms(
genome=genome, insert_pos=pos, left_len=left_len, right_len=right_len
)
if not arms:
continue
left_h, right_h = arms
sc = score_candidate(
genome_dsr=genome,
left_h=left_h,
right_h=right_h,
subset_k=subset_k,
count_subsets=count_subsets,
left_len=left_len,
right_len=right_len,
)
insert_w_homologies_str = left_h + insert_seq + right_h
insert_w_dsr = Dseqrecord(insert_w_homologies_str, circular=False)
candidates.append(
{
"pos": pos,
"left": left_h,
"right": right_h,
"score": sc,
"insert_w_homologies": insert_w_dsr,
}
)
return candidates
def selective_homology_integration(
genome, ranked_candidates, limit_overlap=None, max_tries=50, progress_every=25
):
### Try to integrate a donor DNA with homology arms into a genome,
### testing candidate insertion sites one by one until we find a unique (single-product) homologous recombination.
if not ranked_candidates:
return None, []
left_len = len(
ranked_candidates[0]["left"]
) # ranked_candidates is a (dict), result of build_and_score_candidates() ranked
right_len = len(ranked_candidates[0]["right"])
if limit_overlap is None:
limit_overlap = min(left_len, right_len)
genome_linear = Dseqrecord(str(genome.seq), circular=False) # making genome linear
tried = []
unique_hits = []
for idx, cand in enumerate(ranked_candidates[:max_tries], 1):
insert_w_dsr = cand["insert_w_homologies"] # left_h + INSERT_SEQ + right_h
try:
products = homologous_recombination_integration(
genome_linear, [insert_w_dsr], limit=limit_overlap
)
except Exception:
products = []
tried.append({**cand, "products": products})
if (
products and len(products) == 1
): # exactly one recombination product means a unique, clean integration
unique_hits.append({**cand, "product": products[0]})
break
if idx % progress_every == 0:
print(
" Tried integration on %d / %d top sites..."
% (idx, min(max_tries, len(ranked_candidates)))
)
best_result = unique_hits[0] if unique_hits else None
return best_result, tried