Skip to content
Snippets Groups Projects
proteintablemodel.cpp 11.97 KiB

/*******************************************************************************
* Copyright (c) 2015 Olivier Langella <Olivier.Langella@moulon.inra.fr>.
*
* This file is part of XTPcpp.
*
*     XTPcpp is free software: you can redistribute it and/or modify
*     it under the terms of the GNU General Public License as published by
*     the Free Software Foundation, either version 3 of the License, or
*     (at your option) any later version.
*
*     XTPcpp is distributed in the hope that it will be useful,
*     but WITHOUT ANY WARRANTY; without even the implied warranty of
*     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*     GNU General Public License for more details.
*
*     You should have received a copy of the GNU General Public License
*     along with XTPcpp.  If not, see <http://www.gnu.org/licenses/>.
*
* Contributors:
*     Olivier Langella <Olivier.Langella@moulon.inra.fr> - initial API and implementation
******************************************************************************/

#include "proteintablemodel.h"
#include "proteinlistwindow.h"
#include <pappsomspp/grouping/grpprotein.h>
#include "../../grouping/groupinggroup.h"

#include <QDebug>


ProteinTableProxyModel::ProteinTableProxyModel(ProteinListWindow * p_protein_list_window, ProteinTableModel* protein_table_model_p): QSortFilterProxyModel(protein_table_model_p)
{
    _protein_table_model_p = protein_table_model_p;
    _p_protein_list_window = p_protein_list_window;
}
bool ProteinTableProxyModel::filterAcceptsRow(int source_row,
        const QModelIndex &source_parent) const {
    return _protein_table_model_p->acceptRow(source_row);
    //return true;
}

void ProteinTableProxyModel::onTableClicked(const QModelIndex &index)
{
    qDebug() << "ProteinTableProxyModel::onTableClicked begin " << index.row();
    qDebug() << "ProteinTableProxyModel::onTableClicked begin " << this->mapToSource(index).row();
    _protein_table_model_p->onTableClicked(this->mapToSource(index));
}
void ProteinTableProxyModel::onTableDoubleClicked(const QModelIndex &index)
{
    qDebug() << "ProteinTableProxyModel::onTableDoubleClicked begin " << index.row();
    qDebug() << "ProteinTableProxyModel::onTableDoubleClicked begin " << this->mapToSource(index).row();
    _protein_table_model_p->onTableDoubleClicked(this->mapToSource(index));
}
bool ProteinTableProxyModel::lessThan(const QModelIndex & left, const QModelIndex & right) const {
    QVariant leftData = sourceModel()->data(left);
    QVariant rightData = sourceModel()->data(right);
    if (leftData.type() == QVariant::UInt) {
        return leftData.toUInt() < rightData.toUInt();
    }
    if (leftData.type() == QVariant::UInt) {
        return leftData.toUInt() < rightData.toUInt();
    }
    if (leftData.type() == QVariant::Double) {
        return leftData.toDouble() < rightData.toDouble();
    }
    return leftData.toString() < rightData.toString();
    //if (leftData.type() == QVariant::DateTime) {
    //   return leftData.toDateTime() < rightData.toDateTime();
    //} else {
    // static QRegExp emailPattern("[\\w\\.]*@[\\w\\.]*)");
    /*
            QString leftString = leftData.toString();
            if(left.column() == 1 && emailPattern.indexIn(leftString) != -1)
                leftString = emailPattern.cap(1);

            QString rightString = rightData.toString();
            if(right.column() == 1 && emailPattern.indexIn(rightString) != -1)
                rightString = emailPattern.cap(1);

            return QString::localeAwareCompare(leftString, rightString) < 0;
            */
    // }
}

QVariant ProteinTableProxyModel::headerData(int section, Qt::Orientation orientation,
        int role) const {
    return sourceModel()->headerData(section, orientation,
                                     role);
}

