OpenMS
Loading...
Searching...
No Matches
MsInspectFile.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
15
16#include <fstream>
17#include <vector>
18
19namespace OpenMS
20{
33 class OPENMS_DLLAPI MsInspectFile
34 {
35public:
39 virtual ~MsInspectFile();
40
49 template <typename FeatureMapType>
50 void load(const std::string& filename, FeatureMapType& feature_map)
51 {
52 // load input
53 TextFile input(filename);
54
55 // reset map
56 FeatureMapType fmap;
57 feature_map = fmap;
58
59 bool first_line = true;
60 for (TextFile::ConstIterator it = input.begin(); it != input.end(); ++it)
61 {
62 std::string line = *it;
63
64 //ignore comment lines
65 if (line.empty() || line[0] == '#') continue;
66
67 //skip leader line
68 if (first_line)
69 {
70 first_line = false;
71 continue;
72 }
73
74 //split lines: scan\ttime\tmz\taccurateMZ\tmass\tintensity\tcharge\tchargeStates\tkl\tbackground\tmedian\tpeaks\tscanFirst\tscanLast\tscanCount\ttotalIntensity\tsumSquaresDist\tdescription
75 std::vector<std::string> parts;
76 StringUtils::split(line, '\t', parts);
77
78 if (parts.size() < 18)
79 {
80 throw Exception::ParseError(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, "","Failed to convert line " + StringUtils::toStr((it - input.begin()) + 1) + ". Not enough columns (expected 18 or more, got " + StringUtils::toStr(parts.size()) + ")");
81 }
82
83 //create feature
84 Feature f;
85 Size column_to_convert = 0;
86 try
87 {
88 column_to_convert = 1;
89 f.setRT(StringUtils::toDouble(parts[1]));
90 column_to_convert = 2;
91 f.setMZ(StringUtils::toDouble(parts[2]));
92 column_to_convert = 5;
93 f.setIntensity(StringUtils::toDouble(parts[5]));
94 column_to_convert = 6;
95 f.setCharge(StringUtils::toInt32(parts[6]));
96 column_to_convert = 8;
97 f.setOverallQuality(StringUtils::toDouble(parts[8]));
98
99 column_to_convert = 3;
100 f.setMetaValue("accurateMZ", parts[3]);
101 column_to_convert = 4;
102 f.setMetaValue("mass", StringUtils::toDouble(parts[4]));
103 column_to_convert = 7;
104 f.setMetaValue("chargeStates", StringUtils::toInt32(parts[7]));
105 column_to_convert = 9;
106 f.setMetaValue("background", StringUtils::toDouble(parts[9]));
107 column_to_convert = 10;
108 f.setMetaValue("median", StringUtils::toDouble(parts[10]));
109 column_to_convert = 11;
110 f.setMetaValue("peaks", StringUtils::toInt32(parts[11]));
111 column_to_convert = 12;
112 f.setMetaValue("scanFirst", StringUtils::toInt32(parts[12]));
113 column_to_convert = 13;
114 f.setMetaValue("scanLast", StringUtils::toInt32(parts[13]));
115 column_to_convert = 14;
116 f.setMetaValue("scanCount", StringUtils::toInt32(parts[14]));
117 column_to_convert = 15;
118 f.setMetaValue("totalIntensity", StringUtils::toDouble(parts[15]));
119 column_to_convert = 16;
120 f.setMetaValue("sumSquaresDist", StringUtils::toDouble(parts[16]));
121 }
122 catch ( Exception::BaseException& )
123 {
124 throw Exception::ParseError(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, "","Failed to convert value in column " + StringUtils::toStr(column_to_convert + 1) + " into a number (line '" + StringUtils::toStr((it - input.begin()) + 1) + ")");
125 }
126 f.setMetaValue("description", parts[17]);
127 feature_map.push_back(f);
128 }
129
130 }
131
139 template <typename SpectrumType>
140 void store(const std::string& filename, const SpectrumType& spectrum) const
141 {
142 std::cerr << "Store() for MsInspectFile not implemented. Filename was: " << filename << ", spec of size " << spectrum.size() << "\n";
143 throw Exception::NotImplemented(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION);
144 }
145
146 };
147} // namespace OpenMS
148
void setCharge(const ChargeType &ch)
Set charge state.
Exception base class.
Definition Exception.h:63
Not implemented exception.
Definition Exception.h:400
Parse Error exception.
Definition Exception.h:593
An LC-MS feature.
Definition Feature.h:46
void setOverallQuality(QualityType q)
Set the overall quality.
The representation of a 1D spectrum.
Definition MSSpectrum.h:44
void setMetaValue(const std::string &name, const DataValue &value)
Sets the DataValue corresponding to a name.
Definition MsInspectFile.h:34
void store(const std::string &filename, const SpectrumType &spectrum) const
Stores a featureXML as a MsInspect file.
Definition MsInspectFile.h:140
virtual ~MsInspectFile()
Destructor.
void load(const std::string &filename, FeatureMapType &feature_map)
Loads a MsInspect file into a featureXML.
Definition MsInspectFile.h:50
MsInspectFile()
Default constructor.
void setMZ(CoordinateType coordinate)
Mutable access to the m/z coordinate (index 1)
Definition Peak2D.h:179
void setRT(CoordinateType coordinate)
Mutable access to the RT coordinate (index 0)
Definition Peak2D.h:191
void setIntensity(IntensityType intensity)
Sets data point intensity (height)
Definition Peak2D.h:149
Definition TextFile.h:21
ConstIterator end() const
Gives access to the underlying text buffer.
std::vector< std::string >::const_iterator ConstIterator
Non-mutable iterator.
Definition TextFile.h:30
ConstIterator begin() const
Gives access to the underlying text buffer.
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition Types.h:97
Main OpenMS namespace.
Definition openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19