OpenMS  2.8.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 
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 SpecArrayFile
60  {
61 public:
65  virtual ~SpecArrayFile();
66 
75  template <typename FeatureMapType>
76  void load(const String& filename, FeatureMapType& feature_map)
77  {
78  // load input
79  TextFile input(filename, false);
80 
81  // reset map
82  FeatureMapType fmap;
83  feature_map = fmap;
84 
85  TextFile::ConstIterator it = input.begin();
86  if (it == input.end()) return; // no data to load
87 
88  // skip header line
89  ++it;
90  // process content
91  for (; it != input.end(); ++it)
92  {
93  String line = *it;
94 
95  std::vector<String> parts;
96  line.split('\t', parts);
97 
98  if (parts.size() < 5)
99  {
100  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()) + ")");
101  }
102 
103  Feature f;
104  try
105  {
106  f.setMZ(parts[0].toDouble());
107  f.setRT(parts[1].toDouble() * 60.0);
108  f.setMetaValue("s/n", parts[2].toDouble());
109  f.setCharge(parts[3].toInt());
110  f.setIntensity(parts[4].toDouble());
111  }
112  catch ( Exception::BaseException& )
113  {
114  throw Exception::ParseError(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, "", String("Failed to convert value into a number (line '") + String((it - input.begin()) + 1) + ")");
115  }
116  feature_map.push_back(f);
117  }
118  }
119 
127  template <typename SpectrumType>
128  void store(const String& filename, const SpectrumType& spectrum) const
129  {
130  std::cerr << "Store() for SpecArrayFile not implemented. Filename was: " << filename << ", spec of size " << spectrum.size() << "\n";
131  throw Exception::NotImplemented(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION);
132  }
133 
134  };
135 } // namespace OpenMS
136 
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
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.
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
File adapter for SpecArray (.pepList) files.
Definition: SpecArrayFile.h:60
SpecArrayFile()
Default constructor.
void store(const String &filename, const SpectrumType &spectrum) const
Stores a featureXML as a SpecArray file.
Definition: SpecArrayFile.h:128
virtual ~SpecArrayFile()
Destructor.
void load(const String &filename, FeatureMapType &feature_map)
Loads a SpecArray file into a featureXML.
Definition: SpecArrayFile.h:76
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.
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