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 <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"
43#include "../../../../fasta/fastareader.h"
44
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
59
60const QString &
62{
63 return m_jsonAbsoluteFilePath;
64}
65
66
67void
69{
70 readTsvFile();
71}
72
74{
75 mp_self = self;
76 m_decoyTag = mp_self->getDecoyTag();
77}
78
79void
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 }
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
114{
115 return *mp_cborWriter;
116}
117
118
119void
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
171void
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
189const QString &
190pappso::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 }
198 QObject::tr("MS run %1 not found in Sage json document").append(file_msrun));
199}
200
201
202QString
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
231QString
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
251QString
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 {
271 QObject::tr("Unable to load data from remote URL %1").arg(tsv_url.toString()));
272 }
273 }
274 }
275 return path;
276}
277
278std::vector<pappso::cbor::psm::SageReader::SageModification>
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);
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
320std::vector<pappso::cbor::psm::SageReader::SageModification>
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 {
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
363QString
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
383{
384 return m_sageFileReader;
385}
pappso_double getMass() const
void parse(QFile &fastaFile)
static AaModificationP guessAaModificationPbyMonoisotopicMassDelta(Enums::AminoAcidChar aa, pappso_double mass)
Definition utils.cpp:677
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:100
const QString & getmJsonAbsoluteFilePath() const
void extractMzmlPathList(const QJsonDocument &json_doc)
pappso::cbor::CborStreamWriter & getCborStreamWriter() const
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:99
QString convertToLocalFileOrDie(const QString &file_str) const
std::shared_ptr< Protein > protein_sp