run_cn3d.cpp
上传用户:yhdzpy8989
上传日期:2007-06-13
资源大小:13604k
文件大小:9k
- /*
- * ===========================================================================
- * PRODUCTION $Log: run_cn3d.cpp,v $
- * PRODUCTION Revision 1000.1 2004/06/01 20:55:55 gouriano
- * PRODUCTION PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.7
- * PRODUCTION
- * ===========================================================================
- */
- /* $Id: run_cn3d.cpp,v 1000.1 2004/06/01 20:55:55 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: gbench plugin for invoking Cn3D
- *
- */
- #include <ncbi_pch.hpp>
- #include "run_cn3d.hpp"
- #include <gui/core/idocument.hpp>
- #include <gui/core/plugin_utils.hpp>
- #include <gui/plugin/PluginCommandSet.hpp>
- #include <gui/plugin/PluginValue.hpp>
- #include <gui/plugin/PluginInfo.hpp>
- #include <gui/plugin/PluginReply.hpp>
- #include <gui/plugin/PluginRequest.hpp>
- #include <gui/plugin/PluginValueConstraint.hpp>
- #include <gui/core/version.hpp>
- #include <gui/core/doc_manager.hpp>
- #include <gui/objutils/utils.hpp>
- #include <gui/utils/message_box.hpp>
- #include <FL/Fl_File_Chooser.H>
- #include <gui/utils/file_deletion.hpp>
- #include <corelib/ncbifile.hpp>
- #include <corelib/ncbiexec.hpp>
- #include <corelib/ncbiapp.hpp>
- #include <corelib/ncbireg.hpp>
- #include <serial/serial.hpp>
- #include <serial/objostr.hpp>
- #include <objects/seq/Bioseq.hpp>
- #include <objects/seqalign/Dense_seg.hpp>
- #include <objects/ncbimime/Ncbi_mime_asn1.hpp>
- #include <objects/ncbimime/Biostruc_seqs_aligns_cdd.hpp>
- #include <objects/ncbimime/Bundle_seqs_aligns.hpp>
- #include <objects/seqalign/Dense_seg.hpp>
- BEGIN_NCBI_SCOPE
- USING_SCOPE(objects);
- // standard info boilerplate
- void CAlgoPlugin_RunCn3d::GetInfo(CPluginInfo& info)
- {
- info.Reset();
- // version info macro
- info.SetInfo(CPluginVersion::eMajor, CPluginVersion::eMinor, 0,
- string(__DATE__) + " " + string(__TIME__),
- "CAlgoPlugin_RunCn3d",
- "Alignments/Launch Cn3D",
- "Invoke the Cn3D program",
- "");
- // command info
- CPluginCommandSet& cmds = info.SetCommands();
- CPluginCommand& args = cmds.AddAlgoCommand(eAlgoCommand_run);
- args.AddArgument("aligns", "Input alignment(s)",
- CSeq_align::GetTypeInfo(),
- CPluginArg::TData::e_Array);
- }
- struct SIdLess
- {
- public:
- bool operator() (CRef<CSeq_id> lhs, CRef<CSeq_id> rhs) {
- return *lhs < *rhs;
- }
- };
- // Given a multiple alignment (Dense-seg), express as multiple pairwise
- // alignments, and append these to list.
- // The first sequence in the input alignment is the first sequence
- // in each of the aln.GetDim() - 1 pairwise alignments.
- static void s_AsPairwise(const CSeq_align& aln,
- CSeq_annot::C_Data::TAlign& pairs)
- {
- const CDense_seg& ds = aln.GetSegs().GetDenseg();
- vector<CRef<CDense_seg> > dss(aln.GetSegs().GetDenseg().GetDim() - 1);
- CDense_seg::TIds::const_iterator id = ds.GetIds().begin(); ++id;
- CDense_seg::TStrands::const_iterator strand;
- if (ds.IsSetStrands()) {
- strand = ds.GetStrands().begin();
- ++strand;
- }
- NON_CONST_ITERATE (vector<CRef<CDense_seg> >, iter, dss) {
- *iter = new CDense_seg;
- (*iter)->SetNumseg(ds.GetNumseg());
- (*iter)->SetIds().push_back(ds.GetIds().front());
- (*iter)->SetIds().push_back(*id);
- ++id;
- (*iter)->SetLens() = ds.GetLens();
- if (ds.IsSetStrands()) {
- (*iter)->SetStrands().push_back(ds.GetStrands().front());
- (*iter)->SetStrands().push_back(*strand);
- ++strand;
- }
- }
- unsigned int i = 0;
- ITERATE (CDense_seg::TStarts, iter, ds.GetStarts()) {
- if (i % ds.GetDim()) {
- dss[i % ds.GetDim() - 1]->SetStarts().push_back(*iter);
- } else {
- // start for master sequence; goes in all pairwise
- for (unsigned int p = 0; p < dss.size(); ++p) {
- dss[p]->SetStarts().push_back(*iter);
- }
- }
- ++i;
- }
- // have Dense-segs; make Seq-aligns and append
- for (unsigned int p = 0; p < dss.size(); ++p) {
- CRef<CSeq_align> pairwise(new CSeq_align);
- pairwise->SetType(CSeq_align::eType_partial);
- pairwise->SetDim(2);
- pairwise->SetSegs().SetDenseg(*dss[p]);
- pairs.push_back(pairwise);
- }
- }
- void CAlgoPlugin_RunCn3d::RunCommand(CPluginMessage& msg)
- {
- const CPluginCommand& args = msg.GetRequest().GetCommand();
- CPluginReply& reply = msg.SetReply();
- _TRACE("CAlgoPlugin_RunCn3d::Run()");
- // retrieve our alignments
- plugin_args::TAlignList aligns;
- GetArgValue(args["aligns"], aligns);
- // build a Ncbi-mime-asn1 object around alignments
- CRef<CSeq_annot> annot(new CSeq_annot);
- typedef map<CRef<CSeq_id>, CScope *, SIdLess> TIdScopeMap;
- TIdScopeMap ids;
- ITERATE (plugin_args::TAlignList, iter, aligns) {
- s_AsPairwise(*iter->second, annot->SetData().SetAlign());
- ITERATE (CDense_seg::TIds, id,
- iter->second->GetSegs().GetDenseg().GetIds()) {
- ids.insert(make_pair(*id, &iter->first->GetScope()));
- }
- }
-
- CRef<CNcbi_mime_asn1> mime(new CNcbi_mime_asn1);
- mime->SetGeneral().SetSeq_align_data().SetBundle().SetSeqaligns()
- .push_back(annot);
- ITERATE (TIdScopeMap, iter, ids) {
- CRef<CBioseq> bs(new CBioseq);
- CRef<CSeq_entry> entry(new CSeq_entry);
- bs->Assign(*(iter->second)->GetBioseqHandle(*(iter->first))
- .GetCompleteBioseq());
- entry->SetSeq(*bs);
- mime->SetGeneral().SetSeq_align_data().SetBundle().SetSequences()
- .push_back(entry);
- }
- // Write it to a temp file and launch the app
- string fname = CFile::GetTmpName();
- {{
- auto_ptr<CObjectOStream>
- ostr(CObjectOStream::Open(eSerial_AsnText, fname));
- *ostr << *mime;
- }}
- CDeleteAtExit::Add(fname);
- // Try to run Cn3D. The path to the executable can
- // come from the registry. Otherwise "Cn3D" is tried.
- CNcbiApplication* app = CNcbiApplication::Instance();
- _ASSERT(app);
- CNcbiRegistry& registry = app->GetConfig();
- string path = registry.GetString("external_apps", "cn3d", "Cn3D");
- bool new_path = 0; // has a new path been set?
- int phandle;
- int status;
- while (1) {
- phandle = CExec::SpawnLP(CExec::eDetach, path.c_str(),
- fname.c_str(), 0);
- // this is a dicey way of finding out whether
- // the spawn was successful
- status = CExec::Wait(phandle, 1000);
- if (status == -1) {
- break;
- }
- const char *text =
- fl_file_chooser("Please locate the Cn3D application", "*", "");
- if ( !text || !*text ) {
- break;
- }
- path = text;
- new_path = true;
- }
- if (status == -1) {
- reply.SetStatus(eMessageStatus_success);
- if (new_path) {
- registry.Set("external_apps", "cn3d", path,
- CNcbiRegistry::ePersistent);
- }
- }
- }
- END_NCBI_SCOPE
- /*
- * ===========================================================================
- * $Log: run_cn3d.cpp,v $
- * Revision 1000.1 2004/06/01 20:55:55 gouriano
- * PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.7
- *
- * Revision 1.7 2004/05/21 22:27:47 gorelenk
- * Added PCH ncbi_pch.hpp
- *
- * Revision 1.6 2004/05/20 12:37:58 dicuccio
- * Added missing include for CDense_seg
- *
- * Revision 1.5 2004/05/15 03:17:04 ucko
- * Add missing #includes (formerly indirect?)
- *
- * Revision 1.4 2004/05/03 13:05:42 dicuccio
- * gui/utils --> gui/objutils where needed
- *
- * Revision 1.3 2004/04/16 17:23:06 ucko
- * GetCompleteBioseq returns a CConstRef rather than a C++ reference.
- *
- * Revision 1.2 2004/04/16 14:43:37 dicuccio
- * Use GetCompleteBioseq() instead of GetBioseq()
- *
- * Revision 1.1 2004/03/05 16:44:22 jcherry
- * Initial version
- *
- * ===========================================================================
- */