diff --git a/CHANGELOG.md b/CHANGELOG.md index 57c18a3..f4d02cb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/opr/params.py b/opr/params.py index 8769f3a..b065ea4 100644 --- a/opr/params.py +++ b/opr/params.py @@ -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." diff --git a/opr/primer.py b/opr/primer.py index feeb761..f6fc42a 100644 --- a/opr/primer.py +++ b/opr/primer.py @@ -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: @@ -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. diff --git a/tests/test_operations.py b/tests/test_operations.py index 727520e..3f67952 100644 --- a/tests/test_operations.py +++ b/tests/test_operations.py @@ -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"