-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsequenc_pair.cpp
More file actions
227 lines (186 loc) · 4.39 KB
/
sequenc_pair.cpp
File metadata and controls
227 lines (186 loc) · 4.39 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
* sequence_pair.cpp
*
* Created on: Oct 16, 2019
* Author: rbuecki
*/
#include "sequence_pair.h"
#include <string>
#include <iostream>
// define constructors
sequence_pair::sequence_pair()
{
seq_a = "X"; // two sequences are stored in seq_a and seq_b, the order is not important
seq_b = "X";
ali_prob = 0; // the probability of the sequences under the alignment model
rand_prob = 0; // the probability of the sequences under the random model
annotation = "X";
pair_count++; // update counter of sequences
}
sequence_pair::sequence_pair(const std::string a_seq, const std::string b_seq)
{
seq_a = a_seq; // two sequences are stored in seq_a and seq_b, the order is not important
seq_b = b_seq;
ali_prob = 0; // the probability of the sequences under the alignment model
rand_prob = 0; // the probability of the sequences under the random model
annotation = "X";
pair_count--;
}
sequence_pair::sequence_pair(const sequence_pair &v)
{
seq_a = v.seq_a;
seq_b = v.seq_b;
ali_prob = v.ali_prob;
rand_prob = v.rand_prob;
annotation = v.annotation;
}
sequence_pair::~sequence_pair() {}
// other functions
//void sequence_pair::count(void)
// {
// pair_count++;
// return;
// }
void sequence_pair::set_prob(const float prob, const char Ali_or_Rand)
{
if (Ali_or_Rand == 'A')
{
ali_prob = prob;
}
else if (Ali_or_Rand == 'R')
{
rand_prob = prob;
}
else
{
std::cerr << "Invalid value for Ali_or_Rand in function get_prob in class sequence_pair\n";
exit(8);
}
return;
}
void sequence_pair::annotate_alignment(const std::string state_path)
{
annotation = state_path;
return;
}
int sequence_pair::get_count(void)
{
return(pair_count);
}
unsigned int sequence_pair::seq_len(const char A_or_B)
{
if (A_or_B == 'A')
{
return(seq_a.length());
}
else if (A_or_B == 'B')
{
return(seq_b.length());
}
else
{
std::cerr << "Invalid value for A_or_B in function seq_len in class sequence_pair\n";
exit(8);
}
return(0);
}
float sequence_pair::get_prob(const char Ali_or_Rand)
{
if (Ali_or_Rand == 'A')
{
return(ali_prob);
}
else if (Ali_or_Rand == 'R')
{
return(rand_prob);
}
else
{
std::cerr << "Invalid value for Ali_or_Rand in function get_prob in class sequence_pair\n";
exit(8);
}
return(0);
}
char sequence_pair::get_seq_char(const char A_or_B, const unsigned int pos)
{
if ((0 > pos) | (seq_len(A_or_B) <= pos))
{
std::cerr << "Position out of range in get_seq_char in class sequence_pair\n";
exit(8);
}
if (A_or_B == 'A')
{
return(seq_a.at(pos));
}
else if (A_or_B == 'B')
{
return(seq_b.at(pos));
}
else
{
std::cerr << "Invalid value for A_or_B in function get_seq_char in class sequence_pair\n";
exit(8);
}
}
std::string sequence_pair::get_annotation(void)
{
return(annotation);
}
std::string sequence_pair::insert_gaps(const char A_or_B)
{
int gap_state = -1;
if (A_or_B == 'A')
{
gap_state = '3';
}
else if (A_or_B == 'B')
{
gap_state = '2';
}
else
{
std::cerr << "Invalid value for A_or_B in function insert_gaps\n";
exit(8);
}
if (annotation == "X")
{
std::cerr << "Sequence is not annotated yet. run_viterbi has to be performed before\n";
}
std::string gap_sequence = ""; // sequence containing gaps
int seq_pos = 0; // next position of the sequence to be written
unsigned int gap_count = 0; // count the amount of gaps inserted
for (unsigned int ali_pos = 0; ali_pos < annotation.length(); ali_pos++) // loop over all alignment positions
{
if (annotation.at(ali_pos) == gap_state) // if state is emit character from other sequence, insert gap and continue
{
gap_sequence = gap_sequence + '-';
gap_count++;
continue;
}
else
{
gap_sequence = gap_sequence + get_seq_char(A_or_B, seq_pos); // if state is match or emit character from sequence, insert character from sequence and continue on sequence
seq_pos++;
}
}
// check if annotation was long enough
if (seq_len(A_or_B) != (annotation.length() - gap_count))
{
std::cerr << "Sequence with gaps is not of equal length as annotation in function insert_gaps\n";
exit(8);
}
return(gap_sequence);
}
// operators
sequence_pair& sequence_pair::operator = (const sequence_pair &pair)
{
if (this != &pair)
{
seq_a = pair.seq_a;
seq_b = pair.seq_b;
ali_prob = pair.ali_prob;
rand_prob = pair.rand_prob;
annotation = pair.annotation;
}
return(*this);
}