From 57a6af689e85e5cbfe0a825d5b84ed9c451e6ad7 Mon Sep 17 00:00:00 2001 From: Alexander Kabui Date: Mon, 14 Jun 2021 13:36:40 +0300 Subject: add biweight r script and tests --- tests/unit/computations/test_biweight.py | 21 +++++++++++++++++++++ tests/unit/computations/test_correlation.py | 6 ++++-- 2 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 tests/unit/computations/test_biweight.py (limited to 'tests') diff --git a/tests/unit/computations/test_biweight.py b/tests/unit/computations/test_biweight.py new file mode 100644 index 0000000..3045f78 --- /dev/null +++ b/tests/unit/computations/test_biweight.py @@ -0,0 +1,21 @@ +"""test for biweight script""" +from unittest import TestCase +from unittest import mock + +from gn3.computations.biweight import call_biweight_script + + +class TestBiweight(TestCase): + """test class for biweight""" + + @mock.patch("gn3.computations.biweight.subprocess.check_output") + def test_call_biweight_script(self, mock_check_output): + """test for call_biweight_script func""" + mock_check_output.return_value = "0.1 0.5" + results = call_biweight_script(command="Rscript", + path_to_script="./r_script.R", + trait_vals=[ + 1.2, 1.1, 1.9], + target_vals=[1.9, 0.4, 1.1]) + + self.assertEqual(results, (0.1, 0.5)) diff --git a/tests/unit/computations/test_correlation.py b/tests/unit/computations/test_correlation.py index 5746adf..606f96d 100644 --- a/tests/unit/computations/test_correlation.py +++ b/tests/unit/computations/test_correlation.py @@ -98,12 +98,14 @@ class TestCorrelation(TestCase): self.assertEqual(results, expected_results) - def test_bicor(self): + @mock.patch("gn3.computations.correlations.call_biweight_script") + def test_bicor(self, mock_biweight): """Test for doing biweight mid correlation """ + mock_biweight.return_value = (1.0, 0.0) results = do_bicor(x_val=[1, 2, 3], y_val=[4, 5, 6]) - self.assertEqual(results, (0.0, 0.0) + self.assertEqual(results, (1.0, 0.0) ) @mock.patch("gn3.computations.correlations.compute_corr_coeff_p_value") -- cgit v1.2.3 From 864f231bbdbe2c3b3c96b0158a15811ac7790c3f Mon Sep 17 00:00:00 2001 From: Alexander Kabui Date: Sun, 20 Jun 2021 09:04:55 +0300 Subject: make requested changes to biweight --- gn3/computations/biweight.R | 23 ---------------- gn3/computations/biweight.py | 25 +++++++++-------- gn3/computations/correlations.py | 7 +++-- gn3/computations/test_biweight.R | 25 ----------------- gn3/settings.py | 6 ++++- scripts/calculate_biweight.R | 42 +++++++++++++++++++++++++++++ tests/unit/computations/test_biweight.py | 16 +++++------ tests/unit/computations/test_correlation.py | 2 +- 8 files changed, 71 insertions(+), 75 deletions(-) delete mode 100644 gn3/computations/biweight.R delete mode 100644 gn3/computations/test_biweight.R create mode 100644 scripts/calculate_biweight.R (limited to 'tests') diff --git a/gn3/computations/biweight.R b/gn3/computations/biweight.R deleted file mode 100644 index e424360..0000000 --- a/gn3/computations/biweight.R +++ /dev/null @@ -1,23 +0,0 @@ - -library(WGCNA) - -arg_values <- commandArgs(trailingOnly = TRUE) -ParseArgs <- function(args){ - - trait_vals <- as.numeric(unlist(strsplit(args[1], split=" "))) - target_vals <- as.numeric(unlist(strsplit(args[2], split=" "))) - - return(list(trait_vals= c(trait_vals),target_vals = c(target_vals))) - -} -BiweightMidCorrelation <- function(trait_val,target_val){ - - results <- bicorAndPvalue(c(trait_val),c(target_val)) - return ((c(c(results$bicor)[1],c(results$p)[1]))) - -} - - -parsed_values <- ParseArgs(arg_values) - -cat((BiweightMidCorrelation(parsed_values[1],parsed_values[2]))) \ No newline at end of file diff --git a/gn3/computations/biweight.py b/gn3/computations/biweight.py index e598a5b..f5eecb2 100644 --- a/gn3/computations/biweight.py +++ b/gn3/computations/biweight.py @@ -1,20 +1,17 @@ - - -"""module contains script to call biweight mid\ -correlation in R""" +"""module contains script to call biweight midcorrelation in R""" import subprocess -import os -from pathlib import Path + from typing import List +from typing import Tuple -FILE_PATH = os.path.join(Path(__file__).parent.absolute(), "biweight.R") +from gn3.settings import BIWEIGHT_RSCRIPT -def call_biweight_script(trait_vals: List, - target_vals: List, - path_to_script: str = FILE_PATH, - command: str = "Rscript" - ): +def calculate_biweight_corr(trait_vals: List, + target_vals: List, + path_to_script: str = BIWEIGHT_RSCRIPT, + command: str = "Rscript" + ) -> Tuple[float, float]: '''biweight function''' args_1 = ' '.join(str(trait_val) for trait_val in trait_vals) @@ -23,4 +20,6 @@ def call_biweight_script(trait_vals: List, results = subprocess.check_output(cmd, universal_newlines=True) - return tuple([float(y) for y in results.split()]) + (corr_coeff, p_val) = tuple([float(y) for y in results.split()]) + + return (corr_coeff, p_val) diff --git a/gn3/computations/correlations.py b/gn3/computations/correlations.py index 89d37fc..eae7ae4 100644 --- a/gn3/computations/correlations.py +++ b/gn3/computations/correlations.py @@ -7,7 +7,7 @@ from typing import Optional from typing import Callable import scipy.stats -from gn3.computations.biweight import call_biweight_script +from gn3.computations.biweight import calculate_biweight_corr def map_shared_keys_to_values(target_sample_keys: List, @@ -102,12 +102,11 @@ package :not packaged in guix """ try: - results = call_biweight_script(x_val, y_val) + results = calculate_biweight_corr(x_val, y_val) + return results except Exception as error: raise error - return results - def filter_shared_sample_keys(this_samplelist, target_samplelist) -> Tuple[List, List]: diff --git a/gn3/computations/test_biweight.R b/gn3/computations/test_biweight.R deleted file mode 100644 index 599e733..0000000 --- a/gn3/computations/test_biweight.R +++ /dev/null @@ -1,25 +0,0 @@ -library(testthat) -source("./biweight.R", chdir = TRUE) - -test_that("sum of vector", { - results <- sum(c(1,2)) - expect_equal(results, 3) -}) - - -test_that("biweight results"),{ - vec_1 <- c(1,2,3,4) - vec_2 <- c(1,2,3,4) - - results <- BiweightMidCorrelation(vec_1,vec_2) - expect_equal(c(1.0,0.0),results) -} - - -test_that("parsing args "),{ - my_args <- c("1 2 3 4","5 6 7 8") - results <- ParseArgs(my_args) - - expect_equal(results[1],c(1,2,3,4)) - expect_equal(results[2],c(5,6,7,8)) -} \ No newline at end of file diff --git a/gn3/settings.py b/gn3/settings.py index 2057ce1..bde856d 100644 --- a/gn3/settings.py +++ b/gn3/settings.py @@ -13,9 +13,13 @@ TMPDIR = os.environ.get("TMPDIR", tempfile.gettempdir()) RQTL_WRAPPER = "rqtl_wrapper.R" # SQL confs -SQL_URI = os.environ.get("SQL_URI", "mysql://webqtlout:webqtlout@localhost/db_webqtl") +SQL_URI = os.environ.get( + "SQL_URI", "mysql://webqtlout:webqtlout@localhost/db_webqtl") SECRET_KEY = "password" SQLALCHEMY_TRACK_MODIFICATIONS = False # gn2 results only used in fetching dataset info GN2_BASE_URL = "http://www.genenetwork.org/" + +# biweight script +BIWEIGHT_RSCRIPT = "~/genenetwork3/script/calculate_biweight.R" diff --git a/scripts/calculate_biweight.R b/scripts/calculate_biweight.R new file mode 100644 index 0000000..bad93cb --- /dev/null +++ b/scripts/calculate_biweight.R @@ -0,0 +1,42 @@ + +library(testthat) +library(WGCNA) + +arg_values <- commandArgs(trailingOnly = TRUE) +ParseArgs <- function(args){ + + trait_vals <- as.numeric(unlist(strsplit(args[1], split=" "))) + target_vals <- as.numeric(unlist(strsplit(args[2], split=" "))) + + return(list(trait_vals= c(trait_vals),target_vals = c(target_vals))) + +} +BiweightMidCorrelation <- function(trait_val,target_val){ + + results <- bicorAndPvalue(c(trait_val),c(target_val)) + return ((c(c(results$bicor)[1],c(results$p)[1]))) + +} + + + +test_that("biweight results"),{ + vec_1 <- c(1,2,3,4) + vec_2 <- c(1,2,3,4) + + results <- BiweightMidCorrelation(vec_1,vec_2) + expect_equal(c(1.0,0.0),results) +} + + +test_that("parsing args "),{ + my_args <- c("1 2 3 4","5 6 7 8") + results <- ParseArgs(my_args) + + expect_equal(results[1],c(1,2,3,4)) + expect_equal(results[2],c(5,6,7,8)) +} + +parsed_values <- ParseArgs(arg_values) + +cat((BiweightMidCorrelation(parsed_values[1],parsed_values[2]))) \ No newline at end of file diff --git a/tests/unit/computations/test_biweight.py b/tests/unit/computations/test_biweight.py index 3045f78..ad404f1 100644 --- a/tests/unit/computations/test_biweight.py +++ b/tests/unit/computations/test_biweight.py @@ -2,20 +2,20 @@ from unittest import TestCase from unittest import mock -from gn3.computations.biweight import call_biweight_script +from gn3.computations.biweight import calculate_biweight_corr class TestBiweight(TestCase): """test class for biweight""" @mock.patch("gn3.computations.biweight.subprocess.check_output") - def test_call_biweight_script(self, mock_check_output): - """test for call_biweight_script func""" + def test_calculate_biweight_corr(self, mock_check_output): + """test for calculate_biweight_corr func""" mock_check_output.return_value = "0.1 0.5" - results = call_biweight_script(command="Rscript", - path_to_script="./r_script.R", - trait_vals=[ - 1.2, 1.1, 1.9], - target_vals=[1.9, 0.4, 1.1]) + results = calculate_biweight_corr(command="Rscript", + path_to_script="./r_script.R", + trait_vals=[ + 1.2, 1.1, 1.9], + target_vals=[1.9, 0.4, 1.1]) self.assertEqual(results, (0.1, 0.5)) diff --git a/tests/unit/computations/test_correlation.py b/tests/unit/computations/test_correlation.py index 606f96d..b1bc6ef 100644 --- a/tests/unit/computations/test_correlation.py +++ b/tests/unit/computations/test_correlation.py @@ -98,7 +98,7 @@ class TestCorrelation(TestCase): self.assertEqual(results, expected_results) - @mock.patch("gn3.computations.correlations.call_biweight_script") + @mock.patch("gn3.computations.correlations.calculate_biweight_corr") def test_bicor(self, mock_biweight): """Test for doing biweight mid correlation """ mock_biweight.return_value = (1.0, 0.0) -- cgit v1.2.3