Skip to content

Commit

Permalink
Added function documentation and integer casts to removed signed-unsi…
Browse files Browse the repository at this point in the history
…gned comparison warnings
  • Loading branch information
Thomas Willems committed Mar 19, 2014
1 parent 91c5350 commit fad48b2
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 102 deletions.
20 changes: 8 additions & 12 deletions src/AlignmentFilters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,27 +186,24 @@ namespace AlignmentFilters {
bases = aln->nucleotides.substr(start_index, num_bases);
}


/*
*/

bool HasLargestEndMatches(AlignedRead* aln, const string& ref_seq, int ref_seq_start, int max_external, int max_internal){
// Extract sequence, start and end coordinates of read after clipping
string bases;
int start, end;
GetUnclippedInfo(aln, bases, start, end);

// Check that the prefix match is the longest
if (start >= ref_seq_start && start < ref_seq_start + ref_seq.size()){
if (start >= ref_seq_start && start < ref_seq_start + static_cast<int>(ref_seq.size())){
int start_index = start - ref_seq_start;
int start = max(0, start_index - max_external);
int stop = min((int)(ref_seq.size()-1), start_index + max_internal);
int stop = min(static_cast<int>((ref_seq.size()-1)), start_index + max_internal);
vector<int> match_counts;
ZAlgorithm::GetPrefixMatchCounts(bases, ref_seq, start, stop, match_counts);

int align_index = start_index - start;
int num_matches = match_counts[align_index];
for (unsigned int i = 0; i < match_counts.size(); i++){
for (int i = 0; i < static_cast<int>(match_counts.size()); i++){
if (i == align_index)
continue;
if (match_counts[i] >= num_matches)
Expand All @@ -215,23 +212,22 @@ namespace AlignmentFilters {
}

// Check that the suffix match is the longest
if (end >= ref_seq_start && end < ref_seq_start + ref_seq.size()){
if (end >= ref_seq_start && end < ref_seq_start + static_cast<int>(ref_seq.size())){
int end_index = end - ref_seq_start;
int start = max(0, end_index - max_internal);
int stop = min((int)(ref_seq.size()-1), end_index + max_external);
int stop = min(static_cast<int>(ref_seq.size()-1), end_index + max_external);
vector<int> match_counts;
ZAlgorithm::GetSuffixMatchCounts(bases, ref_seq, start, stop, match_counts);

int align_index = end_index - start;
int num_matches = match_counts[align_index];
for (unsigned int i = 0; i < match_counts.size(); i++){
for (int i = 0; i < static_cast<int>(match_counts.size()); i++){
if (i == align_index)
continue;
if (match_counts[i] >= num_matches)
return false;
}
}

}
return true;
}
}
6 changes: 5 additions & 1 deletion src/AlignmentFilters.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ namespace AlignmentFilters {
/* Minimum distances from 5' and 3' end of reads to first indel. If no such indel exists, returns (-1,-1). */
std::pair<int,int> GetEndDistToIndel(AlignedRead* aln);

/* Returns true iff the alignment ends match maximally compared to other positions within the specified window. */
/* Returns true iff the alignment has:
1) a maximal matching prefix compared to alignments that start [-max_upstream, max_downstream] from the 5' alignment position of the read
2) a maximal matching suffix compared to alignments that end [-max_downstream, max_upstream] from the 3' alignment position of the read
Ignores clipped bases when performing these comparions
*/
bool HasLargestEndMatches(AlignedRead* aln, const std::string& ref_seq, int ref_seq_start, int max_upstream, int max_downstream);
}

Expand Down
93 changes: 4 additions & 89 deletions src/ZAlgorithm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,47 +28,6 @@ along with lobSTR. If not, see <http://www.gnu.org/licenses/>.


namespace ZAlgorithm{
/*
void suffix_helper(int start, const std::string& s1, const std::string& s2,
std::vector<int>& s1_matches, std::vector<int>& num_matches){
num_matches = std::vector<int>(s2.size(), -1);
int leftmost = s2.size(), right_index = s2.size();
for (int i = start; i >= 0; i--){
if (i <= leftmost){
int index_a = s1.size()-1, index_b = i;
while (index_a >= 0 && index_b >= 0 && s1[index_a] == s2[index_b]){
index_a--;
index_b--;
}
num_matches[i] = i - index_b;
if (index_b < i){
right_index = i;
leftmost = index_b + 1;
}
}
else {
int twin = i - right_index + s1.size()-1;
int new_left = i - s1_matches[twin] + 1;
if (new_left > leftmost)
num_matches[i] = s1_matches[twin];
else if (new_left < leftmost)
num_matches[i] = i-leftmost+1;
else {
int index_a = s1.size()-2-i+leftmost, index_b = leftmost-1;
while (index_a >= 0 && index_b >= 0 && s1[index_a] == s2[index_b]){
index_a--;
index_b--;
}
num_matches[i] = i-index_b;
right_index = i;
leftmost = index_b + 1;
}
}
}
}
*/


void suffix_helper(const std::string& s1, const std::string& s2, int s2_left, int s2_right,
std::vector<int>& s1_matches, std::vector<int>& num_matches){
num_matches = std::vector<int>(s2_right - s2_left + 1, -1);
Expand Down Expand Up @@ -107,57 +66,14 @@ namespace ZAlgorithm{
}
}




/*
void prefix_helper(unsigned int start, const std::string& s1, const std::string& s2,
std::vector<int>& s1_matches, std::vector<int>& num_matches){
num_matches = std::vector<int>(s2.size(), -1);
int rightmost = 0, left_index = 0;
for (int i = start; i < s2.size(); i++){
if (i >= rightmost){
int index_a = 0, index_b = i;
while (index_a < s1.size() && index_b < s2.size() && s1[index_a] == s2[index_b]){
index_a++;
index_b++;
}
num_matches[i] = index_b - i;
if (index_b > i){
left_index = i;
rightmost = index_b - 1;
}
}
else {
int twin = i - left_index;
int new_right = i + s1_matches[twin] - 1;
if (new_right < rightmost)
num_matches[i] = s1_matches[twin];
else if (new_right > rightmost)
num_matches[i] = rightmost-i+1;
else {
int index_a = rightmost+1-i, index_b = rightmost+1;
while (index_a < s1.size() && index_b < s2.size() && s1[index_a] == s2[index_b]){
index_a++;
index_b++;
}
num_matches[i] = index_b - i;
left_index = i;
rightmost = index_b - 1;
}
}
}
}
*/

void prefix_helper(const std::string& s1, const std::string& s2, int s2_left, int s2_right,
std::vector<int>& s1_matches, std::vector<int>& num_matches, int offset){
num_matches = std::vector<int>(s2_right-s2_left+1+offset, -1);
int rightmost = -1, left_index = -1;
for (int i = s2_left; i <= s2_right; i++){
if (i >= rightmost){
int index_a = 0, index_b = i;
while (index_a < s1.size() && index_b < s2.size() && s1[index_a] == s2[index_b]){
while (index_a < static_cast<int>(s1.size()) && index_b < static_cast<int>(s2.size()) && s1[index_a] == s2[index_b]){
index_a++;
index_b++;
}
Expand All @@ -176,7 +92,7 @@ namespace ZAlgorithm{
num_matches[i-s2_left+offset] = rightmost-i+1;
else {
int index_a = rightmost+1-i, index_b = rightmost+1;
while (index_a < s1.size() && index_b < s2.size() && s1[index_a] == s2[index_b]){
while (index_a < static_cast<int>(s1.size()) && index_b < static_cast<int>(s2.size()) && s1[index_a] == s2[index_b]){
index_a++;
index_b++;
}
Expand All @@ -189,7 +105,6 @@ namespace ZAlgorithm{
}



void GetPrefixMatchCounts(const std::string& s1, const std::string& s2, std::vector<int>& num_matches) {
std::vector<int> s1_matches;
prefix_helper(s1, s1, 1, s1.size()-1, s1_matches, s1_matches, 1);
Expand All @@ -203,15 +118,15 @@ namespace ZAlgorithm{
}

void GetPrefixMatchCounts(const std::string& s1, const std::string& s2, int s2_start, int s2_stop, std::vector<int>& num_matches) {
if (s2_start < 0 or s2_stop >= s2.size())
if (s2_start < 0 or s2_stop >= static_cast<int>(s2.size()))
PrintMessageDieOnError("Invalid string indices provided to GetPrefixMatchCounts", ERROR);
std::vector<int> s1_matches;
prefix_helper(s1, s1, 1, s1.size()-1, s1_matches, s1_matches, 1);
prefix_helper(s1, s2, s2_start, s2_stop, s1_matches, num_matches, 0);
}

void GetSuffixMatchCounts(const std::string& s1, const std::string& s2, int s2_start, int s2_stop, std::vector<int>& num_matches) {
if (s2_start < 0 or s2_stop >= s2.size())
if (s2_start < 0 or s2_stop >= static_cast<int>(s2.size()))
PrintMessageDieOnError("Invalid string indices provided to GetSuffixMatchCounts", ERROR);
std::vector<int> s1_matches;
suffix_helper(s1, s1, 0, s1.size()-2, s1_matches, s1_matches);
Expand Down
25 changes: 25 additions & 0 deletions src/ZAlgorithm.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,33 @@ along with lobSTR. If not, see <http://www.gnu.org/licenses/>.


namespace ZAlgorithm{
/*
* For each position in s2, calculates the length of the matching prefix of s1 and s2[i...]
* and stores it in num_matches[i]. The provided vector is cleared and sized appropriately.
* Runs in O(length_of_s1 + length_of_s2)
*/
void GetPrefixMatchCounts(const std::string& s1, const std::string& s2, std::vector<int>& num_matches);

/*
* For each position in s2, calculates the length of the matching suffix of s1 and s2[0...i]
* and stores it in num_matches[i]. The provided vector is cleared and sized appropriately.
* Runs in O(length_of_s1 + length_of_s2)
*/
void GetSuffixMatchCounts(const std::string& s1, const std::string& s2, std::vector<int>& num_matches);

/*
* For each position i in s2 in the range [s2_start, s2_stop], calculates the length of
* the matching prefix of s1 and s2[i...] and stores it in num_matches[i-s2_start].
* The provided vector is cleared and sized appropriately.
* Runs in O(length_of_s1 + size_of_s2_range)
*/
void GetPrefixMatchCounts(const std::string& s1, const std::string& s2, int s2_start, int s2_stop, std::vector<int>& num_matches);

/*
* For each position i in s2 in the range [s2_start, s2_stop], calculates the length of
* the matching suffix of s1 and s2[0...i] and stores it in num_matches[i-s2_start].
* The provided vector is cleared and sized appropriately.
* Runs in O(length_of_s1 + size_of_s2_range)
*/
void GetSuffixMatchCounts(const std::string& s1, const std::string& s2, int s2_start, int s2_stop, std::vector<int>& num_matches);
}

0 comments on commit fad48b2

Please sign in to comment.