lds_loader.cpp
上传用户:yhdzpy8989
上传日期:2007-06-13
资源大小:13604k
文件大小:17k
- /*
- * ===========================================================================
- * PRODUCTION $Log: lds_loader.cpp,v $
- * PRODUCTION Revision 1000.5 2004/06/01 20:58:08 gouriano
- * PRODUCTION PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.31
- * PRODUCTION
- * ===========================================================================
- */
- /* $Id: lds_loader.cpp,v 1000.5 2004/06/01 20:58:08 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: Anatoliy Kuznetsov
- *
- * File Description:
- * CDataPlugin_LDSLoader - load sequence information from the local data storage(LDS).
- */
- #include <ncbi_pch.hpp>
- #include <corelib/ncbiapp.hpp>
- #include <corelib/ncbireg.hpp>
- #include <corelib/ncbitime.hpp>
- #include <gui/plugin/PluginInfo.hpp>
- #include <gui/core/version.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/data_store.hpp>
- #include <gui/objutils/utils.hpp>
- #include <gui/plugin/PluginValue.hpp>
- #include <gui/plugin/PluginCommandSet.hpp>
- #include <gui/utils/message_box.hpp>
- #include <objmgr/scope.hpp>
- #include "lds_loader.hpp"
- #include "loader_utils.hpp"
- #include <objtools/lds/lds.hpp>
- #include <objtools/lds/lds_util.hpp>
- #include <objtools/lds/lds_reader.hpp>
- #include <objtools/lds/admin/lds_admin.hpp>
- #include "lds_search_dlg.hpp"
- BEGIN_NCBI_SCOPE
- USING_SCOPE(objects);
- //////////////////////////////////////////////////////////////////
- //
- // class CDataPlugin_LDSLoaderException defines some internal exception types used in
- // processing files.
- //
- // This class is used internally to avoid an ad-hoc exception mechanism;
- //
- class CDataPlugin_LDSLoaderException : EXCEPTION_VIRTUAL_BASE public CException
- {
- public:
- // Enumerated list of exception types
- enum EErrCode {
- eInvalidId
- };
- // Convert an enumerated exception to a human-readable string representation
- // of this exception.
- virtual const char* GetErrCodeString(void) const
- {
- switch (GetErrCode()) {
- case eInvalidId: return "eInvalidId";
- default: return CException::GetErrCodeString();
- }
- }
- // constructor boilerplate
- NCBI_EXCEPTION_DEFAULT(CDataPlugin_LDSLoaderException, CException);
- };
- //////////////////////////////////////////////////////////////////
- void CDataPlugin_LDSLoader::GetInfo(CPluginInfo& info)
- {
- info.Reset();
- // version info macro
- info.SetInfo(CPluginVersion::eMajor, CPluginVersion::eMinor, 0,
- string(__DATE__) + " " + string(__TIME__),
- "CDataPlugin_LDSLoader", "Local sequence",
- "Open a molecule by sequence Id", "");
- // command info
- CPluginCommandSet& cmds = info.SetCommands();
-
- CPluginCommand& load_args = cmds.AddDataCommand(eDataCommand_load);
- load_args.AddArgument("acc", "Local sequence to load",
- CPluginArg::eString,
- CPluginArg::TData::e_Array);
- load_args.AddArgument("search_all", "Search all references",
- CPluginArg::eBoolean,
- CPluginArg::TData::e_Single);
- CPluginCommand& search_args = cmds.AddDataCommand(eDataCommand_search);
- {{
- CPluginArg& arg =
- search_args.AddArgument("query", "Search string",
- CPluginArg::eString);
- arg.SetHidden(true);
- }}
- }
- CDataPlugin_LDSLoader::CDataPlugin_LDSLoader()
- : m_DbHolder(0)
- {
- if (CDataStore::HasObject("LDS_Database")) {
- CObject& obj = CDataStore::GetObject("LDS_Database");
- m_DbHolder = dynamic_cast<CLDS_DatabaseHolder*>(&obj);
- // if (dbh)
- // m_db = dbh->GetDefaultDatabase();
- }
- }
- CDataPlugin_LDSLoader::~CDataPlugin_LDSLoader()
- {
- }
- //
- // load a record from Genbank into a fully-prepared document
- //
- void CDataPlugin_LDSLoader::Load(CPluginMessage& msg)
- {
- const CPluginCommand& args = msg.GetRequest().GetCommand();
- CPluginReply& reply = msg.SetReply();
- reply.SetStatus(eMessageStatus_failed);
- bool request_success = false;
- if (!m_DbHolder) {
- reply.SetStatus(eMessageStatus_ignored);
- LOG_POST(Error << "Local data storage is not open.");
- return;
- }
- vector<string> str_vec = CPluginUtils::ArgToStringVec(args["acc"]);
- CLoaderStrFormatter::PrepareSearchVector(str_vec);
- if (str_vec.empty()) {
- reply.SetStatus(eMessageStatus_ignored);
- return;
- }
- bool search_all = args["search_all"].AsBoolean();
- string alias = args["alias"].AsString();
- LOG_POST(Info << "loading from " << alias);
- string lds_alias = args["alias"].AsString();
- CLDS_Database* db = m_DbHolder->GetDatabase(lds_alias);
- if (!db) {
- reply.SetStatus(eMessageStatus_ignored);
- LOG_POST(Error << "LDS alias " << lds_alias << " not found.");
- return;
- }
- CStopWatch stw;
- stw.Start();
- SLDS_TablesCollection& tables = db->GetTables();
- CLDS_Set obj_search_results;
- CLDS_Query query(tables);
- query.FindSequences(str_vec, &obj_search_results);
- double elapsed = stw.Elapsed();
- LOG_POST(Info << "LDS sequence search time = " << elapsed);
- unsigned int obj_found = 0; // Number of objects found
- CLDS_Set obj_read; // list of objects already loaded
- obj_found += obj_search_results.size();
- if (obj_search_results.size() == 0) {
- LOG_POST(Info
- << "Failed to find local sequence(s).");
- }
- else {
- ITERATE (CLDS_Set, rsit, obj_search_results) {
- try {
- int object_id = *rsit;
- if (!x_LoadLDS_BioTSE(object_id, db, &obj_read, reply)) {
- continue;
- }
- request_success = true;
- }
- catch (CLDS_Exception& _DEBUG_ARG(ex))
- {
- _TRACE("failed to read LDS file: " << ex.what());
- }
- } // ITERATE
- }
- if (search_all) {
- CLDS_Set slist_search_results;
- query.FindSeqIdList(str_vec, &slist_search_results);
- obj_found += slist_search_results.size();
- ITERATE (CLDS_Set, it, slist_search_results) {
- try {
- int id = *it;
- CLDS_Query::SObjectDescr descr =
- query.GetObjectDescr(db->GetObjTypeMap(), id, true);
- if (LDS_SetTest(obj_read, descr.id)) {
- continue;
- }
- if (x_Load(descr, db, &obj_read, reply)) {
- request_success = true;
- }
-
- }
- catch (CLDS_Exception& _DEBUG_ARG(ex))
- {
- _TRACE("failed to read object: " << ex.what());
- }
- } // ITERATE
- }
- if (obj_found == 0) {
- NcbiMessageBox("Failed to find local sequence(s).",
- eDialog_Ok,
- eIcon_Stop,
- "Error");
- }
- // set our status code
- // we don't set a default action - the documents are merely posted for
- // notification in case a user has called this plugin and expects
- // to receive the results in a return value
- if (request_success) {
- reply.SetStatus(eMessageStatus_success);
- }
- }
- void CDataPlugin_LDSLoader::Search(CPluginMessage& msg)
- {
- const CPluginCommand& args = msg.GetRequest().GetCommand();
- CPluginReply& reply = msg.SetReply();
- if (!m_DbHolder) {
- NcbiMessageBox("Local data storage is not configured.",
- eDialog_Ok,
- eIcon_Stop,
- "Error");
- reply.SetStatus(eMessageStatus_ignored);
- return;
- }
- CLDS_Database* db = m_DbHolder->GetDefaultDatabase();
- if (!db) {
- NcbiMessageBox("Local data storage is not configured.",
- eDialog_Ok,
- eIcon_Stop,
- "Error");
- reply.SetStatus(eMessageStatus_ignored);
- return;
- }
-
- CLdsSearchDlg dlg(m_DbHolder);
- if (dlg.Show() == eOK) {
- Fl_Browser* lst = dlg.m_List;
- SLDS_TablesCollection& tables = db->GetTables();
- CLDS_Query query(tables);
- CLDS_Set obj_read; // list of objects already loaded
- for (int i = 1; i <= lst->size(); ++i) {
- int selected = lst->selected(i);
- if (selected) {
- void* data = lst->data(i);
- int id;
- ::memcpy(&id, &data, sizeof(id));
- if (!id) {
- continue;
- }
-
- CLDS_Query::SObjectDescr descr =
- query.GetObjectDescr(db->GetObjTypeMap(), id, true);
- // check if object is already loaded
- if (LDS_SetTest(obj_read, descr.id)) {
- continue;
- }
- x_Load(descr, db, &obj_read, reply);
- }
- } // for
- if (obj_read.size() == 0) {
- NcbiMessageBox("Failed to find local sequence(s).",
- eDialog_Ok,
- eIcon_Stop,
- "Error");
- }
- }
- reply.SetStatus(eMessageStatus_success);
- }
- bool CDataPlugin_LDSLoader::x_Load(const CLDS_Query::SObjectDescr& obj_descr,
- CLDS_Database* db,
- CLDS_Set* obj_read,
- CPluginReply& reply)
- {
- try {
- if (obj_descr.type_str == "Seq-entry" ||
- obj_descr.type_str == "Bioseq" ||
- obj_descr.format == CFormatGuess::eFasta) {
- return x_LoadLDS_BioTSE(obj_descr.id, db, obj_read, reply);
- }
- else {
- if (obj_descr.type_str == "Seq-annot" ||
- obj_descr.type_str == "Seq-align") {
- return x_LoadLDS_Annot(obj_descr, db, obj_read, reply);
- }
- }
-
- }
- catch (CLDS_Exception& ex)
- {
- ERR_POST("failed to read object: " << ex.what());
- }
- return false;
- }
- bool CDataPlugin_LDSLoader::x_LoadLDS_BioTSE(int object_id,
- CLDS_Database* db,
- CLDS_Set* obj_read,
- CPluginReply& reply)
- {
- SLDS_TablesCollection& tables = db->GetTables();
- CRef<CSeq_entry> entry =
- LDS_LoadTSE(tables, db->GetObjTypeMap(), object_id);
- if (!entry) {
- _TRACE("failed to read LDS object id=" << object_id);
- return false;
- }
- obj_read->insert(object_id);
- string dl_name = "LDS_dataloader_";
- dl_name.append(db->GetAlias());
- CRef<CScope> scope(new CScope(CDocManager::GetObjectManager()));
- scope->AddDataLoader(dl_name);
- scope->AddTopLevelSeqEntry(*entry);
- scope->AddDefaults();
- IDocument* doc =
- CDocManager::CreateDocument(*scope, *entry);
- reply.AddObject(*doc);
- CDocManager::UpdateAllViews();
- /**
- CBioseq_Handle handle =
- CSeqUtils::CreateBioseqHandle(*scope, *entry);
- if (handle) {
- scope->AddDefaults();
- IDocument* doc =
- CDocManager::CreateDocument(*scope,
- *CSeqUtils::GetBestId(*entry));
- reply.AddObject(*doc);
- CDocManager::UpdateAllViews();
- }
- **/
- return true;
- }
- bool CDataPlugin_LDSLoader::x_LoadLDS_Annot(const CLDS_Query::SObjectDescr& obj_descr,
- CLDS_Database* db,
- CLDS_Set* obj_read,
- CPluginReply& reply)
- {
- SLDS_TablesCollection& tables = db->GetTables();
- CRef<CSeq_annot> annot = LDS_LoadAnnot(tables, obj_descr);
- if (!annot) {
- _TRACE("failed to read LDS annotation id=" << obj_descr.id);
- return false;
- }
- obj_read->insert(obj_descr.id);
- string dl_name = "LDS_dataloader_";
- dl_name.append(db->GetAlias());
- CRef<CScope> scope(new CScope(CDocManager::GetObjectManager()));
- scope->AddDataLoader(dl_name);
- scope->AddDefaults();
- IDocument* doc = CDocManager::CreateDocument(*scope, *annot);
- reply.AddObject(*doc);
- CDocManager::UpdateAllViews();
- return true;
- }
- END_NCBI_SCOPE
- /*
- * ===========================================================================
- * $Log: lds_loader.cpp,v $
- * Revision 1000.5 2004/06/01 20:58:08 gouriano
- * PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.31
- *
- * Revision 1.31 2004/05/25 17:21:59 dicuccio
- * Modified class names. Fonts to 12 point
- *
- * Revision 1.30 2004/05/21 22:27:48 gorelenk
- * Added PCH ncbi_pch.hpp
- *
- * Revision 1.29 2004/05/03 13:05:43 dicuccio
- * gui/utils --> gui/objutils where needed
- *
- * Revision 1.28 2004/03/23 13:39:07 dicuccio
- * Load objects as seq-entry, not seq-id
- *
- * Revision 1.27 2004/03/16 15:59:50 vasilche
- * Removed warning about unused exception variable
- *
- * Revision 1.26 2003/11/24 15:45:38 dicuccio
- * Renamed CVersion to CPluginVersion
- *
- * Revision 1.25 2003/11/18 17:49:02 dicuccio
- * Added standard processing of return values
- *
- * Revision 1.24 2003/11/06 20:12:15 dicuccio
- * Cleaned up handling of USING_SCOPE - removed from all headers
- *
- * Revision 1.23 2003/11/05 13:30:45 kuznets
- * Fixed application crash when there is no LDS instances configured in the
- * system. (Search command). Put a warning pop up dialog.
- *
- * Revision 1.22 2003/11/04 17:49:25 dicuccio
- * Changed calling parameters for plugins - pass CPluginMessage instead of paired
- * CPluginCommand/CPluginReply
- *
- * Revision 1.21 2003/10/29 16:25:07 kuznets
- * Added support for LDS aliases.
- *
- * Revision 1.20 2003/10/28 14:21:01 kuznets
- * Chages to lds loader to support multiple LDS databases
- *
- * Revision 1.19 2003/10/09 19:20:53 kuznets
- * Added message box if Load or Search fails to find anything
- *
- * Revision 1.18 2003/10/08 18:20:42 kuznets
- * Reflecting changes in LDS library
- *
- * Revision 1.17 2003/10/07 13:47:05 dicuccio
- * Renamed CPluginURL* to CPluginValue*
- *
- * Revision 1.16 2003/10/03 18:55:52 kuznets
- * Added search time logging for CDataPlugin_LDSLoader::Load command
- *
- * Revision 1.15 2003/09/04 14:51:43 dicuccio
- * Use IDocument instead of CDocument
- *
- * Revision 1.14 2003/08/19 14:38:01 kuznets
- * Added non default LDS dataloader to all created scopes.
- *
- * Revision 1.13 2003/08/08 15:28:53 kuznets
- * Corrected documents list update when we open new views.
- *
- * Revision 1.12 2003/08/07 20:31:35 kuznets
- * Implemented document opening based on lds_search_dlg selection
- *
- * Revision 1.11 2003/08/05 17:16:25 kuznets
- * Changes to improve code reuse between loaders + CLDS_Database passed as the
- * construction parameter to the lds search dialog
- *
- * Revision 1.10 2003/07/31 20:44:32 kuznets
- * LDS Plugin uses CDataStore to get the LDS database object.
- *
- * Revision 1.9 2003/07/30 14:04:58 kuznets
- * Improved "gi|" search similar to genbank loader plugin
- *
- * Revision 1.8 2003/07/29 20:06:07 kuznets
- * "Search All" option added to Load command
- *
- * Revision 1.7 2003/07/15 17:57:08 kuznets
- * Added annotaion/alignment search capabilities
- *
- * Revision 1.6 2003/07/14 11:17:03 shomrat
- * Plugin messageing system related changes
- *
- * Revision 1.5 2003/07/09 20:59:12 kuznets
- * Added new lds plugin argument "search_all". Works as a boolean flag
- * indicating that search should include both seaquences and annotations.
- *
- * Revision 1.4 2003/07/01 19:29:14 kuznets
- * Open command implementation
- *
- * Revision 1.3 2003/06/25 17:02:58 dicuccio
- * Split CPluginHandle into a handle (pointer-to-implementation) and
- * implementation file. Lots of #include file clean-ups.
- *
- * Revision 1.2 2003/06/20 14:52:57 dicuccio
- * Revised plugin registration - moved GetInfo() into the plugin handler
- *
- * Revision 1.1 2003/06/17 20:16:09 kuznets
- * Initial revision.
- *
- *
- * ===========================================================================
- */