libpappsomspp
Library for mass spectrometry
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>
41#include "sagetsvhandler.h"
46
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
61
62const QString &
67
68
69void
74
80
81void
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 }
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
119
120
121void
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);
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");
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
173void
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
191const QString &
192pappso::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 }
200 QObject::tr("MS run %1 not found in Sage json document").append(file_msrun));
201}
202
203
204QString
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
233QString
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
253QString
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 {
273 QObject::tr("Unable to load data from remote URL %1").arg(tsv_url.toString()));
274 }
275 }
276 }
277 return path;
278}
279
280std::vector<pappso::cbor::psm::SageReader::SageModification>
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();
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
321std::vector<pappso::cbor::psm::SageReader::SageModification>
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();
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;
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 {
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
405QString
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
const QString & getAccession() const
pappso_double getMass() const
void parse(QFile &fastaFile)
static bool isAminoAcidChar(Enums::AminoAcidChar x)
Definition utils.cpp:822
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
overrides QCborStreamWriter base class to provide convenient functions
void setSequence(const QString &description_in, const QString &sequence_in) override
const QString & getMzmlPath(const QString &file_msrun) const
SageReader(pappso::UiMonitorInterface *p_monitor, pappso::cbor::CborStreamWriter *p_output, const SageFileReader &sage_file_reader, const QString &sage_json_file)
std::vector< SageModification > getStaticModificationList() const
pappso::cbor::CborStreamWriter * mp_cborWriter
Definition sagereader.h:116
const QString & getmJsonAbsoluteFilePath() const
void extractMzmlPathList(const QJsonDocument &json_doc)
pappso::cbor::CborStreamWriter & getCborStreamWriter() const
const SageFileReader & m_sageFileReader
Definition sagereader.h:114
std::vector< SageModification > getVariableModificationList() const
QString getFastaFilePath(const QJsonDocument &json_doc)
const SageFileReader & getSageFileReader() const
QString getTsvFilePath(const QJsonDocument &json_doc)
pappso::UiMonitorInterface * mp_monitor
Definition sagereader.h:115
QString convertToLocalFileOrDie(const QString &file_str) const
std::shared_ptr< Protein > protein_sp