documentation and converted to username based exclusion of adding new kits

This commit is contained in:
Landon Wark
2023-01-30 12:07:38 -06:00
parent bbb65d3fe6
commit 1f832dccf2
16 changed files with 876 additions and 296 deletions

View File

@@ -1,20 +1,20 @@
import re
from PyQt6.QtWidgets import (
QMainWindow, QLabel, QToolBar, QStatusBar,
QMainWindow, QLabel, QToolBar,
QTabWidget, QWidget, QVBoxLayout,
QPushButton, QMenuBar, QFileDialog,
QPushButton, QFileDialog,
QLineEdit, QMessageBox, QComboBox, QDateEdit, QHBoxLayout,
QSpinBox, QScrollArea, QScrollBar, QSizePolicy
QSpinBox, QScrollArea
)
from PyQt6.QtGui import QAction, QIcon
from PyQt6.QtCore import pyqtSlot, QDateTime, QDate, QSignalBlocker, Qt
from PyQt6.QtGui import QAction
from PyQt6.QtCore import QSignalBlocker
from PyQt6.QtWebEngineWidgets import QWebEngineView
import pandas as pd
# import pandas as pd
from pathlib import Path
import plotly
import plotly.express as px
# import plotly.express as px
import yaml
from backend.excel.parser import SheetParser
@@ -30,41 +30,45 @@ import numpy
from frontend.custom_widgets import AddReagentQuestion, AddReagentForm, SubmissionsSheet, ReportDatePicker, KitAdder, ControlsDatePicker, OverwriteSubQuestion
import logging
import difflib
from datetime import date
from frontend.visualizations.charts import create_charts
logger = logging.getLogger(__name__)
logger.info("Hello, I am a logger")
class App(QMainWindow):
# class App(QScrollArea):
def __init__(self, ctx: dict = {}):
super().__init__()
self.ctx = ctx
self.title = 'Submissions App - PyQT6'
try:
self.title = f"Submissions App (v{ctx['package'].__version__})"
except AttributeError:
self.title = f"Submissions App"
# set initial app position and size
self.left = 0
self.top = 0
self.width = 1300
self.height = 1000
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
# insert tabs into main app
self.table_widget = AddSubForm(self)
self.setCentralWidget(self.table_widget)
# run initial setups
self._createActions()
self._createMenuBar()
self._createToolBar()
self._connectActions()
# self.renderPage()
self.controls_getter()
self.show()
def _createMenuBar(self):
"""
adds items to menu bar
"""
menuBar = self.menuBar()
fileMenu = menuBar.addMenu("&File")
# menuBar.addMenu(fileMenu)
# Creating menus using a title
editMenu = menuBar.addMenu("&Edit")
reportMenu = menuBar.addMenu("&Reports")
@@ -73,12 +77,18 @@ class App(QMainWindow):
reportMenu.addAction(self.generateReportAction)
def _createToolBar(self):
"""
adds items to toolbar
"""
toolbar = QToolBar("My main toolbar")
self.addToolBar(toolbar)
toolbar.addAction(self.addReagentAction)
toolbar.addAction(self.addKitAction)
def _createActions(self):
"""
creates actions
"""
self.importAction = QAction("&Import", self)
self.addReagentAction = QAction("Add Reagent", self)
self.generateReportAction = QAction("Make Report", self)
@@ -86,6 +96,9 @@ class App(QMainWindow):
def _connectActions(self):
"""
connect menu and tool bar item to functions
"""
self.importAction.triggered.connect(self.importSubmission)
self.addReagentAction.triggered.connect(self.add_reagent)
self.generateReportAction.triggered.connect(self.generateReport)
@@ -97,21 +110,27 @@ class App(QMainWindow):
def importSubmission(self):
"""
import submission from excel sheet into form
"""
logger.debug(self.ctx)
# initialize samples
self.samples = []
# set file dialog
home_dir = str(Path(self.ctx["directory_path"]))
fname = Path(QFileDialog.getOpenFileName(self, 'Open file', home_dir)[0])
logger.debug(f"Attempting to parse file: {fname}")
assert fname.exists()
# create sheetparser using excel sheet and context from gui
try:
prsr = SheetParser(fname, **self.ctx)
except PermissionError:
return
logger.debug(f"prsr.sub = {prsr.sub}")
# replace formlayout with tab1.layout
# self.form = self.table_widget.formlayout
# destroy any widgets from previous imports
for item in self.table_widget.formlayout.parentWidget().findChildren(QWidget):
item.setParent(None)
# regex to parser out different variable types for decision making
variable_parser = re.compile(r"""
# (?x)
(?P<extraction_kit>^extraction_kit$) |
@@ -119,11 +138,10 @@ class App(QMainWindow):
(?P<submitting_lab>)^submitting_lab$ |
(?P<samples>)^samples$ |
(?P<reagent>^lot_.*$)
""", re.VERBOSE)
for item in prsr.sub:
logger.debug(f"Item: {item}")
# attempt to match variable name to regex group
try:
mo = variable_parser.fullmatch(item).lastgroup
except AttributeError:
@@ -131,17 +149,23 @@ class App(QMainWindow):
logger.debug(f"Mo: {mo}")
match mo:
case 'submitting_lab':
# create label
self.table_widget.formlayout.addWidget(QLabel(item.replace("_", " ").title()))
logger.debug(f"{item}: {prsr.sub[item]}")
# create combobox to hold looked up submitting labs
add_widget = QComboBox()
labs = [item.__str__() for item in lookup_all_orgs(ctx=self.ctx)]
# try to set closest match to top of list
try:
labs = difflib.get_close_matches(prsr.sub[item], labs, len(labs), 0)
except (TypeError, ValueError):
pass
# set combobox values to lookedup values
add_widget.addItems(labs)
case 'extraction_kit':
# create label
self.table_widget.formlayout.addWidget(QLabel(item.replace("_", " ").title()))
# if extraction kit not available, all other values fail
if prsr.sub[item] == 'nan':
msg = QMessageBox()
# msg.setIcon(QMessageBox.critical)
@@ -150,18 +174,27 @@ class App(QMainWindow):
msg.setWindowTitle("Error")
msg.exec()
break
# create combobox to hold looked up kits
add_widget = QComboBox()
# lookup existing kits by 'submission_type' decided on by sheetparser
uses = [item.__str__() for item in lookup_kittype_by_use(ctx=self.ctx, used_by=prsr.sub['submission_type'])]
if len(uses) > 0:
add_widget.addItems(uses)
else:
add_widget.addItems(['bacterial_culture'])
case 'submitted_date':
# create label
self.table_widget.formlayout.addWidget(QLabel(item.replace("_", " ").title()))
# uses base calendar
add_widget = QDateEdit(calendarPopup=True)
# add_widget.setDateTime(QDateTime.date(prsr.sub[item]))
add_widget.setDate(prsr.sub[item])
# sets submitted date based on date found in excel sheet
try:
add_widget.setDate(prsr.sub[item])
# if not found, use today
except:
add_widget.setDate(date.today())
case 'reagent':
# create label
self.table_widget.formlayout.addWidget(QLabel(item.replace("_", " ").title()))
add_widget = QComboBox()
add_widget.setEditable(True)
@@ -174,8 +207,10 @@ class App(QMainWindow):
prsr.sub[item] = int(prsr.sub[item])
except ValueError:
pass
# query for reagents using type name from sheet and kit from sheet
relevant_reagents = [item.__str__() for item in lookup_regent_by_type_name_and_kit_name(ctx=self.ctx, type_name=query_var, kit_name=prsr.sub['extraction_kit'])]
logger.debug(f"Relevant reagents: {relevant_reagents}")
# if reagent in sheet is not found insert it into items
if prsr.sub[item] not in relevant_reagents and prsr.sub[item] != 'nan':
try:
check = not numpy.isnan(prsr.sub[item])
@@ -187,49 +222,35 @@ class App(QMainWindow):
add_widget.addItems(relevant_reagents)
# TODO: make samples not appear in frame.
case 'samples':
# hold samples in 'self' until form submitted
logger.debug(f"{item}: {prsr.sub[item]}")
self.samples = prsr.sub[item]
case _:
# anything else gets added in as a line edit
self.table_widget.formlayout.addWidget(QLabel(item.replace("_", " ").title()))
add_widget = QLineEdit()
add_widget.setText(str(prsr.sub[item]).replace("_", " "))
self.table_widget.formlayout.addWidget(add_widget)
# create submission button
submit_btn = QPushButton("Submit")
self.table_widget.formlayout.addWidget(submit_btn)
submit_btn.clicked.connect(self.submit_new_sample)
# self.table_widget.interior.setSizePolicy(QSizePolicy.Policy.Preferred, QSizePolicy.Policy.MinimumExpanding)
print(self.table_widget.formwidget.size())
# def renderPage(self):
# """
# Test function for plotly chart rendering
# """
# df = pd.read_excel("C:\\Users\\lwark\\Desktop\\test_df.xlsx", engine="openpyxl")
# fig = px.bar(df, x="submitted_date", y="kraken_percent", color="genus", title="Long-Form Input")
# fig.update_layout(
# xaxis_title="Submitted Date (* - Date parsed from fastq file creation date)",
# yaxis_title="Kraken Percent",
# showlegend=True,
# barmode='stack'
# )
# html = '<html><body>'
# html += plotly.offline.plot(fig, output_type='div', include_plotlyjs='cdn', auto_open=True, image = 'png', image_filename='plot_image')
# html += '</body></html>'
# self.table_widget.webengineview.setHtml(html)
# self.table_widget.webengineview.update()
def submit_new_sample(self):
"""
Attempt to add sample to database when 'submit' button clicked
"""
# get info from form
labels, values = self.extract_form_info(self.table_widget.tab1)
info = {item[0]:item[1] for item in zip(labels, values) if not item[0].startswith("lot_")}
reagents = {item[0]:item[1] for item in zip(labels, values) if item[0].startswith("lot_")}
logger.debug(f"Reagents: {reagents}")
parsed_reagents = []
# compare reagents in form to reagent database
for reagent in reagents:
wanted_reagent = lookup_reagent(ctx=self.ctx, reagent_lot=reagents[reagent])
logger.debug(wanted_reagent)
logger.debug(f"Looked up reagent: {wanted_reagent}")
# if reagent not found offer to add to database
if wanted_reagent == None:
dlg = AddReagentQuestion(reagent_type=reagent, reagent_lot=reagents[reagent])
if dlg.exec():
@@ -239,49 +260,78 @@ class App(QMainWindow):
if wanted_reagent != None:
parsed_reagents.append(wanted_reagent)
logger.debug(info)
# move samples into preliminary submission dict
info['samples'] = self.samples
# construct submission object
base_submission, output = construct_submission_info(ctx=self.ctx, info_dict=info)
# check output message for issues
if output['message'] != None:
dlg = OverwriteSubQuestion(output['message'], base_submission.rsl_plate_num)
if dlg.exec():
base_submission.reagents = []
else:
return
# add reagents to submission object
for reagent in parsed_reagents:
base_submission.reagents.append(reagent)
logger.debug(f"Sending submission: {base_submission.rsl_plate_num} to database.")
result = store_submission(ctx=self.ctx, base_submission=base_submission)
# check result of storing for issues
if result != None:
msg = QMessageBox()
# msg.setIcon(QMessageBox.critical)
msg.setText("Error")
msg.setInformativeText(result['message'])
msg.setWindowTitle("Error")
msg.show()
msg.exec()
# update summary sheet
self.table_widget.sub_wid.setData()
# reset form
for item in self.table_widget.formlayout.parentWidget().findChildren(QWidget):
item.setParent(None)
def add_reagent(self, reagent_lot:str|None=None, reagent_type:str|None=None):
"""
Action to create new reagent in DB.
Args:
reagent_lot (str | None, optional): Parsed reagent from import form. Defaults to None.
reagent_type (str | None, optional): Parsed reagent type from import form. Defaults to None.
Returns:
models.Reagent: the constructed reagent object to add to submission
"""
if isinstance(reagent_lot, bool):
reagent_lot = ""
# create form
dlg = AddReagentForm(ctx=self.ctx, reagent_lot=reagent_lot, reagent_type=reagent_type)
if dlg.exec():
# extract form info
labels, values = self.extract_form_info(dlg)
info = {item[0]:item[1] for item in zip(labels, values)}
logger.debug(f"Reagent info: {info}")
# create reagent object
reagent = construct_reagent(ctx=self.ctx, info_dict=info)
# send reagent to db
store_reagent(ctx=self.ctx, reagent=reagent)
return reagent
def extract_form_info(self, object):
"""
retrieves arbitrary number of labels, values from form
Args:
object (_type_): _description_
Returns:
_type_: _description_
"""
labels = []
values = []
# grab all widgets in form
for item in object.layout.parentWidget().findChildren(QWidget):
match item:
case QLabel():
labels.append(item.text().replace(" ", "_").lower())
@@ -295,19 +345,29 @@ class App(QMainWindow):
values.append(item.currentText())
case QDateEdit():
values.append(item.date().toPyDate())
# value for ad hoc check above
prev_item = item
return labels, values
def generateReport(self):
"""
Action to create a summary of sheet data per client
"""
# Custom two date picker for start & end dates
dlg = ReportDatePicker()
if dlg.exec():
labels, values = self.extract_form_info(dlg)
info = {item[0]:item[1] for item in zip(labels, values)}
# find submissions based on date range
subs = lookup_submissions_by_date_range(ctx=self.ctx, start_date=info['start_date'], end_date=info['end_date'])
# convert each object to dict
records = [item.report_dict() for item in subs]
# make dataframe from record dictionaries
df = make_report_xlsx(records=records)
home_dir = Path(self.ctx["directory_path"]).joinpath(f"Submissions_{info['start_date']}-{info['end_date']}.xlsx").resolve().__str__()
# setup filedialog to handle save location of report
home_dir = Path(self.ctx["directory_path"]).joinpath(f"Submissions_Report_{info['start_date']}-{info['end_date']}.xlsx").resolve().__str__()
fname = Path(QFileDialog.getSaveFileName(self, "Save File", home_dir, filter=".xlsx")[0])
# save file
try:
df.to_excel(fname, engine="openpyxl")
except PermissionError:
@@ -315,9 +375,14 @@ class App(QMainWindow):
def add_kit(self):
"""
Constructs new kit from yaml and adds to DB.
"""
# setup file dialog to find yaml flie
home_dir = str(Path(self.ctx["directory_path"]))
fname = Path(QFileDialog.getOpenFileName(self, 'Open file', home_dir, filter = "yml(*.yml)")[0])
assert fname.exists()
# read yaml file
try:
with open(fname.__str__(), "r") as stream:
try:
@@ -327,64 +392,96 @@ class App(QMainWindow):
return {}
except PermissionError:
return
create_kit_from_yaml(ctx=self.ctx, exp=exp)
# send to kit creator function
result = create_kit_from_yaml(ctx=self.ctx, exp=exp)
msg = QMessageBox()
# msg.setIcon(QMessageBox.critical)
match result['code']:
case 0:
msg.setText("Kit added")
msg.setInformativeText(result['message'])
msg.setWindowTitle("Kit added")
case 1:
msg.setText("Permission Error")
msg.setInformativeText(result['message'])
msg.setWindowTitle("Permission Error")
msg.exec()
def controls_getter(self):
# self.table_widget.webengineview.setHtml("")
"""
Lookup controls from database and send to chartmaker
"""
# subtype defaults to disabled
try:
self.table_widget.sub_typer.disconnect()
except TypeError:
pass
# correct start date being more recent than end date and rerun
if self.table_widget.datepicker.start_date.date() > self.table_widget.datepicker.end_date.date():
logger.warning("Start date after end date is not allowed!")
# self.table_widget.datepicker.start_date.setDate(e_date)
threemonthsago = self.table_widget.datepicker.end_date.date().addDays(-90)
# block signal that will rerun controls getter and set start date
with QSignalBlocker(self.table_widget.datepicker.start_date) as blocker:
self.table_widget.datepicker.start_date.setDate(threemonthsago)
self.controls_getter()
return
# convert to python useable date object
self.start_date = self.table_widget.datepicker.start_date.date().toPyDate()
self.end_date = self.table_widget.datepicker.end_date.date().toPyDate()
self.con_type = self.table_widget.control_typer.currentText()
self.mode = self.table_widget.mode_typer.currentText()
self.table_widget.sub_typer.clear()
# lookup subtypes
sub_types = get_control_subtypes(ctx=self.ctx, type=self.con_type, mode=self.mode)
if sub_types != []:
# block signal that will rerun controls getter and update sub_typer
with QSignalBlocker(self.table_widget.sub_typer) as blocker:
self.table_widget.sub_typer.addItems(sub_types)
self.table_widget.sub_typer.setEnabled(True)
self.table_widget.sub_typer.currentTextChanged.connect(self.chart_maker)
else:
self.table_widget.sub_typer.clear()
self.table_widget.sub_typer.setEnabled(False)
self.chart_maker()
def chart_maker(self):
"""
Creates plotly charts for webview
"""
logger.debug(f"Control getter context: \n\tControl type: {self.con_type}\n\tMode: {self.mode}\n\tStart Date: {self.start_date}\n\tEnd Date: {self.end_date}")
if self.table_widget.sub_typer.currentText() == "":
self.subtype = None
else:
self.subtype = self.table_widget.sub_typer.currentText()
logger.debug(f"Subtype: {self.subtype}")
# query all controls using the type/start and end dates from the gui
controls = get_all_controls_by_type(ctx=self.ctx, con_type=self.con_type, start_date=self.start_date, end_date=self.end_date)
# if no data found from query set fig to none for reporting in webview
if controls == None:
return
data = []
for control in controls:
dicts = convert_control_by_mode(ctx=self.ctx, control=control, mode=self.mode)
data.append(dicts)
data = [item for sublist in data for item in sublist]
# logger.debug(data)
df = convert_data_list_to_df(ctx=self.ctx, input=data, subtype=self.subtype)
if self.subtype == None:
title = self.mode
# return
fig = None
else:
title = f"{self.mode} - {self.subtype}"
fig = create_charts(ctx=self.ctx, df=df, ytitle=title)
data = []
for control in controls:
# change each control to list of dicts
dicts = convert_control_by_mode(ctx=self.ctx, control=control, mode=self.mode)
data.append(dicts)
# flatten data to one dimensional list
data = [item for sublist in data for item in sublist]
# logger.debug(data)
# send to dataframe creator
df = convert_data_list_to_df(ctx=self.ctx, input=data, subtype=self.subtype)
if self.subtype == None:
title = self.mode
else:
title = f"{self.mode} - {self.subtype}"
# send dataframe to chart maker
fig = create_charts(ctx=self.ctx, df=df, ytitle=title)
logger.debug(f"Updating figure...")
# construct html for webview
html = '<html><body>'
if fig != None:
html += plotly.offline.plot(fig, output_type='div', include_plotlyjs='cdn')#, image = 'png', auto_open=True, image_filename='plot_image')
@@ -393,24 +490,12 @@ class App(QMainWindow):
html += '</body></html>'
# with open("C:\\Users\\lwark\\Desktop\\test.html", "w") as f:
# f.write(html)
# add html to webview and update.
self.table_widget.webengineview.setHtml(html)
self.table_widget.webengineview.update()
logger.debug("Figure updated... I hope.")
# def datechange(self):
# s_date = self.table_widget.datepicker.start_date.date()
# e_date = self.table_widget.datepicker.end_date.date()
# if s_date > e_date:
# logger.debug("that is not allowed!")
# # self.table_widget.datepicker.start_date.setDate(e_date)
# threemonthsago = e_date.addDays(-90)
# self.table_widget.datepicker.start_date.setDate(threemonthsago)
# self.chart_maker()
class AddSubForm(QWidget):
def __init__(self, parent):
@@ -428,64 +513,57 @@ class AddSubForm(QWidget):
self.tabs.addTab(self.tab1,"Submissions")
self.tabs.addTab(self.tab2,"Controls")
self.tabs.addTab(self.tab3, "Add Kit")
# Create first tab
# self.scroller = QWidget()
# self.scroller.layout = QVBoxLayout(self)
# self.scroller.setLayout(self.scroller.layout)
# self.tab1.setMaximumHeight(1000)
# Create submission adder form
self.formwidget = QWidget(self)
self.formlayout = QVBoxLayout(self)
self.formwidget.setLayout(self.formlayout)
self.formwidget.setFixedWidth(300)
# Make scrollable interior for form
self.interior = QScrollArea(self.tab1)
# self.interior.verticalScrollBar()
# self.interior.setVerticalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOn)
self.interior.setWidgetResizable(True)
self.interior.setFixedWidth(325)
# self.interior.setParent(self.tab1)
self.interior.setWidget(self.formwidget)
# Create sheet to hold existing submissions
self.sheetwidget = QWidget(self)
self.sheetlayout = QVBoxLayout(self)
self.sheetwidget.setLayout(self.sheetlayout)
self.sub_wid = SubmissionsSheet(parent.ctx)
self.sheetlayout.addWidget(self.sub_wid)
# Create layout of first tab to hold form and sheet
self.tab1.layout = QHBoxLayout(self)
self.tab1.setLayout(self.tab1.layout)
# self.tab1.layout.addLayout(self.formlayout)
self.tab1.layout.addWidget(self.interior)
# self.tab1.layout.addWidget(self.formwidget)
self.tab1.layout.addWidget(self.sheetwidget)
# self.tab1.layout.addLayout(self.sheetlayout)
# create widgets for tab 2
self.datepicker = ControlsDatePicker()
self.webengineview = QWebEngineView()
# set tab2 layout
self.tab2.layout = QVBoxLayout(self)
self.control_typer = QComboBox()
# fetch types of controls
con_types = get_all_Control_Types_names(ctx=parent.ctx)
self.control_typer.addItems(con_types)
# create custom widget to get types of analysis
self.mode_typer = QComboBox()
mode_types = get_all_available_modes(ctx=parent.ctx)
self.mode_typer.addItems(mode_types)
# create custom widget to get subtypes of analysis
self.sub_typer = QComboBox()
self.sub_typer.setEnabled(False)
# add widgets to tab2 layout
self.tab2.layout.addWidget(self.datepicker)
self.tab2.layout.addWidget(self.control_typer)
self.tab2.layout.addWidget(self.mode_typer)
self.tab2.layout.addWidget(self.sub_typer)
self.tab2.layout.addWidget(self.webengineview)
self.tab2.setLayout(self.tab2.layout)
# Add tabs to widget
# create custom widget to add new tabs
adder = KitAdder(parent_ctx=parent.ctx)
self.tab3.layout = QVBoxLayout(self)
self.tab3.layout.addWidget(adder)
self.tab3.setLayout(self.tab3.layout)
# add tabs to main widget
self.layout.addWidget(self.tabs)
self.setLayout(self.layout)
print(self.tab1.layout.parentWidget().findChildren(QScrollArea))

