Skip to content

Commit

Permalink
Feature/operator overloading (#18)
Browse files Browse the repository at this point in the history
* overload len magic function

* operator (+,*) overloading

* add operator overloading errors

* add tests for len, addition and multipication

* `CHANGELOG.md` updated

* `auto.pep8.sh` applied

* feedback applied
  • Loading branch information
AHReccese authored Sep 29, 2024
1 parent 33432c2 commit a36ced3
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 1 deletion.
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"

0 comments on commit a36ced3

Please sign in to comment.