OpenMS
Loading...
Searching...
No Matches
IdentificationDataConverter.h
Go to the documentation of this file.
1// Copyright (c) 2002-present, OpenMS Inc. -- EKU Tuebingen, ETH Zurich, and FU Berlin
2// SPDX-License-Identifier: BSD-3-Clause
3//
4// --------------------------------------------------------------------------
5// $Maintainer: Hendrik Weisser $
6// $Authors: Hendrik Weisser $
7// --------------------------------------------------------------------------
8
9#pragma once
10
13#include <OpenMS/FORMAT/MzTab.h>
17#include <set>
18
19namespace OpenMS
20{
21 class FeatureMap;
22
23 class OPENMS_DLLAPI IdentificationDataConverter
24 {
25 public:
26
28 static void importIDs(IdentificationData& id_data,
29 const std::vector<ProteinIdentification>& proteins,
30 const PeptideIdentificationList& peptides);
31
37 static void exportIDs(const IdentificationData& id_data,
38 std::vector<ProteinIdentification>& proteins,
40 bool export_ids_wo_scores = false);
41
50 static MzTab exportMzTab(const IdentificationData& id_data,
51 const std::set<std::string>& observation_meta_keys = {},
52 const std::set<std::string>& match_meta_keys = {});
53
55 static void importSequences(IdentificationData& id_data,
56 const std::vector<FASTAFile::FASTAEntry>& fasta,
58 IdentificationData::MoleculeType::PROTEIN,
59 const std::string& decoy_pattern = "");
60
63 const IdentificationData::ParentMatches& parent_matches, PeptideHit& hit);
64
71 static void importFeatureIDs(FeatureMap& features, bool clear_original = true);
72
79 static void exportFeatureIDs(FeatureMap& features, bool clear_original = true);
80
87 static void importConsensusIDs(ConsensusMap& consensus, bool clear_original = true);
88
95 static void exportConsensusIDs(ConsensusMap& consensus, bool clear_original = true);
96
97 protected:
98
99 using StepOpt = std::optional<IdentificationData::ProcessingStepRef>;
100
103 {
104 bool operator()(const StepOpt& left, const StepOpt& right) const
105 {
106 // @TODO: should runs without associated step go first or last?
107 if (!left) return bool(right);
108 if (!right) return false;
109 return **left < **right;
110 }
111 };
112
115 {
117 const PeptideIdentification& right) const
118 {
119 // @TODO: should IDs without RT go first or last?
120 if (left.hasRT())
121 {
122 if (right.hasRT())
123 {
124 if (right.getRT() != left.getRT())
125 {
126 return left.getRT() < right.getRT();
127 } // else: compare by m/z (below)
128 }
129 else
130 {
131 return false;
132 }
133 }
134 else if (right.hasRT())
135 {
136 return true;
137 }
138 // no RTs or same RTs -> try to compare by m/z:
139 if (left.hasMZ())
140 {
141 if (right.hasMZ())
142 {
143 return left.getMZ() < right.getMZ();
144 }
145 else
146 {
147 return false;
148 }
149 }
150 // if both PI's have nothing, return false (to ensure 'x < x' is false for strict weak ordering)
151 return right.hasMZ();
152 }
153 };
154
156 template <typename MzTabSectionRow>
159 std::vector<MzTabSectionRow>& output,
160 std::map<IdentificationData::ScoreTypeRef, Size>& score_map)
161 {
162 MzTabSectionRow row;
163 row.accession.set(parent.accession);
164 exportStepsAndScoresToMzTab_(parent.steps_and_scores, row.search_engine,
165 row.best_search_engine_score, score_map);
166 row.description.set(parent.description);
167 row.coverage.set(parent.coverage);
168 if (!parent.sequence.empty())
169 {
171 opt_seq.first = "opt_sequence";
172 opt_seq.second.set(parent.sequence);
173 row.opt_.push_back(opt_seq);
174 }
175 output.push_back(row);
176 }
177
179 template <typename MzTabSectionRow, typename IdentSeq>
181 const IdentSeq& identified, std::vector<MzTabSectionRow>& output,
182 std::map<IdentificationData::ScoreTypeRef, Size>& score_map)
183 {
184 MzTabSectionRow row;
185 // @TODO: handle modifications properly
186 row.sequence.set(identified.sequence.toString());
187 exportStepsAndScoresToMzTab_(identified.steps_and_scores,
188 row.search_engine,
189 row.best_search_engine_score, score_map);
190 if (identified.parent_matches.empty()) // no parent information given
191 {
192 // row.unique.set(false); // leave this unset?
193 output.push_back(row);
194 }
195 else // generate entries (with duplicated data) for every accession
196 {
197 // in mzTab, "unique" means "peptide is unique for this protein"
198 row.unique.set(identified.parent_matches.size() == 1);
199 for (const auto& match_pair : identified.parent_matches)
200 {
201 row.accession.set(match_pair.first->accession);
202 for (const IdentificationData::ParentMatch& match :
203 match_pair.second)
204 {
205 MzTabSectionRow copy = row;
206 addMzTabMoleculeParentContext_(match, copy);
207 output.push_back(copy);
208 }
209 }
210 }
211 }
212
214 template <typename MzTabSectionRow>
216 const std::string& sequence,
217 const IdentificationData::ObservationMatch& match, double calc_mass,
218 std::vector<MzTabSectionRow>& output,
219 std::map<IdentificationData::ScoreTypeRef, Size>& score_map,
220 std::map<IdentificationData::InputFileRef, Size>& file_map,
221 const std::set<std::string>& observation_meta_keys,
222 const std::set<std::string>& match_meta_keys)
223 {
224 MzTabSectionRow xsm; // PSM or OSM
225 // @TODO: handle modifications properly
226 xsm.sequence.set(sequence);
227 exportStepsAndScoresToMzTab_(match.steps_and_scores, xsm.search_engine,
228 xsm.search_engine_score, score_map);
230 std::vector<MzTabDouble> rts(1);
231 rts[0].set(query.rt);
232 xsm.retention_time.set(rts);
233 xsm.charge.set(match.charge);
234 xsm.exp_mass_to_charge.set(query.mz);
235 xsm.calc_mass_to_charge.set(calc_mass / abs(match.charge));
236 xsm.spectra_ref.setMSFile(file_map[query.input_file]);
237 xsm.spectra_ref.setSpecRef(query.data_id);
238 // Export only metadata selected by the caller, using the format's generic
239 // column helper. Adduct identity is an intrinsic ObservationMatch field.
240 MetaInfoInterface optional_meta;
241 std::set<std::string> optional_keys = observation_meta_keys;
242 optional_keys.insert(match_meta_keys.begin(), match_meta_keys.end());
243 for (const auto& key : observation_meta_keys)
244 {
245 if (query.metaValueExists(key)) optional_meta.setMetaValue(key, query.getMetaValue(key));
246 }
247 for (const auto& key : match_meta_keys)
248 {
249 if (match.metaValueExists(key)) optional_meta.setMetaValue(key, match.getMetaValue(key));
250 }
251 if (match.adduct_opt)
252 {
253 optional_keys.insert("adduct");
254 optional_meta.setMetaValue("adduct", (*match.adduct_opt)->getName());
255 }
256 MzTab::addMetaInfoToOptionalColumns(optional_keys, xsm.opt_, "global", optional_meta);
257 // don't repeat data from the peptide section (e.g. accessions)
258 // why are "pre"/"post"/"start"/"end" not in the peptide section?!
259 output.push_back(xsm);
260 }
261
264 const IdentificationData::AppliedProcessingSteps& steps_and_scores,
265 MzTabParameterList& steps_out, std::map<Size, MzTabDouble>& scores_out,
266 std::map<IdentificationData::ScoreTypeRef, Size>& score_map);
267
269 static void addMzTabSEScores_(
270 const std::map<IdentificationData::ScoreTypeRef, Size>& scores,
271 std::map<Size, MzTabParameter>& output);
272
277
282
286 IdentificationData& id_data);
287
291
295 ProteinIdentification& protein);
296
297 static void handleFeatureImport_(Feature& feature, const IntList& indexes,
299 Size& id_counter, bool clear_original);
300
301 static void handleFeatureExport_(Feature& feature, const IntList& indexes,
302 IdentificationData& id_data, Size& id_counter);
303 };
304}
A container for consensus elements.
Definition ConsensusMap.h:67
A container for features.
Definition FeatureMap.h:78
An LC-MS feature.
Definition Feature.h:46
Definition IdentificationDataConverter.h:24
static void addMzTabSEScores_(const std::map< IdentificationData::ScoreTypeRef, Size > &scores, std::map< Size, MzTabParameter > &output)
Helper function to add search engine score entries to MzTab's meta data section.
static void exportFeatureIDs(FeatureMap &features, bool clear_original=true)
Convert IDs in a feature map to legacy peptide/protein identifications.
static MzTab exportMzTab(const IdentificationData &id_data, const std::set< std::string > &observation_meta_keys={}, const std::set< std::string > &match_meta_keys={})
Export to mzTab format with caller-selected optional metadata columns.
static void exportObservationMatchToMzTab_(const std::string &sequence, const IdentificationData::ObservationMatch &match, double calc_mass, std::vector< MzTabSectionRow > &output, std::map< IdentificationData::ScoreTypeRef, Size > &score_map, std::map< IdentificationData::InputFileRef, Size > &file_map, const std::set< std::string > &observation_meta_keys, const std::set< std::string > &match_meta_keys)
Export an input match (peptide- or oligonucleotide-spectrum match) to mzTab.
Definition IdentificationDataConverter.h:215
static IdentificationData::SearchParamRef importDBSearchParameters_(const ProteinIdentification::SearchParameters &pisp, IdentificationData &id_data)
Helper function to import DB search parameters from legacy format.
static void addMzTabMoleculeParentContext_(const IdentificationData::ParentMatch &match, MzTabOligonucleotideSectionRow &row)
Helper function for exportPeptideOrOligoToMzTab_() - oligonucleotide variant.
static ProteinIdentification::SearchParameters exportDBSearchParameters_(IdentificationData::SearchParamRef ref)
Helper function to export DB search parameters to legacy format.
static void exportPeptideOrOligoToMzTab_(const IdentSeq &identified, std::vector< MzTabSectionRow > &output, std::map< IdentificationData::ScoreTypeRef, Size > &score_map)
Export an identified sequence (peptide or oligonucleotide, but not small molecule/compound) to mzTab.
Definition IdentificationDataConverter.h:180
static void handleFeatureImport_(Feature &feature, const IntList &indexes, PeptideIdentificationList &peptides, Size &id_counter, bool clear_original)
static void importSequences(IdentificationData &id_data, const std::vector< FASTAFile::FASTAEntry > &fasta, IdentificationData::MoleculeType type=IdentificationData::MoleculeType::PROTEIN, const std::string &decoy_pattern="")
Import FASTA sequences as parent sequences.
static void importIDs(IdentificationData &id_data, const std::vector< ProteinIdentification > &proteins, const PeptideIdentificationList &peptides)
Import from legacy peptide/protein identifications.
static void exportIDs(const IdentificationData &id_data, std::vector< ProteinIdentification > &proteins, PeptideIdentificationList &peptides, bool export_ids_wo_scores=false)
Export to legacy peptide/protein identifications.
static void handleFeatureExport_(Feature &feature, const IntList &indexes, IdentificationData &id_data, Size &id_counter)
std::optional< IdentificationData::ProcessingStepRef > StepOpt
Definition IdentificationDataConverter.h:99
static void exportParentSequenceToMzTab_(const IdentificationData::ParentSequence &parent, std::vector< MzTabSectionRow > &output, std::map< IdentificationData::ScoreTypeRef, Size > &score_map)
Export a parent sequence (protein or nucleic acid) to mzTab.
Definition IdentificationDataConverter.h:157
static void exportStepsAndScoresToMzTab_(const IdentificationData::AppliedProcessingSteps &steps_and_scores, MzTabParameterList &steps_out, std::map< Size, MzTabDouble > &scores_out, std::map< IdentificationData::ScoreTypeRef, Size > &score_map)
Helper function to add processing steps (search engines) and their scores to MzTab.
static void exportMSRunInformation_(IdentificationData::ProcessingStepRef step_ref, ProteinIdentification &protein)
Helper function to export (primary) MS run information to legacy format.
static void importFeatureIDs(FeatureMap &features, bool clear_original=true)
Convert IDs from legacy peptide/protein identifications in a feature map.
static void exportParentMatches(const IdentificationData::ParentMatches &parent_matches, PeptideHit &hit)
Convert parent matches to peptide evidences.
static void exportConsensusIDs(ConsensusMap &consensus, bool clear_original=true)
Convert IDs in a consensus map to legacy peptide/protein identifications.
static void addMzTabMoleculeParentContext_(const IdentificationData::ParentMatch &match, MzTabPeptideSectionRow &row)
Helper function for exportPeptideOrOligoToMzTab_() - peptide variant.
static void importConsensusIDs(ConsensusMap &consensus, bool clear_original=true)
Convert IDs from legacy peptide/protein identifications in a consensus map.
Definition IdentificationData.h:87
IdentificationDataInternal::ParentMatches ParentMatches
Definition IdentificationData.h:138
Interface for classes that can store arbitrary meta information (Type-Name-Value tuples).
Definition MetaInfoInterface.h:35
const DataValue & getMetaValue(const std::string &name) const
Returns the value corresponding to a string, or DataValue::EMPTY if not found.
void setMetaValue(const std::string &name, const DataValue &value)
Sets the DataValue corresponding to a name.
bool metaValueExists(const std::string &name) const
Returns whether an entry with the given name exists.
Definition MzTabBase.h:242
Data model of MzTab files. Please see the official MzTab specification at https://code....
Definition MzTab.h:459
Represents a single spectrum match (candidate) for a specific tandem mass spectrum (MS/MS).
Definition PeptideHit.h:52
Container for peptide identifications from multiple spectra.
Definition PeptideIdentificationList.h:66
Represents the set of candidates (SpectrumMatches) identified for a single precursor spectrum.
Definition PeptideIdentification.h:66
double getRT() const
returns the RT of the MS2 spectrum where the identification occurred
bool hasMZ() const
shortcut for isnan(getRT())
bool hasRT() const
shortcut for isnan(getRT())
double getMZ() const
returns the MZ of the MS2 spectrum
Representation of a protein identification run.
Definition ProteinIdentification.h:55
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition Types.h:97
std::vector< Int > IntList
Vector of signed integers.
Definition TypeAliases.h:24
MoleculeType
Definition MetaData.h:40
Main OpenMS namespace.
Definition openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19
std::pair< std::string, MzTabString > MzTabOptionalColumnEntry
Definition MzTabBase.h:202
OLI - Oligonucleotide section (table-based)
Definition MzTab.h:374
PEP - Peptide section (Table based)
Definition MzTab.h:220
Functor for ordering peptide IDs by RT and m/z (if available)
Definition IdentificationDataConverter.h:115
bool operator()(const PeptideIdentification &left, const PeptideIdentification &right) const
Definition IdentificationDataConverter.h:116
Functor for ordering StepOpt (by date of the steps, if available):
Definition IdentificationDataConverter.h:103
bool operator()(const StepOpt &left, const StepOpt &right) const
Definition IdentificationDataConverter.h:104
Wrapper that adds operator< to iterators, so they can be used as (part of) keys in maps/sets or multi...
Definition MetaData.h:20
Representation of a search hit (e.g. peptide-spectrum match).
Definition ObservationMatch.h:47
AdductOpt adduct_opt
optional reference to adduct
Definition ObservationMatch.h:54
Int charge
Definition ObservationMatch.h:52
ObservationRef observation_ref
Definition ObservationMatch.h:50
Representation of an observation, e.g. a spectrum or feature, in an input data file.
Definition Observation.h:26
double mz
Definition Observation.h:33
std::string data_id
Spectrum or feature ID (from the file referenced by input_file)
Definition Observation.h:28
InputFileRef input_file
Reference to the input file.
Definition Observation.h:31
double rt
Definition Observation.h:33
Meta data for the association between an identified molecule (e.g. peptide) and a parent sequence (e....
Definition ParentMatch.h:20
Representation of a parent sequence that is identified only indirectly (e.g. a protein).
Definition ParentSequence.h:23
std::string description
Definition ParentSequence.h:32
double coverage
sequence coverage as a fraction between 0 and 1
Definition ParentSequence.h:34
std::string sequence
Definition ParentSequence.h:30
std::string accession
Definition ParentSequence.h:24
AppliedProcessingSteps steps_and_scores
Definition ScoredProcessingResult.h:24
Search parameters of the DB search.
Definition ProteinIdentification.h:194