OpenMS  2.4.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-2018.
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 {
85  class OPENMS_DLLAPI ExperimentalDesign
86  {
87 
88  public:
95  class OPENMS_DLLAPI MSFileSectionEntry
96  {
97  public:
98  MSFileSectionEntry() = default;
99  unsigned fraction_group = 1;
100  unsigned fraction = 1;
101  std::string path = "UNKNOWN_FILE";
102  unsigned label = 1;
103  unsigned sample = 1;
104  };
105 
106  class OPENMS_DLLAPI SampleSection
107  {
108  public:
109 
110  SampleSection() = default;
111 
113  const std::vector< std::vector < String > >& content,
114  const std::map< unsigned, Size >& sample_to_rowindex,
115  const std::map< String, Size >& columnname_to_columnindex
116  );
117 
118  // Get set of all samples that are present in the sample section
119  std::set< unsigned > getSamples() const;
120 
121  // Get set of all factors (column names) that were defined for the sample section
122  std::set< String > getFactors() const;
123 
124  // Checks whether sample section has row for a sample number
125  bool hasSample(unsigned sample) const;
126 
127  // Checks whether Sample Section has a specific factor (i.e. column name)
128  bool hasFactor(const String &factor) const;
129 
130  // Returns value of factor for given sample and factor name
131  String getFactorValue(unsigned sample, const String &factor) const;
132 
133  // Returns column index of factor
134  Size getFactorColIdx( const String &factor) const;
135 
136  private:
137 
138  // The entries of the Sample Section, filled while parsing
139  // the Experimental Design File
140  std::vector< std::vector < String > > content_;
141 
142  // Maps the Sample Entry to the row where the sample
143  // appears in the Sample section
144  std::map< unsigned, Size > sample_to_rowindex_;
145 
146  // Maps the column name of the SampleSection to the
147  // Index of the column
148  std::map< String, Size > columnname_to_columnindex_;
149  };
150 
151  using MSFileSection = std::vector<MSFileSectionEntry>;
152 
153  // Experimental Design c'tors
154  ExperimentalDesign() = default;
155 
156  ExperimentalDesign(const MSFileSection& msfile_section, const SampleSection& sample_section);
157 
158  const MSFileSection& getMSFileSection() const;
159 
160  void setMSFileSection(const MSFileSection& msfile_section);
161 
162  // Returns the Sample Section of the experimental design file
163  const ExperimentalDesign::SampleSection& getSampleSection() const;
164 
165  void setSampleSection(const SampleSection& sample_section);
166 
168  std::map<unsigned int, std::vector<String> > getFractionToMSFilesMapping() const;
169 
172  std::vector<std::vector<std::pair<String, unsigned>>> getSampleWOReplicatesToMSFilesMapping() const;
173 
174  /*
175  * The (Path, Label) tuples in the experimental design have to be unique, so we can map them
176  * uniquely to the sample number, fraction number, and fraction_group number
177  */
179  std::map<std::vector<String>, std::set<unsigned>> getConditionToSampleMapping() const;
180 
183  std::map< std::pair< String, unsigned >, unsigned> getPathLabelToConditionMapping(bool) const;
184 
187  std::map<unsigned, unsigned> getSampleToConditionMapping() const;
188 
190  std::map< std::pair< String, unsigned >, unsigned> getPathLabelToSampleMapping(bool) const;
191 
193  std::map< std::pair< String, unsigned >, unsigned> getPathLabelToFractionMapping(bool) const;
194 
196  std::map< std::pair< String, unsigned >, unsigned> getPathLabelToFractionGroupMapping(bool) const;
197 
198  // @return the number of samples measured (= highest sample index)
199  unsigned getNumberOfSamples() const;
200 
201  // @return the number of fractions (= highest fraction index)
202  unsigned getNumberOfFractions() const;
203 
204  // @return the number of labels per file
205  unsigned getNumberOfLabels() const;
206 
207  // @return the number of MS files (= fractions * fraction groups)
208  unsigned getNumberOfMSFiles() const;
209 
210  // @return the number of fraction_groups
211  // Allows to group fraction ids and source files
212  unsigned getNumberOfFractionGroups() const;
213 
214  // @return sample index (depends on fraction_group and label)
215  unsigned getSample(unsigned fraction_group, unsigned label = 1);
216 
218  // This is the case if we have at least one fraction group with >= 2 fractions
219  bool isFractionated() const;
220 
222  bool sameNrOfMSFilesPerFraction() const;
223 
225  static ExperimentalDesign fromConsensusMap(const ConsensusMap& c);
226 
228  static ExperimentalDesign fromFeatureMap(const FeatureMap& f);
229 
231  static ExperimentalDesign fromIdentifications(const std::vector<ProteinIdentification>& proteins);
232  //TODO create another overload here, that takes two enums outerVec and innerVec with entries Replicate, Fraction, Sample
233 
234  private:
235  // MS filename column, optionally trims to basename
236  std::vector< String > getFileNames_(bool basename) const;
237 
238  // returns label column
239  std::vector<unsigned> getLabels_() const;
240 
241  // returns fraction column
242  std::vector<unsigned> getFractions_() const;
243 
245  std::map< std::pair< String, unsigned >, unsigned> pathLabelMapper_(
246  bool,
247  unsigned (*f)(const ExperimentalDesign::MSFileSectionEntry&)) const;
248 
249  // sort to obtain the default order
250  void sort_();
251 
252  template<typename T>
253  static void errorIfAlreadyExists(std::set<T> &container, T &item, const String &message);
254 
255  // basic consistency checks
256  void isValid_();
257 
260  };
261 }
262 
A more convenient string class.
Definition: String.h:58
A container for features.
Definition: FeatureMap.h:93
MSFileSection msfile_section_
Definition: ExperimentalDesign.h:258
A container for consensus elements.
Definition: ConsensusMap.h:75
const double c
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:46
std::map< unsigned, Size > sample_to_rowindex_
Definition: ExperimentalDesign.h:144
Representation of the Experimental Design in OpenMS. Instances can be loaded via the ExperimentalDesi...
Definition: ExperimentalDesign.h:85
Definition: ExperimentalDesign.h:95
std::map< String, Size > columnname_to_columnindex_
Definition: ExperimentalDesign.h:148
SampleSection sample_section_
Definition: ExperimentalDesign.h:259
Definition: ExperimentalDesign.h:106
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
std::vector< std::vector< String > > content_
Definition: ExperimentalDesign.h:140
std::vector< MSFileSectionEntry > MSFileSection
Definition: ExperimentalDesign.h:151