| OpenMS 2.5.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-2020.
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
37 #include <OpenMS/DATASTRUCTURES/String.h>
38 #include <OpenMS/CONCEPT/Exception.h>
39 #include <OpenMS/CONCEPT/LogStream.h>
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:
64 SpecArrayFile();
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
OpenMS::Feature
An LC-MS feature.
Definition: Feature.h:70
OpenMS::Peak2D::setRT
void setRT(CoordinateType coordinate)
Mutable access to the RT coordinate (index 0)
Definition: Peak2D.h:214
OpenMS::Exception::BaseException
Exception base class.
Definition: Exception.h:89
OpenMS::String
A more convenient string class.
Definition: String.h:58
OpenMS::TextFile::ConstIterator
std::vector< String >::const_iterator ConstIterator
Non-mutable iterator.
Definition: TextFile.h:56
Feature.h
OpenMS::Peak2D::setMZ
void setMZ(CoordinateType coordinate)
Mutable access to the m/z coordinate (index 1)
Definition: Peak2D.h:202
OpenMS::SpecArrayFile::load
void load(const String &filename, FeatureMapType &feature_map)
Loads a SpecArray file into a featureXML.
Definition: SpecArrayFile.h:77
Exception.h
OpenMS::MSSpectrum
The representation of a 1D spectrum.
Definition: MSSpectrum.h:67
OpenMS::BaseFeature::setCharge
void setCharge(const ChargeType &ch)
Set charge state.
String.h
TextFile.h
OpenMS
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:46
OpenMS::SpecArrayFile
File adapter for SpecArray (.pepList) files.
Definition: SpecArrayFile.h:60
OpenMS::Exception::ParseError
Parse Error exception.
Definition: Exception.h:622
LogStream.h
OpenMS::TextFile::begin
ConstIterator begin() const
Gives access to the underlying text buffer.
OpenMS::SpecArrayFile::store
void store(const String &filename, const SpectrumType &spectrum) const
Stores a featureXML as a SpecArray file.
Definition: SpecArrayFile.h:129
OpenMS::String::split
bool split(const char splitter, std::vector< String > &substrings, bool quote_protect=false) const
Splits a string into substrings using splitter as delimiter.
OpenMS::MetaInfoInterface::setMetaValue
void setMetaValue(const String &name, const DataValue &value)
Sets the DataValue corresponding to a name.
OpenMS::Exception::NotImplemented
Not implemented exception.
Definition: Exception.h:436
OpenMS::TextFile
This class provides some basic file handling methods for text files.
Definition: TextFile.h:46
OpenMS::TextFile::end
ConstIterator end() const
Gives access to the underlying text buffer.
OpenMS::Peak2D::setIntensity
void setIntensity(IntensityType intensity)
Non-mutable access to the data point intensity (height)
Definition: Peak2D.h:172