Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chores/ctl wgcna enhancement #87

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions gn3/computations/ctl.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from gn3.commands import run_cmd

from gn3.computations.wgcna import dump_wgcna_data
from gn3.computations.wgcna import compose_wgcna_cmd
from gn3.computations.wgcna import compose_rscript_cmd
from gn3.computations.wgcna import process_image

from gn3.settings import TMPDIR
Expand All @@ -13,7 +13,7 @@ def call_ctl_script(data):
"""function to call ctl script"""
data["imgDir"] = TMPDIR
temp_file_name = dump_wgcna_data(data)
cmd = compose_wgcna_cmd("ctl_analysis.R", temp_file_name)
cmd = compose_rscript_cmd("ctl_analysis.R", temp_file_name)

cmd_results = run_cmd(cmd)
with open(temp_file_name, "r", encoding="utf-8") as outputfile:
Expand Down
13 changes: 8 additions & 5 deletions gn3/computations/wgcna.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@


from gn3.settings import TMPDIR
from gn3.settings import R_SCRIPTS
from gn3.commands import run_cmd


Expand Down Expand Up @@ -55,17 +56,19 @@ def process_image(image_loc: str) -> bytes:
return b""


def compose_wgcna_cmd(rscript_path: str, temp_file_path: str):
"""function to componse wgcna cmd"""
# (todo):issue relative paths to abs paths
cmd = f"Rscript ./scripts/{rscript_path} {temp_file_path}"
def compose_rscript_cmd(script_path: str,
file_name: str,
temp_file_path: str):

cmd = f'"Rscript {os.path.join(script_path,file_name)} {temp_file_path}"'
return cmd


def call_wgcna_script(rscript_path: str, request_data: dict):
"""function to call wgcna script"""
generated_file = dump_wgcna_data(request_data)
cmd = compose_wgcna_cmd(rscript_path, generated_file)

cmd = compose_rscript_cmd(rscript_path, generated_file)

# stream_cmd_output(request_data, cmd) disable streaming of data

Expand Down
5 changes: 4 additions & 1 deletion gn3/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import tempfile
import os
import pathlib

BCRYPT_SALT = "$2b$12$mxLvu9XRLlIaaSeDxt8Sle" # Change this!
DATA_DIR = ""
Expand All @@ -24,7 +25,9 @@

GN2_BASE_URL = "http://www.genenetwork.org/"

# wgcna script
# R script
R_SCRIPTS = pathlib.Path("./scripts/").absolute()

WGCNA_RSCRIPT = "wgcna_analysis.R"
# qtlreaper command
REAPER_COMMAND = f"{os.environ.get('GUIX_ENVIRONMENT')}/bin/qtlreaper"
Expand Down
44 changes: 33 additions & 11 deletions tests/unit/computations/test_wgcna.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
"""module contains python code for wgcna"""

from unittest import TestCase
from unittest import mock

import pytest

import os.path

from gn3.computations.wgcna import dump_wgcna_data
from gn3.computations.wgcna import compose_wgcna_cmd
from gn3.computations.wgcna import compose_rscript_cmd
from gn3.computations.wgcna import call_wgcna_script
from gn3.settings import R_SCRIPTS


class TestWgcna(TestCase):
Expand All @@ -15,7 +19,7 @@ class TestWgcna(TestCase):
@pytest.mark.unit_test
@mock.patch("gn3.computations.wgcna.process_image")
@mock.patch("gn3.computations.wgcna.run_cmd")
@mock.patch("gn3.computations.wgcna.compose_wgcna_cmd")
@mock.patch("gn3.computations.wgcna.compose_rscript_cmd")
@mock.patch("gn3.computations.wgcna.dump_wgcna_data")
def test_call_wgcna_script(self,
mock_dumping_data,
Expand Down Expand Up @@ -100,7 +104,7 @@ def test_call_wgcna_script(self,

@pytest.mark.unit_test
@mock.patch("gn3.computations.wgcna.run_cmd")
@mock.patch("gn3.computations.wgcna.compose_wgcna_cmd")
@mock.patch("gn3.computations.wgcna.compose_rscript_cmd")
@mock.patch("gn3.computations.wgcna.dump_wgcna_data")
def test_call_wgcna_script_fails(self, mock_dumping_data, mock_compose_wgcna, mock_run_cmd):
"""test for calling wgcna script\
Expand All @@ -122,16 +126,20 @@ def test_call_wgcna_script_fails(self, mock_dumping_data, mock_compose_wgcna, mo
"input_file.R", ""), expected_error)

@pytest.mark.unit_test
def test_compose_wgcna_cmd(self):
def test_compose_rscript_cmd(self):
"""test for composing wgcna cmd"""
wgcna_cmd = compose_wgcna_cmd(
"wgcna.r", "/tmp/wgcna.json")
self.assertEqual(
wgcna_cmd, "Rscript ./scripts/wgcna.r /tmp/wgcna.json")

@pytest.mark.unit_test
@mock.patch("gn3.computations.wgcna.TMPDIR", "/tmp")
@mock.patch("gn3.computations.wgcna.uuid.uuid4")
expected_cmd = '"Rscript /home/scripts/test_script.R tmp/wgcna.json"'

cmd = compose_rscript_cmd(script_path="/home/scripts",
file_name="test_script.R",
temp_file_path="tmp/wgcna.json")

self.assertEqual(cmd, expected_cmd)

@ pytest.mark.unit_test
@ mock.patch("gn3.computations.wgcna.TMPDIR", "/tmp")
@ mock.patch("gn3.computations.wgcna.uuid.uuid4")
def test_create_json_file(self, file_name_generator):
"""test for writing the data to a csv file"""
# # All the traits we have data for (should not contain duplicates)
Expand Down Expand Up @@ -170,3 +178,17 @@ def test_create_json_file(self, file_name_generator):

self.assertEqual(
results, "/tmp/facb73ff-7eef-4053-b6ea-e91d3a22a00c.json")

def test_if_scripts_file_exists(self):
"""check if certain script files exists"""

files_data = [
("wgcna_analysis.R", True),
("ctl_analysis.R", True),
("control_experiment.R", False)
]

for file_name, file_exists in files_data:
file_path = os.path.join(R_SCRIPTS, file_name)
self.assertIs(os.path.exists(file_path), file_exists,
f"the file {file_path} doesn't exist in path")