feature.cpp
12.7 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
#include <vector>
#include <iostream>
#include <cmath>
#include <utility>
#include <algorithm>
#include <valarray>
#include "feature.h"
#include "feature_callable.h"
#include "util.h"
namespace feature {
const std::vector<std::string> ORF::START{"AUG","ATG","TTG","UUG","CTG","CUG"};
const std::vector<std::string> ORF::END{"UAA","TAA","UAG","TAG","UGA","TGA"};
std::unordered_map<char,int> AN_ORDER{{'A',0},{'C',1},{'T',2},{'U',2},{'G',3}};
const std::string Feature::TYPE = "Feature";
const std::string SeqLength::TYPE = "SeqL";
const std::string Kmer::TYPE = "KMER";
const std::string ORF::TYPE = "ORF";
const std::string CodonBiases::TYPE = "CP";
const std::string Feature::ENTRY_TYPE = "None";
const std::string SeqLength::ENTRY_TYPE = "Sequence";
const std::string Kmer::ENTRY_TYPE = "Sequence";
const std::string ORF::ENTRY_TYPE = "Sequence";
const std::string CodonBiases::ENTRY_TYPE = "Sequence";
// Init func
void Feature::init(const Data_basic *db){
const Feature *tmp = static_cast<const Feature*>(db);
this->name = tmp->name;
}
void SeqLength::init(const Data_basic *db){
const SeqLength *tmp = static_cast<const SeqLength*>(db);
this->l = tmp->l;
}
data::Data_basic *SeqLength::clone(){
return new SeqLength(this);
}
void Kmer::init(const Data_basic *db){
const Kmer *tmp = static_cast<const Kmer*>(db);
this->k = tmp->k;
this->kmer = tmp->kmer;
}
data::Data_basic *Kmer::clone(){
return new Kmer(this);
}
void ORF::init(const Data_basic *db){
const ORF *tmp = static_cast<const ORF*>(db);
this->length = tmp->length;
this->coverage = tmp->coverage;
this->start_mean = tmp->start_mean;
this->start_std = tmp->start_std;
this->end_mean = tmp->end_mean;
this->end_std = tmp->end_std;
}
data::Data_basic *ORF::clone(){
return new ORF(this);
}
void CodonBiases::init(const Data_basic *db){
const CodonBiases *tmp = static_cast<const CodonBiases*>(db);
std::copy(std::begin(tmp->position),std::end(tmp->position),std::begin(this->position));
std::copy(std::begin(tmp->composition),std::end(tmp->composition),std::begin(this->composition));
}
data::Data_basic *CodonBiases::clone(){
return new CodonBiases(this);
}
// To string function
std::string SeqLength::to_dict(){
return std::string("\"name\" :\"") + this->name + "\"\"SeqL\" : " + std::to_string(this->l);
}
std::string SeqLength::to_csv(){
return this->name+","+std::to_string(this->l);
}
int posKmer(std::string id,unsigned int k){
int pos(0);
for(unsigned int i(0); i < id.size(); i++){
int tmp = AN_ORDER[id[i]];
if(tmp>0){
pos += tmp*(int)(pow(4.0,k-i-1));
}
}
return pos;
}
std::string Kmer::to_dict(){
std::string tmp(std::string("\"name\" :\"") + this->name+"\"");
auto it = this->kmer.begin();
while(it != this->kmer.end()){
tmp += ", \""+it->first + "\": " + std::to_string(it->second);
++it;
}
return tmp;
}
std::string Kmer::to_csv(){
std::vector<float> vect((int)pow(4.0,k),0.0);
auto it = this->kmer.begin();
while(it != this->kmer.end()){
vect[posKmer(it->first,this->k)] = it->second;
++it;
}
std::string tmp(this->name);
for(unsigned int i(0); i < vect.size(); i++){
tmp += ","+ std::to_string(vect[i]);
}
return tmp;
}
std::string ORF::to_dict(){
return std::string("\"name\" :\"") + this->name + "\"\"Length\": " + std::to_string(this->length) + ", \"Coverage\" : " + std::to_string(this->coverage) + ", \"Start mean\" : " + std::to_string(this->start_mean) + ", \"Start std\" : " + std::to_string(this->start_std) + ", \"End mean\" : " + std::to_string(this->end_mean) + ", \"End std\" : " + std::to_string(this->end_std);
}
std::string ORF::to_csv(){
return this->name+"," +
std::to_string(this->length) + "," +
std::to_string(this->coverage) + "," +
std::to_string(this->coverage_all_mean) + "," +
std::to_string(this->coverage_all_std) + "," +
std::to_string(this->frame_biases) + "," +
std::to_string(this->frequency) + "," +
std::to_string(this->start_mean) + "," +
std::to_string(this->start_std) + "," +
std::to_string(this->end_mean) + "," +
std::to_string(this->end_std);
}
std::string CodonBiases::to_dict(){
return std::string("\"name\" :\"") + this->name +
"\", \"Position A\" : " + std::to_string(this->position[0]) +
", \"Position C\" : " + std::to_string(this->position[1]) +
", \"Position T\" : " + std::to_string(this->position[2]) +
", \"Position G\" : " + std::to_string(this->position[3])+
", \"Composition A\" : " + std::to_string(this->composition[0]) +
", \"Composition C\" : " + std::to_string(this->composition[1]) +
", \"Composition T\" : " + std::to_string(this->composition[2]) +
", \"Composition G\" : " + std::to_string(this->composition[3]);
}
std::string CodonBiases::to_csv(){
return this->name+","+
std::to_string(this->position[0]) + ","+
std::to_string(this->position[1]) + ","+
std::to_string(this->position[2]) + ","+
std::to_string(this->position[3]) + ","+
std::to_string(this->composition[0]) + ","+
std::to_string(this->composition[1]) + ","+
std::to_string(this->composition[2]) + ","+
std::to_string(this->composition[3])+ ","+
std::to_string(this->gc_content);
}
// Constructor
// Kmer
Kmer::Kmer(entry::Sequence &s, const unsigned int &k):Feature(s.getName()),k(k){
std::string seq = s.getSeq();
for(unsigned int i(0); i < s.getLength() - this->k + 1; i++){
std::string kmer = seq.substr(i,this->k);
std::transform(kmer.begin(),kmer.end(),kmer.begin(),::toupper);
std::replace(kmer.begin(),kmer.end(),'U','T');
std::unordered_map<std::string,float>::iterator kmer_it = this->kmer.find(kmer);
if(kmer_it != this->kmer.end()){
kmer_it->second ++;
}else{
this->kmer[kmer] = 1;
}
}
// compute frequence
for(std::unordered_map<std::string,float>::iterator it = this->kmer.begin(); it!= this->kmer.end();++it){
it->second /=(s.getLength()-(it->first.length()));
}
}
// ORF
ORF::ORF(const entry::Sequence &s):Feature(s.getName()),length(0){
std::string seq = s.getSeq();
std::transform(seq.begin(),seq.end(),seq.begin(),toupper);
std::vector<unsigned int> start_codon;
std::vector<unsigned int> end_codon;
std::vector<std::tuple<unsigned int, unsigned int, unsigned int, unsigned int>> orfs;
//find position codon start and stop
for(unsigned int i(0); i < seq.length(); i++){
std::string tmp = seq.substr(i,3);
if(ORF::in_start(tmp)){
start_codon.push_back(i);
}
if(ORF::in_end(tmp)){
end_codon.push_back(i);
}
}
// ORF detection
for(auto const &start_pos: start_codon){
if(!end_codon.empty()){
if(end_codon.back() - start_pos < this->length){
break;
}
for(auto const &end_pos: end_codon){
if(start_pos < end_pos){
unsigned int length = end_pos - start_pos;
unsigned int frame = start_pos % 3;
if((length%3 == 0)){
orfs.push_back(std::tuple<unsigned int, unsigned int, unsigned int, unsigned int>(frame,length,start_pos,end_pos));
if(length > this->length){
this->length = length;
}
break;
}
}
}
}
}
if(orfs.size() > 0){
int orf_per_frame[3] = {0,0,0};
for(auto const &orf: orfs){
this->coverage_all_mean += static_cast<float>(std::get<1>(orf))/s.getLength();
orf_per_frame[std::get<0>(orf)] += 1;
this->start_mean += std::get<2>(orf);
this->end_mean += std::get<3>(orf);
}
this->start_mean = this->start_mean / orfs.size() / s.getLength();
this->end_mean = this->end_mean / orfs.size() /s.getLength();
this->coverage_all_mean = this->coverage_all_mean / orfs.size();
this->frequency = static_cast<float>(orfs.size())/start_codon.size();
auto mm = std::minmax_element(std::begin(orf_per_frame),std::end(orf_per_frame));
this->frame_biases = 1.0 - static_cast<float>(*mm.first)/
static_cast<float>(*mm.second);
for(auto const &orf: orfs){
this->coverage_all_std += pow(static_cast<float>(std::get<1>(orf))/s.getLength() - this->coverage_all_mean,2.0);
this->start_std += pow(static_cast<float>(std::get<2>(orf))/s.getLength() - this->start_mean,2.0);
this->end_std += pow(static_cast<float>(std::get<3>(orf))/s.getLength() - this->end_mean,2.0);
}
this->coverage_all_std =sqrt(this->coverage_all_std / orfs.size());
this->start_std = sqrt(this->start_std / orfs.size());
this->end_std = sqrt(this->end_std / orfs.size());
this->coverage = static_cast<float>(this->length) / s.getLength();
//this->length = exp(-static_cast<float>(this->length)/200.0);
this->length = log10(static_cast<float>(this->length));
}
}
// Codon position
CodonBiases::CodonBiases(const entry::Sequence &seq):Feature(seq.getName()){
std::string s = seq.getSeq();
std::valarray<int> mat(4*3);
for(unsigned int i(0); i < s.length(); i++){
//std::cout << s.at(i) << " / " << i << std::endl;
switch(s.at(i)){
case 'A':
case 'a':
mat[0+(i%3)] += 1;
this->composition[0] += 1;
break;
case 'C':
case 'c':
mat[3+(i%3)] += 1;
this->composition[1] += 1;
this->gc_content += 1;
break;
case 'T':
case 't':
case 'U':
case 'u':
mat[6+(i%3)] += 1;
this->composition[2] += 1;
break;
case 'G':
case 'g':
mat[9+(i%3)] += 1;
this->composition[3] += 1;
this->gc_content += 1;
break;
}
}
for(int i(0); i < 4; i++){
std::valarray<int> tmp(mat[std::slice(i*3,3,1)]);
auto mm = std::minmax_element(std::begin(tmp),std::end(tmp));
this->position[i] = static_cast<float>(*mm.first)/
(static_cast<float>(*mm.second) + 1.0);
this->composition[i] /= seq.getLength();
}
this->gc_content /= seq.getLength();
}
// Dist
float Kmer::dist(Feature *ka){
Kmer* ptr_a = static_cast<Kmer*>(ka);
Kmer a(ptr_a);
std::unordered_map<std::string,float> b_kmer = this->kmer;
float dist = 0;
while(a.kmer.size() > 0){
auto it_a = a.kmer.begin();
auto it_b = b_kmer.find(it_a->first);
float value_b = 0;
if(it_b != b_kmer.end()){
value_b = it_b->second;
b_kmer.erase(it_b);
}
dist += std::abs(it_a->second - value_b);
a.kmer.erase(it_a);
}
for(auto it_b = b_kmer.begin(); it_b!= b_kmer.end();++it_b){
dist += std::abs(it_b->second);
}
return dist;
}
float SeqLength::dist(Feature *a){
SeqLength *tmp = static_cast<SeqLength*>(a);
float res = static_cast<float>(std::abs(this->l - tmp->l));
return res;
}
float ORF::dist(Feature *a){
ORF *tmp = static_cast<ORF*>(a);
float res = std::abs(this->coverage - tmp->coverage);
return res;
}
float CodonBiases::dist(Feature *a){
CodonBiases *tmp = static_cast<CodonBiases*>(a);
float res(0.0);
for(int i(0); i < 4; i++){
res += std::abs(this->position[i] - tmp->position[i]);
}
return res;
}
// Other
int SeqLength::getL() const
{
return l;
}
unsigned int Kmer::getK() const
{
return k;
}
std::unordered_map<std::string, float> Kmer::getKmer() const
{
return kmer;
}
bool ORF::in_start(const std::string &c){
for(auto const &codon: ORF::START){
if(c.compare(codon)==0){
return true;
}
}
return false;
}
bool ORF::in_end(const std::string &c){
for(auto const &codon: ORF::END){
if(c.compare(codon)==0){
return true;
}
}
return false;
}
unsigned int ORF::getLength() const
{
return length;
}
float ORF::getCoverage() const
{
return coverage;
}
unsigned int ORF::getMax_length() const
{
return length;
}
const float *CodonBiases::getPosition(){
return this->position;
}
/*
* Callable definition
*/
class SeqLength_callable: public feature::Feature_creation{
protected:
feature::Feature *create_feature(data::Data_basic *e){
entry::Sequence *tmp = dynamic_cast<entry::Sequence*>(e);
return new SeqLength(*tmp);
}
public:
SeqLength_callable(){}
};
class Kmer_callable: public feature::Feature_creation{
protected:
unsigned int k;
feature::Feature *create_feature(data::Data_basic *e){
entry::Sequence *tmp = dynamic_cast<entry::Sequence*>(e);
return new Kmer(*tmp,this->k);
}
public:
Kmer_callable(const unsigned int &k):k(k){}
};
class ORF_callable: public feature::Feature_creation{
protected:
feature::Feature *create_feature(data::Data_basic *e){
entry::Sequence *tmp = dynamic_cast<entry::Sequence*>(e);
return new ORF(*tmp);
}
public:
ORF_callable(){}
};
class CodonBiases_callable: public feature::Feature_creation{
protected:
feature::Feature *create_feature(data::Data_basic *e){
entry::Sequence *tmp = dynamic_cast<entry::Sequence*>(e);
return new CodonBiases(*tmp);
}
public:
CodonBiases_callable(){}
};
callable::Callable<data::Data_basic*,data::Data_basic*> *SeqLength::get_callable(){
return new feature::SeqLength_callable();
}
callable::Callable<data::Data_basic*,data::Data_basic*> *Kmer::get_callable(const unsigned int &k){
return new feature::Kmer_callable(k);
}
callable::Callable<data::Data_basic*,data::Data_basic*> *ORF::get_callable(){
return new feature::ORF_callable();
}
callable::Callable<data::Data_basic*,data::Data_basic*> *CodonBiases::get_callable(){
return new feature::CodonBiases_callable();
}
CodonBiases::~CodonBiases(){}
}