Skip to content
Snippets Groups Projects
Commit 0a2b1e6e authored by Madan Mukundan's avatar Madan Mukundan
Browse files

fix: removed GUI and fixed tests

parent 31381637
Branches
No related tags found
1 merge request!26Issue14 prc
Pipeline #13853 failed
"""Plots read counts of gene expression data."""
import logging
import sys
from pathlib import Path
from tkinter import Tk, ttk, filedialog, StringVar
from typing import Union
import pandas
......@@ -15,7 +13,7 @@ import matplotlib.pyplot as plt
def plot_read_counts(
dir_path: Union[str, Path] = None,
gene_id: str = None
) -> None:
) -> bool:
"""Plots histogram of gene ID and read counts from .csv files.
This function reads .csv files from a directory formatted
......@@ -118,101 +116,4 @@ def plot_read_counts(
plt.bar(range(len(gene_count_dict)), counts, tick_label=file_names)
plt.title('Counts for %s' % (gene_id))
plt.show()
def prc_dialog() -> None:
"""Creates a GUI to select directory and enter gene."""
# Create dialog window
dialog = Tk()
dialog.title("Plot Gene Read Counts")
dialog.resizable(False, False)
dialog.geometry("380x210+20+20")
dialog.configure(background="black")
# Create and attach welcome message to GUI
wlcom_text = "Welcome to Plot Read Counts "
wlcom_msg = ttk.Label(dialog,
text=wlcom_text,
font=("Helvetica", 12, "bold"),
foreground="white",
background="black",
padding=5)
wlcom_msg.grid(column=1, row=0, columnspan=2, pady=5)
# Create and attach instruction message to GUI
inst_msg = ttk.Label(dialog,
text="Select a directory of .csv files and a gene ID to plot",
font=("Helvetica", 12),
foreground="white",
background="black",
padding=5)
inst_msg.grid(column=1, row=1, columnspan=2, padx=10, pady=5)
# Create and attach directory selection elements
# Either globals are needed or all functions
# need to be encapsulated
global dir_str_var
dir_str_var = StringVar()
dir_lbl = ttk.Label(dialog,
textvariable=dir_str_var,
font=("Courier", 10, "bold"),
background="black",
foreground="green")
dir_lbl.grid(column=1, row=4, padx=5, pady=1, sticky="e")
dir_btn = ttk.Button(dialog, text="Select Directory",
command=lambda: get_dir())
dir_btn.grid(column=1, row=6, padx=5, pady=1)
# Create and attach gene selection elements
global gene_lbl
gene_lbl = StringVar()
gene_entry = ttk.Entry(dialog)
gene_entry.grid(column=2, row=5, pady=1)
gene_entry_lbl = ttk.Label(anchor="center",
textvariable=gene_lbl,
font=("Courier", 10, "bold"),
background="black",
foreground="green")
gene_entry_lbl.grid(column=2, row=4, pady=1)
gene_entry_btn = ttk.Button(dialog,
text="Confirm Gene (Case Sensitive)",
command=lambda: get_gene_id(gene_entry))
gene_entry_btn.grid(column=2, row=6, padx=2, pady=2)
# Create and attach confirm and close element
kill_btn = ttk.Button(dialog, text="Exit and Plot",
command=lambda: dialog.destroy())
kill_btn.grid(column=1, row=8, columnspan=2, padx=5, pady=15)
dialog.mainloop()
if 'gui_path' in globals() and 'gui_gene_id' in globals():
plot_read_counts(gui_path, gui_gene_id)
else:
sys.exit()
def get_dir() -> Path:
"""Support function to retrieve dir from GUI."""
global gui_path
gui_path = Path(filedialog.askdirectory(
title='Please select the directory containing \
gene reads'))
short_dir = ("%s/.../%s/" % (gui_path.drive, gui_path.name))
dir_str_var.set(short_dir)
def get_gene_id(gene_entry: StringVar) -> str:
"""Support function to retrieve gene ID from GUI."""
global gui_gene_id
gui_gene_id = gene_entry.get()
gene_lbl.set(gene_entry.get())
def main() -> None:
"""Main opens GUI for dir and gene ID selection."""
prc_dialog()
if __name__ == '__main__':
main()
return True
"Test for plot_read_counts module"
"""Test for plot_read_counts module."""
import pytest
from pathlib import Path
from src.plot_read_counts import plot_read_counts
class TestPlotReadCounts ():
"""Tests for plot_read_counts."""
@pytest.mark.parametrize(
"dir_path, gene_id, expected",
......@@ -13,20 +15,23 @@ class TestPlotReadCounts ():
('Path.home()', 5, TypeError),
(None, None, ValueError),
('Path.home()', 'GENE1', AttributeError),
(Path(str(Path.parent())+"/tests/resources/"), 'GENE25', ValueError),
]
)
def test_invalid_input(self, dir_path, gene_id, expected):
"""Tests invalid input."""
with pytest.raises(expected):
plot_read_counts(dir_path, gene_id)
def validate_input():
pass
#plot_dir = PurePath('H:/My Drive/PhD/PLS/PLS scRNAseq Repo/scrna-seq-simulation/tests/resources')
#plot_dir = 'H:/My Drive/PhD/PLS scRNAseq Repo/scrna-seq-simulation/'
# plot_dir = PurePath(filedialog.askdirectory(title='Please select the directory containing \
# gene reads'))
# gene_of_interest = input('\nPlease type a gene ID to sample: ').upper()
#gene_of_interest = 'GENE1'
\ No newline at end of file
@pytest.mark.parametrize(
"dir_path, gene_id, expected",
[
(Path(str(Path.parent())+"/tests/resources/"), 'GENE1', True),
(Path(str(Path.parent())+"/tests/resources/"), 'GENE1', True),
(Path(str(Path.parent())+"/tests/resources/"), 'GENE7', True),
]
)
def validate_input(self, dir_path, gene_id):
"""Tests valid input with return value boolean."""
assert plot_read_counts(dir_path, gene_id) is True
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment