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

Go to the SVN repository for this file.

1 /* $Id: net_blast_job_descr.cpp 32541 2015-03-18 20:41:59Z katargir $
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: Andrey Yazhuk, Anatoliy Kuznetsov
27  *
28  */
29 
30 #include <ncbi_pch.hpp>
31 
33 
36 
37 #include <gui/objutils/label.hpp>
39 
43 
44 
48 
49 ///////////////////////////////////////////////////////////////////////////////
50 /// CNetBlastJobDescriptorLabelHandler
51 
52 // TODO this must be more flexible - extension point ?
53 
55 {
56 public:
57  void GetLabel(const CObject& obj, string* str,
58  CLabel::ELabelType type, objects::CScope* scope) const
59  {
60  const CNetBlastJobDescriptor* descr =
61  dynamic_cast<const CNetBlastJobDescriptor*>(&obj);
62  if (descr) {
63  switch (type) {
64  case CLabel::eUserType:
65  *str += "Net BLAST Job";
66  break;
67 
69  *str += kEmptyStr;
70  break;
71 
72  case CLabel::eType:
73  *str += "Net BLAST Job Descriptor";
74  break;
75 
78  case CLabel::eContent: {{
80 
81  *str += "Job Title: " + descr->GetJobTitle();
82  *str += ", State: " + descr->GetStateLabel(state);
83  string rid = descr->GetRID();
84  if( ! rid.empty()) {
85  *str += ", RID '" + rid + "'";
86  }
87  *str += descr->GetDescription();
88  break;
89  }}
90 
92  GetLabel(obj, str, CLabel::eUserType, scope);
93  *str += "\n";
94  GetLabel(obj, str, CLabel::eContent, scope);
95  break;
96  }}
97  default:
98  break;
99  }
100  }
101  }
102 };
103 
104 ///////////////////////////////////////////////////////////////////////////////
105 /// CNetBlastJobDescriptor
106 
108 : m_NetBlastDS(NULL),
109  m_State(eInvalidState),
110  m_Deleted(false)
111 {
112  // TODO this must be done using an extension point ?
113  string type = typeid(CNetBlastJobDescriptor).name();
114  if ( ! CLabel::HasHandler(type)) {
116  }
117  m_SubmitTime = time(0);
118 }
119 
120 
122 : m_NetBlastDS(&ds),
123  m_State(eInvalidState),
124  m_Deleted(false)
125 {
126  // TODO this must be done using an extension point ?
127  string type = typeid(CNetBlastJobDescriptor).name();
128  if ( ! CLabel::HasHandler(type)) {
130  }
131  m_SubmitTime = time(0);
132 }
133 
134 
136 {
137 }
138 
139 
141 {
142  static string s_labels[] =
143  { "Invalid", "Initial", "Submitted", "Completed", "Failed", "Retrieved", "Expired", "BUG" };
144  size_t i = (state <= eExpired) ? state + 1 : eExpired;
145  return s_labels[i];
146 }
147 
148 
150 {
151  return m_State;
152 }
153 
154 
156 {
157  CMutexGuard lock(m_Mutex);
158 
159  _ASSERT( ! m_RID.empty());
160  return m_RID;
161 }
162 
163 
165 {
166  CMutexGuard lock(m_Mutex);
167  return m_JobTitle;
168 }
169 
170 
172 {
173  CMutexGuard lock(m_Mutex);
174  return m_Description;
175 }
176 
177 
179 {
180  CMutexGuard lock(m_Mutex);
181  return m_Errors;
182 }
183 
184 
186 {
187  CMutexGuard lock(m_Mutex);
188  return m_SubmitTime;
189 }
190 
191 
193 {
194  CMutexGuard lock(m_Mutex);
195  if(m_Deleted != deleted) {
196  m_Deleted = deleted;
198  }
199 }
200 
201 
203 {
204  return m_Deleted;
205 }
206 
207 
209 {
210  CMutexGuard lock(m_Mutex);
211 
214 
215  m_NetBlastDS = &ds;
216 
217  m_JobTitle = params.GetJobTitle();
218  // TODO init description
219 
221 
222  _ASSERT(opts);
223 
224  m_RemoteBlast.Reset(new blast::CRemoteBlast(opts));
225  m_RemoteBlast->SetClientId("GBench2");
226  m_RemoteBlast->EnableDiskCacheUse();
227 
229  if (!dbs.Loaded()) {
230  string msg = "Cannot submit Net BLAST job - BLAST databases not loaded.";
232  }
233 
234  bool nuc_db = params.IsSubjNucInput();
235  const CBLASTDatabases::TDbMap& db_map = dbs.GetDbMap(nuc_db);
236  const string& db_path = params.GetCurrDatabase();
237  CBLASTDatabases::TDbMap::const_iterator it = db_map.find(db_path);
238  if (it == db_map.end()) {
239  string msg = "Cannot submit Net BLAST job - invalid database:\n ";
240  msg += db_path;
242  } else {
243  m_RemoteBlast->SetDatabase(it->second);
244  /*
245  CRef<objects::CBlast4_database_info> blast_db_info(it->second);
246  if (blast_db_info) {
247  string db_name = blast_db_info->GetDatabase().GetName();
248  m_RemoteBlast->SetDatabase(db_name);
249  }
250  else {
251  NCBI_THROW(CException, eUnknown, "Cannot submit Net BLAST job - no database info");
252  }
253  */
254  }
255 
256  string eq = params.GetEntrezQuery();
257  m_RemoteBlast->SetEntrezQuery(eq.c_str());
258 
259  m_RID = "";
260  m_State = eInitial;
261 }
262 
263 
265 {
266  CMutexGuard lock(m_Mutex);
267 
270 
271  m_NetBlastDS = &ds;
272 
273  //TODO init description
274  m_RID = rid;
275  m_State = eInitial;
276 }
277 
278 
280 {
281  CMutexGuard lock(m_Mutex);
282 
283  if(m_State == eInitial) {
285 
286  m_RemoteBlast->SetQueries(CRef<CBioseq_set>(&seq_set));
287 
288  if( m_RemoteBlast->Submit() ){
289  m_SubmitTime = time(0);
290  m_RID = m_RemoteBlast->GetRID();
292 
294  } else {
295  NCBI_THROW(CException, eUnknown, "Fail to submit Net BLAST job - " + m_RemoteBlast->GetErrors() );
296  }
297  } else {
298  NCBI_THROW(CException, eUnknown, "Cannot submit Net BLAST job - invalid state");
299  }
300 }
301 
302 
304 {
305  CMutexGuard lock(m_Mutex);
306 
307  if(m_State == eInitial || m_State == eSubmitted) {
308  EState old_state = m_State;
309 
311 
312  bool done = m_RemoteBlast->CheckDone();
313  m_Errors = m_RemoteBlast->GetErrors();
314 
315  if(m_Errors.empty()) {
316  if(done) {
317  /// job finished, results are ready, launch loading Task
319  }
320  // else - still pending
321  } else {
322  if(done) {
323  m_State = eFailed;
324  } else {
325  m_State = eExpired; // unknown or expired RID
326  }
327  }
328  if(m_State != old_state) {
330  }
331  } else {
332  NCBI_THROW(CException, eUnknown, "Cannot check Net BLAST job - invalid state");
333  }
334 }
335 
336 
338 {
339  CMutexGuard lock(m_Mutex);
340 
341  CRef<CSeq_align_set> results;
342 
343  if(m_State == eCompleted || m_State == eRetrieved) {
345 
346  results = m_RemoteBlast->GetAlignments();
348 
350  } else {
351  NCBI_THROW(CException, eUnknown, "Cannot retrieve Net BLAST job - invalid state");
352  }
353  return results;
354 }
355 
357 {
358  CMutexGuard lock(m_Mutex);
359 
361 
362  if(m_State == eCompleted || m_State == eRetrieved) {
364 
365  result = m_RemoteBlast;
367 
369  } else {
370  NCBI_THROW(CException, eUnknown, "Cannot retrieve Net BLAST job - invalid state");
371  }
372  return result;
373 }
374 
375 
377 {
378  CMutexGuard lock(m_Mutex);
379 
381 }
382 
383 
384 static const char* kBLASTJobTag = "BLAST Job";
385 static const char* kStateTag = "State";
386 static const char* kRIDTag = "RID";
387 static const char* kTitleTag = "Title";
388 static const char* kDescrTag = "Descr";
389 static const char* kErrorsTag = "Errors";
390 static const char* kSubmitTimeTag = "Submittime";
391 
392 
394 {
395  CRef<CUser_object> obj(new CUser_object());
396  obj->SetType().SetStr(kBLASTJobTag);
397 
398  obj->AddField(kStateTag, (int) m_State);
399  obj->AddField(kRIDTag, m_RID);
403 
404  CTime time(m_SubmitTime);
405  string s_time = time.AsString();
406  obj->AddField(kSubmitTimeTag, s_time);
407 
408  return obj.Release();
409 }
410 
411 
412 void CNetBlastJobDescriptor::FromUserObject(const objects::CUser_object& user_obj)
413 {
414  m_State = (EState) user_obj.GetField(kStateTag).GetData().GetInt();
415  m_RID = user_obj.GetField(kRIDTag).GetData().GetStr();
416  m_JobTitle = user_obj.GetField(kTitleTag).GetData().GetStr();
417  m_Description = user_obj.GetField(kDescrTag).GetData().GetStr();
418  m_Errors = user_obj.GetField(kErrorsTag).GetData().GetStr();
419 
420  string s_time = user_obj.GetField(kSubmitTimeTag).GetData().GetStr();
421  CTime time(s_time);
422  m_SubmitTime = time.GetTimeT();
423 
424  if( m_RID.empty() ){
426  }
427 }
428 
429 
431 {
432  if( ! m_RemoteBlast) {
433  _ASSERT( ! m_RID.empty());
434 
436  m_RemoteBlast->SetClientId("GBench2");
437  }
438 }
439 
440 
442 {
444 
445  if(m_NetBlastDS) {
447  }
448 }
449 
bool Loaded() const
CBLASTParams - save user-specified parameters and preferences.
time_t GetTimeT(void) const
Get time in time_t format.
Definition: ncbitime.cpp:1398
API for Remote Blast Requests.
Definition: dbpivot.c:60
bool IsSubjNucInput() const
void GetLabel(const CObject &obj, string *str, CLabel::ELabelType type, objects::CScope *scope) const
CRef< blast::CRemoteBlast > m_RemoteBlast
a proxy to Net BLAST that is used for updating Job state
static bool HasHandler(const CTypeInfo &type)
Definition: label.cpp:242
done
Definition: token1.c:1
void Submit(objects::CBioseq_set &seq_set)
void MarkDeleted(bool deleted)
CRef< objects::CSeq_align_set > Retrieve()
#define NULL
Definition: ncbistd.hpp:225
#define kEmptyStr
Definition: ncbistr.hpp:120
static const char * kDescrTag
int i
CNetBlastJobDescriptor.
CNetBLASTUIDataSource.
hash_map< string, string > TDbMap
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:101
string & GetCurrDatabase()
returns the current database for the current program
ELabelType
Definition: label.hpp:60
CNetBLASTUIDataSource * m_NetBlastDS
CNetBlastJobDescriptor()
CNetBlastJobDescriptor.
ILabelHandler interface.
Definition: label.hpp:134
void SetType(TType &value)
Assign a value to Type data member.
Definition: type.c:8
static const char * kErrorsTag
const TDbMap & GetDbMap(bool nuc)
string AsString(const CTimeFormat &format=kEmptyStr, TSeconds out_tz=eCurrentTimeZone) const
Transform time to string.
Definition: ncbitime.cpp:1514
objects::CUser_object * ToUserObject() const
void x_OnJobDescrChanged(CNetBlastJobDescriptor &descr)
callback - invoked by CNetBlastJobDescriptor
void Init(const CBLASTParams &params, CNetBLASTUIDataSource &ds)
static const char * kRIDTag
static string GetStateLabel(EState state)
CTime –.
Definition: ncbitime.hpp:290
string m_RID
Job attributes.
CNetBlastJobDescriptorLabelHandler.
static CBLASTDatabases & GetInstance()
TObjectType * Release(void)
Release a reference to the object and return a pointer to the object.
Definition: ncbiobj.hpp:819
CException –.
Definition: ncbiexpt.hpp:709
string & GetEntrezQuery()
static const char * str(char *buf, int n)
Definition: stats.c:84
CObject –.
Definition: ncbiobj.hpp:180
static const char * kStateTag
static const char * kBLASTJobTag
USING_SCOPE(objects)
else result
Definition: token2.c:20
static const char * kTitleTag
bool eq(T x_, T y_, T round_)
Definition: njn_approx.hpp:79
static const char * kSubmitTimeTag
#define _ASSERT
void FromUserObject(const objects::CUser_object &user_obj)
#define NCBI_THROW(exception_class, err_code, message)
Generic macro to throw an exception, given the exception class, error code and message string...
Definition: ncbiexpt.hpp:546
db_map template, mimics std::map<> using BerkeleyDB as the underlying Btree mechanism.
Definition: bdb_map.hpp:405
static void RegisterLabelHandler(const string &type, ILabelHandler &handler)
Definition: label.cpp:228
CRef< blast::CRemoteBlast > GetRemoteBlast()
CUser_object & AddField(const string &label, const string &value, EParseField parse=eParse_String)
add a data field to the user object that holds a given value
CRef< blast::CBlastOptionsHandle > ToBlastOptions(bool local=false) const
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:98
void Release()
release CRemoteBlast object to free associated resources
void Reset(void)
Reset reference object.
Definition: ncbiobj.hpp:746
Modified on Wed May 04 16:43:22 2016 by modify_doxy.py rev. 426318