From af6d966bf18c0555786b5242d01b872923db2155 Mon Sep 17 00:00:00 2001
From: Olivier Langella <olivier.langella@u-psud.fr>
Date: Tue, 15 May 2018 22:03:01 +0200
Subject: [PATCH] back to spectral count in one single file

---
 src/gui/mainwindow.cpp           |  6 +-
 src/gui/workerthread.cpp         | 26 ++++++---
 src/output/mcqrspectralcount.cpp | 99 ++++++++++++++++++++++++++++----
 src/output/mcqrspectralcount.h   | 10 +++-
 4 files changed, 117 insertions(+), 24 deletions(-)

diff --git a/src/gui/mainwindow.cpp b/src/gui/mainwindow.cpp
index da0dca12f..232e0049d 100644
--- a/src/gui/mainwindow.cpp
+++ b/src/gui/mainwindow.cpp
@@ -357,9 +357,9 @@ void MainWindow::doActionSpectralCountingMcq()
     QSettings settings;
     QString default_location = settings.value("path/scmcqfile", "").toString();
 
-    QString filename = QFileDialog::getSaveFileName(this, tr("Save spectral count TSV files in a directory"),
-                       QString("%1/untitled.d").arg(default_location),
-                       tr("TSV (*.d)"));
+    QString filename = QFileDialog::getSaveFileName(this, tr("Save spectral count TSV file"),
+                       QString("%1/untitled.tsv").arg(default_location),
+                       tr("TSV (*.tsv)"));
 
     if (filename.isEmpty()) {
         return;
diff --git a/src/gui/workerthread.cpp b/src/gui/workerthread.cpp
index 19833b647..c09396e11 100644
--- a/src/gui/workerthread.cpp
+++ b/src/gui/workerthread.cpp
@@ -29,7 +29,8 @@
 
 #include "workerthread.h"
 #include <odsstream/odsdocwriter.h>
-#include <odsstream/tsvdirectorywriter.h>
+#include <odsstream/tsvoutputstream.h>
+#include <odsstream/odsexception.h>
 #include <pappsomspp/pappsoexception.h>
 #include <QDebug>
 #include "../output/masschroqml.h"
@@ -347,21 +348,30 @@ void WorkerThread::doWritingProticFile(QString filename, ProjectSp project_sp) {
 }
 
 
-void WorkerThread::doWritingMcqrSpectralCountFile(QString directory_name, ProjectSp project_sp) {
-
+void WorkerThread::doWritingMcqrSpectralCountFile(QString filename, ProjectSp project_sp) {
     try {
-        emit loadingMessage(tr("Writing spectral count files for MassChroqR in directory %1").arg(directory_name));
-        TsvDirectoryWriter * p_writer = new TsvDirectoryWriter(directory_name);
-        McqrSpectralCount spectra_sheet(project_sp.get());
-        spectra_sheet.write(p_writer, _p_work_monitor);
+        emit loadingMessage(tr("Writing %1 spectral count file for MassChroqR").arg(filename));
+        QFile outFile;
+        outFile.setFileName(filename);
+        outFile.open(QIODevice::WriteOnly);
+        QTextStream * p_outputStream = new QTextStream(&outFile);
+        TsvOutputStream * p_writer = new TsvOutputStream(*p_outputStream);
+        p_writer->setNoSheetName(true);
+        McqrSpectralCount spectra_sheet(p_writer, project_sp.get());
+        spectra_sheet.writeSheet();
 
         p_writer->close();
         delete p_writer;
+        delete p_outputStream;
+        outFile.close();
         emit operationFinished();
     }
-    catch (pappso::PappsoException & error) {
+    catch (OdsException & error) {
         emit operationFailed(tr("Error while writing spectral count file for MassChroqR :\n%1").arg(error.qwhat()));
     }
+    catch (pappso::PappsoException & errorb) {
+        emit operationFailed(tr("Error while writing spectral count file for MassChroqR :\n%1").arg(errorb.qwhat()));
+    }
 }
 
 
diff --git a/src/output/mcqrspectralcount.cpp b/src/output/mcqrspectralcount.cpp
index da69431bd..3e5b13774 100644
--- a/src/output/mcqrspectralcount.cpp
+++ b/src/output/mcqrspectralcount.cpp
@@ -1,5 +1,5 @@
 /**
- * \file /output/mcqrspectralcount.cpp
+ * \file /output/mcqrspectralcount.h
  * \date 23/3/2018
  * \author Olivier Langella
  * \brief write simple TSV file for MassChroqR spectral count studies
@@ -29,21 +29,98 @@
 
 #include "mcqrspectralcount.h"
 #include <pappsomspp/pappsoexception.h>
-#include <QSettings>
-#include "mcqr/mcqrscpeptide.h"
-#include "mcqr/mcqrscprotein.h"
 
 
-McqrSpectralCount::McqrSpectralCount (const Project * p_project): _p_project(p_project) {
+
+McqrSpectralCount::McqrSpectralCount (CalcWriterInterface * p_writer, const Project * p_project): _p_project(p_project) {
+    _p_writer = p_writer;
 }
 
+void McqrSpectralCount::writeSheet () {
+    _p_writer->writeSheet("MassChroqR spectral counting");
+
+    std::vector<IdentificationGroup *> identification_list = _p_project->getIdentificationGroupList();
+    if (identification_list.size() > 1) {
+        throw pappso::PappsoException(QObject::tr("error : cannot write MassChroqR spectral count file in individual mode"));
+    }
+
+    for (IdentificationGroup * p_ident:identification_list) {
+        //writeHeaders(p_ident);
+        writeIdentificationGroup(p_ident);
+    }
+}
+
+
+void McqrSpectralCount::writeHeaders(IdentificationGroup * p_ident)  {
+
+//ProteinID, msrun, msrunfile, accession, description, q
+    _p_writer->writeCell("protein");
+    _p_writer->writeCell("msrun");
+    _p_writer->writeCell("msrunfile");
+    _p_writer->writeCell("accession");
+    _p_writer->writeCell("description");
+    _p_writer->writeCell("sc");
+    //_p_writer->writeCell(p_protein_match->countSampleScan(state, p_msrun));
+
+}
+
+
+
+void McqrSpectralCount::writeIdentificationGroup(IdentificationGroup * p_ident) {
+
+    _ms_run_sp_list =  p_ident->getMsRunSpList();
+    writeHeaders(p_ident);
+    for (const std::pair<unsigned int, GroupingGroupSp> & group_pair : p_ident->getGroupStore().getGroupMap()) {
+
+        std::vector<const ProteinMatch *> protein_match_list = group_pair.second.get()->getProteinMatchList();
+
+        std::sort(protein_match_list.begin(), protein_match_list.end(),
+                  [](const ProteinMatch * a, const ProteinMatch * b)
+        {
+            return a->getGrpProteinSp().get()->getSubGroupNumber() < b->getGrpProteinSp().get()->getSubGroupNumber();
+        });
+
+        for (auto & protein_match:protein_match_list) {
+            writeOneProtein(group_pair.second.get(), protein_match);
+        }
+
+    }
+    _p_writer->writeLine();
+}
+
+
+void McqrSpectralCount::writeOneProtein(const GroupingGroup * p_group, const ProteinMatch * p_protein_match) {
+    try {
+
+        qDebug() << "McqrSpectralCount::writeOneProtein begin" ;
+        ValidationState validation_state = ValidationState::validAndChecked;
+
+        pappso::GrpProtein * p_grp_protein = p_protein_match->getGrpProteinSp().get();
+
+        ProteinXtp * p_protein = p_protein_match->getProteinXtpSp().get();
+
+        for (MsRunSp & msrun_sp : _ms_run_sp_list) {
+            _p_writer->writeLine();
+
+            _p_writer->writeCell(p_grp_protein->getGroupingId());
 
-void McqrSpectralCount::write(CalcWriterInterface * p_writer, WorkMonitorInterface * p_monitor) {
-    QSettings settings;
+            _p_writer->writeCell(msrun_sp.get()->getXmlId());
+            _p_writer->writeCell(msrun_sp.get()->getFilename());
 
-    p_monitor->message(QObject::tr("writing MassChroqR protein file"));
-    McqRscProtein( p_writer, _p_project).writeSheet();
-    p_monitor->message(QObject::tr("writing MassChroqR peptide file"));
-    McqRscPeptide( p_writer, _p_project).writeSheet();
+            //const std::list<DbXref> & dbxref_list = p_protein->getDbxrefList();
+            //if (dbxref_list.size() == 0) {
+            _p_writer->writeCell(p_protein->getAccession());
+            //}
+            //else {
+            //    _p_writer->writeCell(dbxref_list.front().getUrl(), p_protein->getAccession());
+            //}
+            _p_writer->writeCell(p_protein->getDescription());
+            _p_writer->writeCell(p_protein_match->countSampleScan(validation_state, msrun_sp.get()));
+        }
+        qDebug() << "McqrSpectralCount::writeOneProtein end" ;
+    }
+    catch (pappso::PappsoException error) {
+        throw pappso::PappsoException(QObject::tr("Error writing protein %1 :\n%2").arg(p_protein_match->getProteinXtpSp().get()->getAccession()).arg(error.qwhat()));
+    }
 }
 
diff --git a/src/output/mcqrspectralcount.h b/src/output/mcqrspectralcount.h
index ef1c882f1..1a3d6c3ed 100644
--- a/src/output/mcqrspectralcount.h
+++ b/src/output/mcqrspectralcount.h
@@ -36,12 +36,18 @@
 class McqrSpectralCount
 {
 public :
-    McqrSpectralCount (const Project * project);
+    McqrSpectralCount (CalcWriterInterface * p_writer, const Project * p_project);
     
-    void write(CalcWriterInterface * p_writer, WorkMonitorInterface * p_monitor);
+    void writeSheet();
+private :
+    void writeIdentificationGroup(IdentificationGroup * p_ident);
+    void writeHeaders(IdentificationGroup * p_ident);
+    void writeOneProtein(const GroupingGroup * p_group, const ProteinMatch * p_protein_match);
 
 private :
     const Project * _p_project;
+    CalcWriterInterface * _p_writer;
+    std::vector<MsRunSp> _ms_run_sp_list;
 };
 
 #endif // MCQRSPECTRALCOUNT_H
-- 
GitLab