libpappsomspp
Library for mass spectrometry
Loading...
Searching...
No Matches
xymsfilereader.cpp
Go to the documentation of this file.
1/////////////////////// StdLib includes
2#include <iostream>
3#include <iomanip>
4
5
6/////////////////////// Qt includes
7#include <QDebug>
8#include <QFile>
9#include <QFileInfo>
10
11
12/////////////////////// libpwiz includes
13#include <pwiz/data/msdata/DefaultReaderList.hpp>
14
15
16/////////////////////// Local includes
17#include "xymsfilereader.h"
22
23
24namespace pappso
25{
26
27
28XyMsFileReader::XyMsFileReader(const QString &file_name) : MsFileReader{file_name}
29{
30}
31
32
36
37
38bool
39XyMsFileReader::initialize(std::size_t &line_count)
40{
41 // Here we just test all the lines of the file to check that they comply with
42 // the xy format.
43
44 line_count = 0;
45
46 QFile file(m_fileName);
47
48 if(!file.open(QFile::ReadOnly | QFile::Text))
49 {
50 qDebug() << "Failed to open file" << m_fileName;
51
52 return 0;
53 }
54
55 QRegularExpressionMatch regExpMatch;
56
57 QString line;
58 bool file_reading_failed = false;
59
60 while(!file.atEnd())
61 {
62 line = file.readLine();
63
64 // We only read a given number of lines from the file, that would be
65 // enough to check if that file has the right syntax or not.
66 // if(linesRead >= 2000)
67 // return true;
68
69 if(line.startsWith('#') || line.isEmpty() || Utils::endOfLineRegExp.match(line).hasMatch())
70 continue;
71
72 qDebug() << "Current xy format line:" << line;
73
74 if(Utils::xyMassDataFormatRegExp.match(line).hasMatch())
75 {
76 ++line_count;
77 continue;
78 }
79 else
80 {
81 // the first line of the text file may include column titles
82 if(line_count > 0)
83 {
84 file_reading_failed = true;
85 break;
86 }
87 }
88 }
89
90 if(!file_reading_failed && line_count >= 1)
91 {
93 return true;
94 }
95
97
98 // qDebug() << "m_fileFormat: " << static_cast<int>(m_fileFormat);
99
100 return false;
101}
102
103
109
110
111std::vector<MsRunIdCstSPtr>
112XyMsFileReader::getMsRunIds(const QString &run_prefix)
113{
114 std::vector<MsRunIdCstSPtr> ms_run_ids;
115
116 std::size_t line_count = 0;
117
118 if(!initialize(line_count))
119 return ms_run_ids;
120
121 // Finally create the MsRunId with the file name.
122 MsRunId ms_run_id(m_fileName);
123 ms_run_id.setMsDataFormat(m_fileFormat);
124
125 // We need to set the unambiguous xmlId string.
126 ms_run_id.setXmlId(QString("%1%2").arg(run_prefix).arg(Utils::getLexicalOrderedString(0)));
127
128 // Craft a meaningful sample name because otherwise all the files loaded from
129 // text files will have the same sample name and it will be difficult to
130 // differentiate them.
131 // Orig version:
132 // ms_run_id.setRunId("Single spectrum");
133 // Now the sample name is nothing but the file name without the path.
134
135 QFileInfo file_info(m_fileName);
136
137 // qDebug() << "file name:" << m_fileName;
138
139 QString sample_name = file_info.fileName();
140
141 // qDebug() << "sample name:" << sample_name;
142
143 ms_run_id.setRunId(sample_name);
144
145 // Now set the sample name to the run id:
146
147 ms_run_id.setSampleName(ms_run_id.getRunId());
148
149 // Now set the sample name to the run id:
150
151 ms_run_id.setSampleName(ms_run_id.getRunId());
152
153 // qDebug() << __FILE__ << "@" << __LINE__ << __FUNCTION__ << "()"
154 //<< "Current ms_run_id:" << ms_run_id.toString();
155
156 // Finally make a shared pointer out of it and append it to the vector.
157 ms_run_ids.push_back(std::make_shared<MsRunId>(ms_run_id));
158
159 return ms_run_ids;
160}
161
162
163} // namespace pappso
MsFileReader(const QString &file_name)
Enums::MsDataFormat m_fileFormat
MS run identity MsRunId identifies an MS run with a unique ID (XmlId) and contains eventually informa...
Definition msrunid.h:54
void setMsDataFormat(Enums::MsDataFormat format)
Definition msrunid.cpp:168
const QString & getRunId() const
Definition msrunid.cpp:140
void setRunId(const QString &run_id)
Definition msrunid.cpp:133
void setXmlId(const QString &xml_id)
set an XML unique identifier for this MsRunId
Definition msrunid.cpp:147
void setSampleName(const QString &name)
set a sample name for this MsRunId
Definition msrunid.cpp:77
static QRegularExpression xyMassDataFormatRegExp
Regular expression matching <numerical value><non-numerical*><numericalvalue>.
Definition utils.h:60
static const QString getLexicalOrderedString(unsigned int num)
Definition utils.cpp:73
static QRegularExpression endOfLineRegExp
Regular expression that tracks the end of line in text files.
Definition utils.h:69
virtual std::vector< MsRunIdCstSPtr > getMsRunIds(const QString &run_prefix) override
virtual Enums::MsDataFormat getFileFormat() override
virtual bool initialize(std::size_t &line_count)
XyMsFileReader(const QString &file_name)
@ unknown
unknown format
Definition types.h:149
tries to keep as much as possible monoisotopes, removing any possible C13 peaks and changes multichar...
Definition aa.cpp:39