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

Go to the SVN repository for this file.

1 /* $Id: tax_id_helper.cpp 47479 2023-05-02 13:24:02Z ucko $
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 
34 #include <corelib/ncbi_system.hpp>
35 
39 
41 
44 
45 #include <wx/ctrlsub.h>
46 
48 
52 
54  : m_LocalTaxonsLoaded(false)
55  , m_LocalJobId(CAppJobDispatcher::eInvalidJobID)
56  , m_NetTaxonsLoaded(false)
57  , m_NetJobId(CAppJobDispatcher::eInvalidJobID)
58 {
59 }
60 
62 {
63  x_CancelJobs();
64 }
65 
67 {
68  static CRef<CTaxIdHelper> s_Storage;
69  if (!s_Storage)
70  s_Storage.Reset(new CTaxIdHelper());
71  return *s_Storage;
72 }
73 
74 void CTaxIdHelper::x_FillControl(wxControlWithItems& ctrl, const TTaxonVector& data, int selectTaxId)
75 {
76  if (data.empty()) {
77  ctrl.Append(wxT("none"), (void*)0);
78  ctrl.SetSelection(0);
79  return;
80  }
81 
82  wxArrayString items;
83  vector<void*> itemData;
84 
85  items.push_back(wxT("none"));
86  itemData.push_back((void*)0);
87 
88  int selectIndex = 0;
89  for (size_t i = 0; i < data.size(); ++i) {
90  if (data[i].m_Id == selectTaxId)
91  selectIndex = static_cast<int>(i + 1);
92  items.push_back(data[i].m_Label);
93  itemData.push_back((void*)(intptr_t)data[i].m_Id);
94  }
95  ctrl.Append(items, &itemData[0]);
96  ctrl.SetSelection(selectIndex);
97 }
98 
100 {
101  if (!callback) return;
102 
103  auto it = m_Callbacks.begin();
104  for (; it != m_Callbacks.end(); ++it) {
105  if (*it == callback)
106  break;
107  }
108  if (it == m_Callbacks.end())
109  m_Callbacks.push_back(callback);
110 }
111 
113 {
114  if (!callback) return;
115 
116  for (auto it = m_Callbacks.begin(); it != m_Callbacks.end();) {
117  it = (*it == callback) ? m_Callbacks.erase(it) : ++it;
118  }
119 }
120 
122 {
123  for (auto cb : m_Callbacks)
124  cb->TaxonsLoaded(local);
125 }
126 
127 namespace
128 {
129  class CTaxonDataResult : public CObject
130  {
131  public:
132  CTaxonDataResult() {}
133  virtual ~CTaxonDataResult() {}
134  CTaxIdHelper::TTaxonVector& SetData() { return m_Data; }
135  const CTaxIdHelper::TTaxonVector& GetData() { return m_Data; }
136  private:
138  };
139 
140  class CLocalTaxonDataJob : public CJobCancelable
141  {
142  public:
143  CLocalTaxonDataJob(bool local) : m_Local(local) {}
144  virtual ~CLocalTaxonDataJob() {}
145 
146  /// @name IAppJob implementation
147  /// @{
148  virtual string GetDescr() const { return "Loading taxon data"; }
149  virtual EJobState Run();
151  virtual CRef<CObject> GetResult() { return CRef<CObject>(m_Result.GetPointer()); }
152  virtual CConstIRef<IAppJobError> GetError() { return CConstIRef<IAppJobError>(m_Error.GetPointer()); }
153  /// @}
154 
155  protected:
156  virtual string x_GetJobName() const { return "TaxonDataJob"; }
157 
158  private:
159  bool m_Local;
160  CRef<CObject> m_Result;
161  CRef<CAppJobError> m_Error;
162  };
163 } // namespace
164 
165 
167 {
168  CAppJobNotification* notn = dynamic_cast<CAppJobNotification*>(evt);
169  _ASSERT(notn);
170  if (!notn) return;
171 
172  CAppJobDispatcher::TJobID jobId = notn->GetJobID();
174 
175  switch (notn->GetState()) {
176  case IAppJob::eCompleted:
177  result.Reset(dynamic_cast<CTaxonDataResult*>(notn->GetResult().GetPointerOrNull()));
178  if (result)
179  x_JobCompleted(jobId, result->GetData());
180  else
181  x_JobFailed(jobId);
182  break;
183  case IAppJob::eFailed:
184  x_JobFailed(jobId);
185  break;
186  case IAppJob::eCanceled:
187  x_JobCanceled(jobId);
188  break;
189  default:
190  break;
191  }
192 }
193 
195 {
196  bool local = true;
197  if (jobId == m_LocalJobId) {
199  }
200  else if (jobId == m_NetJobId) {
202  local = false;
203  }
204  else
205  return;
206 
207  bool& loaded = local ? m_LocalTaxonsLoaded : m_NetTaxonsLoaded;
209 
210  loaded = true;
211  data = result;
213 }
214 
216 {
217  if (jobId == m_LocalJobId) {
219  x_DoCallbacks(true);
220  }
221  else if (jobId == m_NetJobId) {
223  x_DoCallbacks(false);
224  }
225 }
226 
228 {
229  if (jobId == m_LocalJobId)
231  else if (jobId == m_NetJobId)
233 }
234 
236 {
240  }
241 
245  }
246 }
247 
248 bool CTaxIdHelper::FillControl(ICallback* callback, wxControlWithItems& ctrl, bool local, int selectTaxId)
249 {
250  bool& loaded = local ? m_LocalTaxonsLoaded : m_NetTaxonsLoaded;
253 
254  ctrl.Clear();
255  x_AddCallback(callback);
256 
257  if (loaded) {
258  x_FillControl(ctrl, data, selectTaxId);
259  return true;
260  }
261  else {
262  if (jobId == CAppJobDispatcher::eInvalidJobID) {
263  jobId = CAppJobDispatcher::GetInstance().StartJob(*new CLocalTaxonDataJob(local), "ObjManagerEngine", *this, -1, true);
264  if (jobId == CAppJobDispatcher::eInvalidJobID) {
265  x_FillControl(ctrl, data, selectTaxId);
266  loaded = true;
267  return true;
268  }
269  }
270  return false;
271  }
272 }
273 
275 {
276  bool& loaded = local ? m_LocalTaxonsLoaded : m_NetTaxonsLoaded;
278 
279  bool reload = loaded;
280  if (jobId != CAppJobDispatcher::eInvalidJobID) {
283  reload = true;
284  }
285 
286  loaded = false;
287 
288  if (reload) {
289  jobId = CAppJobDispatcher::GetInstance().StartJob(*new CLocalTaxonDataJob(local), "ObjManagerEngine", *this, -1, true);
290  if (jobId == CAppJobDispatcher::eInvalidJobID) {
291  loaded = true;
293  }
294  }
295 }
296 
297 CLocalTaxonDataJob::EJobState CLocalTaxonDataJob::Run()
298 {
299  string errMsg;
300  CRef<CTaxonDataResult> result(new CTaxonDataResult());
301  m_Result.Reset(result.GetPointer());
302 
303  vector<int> ids;
304  if (m_Local) {
306  while (storage.IsDownloading()) {
307  if (IsCanceled())
308  return eCanceled;
309  SleepMilliSec(2000);
310  }
311  storage.GetTaxIds(ids);
312  }
313  else {
314  try {
317  ids.reserve(data.size());
318  for_each(data.begin(), data.end(), [&ids](int id) {
319  if (id != 0) ids.push_back(id);
320  });
321  } NCBI_CATCH("CBlastServices::GetTaxIdWithWindowMaskerSupport()");
322  }
323  if (IsCanceled()) return eCanceled;
324  if (ids.empty()) return eCompleted;
325 
326  sort(ids.begin(), ids.end(), [](int i1, int i2) -> bool
327  {
328  if (i2 == 9606) return false;
329  if (i1 == 9606) return true;
330  if (i2 == 10090) return false;
331  if (i1 == 10090) return true;
332  return i1 < i2;
333  });
334 
336  cache.Initialize(ids);
337 
338  CTaxIdHelper::TTaxonVector& data = result->SetData();
339 
340  for (auto i : ids) {
341  if (IsCanceled())
342  return eCanceled;
343 
344  wxString label = wxString::Format(wxT("%6d"), i);
345  string name = cache.GetLabel(i);
346 
347  if (!name.empty()) {
348  label += wxT(" ") + ToWxString(name);
349  }
350  data.push_back(CTaxIdHelper::STaxonData(i, label));
351  }
352 
353  // Close connection to taxon service. Action optional, but useful for better
354  // resource utilization
355  //
356  cache.ResetConnection();
357 
358  return eCompleted;
359 }
360 
Declares the CBlastServices class.
#define false
Definition: bool.h:36
CAppJobDispatcher.
CAppJobNotification Notification send by CAppJobEventTranslator.
API for Remote Blast Services.
CEventHandler.
CEvent - generic event implementation TODO TODO - Attachments.
Definition: event.hpp:86
Base class to build jobs with cancel functionality.
CObject –.
Definition: ncbiobj.hpp:180
CRef –.
Definition: ncbiobj.hpp:618
TTaxonVector m_LocalTaxons
CAppJobDispatcher::TJobID m_NetJobId
CAppJobDispatcher::TJobID m_LocalJobId
void x_OnJobNotification(CEvent *evt)
vector< STaxonData > TTaxonVector
bool FillControl(ICallback *callback, wxControlWithItems &ctrl, bool local, int selectTaxId)
void x_AddCallback(ICallback *callback)
void x_DoCallbacks(bool local)
void Reset(bool local=true)
TTaxonVector m_NetTaxons
void x_JobCanceled(CAppJobDispatcher::TJobID jobId)
void x_JobCompleted(CAppJobDispatcher::TJobID jobId, const TTaxonVector &result)
list< ICallback * > m_Callbacks
void x_DeleteCallback(ICallback *callback)
static CTaxIdHelper & GetInstance()
void x_JobFailed(CAppJobDispatcher::TJobID jobId)
bool m_LocalTaxonsLoaded
static void x_FillControl(wxControlWithItems &ctrl, const TTaxonVector &data, int selectTaxId)
void Initialize(const vector< int > &tax_ids)
void ResetConnection()
Drop connection to NCBI taxon service.
static CTaxonCache & GetInstance()
string GetLabel(int tax_id)
void GetTaxIds(vector< int > &ids)
static CWinMaskerFileStorage & GetInstance()
access the application-wide singleton
bool IsDownloading() const
const_iterator begin() const
Definition: set.hpp:135
size_type size() const
Definition: set.hpp:132
const_iterator end() const
Definition: set.hpp:136
objects::CBlast4_get_windowmasked_taxids_reply::Tdata GetTaxIdWithWindowMaskerSupport()
Retrieve a list of NCBI taxonomy IDs for which there exists windowmasker masking data to support an a...
#define NCBI_CATCH(message)
Catch CExceptions as well This macro is deprecated - use *_X or *_XX variant instead of it.
Definition: ncbiexpt.hpp:580
virtual CConstIRef< IAppJobProgress > GetProgress()=0
return progress object, the function shall be synchronized internally.
static CAppJobDispatcher & GetInstance()
CRef< CObject > GetResult() const
returns non-null pointer only if Completed or Running and has temporary results available
virtual CConstIRef< IAppJobError > GetError()=0
Returns IAppJobError object describing internal error that caused the Job to fail.
virtual CRef< CObject > GetResult()=0
Returns the Job Result.
bool DeleteJob(TJobID job_id)
when a Job is deleted the listener is not notified
TJobID StartJob(IAppJob &job, const string &engine_name, IEngineParams *params=NULL)
Starts a Job on the specified engine in "passive mode" - no notifications or progress reports will be...
virtual string GetDescr() const =0
Returns a human readable description of the Job (optional)
#define ON_EVENT(type, id, handler)
virtual EJobState Run()=0
Function that does all the useful work, called by the Engine.
#define END_EVENT_MAP()
Ends definition of Command Map.
#define BEGIN_EVENT_MAP(thisClass, baseClass)
Begins definition of Command Map for CEventHandler-derived class.
TJobState GetState() const
@ eCanceled
Definition: app_job.hpp:91
@ eCompleted
Definition: app_job.hpp:89
@ eFailed
Definition: app_job.hpp:90
void Reset(void)
Reset reference object.
Definition: ncbiobj.hpp:773
TObjectType * GetPointerOrNull(void) THROWS_NONE
Get pointer value.
Definition: ncbiobj.hpp:986
int intptr_t
Definition: ncbitype.h:185
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
void Run(void)
Enter the main loop.
static const char label[]
int i
#define wxT(x)
Definition: muParser.cpp:41
constexpr auto sort(_Init &&init)
void SleepMilliSec(unsigned long ml_sec, EInterruptOnSignal onsignal=eRestartOnSignal)
Format
Definition: njn_ioutil.hpp:52
@ eCanceled
Request canceled.
#define _ASSERT
else result
Definition: token2.c:20
wxString ToWxString(const string &s)
Definition: wx_utils.hpp:173
#define local
Definition: zutil.h:33
Modified on Sun Mar 03 03:16:50 2024 by modify_doxy.py rev. 669887