OpenMS
SpecArrayFile.h
Go to the documentation of this file.
1 // Copyright (c) 2002-present, OpenMS Inc. -- 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 SpecArrayFile
34  {
35 public:
39  virtual ~SpecArrayFile();
40 
49  template <typename FeatureMapType>
50  void load(const String& filename, FeatureMapType& feature_map)
51  {
52  // load input
53  TextFile input(filename, false);
54 
55  // reset map
56  FeatureMapType fmap;
57  feature_map = fmap;
58 
59  TextFile::ConstIterator it = input.begin();
60  if (it == input.end()) return; // no data to load
61 
62  // skip header line
63  ++it;
64  // process content
65  for (; it != input.end(); ++it)
66  {
67  String line = *it;
68 
69  std::vector<String> parts;
70  line.split('\t', parts);
71 
72  if (parts.size() < 5)
73  {
74  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()) + ")");
75  }
76 
77  Feature f;
78  try
79  {
80  f.setMZ(parts[0].toDouble());
81  f.setRT(parts[1].toDouble() * 60.0);
82  f.setMetaValue("s/n", parts[2].toDouble());
83  f.setCharge(parts[3].toInt());
84  f.setIntensity(parts[4].toDouble());
85  }
86  catch ( Exception::BaseException& )
87  {
88  throw Exception::ParseError(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, "", String("Failed to convert value into a number (line '") + String((it - input.begin()) + 1) + ")");
89  }
90  feature_map.push_back(f);
91  }
92  }
93 
101  template <typename SpectrumType>
102  void store(const String& filename, const SpectrumType& spectrum) const
103  {
104  std::cerr << "Store() for SpecArrayFile not implemented. Filename was: " << filename << ", spec of size " << spectrum.size() << "\n";
105  throw Exception::NotImplemented(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION);
106  }
107 
108  };
109 } // namespace OpenMS
110 
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
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.
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
File adapter for SpecArray (.pepList) files.
Definition: SpecArrayFile.h:34
SpecArrayFile()
Default constructor.
void store(const String &filename, const SpectrumType &spectrum) const
Stores a featureXML as a SpecArray file.
Definition: SpecArrayFile.h:102
virtual ~SpecArrayFile()
Destructor.
void load(const String &filename, FeatureMapType &feature_map)
Loads a SpecArray file into a featureXML.
Definition: SpecArrayFile.h:50
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.
static double toDouble(const String &this_s)
Definition: StringUtils.h:216
Main OpenMS namespace.
Definition: openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19