Skip to content

Commit

Permalink
Merge pull request #260 from waveygang/inv-patch-fast
Browse files Browse the repository at this point in the history
speed up inversion patching
  • Loading branch information
ekg authored Jul 31, 2024
2 parents 78bff59 + 48bea20 commit 810bd0d
Show file tree
Hide file tree
Showing 5 changed files with 603 additions and 246 deletions.
2 changes: 1 addition & 1 deletion src/common/wflign/src/wflign.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1026,7 +1026,7 @@ void WFlign::wflign_affine_wavefront(
} else {
// todo old implementation (and SAM format is not supported)
for (auto x = trace.rbegin(); x != trace.rend(); ++x) {
write_alignment(
write_alignment_paf(
*out,
**x,
query_name,
Expand Down
38 changes: 31 additions & 7 deletions src/common/wflign/src/wflign_alignment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

// Default constructor
alignment_t::alignment_t()
: j(0), i(0), query_length(0), target_length(0), ok(false), keep(false) {
: j(0), i(0), query_length(0), target_length(0), score(std::numeric_limits<int>::max()), ok(false), keep(false), is_rev(false) {
edit_cigar = {nullptr, 0, 0};
}

Expand All @@ -25,7 +25,7 @@ alignment_t::~alignment_t() {

// Copy constructor
alignment_t::alignment_t(const alignment_t& other)
: j(other.j), i(other.i), query_length(other.query_length),
: j(other.j), i(other.i), query_length(other.query_length), is_rev(other.is_rev),
target_length(other.target_length), ok(other.ok), keep(other.keep) {
if (other.edit_cigar.cigar_ops) {
edit_cigar.cigar_ops = (char*)malloc((other.edit_cigar.end_offset - other.edit_cigar.begin_offset) * sizeof(char));
Expand All @@ -40,7 +40,7 @@ alignment_t::alignment_t(const alignment_t& other)

// Move constructor
alignment_t::alignment_t(alignment_t&& other) noexcept
: j(other.j), i(other.i), query_length(other.query_length),
: j(other.j), i(other.i), query_length(other.query_length), is_rev(other.is_rev),
target_length(other.target_length), ok(other.ok), keep(other.keep),
edit_cigar(other.edit_cigar) {
other.edit_cigar = {nullptr, 0, 0};
Expand All @@ -55,6 +55,7 @@ alignment_t& alignment_t::operator=(const alignment_t& other) {
target_length = other.target_length;
ok = other.ok;
keep = other.keep;
is_rev = other.is_rev;

free(edit_cigar.cigar_ops);
if (other.edit_cigar.cigar_ops) {
Expand All @@ -79,6 +80,7 @@ alignment_t& alignment_t::operator=(alignment_t&& other) noexcept {
target_length = other.target_length;
ok = other.ok;
keep = other.keep;
is_rev = other.is_rev;

free(edit_cigar.cigar_ops);
edit_cigar = other.edit_cigar;
Expand All @@ -87,19 +89,19 @@ alignment_t& alignment_t::operator=(alignment_t&& other) noexcept {
return *this;
}

int alignment_t::query_begin() {
int alignment_t::query_begin() const {
return j;
}

int alignment_t::query_end() {
int alignment_t::query_end() const {
return j + query_length;
}

int alignment_t::target_begin() {
int alignment_t::target_begin() const {
return i;
}

int alignment_t::target_end() {
int alignment_t::target_end() const {
return i + target_length;
}

Expand Down Expand Up @@ -707,6 +709,28 @@ int calculate_alignment_score(const wflign_cigar_t& cigar, const wflign_penaltie
return score;
}

std::string cigar_to_string(const wflign_cigar_t& cigar) {
std::stringstream ss;
const int start_idx = cigar.begin_offset;
const int end_idx = cigar.end_offset;
for (int c = start_idx; c < end_idx; c++) {
ss << cigar.cigar_ops[c];
}
return ss.str();
}

std::ostream& operator<<(std::ostream& os, const alignment_t& aln) {
return os << "Alignment: "
<< "Query(" << aln.query_begin() << "-" << aln.query_end() << "/" << aln.query_length << ") "
<< "Target(" << aln.target_begin() << "-" << aln.target_end() << "/" << aln.target_length << ") "
<< "Score=" << aln.score << " "
<< "Rev=" << (aln.is_rev ? "Yes" : "No") << " "
<< "Status=" << (aln.ok ? "OK" : "NotOK") << " "
<< "Keep=" << (aln.keep ? "Yes" : "No") << " "
<< "CIGAR=" << cigar_to_string(aln.edit_cigar) << " "
<< "Indices(i,j)=(" << aln.i << "," << aln.j << ")";
}

/*
// No more necessary
bool hack_cigar(wfa::cigar_t &cigar, const char *query, const char *target,
Expand Down
14 changes: 10 additions & 4 deletions src/common/wflign/src/wflign_alignment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <vector>
#include <cstdint>
#include <sstream>
#include "WFA2-lib/bindings/cpp/WFAligner.hpp"

/*
Expand Down Expand Up @@ -35,6 +36,7 @@ class alignment_t {
int i;
int query_length;
int target_length;
int score;
bool ok;
bool keep;
bool is_rev;
Expand All @@ -56,12 +58,16 @@ class alignment_t {
void trim_back(int query_trim);
// query_begin, query_end, target_begin, target_end
// Accessors
int query_begin();
int query_end();
int target_begin();
int target_end();
int query_begin() const;
int query_end() const;
int target_begin() const;
int target_end() const;
};

// debugging alignment writer
std::ostream& operator<<(std::ostream& os, const alignment_t& aln);
// debugging cigar writer


/*
* Wflign Trace-Pos: Links a position in a traceback matrix to its edit
Expand Down
Loading

0 comments on commit 810bd0d

Please sign in to comment.