OpenMS
MsInspectFile.h
Go to the documentation of this file.
1 // Copyright (c) 2002-present, The OpenMS Team -- EKU Tuebingen, ETH Zurich, and FU Berlin
2 // SPDX-License-Identifier: BSD-3-Clause
3 //
4 // --------------------------------------------------------------------------
5 // $Maintainer: Chris Bielow $
6 // $Authors: Chris Bielow $
7 // --------------------------------------------------------------------------
8 
9 #pragma once
10 
13 #include <OpenMS/KERNEL/Feature.h>
14 #include <OpenMS/FORMAT/TextFile.h>
15 
16 #include <fstream>
17 #include <vector>
18 
19 namespace OpenMS
20 {
33  class OPENMS_DLLAPI MsInspectFile
34  {
35 public:
39  virtual ~MsInspectFile();
40 
49  template <typename FeatureMapType>
50  void load(const String& filename, FeatureMapType& feature_map)
51  {
52  // load input
53  TextFile input(filename);
54 
55  // reset map
56  FeatureMapType fmap;
57  feature_map = fmap;
58 
59  bool first_line = true;
60  for (TextFile::ConstIterator it = input.begin(); it != input.end(); ++it)
61  {
62  String line = *it;
63 
64  //ignore comment lines
65  if (line.empty() || line[0] == '#') continue;
66 
67  //skip leader line
68  if (first_line)
69  {
70  first_line = false;
71  continue;
72  }
73 
74  //split lines: scan\ttime\tmz\taccurateMZ\tmass\tintensity\tcharge\tchargeStates\tkl\tbackground\tmedian\tpeaks\tscanFirst\tscanLast\tscanCount\ttotalIntensity\tsumSquaresDist\tdescription
75  std::vector<String> parts;
76  line.split('\t', parts);
77 
78  if (parts.size() < 18)
79  {
80  throw Exception::ParseError(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, "", String("Failed to convert line ") + String((it - input.begin()) + 1) + ". Not enough columns (expected 18 or more, got " + String(parts.size()) + ")");
81  }
82 
83  //create feature
84  Feature f;
85  Size column_to_convert = 0;
86  try
87  {
88  column_to_convert = 1;
89  f.setRT(parts[1].toDouble());
90  column_to_convert = 2;
91  f.setMZ(parts[2].toDouble());
92  column_to_convert = 5;
93  f.setIntensity(parts[5].toDouble());
94  column_to_convert = 6;
95  f.setCharge(parts[6].toInt());
96  column_to_convert = 8;
97  f.setOverallQuality(parts[8].toDouble());
98 
99  column_to_convert = 3;
100  f.setMetaValue("accurateMZ", parts[3]);
101  column_to_convert = 4;
102  f.setMetaValue("mass", parts[4].toDouble());
103  column_to_convert = 7;
104  f.setMetaValue("chargeStates", parts[7].toInt());
105  column_to_convert = 9;
106  f.setMetaValue("background", parts[9].toDouble());
107  column_to_convert = 10;
108  f.setMetaValue("median", parts[10].toDouble());
109  column_to_convert = 11;
110  f.setMetaValue("peaks", parts[11].toInt());
111  column_to_convert = 12;
112  f.setMetaValue("scanFirst", parts[12].toInt());
113  column_to_convert = 13;
114  f.setMetaValue("scanLast", parts[13].toInt());
115  column_to_convert = 14;
116  f.setMetaValue("scanCount", parts[14].toInt());
117  column_to_convert = 15;
118  f.setMetaValue("totalIntensity", parts[15].toDouble());
119  column_to_convert = 16;
120  f.setMetaValue("sumSquaresDist", parts[16].toDouble());
121  }
122  catch ( Exception::BaseException& )
123  {
124  throw Exception::ParseError(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, "", String("Failed to convert value in column ") + String(column_to_convert + 1) + " into a number (line '" + String((it - input.begin()) + 1) + ")");
125  }
126  f.setMetaValue("description", parts[17]);
127  feature_map.push_back(f);
128  }
129 
130  }
131 
139  template <typename SpectrumType>
140  void store(const String& filename, const SpectrumType& spectrum) const
141  {
142  std::cerr << "Store() for MsInspectFile not implemented. Filename was: " << filename << ", spec of size " << spectrum.size() << "\n";
143  throw Exception::NotImplemented(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION);
144  }
145 
146  };
147 } // namespace OpenMS
148 
void setCharge(const ChargeType &ch)
Set charge state.
Exception base class.
Definition: Exception.h:63
Not implemented exception.
Definition: Exception.h:399
Parse Error exception.
Definition: Exception.h:579
An LC-MS feature.
Definition: Feature.h:46
void setOverallQuality(QualityType q)
Set the overall quality.
The representation of a 1D spectrum.
Definition: MSSpectrum.h:44
void setMetaValue(const String &name, const DataValue &value)
Sets the DataValue corresponding to a name.
Definition: MsInspectFile.h:34
virtual ~MsInspectFile()
Destructor.
void store(const String &filename, const SpectrumType &spectrum) const
Stores a featureXML as a MsInspect file.
Definition: MsInspectFile.h:140
void load(const String &filename, FeatureMapType &feature_map)
Loads a MsInspect file into a featureXML.
Definition: MsInspectFile.h:50
MsInspectFile()
Default constructor.
void setMZ(CoordinateType coordinate)
Mutable access to the m/z coordinate (index 1)
Definition: Peak2D.h:178
void setRT(CoordinateType coordinate)
Mutable access to the RT coordinate (index 0)
Definition: Peak2D.h:190
void setIntensity(IntensityType intensity)
Sets data point intensity (height)
Definition: Peak2D.h:148
A more convenient string class.
Definition: String.h:34
bool split(const char splitter, std::vector< String > &substrings, bool quote_protect=false) const
Splits a string into substrings using splitter as delimiter.
Definition: TextFile.h:21
std::vector< String >::const_iterator ConstIterator
Non-mutable iterator.
Definition: TextFile.h:30
ConstIterator end() const
Gives access to the underlying text buffer.
ConstIterator begin() const
Gives access to the underlying text buffer.
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:97
static double toDouble(const String &this_s)
Definition: StringUtils.h:216
Main OpenMS namespace.
Definition: openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19