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