ProteinTableModel::ProteinTableModel(ProteinListWindow * p_protein_list_window)
    :QAbstractTableModel(p_protein_list_window)
{
    _p_protein_list_window = p_protein_list_window;
    //ui->tableView->show();
    // QModelIndex topLeft = createIndex(0,0);
    //emit a signal to make the view reread identified data
    //emit dataChanged(topLeft, topLeft);
}

void ProteinTableModel::refresh() {
    qDebug() << "ProteinTableModel::refresh begin ";
    QModelIndex topLeft = createIndex(0,0);
    QModelIndex bottomRight = createIndex(rowCount(),columnCount());
    emit dataChanged(topLeft, bottomRight);
    qDebug() << "ProteinTableModel::refresh end ";
}

void ProteinTableModel::hideNotValid(bool hide) {
    _hide_not_valid = hide;
    refresh();
}

void ProteinTableModel::hideNotChecked(bool hide) {
    _hide_not_checked = hide;
    refresh();
}
void ProteinTableModel::hideNotGrouped(bool hide) {
    _hide_not_grouped = hide;
    refresh();
}
void ProteinTableModel::onProteinSearchEdit(QString protein_search_string) {
    qDebug() << "ProteinTableModel::onProteinSearchEdit begin " << protein_search_string;
    _protein_search_string = protein_search_string;
    QModelIndex topLeft = createIndex(0,0);
    QModelIndex bottomRight = createIndex(rowCount(),columnCount());
    emit dataChanged(topLeft, bottomRight);
    _p_protein_list_window->updateStatusBar();
}

void ProteinTableModel::setIdentificationGroup(IdentificationGroup * p_identification_group) {
    qDebug() << "ProteinTableModel::setIdentificationGroup begin " << p_identification_group->getProteinMatchList().size();
    _p_identification_group = p_identification_group;

    QModelIndex topLeft = createIndex(0,0);
    QModelIndex bottomRight = createIndex(rowCount(),columnCount());
    emit dataChanged(topLeft, topLeft);
    emit headerDataChanged(Qt::Horizontal, 0,3);
}

