Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/operator overloading #18

Merged
merged 7 commits into from
Sep 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
### Changed
## [0.1] - 2024-06-05
### Added
- `addition` and `multipication` operators overload
- `len` magic overload
- `Primer` class
- Molecular weight calculation
- GC content calculation
Expand Down
3 changes: 3 additions & 0 deletions opr/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@
PRIMER_SEQUENCE_VALID_GC_CONTENT_RANGE_WARNING = "The recommended range for GC content is between 30% and 80%."
PRIMER_READ_ONLY_ATTRIBUTE_ERROR = "This attribute is read-only."
PRIMER_NOT_REMOVABLE_ATTRIBUTE_ERROR = "This attribute is not removable."

PRIMER_ADDITION_ERROR = "You can only add two Primer objects."
PRIMER_MULTIPICATION_ERROR = "The primer sequence can only be multiplied by an integer."
33 changes: 33 additions & 0 deletions opr/primer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from .params import PRIMER_READ_ONLY_ATTRIBUTE_ERROR, PRIMER_NOT_REMOVABLE_ATTRIBUTE_ERROR
from .params import A_WEIGHT, T_WEIGHT, C_WEIGHT, G_WEIGHT, ANHYDROUS_MOLECULAR_WEIGHT_CONSTANT
from .params import DNA_COMPLEMENT_MAP
from .params import PRIMER_ADDITION_ERROR, PRIMER_MULTIPICATION_ERROR


class Primer:
Expand All @@ -30,6 +31,38 @@ def __init__(self, primer_sequence):
self._molecular_weight = None
self._gc_content = None

def __len__(self):
"""
Return the length of the Primer sequence.

:return: length of the Primer sequence
"""
return len(self._sequence)

def __add__(self, other_primer):
"""
Concatenate the sequences of the current Primer with another one.

:param other_primer: another Primer to concat its sequence to the current Primer
:type other_primer: Primer
:return: new Primer with concatenated sequence
"""
if isinstance(other_primer, Primer):
return Primer(self._sequence + other_primer._sequence)
raise OPRBaseError(PRIMER_ADDITION_ERROR)

def __mul__(self, number):
"""
Multiply the Primer sequence `number` times.

:param number: times to concat the Primer sequence to itself
:type number: int
:return: new Primer with multiplied sequence
"""
if isinstance(number, int):
return Primer(self._sequence * number)
raise OPRBaseError(PRIMER_MULTIPICATION_ERROR)

def reverse(self, inplace=False):
"""
Reverse sequence.
Expand Down
15 changes: 14 additions & 1 deletion tests/test_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,17 @@ def test_complement_3(): #Reference: https://www.qiagen.com/us/applications/enzy
oprimer.complement(inplace=True)
assert oprimer.sequence == "TAGCCGATTTAGCCGATT"


def test_length():
oprimer = Primer("ATCGGCTAAATCGGCTAA")
assert len(oprimer) == 18

def test_addition():
oprimer_1 = Primer("ATCG")
oprimer_2 = Primer("GATC")
oprimer_concat = oprimer_1 + oprimer_2
assert oprimer_concat.sequence == "ATCGGATC"

def test_multiply():
oprimer_1 = Primer("ATCG")
oprimer_concat = oprimer_1 * 4
assert oprimer_concat.sequence == "ATCGATCGATCGATCG"
Loading