indexed_export.cpp

来自「ncbi源码」· C++ 代码 · 共 432 行

CPP
432
字号
/* * =========================================================================== * PRODUCTION $Log: indexed_export.cpp,v $ * PRODUCTION Revision 1000.5  2004/06/01 20:58:04  gouriano * PRODUCTION PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.10 * PRODUCTION * =========================================================================== *//*  $Id: indexed_export.cpp,v 1000.5 2004/06/01 20:58:04 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:  Mike DiCuccio * * File Description: *    CDataPlugin_IndexedExport - save a sequence and all of its referenced components *                     into a directory */#include <ncbi_pch.hpp>#include "indexed_export.hpp"#include <corelib/ncbiapp.hpp>#include <corelib/ncbireg.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/dialogs/progress/progress_dlg_ex.hpp>#include <objects/seqset/Seq_entry.hpp>#include <objmgr/util/sequence.hpp>#include <objmgr/bioseq_ci.hpp>#include <objmgr/seq_map_ci.hpp>#include <serial/objostrasn.hpp>BEGIN_NCBI_SCOPEUSING_SCOPE(objects);static const string sc_text_str  ("Text ASN.1");static const string sc_binary_str("Binary ASN.1");static const string sc_xml_str   ("XML");static const string sc_fasta_str ("FastA");static const string sc_flat_str  ("GenBank Flat-File");void CDataPlugin_IndexedExport::GetInfo(CPluginInfo& info){    info.Reset();    // version info macro    info.SetInfo(CPluginVersion::eMajor, CPluginVersion::eMinor, 0,                 string(__DATE__) + " " + string(__TIME__),                 "CDataPlugin_IndexedExport", "Indexed Directory",                 "Save this record and all its components "                 "into a directory that can be indexed", "");    // command info    CPluginCommandSet& cmds     = info.SetCommands();    CPluginCommand& save_args   = cmds.AddDataCommand(eDataCommand_save);    save_args.AddArgument("seqid", "Sequence", CSeq_id::GetTypeInfo());    save_args.AddDefaultArgument("fmt", "Format", CPluginArg::eString,                                 sc_binary_str);    save_args.SetConstraint("fmt",                            (*CPluginValueConstraint::CreateSet(),                             sc_text_str,                             sc_binary_str,                             sc_xml_str,                             sc_fasta_str                             ));    save_args.AddArgument("file", "File name", CPluginArg::eFile);    save_args.AddArgument("alias", "Alias", CPluginArg::eString);}class CCancelButtonException : EXCEPTION_VIRTUAL_BASE public CException{public:    // Enumerated list of document management errors    enum EErrCode {        eCancelPressed    };    // Translate the specific error code into a string representations of    // that error code.    virtual const char* GetErrCodeString(void) const    {        switch (GetErrCode()) {        case eCancelPressed: return "eCancelPressed";        default:             return CException::GetErrCodeString();        }    }    NCBI_EXCEPTION_DEFAULT(CCancelButtonException, CException);};//// Save()// Save the information in a given document into a file//void CDataPlugin_IndexedExport::Save(CPluginMessage& msg){    const CPluginCommand& args = msg.GetRequest().GetCommand();    CPluginReply& reply = msg.SetReply();    const IDocument& doc = *args["seqid"].GetDocument();    const CSeq_id* id = dynamic_cast<const CSeq_id*> (&args["seqid"].AsObject());    if ( !id ) {        reply.SetStatus(eMessageStatus_failed);        return;    }    CProgressDlgEx dlg;    dlg.Show();    try {        string fmt_str   = args["fmt"].AsString();        string file_base = args["file"].AsString();        string alias     = args["alias"].AsString();        string path;        string base;        string sub_path;        {{            CDirEntry::SplitPath(file_base, &path, &base);            sub_path = path;            sub_path += base + ".data";            sub_path += CDirEntry::GetPathSeparator();            CDir dir(sub_path);            if ( !dir.Exists() ) {                dir.Create();            }        }}        // find out what object to serialize        // in general, we serialize precisely what the document contains        // the exception to this is that we dereference any seq-id        // to its largest component (= top-level seq-entry)        CConstRef<CSerialObject> obj            (dynamic_cast<const CSerialObject*> (doc.GetObject()));        if (obj.GetPointer()  &&            obj->GetThisTypeInfo() == CSeq_id::GetTypeInfo()) {            const CSeq_id& id =                dynamic_cast<const CSeq_id&>(*doc.GetObject());            CBioseq_Handle handle = doc.GetScope().GetBioseqHandle(id);            obj.Reset(&handle.GetTopLevelSeqEntry());        }        //        // format handling        //        EFormat fmt = eFormat_text_asn;        if (fmt_str == sc_text_str) {            // ASN.1 text            fmt = eFormat_text_asn;        } else if (fmt_str == sc_binary_str) {            // ASN.1 binary            fmt = eFormat_binary_asn;        } else if (fmt_str == sc_xml_str) {            // XML            fmt = eFormat_xml;        } else if (fmt_str == sc_fasta_str) {            // FastA            fmt = eFormat_fasta;        }        try {            //            // first, save the sequence of interest            //            set< CConstRef<CSeq_entry> > entries;            list< CConstRef<CSeq_entry> > entries_for_products;            dlg.SetMessage("Saving document...");            CScope& scope = doc.GetScope();            CBioseq_Handle handle = scope.GetBioseqHandle(*id);            x_Save(scope, handle.GetTopLevelSeqEntry(), fmt, file_base);            {{                CConstRef<CSeq_entry> ref(&handle.GetTopLevelSeqEntry());                entries.insert(ref);                entries_for_products.push_back(ref);            }}            //            // now, we iterate all sequences referred to by this sequence            //            int count = 0;            const CSeqMap& seqmap = handle.GetSeqMap();            CSeqMap::const_iterator map_iter =                seqmap.begin_resolved(&scope, -1, CSeqMap::fFindRef);            for ( ;  map_iter;  ++map_iter) {                if (map_iter.GetType() != CSeqMap::eSeqRef) {                    continue;                }                CSeq_id_Handle idh = map_iter.GetRefSeqid();                dlg.SetMessage("Retrieving sequence: " + idh.AsString());                LOG_POST(Info << "retrieving " << idh.AsString());                 CBioseq_Handle h = scope.GetBioseqHandle(idh);                const CSeq_entry& entry = h.GetTopLevelSeqEntry();                CConstRef<CSeq_entry> ref(&entry);                if ( !entries.insert(ref).second ) {                    continue;                }                entries_for_products.push_back(ref);                dlg.SetMessage("Saving sequence: " + idh.AsString());                LOG_POST(Info << "saving " << idh.AsString());                 string file_name =                    sub_path + base + "." + NStr::IntToString(++count);                x_Save(scope, entry, fmt, file_name);                Fl::check();                if ( !dlg.IsShown() ) {                    NCBI_THROW(CCancelButtonException,                               eCancelPressed, "");                }            }            //            // now, iterate all products in all seq-entries            //            ITERATE (list< CConstRef<CSeq_entry> >, iter, entries_for_products) {                CTypeConstIterator<CSeq_feat> feat_iter(**iter);                for ( ; feat_iter;  ++feat_iter) {                    const CSeq_feat& feat = *feat_iter;                    if ( !feat.IsSetProduct() ) {                        continue;                    }                    string id_label;                    sequence::GetId(feat.GetProduct()).GetLabel(&id_label);                    dlg.SetMessage("Retrieving product sequence: " + id_label);                    LOG_POST(Info << "retrieving " << id_label);                     CBioseq_Handle h = scope.GetBioseqHandle(feat.GetLocation());                    const CSeq_entry& entry = h.GetTopLevelSeqEntry();                    CConstRef<CSeq_entry> ref(&entry);                    if ( !entries.insert(ref).second ) {                        continue;                    }                    entries_for_products.push_back(ref);                    dlg.SetMessage("Saving product sequence: " + id_label);                    LOG_POST(Info << "saving " << id_label);                     string file_name =                        sub_path + base + "." + NStr::IntToString(++count);                    x_Save(scope, entry, fmt, file_name);                    Fl::check();                    if ( !dlg.IsShown() ) {                        NCBI_THROW(CCancelButtonException,                                   eCancelPressed, "");                    }                }            }        }        catch (CCancelButtonException&) {        }        //        // configure this path to be indexed at app start-up        //        CNcbiApplication* app = CNcbiApplication::Instance();        _ASSERT(app);        CNcbiRegistry& reg = app->GetConfig();        string section_name = "LDS";        {{            list<string> sections;            reg.EnumerateSections(&sections);            size_t count = 0;            ITERATE (list<string>, iter, sections) {                if (iter->find("LDS") == 0) {                    ++count;                }            }            section_name += NStr::IntToString(count + 1);        }}        reg.Set(section_name, "Path",  path, CNcbiRegistry::ePersistent);        reg.Set(section_name, "Alias", alias, CNcbiRegistry::ePersistent);        reply.SetStatus(eMessageStatus_success);    }    catch (CException& e) {        string msg("Failed to save file:\n");        msg += e.GetMsg();        NcbiMessageBox(msg);        reply.SetStatus(eMessageStatus_failed);    }#ifndef _DEBUG    catch (...) {        NcbiMessageBox("Failed to save file:\nUnknown error\n");        reply.SetStatus(eMessageStatus_failed);    }#endif}void CDataPlugin_IndexedExport::x_Save(CScope& scope, const CSeq_entry& entry,                            EFormat fmt, const string& file_name){    CNcbiOfstream ostr(file_name.c_str(), ios::binary);    switch (fmt) {    case eFormat_text_asn:        {{            auto_ptr<CObjectOStream> os                (CObjectOStream::Open(eSerial_AsnText, ostr));            *os << entry;        }}        break;    case eFormat_binary_asn:        {{            auto_ptr<CObjectOStream> os                (CObjectOStream::Open(eSerial_AsnBinary, ostr));            *os << entry;        }}        break;    case eFormat_xml:        {{            auto_ptr<CObjectOStream> os                (CObjectOStream::Open(eSerial_Xml, ostr));            *os << entry;        }}        break;    case eFormat_fasta:        {{            CFastaOstream fasta_ostr(ostr);            CBioseq_CI bioseq_iter(scope, entry);            for ( ;  bioseq_iter;  ++bioseq_iter) {                fasta_ostr.Write(*bioseq_iter);            }        }}        break;    }}END_NCBI_SCOPE/* * =========================================================================== * $Log: indexed_export.cpp,v $ * Revision 1000.5  2004/06/01 20:58:04  gouriano * PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.10 * * Revision 1.10  2004/05/25 17:21:59  dicuccio * Modified class names.  Fonts to 12 point * * Revision 1.9  2004/05/21 22:27:48  gorelenk * Added PCH ncbi_pch.hpp * * Revision 1.8  2004/03/11 17:43:41  dicuccio * Use new file chooser dialog * * Revision 1.7  2004/02/05 18:05:07  dicuccio * Added better (GUI) handling of sequence download.  Added saving of products as * well as main sequence. * * Revision 1.6  2004/01/27 18:44:25  dicuccio * Added progress dialog.  Added missing headers.  Added parameter handling of * fiel to save to.  CHanged directory layout of indexed structure to show master * record in top-level directory * * Revision 1.5  2003/12/05 13:07:54  dicuccio * Split management interface into a separate plugin.  Fixed linker error * introduced with status bar * * Revision 1.4  2003/11/24 15:45:37  dicuccio * Renamed CVersion to CPluginVersion * * Revision 1.3  2003/11/06 20:12:15  dicuccio * Cleaned up handling of USING_SCOPE - removed from all headers * * Revision 1.2  2003/11/04 17:49:25  dicuccio * Changed calling parameters for plugins - pass CPluginMessage instead of paired * CPluginCommand/CPluginReply * * Revision 1.1  2003/10/07 13:41:13  dicuccio * Added indexed export plugin * * Revision 1.1  2003/09/16 14:06:48  dicuccio * Initial revision - split from CFileLoader * * =========================================================================== */

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?