Skip to content

Commit

Permalink
Updates to class and function names
Browse files Browse the repository at this point in the history
  • Loading branch information
dawnandrew100 committed Jul 8, 2024
1 parent 946a2aa commit 369e3da
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 130 deletions.
60 changes: 30 additions & 30 deletions limestone/editdistance.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from __future__ import annotations
from re import sub
try:
# external dependency
import numpy
Expand All @@ -8,14 +9,10 @@
numpy = None

def main():
qqs = "AFTG"
sss = "ACTG"
qqs = "AT"
sss = "AAGT"

print(needlemanWunsch.matrix(qqs,sss))
print(needlemanWunsch.align(qqs,sss))
print(watermanSmithBeyer.matrix(qqs,sss))
print(watermanSmithBeyer.align(qqs,sss))
print(hirschbergDist.align(qqs,sss))
print(watermanSmithBeyer.matrix(qqs, sss))

class _GLOBALBASE():
def matrix(self, querySequence: str, subjectSequence: str)->list[list[float]]:
Expand Down Expand Up @@ -124,7 +121,7 @@ def align(self, querySequence: str, subjectSequence: str)->str:

return f"{queryAlign}\n{subjectAlign}"

class hamming(_GLOBALBASE):
class Hamming(_GLOBALBASE):
def align(self, querySequence: str, subjectSequence: str)->str:
return f"{querySequence}\n{subjectSequence}"

Expand All @@ -134,13 +131,17 @@ def matrix(self, qs: str, ss: str) -> None:
def __call__(self, querySequence: str, subjectSequence: str)->tuple[int,list[int]]:
if not numpy:
raise ImportError('Please pip install numpy!')

qs,ss = map(lambda x: x.upper(), [querySequence,subjectSequence])
qs = [x for x in qs]
ss = [x for x in ss]

long = max(ss,qs)
short = min(ss,qs)
if len(qs) == 1 and len(ss) == 1:
dist = 1 if qs != ss else 0
dist_array = [dist]
return dist, dist_array

shortlen = min(map(len, [ss,qs]))
short = qs if len(qs) == shortlen else ss
long = ss if len(qs) == shortlen else qs

dist = 0
dist_array = []
Expand Down Expand Up @@ -173,7 +174,7 @@ def binary_similarity_array(self, querySequence: str, subjectSequence: str)->lis
simarray = [1 if num == 0 else 0 for num in distarray]
return simarray

