OpenMS  2.6.0
MapAlignmentAlgorithmIdentification.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-2020.
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: Eva Lange, Clemens Groepl, Hendrik Weisser $
33 // --------------------------------------------------------------------------
34 
35 #pragma once
36 
46 
47 #include <cmath> // for "abs"
48 #include <limits> // for "max"
49 #include <map>
50 
51 namespace OpenMS
52 {
72  public DefaultParamHandler,
73  public ProgressLogger
74  {
75 public:
78 
81 
82  // Set a reference for the alignment
83  template <typename DataType> void setReference(DataType& data)
84  {
85  reference_.clear();
86  if (data.empty()) return; // empty input resets the reference
87  use_feature_rt_ = param_.getValue("use_feature_rt").toBool();
88  SeqToList rt_data;
89  bool sorted = getRetentionTimes_(data, rt_data);
90  computeMedians_(rt_data, reference_, sorted);
91  if (reference_.empty())
92  {
93  throw Exception::MissingInformation(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, "Could not extract retention time information from the reference file");
94  }
95  }
96 
106  template <typename DataType>
107  void align(std::vector<DataType>& data,
108  std::vector<TransformationDescription>& transformations,
109  Int reference_index = -1)
110  {
111  checkParameters_(data.size());
112  startProgress(0, 3, "aligning maps");
113 
114  reference_index_ = reference_index;
115  // is reference one of the input files?
116  bool use_internal_reference = (reference_index >= 0);
117  if (use_internal_reference)
118  {
119  if (reference_index >= static_cast<Int>(data.size()))
120  {
121  throw Exception::IndexOverflow(__FILE__, __LINE__,
122  OPENMS_PRETTY_FUNCTION, reference_index,
123  data.size());
124  }
125  setReference(data[reference_index]);
126  }
127 
128  // one set of RT data for each input map, except reference (if any):
129  std::vector<SeqToList> rt_data(data.size() - use_internal_reference);
130  bool all_sorted = true;
131  for (Size i = 0, j = 0; i < data.size(); ++i)
132  {
133  if ((reference_index >= 0) && (i == Size(reference_index)))
134  {
135  continue; // skip reference map, if any
136  }
137  all_sorted &= getRetentionTimes_(data[i], rt_data[j++]);
138  }
139  setProgress(1);
140 
141  computeTransformations_(rt_data, transformations, all_sorted);
142  setProgress(2);
143 
144  setProgress(3);
145  endProgress();
146  }
147 
148 protected:
149 
151  typedef std::map<String, DoubleList> SeqToList;
152 
154  typedef std::map<String, double> SeqToValue;
155 
158 
161 
164 
167 
169  double min_score_;
170 
173 
175  bool (*better_) (double,double) = [](double, double) {return true;};
176 
186  void computeMedians_(SeqToList& rt_data, SeqToValue& medians,
187  bool sorted = false);
188 
197  bool getRetentionTimes_(std::vector<PeptideIdentification>& peptides,
198  SeqToList& rt_data);
199 
208  bool getRetentionTimes_(PeakMap& experiment, SeqToList& rt_data);
209 
224  template <typename MapType>
225  bool getRetentionTimes_(MapType& features, SeqToList& rt_data)
226  {
227  if (!score_cutoff_)
228  {
229  better_ = [](double, double)
230  {return true;};
231  }
232  else if (features[0].getPeptideIdentifications()[0].isHigherScoreBetter())
233  {
234  better_ = [](double a, double b)
235  { return a >= b; };
236  }
237  else
238  {
239  better_ = [](double a, double b)
240  { return a <= b; };
241  }
242 
243  for (typename MapType::Iterator feat_it = features.begin();
244  feat_it != features.end(); ++feat_it)
245  {
246  if (use_feature_rt_)
247  {
248  // find the peptide ID closest in RT to the feature centroid:
249  String sequence;
250  double rt_distance = std::numeric_limits<double>::max();
251  bool any_hit = false;
252  for (std::vector<PeptideIdentification>::iterator pep_it =
253  feat_it->getPeptideIdentifications().begin(); pep_it !=
254  feat_it->getPeptideIdentifications().end(); ++pep_it)
255  {
256  if (!pep_it->getHits().empty())
257  {
258  any_hit = true;
259  double current_distance = fabs(pep_it->getRT() -
260  feat_it->getRT());
261  if (current_distance < rt_distance)
262  {
263  pep_it->sort();
264  if (better_(pep_it->getHits()[0].getScore(), min_score_))
265  {
266  sequence = pep_it->getHits()[0].getSequence().toString();
267  rt_distance = current_distance;
268  }
269  }
270  }
271  }
272 
273  if (any_hit) rt_data[sequence].push_back(feat_it->getRT());
274  }
275  else
276  {
277  getRetentionTimes_(feat_it->getPeptideIdentifications(), rt_data);
278  }
279  }
280 
281  if (!use_feature_rt_ &&
282  param_.getValue("use_unassigned_peptides").toBool())
283  {
284  getRetentionTimes_(features.getUnassignedPeptideIdentifications(),
285  rt_data);
286  }
287 
288  // remove duplicates (can occur if a peptide ID was assigned to several
289  // features due to overlap or annotation tolerance):
290  for (SeqToList::iterator rt_it = rt_data.begin(); rt_it != rt_data.end();
291  ++rt_it)
292  {
293  DoubleList& rt_values = rt_it->second;
294  sort(rt_values.begin(), rt_values.end());
295  DoubleList::iterator it = unique(rt_values.begin(), rt_values.end());
296  rt_values.resize(it - rt_values.begin());
297  }
298  return true; // RTs were already sorted for duplicate detection
299  }
300 
308  void computeTransformations_(std::vector<SeqToList>& rt_data,
309  std::vector<TransformationDescription>&
310  transforms, bool sorted = false);
311 
319  void checkParameters_(const Size runs);
320 
327  void getReference_();
328 
329 private:
330 
333 
336 
337  };
338 
339 } // namespace OpenMS
DefaultParamHandler.h
OpenMS::FileTypes::IDXML
OpenMS identification format (.idXML)
Definition: FileTypes.h:66
OpenMS::ProgressLogger::setProgress
void setProgress(SignedSize value) const
Sets the current progress.
OpenMS::ExperimentalDesign
Representation of an experimental design in OpenMS. Instances can be loaded with the ExperimentalDesi...
Definition: ExperimentalDesign.h:243
OpenMS::MapAlignmentAlgorithmTreeGuided::buildTree
static void buildTree(std::vector< FeatureMap > &feature_maps, std::vector< BinaryTreeNode > &tree, std::vector< std::vector< double >> &maps_ranges)
Extract RTs given for individual features of each map, calculate distances for each pair of maps and ...
OpenMS::Param::copy
Param copy(const String &prefix, bool remove_prefix=false) const
Returns a new Param object containing all entries that start with prefix.
OpenMS::ExperimentalDesignFile::load
static ExperimentalDesign load(const String &tsv_file, bool require_spectra_files)
Loads an experimental design from a tabular separated file.
OpenMS::MapAlignmentTransformer::transformRetentionTimes
static void transformRetentionTimes(PeakMap &msexp, const TransformationDescription &trafo, bool store_original_rt=false)
Applies the given transformation to a peak map.
OpenMS::FeatureFileOptions
Options for loading files containing features.
Definition: FeatureFileOptions.h:46
double
OpenMS::MapAlignmentAlgorithmIdentification::SeqToValue
std::map< String, double > SeqToValue
Type to store one representative retention time per peptide sequence.
Definition: MapAlignmentAlgorithmIdentification.h:154
OpenMS::MapAlignmentAlgorithmIdentification::min_run_occur_
Size min_run_occur_
Minimum number of runs a peptide must occur in.
Definition: MapAlignmentAlgorithmIdentification.h:163
ExperimentalDesignFile.h
OpenMS::DataProcessing::ALIGNMENT
Retention time alignment of different maps.
Definition: DataProcessing.h:68
OpenMS::ExperimentalDesign::getFractionToMSFilesMapping
std::map< unsigned int, std::vector< String > > getFractionToMSFilesMapping() const
return fraction index to file paths (ordered by fraction_group)
OpenMS::MzMLFile::store
void store(const String &filename, const PeakMap &map) const
Stores a map in an MzML file.
OpenMS::FileTypes::MZML
MzML file (.mzML)
Definition: FileTypes.h:72
OpenMS::MzMLFile
File adapter for MzML files.
Definition: MzMLFile.h:55
OpenMS::String
A more convenient string class.
Definition: String.h:59
OpenMS::MSExperiment::begin
Iterator begin()
Definition: MSExperiment.h:157
OpenMS::DoubleList
std::vector< double > DoubleList
Vector of double precision real types.
Definition: ListUtils.h:62
ConsensusMap.h
OpenMS::MSExperiment
In-Memory representation of a mass spectrometry experiment.
Definition: MSExperiment.h:77
OpenMS::FileTypes::CONSENSUSXML
OpenMS consensus map format (.consensusXML)
Definition: FileTypes.h:67
OpenMS::Size
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
ClusterAnalyzer.h
MapAlignmentAlgorithmTreeGuided.h
OpenMS::Param::getValue
const DataValue & getValue(const String &key) const
Returns a value of a parameter.
OpenMS::ProgressLogger::startProgress
void startProgress(SignedSize begin, SignedSize end, const String &label) const
Initializes the progress display.
OpenMS::MapAlignmentAlgorithmIdentification::setReference
void setReference(DataType &data)
Definition: MapAlignmentAlgorithmIdentification.h:83
TransformationDescription.h
OpenMS::MapAlignmentAlgorithmIdentification::getRetentionTimes_
bool getRetentionTimes_(MapType &features, SeqToList &rt_data)
Collect retention time data ("RT" MetaInfo) from peptide IDs contained in feature maps or consensus m...
Definition: MapAlignmentAlgorithmIdentification.h:225
MapAlignmentAlgorithmSpectrumAlignment.h
OpenMS::ProgressLogger::endProgress
void endProgress() const
Ends the progress display.
ListUtils.h
OpenMS::DefaultParamHandler
A base class for all classes handling default parameters.
Definition: DefaultParamHandler.h:92
OpenMS::TransformationDescription::fitModel
void fitModel(const String &model_type, const Param &params=Param())
Fits a model to the data.
OpenMS::IdXMLFile::load
void load(const String &filename, std::vector< ProteinIdentification > &protein_ids, std::vector< PeptideIdentification > &peptide_ids)
Loads the identifications of an idXML file without identifier.
OpenMS::MapAlignmentAlgorithmIdentification::use_feature_rt_
bool use_feature_rt_
Use feature RT instead of RT from best peptide ID in the feature.
Definition: MapAlignmentAlgorithmIdentification.h:166
OpenMS::Int
int Int
Signed integer type.
Definition: Types.h:102
OpenMS
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:46
OpenMS::MSExperiment::Iterator
std::vector< SpectrumType >::iterator Iterator
Mutable iterator.
Definition: MSExperiment.h:111
OpenMS::MSExperiment::getSize
UInt64 getSize() const
returns the total number of peaks
OpenMS::MapAlignmentAlgorithmSpectrumAlignment::align
virtual void align(std::vector< PeakMap > &, std::vector< TransformationDescription > &)
Align peak maps.
OpenMS::FileTypes::FEATUREXML
OpenMS feature file (.featureXML)
Definition: FileTypes.h:65
OpenMS::ProgressLogger
Base class for all classes that want to report their progress.
Definition: ProgressLogger.h:54
OpenMS::MapAlignmentAlgorithmIdentification::min_score_
double min_score_
Minimum score to reach for a peptide to be considered.
Definition: MapAlignmentAlgorithmIdentification.h:169
ProgressLogger.h
OpenMS::MzMLFile::load
void load(const String &filename, PeakMap &map)
Loads a map from a MzML file. Spectra and chromatograms are sorted by default (this can be disabled u...
OpenMS::ClusterAnalyzer
Bundles analyzing tools for a clustering (given as sequence of BinaryTreeNode's)
Definition: ClusterAnalyzer.h:51
int
OpenMS::FeatureXMLFile::loadSize
Size loadSize(const String &filename)
OpenMS::FeatureXMLFile::load
void load(const String &filename, FeatureMap &feature_map)
loads the file with name filename into map and calls updateRanges().
FeatureMap.h
OpenMS::FileTypes::Type
Type
Actual file types enum.
Definition: FileTypes.h:58
OpenMS::MapAlignmentAlgorithmPoseClustering::align
void align(const FeatureMap &map, TransformationDescription &trafo)
MapAlignmentAlgorithmIdentification.h
OpenMS::MapAlignmentAlgorithmIdentification::align
void align(std::vector< DataType > &data, std::vector< TransformationDescription > &transformations, Int reference_index=-1)
Align feature maps, consensus maps, peak maps, or peptide identifications.
Definition: MapAlignmentAlgorithmIdentification.h:107
OpenMS::FeatureXMLFile::setOptions
void setOptions(const FeatureFileOptions &)
setter for options for loading/storing
OpenMS::FileHandler::getType
static FileTypes::Type getType(const String &filename)
Tries to determine the file type (by name or content)
OpenMS::MapAlignmentAlgorithmPoseClustering
A map alignment algorithm based on pose clustering.
Definition: MapAlignmentAlgorithmPoseClustering.h:70
OpenMS::DefaultParamHandler::setParameters
void setParameters(const Param &param)
Sets the parameters.
OpenMS::DefaultParamHandler::getParameters
const Param & getParameters() const
Non-mutable access to the parameters.
OpenMS::MapAlignmentAlgorithmIdentification::score_cutoff_
bool score_cutoff_
Actually use the above defined score_cutoff? Needed since it is hard to define a non-cutting score fo...
Definition: MapAlignmentAlgorithmIdentification.h:172
OpenMS::MapAlignmentAlgorithmSpectrumAlignment
A map alignment algorithm based on spectrum similarity (dynamic programming).
Definition: MapAlignmentAlgorithmSpectrumAlignment.h:54
OpenMS::ConsensusMap
A container for consensus elements.
Definition: ConsensusMap.h:80
OpenMS::StringList
std::vector< String > StringList
Vector of String.
Definition: ListUtils.h:70
OpenMS::FeatureXMLFile::store
void store(const String &filename, const FeatureMap &feature_map)
stores the map feature_map in file with name filename.
OpenMS::ConsensusXMLFile::load
void load(const String &filename, ConsensusMap &map)
Loads a consensus map from file and calls updateRanges.
OpenMS::MapAlignmentAlgorithmIdentification::SeqToList
std::map< String, DoubleList > SeqToList
Type to store retention times given for individual peptide sequences.
Definition: MapAlignmentAlgorithmIdentification.h:151
OpenMS::MapAlignmentAlgorithmIdentification
A map alignment algorithm based on peptide identifications from MS2 spectra.
Definition: MapAlignmentAlgorithmIdentification.h:71
OpenMS::Exception::IndexOverflow
Int overflow exception.
Definition: Exception.h:254
main
int main(int argc, const char **argv)
Definition: INIFileEditor.cpp:73
ExperimentalDesign.h
OpenMS::MapAlignmentAlgorithmIdentification::reference_index_
Int reference_index_
Index of input file to use as reference (if any)
Definition: MapAlignmentAlgorithmIdentification.h:157
MSExperiment.h
OpenMS::MapAlignmentAlgorithmIdentification::reference_
SeqToValue reference_
Reference retention times (per peptide sequence)
Definition: MapAlignmentAlgorithmIdentification.h:160
OpenMS::FeatureFileOptions::setLoadSubordinates
void setLoadSubordinates(bool sub)
OPENMS_LOG_ERROR
#define OPENMS_LOG_ERROR
Macro to be used if non-fatal error are reported (processing continues)
Definition: LogStream.h:455
OpenMS::FeatureMap
A container for features.
Definition: FeatureMap.h:97
OpenMS::TransformationXMLFile
Used to load and store TransformationXML files.
Definition: TransformationXMLFile.h:56
OpenMS::MSExperiment::end
Iterator end()
Definition: MSExperiment.h:167
MapAlignmentAlgorithmPoseClustering.h
OpenMS::FeatureXMLFile
This class provides Input/Output functionality for feature maps.
Definition: FeatureXMLFile.h:68
OpenMS::MapAlignmentAlgorithmTreeGuided::computeTrafosByOriginalRT
void computeTrafosByOriginalRT(std::vector< FeatureMap > &feature_maps, FeatureMap &map_transformed, std::vector< TransformationDescription > &transformations, const std::vector< Size > &trafo_order)
Extract original RT ("original_RT" MetaInfo) and transformed RT for each feature to compute RT transf...
OpenMS::Exception::BaseException::getMessage
const char * getMessage() const noexcept
Returns the message.
OpenMS::MSExperiment::updateRanges
void updateRanges() override
Updates minimum and maximum position/intensity.
OpenMS::MapAlignmentAlgorithmTreeGuided
A map alignment algorithm based on peptide identifications from MS2 spectra.
Definition: MapAlignmentAlgorithmTreeGuided.h:71
MapAlignerBase.h
OpenMS::Param
Management and storage of parameters / INI files.
Definition: Param.h:73
OpenMS::TransformationXMLFile::store
void store(String filename, const TransformationDescription &transformation)
Stores the data in an TransformationXML file.
OpenMS::Exception::MissingInformation
Not all required information provided.
Definition: Exception.h:195
OPENMS_LOG_INFO
#define OPENMS_LOG_INFO
Macro if a information, e.g. a status should be reported.
Definition: LogStream.h:465
OpenMS::FeatureFileOptions::setLoadConvexHull
void setLoadConvexHull(bool convex)
PeptideIdentification.h
OpenMS::MapAlignmentAlgorithmTreeGuided::computeTransformedFeatureMaps
static void computeTransformedFeatureMaps(std::vector< FeatureMap > &feature_maps, const std::vector< TransformationDescription > &transformations)
Apply transformations on input maps.
OpenMS::TransformationDescription
Generic description of a coordinate transformation.
Definition: TransformationDescription.h:61
OpenMS::MapAlignmentAlgorithmPoseClustering::setReference
void setReference(const MapType &map)
Sets the reference for the alignment.
Definition: MapAlignmentAlgorithmPoseClustering.h:87
StandardTypes.h
OpenMS::FeatureXMLFile::getOptions
FeatureFileOptions & getOptions()
Mutable access to the options for loading/storing.
OpenMS::ProgressLogger::setLogType
void setLogType(LogType type) const
Sets the progress log that should be used. The default type is NONE!
OpenMS::ExperimentalDesign::sameNrOfMSFilesPerFraction
bool sameNrOfMSFilesPerFraction() const
OpenMS::ConsensusXMLFile
This class provides Input functionality for ConsensusMaps and Output functionality for alignments and...
Definition: ConsensusXMLFile.h:62
OpenMS::IdXMLFile
Used to load and store idXML files.
Definition: IdXMLFile.h:63
OpenMS::ClusterAnalyzer::newickTree
String newickTree(const std::vector< BinaryTreeNode > &tree, const bool include_distance=false)
Returns the hierarchy described by a clustering tree as Newick-String.
OpenMS::MapAlignmentAlgorithmTreeGuided::treeGuidedAlignment
void treeGuidedAlignment(const std::vector< BinaryTreeNode > &tree, std::vector< FeatureMap > &feature_maps_transformed, std::vector< std::vector< double >> &maps_ranges, FeatureMap &map_transformed, std::vector< Size > &trafo_order)
Align feature maps tree guided using align() of OpenMS::MapAlignmentAlgorithmIdentification and use T...