From 496505bed58ca7ecb04ca1d7c87add9fec2d8fb4 Mon Sep 17 00:00:00 2001
From: Olivier Langella <Olivier.Langella@moulon.inra.fr>
Date: Fri, 19 May 2017 23:35:49 +0200
Subject: [PATCH] select contaminant database

---
 src/files/fastafile.cpp                | 35 +++++++++++++++++++++++++-
 src/files/fastafile.h                  |  5 ++++
 src/gui/project_view/projectwindow.cpp | 16 ++++++++++--
 src/input/xpipsaxhandler.cpp           |  1 +
 src/utils/fastafilestore.cpp           |  4 +--
 src/utils/proteinstore.cpp             |  9 +++++++
 src/utils/proteinstore.h               |  4 +++
 7 files changed, 69 insertions(+), 5 deletions(-)

diff --git a/src/files/fastafile.cpp b/src/files/fastafile.cpp
index 2ac6b0da..688eb2c3 100644
--- a/src/files/fastafile.cpp
+++ b/src/files/fastafile.cpp
@@ -22,10 +22,29 @@
 ******************************************************************************/
 
 #include "fastafile.h"
+#include <pappsomspp/fasta/fastareader.h>
+#include <QStringList>
+#include <QDebug>
+
+class AccessionContaminantReader: public pappso::FastaHandlerInterface {
+public:
+    AccessionContaminantReader (ProteinStore & protein_store) :_protein_store(protein_store) {
+    };
+
+    void setSequence(const QString& description, const QString& sequence) override {
+        //qDebug() << "PeptideReader::setSequence " << description << " " <<sequence;
+        QStringList descr_split = description.simplified().split(" ");
+        QString accession = descr_split.at(0);
+        _protein_store.setContaminantAccession(accession);
+    };
+private:
+    ProteinStore & _protein_store;
+};
+
 
 FastaFile::FastaFile(const QString & fasta_source) : _fasta_source(fasta_source)
 {
-
+    qDebug() << "FastaFile::FastaFile "<< fasta_source;
 }
 FastaFile::FastaFile(const QFileInfo & fasta_source): _fasta_source(fasta_source)
 {
@@ -46,3 +65,17 @@ const QString FastaFile::getFilename() const {
 const QString FastaFile::getAbsoluteFilePath() const {
     return _fasta_source.absoluteFilePath();
 }
+void FastaFile::setContaminants(ProteinStore & protein_store) const {
+
+    if (_fasta_source.exists()) {
+        AccessionContaminantReader accession_reader(protein_store);
+        pappso::FastaReader reader(accession_reader);
+        QFile fasta_file(_fasta_source.absoluteFilePath());
+        fasta_file.open(QIODevice::ReadOnly);
+        reader.parse(&fasta_file);
+        fasta_file.close();
+    }
+    else {
+        qDebug() << "FastaFile::setContaminants "<< _fasta_source.absoluteFilePath() << " does not exists";
+    }
+}
diff --git a/src/files/fastafile.h b/src/files/fastafile.h
index 1ba90827..1c424741 100644
--- a/src/files/fastafile.h
+++ b/src/files/fastafile.h
@@ -26,6 +26,7 @@
 
 #include <QFileInfo>
 #include <memory>
+#include "../utils/proteinstore.h"
 
 class FastaFile;
 typedef std::shared_ptr<FastaFile> FastaFileSp;
@@ -41,6 +42,10 @@ public:
     
     const QString getFilename() const;
     const QString getAbsoluteFilePath() const;
+    
+    /** @brief read fasta file and set accessions as contaminants
+     * */
+    void setContaminants(ProteinStore & protein_store) const;
 
 private :
     const QFileInfo _fasta_source;
diff --git a/src/gui/project_view/projectwindow.cpp b/src/gui/project_view/projectwindow.cpp
index 1b0dd5cd..cb8ade14 100644
--- a/src/gui/project_view/projectwindow.cpp
+++ b/src/gui/project_view/projectwindow.cpp
@@ -51,6 +51,8 @@ ProjectWindow::ProjectWindow(MainWindow *parent):
     _p_fasta_str_li = new QStandardItemModel();
     ui->decoy_database_listview->setModel(_p_fasta_str_li);
     ui->contaminant_database_listview->setModel(_p_fasta_str_li);
+    QItemSelectionModel * selection_model = ui->contaminant_database_listview->selectionModel();
+    ui->contaminant_database_listview->setSelectionMode(QAbstractItemView::MultiSelection);
 
     _p_automatic_filter_widget = new AutomaticFilterWidget(this);
     ui->filter_parameter_layout->addWidget(_p_automatic_filter_widget);
@@ -236,7 +238,17 @@ void ProjectWindow::doAutomaticFilterParametersChanged(AutomaticFilterParameters
     showWaitingMessage(tr("Updating filters"));
 
     doDisplayLoadingMessage(tr("tagging contaminant proteins"));
-    _project_sp.get()->getProteinStore().setRegexpContaminantPattern(ui->contaminant_protein_regexp_line_edit->text());
+    QModelIndexList index_list = ui->contaminant_database_listview->selectionModel()->selectedIndexes();
+    if (index_list.size() > 0) {
+        _project_sp.get()->getProteinStore().clearContaminants();
+        for (QModelIndex index :index_list) {
+            FastaFile fasta_file (_p_fasta_str_li->itemFromIndex(index)->data(Qt::UserRole).toString());
+            fasta_file.setContaminants(_project_sp.get()->getProteinStore());
+        }
+    }
+    else {
+        _project_sp.get()->getProteinStore().setRegexpContaminantPattern(ui->contaminant_protein_regexp_line_edit->text());
+    }
     doDisplayLoadingMessage(tr("updating filters"));
     _project_sp.get()->updateAutomaticFilters(parameters);
 
@@ -380,7 +392,7 @@ void ProjectWindow::setProjectSp(ProjectSp project_sp) {
         item = new QStandardItem(QString("%1").arg(fasta_file.get()->getFilename()));
         item->setEditable(false);
         _p_fasta_str_li->appendRow(item);
-        item->setData(QVariant(QString("%1").arg(fasta_file.get()->getFilename())),Qt::UserRole);
+        item->setData(QVariant(QString("%1").arg(fasta_file.get()->getAbsoluteFilePath())),Qt::UserRole);
     }
 
 
diff --git a/src/input/xpipsaxhandler.cpp b/src/input/xpipsaxhandler.cpp
index 739fc8ac..f6d4affe 100644
--- a/src/input/xpipsaxhandler.cpp
+++ b/src/input/xpipsaxhandler.cpp
@@ -142,6 +142,7 @@ bool XpipSaxHandler::startElement_filter_params(QXmlAttributes attributes) {
     if (attributes.value("filter_to_all").simplified() == "true") {
         _automatic_filter_parameters.setFilterCrossSamplePeptideNumber(true);
     }
+    _p_project->getFastaFileStore().getInstance(FastaFile(attributes.value("database_filter")));
     qDebug() << "startElement_filter_params end" ;
     return true;
 }
diff --git a/src/utils/fastafilestore.cpp b/src/utils/fastafilestore.cpp
index ab5a58b4..ddafcd3c 100644
--- a/src/utils/fastafilestore.cpp
+++ b/src/utils/fastafilestore.cpp
@@ -46,9 +46,9 @@ qDebug() << "FastaFileStore::getInstance() begin "<< location.getAbsoluteFilePat
     std::vector<FastaFileSp>::iterator it = _map_fastafile.begin();
     std::vector<FastaFileSp>::iterator itend = _map_fastafile.end();
     while (it != itend) {
-        if (it->get()->getAbsoluteFilePath() == location.getAbsoluteFilePath()) {
+        if (it->get()->getFilename() == location.getFilename()) {
             
-qDebug() << "FastaFileStore::getInstance() end b "<< it->get()->getAbsoluteFilePath();
+qDebug() << "FastaFileStore::getInstance() end b "<< it->get()->getFilename();
             return *it;
         }
         it++;
diff --git a/src/utils/proteinstore.cpp b/src/utils/proteinstore.cpp
index 6cf29b59..4bf6eb4a 100644
--- a/src/utils/proteinstore.cpp
+++ b/src/utils/proteinstore.cpp
@@ -72,6 +72,15 @@ void ProteinStore::setRegexpDecoyPattern(const QString & pattern) {
 
 }
 
+void ProteinStore::clearContaminants() {
+    for  (std::pair<const QString, ProteinXtpSp> & acc_protein :_map_accession_protein_list) {
+        acc_protein.second.get()->setIsContaminant(false);
+    }
+}
+
+void ProteinStore::setContaminantAccession(QString accession) {
+    _map_accession_protein_list.at(accession).get()->setIsContaminant(true);
+}
 ProteinXtpSp & ProteinStore::getInstance(ProteinXtpSp & peptide_in) {
 
     std::pair<std::map< QString, ProteinXtpSp>::iterator,bool> ret = _map_accession_protein_list.insert(std::pair<QString, ProteinXtpSp>(peptide_in.get()->getAccession(),peptide_in));
diff --git a/src/utils/proteinstore.h b/src/utils/proteinstore.h
index 2208156c..c6632503 100644
--- a/src/utils/proteinstore.h
+++ b/src/utils/proteinstore.h
@@ -53,6 +53,10 @@ public:
     void setRegexpContaminantPattern(const QString & pattern);
     
     QRegExp getRegexpContaminant() const;
+    
+    void clearContaminants();
+    
+    void setContaminantAccession(QString accession);
 
 private :
     void setProteinInformations(ProteinXtpSp & protein_in);
-- 
GitLab