Skip to content

Commit

Permalink
Small improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
SteampunkIslande committed Jul 17, 2024
1 parent 8cf5475 commit 0cd7f75
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 18 deletions.
7 changes: 4 additions & 3 deletions config_folder/validation_methods/validation_ppi.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,15 @@
"select": {
"fields": [
"main_table.run_name AS 'Nom du run'",
"validation_hash AS '.validation_hash'",
"main_table.sample_name AS 'Échantillon'",
"main_table.snpeff_Gene_Name AS 'Nom du gène'",
"main_table.snpeff_Gene_Name AS 'Nom du gène:color=red:bold='",
"main_table.snpeff_Feature_ID AS 'NM'",
"main_table.chromosome AS 'Chromosome'",
"main_table.position AS 'Position'",
"main_table.reference AS 'Allèle de référence'",
"main_table.alternate AS 'Allèle alternatif'",
"main_table.cv_AF AS 'Fréquence allélique'",
"format('{{:.2f}}',main_table.cv_AF) AS 'Fréquence allélique'",
"main_table.snpeff_Annotation AS 'Annotation'",
"main_table.cv_GT AS 'Génotype'",
"main_table.snpeff_Annotation_Impact AS 'Annotation impact'",
Expand Down Expand Up @@ -157,7 +158,7 @@
"expression": "read_csv('{pwd}/lists/liste_nms.csv',sep=',',header=false,names=['NM'])",
"alias": "nms",
"on": "main_table.snpeff_Feature_ID = nms.NM",
"how": "right"
"how": ""
}
],
"filter": {
Expand Down
46 changes: 37 additions & 9 deletions query_table_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,73 @@


import PySide6.QtCore as qc
import PySide6.QtGui as qg

from query import Query


def style_from_colname(colname: str):
_, *options = colname.split(":")
if not options:
return {}
return dict([opt.split("=") for opt in options])


class QueryTableModel(qc.QAbstractTableModel):

def __init__(self, query: Query, parent=None):
super().__init__(parent)
self.query = query

self.header = self.query.get_header()
self._data = self.query.get_data()

self.query.query_changed.connect(self.update)

def rowCount(self, parent):
if parent.isValid():
return 0
return len(self.query.get_data())
return len(self._data)

def columnCount(self, parent):
if parent.isValid():
return 0
if self.query.get_data():
return len(self.query.get_data()[0])
if self._data:
return len(self._data[0])
return 0

def data(self, index, role=qc.Qt.ItemDataRole.DisplayRole):
def data(self, index: qc.QModelIndex, role=qc.Qt.ItemDataRole.DisplayRole):
if role == qc.Qt.ItemDataRole.DisplayRole:
if index.row() < 0 or index.row() >= len(self.query.get_data()):
if index.row() < 0 or index.row() >= len(self._data):
return None
if index.column() < 0 or index.column() >= len(self.query.get_data()[0]):
if index.column() < 0 or index.column() >= len(self._data[0]):
return None
return self.query.get_data()[index.row()][index.column()]

return str(self._data[index.row()][index.column()])

def headerData(self, section, orientation, role=qc.Qt.ItemDataRole.DisplayRole):
if section >= len(self.query.get_header()):
if section >= len(self.header):
return None
if role == qc.Qt.ItemDataRole.DisplayRole:
if orientation == qc.Qt.Orientation.Horizontal:
return self.query.get_header()[section]
return str(self.header[section])

draw_options = style_from_colname(self.header[section])

if role == qc.Qt.ItemDataRole.ForegroundRole:
if "color" in draw_options:
return qg.QColor(draw_options["color"])
if role == qc.Qt.ItemDataRole.BackgroundRole:
if "background" in draw_options:
return qg.QColor(draw_options["background"])
if role == qc.Qt.ItemDataRole.FontRole:
if "bold" in draw_options:
font = qg.QFont()
font.setBold(True)
return font

def update(self):
self.beginResetModel()
self.header = self.query.get_header()
self._data = self.query.get_data()
self.endResetModel()
30 changes: 24 additions & 6 deletions query_table_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,39 @@
from query_table_model import QueryTableModel


class QueryTableProxyModel(qc.QSortFilterProxyModel):
# Automatically hides columns which names start with a dot
def filterAcceptsColumn(self, source_column: int, source_parent: qc.QModelIndex):
source_model = self.sourceModel()
header: str = source_model.headerData(
source_column, qc.Qt.Orientation.Horizontal
)
return not header.startswith(".")

# Rename columns by splitting on every colon
def headerData(self, section: int, orientation: qc.Qt.Orientation, role: int):
source_model = self.sourceModel()
actual_section = self.mapToSource(self.index(0, section)).column()
header: str = source_model.headerData(actual_section, orientation, role)
if role == qc.Qt.ItemDataRole.DisplayRole:
return header.split(":")[0] if header else header
return header


class QueryTableWidget(qw.QWidget):

def __init__(self, query: Query, parent=None):
super().__init__(parent)

self.query = query
self.model = QueryTableModel(query)
self.proxy_model = QueryTableProxyModel()
self.proxy_model.setSourceModel(self.model)

self.table_view = qw.QTableView()
self.table_view.setSelectionBehavior(
qw.QAbstractItemView.SelectionBehavior.SelectRows
)
self.table_view.setSelectionMode(
qw.QAbstractItemView.SelectionMode.SingleSelection
)
self.table_view.horizontalHeader().setStretchLastSection(
True
) # Set last column to expand
Expand All @@ -37,7 +55,7 @@ def __init__(self, query: Query, parent=None):
self.table_view.horizontalHeader().customContextMenuRequested.connect(
self.show_header_context_menu
)
self.table_view.setModel(self.model)
self.table_view.setModel(self.proxy_model)

self.page_selector = PageSelector(query)

Expand Down Expand Up @@ -71,8 +89,8 @@ def filter_column(self, index: qc.QModelIndex):

if dialog.exec_() == qw.QDialog.DialogCode.Accepted:
filter_text = dialog.textValue()
self.model.query.filter_model.add_filter(
self.model.query.filter_model.index(0, 0),
self.query.filter_model.add_filter(
self.query.filter_model.index(0, 0),
FilterType.LEAF,
f"{col_name} {filter_text}",
)

0 comments on commit 0cd7f75

Please sign in to comment.