tree_loader.cpp
上传用户:yhdzpy8989
上传日期:2007-06-13
资源大小:13604k
文件大小:7k
- /*
- * ===========================================================================
- * PRODUCTION $Log: tree_loader.cpp,v $
- * PRODUCTION Revision 1000.2 2004/06/02 20:24:21 gouriano
- * PRODUCTION PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.5
- * PRODUCTION
- * ===========================================================================
- */
- /* $Id: tree_loader.cpp,v 1000.2 2004/06/02 20:24:21 gouriano Exp $
- * ===========================================================================
- *
- * PUBLIC DOMAIN NOTICE
- * National Center for Biotechnology Information
- *
- * This software/database is a "United States Government Work" under the
- * terms of the United States Copyright Act. It was written as part of
- * the author's official duties as a United States Government employee and
- * thus cannot be copyrighted. This software/database is freely available
- * to the public for use. The National Library of Medicine and the U.S.
- * Government have not placed any restriction on its use or reproduction.
- *
- * Although all reasonable efforts have been taken to ensure the accuracy
- * and reliability of the software and data, the NLM and the U.S.
- * Government do not and cannot warrant the performance or results that
- * may be obtained by using this software or data. The NLM and the U.S.
- * Government disclaim all warranties, express or implied, including
- * warranties of performance, merchantability or fitness for any particular
- * purpose.
- *
- * Please cite the author in any work or product based on this material.
- *
- * ===========================================================================
- *
- * Authors: Josh Cherry
- *
- * File Description:
- * CDataPlugin_TreeLoader - load a phylogenetic tree from a file
- */
- #include <ncbi_pch.hpp>
- #include "tree_loader.hpp"
- #include <gui/core/doc_exception.hpp>
- #include <gui/core/doc_manager.hpp>
- #include <gui/core/idocument.hpp>
- #include <gui/core/plugin_utils.hpp>
- #include <gui/core/version.hpp>
- #include <gui/plugin/PluginCommandSet.hpp>
- #include <gui/plugin/PluginInfo.hpp>
- #include <gui/plugin/PluginValueConstraint.hpp>
- #include <gui/utils/message_box.hpp>
- #include <gui/objutils/utils.hpp>
- #include <algo/phy_tree/phy_tree_serial.hpp>
- #include <objects/biotree/BioTreeContainer.hpp>
- #include <objects/biotree/FeatureDescr.hpp>
- #include <objects/biotree/Node.hpp>
- #include <objects/biotree/NodeSet.hpp>
- #include <objects/biotree/NodeFeature.hpp>
- #include <objects/biotree/NodeFeatureSet.hpp>
- #include <objects/biotree/FeatureDictSet.hpp>
- BEGIN_NCBI_SCOPE
- USING_SCOPE(objects);
- //
- // factory implementations
- //
- void CDataPlugin_TreeLoader::GetInfo(CPluginInfo& info)
- {
- info.Reset();
- // version info macro
- info.SetInfo(CPluginVersion::eMajor, CPluginVersion::eMinor, 0,
- string(__DATE__) + " " + string(__TIME__),
- "CDataPlugin_TreeLoader",
- "Phylogenetic Tree File (Newick)",
- "Read a phylogenetic tree in Newick format",
- "");
- // command info
- CPluginCommandSet& cmds = info.SetCommands();
- CPluginCommand& load_args = cmds.AddDataCommand(eDataCommand_load);
- load_args.AddArgument("file", "File name", CPluginArg::eFile);
- load_args.AddArgument("make_btc",
- "Create BioTreeContainer object "
- "(experimental)",
- CPluginArg::eBoolean);
- }
- //
- // Default ctor
- //
- CDataPlugin_TreeLoader::CDataPlugin_TreeLoader()
- {
- }
- //
- // One and only dtor
- //
- CDataPlugin_TreeLoader::~CDataPlugin_TreeLoader()
- {
- }
- /// Recursive function for adding TPhyTreeNodes to BioTreeContainer
- static void s_AddNodeToBtc(CRef<CBioTreeContainer> btc, TPhyTreeNode* ptn,
- int parent_uid, int& next_uid)
- {
- const int label_fid = 0;
- const int dist_fid = 1;
- CRef<CNode> node;
- CRef<CNodeFeature> node_feature;
- int my_uid = next_uid++;
- // first do this node
- node = new CNode;
- node->SetId(my_uid);
- node->SetParent(parent_uid);
- if (ptn->GetValue().GetLabel() != "") {
- node_feature = new CNodeFeature;
- node_feature->SetFeatureid(label_fid);
- node_feature->SetValue(ptn->GetValue().GetLabel());
- node->SetFeatures().Set().push_back(node_feature);
- }
- if (ptn->GetValue().IsSetDist()) {
- node_feature = new CNodeFeature;
- node_feature->SetFeatureid(dist_fid);
- node_feature->SetValue
- (NStr::DoubleToString(ptn->GetValue().GetDist()));
- node->SetFeatures().Set().push_back(node_feature);
- }
- btc->SetNodes().Set().push_back(node);
- // now do its children
- for (TPhyTreeNode::TNodeList_CI it = ptn->SubNodeBegin();
- it != ptn->SubNodeEnd(); ++it) {
- s_AddNodeToBtc(btc, *it, my_uid, next_uid);
- }
- }
- /// Conversion from TPhyTreeNode to CBioTreeContainer
- static CRef<CBioTreeContainer> s_MakeBtc(TPhyTreeNode *tree)
- {
- const int label_fid = 0;
- const int dist_fid = 1;
- CRef<CBioTreeContainer> btc(new CBioTreeContainer);
- CRef<CFeatureDescr> fdescr;
- fdescr = new CFeatureDescr();
- fdescr->SetId(label_fid);
- fdescr->SetName("label");
- btc->SetFdict().Set().push_back(fdescr);
-
- fdescr = new CFeatureDescr();
- fdescr->SetId(dist_fid);
- fdescr->SetName("dist");
- btc->SetFdict().Set().push_back(fdescr);
- int next_uid = 0;
- s_AddNodeToBtc(btc, tree, -1, next_uid);
- // unset parent id of root node
- btc->SetNodes().Set().front()->ResetParent();
- return btc;
- }
- //
- // Load()
- // This is a pure virtual requirement and is the main plugin hook.
- //
- void CDataPlugin_TreeLoader::Load(CPluginMessage& msg)
- {
- const CPluginCommand& args = msg.GetRequest().GetCommand();
- CPluginReply& reply = msg.SetReply();
- string fname = args["file"].AsString();
- try {
- // load the tree
- CNcbiIfstream istr(fname.c_str());
- TPhyTreeNode *tree = ReadNewickTree(istr);
- // make a document containing this
- CRef<CScope> scope(new CScope(CDocManager::GetObjectManager()));
- scope->AddDefaults();
- IDocument* new_doc;
- if (args["make_btc"].AsBoolean()) {
- CRef<CBioTreeContainer> btc = s_MakeBtc(tree);
- new_doc = CDocManager::CreateDocument(*scope, *btc);
- } else {
- CRef<CPhyTreeSerial> stree(new CPhyTreeSerial(*tree));
- new_doc = CDocManager::CreateDocument(*scope, *stree);
- }
- delete tree;
- reply.AddObject(*new_doc);
- reply.SetStatus(eMessageStatus_success);
- }
- catch (exception& e) {
- _TRACE("failed to read tree file: " << e.what());
- }
- catch (...) {
- _TRACE("failed to read tree file: unknown error");
- }
- }
- END_NCBI_SCOPE
- /*
- * ===========================================================================
- * $Log: tree_loader.cpp,v $
- * Revision 1000.2 2004/06/02 20:24:21 gouriano
- * PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.5
- *
- * Revision 1.5 2004/06/02 19:47:04 jcherry
- * Added option for creating CBioTreeContainer rather than
- * CPhyTreeSerial
- *
- * Revision 1.4 2004/05/25 17:21:59 dicuccio
- * Modified class names. Fonts to 12 point
- *
- * Revision 1.3 2004/05/21 22:27:48 gorelenk
- * Added PCH ncbi_pch.hpp
- *
- * Revision 1.2 2004/05/03 13:05:43 dicuccio
- * gui/utils --> gui/objutils where needed
- *
- * Revision 1.1 2004/02/17 05:39:02 jcherry
- * Initial version
- *
- * ===========================================================================
- */