OpenMS  2.6.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-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: 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 
320  const MSFileSection& getMSFileSection() const;
321 
322  void setMSFileSection(const MSFileSection& msfile_section);
323 
324  // Returns the Sample Section of the experimental design file
325  const ExperimentalDesign::SampleSection& getSampleSection() const;
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 
399  bool sameNrOfMSFilesPerFraction() const;
400 
402  static ExperimentalDesign fromConsensusMap(const ConsensusMap& c);
403 
405  static ExperimentalDesign fromFeatureMap(const FeatureMap& f);
406 
408  static ExperimentalDesign fromIdentifications(const std::vector<ProteinIdentification>& proteins);
409  //TODO create another overload here, that takes two enums outerVec and innerVec with entries Replicate, Fraction, Sample
410 
411  private:
412  // MS filename column, optionally trims to basename
413  std::vector< String > getFileNames_(bool basename) const;
414 
415  // returns label column
416  std::vector<unsigned> getLabels_() const;
417 
418  // returns fraction column
419  std::vector<unsigned> getFractions_() const;
420 
422  std::map< std::pair< String, unsigned >, unsigned> pathLabelMapper_(
423  bool,
424  unsigned (*f)(const ExperimentalDesign::MSFileSectionEntry&)) const;
425 
426  // sort to obtain the default order
427  void sort_();
428 
429  template<typename T>
430  static void errorIfAlreadyExists(std::set<T> &container, T &item, const String &message);
431 
432  // basic consistency checks
433  void isValid_();
434 
437  };
438 }
439 
OpenMS::TOPPBase
Base class for TOPP applications.
Definition: TOPPBase.h:144
OpenMS::ExperimentalDesign
Representation of an experimental design in OpenMS. Instances can be loaded with the ExperimentalDesi...
Definition: ExperimentalDesign.h:243
OpenMS::String::substitute
String & substitute(char from, char to)
Replaces all occurrences of the character from by the character to.
TOPPASScene.h
OpenMS::TOPPASScene
A container for all visual items of a TOPPAS workflow.
Definition: TOPPASScene.h:85
OpenMS::String
A more convenient string class.
Definition: String.h:59
ConsensusMap.h
OpenMS::Size
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
OpenMS::Constants::c
const double c
OpenMS::File::getUniqueName
static String getUniqueName(bool include_hostname=true)
Returns a string, consisting of date, time, hostname, process id, and a incrementing number....
OpenMS::String::hasPrefix
bool hasPrefix(const String &string) const
true if String begins with string, false otherwise
ListUtils.h
OpenMS::File::writable
static bool writable(const String &file)
Return true if the file is writable.
OpenMS::ExperimentalDesign::SampleSection::columnname_to_columnindex_
std::map< String, Size > columnname_to_columnindex_
Definition: ExperimentalDesign.h:310
OpenMS::File::exists
static bool exists(const String &file)
Method used to test if a file exists.
OpenMS
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:46
OpenMS::ExperimentalDesign::SampleSection::content_
std::vector< std::vector< String > > content_
Definition: ExperimentalDesign.h:302
OpenMS::ExperimentalDesign::SampleSection
Definition: ExperimentalDesign.h:264
TOPPASResources.h
OpenMS::File::isDirectory
static bool isDirectory(const String &path)
Return true if the given path specifies a directory.
FeatureMap.h
ProteinIdentification.h
OpenMS::TOPPASResources
A dictionary mapping string keys to lists of TOPPASResource objects.
Definition: TOPPASResources.h:55
OpenMS::TOPPASResources::load
void load(const QString &file_name)
Loads the dictionary from file file_name.
OpenMS::String::toQString
QString toQString() const
Conversion to Qt QString.
OpenMS::ExperimentalDesign::MSFileSectionEntry
Definition: ExperimentalDesign.h:253
OpenMS::ConsensusMap
A container for consensus elements.
Definition: ConsensusMap.h:80
main
int main(int argc, const char **argv)
Definition: INIFileEditor.cpp:73
OpenMS::ExperimentalDesign::MSFileSection
std::vector< MSFileSectionEntry > MSFileSection
Definition: ExperimentalDesign.h:313
OpenMS::FeatureMap
A container for features.
Definition: FeatureMap.h:97
OpenMS::ExperimentalDesign::msfile_section_
MSFileSection msfile_section_
Definition: ExperimentalDesign.h:435
String.h
OpenMS::ExperimentalDesign::sample_section_
SampleSection sample_section_
Definition: ExperimentalDesign.h:436
OpenMS::File::getTempDirectory
static String getTempDirectory()
The current OpenMS temporary data path (for temporary files)
OpenMS::File::removeDirRecursively
static bool removeDirRecursively(const String &dir_name)
Removes the subdirectories of the specified directory (absolute path). Returns true if successful.
QApplication
StandardTypes.h
File.h
TOPPBase.h
OpenMS::ExperimentalDesign::SampleSection::sample_to_rowindex_
std::map< unsigned, Size > sample_to_rowindex_
Definition: ExperimentalDesign.h:306