1 #ifndef ALGO_PHY_TREE___BIO_TREE_CONV__HPP
2 #define ALGO_PHY_TREE___BIO_TREE_CONV__HPP
55 template<
class TDstTreeNode,
class TSrcTreeNode,
class TNodeConvFunc>
78 if (delta_level == 0) {
85 parent_node->AddNode(pnode);
90 if (delta_level == 1) {
94 parent_node->AddNode(pnode);
98 if (delta_level == -1) {
112 unsigned int uid = src_node.GetValue().GetId();
113 pnode->GetValue().SetId(uid);
116 return pnode.release();
129 template<
class TDynamicTree,
class TSrcBioTree,
class TNodeConvFunc>
131 const TSrcBioTree& bio_tree,
132 TNodeConvFunc node_conv)
135 typedef typename TSrcBioTree::TBioTreeNode TSrcTreeNodeType;
136 typedef typename TDynamicTree::TBioTreeNode TDstTreeNodeType;
142 const TSrcTreeNodeType *
n = bio_tree.GetTreeNode();
157 template<
class TDynamicTree,
class TTreeNode,
class TNodeConvFunc>
159 const TTreeNode* src_tree,
160 TNodeConvFunc node_conv)
164 typedef TTreeNode TSrcTreeNodeType;
165 typedef typename TDynamicTree::TBioTreeNode TDstTreeNodeType;
181 template<
class TDynamicTree,
class TTreeNode,
class TNodeConvFunc>
183 TNodeConvFunc node_conv,
184 TTreeNode*& dst_node)
186 typedef TTreeNode TDstTreeNodeType;
187 typedef typename TDynamicTree::TBioTreeNode TSrcTreeNodeType;
192 const TSrcTreeNodeType *
n = dyn_tree.GetTreeNode();
210 template<
class TBioTreeContainer,
class TDynamicTree>
219 typedef typename TNodeList::value_type::element_type
TCNode;
228 m_NodeList = &(tree_container->SetNodes().Set());
235 if (delta_level < 0) {
248 cnode->SetParent(node_parent->GetValue().GetId());
252 TDynamicNodeValueType::TNodeFeaturesType::TFeatureList TFList;
253 const TFList& flist = v.features.GetFeatureList();
255 if (!flist.empty()) {
259 ITERATE(
typename TFList, it, flist) {
261 const string fvalue = it->value;
264 cfeat->SetFeatureid(fid);
265 cfeat->SetValue(fvalue);
267 fset.Set().push_back(cfeat);
286 template<
class TBioTreeContainer,
class TDynamicTree>
288 const TDynamicTree& dyn_tree)
292 typedef typename TBioTreeContainer::TFdict TContainerDict;
298 TContainerDict& fd = tree_container.SetFdict();
299 typename TContainerDict::Tdata& feat_list = fd.Set();
301 typename TContainerDict::Tdata::value_type::element_type TCFeatureDescr;
305 const string& fvalue = it->second;
312 feat_list.push_back(d);
319 typedef typename TDynamicTree::TBioTreeNode TTreeNode;
320 const TTreeNode *
n = dyn_tree.GetTreeNode();
323 func(&tree_container);
334 template<
class TBioTreeContainer,
class TDynamicTree>
336 const TBioTreeContainer& tree_container,
337 bool preserve_node_ids =
false)
343 typedef typename TBioTreeContainer::TFdict TContainerDict;
346 const TContainerDict& fd = tree_container.GetFdict();
347 const typename TContainerDict::Tdata& feat_list = fd.Get();
349 ITERATE(
typename TContainerDict::Tdata, it, feat_list) {
351 const string& fvalue = (*it)->GetName();
357 typedef typename TBioTreeContainer::TNodes TCNodeSet;
358 typedef typename TCNodeSet::Tdata TNodeList;
359 typedef typename TNodeList::value_type::element_type TCNode;
361 const TNodeList node_list = tree_container.GetNodes().Get();
363 std::map<TBioTreeNodeId, typename TDynamicTree::TBioTreeNode*> pmap;
365 ITERATE(
typename TNodeList, it, node_list) {
371 typedef typename TDynamicTree::TBioTreeNode TDynamicNodeType;
372 typedef typename TDynamicNodeType::TValueType TDynamicNodeValueType;
374 TDynamicNodeValueType v;
375 if (preserve_node_ids) {
379 typedef typename TCNode::TFeatures TCNodeFeatureSet;
381 if (cnode->CanGetFeatures()) {
382 const TCNodeFeatureSet& fset = cnode->GetFeatures();
384 const typename TCNodeFeatureSet::Tdata& flist = fset.Get();
386 ITERATE(
typename TCNodeFeatureSet::Tdata, fit, flist) {
387 unsigned int fid = (*fit)->GetFeatureid();
388 const string& fvalue = (*fit)->GetValue();
390 v.features.SetFeature(fid, fvalue);
396 if (cnode->CanGetParent()) {
398 typename TDynamicTree::TBioTreeNode* node =
NULL;
400 typename TDynamicTree::TBioTreeNode* parent_node = pmap[parent_id];
401 if (parent_node !=
NULL) {
402 node = dyn_tree.AddNode(v, parent_node);
403 if (!preserve_node_ids) {
404 dyn_tree.SetNodeId(node);
413 TDynamicNodeType* dnode =
new TDynamicNodeType(v);
414 dyn_tree.SetTreeNode(dnode);
415 if (!preserve_node_ids) {
416 dyn_tree.SetNodeId(dnode);
427 template<
class TBioTreeContainer,
class TDynamicForest>
429 const TDynamicForest& dyn_forest)
433 typedef typename TBioTreeContainer::TFdict TContainerDict;
439 TContainerDict& fd = tree_container.SetFdict();
440 typename TContainerDict::Tdata& feat_list = fd.Set();
442 typename TContainerDict::Tdata::value_type::element_type TCFeatureDescr;
446 const string& fvalue = it->second;
453 feat_list.push_back(d);
459 typedef typename TDynamicForest::TBioTree::TBioTreeNode TTreeNode;
461 func(&tree_container);
463 for (
unsigned int i=0;
i<dyn_forest.GetTrees().
size(); ++
i) {
464 const TTreeNode *
n = dyn_forest.GetTrees()[
i]->GetTreeNode();
474 template<
class TBioTreeContainer,
class TDynamicForest>
476 const TBioTreeContainer& tree_container)
482 typedef typename TBioTreeContainer::TFdict TContainerDict;
485 const TContainerDict& fd = tree_container.GetFdict();
486 const typename TContainerDict::Tdata& feat_list = fd.Get();
488 ITERATE(
typename TContainerDict::Tdata, it, feat_list) {
490 const string& fvalue = (*it)->GetName();
496 typedef typename TBioTreeContainer::TNodes TCNodeSet;
497 typedef typename TCNodeSet::Tdata TNodeList;
498 typedef typename TNodeList::value_type::element_type TCNode;
500 const TNodeList node_list = tree_container.GetNodes().Get();
502 typename TDynamicForest::TBioTree* current_tree =
NULL;
504 ITERATE(
typename TNodeList, it, node_list) {
510 typedef typename TDynamicForest::TBioTree TDynamicTree;
511 typedef typename TDynamicTree::TBioTreeNode TDynamicNodeType;
512 typedef typename TDynamicNodeType::TValueType TDynamicNodeValueType;
514 TDynamicNodeValueType v;
517 typedef typename TCNode::TFeatures TCNodeFeatureSet;
519 if (cnode->CanGetFeatures()) {
520 const TCNodeFeatureSet& fset = cnode->GetFeatures();
522 const typename TCNodeFeatureSet::Tdata& flist = fset.Get();
524 ITERATE(
typename TCNodeFeatureSet::Tdata, fit, flist) {
525 unsigned int fid = (*fit)->GetFeatureid();
526 const string& fvalue = (*fit)->GetValue();
528 v.features.SetFeature(fid, fvalue);
534 if (cnode->CanGetParent()) {
535 if (current_tree !=
NULL) {
537 current_tree->AddNode(v, parent_id);
544 current_tree =
new typename TDynamicForest::TBioTree();
545 dyn_forest.AddTree(current_tree);
547 TDynamicNodeType* dnode =
new TDynamicNodeType(v);
548 current_tree->SetTreeNode(dnode);
578 template<
class TITaxon4Each,
class TITaxon1Node,
579 class TITreeIterator,
class TBioTreeContainer>
583 typedef typename TITreeIterator::EAction
EAction;
587 typedef typename TNodeList::value_type::element_type
TCNode;
598 m_NodeList = &(tree_container->SetNodes().Set());
616 vector<TTaxId>::size_type psize =
m_Parents.size();
619 cnode->SetParent(
TAX_ID_TO(
int, parent_tax_id));
631 cfeat->SetValue(pNode->GetName());
633 fset.Set().push_back(cfeat);
639 cfeat->SetValue(pNode->GetName());
641 fset.Set().push_back(cfeat);
648 cfeat->SetValue(pNode->GetBlastName());
650 fset.Set().push_back(cfeat);
657 int v = pNode->GetRank();
660 fset.Set().push_back(cfeat);
667 int v = pNode->GetDivision();
670 fset.Set().push_back(cfeat);
677 int v = pNode->GetGC();
680 fset.Set().push_back(cfeat);
687 int v = pNode->GetMGC();
690 fset.Set().push_back(cfeat);
697 int v = pNode->IsUncultured();
700 fset.Set().push_back(cfeat);
708 fset.Set().push_back(cfeat);
713 return TITreeIterator::eOk;
719 m_Parents.push_back(pParent->GetTaxId());
720 return TITreeIterator::eOk;
727 return TITreeIterator::eOk;
742 template<
class TBioTreeContainer>
744 unsigned int feature_id,
745 const string& feature_name)
747 typedef typename TBioTreeContainer::TFdict TContainerDict;
748 typedef typename TContainerDict::Tdata::value_type::element_type TFeatureDescr;
750 TContainerDict& fd = tree_container.SetFdict();
751 typename TContainerDict::Tdata& feat_list = fd.Set();
754 ITERATE(
typename TContainerDict::Tdata, it, feat_list) {
755 if ( (
unsigned int)((*it)->GetId()) == feature_id )
760 d->SetId(feature_id);
761 d->SetName(feature_name);
763 feat_list.push_back(d);
768 template<
class TBioTreeContainer,
769 class TTaxon1,
class TITaxon1Node,
class TITreeIterator>
773 typedef typename TITreeIterator::I4Each
T4Each;
775 TITreeIterator, TBioTreeContainer>
791 const TITaxon1Node* tax_node=0;
792 bool res = tax.LoadSubtree(tax_id, &tax_node);
795 tax_tree_iter->GoNode(tax_node);
797 tax_tree_iter->TraverseDownward(tax_vis);
808 tax_tree_iter->GoRoot();
810 tax_tree_iter->TraverseDownward(tax_vis);
843 template<
class TBioTreeContainer>
849 typedef typename TBioTreeContainer::TNodes TCNodeSet;
850 typedef typename TCNodeSet::Tdata TNodeList;
851 typedef typename TNodeList::value_type::element_type TCNode;
853 const TNodeList node_list = tree_container.GetNodes().Get();
855 int number_roots = 0;
857 ITERATE(
typename TNodeList, it, node_list) {
861 if (!cnode->CanGetParent())
866 return (number_roots > 1);
Things for representing and manipulating bio trees.
Visitor functor to convert dynamic tree nodes to ASN.1 BioTree container.
Taxon1 tree visitor functor.
Visitor functor to convert one tree to another.
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
#define TAX_ID_TO(T, tax_id)
SStrictId_Tax::TId TTaxId
Taxon id type.
#define NCBI_THROW(exception_class, err_code, message)
Generic macro to throw an exception, given the exception class, error code and message string.
#define END_NCBI_SCOPE
End previously defined NCBI scope.
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
static string IntToString(int value, TNumToStringFlags flags=0, int base=10)
Convert int to string.
virtual ~CTaxon1NodeConvertVisitor()
TNodeList::value_type::element_type TCNode
void TreeConvert2Dynamic(TDynamicTree &dyn_tree, const TTreeNode *src_tree, TNodeConvFunc node_conv)
Convert CTreeNode<> to dynamic tree using a node converter.
TCNodeFeatureSet::Tdata TNodeFeatureList
void BioTreeConvert2Dynamic(TDynamicTree &dyn_tree, const TSrcBioTree &bio_tree, TNodeConvFunc node_conv)
Convert biotree to dynamic tree using a node converter.
void BioTreeAddFeatureToDictionary(TBioTreeContainer &tree_container, unsigned int feature_id, const string &feature_name)
void SetupFeatureDictionary(TBioTreeContainer &tree_container)
Add elements to the feature dictionary.
CTaxon1NodeConvertVisitor(TBioTreeContainer *tree_container)
TNodeList::value_type::element_type TCNode
TDynamicNodeType::TValueType TDynamicNodeValueType
TCNode::TFeatures TCNodeFeatureSet
Fun TreeDepthFirstTraverse(TTreeNode &tree_node, Fun func)
Depth-first tree traversal algorithm.
ETreeTraverseCode
Tree traverse code returned by the traverse predicate function.
unsigned int TBioTreeNodeId
Tree node id. Every node has its unique id in the tree.
TSrcTreeNode TSrcTreeNodeType
TCNode::TFeatures TCNodeFeatureSet
ETaxon1ConvFeatures
Feature ids for Bio-Tree.
TNodeConvFunc & m_ConvFunc
void operator()(TBioTreeContainer &tree_container, TTaxon1 &tax, TTaxId tax_id)
TBioTreeContainer::TNodes TCNodeSet
unsigned int TBioTreeFeatureId
Feature Id.
TDstTreeNodeType * MakeNewTreeNode(const TSrcTreeNodeType &src_node)
TNodeFeatureList::value_type::element_type TCNodeFeature
TBioTreeContainer * m_TreeContainer
CTree2TreeFunc(TNodeConvFunc &func)
CTaxon1ConvertToBioTreeContainer()
void BioTreeConvert2Container(TBioTreeContainer &tree_container, const TDynamicTree &dyn_tree)
Convert Dynamic tree to ASN.1 BioTree container.
virtual EAction LevelBegin(const TITaxon1Node *pParent)
TBioTreeFeatureId Register(const string &feature_name)
Register new feature, return its id.
vector< TTaxId > m_Parents
TNodeFeatureList::value_type::element_type TCNodeFeature
void BioTreeConvertContainer2Dynamic(TDynamicTree &dyn_tree, const TBioTreeContainer &tree_container, bool preserve_node_ids=false)
Convert ASN.1 BioTree container to dynamic tree.
TBioTreeContainer * m_Container
void operator()(TBioTreeContainer &tree_container, CRef< TITreeIterator > tax_tree_iter)
TDstTreeNodeType * m_DstTree
virtual EAction Execute(const TITaxon1Node *pNode)
CTaxon1NodeConvertVisitor< T4Each, TITaxon1Node, TITreeIterator, TBioTreeContainer > TTaxon1Visitor
TDstTreeNode TDstTreeNodeType
TCNodeFeatureSet::Tdata TNodeFeatureList
CBioTreeConvert2ContainerFunc(TBioTreeContainer *tree_container)
TITreeIterator::EAction EAction
TDstTreeNodeType * GetTreeNode()
TCNodeSet::Tdata TNodeList
TBioTreeContainer::TNodes TCNodeSet
const TFeatureDict & GetFeatureDict() const
Get reference on the internal map.
bool BioTreeContainerIsForest(const TBioTreeContainer &tree_container)
Function to determine tree if a given biotree container is a single tree or a forest.
ETreeTraverseCode operator()(const TDynamicNodeType &node, int delta_level)
TDynamicTree::TBioTreeNode TDynamicNodeType
TTreeNode * DynamicConvert2Tree(TDynamicTree &dyn_tree, TNodeConvFunc node_conv, TTreeNode *&dst_node)
Convert dynamic tree to CTreeNode<>, returned CTReeNode<> to be deleted by caller.
vector< TDstTreeNodeType * > m_TreeStack
virtual EAction LevelEnd(const TITaxon1Node *)
void BioTreeForestConvert2Container(TBioTreeContainer &tree_container, const TDynamicForest &dyn_forest)
Convert forest of Dynamic trees to ASN.1 BioTree container.
int GetMaxNodeId() const
Get max node id (available after conversion)
TITreeIterator::I4Each T4Each
void BioTreeConvertContainer2DynamicForest(TDynamicForest &dyn_forest, const TBioTreeContainer &tree_container)
Convert ASN.1 BioTree container to forest of dynamic trees.
TCNodeSet::Tdata TNodeList
ETreeTraverseCode operator()(const TSrcTreeNodeType &node, int delta_level)
@ eTreeTraverse
Keep traversal.
unsigned int
A callback function used to compare two keys in a database.
const struct ncbi::grid::netcache::search::fields::SIZE size