OpenMS  2.7.0
ExperimentalDesign.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 
39 #include <OpenMS/SYSTEM/File.h>
43 
44 #include <vector>
45 #include <map>
46 #include <set>
47 #include <algorithm>
48 
49 namespace OpenMS
50 {
243  class OPENMS_DLLAPI ExperimentalDesign
244  {
245 
246  public:
253  class OPENMS_DLLAPI MSFileSectionEntry
254  {
255  public:
256  MSFileSectionEntry() = default;
257  unsigned fraction_group = 1;
258  unsigned fraction = 1;
259  std::string path = "UNKNOWN_FILE";
260  unsigned label = 1;
261  unsigned sample = 1;
262  };
263 
264  class OPENMS_DLLAPI SampleSection
265  {
266  public:
267 
268  SampleSection() = default;
269 
271  const std::vector< std::vector < String > >& content,
272  const std::map< unsigned, Size >& sample_to_rowindex,
273  const std::map< String, Size >& columnname_to_columnindex
274  );
275 
276  // Get set of all samples that are present in the sample section
277  std::set< unsigned > getSamples() const;
278 
279  // Add a sample as the last row
280  void addSample(unsigned sample, const std::vector<String>& content = {});
281 
282  // TODO should it include the Sample ID column or not??
283  // Get set of all factors (column names) that were defined for the sample section
284  std::set< String > getFactors() const;
285 
286  // Checks whether sample section has row for a sample number
287  bool hasSample(unsigned sample) const;
288 
289  // Checks whether Sample Section has a specific factor (i.e. column name)
290  bool hasFactor(const String &factor) const;
291 
292  // Returns value of factor for given sample and factor name
293  String getFactorValue(unsigned sample, const String &factor) const;
294 
295  // Returns column index of factor
296  Size getFactorColIdx( const String &factor) const;
297 
298  private:
299 
300  // The entries of the Sample Section, filled while parsing
301  // the Experimental Design File
302  std::vector< std::vector < String > > content_;
303 
304  // Maps the Sample Entry to the row where the sample
305  // appears in the Sample section
306  std::map< unsigned, Size > sample_to_rowindex_;
307 
308  // Maps the column name of the SampleSection to the
309  // Index of the column
310  std::map< String, Size > columnname_to_columnindex_;
311  };
312 
313  using MSFileSection = std::vector<MSFileSectionEntry>;
314 
315  // Experimental Design c'tors
316  ExperimentalDesign() = default;
317 
318  ExperimentalDesign(const MSFileSection& msfile_section, const SampleSection& sample_section);
319 
321 
322  void setMSFileSection(const MSFileSection& msfile_section);
323 
324  // Returns the Sample Section of the experimental design file
326 
327  void setSampleSection(const SampleSection& sample_section);
328 
331  std::map<std::vector<String>, std::set<unsigned>> getUniqueSampleRowToSampleMapping() const;
332 
335  std::map<unsigned, unsigned> getSampleToPrefractionationMapping() const;
336 
338  //TODO this probably needs a basename parameter to be fully compatible with the other mappings!! Implicit full path.
339  std::map<unsigned int, std::vector<String> > getFractionToMSFilesMapping() const;
340 
343  //TODO this probably needs a basename parameter to be fully compatible with the other mappings!! Implicit full path.
344  std::vector<std::vector<std::pair<String, unsigned>>> getConditionToPathLabelVector() const;
345 
347  std::map<std::vector<String>, std::set<unsigned>> getConditionToSampleMapping() const;
348 
349  /*
350  * The (Path, Label) tuples in the experimental design have to be unique, so we can map them
351  * uniquely to the sample number, fraction number, and fraction_group number
352  */
353 
356  std::map< std::pair< String, unsigned >, unsigned> getPathLabelToPrefractionationMapping(bool use_basename_only) const;
357 
360  std::map< std::pair< String, unsigned >, unsigned> getPathLabelToConditionMapping(bool use_basename_only) const;
361 
364  std::map<unsigned, unsigned> getSampleToConditionMapping() const;
365 
367  std::map< std::pair< String, unsigned >, unsigned> getPathLabelToSampleMapping(bool use_basename_only) const;
368 
370  std::map< std::pair< String, unsigned >, unsigned> getPathLabelToFractionMapping(bool use_basename_only) const;
371 
373  std::map< std::pair< String, unsigned >, unsigned> getPathLabelToFractionGroupMapping(bool use_basename_only) const;
374 
375  // @return the number of samples measured (= highest sample index)
376  unsigned getNumberOfSamples() const;
377 
378  // @return the number of fractions (= highest fraction index)
379  unsigned getNumberOfFractions() const;
380 
381  // @return the number of labels per file
382  unsigned getNumberOfLabels() const;
383 
384  // @return the number of MS files (= fractions * fraction groups)
385  unsigned getNumberOfMSFiles() const;
386 
387  // @return the number of fraction_groups
388  // Allows to group fraction ids and source files
389  unsigned getNumberOfFractionGroups() const;
390 
391  // @return sample index (depends on fraction_group and label)
392  unsigned getSample(unsigned fraction_group, unsigned label = 1);
393 
395  // This is the case if we have at least one fraction group with >= 2 fractions
396  bool isFractionated() const;
397 
401  Size filterByBasenames(const std::set<String>& bns);
402 
405 
408 
411 
413  static ExperimentalDesign fromIdentifications(const std::vector<ProteinIdentification>& proteins);
414  //TODO create another overload here, that takes two enums outerVec and innerVec with entries Replicate, Fraction, Sample
415 
416  private:
417  // MS filename column, optionally trims to basename
418  std::vector< String > getFileNames_(bool basename) const;
419 
420  // returns label column
421  std::vector<unsigned> getLabels_() const;
422 
423  // returns fraction column
424  std::vector<unsigned> getFractions_() const;
425 
427  std::map< std::pair< String, unsigned >, unsigned> pathLabelMapper_(
428  bool,
429  unsigned (*f)(const ExperimentalDesign::MSFileSectionEntry&)) const;
430 
431  // sort to obtain the default order
432  void sort_();
433 
434  template<typename T>
435  static void errorIfAlreadyExists(std::set<T> &container, T &item, const String &message);
436 
437  // basic consistency checks
438  void isValid_();
439 
442  };
443 }
444 
A container for consensus elements.
Definition: ConsensusMap.h:88
Definition: ExperimentalDesign.h:254
Definition: ExperimentalDesign.h:265
SampleSection(const std::vector< std::vector< String > > &content, const std::map< unsigned, Size > &sample_to_rowindex, const std::map< String, Size > &columnname_to_columnindex)
std::vector< std::vector< String > > content_
Definition: ExperimentalDesign.h:302
std::map< String, Size > columnname_to_columnindex_
Definition: ExperimentalDesign.h:310
String getFactorValue(unsigned sample, const String &factor) const
bool hasSample(unsigned sample) const
std::set< String > getFactors() const
Size getFactorColIdx(const String &factor) const
bool hasFactor(const String &factor) const
std::set< unsigned > getSamples() const
std::map< unsigned, Size > sample_to_rowindex_
Definition: ExperimentalDesign.h:306
void addSample(unsigned sample, const std::vector< String > &content={})
Representation of an experimental design in OpenMS. Instances can be loaded with the ExperimentalDesi...
Definition: ExperimentalDesign.h:244
unsigned getNumberOfLabels() const
static void errorIfAlreadyExists(std::set< T > &container, T &item, const String &message)
unsigned getNumberOfFractions() const
static ExperimentalDesign fromConsensusMap(const ConsensusMap &c)
Extract experimental design from consensus map.
std::map< std::vector< String >, std::set< unsigned > > getUniqueSampleRowToSampleMapping() const
unsigned getSample(unsigned fraction_group, unsigned label=1)
unsigned getNumberOfSamples() const
void setSampleSection(const SampleSection &sample_section)
std::vector< unsigned > getLabels_() const
bool sameNrOfMSFilesPerFraction() const
Size filterByBasenames(const std::set< String > &bns)
unsigned getNumberOfFractionGroups() const
std::map< unsigned int, std::vector< String > > getFractionToMSFilesMapping() const
return fraction index to file paths (ordered by fraction_group)
std::map< std::pair< String, unsigned >, unsigned > getPathLabelToFractionMapping(bool use_basename_only) const
return <file_path, label> to fraction mapping
std::map< std::pair< String, unsigned >, unsigned > getPathLabelToPrefractionationMapping(bool use_basename_only) const
std::map< std::vector< String >, std::set< unsigned > > getConditionToSampleMapping() const
return a condition (unique combination of sample section values except replicate) to Sample index map...
void setMSFileSection(const MSFileSection &msfile_section)
std::vector< MSFileSectionEntry > MSFileSection
Definition: ExperimentalDesign.h:313
std::map< std::pair< String, unsigned >, unsigned > getPathLabelToConditionMapping(bool use_basename_only) const
MSFileSection msfile_section_
Definition: ExperimentalDesign.h:440
static ExperimentalDesign fromIdentifications(const std::vector< ProteinIdentification > &proteins)
Extract experimental design from identifications.
std::map< std::pair< String, unsigned >, unsigned > pathLabelMapper_(bool, unsigned(*f)(const ExperimentalDesign::MSFileSectionEntry &)) const
Generic Mapper (Path, Label) -> f(row)
ExperimentalDesign(const MSFileSection &msfile_section, const SampleSection &sample_section)
std::vector< unsigned > getFractions_() const
const MSFileSection & getMSFileSection() const
std::vector< String > getFileNames_(bool basename) const
const ExperimentalDesign::SampleSection & getSampleSection() const
std::map< unsigned, unsigned > getSampleToConditionMapping() const
static ExperimentalDesign fromFeatureMap(const FeatureMap &f)
Extract experimental design from feature map.
std::vector< std::vector< std::pair< String, unsigned > > > getConditionToPathLabelVector() const
std::map< std::pair< String, unsigned >, unsigned > getPathLabelToSampleMapping(bool use_basename_only) const
return <file_path, label> to sample mapping
SampleSection sample_section_
Definition: ExperimentalDesign.h:441
unsigned getNumberOfMSFiles() const
std::map< std::pair< String, unsigned >, unsigned > getPathLabelToFractionGroupMapping(bool use_basename_only) const
return <file_path, label> to fraction_group mapping
std::map< unsigned, unsigned > getSampleToPrefractionationMapping() const
A container for features.
Definition: FeatureMap.h:105
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
const double c
Definition: Constants.h:209
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47