View File

@@ -4,7 +4,8 @@ from PyQt6.QtWidgets import (
QDialogButtonBox, QDateEdit, QTableView,
QTextEdit, QSizePolicy, QWidget,
QGridLayout, QPushButton, QSpinBox,
QScrollBar, QScrollArea, QHBoxLayout
QScrollBar, QScrollArea, QHBoxLayout,
QMessageBox
)
from PyQt6.QtCore import Qt, QDate, QAbstractTableModel, QSize
from PyQt6.QtGui import QFontMetrics
@@ -26,7 +27,10 @@ loader = FileSystemLoader(loader_path)
env = Environment(loader=loader)
class AddReagentQuestion(QDialog):
def __init__(self, reagent_type:str, reagent_lot:str):
"""
dialog to ask about adding a new reagne to db
"""
def __init__(self, reagent_type:str, reagent_lot:str) -> None:
super().__init__()
self.setWindowTitle(f"Add {reagent_lot}?")
@@ -45,7 +49,10 @@ class AddReagentQuestion(QDialog):
class OverwriteSubQuestion(QDialog):
def __init__(self, message:str, rsl_plate_num:str):
"""
dialog to ask about overwriting existing submission
"""
def __init__(self, message:str, rsl_plate_num:str) -> None:
super().__init__()
self.setWindowTitle(f"Overwrite {rsl_plate_num}?")
@@ -64,7 +71,10 @@ class OverwriteSubQuestion(QDialog):
class AddReagentForm(QDialog):
def __init__(self, ctx:dict, reagent_lot:str|None, reagent_type:str|None):
"""
dialog to add gather info about new reagent
"""
def __init__(self, ctx:dict, reagent_lot:str|None, reagent_type:str|None) -> None:
super().__init__()
if reagent_lot == None:
@@ -77,20 +87,23 @@ class AddReagentForm(QDialog):
self.buttonBox = QDialogButtonBox(QBtn)
self.buttonBox.accepted.connect(self.accept)
self.buttonBox.rejected.connect(self.reject)
# get lot info
lot_input = QLineEdit()
lot_input.setText(reagent_lot)
# get expiry info
exp_input = QDateEdit(calendarPopup=True)
exp_input.setDate(QDate.currentDate())
# get reagent type info
type_input = QComboBox()
type_input.addItems([item.replace("_", " ").title() for item in get_all_reagenttype_names(ctx=ctx)])
logger.debug(f"Trying to find index of {reagent_type}")
# convert input to user friendly string?
try:
reagent_type = reagent_type.replace("_", " ").title()
except AttributeError:
reagent_type = None
# set parsed reagent type to top of list
index = type_input.findText(reagent_type, Qt.MatchFlag.MatchEndsWith)
if index >= 0:
type_input.setCurrentIndex(index)
self.layout = QVBoxLayout()
@@ -106,18 +119,38 @@ class AddReagentForm(QDialog):
class pandasModel(QAbstractTableModel):
def __init__(self, data):
"""
pandas model for inserting summary sheet into gui
"""
def __init__(self, data) -> None:
QAbstractTableModel.__init__(self)
self._data = data
def rowCount(self, parent=None):
def rowCount(self, parent=None) -> int:
"""
does what it says
Args:
parent (_type_, optional): _description_. Defaults to None.
Returns:
int: number of rows in data
"""
return self._data.shape[0]
def columnCount(self, parnet=None):
def columnCount(self, parnet=None) -> int:
"""
does what it says
Args:
parnet (_type_, optional): _description_. Defaults to None.
Returns:
int: number of columns in data
"""
return self._data.shape[1]
def data(self, index, role=Qt.ItemDataRole.DisplayRole):
def data(self, index, role=Qt.ItemDataRole.DisplayRole) -> str|None:
if index.isValid():
if role == Qt.ItemDataRole.DisplayRole:
return str(self._data.iloc[index.row(), index.column()])
@@ -130,7 +163,16 @@ class pandasModel(QAbstractTableModel):
class SubmissionsSheet(QTableView):
def __init__(self, ctx:dict):
"""
presents submission summary to user in tab1
"""
def __init__(self, ctx:dict) -> None:
"""
initialize
Args:
ctx (dict): settings passed from gui
"""
super().__init__()
self.ctx = ctx
self.setData()
@@ -139,20 +181,19 @@ class SubmissionsSheet(QTableView):
# self.clicked.connect(self.test)
self.doubleClicked.connect(self.show_details)
def setData(self):
# horHeaders = []
# for n, key in enumerate(sorted(self.data.keys())):
# horHeaders.append(key)
# for m, item in enumerate(self.data[key]):
# newitem = QTableWidgetItem(item)
# self.setItem(m, n, newitem)
# self.setHorizontalHeaderLabels(horHeaders)
def setData(self) -> None:
"""
sets data in model
"""
self.data = submissions_to_df(ctx=self.ctx)
self.model = pandasModel(self.data)
self.setModel(self.model)
# self.resize(800,600)
def show_details(self, item):
def show_details(self) -> None:
"""
creates detailed data to show in seperate window
"""
index=(self.selectionModel().currentIndex())
# logger.debug(index)
value=index.sibling(index.row(),0).data()
@@ -165,60 +206,65 @@ class SubmissionsSheet(QTableView):
class SubmissionDetails(QDialog):
"""
a window showing text details of submission
"""
def __init__(self, ctx:dict, id:int) -> None:
super().__init__()
self.setWindowTitle("Submission Details")
# create scrollable interior
interior = QScrollArea()
interior.setParent(self)
# get submision from db
data = lookup_submission_by_id(ctx=ctx, id=id)
base_dict = data.to_dict()
# don't want id
del base_dict['id']
# convert sub objects to dicts
base_dict['reagents'] = [item.to_sub_dict() for item in data.reagents]
base_dict['samples'] = [item.to_sub_dict() for item in data.samples]
# retrieve jinja template
template = env.get_template("submission_details.txt")
# render using object dict
text = template.render(sub=base_dict)
# create text field
txt_editor = QTextEdit(self)
txt_editor.setReadOnly(True)
txt_editor.document().setPlainText(text)
# resize
font = txt_editor.document().defaultFont()
fontMetrics = QFontMetrics(font)
textSize = fontMetrics.size(0, txt_editor.toPlainText())
w = textSize.width() + 10
h = textSize.height() + 10
txt_editor.setMinimumSize(w, h)
txt_editor.setMaximumSize(w, h)
txt_editor.resize(w, h)
interior.resize(w,900)
# txt_field.setSizePolicy(QSizePolicy.Policy.Preferred, QSizePolicy.Policy.MinimumExpanding)
# QBtn = QDialogButtonBox.StandardButton.Ok
# self.buttonBox = QDialogButtonBox(QBtn)
# self.buttonBox.accepted.connect(self.accept)
txt_editor.setText(text)
# txt_editor.verticalScrollBar()
interior.setWidget(txt_editor)
self.layout = QVBoxLayout()
self.setFixedSize(w, 900)
# self.layout.addWidget(txt_editor)
# self.layout.addStretch()
self.layout.addWidget(interior)
class ReportDatePicker(QDialog):
"""
custom dialog to ask for report start/stop dates
"""
def __init__(self) -> None:
super().__init__()
self.setWindowTitle("Select Report Date Range")
# make confirm/reject buttons
QBtn = QDialogButtonBox.StandardButton.Ok | QDialogButtonBox.StandardButton.Cancel
self.buttonBox = QDialogButtonBox(QBtn)
self.buttonBox.accepted.connect(self.accept)
self.buttonBox.rejected.connect(self.reject)
# widgets to ask for dates
start_date = QDateEdit(calendarPopup=True)
start_date.setDate(QDate.currentDate())
end_date = QDateEdit(calendarPopup=True)
@@ -233,46 +279,61 @@ class ReportDatePicker(QDialog):
class KitAdder(QWidget):
def __init__(self, parent_ctx:dict):
"""
dialog to get information to add kit
"""
def __init__(self, parent_ctx:dict) -> None:
super().__init__()
self.ctx = parent_ctx
self.grid = QGridLayout()
self.setLayout(self.grid)
# insert submit button at top
self.submit_btn = QPushButton("Submit")
self.grid.addWidget(self.submit_btn,0,0,1,1)
self.grid.addWidget(QLabel("Password:"),1,0)
self.grid.addWidget(QLineEdit(),1,1)
# need to exclude ordinary users to mitigate garbage database entries
# self.grid.addWidget(QLabel("Password:"),1,0)
# self.grid.addWidget(QLineEdit(),1,1)
self.grid.addWidget(QLabel("Kit Name:"),2,0)
self.grid.addWidget(QLineEdit(),2,1)
self.grid.addWidget(QLabel("Used For Sample Type:"),3,0)
used_for = QComboBox()
# Insert all existing sample types
used_for.addItems(lookup_all_sample_types(ctx=parent_ctx))
used_for.setEditable(True)
self.grid.addWidget(used_for,3,1)
# set cost per run
self.grid.addWidget(QLabel("Cost per run:"),4,0)
cost = QSpinBox()
cost.setMinimum(0)
cost.setMaximum(9999)
self.grid.addWidget(cost,4,1)
# button to add additional reagent types
self.add_RT_btn = QPushButton("Add Reagent Type")
self.grid.addWidget(self.add_RT_btn)
self.add_RT_btn.clicked.connect(self.add_RT)
self.submit_btn.clicked.connect(self.submit)
def add_RT(self):
def add_RT(self) -> None:
"""
insert new reagent type row
"""
maxrow = self.grid.rowCount()
self.grid.addWidget(ReagentTypeForm(parent_ctx=self.ctx), maxrow + 1,0,1,2)
def submit(self):
def submit(self) -> None:
"""
send kit to database
"""
# get form info
labels, values, reagents = self.extract_form_info(self)
info = {item[0]:item[1] for item in zip(labels, values)}
logger.debug(info)
# info['reagenttypes'] = reagents
# del info['name']
# del info['extension_of_life_(months)']
yml_type = {}
yml_type['password'] = info['password']
try:
yml_type['password'] = info['password']
except KeyError:
pass
used = info['used_for_sample_type'].replace(" ", "_").lower()
yml_type[used] = {}
yml_type[used]['kits'] = {}
@@ -280,9 +341,32 @@ class KitAdder(QWidget):
yml_type[used]['kits'][info['kit_name']]['cost'] = info['cost_per_run']
yml_type[used]['kits'][info['kit_name']]['reagenttypes'] = reagents
logger.debug(yml_type)
create_kit_from_yaml(ctx=self.ctx, exp=yml_type)
# send to kit constructor
result = create_kit_from_yaml(ctx=self.ctx, exp=yml_type)
# result = create_kit_from_yaml(ctx=self.ctx, exp=exp)
msg = QMessageBox()
# msg.setIcon(QMessageBox.critical)
match result['code']:
case 0:
msg.setText("Kit added")
msg.setInformativeText(result['message'])
msg.setWindowTitle("Kit added")
case 1:
msg.setText("Permission Error")
msg.setInformativeText(result['message'])
msg.setWindowTitle("Permission Error")
msg.exec()
def extract_form_info(self, object):
"""
retrieves arbitrary number of labels, values from form
Args:
object (_type_): the object to extract info from
Returns:
_type_: _description_
"""
labels = []
values = []
reagents = {}
@@ -317,26 +401,35 @@ class KitAdder(QWidget):
class ReagentTypeForm(QWidget):
"""
custom widget to add information about a new reagenttype
"""
def __init__(self, parent_ctx:dict) -> None:
super().__init__()
grid = QGridLayout()
self.setLayout(grid)
grid.addWidget(QLabel("Name:"),0,0)
reagent_getter = QComboBox()
# lookup all reagent type names from db
reagent_getter.addItems(get_all_reagenttype_names(ctx=parent_ctx))
reagent_getter.setEditable(True)
grid.addWidget(reagent_getter,0,1)
grid.addWidget(QLabel("Extension of Life (months):"),0,2)
# get extension of life
eol = QSpinBox()
eol.setMinimum(0)
grid.addWidget(eol, 0,3)
class ControlsDatePicker(QWidget):
"""
custom widget to pick start and end dates for controls graphs
"""
def __init__(self) -> None:
super().__init__()
self.start_date = QDateEdit(calendarPopup=True)
# start date is three month prior to end date by default
threemonthsago = QDate.currentDate().addDays(-90)
self.start_date.setDate(threemonthsago)
self.end_date = QDateEdit(calendarPopup=True)
@@ -350,5 +443,5 @@ class ControlsDatePicker(QWidget):
self.setLayout(self.layout)
self.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Fixed)
def sizeHint(self):
def sizeHint(self) -> QSize:
return QSize(80,20)

