-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpeptide.cpp
1765 lines (1651 loc) · 52 KB
/
peptide.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
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
#include <regex>
#include <iterator>
#include <type_traits>
#include <map>
#include <chrono>
#include <numeric>
#include <cstdint>
#include <random>
// define static functions
std::size_t compute_original_index(std::size_t aa_pos, std::size_t orig_len, std::size_t offset, std::size_t search_aa_len, bool is_reverse);
template<typename int_type>
int_type str_to_int_type(std::string &text);
// define Objects
template<typename int_type>
class intVectorFiller {
std::vector<int_type>& v;
public:
intVectorFiller(std::vector<int_type>& v);
void operator()(std::string& item);
};
class InputData {
private:
// regexes
static std::regex whitespace_trimmer_regex_lead_and_trail;
static std::regex whitespace_trimmer_regex_lead_only;
static std::regex whitespace_trimmer_regex_trail_only;
static std::regex whitespace_re;
std::queue<std::string> data;
public:
InputData();
template<typename int_type>
int_type next_as_int_type();
template<typename int_type>
void next_into_int_type_vector(std::vector<int_type> &intv, std::string &separator);
template<typename int_type>
void rest_into_int_type_vector(std::vector<int_type> &intv);
void next_into_str_vector(std::vector<std::string> &strv, std::string &separator);
void rest_into_str_vector(std::vector<std::string> &strv);
std::string next();
std::size_t size();
std::string StripWhitespace(std::string& text);
std::string StripLeadingWhitespace(std::string& text);
std::string StripTrailingWhitespace(std::string& text);
};
//instantiate static variables
std::regex InputData::whitespace_trimmer_regex_lead_and_trail("\\s+$|^\\s+");
std::regex InputData::whitespace_trimmer_regex_lead_only("^\\s*");
std::regex InputData::whitespace_trimmer_regex_trail_only("\\s*$");
std::regex InputData::whitespace_re("\\s+");
//implement object functions
InputData::InputData() {
std::string inputline;
while(std::getline(std::cin, inputline)){
inputline = StripWhitespace(inputline);
this->data.push(inputline);
}
}
template<typename int_type>
int_type str_to_int_type(std::string &text){
std::string t(text);
if(t.find(',') >= 0) {
t.erase(std::remove(t.begin(), t.end(), ','), t.end());
}
std::stringstream ss(t);
int_type i;
ss>>i;
return i;
}
template<typename int_type>
intVectorFiller<int_type>::intVectorFiller(std::vector<int_type>& v): v(v){}
template<typename int_type>
void intVectorFiller<int_type>::operator()(std::string& item) {
v.push_back(str_to_int_type<int_type>(item));
}
template<typename int_type>
int_type InputData::next_as_int_type(){
int_type next = str_to_int_type<int_type>(this->data.front());
this->data.pop();
return next;
}
template<typename int_type>
void InputData::next_into_int_type_vector(std::vector<int_type> &intv, std::string &separator){
std::vector<std::string> str_form;
this->next_into_str_vector(str_form, separator);
std::for_each(str_form.begin(),str_form.end(),intVectorFiller<int_type>(intv));
}
template<typename int_type>
void InputData::rest_into_int_type_vector(std::vector<int_type> &intv){
std::vector<std::string> str_form;
this->rest_into_str_vector(str_form);
std::for_each(str_form.begin(),str_form.end(),intVectorFiller<int_type>(intv));
}
std::string InputData::next(){
std::string next(this->data.front().c_str());
this->data.pop();
return next;
}
std::size_t InputData::size(){
return this->data.size();
}
void InputData::rest_into_str_vector(std::vector<std::string> &strv) {
while(this->data.size() > 0){
strv.push_back(this->next());
}
}
void InputData::next_into_str_vector(std::vector<std::string> &strv, std::string &separator) {
std::regex re(separator);
std::string &next = this->data.front();
std::sregex_token_iterator first{next.begin(), next.end(), re, -1}, last;
std::copy(first, last, std::back_inserter<std::vector<std::string> >(strv));
this->data.pop();
}
std::string InputData::StripWhitespace(std::string& text){
return std::regex_replace(text, whitespace_trimmer_regex_lead_and_trail, "", std::regex_constants::match_any | std::regex_constants::format_sed);
}
std::string InputData::StripLeadingWhitespace(std::string& text){
return std::regex_replace(text, whitespace_trimmer_regex_lead_only, "", std::regex_constants::match_any | std::regex_constants::format_sed);
}
std::string InputData::StripTrailingWhitespace(std::string& text){
return std::regex_replace(text, whitespace_trimmer_regex_trail_only, "", std::regex_constants::match_any | std::regex_constants::format_sed);
}
//template <typename T, template<typename...> class C>
template <typename T, template<typename...> class Container>
std::string join(Container<T> &c, std::string s){
if(c.size() == 0){
return "";
}
std::stringstream ss;
std::copy(c.begin(), c.end()-1, std::ostream_iterator<T>(ss, s.c_str()));
ss << c[c.size() -1];
return ss.str();
}
struct Spectrum{
std::vector<std::size_t> masses;
std::map<std::size_t, std::size_t> mass_count;
Spectrum(std::vector<std::size_t>& spectrum);
std::vector<std::size_t> to_vector();
std::string to_string();
std::string to_table_string();
std::size_t parentmass();
bool operator==(Spectrum other);
Spectrum convolve();
};
class Peptide {
public:
enum PeptideCode {
STOP,
HISTIDINE,
GLUTAMINE,
PROLINE,
ARGININE,
LEUCINE,
ASPARTIC_ACID,
GLUTAMIC_ACID,
ALANINE,
GLYCINE,
VALINE,
TYROSINE,
SERINE,
CYSTEINE,
TRYPTOPHAN,
PHENYLALANINE,
ASPARAGINE,
LYSINE,
THREONINE,
ISOLEUCINE,
METHIONINE,
NONPROTEINOGENIC
};
static Peptide translateDNA(std::string& dnaseq);
static std::string transcribeDNA(std::string& dnaseq);
static Peptide translateRNA(std::string& rnaseq);
static std::string PeptideCode_to_string(PeptideCode aa);
static std::string PeptideCode_to_abbrev(PeptideCode aa);
static char PeptideCode_to_symbol(PeptideCode aa);
static PeptideCode codon_to_peptide(char cod1, char cod2, char cod3);
static int number_of_codons(PeptideCode aa);
static PeptideCode symbol_to_peptide(char symb);
static std::string revcomp(std::string& dna, bool rna_mode=false);
static char complement(char nuc, bool rna_mode=false);
static std::size_t getMassFor(PeptideCode aa);
static Peptide::PeptideCode getPeptideCodeFor(std::size_t mass);
static std::vector<Peptide> cyclopeptide_sequencing(Spectrum& spectrum);
static Peptide leaderboard_cyclopeptide_sequencing(Spectrum& spectrum, std::size_t N, bool extended=false);
static std::vector<Peptide> leaderboard_cyclopeptide_sequencing_all(Spectrum& spectrum, std::size_t N, bool extended=false);
template <typename Iter>
static std::string pep_container_to_string(Iter it, Iter end, std::string separator="\n", bool include_stops=false);
bool is_consistent_with_spectrum(Spectrum& spectrum);\
Spectrum * linspec_ptr();
Spectrum * cycspec_ptr();
// static std::size_t get_peps_with_mass(std::size_t mass);
Peptide();
Peptide(std::string aastr);
Peptide(PeptideCode& aa);
Peptide(std::vector<PeptideCode>& aa);
Peptide(std::vector<std::size_t>& mass_seq);
Peptide(const Peptide& other);
Peptide& operator=(const Peptide & other);
~ Peptide ();
void addAminoAcid(PeptideCode aa);
void addAminoAcidMass(std::size_t mass);
std::string to_mass_string(bool include_stops=false);
template <typename Iter>
static std::string pep_container_to_mass_string(Iter it, Iter end, std::string separator="\n", bool include_stops=false);
std::string to_string(bool include_stops=false);
std::string to_abbrev_string(bool include_stops=false);
std::string to_fullword_string(bool include_stops=false);
std::size_t number_of_sequences(bool include_stops=false);
std::vector<std::string> subseqs_encoding(std::string& dna);
std::size_t size(bool include_stops=false);
std::size_t nuc_size(bool include_stops=false);
bool operator ==(const Peptide prot2) const;
bool operator !=(const Peptide prot2) const;
bool operator <(const Peptide prot2) const;
bool operator <=(const Peptide prot2) const;
bool operator >(const Peptide prot2) const;
bool operator >=(const Peptide prot2) const;
bool isSameAs(const Peptide &prot2, bool include_stops=false);
Peptide subseq(std::size_t i, std::size_t len);
Spectrum &linear_spectrum();
Spectrum &cyclic_spectrum();
std::size_t get_mass();
Peptide prefix(std::size_t len);
std::vector<Peptide> expand(bool extended=false);
bool has_spectrum(Spectrum& spectrum, bool cyclic=false);
std::size_t score_spectrum(Spectrum& spectrum, bool cyclic=false);
PeptideCode aa_at(std::size_t index);
std::size_t aa_mass_at(std::size_t index);
static std::vector<std::size_t> get_aa_mass_list();
static std::vector<std::size_t> get_aa_mass_list_extended();
// bool bind();
static void set_extended(Spectrum s,std::size_t M);
private:
std::size_t totalmass;
std::vector<PeptideCode> sequence;
std::vector<std::size_t> mass_sequence;
Spectrum * linspectrum;
Spectrum * cycspectrum;
static std::vector<PeptideCode> aa_list;
static std::vector<std::size_t> aa_mass_list;
static std::vector<std::size_t> aa_mass_list_extended;
void invalidate_spectra();
void generate_linear_spectrum();
void generate_cyclic_spectrum();
};
Peptide::Peptide(const Peptide& other):totalmass(other.totalmass), sequence(other.sequence), mass_sequence(other.mass_sequence),linspectrum(NULL),cycspectrum(NULL){
// std::cerr<<"[copy constructor] executed"<<std::endl;
if(other.linspectrum != NULL){
// std::cerr<<"[copy constructor] linspectrum was not NULL"<<std::endl;
this->linspectrum = new Spectrum(*(other.linspectrum));
// } else {
// std::cerr<<"[copy constructor] linspectrum was NULL"<<std::endl;
}
if(other.cycspectrum != NULL){
// std::cerr<<"[copy constructor] cycspectrum was not NULL"<<std::endl;
this->cycspectrum = new Spectrum(*(other.cycspectrum));
// } else {
// std::cerr<<"[copy constructor] cycspectrum was NULL"<<std::endl;
}
}
Peptide& Peptide::operator=(const Peptide & other){
// std::cerr<<"[operator =] executed"<<std::endl;
this->invalidate_spectra();
this->totalmass = other.totalmass;
this->sequence = other.sequence;
this->mass_sequence = other.mass_sequence;
if(other.linspectrum != NULL){
// std::cerr<<"[operator =] linspectrum was not NULL"<<std::endl;
this->linspectrum = new Spectrum(*(other.linspectrum));
// } else {
// std::cerr<<"[operator =] linspectrum was NULL"<<std::endl;
}
if(other.cycspectrum != NULL){
// std::cerr<<"[operator =] cycspectrum was not NULL"<<std::endl;
this->cycspectrum = new Spectrum(*(other.cycspectrum));
// } else {
// std::cerr<<"[operator =] cycspectrum was NULL"<<std::endl;
}
}
Spectrum * Peptide::linspec_ptr(){ return this->linspectrum;}
Spectrum * Peptide::cycspec_ptr(){return this->cycspectrum;}
Spectrum Spectrum::convolve(){
std::vector<std::size_t> spec = this->to_vector();
std::vector<std::size_t> conv_v;
for(std::size_t i=0;i<spec.size()-1;i++){
std::size_t smallmass = spec[i];
for(std::size_t j=i+1;j<spec.size();j++){
std::size_t bigmass = spec[j];
if(smallmass != bigmass) {
// std::cout << "smallmass: " << smallmass << ", bigmass:" << bigmass << std::endl;
conv_v.push_back(bigmass - smallmass);
}
}
}
return Spectrum(conv_v);
}
bool Spectrum::operator==(Spectrum other){
return this->mass_count == other.mass_count;
}
Spectrum::Spectrum(std::vector<std::size_t>& spectrum){
for(std::size_t i =0;i< spectrum.size(); i++ ){
std::size_t mass = spectrum[i];
if(!this->mass_count.count(mass)){
this->masses.push_back(mass);
this->mass_count[mass] = std::count(spectrum.begin(),spectrum.end(), mass);
}
}
std::sort(this->masses.begin(),this->masses.end());
}
std::size_t Spectrum::parentmass(){
return this->masses[this->masses.size()-1];
}
std::vector<std::size_t> Spectrum::to_vector(){
std::vector<std::size_t> spect;
for(std::size_t i =0 ;i<this->masses.size();i++){
std::size_t mass = this->masses[i];
std::size_t count = this->mass_count[mass];
for(std::size_t j = 0; j<count;j++){
spect.push_back(mass);
}
}
return spect;
}
std::string Spectrum::to_string(){
std::stringstream ss;
for(std::size_t i=0;i<this->masses.size();i++){
for(std::size_t j = 0; j<this->mass_count[this->masses[i]]; j++) {
if(!(i == 0 && j == 0)){
ss <<" ";
}
ss << this->masses[i];
}
}
return ss.str();
}
std::string Spectrum::to_table_string(){
std::stringstream ss;
for(std::size_t i=0;i<this->masses.size();i++){
ss<<this->masses[i]<<"\t"<<this->mass_count[this->masses[i]]<<std::endl;
}
return ss.str();
}
std::vector<std::size_t> Peptide::get_aa_mass_list(){
return Peptide::aa_mass_list;
}
std::vector<std::size_t> Peptide::get_aa_mass_list_extended(){
return Peptide::aa_mass_list_extended;
}
Peptide::PeptideCode Peptide::aa_at(std::size_t index){
return this->sequence[index];
}
std::size_t Peptide::aa_mass_at(std::size_t index){
return Peptide::getMassFor(this->sequence[index]);
}
std::size_t Peptide::get_mass(){
return this->totalmass;
}
void Peptide::invalidate_spectra(){
if(this->cycspectrum != NULL){
delete this->cycspectrum;
this->cycspectrum = NULL;
}
if(this->linspectrum != NULL){
delete this->linspectrum;
this->linspectrum = NULL;
}
}
void Peptide::addAminoAcid(PeptideCode aa){
this->sequence.push_back(aa);
this->mass_sequence.push_back(Peptide::getMassFor(aa));
this->totalmass += this->mass_sequence.back();
this->invalidate_spectra();
}
void Peptide::addAminoAcidMass(std::size_t mass){
this->mass_sequence.push_back(mass);
this->sequence.push_back(Peptide::getPeptideCodeFor(mass));
this->totalmass += mass;
this->invalidate_spectra();
}
//Peptide::Peptide(Peptide& other):totalmass(other.totalmass), sequence(other.sequence){}
Peptide::~Peptide (){
this->invalidate_spectra();
}
Peptide::Peptide():totalmass(0), sequence(), mass_sequence(), linspectrum(NULL), cycspectrum(NULL){}
Peptide::Peptide(PeptideCode& aa):totalmass(0), sequence(), mass_sequence(), linspectrum(NULL), cycspectrum(NULL){
this->addAminoAcid(aa);
}
Peptide::Peptide(std::vector<PeptideCode>& aaseq):totalmass(0), sequence(), mass_sequence(), linspectrum(NULL), cycspectrum(NULL){
std::for_each(aaseq.begin(), aaseq.end(),std::bind1st(std::mem_fun(&Peptide::addAminoAcid), this));
}
Peptide::Peptide(std::vector<std::size_t>& mass_seq):totalmass(0), sequence(), mass_sequence(), linspectrum(NULL), cycspectrum(NULL){
std::for_each(mass_seq.begin(), mass_seq.end(),std::bind1st(std::mem_fun(&Peptide::addAminoAcidMass), this));
}
Peptide::Peptide(std::string aastr):totalmass(0), sequence(), mass_sequence(), linspectrum(NULL), cycspectrum(NULL){
for(std::size_t i =0;i < aastr.size();i++){
this->addAminoAcid(Peptide::symbol_to_peptide(aastr[i]));
}
// std::cout<<"number of peptide entries: "<<this->sequence.size()<<std::endl<<std::flush;
}
Peptide Peptide::prefix(std::size_t len){
return this->subseq(0,len);
}
template <typename Iter>
std::string Peptide::pep_container_to_string(Iter it, Iter end, std::string separator, bool include_stops){
std::vector<std::string> pstr;
for(; it < end ; it++){
Peptide current = *it;
pstr.push_back(current.to_string(include_stops));
}
return join(pstr, separator);
}
template <typename Iter>
std::string Peptide::pep_container_to_mass_string(Iter it, Iter end, std::string separator, bool include_stops){
std::vector<std::string> pstr;
for(; it < end ; it++){
Peptide current = *it;
pstr.push_back(current.to_mass_string(include_stops));
}
return join(pstr, separator);
}
std::string Peptide::to_mass_string(bool include_stops){
if(!include_stops) {
std::vector<std::size_t> sizes;
for (std::size_t i = 0; i < this->mass_sequence.size(); i++) {
std::size_t current = this->mass_sequence[i];
if (current == 0) {
break;
}
sizes.push_back(current);
}
return join(sizes, "-");
} else {
return join(this->mass_sequence, "-");
}
}
std::string Peptide::to_string(bool include_stops){
std::vector<char> aastr;
for(std::vector<Peptide::PeptideCode>::iterator it = this->sequence.begin(); it < this->sequence.end(); it++){
Peptide::PeptideCode current = *it;
if((! include_stops) && current == Peptide::PeptideCode::STOP){
break;
}
aastr.push_back(Peptide::PeptideCode_to_symbol(current));
}
return join(aastr, "");
}
std::string Peptide::to_abbrev_string(bool include_stops){
std::vector<std::string> abbrs;
for(std::size_t i = 0; i < this->sequence.size() ;i++){
Peptide::PeptideCode current = this->sequence[i];
if((! include_stops) && current == Peptide::PeptideCode::STOP){
break;
}
abbrs.push_back(Peptide::PeptideCode_to_abbrev(current));
}
return join(abbrs, "");
}
std::string Peptide::to_fullword_string(bool include_stops){
std::vector<std::string> words;
for(std::size_t i = 0; i < this->sequence.size() ;i++){
Peptide::PeptideCode current = this->sequence[i];
if((! include_stops) && current == Peptide::PeptideCode::STOP){
break;
}
words.push_back(Peptide::PeptideCode_to_string(current));
}
return join(words, "");
}
std::size_t Peptide::number_of_sequences(bool include_stops){
std::size_t total = 1;
int num_codons;
for(std::size_t i =0;i < this->sequence.size();i++){
Peptide::PeptideCode current = this->sequence[i];
if((! include_stops) && current == Peptide::PeptideCode::STOP){
break;
}
num_codons = Peptide::number_of_codons(current);
// std::cout<< Peptide::PeptideCode_to_string(current) << "\t" <<Peptide::PeptideCode_to_symbol(current) << "\t" << Peptide::number_of_codons(current)<<std::endl<<std::flush;
total *= num_codons;
}
return total;
}
Peptide Peptide::translateDNA(std::string& dnaseq){
// std::cout<<"[Peptide::translateDNA] before: '"<<dnaseq<<"'"<<std::endl<<std::flush;
std::string rnaseq(Peptide::transcribeDNA(dnaseq));
// std::cout<<"[Peptide::translateDNA] after: '"<<rnaseq<<"'"<<std::endl<<std::flush;
return Peptide::translateRNA(rnaseq);
}
std::string Peptide::transcribeDNA(std::string& dnaseq){
std::string rnaseq(dnaseq);
std::replace(rnaseq.begin(),rnaseq.end(),'T','U');
return rnaseq;
}
Peptide Peptide::translateRNA(std::string& rnaseq){
std::vector<PeptideCode> aaseq;
for(std::size_t i = 0; i+2 < rnaseq.size() ; i+=3){
aaseq.push_back(Peptide::codon_to_peptide(rnaseq[i],rnaseq[i+1],rnaseq[i+2]));
}
Peptide prot(aaseq);
return prot;
}
bool Peptide::isSameAs(const Peptide &prot2, bool include_stops){
if(this->sequence.size() != prot2.sequence.size()){
return false;
}
if(this->mass_sequence.size() != prot2.mass_sequence.size()){
return false;
}
//then by mass
if(this->totalmass != prot2.totalmass){
return false;
}
for(std::size_t i = 0; i < this->sequence.size() ;i++){
if(this->sequence[i] != prot2.sequence[i]){
return false;
}
if((!include_stops) && this->sequence[i] == Peptide::PeptideCode::STOP){
break;
}
}
for(std::size_t i = 0; i < this->mass_sequence.size() ;i++){
if(this->mass_sequence[i] != prot2.mass_sequence[i]){
return false;
}
if((!include_stops) && this->mass_sequence[i] == 0){
break;
}
}
return true;
}
bool Peptide::operator ==(const Peptide prot2) const{
//first by length
if(this->sequence.size() != prot2.sequence.size()){
return false;
}
if(this->mass_sequence.size() != prot2.mass_sequence.size()){
return false;
}
//then by mass
if(this->totalmass != prot2.totalmass){
return false;
}
// std::string mystr(this->to_string()), prot2str(prot2.to_string());
for(std::size_t i = 0; i< this->sequence.size();i++){
if(this->sequence[i] != prot2.sequence[i]){
return false;
}
}
for(std::size_t i = 0; i< this->mass_sequence.size();i++){
if(this->mass_sequence[i] != prot2.mass_sequence[i]){
return false;
}
}
return true;
}
bool Peptide::operator !=(const Peptide prot2) const{
return ! (this->operator==(prot2));
}
bool Peptide::operator <(const Peptide prot2) const{
//first by length
if(this->sequence.size() < prot2.sequence.size()){
return true;
} else if (this->sequence.size() > prot2.sequence.size()){
return false;
}
//then by mass
if(this->totalmass < prot2.totalmass){
return true;
} else if (this->totalmass > prot2.totalmass){
return false;
}
for(std::size_t i = 0; i< this->mass_sequence.size();i++){
if(this->mass_sequence[i] < prot2.mass_sequence[i]){
return true;
} else if(this->mass_sequence[i] > prot2.mass_sequence[i]){
return false;
}
}
return false;
}
bool Peptide::operator >(const Peptide prot2) const{
//first by length
if(this->sequence.size() > prot2.sequence.size()){
return true;
} else if (this->sequence.size() < prot2.sequence.size()){
return false;
}
//then by mass
if(this->totalmass > prot2.totalmass){
return true;
} else if (this->totalmass < prot2.totalmass){
return false;
}
for(std::size_t i = 0; i< this->mass_sequence.size();i++){
if(this->mass_sequence[i] > prot2.mass_sequence[i]){
return true;
} else if(this->mass_sequence[i] < prot2.mass_sequence[i]){
return false;
}
}
return false;
}
bool Peptide::operator <=(const Peptide prot2) const{
//first by length
if(this->sequence.size() < prot2.sequence.size()){
return true;
} else if (this->sequence.size() > prot2.sequence.size()){
return false;
}
//then by mass
if(this->totalmass < prot2.totalmass){
return true;
} else if (this->totalmass > prot2.totalmass){
return false;
}
// std::string mystr(this->to_string()), prot2str(prot2.to_string());
for(std::size_t i = 0; i< this->mass_sequence.size();i++){
if(this->mass_sequence[i] < prot2.mass_sequence[i]){
return true;
} else if(this->mass_sequence[i] > prot2.mass_sequence[i]){
return false;
}
}
return true;
}
bool Peptide::operator >=(const Peptide prot2) const{
//first by length
if(this->sequence.size() > prot2.sequence.size()){
return true;
} else if (this->sequence.size() < prot2.sequence.size()){
return false;
}
//then by mass
if(this->totalmass > prot2.totalmass){
return true;
} else if (this->totalmass < prot2.totalmass){
return false;
}
for(std::size_t i = 0; i< this->mass_sequence.size();i++){
if(this->mass_sequence[i] > prot2.mass_sequence[i]){
return true;
} else if(this->mass_sequence[i] < prot2.mass_sequence[i]){
return false;
}
}
return true;
}
Peptide Peptide::subseq(std::size_t i, std::size_t len){
Peptide newpep;
for(std::size_t j = i; j - i < len && j < this->sequence.size(); j++){
if(this->sequence[j] == PeptideCode::NONPROTEINOGENIC){
newpep.addAminoAcidMass(this->mass_sequence[j]);
} else {
newpep.addAminoAcid(this->sequence[j]);
}
}
return newpep;
}
std::size_t compute_original_index(std::size_t aa_pos, std::size_t orig_len, std::size_t offset, std::size_t search_aa_len, bool is_reverse){
std::size_t nuc_pos = aa_pos * 3;
if(is_reverse){
std::size_t rev_index = nuc_pos + offset; // position in original reversed string before bases were truncated away
std::size_t rev_index_end = rev_index + (search_aa_len*3) - 1; //0-3 in 4 lenstr 0 + 4 -1
return orig_len - rev_index_end - 1; //originally 6 long, position 4 in original is 1 in reverse, and 0 in offset of 1
} else{
return nuc_pos + offset;
}
}
std::vector<std::string> Peptide::subseqs_encoding(std::string& dna){
std::vector<std::string> strv;
if(this->sequence.size() * 3 > dna.size()){
return strv;
}
std::string rdna = Peptide::revcomp(dna);
if(this->sequence.size() * 3 == dna.size()){
//only one frame is possible
Peptide aastr = Peptide::translateDNA(dna);
Peptide aarstr = Peptide::translateDNA(rdna);
if(this->operator==(aastr)){
strv.push_back(dna);
}
if(this->operator==(aarstr)){
strv.push_back(rdna);
}
}
std::vector<std::string> frames;
std::vector<Peptide> transframes;
std::vector<std::size_t> coords;
std::vector<bool> is_reverse;
for(std::size_t i =0; i<3 && this->sequence.size() * 3 <= dna.size();i++){
std::string subdna = dna.substr(i,dna.size()-i); //ATCG -> TGC
std::string subrdna = rdna.substr(i,dna.size()-i); // ATGC -> CGTA -> GCAT -> CAT
frames.push_back(subdna);
frames.push_back(subrdna);
transframes.push_back(Peptide::translateDNA(subdna));
coords.push_back(i);
is_reverse.push_back(false);
transframes.push_back(Peptide::translateDNA(subrdna));
coords.push_back(i);
is_reverse.push_back(true);
}
for(std::size_t i = 0; i < transframes.size() ; i++){
std::string pointers(dna.size(), ' ');
std::size_t offset = coords[i];
for(std::size_t j = 0;this->size(true) + j <= transframes[i].size(true) && j < transframes[i].size(true) ; j++) {
//make subseqs of same size as self
Peptide subseq = transframes[i].subseq(j,this->sequence.size());
std::size_t substr_pos = j * 3;
std::size_t orig_index = compute_original_index(j,dna.size(),offset, this->size(true),is_reverse[i]);
if(this->isSameAs(subseq)){
strv.push_back(dna.substr(orig_index,this->nuc_size(true)));
pointers[orig_index] = '^';
}
}
}
return strv;
}
std::size_t Peptide::size(bool include_stops){
if(include_stops){
return this->sequence.size();
}else {
return this->to_string(include_stops).size();
}
}
std::size_t Peptide::nuc_size(bool include_stops){
if(include_stops){
return this->sequence.size() * 3;
}else {
return this->to_string(include_stops).size() * 3;
}
}
char Peptide::complement(char nuc, bool rna_mode){
switch(nuc){
default:
throw std::runtime_error("[Peptide::complement] invalid nucleotide");
case 'A':
if(rna_mode){
return 'U';
}
return 'T';
case 'G':
return 'C';
case 'C':
return 'G';
case 'T':
if(rna_mode){
throw std::runtime_error("[Peptide::complement] DNA character in RNA: ");
}
return 'A';
case 'U':
if(!rna_mode){
throw std::runtime_error("[Peptide::complement] RNA character in DNA: ");
}
return 'A';
}
}
std::string Peptide::revcomp(std::string& dna, bool rna_mode){
std::string rev(dna);
std::reverse(rev.begin(),rev.end());
std::vector<char> revcomp;
for(std::size_t i = 0; i<rev.size() ; i++){
revcomp.push_back(Peptide::complement(rev[i],rna_mode));
}
return join(revcomp, "");
}
Peptide::PeptideCode Peptide::symbol_to_peptide(char symb){
switch ((char)std::toupper(symb)){
default:
throw std::runtime_error("[Peptide::symbol_to_peptide] invalid amino acid symbol");
case '*':
return PeptideCode::STOP;
case 'H':
return PeptideCode::HISTIDINE;
case 'Q':
return PeptideCode::GLUTAMINE;
case 'P':
return PeptideCode::PROLINE;
case 'R':
return PeptideCode::ARGININE;
case 'L':
return PeptideCode::LEUCINE;
case 'D':
return PeptideCode::ASPARTIC_ACID;
case 'E':
return PeptideCode::GLUTAMIC_ACID;
case 'A':
return PeptideCode::ALANINE;
case 'G':
return PeptideCode::GLYCINE;
case 'V':
return PeptideCode::VALINE;
case 'Y':
return PeptideCode::TYROSINE;
case 'S':
return PeptideCode::SERINE;
case 'C':
return PeptideCode::CYSTEINE;
case 'W':
return PeptideCode::TRYPTOPHAN;
case 'F':
return PeptideCode::PHENYLALANINE;
case 'N':
return PeptideCode::ASPARAGINE;
case 'K':
return PeptideCode::LYSINE;
case 'T':
return PeptideCode::THREONINE;
case 'I':
return PeptideCode::ISOLEUCINE;
case 'M':
return PeptideCode::METHIONINE;
case '@':
return PeptideCode::NONPROTEINOGENIC;
}
}
int Peptide::number_of_codons(Peptide::PeptideCode aa){
switch (aa){
case ISOLEUCINE:
case STOP:
return 3;
case HISTIDINE:
case GLUTAMINE:
case ASPARTIC_ACID:
case GLUTAMIC_ACID:
case TYROSINE:
case CYSTEINE:
case PHENYLALANINE:
case ASPARAGINE:
case LYSINE:
return 2;
case ALANINE:
case PROLINE:
case GLYCINE:
case VALINE:
case THREONINE:
return 4;
case ARGININE:
case LEUCINE:
case SERINE:
return 6;
case TRYPTOPHAN:
case METHIONINE:
return 1;
case NONPROTEINOGENIC:
return 0;
}
}
std::string Peptide::PeptideCode_to_string(Peptide::PeptideCode aa){
switch (aa){
case STOP:
return "STOP";
case HISTIDINE:
return "Histidine";
case GLUTAMINE:
return "Glutamine";
case PROLINE:
return "Proline";
case ARGININE:
return "Arginine";
case LEUCINE:
return "Leucine";
case ASPARTIC_ACID:
return "Aspartic acid";
case GLUTAMIC_ACID:
return "Glutamic acid";
case ALANINE:
return "Alanine";
case GLYCINE:
return "Glycine";
case VALINE:
return "Valine";
case TYROSINE:
return "Tyrosine";
case SERINE:
return "Serine";
case CYSTEINE:
return "Cysteine";
case TRYPTOPHAN:
return "Tryptophan";
case PHENYLALANINE:
return "Phenylalanine";
case ASPARAGINE:
return "Asparagine";
case LYSINE:
return "Lysine";
case THREONINE:
return "Threonine";
case ISOLEUCINE:
return "Isoleucine";
case METHIONINE:
return "Methionine";
case NONPROTEINOGENIC:
return "Non_Proteinogenic";
}
}
std::string Peptide::PeptideCode_to_abbrev(PeptideCode aa){
switch (aa){
case STOP:
return "STP";
case HISTIDINE:
return "His";
case GLUTAMINE:
return "Glu";
case PROLINE:
return "Pro";
case ARGININE:
return "Arg";
case LEUCINE: