Skip to content

Commit

Permalink
Merge pull request #4 from Edinburgh-Genome-Foundry/dev
Browse files Browse the repository at this point in the history
Biopython v1.78 compatibility
  • Loading branch information
veghp authored Sep 11, 2020
2 parents 0892ace + b61b7e7 commit 962822c
Show file tree
Hide file tree
Showing 12 changed files with 258 additions and 190 deletions.
14 changes: 5 additions & 9 deletions LICENCE.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@

The MIT License (MIT)
[OSI Approved License]

The MIT License (MIT)
MIT License

Copyright (c) 2020 Edinburgh Genome Foundry

Expand All @@ -13,13 +9,13 @@ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
16 changes: 7 additions & 9 deletions dnacauldron/Fragment/Fragment.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def from_biopython_record(biopython_record):

def plot(self, ax=None):
"""Plot the fragment and its features on a Matplotlib ax.
This creates a new ax if no ax is provided. The ax is returned at the
end.
"""
Expand All @@ -37,9 +37,9 @@ def reverse_complement(self):

def to_standard_string(self):
"""Return a standard string to represent and identify the fragment.
This method is used to standardize and recognize similar FragmentChain
instances.
instances.
"""

return str(self.seq)
Expand All @@ -53,13 +53,11 @@ def create_homology_annotation(
"ApEinfo_fwdcolor": color,
}
return SeqFeature(
FeatureLocation(start, end),
type=annotation_type,
qualifiers=qualifiers,
FeatureLocation(start, end), type=annotation_type, qualifiers=qualifiers,
)

def text_representation_in_plots(self):
return r"$\bf{%s}$" % self.original_part.id
def as_bioptyhon_record(self):

def as_biopython_record(self):
return self
55 changes: 30 additions & 25 deletions dnacauldron/Fragment/HomologousFragment/HomologousFragment.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,33 @@
from copy import deepcopy
from Bio.SeqRecord import SeqRecord
from ..Fragment import Fragment
from Bio.Alphabet import DNAAlphabet

try:
# Biopython <1.78
from Bio.Alphabet import DNAAlphabet

has_dna_alphabet = True
except ImportError:
# Biopython >=1.78
has_dna_alphabet = False

from ...biotools import set_record_topology, crop_record_with_saddling_features


class HomologousFragment(Fragment):
@staticmethod
def from_biopython_record(biopython_record):
"""Convert a biopython record into a HomologousFragment (class change).
"""Convert a Biopython record into a HomologousFragment (class change).
"""
new_record = deepcopy(biopython_record)
new_record.original_part = biopython_record
new_record.__class__ = HomologousFragment
return new_record

def circularized(
self,
homology_checker,
annotate_homology=False,
annotation_type="homology",
self, homology_checker, annotate_homology=False, annotation_type="homology",
):
"""Return the biopython record obtained by cirularizing the result.
"""Return the Biopython record obtained by cirularizing the result.
Only works if the left and right sticky ends are compatible. The
return is a simple Biopython record where the sticky end has been
Expand All @@ -33,8 +39,10 @@ def circularized(
annotate_homology=True,
annotation_type="homology",
)

def only_parts_indicators(feature):
return feature.qualifiers.get("indicates_part", False)

result = crop_record_with_saddling_features(
record=double_self,
start=len(self),
Expand Down Expand Up @@ -64,13 +72,11 @@ def _push_source_features(self, homology_size, side="left"):
def will_clip_in_this_order_with(self, other_fragment, homology_checker):
"""Return whether the fragment will assemble with anoter via homology
recombination.
homology_checker should be an HomologyChecker instance definining the
homology conditions.
"""
homology_size = homology_checker.find_end_homologies(
self, other_fragment
)
homology_size = homology_checker.find_end_homologies(self, other_fragment)
return homology_size > 0

def assemble_with(
Expand All @@ -82,16 +88,16 @@ def assemble_with(
):
"""Return the fragment resulting from the assembly of this fragment
with another, in that order.
Parameters
----------
fragment
The other parameter to assemble with
The other parameter to assemble with.
homology_checker
An HomologyChecker instance definining the homology conditions.
annotate_homology
If true, homologies will have an annotation in the final, predicted
construct records.
Expand Down Expand Up @@ -130,30 +136,25 @@ def only_parts_indicators(feature):

@staticmethod
def assemble(
fragments,
homology_checker,
circularize=False,
annotate_homologies=False,
fragments, homology_checker, circularize=False, annotate_homologies=False,
):
"""Return the record obtained by assembling the fragments.
Parameters
----------
fragments
List of HomologousFragments to assemble
List of HomologousFragments to assemble.
homology_checker
An HomologyChecker instance definining the homology conditions.
circularize
True to also assemble the end flanks of the final construct.
annotate_homologies
If true, homologies will have an annotation in the final, predicted
construct records.
"""
result = fragments[0]
for fragment in fragments[1:]:
Expand All @@ -167,5 +168,9 @@ def assemble(
annotate_homology=annotate_homologies,
homology_checker=homology_checker,
)
result.seq.alphabet = DNAAlphabet()

if has_dna_alphabet: # Biopython <1.78
result.seq.alphabet = DNAAlphabet()
result.annotations["molecule_type"] = "DNA"

return result
31 changes: 22 additions & 9 deletions dnacauldron/Fragment/StickyEndFragment/StickyEnd.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
from Bio.Seq import Seq

try:
# Biopython <1.78
from Bio.Alphabet import DNAAlphabet

has_dna_alphabet = True
except ImportError:
# Biopython >=1.78
has_dna_alphabet = False
from ...biotools import sequence_to_biopython_record, annotate_record


class StickyEnd(Seq):
"""A class to represent the sticky end of a sequence.
Expand All @@ -10,10 +20,10 @@ class StickyEnd(Seq):
----------
data
A DNA sequence in ATGC format
A DNA sequence in ATGC format.
strand
The strand (+1 or -1) on which the protusion is
The strand (+1 or -1) on which the protusion is.
**k
Optional keyword arguments for the sequence, such as ``alphabet`` etc.
Expand All @@ -24,11 +34,15 @@ def __init__(self, data, strand, **k):
self.strand = strand

def reverse_complement(self):
return StickyEnd(
str(Seq.reverse_complement(self)),
strand=-self.strand,
alphabet=self.alphabet,
)

if has_dna_alphabet: # Biopython <1.78
return StickyEnd(
str(Seq.reverse_complement(self)),
strand=-self.strand,
alphabet=self.alphabet,
)
else:
return StickyEnd(str(Seq.reverse_complement(self)), strand=-self.strand,)

def __repr__(self):
return "%s(%s)" % (Seq.__str__(self), {1: "+", -1: "-"}[self.strand])
Expand All @@ -40,10 +54,9 @@ def will_clip_directly_with(self, other):
and (self.strand == -other.strand)
and (str(self) == str(other))
)

def as_biopython_record(self):
record = sequence_to_biopython_record(str(self))
sign = "+" if self.strand == 1 else "-"
annotate_record(record, label="(%s) strand" % sign)
return record

Loading

0 comments on commit 962822c

Please sign in to comment.