OpenMS
OMSFileStore.h
Go to the documentation of this file.
1 // Copyright (c) 2002-2023, The OpenMS Team -- EKU Tuebingen, ETH Zurich, and FU Berlin
2 // SPDX-License-Identifier: BSD-3-Clause
3 //
4 // --------------------------------------------------------------------------
5 // $Maintainer: Hendrik Weisser $
6 // $Authors: Hendrik Weisser, Chris Bielow $
7 // --------------------------------------------------------------------------
8 
9 #pragma once
10 
15 
16 namespace SQLite
17 {
18  class Database;
19  class Exception;
20  class Statement;
21 }
22 
23 namespace OpenMS
24 {
25  namespace Internal
26  {
40  void raiseDBError_(const String& error, int line, const char* function, const String& context, const String& query = "");
41 
47  bool execAndReset(SQLite::Statement& query, int expected_modifications);
48 
50  void execWithExceptionAndReset(SQLite::Statement& query, int expected_modifications, int line, const char* function, const char* context);
51 
52 
59  {
60  public:
62  using Key = int64_t; //std::decltype(((SQLite::Database*)nullptr)->getLastInsertRowid());
63 
75  OMSFileStore(const String& filename, LogType log_type);
76 
83 
85  void store(const IdentificationData& id_data);
86 
88  void store(const FeatureMap& features);
89 
91  void store(const ConsensusMap& consensus);
92 
93  private:
101  void createTable_(const String& name, const String& definition, bool may_exist = false);
102 
105 
108 
110  void createTableMetaInfo_(const String& parent_table,
111  const String& key_column = "id");
112 
115 
117  Key storeCVTerm_(const CVTerm& cv_term);
118 
120  void storeMetaInfo_(const MetaInfoInterface& info, const String& parent_table,
121  Key parent_id);
122 
124  template<class MetaInfoInterfaceContainer, class DBKeyTable>
125  void storeMetaInfos_(const MetaInfoInterfaceContainer& container,
126  const String& parent_table, const DBKeyTable& db_keys)
127  {
128  bool table_created = false;
129  for (const auto& element : container)
130  {
131  if (!element.isMetaEmpty())
132  {
133  if (!table_created)
134  {
135  createTableMetaInfo_(parent_table);
136  table_created = true;
137  }
138  storeMetaInfo_(element, parent_table, db_keys.at(&element));
139  }
140  }
141  }
142 
146  void storeScoreTypes_(const IdentificationData& id_data);
147 
149  void storeInputFiles_(const IdentificationData& id_data);
150 
153 
156 
159 
162 
165 
168 
171 
174 
176  void storeAdducts_(const IdentificationData& id_data);
177 
180 
183 
185  void createTableAppliedProcessingStep_(const String& parent_table);
186 
189  const IdentificationData::AppliedProcessingStep& step, Size step_order,
190  const String& parent_table, Key parent_id);
191 
194 
197 
200 
203  const IdentificationData::ParentMatches& matches, Key molecule_id);
204 
206  template<class ScoredProcessingResultContainer, class DBKeyTable>
207  void storeScoredProcessingResults_(const ScoredProcessingResultContainer& container,
208  const String& parent_table, const DBKeyTable& db_keys)
209  {
210  bool table_created = false;
211  for (const auto& element : container)
212  {
213  if (!element.steps_and_scores.empty())
214  {
215  if (!table_created)
216  {
217  createTableAppliedProcessingStep_(parent_table);
218  table_created = true;
219  }
220  Size counter = 0;
221  for (const IdentificationData::AppliedProcessingStep& step : element.steps_and_scores)
222  {
223  storeAppliedProcessingStep_(step, ++counter, parent_table, db_keys.at(&element));
224  }
225  }
226  }
227  storeMetaInfos_(container, parent_table, db_keys);
228  }
230 
234  void createTableBaseFeature_(bool with_metainfo, bool with_idmatches);
235 
237  void storeBaseFeature_(const BaseFeature& feature, int feature_id, int parent_id);
238 
240  void storeFeatures_(const FeatureMap& features);
241 
244  const Feature& feature, int& feature_id, int parent_id);
245 
247  template <class FeatureContainer, class Predicate>
248  bool anyFeaturePredicate_(const FeatureContainer& features, const Predicate& pred)
249  {
250  if (features.empty()) return false;
251  for (const Feature& feature : features)
252  {
253  if (pred(feature)) return true;
254  if (anyFeaturePredicate_(feature.getSubordinates(), pred)) return true;
255  }
256  return false;
257  }
258 
260  template <class MapType>
261  void storeMapMetaData_(const MapType& features, const String& experiment_type = "");
262 
264  void storeDataProcessing_(const std::vector<DataProcessing>& data_processing);
265 
267  void storeConsensusFeatures_(const ConsensusMap& consensus);
268 
272 
274  std::unique_ptr<SQLite::Database> db_;
275 
277  std::map<std::string, std::unique_ptr<SQLite::Statement>> prepared_queries_;
278 
279  // mapping between loaded data and database keys:
280  // @NOTE: in principle we could use `unordered_map` here for efficiency,
281  // but that gives compiler errors when pointers or iterators (`...Ref`)
282  // are used as keys (because they aren't hashable?)
283  std::map<const IdentificationData::ScoreType*, Key> score_type_keys_;
284  std::map<const IdentificationData::InputFile*, Key> input_file_keys_;
285  std::map<const IdentificationData::ProcessingSoftware*, Key> processing_software_keys_;
286  std::map<const IdentificationData::ProcessingStep*, Key> processing_step_keys_;
287  std::map<const IdentificationData::DBSearchParam*, Key> search_param_keys_;
288  std::map<const IdentificationData::Observation*, Key> observation_keys_;
289  std::map<const IdentificationData::ParentSequence*, Key> parent_sequence_keys_;
290  std::map<const IdentificationData::ParentGroupSet*, Key> parent_grouping_keys_;
291  std::map<const IdentificationData::IdentifiedCompound*, Key> identified_compound_keys_;
292  std::map<const IdentificationData::IdentifiedPeptide*, Key> identified_peptide_keys_;
293  std::map<const IdentificationData::IdentifiedOligo*, Key> identified_oligo_keys_;
294  std::map<const AdductInfo*, Key> adduct_keys_;
295  std::map<const IdentificationData::ObservationMatch*, Key> observation_match_keys_;
296  // for feature/consensus maps:
297  std::map<const DataProcessing*, Key> feat_processing_keys_;
298  };
299  }
300 }
A basic LC-MS feature.
Definition: BaseFeature.h:33
Representation of controlled vocabulary term.
Definition: CVTerm.h:27
A container for consensus elements.
Definition: ConsensusMap.h:66
A container for features.
Definition: FeatureMap.h:80
An LC-MS feature.
Definition: Feature.h:46
Definition: IdentificationData.h:87
IdentificationDataInternal::ParentMatches ParentMatches
Definition: IdentificationData.h:138
Helper class for storing .oms files (SQLite format)
Definition: OMSFileStore.h:59
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:248
void storeProcessingSoftwares_(const IdentificationData &id_data)
Store information on data processing software from IdentificationData in the database.
void createTableIdentifiedMolecule_()
Create a database table for storing identified molecules (peptides, compounds, oligonucleotides)
void store(const FeatureMap &features)
Write data from a FeatureMap object to database.
std::map< const IdentificationData::ScoreType *, Key > score_type_keys_
Definition: OMSFileStore.h:283
void createTableBaseFeature_(bool with_metainfo, bool with_idmatches)
std::map< const IdentificationData::ProcessingStep *, Key > processing_step_keys_
Definition: OMSFileStore.h:286
void createTableAppliedProcessingStep_(const String &parent_table)
Create a database table for storing processing metadata.
void storeMetaInfos_(const MetaInfoInterfaceContainer &container, const String &parent_table, const DBKeyTable &db_keys)
Store meta values (for all objects in a container) in the database.
Definition: OMSFileStore.h:125
void storeConsensusColumnHeaders_(const ConsensusMap &consensus)
Store information on column headers from a consensus map in the database.
void storeMapMetaData_(const MapType &features, const String &experiment_type="")
Store feature/consensus map meta data in the database.
void createTableMoleculeType_()
Create a database table for molecule types (proteins, compounds, RNA)
void storeDBSearchParams_(const IdentificationData &id_data)
Store sequence database search parameters from IdentificationData in the database.
void storeIdentifiedCompounds_(const IdentificationData &id_data)
Store information on identified compounds from IdentificationData in the database.
void storeScoreTypes_(const IdentificationData &id_data)
std::map< const IdentificationData::Observation *, Key > observation_keys_
Definition: OMSFileStore.h:288
void createTableDataValue_DataType_()
Create a database table for the data types used in DataValue.
void store(const ConsensusMap &consensus)
Write data from a ConsensusMap object to database.
std::map< const IdentificationData::ProcessingSoftware *, Key > processing_software_keys_
Definition: OMSFileStore.h:285
std::map< const IdentificationData::IdentifiedPeptide *, Key > identified_peptide_keys_
Definition: OMSFileStore.h:292
void createTable_(const String &name, const String &definition, bool may_exist=false)
Helper function to create a database table.
void storeMetaInfo_(const MetaInfoInterface &info, const String &parent_table, Key parent_id)
Store meta values (associated with one object) in the database.
std::map< const IdentificationData::DBSearchParam *, Key > search_param_keys_
Definition: OMSFileStore.h:287
std::map< const IdentificationData::InputFile *, Key > input_file_keys_
Definition: OMSFileStore.h:284
std::map< const AdductInfo *, Key > adduct_keys_
Definition: OMSFileStore.h:294
int64_t Key
< Type used for database keys
Definition: OMSFileStore.h:62
std::map< const IdentificationData::ParentGroupSet *, Key > parent_grouping_keys_
Definition: OMSFileStore.h:290
Key storeCVTerm_(const CVTerm &cv_term)
Store a CV term in the database.
std::map< const IdentificationData::IdentifiedOligo *, Key > identified_oligo_keys_
Definition: OMSFileStore.h:293
void store(const IdentificationData &id_data)
Write data from an IdentificationData object to database.
void createTableCVTerm_()
Create a database table (and prepare a query) for storing CV terms.
void storeObservationMatches_(const IdentificationData &id_data)
Store information on observation matches (e.g. PSMs) from IdentificationData in the database.
void storeParentGroupSets_(const IdentificationData &id_data)
Store information on parent group sets (e.g. protein groups) from IdentificationData in the database.
void storeObservations_(const IdentificationData &id_data)
Store information on observations (e.g. spectra) from IdentificationData in the database.
void storeParentSequences_(const IdentificationData &id_data)
Store information on parent sequences (e.g. proteins) from IdentificationData in the database.
void storeBaseFeature_(const BaseFeature &feature, int feature_id, int parent_id)
Store information on a feature in the database.
void createTableMetaInfo_(const String &parent_table, const String &key_column="id")
Create a database table (and prepare a query) for storing meta values.
void storeConsensusFeatures_(const ConsensusMap &consensus)
Store information on consensus features from a consensus map in the database.
void storeAppliedProcessingStep_(const IdentificationData::AppliedProcessingStep &step, Size step_order, const String &parent_table, Key parent_id)
Store processing metadata for a particular class (stored in parent_table) in the database.
std::map< std::string, std::unique_ptr< SQLite::Statement > > prepared_queries_
Prepared queries for inserting data into different tables.
Definition: OMSFileStore.h:277
std::map< const DataProcessing *, Key > feat_processing_keys_
Definition: OMSFileStore.h:297
void storeFeatures_(const FeatureMap &features)
Store information on features from a feature map in the database.
void storeInputFiles_(const IdentificationData &id_data)
Store input file information from IdentificationData in the database.
std::unique_ptr< SQLite::Database > db_
The database connection (read/write)
Definition: OMSFileStore.h:274
void storeProcessingSteps_(const IdentificationData &id_data)
Store information on data processing steps from IdentificationData in the database.
Key getDatabaseKey_(const IdentificationData::IdentifiedMolecule &molecule_var)
Return the database key used for an identified molecule (peptide, compound or oligonucleotide)
void storeAdducts_(const IdentificationData &id_data)
Store information on adducts from IdentificationData in the database.
void storeVersionAndDate_()
Store version information and current date/time in the database.
void storeIdentifiedSequences_(const IdentificationData &id_data)
Store information on identified sequences (peptides or oligonucleotides) from IdentificationData in t...
std::map< const IdentificationData::IdentifiedCompound *, Key > identified_compound_keys_
Definition: OMSFileStore.h:291
void storeFeatureAndSubordinates_(const Feature &feature, int &feature_id, int parent_id)
Store a feature (incl. its subordinate features) in the database.
std::map< const IdentificationData::ParentSequence *, Key > parent_sequence_keys_
Definition: OMSFileStore.h:289
void storeParentMatches_(const IdentificationData::ParentMatches &matches, Key molecule_id)
Store information on parent matches in the database.
void storeScoredProcessingResults_(const ScoredProcessingResultContainer &container, const String &parent_table, const DBKeyTable &db_keys)
Store metadata on scores/processing steps (for all objects in a container) in the database.
Definition: OMSFileStore.h:207
void storeDataProcessing_(const std::vector< DataProcessing > &data_processing)
Store information on data processing from a feature/consensus map in the database.
OMSFileStore(const String &filename, LogType log_type)
Constructor.
std::map< const IdentificationData::ObservationMatch *, Key > observation_match_keys_
Definition: OMSFileStore.h:295
void createTableParentMatches_()
Create a database table for storing parent matches (e.g. proteins for a peptide)
In-Memory representation of a mass spectrometry run.
Definition: MSExperiment.h:46
Interface for classes that can store arbitrary meta information (Type-Name-Value tuples).
Definition: MetaInfoInterface.h:35
Base class for all classes that want to report their progress.
Definition: ProgressLogger.h:27
LogType
Possible log types.
Definition: ProgressLogger.h:43
A more convenient string class.
Definition: String.h:34
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:101
bool execAndReset(SQLite::Statement &query, int expected_modifications)
Execute and reset an SQL query.
void execWithExceptionAndReset(SQLite::Statement &query, int expected_modifications, int line, const char *function, const char *context)
If execAndReset() returns false, call raiseDBError_()
void raiseDBError_(const String &error, int line, const char *function, const String &context, const String &query="")
Raise a more informative database error.
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:22
Definition: OMSFileLoad.h:18
Definition: AppliedProcessingStep.h:30
Variant type holding Peptide/Compound/Oligo references and convenience functions.
Definition: IdentifiedMolecule.h:29