src/sample/app/eutils/eutils_sample.cpp

Go to the documentation of this file.
00001 /*  $Id: eutils_sample.cpp 144153 2008-10-27 19:49:01Z vakatov $
00002  * ===========================================================================
00003  *
00004  *                            PUBLIC DOMAIN NOTICE
00005  *               National Center for Biotechnology Information
00006  *
00007  *  This software/database is a "United States Government Work" under the
00008  *  terms of the United States Copyright Act.  It was written as part of
00009  *  the author's official duties as a United States Government employee and
00010  *  thus cannot be copyrighted.  This software/database is freely available
00011  *  to the public for use. The National Library of Medicine and the U.S.
00012  *  Government have not placed any restriction on its use or reproduction.
00013  *
00014  *  Although all reasonable efforts have been taken to ensure the accuracy
00015  *  and reliability of the software and data, the NLM and the U.S.
00016  *  Government do not and cannot warrant the performance or results that
00017  *  may be obtained by using this software or data. The NLM and the U.S.
00018  *  Government disclaim all warranties, express or implied, including
00019  *  warranties of performance, merchantability or fitness for any particular
00020  *  purpose.
00021  *
00022  *  Please cite the author in any work or product based on this material.
00023  *
00024  * ===========================================================================
00025  *
00026  * Author:  Aleksey Grichenko
00027  *
00028  * File Description:
00029  *   Example of using e-utils
00030  *
00031  */
00032 
00033 #include <ncbi_pch.hpp>
00034 
00035 #include <corelib/ncbiapp.hpp>
00036 #include <corelib/ncbienv.hpp>
00037 #include <corelib/ncbiargs.hpp>
00038 
00039 #include <objects/seq/Bioseq.hpp>
00040 #include <objects/seqset/Seq_entry.hpp>
00041 #include <objects/seqset/Bioseq_set.hpp>
00042 
00043 #include <objtools/eutils/api/eutils.hpp>
00044 #include <objtools/eutils/api/einfo.hpp>
00045 #include <objtools/eutils/api/esearch.hpp>
00046 #include <objtools/eutils/api/egquery.hpp>
00047 #include <objtools/eutils/api/efetch.hpp>
00048 #include <objtools/eutils/api/epost.hpp>
00049 #include <objtools/eutils/api/elink.hpp>
00050 #include <objtools/eutils/api/esummary.hpp>
00051 #include <objtools/eutils/api/espell.hpp>
00052 #include <objtools/eutils/api/ehistory.hpp>
00053 #include <objtools/eutils/einfo/einfo__.hpp>
00054 #include <objtools/eutils/esearch/esearch__.hpp>
00055 #include <objtools/eutils/egquery/egquery__.hpp>
00056 #include <objtools/eutils/elink/elink__.hpp>
00057 #include <objtools/eutils/esummary/esummary__.hpp>
00058 #include <objtools/eutils/espell/espell__.hpp>
00059 #include <objtools/eutils/ehistory/ehistory__.hpp>
00060 
00061 
00062 /////////////////////////////////////////////////////////////////////////////
00063 //
00064 //  EUtils demo application
00065 //
00066 
00067 
00068 BEGIN_NCBI_SCOPE
00069 USING_SCOPE(NCBI_NS_NCBI::objects);
00070 
00071 
00072 class CEUtilsApp : public CNcbiApplication
00073 {
00074 public:
00075     virtual void Init(void);
00076     virtual int  Run (void);
00077     virtual void Exit(void);
00078 private:
00079     void CallEInfo(const CArgs& args);
00080     void CallESearch(const CArgs& args);
00081     void CallEPost(const CArgs& args);
00082     void CallEGQuery(const CArgs& args);
00083     void CallELink(const CArgs& args);
00084     void CallESummary(const CArgs& args);
00085     void CallEFetch(const CArgs& args);
00086     void CallESpell(const CArgs& args);
00087     void CallEHistory(const CArgs& args);
00088 
00089     CEFetch_Request* x_CreateLitRequest(const CArgs& args);
00090     CEFetch_Request* x_CreateSeqRequest(const CArgs& args);
00091     CEFetch_Request* x_CreateTaxRequest(const CArgs& args);
00092 
00093     CRef<CEUtils_ConnContext>& x_GetCtx(void);
00094     void x_SetHttpMethod(CEUtils_Request& req, const CArgs& args);
00095     void x_DumpRequest(CEUtils_Request& req);
00096 
00097     CRef<CEUtils_ConnContext> m_Ctx;
00098     bool                      m_DumpRequests;
00099 };
00100 
00101 
00102 CRef<CEUtils_ConnContext>& CEUtilsApp::x_GetCtx(void)
00103 {
00104     if ( !m_Ctx ) {
00105         m_Ctx.Reset(new CEUtils_ConnContext);
00106     }
00107     return m_Ctx;
00108 }
00109 
00110 
00111 void CEUtilsApp::Init(void)
00112 {
00113     // Prepare command line descriptions
00114     //
00115 
00116     // Create
00117     auto_ptr<CArgDescriptions> arg_desc(new CArgDescriptions);
00118 
00119     arg_desc->AddFlag("einfo", "Call einfo utility", true);
00120     arg_desc->AddFlag("efetch", "Call efetch utility", true);
00121     // arg_desc->SetDependency("einfo", CArgDescriptions::eExcludes, "efetch");
00122     arg_desc->AddFlag("esearch", "Call esearch utility", true);
00123     arg_desc->AddFlag("epost", "Call epost utility", true);
00124     arg_desc->AddFlag("elink", "Call elink utility", true);
00125     arg_desc->AddFlag("egquery", "Call egquery utility", true);
00126     arg_desc->AddFlag("esummary", "Call esummary utility", true);
00127     arg_desc->AddFlag("espell", "Call espell utility", true);
00128     arg_desc->AddFlag("ehistory", "Call ehistory utility", true);
00129 
00130     // Switch HTTP method
00131     arg_desc->AddDefaultKey("http", "Method",
00132         "HTTP method used to send requests",
00133         CArgDescriptions::eString, "post");
00134     arg_desc->SetConstraint("http", &(*new CArgAllow_Strings,
00135         "post", "get"));
00136 
00137     // Debug flag
00138     arg_desc->AddFlag("dump", "Print raw incoming data", true);
00139 
00140     // Context setup
00141     arg_desc->AddOptionalKey("webenv", "WebEnv", "Web environment",
00142         CArgDescriptions::eString);
00143     arg_desc->AddOptionalKey("query_key", "query_key", "Query key",
00144         CArgDescriptions::eString);
00145     arg_desc->AddOptionalKey("tool", "tool", "Client tool",
00146         CArgDescriptions::eString);
00147     arg_desc->AddOptionalKey("email", "email", "Client e-mail",
00148         CArgDescriptions::eString);
00149 
00150     // Arguments for all queries
00151     arg_desc->AddOptionalKey("db", "db", "Database name",
00152         CArgDescriptions::eString);
00153     arg_desc->AddOptionalKey("id", "id", "ID to fetch",
00154         CArgDescriptions::eString);
00155     arg_desc->AddOptionalKey("term", "term", "Term to search for",
00156         CArgDescriptions::eString);
00157     arg_desc->AddOptionalKey("retstart", "RetStart", "First item to fetch",
00158         CArgDescriptions::eInteger);
00159     arg_desc->SetConstraint("retstart", new CArgAllow_Integers(1, kMax_Int));
00160     arg_desc->AddOptionalKey("retmax", "RetMax", "Number of items to fetch",
00161         CArgDescriptions::eInteger);
00162     arg_desc->SetConstraint("retmax", new CArgAllow_Integers(1, kMax_Int));
00163     arg_desc->AddDefaultKey("retmode", "RetMode", "Data format",
00164         CArgDescriptions::eString, "text");
00165     arg_desc->SetConstraint("retmode", &(*new CArgAllow_Strings,
00166         "text", "xml", "asn", "html"));
00167     arg_desc->AddOptionalKey("rettype", "RetType", "Fetched data type",
00168         CArgDescriptions::eString);
00169     arg_desc->SetConstraint("rettype", &(*new CArgAllow_Strings,
00170         // Literature
00171         "uilist", "abstract", "citation", "medline", "full",
00172         // Sequence
00173         "native", "fasta", "gb", "gbc", "gbwithparts", "est", "gss",
00174         "gp", "gpc", "seqid", "acc", "chr", "flt", "rsr", "brief", "docset"));
00175     arg_desc->AddOptionalKey("reldate", "RelDate", "Age of date in days",
00176         CArgDescriptions::eInteger);
00177     // taxonomy only
00178     arg_desc->AddOptionalKey("report", "Report", "Taxonomy data format",
00179         CArgDescriptions::eString);
00180     arg_desc->SetConstraint("report", &(*new CArgAllow_Strings,
00181         "uilist", "brief", "docsum", "xml"));
00182     // esearch flag
00183     arg_desc->AddOptionalKey("usehistory", "UseHistory", "Use history",
00184         CArgDescriptions::eBoolean);
00185 
00186     // Dependencies
00187     // ESearch
00188     arg_desc->SetDependency("esearch", CArgDescriptions::eRequires, "db");
00189     arg_desc->SetDependency("esearch", CArgDescriptions::eRequires, "term");
00190     arg_desc->SetDependency("usehistory", CArgDescriptions::eRequires, "esearch");
00191     // EGQuery
00192     arg_desc->SetDependency("egquery", CArgDescriptions::eRequires, "term");
00193     // EFetch
00194     arg_desc->SetDependency("efetch", CArgDescriptions::eRequires, "db");
00195     arg_desc->SetDependency("epost", CArgDescriptions::eRequires, "db");
00196     arg_desc->SetDependency("epost", CArgDescriptions::eRequires, "id");
00197     // ELink
00198     arg_desc->SetDependency("elink", CArgDescriptions::eRequires, "db");
00199     // ESummary
00200     arg_desc->SetDependency("esummary", CArgDescriptions::eRequires, "db");
00201     // ESpell
00202     arg_desc->SetDependency("espell", CArgDescriptions::eRequires, "db");
00203     arg_desc->SetDependency("espell", CArgDescriptions::eRequires, "term");
00204 
00205     // elink arguments
00206     // dbfrom
00207     arg_desc->AddOptionalKey("dbfrom", "dbfrom", "origination db for elink",
00208         CArgDescriptions::eString);
00209     // cmd
00210     arg_desc->AddOptionalKey("cmd", "Command", "elink command",
00211         CArgDescriptions::eString);
00212     arg_desc->SetConstraint("cmd", &(*new CArgAllow_Strings,
00213         "prlinks", "llinks", "llinkslib", "lcheck", "ncheck",
00214         "neighbor", "neighbor_history", "acheck"));
00215     // linkname
00216     arg_desc->AddOptionalKey("linkname", "Linkname", "elink linkname",
00217         CArgDescriptions::eString);
00218     // holding
00219     arg_desc->AddOptionalKey("holding", "holding", "elink holding",
00220         CArgDescriptions::eString);
00221     // version
00222     arg_desc->AddOptionalKey("version", "Version", "elink DTD version",
00223         CArgDescriptions::eString);
00224 
00225     // Program description
00226     string prog_description =
00227         "Test loading data from EUtils";
00228     arg_desc->SetUsageContext(GetArguments().GetProgramBasename(),
00229                               prog_description, false);
00230 
00231     // Pass argument descriptions to the application
00232     SetupArgDescriptions(arg_desc.release());
00233 }
00234 
00235 
00236 void CEUtilsApp::x_SetHttpMethod(CEUtils_Request& req, const CArgs& args)
00237 {
00238     if ( args["http"].AsString() == "get" ) {
00239         req.SetRequestMethod(CEUtils_Request::eHttp_Get);
00240     }
00241 }
00242 
00243 
00244 void CEUtilsApp::x_DumpRequest(CEUtils_Request& req)
00245 {
00246     string data;
00247     req.Read(&data);
00248     cout << data << endl;
00249 }
00250 
00251 
00252 void CEUtilsApp::CallEInfo(const CArgs& args)
00253 {
00254     string db = args["db"] ? args["db"].AsString() : kEmptyStr;
00255     CEInfo_Request req(db, x_GetCtx());
00256     x_SetHttpMethod(req, args);
00257 
00258     cout << req.GetScriptName() << "?" << req.GetQueryString() << endl;
00259 
00260     if ( m_DumpRequests ) {
00261         x_DumpRequest(req);
00262         return;
00263     }
00264 
00265     CRef<einfo::CEInfoResult> result = req.GetEInfoResult();
00266     _ASSERT(result);
00267     cout << MSerial_Xml << *result << endl;
00268     if ( result->IsDbList() ) {
00269         ITERATE(einfo::CDbList::TDbName, it, result->GetDbList().GetDbName()) {
00270             req.SetDatabase(*it);
00271             cout << req.GetScriptName() << "?" << req.GetQueryString() << endl;
00272             CRef<einfo::CEInfoResult> db_info = req.GetEInfoResult();
00273             _ASSERT(db_info);
00274             cout << MSerial_Xml << *db_info << endl;
00275         }
00276     }
00277 }
00278 
00279 
00280 void CEUtilsApp::CallESearch(const CArgs& args)
00281 {
00282     // Prepare request
00283     CESearch_Request req(args["db"].AsString(), x_GetCtx());
00284     x_SetHttpMethod(req, args);
00285 
00286     req.SetTerm(args["term"].AsString());
00287     if ( args["usehistory"] ) {
00288         req.SetUseHistory(args["usehistory"].AsBoolean());
00289     }
00290     if ( args["reldate"] ) {
00291         req.SetRelDate(args["reldate"].AsInteger());
00292     }
00293 
00294     // Print query string
00295     cout << req.GetScriptName() << "?" << req.GetQueryString() << endl;
00296 
00297     if ( m_DumpRequests ) {
00298         x_DumpRequest(req);
00299         return;
00300     }
00301 
00302     // Get and show the results
00303     CRef<esearch::CESearchResult> result = req.GetESearchResult();
00304     _ASSERT(result);
00305     cout << MSerial_Xml << *result << endl;
00306     cout << "WebEnv=" << x_GetCtx()->GetWebEnv() << endl;
00307     cout << "query_key=" << x_GetCtx()->GetQueryKey() << endl;
00308 }
00309 
00310 
00311 void CEUtilsApp::CallEGQuery(const CArgs& args)
00312 {
00313     // Prepare request
00314     CEGQuery_Request req(x_GetCtx());
00315     x_SetHttpMethod(req, args);
00316 
00317     req.SetTerm(args["term"].AsString());
00318 
00319     // Print query string
00320     cout << req.GetScriptName() << "?" << req.GetQueryString() << endl;
00321 
00322     if ( m_DumpRequests ) {
00323         x_DumpRequest(req);
00324         return;
00325     }
00326 
00327     // Get and show the results
00328     CRef<egquery::CResult> result = req.GetResult();
00329     _ASSERT(result);
00330     cout << MSerial_Xml << *result << endl;
00331 }
00332 
00333 
00334 void CEUtilsApp::CallEPost(const CArgs& args)
00335 {
00336     // Prepare request
00337     CEPost_Request req(args["db"].AsString(), x_GetCtx());
00338     x_SetHttpMethod(req, args);
00339 
00340     req.GetId().SetIds(args["id"].AsString());
00341 
00342     // Print query string
00343     cout << req.GetScriptName() << "?" << req.GetQueryString() << endl;
00344 
00345     if ( m_DumpRequests ) {
00346         x_DumpRequest(req);
00347         return;
00348     }
00349 
00350     // Get and show the results
00351     CRef<epost::CEPostResult> result = req.GetEPostResult();
00352     _ASSERT(result);
00353     cout << MSerial_Xml << *result << endl;
00354     cout << "WebEnv=" << x_GetCtx()->GetWebEnv() << endl;
00355     cout << "query_key=" << x_GetCtx()->GetQueryKey() << endl;
00356 }
00357 
00358 
00359 void CEUtilsApp::CallELink(const CArgs& args)
00360 {
00361     // Prepare request
00362     CELink_Request req(args["db"].AsString(), x_GetCtx());
00363     x_SetHttpMethod(req, args);
00364 
00365     if ( args["dbfrom"] ) {
00366         req.SetDbFrom(args["dbfrom"].AsString());
00367     }
00368     // If WebEnv was set (as an command line argument or by previous requests)
00369     // use it instead of id.
00370     if ( req.GetConnContext()->GetWebEnv().empty()  &&  args["id"] ) {
00371         req.GetIdGroups().SetGroups(args["id"].AsString());
00372     }
00373     if ( args["cmd"] ) {
00374         string cmd = args["cmd"].AsString();
00375         if (cmd == "prlinks") {
00376             req.SetCommand(CELink_Request::eCmd_prlinks);
00377         }
00378         else if (cmd == "llinks" ) {
00379             req.SetCommand(CELink_Request::eCmd_llinks);
00380         }
00381         else if (cmd == "llinkslib") {
00382             req.SetCommand(CELink_Request::eCmd_llinkslib);
00383         }
00384         else if (cmd == "lcheck") {
00385             req.SetCommand(CELink_Request::eCmd_lcheck);
00386         }
00387         else if (cmd == "ncheck") {
00388             req.SetCommand(CELink_Request::eCmd_ncheck);
00389         }
00390         else if (cmd == "neighbor") {
00391             req.SetCommand(CELink_Request::eCmd_neighbor);
00392         }
00393         else if (cmd == "neighbor_history") {
00394             req.SetCommand(CELink_Request::eCmd_neighbor_history);
00395         }
00396         else if (cmd == "acheck") {
00397             req.SetCommand(CELink_Request::eCmd_acheck);
00398         }
00399         else {
00400             ERR_POST(Error << "Unsupported elink command: " << cmd);
00401         }
00402     }
00403     if ( args["linkname"] ) {
00404         req.SetLinkName(args["linkname"].AsString());
00405     }
00406     if ( args["holding"] ) {
00407         req.SetHolding(args["holding"].AsString());
00408     }
00409     if ( args["version"] ) {
00410         req.SetHolding(args["version"].AsString());
00411     }
00412     if ( args["reldate"] ) {
00413         req.SetRelDate(args["reldate"].AsInteger());
00414     }
00415 
00416     // Print query string
00417     cout << req.GetScriptName() << "?" << req.GetQueryString() << endl;
00418 
00419     if ( m_DumpRequests ) {
00420         x_DumpRequest(req);
00421         return;
00422     }
00423 
00424     // Get and show the results
00425     CRef<elink::CELinkResult> result = req.GetELinkResult();
00426     _ASSERT(result);
00427     cout << MSerial_Xml << *result << endl;
00428 }
00429 
00430 
00431 void CEUtilsApp::CallESummary(const CArgs& args)
00432 {
00433     // Prepare request
00434     string db = args["db"] ? args["db"].AsString() : kEmptyStr;
00435     CESummary_Request req(db, x_GetCtx());
00436     x_SetHttpMethod(req, args);
00437 
00438     // If WebEnv was set (as an command line argument or by previous requests)
00439     // use it instead of id.
00440     if ( req.GetConnContext()->GetWebEnv().empty()  &&  args["id"] ) {
00441         req.GetId().SetIds(args["id"].AsString());
00442     }
00443     if ( args["retstart"] ) {
00444         req.SetRetStart(args["retstart"].AsInteger());
00445     }
00446     if ( args["retmax"] ) {
00447         req.SetRetMax(args["retmax"].AsInteger());
00448     }
00449 
00450     // Print query string
00451     cout << req.GetScriptName() << "?" << req.GetQueryString() << endl;
00452 
00453     if ( m_DumpRequests ) {
00454         x_DumpRequest(req);
00455         return;
00456     }
00457 
00458     // Get and show the results
00459     CRef<esummary::CESummaryResult> result = req.GetESummaryResult();
00460     _ASSERT(result);
00461     cout << MSerial_Xml << *result << endl;
00462 }
00463 
00464 
00465 void CEUtilsApp::CallESpell(const CArgs& args)
00466 {
00467     // Prepare request
00468     string db = args["db"] ? args["db"].AsString() : kEmptyStr;
00469     CESpell_Request req(db, x_GetCtx());
00470     x_SetHttpMethod(req, args);
00471 
00472     req.SetTerm(args["term"].AsString());
00473 
00474     // Print query string
00475     cout << req.GetScriptName() << "?" << req.GetQueryString() << endl;
00476 
00477     if ( m_DumpRequests ) {
00478         x_DumpRequest(req);
00479         return;
00480     }
00481 
00482     // Get and show the results
00483     CRef<espell::CESpellResult> result = req.GetESpellResult();
00484     _ASSERT(result);
00485     cout << MSerial_Xml << *result << endl;
00486 }
00487 
00488 
00489 void CEUtilsApp::CallEHistory(const CArgs& args)
00490 {
00491     // Prepare request
00492     string db;
00493     if ( args["db"] ) {
00494         db = args["db"].AsString();
00495     }
00496     CEHistory_Request req(db, x_GetCtx());
00497     x_SetHttpMethod(req, args);
00498 
00499     // Print query string
00500     cout << req.GetScriptName() << "?" << req.GetQueryString() << endl;
00501 
00502     if ( m_DumpRequests ) {
00503         x_DumpRequest(req);
00504         return;
00505     }
00506 
00507     // Get and show the results
00508     CRef<ehistory::CEHistoryResult> result = req.GetEHistoryResult();
00509     _ASSERT(result);
00510     cout << MSerial_Xml << *result << endl;
00511 }
00512 
00513 
00514 CEFetch_Request* CEUtilsApp::x_CreateLitRequest(const CArgs& args)
00515 {
00516     // Prepare literature request
00517     string db = args["db"].AsString();
00518     CEFetch_Literature_Request::ELiteratureDB ldb;
00519     if (db == "pubmed") {
00520         ldb = CEFetch_Literature_Request::eDB_pubmed;
00521     }
00522     else if (db == "pmc") {
00523         ldb = CEFetch_Literature_Request::eDB_pmc;
00524     }
00525     else if (db == "journals") {
00526         ldb = CEFetch_Literature_Request::eDB_journals;
00527     }
00528     else if (db == "omim") {
00529         ldb = CEFetch_Literature_Request::eDB_omim;
00530     }
00531     else {
00532         return 0;
00533     }
00534     auto_ptr<CEFetch_Literature_Request> lit_req(
00535         new CEFetch_Literature_Request(ldb, x_GetCtx()));
00536 
00537     string rettype = args["rettype"] ? args["rettype"].AsString() : kEmptyStr;
00538     if (rettype == "uilist") {
00539         lit_req->SetRetType(CEFetch_Literature_Request::eRetType_uilist);
00540     }
00541     else if (rettype == "abstract") {
00542         lit_req->SetRetType(CEFetch_Literature_Request::eRetType_abstract);
00543     }
00544     else if (rettype == "citation") {
00545         lit_req->SetRetType(CEFetch_Literature_Request::eRetType_citation);
00546     }
00547     else if (rettype == "medline") {
00548         lit_req->SetRetType(CEFetch_Literature_Request::eRetType_medline);
00549     }
00550     else if (rettype == "full") {
00551         lit_req->SetRetType(CEFetch_Literature_Request::eRetType_full);
00552     }
00553     else if ( !rettype.empty() ) {
00554         ERR_POST(Error << "Rettype " << rettype <<
00555             " is incompatible with the selected database " << db);
00556         return 0;
00557     }
00558 
00559     return lit_req.release();
00560 }
00561 
00562 
00563 CEFetch_Request* CEUtilsApp::x_CreateSeqRequest(const CArgs& args)
00564 {
00565     // Prepare sequence request
00566     string db = args["db"].AsString();
00567     CEFetch_Sequence_Request::ESequenceDB sdb;
00568     if (db == "gene") {
00569         sdb = CEFetch_Sequence_Request::eDB_gene;
00570     }
00571     else if (db == "genome") {
00572         sdb = CEFetch_Sequence_Request::eDB_genome;
00573     }
00574     else if (db == "nucleotide") {
00575         sdb = CEFetch_Sequence_Request::eDB_nucleotide;
00576     }
00577     else if (db == "nuccore") {
00578         sdb = CEFetch_Sequence_Request::eDB_nuccore;
00579     }
00580     else if (db == "nucest") {
00581         sdb = CEFetch_Sequence_Request::eDB_nucest;
00582     }
00583     else if (db == "nucgss") {
00584         sdb = CEFetch_Sequence_Request::eDB_nucgss;
00585     }
00586     else if (db == "protein") {
00587         sdb = CEFetch_Sequence_Request::eDB_protein;
00588     }
00589     else if (db == "popset") {
00590         sdb = CEFetch_Sequence_Request::eDB_popset;
00591     }
00592     else if (db == "snp") {
00593         sdb = CEFetch_Sequence_Request::eDB_snp;
00594     }
00595     else if (db == "sequences") {
00596         sdb = CEFetch_Sequence_Request::eDB_sequences;
00597     }
00598     else {
00599         return 0;
00600     }
00601     auto_ptr<CEFetch_Sequence_Request> seq_req(
00602         new CEFetch_Sequence_Request(sdb, x_GetCtx()));
00603 
00604     string rettype = args["rettype"] ? args["rettype"].AsString() : kEmptyStr;
00605     if (rettype == "native") {
00606         seq_req->SetRetType(CEFetch_Sequence_Request::eRetType_native);
00607     }
00608     else if (rettype == "fasta") {
00609         seq_req->SetRetType(CEFetch_Sequence_Request::eRetType_fasta);
00610     }
00611     else if (rettype == "gb") {
00612         seq_req->SetRetType(CEFetch_Sequence_Request::eRetType_gb);
00613     }
00614     else if (rettype == "gbc") {
00615         seq_req->SetRetType(CEFetch_Sequence_Request::eRetType_gbc);
00616     }
00617     else if (rettype == "gbwithparts") {
00618         seq_req->SetRetType(CEFetch_Sequence_Request::eRetType_gbwithparts);
00619     }
00620     else if (rettype == "est") {
00621         seq_req->SetRetType(CEFetch_Sequence_Request::eRetType_est);
00622     }
00623     else if (rettype == "gss") {
00624         seq_req->SetRetType(CEFetch_Sequence_Request::eRetType_gss);
00625     }
00626     else if (rettype == "gp") {
00627         seq_req->SetRetType(CEFetch_Sequence_Request::eRetType_gp);
00628     }
00629     else if (rettype == "gpc") {
00630         seq_req->SetRetType(CEFetch_Sequence_Request::eRetType_gpc);
00631     }
00632     else if (rettype == "seqid") {
00633         seq_req->SetRetType(CEFetch_Sequence_Request::eRetType_seqid);
00634     }
00635     else if (rettype == "acc") {
00636         seq_req->SetRetType(CEFetch_Sequence_Request::eRetType_acc);
00637     }
00638     else if (rettype == "chr") {
00639         seq_req->SetRetType(CEFetch_Sequence_Request::eRetType_chr);
00640     }
00641     else if (rettype == "flt") {
00642         seq_req->SetRetType(CEFetch_Sequence_Request::eRetType_flt);
00643     }
00644     else if (rettype == "rsr") {
00645         seq_req->SetRetType(CEFetch_Sequence_Request::eRetType_rsr);
00646     }
00647     else if (rettype == "brief") {
00648         seq_req->SetRetType(CEFetch_Sequence_Request::eRetType_brief);
00649     }
00650     else if (rettype == "docset") {
00651         seq_req->SetRetType(CEFetch_Sequence_Request::eRetType_docset);
00652     }
00653     else if ( !rettype.empty() ) {
00654         ERR_POST(Error << "Rettype " << rettype <<
00655             " is incompatible with the selected database " << db);
00656         return 0;
00657     }
00658 
00659     return seq_req.release();
00660 }
00661 
00662 
00663 CEFetch_Request* CEUtilsApp::x_CreateTaxRequest(const CArgs& args)
00664 {
00665     // Prepare taxonomy request
00666     string db = args["db"].AsString();
00667     if (db != "taxonomy") {
00668         return 0;
00669     }
00670     auto_ptr<CEFetch_Taxonomy_Request> tax_req(
00671         new CEFetch_Taxonomy_Request(x_GetCtx()));
00672 
00673     if ( args["report"] ) {
00674         string report = args["report"].AsString();
00675         if (report == "uilist") {
00676             tax_req->SetReport(CEFetch_Taxonomy_Request::eReport_uilist);
00677         }
00678         else if (report == "brief") {
00679             tax_req->SetReport(CEFetch_Taxonomy_Request::eReport_brief);
00680         }
00681         else if (report == "docsum") {
00682             tax_req->SetReport(CEFetch_Taxonomy_Request::eReport_docsum);
00683         }
00684         else if (report == "xml") {
00685             tax_req->SetReport(CEFetch_Taxonomy_Request::eReport_xml);
00686         }
00687         else {
00688             ERR_POST(Error << "Unsupported taxonomy report: " << report);
00689             return 0;
00690         }
00691     }
00692     return tax_req.release();
00693 }
00694 
00695 
00696 void CEUtilsApp::CallEFetch(const CArgs& args)
00697 {
00698     // Try to create literature request
00699     auto_ptr<CEFetch_Request> req(x_CreateLitRequest(args));
00700     if ( !req.get() ) {
00701         // Try to create sequence request
00702         req.reset(x_CreateSeqRequest(args));
00703     }
00704     if ( !req.get() ) {
00705         // Try to create taxonomy request
00706         req.reset(x_CreateTaxRequest(args));
00707     }
00708     if ( !req.get() ) {
00709         // the database is not related to any known request type
00710         ERR_POST(Error << "Can not connect to database "
00711             << args["db"].AsString() << " using the specified arguments.");
00712         return;
00713     }
00714     x_SetHttpMethod(*req, args);
00715 
00716     // If WebEnv was set (as an command line argument or by previous requests)
00717     // use it instead of id.
00718     if ( req->GetConnContext()->GetWebEnv().empty()  &&  args["id"] ) {
00719         req->GetId().SetIds(args["id"].AsString());
00720     }
00721 
00722     string retmode_str = args["retmode"].AsString();
00723     CEFetch_Request::ERetMode retmode = CEFetch_Request::eRetMode_none;
00724     if (retmode_str == "text") {
00725         retmode = CEFetch_Request::eRetMode_text;
00726     }
00727     else if (retmode_str == "xml") {
00728         retmode = CEFetch_Request::eRetMode_xml;
00729     }
00730     else if (retmode_str == "html") {
00731         retmode = CEFetch_Request::eRetMode_html;
00732     }
00733     else if (retmode_str == "asn") {
00734         retmode = CEFetch_Request::eRetMode_asn;
00735     }
00736     else {
00737         ERR_POST(Error << "Unknown retmode: " << retmode_str);
00738     }
00739     req->SetRetMode(retmode);
00740 
00741     cout << req->GetScriptName() << "?" << req->GetQueryString() << endl;
00742 
00743     if ( m_DumpRequests ) {
00744         x_DumpRequest(*req);
00745         return;
00746     }
00747 
00748     // efetch can return different object types, just print plain content
00749     string content;
00750     req->Read(&content);
00751     cout << content << endl;
00752 }
00753 
00754 
00755 int CEUtilsApp::Run(void)
00756 {
00757     // Process command line args
00758     const CArgs& args = GetArgs();
00759 
00760     m_DumpRequests = args["dump"];
00761 
00762     // Set connection context parameters
00763     if (args["webenv"]) {
00764         x_GetCtx()->SetWebEnv(args["webenv"].AsString());
00765     }
00766     if (args["query_key"]) {
00767         x_GetCtx()->SetQueryKey(args["query_key"].AsString());
00768     }
00769     if (args["tool"]) {
00770         x_GetCtx()->SetTool(args["tool"].AsString());
00771     }
00772     if (args["email"]) {
00773         x_GetCtx()->SetEmail(args["email"].AsString());
00774     }
00775 
00776     // Call the requested utils
00777     if ( args["einfo"] ) {
00778         CallEInfo(args);
00779     }
00780     if ( args["egquery"] ) {
00781         CallEGQuery(args);
00782     }
00783     if ( args["espell"] ) {
00784         CallESpell(args);
00785     }
00786     if ( args["esearch"] ) {
00787         CallESearch(args);
00788     }
00789     if ( args["epost"] ) {
00790         CallEPost(args);
00791     }
00792     if ( args["elink"] ) {
00793         CallELink(args);
00794     }
00795     if ( args["esummary"] ) {
00796         CallESummary(args);
00797     }
00798     if ( args["efetch"] ) {
00799         CallEFetch(args);
00800     }
00801     // EHistory is the last one - shows all other requests if any
00802     if ( args["ehistory"] ) {
00803         CallEHistory(args);
00804     }
00805     return 0;
00806 }
00807 
00808 
00809 void CEUtilsApp::Exit(void)
00810 {
00811 }
00812 
00813 
00814 END_NCBI_SCOPE
00815 
00816 
00817 /////////////////////////////////////////////////////////////////////////////
00818 //  MAIN
00819 
00820 
00821 USING_NCBI_SCOPE;
00822 
00823 int main(int argc, const char* argv[])
00824 {
00825     return CEUtilsApp().AppMain(argc, argv);
00826 }
00827 
00828 

Generated on Sun Dec 6 22:42:53 2009 for NCBI C++ ToolKit by  doxygen 1.4.6
Modified on Mon Dec 07 16:21:14 2009 by modify_doxy.py rev. 173732