file_export.cpp
上传用户:yhdzpy8989
上传日期:2007-06-13
资源大小:13604k
文件大小:10k
- /*
- * ===========================================================================
- * PRODUCTION $Log: file_export.cpp,v $
- * PRODUCTION Revision 1000.5 2004/06/01 20:57:30 gouriano
- * PRODUCTION PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.14
- * PRODUCTION
- * ===========================================================================
- */
- /* $Id: file_export.cpp,v 1000.5 2004/06/01 20:57:30 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_FileExport - load sequence information from a file.
- */
- #include <ncbi_pch.hpp>
- #include "file_export.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 <objects/seqset/Seq_entry.hpp>
- #include <objmgr/util/sequence.hpp>
- #include <objmgr/bioseq_ci.hpp>
- #include <objtools/flat/flat_gbseq_formatter.hpp>
- #include <objtools/flat/flat_gff_formatter.hpp>
- #include <objtools/flat/flat_table_formatter.hpp>
- #include <serial/objostrasn.hpp>
- BEGIN_NCBI_SCOPE
- USING_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 ("full XML");
- static const string sc_gbseq_str ("GBSeq XML");
- static const string sc_fasta_str ("FastA");
- static const string sc_flat_str ("GenBank/EMBL/DDBJ Flat-File");
- static const string sc_gff_str ("GFF/GTF");
- static const string sc_table_str ("Sequin 5-column Feature Table");
- void CDataPlugin_FileExport::GetInfo(CPluginInfo& info)
- {
- info.Reset();
- // version info macro
- info.SetInfo(CPluginVersion::eMajor, CPluginVersion::eMinor, 0,
- string(__DATE__) + " " + string(__TIME__),
- "CDataPlugin_FileExport",
- "File", "File import / export plugin", "");
- // command info
- CPluginCommandSet& cmds = info.SetCommands();
- CPluginCommand& save_args = cmds.AddDataCommand(eDataCommand_save);
- save_args.AddArgument("file", "File name", CPluginArg::eFile);
- save_args.AddArgument("document", "Document", CPluginArg::eDocument);
- save_args.AddArgument("fmt", "Format", CPluginArg::eString);
- save_args.SetConstraint("fmt",
- (*CPluginValueConstraint::CreateSet(),
- sc_text_str,
- sc_binary_str,
- sc_xml_str,
- sc_gbseq_str,
- sc_fasta_str,
- sc_flat_str,
- sc_gff_str,
- sc_table_str
- ));
- }
- //
- // Save()
- // Save the information in a given document into a file
- //
- void CDataPlugin_FileExport::Save(CPluginMessage& msg)
- {
- const CPluginCommand& args = msg.GetRequest().GetCommand();
- CPluginReply& reply = msg.SetReply();
- IDocument* doc = const_cast<IDocument*> (&args["document"].AsDocument());
- if ( !doc ) {
- reply.SetStatus(eMessageStatus_failed);
- return;
- }
- string fname = args["file"].AsString();
- try {
- string fmt_str = args["fmt"].AsString();
- // 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
- //
- ESerialDataFormat fmt = eSerial_None;
- if (fmt_str == sc_text_str) {
- // ASN.1 text
- fmt = eSerial_AsnText;
- } else if (fmt_str == sc_binary_str) {
- // ASN.1 binary
- fmt = eSerial_AsnBinary;
- } else if (fmt_str == sc_xml_str) {
- // XML
- fmt = eSerial_Xml;
- } else if (fmt_str == sc_fasta_str) {
- //
- // FastA - alternate format
- //
- CScope& scope = doc->GetScope();
- CNcbiOfstream ostr(fname.c_str());
- CFastaOstream fasta_ostr(ostr);
- CObjectConverter::TObjList objs;
- CObjectConverter::Convert(scope, *obj, CSeq_entry::GetTypeInfo(),
- objs);
- ITERATE (CObjectConverter::TObjList, iter, objs) {
- const CSeq_entry& entry =
- dynamic_cast<const CSeq_entry&> (**iter);
- CBioseq_CI bioseq_iter(scope, entry);
- for ( ; bioseq_iter; ++bioseq_iter) {
- fasta_ostr.Write(*bioseq_iter);
- }
- }
- } else {
- //
- // Flat-File - alternate format
- //
- // should bring up a second dialog box rather than hardcoding
- // parameters
- CScope& scope = doc->GetScope();
- CNcbiOfstream ostr(fname.c_str());
- CFlatTextOStream ftos(ostr);
- auto_ptr<CObjectOStream> oos;
- auto_ptr<IFlatFormatter> ff;
- IFlatFormatter::EMode mode = IFlatFormatter::eMode_GBench;
- if (fmt_str == sc_flat_str) {
- ff.reset(CFlatTextFormatter::New(ftos, scope, mode,
- IFlatFormatter::eDB_NCBI));
- } else if (fmt_str == sc_table_str) {
- ff.reset(new CFlatTableFormatter(ftos, scope));
- } else if (fmt_str == sc_gff_str) {
- ff.reset(new CFlatGFFFormatter(ftos, scope, mode,
- CFlatGFFFormatter::fGTFCompat));
- } else if (fmt_str == sc_gbseq_str) {
- oos.reset(CObjectOStream::Open(eSerial_Xml, ostr));
- ff.reset(new CFlatGBSeqFormatter(*oos, scope, mode));
- }
- CObjectConverter::TObjList objs;
- CObjectConverter::Convert(scope, *obj,
- CSeq_entry::GetTypeInfo(), objs);
- ITERATE (CObjectConverter::TObjList, iter, objs) {
- ff->Format(dynamic_cast<const CSeq_entry&>(**iter), *ff);
- }
- }
- //
- // generic handler - use object ostreams
- if ( fmt != eSerial_None ) {
- auto_ptr<CObjectOStream> os(CObjectOStream::Open(fmt, fname));
- os->Write(obj, obj->GetThisTypeInfo());
- }
- 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 errorn");
- reply.SetStatus(eMessageStatus_failed);
- }
- #endif
- }
- END_NCBI_SCOPE
- /*
- * ===========================================================================
- * $Log: file_export.cpp,v $
- * Revision 1000.5 2004/06/01 20:57:30 gouriano
- * PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.14
- *
- * Revision 1.14 2004/05/25 17:21:59 dicuccio
- * Modified class names. Fonts to 12 point
- *
- * Revision 1.13 2004/05/21 22:27:48 gorelenk
- * Added PCH ncbi_pch.hpp
- *
- * Revision 1.12 2004/03/11 17:42:01 dicuccio
- * Dropped unneeded #includes
- *
- * Revision 1.11 2004/01/27 18:45:31 dicuccio
- * Added missing header files
- *
- * Revision 1.10 2004/01/21 12:38:19 dicuccio
- * redesigned CObjectCOnverter API to eliminate temporary object creation
- *
- * Revision 1.9 2004/01/13 20:38:16 dicuccio
- * Use the filename provided by the plugin arg dialog
- *
- * Revision 1.8 2003/12/31 20:29:31 dicuccio
- * Turned on use of file type arguments. Fixed issues relating to importation of
- * items into scopes - can now import anything.
- *
- * Revision 1.7 2003/12/05 13:07:52 dicuccio
- * Split management interface into a separate plugin. Fixed linker error
- * introduced with status bar
- *
- * Revision 1.6 2003/11/24 15:45:33 dicuccio
- * Renamed CVersion to CPluginVersion
- *
- * Revision 1.5 2003/11/06 20:12:14 dicuccio
- * Cleaned up handling of USING_SCOPE - removed from all headers
- *
- * Revision 1.4 2003/11/04 17:49:24 dicuccio
- * Changed calling parameters for plugins - pass CPluginMessage instead of paired
- * CPluginCommand/CPluginReply
- *
- * Revision 1.3 2003/10/09 16:11:07 ucko
- * Added types mediated by the flat-file generator: GBSeq, GenBank,
- * GFF/GTF, and Sequin feature table.
- *
- * Revision 1.2 2003/10/07 13:47:05 dicuccio
- * Renamed CPluginURL* to CPluginValue*
- *
- * Revision 1.1 2003/09/16 14:06:48 dicuccio
- * Initial revision - split from CFileLoader
- *
- * ===========================================================================
- */