class needleman_wunsch(_GLOBALBASE):
class Needleman_Wunsch(_GLOBALBASE):
def __init__(self, match_score:int = 0, mismatch_penalty:int = 1, gap_penalty:int = 2)->None:
self.match_score = match_score
self.mismatch_penalty = mismatch_penalty
Expand All @@ -194,14 +195,14 @@ def __call__(self, querySequence: str, subjectSequence: str)->tuple[NDArray[floa
self.pointer = numpy.zeros((len(qs), len(ss)))
self.pointer[:,0] = 3
self.pointer[0,:] = 4
#keeps first row and column consistent throughout all calculations
#initialisation of starter values for first column and first row
self.alignment_score[:,0] = [n*self.gap_penalty for n in range(len(qs))]
self.alignment_score[0,:] = [n*self.gap_penalty for n in range(len(ss))]

for i, query_char in enumerate(qs):
for j, subject_char in enumerate(ss):
if i == 0 or j == 0:
#doesn't override initialization
#keeps first row and column consistent throughout all calculations
continue
if query_char == subject_char:
match = self.alignment_score[i-1][j-1] - self.match_score
Expand All @@ -223,13 +224,13 @@ def __call__(self, querySequence: str, subjectSequence: str)->tuple[NDArray[floa

return self.alignment_score, self.pointer

class levenshtein(needleman_wunsch):
class Levenshtein(Needleman_Wunsch):
def __init__(self):
self.match_score = 0
self.mismatch_penalty = 1
self.gap_penalty = 1

class smith_waterman(_LOCALBASE):
class Smith_Waterman(_LOCALBASE):
def __init__(self, match_score:int = 1, mismatch_penalty:int = 1, gap_penalty:int = 2)->None:
self.match_score = match_score
self.mismatch_penalty = mismatch_penalty
Expand Down Expand Up @@ -268,7 +269,7 @@ def __call__(self, subjectSequence: str, querySequence: str)-> NDArray[float64]:
self.alignment_score[i][j] = tmax
return self.alignment_score

class waterman_smith_beyer(_GLOBALBASE):
class Waterman_Smith_Beyer(_GLOBALBASE):
def __init__(self, match_score:int = 0, mismatch_penalty:int = 1, new_gap_penalty:int = 3, continue_gap_penalty:int = 1)->None:
self.match_score = match_score
self.mismatch_penalty = mismatch_penalty
Expand All @@ -290,6 +291,7 @@ def __call__(self, querySequence: str, subjectSequence: str)->tuple[NDArray[floa
self.pointer = numpy.zeros((len(qs), len(ss)))
self.pointer[:,0] = 3
self.pointer[0,:] = 4
#initialisation of starter values for first column and first row
self.alignment_score[:,0] = [self.new_gap_penalty + n * self.continue_gap_penalty for n in range(len(qs))]
self.alignment_score[0,:] = [self.new_gap_penalty + n * self.continue_gap_penalty for n in range(len(ss))]
self.alignment_score[0][0] = 0
Expand Down Expand Up @@ -325,7 +327,7 @@ def __call__(self, querySequence: str, subjectSequence: str)->tuple[NDArray[floa

return self.alignment_score, self.pointer

class hirschberg():
class Hirschberg():
def __init__(self, match_score: int = 1, mismatch_penalty: int = -1, gap_penalty: int = -2)->None:
self.match_score = match_score
self.mismatch_penalty = mismatch_penalty
Expand Down Expand Up @@ -387,15 +389,13 @@ def _align(self, qs, ss):

for i in range(1, m + 1):
for j in range(1, n + 1):

if qs[i-1] == ss[j-1]:
match = score[i-1][j-1] + self.match_score
else:
match = score[i-1][j-1] + self.mismatch_penalty
delete = score[i - 1][j] + self.gap_penalty
insert = score[i][j - 1] + self.gap_penalty
score[i][j] = max(match, delete, insert)

if score[i][j] == match:
pointer[i][j] = 3
elif score[i][j] == delete:
Expand Down Expand Up @@ -434,18 +434,18 @@ def frontWhiteSpace(qs: list[str], ss: list[str])->tuple[list[str],list[str]]:
return qs, ss

def ljustlist(sequence: list[str], n: int, fillvalue='')->list[str]:
return sequence + [fillvalue] * (n - len(sequence))
return sequence + [fillvalue] * (n - len(sequence))

def rjustlist(sequence: list[str], fillvalue='')->list[str]:
return [fillvalue] + sequence
return [fillvalue] + sequence


needlemanWunsch = needleman_wunsch()
watermanSmithBeyer = waterman_smith_beyer()
smithWaterman = smith_waterman()
levenshteinDist = levenshtein()
hirschbergDist = hirschberg()
hammingDist = hamming()
needleman_wunsch = Needleman_Wunsch()
waterman_smith_beyer = Waterman_Smith_Beyer()
smith_waterman = Smith_Waterman()
levenshtein = Levenshtein()
hirschberg = Hirschberg()
hamming = Hamming()

if __name__ == "__main__":
main()
42 changes: 21 additions & 21 deletions tests/test_Hamming.py
Original file line number Diff line number Diff line change
@@ -1,89 +1,89 @@
from __future__ import annotations
import unittest
from limestone.editdistance import hammingDist
from limestone.editdistance import hamming

class TestHamming(unittest.TestCase):
def test_distance_diff(self):
dist = hammingDist.distance("ACTG","FHYU")
dist = hamming.distance("ACTG","FHYU")
self.assertEqual(dist, 4.0)

def test_similarity_diff(self):
sim = hammingDist.similarity("ACTG","FHYU")
sim = hamming.similarity("ACTG","FHYU")
self.assertEqual(sim, 0.0)

def test_norm_distance_diff(self):
dist = hammingDist.normalized_distance("ACTG","FHYU")
dist = hamming.normalized_distance("ACTG","FHYU")
self.assertEqual(dist, 1.0)

def test_norm_similarity_diff(self):
sim = hammingDist.normalized_similarity("ACTG","FHYU")
sim = hamming.normalized_similarity("ACTG","FHYU")
self.assertEqual(sim, 0.0)

def test_distance_sim(self):
dist = hammingDist.distance("ACTG","ACTG")
dist = hamming.distance("ACTG","ACTG")
self.assertEqual(dist, 0.0)

def test_similarity_sim(self):
sim = hammingDist.similarity("ACTG","ACTG")
sim = hamming.similarity("ACTG","ACTG")
self.assertEqual(sim, 4.0)

def test_norm_distance_sim(self):
dist = hammingDist.normalized_distance("ACTG","ACTG")
dist = hamming.normalized_distance("ACTG","ACTG")
self.assertEqual(dist, 0.0)

def test_norm_similarity_sim(self):
sim = hammingDist.normalized_similarity("ACTG","ACTG")
sim = hamming.normalized_similarity("ACTG","ACTG")
self.assertEqual(sim, 1.0)

def test_norm_distance1(self):
dist = hammingDist.normalized_distance("ACTG","AATG")
dist = hamming.normalized_distance("ACTG","AATG")
self.assertEqual(dist, 0.25)

def test_norm_distance2(self):
dist = hammingDist.normalized_distance("ACTG","AAAG")
dist = hamming.normalized_distance("ACTG","AAAG")
self.assertEqual(dist, 0.5)

def test_norm_distance3(self):
dist = hammingDist.normalized_distance("ACTG","AAAA")
dist = hamming.normalized_distance("ACTG","AAAA")
self.assertEqual(dist, 0.75)

def test_norm_similarity1(self):
dist = hammingDist.normalized_similarity("ACTG","AATG")
dist = hamming.normalized_similarity("ACTG","AATG")
self.assertEqual(dist, 0.75)

def test_norm_similarity2(self):
dist = hammingDist.normalized_similarity("ACTG","AAAG")
dist = hamming.normalized_similarity("ACTG","AAAG")
self.assertEqual(dist, 0.5)

def test_norm_similarity3(self):
dist = hammingDist.normalized_similarity("ACTG","AAAA")
dist = hamming.normalized_similarity("ACTG","AAAA")
self.assertEqual(dist, 0.25)

def test_diff_len(self):
dist = hammingDist.distance("ACTG","AATGA")
dist = hamming.distance("ACTG","AATGA")
self.assertEqual(dist,2.0)

def test_diff_len2(self):
dist = hammingDist.distance("AATGA","ACTG")
dist = hamming.distance("AATGA","ACTG")
self.assertEqual(dist,2.0)

def test_binary_diff(self):
dist = hammingDist.binary_distance_array("ACTG","AATG")
dist = hamming.binary_distance_array("ACTG","AATG")
ans = [1,0,1,1]
self.assertEqual(dist, ans)

def test_binary_sim(self):
dist = hammingDist.binary_similarity_array("ACTG","AATG")
dist = hamming.binary_similarity_array("ACTG","AATG")
ans = [0,1,0,0]
self.assertEqual(dist, ans)

def test_align1(self):
dist = hammingDist.align("ACTG","ATGA")
dist = hamming.align("ACTG","ATGA")
ans = f"ACTG\nATGA"
self.assertEqual(dist,ans)

def test_align2(self):
dist = hammingDist.align("ACTGAA","ATGA")
dist = hamming.align("ACTGAA","ATGA")
ans = f"ACTGAA\nATGA"
self.assertEqual(dist,ans)

Expand Down
28 changes: 14 additions & 14 deletions tests/test_Hirschberg.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
from __future__ import annotations
import unittest
from limestone.editdistance import needlemanWunsch
from limestone.editdistance import hirschbergDist
from limestone.editdistance import needleman_wunsch
from limestone.editdistance import hirschberg

class TestHirschberg(unittest.TestCase):

def test_align1(self):
nwalignment = needlemanWunsch.align("BA", "ABA")
hsalignment = hirschbergDist.align("BA","ABA")
nwalignment = needleman_wunsch.align("BA", "ABA")
hsalignment = hirschberg.align("BA","ABA")
self.assertEqual(nwalignment, hsalignment)

def test_align2(self):
nwalignment = needlemanWunsch.align("ATCG","ATCG")
hsalignment = hirschbergDist.align("ATCG","ATCG")
nwalignment = needleman_wunsch.align("ATCG","ATCG")
hsalignment = hirschberg.align("ATCG","ATCG")
self.assertEqual(nwalignment, hsalignment)

def test_align3(self):
nwalignment = needlemanWunsch.align("ACTG","FHYU")
hsalignment = hirschbergDist.align("ACTG","FHYU")
nwalignment = needleman_wunsch.align("ACTG","FHYU")
hsalignment = hirschberg.align("ACTG","FHYU")
self.assertEqual(nwalignment, hsalignment)

def test_align4(self):
nwalignment = needlemanWunsch.align("AGTACGCA","TATGC")
hsalignment = hirschbergDist.align("AGTACGCA","TATGC")
nwalignment = needleman_wunsch.align("AGTACGCA","TATGC")
hsalignment = hirschberg.align("AGTACGCA","TATGC")
self.assertEqual(nwalignment, hsalignment)

def test_align5(self):
nwalignment = needlemanWunsch.align("TACOBELL","TABELL")
hsalignment = hirschbergDist.align("TACOBELL","TABELL")
nwalignment = needleman_wunsch.align("TACOBELL","TABELL")
hsalignment = hirschberg.align("TACOBELL","TABELL")
self.assertEqual(nwalignment, hsalignment)

def test_align6(self):
nwalignment = needlemanWunsch.align("FASTFOAODKING","FASTAKING")
hsalignment = hirschbergDist.align("FASTFOAODKING","FASTAKING")
nwalignment = needleman_wunsch.align("FASTFOAODKING","FASTAKING")
hsalignment = hirschberg.align("FASTFOAODKING","FASTAKING")
self.assertEqual(nwalignment, hsalignment)


Expand Down
Loading

0 comments on commit 369e3da

Please sign in to comment.