40 #ifndef _SUBSTITUTIONREGISTER_H_ 41 #define _SUBSTITUTIONREGISTER_H_ 43 #include "../Model/SubstitutionModel.h" 44 #include "../Model/Nucleotide/NucleotideSubstitutionModel.h" 45 #include "../Model/Codon/CodonSubstitutionModel.h" 48 #include <Bpp/Clonable.h> 49 #include <Bpp/Numeric/Matrix/Matrix.h> 50 #include <Bpp/Text/StringTokenizer.h> 53 #include <Bpp/Seq/Alphabet/NucleicAlphabet.h> 54 #include <Bpp/Seq/Alphabet/CodonAlphabet.h> 55 #include <Bpp/Seq/GeneticCode/GeneticCode.h> 73 public virtual Clonable
79 #ifndef NO_VIRTUAL_COV 107 virtual size_t getType(
size_t fromState,
size_t toState)
const = 0;
118 virtual std::string
getTypeName(
size_t type)
const = 0;
163 std::vector< std::vector<size_t> >
index_;
189 std::map<T, size_t> cats;
190 for (
typename std::map<int, T>::const_iterator it = categories.begin(); it != categories.end(); ++it)
192 if (cats.find(it->second) == cats.end())
203 for (
size_t i = 0; i < types.size(); ++i)
205 typename std::map<int, T>::const_iterator it = categories.find(types[i]);
206 if (it != categories.end())
219 for (
size_t i = 0; i <
index_.size(); ++i)
222 for (
size_t j = 0; j <
index_.size(); ++j)
227 std::vector<size_t> pos(2);
236 for (
size_t i = 0; i <
index_.size(); ++i)
239 std::vector<size_t> pos(2);
265 throw Exception(
"CategorySubstitutionRegister::getCategoryFrom. Bad substitution type.");
280 throw Exception(
"CategorySubstitutionRegister::getCategoryTo. Bad substitution type.");
295 virtual size_t getType(
size_t fromState,
size_t toState)
const 299 if (fromCat > 0 && toCat > 0)
300 return index_[fromCat - 1][toCat - 1];
332 size_t getType(
size_t fromState,
size_t toState)
const 334 return fromState == toState ? 0 : 1;
341 return "no substitution";
345 return "substitution";
349 throw Exception(
"TotalSubstitutionRegister::getTypeName. Bad substitution type.");
374 for (
size_t i = 0; i < size; i++)
376 for (
size_t j = 0; j < size; j++)
378 if ((i != j) && reg.
getType(i, j) == 0)
416 size_t getType(
size_t fromState,
size_t toState)
const 434 return "Completion substitution";
436 throw Exception(
"CompleteSubstitutionRegister::getTypeName. Bad substitution type.");
455 std::map<int, int> categories;
456 for (
int i = 0; i < static_cast<int>(model->
getAlphabet()->getSize()); ++i)
460 setCategories<int>(categories);
496 std::map<size_t, std::map<size_t, std::vector<size_t> > >
types_;
501 size_(model->getNumberOfStates()),
508 size_(model->getNumberOfStates()),
513 throw DimensionException(
"GeneralSubstitutionRegister",
size_,
matrix_.getNumberOfRows());
515 throw DimensionException(
"GeneralSubstitutionRegister",
size_,
matrix_.getNumberOfColumns());
555 return TextTools::toString(type);
557 throw Exception(
"Bad type number " + TextTools::toString(type) +
" in GeneralSubstitutionRegister::getTypeName.");
583 selectedSubstitutions.erase(std::remove(selectedSubstitutions.begin(), selectedSubstitutions.end(),
' '), selectedSubstitutions.end());
598 std::string codon1 =
"";
599 std::string codon2 =
"";
600 StringTokenizer subsGroup(selectedSubstitutions,
"()");
601 while (subsGroup.hasMoreToken())
604 StringTokenizer namesSubs(subsGroup.nextToken(),
":");
605 if (namesSubs.numberOfRemainingTokens() == 2)
609 else if (namesSubs.numberOfRemainingTokens() == 1)
613 StringTokenizer substitutions(namesSubs.nextToken(),
",");
614 while (substitutions.hasMoreToken())
616 StringTokenizer coordinates(substitutions.nextToken(),
"->");
617 codon1 = coordinates.nextToken();
618 codon2 = coordinates.nextToken();
621 this->
matrix_(coord1, coord2) = typeSubs;
636 throw Exception(
"Bad type number " + TextTools::toString(type) +
" in GeneralSubstitutionRegister::getTypeName.");
658 size_t categoryIndex = 1;
659 for (
size_t i = 0; i < model->
getAlphabet()->getSize(); ++i)
661 int state1 = model->
getAlphabet()->getStateAt(i).getNum();
662 for (
size_t j = i + 1; j < model->
getAlphabet()->getSize(); ++j)
664 int state2 = model->
getAlphabet()->getStateAt(j).getNum();
695 if (it->second == type)
696 return TextTools::toString(it->first);
699 throw Exception(
"Bad type number " + TextTools::toString(type) +
" in GeneralSubstitutionRegister::getTypeName.");
720 size_t categoryIndex = 1;
721 for (
size_t i = 0; i < model->
getAlphabet()->getSize(); ++i)
723 int state1 = model->
getAlphabet()->getStateAt(i).getNum();
724 for (
size_t j = i + 1; j < model->
getAlphabet()->getSize(); ++j)
726 int state2 = model->
getAlphabet()->getStateAt(j).getNum();
764 if (it->second == type)
765 return TextTools::toString(it->first);
768 throw Exception(
"Bad type number " + TextTools::toString(type) +
" in GeneralSubstitutionRegister::getTypeName.");
787 std::map<int, int> categories;
792 setCategories<int>(categories);
819 size_t getType(
size_t fromState,
size_t toState)
const 825 if ((x == 0 && y == 2)
826 || (x == 2 && y == 0)
827 || (x == 1 && y == 3)
828 || (x == 3 && y == 1))
837 return "no substitution";
845 return "transversion";
849 throw Exception(
"TsTvSubstitutionRegister::getTypeName. Bad substitution type.");
872 code_(model->getGeneticCode()),
895 size_t getType(
size_t fromState,
size_t toState)
const 899 const CodonAlphabet* cAlpha =
dynamic_cast<const CodonAlphabet*
>(
model_->
getAlphabet());
907 if (cAlpha->getFirstPosition(x) != cAlpha->getFirstPosition(y))
909 if (cAlpha->getSecondPosition(x) != cAlpha->getSecondPosition(y))
911 if (cAlpha->getThirdPosition(x) != cAlpha->getThirdPosition(y))
916 return code_->areSynonymous(x, y) ? 1 : 2;
923 return "no substitution";
931 return "non synonymous";
935 throw Exception(
"DnDsSubstitutionRegister::getTypeName. Bad substitution type.");
962 code_(model->getGeneticCode())
964 const CodonAlphabet* pCA =
dynamic_cast<const CodonAlphabet*
>(
code_->getSourceAlphabet());
966 std::map<int, int> categories;
967 for (
int i = 0; i < static_cast<int>(pCA->getSize()); i++)
969 int n = pCA->getThirdPosition(i);
982 setCategories<int>(categories);
1002 size_t getType(
size_t fromState,
size_t toState)
const 1006 const CodonAlphabet* pCA =
dynamic_cast<const CodonAlphabet*
>(
code_->getSourceAlphabet());
1007 if (
code_->isStop(x) ||
code_->isStop( y) || !
code_->areSynonymous(x, y))
1012 if ((pCA->getFirstPosition(x) != pCA->getFirstPosition(y)) ||
1013 (pCA->getSecondPosition(x) != pCA->getSecondPosition(y)))
1019 if (fromCat > 0 && toCat > 0)
1020 return index_[fromCat - 1][toCat - 1];
1029 return "no AT<->GC substitution or non-synonymous substitution";
1033 return "AT->GC synonymous";
1037 return "GC->AT synonymous";
1041 throw Exception(
"GCSynonymousSubstitutionRegister::getTypeName. Bad substitution type.");
1047 #endif // _SUBSTITUTIONREGISTER_H_ std::string getTypeName(size_t type) const
Get the name of a given substitution type.
CompleteSubstitutionRegister * clone() const
GCSynonymousSubstitutionRegister(const GCSynonymousSubstitutionRegister ®)
size_t getNumberOfSubstitutionTypes() const
Interface for all substitution models.
size_t getNumberOfSubstitutionTypes() const
size_t getType(size_t fromState, size_t toState) const
Get the substitution type far a given pair of model states.
CompleteSubstitutionRegister & operator=(const CompleteSubstitutionRegister &csr)
virtual int getAlphabetStateAsInt(size_t index) const =0
GCSynonymousSubstitutionRegister & operator=(const GCSynonymousSubstitutionRegister ®)
Completion of a given substitution register to consider all substitutions. The new substitutions are ...
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 getType(size_t fromState, size_t toState) const
Get the substitution type far a given pair of model states.
virtual const std::vector< int > & getAlphabetStates() const =0
Distinguishes AT<->GC from GC<->AT.
virtual size_t getCategoryTo(size_t type) const
std::string getTypeName(size_t type) const
names of the types are their number.
virtual SubstitutionRegister * clone() const =0
AAExteriorSubstitutionRegister * clone() const
virtual ~SubstitutionRegister()
virtual const SubstitutionModel * getSubstitutionModel() const =0
The SubstitutionRegister interface.
size_t getType(size_t i, size_t j) const
Get the substitution type far a given pair of model states.
virtual const Alphabet * getAlphabet() const =0
~AAExteriorSubstitutionRegister()
GCSubstitutionRegister * clone() const
DnDsSubstitutionRegister(const DnDsSubstitutionRegister ®)
virtual size_t getCategoryFrom(size_t type) const
GCSubstitutionRegister(const NucleotideSubstitutionModel *model, bool within=false)
size_t getNumberOfCategories() const
const SubstitutionRegister * preg_
RowMatrix< size_t > matrix_
The matrix of the substitution register.
std::map< std::string, size_t > categoryCorrespondance_
size_t getType(size_t fromState, size_t toState) const
Get the substitution type far a given pair of model states.
~CompleteSubstitutionRegister()
size_t getType(size_t fromState, size_t toState) const
Get the substitution type far a given pair of model states.
Indexes only intra amino-acid substitutions. Every group represents a substitutions for the same amin...
Indexes only substitutions between amino-acids. Groups are distinguished by their direction...
virtual bool allowWithin() const
AAInteriorSubstitutionRegister * clone() const
std::string getTypeName(size_t type) const
names of the types are their number.
Gather states into defined categories, and count the changes between categories.
size_t getNumberOfSubstitutionTypes() const
TotalSubstitutionRegister(const SubstitutionModel *model)
AAInteriorSubstitutionRegister(const CodonSubstitutionModel *model)
TsTvSubstitutionRegister(const NucleotideSubstitutionModel *model)
GCSynonymousSubstitutionRegister(const CodonSubstitutionModel *model, bool within=false)
size_t size_
The size of the matrix, i.e. the number of states.
size_t getNumberOfSubstitutionTypes() const
CompleteSubstitutionRegister(const SubstitutionRegister ®)
Class inheriting from GeneralSubstitutionRegister, this one uses a special constructor which allows i...
ComprehensiveSubstitutionRegister(const SubstitutionModel *model, bool within=false)
TsTvSubstitutionRegister * clone() const
Distinguishes synonymous from non-synonymous substitutions.
virtual std::string getCategoryName(size_t category) const
DnDsSubstitutionRegister & operator=(const DnDsSubstitutionRegister ®)
Sets a Register based on a matrix of integers. If M is the matrix, M[i,j] is the number of the substi...
virtual ~AbstractSubstitutionRegister()
Distinguishes transitions from transversions.
size_t getNumberOfSubstitutionTypes() const
const SubstitutionModel * getSubstitutionModel() const
AbstractSubstitutionRegister(const SubstitutionModel *model)
virtual size_t getCategory(size_t state) const
ComprehensiveSubstitutionRegister * clone() const
virtual size_t getNumberOfSubstitutionTypes() const =0
SelectedSubstitutionRegister * clone() const
const GeneticCode * code_
virtual size_t getType(size_t fromState, size_t toState) const =0
Get the substitution type far a given pair of model states.
std::string getTypeName(size_t type) const
Get the name of a given substitution type.
CompleteSubstitutionRegister(const CompleteSubstitutionRegister &csr)
std::vector< std::vector< size_t > > revIndex_
DnDsSubstitutionRegister(const CodonSubstitutionModel *model, bool countMultiple=false)
AbstractSubstitutionRegister & operator=(const AbstractSubstitutionRegister &asr)
Specialisation interface for nucleotide substitution model.
std::map< std::string, size_t > categoryCorrespondance_
TotalSubstitutionRegister * clone() const
virtual std::string getTypeName(size_t type) const =0
Get the name of a given substitution type.
GeneralSubstitutionRegister & operator=(const GeneralSubstitutionRegister &gsr)
GCSynonymousSubstitutionRegister * clone() const
AAExteriorSubstitutionRegister(const CodonSubstitutionModel *model)
std::string getTypeName(size_t type) const
Get the name of a given substitution type.
GeneralSubstitutionRegister * clone() const
size_t getNumberOfSubstitutionTypes() const
size_t getType(size_t fromState, size_t toState) const
Get the substitution type far a given pair of model states.
std::string getTypeName(size_t type) const
Get the name of a given substitution type.
DnDsSubstitutionRegister * clone() const
AbstractSubstitutionRegister(const AbstractSubstitutionRegister &asr)
Distinguishes all types of substitutions.
std::map< int, size_t > categories_
GeneralSubstitutionRegister(const SubstitutionModel *model, const RowMatrix< size_t > &matrix)
std::vector< std::string > categoryNames_
std::string getTypeName(size_t type) const
names of the types are their number.
GeneralSubstitutionRegister(const GeneralSubstitutionRegister &gsr)
~AAInteriorSubstitutionRegister()
std::vector< std::vector< size_t > > index_
Abstract class for codon models.
SelectedSubstitutionRegister(const SubstitutionModel *model, std::string selectedSubstitutions)
virtual const GeneticCode * getGeneticCode() const =0
const Alphabet * getAlphabet() const
const SubstitutionModel * model_
Distinguishes AT->GC vs GC->AT inside synonymous substitutions on third codon position.
std::map< size_t, std::string > categoryNames_
const GeneticCode * code_
virtual ~GeneralSubstitutionRegister()
std::string getTypeName(size_t type) const
Get the name of a given substitution type.
size_t getNumberOfSubstitutionTypes() const
std::string getTypeName(size_t type) const
names of the types are their number.
~SelectedSubstitutionRegister()
GeneralSubstitutionRegister(const SubstitutionModel *model)
CategorySubstitutionRegister(const SubstitutionModel *model, bool within=false)
Build a new substitution register with categories. This class is meant to be inherited.
virtual const Alphabet * getAlphabet() const =0
virtual size_t getType(size_t fromState, size_t toState) const
Get the substitution type far a given pair of model states.
std::string getTypeName(size_t type) const
Get the name of a given substitution type.
void setCategories(const std::map< int, T > &categories)