-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutility.cpp
180 lines (165 loc) · 5.92 KB
/
utility.cpp
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
#include "utility.hpp"
namespace vg {
char reverse_complement(const char& c) {
switch (c) {
case 'A': return 'T'; break;
case 'T': return 'A'; break;
case 'G': return 'C'; break;
case 'C': return 'G'; break;
case 'N': return 'N'; break;
// Handle the GCSA2 start/stop characters.
case '#': return '$'; break;
case '$': return '#'; break;
default: return 'N';
}
}
string reverse_complement(const string& seq) {
string rc;
rc.assign(seq.rbegin(), seq.rend());
for (auto& c : rc) {
switch (c) {
case 'A': c = 'T'; break;
case 'T': c = 'A'; break;
case 'G': c = 'C'; break;
case 'C': c = 'G'; break;
case 'N': c = 'N'; break;
// Handle the GCSA2 start/stop characters.
case '#': c = '$'; break;
case '$': c = '#'; break;
default: break;
}
}
return rc;
}
int get_thread_count(void) {
int thread_count = 1;
#pragma omp parallel
{
#pragma omp master
thread_count = omp_get_num_threads();
}
return thread_count;
}
std::vector<std::string> &split_delims(const std::string &s, const std::string& delims, std::vector<std::string> &elems) {
char* tok;
char cchars [s.size()+1];
char* cstr = &cchars[0];
strcpy(cstr, s.c_str());
tok = strtok(cstr, delims.c_str());
while (tok != NULL) {
elems.push_back(tok);
tok = strtok(NULL, delims.c_str());
}
return elems;
}
std::vector<std::string> split_delims(const std::string &s, const std::string& delims) {
std::vector<std::string> elems;
return split_delims(s, delims, elems);
}
const std::string sha1sum(const std::string& data) {
SHA1 checksum;
checksum.update(data);
return checksum.final();
}
const std::string sha1head(const std::string& data, size_t head) {
return sha1sum(data).substr(0, head);
}
bool allATGC(string& s) {
for (string::iterator c = s.begin(); c != s.end(); ++c) {
char b = *c;
if (b != 'A' && b != 'T' && b != 'G' && b != 'C') {
return false;
}
}
return true;
}
void mapping_cigar(const Mapping& mapping, vector<pair<int, char> >& cigar) {
for (const auto& edit : mapping.edit()) {
if (edit.from_length() && edit.from_length() == edit.to_length()) {
// *matches* from_length == to_length, or from_length > 0 and offset unset
// match state
cigar.push_back(make_pair(edit.from_length(), 'M'));
} else {
// mismatch/sub state
// *snps* from_length == to_length; sequence = alt
if (edit.from_length() == edit.to_length()) {
cigar.push_back(make_pair(edit.from_length(), 'M'));
} else if (edit.from_length() == 0 && edit.sequence().empty()) {
// *skip* from_length == 0, to_length > 0; implies "soft clip" or sequence skip
cigar.push_back(make_pair(edit.to_length(), 'S'));
} else if (edit.from_length() > edit.to_length()) {
// *deletions* from_length > to_length; sequence may be unset or empty
int32_t del = edit.from_length() - edit.to_length();
int32_t eq = edit.to_length();
if (eq) cigar.push_back(make_pair(eq, 'M'));
cigar.push_back(make_pair(del, 'D'));
} else if (edit.from_length() < edit.to_length()) {
// *insertions* from_length < to_length; sequence contains relative insertion
int32_t ins = edit.to_length() - edit.from_length();
int32_t eq = edit.from_length();
if (eq) cigar.push_back(make_pair(eq, 'M'));
cigar.push_back(make_pair(ins, 'I'));
}
}
}
}
string mapping_string(const string& source, const Mapping& mapping) {
string result;
int p = mapping.position().offset();
for (const auto& edit : mapping.edit()) {
// mismatch/sub state
// *matches* from_length == to_length, or from_length > 0 and offset unset
// *snps* from_length == to_length; sequence = alt
// mismatch/sub state
if (edit.from_length() == edit.to_length()) {
if (!edit.sequence().empty()) {
result += edit.sequence();
} else {
result += source.substr(p, edit.from_length());
}
p += edit.from_length();
} else if (edit.from_length() == 0 && edit.sequence().empty()) {
// *skip* from_length == 0, to_length > 0; implies "soft clip" or sequence skip
//cigar.push_back(make_pair(edit.to_length(), 'S'));
} else if (edit.from_length() > edit.to_length()) {
// *deletions* from_length > to_length; sequence may be unset or empty
result += edit.sequence();
p += edit.from_length();
} else if (edit.from_length() < edit.to_length()) {
// *insertions* from_length < to_length; sequence contains relative insertion
result += edit.sequence();
p += edit.from_length();
}
}
return result;
}
string cigar_string(vector<pair<int, char> >& cigar) {
vector<pair<int, char> > cigar_comp;
pair<int, char> cur = make_pair(0, '\0');
for (auto& e : cigar) {
if (cur == make_pair(0, '\0')) {
cur = e;
} else {
if (cur.second == e.second) {
cur.first += e.first;
} else {
cigar_comp.push_back(cur);
cur = e;
}
}
}
cigar_comp.push_back(cur);
stringstream cigarss;
for (auto& e : cigar_comp) {
cigarss << e.first << e.second;
}
return cigarss.str();
}
// todo, make a version that works for non-invariants
void divide_invariant_mapping(Mapping& orig, Mapping& left, Mapping& right, int offset, Node* nl, Node* nr) {
// an invariant mapping is, by definition, without any edits
//assert(orig.edit_size() == 0);
left.mutable_position()->set_node_id(nl->id());
right.mutable_position()->set_node_id(nr->id());
}
}