/usr/include/Bpp/Seq/Io/Maf/MafStatistics.h is in libbpp-seq-omics-dev 2.1.0-1.
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 | //
// File: MafStatistics.h
// Authors: Julien Dutheil
// Created: Mon Jun 25 2012
//
/*
Copyright or © or Copr. Bio++ Development Team, (2012)
This software is a computer program whose purpose is to provide classes
for sequences 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 _MAFSTATISTICS_H_
#define _MAFSTATISTICS_H_
#include "MafBlock.h"
//From bpp-core:
#include <Bpp/Utils/MapTools.h>
//From the STL:
#include <map>
#include <string>
namespace bpp {
/**
* @brief General interface for storing statistical results.
*
* This interface will most likely be extended in the future...
*
* @author Julien Dutheil
* @see MafStatistics
*/
class MafStatisticsResult
{
protected:
mutable std::map<std::string, double> values_;
public:
MafStatisticsResult(): values_() {}
virtual ~MafStatisticsResult() {}
public:
virtual double getValue(const std::string& tag) const throw (Exception) {
std::map<std::string, double>::iterator it = values_.find(tag);
if (it != values_.end())
return it->second;
else
throw Exception("MafStatisticsResult::getValue(). No value found for tag: " + tag + ".");
}
/**
* @brief Associate a value to a certain tag. Any existing tag will be overwritten
*
* @param tag The name of the value to associate.
* @param value The value to associate to the tag.
*/
virtual void setValue(const std::string& tag, double value) throw (Exception) { values_[tag] = value; }
/**
* @return A boolean saying whether a value is available for the given tag.
* @param tag The name of the value to associate.
*/
virtual bool hasValue(const std::string& tag) const {
return (values_.find(tag) != values_.end());
}
/**
* @return A vector with all available tags.
*/
std::vector<std::string> getAvailableTags() const { return MapTools::getKeys(values_); }
};
/**
* @brief A simple maf statistics result, with only one value.
*/
class SimpleMafStatisticsResult:
public virtual MafStatisticsResult
{
private:
std::string name_;
public:
SimpleMafStatisticsResult(const std::string& name): MafStatisticsResult(), name_(name) {
setValue(name, 0);
}
virtual ~SimpleMafStatisticsResult() {}
public:
virtual double getValue() const { return values_[name_]; }
/**
* @brief Associate a value to a certain tag. Any existing tag will be overwritten
*
* @param tag The name of the value to associate.
* @param value The value to associate to the tag.
*/
virtual void setValue(const std::string& tag, double value) throw (Exception) {
if (tag == name_)
values_[tag] = value;
else
throw Exception("SimpleMafStatisticsResult::setValue(). Unvalid tag name: " + tag + ".");
}
virtual void setValue(double value) { values_[name_] = value; }
};
/**
* @brief General interface for computing statistics based on a Maf block.
*
* @author Julien Dutheil
* @see MafBlock
*/
class MafStatistics
{
public:
MafStatistics() {}
virtual ~MafStatistics() {}
public:
virtual std::string getShortName() const = 0;
virtual std::string getFullName() const = 0;
virtual const MafStatisticsResult& getResult() const = 0;
virtual void compute(const MafBlock& block) = 0;
/**
* @return A vector with all available tags.
*/
virtual std::vector<std::string> getSupportedTags() const = 0;
};
/**
* @brief Partial implementation of MafStatistics, for convenience.
*/
class AbstractMafStatistics:
public virtual MafStatistics
{
protected:
MafStatisticsResult result_;
public:
AbstractMafStatistics(): result_() {}
virtual ~AbstractMafStatistics() {}
public:
const MafStatisticsResult& getResult() const { return result_; }
};
/**
* @brief Partial implementation of MafStatistics, for convenience.
*/
class AbstractMafStatisticsSimple:
public MafStatistics
{
protected:
SimpleMafStatisticsResult result_;
public:
AbstractMafStatisticsSimple(const std::string& name): result_(name) {}
virtual ~AbstractMafStatisticsSimple() {}
public:
const SimpleMafStatisticsResult& getResult() const { return result_; }
std::vector<std::string> getSupportedTags() const { return result_.getAvailableTags(); }
};
/**
* @brief Computes the pairwise divergence for a pair of sequences in a maf block.
*/
class PairwiseDivergenceMafStatistics:
public AbstractMafStatisticsSimple
{
private:
std::string species1_;
std::string species2_;
public:
PairwiseDivergenceMafStatistics(const std::string& species1, const std::string& species2):
AbstractMafStatisticsSimple("Divergence"), species1_(species1), species2_(species2) {}
~PairwiseDivergenceMafStatistics() {}
public:
std::string getShortName() const { return "Div." + species1_ + "-" + species2_; }
std::string getFullName() const { return "Pairwise divergence between " + species1_ + " and " + species2_ + "."; }
void compute(const MafBlock& block);
};
/**
* @brief Computes the number of sequences in a maf block.
*/
class BlockSizeMafStatistics:
public AbstractMafStatisticsSimple
{
public:
BlockSizeMafStatistics(): AbstractMafStatisticsSimple("BlockSize") {}
~BlockSizeMafStatistics() {}
public:
std::string getShortName() const { return "BlockSize"; }
std::string getFullName() const { return "Number of sequences."; }
void compute(const MafBlock& block) {
result_.setValue(static_cast<double>(block.getNumberOfSequences()));
}
};
/**
* @brief Computes the number of columns in a maf block.
*/
class BlockLengthMafStatistics:
public AbstractMafStatisticsSimple
{
public:
BlockLengthMafStatistics(): AbstractMafStatisticsSimple("BlockLength") {}
~BlockLengthMafStatistics() {}
public:
std::string getShortName() const { return "BlockLength"; }
std::string getFullName() const { return "Number of sites."; }
void compute(const MafBlock& block) {
result_.setValue(static_cast<double>(block.getNumberOfSites()));
}
};
/**
* @brief Retrieve the sequence length (number of nucleotides) for a given species in a maf block.
*
* If no sequence is found for the current block, 0 is returned.
* If several sequences are found for a given species, an exception is thrown.
*/
class SequenceLengthMafStatistics:
public AbstractMafStatisticsSimple
{
private:
std::string species_;
public:
SequenceLengthMafStatistics(const std::string& species): AbstractMafStatisticsSimple("BlockSize"), species_(species) {}
~SequenceLengthMafStatistics() {}
public:
std::string getShortName() const { return "SequenceLengthFor" + species_; }
std::string getFullName() const { return "Sequence length for species " + species_; }
void compute(const MafBlock& block) {
std::vector<const MafSequence*> seqs = block.getSequencesForSpecies(species_);
if (seqs.size() == 0)
result_.setValue(0.);
else if (seqs.size() == 1)
result_.setValue(static_cast<double>(SequenceTools::getNumberOfSites(*seqs[0])));
else
throw Exception("SequenceLengthMafStatistics::compute. More than one sequence found for species " + species_ + " in current block.");
}
};
/**
* @brief Retrieves the alignment score of a maf block.
*/
class AlignmentScoreMafStatistics:
public AbstractMafStatisticsSimple
{
public:
AlignmentScoreMafStatistics(): AbstractMafStatisticsSimple("AlnScore") {}
~AlignmentScoreMafStatistics() {}
public:
std::string getShortName() const { return "AlnScore"; }
std::string getFullName() const { return "Alignment score."; }
void compute(const MafBlock& block) {
result_.setValue(block.getScore());
}
};
/**
* @brief Compute the base frequencies of a maf block.
*
* For each block, provides the following numbers (with their corresponding tags):
* - A: total counts of A
* - C: total counts of C
* - G: total counts of G
* - T [or U]: total counts of T/U
* - Gap: total counts of gaps
* - Unresolved: total counts of unresolved characters
* The sum of all characters should equal BlockSize x BlockLength
*/
class CharacterCountsMafStatistics:
public AbstractMafStatistics
{
private:
const Alphabet* alphabet_;
public:
CharacterCountsMafStatistics(const Alphabet* alphabet): AbstractMafStatistics(), alphabet_(alphabet) {}
CharacterCountsMafStatistics(const CharacterCountsMafStatistics& stats):
AbstractMafStatistics(stats), alphabet_(stats.alphabet_) {}
CharacterCountsMafStatistics& operator=(const CharacterCountsMafStatistics& stats) {
AbstractMafStatistics::operator=(stats);
alphabet_ = stats.alphabet_;
return *this;
}
virtual ~CharacterCountsMafStatistics() {}
public:
std::string getShortName() const { return "Count"; }
std::string getFullName() const { return "Character counts."; }
void compute(const MafBlock& block);
std::vector<std::string> getSupportedTags() const;
};
/**
* @brief Partial implementation of MafStatistics for method working on a subset of species, in a site-wise manner.
*
* This class stores a seleciton of species and create for each block the corresponding SiteContainer.
*/
class AbstractSpeciesSelectionMafStatistics:
public virtual MafStatistics
{
private:
std::vector<std::string> species_;
public:
AbstractSpeciesSelectionMafStatistics(const std::vector<std::string>& species):
species_(species) {}
protected:
SiteContainer* getSiteContainer(const MafBlock& block);
};
/**
* @brief Compute the Site Frequency Spectrum of a maf block.
*
* The ancestral states are considered as unknown, so that 10000 and 11110 sites are treated equally.
*/
class SiteFrequencySpectrumMafStatistics:
public AbstractMafStatistics,
public AbstractSpeciesSelectionMafStatistics
{
private:
class Categorizer {
private:
std::vector<double> bounds_;
public:
Categorizer(const std::vector<double>& bounds): bounds_(bounds) {
std::sort(bounds_.begin(), bounds_.end());
}
public:
size_t getNumberOfCategories() const { return (bounds_.size() - 1); }
//Category numbers start at 1!
size_t getCategory(double value) const throw (OutOfRangeException) {
if (value < bounds_[0])
throw OutOfRangeException("SiteFrequencySpectrumMafStatistics::Categorizer::getCategory.", value, *bounds_.begin(), *bounds_.rbegin());
for (size_t i = 1; i < bounds_.size(); ++i) {
if (value < bounds_[i])
return i;
}
throw OutOfRangeException("SiteFrequencySpectrumMafStatistics::Categorizer::getCategory.", value, *bounds_.begin(), *bounds_.rbegin());
}
};
private:
const Alphabet* alphabet_;
Categorizer categorizer_;
std::vector<unsigned int> counts_;
public:
SiteFrequencySpectrumMafStatistics(const Alphabet* alphabet, const std::vector<double>& bounds, const std::vector<std::string>& ingroup):
AbstractMafStatistics(),
AbstractSpeciesSelectionMafStatistics(ingroup),
alphabet_(alphabet),
categorizer_(bounds),
counts_(bounds.size() - 1)
{}
SiteFrequencySpectrumMafStatistics(const SiteFrequencySpectrumMafStatistics& stats):
AbstractMafStatistics(),
AbstractSpeciesSelectionMafStatistics(stats),
alphabet_(stats.alphabet_),
categorizer_(stats.categorizer_),
counts_(stats.counts_)
{}
SiteFrequencySpectrumMafStatistics& operator=(const SiteFrequencySpectrumMafStatistics& stats) {
AbstractMafStatistics::operator=(stats);
AbstractSpeciesSelectionMafStatistics::operator=(stats);
alphabet_ = stats.alphabet_;
categorizer_ = stats.categorizer_;
counts_ = stats.counts_;
return *this;
}
virtual ~SiteFrequencySpectrumMafStatistics() {}
public:
std::string getShortName() const { return "SiteFrequencySpectrum"; }
std::string getFullName() const { return "Site frequency spectrum."; }
void compute(const MafBlock& block);
std::vector<std::string> getSupportedTags() const;
};
/**
* @brief Compute a few site statistics in a maf block.
*
* Computed statistics include:
* - Number of sites without gaps
* - Number of complete sites (no gap, no unresolved)
* - Number of parsimony informative sites
*/
class SiteMafStatistics:
public AbstractMafStatistics,
public AbstractSpeciesSelectionMafStatistics
{
public:
SiteMafStatistics(const std::vector<std::string>& species):
AbstractMafStatistics(),
AbstractSpeciesSelectionMafStatistics(species)
{}
virtual ~SiteMafStatistics() {}
public:
std::string getShortName() const { return "SiteStatistics"; }
std::string getFullName() const { return "Site statistics."; }
void compute(const MafBlock& block);
std::vector<std::string> getSupportedTags() const;
};
} // end of namespace bpp
#endif //_MAFSTATISTICS_H_
|