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

Go to the SVN repository for this file.

1 /* $Id: attached_handles.cpp 45111 2020-06-02 20:15:06Z asztalos $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Authors: Roman Katargin
27  *
28  * File Description:
29  *
30  */
31 
32 #include <ncbi_pch.hpp>
33 
35 
38 
46 
47 #include <objmgr/util/sequence.hpp>
48 
49 #include <objmgr/scope.hpp>
50 #include <objmgr/bioseq_handle.hpp>
51 
54 
56 {
58  try {
60 
61  } catch( CException& ex ){
62 // try {
63 // seh = scope.GetSeq_entryHandle(entry);
64 // LOG_POST(Warning << "attempted to attach already existing seq-entry");
65 // }
66 // catch( CException& ){
67  LOG_POST( Error << "Failed to attach seq-entry: " << ex.GetMsg() );
68 // }
69  }
70 
71  return seh;
72 }
73 
74 
76 {
77  switch( annot.GetData().Which() ){
82  try{
84 
85  } catch( CException& ex ){
86  LOG_POST( Error << "Failed to attach seq-annot: " << ex.GetMsg() );
87  }
88 
89  default:
90  /// bags of IDs or locs ignored
91  break;
92  }
93 
94  return CSeq_annot_Handle();
95 }
96 
97 
99 {
100  CRef<CSeq_annot> annot;
101  const CSeq_align_set* align_set = dynamic_cast<const CSeq_align_set*>(&obj);
102  if (align_set) {
103  annot.Reset(new CSeq_annot());
104  ITERATE (CSeq_align_set::Tdata, iter, align_set->Get()) {
105  CRef<CSeq_align> ref(const_cast<CSeq_align*>(iter->GetPointer()));
106  annot->SetData().SetAlign().push_back(ref);
107  }
108  }
109  return annot;
110 }
111 
112 
114 {
115  CRef<CSeq_annot> annot;
116  const CSeq_align* align = dynamic_cast<const CSeq_align*>(&obj);
117  if (align) {
118  annot.Reset(new CSeq_annot());
119  annot->SetData().SetAlign()
120  .push_back(CRef<CSeq_align>(const_cast<CSeq_align*>(align)));
121  }
122  return annot;
123 }
124 
125 #define USE_ANNOT_FOR_WELL_KNOWN_SEQUENCES
126 
128  bool annot_only)
129 {
130  switch (item.GetItem().Which()) {
132  {{
133  int n_bioseq = 0;
134  CSeq_entry &se = item.SetItem().SetEntry();
135  if (annot_only) {
136  for (CTypeIterator<CBioseq> bioseq(Begin(se)); bioseq; ++bioseq) {
137  ++n_bioseq;
138  CBioseq& bs = *bioseq;
139  // Check that there are no local (or general) ids in the
140  // bioseqs id set
141  bool local = false;
142  ITERATE(list<CRef<CSeq_id> >, it, bs.GetId()) {
143  const CSeq_id &id = **it;
144  if (id.IsLocal() || id.IsGeneral()) {
145  local = true;
146  break;
147  }
148  }
149  if (!local) {
150  // Attach annot only
151  if (bs.CanGetAnnot()) {
154  s_AttachAnnotToScope(scope, **iter);
155  if (h) {
156  m_Seq_annot_Handles.push_back(h);
157  }
158  }
159  }
160  } else {
161  // Whole bioseq, wrap to seq-entry first.
162  CRef<CSeq_entry> se_wrap(new CSeq_entry);
163  se_wrap->SetSeq(bs);
164  CSeq_entry_Handle seh =
165  s_AttachEntryToScope(scope, *se_wrap);
166  if (seh) {
167  m_Seq_entry_Handles.push_back(seh);
168  }
169  }
170  }
171  // Top-level annot
172  if (se.IsSet() && se.GetSet().CanGetAnnot()) {
175  s_AttachAnnotToScope(scope, **iter);
176  if (h) {
177  m_Seq_annot_Handles.push_back(h);
178  }
179  }
180  }
181  } else {
182  /// CSeq_entry already - just add to scope
183  CSeq_entry_Handle seh =
184  s_AttachEntryToScope(scope, item.SetItem().SetEntry());
185  if (seh) {
186  m_Seq_entry_Handles.push_back(seh);
187  }
188  }
189  }}
190  break;
191 
193  {{
194  /// Annotation - call CScope::AddSeq_annot()
196  s_AttachAnnotToScope(scope, item.SetItem().SetAnnot());
197  if (h) {
198  m_Seq_annot_Handles.push_back(h);
199  }
200  }}
201  break;
202 
204  /// Seq-submit may be either entries or annotations
205  /// these are handled as above
206  switch (item.GetItem().GetSubmit().GetData().Which()) {
208  NON_CONST_ITERATE (CSeq_submit::TData::TEntrys, iter, item.SetItem().SetSubmit().SetData().SetEntrys()) {
210  s_AttachEntryToScope(scope, **iter);
211  if (h) {
212  m_Seq_entry_Handles.push_back(h);
213  }
214  }
215  break;
216 
218  NON_CONST_ITERATE (CSeq_submit::TData::TAnnots, iter, item.SetItem().SetSubmit().SetData().SetAnnots()) {
220  s_AttachAnnotToScope(scope, **iter);
221  if (h) {
222  m_Seq_annot_Handles.push_back(h);
223  }
224  }
225  break;
226 
227  default:
228  break;
229  }
230  break;
231 
233  /// alignments have a simple annotation representation
234  /// and can be converted inexpensively
235  {{
236  CRef<CSeq_annot> annot = s_Align2Annot(*item.GetObject());
237  CSeq_annot_Handle h = s_AttachAnnotToScope(scope, *annot);
238  if (h) {
239  m_Seq_annot_Handles.push_back(h);
240  }
241  }}
242  break;
243 
245  /// alignments have a simple annotation representation
246  /// and can be converted inexpensively
247  {{
248  CRef<CSeq_annot> annot = s_AlignSet2Annot(*item.GetObject());
249  CSeq_annot_Handle h = s_AttachAnnotToScope(scope, *annot);
250  if (h) {
251  m_Seq_annot_Handles.push_back(h);
252  }
253  }}
254  break;
255 
257  /// other items should be converted to top-level
258  /// seq-entries and added to the scope
259  {{
260  const CObject* obj = item.GetObject();
261  {{
262  /// exclude certain types of object
263  const CSerialObject* cso = dynamic_cast<const CSerialObject*>(obj);
264  if (cso) {
265  const CTypeInfo* ti = cso->GetThisTypeInfo();
266  if (ti == CSeq_loc::GetTypeInfo()) {
267  /// nothing to do here - seq-locs are independent
268  /// entities
269  break;
270  }
271  if (ti == CEntrezgene::GetTypeInfo()) {
272  /// nothing to do here - Entrezgene records
273  /// are independent entities
274  break;
275  }
276  if (ti == CEntrezgene_Set::GetTypeInfo()) {
277  /// nothing to do here - Entrezgene records
278  /// are independent entities
279  break;
280  }
281  if (ti == CSeq_feat::GetTypeInfo()) {
282  CSeq_feat& feat = const_cast<CSeq_feat&>
283  (dynamic_cast<const CSeq_feat&>(*obj));
284 
285  CRef<CSeq_annot> annot(new CSeq_annot());
286  annot->SetData().SetFtable().push_back(CRef<CSeq_feat>(&feat));
288  if (h) {
289  m_Seq_annot_Handles.push_back(h);
290  }
291  break;
292  }
293  if (ti == CGC_Assembly::GetTypeInfo()) {
294  // Attach all alignment annotations to scope
295  CGC_Assembly* assm = const_cast<CGC_Assembly*>(
296  dynamic_cast<const CGC_Assembly*>(cso));
297  CTypeIterator<CSeq_annot> annot_iter(*assm);
298  for (; annot_iter; ++annot_iter) {
299  if (annot_iter->IsAlign()) {
300  // We only collect seq-aligns with type not set to 'global', and
301  // alignments will be sorted by assembly unit, JIRA GB-2344
302  typedef map<string, CSeq_annot::TData::TAlign> TSortedAligns;
303  TSortedAligns sorted_aligns;
304  NON_CONST_ITERATE(CSeq_annot::TData::TAlign, aln_iter, annot_iter->SetData().SetAlign()) {
305  if ((*aln_iter)->GetType() != CSeq_align::eType_global) {
306  CSeq_id_Handle alt_locus_id =
307  sequence::GetId((*aln_iter)->GetSeq_id(0), scope, sequence::eGetId_Canonical);
308  CConstRef< CGC_Sequence > gc_seq = assm->Find (alt_locus_id) ;
309  if (gc_seq) {
310  string assm_unit = gc_seq->GetAssemblyUnit()->GetName();
311  sorted_aligns[assm_unit].push_back(*aln_iter);
312  }
313  }
314  }
315 
316  if ( !sorted_aligns.empty() ) {
317  string annot_name = "";
318  if (annot_iter->IsSetDesc()) {
319  ITERATE (objects::CAnnot_descr::Tdata, descrIter, annot_iter->GetDesc().Get()) {
320  if ((*descrIter)->IsTitle()) {
321  annot_name = (*descrIter)->GetTitle();
322  break;
323  } else if ((*descrIter)->IsName()) {
324  annot_name = (*descrIter)->GetName();
325  }
326  }
327  }
328  if ( !annot_name.empty() ) {
329  annot_name += " - ";
330  }
331 
332  NON_CONST_ITERATE(TSortedAligns, sorted_annot_iter, sorted_aligns) {
333  CRef<CSeq_annot> annot(new CSeq_annot());
334  annot->SetNameDesc(annot_name + sorted_annot_iter->first);
335  annot->SetData().SetAlign() = sorted_annot_iter->second;
337  s_AttachAnnotToScope(scope, *annot);
338  if (h) {
339  m_Seq_annot_Handles.push_back(h);
340  }
341  }
342  }
343  }
344  }
345  break;
346  }
347 
348  if (ti == CBioTreeContainer::GetTypeInfo()) {
349  // nothing to do here
350  break;
351  }
352 
353  if (ti == CProjectItem::GetTypeInfo()) {
354  CProjectItem& sub_item = const_cast<CProjectItem&>(dynamic_cast<const CProjectItem&>(*obj));
355  Attach(scope, sub_item);
356  break;
357  }
358  }
359  }}
360 
361  if (obj) {
363  CObjectConverter::Convert(scope, *obj,
364  CSeq_entry::GetTypeInfo(), objs);
365  ITERATE (CObjectConverter::TObjList, obj_iter, objs) {
366  CSeq_entry& entry =
367  const_cast<CSeq_entry&>
368  (dynamic_cast<const CSeq_entry&>(obj_iter->GetObject()));
370  s_AttachEntryToScope(scope, entry);
371  if (h) {
372  m_Seq_entry_Handles.push_back(h);
373  }
374  }
375  }
376  }}
377  break;
378 
380  /// ID -> do nothing; IDs should be referrable directly
382  /// don't do anything special to a plugin message
383  default:
384  /// default trap: pubmed ID, taxonomy ID
385  break;
386  }
387 }
388 
389 static void s_DetachEntryFromScope(CScope& scope, CSeq_entry_Handle& handle)
390 {
391  if( !handle ){
392  return;
393  }
394 
395  try {
396  scope.RemoveTopLevelSeqEntry( const_cast<CTSE_Handle&>(handle.GetTSE_Handle()) );
397 
398  } catch( CException& ex ){
399  LOG_POST(
400  Error << "s_DetachEntryFromScope(): error removing seq-entry: " << ex.GetMsg()
401  );
402  }
403 }
404 
406 {
407  if( annot ){
408  try {
409  annot.GetEditHandle().Remove();
410 
411  } catch( CException& ex ){
412  LOG_POST(Error
413  << "s_DetachAnnotFromScope(): error removing seq-annot: "
414  << ex.GetMsg()
415  );
416  }
417  }
418 }
419 
421 {
422  NON_CONST_ITERATE (vector<CSeq_entry_Handle>, iter, m_Seq_entry_Handles) {
423  s_DetachEntryFromScope(scope, *iter);
424  }
425  m_Seq_entry_Handles.clear();
426 
427  NON_CONST_ITERATE (vector<CSeq_annot_Handle>, iter, m_Seq_annot_Handles) {
428  s_DetachAnnotFromScope(scope, *iter);
429  }
430  m_Seq_annot_Handles.clear();
431 }
432 
User-defined methods of the data storage class.
User-defined methods of the data storage class.
User-defined methods of the data storage class.
User-defined methods of the data storage class.
User-defined methods of the data storage class.
User-defined methods of the data storage class.
User-defined methods of the data storage class.
static CRef< CSeq_annot > s_AlignSet2Annot(const CObject &obj)
USING_SCOPE(objects)
static CSeq_annot_Handle s_AttachAnnotToScope(CScope &scope, CSeq_annot &annot)
static CRef< CSeq_annot > s_Align2Annot(const CObject &obj)
static void s_DetachAnnotFromScope(CScope &scope, CSeq_annot_Handle &annot)
static CSeq_entry_Handle s_AttachEntryToScope(CScope &scope, CSeq_entry &entry)
static void s_DetachEntryFromScope(CScope &scope, CSeq_entry_Handle &handle)
vector< objects::CSeq_annot_Handle > m_Seq_annot_Handles
vector< objects::CSeq_entry_Handle > m_Seq_entry_Handles
void Attach(objects::CScope &scope, objects::CProjectItem &item, bool annot_only=false)
void Detach(objects::CScope &scope)
CConstRef –.
Definition: ncbiobj.hpp:1266
void Find(const CSeq_id_Handle &id, TSequenceList &sequences) const
Find all references to a given sequence within an assembly.
static void Convert(objects::CScope &scope, const CObject &obj, const CTypeInfo *info, TObjList &objs, TFlags flags=eDefault)
Convert an object of potentially unknown type to a set of objects of known type.
ITypeConverter::TObjList TObjList
CObject –.
Definition: ncbiobj.hpp:180
const CSerialObject * GetObject() const
retrieve the object pointed to as a CObject*
CScope –.
Definition: scope.hpp:92
CSeq_annot_Handle –.
void SetNameDesc(const string &name)
Definition: Seq_annot.cpp:66
CSeq_entry_Handle –.
Definition: Seq_entry.hpp:56
namespace ncbi::objects::
Definition: Seq_feat.hpp:58
Base class for all serializable objects.
Definition: serialbase.hpp:150
CTypeInfo class contains all information about C++ types (both basic and classes): members and layout...
Definition: typeinfo.hpp:76
Definition: map.hpp:338
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
Definition: ncbimisc.hpp:815
#define NON_CONST_ITERATE(Type, Var, Cont)
Non constant version of ITERATE macro.
Definition: ncbimisc.hpp:822
#define LOG_POST(message)
This macro is deprecated and it's strongly recomended to move in all projects (except tests) to macro...
Definition: ncbidiag.hpp:226
void Error(CExceptionArgs_Base &args)
Definition: ncbiexpt.hpp:1197
const string & GetMsg(void) const
Get message string.
Definition: ncbiexpt.cpp:461
virtual const CTypeInfo * GetThisTypeInfo(void) const =0
TObjectInfo & Get(void)
Get information about current object.
Definition: iterator.hpp:209
CBeginInfo Begin(C &obj)
Get starting point of object hierarchy.
Definition: iterator.hpp:1004
const CSeq_id & GetId(const CSeq_loc &loc, CScope *scope)
If all CSeq_ids embedded in CSeq_loc refer to the same CBioseq, returns the first CSeq_id found,...
@ eGetId_Canonical
Definition: sequence.hpp:114
CSeq_entry_Handle AddTopLevelSeqEntry(CSeq_entry &top_entry, TPriority pri=kPriority_Default, EExist action=eExist_Default)
Add seq_entry, default priority is higher than for defaults or loaders Add object to the score with p...
Definition: scope.cpp:522
CSeq_annot_Handle AddSeq_annot(CSeq_annot &annot, TPriority pri=kPriority_Default, EExist action=eExist_Throw)
Add Seq-annot, return its CSeq_annot_Handle.
Definition: scope.cpp:538
void RemoveTopLevelSeqEntry(const CTSE_Handle &entry)
Revoke TSE previously added using AddTopLevelSeqEntry() or AddBioseq().
Definition: scope.cpp:376
@ eExist_Get
Definition: scope.hpp:260
@ kPriority_Default
Use default priority for added data.
Definition: scope.hpp:100
void Remove(void) const
Remove current annot.
const CTSE_Handle & GetTSE_Handle(void) const
CSeq_annot_EditHandle GetEditHandle(void) const
Get 'edit' version of handle.
void Reset(void)
Reset reference object.
Definition: ncbiobj.hpp:773
TObjectType & GetObject(void)
Get object.
Definition: ncbiobj.hpp:1011
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
E_Choice Which(void) const
Which variant is currently selected.
const TItem & GetItem(void) const
Get the Item member data.
void SetItem(TItem &value)
Assign a value to Item data member.
const TSubmit & GetSubmit(void) const
Get the variant data.
@ e_Obsolete_plugin
plugin message This is deprecated. DO NOT USE THIS FIELD. The original spec is: plugin PluginMessage....
@ e_Other
arbitrary object (use with care!)
@ e_Annot
annotation only
@ e_Id
for projects that simply import a far-pointer
@ e_Seq_align
seq-align and seq-align-set as registered items
@ e_Entry
for projects that contain something else
list< CRef< CSeq_align > > Tdata
const Tdata & Get(void) const
Get the member data.
TSet & SetSet(void)
Select the variant.
Definition: Seq_entry_.cpp:130
bool CanGetAnnot(void) const
Check if it is safe to call GetAnnot method.
TAnnot & SetAnnot(void)
Assign a value to Annot data member.
const TSet & GetSet(void) const
Get the variant data.
Definition: Seq_entry_.cpp:124
bool IsSet(void) const
Check if variant Set is selected.
Definition: Seq_entry_.hpp:263
TSeq & SetSeq(void)
Select the variant.
Definition: Seq_entry_.cpp:108
void SetData(TData &value)
Assign a value to Data data member.
Definition: Seq_annot_.cpp:244
bool CanGetAnnot(void) const
Check if it is safe to call GetAnnot method.
Definition: Bioseq_.hpp:360
TAnnot & SetAnnot(void)
Assign a value to Annot data member.
Definition: Bioseq_.hpp:372
const TId & GetId(void) const
Get the Id member data.
Definition: Bioseq_.hpp:290
list< CRef< CSeq_align > > TAlign
Definition: Seq_annot_.hpp:194
const TData & GetData(void) const
Get the Data member data.
Definition: Seq_annot_.hpp:873
list< CRef< CSeq_annot > > TAnnot
Definition: Bioseq_.hpp:97
E_Choice Which(void) const
Which variant is currently selected.
Definition: Seq_annot_.hpp:586
list< CRef< CSeq_entry > > TEntrys
list< CRef< CSeq_annot > > TAnnots
E_Choice Which(void) const
Which variant is currently selected.
const TData & GetData(void) const
Get the Data member data.
#define local
Definition: zutil.h:33
Modified on Tue Apr 23 07:38:14 2024 by modify_doxy.py rev. 669887