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