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 
72  void raiseDBError_(const QSqlError& error, int line,
73  const char* function, const String& context, const String& query = "");
74 
75 
82  {
83  public:
84  using Key = qint64;
85 
97  OMSFileStore(const String& filename, LogType log_type);
98 
104  ~OMSFileStore();
105 
107  void store(const IdentificationData& id_data);
108 
110  void store(const FeatureMap& features);
111 
112  private:
113  void storeVersionAndDate_();
114 
115  void storeScoreTypes_(const IdentificationData& id_data);
116 
117  void storeInputFiles_(const IdentificationData& id_data);
118 
119  void storeProcessingSoftwares_(const IdentificationData& id_data);
120 
121  void storeDBSearchParams_(const IdentificationData& id_data);
122 
123  void storeProcessingSteps_(const IdentificationData& id_data);
124 
125  void storeObservations_(const IdentificationData& id_data);
126 
127  void storeParentSequences_(const IdentificationData& id_data);
128 
129  void storeParentGroupSets_(const IdentificationData& id_data);
130 
131  void storeIdentifiedCompounds_(const IdentificationData& id_data);
132 
133  void storeIdentifiedSequences_(const IdentificationData& id_data);
134 
135  void storeAdducts_(const IdentificationData& id_data);
136 
137  void storeObservationMatches_(const IdentificationData& id_data);
138 
139  void storeFeatures_(const FeatureMap& features);
140 
141  void createTable_(const String& name, const String& definition,
142  bool may_exist = false);
143 
145 
146  void createTableDataValue_();
147 
148  Key storeDataValue_(const DataValue& value);
149 
150  void createTableCVTerm_();
151 
152  Key storeCVTerm_(const CVTerm& cv_term);
153 
154  void createTableMetaInfo_(const String& parent_table,
155  const String& key_column = "id");
156 
157  void storeMetaInfo_(const MetaInfoInterface& info, const String& parent_table,
158  Key parent_id);
159 
160  void createTableAppliedProcessingStep_(const String& parent_table);
161 
163  const IdentificationData::AppliedProcessingStep& step, Size step_order,
164  const String& parent_table, Key parent_id);
165 
167 
169 
171 
172  void storeParentMatches_(
173  const IdentificationData::ParentMatches& matches, Key molecule_id);
174 
175  template<class MetaInfoInterfaceContainer, class DBKeyTable>
176  void storeMetaInfos_(const MetaInfoInterfaceContainer& container,
177  const String& parent_table, const DBKeyTable& db_keys)
178  {
179  bool table_created = false;
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, db_keys.at(&element));
190  }
191  }
192  }
193 
194  template<class ScoredProcessingResultContainer, class DBKeyTable>
195  void storeScoredProcessingResults_(const ScoredProcessingResultContainer& container,
196  const String& parent_table, const DBKeyTable& db_keys)
197  {
198  bool table_created = false;
199  for (const auto& element : container)
200  {
201  if (!element.steps_and_scores.empty())
202  {
203  if (!table_created)
204  {
205  createTableAppliedProcessingStep_(parent_table);
206  table_created = true;
207  }
208  Size counter = 0;
209  for (const IdentificationData::AppliedProcessingStep& step : element.steps_and_scores)
210  {
211  storeAppliedProcessingStep_(step, ++counter, parent_table, db_keys.at(&element));
212  }
213  }
214  }
215  storeMetaInfos_(container, parent_table, db_keys);
216  }
217 
218  void storeFeature_(const FeatureMap& features);
219 
221  const Feature& feature, int& feature_id, int parent_id);
222 
224  template <class FeatureContainer, class Predicate>
225  bool anyFeaturePredicate_(const FeatureContainer& features, const Predicate& pred)
226  {
227  if (features.empty()) return false;
228  for (const Feature& feature : features)
229  {
230  if (pred(feature)) return true;
231  if (anyFeaturePredicate_(feature.getSubordinates(), pred)) return true;
232  }
233  return false;
234  }
235 
236  void storeMapMetaData_(const FeatureMap& features);
237 
238  void storeDataProcessing_(const FeatureMap& features);
239 
240  // store name, not database connection itself (see https://stackoverflow.com/a/55200682):
241  QString db_name_;
242 
244  std::map<std::string, QSqlQuery> prepared_queries_;
245 
246  // mapping between loaded data and database keys:
247  // @NOTE: in principle we could use `unordered_map` here for efficiency,
248  // but that gives compiler errors when pointers or iterators (`...Ref`)
249  // are used as keys (because they aren't hashable?)
250  std::map<const IdentificationData::ScoreType*, Key> score_type_keys_;
251  std::map<const IdentificationData::InputFile*, Key> input_file_keys_;
252  std::map<const IdentificationData::ProcessingSoftware*, Key> processing_software_keys_;
253  std::map<const IdentificationData::ProcessingStep*, Key> processing_step_keys_;
254  std::map<const IdentificationData::DBSearchParam*, Key> search_param_keys_;
255  std::map<const IdentificationData::Observation*, Key> observation_keys_;
256  std::map<const IdentificationData::ParentSequence*, Key> parent_sequence_keys_;
257  std::map<const IdentificationData::ParentGroupSet*, Key> parent_grouping_keys_;
258  std::map<const IdentificationData::IdentifiedCompound*, Key> identified_compound_keys_;
259  std::map<const IdentificationData::IdentifiedPeptide*, Key> identified_peptide_keys_;
260  std::map<const IdentificationData::IdentifiedOligo*, Key> identified_oligo_keys_;
261  std::map<const AdductInfo*, Key> adduct_keys_;
262  std::map<const IdentificationData::ObservationMatch*, Key> observation_match_keys_;
263  // for feature maps:
264  std::map<const DataProcessing*, Key> feat_processing_keys_;
265  };
266  }
267 }
std::map< const IdentificationData::ObservationMatch *, Key > observation_match_keys_
Definition: OMSFileStore.h:262
void storeFeatureAndSubordinates_(const Feature &feature, int &feature_id, int parent_id)
std::map< const IdentificationData::DBSearchParam *, Key > search_param_keys_
Definition: OMSFileStore.h:254
A more convenient string class.
Definition: String.h:58
void storeIdentifiedSequences_(const IdentificationData &id_data)
Definition: AppliedProcessingStep.h:55
LogType
Possible log types.
Definition: ProgressLogger.h:68
std::map< const IdentificationData::IdentifiedCompound *, Key > identified_compound_keys_
Definition: OMSFileStore.h:258
A container for features.
Definition: FeatureMap.h:98
std::map< const IdentificationData::ScoreType *, Key > score_type_keys_
Definition: OMSFileStore.h:250
std::map< const IdentificationData::InputFile *, Key > input_file_keys_
Definition: OMSFileStore.h:251
IdentificationDataInternal::ParentMatches ParentMatches
Definition: IdentificationData.h:146
Representation of spectrum identification results and associated data.
Definition: IdentificationData.h:94
Key getDatabaseKey_(const IdentificationData::IdentifiedMolecule &molecule_var)
void storeParentMatches_(const IdentificationData::ParentMatches &matches, Key molecule_id)
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
void storeAdducts_(const IdentificationData &id_data)
void storeProcessingSteps_(const IdentificationData &id_data)
Class to hold strings, numeric values, lists of strings and lists of numeric values.
Definition: DataValue.h:58
QString db_name_
Definition: OMSFileStore.h:241
std::map< const DataProcessing *, Key > feat_processing_keys_
Definition: OMSFileStore.h:264
void createTableMetaInfo_(const String &parent_table, const String &key_column="id")
OMSFileStore(const String &filename, LogType log_type)
Constructor.
void storeParentSequences_(const IdentificationData &id_data)
void storeParentGroupSets_(const IdentificationData &id_data)
void raiseDBError_(const QSqlError &error, int line, const char *function, const String &context, const String &query="")
Raise a more informative database error.
std::map< const IdentificationData::IdentifiedOligo *, Key > identified_oligo_keys_
Definition: OMSFileStore.h:260
void storeScoredProcessingResults_(const ScoredProcessingResultContainer &container, const String &parent_table, const DBKeyTable &db_keys)
Definition: OMSFileStore.h:195
Helper class for storing .oms files (SQLite format)
Definition: OMSFileStore.h:81
bool tableExists_(const String &db_name, const String &table_name)
Check if a specified database table exists.
void storeIdentifiedCompounds_(const IdentificationData &id_data)
std::map< const IdentificationData::ParentSequence *, Key > parent_sequence_keys_
Definition: OMSFileStore.h:256
std::map< std::string, QSqlQuery > prepared_queries_
prepared queries for inserting data into different tables
Definition: OMSFileStore.h:244
void storeDBSearchParams_(const IdentificationData &id_data)
Representation of controlled vocabulary term.
Definition: CVTerm.h:52
An LC-MS feature.
Definition: Feature.h:70
qint64 Key
Type used for database keys.
Definition: OMSFileStore.h:84
void storeFeature_(const FeatureMap &features)
Interface for classes that can store arbitrary meta information (Type-Name-Value tuples).
Definition: MetaInfoInterface.h:60
Variant type holding Peptide/Compound/Oligo references and convenience functions. ...
Definition: IdentifiedMolecule.h:54
void storeObservationMatches_(const IdentificationData &id_data)
void storeInputFiles_(const IdentificationData &id_data)
void storeProcessingSoftwares_(const IdentificationData &id_data)
void store(const IdentificationData &id_data)
Write data from an IdentificationData object to database.
std::map< const IdentificationData::IdentifiedPeptide *, Key > identified_peptide_keys_
Definition: OMSFileStore.h:259
void storeDataProcessing_(const FeatureMap &features)
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:225
void storeMetaInfos_(const MetaInfoInterfaceContainer &container, const String &parent_table, const DBKeyTable &db_keys)
Definition: OMSFileStore.h:176
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
std::map< const IdentificationData::ProcessingSoftware *, Key > processing_software_keys_
Definition: OMSFileStore.h:252
Key storeCVTerm_(const CVTerm &cv_term)
void storeScoreTypes_(const IdentificationData &id_data)
std::map< const IdentificationData::ProcessingStep *, Key > processing_step_keys_
Definition: OMSFileStore.h:253
Base class for all classes that want to report their progress.
Definition: ProgressLogger.h:52
std::map< const IdentificationData::ParentGroupSet *, Key > parent_grouping_keys_
Definition: OMSFileStore.h:257
void createTableAppliedProcessingStep_(const String &parent_table)
std::map< const IdentificationData::Observation *, Key > observation_keys_
Definition: OMSFileStore.h:255
std::map< const AdductInfo *, Key > adduct_keys_
Definition: OMSFileStore.h:261
void storeFeatures_(const FeatureMap &features)
void storeObservations_(const IdentificationData &id_data)
void createTable_(const String &name, const String &definition, bool may_exist=false)
void storeMapMetaData_(const FeatureMap &features)
void storeAppliedProcessingStep_(const IdentificationData::AppliedProcessingStep &step, Size step_order, const String &parent_table, Key parent_id)
void storeMetaInfo_(const MetaInfoInterface &info, const String &parent_table, Key parent_id)
Key storeDataValue_(const DataValue &value)