aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Kabui2021-06-14 13:36:40 +0300
committerAlexander Kabui2021-06-14 13:36:40 +0300
commit57a6af689e85e5cbfe0a825d5b84ed9c451e6ad7 (patch)
treec151d405b40186d00d99a42aee715f441b25b6fc
parent9d6af6049fa73c6aae4d224245d319e87bccbd6a (diff)
downloadgenenetwork3-57a6af689e85e5cbfe0a825d5b84ed9c451e6ad7.tar.gz
add biweight r script and tests
-rw-r--r--gn3/computations/biweight.R15
-rw-r--r--gn3/computations/biweight.py22
-rw-r--r--gn3/computations/correlations.py10
-rw-r--r--tests/unit/computations/test_biweight.py21
-rw-r--r--tests/unit/computations/test_correlation.py6
5 files changed, 70 insertions, 4 deletions
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,
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")