@@ -501,7 +501,23 @@ size_t NcbiTaxonomy::loadMerged(const std::string &mergedFile) {
501501 return count;
502502}
503503
504- std::unordered_map<TaxID, TaxonCounts> NcbiTaxonomy::getCladeCounts (const std::unordered_map<TaxID, unsigned int >& taxonCounts) const {
504+ std::unordered_map<TaxID, std::vector<TaxID>> NcbiTaxonomy::getParentToChildren () const {
505+ std::unordered_map<TaxID, std::vector<TaxID>> result;
506+ result.reserve (maxNodes);
507+
508+ // Build the adjacency (parent -> children)
509+ for (size_t i = 0 ; i < maxNodes; ++i) {
510+ const TaxonNode& tn = taxonNodes[i];
511+ if (tn.parentTaxId == tn.taxId ) {
512+ continue ;
513+ }
514+ result[tn.parentTaxId ].push_back (tn.taxId );
515+ }
516+
517+ return result;
518+ }
519+
520+ std::unordered_map<TaxID, TaxonCounts> NcbiTaxonomy::getCladeCounts (const std::unordered_map<TaxID, unsigned int >& taxonCounts, const std::unordered_map<TaxID, std::vector<TaxID>>& parentToChildren) const {
505521 std::unordered_map<TaxID, TaxonCounts> cladeCounts;
506522
507523 for (std::unordered_map<TaxID, unsigned int >::const_iterator it = taxonCounts.begin (); it != taxonCounts.end (); ++it) {
@@ -516,11 +532,12 @@ std::unordered_map<TaxID, TaxonCounts> NcbiTaxonomy::getCladeCounts(const std::u
516532 }
517533 }
518534
519- for (size_t i = 0 ; i < maxNodes; ++i) {
520- TaxonNode& tn = taxonNodes[i];
521- if (tn.parentTaxId != tn.taxId && cladeCounts.count (tn.taxId )) {
522- std::unordered_map<TaxID, TaxonCounts>::iterator itp = cladeCounts.find (tn.parentTaxId );
523- itp->second .children .push_back (tn.taxId );
535+ for (std::unordered_map<TaxID, TaxonCounts>::iterator it = cladeCounts.begin (); it != cladeCounts.end (); ++it) {
536+ TaxID parentTaxId = it->first ;
537+ TaxonCounts& taxCounts = it->second ;
538+ std::unordered_map<TaxID, std::vector<TaxID>>::const_iterator ptcIt = parentToChildren.find (parentTaxId);
539+ if (ptcIt != parentToChildren.end ()) {
540+ taxCounts.children = ptcIt->second ;
524541 }
525542 }
526543
0 commit comments