Skip to content

Commit

Permalink
closes #147
Browse files Browse the repository at this point in the history
  • Loading branch information
manulera committed Nov 10, 2023
1 parent 04d9a45 commit 6492441
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 1 deletion.
71 changes: 70 additions & 1 deletion src/pydna/dseq.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ class Dseq(_Seq):
The most common usage is probably to create a Dseq object as a
part of a Dseqrecord object (see :class:`pydna.dseqrecord.Dseqrecord`).
There are three ways of creating a Dseq object directly:
There are three ways of creating a Dseq object directly listed below, but you can also
use the function Dseq.from_full_sequence_and_overhangs() to create a Dseq:
Only one argument (string):
Expand Down Expand Up @@ -428,6 +429,70 @@ def from_representation(cls, dsdna: str, *args, **kwargs):
obj._data = _rc(cb[-max(0, ovhg) or len(cb) :]) + wb + _rc(cb[: max(0, len(cb) - ovhg - len(wb))])
return obj

@classmethod
def from_full_sequence_and_overhangs(cls, full_sequence: str, crick_ovhg: int, watson_ovhg: int):
"""Create a linear Dseq object from a full sequence and the 3' overhangs of each strand.
The order of the parameters is like this because the 3' overhang of the crick strand is the one
on the left side of the sequence.
Parameters
----------
full_sequence: str
The full sequence of the Dseq object.
crick_ovhg: int
The overhang of the crick strand in the 3' end. Equivalent to Dseq.ovhg.
watson_ovhg: int
The overhang of the watson strand in the 5' end.
Returns
-------
Dseq
A Dseq object.
Examples
--------
```
>>> Dseq.from_full_sequence_and_overhangs('AAAAAA', crick_ovhg=2, watson_ovhg=2)
Dseq(-6)
AAAA
TTTT
>>> Dseq.from_full_sequence_and_overhangs('AAAAAA', crick_ovhg=-2, watson_ovhg=2)
Dseq(-6)
AAAAAA
TT
>>> Dseq.from_full_sequence_and_overhangs('AAAAAA', crick_ovhg=2, watson_ovhg=-2)
Dseq(-6)
AA
TTTTTT
>>> Dseq.from_full_sequence_and_overhangs('AAAAAA', crick_ovhg=-2, watson_ovhg=-2)
Dseq(-6)
AAAA
TTTT
```
"""
full_sequence_rev = str(Dseq(full_sequence).reverse_complement())
watson = full_sequence
crick = full_sequence_rev

# If necessary, we trim the left side
if crick_ovhg < 0:
crick = crick[:crick_ovhg]
elif crick_ovhg > 0:
watson = watson[crick_ovhg:]

# If necessary, we trim the right side
if watson_ovhg < 0:
watson = watson[:watson_ovhg]
elif watson_ovhg > 0:
crick = crick[watson_ovhg:]

return Dseq(watson, crick=crick, ovhg=crick_ovhg)


# @property
# def ovhg(self):
# """The ovhg property. This cannot be set directly, but is a
Expand Down Expand Up @@ -942,6 +1007,10 @@ def three_prime_end(self):
type_ = "blunt"
return type_, sticky

def watson_ovhg(self):
"""Returns the overhang of the watson strand at the three prime."""
return len(self.watson) - len(self.crick) + self.ovhg

def __add__(self, other):
"""Simulates ligation between two DNA fragments.
Expand Down
17 changes: 17 additions & 0 deletions tests/test_module_dseq.py
Original file line number Diff line number Diff line change
Expand Up @@ -768,5 +768,22 @@ def test_translate():
assert str(x.reverse_complement().translate()) == "LFH"


def test_from_full_sequence_and_overhangs():
from pydna.dseq import Dseq
test_cases = [
(2, 2, "AAAA", "TTTT"),
(-2, 2, "AAAAAA", "TT" ),
(2, -2, "AA", "TTTTTT"),
(-2, -2, "AAAA", "TTTT"),
(0, 0, "AAAAAA", "TTTTTT"),
]
for crick_ovhg, watson_ovhg, watson, crick in test_cases:
dseq_1 = Dseq.from_full_sequence_and_overhangs("AAAAAA", crick_ovhg=crick_ovhg, watson_ovhg=watson_ovhg)
dseq_2 = Dseq(watson, crick, ovhg=crick_ovhg, circular=False)

assert dseq_1 == dseq_2
assert dseq_2.watson_ovhg() == watson_ovhg


if __name__ == "__main__":
pytest.main([__file__, "-vv", "-s"])

0 comments on commit 6492441

Please sign in to comment.