GCC Code Coverage Report


./
File: src/XpertMass/MonomerSpec.cpp
Date: 2024-08-24 11:26:06
Lines:
0/66
0.0%
Functions:
0/15
0.0%
Branches:
0/52
0.0%

Line Branch Exec Source
1 /* BEGIN software license
2 *
3 * MsXpertSuite - mass spectrometry software suite
4 * -----------------------------------------------
5 * Copyright(C) 2009,...,2018 Filippo Rusconi
6 *
7 * http://www.msxpertsuite.org
8 *
9 * This file is part of the MsXpertSuite project.
10 *
11 * The MsXpertSuite project is the successor of the massXpert project. This
12 * project now includes various independent modules:
13 *
14 * - massXpert, model polymer chemistries and simulate mass spectrometric data;
15 * - mineXpert, a powerful TIC chromatogram/mass spectrum viewer/miner;
16 *
17 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License
28 * along with this program. If not, see <http://www.gnu.org/licenses/>.
29 *
30 * END software license
31 */
32
33
34 /////////////////////// Qt includes
35 #include <QFile>
36 #include <QDebug>
37 #include <QRegularExpression>
38 #include <QRegularExpressionMatch>
39
40 /////////////////////// Local includes
41 #include "MonomerSpec.hpp"
42
43
44 namespace MsXpS
45 {
46
47 namespace libXpertMass
48 {
49
50 /*!
51 \class MsXpS::libXpertMass::MonomerSpec
52 \inmodule libXpertMass
53 \ingroup PolChemDefBuildingdBlocks
54 \inheaderfile MonomerSpec.hpp
55
56 \brief The MonomerSpec class provides the specification about how \l Monomer
57 objects are represented.
58
59 The MonomerSpec class specifies how a \l Monomer object is represented
60 graphically, mainly by connecting its monomer code to a graphics SVG file that
61 is located in the Polymer chemistry definition directory. That connection is
62 performed in the "monomer_dictionary" dictionary file itself also located in
63 the polymer chemistry definition directory. Its contents look like this:
64
65 \code
66 A%alanine.svg
67 C%cysteine.svg
68 D%aspartate.svg
69 E%glutamate.svg
70 F%phenylalanine.svg
71 G%glycine.svg
72 H%histidine.svg"
73 \endcode
74
75 The \c{A%alanine.svg} line indicates that, when a Monomer object by code 'A' is
76 to be rendered graphically, the corresponding vignette to be used is in the
77 file named "alanine.svg" in the polymer chemistry definition directory.
78 */
79
80 /*!
81 \variable int MsXpS::libXpertMass::MonomerSpec::m_name
82
83 \brief The name of the monomer.
84 */
85
86 /*!
87 \variable int MsXpS::libXpertMass::MonomerSpec::m_code
88
89 \brief The code of the monomer.
90 */
91
92 /*!
93 \variable int MsXpS::libXpertMass::MonomerSpec::m_raster
94
95 \brief The file name of the raster representation of the monomer.
96 */
97
98 /*!
99 \variable int MsXpS::libXpertMass::MonomerSpec::m_vector
100
101 \brief The filename of the vector representation of the monomer.
102 */
103
104 /*!
105 \variable int MsXpS::libXpertMass::MonomerSpec::m_nameSound
106
107 \brief The file name of the sound for the name of the monomer.
108 */
109
110 /*!
111 \variable int MsXpS::libXpertMass::MonomerSpec::m_codeSound
112
113 \brief The file name of the sound for the code of the monomer.
114 */
115
116 /*!
117 \brief Constructs a MonomerSpec instance.
118 */
119 MonomerSpec::MonomerSpec()
120 {
121 }
122
123 /*!
124 \brief Destructs this MonomerSpec instance.
125 */
126 MonomerSpec::~MonomerSpec()
127 {
128 }
129
130 /*!
131 \brief Sets the monomer \a name.
132 */
133 void
134 MonomerSpec::setName(const QString &name)
135 {
136 m_name = name;
137 }
138
139 /*!
140 \brief Returns the monomer name.
141 */
142 const QString &
143 MonomerSpec::name()
144 {
145 return m_name;
146 }
147
148
149 /*!
150 \brief Sets the monomer \a code.
151 */
152 void
153 MonomerSpec::setCode(const QString &code)
154 {
155 m_code = code;
156 }
157
158
159 /*!
160 \brief Returns the monomer code.
161 */
162 const QString &
163 MonomerSpec::code()
164 {
165 return m_code;
166 }
167
168 /*!
169 \brief Sets the raster image file name to \a raster.
170 */
171 void
172 MonomerSpec::setRaster(const QString &raster)
173 {
174 m_raster = raster;
175 }
176
177 /*!
178 \brief Returns the raster image file name.
179 */
180 const QString &
181 MonomerSpec::raster()
182 {
183 return m_raster;
184 }
185
186 /*!
187 \brief Sets the vector image file name to \a vector.
188 */
189 void
190 MonomerSpec::setVector(const QString &vector)
191 {
192 m_vector = vector;
193 }
194
195 /*!
196 \brief Returns the vector image file name.
197 */
198 const QString &
199 MonomerSpec::vector()
200 {
201 return m_vector;
202 }
203
204
205 /*!
206 \brief Sets the file name of the Monomer's name \a sound file.
207 */
208 void
209 MonomerSpec::setNameSound(const QString &sound)
210 {
211 m_nameSound = sound;
212 }
213
214
215 /*!
216 \brief Returns the file name of the Monomer's name sound file.
217 */
218 const QString &
219 MonomerSpec::nameSound()
220 {
221 return m_nameSound;
222 }
223
224
225 /*!
226 \brief Sets the file name of the Monomer's code \a sound file.
227 */
228 void
229 MonomerSpec::setCodeSound(const QString &sound)
230 {
231 m_codeSound = sound;
232 }
233
234
235 /*!
236 \brief Returns the file name of the Monomer's code sound file.
237 */
238 const QString &
239 MonomerSpec::codeSound()
240 {
241 return m_codeSound;
242 }
243
244
245 /*!
246 \brief Parses the \a file_path dictionary containing the Monomer
247 specifications.
248
249 At the moment the file has this format:
250
251 \code
252 A%alanine.svg
253 C%cysteine.svg
254 D%aspartate.svg
255 E%glutamate.svg
256 \endcode
257
258 Upon parsing, the \a monomer_spec_list of MonomerSpec instances will
259 be filled with instances created on the basis of each parsed line in the file.
260
261 Returns true if the parsing was successful, false otherwise.
262 */
263 bool
264 MonomerSpec::parseFile(QString &file_path, QList<MonomerSpec *>
265 *monomer_spec_list)
266 {
267 MonomerSpec *monomerSpec = 0;
268
269 qint64 lineLength;
270
271 QString line;
272 QString temp;
273
274 char buffer[1024];
275
276 Q_ASSERT(monomer_spec_list != 0);
277
278 if(file_path.isEmpty())
279 return false;
280
281 QFile file(file_path);
282
283 if(!file.open(QFile::ReadOnly))
284 return false;
285
286 // The lines we have to parse are of the following type:
287 // A%alanine.svg|alanine.png
288 // Any line starting with '#' are not parsed.
289
290 // Get the first line of the file. Next we enter in to a
291 // while loop.
292
293 lineLength = file.readLine(buffer, sizeof(buffer));
294
295 while(lineLength != -1)
296 {
297 // The line is now in buffer, and we want to convert
298 // it to Unicode by setting it in a QString.
299 line = buffer;
300
301 // The line that is in line should contain something like:
302 // A%alanine.svg
303
304 // Remove all the spaces from the borders: whitespace means any
305 // character for which QChar::isSpace() returns true. This
306 // includes the ASCII characters '\t', '\n', '\v', '\f', '\r',
307 // and ' '.
308
309 line = line.trimmed();
310
311 if(line.isEmpty() || line.startsWith('#', Qt::CaseInsensitive))
312 {
313 lineLength = file.readLine(buffer, sizeof(buffer));
314 continue;
315 }
316
317 // Now some other checks. Remember the format of the line:
318 // A%alanine.svg
319 QString code;
320 QString file_name;
321 QRegularExpression reg_exp(QString("^([A-Z][a-z]*)%(.*)$"));
322
323 QRegularExpressionMatch match = reg_exp.match(line);
324
325 if(match.hasMatch())
326 {
327 code = match.captured(1);
328 file_name = match.captured(2);
329 }
330
331 // qDebug() << "code:" << code << "file_name:" << file_name;
332
333 // OK, we finally can allocate a new MonomerSpec *.
334 monomerSpec = new MonomerSpec();
335
336 monomerSpec->m_code = code;
337 monomerSpec->m_vector = file_name;
338
339 monomer_spec_list->append(monomerSpec);
340 lineLength = file.readLine(buffer, sizeof(buffer));
341 }
342
343 return true;
344 }
345
346 } // namespace libXpertMass
347
348 } // namespace MsXpS
349