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

Go to the SVN repository for this file.

1 /* $Id: tax4blastsqlite.cpp 100250 2023-07-12 18:31:00Z 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 #ifndef SKIP_DOXYGEN_PROCESSING
32 static char const rcsid[] = "$Id: tax4blastsqlite.cpp 100250 2023-07-12 18:31:00Z camacho $";
33 #endif /* SKIP_DOXYGEN_PROCESSING */
34 
35 #include <ncbi_pch.hpp>
36 #include <corelib/ncbitime.hpp>
39 
41 
42 const string CTaxonomy4BlastSQLite::kDefaultName("taxonomy4blast.sqlite3");
43 
44 const char* kSQLQuery = R"(
45 WITH RECURSIVE descendants_from(taxid, parent) AS
46  (SELECT ?1, NULL
47  UNION ALL
48  SELECT T.taxid, T.parent
49  FROM descendants_from D, TaxidInfo T
50  WHERE D.taxid = T.parent)
51 SELECT taxid from descendants_from;
52 )";
53 
55 {
56  const string kFileName = dbname.empty() ? kDefaultName : dbname;
58  if (m_DbName.empty()) {
59  CNcbiOstrstream oss;
60  oss << "Database '" << kFileName << "' not found";
62  }
63 
71  m_DbConn.reset(new CSQLITE_Connection(m_DbName, opFlags));
72  x_SanityCheck();
73 }
74 
76 {
77  m_Statement.reset();
78  m_DbConn.reset();
79 }
80 
82 {
83  const map<string, string> kRequiredElements {
84  { "table", "TaxidInfo" },
85  { "index", "TaxidInfoCompositeIdx_parent" }
86  };
87  const string kSqlStmt =
88  "SELECT COUNT(*) FROM sqlite_master WHERE type=? and name=?;";
89  for (auto& [type, name] : kRequiredElements) {
90  unique_ptr<CSQLITE_Statement> s(new CSQLITE_Statement(&*m_DbConn, kSqlStmt));
91  s->Bind(1, type);
92  s->Bind(2, name);
93  s->Execute();
94  if (s->Step()) {
95  if (s->GetInt(0) != 1) {
96  CNcbiOstrstream oss;
97  oss << "Database '" << m_DbName << "' does not have " << type;
98  oss << " " << name << ". Please run the following command or ";
99  oss << "contact your system administrator to install it:" << endl;
100  oss << "update_blastdb.pl --decompress taxdb";
102  }
103  } else {
104  CNcbiOstrstream oss;
105  oss << "Failed to check for " << type << " " << name << " in '";
106  oss << m_DbName << "'";
108  }
109  }
110 }
111 
112 void CTaxonomy4BlastSQLite::GetLeafNodeTaxids(const int taxid, vector<int>& descendants)
113 {
114  descendants.clear();
115  if (taxid <= 0) return;
116 
117  if (!m_Statement) {
119  }
120  m_Statement->Reset();
121  m_Statement->ClearBindings();
122  m_Statement->Bind(1, taxid);
123  m_Statement->Execute();
124 
125  while (m_Statement->Step()) {
126  auto desc_taxid = m_Statement->GetInt(0);
127  if (desc_taxid != taxid)
128  descendants.push_back(desc_taxid);
129  }
130 }
131 
132 
134 
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
static char const rcsid[]
const char * kSQLQuery
Modified on Thu Feb 22 17:10:27 2024 by modify_doxy.py rev. 669887