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

Go to the SVN repository for this file.

1 /* $Id: entrez2client.cpp 91807 2020-12-14 15:03:53Z grichenk $
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: Mike DiCuccio
27  *
28  * File Description:
29  *
30  */
31 
32 #include <ncbi_pch.hpp>
33 #include <corelib/ncbiapp.hpp>
34 #include <corelib/ncbiargs.hpp>
35 #include <corelib/ncbienv.hpp>
36 #include <corelib/ncbistd.hpp>
37 
51 
52 #include <serial/objostrasn.hpp>
53 #include <serial/serial.hpp>
54 
55 
58 
59 
61 {
62 public:
64 
65  virtual void Init(void);
66  virtual int Run (void);
67 
68 private:
71  unique_ptr<CObjectOStream> m_ObjOstream;
72 
73  //
74  // handlers for specific requests
75  //
76 
79  const string& query, const string& db);
81  const string& query, const string& db);
83  const string& query, const string& db,
84  int start, int max_num);
86  const string& query, const string& db,
87  int start, int max_num);
89  const string& query, const string& db);
91  const string& query, const string& db);
93  const string& query, const string& db);
95  const string& query, const string& db);
97  const string& query, const string& db);
99  const string& query, const string& db);
100 
101  // format a reply (basic formatting only)
103 
104  // main internal query function
106  const string& query,
107  const string& db,
108  bool parse, bool uids,
109  int start = -1, int max_num = -1);
110 };
111 
112 
114 {
116  // Hide meaningless options so not to confuse the public
118 }
119 
120 
122 {
123  // Prepare command line descriptions
124  unique_ptr<CArgDescriptions> arg_desc(new CArgDescriptions);
125 
126  arg_desc->AddKey("query", "QueryString", "Query to submit",
128 
129  arg_desc->AddKey("db", "Database", "Name of database to query",
131 
132  arg_desc->AddDefaultKey("lt", "Lookup", "Type of lookup to perform",
133  CArgDescriptions::eString, "count");
134  arg_desc->SetConstraint("lt",
135  &(*new CArgAllow_Strings,
136  "info", // get-info request
137  "count", // eval-boolean: count only
138  "parse", // eval-boolean: parsed query
139  "uids", // eval-boolean: uid list
140  "docsum", // get docsums for our query
141  "termpos", // get term positions
142  "termhier", // get term hierarchy
143  "termlist", // get term list
144  "links", // get links
145  "linked", // get linked status
146  "linkct" // get link count
147  ));
148 
149  arg_desc->AddDefaultKey("start", "StartPos",
150  "Starting point in the UID list for retrieval",
152  arg_desc->AddDefaultKey("max", "MaxNum",
153  "Maximum number of records (or UIDs) to retrieve",
155 
156  arg_desc->AddDefaultKey("out", "OutputFile", "File to dump output to",
158 
159  arg_desc->SetUsageContext(GetArguments().GetProgramBasename(),
160  "Entrez2Client command-line demo application");
161 
162  // Pass argument descriptions to the application
163  SetupArgDescriptions(arg_desc.release());
164 }
165 
166 
168 {
169  const CArgs& args = GetArgs();
170 
171  string db = args["db"].AsString();
172  string query = args["query"].AsString();
173  string lt = args["lt"].AsString();
174  CNcbiOstream& ostr = args["out"].AsOutputFile();
175 
176  int start = args["start"].AsInteger();
177  int max_num = args["max"].AsInteger();
178 
180  m_Client->SetDefaultRequest().SetTool("Entrez2 Command-Line Test");
182  m_Ostream = &ostr;
183 
184  //
185  // process each lookup type
186  //
187 
188  if (lt == "info") {
189  LOG_POST(Info << "performing look-up type: get-info");
191  } else if (lt == "count") {
192  LOG_POST(Info << "performing look-up type: eval-boolean "
193  "(count only)");
194  x_GetCount(*m_Client, query, db);
195  } else if (lt == "parse") {
196  LOG_POST(Info << "performing look-up type: eval-boolean"
197  " (show parsed expression)");
199  } else if (lt == "uids") {
200  LOG_POST(Info << "performing look-up type: eval-boolean"
201  " (UID list)");
202  x_GetUids(*m_Client, query, db, start, max_num);
203  } else if (lt == "docsum") {
204  LOG_POST(Info << "performing look-up type: get-docsum");
205  x_GetDocsums(*m_Client, query, db, start, max_num);
206  } else if (lt == "termpos") {
207  LOG_POST(Info << "performing look-up type: get-term-pos");
208  x_GetTermPos(*m_Client, query, db);
209  } else if (lt == "termlist") {
210  LOG_POST(Info << "performing look-up type: get-term-list");
212  } else if (lt == "termhier") {
213  LOG_POST(Info << "performing look-up type: get-term-hierarchy");
215  } else if (lt == "links") {
216  LOG_POST(Info << "performing look-up type: get-links");
217  x_GetLinks(*m_Client, query, db);
218  } else if (lt == "linked") {
219  LOG_POST(Info << "performing look-up type: get-linked");
220  x_GetLinked(*m_Client, query, db);
221  } else if (lt == "linkct") {
222  LOG_POST(Info << "performing look-up type: get-link-counts");
224  } else {
225  LOG_POST(Error << "unrecognized lookup type: " << lt);
226  return 1;
227  }
228 
229  return 0;
230 }
231 
232 
233 //
234 // get-info
235 // returns information about active databases
236 //
238 {
239  CRef<CEntrez2_info> info = client.AskGet_info();
240  *m_ObjOstream << *info;
241 }
242 
243 
244 //
245 // display only the number fo records that match a query
246 //
248  const string& query, const string& db)
249 {
250  *m_Ostream << "query: " << query << endl;
252  false, false));
253  x_FormatReply(*reply);
254 }
255 
256 
257 //
258 // display only the number fo records that match a query
259 //
261  const string& query, const string& db,
262  int start, int max_num)
263 {
265  true, true,
266  start, max_num));
267  *m_Ostream << "query: " << query << endl;
268  x_FormatReply(*reply);
269 }
270 
271 
272 //
273 // display only the number fo records that match a query
274 //
276  const string& query,
277  const string& db)
278 {
280  true, false));
281  x_FormatReply(*reply);
282 }
283 
284 
285 //
286 // display docsums for a given query
287 //
289  const string& query,
290  const string& db,
291  int start, int max_num)
292 {
293  // retrieve our list of UIDs
295  false, true,
296  start, max_num));
297 
298  // retrieve the docsums
300  client.AskGet_docsum(reply->GetUids());
301 
302  *m_Ostream << reply->GetCount() << " records match" << endl;
303  *m_Ostream << "docsums:" << endl;
304  *m_Ostream << string(72, '-') << endl;
305  *m_ObjOstream << *docsums;
306  *m_Ostream << endl << string(72, '-') << endl;
307 }
308 
309 
310 //
311 // display term position for a given query
312 //
314  const string& /* query */,
315  const string& /* db */)
316 {
317  LOG_POST(Error << "get-term-pos query unimplemented");
318 }
319 
320 
321 //
322 // display term list for a given query
323 //
325  const string& /* query */,
326  const string& /* db */)
327 {
328  LOG_POST(Error << "get-term-list query unimplemented");
329 }
330 
331 
332 //
333 // display term hierarchy for a given query
334 //
336  const string& /* query */,
337  const string& /* db */)
338 {
339  LOG_POST(Error << "get-term-hierarchy query unimplemented");
340 }
341 
342 
343 //
344 // display links for a given query
345 //
347  const string& /* query */,
348  const string& /* db */)
349 {
350  LOG_POST(Error << "get-links query unimplemented");
351 }
352 
353 
354 //
355 // display linked status for a given query
356 //
358  const string& /* query */,
359  const string& /* db */)
360 {
361  LOG_POST(Error << "get-linked query unimplemented");
362 }
363 
364 
365 //
366 // display link counts for a given query
367 //
369  const string& query,
370  const string& db)
371 {
372  *m_Ostream << "query: " << query << endl;
373 
374  CEntrez2_id req;
375 
376  req.SetDb(CEntrez2_db_id(db));
377  req.SetUid(NStr::StringToNumeric<TEntrezId>(query));
378 
379  CRef<CEntrez2_link_count_list> reply = client.AskGet_link_counts(req);
380 
381  *m_Ostream << "Link counts: " << reply->GetLink_type_count() << endl;
382  ITERATE(CEntrez2_link_count_list::TLinks, it, reply->GetLinks()) {
383  *m_Ostream << " Type: " << string((*it)->GetLink_type()) << endl;
384  *m_Ostream << " Count: " << (*it)->GetLink_count() << endl;
385  }
386 }
387 
388 
389 //
390 // eval-boolean
391 // returns a count of the number of items that match
392 //
395  const string& query, const string& db,
396  bool parse, bool uids,
397  int start, int max_num)
398 {
400 
401  if (parse) {
402  req.SetReturn_parse(true);
403  }
404  if (uids) {
405  req.SetReturn_UIDs(true);
406  }
407  CEntrez2_boolean_exp& exp = req.SetQuery();
408 
409  if (start != -1) {
410  exp.SetLimits().SetOffset_UIDs(start);
411  }
412 
413  if (max_num != -1) {
414  exp.SetLimits().SetMax_UIDs(max_num);
415  }
416 
417  // set the database we're querying
418  exp.SetDb().Set(db);
419 
420  // set the query
422  elem->SetStr(query);
423  exp.SetExp().push_back(elem);
424 
425  CRef<CEntrez2_boolean_reply> reply = client.AskEval_boolean(req);
426  return reply.Release();
427 }
428 
429 
430 //
431 // x_FormatReply()
432 // dump results from the reply block
433 //
435 {
436  if (reply.CanGetCount()) {
437  *m_Ostream << reply.GetCount() << " uid(s) match" << endl;
438  }
439 
440  if (reply.IsSetQuery()) {
441  *m_Ostream << "parsed query:" << endl;
442  *m_Ostream << string(75, '-') << endl;
443  *m_ObjOstream << reply.GetQuery();
444  *m_Ostream << endl << string(75, '-') << endl;
445  }
446 
447  if (reply.IsSetUids()) {
448  *m_Ostream << "decoded UIDs:" << endl;
450  reply.GetUids().GetConstUidIterator();
451  for ( ; !iter.AtEnd(); ++iter) {
452  *m_Ostream << " " << *iter << endl;
453  }
454  }
455 }
456 
457 
458 
459 /////////////////////////////////////////////////////////////////////////////
460 // MAIN
461 
462 
463 int main(int argc, const char* argv[])
464 {
465  return CEntrez2ClientApp().AppMain(argc, argv);
466 }
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.
CArgAllow_Strings –.
Definition: ncbiargs.hpp:1641
CArgDescriptions –.
Definition: ncbiargs.hpp:541
CArgs –.
Definition: ncbiargs.hpp:379
virtual void Init(void)
Initialize the application.
CNcbiOstream * m_Ostream
void x_GetLinks(CEntrez2Client &client, const string &query, const string &db)
void x_FormatReply(CEntrez2_boolean_reply &reply)
void x_GetLinkCounts(CEntrez2Client &client, const string &query, const string &db)
CRef< CEntrez2Client > m_Client
void x_GetTermList(CEntrez2Client &client, const string &query, const string &db)
void x_GetDocsums(CEntrez2Client &client, const string &query, const string &db, int start, int max_num)
void x_GetTermPos(CEntrez2Client &client, const string &query, const string &db)
void x_GetParsedQuery(CEntrez2Client &client, const string &query, const string &db)
void x_GetTermHierarchy(CEntrez2Client &client, const string &query, const string &db)
void x_GetUids(CEntrez2Client &client, const string &query, const string &db, int start, int max_num)
void x_GetLinked(CEntrez2Client &client, const string &query, const string &db)
void x_GetCount(CEntrez2Client &client, const string &query, const string &db)
virtual int Run(void)
Run the application.
void x_GetInfo(CEntrez2Client &client)
unique_ptr< CObjectOStream > m_ObjOstream
CEntrez2_boolean_reply * x_EvalBoolean(CEntrez2Client &client, const string &query, const string &db, bool parse, bool uids, int start=-1, int max_num=-1)
CEntrez2_boolean_element –.
CEntrez2_boolean_exp –.
CEntrez2_boolean_reply –.
CEntrez2_db_id –.
CEntrez2_eval_boolean –.
TConstUidIterator GetConstUidIterator() const
CEntrez2_id –.
Definition: Entrez2_id.hpp:66
Include a standard set of the NCBI C++ Toolkit most basic headers.
USING_SCOPE(objects)
int main(int argc, const char *argv[])
USING_NCBI_SCOPE
void HideStdArgs(THideStdArgs hide_mask)
Set the hide mask for the Hide Std Flags.
Definition: ncbiapp.cpp:1312
virtual const CArgs & GetArgs(void) const
Get parsed command line arguments.
Definition: ncbiapp.cpp:305
int AppMain(int argc, const char *const *argv, const char *const *envp=0, EAppDiagStream diag=eDS_Default, const char *conf=NcbiEmptyCStr, const string &name=NcbiEmptyString)
Main function (entry point) for the NCBI application.
Definition: ncbiapp.cpp:819
virtual void SetupArgDescriptions(CArgDescriptions *arg_desc)
Setup the command line argument descriptions.
Definition: ncbiapp.cpp:1195
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
Definition: ncbimisc.hpp:815
void DisableArgDescriptions(TDisableArgDesc disable=fDisableStdArgs)
Definition: ncbiapp.cpp:1306
const CNcbiArguments & GetArguments(void) const
Get the application's cached unprocessed command-line arguments.
@ fHideLogfile
Hide log file description.
@ fHideDryRun
Hide dryrun description.
@ fHideConffile
Hide configuration file description.
@ fHideVersion
Hide version description.
@ fDisableStdArgs
(-logfile, -conffile, -version etc)
@ eString
An arbitrary string.
Definition: ncbiargs.hpp:589
@ eOutputFile
Name of file (must be writable)
Definition: ncbiargs.hpp:596
@ eInteger
Convertible into an integer number (int or Int8)
Definition: ncbiargs.hpp:592
string
Definition: cgiapp.hpp:687
#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
void Info(CExceptionArgs_Base &args)
Definition: ncbiexpt.hpp:1185
@ eSerial_AsnText
ASN.1 text.
Definition: serialdef.hpp:73
static CObjectOStream * Open(ESerialDataFormat format, CNcbiOstream &outStream, bool deleteOutStream)
Create serial object writer and attach it to an output stream.
Definition: objostr.cpp:126
void Reset(void)
Reset reference object.
Definition: ncbiobj.hpp:773
TObjectType * Release(void)
Release a reference to the object and return a pointer to the object.
Definition: ncbiobj.hpp:846
IO_PREFIX::ostream CNcbiOstream
Portable alias for ostream.
Definition: ncbistre.hpp:149
void SetDb(const TDb &value)
Assign a value to Db data member.
const TUids & GetUids(void) const
Get the Uids member data.
void SetTool(const TTool &value)
Assign a value to Tool data member.
const TQuery & GetQuery(void) const
Get the Query member data.
void SetReturn_parse(TReturn_parse value)
Assign a value to Return_parse data member.
bool CanGetCount(void) const
Check if it is safe to call GetCount method.
virtual TRequest & SetDefaultRequest(void)
bool IsSetUids(void) const
if uids requested Check if a value has been assigned to Uids data member.
void SetLimits(TLimits &value)
Assign a value to Limits data member.
void SetUid(TUid value)
Assign a value to Uid data member.
void SetReturn_UIDs(TReturn_UIDs value)
Assign a value to Return_UIDs data member.
list< CRef< CEntrez2_link_count > > TLinks
TExp & SetExp(void)
Assign a value to Exp data member.
TCount GetCount(void) const
Get the Count member data.
bool IsSetQuery(void) const
if parsed query requested Check if a value has been assigned to Query data member.
void SetQuery(TQuery &value)
Assign a value to Query data member.
void SetDb(const TDb &value)
Assign a value to Db data member.
static MDB_envinfo info
Definition: mdb_load.c:37
Defines the CNcbiApplication and CAppException classes for creating NCBI applications.
Defines command line argument related classes.
Defines unified interface to application:
static CNamedPipeClient * client
static string query
Modified on Wed Apr 17 13:10:34 2024 by modify_doxy.py rev. 669887