Skip to content

Commit

Permalink
Minor update (#47)
Browse files Browse the repository at this point in the history
* remove `single_run_length` function

* refactored code to use `repeats` function

* return 1 instead of 0 for cosecutive mode (if the occurence number is 1)

* minor refactorings

* update testcases to follow the new rule

* `CHANGELOG.md` updated

* `README.md` updated

* `CHANGELOG.md` updated
  • Loading branch information
AHReccese authored Feb 3, 2025
1 parent ccfaa68 commit 71fefde
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 53 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- `repeats` method
- `name` property
### Changed
- `single_runs` property updated
- Test system modified
### Removed
- `single_run_length` function
## [0.2] - 2025-01-09
### Added
- `__eq__` overload
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,12 @@
#### Single run length
```pycon
>>> primer1.single_runs
{'A': 2, 'T': 0, 'C': 0, 'G': 2}
{'A': 2, 'T': 1, 'C': 1, 'G': 2}
```
#### Double run length
```pycon
>>> primer1.double_runs
{'AT': 0, 'AG': 4, 'AC': 0, 'TA': 0, 'TG': 0, 'TC': 0, 'GA': 5, 'GT': 0, 'GC': 0, 'CA': 0, 'CT': 0, 'CG': 0}
{'TA': 1, 'TC': 0, 'TG': 1, 'AT': 0, 'AC': 1, 'AG': 2, 'CT': 1, 'CA': 0, 'CG': 1, 'GT': 1, 'GA': 1, 'GC': 0}
```
#### Repeats
```pycon
Expand All @@ -104,7 +104,7 @@
```
```pycon
>>> primer1.repeats(sequence="GG", consecutive=True)
0
1
```
#### Melting temperature
```pycon
Expand Down
23 changes: 0 additions & 23 deletions opr/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,26 +49,3 @@ def gc_clamp_calc(sequence):
if len(sequence) < 5:
return 0
return sequence[-5:].count('G') + sequence[-5:].count('C')


def single_run_length(sequence, base):
"""
Calculate the maximum consecutive occurrence of a Nucleic acid (base) in a sequence.
:param sequence: primer sequence
:type sequence: str
:param base: target Nucleic acid
:type base: str
:return: the length of maximum consecutive occurrence
"""
max_length = 0
current_length = 0
for c in sequence:
if c == base:
current_length += 1
max_length = max(max_length, current_length)
else:
current_length = 0
if max_length == 1:
return 0
return max_length
19 changes: 6 additions & 13 deletions opr/primer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from .params import DNA_COMPLEMENT_MAP
from .params import PRIMER_ADDITION_ERROR, PRIMER_MULTIPLICATION_ERROR
from .params import PRIMER_MELTING_TEMPERATURE_NOT_IMPLEMENTED_ERROR
from .functions import molecular_weight_calc, basic_melting_temperature_calc, gc_clamp_calc, single_run_length
from .functions import molecular_weight_calc, basic_melting_temperature_calc, gc_clamp_calc


class MeltingTemperature(Enum):
Expand Down Expand Up @@ -226,14 +226,9 @@ def single_runs(self):
:return: single runs of the primer
"""
if self._single_runs is None:
self._single_runs = {
'A': 0,
'T': 0,
'C': 0,
'G': 0,
}
for base in self._single_runs:
self._single_runs[base] = single_run_length(self._sequence, base)
self._single_runs = {}
for base in VALID_BASES:
self._single_runs[base] = self.repeats(base, consecutive=True)
return self._single_runs

@property
Expand All @@ -247,8 +242,8 @@ def double_runs(self):
"""
if self._double_runs is None:
pairs = [''.join(pair) for pair in itertools.product(VALID_BASES, repeat=2) if pair[0] != pair[1]]
counts = {pair: 0 for pair in pairs}
for pair in counts:
counts = {}
for pair in pairs:
counts[pair] = self.repeats(pair, consecutive=True)
self._double_runs = counts
return self._double_runs
Expand All @@ -267,8 +262,6 @@ def repeats(self, sequence, consecutive=False):
pattern = f"(?:{re.escape(sequence)})+"
matches = re.findall(f"({pattern})+", self.sequence)
result = max((len(match) // len(sequence) for match in matches), default=0)
if result == 1:
result = 0
return result
else:
return self.sequence.count(sequence)
Expand Down
28 changes: 14 additions & 14 deletions tests/test_calculations.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ def test_melt_temp_2(): # Reference: http://biotools.nubic.northwestern.edu/Oli
def test_single_runs_1(): # Reference: https://www.oligoevaluator.com/OligoCalcServlet
oprimer = Primer("ATCGATCG")
runs = oprimer.single_runs
assert runs['A'] == 0 and runs['T'] == 0 and runs['C'] == 0 and runs['G'] == 0
assert runs['A'] == 1 and runs['T'] == 1 and runs['C'] == 1 and runs['G'] == 1


def test_single_runs_2(): # Reference: https://www.oligoevaluator.com/OligoCalcServlet
oprimer = Primer("ATTCGATCCCCG")
runs = oprimer.single_runs
assert runs['A'] == 0 and runs['T'] == 2 and runs['C'] == 4 and runs['G'] == 0
assert runs['A'] == 1 and runs['T'] == 2 and runs['C'] == 4 and runs['G'] == 1


def test_single_runs_3(): # Reference: https://www.oligoevaluator.com/OligoCalcServlet
Expand All @@ -71,20 +71,20 @@ def test_single_runs_3(): # Reference: https://www.oligoevaluator.com/OligoCalc
def test_double_runs():
p1 = Primer("ATATCGAACACACACACA")
double_runs = p1.double_runs
print(double_runs)
true_answer = {
'GT': 0,
'CA': 5,
'AT': 2,
'TA': 0,
'GC': 0,
'GA': 0,
'AC': 5,
'AG': 0,
'TA': 1,
'TC': 1,
'TG': 0,
'CG': 0,
'TC': 0,
'AC': 5,
'CT': 0}
'CA': 5,
'CT': 0,
'CG': 1,
'GA': 1,
'GT': 0,
'GC': 0
}
assert len(true_answer) == len(double_runs) and all(double_runs[pair] == true_answer[pair] for pair in double_runs)


Expand All @@ -94,8 +94,8 @@ def test_repeats_1():
p.repeats(sequence="A", consecutive=False) == 1 and
p.repeats(sequence="AT", consecutive=False) == 1 and
p.repeats(sequence="AC", consecutive=False) == 0 and
p.repeats(sequence="A", consecutive=True) == 0 and
p.repeats(sequence="AT", consecutive=True) == 0
p.repeats(sequence="A", consecutive=True) == 1 and
p.repeats(sequence="AT", consecutive=True) == 1
)


Expand Down

0 comments on commit 71fefde

Please sign in to comment.