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 --- gn3/computations/biweight.R | 15 +++++++++++++++ gn3/computations/biweight.py | 22 ++++++++++++++++++++++ gn3/computations/correlations.py | 10 ++++++++-- 3 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 gn3/computations/biweight.R create mode 100644 gn3/computations/biweight.py (limited to 'gn3') diff --git a/gn3/computations/biweight.R b/gn3/computations/biweight.R new file mode 100644 index 0000000..d0d8de4 --- /dev/null +++ b/gn3/computations/biweight.R @@ -0,0 +1,15 @@ + +library(WGCNA) + + +myArgs <- commandArgs(trailingOnly = TRUE) +trait_vals <- as.numeric(unlist(strsplit(myArgs[1], split=" "))) +target_vals <- as.numeric(unlist(strsplit(myArgs[2], split=" "))) + +BiweightMidCorrelation <- function(trait_val,target_val){ + # results <- bicorAndPvalue(x,y) + return (list(c(results$bicor)[1],c(results$p)[1])) +} +cat(BiweightMidCorrelation(trait_vals,target_vals)) + + diff --git a/gn3/computations/biweight.py b/gn3/computations/biweight.py new file mode 100644 index 0000000..c17de8e --- /dev/null +++ b/gn3/computations/biweight.py @@ -0,0 +1,22 @@ + + +"""module contains script to call biweight mid\ +correlation in R""" + +import subprocess +from typing import List + + +def call_biweight_script(trait_vals: List, + target_vals: List, + path_to_script: str = "./biweight_R", + command: str = "Rscript" + ): + '''biweight function''' + args_1 = ' '.join(str(trait_val) for trait_val in trait_vals) + args_2 = ' '.join(str(target_val) for target_val in target_vals) + cmd = [command, path_to_script] + [args_1] + [args_2] + + results = subprocess.check_output(cmd, universal_newlines=True) + + return tuple([float(y) for y in results.split()]) diff --git a/gn3/computations/correlations.py b/gn3/computations/correlations.py index f0ce502..89d37fc 100644 --- a/gn3/computations/correlations.py +++ b/gn3/computations/correlations.py @@ -7,6 +7,7 @@ from typing import Optional from typing import Callable import scipy.stats +from gn3.computations.biweight import call_biweight_script def map_shared_keys_to_values(target_sample_keys: List, @@ -99,8 +100,13 @@ def do_bicor(x_val, y_val) -> Tuple[float, float]: package :not packaged in guix """ - x_val, y_val = 0, 0 - return (x_val, y_val) + + try: + results = call_biweight_script(x_val, y_val) + except Exception as error: + raise error + + return results def filter_shared_sample_keys(this_samplelist, -- cgit v1.2.3 From f9dec15fb4a1fc24dfb963ed69f2a5cbbd4f31d3 Mon Sep 17 00:00:00 2001 From: Alexander Kabui Date: Mon, 14 Jun 2021 13:50:21 +0300 Subject: fix missing call to bicorAndPvalue --- gn3/computations/biweight.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'gn3') diff --git a/gn3/computations/biweight.R b/gn3/computations/biweight.R index d0d8de4..05f7145 100644 --- a/gn3/computations/biweight.R +++ b/gn3/computations/biweight.R @@ -7,7 +7,7 @@ trait_vals <- as.numeric(unlist(strsplit(myArgs[1], split=" "))) target_vals <- as.numeric(unlist(strsplit(myArgs[2], split=" "))) BiweightMidCorrelation <- function(trait_val,target_val){ - # results <- bicorAndPvalue(x,y) + results <- bicorAndPvalue(x,y) return (list(c(results$bicor)[1],c(results$p)[1])) } cat(BiweightMidCorrelation(trait_vals,target_vals)) -- cgit v1.2.3 From c708f435f14e5394aa4e6e8f556aec976d61341f Mon Sep 17 00:00:00 2001 From: Alexander Kabui Date: Tue, 15 Jun 2021 08:08:38 +0300 Subject: fix script path issues --- gn3/computations/biweight.R | 19 ++++++++++++++++--- gn3/computations/biweight.py | 8 ++++++-- 2 files changed, 22 insertions(+), 5 deletions(-) (limited to 'gn3') diff --git a/gn3/computations/biweight.R b/gn3/computations/biweight.R index 05f7145..650c5fd 100644 --- a/gn3/computations/biweight.R +++ b/gn3/computations/biweight.R @@ -1,5 +1,5 @@ -library(WGCNA) +# library(WGCNA) myArgs <- commandArgs(trailingOnly = TRUE) @@ -7,9 +7,22 @@ trait_vals <- as.numeric(unlist(strsplit(myArgs[1], split=" "))) target_vals <- as.numeric(unlist(strsplit(myArgs[2], split=" "))) BiweightMidCorrelation <- function(trait_val,target_val){ - results <- bicorAndPvalue(x,y) + results <- bicorAndPvalue(trait_val,target_val) return (list(c(results$bicor)[1],c(results$p)[1])) } -cat(BiweightMidCorrelation(trait_vals,target_vals)) + + + + +# the idea is that you get the entire dataset in any format +# and then do ther correlation + +ComputeAll <-function(trait_val,target_dataset) { + for target_val in target_dataset { + results = BiweightMidCorrelation(trait_val,target_val) + cat(BiweightMidCorrelation(trait_vals,target_vals)) + } +} + diff --git a/gn3/computations/biweight.py b/gn3/computations/biweight.py index c17de8e..e598a5b 100644 --- a/gn3/computations/biweight.py +++ b/gn3/computations/biweight.py @@ -2,17 +2,21 @@ """module contains script to call biweight mid\ correlation in R""" - import subprocess +import os +from pathlib import Path from typing import List +FILE_PATH = os.path.join(Path(__file__).parent.absolute(), "biweight.R") + def call_biweight_script(trait_vals: List, target_vals: List, - path_to_script: str = "./biweight_R", + path_to_script: str = FILE_PATH, command: str = "Rscript" ): '''biweight function''' + args_1 = ' '.join(str(trait_val) for trait_val in trait_vals) args_2 = ' '.join(str(target_val) for target_val in target_vals) cmd = [command, path_to_script] + [args_1] + [args_2] -- cgit v1.2.3 From 88e3bd10a2481e6c00c454a6bf2c60448ada363c Mon Sep 17 00:00:00 2001 From: Alexander Kabui Date: Wed, 16 Jun 2021 08:44:56 +0300 Subject: add fetch args function --- gn3/computations/biweight.R | 31 ++++++++++++------------------- 1 file changed, 12 insertions(+), 19 deletions(-) (limited to 'gn3') diff --git a/gn3/computations/biweight.R b/gn3/computations/biweight.R index 650c5fd..8730536 100644 --- a/gn3/computations/biweight.R +++ b/gn3/computations/biweight.R @@ -1,28 +1,21 @@ -# library(WGCNA) +library(WGCNA) +FetchArgs <- function(){ + myArgs <- commandArgs(trailingOnly = TRUE) + trait_vals <- as.numeric(unlist(strsplit(myArgs[1], split=" "))) + target_vals <- as.numeric(unlist(strsplit(myArgs[2], split=" "))) -myArgs <- commandArgs(trailingOnly = TRUE) -trait_vals <- as.numeric(unlist(strsplit(myArgs[1], split=" "))) -target_vals <- as.numeric(unlist(strsplit(myArgs[2], split=" "))) + return(list(trait_vals= c(trait_vals),target_vals = c(target_vals))) -BiweightMidCorrelation <- function(trait_val,target_val){ - results <- bicorAndPvalue(trait_val,target_val) - return (list(c(results$bicor)[1],c(results$p)[1])) } +BiweightMidCorrelation <- function(trait_val,target_val){ + results <- bicorAndPvalue(c(trait_val),c(target_val)) + return ((c(c(results$bicor)[1],c(results$p)[1]))) +} +results <- (BiweightMidCorrelation(FetchArgs()[1],FetchArgs()[2])) - - -# the idea is that you get the entire dataset in any format -# and then do ther correlation - -ComputeAll <-function(trait_val,target_dataset) { - for target_val in target_dataset { - results = BiweightMidCorrelation(trait_val,target_val) - cat(BiweightMidCorrelation(trait_vals,target_vals)) - } -} - +cat(results) \ No newline at end of file -- cgit v1.2.3 From c0931bda121c1b39e1ae2adc7bfa49c2ca207a4d Mon Sep 17 00:00:00 2001 From: Alexander Kabui Date: Wed, 16 Jun 2021 13:41:28 +0300 Subject: refactor script and add tests --- gn3/computations/biweight.R | 14 ++++++++------ gn3/computations/test_biweight.R | 28 ++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 6 deletions(-) create mode 100644 gn3/computations/test_biweight.R (limited to 'gn3') diff --git a/gn3/computations/biweight.R b/gn3/computations/biweight.R index 8730536..e424360 100644 --- a/gn3/computations/biweight.R +++ b/gn3/computations/biweight.R @@ -1,10 +1,11 @@ library(WGCNA) -FetchArgs <- function(){ - myArgs <- commandArgs(trailingOnly = TRUE) - trait_vals <- as.numeric(unlist(strsplit(myArgs[1], split=" "))) - target_vals <- as.numeric(unlist(strsplit(myArgs[2], split=" "))) +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))) @@ -13,9 +14,10 @@ BiweightMidCorrelation <- function(trait_val,target_val){ results <- bicorAndPvalue(c(trait_val),c(target_val)) return ((c(c(results$bicor)[1],c(results$p)[1]))) + } -results <- (BiweightMidCorrelation(FetchArgs()[1],FetchArgs()[2])) +parsed_values <- ParseArgs(arg_values) -cat(results) \ No newline at end of file +cat((BiweightMidCorrelation(parsed_values[1],parsed_values[2]))) \ No newline at end of file diff --git a/gn3/computations/test_biweight.R b/gn3/computations/test_biweight.R new file mode 100644 index 0000000..149c8b6 --- /dev/null +++ b/gn3/computations/test_biweight.R @@ -0,0 +1,28 @@ +library(testthat) +source("./biweight.R", chdir = TRUE) + +test_that("sum of vector", { + results <- sum(c(1,2)) + expect_equal(results, 3) +}) + + + +test + +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 -- cgit v1.2.3 From bc7d84077a3e99f8fd54f2e0050ac4bad8a36b01 Mon Sep 17 00:00:00 2001 From: Alexander Kabui Date: Wed, 16 Jun 2021 14:03:28 +0300 Subject: minor fix --- gn3/computations/test_biweight.R | 3 --- 1 file changed, 3 deletions(-) (limited to 'gn3') diff --git a/gn3/computations/test_biweight.R b/gn3/computations/test_biweight.R index 149c8b6..2269120 100644 --- a/gn3/computations/test_biweight.R +++ b/gn3/computations/test_biweight.R @@ -7,9 +7,6 @@ test_that("sum of vector", { }) - -test - test_that("biweight results"),{ vec_1 = c(1,2,3,4) vec_2 = c(1,2,3,4) -- cgit v1.2.3 From b566a8950864a373f95ae2914b862e9846297b2d Mon Sep 17 00:00:00 2001 From: Alexander Kabui Date: Wed, 16 Jun 2021 14:07:24 +0300 Subject: add r-wgna package --- gn3/computations/test_biweight.R | 8 ++++---- guix.scm | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) (limited to 'gn3') diff --git a/gn3/computations/test_biweight.R b/gn3/computations/test_biweight.R index 2269120..599e733 100644 --- a/gn3/computations/test_biweight.R +++ b/gn3/computations/test_biweight.R @@ -8,16 +8,16 @@ test_that("sum of vector", { test_that("biweight results"),{ - vec_1 = c(1,2,3,4) - vec_2 = c(1,2,3,4) + vec_1 <- c(1,2,3,4) + vec_2 <- c(1,2,3,4) - results = BiweightMidCorrelation(vec_1,vec_2) + 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") + my_args <- c("1 2 3 4","5 6 7 8") results <- ParseArgs(my_args) expect_equal(results[1],c(1,2,3,4)) diff --git a/guix.scm b/guix.scm index bf700d0..d41600d 100644 --- a/guix.scm +++ b/guix.scm @@ -89,7 +89,7 @@ ("r-qtl" ,r-qtl) ("r-testthat" ,r-testthat) ("r-optparse" ,r-optparse) - ; ("r-wgcna" ,r-wgcna) + ("r-wgcna" ,r-wgcna) )) (build-system python-build-system) (home-page "https://github.com/genenetwork/genenetwork3") -- 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 'gn3') 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 From 10140ab707021dd2dffb1b439f52a62e3d59c29a Mon Sep 17 00:00:00 2001 From: Alexander Kabui Date: Sun, 20 Jun 2021 09:15:00 +0300 Subject: minor fix docstring --- gn3/computations/biweight.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'gn3') diff --git a/gn3/computations/biweight.py b/gn3/computations/biweight.py index f5eecb2..6d031ad 100644 --- a/gn3/computations/biweight.py +++ b/gn3/computations/biweight.py @@ -12,7 +12,7 @@ def calculate_biweight_corr(trait_vals: List, path_to_script: str = BIWEIGHT_RSCRIPT, command: str = "Rscript" ) -> Tuple[float, float]: - '''biweight function''' + """biweight function""" args_1 = ' '.join(str(trait_val) for trait_val in trait_vals) args_2 = ' '.join(str(target_val) for target_val in target_vals) -- cgit v1.2.3