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

Go to the SVN repository for this file.

1 /* $Id: gb_object_loader.cpp 47464 2023-04-20 00:19:10Z evgeniev $
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 
29 
30 #include <ncbi_pch.hpp>
31 
33 
34 #include <util/icanceled.hpp>
35 
37 
38 #include <gui/objutils/label.hpp>
40 
42 #include <objmgr/annot_ci.hpp>
43 
46 
47 CGBObjectLoader::CGBObjectLoader(const vector<CRef<CObject> >& ids, const vector<string>& NAs)
48  : m_Ids(ids), m_NAs(NAs)
49 {
50 }
51 
53 {
54  return m_Objects;
55 }
56 
58 {
59  auto n = m_Ids.size() + m_NAs.size();
60  string s = NStr::NumericToString(n) + (n == 1 ? " id" : " ids");
61  return "Loading " + s + " from GenBank";
62 }
63 
65 {
66  return true;
67 }
68 
70 {
72  CRef<CScope> scope(new CScope(*obj_mgr));
73  scope->AddDefaults();
74 
75  NON_CONST_ITERATE(vector<CRef<CObject> >, it, m_Ids) {
76  if (canceled.IsCanceled())
77  return false;
78 
79  string id_str;
80  try {
81  CSeq_id* seq_id = dynamic_cast<CSeq_id*> (it->GetPointer());
82  CSeq_loc* seq_loc = dynamic_cast<CSeq_loc*> (it->GetPointer());
83 
84  if (seq_id == 0 && seq_loc == 0) {
85  LOG_POST(Error << "CGBObjectLoader::Execute(): Invalid object type to open.");
86  continue;
87  }
88 
89  if (seq_id) {
90  id_str = seq_id->GetSeqIdString(true);
91  } else if (seq_loc) {
92  const CSeq_id* loc_id = seq_loc->GetId();
93  if (loc_id) {
94  id_str = loc_id->GetSeqIdString(true);
95  }
96  }
97 
98  if (seq_loc && seq_loc->IsInt()) {
99  CBioseq_Handle handle = scope->GetBioseqHandle(*seq_loc);
100  TSeqPos bioseqLength = handle.GetBioseqLength();
101  CSeq_interval& ival = seq_loc->SetInt();
102 
103  if (ival.GetFrom() < bioseqLength) {
104  if (ival.GetTo() > bioseqLength)
105  ival.SetTo(bioseqLength);
106  } else {
107  ival.SetFrom(0);
108  ival.SetTo(bioseqLength);
109  }
110  }
111 
112  string label;
113  CLabel::GetLabel(**it, &label, CLabel::eDefault, scope);
114  m_Objects.push_back(SObject(**it, label));
115  }
116  catch (const CException& e) {
117  x_UpdateHTMLResults(id_str, 0, e.GetMsg());
118  }
119  catch (const exception& e) {
120  x_UpdateHTMLResults(id_str, 0, e.what());
121  }
122  }
123 
124  for (const auto& na : m_NAs) {
125  if (canceled.IsCanceled())
126  return false;
127 
128  try {
130  CNAUtils::TGis gis;
131  CNAUtils::GetAllGIs(na, eedb, gis);
132  if (gis.empty()) {
133  x_UpdateHTMLResults(na, 0, "Failed to find associated sequences");
134  continue;
135  }
136 
137  CBioseq_Handle handle;
138 
139  for (auto gi : gis) {
140  CRef<CSeq_id> seqId(new CSeq_id());
141  seqId->SetGi(gi);
142  handle = scope->GetBioseqHandle(*seqId);
143  if (handle)
144  break;
145  }
146 
147  if (!handle) {
148  x_UpdateHTMLResults(na, 0, "Failed to load associated sequences");
149  continue;
150  }
151 
153  sel.AddNamedAnnots(na);
155 
156  CAnnot_CI annot_iter(handle, sel);
157  if (!annot_iter) {
158  x_UpdateHTMLResults(na, 0, "Failed to load annotation");
159  continue;
160  }
161 
162  CConstRef<CSeq_annot> annot = (*annot_iter).GetCompleteSeq_annot();
163  if (!annot) {
164  x_UpdateHTMLResults(na, 0, "Failed to load annotation");
165  continue;
166  }
167 
168  string label;
169  CLabel::GetLabel(*annot, &label, CLabel::eDefault, scope);
170  m_Objects.push_back(SObject(const_cast<CSeq_annot&>(*annot), label));
171  }
172  catch (const CException& e) {
173  x_UpdateHTMLResults(na, 0, e.GetMsg());
174  }
175  catch (const exception& e) {
176  x_UpdateHTMLResults(na, 0, e.what());
177  }
178  }
179 
180  return true;
181 }
182 
184 {
185  x_ShowErrorsDlg(wxT("GenBank import errors"));
186  return true;
187 }
188 
CAnnot_CI –.
Definition: annot_ci.hpp:59
CBioseq_Handle –.
void x_ShowErrorsDlg(const wxString &title)
void x_UpdateHTMLResults(const wxString &object, objects::ILineErrorListener *errCont, const string &exception="", const string &error_msg="", const wxString &objectName=wxT("File:"))
CScope –.
Definition: scope.hpp:92
Interface for testing cancellation request in a long lasting operation.
Definition: icanceled.hpp:51
vector< SObject > TObjects
Definition: set.hpp:45
bool empty() const
Definition: set.hpp:133
USING_SCOPE(objects)
unsigned int TSeqPos
Type for sequence locations and lengths.
Definition: ncbimisc.hpp:875
#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 char * what(void) const noexcept
Standard report (includes full backlog).
Definition: ncbiexpt.cpp:342
static objects::SAnnotSelector GetAnnotSelector(TAnnotFlags flags=0)
request an annotation selector for a given type
Definition: utils.cpp:168
static void GetAllGIs(const TNAA &naa, EEntrezDB eedb, TGis &gis, EUidsSource *pUidsSource=NULL)
Definition: na_utils.cpp:1068
EEntrezDB
eutils databases that can be used
Definition: na_utils.hpp:126
@ EEDB_All
try both nucleotide and protein databases, merge results
Definition: na_utils.hpp:130
CGBObjectLoader(const vector< CRef< CObject > > &ids, const vector< string > &NAs)
virtual bool PreExecute()
virtual bool PostExecute()
vector< CRef< CObject > > m_Ids
virtual TObjects & GetObjects()
virtual bool Execute(ICanceled &canceled)
vector< string > m_NAs
virtual string GetDescription() const
static void GetLabel(const CObject &obj, string *label, ELabelType type=eDefault)
Definition: label.cpp:140
@ eDefault
Definition: label.hpp:73
string GetSeqIdString(bool with_version=false) const
Return seqid string with optional version for text seqid type.
Definition: Seq_id.cpp:2145
void SetInt(TInt &v)
Definition: Seq_loc.hpp:983
const CSeq_id * GetId(void) const
Get the id of the location return NULL if has multiple ids or no id at all.
Definition: Seq_loc.hpp:941
static CRef< CObjectManager > GetInstance(void)
Return the existing object manager or create one.
CBioseq_Handle GetBioseqHandle(const CSeq_id &id)
Get bioseq handle by seq-id.
Definition: scope.cpp:95
void AddDefaults(TPriority pri=kPriority_Default)
Add default data loaders from object manager.
Definition: scope.cpp:504
TSeqPos GetBioseqLength(void) const
SAnnotSelector & IncludeNamedAnnotAccession(const string &acc, int zoom_level=0)
SAnnotSelector & AddNamedAnnots(const CAnnotName &name)
Add named annot to set of annots names to look for.
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
virtual bool IsCanceled(void) const =0
static enable_if< is_arithmetic< TNumeric >::value||is_convertible< TNumeric, Int8 >::value, string >::type NumericToString(TNumeric value, TNumToStringFlags flags=0, int base=10)
Convert numeric value to string.
Definition: ncbistr.hpp:673
static const char label[]
void SetTo(TTo value)
Assign a value to To data member.
TFrom GetFrom(void) const
Get the From member data.
void SetFrom(TFrom value)
Assign a value to From data member.
TGi & SetGi(void)
Select the variant.
Definition: Seq_id_.hpp:896
TTo GetTo(void) const
Get the To member data.
bool IsInt(void) const
Check if variant Int is selected.
Definition: Seq_loc_.hpp:528
yy_size_t n
#define wxT(x)
Definition: muParser.cpp:41
The Object manager core.
SAnnotSelector –.
Modified on Fri Sep 20 14:57:20 2024 by modify_doxy.py rev. 669887