OpenMS  2.7.0
SpecArrayFile.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 
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 SpecArrayFile
61  {
62 public:
66  virtual ~SpecArrayFile();
67 
76  template <typename FeatureMapType>
77  void load(const String& filename, FeatureMapType& feature_map)
78  {
79  // load input
80  TextFile input(filename, false);
81 
82  // reset map
83  FeatureMapType fmap;
84  feature_map = fmap;
85 
86  TextFile::ConstIterator it = input.begin();
87  if (it == input.end()) return; // no data to load
88 
89  // skip header line
90  ++it;
91  // process content
92  for (; it != input.end(); ++it)
93  {
94  String line = *it;
95 
96  std::vector<String> parts;
97  line.split('\t', parts);
98 
99  if (parts.size() < 5)
100  {
101  throw Exception::ParseError(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, "", String("Failed to convert line") + String((it - input.begin()) + 1) + "not enough columns (expected 5 or more, got " + String(parts.size()) + ")");
102  }
103 
104  Feature f;
105  try
106  {
107  f.setMZ(parts[0].toDouble());
108  f.setRT(parts[1].toDouble() * 60.0);
109  f.setMetaValue("s/n", parts[2].toDouble());
110  f.setCharge(parts[3].toInt());
111  f.setIntensity(parts[4].toDouble());
112  }
113  catch ( Exception::BaseException& )
114  {
115  throw Exception::ParseError(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, "", String("Failed to convert value into a number (line '") + String((it - input.begin()) + 1) + ")");
116  }
117  feature_map.push_back(f);
118  }
119  }
120 
128  template <typename SpectrumType>
129  void store(const String& filename, const SpectrumType& spectrum) const
130  {
131  std::cerr << "Store() for SpecArrayFile not implemented. Filename was: " << filename << ", spec of size " << spectrum.size() << "\n";
132  throw Exception::NotImplemented(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION);
133  }
134 
135  };
136 } // namespace OpenMS
137 
void setCharge(const ChargeType &ch)
Set charge state.
Exception base class.
Definition: Exception.h:92
Not implemented exception.
Definition: Exception.h:430
Parse Error exception.
Definition: Exception.h:630
An LC-MS feature.
Definition: Feature.h:72
The representation of a 1D spectrum.
Definition: MSSpectrum.h:71
void setMetaValue(const String &name, const DataValue &value)
Sets the DataValue corresponding to a name.
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)
Non-mutable access to the data point intensity (height)
Definition: Peak2D.h:172
File adapter for SpecArray (.pepList) files.
Definition: SpecArrayFile.h:61
SpecArrayFile()
Default constructor.
void store(const String &filename, const SpectrumType &spectrum) const
Stores a featureXML as a SpecArray file.
Definition: SpecArrayFile.h:129
virtual ~SpecArrayFile()
Destructor.
void load(const String &filename, FeatureMapType &feature_map)
Loads a SpecArray file into a featureXML.
Definition: SpecArrayFile.h:77
A more convenient string class.
Definition: String.h:61
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.
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47