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

Go to the SVN repository for this file.

1 /* $Id: tax4blastsqlite.cpp 102252 2024-04-11 12:37:45Z camacho $
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 
27 /// @file tax4blastsqlite.cpp
28 /// @author Christiam Camacho
29 /// Implementation for the CTaxonomy4BlastSQLite class
30 
31 #include <ncbi_pch.hpp>
32 #include <corelib/ncbitime.hpp>
35 
37 
38 const string CTaxonomy4BlastSQLite::kDefaultName("taxonomy4blast.sqlite3");
39 
40 const char* kSQLQuery = R"(
41 WITH RECURSIVE descendants_from(taxid, parent) AS
42  (SELECT ?1, NULL
43  UNION ALL
44  SELECT T.taxid, T.parent
45  FROM descendants_from D, TaxidInfo T
46  WHERE D.taxid = T.parent)
47 SELECT taxid from descendants_from;
48 )";
49 
51 {
52  const string kFileName = dbname.empty() ? kDefaultName : dbname;
54  if (m_DbName.empty()) {
55  CNcbiOstrstream oss;
56  oss << "Database '" << kFileName << "' not found";
58  }
59 
67  m_DbConn.reset(new CSQLITE_Connection(m_DbName, opFlags));
68  x_SanityCheck();
69 }
70 
72 {
73  m_Statement.reset();
74  m_DbConn.reset();
75 }
76 
78 {
79  const map<string, string> kRequiredElements {
80  { "table", "TaxidInfo" },
81  { "index", "TaxidInfoCompositeIdx_parent" }
82  };
83  const string kSqlStmt =
84  "SELECT COUNT(*) FROM sqlite_master WHERE type=? and name=?;";
85  for (auto& [type, name] : kRequiredElements) {
86  unique_ptr<CSQLITE_Statement> s(new CSQLITE_Statement(&*m_DbConn, kSqlStmt));
87  s->Bind(1, type);
88  s->Bind(2, name);
89  s->Execute();
90  if (s->Step()) {
91  if (s->GetInt(0) != 1) {
92  CNcbiOstrstream oss;
93  oss << "Database '" << m_DbName << "' does not have " << type;
94  oss << " " << name << ". Please run the following command or ";
95  oss << "contact your system administrator to install it:" << endl;
96  oss << "update_blastdb.pl --decompress taxdb";
98  }
99  } else {
100  CNcbiOstrstream oss;
101  oss << "Failed to check for " << type << " " << name << " in '";
102  oss << m_DbName << "'";
104  }
105  }
106 }
107 
108 void CTaxonomy4BlastSQLite::GetLeafNodeTaxids(const int taxid, vector<int>& descendants)
109 {
110  descendants.clear();
111  if (taxid <= 0) return;
112 
113  if (!m_Statement) {
115  }
116  m_Statement->Reset();
117  m_Statement->ClearBindings();
118  m_Statement->Bind(1, taxid);
119  m_Statement->Execute();
120 
121  while (m_Statement->Step()) {
122  auto desc_taxid = m_Statement->GetInt(0);
123  if (desc_taxid != taxid)
124  descendants.push_back(desc_taxid);
125  }
126 }
127 
128 
130 
static const char * kFileName
CNcbiOstrstreamToString class helps convert CNcbiOstrstream to a string Sample usage:
Definition: ncbistre.hpp:802
Connection to SQLite database.
int TOperationFlags
Bit mask of EOperationFlags.
@ fExternalMT
Object and all statements and blobs created on top of it will not be used from different threads simu...
@ fJournalOff
Journaling is completely off (not recommended - transactions cannot be rollbacked unless they consist...
@ fSyncOff
Synchronization is off, database can be corrupted on OS crash or power outage.
@ fReadOnly
The DB is read-only. Also forces fVacuumOff flag.
@ fVacuumOff
Vacuuming is off, database file can only grow.
@ fTempToMemory
Mode of storing temporary data.
SQL statement executing on SQLite database.
CSeqDBException.
Definition: seqdbcommon.hpp:73
static const string kDefaultName
Default name for SQLite database (by default installed in $BLASTDB)
void x_SanityCheck()
Simple sanity check to validate the integrity of the database.
unique_ptr< CSQLITE_Statement > m_Statement
Most recent SQLite statement.
virtual void GetLeafNodeTaxids(const int taxid, vector< int > &descendants)
@inheritDoc
string m_DbName
SQLite3 database file name.
virtual ~CTaxonomy4BlastSQLite()
Destructor.
CTaxonomy4BlastSQLite(const string &dbname=kEmptyStr)
Parametrized constructor, uses its arguments to initialize database.
unique_ptr< CSQLITE_Connection > m_DbConn
Connection to SQLite engine.
static int type
Definition: getdata.c:31
#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
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
char * dbname(DBPROCESS *dbproc)
Get name of current database.
Definition: dblib.c:6929
Defines: CTimeFormat - storage class for time format.
Defines exception class and several constants for SeqDB.
string SeqDB_ResolveDbPath(const string &filename)
Resolve a file path using SeqDB's path algorithms.
Definition: type.c:6
const char * kSQLQuery
Modified on Fri Sep 20 14:58:09 2024 by modify_doxy.py rev. 669887