OpenMS
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-2023.
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 
41 
42 namespace SQLite
43 {
44  class Database;
45  class Exception;
46  class Statement;
47 }
48 
49 namespace OpenMS
50 {
51  namespace Internal
52  {
66  void raiseDBError_(const String& error, int line, const char* function, const String& context, const String& query = "");
67 
73  bool execAndReset(SQLite::Statement& query, int expected_modifications);
74 
76  void execWithExceptionAndReset(SQLite::Statement& query, int expected_modifications, int line, const char* function, const char* context);
77 
78 
85  {
86  public:
88  using Key = int64_t; //std::decltype(((SQLite::Database*)nullptr)->getLastInsertRowid());
89 
101  OMSFileStore(const String& filename, LogType log_type);
102 
109 
111  void store(const IdentificationData& id_data);
112 
114  void store(const FeatureMap& features);
115 
117  void store(const ConsensusMap& consensus);
118 
119  private:
127  void createTable_(const String& name, const String& definition, bool may_exist = false);
128 
131 
134 
136  void createTableMetaInfo_(const String& parent_table,
137  const String& key_column = "id");
138 
141 
143  Key storeCVTerm_(const CVTerm& cv_term);
144 
146  void storeMetaInfo_(const MetaInfoInterface& info, const String& parent_table,
147  Key parent_id);
148 
150  template<class MetaInfoInterfaceContainer, class DBKeyTable>
151  void storeMetaInfos_(const MetaInfoInterfaceContainer& container,
152  const String& parent_table, const DBKeyTable& db_keys)
153  {
154  bool table_created = false;
155  for (const auto& element : container)
156  {
157  if (!element.isMetaEmpty())
158  {
159  if (!table_created)
160  {
161  createTableMetaInfo_(parent_table);
162  table_created = true;
163  }
164  storeMetaInfo_(element, parent_table, db_keys.at(&element));
165  }
166  }
167  }
168 
172  void storeScoreTypes_(const IdentificationData& id_data);
173 
175  void storeInputFiles_(const IdentificationData& id_data);
176 
179 
182 
185 
188 
191 
194 
197 
200 
202  void storeAdducts_(const IdentificationData& id_data);
203 
206 
209 
211  void createTableAppliedProcessingStep_(const String& parent_table);
212 
215  const IdentificationData::AppliedProcessingStep& step, Size step_order,
216  const String& parent_table, Key parent_id);
217 
220 
223 
226 
229  const IdentificationData::ParentMatches& matches, Key molecule_id);
230 
232  template<class ScoredProcessingResultContainer, class DBKeyTable>
233  void storeScoredProcessingResults_(const ScoredProcessingResultContainer& container,
234  const String& parent_table, const DBKeyTable& db_keys)
235  {
236  bool table_created = false;
237  for (const auto& element : container)
238  {
239  if (!element.steps_and_scores.empty())
240  {
241  if (!table_created)
242  {
243  createTableAppliedProcessingStep_(parent_table);
244  table_created = true;
245  }
246  Size counter = 0;
247  for (const IdentificationData::AppliedProcessingStep& step : element.steps_and_scores)
248  {
249  storeAppliedProcessingStep_(step, ++counter, parent_table, db_keys.at(&element));
250  }
251  }
252  }
253  storeMetaInfos_(container, parent_table, db_keys);
254  }
256 
260  void createTableBaseFeature_(bool with_metainfo, bool with_idmatches);
261 
263  void storeBaseFeature_(const BaseFeature& feature, int feature_id, int parent_id);
264 
266  void storeFeatures_(const FeatureMap& features);
267 
270  const Feature& feature, int& feature_id, int parent_id);
271 
273  template <class FeatureContainer, class Predicate>
274  bool anyFeaturePredicate_(const FeatureContainer& features, const Predicate& pred)
275  {
276  if (features.empty()) return false;
277  for (const Feature& feature : features)
278  {
279  if (pred(feature)) return true;
280  if (anyFeaturePredicate_(feature.getSubordinates(), pred)) return true;
281  }
282  return false;
283  }
284 
286  template <class MapType>
287  void storeMapMetaData_(const MapType& features, const String& experiment_type = "");
288 
290  void storeDataProcessing_(const std::vector<DataProcessing>& data_processing);
291 
293  void storeConsensusFeatures_(const ConsensusMap& consensus);
294 
298 
300  std::unique_ptr<SQLite::Database> db_;
301 
303  std::map<std::string, std::unique_ptr<SQLite::Statement>> prepared_queries_;
304 
305  // mapping between loaded data and database keys:
306  // @NOTE: in principle we could use `unordered_map` here for efficiency,
307  // but that gives compiler errors when pointers or iterators (`...Ref`)
308  // are used as keys (because they aren't hashable?)
309  std::map<const IdentificationData::ScoreType*, Key> score_type_keys_;
310  std::map<const IdentificationData::InputFile*, Key> input_file_keys_;
311  std::map<const IdentificationData::ProcessingSoftware*, Key> processing_software_keys_;
312  std::map<const IdentificationData::ProcessingStep*, Key> processing_step_keys_;
313  std::map<const IdentificationData::DBSearchParam*, Key> search_param_keys_;
314  std::map<const IdentificationData::Observation*, Key> observation_keys_;
315  std::map<const IdentificationData::ParentSequence*, Key> parent_sequence_keys_;
316  std::map<const IdentificationData::ParentGroupSet*, Key> parent_grouping_keys_;
317  std::map<const IdentificationData::IdentifiedCompound*, Key> identified_compound_keys_;
318  std::map<const IdentificationData::IdentifiedPeptide*, Key> identified_peptide_keys_;
319  std::map<const IdentificationData::IdentifiedOligo*, Key> identified_oligo_keys_;
320  std::map<const AdductInfo*, Key> adduct_keys_;
321  std::map<const IdentificationData::ObservationMatch*, Key> observation_match_keys_;
322  // for feature/consensus maps:
323  std::map<const DataProcessing*, Key> feat_processing_keys_;
324  };
325  }
326 }
A basic LC-MS feature.
Definition: BaseFeature.h:59
Representation of controlled vocabulary term.
Definition: CVTerm.h:53
A container for consensus elements.
Definition: ConsensusMap.h:92
A container for features.
Definition: FeatureMap.h:106
An LC-MS feature.
Definition: Feature.h:72
Definition: IdentificationData.h:113
IdentificationDataInternal::ParentMatches ParentMatches
Definition: IdentificationData.h:164
Helper class for storing .oms files (SQLite format)
Definition: OMSFileStore.h:85
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:274
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:309
void createTableBaseFeature_(bool with_metainfo, bool with_idmatches)
std::map< const IdentificationData::ProcessingStep *, Key > processing_step_keys_
Definition: OMSFileStore.h:312
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:151
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:314
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:311
std::map< const IdentificationData::IdentifiedPeptide *, Key > identified_peptide_keys_
Definition: OMSFileStore.h:318
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:313
std::map< const IdentificationData::InputFile *, Key > input_file_keys_
Definition: OMSFileStore.h:310
std::map< const AdductInfo *, Key > adduct_keys_
Definition: OMSFileStore.h:320
int64_t Key
< Type used for database keys
Definition: OMSFileStore.h:88
std::map< const IdentificationData::ParentGroupSet *, Key > parent_grouping_keys_
Definition: OMSFileStore.h:316
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:319
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:303
std::map< const DataProcessing *, Key > feat_processing_keys_
Definition: OMSFileStore.h:323
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:300
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:317
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:315
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:233
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:321
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:72
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
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:48
Definition: OMSFileLoad.h:44
Definition: AppliedProcessingStep.h:56
Variant type holding Peptide/Compound/Oligo references and convenience functions.
Definition: IdentifiedMolecule.h:55