OpenMS  2.8.0
OMSFileStore.h
Go to the documentation of this file.
1 // --------------------------------------------------------------------------
2 // OpenMS -- Open-Source Mass Spectrometry
3 // --------------------------------------------------------------------------
4 // Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
5 // ETH Zurich, and Freie Universitaet Berlin 2002-2021.
6 //
7 // This software is released under a three-clause BSD license:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of any author or any participating institution
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
16 // For a full list of authors, refer to the file AUTHORS.
17 // --------------------------------------------------------------------------
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
22 // INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // --------------------------------------------------------------------------
31 // $Maintainer: Hendrik Weisser $
32 // $Authors: Hendrik Weisser $
33 // --------------------------------------------------------------------------
34 
35 #pragma once
36 
40 
41 #include <QtSql/QSqlQuery>
42 
43 class QSqlError;
44 
45 namespace OpenMS
46 {
47  namespace Internal
48  {
57  bool tableExists_(const String& db_name, const String& table_name);
58 
71  void raiseDBError_(const QSqlError& error, int line,
72  const char* function, const String& context);
73 
74 
81  {
82  public:
83  using Key = qint64;
84 
96  OMSFileStore(const String& filename, LogType log_type);
97 
104 
106  void store(const IdentificationData& id_data);
107 
109  void store(const FeatureMap& features);
110 
111  private:
113 
114  void storeScoreTypes_(const IdentificationData& id_data);
115 
116  void storeInputFiles_(const IdentificationData& id_data);
117 
119 
121 
123 
125 
127 
129 
131 
133 
134  void storeAdducts_(const IdentificationData& id_data);
135 
137 
138  void storeFeatures_(const FeatureMap& features);
139 
140  void createTable_(const String& name, const String& definition,
141  bool may_exist = false);
142 
144 
146 
148 
150 
151  Key storeCVTerm_(const CVTerm& cv_term);
152 
153  void createTableMetaInfo_(const String& parent_table,
154  const String& key_column = "id");
155 
156  void storeMetaInfo_(const MetaInfoInterface& info, const String& parent_table,
157  Key parent_id);
158 
159  void createTableAppliedProcessingStep_(const String& parent_table);
160 
162  const IdentificationData::AppliedProcessingStep& step, Size step_order,
163  const String& parent_table, Key parent_id);
164 
166 
168 
170 
172  const IdentificationData::ParentMatches& matches, Key molecule_id);
173 
174  template<class MetaInfoInterfaceContainer>
175  void storeMetaInfos_(const MetaInfoInterfaceContainer& container,
176  const String& parent_table)
177  {
178  bool table_created = false;
179  QSqlQuery query; // prepare query only once and only if needed
180  for (const auto& element : container)
181  {
182  if (!element.isMetaEmpty())
183  {
184  if (!table_created)
185  {
186  createTableMetaInfo_(parent_table);
187  table_created = true;
188  }
189  storeMetaInfo_(element, parent_table, Key(&element));
190  }
191  }
192  }
193 
194  template<class ScoredProcessingResultContainer>
196  const ScoredProcessingResultContainer& container,
197  const String& parent_table)
198  {
199  bool table_created = false;
200  for (const auto& element : container)
201  {
202  if (!element.steps_and_scores.empty())
203  {
204  if (!table_created)
205  {
206  createTableAppliedProcessingStep_(parent_table);
207  table_created = true;
208  }
209  Size counter = 0;
211  element.steps_and_scores)
212  {
213  storeAppliedProcessingStep_(step, ++counter, parent_table,
214  Key(&element));
215  }
216  }
217  }
218  storeMetaInfos_(container, parent_table);
219  }
220 
221  void storeFeature_(const FeatureMap& features);
222 
224  const Feature& feature, int& feature_id, int parent_id);
225 
227  template <class FeatureContainer, class Predicate>
228  bool anyFeaturePredicate_(const FeatureContainer& features, const Predicate& pred)
229  {
230  if (features.empty()) return false;
231  for (const Feature& feature : features)
232  {
233  if (pred(feature)) return true;
234  if (anyFeaturePredicate_(feature.getSubordinates(), pred)) return true;
235  }
236  return false;
237  }
238 
239  void storeMapMetaData_(const FeatureMap& features);
240 
241  void storeDataProcessing_(const FeatureMap& features);
242 
243  // store name, not database connection itself (see https://stackoverflow.com/a/55200682):
244  QString db_name_;
245 
247  std::map<std::string, QSqlQuery> prepared_queries_;
248  };
249  }
250 }
Representation of controlled vocabulary term.
Definition: CVTerm.h:53
Class to hold strings, numeric values, lists of strings and lists of numeric values.
Definition: DataValue.h:59
A container for features.
Definition: FeatureMap.h:106
An LC-MS feature.
Definition: Feature.h:72
Representation of spectrum identification results and associated data.
Definition: IdentificationData.h:95
IdentificationDataInternal::ParentMatches ParentMatches
Definition: IdentificationData.h:146
Helper class for storing .oms files (SQLite format)
Definition: OMSFileStore.h:81
bool anyFeaturePredicate_(const FeatureContainer &features, const Predicate &pred)
check whether a predicate is true for any feature (or subordinate thereof) in a container
Definition: OMSFileStore.h:228
Key getAddress_(const IdentificationData::IdentifiedMolecule &molecule_var)
void storeProcessingSoftwares_(const IdentificationData &id_data)
void store(const FeatureMap &features)
Write data from a FeatureMap object to database.
void createTableAppliedProcessingStep_(const String &parent_table)
void storeFeature_(const FeatureMap &features)
Key storeDataValue_(const DataValue &value)
void storeDBSearchParams_(const IdentificationData &id_data)
void storeIdentifiedCompounds_(const IdentificationData &id_data)
void storeScoreTypes_(const IdentificationData &id_data)
void storeMetaInfos_(const MetaInfoInterfaceContainer &container, const String &parent_table)
Definition: OMSFileStore.h:175
void createTable_(const String &name, const String &definition, bool may_exist=false)
void storeMetaInfo_(const MetaInfoInterface &info, const String &parent_table, Key parent_id)
void storeMapMetaData_(const FeatureMap &features)
qint64 Key
Type used for database keys.
Definition: OMSFileStore.h:83
void storeDataProcessing_(const FeatureMap &features)
Key storeCVTerm_(const CVTerm &cv_term)
void store(const IdentificationData &id_data)
Write data from an IdentificationData object to database.
void storeObservationMatches_(const IdentificationData &id_data)
void storeParentGroupSets_(const IdentificationData &id_data)
void storeObservations_(const IdentificationData &id_data)
void storeParentSequences_(const IdentificationData &id_data)
void storeScoredProcessingResults_(const ScoredProcessingResultContainer &container, const String &parent_table)
Definition: OMSFileStore.h:195
std::map< std::string, QSqlQuery > prepared_queries_
prepared queries for inserting data into different tables
Definition: OMSFileStore.h:247
void createTableMetaInfo_(const String &parent_table, const String &key_column="id")
void storeAppliedProcessingStep_(const IdentificationData::AppliedProcessingStep &step, Size step_order, const String &parent_table, Key parent_id)
void storeFeatures_(const FeatureMap &features)
void storeInputFiles_(const IdentificationData &id_data)
void storeProcessingSteps_(const IdentificationData &id_data)
void storeAdducts_(const IdentificationData &id_data)
void storeIdentifiedSequences_(const IdentificationData &id_data)
void storeFeatureAndSubordinates_(const Feature &feature, int &feature_id, int parent_id)
void storeParentMatches_(const IdentificationData::ParentMatches &matches, Key molecule_id)
OMSFileStore(const String &filename, LogType log_type)
Constructor.
QString db_name_
Definition: OMSFileStore.h:244
Interface for classes that can store arbitrary meta information (Type-Name-Value tuples).
Definition: MetaInfoInterface.h:61
Base class for all classes that want to report their progress.
Definition: ProgressLogger.h:53
LogType
Possible log types.
Definition: ProgressLogger.h:69
A more convenient string class.
Definition: String.h:60
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
void raiseDBError_(const QSqlError &error, int line, const char *function, const String &context)
Raise a more informative database error.
bool tableExists_(const String &db_name, const String &table_name)
Check if a specified database table exists.
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
Definition: AppliedProcessingStep.h:56
Variant type holding Peptide/Compound/Oligo references and convenience functions.
Definition: IdentifiedMolecule.h:55