/usr/include/Bpp/Phyl/Mapping/SubstitutionRegister.h is in libbpp-phyl-dev 2.1.0-1ubuntu3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | //
// File: SubstitutionRegister.h
// Created by: Julien Dutheil
// Created on: Mon Dec 6 16:32 2010
//
/*
Copyright or © or Copr. Bio++ Development Team, (November 16, 2004, 2005, 2006)
This software is a computer program whose purpose is to provide classes
for phylogenetic data analysis.
This software is governed by the CeCILL license under French law and
abiding by the rules of distribution of free software. You can use,
modify and/ or redistribute the software under the terms of the CeCILL
license as circulated by CEA, CNRS and INRIA at the following URL
"http://www.cecill.info".
As a counterpart to the access to the source code and rights to copy,
modify and redistribute granted by the license, users are provided only
with a limited warranty and the software's author, the holder of the
economic rights, and the successive licensors have only limited
liability.
In this respect, the user's attention is drawn to the risks associated
with loading, using, modifying and/or developing or reproducing the
software by the user in light of its specific status of free software,
that may mean that it is complicated to manipulate, and that also
therefore means that it is reserved for developers and experienced
professionals having in-depth computer knowledge. Users are therefore
encouraged to load and test the software's suitability as regards their
requirements in conditions enabling the security of their systems and/or
data to be ensured and, more generally, to use and operate it in the
same conditions as regards security.
The fact that you are presently reading this means that you have had
knowledge of the CeCILL license and that you accept its terms.
*/
#ifndef _SUBSTITUTIONREGISTER_H_
#define _SUBSTITUTIONREGISTER_H_
// From bpp-core:
#include <Bpp/Clonable.h>
// From bpp-seq:
#include <Bpp/Seq/Alphabet/Alphabet.h>
#include <Bpp/Seq/Alphabet/NucleicAlphabet.h>
#include <Bpp/Seq/GeneticCode/GeneticCode.h>
// From the STL:
#include <vector>
namespace bpp
{
/**
* @brief The SubstitutionRegister interface.
*
* Substitution registers are simple classes that define categories of substitutions, and assign them an index.
*
* @author Julien Dutheil
*/
class SubstitutionRegister :
public virtual Clonable
{
public:
SubstitutionRegister() {}
virtual ~SubstitutionRegister() {}
#ifndef NO_VIRTUAL_COV
virtual SubstitutionRegister* clone() const = 0;
#endif
public:
/**
* @return The alphabet associated to this instance.
*/
virtual const Alphabet* getAlphabet() const = 0;
/**
* @return The number of substitution types supported by this class.
*/
virtual size_t getNumberOfSubstitutionTypes() const = 0;
/**
* @brief Get the substitution type far a given pair of states.
*
* @param fromState Initial state (should be a state supported by the specified alphabet).
* @param toState Final state (should be a state supported by the specified alphabet).
* @return The index of the corresponding substitution type, ranging from 0 to 'getNumberOfSubstitutionTypes' + 1,
* as non-substitution (that is when fromState == toState) will always return 0.
*/
virtual size_t getType(int fromState, int toState) const = 0;
/**
* @brief Get the name of a given substitution type.
*
* This method is only used for user-friendlyness purposes, not computational goal.
* I can therefore be left unimplemented in some cases.
*
* @param type Index of the substitution (should be an size_t contained in the register).
* @return A string describing the substitution type.
*/
virtual std::string getTypeName(size_t type) const = 0;
};
class AbstractSubstitutionRegister :
public virtual SubstitutionRegister
{
protected:
const Alphabet* alphabet_;
public:
AbstractSubstitutionRegister(const Alphabet* alphabet) :
alphabet_(alphabet)
{}
AbstractSubstitutionRegister(const AbstractSubstitutionRegister& asr) :
alphabet_(asr.alphabet_)
{}
AbstractSubstitutionRegister& operator=(const AbstractSubstitutionRegister& asr)
{
alphabet_ = asr.alphabet_;
return *this;
}
virtual ~AbstractSubstitutionRegister() {}
public:
const Alphabet* getAlphabet() const { return alphabet_; }
};
/**
* @brief Gather states into defined categories, and count the changes between categories.
*
* Optionally allows for within categories substitutions.
*/
class CategorySubstitutionRegister :
public AbstractSubstitutionRegister
{
protected:
bool within_;
size_t nbCategories_;
mutable std::map<int, size_t> categories_;
std::vector<std::string> categoryNames_;
std::vector< std::vector<size_t> > index_;
std::vector< std::vector<size_t> > revIndex_;
public:
/**
* @brief Build a new substitution register with categories. This class is mean to be inherited.
*
* @param alphabet The input alphabet.
* @param within Specifies if within categories substitutions should be counted as well.
*/
CategorySubstitutionRegister(const Alphabet* alphabet, bool within = false) :
AbstractSubstitutionRegister(alphabet),
within_(within),
nbCategories_(0),
categories_(),
categoryNames_(),
index_(),
revIndex_()
{}
protected:
template<class T>
void setCategories(const std::map<int, T>& categories)
{
// First index categories:
nbCategories_ = 0;
std::map<T, size_t> cats;
for (typename std::map<int, T>::const_iterator it = categories.begin(); it != categories.end(); ++it)
{
if (cats.find(it->second) == cats.end())
{
++nbCategories_;
cats[it->second] = nbCategories_;
}
}
// Now creates categories:
categories_.clear();
categoryNames_.resize(nbCategories_);
std::vector<int> types = alphabet_->getSupportedInts();
for (size_t i = 0; i < types.size(); ++i)
{
typename std::map<int, T>::const_iterator it = categories.find(types[i]);
if (it != categories.end())
{
categories_[types[i]] = cats[it->second];
categoryNames_[cats[it->second] - 1] += alphabet_->intToChar(types[i]);
}
else
{
categories_[types[i]] = 0;
}
}
size_t count = 1;
index_.resize(nbCategories_);
for (size_t i = 0; i < index_.size(); ++i)
{
index_[i].resize(nbCategories_);
for (size_t j = 0; j < index_.size(); ++j)
{
if (j != i)
{
index_[i][j] = count++;
std::vector<size_t> pos(2);
pos[0] = i;
pos[1] = j;
revIndex_.push_back(pos);
}
}
}
if (within_)
{
for (size_t i = 0; i < index_.size(); ++i)
{
index_[i][i] = count++;
std::vector<size_t> pos(2);
pos[0] = i;
pos[1] = i;
revIndex_.push_back(pos);
}
}
}
public:
virtual size_t getCategory(int state) const
{
if (!alphabet_->isIntInAlphabet(state))
throw Exception("CategorySubstitutionRegister::getCategory(). State is not supported by alphabet.");
return categories_[state];
}
virtual size_t getCategoryFrom(size_t type) const
{
if (type <= nbCategories_ * (nbCategories_ - 1))
{
return revIndex_[type - 1][0] + 1;
}
else
{
if (within_)
return revIndex_[type - 1][0] + 1;
else
throw Exception("CategorySubstitutionRegister::getCategoryFrom. Bad substitution type.");
}
}
virtual size_t getCategoryTo(size_t type) const
{
if (type <= nbCategories_ * (nbCategories_ - 1))
{
return revIndex_[type - 1][1] + 1;
}
else
{
if (within_)
return revIndex_[type - 1][1] + 1;
else
throw Exception("CategorySubstitutionRegister::getCategoryTo. Bad substitution type.");
}
}
virtual std::string getCategoryName(size_t category) const
{
return categoryNames_[category - 1];
}
virtual bool allowWithin() const { return within_; }
size_t getNumberOfCategories() const { return nbCategories_; }
size_t getNumberOfSubstitutionTypes() const { return nbCategories_ * (nbCategories_ - 1) + (within_ ? nbCategories_ : 0); }
virtual size_t getType(int fromState, int toState) const
{
size_t fromCat = categories_[fromState];
size_t toCat = categories_[toState];
if (fromCat > 0 && toCat > 0)
return index_[fromCat - 1][toCat - 1];
else
return 0;
}
std::string getTypeName(size_t type) const
{
return getCategoryName(getCategoryFrom(type)) + "->" + getCategoryName(getCategoryTo(type));
}
};
/**
* @brief Count all substitutions.
*
* This register has only 1 substitution type, mapped as:
* - 0 not a substitution
* - 1 a substitution
*/
class TotalSubstitutionRegister :
public AbstractSubstitutionRegister
{
public:
TotalSubstitutionRegister(const Alphabet* alphabet) :
AbstractSubstitutionRegister(alphabet)
{}
TotalSubstitutionRegister* clone() const { return new TotalSubstitutionRegister(*this); }
public:
size_t getNumberOfSubstitutionTypes() const { return 1; }
size_t getType(int fromState, int toState) const
{
return fromState == toState ? 0 : 1;
}
std::string getTypeName(size_t type) const
{
if (type == 0)
{
return "no substitution";
}
else if (type == 1)
{
return "substitution";
}
else
{
throw Exception("TotalSubstitutionRegister::getTypeName. Bad substitution type.");
}
}
};
/**
* @brief Distinguishes all types of substitutions.
*
* This register has only n * (n-1) substitution type, where n is the size of the alphabet, mapped as:
* - 0 not a substitution
* - x in [1, n(n-1)] a substitution
*/
class ComprehensiveSubstitutionRegister :
public CategorySubstitutionRegister
{
public:
ComprehensiveSubstitutionRegister(const Alphabet* alphabet, bool within = false) :
CategorySubstitutionRegister(alphabet, within)
{
std::map<int, int> categories;
for (int i = 0; i < static_cast<int>(alphabet->getSize()); ++i)
{
categories[i] = i;
}
setCategories<int>(categories);
}
ComprehensiveSubstitutionRegister* clone() const { return new ComprehensiveSubstitutionRegister(*this); }
};
/**
* @brief Distinguishes AT<->GC from GC<->AT.
*
* This register has two substitution types, mapped as:
* - 0 not a substitution
* - 1 a AT->GC substitution
* - 2 a GC->AT substitution
*/
class GCSubstitutionRegister :
public CategorySubstitutionRegister
{
public:
GCSubstitutionRegister(const NucleicAlphabet* alphabet, bool within = false) :
CategorySubstitutionRegister(alphabet, within)
{
std::map<int, int> categories;
categories[0] = 1;
categories[1] = 2;
categories[2] = 2;
categories[3] = 1;
setCategories<int>(categories);
}
GCSubstitutionRegister* clone() const { return new GCSubstitutionRegister(*this); }
};
/**
* @brief Distinguishes transitions from transversions.
*
* This register has two substitution types, mapped as:
* - 0 not a substitution
* - 1 a transition
* - 2 a transversion
*/
class TsTvSubstitutionRegister :
public AbstractSubstitutionRegister
{
public:
TsTvSubstitutionRegister(const NucleicAlphabet* alphabet) :
AbstractSubstitutionRegister(alphabet)
{}
TsTvSubstitutionRegister* clone() const { return new TsTvSubstitutionRegister(*this); }
public:
size_t getNumberOfSubstitutionTypes() const { return 2; }
size_t getType(int fromState, int toState) const
{
if (fromState == toState)
return 0; // nothing happens
if ((fromState == 0 && toState == 2)
|| (fromState == 2 && toState == 0)
|| (fromState == 1 && toState == 3)
|| (fromState == 3 && toState == 1))
return 1; // This is a transition
return 2; // This is a transversion
}
std::string getTypeName(size_t type) const
{
if (type == 0)
{
return "no substitution";
}
else if (type == 1)
{
return "transition";
}
else if (type == 2)
{
return "transversion";
}
else
{
throw Exception("TsTvSubstitutionRegister::getTypeName. Bad substitution type.");
}
}
};
/**
* @brief Distinguishes synonymous from non-synonymous substitutions.
*
* This register has two substitution types, mapped as:
* - 0 not a substitution
* - 1 a synonymous substitution
* - 2 a non-synonymous substitution
*/
class DnDsSubstitutionRegister :
public AbstractSubstitutionRegister
{
private:
const GeneticCode* code_;
bool countMultiple_;
public:
DnDsSubstitutionRegister(const GeneticCode* gc, bool countMultiple = false) :
AbstractSubstitutionRegister(gc->getSourceAlphabet()),
code_(gc),
countMultiple_(countMultiple)
{}
DnDsSubstitutionRegister(const DnDsSubstitutionRegister& reg) :
AbstractSubstitutionRegister(reg),
code_(reg.code_),
countMultiple_(reg.countMultiple_)
{}
DnDsSubstitutionRegister& operator=(const DnDsSubstitutionRegister& reg)
{
AbstractSubstitutionRegister::operator=(reg);
code_ = reg.code_;
countMultiple_ = reg.countMultiple_;
return *this;
}
DnDsSubstitutionRegister* clone() const { return new DnDsSubstitutionRegister(*this); }
public:
size_t getNumberOfSubstitutionTypes() const { return 2; }
size_t getType(int fromState, int toState) const
{
const CodonAlphabet* cAlpha = dynamic_cast<const CodonAlphabet*>(alphabet_);
if (cAlpha->isStop(fromState) || cAlpha->isStop(toState))
return 0;
if (fromState == toState)
return 0; // nothing happens
if (!countMultiple_)
{
size_t countPos = 0;
if (cAlpha->getFirstPosition(fromState) != cAlpha->getFirstPosition(toState))
countPos++;
if (cAlpha->getSecondPosition(fromState) != cAlpha->getSecondPosition(toState))
countPos++;
if (cAlpha->getThirdPosition(fromState) != cAlpha->getThirdPosition(toState))
countPos++;
if (countPos > 1)
return 0;
}
return code_->areSynonymous(fromState, toState) ? 1 : 2;
}
std::string getTypeName (size_t type) const
{
if (type == 0)
{
return "no substitution";
}
else if (type == 1)
{
return "synonymous";
}
else if (type == 2)
{
return "non synonymous";
}
else
{
throw Exception("DnDsSubstitutionRegister::getTypeName. Bad substitution type.");
}
}
};
/**
* @brief Distinguishes AT->GC vs GC->AT inside synonymous
* substitutions on third codon position.
*
* This register has two substitution types, mapped as:
* - 0 not a counted substitution
* - 1 a AT->GC synonymous substitution
* - 2 a GC->AT synonymous substitution
*
* Multiple substitutions are forbidden.
*
*/
class GCSynonymousSubstitutionRegister :
public CategorySubstitutionRegister
{
private:
const GeneticCode* code_;
public:
GCSynonymousSubstitutionRegister(const GeneticCode* gc, bool within = false) :
CategorySubstitutionRegister(gc->getSourceAlphabet(), within),
code_(gc)
{
const CodonAlphabet* pCA = dynamic_cast<const CodonAlphabet*>(gc->getSourceAlphabet());
std::map<int, int> categories;
for (int i = 0; i < static_cast<int>(pCA->getSize()); i++)
{
int n = pCA->getThirdPosition(i);
switch (n)
{
case 0:
case 3:
categories[i] = 1;
break;
case 1:
case 2:
categories[i] = 2;
break;
}
}
setCategories<int>(categories);
}
GCSynonymousSubstitutionRegister(const GCSynonymousSubstitutionRegister& reg) :
CategorySubstitutionRegister(reg),
code_(reg.code_)
{}
GCSynonymousSubstitutionRegister& operator=(const GCSynonymousSubstitutionRegister& reg)
{
CategorySubstitutionRegister::operator=(reg);
code_ = reg.code_;
return *this;
}
GCSynonymousSubstitutionRegister* clone() const { return new GCSynonymousSubstitutionRegister(*this); }
public:
size_t getNumberOfSubstitutionTypes() const { return 2; }
size_t getType(int fromState, int toState) const
{
const CodonAlphabet* pCA = dynamic_cast<const CodonAlphabet*>(code_->getSourceAlphabet());
if (pCA->isStop(fromState) || pCA->isStop(toState) || !code_->areSynonymous(fromState, toState))
return 0;
// only substitutions between 3rd positions
if ((pCA->getFirstPosition(fromState) != pCA->getFirstPosition(toState)) ||
(pCA->getSecondPosition(fromState) != pCA->getSecondPosition(toState)))
return 0;
size_t fromCat = categories_[fromState];
size_t toCat = categories_[toState];
if (fromCat > 0 && toCat > 0)
return index_[fromCat - 1][toCat - 1];
else
return 0;
}
std::string getTypeName (size_t type) const
{
if (type == 0)
{
return "no AT<->GC substitution or non-synonymous substitution";
}
else if (type == 1)
{
return "AT->GC synonymous";
}
else if (type == 2)
{
return "GC->AT synonymous";
}
else
{
throw Exception("GCSynonymousSubstitutionRegister::getTypeName. Bad substitution type.");
}
}
};
} // end of namespace bpp.
#endif // _SUBSTITUTIONREGISTER_H_
|