NCBI C++ ToolKit
bio_tree_conv.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef ALGO_PHY_TREE___BIO_TREE_CONV__HPP
2 #define ALGO_PHY_TREE___BIO_TREE_CONV__HPP
3 
4 /* $Id: bio_tree_conv.hpp 92083 2020-12-21 15:19:57Z grichenk $
5  * ===========================================================================
6  *
7  * PUBLIC DOMAIN NOTICE
8  * National Center for Biotechnology Information
9  *
10  * This software/database is a "United States Government Work" under the
11  * terms of the United States Copyright Act. It was written as part of
12  * the author's official duties as a United States Government employee and
13  * thus cannot be copyrighted. This software/database is freely available
14  * to the public for use. The National Library of Medicine and the U.S.
15  * Government have not placed any restriction on its use or reproduction.
16  *
17  * Although all reasonable efforts have been taken to ensure the accuracy
18  * and reliability of the software and data, the NLM and the U.S.
19  * Government do not and cannot warrant the performance or results that
20  * may be obtained by using this software or data. The NLM and the U.S.
21  * Government disclaim all warranties, express or implied, including
22  * warranties of performance, merchantability or fitness for any particular
23  * purpose.
24  *
25  * Please cite the author in any work or product based on this material.
26  *
27  * ===========================================================================
28  *
29  * Authors: Anatoliy Kuznetsov
30  *
31  * File Description: bio trees convertions
32  *
33  */
34 
35 /// @file bio_tree.hpp
36 /// Things for bio tree convertions
37 
39 
41 
42 /** @addtogroup Tree
43  *
44  * @{
45  */
46 
47 
48 // --------------------------------------------------------------------------
49 
50 
51 /// Visitor functor to convert one tree to another
52 ///
53 /// @internal
54 
55 template<class TDstTreeNode, class TSrcTreeNode, class TNodeConvFunc>
57 {
58 public:
59  typedef TDstTreeNode TDstTreeNodeType;
60  typedef TSrcTreeNode TSrcTreeNodeType;
61 
62 public:
63  CTree2TreeFunc(TNodeConvFunc& func) : m_DstTree(0), m_ConvFunc(func) {}
64 
65 
68  int delta_level)
69  {
70  if (m_TreeStack.size() == 0) {
71  unique_ptr<TDstTreeNodeType> pnode(MakeNewTreeNode(node));
72 
73  m_TreeStack.push_back(pnode.get());
74  m_DstTree = pnode.release();
75  return eTreeTraverse;
76  }
77 
78  if (delta_level == 0) {
79  if (m_TreeStack.size() > 0) {
80  m_TreeStack.pop_back();
81  }
82  TDstTreeNodeType* parent_node = m_TreeStack.back();
83  TDstTreeNodeType* pnode= MakeNewTreeNode(node);
84 
85  parent_node->AddNode(pnode);
86  m_TreeStack.push_back(pnode);
87  return eTreeTraverse;
88  }
89 
90  if (delta_level == 1) {
91  TDstTreeNodeType* parent_node = m_TreeStack.back();
92  TDstTreeNodeType* pnode= MakeNewTreeNode(node);
93 
94  parent_node->AddNode(pnode);
95  m_TreeStack.push_back(pnode);
96  return eTreeTraverse;
97  }
98  if (delta_level == -1) {
99  m_TreeStack.pop_back();
100  }
101 
102  return eTreeTraverse;
103  }
104 
106 
107 protected:
108 
110  {
111  unique_ptr<TDstTreeNodeType> pnode(new TDstTreeNodeType());
112  unsigned int uid = src_node.GetValue().GetId();
113  pnode->GetValue().SetId(uid);
114 
115  m_ConvFunc(*pnode.get(), src_node);
116  return pnode.release();
117  }
118 
119 private:
121  TNodeConvFunc& m_ConvFunc;
122  vector<TDstTreeNodeType*> m_TreeStack;
123 };
124 
125 
126 
127 /// Convert biotree to dynamic tree using a node converter
128 ///
129 template<class TDynamicTree, class TSrcBioTree, class TNodeConvFunc>
130 void BioTreeConvert2Dynamic(TDynamicTree& dyn_tree,
131  const TSrcBioTree& bio_tree,
132  TNodeConvFunc node_conv)
133 {
134  dyn_tree.Clear();
135  typedef typename TSrcBioTree::TBioTreeNode TSrcTreeNodeType;
136  typedef typename TDynamicTree::TBioTreeNode TDstTreeNodeType;
137 
138 
140  func(node_conv);
141 
142  const TSrcTreeNodeType *n = bio_tree.GetTreeNode();
143 
145  rfunc(
146  TreeDepthFirstTraverse(*(const_cast<TSrcTreeNodeType*>(n)), func)
147  );
148 
149  dyn_tree.SetTreeNode(rfunc.GetTreeNode());
150 }
151 
152 
153 
154 
155 /// Convert CTreeNode<> to dynamic tree using a node converter
156 ///
157 template<class TDynamicTree, class TTreeNode, class TNodeConvFunc>
158 void TreeConvert2Dynamic(TDynamicTree& dyn_tree,
159  const TTreeNode* src_tree,
160  TNodeConvFunc node_conv)
161 {
162  dyn_tree.Clear();
163 
164  typedef TTreeNode TSrcTreeNodeType;
165  typedef typename TDynamicTree::TBioTreeNode TDstTreeNodeType;
166 
168  func(node_conv);
170  rfunc(
171  TreeDepthFirstTraverse(*(const_cast<TTreeNode*>(src_tree)), func)
172  );
173  dyn_tree.SetTreeNode(rfunc.GetTreeNode());
174 }
175 
176 
177 
178 /// Convert dynamic tree to CTreeNode<>,
179 /// returned CTReeNode<> to be deleted by caller.
180 ///
181 template<class TDynamicTree, class TTreeNode, class TNodeConvFunc>
182 TTreeNode* DynamicConvert2Tree(TDynamicTree& dyn_tree,
183  TNodeConvFunc node_conv,
184  TTreeNode*& dst_node)
185 {
186  typedef TTreeNode TDstTreeNodeType;
187  typedef typename TDynamicTree::TBioTreeNode TSrcTreeNodeType;
188 
190  func(node_conv);
191 
192  const TSrcTreeNodeType *n = dyn_tree.GetTreeNode();
193 
195  rfunc(
196  TreeDepthFirstTraverse(*(const_cast<TSrcTreeNodeType*>(n)), func)
197  );
198  return (dst_node = rfunc.GetTreeNode());
199 }
200 
201 
202 
203 
204 
205 // --------------------------------------------------------------------------
206 
207 /// Visitor functor to convert dynamic tree nodes to ASN.1 BioTree container
208 ///
209 /// @internal
210 template<class TBioTreeContainer, class TDynamicTree>
212 {
213 protected:
214  typedef typename TDynamicTree::TBioTreeNode TDynamicNodeType;
215  typedef typename TDynamicNodeType::TValueType TDynamicNodeValueType;
216 
217  typedef typename TBioTreeContainer::TNodes TCNodeSet;
218  typedef typename TCNodeSet::Tdata TNodeList;
219  typedef typename TNodeList::value_type::element_type TCNode;
220  typedef typename TCNode::TFeatures TCNodeFeatureSet;
221  typedef typename TCNodeFeatureSet::Tdata TNodeFeatureList;
222  typedef typename
223  TNodeFeatureList::value_type::element_type TCNodeFeature;
224 public:
225  CBioTreeConvert2ContainerFunc(TBioTreeContainer* tree_container)
226  : m_Container(tree_container)
227  {
228  m_NodeList = &(tree_container->SetNodes().Set());
229  }
230 
233  int delta_level)
234  {
235  if (delta_level < 0) {
236  return eTreeTraverse;
237  }
238 
239  const TDynamicNodeValueType& v = node.GetValue();
240  TBioTreeNodeId uid = v.GetId();
241 
242  CRef<TCNode> cnode(new TCNode);
243  cnode->SetId(uid);
244 
245  const TDynamicNodeType* node_parent =
246  (TDynamicNodeType*) node.GetParent();
247  if (node_parent) {
248  cnode->SetParent(node_parent->GetValue().GetId());
249  }
250 
251  typedef typename
252  TDynamicNodeValueType::TNodeFeaturesType::TFeatureList TFList;
253  const TFList& flist = v.features.GetFeatureList();
254 
255  if (!flist.empty()) {
256 
257  TCNodeFeatureSet& fset = cnode->SetFeatures();
258 
259  ITERATE(typename TFList, it, flist) {
260  TBioTreeFeatureId fid = it->id;
261  const string fvalue = it->value;
262 
264  cfeat->SetFeatureid(fid);
265  cfeat->SetValue(fvalue);
266 
267  fset.Set().push_back(cfeat);
268 
269 
270  } // ITERATE
271  }
272 
273  m_NodeList->push_back(cnode);
274 
275  return eTreeTraverse;
276  }
277 
278 private:
279  TBioTreeContainer* m_Container;
281 };
282 
283 
284 /// Convert Dynamic tree to ASN.1 BioTree container
285 ///
286 template<class TBioTreeContainer, class TDynamicTree>
287 void BioTreeConvert2Container(TBioTreeContainer& tree_container,
288  const TDynamicTree& dyn_tree)
289 {
290  // Convert feature dictionary
291 
292  typedef typename TBioTreeContainer::TFdict TContainerDict;
293 
294  const CBioTreeFeatureDictionary& dict = dyn_tree.GetFeatureDict();
295  const CBioTreeFeatureDictionary::TFeatureDict& dict_map =
296  dict.GetFeatureDict();
297 
298  TContainerDict& fd = tree_container.SetFdict();
299  typename TContainerDict::Tdata& feat_list = fd.Set();
300  typedef
301  typename TContainerDict::Tdata::value_type::element_type TCFeatureDescr;
302 
304  TBioTreeFeatureId fid = it->first;
305  const string& fvalue = it->second;
306 
307  {{
308  CRef<TCFeatureDescr> d(new TCFeatureDescr);
309  d->SetId(fid);
310  d->SetName(fvalue);
311 
312  feat_list.push_back(d);
313  }}
314  } // ITERATE
315 
316 
317  // convert tree data (nodes)
318 
319  typedef typename TDynamicTree::TBioTreeNode TTreeNode;
320  const TTreeNode *n = dyn_tree.GetTreeNode();
321 
323  func(&tree_container);
324  TreeDepthFirstTraverse(*(const_cast<TTreeNode*>(n)), func);
325 
326 }
327 
328 /// Convert ASN.1 BioTree container to dynamic tree
329 /// @param dyn_tree Ouput dynamic tree
330 /// @param tree_container Input tree container
331 /// @param preserve_node_ids If true, node ids in the resulting dynamic tree
332 /// will be the same is in the input tree container
333 ///
334 template<class TBioTreeContainer, class TDynamicTree>
335 void BioTreeConvertContainer2Dynamic(TDynamicTree& dyn_tree,
336  const TBioTreeContainer& tree_container,
337  bool preserve_node_ids = false)
338 {
339  dyn_tree.Clear();
340 
341  // Convert feature dictionary
342 
343  typedef typename TBioTreeContainer::TFdict TContainerDict;
344 
345  CBioTreeFeatureDictionary& dict = dyn_tree.GetFeatureDict();
346  const TContainerDict& fd = tree_container.GetFdict();
347  const typename TContainerDict::Tdata& feat_list = fd.Get();
348 
349  ITERATE(typename TContainerDict::Tdata, it, feat_list) {
350  TBioTreeFeatureId fid = (*it)->GetId();
351  const string& fvalue = (*it)->GetName();
352 
353  dict.Register(fid, fvalue);
354  }
355 
356  // convert tree data (nodes)
357  typedef typename TBioTreeContainer::TNodes TCNodeSet;
358  typedef typename TCNodeSet::Tdata TNodeList;
359  typedef typename TNodeList::value_type::element_type TCNode;
360 
361  const TNodeList node_list = tree_container.GetNodes().Get();
362 
363  std::map<TBioTreeNodeId, typename TDynamicTree::TBioTreeNode*> pmap;
364 
365  ITERATE(typename TNodeList, it, node_list) {
366 
367  const CRef<TCNode>& cnode = *it;
368 
369  TBioTreeNodeId uid = cnode->GetId();
370 
371  typedef typename TDynamicTree::TBioTreeNode TDynamicNodeType;
372  typedef typename TDynamicNodeType::TValueType TDynamicNodeValueType;
373 
374  TDynamicNodeValueType v;
375  if (preserve_node_ids) {
376  v.SetId(uid);
377  }
378 
379  typedef typename TCNode::TFeatures TCNodeFeatureSet;
380 
381  if (cnode->CanGetFeatures()) {
382  const TCNodeFeatureSet& fset = cnode->GetFeatures();
383 
384  const typename TCNodeFeatureSet::Tdata& flist = fset.Get();
385 
386  ITERATE(typename TCNodeFeatureSet::Tdata, fit, flist) {
387  unsigned int fid = (*fit)->GetFeatureid();
388  const string& fvalue = (*fit)->GetValue();
389 
390  v.features.SetFeature(fid, fvalue);
391 
392  } // ITERATE
393 
394  }
395 
396  if (cnode->CanGetParent()) {
397  TBioTreeNodeId parent_id = cnode->GetParent();
398  typename TDynamicTree::TBioTreeNode* node = NULL;
399 
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);
405  }
406  }
407  else {
408  NCBI_THROW(CException, eUnknown, "Parent not found");
409  }
410 
411  pmap[uid] = node;
412  } else {
413  TDynamicNodeType* dnode = new TDynamicNodeType(v);
414  dyn_tree.SetTreeNode(dnode);
415  if (!preserve_node_ids) {
416  dyn_tree.SetNodeId(dnode);
417  }
418  pmap[uid] = dnode;
419  }
420 
421 
422  } // ITERATE TNodeList
423 }
424 
425 /// Convert forest of Dynamic trees to ASN.1 BioTree container
426 ///
427 template<class TBioTreeContainer, class TDynamicForest>
428 void BioTreeForestConvert2Container(TBioTreeContainer& tree_container,
429  const TDynamicForest& dyn_forest)
430 {
431  // Convert feature dictionary
432 
433  typedef typename TBioTreeContainer::TFdict TContainerDict;
434 
435  const CBioTreeFeatureDictionary& dict = dyn_forest.GetFeatureDict();
436  const CBioTreeFeatureDictionary::TFeatureDict& dict_map =
437  dict.GetFeatureDict();
438 
439  TContainerDict& fd = tree_container.SetFdict();
440  typename TContainerDict::Tdata& feat_list = fd.Set();
441  typedef
442  typename TContainerDict::Tdata::value_type::element_type TCFeatureDescr;
443 
445  TBioTreeFeatureId fid = it->first;
446  const string& fvalue = it->second;
447 
448  {{
449  CRef<TCFeatureDescr> d(new TCFeatureDescr);
450  d->SetId(fid);
451  d->SetName(fvalue);
452 
453  feat_list.push_back(d);
454  }}
455  } // ITERATE
456 
457 
458  // convert tree data (nodes)
459  typedef typename TDynamicForest::TBioTree::TBioTreeNode TTreeNode;
461  func(&tree_container);
462 
463  for (unsigned int i=0; i<dyn_forest.GetTrees().size(); ++i) {
464  const TTreeNode *n = dyn_forest.GetTrees()[i]->GetTreeNode();
465  TreeDepthFirstTraverse(*(const_cast<TTreeNode*>(n)), func);
466  }
467 }
468 
469 /// Convert ASN.1 BioTree container to forest of dynamic trees
470 ///
471 // Assume that the data in the container is sorted such that the nodes of trees
472 // are not interleaved with nodes of other trees, and that all nodes within a
473 // tree are sorted such that the parent of every node comes before the node itself.
474 template<class TBioTreeContainer, class TDynamicForest>
475 void BioTreeConvertContainer2DynamicForest(TDynamicForest& dyn_forest,
476  const TBioTreeContainer& tree_container)
477 {
478  dyn_forest.Clear();
479 
480  // Convert feature dictionary
481 
482  typedef typename TBioTreeContainer::TFdict TContainerDict;
483 
484  CBioTreeFeatureDictionary& dict = dyn_forest.GetFeatureDict();
485  const TContainerDict& fd = tree_container.GetFdict();
486  const typename TContainerDict::Tdata& feat_list = fd.Get();
487 
488  ITERATE(typename TContainerDict::Tdata, it, feat_list) {
489  TBioTreeFeatureId fid = (*it)->GetId();
490  const string& fvalue = (*it)->GetName();
491 
492  dict.Register(fid, fvalue);
493  }
494 
495  // convert tree data (nodes)
496  typedef typename TBioTreeContainer::TNodes TCNodeSet;
497  typedef typename TCNodeSet::Tdata TNodeList;
498  typedef typename TNodeList::value_type::element_type TCNode;
499 
500  const TNodeList node_list = tree_container.GetNodes().Get();
501 
502  typename TDynamicForest::TBioTree* current_tree = NULL;
503 
504  ITERATE(typename TNodeList, it, node_list) {
505 
506  const CRef<TCNode>& cnode = *it;
507 
508  TBioTreeNodeId uid = cnode->GetId();
509 
510  typedef typename TDynamicForest::TBioTree TDynamicTree;
511  typedef typename TDynamicTree::TBioTreeNode TDynamicNodeType;
512  typedef typename TDynamicNodeType::TValueType TDynamicNodeValueType;
513 
514  TDynamicNodeValueType v;
515  v.SetId(uid);
516 
517  typedef typename TCNode::TFeatures TCNodeFeatureSet;
518 
519  if (cnode->CanGetFeatures()) {
520  const TCNodeFeatureSet& fset = cnode->GetFeatures();
521 
522  const typename TCNodeFeatureSet::Tdata& flist = fset.Get();
523 
524  ITERATE(typename TCNodeFeatureSet::Tdata, fit, flist) {
525  unsigned int fid = (*fit)->GetFeatureid();
526  const string& fvalue = (*fit)->GetValue();
527 
528  v.features.SetFeature(fid, fvalue);
529 
530  } // ITERATE
531 
532  }
533 
534  if (cnode->CanGetParent()) {
535  if (current_tree != NULL) {
536  TBioTreeNodeId parent_id = cnode->GetParent();
537  current_tree->AddNode(v, parent_id);
538  }
539  else {
540  // throw exception?
541  }
542  } else {
543  // This should be the root no de in a new tree:
544  current_tree = new typename TDynamicForest::TBioTree();
545  dyn_forest.AddTree(current_tree);
546 
547  TDynamicNodeType* dnode = new TDynamicNodeType(v);
548  current_tree->SetTreeNode(dnode);
549  }
550 
551 
552  } // ITERATE TNodeList
553 }
554 
555 // --------------------------------------------------------------------------
556 
557 /// Feature ids for Bio-Tree
558 ///
560 {
570  eTaxTree_Label = 10
571 };
572 
573 
574 /// Taxon1 tree visitor functor. Converts taxonomy into dynamic tree.
575 ///
576 /// @internal
577 ///
578 template<class TITaxon4Each, class TITaxon1Node,
579  class TITreeIterator, class TBioTreeContainer>
580 class CTaxon1NodeConvertVisitor : public TITaxon4Each
581 {
582 public:
583  typedef typename TITreeIterator::EAction EAction;
584 
585  typedef typename TBioTreeContainer::TNodes TCNodeSet;
586  typedef typename TCNodeSet::Tdata TNodeList;
587  typedef typename TNodeList::value_type::element_type TCNode;
588  typedef typename TCNode::TFeatures TCNodeFeatureSet;
589  typedef typename TCNodeFeatureSet::Tdata TNodeFeatureList;
590  typedef typename
591  TNodeFeatureList::value_type::element_type TCNodeFeature;
592 
593 public:
594  CTaxon1NodeConvertVisitor(TBioTreeContainer* tree_container)
595  : m_TreeContainer(tree_container),
596  m_MaxUID(0)
597  {
598  m_NodeList = &(tree_container->SetNodes().Set());
599  }
600 
602 
603  virtual
604  EAction Execute(const TITaxon1Node* pNode)
605  {
606 
607  TBioTreeNodeId uid = TAX_ID_TO(int, pNode->GetTaxId());
608 
609  CRef<TCNode> cnode(new TCNode);
610  cnode->SetId(uid);
611 
612  if (uid > (unsigned int)m_MaxUID) { // new tree node id max?
613  m_MaxUID = (int)uid;
614  }
615 
616  vector<TTaxId>::size_type psize = m_Parents.size();
617  if (psize != 0) {
618  TTaxId parent_tax_id = m_Parents[psize - 1];
619  cnode->SetParent(TAX_ID_TO(int, parent_tax_id));
620  }
621 
622 
623  TCNodeFeatureSet& fset = cnode->SetFeatures();
624 
625  // Convert features
626 
627  // Name
628  {{
630  cfeat->SetFeatureid(eTaxTree_Name);
631  cfeat->SetValue(pNode->GetName());
632 
633  fset.Set().push_back(cfeat);
634  }}
635  // Label
636  {{
638  cfeat->SetFeatureid(eTaxTree_Label);
639  cfeat->SetValue(pNode->GetName());
640 
641  fset.Set().push_back(cfeat);
642  }}
643 
644  // Blast_name
645  {{
647  cfeat->SetFeatureid(eTaxTree_BlastName);
648  cfeat->SetValue(pNode->GetBlastName());
649 
650  fset.Set().push_back(cfeat);
651  }}
652 
653  // Rank
654  {{
656  cfeat->SetFeatureid(eTaxTree_Rank);
657  int v = pNode->GetRank();
658  cfeat->SetValue(NStr::IntToString(v));
659 
660  fset.Set().push_back(cfeat);
661  }}
662 
663  // Division
664  {{
666  cfeat->SetFeatureid(eTaxTree_Division);
667  int v = pNode->GetDivision();
668  cfeat->SetValue(NStr::IntToString(v));
669 
670  fset.Set().push_back(cfeat);
671  }}
672 
673  // Genetic code
674  {{
676  cfeat->SetFeatureid(eTaxTree_GC);
677  int v = pNode->GetGC();
678  cfeat->SetValue(NStr::IntToString(v));
679 
680  fset.Set().push_back(cfeat);
681  }}
682 
683  // Mitocondrial genetic code
684  {{
686  cfeat->SetFeatureid(eTaxTree_MGC);
687  int v = pNode->GetMGC();
688  cfeat->SetValue(NStr::IntToString(v));
689 
690  fset.Set().push_back(cfeat);
691  }}
692 
693  // Uncultured
694  {{
696  cfeat->SetFeatureid(eTaxTree_IsUncultured);
697  int v = pNode->IsUncultured();
698  cfeat->SetValue(NStr::IntToString(v));
699 
700  fset.Set().push_back(cfeat);
701  }}
702  // tax-id
703  {{
705  cfeat->SetFeatureid(eTaxTree_TaxId);
706  cfeat->SetValue(NStr::IntToString(uid));
707 
708  fset.Set().push_back(cfeat);
709  }}
710 
711  m_NodeList->push_back(cnode);
712 
713  return TITreeIterator::eOk;
714  }
715 
716  virtual
717  EAction LevelBegin(const TITaxon1Node* pParent)
718  {
719  m_Parents.push_back(pParent->GetTaxId());
720  return TITreeIterator::eOk;
721  }
722 
723  virtual
724  EAction LevelEnd(const TITaxon1Node* /*pParent*/)
725  {
726  m_Parents.pop_back();
727  return TITreeIterator::eOk;
728  }
729 
730  int GetMaxNodeId() const
731  {
732  return m_MaxUID;
733  }
734 private:
735  TBioTreeContainer* m_TreeContainer;
737  vector<TTaxId> m_Parents; //<! Stack of parent tax ids
738  int m_MaxUID; //<! Maximum node ID
739 };
740 
741 
742 template<class TBioTreeContainer>
743 void BioTreeAddFeatureToDictionary(TBioTreeContainer& tree_container,
744  unsigned int feature_id,
745  const string& feature_name)
746 {
747  typedef typename TBioTreeContainer::TFdict TContainerDict;
748  typedef typename TContainerDict::Tdata::value_type::element_type TFeatureDescr;
749 
750  TContainerDict& fd = tree_container.SetFdict();
751  typename TContainerDict::Tdata& feat_list = fd.Set();
752 
753  // Don't add duplicate ids:
754  ITERATE(typename TContainerDict::Tdata, it, feat_list) {
755  if ( (unsigned int)((*it)->GetId()) == feature_id )
756  return;
757  }
758 
759  CRef<TFeatureDescr> d(new TFeatureDescr);
760  d->SetId(feature_id);
761  d->SetName(feature_name);
762 
763  feat_list.push_back(d);
764 }
765 
766 
767 
768 template<class TBioTreeContainer,
769  class TTaxon1, class TITaxon1Node, class TITreeIterator>
771 {
772 public:
773  typedef typename TITreeIterator::I4Each T4Each;
774  typedef CTaxon1NodeConvertVisitor<T4Each, TITaxon1Node,
775  TITreeIterator, TBioTreeContainer>
777 
778 public:
779 
781  : m_MaxNodeId(0)
782  {}
783 
784  void operator()(TBioTreeContainer& tree_container,
785  TTaxon1& tax,
786  TTaxId tax_id)
787  {
788  SetupFeatureDictionary(tree_container);
789 
790  // Convert nodes
791  const TITaxon1Node* tax_node=0;
792  bool res = tax.LoadSubtree(tax_id, &tax_node);
793  if (res) {
794  CRef<TITreeIterator> tax_tree_iter(tax.GetTreeIterator());
795  tax_tree_iter->GoNode(tax_node);
796  TTaxon1Visitor tax_vis(&tree_container);
797  tax_tree_iter->TraverseDownward(tax_vis);
798  m_MaxNodeId = tax_vis.GetMaxNodeId();
799  }
800  }
801 
802  void operator()(TBioTreeContainer& tree_container,
803  CRef<TITreeIterator> tax_tree_iter)
804  {
805  SetupFeatureDictionary(tree_container);
806 
807  // Convert nodes
808  tax_tree_iter->GoRoot();
809  TTaxon1Visitor tax_vis(&tree_container);
810  tax_tree_iter->TraverseDownward(tax_vis);
811  m_MaxNodeId = tax_vis.GetMaxNodeId();
812  }
813 
814  /// Get max node id (available after conversion)
815  int GetMaxNodeId() const
816  {
817  return m_MaxNodeId;
818  }
819 protected:
820  /// Add elements to the feature dictionary
821  ///
822  void SetupFeatureDictionary(TBioTreeContainer& tree_container)
823  {
824  BioTreeAddFeatureToDictionary(tree_container, eTaxTree_Name, "name");
825  BioTreeAddFeatureToDictionary(tree_container, eTaxTree_BlastName, "blast_name");
826  BioTreeAddFeatureToDictionary(tree_container, eTaxTree_Rank, "rank");
827  BioTreeAddFeatureToDictionary(tree_container, eTaxTree_Division, "division");
828  BioTreeAddFeatureToDictionary(tree_container, eTaxTree_GC, "GC");
829  BioTreeAddFeatureToDictionary(tree_container, eTaxTree_MGC, "MGC");
830  BioTreeAddFeatureToDictionary(tree_container, eTaxTree_IsUncultured, "IsUncultured");
831  BioTreeAddFeatureToDictionary(tree_container, eTaxTree_TaxId, "tax-id");
832  BioTreeAddFeatureToDictionary(tree_container, eTaxTree_SeqId, "seq-id");
833  BioTreeAddFeatureToDictionary(tree_container, eTaxTree_Label, "label");
834  }
835 private:
837 };
838 
839 /// Function to determine tree if a given biotree container
840 /// is a single tree or a forest.
841 ///
842 /// @internal
843 template<class TBioTreeContainer>
844 bool BioTreeContainerIsForest(const TBioTreeContainer& tree_container)
845 {
846  // Definition of a tree : exactly one node has no parent node (is a root).
847  // Definition of a forest: more than one node has no parent node (multiple roots).
848 
849  typedef typename TBioTreeContainer::TNodes TCNodeSet;
850  typedef typename TCNodeSet::Tdata TNodeList;
851  typedef typename TNodeList::value_type::element_type TCNode;
852 
853  const TNodeList node_list = tree_container.GetNodes().Get();
854 
855  int number_roots = 0;
856 
857  ITERATE(typename TNodeList, it, node_list) {
858 
859  const CRef<TCNode>& cnode = *it;
860 
861  if (!cnode->CanGetParent())
862  ++number_roots;
863 
864  } // ITERATE TNodeList
865 
866  return (number_roots > 1);
867 }
868 /* @} */
869 
870 
871 
873 
874 #endif
Things for representing and manipulating bio trees.
Visitor functor to convert dynamic tree nodes to ASN.1 BioTree container.
Feature dictionary.
Definition: bio_tree.hpp:176
CRef –.
Definition: ncbiobj.hpp:618
Taxon1 tree visitor functor.
Visitor functor to convert one tree to another.
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
Definition: ncbimisc.hpp:815
#define TAX_ID_TO(T, tax_id)
Definition: ncbimisc.hpp:1110
SStrictId_Tax::TId TTaxId
Taxon id type.
Definition: ncbimisc.hpp:1048
#define NULL
Definition: ncbistd.hpp:225
#define NCBI_THROW(exception_class, err_code, message)
Generic macro to throw an exception, given the exception class, error code and message string.
Definition: ncbiexpt.hpp:704
@ eUnknown
Definition: app_popup.hpp:72
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
static string IntToString(int value, TNumToStringFlags flags=0, int base=10)
Convert int to string.
Definition: ncbistr.hpp:5078
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.
Definition: ncbi_tree.hpp:504
ETreeTraverseCode
Tree traverse code returned by the traverse predicate function.
Definition: ncbi_tree.hpp:51
unsigned int TBioTreeNodeId
Tree node id. Every node has its unique id in the tree.
Definition: bio_tree.hpp:63
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.
Definition: bio_tree.hpp:60
TDstTreeNodeType * MakeNewTreeNode(const TSrcTreeNodeType &src_node)
TNodeFeatureList::value_type::element_type TCNodeFeature
TBioTreeContainer * m_TreeContainer
CTree2TreeFunc(TNodeConvFunc &func)
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.
Definition: bio_tree.cpp:160
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()
TBioTreeContainer::TNodes TCNodeSet
const TFeatureDict & GetFeatureDict() const
Get reference on the internal map.
Definition: bio_tree.hpp:219
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.
Definition: ncbi_tree.hpp:52
@ eTaxTree_Rank
@ eTaxTree_GC
@ eTaxTree_TaxId
@ eTaxTree_IsUncultured
@ eTaxTree_Name
@ eTaxTree_SeqId
@ eTaxTree_Label
@ eTaxTree_BlastName
@ eTaxTree_MGC
@ eTaxTree_Division
unsigned int
A callback function used to compare two keys in a database.
Definition: types.hpp:1210
int i
yy_size_t n
const struct ncbi::grid::netcache::search::fields::SIZE size
Modified on Fri Sep 20 14:57:43 2024 by modify_doxy.py rev. 669887