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