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

Go to the SVN repository for this file.

1 /* $Id: genomic_collections_cli.cpp 91252 2020-09-30 20:45:01Z boukn $
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  * Author: Vinay Kumar
27  *
28  * File Description:
29  * .......
30  *
31  * Remark:
32  * This code was originally generated by application DATATOOL
33  * using the following specifications:
34  * 'gencoll_client.asn'.
35  */
36 
37 // standard includes
38 #include <ncbi_pch.hpp>
39 
40 // generated includes
52 #include <sstream>
54 #include <corelib/ncbiargs.hpp>
55 
56 // generated classes
57 
59 BEGIN_objects_SCOPE
60 
61 
62 CGCServiceException::CGCServiceException(const CDiagCompileInfo& diag, const objects::CGCClient_Error& srv_error)
64  CGCServiceException::EErrCode(srv_error.CanGetError_id() ? srv_error.GetError_id() : CException::eInvalid),
65  srv_error.GetDescription())
66 {}
67 
69 {
70  return CGCClient_Error::ENUM_METHOD_NAME(EError_id)()->FindName((int)GetErrCode(), true).c_str();
71 }
72 
73 static const STimeout kTimeout = {600, 0};
74 
76 {
78 
79  if(!cache_file.empty()) {
80  x_ConfigureCache(cache_file);
81  }
82 }
83 
85 {
87 
88  if (args.Exist("gc-cache") &&
89  args["gc-cache"].HasValue()) {
90  x_ConfigureCache(args["gc-cache"].AsString());
91  }
92 }
93 
95 {
97  SetRetryLimit(40);
98 
99  // it's a backward-compatibility fix for old versions of server (no much harm to leave it - only little data overhead is expected)
100  // always send request and get response in ASN text format so that server can properly parse request
101  // For binary ASN request: client\server versions of ASN request must be exactly the same (compiled using one ASN definition - strong typing)
102  // For text ASN request: client\server versions of ASN request can be different (use different ASN definitions - duck typing)
104  // SetFormat() - sets both Request and Response encoding, so we put "fo=text" as well (though not needed now it may be usefull in the future for the client back-compatibility)
105  SetArgs("fi=text&fo=text");
106 }
107 
108 
110 {
111  if(!m_CacheFile.empty()) {
112  m_CacheConn.reset(NULL);
114  }
115 }
116 
117 
118 template<typename TReq>
119 void LogRequest(const TReq& req)
120 {
121 #ifdef _DEBUG
122  ostringstream ostrstrm;
123  ostrstrm << "Making request -" << MSerial_AsnText << req;
124  ERR_POST(Info << ostrstrm.str());
125 #endif
126 }
127 
128 static void ValidateAsmAccession(const string& acc)
129 {
130  if(acc.find_first_not_of("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_.") != string::npos)
131  NCBI_THROW(CException, eUnknown, "Invalid accession format: " + acc);
132 }
133 
135 {
136  string acc = NStr::TruncateSpaces(acc_);
138 
139  if(!m_CacheFile.empty()) {
140  const string SltSql = "SELECT gc_blob FROM GetAssemblyBlob "
141  "WHERE acc_ver = ? AND mode = ?";
142  CSQLITE_Statement Stmt(m_CacheConn.get(), SltSql);
143  Stmt.Bind(1, acc);
144  Stmt.Bind(2, mode);
145  while(Stmt.Step()) {
146  const string Blob = Stmt.GetString(0);
147  CRef<CCachedAssembly> CAsm(new CCachedAssembly(Blob));
148  return CAsm->Assembly();
149  }
150  ERR_POST(Info << "Cache query for " << acc << " and " << mode << " found nothing, fallback to remote service.");
151  }
152 
153 
155  CGCClientResponse reply;
156 
157  req.SetAccession(acc);
158  req.SetMode(mode);
159 
160  LogRequest(req);
161 
162  try {
163  return CCachedAssembly(AskGet_assembly_blob(req, &reply)).Assembly();
164  } catch (CRPCClientException& e) {
165  if (reply.IsSrvr_error()) {
167  }
169  ERR_POST(Error << "Service failed to return result. Check applog for errors in gc_get_assembly_v3 and refresh_cache");
170  }
171  throw;
172  } catch (CException& ) {
173  if (reply.IsSrvr_error()) {
175  }
176  throw;
177  }
178 }
179 
181 {
183  CGCClientResponse reply;
184 
185  if(!m_CacheFile.empty()) {
186  const string SltSql = "SELECT gc_blob FROM GetAssemblyBlob "
187  "WHERE release_id = ? AND mode = ?";
188  CSQLITE_Statement Stmt(m_CacheConn.get(), SltSql);
189  Stmt.Bind(1, releaseId);
190  Stmt.Bind(2, mode);
191  while(Stmt.Step()) {
192  const string Blob = Stmt.GetString(0);
193  CRef<CCachedAssembly> CAsm(new CCachedAssembly(Blob));
194  return CAsm->Assembly();
195  }
196  }
197 
198  req.SetRelease_id(releaseId);
199  req.SetMode(mode);
200 
201  LogRequest(req);
202 
203  try {
204  return CCachedAssembly(AskGet_assembly_blob(req, &reply)).Assembly();
205  } catch (CRPCClientException& e) {
206  if (reply.IsSrvr_error()) {
208  }
210  ERR_POST(Error << "Service failed to return result. Check applog for errors in gc_get_assembly_v3 and refresh_cache");
211  }
212  throw;
213  } catch (CException& ) {
214  if (reply.IsSrvr_error()) {
216  }
217  throw;
218  }
219 }
220 
221 string CGenomicCollectionsService::ValidateChrType(const string& chrType, const string& chrLoc)
222 {
224  CGCClientResponse reply;
225  req.SetType(chrType);
226  req.SetLocation(chrLoc);
227 
228  LogRequest(req);
229 
230  int retry_counter=0;
231  const int RETRY_MAX = 3;
232  for(retry_counter=1; retry_counter <= RETRY_MAX; retry_counter++) {
233  try {
234  return AskGet_chrtype_valid(req, &reply);
235  } catch (const CException& e) {
236  if( retry_counter == RETRY_MAX) {
237  if (reply.IsSrvr_error())
239  throw e;
240  } else {
241  ERR_POST(Warning <<"Try "<<retry_counter<<":"<<e.GetMsg());
242  SleepSec(10);
243  }
244  } // end catch
245  } // end retry for
246 
247  if (reply.IsSrvr_error())
249  NCBI_THROW(CException, eUnknown, "ValidateChrType ran out of retries.");
250 }
251 
252 
254 {
255  CRef<CGCClient_AssemblySequenceInfo> asmseq_info(FindOneAssemblyBySequences(list<string>(1, sequence_acc), filter, sort));
256 
257  return asmseq_info ? CRef<CGCClient_AssemblyInfo>(&asmseq_info->SetAssembly()) : CRef<CGCClient_AssemblyInfo>();
258 }
259 
261 {
262  CRef<CGCClient_AssembliesForSequences> assm(x_FindAssembliesBySequences(sequence_acc, filter, sort, true, with_roles));
263 
264  return assm->CanGetAssemblies() && !assm->GetAssemblies().empty() ?
265  CRef<CGCClient_AssemblySequenceInfo>(assm->SetAssemblies().front()) :
267 }
268 
270 {
271  return FindAssembliesBySequences(list<string>(1, sequence_acc), filter, sort, with_roles);
272 }
273 
275 {
276  return x_FindAssembliesBySequences(sequence_acc, filter, sort, false, with_roles);
277 }
279 
280 {
282  CGCClientResponse reply;
283 
284  for(auto acc : sequence_acc)
285  if(acc.length() > 30) {
286  NCBI_THROW(CException, eUnknown, "Accession is longer than 30 characters: " + acc);
287  }
288 
289  req.SetSequence_acc().assign(sequence_acc.begin(), sequence_acc.end());
290  req.SetFilter(filter);
291  req.SetSort(sort);
292  req.SetTop_assembly_only(top_only ? 1 : 0);
293  if (with_roles) {
294  req.SetAdd_sequence_roles(true);
295  }
296 
297  LogRequest(req);
298 
299  int retry_counter=0;
300  const int RETRY_MAX = 3;
301  for(retry_counter=1; retry_counter <= RETRY_MAX; retry_counter++) {
302  try {
303  return AskGet_assembly_by_sequence(req, &reply);
304  } catch (const CException& e) {
305  if( retry_counter == RETRY_MAX) {
306  if (reply.IsSrvr_error())
308  throw e;
309  } else {
310  ERR_POST(Warning <<"Try "<<retry_counter<<":"<<e.GetMsg());
311  SleepSec(10);
312  }
313  } // end catch
314  } // end retry for
315 
316  if (reply.IsSrvr_error())
318  NCBI_THROW(CException, eUnknown, "FindAssembliesBySequences ran out of retries.");
319 }
320 
321 
323 {
325  CGCClientResponse reply;
326 
327  req.SetAccession(acc);
328  req.SetEquivalency(equivalency);
329 
330  LogRequest(req);
331 
332  int retry_counter=0;
333  const int RETRY_MAX = 3;
334  for(retry_counter=1; retry_counter <= RETRY_MAX; retry_counter++) {
335  try {
336  return AskGet_equivalent_assemblies(req, &reply);
337  } catch (const CException& e) {
338  if( retry_counter == RETRY_MAX) {
339  if (reply.IsSrvr_error())
341  throw e;
342  } else {
343  ERR_POST(Warning <<"Try "<<retry_counter<<":"<<e.GetMsg());
344  SleepSec(10);
345  }
346  } // end catch
347  } // end retry for
348 
349  if (reply.IsSrvr_error())
351  NCBI_THROW(CException, eUnknown, "GetEquivalentAssemblies ran out of retries.");
352 }
353 
354 
356 {
357  arg_desc.SetCurrentGroup("Assembly cache options");
358  arg_desc.AddOptionalKey("gc-cache", "gc_cache_file",
359  "Full path for local gencoll assembly cache", CArgDescriptions::eString);
360 }
361 
362 void CGenomicCollectionsService::x_ConfigureCache(const string& cache_file)
363 {
364  if(!cache_file.empty() && CFile(cache_file).Exists()) {
365  m_CacheFile = cache_file;
369  }
370 }
371 
372 END_objects_SCOPE
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.
User-defined methods of the data storage class.
User-defined methods of the data storage class.
CArgDescriptions –.
Definition: ncbiargs.hpp:541
CArgs –.
Definition: ncbiargs.hpp:379
CRef< objects::CGC_Assembly > Assembly()
Incapsulate compile time information such as __FILE__, __LINE__, NCBI_MODULE, current function.
Definition: ncbidiag.hpp:65
CFile –.
Definition: ncbifile.hpp:1605
CGCClientResponse –.
CGCClient_GetAssemblyBlobRequest –.
CGCClient_GetEquivalentAssembliesRequest –.
CGCClient_ValidateChrTypeLocRequest –.
const char * GetErrCodeString(void) const override
Get error code interpreted as text.
CGCServiceException(const CDiagCompileInfo &diag, const objects::CGCClient_Error &srv_error)
CRef< CGCClient_EquivalentAssemblies > GetEquivalentAssemblies(const string &acc, int equivalency)
CGenomicCollectionsService(const string &cache_file="")
unique_ptr< CSQLITE_Connection > m_CacheConn
static void AddArguments(CArgDescriptions &arg_desc)
CRef< CGCClient_AssembliesForSequences > FindAssembliesBySequences(const string &sequence_acc, int filter, CGCClient_GetAssemblyBySequenceRequest::ESort sort=CGCClient_GetAssemblyBySequenceRequest::eSort_default, bool with_roles=false)
Find assemblies by sequence accession.
CRef< CGC_Assembly > GetAssembly(const string &acc, const string &mode)
CRef< CGCClient_AssemblyInfo > FindOneAssemblyBySequences(const string &sequence_acc, int filter, CGCClient_GetAssemblyBySequenceRequest::ESort sort=CGCClient_GetAssemblyBySequenceRequest::eSort_default)
Find assembly by sequence accession.
string ValidateChrType(const string &chrType, const string &chrLoc)
CRef< CGCClient_AssembliesForSequences > x_FindAssembliesBySequences(const list< string > &sequence_acc, int filter, CGCClient_GetAssemblyBySequenceRequest::ESort sort, bool top_only, bool with_roles)
void x_ConfigureCache(const string &cache_file)
Connection to SQLite database.
@ fReadOnly
The DB is read-only. Also forces fVacuumOff flag.
static void Initialize(void)
Initialization of SQLite and tuning some default parameters.
static void Finalize(void)
Finish all SQLite operations.
SQL statement executing on SQLite database.
void Bind(int index, int val)
Bind integer value to parameter index.
bool Step(void)
Step through results of the statement.
string GetString(int col_ind) const
Get text value from column col_ind in current row.
static void ValidateAsmAccession(const string &acc)
static const STimeout kTimeout
void LogRequest(const TReq &req)
User-defined methods of the data storage class.
bool Exist(const string &name) const
Check existence of argument description.
Definition: ncbiargs.cpp:1813
void AddOptionalKey(const string &name, const string &synopsis, const string &comment, EType type, TFlags flags=0)
Add description for optional key without default value.
Definition: ncbiargs.cpp:2427
void SetCurrentGroup(const string &group)
Set current arguments group name.
Definition: ncbiargs.cpp:2632
@ eString
An arbitrary string.
Definition: ncbiargs.hpp:589
#define NULL
Definition: ncbistd.hpp:225
#define DIAG_COMPILE_INFO
Make compile time diagnostic information object to use in CNcbiDiag and CException.
Definition: ncbidiag.hpp:170
#define ERR_POST(message)
Error posting with file, line number information but without error codes.
Definition: ncbidiag.hpp:186
void Error(CExceptionArgs_Base &args)
Definition: ncbiexpt.hpp:1197
TErrCode GetErrCode(void) const
Get error code.
Definition: ncbiexpt.cpp:453
#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:704
const string & GetMsg(void) const
Get message string.
Definition: ncbiexpt.cpp:461
void Warning(CExceptionArgs_Base &args)
Definition: ncbiexpt.hpp:1191
EErrCode
Error types that an application can generate.
Definition: ncbiexpt.hpp:884
void Info(CExceptionArgs_Base &args)
Definition: ncbiexpt.hpp:1185
@ eUnknown
Definition: app_popup.hpp:72
void SetRetryLimit(unsigned int n)
void SetFormat(ESerialDataFormat fmt)
void SetArgs(const string &args)
Set additional connection arguments.
#define ENUM_METHOD_NAME(EnumName)
Definition: serialbase.hpp:994
#define MSerial_AsnText
I/O stream manipulators –.
Definition: serialbase.hpp:696
EIO_Status SetTimeout(const STimeout *timeout, EIO_Event direction=eIO_ReadWrite)
Definition: rpcbase.hpp:251
@ eFailed
Request (or retry) failed.
@ eSerial_AsnText
ASN.1 text.
Definition: serialdef.hpp:73
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
static string TruncateSpaces(const string &str, ETrunc where=eTrunc_Both)
Truncate whitespace in a string.
Definition: ncbistr.cpp:3177
void SetLocation(const TLocation &value)
Assign a value to Location data member.
virtual CRef< CGCClient_EquivalentAssemblies > AskGet_equivalent_assemblies(const CGCClient_GetEquivalentAssembliesRequest &req, TReply *reply=0)
void SetMode(const TMode &value)
Assign a value to Mode data member.
void SetFilter(TFilter value)
Assign a value to Filter data member.
void SetAccession(const TAccession &value)
Assign a value to Accession data member.
void SetAccession(const TAccession &value)
Assign a value to Accession data member.
TSequence_acc & SetSequence_acc(void)
Assign a value to Sequence_acc data member.
void SetTop_assembly_only(TTop_assembly_only value)
Assign a value to Top_assembly_only data member.
virtual vector< char > AskGet_assembly_blob(const CGCClient_GetAssemblyBlobRequest &req, TReply *reply=0)
void SetRelease_id(TRelease_id value)
Assign a value to Release_id data member.
void SetSort(TSort value)
Assign a value to Sort data member.
virtual CRef< CGCClient_AssembliesForSequences > AskGet_assembly_by_sequence(const CGCClient_GetAssemblyBySequenceRequest &req, TReply *reply=0)
void SetType(const TType &value)
Assign a value to Type data member.
virtual string AskGet_chrtype_valid(const CGCClient_ValidateChrTypeLocRequest &req, TReply *reply=0)
void SetEquivalency(TEquivalency value)
Assign a value to Equivalency data member.
bool IsSrvr_error(void) const
Check if variant Srvr_error is selected.
const TSrvr_error & GetSrvr_error(void) const
Get the variant data.
void SetAdd_sequence_roles(TAdd_sequence_roles value)
Assign a value to Add_sequence_roles data member.
constexpr auto sort(_Init &&init)
mdb_mode_t mode
Definition: lmdb++.h:38
void SleepSec(unsigned long sec, EInterruptOnSignal onsignal=eRestartOnSignal)
Sleep.
Defines command line argument related classes.
#define nullptr
Definition: ncbimisc.hpp:45
Timeout structure.
Definition: ncbi_types.h:76
Modified on Fri Sep 20 14:57:33 2024 by modify_doxy.py rev. 669887