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, Chris Bielow $
33 // --------------------------------------------------------------------------
34 
35 #pragma once
36 
40 
41 namespace SQLite
42 {
43  class Database;
44  class Exception;
45  class Statement;
46 }
47 
48 namespace OpenMS
49 {
50  namespace Internal
51  {
65  void raiseDBError_(const String& error, int line, const char* function, const String& context, const String& query = "");
66 
67  bool execAndReset(SQLite::Statement& query, int expected_modifications);
68 
69  void execWithExceptionAndReset(SQLite::Statement& query, int expected_modifications, int line, const char* function, const char* context);
70 
71 
78  {
79  public:
81  using Key = int64_t; //std::decltype(((SQLite::Database*)nullptr)->getLastInsertRowid());
82 
94  OMSFileStore(const String& filename, LogType log_type);
95 
101  ~OMSFileStore();
102 
104  void store(const IdentificationData& id_data);
105 
107  void store(const FeatureMap& features);
108 
109  private:
110  void storeVersionAndDate_();
111 
112  void storeScoreTypes_(const IdentificationData& id_data);
113 
114  void storeInputFiles_(const IdentificationData& id_data);
115 
116  void storeProcessingSoftwares_(const IdentificationData& id_data);
117 
118  void storeDBSearchParams_(const IdentificationData& id_data);
119 
120  void storeProcessingSteps_(const IdentificationData& id_data);
121 
122  void storeObservations_(const IdentificationData& id_data);
123 
124  void storeParentSequences_(const IdentificationData& id_data);
125 
126  void storeParentGroupSets_(const IdentificationData& id_data);
127 
128  void storeIdentifiedCompounds_(const IdentificationData& id_data);
129 
130  void storeIdentifiedSequences_(const IdentificationData& id_data);
131 
132  void storeAdducts_(const IdentificationData& id_data);
133 
134  void storeObservationMatches_(const IdentificationData& id_data);
135 
136  void storeFeatures_(const FeatureMap& features);
137 
138  void createTable_(const String& name, const String& definition,
139  bool may_exist = false);
140 
142 
143  void createTableDataValue_();
144 
145  Key storeDataValue_(const DataValue& value);
146 
147  void createTableCVTerm_();
148 
149  Key storeCVTerm_(const CVTerm& cv_term);
150 
151  void createTableMetaInfo_(const String& parent_table,
152  const String& key_column = "id");
153 
154  void storeMetaInfo_(const MetaInfoInterface& info, const String& parent_table,
155  Key parent_id);
156 
157  void createTableAppliedProcessingStep_(const String& parent_table);
158 
160  const IdentificationData::AppliedProcessingStep& step, Size step_order,
161  const String& parent_table, Key parent_id);
162 
164 
166 
168 
169  void storeParentMatches_(
170  const IdentificationData::ParentMatches& matches, Key molecule_id);
171 
172  template<class MetaInfoInterfaceContainer, class DBKeyTable>
173  void storeMetaInfos_(const MetaInfoInterfaceContainer& container,
174  const String& parent_table, const DBKeyTable& db_keys)
175  {
176  bool table_created = false;
177  for (const auto& element : container)
178  {
179  if (!element.isMetaEmpty())
180  {
181  if (!table_created)
182  {
183  createTableMetaInfo_(parent_table);
184  table_created = true;
185  }
186  storeMetaInfo_(element, parent_table, db_keys.at(&element));
187  }
188  }
189  }
190 
191  template<class ScoredProcessingResultContainer, class DBKeyTable>
192  void storeScoredProcessingResults_(const ScoredProcessingResultContainer& container,
193  const String& parent_table, const DBKeyTable& db_keys)
194  {
195  bool table_created = false;
196  for (const auto& element : container)
197  {
198  if (!element.steps_and_scores.empty())
199  {
200  if (!table_created)
201  {
202  createTableAppliedProcessingStep_(parent_table);
203  table_created = true;
204  }
205  Size counter = 0;
206  for (const IdentificationData::AppliedProcessingStep& step : element.steps_and_scores)
207  {
208  storeAppliedProcessingStep_(step, ++counter, parent_table, db_keys.at(&element));
209  }
210  }
211  }
212  storeMetaInfos_(container, parent_table, db_keys);
213  }
214 
215  void storeFeature_(const FeatureMap& features);
216 
218  const Feature& feature, int& feature_id, int parent_id);
219 
221  template <class FeatureContainer, class Predicate>
222  bool anyFeaturePredicate_(const FeatureContainer& features, const Predicate& pred)
223  {
224  if (features.empty()) return false;
225  for (const Feature& feature : features)
226  {
227  if (pred(feature)) return true;
228  if (anyFeaturePredicate_(feature.getSubordinates(), pred)) return true;
229  }
230  return false;
231  }
232 
233  void storeMapMetaData_(const FeatureMap& features);
234 
235  void storeDataProcessing_(const FeatureMap& features);
236 
238  std::unique_ptr<SQLite::Database> db_;
239 
241  std::map<std::string, std::unique_ptr<SQLite::Statement>> prepared_queries_;
242 
243  // mapping between loaded data and database keys:
244  // @NOTE: in principle we could use `unordered_map` here for efficiency,
245  // but that gives compiler errors when pointers or iterators (`...Ref`)
246  // are used as keys (because they aren't hashable?)
247  std::map<const IdentificationData::ScoreType*, Key> score_type_keys_;
248  std::map<const IdentificationData::InputFile*, Key> input_file_keys_;
249  std::map<const IdentificationData::ProcessingSoftware*, Key> processing_software_keys_;
250  std::map<const IdentificationData::ProcessingStep*, Key> processing_step_keys_;
251  std::map<const IdentificationData::DBSearchParam*, Key> search_param_keys_;
252  std::map<const IdentificationData::Observation*, Key> observation_keys_;
253  std::map<const IdentificationData::ParentSequence*, Key> parent_sequence_keys_;
254  std::map<const IdentificationData::ParentGroupSet*, Key> parent_grouping_keys_;
255  std::map<const IdentificationData::IdentifiedCompound*, Key> identified_compound_keys_;
256  std::map<const IdentificationData::IdentifiedPeptide*, Key> identified_peptide_keys_;
257  std::map<const IdentificationData::IdentifiedOligo*, Key> identified_oligo_keys_;
258  std::map<const AdductInfo*, Key> adduct_keys_;
259  std::map<const IdentificationData::ObservationMatch*, Key> observation_match_keys_;
260  // for feature maps:
261  std::map<const DataProcessing*, Key> feat_processing_keys_;
262  };
263  }
264 }
std::map< const IdentificationData::ObservationMatch *, Key > observation_match_keys_
Definition: OMSFileStore.h:259
void execWithExceptionAndReset(SQLite::Statement &query, int expected_modifications, int line, const char *function, const char *context)
void storeFeatureAndSubordinates_(const Feature &feature, int &feature_id, int parent_id)
std::map< const IdentificationData::DBSearchParam *, Key > search_param_keys_
Definition: OMSFileStore.h:251
A more convenient string class.
Definition: String.h:58
void storeIdentifiedSequences_(const IdentificationData &id_data)
bool execAndReset(SQLite::Statement &query, int expected_modifications)
Definition: AppliedProcessingStep.h:55
LogType
Possible log types.
Definition: ProgressLogger.h:68
std::map< const IdentificationData::IdentifiedCompound *, Key > identified_compound_keys_
Definition: OMSFileStore.h:255
A container for features.
Definition: FeatureMap.h:98
std::map< const IdentificationData::ScoreType *, Key > score_type_keys_
Definition: OMSFileStore.h:247
std::map< const IdentificationData::InputFile *, Key > input_file_keys_
Definition: OMSFileStore.h:248
int64_t Key
< Type used for database keys
Definition: OMSFileStore.h:81
IdentificationDataInternal::ParentMatches ParentMatches
Definition: IdentificationData.h:164
Definition: IdentificationData.h:112
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
std::map< const DataProcessing *, Key > feat_processing_keys_
Definition: OMSFileStore.h:261
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)
std::map< const IdentificationData::IdentifiedOligo *, Key > identified_oligo_keys_
Definition: OMSFileStore.h:257
void storeScoredProcessingResults_(const ScoredProcessingResultContainer &container, const String &parent_table, const DBKeyTable &db_keys)
Definition: OMSFileStore.h:192
Helper class for storing .oms files (SQLite format)
Definition: OMSFileStore.h:77
void storeIdentifiedCompounds_(const IdentificationData &id_data)
std::map< const IdentificationData::ParentSequence *, Key > parent_sequence_keys_
Definition: OMSFileStore.h:253
std::map< std::string, std::unique_ptr< SQLite::Statement > > prepared_queries_
prepared queries for inserting data into different tables
Definition: OMSFileStore.h:241
void storeDBSearchParams_(const IdentificationData &id_data)
Representation of controlled vocabulary term.
Definition: CVTerm.h:52
An LC-MS feature.
Definition: Feature.h:70
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)
std::unique_ptr< SQLite::Database > db_
The database connection (read/write)
Definition: OMSFileStore.h:238
Definition: OMSFileLoad.h:44
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:256
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:222
void storeMetaInfos_(const MetaInfoInterfaceContainer &container, const String &parent_table, const DBKeyTable &db_keys)
Definition: OMSFileStore.h:173
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:249
Key storeCVTerm_(const CVTerm &cv_term)
void storeScoreTypes_(const IdentificationData &id_data)
std::map< const IdentificationData::ProcessingStep *, Key > processing_step_keys_
Definition: OMSFileStore.h:250
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:254
void createTableAppliedProcessingStep_(const String &parent_table)
std::map< const IdentificationData::Observation *, Key > observation_keys_
Definition: OMSFileStore.h:252
std::map< const AdductInfo *, Key > adduct_keys_
Definition: OMSFileStore.h:258
void storeFeatures_(const FeatureMap &features)
void storeObservations_(const IdentificationData &id_data)
void createTable_(const String &name, const String &definition, bool may_exist=false)
void raiseDBError_(const String &error, int line, const char *function, const String &context, const String &query="")
Raise a more informative database error.
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)