View File

@@ -13,14 +13,15 @@ def create_charts(ctx:dict, df:pd.DataFrame, ytitle:str|None=None) -> Figure:
Constructs figures based on parsed pandas dataframe.
Args:
settings (dict): settings passed down from click
settings (dict): settings passed down from gui
df (pd.DataFrame): input dataframe
group_name (str): controltype
Returns:
Figure: _description_
Figure: plotly figure
"""
from backend.excel import drop_reruns_from_df
# converts starred genera to normal and splits off list of starred
genera = []
if df.empty:
return None
@@ -35,15 +36,17 @@ def create_charts(ctx:dict, df:pd.DataFrame, ytitle:str|None=None) -> Figure:
df['genus'] = df['genus'].replace({'\*':''}, regex=True)
df['genera'] = genera
df = df.dropna()
# remove original runs, using reruns if applicable
df = drop_reruns_from_df(ctx=ctx, df=df)
# sort by and exclude from
sorts = ['submitted_date', "target", "genus"]
exclude = ['name', 'genera']
modes = [item for item in df.columns if item not in sorts and item not in exclude and "_hashes" not in item]
# Set descending for any columns that have "{mode}" in the header.
ascending = [False if item == "target" else True for item in sorts]
df = df.sort_values(by=sorts, ascending=ascending)
# actual chart construction is done by
fig = construct_chart(ctx=ctx, df=df, modes=modes, ytitle=ytitle)
return fig
@@ -186,7 +189,7 @@ def construct_chart(ctx:dict, df:pd.DataFrame, modes:list, ytitle:str|None=None)
def construct_refseq_chart(settings:dict, df:pd.DataFrame, group_name:str, mode:str) -> Figure:
"""
Constructs intial refseq chart for both contains and matches.
Constructs intial refseq chart for both contains and matches (depreciated).
Args:
settings (dict): settings passed down from click.
@@ -218,7 +221,7 @@ def construct_refseq_chart(settings:dict, df:pd.DataFrame, group_name:str, mode:
def construct_kraken_chart(settings:dict, df:pd.DataFrame, group_name:str, mode:str) -> Figure:
"""
Constructs intial refseq chart for each mode in the kraken config settings.
Constructs intial refseq chart for each mode in the kraken config settings. (depreciated)
Args:
settings (dict): settings passed down from click.