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 <odsstream/tsvreader.h>
33
#include <odsstream/odsexception.h>
34
#include <QUrl>
35
#include <qcontainerfwd.h>
36
#include <qfileinfo.h>
37
#include <qlogging.h>
38
#include <qobject.h>
39
#include "
sagetsvhandler.h
"
40
#include "
pappsomspp/core/utils.h
"
41
#include "
pappsomspp/core/exception/exceptionnotfound.h
"
42
#include "
pappsomspp/core/pappsoexception.h
"
43
#include "
../../../../fasta/fastareader.h
"
44
45
pappso::cbor::psm::SageReader::SageReader
(
pappso::UiMonitorInterface
*p_monitor,
46
pappso::cbor::CborStreamWriter
*p_output,
47
const
pappso::cbor::psm::SageFileReader
&sage_file_reader,
48
const
QString &sage_json_file)
49
:
m_sageFileReader
(sage_file_reader)
50
{
51
mp_monitor
= p_monitor;
52
mp_cborWriter
= p_output;
53
m_jsonAbsoluteFilePath
= sage_json_file;
54
}
55
56
pappso::cbor::psm::SageReader::~SageReader
()
57
{
58
}
59
60
const
QString &
61
pappso::cbor::psm::SageReader::getmJsonAbsoluteFilePath
()
const
62
{
63
return
m_jsonAbsoluteFilePath
;
64
}
65
66
67
void
68
pappso::cbor::psm::SageReader::read
()
69
{
70
readTsvFile
();
71
}
72
73
pappso::cbor::psm::SageReader::FastaSeq::FastaSeq
(
SageReader
*self)
74
{
75
mp_self
= self;
76
m_decoyTag
=
mp_self
->getDecoyTag();
77
}
78
79
void
80
pappso::cbor::psm::SageReader::FastaSeq::setSequence
(
const
QString &description_in,
81
const
QString &sequence_in)
82
{
83
84
QStringList description_split = description_in.split(
" "
, Qt::SkipEmptyParts);
85
QString accession = description_split.at(0);
86
if
(description_split.length() > 0)
87
description_split.remove(0);
88
try
89
{
90
const
PsmProtein
&psm_protein =
mp_self
->m_psmProteinMap.getByAccession(accession);
91
psm_protein.
protein_sp
.get()->setSequence(sequence_in);
92
psm_protein.
protein_sp
.get()->setDescription(description_split.join(
" "
));
93
}
94
catch
(
pappso::ExceptionNotFound
&err)
95
{
96
}
97
try
98
{
99
QString rev_accession = accession.prepend(
m_decoyTag
);
100
const
PsmProtein
&psm_protein =
mp_self
->m_psmProteinMap.getByAccession(rev_accession);
101
psm_protein.
protein_sp
.get()->setSequence(sequence_in);
102
103
// description_split[0] = rev_accession;
104
psm_protein.
protein_sp
.get()->setDescription(description_split.join(
" "
));
105
psm_protein.
protein_sp
.get()->reverse();
106
}
107
catch
(
pappso::ExceptionNotFound
&err)
108
{
109
}
110
}
111
112
pappso::cbor::CborStreamWriter
&
113
pappso::cbor::psm::SageReader::getCborStreamWriter
()
const
114
{
115
return
*
mp_cborWriter
;
116
}
117
118
119
void
120
pappso::cbor::psm::SageReader::readTsvFile
()
121
{
122
123
extractMzmlPathList
(
m_sageFileReader
.getJsonDocument());
124
// getTsvFilePath(mp_identificationDataSource->getJsonDocument().object());
125
QString file_str =
getTsvFilePath
(
m_sageFileReader
.getJsonDocument());
126
QFileInfo tsv_file_info(file_str);
127
SageTsvHandler
handler(
mp_monitor
, *
this
,
m_psmProteinMap
);
128
try
129
{
130
TsvReader tsv_reader(handler);
131
132
QFile tsv_file(tsv_file_info.absoluteFilePath());
133
tsv_reader.parse(tsv_file);
134
tsv_file.close();
135
}
136
catch
(OdsException &error_ods)
137
{
138
throw
pappso::PappsoException
(QObject::tr(
"Error reading %1 file:\n %2"
)
139
.arg(tsv_file_info.absoluteFilePath())
140
.arg(error_ods.qwhat()));
141
}
142
143
144
// collect protein sequences
145
QFile fastaFile(
getFastaFilePath
(
m_sageFileReader
.getJsonDocument()));
146
SageReader::FastaSeq
seq(
this
);
147
pappso::FastaReader
reader(seq);
148
reader.
parse
(fastaFile);
149
150
qDebug();
151
mp_cborWriter
->append(
"protein_map"
);
152
m_psmProteinMap
.writeMap(*
mp_cborWriter
);
153
154
155
mp_cborWriter
->append(
"sample_list"
);
156
mp_cborWriter
->startArray();
157
try
158
{
159
handler.
writeSampleList
();
160
}
161
catch
(OdsException &error_ods)
162
{
163
throw
pappso::PappsoException
(QObject::tr(
"Error reading %1 file:\n %2"
)
164
.arg(tsv_file_info.absoluteFilePath())
165
.arg(error_ods.qwhat()));
166
}
167
168
mp_cborWriter
->endArray();
169
}
170
171
void
172
pappso::cbor::psm::SageReader::extractMzmlPathList
(
const
QJsonDocument &json_doc)
173
{
174
175
QJsonObject sage_object = json_doc.object();
176
QJsonValue json_mzml_path_list = sage_object.value(
"mzml_paths"
);
177
if
(json_mzml_path_list.isUndefined())
178
{
179
throw
pappso::ExceptionNotFound
(QObject::tr(
"mzml_paths not found in Sage json document"
));
180
}
181
m_mzmlPathList
.clear();
182
183
for
(
auto
path_mzml : json_mzml_path_list.toArray())
184
{
185
m_mzmlPathList
<<
convertToLocalFileOrDie
(path_mzml.toString());
186
}
187
}
188
189
const
QString &
190
pappso::cbor::psm::SageReader::getMzmlPath
(
const
QString &file_msrun)
const
191
{
192
for
(
auto
&file_path :
m_mzmlPathList
)
193
{
194
if
(file_path.endsWith(file_msrun))
195
return
file_path;
196
}
197
throw
pappso::ExceptionNotFound
(
198
QObject::tr(
"MS run %1 not found in Sage json document"
).append(file_msrun));
199
}
200
201
202
QString
203
pappso::cbor::psm::SageReader::getTsvFilePath
(
const
QJsonDocument &json_doc)
204
{
205
QString path;
206
QJsonObject sage_object = json_doc.object();
207
QJsonValue output_path = sage_object.value(
"output_paths"
);
208
if
(output_path.isUndefined())
209
{
210
throw
pappso::ExceptionNotFound
(QObject::tr(
"output_paths not found in Sage json document"
));
211
}
212
213
if
(!output_path.isArray())
214
{
215
throw
pappso::ExceptionNotFound
(QObject::tr(
"output_paths is not an array"
));
216
}
217
for
(
auto
element : output_path.toArray())
218
{
219
if
(element.isString())
220
{
221
if
(element.toString().endsWith(
".tsv"
))
222
{
223
path = element.toString();
224
}
225
}
226
}
227
228
return
convertToLocalFileOrDie
(path);
229
}
230
231
QString
232
pappso::cbor::psm::SageReader::getFastaFilePath
(
const
QJsonDocument &json_doc)
233
{
234
QString path;
235
QJsonObject sage_object = json_doc.object();
236
QJsonValue database = sage_object.value(
"database"
);
237
if
(database.isUndefined())
238
{
239
throw
pappso::ExceptionNotFound
(QObject::tr(
"database not found in Sage json document"
));
240
}
241
path = database.toObject().value(
"fasta"
).toString();
242
if
(path.isEmpty())
243
{
244
throw
pappso::ExceptionNotFound
(QObject::tr(
"fasta value is empty"
));
245
}
246
247
248
return
convertToLocalFileOrDie
(path);
249
}
250
251
QString
252
pappso::cbor::psm::SageReader::convertToLocalFileOrDie
(
const
QString &path)
const
253
{
254
255
// if we have an URL : convert it to local file
256
qDebug() << path;
257
if
(path.startsWith(
"file:"
) || path.startsWith(
"http:"
) || path.startsWith(
"https:"
))
258
{
259
QUrl tsv_url(path);
260
if
(tsv_url.isValid())
261
{
262
qDebug() <<
"tsv_url.isValid()"
;
263
if
(tsv_url.isLocalFile())
264
{
265
qDebug() <<
"tsv_url.isLocalFile()"
;
266
return
tsv_url.toLocalFile();
267
}
268
else
269
{
270
throw
pappso::PappsoException
(
271
QObject::tr(
"Unable to load data from remote URL %1"
).arg(tsv_url.toString()));
272
}
273
}
274
}
275
return
path;
276
}
277
278
std::vector<pappso::cbor::psm::SageReader::SageModification>
279
pappso::cbor::psm::SageReader::getStaticModificationList
()
const
280
{
281
std::vector<SageReader::SageModification> list;
282
QJsonObject sage_object =
m_sageFileReader
.getJsonDocument().object();
283
QJsonValue database = sage_object.value(
"database"
);
284
if
(database.isUndefined())
285
{
286
throw
pappso::ExceptionNotFound
(QObject::tr(
"database not found in Sage json document"
));
287
}
288
289
QJsonValue static_mods = database.toObject().value(
"static_mods"
);
290
if
(static_mods.isUndefined())
291
{
292
throw
pappso::ExceptionNotFound
(QObject::tr(
"static_mods not found in Sage json document"
));
293
}
294
for
(QString residue_str : static_mods.toObject().keys())
295
{
296
SageModification
modif;
297
modif.
residue
= residue_str.at(0);
298
modif.
modification
=
pappso::Utils::guessAaModificationPbyMonoisotopicMassDelta
(
299
(
Enums::AminoAcidChar
)modif.
residue
.toLatin1(),
300
static_mods.toObject().value(residue_str).toDouble());
301
modif.
strModification
=
302
QString::number(static_mods.toObject().value(residue_str).toDouble(),
'f'
, 6);
303
if
(modif.
strModification
.isEmpty())
304
{
305
throw
pappso::PappsoException
(QObject::tr(
" modif.strModification is empty"
));
306
}
307
if
(modif.
modification
->
getMass
() < 0)
308
{
309
modif.
strModification
= QString(
"[%1]"
).arg(modif.
strModification
);
310
}
311
else
312
{
313
modif.
strModification
= QString(
"[+%1]"
).arg(modif.
strModification
);
314
}
315
list.push_back(modif);
316
}
317
return
list;
318
}
319
320
std::vector<pappso::cbor::psm::SageReader::SageModification>
321
pappso::cbor::psm::SageReader::getVariableModificationList
()
const
322
{
323
std::vector<SageReader::SageModification> list;
324
QJsonObject sage_object =
m_sageFileReader
.getJsonDocument().object();
325
QJsonValue database = sage_object.value(
"database"
);
326
if
(database.isUndefined())
327
{
328
throw
pappso::ExceptionNotFound
(QObject::tr(
"database not found in Sage json document"
));
329
}
330
331
QJsonValue var_mods = database.toObject().value(
"variable_mods"
);
332
if
(var_mods.isUndefined())
333
{
334
throw
pappso::ExceptionNotFound
(QObject::tr(
"static_mods not found in Sage json document"
));
335
}
336
for
(QString residue_str : var_mods.toObject().keys())
337
{
338
SageModification
modif;
339
modif.
residue
= residue_str.at(0);
340
for
(QJsonValue one_mass : var_mods.toObject().value(residue_str).toArray())
341
{
342
modif.
modification
=
pappso::Utils::guessAaModificationPbyMonoisotopicMassDelta
(
343
(
Enums::AminoAcidChar
)modif.
residue
.toLatin1(), one_mass.toDouble());
344
modif.
strModification
= QString::number(one_mass.toDouble(),
'f'
, 6);
345
if
(modif.
strModification
.isEmpty())
346
{
347
throw
pappso::PappsoException
(QObject::tr(
" modif.strModification is empty"
));
348
}
349
if
(modif.
modification
->
getMass
() < 0)
350
{
351
modif.
strModification
= QString(
"[%1]"
).arg(modif.
strModification
);
352
}
353
else
354
{
355
modif.
strModification
= QString(
"[+%1]"
).arg(modif.
strModification
);
356
}
357
list.push_back(modif);
358
}
359
}
360
return
list;
361
}
362
363
QString
364
pappso::cbor::psm::SageReader::getDecoyTag
()
const
365
{
366
QString path;
367
QJsonObject sage_object =
m_sageFileReader
.getJsonDocument().object();
368
QJsonValue database = sage_object.value(
"database"
);
369
if
(database.isUndefined())
370
{
371
throw
pappso::ExceptionNotFound
(QObject::tr(
"database not found in Sage json document"
));
372
}
373
path = database.toObject().value(
"decoy_tag"
).toString();
374
if
(path.isEmpty())
375
{
376
throw
pappso::ExceptionNotFound
(QObject::tr(
"decoy_tag value is empty"
));
377
}
378
return
path;
379
}
380
381
const
pappso::cbor::psm::SageFileReader
&
382
pappso::cbor::psm::SageReader::getSageFileReader
()
const
383
{
384
return
m_sageFileReader
;
385
}
pappso::AaModification::getMass
pappso_double getMass() const
Definition
aamodification.cpp:759
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::guessAaModificationPbyMonoisotopicMassDelta
static AaModificationP guessAaModificationPbyMonoisotopicMassDelta(Enums::AminoAcidChar aa, pappso_double mass)
Definition
utils.cpp:677
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:87
pappso::cbor::psm::SageReader::FastaSeq::mp_self
SageReader * mp_self
Definition
sagereader.h:93
pappso::cbor::psm::SageReader::FastaSeq::setSequence
void setSequence(const QString &description_in, const QString &sequence_in) override
Definition
sagereader.cpp:80
pappso::cbor::psm::SageReader::FastaSeq::m_decoyTag
QString m_decoyTag
Definition
sagereader.h:94
pappso::cbor::psm::SageReader::FastaSeq::FastaSeq
FastaSeq(SageReader *self)
Definition
sagereader.cpp:73
pappso::cbor::psm::SageReader::m_psmProteinMap
PsmProteinMap m_psmProteinMap
Definition
sagereader.h:101
pappso::cbor::psm::SageReader::getMzmlPath
const QString & getMzmlPath(const QString &file_msrun) const
Definition
sagereader.cpp:190
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:45
pappso::cbor::psm::SageReader::getStaticModificationList
std::vector< SageModification > getStaticModificationList() const
Definition
sagereader.cpp:279
pappso::cbor::psm::SageReader::readTsvFile
void readTsvFile()
Definition
sagereader.cpp:120
pappso::cbor::psm::SageReader::m_mzmlPathList
QStringList m_mzmlPathList
Definition
sagereader.h:103
pappso::cbor::psm::SageReader::mp_cborWriter
pappso::cbor::CborStreamWriter * mp_cborWriter
Definition
sagereader.h:100
pappso::cbor::psm::SageReader::getmJsonAbsoluteFilePath
const QString & getmJsonAbsoluteFilePath() const
Definition
sagereader.cpp:61
pappso::cbor::psm::SageReader::extractMzmlPathList
void extractMzmlPathList(const QJsonDocument &json_doc)
Definition
sagereader.cpp:172
pappso::cbor::psm::SageReader::~SageReader
virtual ~SageReader()
Definition
sagereader.cpp:56
pappso::cbor::psm::SageReader::getCborStreamWriter
pappso::cbor::CborStreamWriter & getCborStreamWriter() const
Definition
sagereader.cpp:113
pappso::cbor::psm::SageReader::m_jsonAbsoluteFilePath
QString m_jsonAbsoluteFilePath
Definition
sagereader.h:102
pappso::cbor::psm::SageReader::m_sageFileReader
const SageFileReader & m_sageFileReader
Definition
sagereader.h:98
pappso::cbor::psm::SageReader::getVariableModificationList
std::vector< SageModification > getVariableModificationList() const
Definition
sagereader.cpp:321
pappso::cbor::psm::SageReader::getFastaFilePath
QString getFastaFilePath(const QJsonDocument &json_doc)
Definition
sagereader.cpp:232
pappso::cbor::psm::SageReader::getDecoyTag
QString getDecoyTag() const
Definition
sagereader.cpp:364
pappso::cbor::psm::SageReader::getSageFileReader
const SageFileReader & getSageFileReader() const
Definition
sagereader.cpp:382
pappso::cbor::psm::SageReader::getTsvFilePath
QString getTsvFilePath(const QJsonDocument &json_doc)
Definition
sagereader.cpp:203
pappso::cbor::psm::SageReader::mp_monitor
pappso::UiMonitorInterface * mp_monitor
Definition
sagereader.h:99
pappso::cbor::psm::SageReader::read
void read()
Definition
sagereader.cpp:68
pappso::cbor::psm::SageReader::convertToLocalFileOrDie
QString convertToLocalFileOrDie(const QString &file_str) const
Definition
sagereader.cpp:252
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:187
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:60
pappso::cbor::psm::SageReader::SageModification::residue
QChar residue
Definition
sagereader.h:62
pappso::cbor::psm::SageReader::SageModification::strModification
QString strModification
Definition
sagereader.h:63
pappso::cbor::psm::SageReader::SageModification::modification
pappso::AaModificationP modification
Definition
sagereader.h:61
utils.h
pappsomspp
core
processing
cbor
psm
sage
sagereader.cpp
Generated on
for libpappsomspp by
1.17.0