OpenMS  2.7.0
MzTabFile.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-2021.
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: Timo Sachsenberg $
32 // $Authors: Timo Sachsenberg $
33 // --------------------------------------------------------------------------
34 
35 #pragma once
36 
37 #include <OpenMS/FORMAT/MzTab.h>
38 
43 
44 #include <boost/math/special_functions/fpclassify.hpp>
45 
46 #include <vector>
47 #include <algorithm>
48 
49 namespace OpenMS
50 {
51  class String;
52  class SVOutStream;
58  class OPENMS_DLLAPI MzTabFile
59  {
60  public:
65 
66  typedef std::map<std::pair<String, String>, std::vector<PeptideHit> > MapAccPepType;
67 
68  // store MzTab file
69  void store(const String& filename, const MzTab& mz_tab) const;
70 
71  // stream IDs to file
72  void store(
73  const String& filename,
74  const std::vector<ProteinIdentification>& protein_identifications,
75  const std::vector<PeptideIdentification>& peptide_identifications,
76  bool first_run_inference_only,
77  bool export_empty_pep_ids = false,
78  bool export_all_psms = false,
79  const String& title = "ID export from OpenMS");
80 
81  // stream ConsensusMap to file
82  void store(
83  const String& filename,
84  const ConsensusMap& cmap,
85  const bool first_run_inference_only,
86  const bool export_unidentified_features,
87  const bool export_unassigned_ids,
88  const bool export_subfeatures,
89  const bool export_empty_pep_ids = false,
90  const bool export_all_psms = false) const;
91 
92  // Set store behaviour of optional "reliability" and "uri" columns (default=no)
95  void storePSMReliabilityColumn(bool store);
97  void storeProteinUriColumn(bool store);
98  void storePeptideUriColumn(bool store);
99  void storePSMUriColumn(bool store);
100  void storeSmallMoleculeUriColumn(bool store);
101  void storeProteinGoTerms(bool store);
102 
103  // load MzTab file
104  void load(const String& filename, MzTab& mz_tab);
105 
106  protected:
123 
125 
129  const Size n_best_search_engine_scores,
130  const std::vector<String>& optional_columns,
131  const MzTabMetaData& meta,
132  size_t& n_columns) const;
133 
134  String generateMzTabSectionRow_(const MzTabProteinSectionRow& row, const std::vector<String>& optional_columns, const MzTabMetaData& meta, size_t& n_columns) const;
135 
136  String generateMzTabPeptideHeader_(Size search_ms_runs, Size n_best_search_engine_scores, Size n_search_engine_score, Size assays, Size study_variables, const std::vector<String>& optional_columns, size_t& n_columns) const;
137 
138  String generateMzTabSectionRow_(const MzTabPeptideSectionRow& row, const std::vector<String>& optional_columns, const MzTabMetaData& meta, size_t& n_columns) const;
139 
140  String generateMzTabPSMHeader_(Size n_search_engine_scores, const std::vector<String>& optional_columns, size_t& n_columns) const;
141 
142  String generateMzTabSectionRow_(const MzTabPSMSectionRow& row, const std::vector<String>& optional_columns, const MzTabMetaData& meta, size_t& n_columns) const;
143 
144  String generateMzTabSmallMoleculeHeader_(Size search_ms_runs, Size n_best_search_engine_scores, Size n_search_engine_score, Size assays, Size study_variables, const std::vector<String>& optional_columns, size_t& n_columns) const;
145 
146  String generateMzTabSectionRow_(const MzTabSmallMoleculeSectionRow& row, const std::vector<String>& optional_columns, const MzTabMetaData& meta, size_t& n_columns) const;
147 
148  String generateMzTabNucleicAcidHeader_(Size search_ms_runs, Size n_best_search_engine_scores, Size n_search_engine_scores, const std::vector<String>& optional_columns, size_t& n_columns) const;
149 
150  String generateMzTabSectionRow_(const MzTabNucleicAcidSectionRow& row, const std::vector<String>& optional_columns, const MzTabMetaData& meta, size_t& n_columns) const;
151 
152  String generateMzTabOligonucleotideHeader_(Size search_ms_runs, Size n_best_search_engine_scores, Size n_search_engine_score, const std::vector<String>& optional_columns, size_t& n_columns) const;
153 
154  String generateMzTabSectionRow_(const MzTabOligonucleotideSectionRow& row, const std::vector<String>& optional_columns, const MzTabMetaData& meta, size_t& n_columns) const;
155 
156  String generateMzTabOSMHeader_(Size n_search_engine_scores, const std::vector<String>& optional_columns, size_t& n_columns) const;
157 
158  String generateMzTabSectionRow_(const MzTabOSMSectionRow& row, const std::vector<String>& optional_columns, const MzTabMetaData& meta, size_t& n_columns) const;
159 
161  template <typename SectionRow> void generateMzTabSection_(const std::vector<SectionRow>& rows, const std::vector<String>& optional_columns, const MzTabMetaData& meta, StringList& output, size_t n_header_columns) const
162  {
163  output.reserve(output.size() + rows.size() + 1);
164  for (const auto& row : rows)
165  {
166  size_t n_section_columns = 0;
167  output.push_back(generateMzTabSectionRow_(row, optional_columns, meta, n_section_columns));
168  if (n_header_columns != n_section_columns) throw Exception::Postcondition(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, "Header and content differs in columns. Please report this bug to the OpenMS developers.");
169  }
170  }
171 
172  // auxiliary functions
173 
175  static void addOptionalColumnsToSectionRow_(const std::vector<String>& column_names, const std::vector<MzTabOptionalColumnEntry>& column_entries, StringList& output);
176 
177  // extract two integers from string (e.g. search_engine_score[1]_ms_run[2] -> 1,2)
178  static std::pair<int, int> extractIndexPairsFromBrackets_(const String& s);
179 
180  static void sortPSM_(std::vector<PeptideIdentification>::iterator begin, std::vector<PeptideIdentification>::iterator end);
181 
182  static void keepFirstPSM_(std::vector<PeptideIdentification>::iterator begin, std::vector<PeptideIdentification>::iterator end);
183 
185  static void partitionIntoRuns_(const std::vector<PeptideIdentification>& pep_ids,
186  const std::vector<ProteinIdentification>& pro_ids,
187  std::map<String, std::vector<PeptideIdentification> >& map_run_to_pepids,
188  std::map<String, std::vector<ProteinIdentification> >& map_run_to_proids
189  );
190 
191 
193  static void createProteinToPeptideLinks_(const std::map<String, std::vector<PeptideIdentification> >& map_run_to_pepids, MapAccPepType& map_run_accession_to_pephits);
194 
196  static String extractProteinAccession_(const PeptideHit& peptide_hit);
197 
199  static String extractPeptideModifications_(const PeptideHit& peptide_hit);
200 
202  static String mapSearchEngineToCvParam_(const String& openms_search_engine_name);
203 
204  static String mapSearchEngineScoreToCvParam_(const String& openms_search_engine_name, double score, String score_type);
205 
206  static String extractNumPeptides_(const String& common_identifier, const String& protein_accession,
207  const MapAccPepType& map_run_accession_to_peptides);
208 
209  // mzTab definition of distinct
210  static String extractNumPeptidesDistinct_(String common_identifier, String protein_accession,
211  const MapAccPepType& map_run_accession_to_peptides);
212 
213  // same as distinct but additional constraint of uniqueness (=maps to exactly one Protein)
214  static String extractNumPeptidesUnambiguous_(String common_identifier, String protein_accession,
215  const MapAccPepType& map_run_accession_to_peptides);
216 
217  static std::map<String, Size> extractNumberOfSubSamples_(const std::map<String, std::vector<ProteinIdentification> >& map_run_to_proids);
218 
219  static void writePeptideHeader_(SVOutStream& output, std::map<String, Size> n_sub_samples);
220 
221  static void writeProteinHeader_(SVOutStream& output, std::map<String, Size> n_sub_samples);
222 
223  static void writeProteinData_(SVOutStream& output,
224  const ProteinIdentification& prot_id,
225  Size run_count,
226  String input_filename,
227  bool has_coverage,
228  const MapAccPepType& map_run_accession_to_peptides,
229  const std::map<String, Size>& map_run_to_num_sub
230  );
231 
232  };
233 
234 } // namespace OpenMS
235 
A container for consensus elements.
Definition: ConsensusMap.h:88
Postcondition failed exception.
Definition: Exception.h:174
File adapter for MzTab files.
Definition: MzTabFile.h:59
static void sortPSM_(std::vector< PeptideIdentification >::iterator begin, std::vector< PeptideIdentification >::iterator end)
String generateMzTabProteinHeader_(const MzTabProteinSectionRow &reference_row, const Size n_best_search_engine_scores, const std::vector< String > &optional_columns, const MzTabMetaData &meta, size_t &n_columns) const
void storePSMUriColumn(bool store)
static void keepFirstPSM_(std::vector< PeptideIdentification >::iterator begin, std::vector< PeptideIdentification >::iterator end)
static std::pair< int, int > extractIndexPairsFromBrackets_(const String &s)
void store(const String &filename, const std::vector< ProteinIdentification > &protein_identifications, const std::vector< PeptideIdentification > &peptide_identifications, bool first_run_inference_only, bool export_empty_pep_ids=false, bool export_all_psms=false, const String &title="ID export from OpenMS")
String generateMzTabOSMHeader_(Size n_search_engine_scores, const std::vector< String > &optional_columns, size_t &n_columns) const
static String mapSearchEngineScoreToCvParam_(const String &openms_search_engine_name, double score, String score_type)
bool store_osm_uri_
Definition: MzTabFile.h:121
bool store_psm_uri_
Definition: MzTabFile.h:113
static void writePeptideHeader_(SVOutStream &output, std::map< String, Size > n_sub_samples)
bool store_smallmolecule_reliability_
Definition: MzTabFile.h:110
String generateMzTabSectionRow_(const MzTabPSMSectionRow &row, const std::vector< String > &optional_columns, const MzTabMetaData &meta, size_t &n_columns) const
bool store_protein_goterms_
Definition: MzTabFile.h:115
void storeSmallMoleculeReliabilityColumn(bool store)
bool store_protein_uri_
Definition: MzTabFile.h:111
bool store_nucleic_acid_goterms_
Definition: MzTabFile.h:122
void storePSMReliabilityColumn(bool store)
bool store_protein_reliability_
Definition: MzTabFile.h:107
static String mapSearchEngineToCvParam_(const String &openms_search_engine_name)
Map search engine identifier to CV, param etc.
bool store_psm_reliability_
Definition: MzTabFile.h:109
String generateMzTabSectionRow_(const MzTabSmallMoleculeSectionRow &row, const std::vector< String > &optional_columns, const MzTabMetaData &meta, size_t &n_columns) const
String generateMzTabSectionRow_(const MzTabProteinSectionRow &row, const std::vector< String > &optional_columns, const MzTabMetaData &meta, size_t &n_columns) const
static void writeProteinData_(SVOutStream &output, const ProteinIdentification &prot_id, Size run_count, String input_filename, bool has_coverage, const MapAccPepType &map_run_accession_to_peptides, const std::map< String, Size > &map_run_to_num_sub)
bool store_oligonucleotide_uri_
Definition: MzTabFile.h:120
String generateMzTabSectionRow_(const MzTabNucleicAcidSectionRow &row, const std::vector< String > &optional_columns, const MzTabMetaData &meta, size_t &n_columns) const
static void writeProteinHeader_(SVOutStream &output, std::map< String, Size > n_sub_samples)
static void addOptionalColumnsToSectionRow_(const std::vector< String > &column_names, const std::vector< MzTabOptionalColumnEntry > &column_entries, StringList &output)
Helper function for "generateMzTabSectionRow_" functions.
~MzTabFile()
Destructor.
String generateMzTabNucleicAcidHeader_(Size search_ms_runs, Size n_best_search_engine_scores, Size n_search_engine_scores, const std::vector< String > &optional_columns, size_t &n_columns) const
static String extractProteinAccession_(const PeptideHit &peptide_hit)
Extracts, if possible a unique protein accession for a peptide hit in mzTab format....
static String extractPeptideModifications_(const PeptideHit &peptide_hit)
Extracts, modifications and positions of a peptide hit in mzTab format.
static String extractNumPeptidesDistinct_(String common_identifier, String protein_accession, const MapAccPepType &map_run_accession_to_peptides)
bool store_smallmolecule_uri_
Definition: MzTabFile.h:114
static void createProteinToPeptideLinks_(const std::map< String, std::vector< PeptideIdentification > > &map_run_to_pepids, MapAccPepType &map_run_accession_to_pephits)
create links from protein to peptides
bool store_oligonucleotide_reliability_
Definition: MzTabFile.h:117
String generateMzTabSmallMoleculeHeader_(Size search_ms_runs, Size n_best_search_engine_scores, Size n_search_engine_score, Size assays, Size study_variables, const std::vector< String > &optional_columns, size_t &n_columns) const
String generateMzTabOligonucleotideHeader_(Size search_ms_runs, Size n_best_search_engine_scores, Size n_search_engine_score, const std::vector< String > &optional_columns, size_t &n_columns) const
String generateMzTabPeptideHeader_(Size search_ms_runs, Size n_best_search_engine_scores, Size n_search_engine_score, Size assays, Size study_variables, const std::vector< String > &optional_columns, size_t &n_columns) const
bool store_osm_reliability_
Definition: MzTabFile.h:118
void store(const String &filename, const MzTab &mz_tab) const
std::map< std::pair< String, String >, std::vector< PeptideHit > > MapAccPepType
Definition: MzTabFile.h:66
void store(const String &filename, const ConsensusMap &cmap, const bool first_run_inference_only, const bool export_unidentified_features, const bool export_unassigned_ids, const bool export_subfeatures, const bool export_empty_pep_ids=false, const bool export_all_psms=false) const
void storeProteinReliabilityColumn(bool store)
void storeProteinGoTerms(bool store)
void generateMzTabMetaDataSection_(const MzTabMetaData &map, StringList &sl) const
String generateMzTabSectionRow_(const MzTabPeptideSectionRow &row, const std::vector< String > &optional_columns, const MzTabMetaData &meta, size_t &n_columns) const
static String extractNumPeptidesUnambiguous_(String common_identifier, String protein_accession, const MapAccPepType &map_run_accession_to_peptides)
bool store_nucleic_acid_reliability_
Definition: MzTabFile.h:116
void storeSmallMoleculeUriColumn(bool store)
void load(const String &filename, MzTab &mz_tab)
MzTabFile()
Default constructor.
bool store_peptide_uri_
Definition: MzTabFile.h:112
String generateMzTabSectionRow_(const MzTabOSMSectionRow &row, const std::vector< String > &optional_columns, const MzTabMetaData &meta, size_t &n_columns) const
static String extractNumPeptides_(const String &common_identifier, const String &protein_accession, const MapAccPepType &map_run_accession_to_peptides)
String generateMzTabPSMHeader_(Size n_search_engine_scores, const std::vector< String > &optional_columns, size_t &n_columns) const
String generateMzTabSectionRow_(const MzTabOligonucleotideSectionRow &row, const std::vector< String > &optional_columns, const MzTabMetaData &meta, size_t &n_columns) const
static std::map< String, Size > extractNumberOfSubSamples_(const std::map< String, std::vector< ProteinIdentification > > &map_run_to_proids)
void storePeptideUriColumn(bool store)
bool store_peptide_reliability_
Definition: MzTabFile.h:108
void storeProteinUriColumn(bool store)
void generateMzTabSection_(const std::vector< SectionRow > &rows, const std::vector< String > &optional_columns, const MzTabMetaData &meta, StringList &output, size_t n_header_columns) const
Generate an mzTab section comprising multiple rows of the same type and perform sanity check.
Definition: MzTabFile.h:161
void storePeptideReliabilityColumn(bool store)
static void partitionIntoRuns_(const std::vector< PeptideIdentification > &pep_ids, const std::vector< ProteinIdentification > &pro_ids, std::map< String, std::vector< PeptideIdentification > > &map_run_to_pepids, std::map< String, std::vector< ProteinIdentification > > &map_run_to_proids)
Extract protein and peptide identifications for each run. maps are assumed empty.
bool store_nucleic_acid_uri_
Definition: MzTabFile.h:119
all meta data of a mzTab file. Please refer to specification for documentation.
Definition: MzTab.h:470
Data model of MzTab files. Please see the official MzTab specification at https://code....
Definition: MzTab.h:809
Representation of a peptide hit.
Definition: PeptideHit.h:57
Representation of a protein identification run.
Definition: ProteinIdentification.h:72
Stream class for writing to comma/tab/...-separated values files.
Definition: SVOutStream.h:56
A more convenient string class.
Definition: String.h:61
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
std::vector< String > StringList
Vector of String.
Definition: ListUtils.h:70
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
NUC - Nucleic acid section (table-based)
Definition: MzTab.h:693
OSM - OSM (oligonucleotide-spectrum match) section (table-based)
Definition: MzTab.h:763
OLI - Oligonucleotide section (table-based)
Definition: MzTab.h:728
PEP - Peptide section (Table based)
Definition: MzTab.h:574
SML Small molecule section (table based)
Definition: MzTab.h:663
PSM - PSM section (Table based)
Definition: MzTab.h:612
PRT - Protein section (Table based)
Definition: MzTab.h:535