-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadaptors.cpp
More file actions
155 lines (146 loc) · 4.97 KB
/
Copy pathadaptors.cpp
File metadata and controls
155 lines (146 loc) · 4.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <iostream>
#include <fstream>
#include <stdexcept>
#include <algorithm>
#include "adaptors.hpp"
#include "sff.hpp"
namespace sff
{
/* Being AdaptorAligner implememtation */
AdaptorAligner::AdaptorAligner(const std::string &s1,
const std::string &s2) :
match_score(0),
gap_score(1),
mismatch_score(1),
p1(&s1),
p2(&s2)
{
int l1 = p1->size();
int l2 = p2->size();
/* Initialization of matrix */
scoremat.reserve(l1+1);
int i;
for (i = 0; i < l1+1; i++)
{
std::vector<int> tmp(l2+1, 0);
scoremat.push_back(tmp);
}
for (i = 0; i < l1+1; i++)
scoremat[i][0] = i*gap_score;
for (i = 0; i < l2+1; i++)
scoremat[0][i] = i*gap_score;
}
AdaptorAligner::~AdaptorAligner()
{}
int AdaptorAligner::compute_alignment_score()
{
int l1 = p1->size();
int l2 = p2->size();
int score_up, score_left, score_diag, score_match, curr_score;
std::string::const_iterator rowiterator, coliterator;
int row = 1;
int col;
for (rowiterator = p1->begin(); rowiterator != p1->end(); ++rowiterator)
{
col = 1;
for (coliterator = p2->begin(); coliterator != p2->end(); ++coliterator)
{
bool equal = (*rowiterator == *coliterator);
score_match = equal ? match_score : mismatch_score;
score_up = scoremat[row-1][col] + gap_score;
score_left = scoremat[row][col-1] + gap_score;
score_diag = scoremat[row-1][col-1] + score_match;
curr_score = std::min(
score_up, std::min(score_left, score_diag)
);
scoremat[row][col] = curr_score;
col ++;
}
row ++;
}
return scoremat[l1][l2];
}
/* Begin AdaptorFinder implementation */
AdaptorFinder::AdaptorFinder(int maxmismatch) :
maxmismatch(maxmismatch)
{
if (maxmismatch < 0)
throw std::runtime_error("maxmismatch must be greater or equal than 0");
}
AdaptorFinder::~AdaptorFinder()
{}
bool AdaptorFinder::read(const std::string &filename)
{
std::ifstream ifs(filename.c_str());
if (!ifs.is_open())
throw std::runtime_error("Could not open adaptor file for reading");
std::string name, sequence;
while (ifs >> name >> sequence)
{
/* adaptors is a hash table allowing fast adaptor sequence lookup
* length_of_adaptor -> adaptor_sequence -> adaptor_name
*/
adaptors[sequence.size()].insert(std::make_pair(sequence, name));
}
ifs.close();
return true;
}
bool AdaptorFinder::find(const SFFField &field,
std::string &match)
{
adaptormap::const_iterator sizeiter;
stringmap::const_iterator striter;
std::string sequence;
for (sizeiter = adaptors.begin(); sizeiter != adaptors.end(); ++sizeiter)
{
sequence = field.get_left_adaptor_sequence(sizeiter->first);
/* First check if adaptor runs over to actual read.
* If yes, then we consider the adaptor does not match.
*/
if (sequence.size() < sizeiter->first)
continue;
striter = sizeiter->second.find(sequence);
if (striter != sizeiter->second.end())
{
/* We found a perfect match */
match = striter->second;
return true;
}
}
/* We have not found a perfect match.
* Attempt to find an imperfect one.
*/
if (maxmismatch > 0)
return find_imperfect(field, match);
return false;
}
/* Look for imperfect match using Levenstein distance */
bool AdaptorFinder::find_imperfect(const SFFField &field,
std::string &match)
{
adaptormap::const_iterator sizeiter;
stringmap::const_iterator striter;
int best = maxmismatch+1;
int alignment = 0;
std::string sequence;
for (sizeiter = adaptors.begin();
sizeiter != adaptors.end();
++sizeiter)
{
sequence = field.get_left_adaptor_sequence(sizeiter->first);
for (striter = sizeiter->second.begin();
striter != sizeiter->second.end();
++striter)
{
AdaptorAligner sw(sequence, striter->first);
alignment = sw.compute_alignment_score();
if (alignment < best)
{
best = alignment;
match = striter->second;
}
}
}
return (best <= maxmismatch);
}
}