|
A library for working with phylogenetic and population genetic data.
v0.32.0
|
|
Go to the documentation of this file.
44 #include <unordered_set>
82 while( parent !=
nullptr ) {
91 size_t count = tax.
size();
92 for(
auto const& t : tax ) {
101 for(
auto const& t : tax ) {
102 if( t.size() == 0 ) {
118 for(
auto& c : tax ) {
127 if( tax.
size() == 0 ) {
128 return std::vector< size_t >();
131 std::vector< size_t > result( 1, 0 );
132 result[ 0 ] = tax.
size();
134 for(
auto const& child : tax ) {
137 if( result.size() < cres.size() + 1 ) {
138 result.resize( cres.size() + 1, 0 );
141 for(
size_t i = 0; i < cres.size(); ++i ) {
142 result[ i+1 ] += cres[ i ];
150 std::string
const& rank,
154 for(
auto const& t : tax ) {
157 if( case_sensitive ) {
158 if( t.rank() == rank ) {
177 std::unordered_map< std::string, size_t> result;
179 for(
auto const& taxon : tax ) {
180 if( case_sensitive ) {
181 ++result[ taxon.rank() ];
188 for(
auto const& ctaxon : children ) {
189 result[ ctaxon.first ] += ctaxon.second;
198 std::unordered_set<std::string> ids;
199 bool has_duplicates =
false;
201 auto collect_and_check = [&](
Taxon const& tax ){
202 if( ids.count( tax.id() ) > 0 ) {
203 has_duplicates =
true;
206 ids.insert( tax.id() );
210 return ! has_duplicates;
220 auto comp_by_name_cs = [](
Taxon const& lhs,
Taxon const& rhs ) {
221 return lhs.
name() < rhs.name();
223 auto comp_by_name_ci = [](
Taxon const& lhs,
Taxon const& rhs ) {
228 if( case_sensitive ) {
230 tax.
sort( comp_by_name_cs );
233 tax.
sort( comp_by_name_ci );
238 for(
auto& child : tax ) {
250 for(
auto& c : tax ) {
264 printer.line_limit( 10 );
265 printer.print( out, tax );
272 std::function< bool (
Taxonomy const&,
bool,
size_t ) > validate_rec = [&] (
273 Taxonomy const& tax,
bool stop_at_first_error,
size_t level
276 for(
auto const& c : tax ) {
279 if( (level == 0 && c.parent() !=
nullptr) || (level > 0 && c.parent() != &tax) ) {
280 LOG_INFO <<
"Taxon child with invalid parent pointer: " << c.name();
281 if( stop_at_first_error ) {
289 if( ! validate_rec( c, stop_at_first_error, level + 1 )) {
290 if( stop_at_first_error ) {
300 return validate_rec( taxonomy, stop_at_first_error, 0 );
std::unordered_map< std::string, size_t > taxa_count_ranks(Taxonomy const &tax, bool case_sensitive)
Count the number of Taxa in a Taxonomy per rank.
#define LOG_INFO
Log an info message. See genesis::utils::LoggingLevel.
Taxon const * parent() const
Return a pointer to the parent of this taxon, or a nullptr if this is the top level taxon.
bool has_unique_ids(Taxonomy const &tax)
Return true iff all IDs of the Taxa in the Taxonomy are unique.
bool equals_ci(std::string const &lhs, std::string const &rhs)
Compare two strings, case insensitive.
size_t taxon_level(Taxon const &taxon)
Return the level of depth of a given Taxon.
Tag used for find_taxon().
Taxon const * find_taxon_by_id(Taxonomy const &tax, std::string const &id)
Alias for find_taxon_by_id(..., DepthFirstSearch{}).
Store a Taxon, i.e., an element in a Taxonomy, with its name, rank, ID and sub-taxa.
Taxon const * find_taxon_by_name(Taxonomy const &tax, std::string const &name)
Alias for find_taxon_by_name(..., DepthFirstSearch{}).
bool validate(Taxonomy const &taxonomy, bool stop_at_first_error)
Validate the internal data structures of a Taxonomy and its child Taxa Taxa.
Provides some commonly used string utility functions.
void sort(Compare comp)
Sort the taxonomy according to some compare criterion.
size_t taxa_count_lowest_levels(Taxonomy const &tax)
Return the number of lowest level Taxa (i.e., taxa without sub-taxa) in the Taxonomy.
void sort_by_name(Taxonomy &tax, bool recursive, bool case_sensitive)
Sort the Taxa of a Taxonomy by their name.
Provides easy and fast logging functionality.
std::string const & name() const
Return the name of this taxon.
void preorder_for_each(Taxonomy &tax, std::function< void(Taxon &)> fn, bool include_inner_taxa=true)
Apply a function to all taxa of the Taxonomy, traversing it in preorder.
Container namespace for all symbols of genesis in order to keep them separate when used as a library.
Store a Taxonomy, i.e., a nested hierarchy of Taxa.
size_t total_taxa_count(Taxonomy const &tax)
Return the total number of taxa contained in the Taxomony, i.e., the number of (non-unique) names of ...
size_t taxa_count_with_rank(Taxonomy const &tax, std::string const &rank, bool case_sensitive)
Count the number of Taxa in a Taxonomy that have a certain rank assigned to them.
std::vector< size_t > taxa_count_levels(Taxonomy const &tax)
Count the number of Taxa at each level of depth in the Taxonomy.
size_t size() const
Return the number of immediate child Taxa.
void clear_children()
Remove all children.
size_t taxa_count_at_level(Taxonomy const &tax, size_t level)
Count the number of Taxa at a certain level of depth in the Taxonomy.
void remove_taxa_at_level(Taxonomy &tax, size_t level)
Remove all Taxa at a given level of depth in the Taxonomy hierarchy, and all their children.
constexpr char to_lower(char c) noexcept
Return the lower case version of a letter, ASCII-only.
Simple printer class for Taxonomy.
std::ostream & operator<<(std::ostream &out, Taxonomy const &tax)
Print the contents of a Taxonomy, i.e., all nested taxa, up to a limit of 10.