int ProteinTableModel::rowCount(const QModelIndex &parent ) const {
    if (_p_identification_group != nullptr) {
        qDebug() << "ProteinTableModel::rowCount(const QModelIndex &parent ) " << _p_identification_group->getProteinMatchList().size();
        return _p_identification_group->getProteinMatchList().size();
    }
    return 0;
}
int ProteinTableModel::columnCount(const QModelIndex &parent ) const {
    return 11;
}
QVariant ProteinTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (role == Qt::DisplayRole)
    {
        if (orientation == Qt::Horizontal) {
            switch (section)
            {
            case 0:
                return QString("checked");
            case 1:
                return QString("group");
            case 2:
                return QString("accession");
            case 3:
                return QString("protein description");
            case 4:
                return QString("Evalue");
            case 5:
                return QString("spectrum");
            case 6:
                return QString("specific");
            case 7:
                return QString("sequence");
            case 8:
                return QString("specific sequence");
            case 9:
                return QString("coverage");
            case 10:
                return QString("PAI");
            }
        }
    }
    return QVariant();
}
QVariant ProteinTableModel::data(const QModelIndex &index, int role ) const {
    // generate a log message when this method gets called
    int row = index.row();
    int col = index.column();
    qDebug() << QString("row %1, col%2, role %3")
             .arg(row).arg(col).arg(role);


    switch(role) {
    case Qt::CheckStateRole:

        if (col == 0) //add a checkbox to cell(1,0)
        {
            if ( _p_identification_group->getProteinMatchList().at(row)->isChecked()) {
                return Qt::Checked;
            }
            else {
                return Qt::Unchecked;
            }
        }
        break;
    case Qt::BackgroundRole:
        if (_p_identification_group->getProteinMatchList().at(row)->isValid() == false)
        {
            return QVariant(QColor("grey"));
        }
        if (_p_identification_group->getProteinMatchList().at(row)->getProteinXtpSp().get()->isDecoy() == true)
        {
            return QVariant(QColor("orange"));
        }
        if (_p_identification_group->getProteinMatchList().at(row)->getProteinXtpSp().get()->isContaminant() == true)
        {
            return QVariant(QColor("orange"));
        }
        break;
    case Qt::DisplayRole:
        if (_p_identification_group == nullptr) {
            return QVariant();
        }
        if (col == 0) {
            return QVariant();
        }

        if (col == 1) {
            pappso::GrpProtein * p_grp_prot = _p_identification_group->getProteinMatchList().at(row)->getGrpProteinSp().get();
            if (p_grp_prot != nullptr) return p_grp_prot->getGroupingId();
            return QVariant();
        }

        if (col == 2) {
            return _p_identification_group->getProteinMatchList().at(row)->getProteinXtpSp().get()->getAccession();

        }
        if (col == 3) {
            return _p_identification_group->getProteinMatchList().at(row)->getProteinXtpSp().get()->getDescription();

        }
        if (col == 4) {
            return QVariant ((qreal)_p_identification_group->getProteinMatchList().at(row)->getEvalue());

        }

        if (col == 5) {//spectrum
            return QVariant ((qreal) _p_identification_group->getProteinMatchList().at(row)->countValidAndCheckedSpectrum());
        }
        if (col == 6) {//specific
            GroupingGroup * p_groupin_group = _p_identification_group->getProteinMatchList().at(row)->getGroupingGroupSp().get();
            if (p_groupin_group != nullptr) {
                return QVariant ((qreal) p_groupin_group->countSpecificSpectrum(_p_identification_group->getProteinMatchList().at(row)));
            }
        }
        if (col == 7) {//sequence
            return QVariant ((qreal) _p_identification_group->getProteinMatchList().at(row)->countUniqueSequence());
        }
        if (col == 8) {//specific sequence
            GroupingGroup * p_groupin_group = _p_identification_group->getProteinMatchList().at(row)->getGroupingGroupSp().get();
            if (p_groupin_group != nullptr) {
                return QVariant ((qreal) p_groupin_group->countSpecificSequence(_p_identification_group->getProteinMatchList().at(row)));
            }
        }
        if (col == 9) {
            return QVariant ((qreal)_p_identification_group->getProteinMatchList().at(row)->getCoverage());
        }
        if (col == 10) {
            return QVariant ((qreal)_p_identification_group->getProteinMatchList().at(row)->getPAI());
        }

        return QVariant();
    }
    return QVariant();
}

void ProteinTableModel::onTableClicked(const QModelIndex &index)
{
    int row = index.row();
    int col = index.column();
    if (col == 0) //add a checkbox to cell(1,0)
    {
        if ( _p_identification_group->getProteinMatchList().at(row)->isChecked()) {
            _p_identification_group->getProteinMatchList().at(row)->setChecked(false);
        }
        else {
            _p_identification_group->getProteinMatchList().at(row)->setChecked(true);
        }
        _p_protein_list_window->edited();
    }
}

void ProteinTableModel::onTableDoubleClicked(const QModelIndex &index)
{
    int row = index.row();
    int col = index.column();
    _p_protein_list_window->doubleclickOnproteinMatch(_p_identification_group->getProteinMatchList().at(row));
}

bool ProteinTableModel::acceptRow(int source_row) {
    ProteinMatch * protein_match = _p_identification_group->getProteinMatchList().at(source_row);
    if (!_protein_search_string.isEmpty()) {
        if (!protein_match->getProteinXtpSp().get()->getDescription().contains(_protein_search_string)) {
            return false;
        }
    }
    if (_hide_not_valid) {
        if (! protein_match->isValid()) {
            return false;
        }
    }
    if (_hide_not_checked) {
        if (! protein_match->isChecked()) {
            return false;
        }
    }
    if (_hide_not_grouped) {
        pappso::GrpProtein * p_grp_prot = protein_match->getGrpProteinSp().get();
        if (p_grp_prot == nullptr) {
            return false;
        }
        if (p_grp_prot->getGroupingId().isEmpty()) {
            return false;
        }
    }

    return true;
}