OpenMS  2.8.0
MsInspectFile.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: Chris Bielow $
32 // $Authors: Chris Bielow $
33 // --------------------------------------------------------------------------
34 
35 #pragma once
36 
39 #include <OpenMS/KERNEL/Feature.h>
40 #include <OpenMS/FORMAT/TextFile.h>
41 
42 #include <fstream>
43 #include <vector>
44 
45 namespace OpenMS
46 {
59  class OPENMS_DLLAPI MsInspectFile
60  {
61 public:
65  virtual ~MsInspectFile();
66 
75  template <typename FeatureMapType>
76  void load(const String& filename, FeatureMapType& feature_map)
77  {
78  // load input
79  TextFile input(filename);
80 
81  // reset map
82  FeatureMapType fmap;
83  feature_map = fmap;
84 
85  bool first_line = true;
86  for (TextFile::ConstIterator it = input.begin(); it != input.end(); ++it)
87  {
88  String line = *it;
89 
90  //ignore comment lines
91  if (line.empty() || line[0] == '#') continue;
92 
93  //skip leader line
94  if (first_line)
95  {
96  first_line = false;
97  continue;
98  }
99 
100  //split lines: scan\ttime\tmz\taccurateMZ\tmass\tintensity\tcharge\tchargeStates\tkl\tbackground\tmedian\tpeaks\tscanFirst\tscanLast\tscanCount\ttotalIntensity\tsumSquaresDist\tdescription
101  std::vector<String> parts;
102  line.split('\t', parts);
103 
104  if (parts.size() < 18)
105  {
106  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()) + ")");
107  }
108 
109  //create feature
110  Feature f;
111  Size column_to_convert = 0;
112  try
113  {
114  column_to_convert = 1;
115  f.setRT(parts[1].toDouble());
116  column_to_convert = 2;
117  f.setMZ(parts[2].toDouble());
118  column_to_convert = 5;
119  f.setIntensity(parts[5].toDouble());
120  column_to_convert = 6;
121  f.setCharge(parts[6].toInt());
122  column_to_convert = 8;
123  f.setOverallQuality(parts[8].toDouble());
124 
125  column_to_convert = 3;
126  f.setMetaValue("accurateMZ", parts[3]);
127  column_to_convert = 4;
128  f.setMetaValue("mass", parts[4].toDouble());
129  column_to_convert = 7;
130  f.setMetaValue("chargeStates", parts[7].toInt());
131  column_to_convert = 9;
132  f.setMetaValue("background", parts[9].toDouble());
133  column_to_convert = 10;
134  f.setMetaValue("median", parts[10].toDouble());
135  column_to_convert = 11;
136  f.setMetaValue("peaks", parts[11].toInt());
137  column_to_convert = 12;
138  f.setMetaValue("scanFirst", parts[12].toInt());
139  column_to_convert = 13;
140  f.setMetaValue("scanLast", parts[13].toInt());
141  column_to_convert = 14;
142  f.setMetaValue("scanCount", parts[14].toInt());
143  column_to_convert = 15;
144  f.setMetaValue("totalIntensity", parts[15].toDouble());
145  column_to_convert = 16;
146  f.setMetaValue("sumSquaresDist", parts[16].toDouble());
147  }
148  catch ( Exception::BaseException& )
149  {
150  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) + ")");
151  }
152  f.setMetaValue("description", parts[17]);
153  feature_map.push_back(f);
154  }
155 
156  }
157 
165  template <typename SpectrumType>
166  void store(const String& filename, const SpectrumType& spectrum) const
167  {
168  std::cerr << "Store() for MsInspectFile not implemented. Filename was: " << filename << ", spec of size " << spectrum.size() << "\n";
169  throw Exception::NotImplemented(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION);
170  }
171 
172  };
173 } // namespace OpenMS
174 
void setCharge(const ChargeType &ch)
Set charge state.
Exception base class.
Definition: Exception.h:91
Not implemented exception.
Definition: Exception.h:430
Parse Error exception.
Definition: Exception.h:624
An LC-MS feature.
Definition: Feature.h:72
void setOverallQuality(QualityType q)
Set the overall quality.
The representation of a 1D spectrum.
Definition: MSSpectrum.h:70
void setMetaValue(const String &name, const DataValue &value)
Sets the DataValue corresponding to a name.
File adapter for MsInspect files.
Definition: MsInspectFile.h:60
virtual ~MsInspectFile()
Destructor.
void store(const String &filename, const SpectrumType &spectrum) const
Stores a featureXML as a MsInspect file.
Definition: MsInspectFile.h:166
void load(const String &filename, FeatureMapType &feature_map)
Loads a MsInspect file into a featureXML.
Definition: MsInspectFile.h:76
MsInspectFile()
Default constructor.
void setMZ(CoordinateType coordinate)
Mutable access to the m/z coordinate (index 1)
Definition: Peak2D.h:202
void setRT(CoordinateType coordinate)
Mutable access to the RT coordinate (index 0)
Definition: Peak2D.h:214
void setIntensity(IntensityType intensity)
Sets data point intensity (height)
Definition: Peak2D.h:172
A more convenient string class.
Definition: String.h:60
bool split(const char splitter, std::vector< String > &substrings, bool quote_protect=false) const
Splits a string into substrings using splitter as delimiter.
This class provides some basic file handling methods for text files.
Definition: TextFile.h:47
std::vector< String >::const_iterator ConstIterator
Non-mutable iterator.
Definition: TextFile.h:56
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:127
static Int toInt(const String &this_s)
Definition: StringUtils.h:206
static double toDouble(const String &this_s)
Definition: StringUtils.h:216
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47