bio_tree_conv.hpp
上传用户:yhdzpy8989
上传日期:2007-06-13
资源大小:13604k
文件大小:11k
源码类别:

生物技术

开发平台:

C/C++

  1. /*
  2.  * ===========================================================================
  3.  * PRODUCTION $Log: bio_tree_conv.hpp,v $
  4.  * PRODUCTION Revision 1000.0  2004/06/01 18:12:08  gouriano
  5.  * PRODUCTION PRODUCTION: IMPORTED [GCC34_MSVC7] Dev-tree R1.2
  6.  * PRODUCTION
  7.  * ===========================================================================
  8.  */
  9. #ifndef ALGO_PHY_TREE___BIO_TREE_CONV__HPP
  10. #define ALGO_PHY_TREE___BIO_TREE_CONV__HPP
  11. /*  $Id: bio_tree_conv.hpp,v 1000.0 2004/06/01 18:12:08 gouriano Exp $
  12.  * ===========================================================================
  13.  *
  14.  *                            PUBLIC DOMAIN NOTICE
  15.  *               National Center for Biotechnology Information
  16.  *
  17.  *  This software/database is a "United States Government Work" under the
  18.  *  terms of the United States Copyright Act.  It was written as part of
  19.  *  the author's official duties as a United States Government employee and
  20.  *  thus cannot be copyrighted.  This software/database is freely available
  21.  *  to the public for use. The National Library of Medicine and the U.S.
  22.  *  Government have not placed any restriction on its use or reproduction.
  23.  *
  24.  *  Although all reasonable efforts have been taken to ensure the accuracy
  25.  *  and reliability of the software and data, the NLM and the U.S.
  26.  *  Government do not and cannot warrant the performance or results that
  27.  *  may be obtained by using this software or data. The NLM and the U.S.
  28.  *  Government disclaim all warranties, express or implied, including
  29.  *  warranties of performance, merchantability or fitness for any particular
  30.  *  purpose.
  31.  *
  32.  *  Please cite the author in any work or product based on this material.
  33.  *
  34.  * ===========================================================================
  35.  *
  36.  * Authors:  Anatoliy Kuznetsov
  37.  *
  38.  * File Description:  bio trees convertions
  39.  *
  40.  */
  41. /// @file bio_tree.hpp
  42. /// Things for bio tree convertions
  43. BEGIN_NCBI_SCOPE
  44. /// Functor to convert bio tree nodes to dynamic tree
  45. ///
  46. /// @internal
  47. template<class TDynamicTree, class TSrcBioTree, class TNodeConvFunc>
  48. class CBioTreeConvert2DynamicFunc
  49. {
  50. public:
  51.     typedef typename TSrcBioTree::TBioTreeNode   TBioTreeNodeType;
  52.     typedef typename TDynamicTree::TBioTreeNode  TDynamicNodeType;
  53. public:
  54.     CBioTreeConvert2DynamicFunc(TDynamicTree* dyn_tree, TNodeConvFunc func)
  55.     : m_DynTree(dyn_tree),
  56.       m_ConvFunc(func)
  57.     {}
  58.     ETreeTraverseCode 
  59.     operator()(const TBioTreeNodeType& node, 
  60.                int                     delta_level)
  61.     {
  62.         if (m_TreeStack.size() == 0) {
  63.             auto_ptr<TDynamicNodeType> pnode(MakeDynamicNode(node));
  64.             m_TreeStack.push_back(pnode.get());
  65.             m_DynTree->SetTreeNode(pnode.release());
  66.             return eTreeTraverse;
  67.         }
  68.         if (delta_level == 0) {
  69.             if (m_TreeStack.size() > 0) {
  70.                 m_TreeStack.pop_back();
  71.             }
  72.             TDynamicNodeType* parent_node = m_TreeStack.back();
  73.             TDynamicNodeType* pnode= MakeDynamicNode(node);
  74.             parent_node->AddNode(pnode);
  75.             m_TreeStack.push_back(pnode);
  76.             return eTreeTraverse;
  77.         }
  78.         if (delta_level == 1) {
  79.             TDynamicNodeType* parent_node = m_TreeStack.back();
  80.             TDynamicNodeType* pnode= MakeDynamicNode(node);
  81.             parent_node->AddNode(pnode);
  82.             m_TreeStack.push_back(pnode);
  83.             return eTreeTraverse;                        
  84.         }
  85.         if (delta_level == -1) {
  86.             m_TreeStack.pop_back();
  87.         }
  88.         return eTreeTraverse;
  89.     }
  90. protected:
  91.     TDynamicNodeType* MakeDynamicNode(const TBioTreeNodeType& src_node)
  92.     {
  93.         auto_ptr<TDynamicNodeType> pnode(new TDynamicNodeType());
  94.         TBioTreeNodeId uid = src_node.GetValue().GetId();
  95.         pnode->GetValue().SetId(uid);
  96.         m_ConvFunc(*pnode.get(), src_node);
  97.         return pnode.release();        
  98.     }
  99. private:
  100.     TDynamicTree*                m_DynTree;
  101.     TNodeConvFunc                m_ConvFunc;
  102.     vector<TDynamicNodeType*>    m_TreeStack;
  103. };
  104. /// Convert any tree to dynamic tree using a node converter
  105. ///
  106. template<class TDynamicTree, class TBioTree, class TNodeConvFunc>
  107. void BioTreeConvert2Dynamic(TDynamicTree&      dyn_tree, 
  108.                             const TBioTree&    bio_tree,
  109.                             TNodeConvFunc      node_conv)
  110. {
  111.     dyn_tree.Clear();
  112.     CBioTreeConvert2DynamicFunc<TDynamicTree, TBioTree, TNodeConvFunc> 
  113.        func(&dyn_tree, node_conv);
  114.     typedef typename TBioTree::TBioTreeNode TTreeNode;
  115.     const TTreeNode *n = bio_tree.GetTreeNode();
  116.     TreeDepthFirstTraverse(*(const_cast<TTreeNode*>(n)), func);
  117. }
  118. /// Functor to convert dynamic tree nodes to ASN.1 BioTree container
  119. ///
  120. /// @internal
  121. template<class TBioTreeContainer, class TDynamicTree>
  122. class CBioTreeConvert2ContainerFunc
  123. {
  124. protected:
  125.     typedef typename TDynamicTree::TBioTreeNode         TDynamicNodeType;
  126.     typedef typename TDynamicNodeType::TValueType       TDynamicNodeValueType;
  127.     typedef typename TBioTreeContainer::TNodes           TCNodeSet;
  128.     typedef typename TCNodeSet::Tdata                    TNodeList;
  129.     typedef typename TNodeList::value_type::element_type TCNode;
  130.     typedef typename TCNode::TFeatures                   TCNodeFeatureSet;
  131.     typedef typename TCNodeFeatureSet::Tdata             TNodeFeatureList;
  132.     typedef typename 
  133.        TNodeFeatureList::value_type::element_type        TCNodeFeature;
  134. public:
  135.     CBioTreeConvert2ContainerFunc(TBioTreeContainer* tree_container)
  136.     : m_Container(tree_container)
  137.     {
  138.         m_NodeList = &(tree_container->SetNodes().Set());
  139.     }
  140.     ETreeTraverseCode 
  141.     operator()(const TDynamicNodeType& node, 
  142.                int                     delta_level)
  143.     {
  144.         if (delta_level < 0) {
  145.             return eTreeTraverse;
  146.         }
  147.         const TDynamicNodeValueType& v = node.GetValue();
  148.         TBioTreeNodeId uid = v.GetId();
  149.         CRef<TCNode> cnode(new TCNode);
  150.         cnode->SetId(uid);
  151.         const TDynamicNodeType* node_parent = node.GetParent();
  152.         if (node_parent) {
  153.             cnode->SetParent(node_parent->GetValue().GetId());
  154.         }
  155.         
  156.         typedef typename 
  157.            TDynamicNodeValueType::TNodeFeaturesType::TFeatureList TFList;
  158.         const TFList& flist = v.features.GetFeatureList();
  159.         if (!flist.empty()) {
  160.             
  161.             TCNodeFeatureSet& fset = cnode->SetFeatures();
  162.             ITERATE(typename TFList, it, flist) {
  163.                 TBioTreeFeatureId fid = it->id;
  164.                 const string fvalue = it->value;
  165.                 CRef<TCNodeFeature>  cfeat(new TCNodeFeature);
  166.                 cfeat->SetFeatureid(fid);
  167.                 cfeat->SetValue(fvalue);
  168.                 
  169.                 fset.Set().push_back(cfeat);
  170.             
  171.             } // ITERATE
  172.         }
  173.         m_NodeList->push_back(cnode);
  174.         return eTreeTraverse;
  175.     }
  176. private:
  177.     TBioTreeContainer*   m_Container;
  178.     TNodeList*           m_NodeList;
  179. };
  180. /// Convert Dynamic tree to ASN.1 BioTree container
  181. ///
  182. template<class TBioTreeContainer, class TDynamicTree>
  183. void BioTreeConvert2Container(TBioTreeContainer&      tree_container,
  184.                               const TDynamicTree&     dyn_tree)
  185. {
  186.     // Convert feature dictionary
  187.     typedef typename TBioTreeContainer::TFdict  TContainerDict;
  188.     const CBioTreeFeatureDictionary& dict = dyn_tree.GetFeatureDict();
  189.     const CBioTreeFeatureDictionary::TFeatureDict& dict_map = 
  190.                                                 dict.GetFeatureDict();
  191.     TContainerDict& fd = tree_container.SetFdict();
  192.     typename TContainerDict::Tdata& feat_list = fd.Set();
  193.     typedef 
  194.     typename TContainerDict::Tdata::value_type::element_type TCFeatureDescr;
  195.     
  196.     ITERATE(CBioTreeFeatureDictionary::TFeatureDict, it, dict_map) {
  197.         TBioTreeFeatureId fid = it->first;
  198.         const string& fvalue = it->second;
  199.         {{
  200.         CRef<TCFeatureDescr> d(new TCFeatureDescr);
  201.         d->SetId(fid);
  202.         d->SetName(fvalue);
  203.         feat_list.push_back(d);
  204.         }}
  205.     } // ITERATE
  206.     // convert tree data (nodes)
  207.     typedef typename TBioTreeContainer::TNodes  TDynNodes;
  208.     TDynNodes& nodes = tree_container.SetNodes();
  209.     typedef typename TDynamicTree::TBioTreeNode TTreeNode;
  210.     const TTreeNode *n = dyn_tree.GetTreeNode();
  211.     CBioTreeConvert2ContainerFunc<TBioTreeContainer, TDynamicTree>
  212.         func(&tree_container);
  213.     TreeDepthFirstTraverse(*(const_cast<TTreeNode*>(n)), func);
  214.     
  215. }
  216. /// Convert ASN.1 BioTree container to dynamic tree
  217. ///
  218. template<class TBioTreeContainer, class TDynamicTree>
  219. void BioTreeConvertContainer2Dynamic(TDynamicTree&             dyn_tree,
  220.                              const TBioTreeContainer&  tree_container)
  221. {
  222. dyn_tree.Clear();
  223.     // Convert feature dictionary
  224.     typedef typename TBioTreeContainer::TFdict  TContainerDict;
  225.     CBioTreeFeatureDictionary& dict = dyn_tree.GetFeatureDict();
  226.     const TContainerDict& fd = tree_container.GetFdict();
  227.     typename const TContainerDict::Tdata& feat_list = fd.Get();
  228. ITERATE(typename TContainerDict::Tdata, it, feat_list) {
  229.         TBioTreeFeatureId fid = (*it)->GetId();
  230.         const string& fvalue = (*it)->GetName();
  231. dict.Register(fid, fvalue);
  232.     }
  233. // convert tree data (nodes)
  234.     typedef typename TBioTreeContainer::TNodes           TCNodeSet;
  235.     typedef typename TCNodeSet::Tdata                    TNodeList;
  236.     const TNodeList node_list = tree_container.GetNodes().Get();
  237. ITERATE(TNodeList, it, node_list) {
  238. const CRef<CNode>& cnode = *it;
  239.         TBioTreeNodeId uid = cnode->GetId();
  240.     typedef typename TDynamicTree::TBioTreeNode         TDynamicNodeType;
  241. typedef typename TDynamicNodeType::TValueType       TDynamicNodeValueType;
  242. TDynamicNodeValueType v;
  243. v.SetId(uid);
  244. if (cnode->CanGetFeatures()) {
  245. const typename CNodeFeatureSet& fset = cnode->GetFeatures();
  246. const typename CNodeFeatureSet::Tdata& flist = fset.Get();
  247. ITERATE(typename CNodeFeatureSet::Tdata, fit, flist) {
  248. unsigned int fid = (*fit)->GetFeatureid();
  249. const string& fvalue = (*fit)->GetValue();
  250. v.features.SetFeature(fid, fvalue);
  251. } // ITERATE 
  252. }
  253. if (cnode->CanGetParent()) {
  254.         TBioTreeNodeId parent_id = cnode->GetParent();
  255. /*TDynamicNodeType* dnode = */dyn_tree.AddNode(v, parent_id);
  256. } else {
  257. TDynamicNodeType* dnode = new TDynamicNodeType(v);
  258. dyn_tree.SetTreeNode(dnode);
  259. }
  260. } // ITERATE TNodeList
  261. }
  262. END_NCBI_SCOPE 
  263. /*
  264.  * ===========================================================================
  265.  * $Log: bio_tree_conv.hpp,v $
  266.  * Revision 1000.0  2004/06/01 18:12:08  gouriano
  267.  * PRODUCTION: IMPORTED [GCC34_MSVC7] Dev-tree R1.2
  268.  *
  269.  * Revision 1.2  2004/06/01 15:20:48  kuznets
  270.  * + coversion function ASN.1 -> dynamic tree
  271.  *
  272.  * Revision 1.1  2004/05/26 15:15:19  kuznets
  273.  * Initial revision. Tree conversion algorithms moved from bio_tree.hpp
  274.  *
  275.  *
  276.  * ===========================================================================
  277.  */
  278. #endif