OpenMS  3.0.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-2022.
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 
103  ~OMSFileStore();
104 
106  void store(const IdentificationData& id_data);
107 
109  void store(const FeatureMap& features);
110 
111  private:
112  void storeVersionAndDate_();
113 
114  void storeScoreTypes_(const IdentificationData& id_data);
115 
116  void storeInputFiles_(const IdentificationData& id_data);
117 
118  void storeProcessingSoftwares_(const IdentificationData& id_data);
119 
120  void storeDBSearchParams_(const IdentificationData& id_data);
121 
122  void storeProcessingSteps_(const IdentificationData& id_data);
123 
124  void storeObservations_(const IdentificationData& id_data);
125 
126  void storeParentSequences_(const IdentificationData& id_data);
127 
128  void storeParentGroupSets_(const IdentificationData& id_data);
129 
130  void storeIdentifiedCompounds_(const IdentificationData& id_data);
131 
132  void storeIdentifiedSequences_(const IdentificationData& id_data);
133 
134  void storeAdducts_(const IdentificationData& id_data);
135 
136  void storeObservationMatches_(const IdentificationData& id_data);
137 
138  void storeFeatures_(const FeatureMap& features);
139 
140  void createTable_(const String& name, const String& definition,
141  bool may_exist = false);
142 
144 
145  void createTableDataValue_();
146 
147  Key storeDataValue_(const DataValue& value);
148 
149  void createTableCVTerm_();
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 
171  void storeParentMatches_(
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 }
OpenMS::Internal::OMSFileStore::prepared_queries_
std::map< std::string, QSqlQuery > prepared_queries_
prepared queries for inserting data into different tables
Definition: OMSFileStore.h:247
OpenMS::Internal::OMSFileStore::storeDataValue_
Key storeDataValue_(const DataValue &value)
OpenMS::Internal::tableExists_
bool tableExists_(const String &db_name, const String &table_name)
Check if a specified database table exists.
OpenMS::Internal::OMSFileStore::getAddress_
Key getAddress_(const IdentificationData::IdentifiedMolecule &molecule_var)
OpenMS::Internal::OMSFileStore::Key
qint64 Key
Type used for database keys.
Definition: OMSFileStore.h:83
OpenMS::Internal::OMSFileStore::storeProcessingSoftwares_
void storeProcessingSoftwares_(const IdentificationData &id_data)
OpenMS::Internal::OMSFileStore::anyFeaturePredicate_
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
OpenMS::Internal::OMSFileStore::storeFeatureAndSubordinates_
void storeFeatureAndSubordinates_(const Feature &feature, int &feature_id, int parent_id)
OpenMS::Internal::OMSFileStore::storeIdentifiedCompounds_
void storeIdentifiedCompounds_(const IdentificationData &id_data)
OpenMS::Internal::OMSFileStore::storeAppliedProcessingStep_
void storeAppliedProcessingStep_(const IdentificationData::AppliedProcessingStep &step, Size step_order, const String &parent_table, Key parent_id)
OpenMS::Internal::OMSFileStore::~OMSFileStore
~OMSFileStore()
Destructor.
OpenMS::Internal::OMSFileStore::createTableDataValue_
void createTableDataValue_()
OpenMS::Internal::OMSFileStore::storeParentMatches_
void storeParentMatches_(const IdentificationData::ParentMatches &matches, Key molecule_id)
OpenMS::String
A more convenient string class.
Definition: String.h:58
OpenMS::IdentificationDataInternal::IdentifiedMolecule
Variant type holding Peptide/Compound/Oligo references and convenience functions.
Definition: IdentifiedMolecule.h:54
OpenMS::Size
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
OpenMS::Internal::OMSFileStore::storeAdducts_
void storeAdducts_(const IdentificationData &id_data)
OpenMS::Internal::OMSFileStore::storeDBSearchParams_
void storeDBSearchParams_(const IdentificationData &id_data)
OpenMS::Internal::OMSFileStore::createTableCVTerm_
void createTableCVTerm_()
OpenMS::Internal::OMSFileStore::createTableAppliedProcessingStep_
void createTableAppliedProcessingStep_(const String &parent_table)
OpenMS::Internal::OMSFileStore::storeParentGroupSets_
void storeParentGroupSets_(const IdentificationData &id_data)
OpenMS::Internal::OMSFileStore::createTableIdentifiedMolecule_
void createTableIdentifiedMolecule_()
OpenMS::Internal::OMSFileStore::storeScoredProcessingResults_
void storeScoredProcessingResults_(const ScoredProcessingResultContainer &container, const String &parent_table)
Definition: OMSFileStore.h:195
OpenMS::Internal::OMSFileStore::storeFeatures_
void storeFeatures_(const FeatureMap &features)
OpenMS
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
OpenMS::Internal::OMSFileStore::store
void store(const IdentificationData &id_data)
Write data from an IdentificationData object to database.
OpenMS::ProgressLogger
Base class for all classes that want to report their progress.
Definition: ProgressLogger.h:52
OpenMS::DataValue
Class to hold strings, numeric values, lists of strings and lists of numeric values.
Definition: DataValue.h:58
ProgressLogger.h
OpenMS::Internal::OMSFileStore::storeFeature_
void storeFeature_(const FeatureMap &features)
OpenMS::MetaInfoInterface
Interface for classes that can store arbitrary meta information (Type-Name-Value tuples).
Definition: MetaInfoInterface.h:60
OpenMS::Internal::OMSFileStore::createTableMetaInfo_
void createTableMetaInfo_(const String &parent_table, const String &key_column="id")
OpenMS::IdentificationData::ParentMatches
IdentificationDataInternal::ParentMatches ParentMatches
Definition: IdentificationData.h:146
OpenMS::Internal::OMSFileStore::db_name_
QString db_name_
Definition: OMSFileStore.h:244
OpenMS::Internal::OMSFileStore::createTableParentMatches_
void createTableParentMatches_()
FeatureMap.h
OpenMS::Internal::OMSFileStore::storeScoreTypes_
void storeScoreTypes_(const IdentificationData &id_data)
OpenMS::Internal::OMSFileStore::storeObservations_
void storeObservations_(const IdentificationData &id_data)
OpenMS::Internal::OMSFileStore::storeInputFiles_
void storeInputFiles_(const IdentificationData &id_data)
OpenMS::Internal::OMSFileStore::storeObservationMatches_
void storeObservationMatches_(const IdentificationData &id_data)
OpenMS::Internal::OMSFileStore::storeCVTerm_
Key storeCVTerm_(const CVTerm &cv_term)
OpenMS::ProgressLogger::LogType
LogType
Possible log types.
Definition: ProgressLogger.h:68
OpenMS::IdentificationDataInternal::AppliedProcessingStep
Definition: AppliedProcessingStep.h:55
OpenMS::Internal::OMSFileStore::storeMapMetaData_
void storeMapMetaData_(const FeatureMap &features)
OpenMS::IdentificationData
Representation of spectrum identification results and associated data.
Definition: IdentificationData.h:94
OpenMS::FeatureMap
A container for features.
Definition: FeatureMap.h:98
OpenMS::Internal::OMSFileStore::storeMetaInfo_
void storeMetaInfo_(const MetaInfoInterface &info, const String &parent_table, Key parent_id)
OpenMS::Feature
An LC-MS feature.
Definition: Feature.h:70
OpenMS::Internal::OMSFileStore::OMSFileStore
OMSFileStore(const String &filename, LogType log_type)
Constructor.
OpenMS::CVTerm
Representation of controlled vocabulary term.
Definition: CVTerm.h:52
OpenMS::Internal::OMSFileStore::storeMetaInfos_
void storeMetaInfos_(const MetaInfoInterfaceContainer &container, const String &parent_table)
Definition: OMSFileStore.h:175
IdentificationData.h
OpenMS::Internal::OMSFileStore
Helper class for storing .oms files (SQLite format)
Definition: OMSFileStore.h:80
OpenMS::Internal::OMSFileStore::storeParentSequences_
void storeParentSequences_(const IdentificationData &id_data)
OpenMS::Internal::OMSFileStore::storeVersionAndDate_
void storeVersionAndDate_()
OpenMS::Internal::raiseDBError_
void raiseDBError_(const QSqlError &error, int line, const char *function, const String &context)
Raise a more informative database error.
OpenMS::Internal::OMSFileStore::createTable_
void createTable_(const String &name, const String &definition, bool may_exist=false)
OpenMS::Internal::OMSFileStore::storeDataProcessing_
void storeDataProcessing_(const FeatureMap &features)
OpenMS::Internal::OMSFileStore::createTableMoleculeType_
void createTableMoleculeType_()
OpenMS::Internal::OMSFileStore::storeProcessingSteps_
void storeProcessingSteps_(const IdentificationData &id_data)
OpenMS::Internal::OMSFileStore::storeIdentifiedSequences_
void storeIdentifiedSequences_(const IdentificationData &id_data)