libpappsomspp
Library for mass spectrometry
Toggle main menu visibility
Loading...
Searching...
No Matches
sagereader.cpp
Go to the documentation of this file.
1
/**
2
* \file input/sage/sagereader.cpp
3
* \date 21/08/2024
4
* \author Olivier Langella
5
* \brief read data files from Sage output
6
*/
7
8
/*******************************************************************************
9
* Copyright (c) 2024 Olivier Langella
10
*<Olivier.Langella@universite-paris-saclay.fr>.
11
*
12
* This file is part of i2MassChroQ.
13
*
14
* i2MassChroQ is free software: you can redistribute it and/or modify
15
* it under the terms of the GNU General Public License as published by
16
* the Free Software Foundation, either version 3 of the License, or
17
* (at your option) any later version.
18
*
19
* i2MassChroQ is distributed in the hope that it will be useful,
20
* but WITHOUT ANY WARRANTY; without even the implied warranty of
21
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22
* GNU General Public License for more details.
23
*
24
* You should have received a copy of the GNU General Public License
25
* along with i2MassChroQ. If not, see <http://www.gnu.org/licenses/>.
26
*
27
******************************************************************************/
28
29
#include "
sagereader.h
"
30
#include <QJsonObject>
31
#include <QJsonArray>
32
#include <odsdochandlerinterface.h>
33
#include <odsstream/tsvreader.h>
34
#include <odsstream/odsexception.h>
35
#include <QUrl>
36
#include <qcontainerfwd.h>
37
#include <qfileinfo.h>
38
#include <qlogging.h>
39
#include <qobject.h>
40
#include "
pappsomspp/core/types.h
"
41
#include "
sagetsvhandler.h
"
42
#include "
pappsomspp/core/utils.h
"
43
#include "
pappsomspp/core/exception/exceptionnotfound.h
"
44
#include "
pappsomspp/core/pappsoexception.h
"
45
#include "
../../../../fasta/fastareader.h
"
46
47
pappso::cbor::psm::SageReader::SageReader
(
pappso::UiMonitorInterface
*p_monitor,
48
pappso::cbor::CborStreamWriter
*p_output,
49
const
pappso::cbor::psm::SageFileReader
&sage_file_reader,
50
const
QString &sage_json_file)
51
:
m_sageFileReader
(sage_file_reader)
52
{
53
mp_monitor
= p_monitor;
54
mp_cborWriter
= p_output;
55
m_jsonAbsoluteFilePath
= sage_json_file;
56
}
57
58
pappso::cbor::psm::SageReader::~SageReader
()
59
{
60
}
61
62
const
QString &
63
pappso::cbor::psm::SageReader::getmJsonAbsoluteFilePath
()
const
64
{
65
return
m_jsonAbsoluteFilePath
;
66
}
67
68
69
void
70
pappso::cbor::psm::SageReader::read
()
71
{
72
readTsvFile
();
73
}
74
75
pappso::cbor::psm::SageReader::FastaSeq::FastaSeq
(
SageReader
*self)
76
{
77
mp_self
= self;
78
m_decoyTag
=
mp_self
->getDecoyTag();
79
}
80
81
void
82
pappso::cbor::psm::SageReader::FastaSeq::setSequence
(
const
QString &description_in,
83
const
QString &sequence_in)
84
{
85
86
QStringList description_split = description_in.split(
" "
, Qt::SkipEmptyParts);
87
QString accession = description_split.at(0);
88
if
(description_split.length() > 0)
89
description_split.remove(0);
90
try
91
{
92
const
PsmProtein
&psm_protein =
mp_self
->m_psmProteinMap.getByAccession(accession);
93
psm_protein.
protein_sp
.get()->setSequence(sequence_in);
94
psm_protein.
protein_sp
.get()->setDescription(description_split.join(
" "
));
95
}
96
catch
(
pappso::ExceptionNotFound
&err)
97
{
98
}
99
try
100
{
101
QString rev_accession = accession.prepend(
m_decoyTag
);
102
const
PsmProtein
&psm_protein =
mp_self
->m_psmProteinMap.getByAccession(rev_accession);
103
psm_protein.
protein_sp
.get()->setSequence(sequence_in);
104
105
// description_split[0] = rev_accession;
106
psm_protein.
protein_sp
.get()->setDescription(description_split.join(
" "
));
107
psm_protein.
protein_sp
.get()->reverse();
108
}
109
catch
(
pappso::ExceptionNotFound
&err)
110
{
111
}
112
}
113
114
pappso::cbor::CborStreamWriter
&
115
pappso::cbor::psm::SageReader::getCborStreamWriter
()
const
116
{
117
return
*
mp_cborWriter
;
118
}
119
120
121
void
122
pappso::cbor::psm::SageReader::readTsvFile
()
123
{
124
125
extractMzmlPathList
(
m_sageFileReader
.getJsonDocument());
126
// getTsvFilePath(mp_identificationDataSource->getJsonDocument().object());
127
QString file_str =
getTsvFilePath
(
m_sageFileReader
.getJsonDocument());
128
QFileInfo tsv_file_info(file_str);
129
SageTsvHandler
handler(
mp_monitor
, *
this
,
m_psmProteinMap
);
130
try
131
{
132
TsvReader tsv_reader(handler);
133
134
QFile tsv_file(tsv_file_info.absoluteFilePath());
135
tsv_reader.parse(tsv_file);
136
tsv_file.close();
137
}
138
catch
(OdsException &error_ods)
139
{
140
throw
pappso::PappsoException
(QObject::tr(
"Error reading %1 file:\n %2"
)
141
.arg(tsv_file_info.absoluteFilePath())
142
.arg(error_ods.qwhat()));
143
}
144
145
146
// collect protein sequences
147
QFile fastaFile(
getFastaFilePath
(
m_sageFileReader
.getJsonDocument()));
148
SageReader::FastaSeq
seq(
this
);
149
pappso::FastaReader
reader(seq);
150
reader.
parse
(fastaFile);
151
152
qDebug();
153
mp_cborWriter
->append(
"protein_map"
);
154
m_psmProteinMap
.writeMap(*
mp_cborWriter
);
155
156
157
mp_cborWriter
->append(
"sample_list"
);
158
mp_cborWriter
->startArray();
159
try
160
{
161
handler.
writeSampleList
();
162
}
163
catch
(OdsException &error_ods)
164
{
165
throw
pappso::PappsoException
(QObject::tr(
"Error reading %1 file:\n %2"
)
166
.arg(tsv_file_info.absoluteFilePath())
167
.arg(error_ods.qwhat()));
168
}
169
170
mp_cborWriter
->endArray();
171
}
172
173
void
174
pappso::cbor::psm::SageReader::extractMzmlPathList
(
const
QJsonDocument &json_doc)
175
{
176
177
QJsonObject sage_object = json_doc.object();
178
QJsonValue json_mzml_path_list = sage_object.value(
"mzml_paths"
);
179
if
(json_mzml_path_list.isUndefined())
180
{
181
throw
pappso::ExceptionNotFound
(QObject::tr(
"mzml_paths not found in Sage json document"
));
182
}
183
m_mzmlPathList
.clear();
184
185
for
(
auto
path_mzml : json_mzml_path_list.toArray())
186
{
187
m_mzmlPathList
<<
convertToLocalFileOrDie
(path_mzml.toString());
188
}
189
}
190
191
const
QString &
192
pappso::cbor::psm::SageReader::getMzmlPath
(
const
QString &file_msrun)
const
193
{
194
for
(
auto
&file_path :
m_mzmlPathList
)
195
{
196
if
(file_path.endsWith(file_msrun))
197
return
file_path;
198
}
199
throw
pappso::ExceptionNotFound
(
200
QObject::tr(
"MS run %1 not found in Sage json document"
).append(file_msrun));
201
}
202
203
204
QString
205
pappso::cbor::psm::SageReader::getTsvFilePath
(
const
QJsonDocument &json_doc)
206
{
207
QString path;
208
QJsonObject sage_object = json_doc.object();
209
QJsonValue output_path = sage_object.value(
"output_paths"
);
210
if
(output_path.isUndefined())
211
{
212
throw
pappso::ExceptionNotFound
(QObject::tr(
"output_paths not found in Sage json document"
));
213
}
214
215
if
(!output_path.isArray())
216
{
217
throw
pappso::ExceptionNotFound
(QObject::tr(
"output_paths is not an array"
));
218
}
219
for
(
auto
element : output_path.toArray())
220
{
221
if
(element.isString())
222
{
223
if
(element.toString().endsWith(
".tsv"
))
224
{
225
path = element.toString();
226
}
227
}
228
}
229
230
return
convertToLocalFileOrDie
(path);
231
}
232
233
QString
234
pappso::cbor::psm::SageReader::getFastaFilePath
(
const
QJsonDocument &json_doc)
235
{
236
QString path;
237
QJsonObject sage_object = json_doc.object();
238
QJsonValue database = sage_object.value(
"database"
);
239
if
(database.isUndefined())
240
{
241
throw
pappso::ExceptionNotFound
(QObject::tr(
"database not found in Sage json document"
));
242
}
243
path = database.toObject().value(
"fasta"
).toString();
244
if
(path.isEmpty())
245
{
246
throw
pappso::ExceptionNotFound
(QObject::tr(
"fasta value is empty"
));
247
}
248
249
250
return
convertToLocalFileOrDie
(path);
251
}
252
253
QString
254
pappso::cbor::psm::SageReader::convertToLocalFileOrDie
(
const
QString &path)
const
255
{
256
257
// if we have an URL : convert it to local file
258
qDebug() << path;
259
if
(path.startsWith(
"file:"
) || path.startsWith(
"http:"
) || path.startsWith(
"https:"
))
260
{
261
QUrl tsv_url(path);
262
if
(tsv_url.isValid())
263
{
264
qDebug() <<
"tsv_url.isValid()"
;
265
if
(tsv_url.isLocalFile())
266
{
267
qDebug() <<
"tsv_url.isLocalFile()"
;
268
return
tsv_url.toLocalFile();
269
}
270
else
271
{
272
throw
pappso::PappsoException
(
273
QObject::tr(
"Unable to load data from remote URL %1"
).arg(tsv_url.toString()));
274
}
275
}
276
}
277
return
path;
278
}
279
280
std::vector<pappso::cbor::psm::SageReader::SageModification>
281
pappso::cbor::psm::SageReader::getStaticModificationList
()
const
282
{
283
std::vector<SageReader::SageModification> list;
284
QJsonObject sage_object =
m_sageFileReader
.getJsonDocument().object();
285
QJsonValue database = sage_object.value(
"database"
);
286
if
(database.isUndefined())
287
{
288
throw
pappso::ExceptionNotFound
(QObject::tr(
"database not found in Sage json document"
));
289
}
290
291
QJsonValue static_mods = database.toObject().value(
"static_mods"
);
292
if
(static_mods.isUndefined())
293
{
294
throw
pappso::ExceptionNotFound
(QObject::tr(
"static_mods not found in Sage json document"
));
295
}
296
for
(QString residue_str : static_mods.toObject().keys())
297
{
298
SageModification
modif;
299
modif.
residue
= (
Enums::AminoAcidChar
)residue_str.at(0).toLatin1();
300
modif.
modification
=
pappso::Utils::guessAaModificationPbyMonoisotopicMassDelta
(
301
modif.
residue
, static_mods.toObject().value(residue_str).toDouble());
302
modif.
strModification
=
303
QString::number(static_mods.toObject().value(residue_str).toDouble(),
'f'
, 6);
304
if
(modif.
strModification
.isEmpty())
305
{
306
throw
pappso::PappsoException
(QObject::tr(
" modif.strModification is empty"
));
307
}
308
if
(modif.
modification
->
getMass
() < 0)
309
{
310
modif.
strModification
= QString(
"[%1]"
).arg(modif.
strModification
);
311
}
312
else
313
{
314
modif.
strModification
= QString(
"[+%1]"
).arg(modif.
strModification
);
315
}
316
list.push_back(modif);
317
}
318
return
list;
319
}
320
321
std::vector<pappso::cbor::psm::SageReader::SageModification>
322
pappso::cbor::psm::SageReader::getVariableModificationList
()
const
323
{
324
std::vector<SageReader::SageModification> list;
325
QJsonObject sage_object =
m_sageFileReader
.getJsonDocument().object();
326
QJsonValue database = sage_object.value(
"database"
);
327
if
(database.isUndefined())
328
{
329
throw
pappso::ExceptionNotFound
(QObject::tr(
"database not found in Sage json document"
));
330
}
331
332
QJsonValue var_mods = database.toObject().value(
"variable_mods"
);
333
if
(var_mods.isUndefined())
334
{
335
throw
pappso::ExceptionNotFound
(QObject::tr(
"variable_mods not found in Sage json document"
));
336
}
337
for
(QString residue_str : var_mods.toObject().keys())
338
{
339
qDebug() << residue_str;
340
SageModification
modif;
341
modif.
residue
= (
Enums::AminoAcidChar
)residue_str.at(0).toLatin1();
342
if
(!
Utils::isAminoAcidChar
(modif.
residue
))
343
{
344
qDebug() <<
"not amino acid"
;
345
QChar pos = residue_str.at(0).toLatin1();
346
qDebug() << residue_str <<
"not amino acid "
<< pos;
347
if
(pos ==
'^'
)
348
{
349
modif.
position
= pos;
350
modif.
residue
= (
Enums::AminoAcidChar
)residue_str.at(1).toLatin1();
351
qDebug() << (char)modif.
residue
;
352
if
(!
Utils::isAminoAcidChar
(modif.
residue
))
353
{
354
throw
pappso::PappsoException
(QObject::tr(
"unable to parse %1"
).arg(residue_str));
355
}
356
}
357
else
if
((pos ==
'['
) || (pos ==
']'
))
358
{
359
modif.
position
= pos;
360
// modif.residue = (Enums::AminoAcidChar)residue_str.at(1).toLatin1();
361
// qDebug() << (char)modif.residue;
362
}
363
else
364
{
365
throw
pappso::PappsoException
(QObject::tr(
"unable to parse %1"
).arg(residue_str));
366
}
367
qDebug() << (char)modif.
residue
;
368
369
qDebug() << (char)modif.
residue
;
370
}
371
372
// Enums::AminoAcidChar aa = (Enums::AminoAcidChar)modif.residue.toLatin1();
373
qDebug() << residue_str <<
" "
<< (char)modif.
residue
;
374
for
(QJsonValue one_mass : var_mods.toObject().value(residue_str).toArray())
375
{
376
modif.
modification
=
pappso::Utils::guessAaModificationPbyMonoisotopicMassDelta
(
377
modif.
residue
, one_mass.toDouble(), modif.
position
);
378
qDebug() << modif.
modification
->
getAccession
();
379
modif.
strModification
= QString::number(one_mass.toDouble(),
'f'
, 6);
380
if
(modif.
strModification
.isEmpty())
381
{
382
throw
pappso::PappsoException
(QObject::tr(
" modif.strModification is empty"
));
383
}
384
if
(modif.
modification
->
getMass
() < 0)
385
{
386
modif.
strModification
= QString(
"[%1]"
).arg(modif.
strModification
);
387
}
388
else
389
{
390
modif.
strModification
= QString(
"[+%1]"
).arg(modif.
strModification
);
391
}
392
393
qDebug() << modif.
strModification
;
394
list.push_back(modif);
395
qDebug() << modif.
strModification
;
396
}
397
qDebug() << list.size();
398
}
399
400
qDebug() << list.size();
401
402
return
list;
403
}
404
405
QString
406
pappso::cbor::psm::SageReader::getDecoyTag
()
const
407
{
408
QString path;
409
QJsonObject sage_object =
m_sageFileReader
.getJsonDocument().object();
410
QJsonValue database = sage_object.value(
"database"
);
411
if
(database.isUndefined())
412
{
413
throw
pappso::ExceptionNotFound
(QObject::tr(
"database not found in Sage json document"
));
414
}
415
path = database.toObject().value(
"decoy_tag"
).toString();
416
if
(path.isEmpty())
417
{
418
throw
pappso::ExceptionNotFound
(QObject::tr(
"decoy_tag value is empty"
));
419
}
420
return
path;
421
}
422
423
const
pappso::cbor::psm::SageFileReader
&
424
pappso::cbor::psm::SageReader::getSageFileReader
()
const
425
{
426
return
m_sageFileReader
;
427
}
pappso::AaModification::getAccession
const QString & getAccession() const
Definition
aamodification.cpp:94
pappso::AaModification::getMass
pappso_double getMass() const
Definition
aamodification.cpp:762
pappso::ExceptionNotFound
Definition
exceptionnotfound.h:32
pappso::FastaReader
Definition
fastareader.h:34
pappso::FastaReader::parse
void parse(QFile &fastaFile)
Definition
fastareader.cpp:39
pappso::PappsoException
Definition
pappsoexception.h:41
pappso::UiMonitorInterface
Definition
uimonitorinterface.h:50
pappso::Utils::isAminoAcidChar
static bool isAminoAcidChar(Enums::AminoAcidChar x)
Definition
utils.cpp:822
pappso::Utils::guessAaModificationPbyMonoisotopicMassDelta
static AaModificationP guessAaModificationPbyMonoisotopicMassDelta(Enums::AminoAcidChar aa, pappso_double mass, QChar position=' ')
get the most suitable modification given an amino acid, a mass and a position
Definition
utils.cpp:678
pappso::cbor::CborStreamWriter
overrides QCborStreamWriter base class to provide convenient functions
Definition
cborstreamwriter.h:46
pappso::cbor::psm::SageFileReader
Definition
sagefilereader.h:48
pappso::cbor::psm::SageReader::FastaSeq
Definition
sagereader.h:103
pappso::cbor::psm::SageReader::FastaSeq::mp_self
SageReader * mp_self
Definition
sagereader.h:109
pappso::cbor::psm::SageReader::FastaSeq::setSequence
void setSequence(const QString &description_in, const QString &sequence_in) override
Definition
sagereader.cpp:82
pappso::cbor::psm::SageReader::FastaSeq::m_decoyTag
QString m_decoyTag
Definition
sagereader.h:110
pappso::cbor::psm::SageReader::FastaSeq::FastaSeq
FastaSeq(SageReader *self)
Definition
sagereader.cpp:75
pappso::cbor::psm::SageReader::m_psmProteinMap
PsmProteinMap m_psmProteinMap
Definition
sagereader.h:117
pappso::cbor::psm::SageReader::getMzmlPath
const QString & getMzmlPath(const QString &file_msrun) const
Definition
sagereader.cpp:192
pappso::cbor::psm::SageReader::SageReader
SageReader(pappso::UiMonitorInterface *p_monitor, pappso::cbor::CborStreamWriter *p_output, const SageFileReader &sage_file_reader, const QString &sage_json_file)
Definition
sagereader.cpp:47
pappso::cbor::psm::SageReader::getStaticModificationList
std::vector< SageModification > getStaticModificationList() const
Definition
sagereader.cpp:281
pappso::cbor::psm::SageReader::readTsvFile
void readTsvFile()
Definition
sagereader.cpp:122
pappso::cbor::psm::SageReader::m_mzmlPathList
QStringList m_mzmlPathList
Definition
sagereader.h:119
pappso::cbor::psm::SageReader::mp_cborWriter
pappso::cbor::CborStreamWriter * mp_cborWriter
Definition
sagereader.h:116
pappso::cbor::psm::SageReader::getmJsonAbsoluteFilePath
const QString & getmJsonAbsoluteFilePath() const
Definition
sagereader.cpp:63
pappso::cbor::psm::SageReader::extractMzmlPathList
void extractMzmlPathList(const QJsonDocument &json_doc)
Definition
sagereader.cpp:174
pappso::cbor::psm::SageReader::~SageReader
virtual ~SageReader()
Definition
sagereader.cpp:58
pappso::cbor::psm::SageReader::getCborStreamWriter
pappso::cbor::CborStreamWriter & getCborStreamWriter() const
Definition
sagereader.cpp:115
pappso::cbor::psm::SageReader::m_jsonAbsoluteFilePath
QString m_jsonAbsoluteFilePath
Definition
sagereader.h:118
pappso::cbor::psm::SageReader::m_sageFileReader
const SageFileReader & m_sageFileReader
Definition
sagereader.h:114
pappso::cbor::psm::SageReader::getVariableModificationList
std::vector< SageModification > getVariableModificationList() const
Definition
sagereader.cpp:322
pappso::cbor::psm::SageReader::getFastaFilePath
QString getFastaFilePath(const QJsonDocument &json_doc)
Definition
sagereader.cpp:234
pappso::cbor::psm::SageReader::getDecoyTag
QString getDecoyTag() const
Definition
sagereader.cpp:406
pappso::cbor::psm::SageReader::getSageFileReader
const SageFileReader & getSageFileReader() const
Definition
sagereader.cpp:424
pappso::cbor::psm::SageReader::getTsvFilePath
QString getTsvFilePath(const QJsonDocument &json_doc)
Definition
sagereader.cpp:205
pappso::cbor::psm::SageReader::mp_monitor
pappso::UiMonitorInterface * mp_monitor
Definition
sagereader.h:115
pappso::cbor::psm::SageReader::read
void read()
Definition
sagereader.cpp:70
pappso::cbor::psm::SageReader::convertToLocalFileOrDie
QString convertToLocalFileOrDie(const QString &file_str) const
Definition
sagereader.cpp:254
pappso::cbor::psm::SageTsvHandler
Definition
sagetsvhandler.h:50
pappso::cbor::psm::SageTsvHandler::writeSampleList
void writeSampleList()
Definition
sagetsvhandler.cpp:66
exceptionnotfound.h
fastareader.h
pappso::Enums::AminoAcidChar
AminoAcidChar
Definition
types.h:188
pappsoexception.h
sagereader.h
sagetsvhandler.h
pappso::cbor::psm::PsmProtein
Definition
psmproteinmap.h:46
pappso::cbor::psm::PsmProtein::protein_sp
std::shared_ptr< Protein > protein_sp
Definition
psmproteinmap.h:47
pappso::cbor::psm::SageReader::SageModification
Definition
sagereader.h:61
pappso::cbor::psm::SageReader::SageModification::position
QChar position
Definition
sagereader.h:77
pappso::cbor::psm::SageReader::SageModification::strModification
QString strModification
Definition
sagereader.h:79
pappso::cbor::psm::SageReader::SageModification::modification
pappso::AaModificationP modification
Definition
sagereader.h:76
pappso::cbor::psm::SageReader::SageModification::residue
Enums::AminoAcidChar residue
Definition
sagereader.h:78
types.h
utils.h
pappsomspp
core
processing
cbor
psm
sage
sagereader.cpp
Generated on
for libpappsomspp by
1.17.0