bpp-phyl3 3.0.0
SubstitutionRegister.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: The Bio++ Development Group
2//
3// SPDX-License-Identifier: CECILL-2.1
4
5#ifndef BPP_PHYL_MAPPING_SUBSTITUTIONREGISTER_H
6#define BPP_PHYL_MAPPING_SUBSTITUTIONREGISTER_H
7
8
9#include "../Model/StateMap.h"
10
11// From bpp-core:
12#include <Bpp/Clonable.h>
15
16// From bpp-seq:
21
22// From the STL:
23#include <vector>
24#include <string>
25#include <algorithm>
26
27namespace bpp
28{
39 public virtual Clonable
40{
41public:
44
46
47public:
51 virtual const Alphabet& alphabet() const = 0;
52
56 virtual std::shared_ptr<const Alphabet> getAlphabet() const = 0;
57
61 virtual const StateMapInterface& stateMap() const = 0;
62
66 virtual std::shared_ptr<const StateMapInterface> getStateMap() const = 0;
67
71 virtual size_t getNumberOfSubstitutionTypes() const = 0;
72
79 virtual const std::string& getName() const = 0;
80
89 virtual size_t getType(size_t fromState, size_t toState) const = 0;
90
100 virtual std::string getTypeName(size_t type) const = 0;
101};
102
104 public virtual SubstitutionRegisterInterface
105{
106protected:
107 std::shared_ptr<const StateMapInterface> stateMap_;
108 std::string name_;
109
110public:
111 AbstractSubstitutionRegister(std::shared_ptr<const StateMapInterface> stateMap, const std::string& name) :
112 stateMap_(stateMap), name_(name)
113 {}
114
116 stateMap_(asr.stateMap_), name_(asr.name_)
117 {}
118
120 {
121 stateMap_ = asr.stateMap_;
122 name_ = asr.name_;
123 return *this;
124 }
125
127
128public:
129 const StateMapInterface& stateMap() const override { return *stateMap_; }
130
131 std::shared_ptr<const StateMapInterface> getStateMap() const override { return stateMap_; }
132
133 const Alphabet& alphabet() const override { return *stateMap_->getAlphabet(); }
134
135 std::shared_ptr<const Alphabet> getAlphabet() const override { return stateMap_->getAlphabet(); }
136
137 const std::string& getName() const override
138 {
139 return name_;
140 }
141};
142
152{
153public:
154 TotalSubstitutionRegister(std::shared_ptr<const StateMapInterface> stateMap) :
156 {}
157
158 TotalSubstitutionRegister* clone() const override { return new TotalSubstitutionRegister(*this); }
159
160public:
161 size_t getNumberOfSubstitutionTypes() const override { return 1; }
162
163 size_t getType(size_t fromState, size_t toState) const override
164 {
165 return fromState == toState ? 0 : 1;
166 }
167
168 std::string getTypeName(size_t type) const override
169 {
170 if (type == 0)
171 {
172 return "nosub";
173 }
174 else if (type == 1)
175 {
176 return "substitution";
177 }
178 else
179 {
180 throw Exception("TotalSubstitutionRegister::getTypeName. Bad substitution type.");
181 }
182 }
183};
184
193{
194private:
195 std::shared_ptr<const SubstitutionRegisterInterface> preg_;
196
198
199public:
200 CompleteSubstitutionRegister(std::shared_ptr<const SubstitutionRegisterInterface> reg) :
202 preg_(reg),
203 isRegComplete_(true)
204 {
205 size_t size = reg->alphabet().getSize();
206 for (size_t i = 0; i < size; ++i)
207 {
208 for (size_t j = 0; j < size; ++j)
209 {
210 if ((i != j) && reg->getType(i, j) == 0)
211 {
212 isRegComplete_ = false;
213 return;
214 }
215 }
216 }
217 }
218
219 CompleteSubstitutionRegister* clone() const override { return new CompleteSubstitutionRegister(*this); }
220
223 preg_(csr.preg_),
225 {}
226
228 {
230 preg_ = csr.preg_;
232 return *this;
233 }
234
236
237public:
238 size_t getNumberOfSubstitutionTypes() const override
239 {
240 return preg_->getNumberOfSubstitutionTypes() + (isRegComplete_ ? 0 : 1);
241 }
242
243 size_t getType(size_t fromState, size_t toState) const override
244 {
245 if (fromState == toState)
246 return 0;
247
248 size_t t = preg_->getType(fromState, toState);
249 if (t == 0)
251 else
252 return t;
253 }
254
255 std::string getTypeName(size_t type) const override
256 {
257 try
258 {
259 return preg_->getTypeName(type);
260 }
261 catch (Exception& e)
262 {
263 if (type == getNumberOfSubstitutionTypes())
264 return "Completion_substitution";
265 else
266 throw Exception("CompleteSubstitutionRegister::getTypeName. Bad substitution type.");
267 }
268 }
269};
270
271
282{
283private:
287 std::vector< std::shared_ptr<SubstitutionRegisterInterface>> vSubReg_;
288
289public:
290 VectorOfSubstitutionRegisters(std::shared_ptr<const StateMapInterface> stateMap) :
292 vSubReg_()
293 {}
294
297 vSubReg_(vosr.vSubReg_)
298 {}
299
301 {
302 return new VectorOfSubstitutionRegisters(*this);
303 }
304
306
307 void addRegister(std::shared_ptr<SubstitutionRegisterInterface> reg)
308 {
309 if (reg)
310 {
311 if (reg->stateMap() != stateMap())
312 throw Exception("VectorOfSubstitutionRegisters::addRegister : mismatch between state maps");
313
314 vSubReg_.push_back(reg);
315 }
316 }
317
318 size_t getType(size_t i, size_t j) const override
319 {
320 if (i == j)
321 return 0;
322
323 size_t x = 0;
324
325 for (size_t p = 0; p < vSubReg_.size(); ++p)
326 {
327 x *= vSubReg_[p]->getNumberOfSubstitutionTypes();
328 size_t z = vSubReg_[p]->getType(i, j);
329 if (z == 0)
330 return 0;
331 x += z - 1;
332 }
333
334 return x + 1;
335 }
336
337 size_t getNumberOfSubstitutionTypes() const override
338 {
339 if (vSubReg_.size() == 0)
340 return 0;
341
342 size_t n = 1;
343
344 for (size_t i = 0; i < vSubReg_.size(); ++i)
345 {
346 n *= vSubReg_[i]->getNumberOfSubstitutionTypes();
347 }
348
349 return n;
350 }
351
356 std::string getTypeName(size_t type) const override
357 {
358 if (type == 0)
359 return "nosub";
360
361 std::string res = "";
362 size_t ty = type - 1;
363
364 for (size_t p = vSubReg_.size(); p > 0; p--)
365 {
366 if (p != vSubReg_.size())
367 res = "_X_" + res;
368 res = vSubReg_[p - 1]->getTypeName((ty % vSubReg_[p - 1]->getNumberOfSubstitutionTypes()) + 1) + res;
369
370 ty /= vSubReg_[p - 1]->getNumberOfSubstitutionTypes();
371 }
372
373 return res;
374 }
375};
376
377
387{
388protected:
392 size_t size_;
393
398
405 std::map<size_t, std::map<size_t, std::vector<size_t>>> types_;
406
407public:
408 GeneralSubstitutionRegister(std::shared_ptr<const StateMapInterface> stateMap) :
410 size_(stateMap->getNumberOfModelStates()),
412 types_()
413 {}
414
415 GeneralSubstitutionRegister(std::shared_ptr<const StateMapInterface> stateMap, const RowMatrix<size_t>& matrix) :
417 size_(stateMap->getNumberOfModelStates()),
418 matrix_(matrix),
419 types_()
420 {
422 throw DimensionException("GeneralSubstitutionRegister", size_, matrix_.getNumberOfRows());
424 throw DimensionException("GeneralSubstitutionRegister", size_, matrix_.getNumberOfColumns());
425 updateTypes_();
426 }
427
430 size_(gsr.size_),
431 matrix_(gsr.matrix_),
432 types_(gsr.types_)
433 {}
434
436 {
438 size_ = gsr.size_;
439 matrix_ = gsr.matrix_;
440 types_ = gsr.types_;
441 return *this;
442 }
443
445 {
446 return new GeneralSubstitutionRegister(*this);
447 }
448
450
451 size_t getType(size_t i, size_t j) const override
452 {
453 return matrix_(i, j);
454 }
455
456 size_t getNumberOfSubstitutionTypes() const override
457 {
458 return types_.find(0) == types_.end() ? types_.size() : types_.size() - 1;
459 }
460
464 std::string getTypeName(size_t type) const override
465 {
466 if (types_.find(type) != types_.end())
467 return TextTools::toString(type);
468
469 throw Exception("Bad type number " + TextTools::toString(type) + " in GeneralSubstitutionRegister::getTypeName.");
470 }
471
472protected:
473 void updateTypes_();
474};
475
476
486{
487 std::map<size_t, std::string> categoryNames_;
488
489public:
490 SelectedSubstitutionRegister (std::shared_ptr<const StateMapInterface> stateMap, std::string selectedSubstitutions) :
493 {
494 selectedSubstitutions.erase(std::remove(selectedSubstitutions.begin(), selectedSubstitutions.end(), ' '), selectedSubstitutions.end());
506 size_t typeSubs = 0;
507 size_t coord1 = 0;
508 size_t coord2 = 0;
509 std::string codon1 = "";
510 std::string codon2 = "";
511 StringTokenizer subsGroup(selectedSubstitutions, "()");
512 while (subsGroup.hasMoreToken())
513 {
514 typeSubs++;
515 StringTokenizer namesSubs(subsGroup.nextToken(), ":");
516 if (namesSubs.numberOfRemainingTokens() == 2)
517 {
518 categoryNames_[typeSubs] = namesSubs.nextToken();
519 }
520 else if (namesSubs.numberOfRemainingTokens() == 1)
521 {
522 categoryNames_[typeSubs] = TextTools::toString(typeSubs);
523 }
524 StringTokenizer substitutions(namesSubs.nextToken(), ",");
525 while (substitutions.hasMoreToken())
526 {
527 StringTokenizer coordinates(substitutions.nextToken(), "->");
528 codon1 = coordinates.nextToken();
529 codon2 = coordinates.nextToken();
530 coord1 = stateMap->alphabet().getStateIndex(codon1);
531 coord2 = stateMap->alphabet().getStateIndex(codon2);
532 this->matrix_(coord1, coord2) = typeSubs;
533 }
534 }
535 updateTypes_();
536 }
537
539
541
542 std::string getTypeName(size_t type) const
543 {
544 if (types_.find(type) != types_.end())
545 return TextTools::toString(categoryNames_.find(type)->second);
546
547 throw Exception("Bad type number " + TextTools::toString(type) + " in GeneralSubstitutionRegister::getTypeName.");
548 }
549};
550
560{
561 std::map<std::string, size_t> categoryCorrespondance_;
562 std::shared_ptr<const GeneticCode> genCode_;
563
564public:
566 std::shared_ptr<const StateMapInterface> stateMap,
567 std::shared_ptr<const GeneticCode> genCode) :
570 genCode_(genCode)
571 {
573 updateTypes_();
574 }
575
579 genCode_(ais.genCode_)
580 {}
581
583 {
585 genCode_ = ais.genCode_;
587
588 return *this;
589 }
590
592
594
595
596 std::string getTypeName(size_t type) const
597 {
598 if (types_.find(type) != types_.end())
599 {
600 for (std::map<std::string, size_t>::const_iterator it = categoryCorrespondance_.begin(); it != categoryCorrespondance_.end(); it++)
601 {
602 if (it->second == type)
603 return TextTools::toString(it->first);
604 }
605 }
606 throw Exception("Bad type number " + TextTools::toString(type) + " in GeneralSubstitutionRegister::getTypeName.");
607 }
608
609protected:
611 {
612 size_t categoryIndex = 1;
613 for (size_t i = 1; i <= stateMap().getNumberOfModelStates(); ++i)
614 {
615 int state1 = stateMap().alphabet().getStateAt(i).getNum();
616 for (size_t j = i + 1; j <= stateMap().getNumberOfModelStates(); ++j)
617 {
618 int state2 = stateMap().alphabet().getStateAt(j).getNum();
619 if (!(genCode_->isStop(state1)) && !(genCode_->isStop(state2)))
620 {
621 if (genCode_->translate(state1) == genCode_->translate(state2))
622 {
623 std::string aminoAcid = genCode_->getTargetAlphabet()->intToChar(genCode_->translate(state1));
624 if (categoryCorrespondance_.find(aminoAcid) == categoryCorrespondance_.end())
625 {
626 categoryCorrespondance_[aminoAcid] = categoryIndex;
627 categoryIndex++;
628 }
629 matrix_(i, j) = categoryCorrespondance_[aminoAcid];
630 matrix_(j, i) = categoryCorrespondance_[aminoAcid];
631 }
632 }
633 }
634 }
635 }
636};
637
646{
647 std::map<std::string, size_t> categoryCorrespondance_;
648 std::shared_ptr<const GeneticCode> genCode_;
649
650public:
652 std::shared_ptr<const StateMapInterface> stateMap,
653 std::shared_ptr<const GeneticCode> genCode) :
656 genCode_(genCode)
657 {
659 updateTypes_();
660 }
661
665 genCode_(ais.genCode_)
666 {}
667
669 {
672 genCode_ = ais.genCode_;
673
674 return *this;
675 }
676
678
680
681 std::string getTypeName(size_t type) const
682 {
683 if (types_.find(type) != types_.end())
684 {
685 for (std::map<std::string, size_t>::const_iterator it = categoryCorrespondance_.begin(); it != categoryCorrespondance_.end(); it++)
686 {
687 if (it->second == type)
688 return TextTools::toString(it->first);
689 }
690 }
691 throw Exception("Bad type number " + TextTools::toString(type) + " in GeneralSubstitutionRegister::getTypeName.");
692 }
693
694protected:
696 {
697 size_t categoryIndex = 1;
698 for (size_t i = 1; i <= stateMap().getNumberOfModelStates(); ++i)
699 {
700 int state1 = stateMap().alphabet().getStateAt(i).getNum();
701 for (size_t j = i + 1; j <= stateMap().getNumberOfModelStates(); ++j)
702 {
703 int state2 = stateMap().alphabet().getStateAt(j).getNum();
704 if (!(genCode_->isStop(state1)) && !(genCode_->isStop(state2)))
705 {
706 if (genCode_->translate(state1) != genCode_->translate(state2))
707 {
708 std::string aminoAcid1 = genCode_->getTargetAlphabet()->intToChar(genCode_->translate(state1));
709 std::string aminoAcid2 = genCode_->getTargetAlphabet()->intToChar(genCode_->translate(state2));
710 bool AA1IsNotInGroup = ((categoryCorrespondance_.find(aminoAcid1 + "->" + aminoAcid2) == categoryCorrespondance_.end()));
711 bool AA2IsNotInGroup = ((categoryCorrespondance_.find(aminoAcid2 + "->" + aminoAcid1) == categoryCorrespondance_.end()));
712 if (AA1IsNotInGroup)
713 {
714 categoryCorrespondance_[aminoAcid1 + "->" + aminoAcid2] = categoryIndex;
715 categoryIndex++;
716 }
717 this->matrix_(i, j) = categoryCorrespondance_[aminoAcid1 + "->" + aminoAcid2];
718 if (AA2IsNotInGroup)
719 {
720 categoryCorrespondance_[aminoAcid2 + "->" + aminoAcid1] = categoryIndex;
721 categoryIndex++;
722 }
723 this->matrix_(j, i) = categoryCorrespondance_[aminoAcid2 + "->" + aminoAcid1];
724 }
725 }
726 }
727 }
728 }
729};
730
742{
743private:
747 std::shared_ptr<const GeneticCode> genCode_;
748
749public:
750 TsTvSubstitutionRegister(std::shared_ptr<const StateMapInterface> stateMap) :
752 genCode_()
753 {}
754
755 TsTvSubstitutionRegister(std::shared_ptr<const StateMapInterface> stateMap, std::shared_ptr<const GeneticCode> genCode) :
757 genCode_(genCode)
758 {}
759
762 genCode_(reg.genCode_)
763 {}
764
766 {
768 genCode_ = reg.genCode_;
769 return *this;
770 }
771
773
774public:
775 size_t getNumberOfSubstitutionTypes() const { return 2; }
776
777 size_t getType(size_t fromState, size_t toState) const
778 {
779 int x = stateMap().getAlphabetStateAsInt(fromState);
780 int y = stateMap().getAlphabetStateAsInt(toState);
781 if (x == y)
782 return 0; // nothing happens
783
784
785 if (genCode_)
786 {
787 auto cAlpha = genCode_->getCodonAlphabet();
788 if (cAlpha->isGap(x)
789 || cAlpha->isGap(y)
790 || genCode_->isStop(x)
791 || genCode_->isStop(y))
792 return 0;
793
794 size_t countPos = 0;
795 if (cAlpha->getFirstPosition(x) != cAlpha->getFirstPosition(y))
796 countPos++;
797 if (cAlpha->getSecondPosition(x) != cAlpha->getSecondPosition(y))
798 countPos++;
799 if (cAlpha->getThirdPosition(x) != cAlpha->getThirdPosition(y))
800 countPos++;
801 if (countPos > 1)
802 return 0;
803 }
804
805 int d = std::abs(x - y);
806
807 while (d != 0)
808 {
809 if (d == 2)
810 return 1; // This is a transition
811 else
812 d /= 4;
813 }
814
815 return 2; // This is a transversion
816 }
817
818 std::string getTypeName(size_t type) const
819 {
820 if (type == 0)
821 {
822 return "nosub";
823 }
824 else if (type == 1)
825 {
826 return "Ts";
827 }
828 else if (type == 2)
829 {
830 return "Tv";
831 }
832 else
833 {
834 throw Exception("TsTvSubstitutionRegister::getTypeName. Bad substitution type.");
835 }
836 }
837};
838
852{
853private:
857 std::shared_ptr<const GeneticCode> genCode_;
858
859public:
860 SWSubstitutionRegister(std::shared_ptr<const StateMapInterface> stateMap) :
862 genCode_()
863 {}
864
866 std::shared_ptr<const StateMapInterface> stateMap,
867 std::shared_ptr<const GeneticCode> genCode) :
869 genCode_(genCode)
870 {}
871
874 genCode_(reg.genCode_)
875 {}
876
878 {
880 genCode_ = reg.genCode_;
881 return *this;
882 }
883
885
886public:
887 size_t getNumberOfSubstitutionTypes() const { return 4; }
888
889 size_t getType(size_t fromState, size_t toState) const
890 {
891 int x = stateMap().getAlphabetStateAsInt(fromState);
892 int y = stateMap().getAlphabetStateAsInt(toState);
893 if (x == y)
894 return 0; // nothing happens
895
896 int nd, na;
897 if (genCode_)
898 {
899 auto cAlpha = genCode_->getCodonAlphabet();
900 if (cAlpha->isGap(x)
901 || cAlpha->isGap(y)
902 || genCode_->isStop(x)
903 || genCode_->isStop(y))
904 return 0;
905
906 nd = cAlpha->getFirstPosition(x);
907 na = cAlpha->getFirstPosition(y);
908 if (na != nd)
909 {
910 if (cAlpha->getSecondPosition(x) != cAlpha->getSecondPosition(y)
911 || (cAlpha->getThirdPosition(x) != cAlpha->getThirdPosition(y)))
912 return 0;
913 }
914 else
915 {
916 nd = cAlpha->getSecondPosition(x);
917 na = cAlpha->getSecondPosition(y);
918 if (na != nd)
919 {
920 if (cAlpha->getThirdPosition(x) != cAlpha->getThirdPosition(y))
921 return 0;
922 }
923 else
924 {
925 nd = cAlpha->getThirdPosition(x);
926 na = cAlpha->getThirdPosition(y);
927 }
928 }
929 }
930 else
931 {
932 nd = x;
933 na = y;
934 }
935
936 switch (nd)
937 {
938 case 0:
939 return (na == 3) ? 4 : 3;
940 case 1:
941 return (na == 2) ? 1 : 2;
942 case 2:
943 return (na == 1) ? 1 : 2;
944 case 3:
945 return (na == 0) ? 4 : 3;
946 default:
947 return 0;
948 }
949 }
950
951 std::string getTypeName(size_t type) const
952 {
953 switch (type)
954 {
955 case 0:
956 return "nosub";
957 case 1:
958 return "S->S";
959 case 2:
960 return "S->W";
961 case 3:
962 return "W->S";
963 case 4:
964 return "W->W";
965 default:
966 throw Exception("SWSubstitutionRegister::getTypeName. Bad substitution type.");
967 }
968 }
969};
970
971
982{
983private:
984 std::shared_ptr<const GeneticCode> genCode_;
986
987public:
989 std::shared_ptr<const StateMapInterface> stateMap,
990 std::shared_ptr<const GeneticCode> genCode,
991 bool countMultiple = false) :
993 genCode_(genCode),
994 countMultiple_(countMultiple)
995 {}
996
999 genCode_(reg.genCode_),
1001 {}
1002
1004 {
1006 genCode_ = reg.genCode_;
1008 return *this;
1009 }
1010
1012
1013public:
1014 size_t getNumberOfSubstitutionTypes() const { return 2; }
1015
1016 size_t getType(size_t fromState, size_t toState) const
1017 {
1018 int x = stateMap().getAlphabetStateAsInt(fromState);
1019 int y = stateMap().getAlphabetStateAsInt(toState);
1020
1021 auto cAlpha = genCode_->getCodonAlphabet();
1022 if (cAlpha->isGap(x)
1023 || cAlpha->isGap(y)
1024 || genCode_->isStop(x)
1025 || genCode_->isStop(y))
1026 return 0;
1027 if (x == y)
1028 return 0; // nothing happens
1029 if (!countMultiple_)
1030 {
1031 size_t countPos = 0;
1032 if (cAlpha->getFirstPosition(x) != cAlpha->getFirstPosition(y))
1033 countPos++;
1034 if (cAlpha->getSecondPosition(x) != cAlpha->getSecondPosition(y))
1035 countPos++;
1036 if (cAlpha->getThirdPosition(x) != cAlpha->getThirdPosition(y))
1037 countPos++;
1038 if (countPos > 1)
1039 return 0;
1040 }
1041 return genCode_->areSynonymous(x, y) ? 1 : 2;
1042 }
1043
1044 std::string getTypeName (size_t type) const
1045 {
1046 if (type == 0)
1047 {
1048 return "nosub";
1049 }
1050 else if (type == 1)
1051 {
1052 return "dS";
1053 }
1054 else if (type == 2)
1055 {
1056 return "dN";
1057 }
1058 else
1059 {
1060 throw Exception("DnDsSubstitutionRegister::getTypeName. Bad substitution type.");
1061 }
1062 }
1063};
1064
1076{
1077private:
1078 std::vector< std::vector<bool>> types_;
1079
1080 // In case of codon model
1081 std::shared_ptr<const GeneticCode> genCode_;
1082
1083public:
1084 KrKcSubstitutionRegister(std::shared_ptr<const StateMapInterface> stateMap) :
1086 types_(20),
1087 genCode_()
1088 {
1089 init();
1090 }
1091
1093 std::shared_ptr<const StateMapInterface> stateMap,
1094 std::shared_ptr<const GeneticCode> genCode) :
1096 types_(20),
1097 genCode_(genCode)
1098 {
1099 init();
1100 }
1101
1104 types_(kreg.types_),
1105 genCode_(kreg.genCode_)
1106 {}
1107
1109 {
1111 types_ = kreg.types_;
1112 genCode_ = kreg.genCode_;
1113
1114 return *this;
1115 }
1116
1117 void init()
1118 {
1119 for (size_t i = 0; i < 20; ++i)
1120 {
1121 types_[i].resize(20);
1122 for (size_t j = 0; j < 20; ++j)
1123 {
1124 types_[i][j] = false;
1125 }
1126 }
1127
1128 types_[0][7] = true;
1129 types_[0][14] = true;
1130 types_[0][19] = true;
1131 types_[7][0] = true;
1132 types_[7][14] = true;
1133 types_[7][19] = true;
1134 types_[14][0] = true;
1135 types_[14][7] = true;
1136 types_[14][19] = true;
1137 types_[19][0] = true;
1138 types_[19][7] = true;
1139 types_[19][14] = true;
1140
1141 types_[1][5] = true;
1142 types_[1][6] = true;
1143 types_[1][8] = true;
1144 types_[1][11] = true;
1145 types_[1][17] = true;
1146 types_[1][18] = true;
1147 types_[5][6] = true;
1148 types_[5][8] = true;
1149 types_[5][11] = true;
1150 types_[5][1] = true;
1151 types_[5][17] = true;
1152 types_[5][18] = true;
1153 types_[6][8] = true;
1154 types_[6][11] = true;
1155 types_[6][5] = true;
1156 types_[6][1] = true;
1157 types_[6][17] = true;
1158 types_[6][18] = true;
1159 types_[8][6] = true;
1160 types_[8][11] = true;
1161 types_[8][5] = true;
1162 types_[8][1] = true;
1163 types_[8][17] = true;
1164 types_[8][18] = true;
1165 types_[11][1] = true;
1166 types_[11][5] = true;
1167 types_[11][6] = true;
1168 types_[11][8] = true;
1169 types_[11][17] = true;
1170 types_[11][18] = true;
1171 types_[17][1] = true;
1172 types_[17][5] = true;
1173 types_[17][6] = true;
1174 types_[17][8] = true;
1175 types_[17][11] = true;
1176 types_[17][18] = true;
1177 types_[18][1] = true;
1178 types_[18][6] = true;
1179 types_[18][8] = true;
1180 types_[18][11] = true;
1181 types_[18][5] = true;
1182 types_[18][17] = true;
1183
1184 types_[2][3] = true;
1185 types_[2][4] = true;
1186 types_[2][15] = true;
1187 types_[2][16] = true;
1188 types_[3][2] = true;
1189 types_[3][4] = true;
1190 types_[3][15] = true;
1191 types_[3][16] = true;
1192 types_[4][2] = true;
1193 types_[4][3] = true;
1194 types_[4][15] = true;
1195 types_[4][16] = true;
1196 types_[15][2] = true;
1197 types_[15][3] = true;
1198 types_[15][4] = true;
1199 types_[15][16] = true;
1200 types_[16][2] = true;
1201 types_[16][3] = true;
1202 types_[16][4] = true;
1203 types_[16][15] = true;
1204
1205 types_[9][10] = true;
1206 types_[9][12] = true;
1207 types_[9][13] = true;
1208 types_[10][9] = true;
1209 types_[10][12] = true;
1210 types_[10][13] = true;
1211 types_[12][9] = true;
1212 types_[12][10] = true;
1213 types_[12][13] = true;
1214 types_[13][9] = true;
1215 types_[13][10] = true;
1216 types_[13][12] = true;
1217 }
1218
1220
1221public:
1222 size_t getNumberOfSubstitutionTypes() const { return 2; }
1223
1224 size_t getType(size_t fromState, size_t toState) const
1225 {
1226 if (fromState == toState)
1227 return 0; // nothing happens
1228 int x = stateMap().getAlphabetStateAsInt(fromState);
1229 int y = stateMap().getAlphabetStateAsInt(toState);
1230
1231 if (genCode_) // Codon model
1232 {
1233 if (genCode_->getSourceAlphabet()->isGap(x)
1234 || genCode_->getSourceAlphabet()->isGap(y)
1235 || genCode_->isStop(x)
1236 || genCode_->isStop(y))
1237 return 0;
1238 if (genCode_->areSynonymous(x, y))
1239 return 0;
1240
1241 // avoid multiple substitutions
1242 auto cAlpha = genCode_->getCodonAlphabet();
1243
1244 size_t countPos = 0;
1245 if (cAlpha->getFirstPosition(x) != cAlpha->getFirstPosition(y))
1246 countPos++;
1247 if (cAlpha->getSecondPosition(x) != cAlpha->getSecondPosition(y))
1248 countPos++;
1249 if (cAlpha->getThirdPosition(x) != cAlpha->getThirdPosition(y))
1250 countPos++;
1251 if (countPos > 1)
1252 return 0;
1253
1254 x = genCode_->translate(x);
1255 y = genCode_->translate(y);
1256 }
1257
1258 return types_[static_cast<size_t>(x)][static_cast<size_t>(y)] ? 1 : 2;
1259 }
1260
1261 std::string getTypeName(size_t type) const
1262 {
1263 if (type == 0)
1264 {
1265 return "nosub";
1266 }
1267 else if (type == 1)
1268 {
1269 return "Kc";
1270 }
1271 else if (type == 2)
1272 {
1273 return "Kr";
1274 }
1275 else
1276 {
1277 throw Exception("KrKcSubstitutionRegister::getTypeName. Bad substitution type.");
1278 }
1279 }
1280};
1281} // end of namespace bpp.
1282#endif // BPP_PHYL_MAPPING_SUBSTITUTIONREGISTER_H
Indexes only substitutions between amino-acids. Groups are distinguished by their direction.
std::shared_ptr< const GeneticCode > genCode_
AAExteriorSubstitutionRegister * clone() const
std::string getTypeName(size_t type) const
names of the types are their number.
AAExteriorSubstitutionRegister(const AAExteriorSubstitutionRegister &ais)
AAExteriorSubstitutionRegister(std::shared_ptr< const StateMapInterface > stateMap, std::shared_ptr< const GeneticCode > genCode)
std::map< std::string, size_t > categoryCorrespondance_
AAExteriorSubstitutionRegister & operator=(const AAExteriorSubstitutionRegister &ais)
Indexes only intra amino-acid substitutions. Every group represents a substitutions for the same amin...
AAInteriorSubstitutionRegister(std::shared_ptr< const StateMapInterface > stateMap, std::shared_ptr< const GeneticCode > genCode)
AAInteriorSubstitutionRegister & operator=(const AAInteriorSubstitutionRegister &ais)
AAInteriorSubstitutionRegister(const AAInteriorSubstitutionRegister &ais)
std::map< std::string, size_t > categoryCorrespondance_
std::string getTypeName(size_t type) const
names of the types are their number.
std::shared_ptr< const GeneticCode > genCode_
AAInteriorSubstitutionRegister * clone() const
std::shared_ptr< const Alphabet > getAlphabet() const override
const StateMapInterface & stateMap() const override
AbstractSubstitutionRegister(const AbstractSubstitutionRegister &asr)
AbstractSubstitutionRegister(std::shared_ptr< const StateMapInterface > stateMap, const std::string &name)
std::shared_ptr< const StateMapInterface > getStateMap() const override
const std::string & getName() const override
Get the name of the register.
std::shared_ptr< const StateMapInterface > stateMap_
const Alphabet & alphabet() const override
AbstractSubstitutionRegister & operator=(const AbstractSubstitutionRegister &asr)
int getNum() const
virtual size_t getStateIndex(int state) const=0
virtual const AlphabetState & getStateAt(size_t stateIndex) const=0
Completion of a given substitution register to consider all substitutions. The new substitutions are ...
size_t getType(size_t fromState, size_t toState) const override
Get the substitution type far a given pair of model states.
std::shared_ptr< const SubstitutionRegisterInterface > preg_
CompleteSubstitutionRegister(const CompleteSubstitutionRegister &csr)
CompleteSubstitutionRegister * clone() const override
std::string getTypeName(size_t type) const override
Get the name of a given substitution type.
CompleteSubstitutionRegister & operator=(const CompleteSubstitutionRegister &csr)
size_t getNumberOfSubstitutionTypes() const override
CompleteSubstitutionRegister(std::shared_ptr< const SubstitutionRegisterInterface > reg)
Distinguishes synonymous from non-synonymous substitutions.
DnDsSubstitutionRegister(std::shared_ptr< const StateMapInterface > stateMap, std::shared_ptr< const GeneticCode > genCode, bool countMultiple=false)
size_t getType(size_t fromState, size_t toState) const
Get the substitution type far a given pair of model states.
std::shared_ptr< const GeneticCode > genCode_
DnDsSubstitutionRegister & operator=(const DnDsSubstitutionRegister &reg)
DnDsSubstitutionRegister(const DnDsSubstitutionRegister &reg)
std::string getTypeName(size_t type) const
Get the name of a given substitution type.
DnDsSubstitutionRegister * clone() const
Sets a Register based on a matrix of integers. If M is the matrix, M[i,j] is the number of the substi...
GeneralSubstitutionRegister(const GeneralSubstitutionRegister &gsr)
std::string getTypeName(size_t type) const override
names of the types are their number.
GeneralSubstitutionRegister(std::shared_ptr< const StateMapInterface > stateMap)
size_t size_
The size of the matrix, i.e. the number of states.
GeneralSubstitutionRegister * clone() const override
RowMatrix< size_t > matrix_
The matrix of the substitution register.
GeneralSubstitutionRegister & operator=(const GeneralSubstitutionRegister &gsr)
size_t getType(size_t i, size_t j) const override
Get the substitution type far a given pair of model states.
GeneralSubstitutionRegister(std::shared_ptr< const StateMapInterface > stateMap, const RowMatrix< size_t > &matrix)
std::map< size_t, std::map< size_t, std::vector< size_t > > > types_
The map from substitution types to the map of from states to the vector of target states.
size_t getNumberOfSubstitutionTypes() const override
Count conservative and radical amino-acid substitutions.
KrKcSubstitutionRegister * clone() const
KrKcSubstitutionRegister(std::shared_ptr< const StateMapInterface > stateMap)
KrKcSubstitutionRegister(const KrKcSubstitutionRegister &kreg)
std::shared_ptr< const GeneticCode > genCode_
KrKcSubstitutionRegister(std::shared_ptr< const StateMapInterface > stateMap, std::shared_ptr< const GeneticCode > genCode)
std::vector< std::vector< bool > > types_
std::string getTypeName(size_t type) const
Get the name of a given substitution type.
size_t getType(size_t fromState, size_t toState) const
Get the substitution type far a given pair of model states.
KrKcSubstitutionRegister & operator=(const KrKcSubstitutionRegister &kreg)
size_t getNumberOfRows() const
size_t getNumberOfColumns() const
Distinguishes substitutions given the link between the changed nucleotides : S for strong (GC) and W ...
std::shared_ptr< const GeneticCode > genCode_
useful for codon alphabet
std::string getTypeName(size_t type) const
Get the name of a given substitution type.
SWSubstitutionRegister & operator=(const SWSubstitutionRegister &reg)
size_t getType(size_t fromState, size_t toState) const
Get the substitution type far a given pair of model states.
SWSubstitutionRegister * clone() const
SWSubstitutionRegister(const SWSubstitutionRegister &reg)
SWSubstitutionRegister(std::shared_ptr< const StateMapInterface > stateMap, std::shared_ptr< const GeneticCode > genCode)
SWSubstitutionRegister(std::shared_ptr< const StateMapInterface > stateMap)
Class inheriting from GeneralSubstitutionRegister, this one uses a special constructor which allows i...
std::map< size_t, std::string > categoryNames_
std::string getTypeName(size_t type) const
names of the types are their number.
SelectedSubstitutionRegister(std::shared_ptr< const StateMapInterface > stateMap, std::string selectedSubstitutions)
SelectedSubstitutionRegister * clone() const
Map the states of a given alphabet which have a model state.
Definition: StateMap.h:25
virtual const Alphabet & alphabet() const =0
virtual size_t getNumberOfModelStates() const =0
virtual int getAlphabetStateAsInt(size_t index) const =0
size_t numberOfRemainingTokens() const
const std::string & nextToken()
bool hasMoreToken() const
The SubstitutionRegister interface.
virtual const Alphabet & alphabet() const =0
virtual std::string getTypeName(size_t type) const =0
Get the name of a given substitution type.
virtual size_t getNumberOfSubstitutionTypes() const =0
virtual const StateMapInterface & stateMap() const =0
virtual const std::string & getName() const =0
Get the name of the register.
virtual size_t getType(size_t fromState, size_t toState) const =0
Get the substitution type far a given pair of model states.
virtual SubstitutionRegisterInterface * clone() const =0
virtual std::shared_ptr< const Alphabet > getAlphabet() const =0
virtual std::shared_ptr< const StateMapInterface > getStateMap() const =0
TotalSubstitutionRegister * clone() const override
std::string getTypeName(size_t type) const override
Get the name of a given substitution type.
size_t getType(size_t fromState, size_t toState) const override
Get the substitution type far a given pair of model states.
size_t getNumberOfSubstitutionTypes() const override
TotalSubstitutionRegister(std::shared_ptr< const StateMapInterface > stateMap)
Distinguishes transitions from transversions.
TsTvSubstitutionRegister * clone() const
TsTvSubstitutionRegister(const TsTvSubstitutionRegister &reg)
size_t getType(size_t fromState, size_t toState) const
Get the substitution type far a given pair of model states.
TsTvSubstitutionRegister(std::shared_ptr< const StateMapInterface > stateMap, std::shared_ptr< const GeneticCode > genCode)
TsTvSubstitutionRegister & operator=(const TsTvSubstitutionRegister &reg)
std::string getTypeName(size_t type) const
Get the name of a given substitution type.
std::shared_ptr< const GeneticCode > genCode_
useful for codon alphabet
TsTvSubstitutionRegister(std::shared_ptr< const StateMapInterface > stateMap)
Sets a Register based on a vector of Registers. The categories are intersection of categories of thos...
std::vector< std::shared_ptr< SubstitutionRegisterInterface > > vSubReg_
the vector of pointers to SubstitutionRegisters.
VectorOfSubstitutionRegisters * clone() const override
VectorOfSubstitutionRegisters(const VectorOfSubstitutionRegisters &vosr)
size_t getNumberOfSubstitutionTypes() const override
VectorOfSubstitutionRegisters(std::shared_ptr< const StateMapInterface > stateMap)
std::string getTypeName(size_t type) const override
names of the types are the list of their types in the registers (separated with _).
size_t getType(size_t i, size_t j) const override
Get the substitution type far a given pair of model states.
void addRegister(std::shared_ptr< SubstitutionRegisterInterface > reg)
std::string toString(T t)
Defines the basic types of data flow nodes.
ExtendedFloat abs(const ExtendedFloat &ef)