Skip to content

Commit

Permalink
fix: update string indexing
Browse files Browse the repository at this point in the history
  • Loading branch information
BenTenmann committed Jan 29, 2022
1 parent 980c764 commit 145430b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
14 changes: 7 additions & 7 deletions src/setriq/_C/PairwiseDistanceComputer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ double_vector_t PairwiseDistanceComputer::compute_distance(const string_vector_t
* the method will be multi-threaded.
*
* @param input_strings: a vector of input strings
* @return a flat (N * (N - 1)) / 2 vector of doubles
* @return a flat (n * (n - 1)) / 2 vector of doubles
*/
const auto& N = input_strings.size();
double_vector_t distance_matrix(N * (N - 1) / 2);
const auto& n = input_strings.size();
double_vector_t distance_matrix(n * (n - 1) / 2);

#pragma omp parallel for default(none) shared(input_strings, distance_matrix, N)
for (size_t i = 0; i < N - 1; i++) {
for (size_t j = i + 1; j < N; j++) {
size_t index = (N * (N - 1)) / 2 - (N - i) * ((N - i) - 1) / 2 + j - i - 1;
#pragma omp parallel for default(none) firstprivate(n) shared(input_strings, distance_matrix)
for (size_t i = 0; i < n - 1; i++) {
for (size_t j = i + 1; j < n; j++) {
size_t index = (n * (n - 1)) / 2 - (n - i) * ((n - i) - 1) / 2 + j - i - 1;
distance_matrix[index] = (*this->distance_metric_)(input_strings[i], input_strings[j]);
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/setriq/_C/alignment/SmithWaterman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,11 @@ double SmithWaterman::identity_score(const std::string &input_string) {
* @param input_string: an input string to be aligned with itself
* @return the maximal self-alignment score for an input string
*/
const auto& N = input_string.size();
const auto* end = &input_string.back() + 1;

double score {0};
for (size_t i = 0; i < N; i++) {
score += this->substitution_matrix_(input_string[i], input_string[i]);
auto&& score = 0.f;
for (auto* ptr = &input_string.front(); ptr != end; ptr++) {
score += this->substitution_matrix_(*ptr, *ptr);
}
return score;
}
18 changes: 12 additions & 6 deletions src/setriq/_C/metrics/TcrDist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,24 @@ double metric::TcrDist::forward(const std::string &a, const std::string &b) {
* @param b: another string to be compared
* @return TcrDist metric between the two strings
*/
const auto& max_distance {4.};
constexpr double max_distance = 4;
const auto& n = a.size();

double distance {0}, substitution;
for (size_t i = 0; i < a.size(); i++) {
if (a[i] == b[i]) continue;
const auto* ptr_a = &a.front();
const auto* ptr_b = &b.front();
auto &&distance = 0.f;
for (size_t i = 0; i < n; i++) {
const auto& _a = *(ptr_a + i);
const auto& _b = *(ptr_b + i);
if (_a == _b)
continue;

if (a[i] == this->gap_symbol_ || b[i] == this->gap_symbol_) {
if (_a == this->gap_symbol_ || _b == this->gap_symbol_) {
distance += this->gap_penalty_;
continue;
}

substitution = max_distance - this->substitution_matrix_(a[i], b[i]);
const auto& substitution = max_distance - this->substitution_matrix_(_a, _b);
distance += std::min(max_distance, substitution);
}
return distance * this->distance_weight_;
Expand Down

0 comments on commit 145430b

Please sign in to comment.