Skip to content

Commit

Permalink
Refactor handling of score matrix weights in Aligner class
Browse files Browse the repository at this point in the history
  • Loading branch information
althonos committed May 3, 2024
1 parent 63b06ee commit e8e692a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
2 changes: 2 additions & 0 deletions pyfamsa/_famsa.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ cdef class Aligner:
cdef CParams _params
cdef readonly ScoringMatrix scoring_matrix

cdef int _copy_matrix(self, CFAMSA* famsa) except 1 nogil

cpdef Alignment align(self, object sequences)
cpdef GuideTree build_tree(self, object sequences)

Expand Down
26 changes: 14 additions & 12 deletions pyfamsa/_famsa.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ cdef class Aligner:
with ``tree_heuristic``.
n_refinements (`int`): The number of refinement iterations to
run.
keep_duplicates (`bool`): Set to `True` to avoid discarding
keep_duplicates (`bool`): Set to `True` to avoid discarding
duplicate sequences before building trees or alignments.
refine (`bool` or `None`): Set to `True` to force refinement,
`False` to disable refinement, or leave as `None` to disable
Expand Down Expand Up @@ -392,6 +392,16 @@ cdef class Aligner:

# --- Methods ------------------------------------------------------------

cdef int _copy_matrix(self, CFAMSA* famsa) except 1 nogil:
cdef size_t i
cdef size_t j
cdef const float** matrix = self.scoring_matrix.matrix()
for i in range(NO_AMINOACIDS):
famsa.score_vector[i] = <score_t> roundf(cost_cast_factor * matrix[i][i])
for j in range(NO_AMINOACIDS):
famsa.score_matrix[i][j] = <score_t> roundf(cost_cast_factor * matrix[i][j])
return 0

cpdef Alignment align(self, object sequences):
"""align(self, sequences)\n--
Expand All @@ -417,11 +427,7 @@ cdef class Aligner:

# copy score matrix weights
with nogil:
matrix = self.scoring_matrix.matrix()
for i in range(NO_AMINOACIDS):
famsa.score_vector[i] = <score_t> roundf(cost_cast_factor * matrix[i][i])
for j in range(NO_AMINOACIDS):
famsa.score_matrix[i][j] = <score_t> roundf(cost_cast_factor * matrix[i][j])
self._copy_matrix(famsa)

# record the aligner on the resulting alignment
alignment._famsa = shared_ptr[CFAMSA](famsa)
Expand Down Expand Up @@ -466,11 +472,7 @@ cdef class Aligner:

# copy score matrix weights
with nogil:
matrix = self.scoring_matrix.matrix()
for i in range(NO_AMINOACIDS):
famsa.score_vector[i] = <score_t> roundf(cost_cast_factor * matrix[i][i])
for j in range(NO_AMINOACIDS):
famsa.score_matrix[i][j] = <score_t> roundf(cost_cast_factor * matrix[i][j])
self._copy_matrix(famsa)

# copy the aligner input and record original order
for i, sequence in enumerate(sequences):
Expand All @@ -485,7 +487,7 @@ cdef class Aligner:
og2map.push_back(i)
ptrvec.push_back(&seqvec.data()[i])
tree._names.push_back(move(CSequence(seqvec[i].id, string(), i, NULL)))

# remove duplicates and record sequence order
if not self._params.keepDuplicates:
famsa.removeDuplicates(ptrvec, og2map)
Expand Down

0 comments on commit e8e692a

Please sign in to comment.