aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBonfaceKilz2021-06-21 08:58:27 +0300
committerGitHub2021-06-21 08:58:27 +0300
commitf949189dc727976a1574a57d3b0e895ff6598d07 (patch)
treee7e0634176d55afefa25467652b4f97601287837
parentd653a635d0efd2291754c18f51d31f91a1c0a25c (diff)
parent10140ab707021dd2dffb1b439f52a62e3d59c29a (diff)
downloadgenenetwork3-f949189dc727976a1574a57d3b0e895ff6598d07.tar.gz
Merge pull request #20 from genenetwork/feature/biweight-correlation
add biweight r script and tests
-rw-r--r--gn3/computations/biweight.py25
-rw-r--r--gn3/computations/correlations.py9
-rw-r--r--gn3/settings.py6
-rw-r--r--guix.scm4
-rw-r--r--scripts/calculate_biweight.R42
-rw-r--r--tests/unit/computations/test_biweight.py21
-rw-r--r--tests/unit/computations/test_correlation.py6
7 files changed, 107 insertions, 6 deletions
diff --git a/gn3/computations/biweight.py b/gn3/computations/biweight.py
new file mode 100644
index 0000000..6d031ad
--- /dev/null
+++ b/gn3/computations/biweight.py
@@ -0,0 +1,25 @@
+"""module contains script to call biweight midcorrelation in R"""
+import subprocess
+
+from typing import List
+from typing import Tuple
+
+from gn3.settings import BIWEIGHT_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)
+ 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)
+
+ (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 0fe46ab..bc738a7 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 calculate_biweight_corr
def map_shared_keys_to_values(target_sample_keys: List,
@@ -99,8 +100,12 @@ 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 = calculate_biweight_corr(x_val, y_val)
+ return results
+ except Exception as error:
+ raise error
def filter_shared_sample_keys(this_samplelist,
diff --git a/gn3/settings.py b/gn3/settings.py
index ecfd502..770ba3d 100644
--- a/gn3/settings.py
+++ b/gn3/settings.py
@@ -14,9 +14,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/guix.scm b/guix.scm
index d20c7f0..104dc51 100644
--- a/guix.scm
+++ b/guix.scm
@@ -30,6 +30,7 @@
(gnu packages cran)
(gnu packages databases)
(gnu packages statistics)
+ (gnu packages bioconductor)
(gnu packages python)
(gnu packages python-check)
(gnu packages python-crypto)
@@ -89,7 +90,8 @@
("r-optparse" ,r-optparse)
("r-stringi" ,r-stringi)
("r-stringr" ,r-stringr)
- ; ("r-wgcna" ,r-wgcna)
+ ("r-testthat" ,r-testthat)
+ ("r-wgcna" ,r-wgcna)
))
(build-system python-build-system)
(home-page "https://github.com/genenetwork/genenetwork3")
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
new file mode 100644
index 0000000..ad404f1
--- /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 calculate_biweight_corr
+
+
+class TestBiweight(TestCase):
+ """test class for biweight"""
+
+ @mock.patch("gn3.computations.biweight.subprocess.check_output")
+ 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 = 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 5746adf..b1bc6ef 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.calculate_biweight_corr")
+ 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")