From 0a2b1e6e498e06412044ba366b7f467fcf867005 Mon Sep 17 00:00:00 2001 From: Madan Mukundan <madan.mukundan@gmail.com> Date: Mon, 20 Dec 2021 17:40:53 +0100 Subject: [PATCH] fix: removed GUI and fixed tests --- src/plot_read_counts.py | 103 +-------------------------------- tests/test_plot_read_counts.py | 29 ++++++---- 2 files changed, 19 insertions(+), 113 deletions(-) diff --git a/src/plot_read_counts.py b/src/plot_read_counts.py index 20af36a..fa489c1 100644 --- a/src/plot_read_counts.py +++ b/src/plot_read_counts.py @@ -1,9 +1,7 @@ """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 diff --git a/tests/test_plot_read_counts.py b/tests/test_plot_read_counts.py index add8572..293b450 100644 --- a/tests/test_plot_read_counts.py +++ b/tests/test_plot_read_counts.py @@ -1,10 +1,12 @@ -"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 -- GitLab