NCBI C++ ToolKit
cmd_del_bioseq.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

00001 /*  $Id: cmd_del_bioseq.cpp 29499 2013-12-27 16:02:54Z filippov $
00002  * ===========================================================================
00003  *
00004  *                            PUBLIC DOMAIN NOTICE
00005  *               National Center for Biotechnology Information
00006  *
00007  *  This software/database is a "United States Government Work" under the
00008  *  terms of the United States Copyright Act.  It was written as part of
00009  *  the author's official duties as a United States Government employee and
00010  *  thus cannot be copyrighted.  This software/database is freely available
00011  *  to the public for use. The National Library of Medicine and the U.S.
00012  *  Government have not placed any restriction on its use or reproduction.
00013  *
00014  *  Although all reasonable efforts have been taken to ensure the accuracy
00015  *  and reliability of the software and data,  the NLM and the U.S.
00016  *  Government do not and cannot warrant the performance or results that
00017  *  may be obtained by using this software or data. The NLM and the U.S.
00018  *  Government disclaim all warranties,  express or implied,  including
00019  *  warranties of performance,  merchantability or fitness for any particular
00020  *  purpose.
00021  *
00022  *  Please cite the author in any work or product based on this material.
00023  *
00024  * ===========================================================================
00025  *
00026  * Authors:  Roman Katargin, Igor Filippov
00027  */
00028 
00029 
00030 #include <ncbi_pch.hpp>
00031 
00032 #include <gui/objutils/cmd_del_bioseq.hpp>
00033 #include <objmgr/seq_annot_ci.hpp>
00034 #include <objmgr/seqdesc_ci.hpp>
00035 #include <objects/seqloc/Seq_id.hpp>
00036 
00037 BEGIN_NCBI_SCOPE
00038 USING_SCOPE(objects);
00039 
00040 CCmdDelBioseqInst::CCmdDelBioseqInst(CBioseq_Handle& bsh)
00041     : m_BSH(bsh)
00042 {
00043 }
00044 
00045 void CCmdDelBioseqInst::Execute()  
00046 {
00047     if (m_BSH.IsRemoved()) return;
00048     objects::CBioseq_set_Handle bssh = m_BSH.GetParentBioseq_set(); // get the parent set - could be empty
00049     objects::CSeq_entry_Handle seh =  m_BSH.GetSeq_entry_Handle();
00050     if (bssh) {
00051         m_index = bssh.GetSeq_entry_Index(seh);   // undocumented function but seems to do what it says
00052         m_Set_SEH = bssh.GetParentEntry();
00053     } else {
00054         m_index = -1;
00055         m_Set_SEH = seh;
00056     }
00057 
00058     m_Inst.Reset();
00059     m_Inst = m_BSH.GetCompleteBioseq();
00060     bool is_prot = m_BSH.IsAa();
00061     CBioseq_EditHandle eh(m_BSH);
00062     eh.Remove();
00063     if (is_prot && bssh && bssh.IsSetClass() && bssh.GetClass() == CBioseq_set::eClass_nuc_prot
00064         && !bssh.IsEmptySeq_set() && bssh.GetBioseq_setCore()->GetSeq_set().size() == 1) {
00065 
00066         // Before renormalizing, SAVE setlevel annots so that you can reattach 
00067         // them to the set during UNDO
00068         CSeq_annot_CI annot_ci( m_Set_SEH, 
00069                 CSeq_annot_CI::eSearch_entry /*nonrecursive*/ );
00070     m_SetLevelAnnots.clear();
00071         for( ; annot_ci; ++annot_ci ) {
00072         m_SetLevelAnnots.push_back( *annot_ci );
00073     }
00074 
00075     // Before renormalizing, SAVE setlevel descrs so that you can reattach 
00076     // them to the set during UNDO
00077         CSeqdesc_CI desc_ci( m_Set_SEH, 
00078                  CSeqdesc::e_not_set, /* all descrs */
00079                  1 /* search this seqentry only */ );
00080     m_SetLevelDescrs.clear();
00081         for( ; desc_ci; ++desc_ci ) {
00082         m_SetLevelDescrs.push_back( CConstRef<CSeqdesc>(&*desc_ci) );
00083     }
00084 
00085         // Renormalize the lone nuc that's inside the nuc-prot set into a 
00086         // nuc bioseq.  This call will remove annots/descrs from the set and 
00087         // attach them to the seq.
00088         m_Set_SEH.GetEditHandle().ConvertSetToSeq();
00089     }
00090 }
00091 
00092 void CCmdDelBioseqInst::Unexecute() 
00093 {
00094     if (!m_BSH.IsRemoved()) return;
00095     objects::CBioseq_set_Handle bssh = m_BSH.GetParentBioseq_set();
00096     if (m_Set_SEH && !m_Set_SEH.IsSet() && m_Inst->IsAa()) {
00097 
00098     // If there were setlevel descrs originally, restore them by
00099     // removing them from the seq and adding them to the set
00100         vector< CRef<CSeqdesc> > removedDescrs;
00101     for (unsigned int bb = 0; bb < m_SetLevelDescrs.size(); ++bb) {
00102         // Remove the descr from the seq and store the returned
00103         // non-const! CRef<CSeqdesc> for later insertion to nuc-prot set.
00104         // Later call to AddSeqdesc requires non-const obj
00105         removedDescrs.push_back(
00106             m_Set_SEH.GetEditHandle().RemoveSeqdesc(*m_SetLevelDescrs[bb]));
00107     }
00108 
00109         // UNDO the renormalization
00110         CSeq_entry_EditHandle edit_handle = m_Set_SEH.GetEditHandle();
00111         edit_handle.ConvertSeqToSet();
00112         edit_handle.SetSet().SetClass(CBioseq_set::eClass_nuc_prot);
00113 
00114 
00115     // If there were setlevel annots originally, restore them by
00116     // taking them from the nuc
00117     for (unsigned int ii = 0; ii < m_SetLevelAnnots.size(); ++ii) {
00118         m_Set_SEH.GetEditHandle().TakeAnnot( 
00119                 m_SetLevelAnnots[ii].GetEditHandle() );
00120     }
00121 
00122     // If there were setlevel descrs originally, restore them by
00123     // removing them from the seq and adding them to the set
00124     for (unsigned int nn = 0; nn < removedDescrs.size(); ++nn) {
00125         m_Set_SEH.GetEditHandle().AddSeqdesc(*removedDescrs[nn]);
00126     }
00127     }
00128 
00129     if (m_Set_SEH &&m_Set_SEH.IsSet())
00130         bssh = m_Set_SEH.GetSet();
00131 
00132     if (bssh)
00133     {
00134         CBioseq_set_EditHandle eh(bssh);
00135         m_BSH = eh.AttachBioseq(const_cast<CBioseq &>(*m_Inst), m_index); 
00136     }
00137     else if (m_Set_SEH) // TODO
00138     {
00139         CSeq_entry_EditHandle eh(m_Set_SEH);
00140         m_BSH = eh.AttachBioseq(const_cast<CBioseq &>(*m_Inst));
00141     }
00142 }
00143 
00144 string CCmdDelBioseqInst::GetLabel()
00145 {
00146     return "Delete Bioseq";
00147 }
00148 
00149 END_NCBI_SCOPE
Modified on Sun Sep 21 18:23:25 2014 by modify_doxy.py rev. 426318