From 95c0f5ca18a3d6c4176e72b0c9daaf25a5e872b2 Mon Sep 17 00:00:00 2001
From: kylacochrane <Kyla.Cochrane@phac-aspc.gc.ca>
Date: Mon, 17 Jun 2024 12:58:54 -0400
Subject: [PATCH] Add gas parameter tests

---
 tests/pipelines/main_gm_threshold.nf.test | 135 ++++++++++++++++++++++
 workflows/gas_nomenclature.nf             |  22 +++-
 2 files changed, 156 insertions(+), 1 deletion(-)
 create mode 100644 tests/pipelines/main_gm_threshold.nf.test

diff --git a/tests/pipelines/main_gm_threshold.nf.test b/tests/pipelines/main_gm_threshold.nf.test
new file mode 100644
index 0000000..a060db7
--- /dev/null
+++ b/tests/pipelines/main_gm_threshold.nf.test
@@ -0,0 +1,135 @@
+nextflow_pipeline {
+
+    name "Integration Tests of adjusting gm_thresholds parameters"
+    script "main.nf"
+
+    test("Test fail pipeline if null threshold set") {
+        tag "pipeline_failure_null_threshold"
+
+        when {
+            params {
+                input = "$baseDir/tests/data/samplesheets/samplesheet1.csv"
+                outdir = "results"
+
+                gm_thresholds = null
+            }
+        }
+
+        then {
+            assert workflow.failed
+            assert workflow.stdout.contains("ERROR ~ --gm_thresholds null: Cannot pass null or empty string")
+        }
+    }
+
+    test("Test fail pipeline if empty threshold set") {
+        tag "pipeline_failure_no_threshold"
+
+        when {
+            params {
+                input = "$baseDir/tests/data/samplesheets/samplesheet1.csv"
+                outdir = "results"
+
+                gm_thresholds = ""
+            }
+        }
+
+        then {
+            assert workflow.failed
+            assert workflow.stdout.contains("ERROR ~ --gm_thresholds : Cannot pass null or empty string")
+        }
+    }
+
+    test("Test fail pipeline if negative threshold set") {
+        tag "pipeline_failure_negative_threshold"
+
+        when {
+            params {
+                input = "$baseDir/tests/data/samplesheets/samplesheet1.csv"
+                outdir = "results"
+
+                gm_thresholds = "-1"
+            }
+        }
+
+        then {
+            assert workflow.failed
+            assert workflow.stderr.contains('* --gm_thresholds: string [-1] does not match pattern ^(\\d+(\\.\\d+)?,)*\\d+(\\.\\d+)?$ (-1)')
+        }
+    }
+
+    test("Test fail pipeline if mismatch between thresholds and scaled distm") {
+        tag "pipeline_failure_threshold_scaled"
+
+        when {
+            params {
+                input = "$baseDir/tests/data/samplesheets/samplesheet1.csv"
+                outdir = "results"
+
+                gm_thresholds = "1,0.5,2"
+                pd_distm = "scaled"
+            }
+        }
+
+        then {
+            assert workflow.failed
+            assert workflow.stdout.contains("ERROR ~ '--pd_distm scaled' is set, but '--gm_thresholds 1,0.5,2' contains thresholds outside of range [0,1]."
+                                            + " Please either set '--pd_distm hamming' or adjust the threshold values.")
+        }
+    }
+
+    test("Test fail pipeline if mismatch between thresholds and hamming distm") {
+        tag "pipeline_failure_threshold_hamming"
+
+        when {
+            params {
+                input = "$baseDir/tests/data/samplesheets/samplesheet1.csv"
+                outdir = "results"
+
+                gm_thresholds = "2,1,0.5"
+                pd_distm = "hamming"
+            }
+        }
+
+        then {
+            assert workflow.failed
+            assert workflow.stdout.contains("ERROR ~ '--pd_distm hamming' is set, but '--gm_thresholds 2,1,0.5' contains fractions."
+                                            + " Please either set '--pd_distm scaled' or remove fractions from distance thresholds.")
+        }
+    }
+
+    test("Test pipeline with single threshold set to 1") {
+        tag "pipeline_thresh_1"
+
+        when {
+            params {
+                input = "$baseDir/tests/data/samplesheets/samplesheet1.csv"
+                outdir = "results"
+
+                gm_thresholds = "1"
+            }
+        }
+
+        then {
+            assert workflow.failed
+            assert (workflow.stdout =~ /Error \[1.0\]  supplied thresholds do not equal the number of threshold columns in reference_clusters.txt/).find()
+        }
+    }
+
+    test("Test pipeline with threshold set to 1,0") {
+        tag "pipeline_thresh_1.0"
+
+        when {
+            params {
+                input = "$baseDir/tests/data/samplesheets/samplesheet1.csv"
+                outdir = "results"
+
+                gm_thresholds = "1,0"
+            }
+        }
+
+        then {
+            assert workflow.failed
+            assert (workflow.stdout =~ /Error \[1.0, 0.0\]  supplied thresholds do not equal the number of threshold columns in reference_clusters.txt/).find()
+        }
+    }
+}
diff --git a/workflows/gas_nomenclature.nf b/workflows/gas_nomenclature.nf
index 6225f3a..b0b05bb 100644
--- a/workflows/gas_nomenclature.nf
+++ b/workflows/gas_nomenclature.nf
@@ -119,7 +119,27 @@ workflow GAS_NOMENCLATURE {
 
     expected_clusters = CLUSTER_FILE(clusters)
 
-    // GAS CALL
+    // GAS CALL processes
+
+    if(params.gm_thresholds == null || params.gm_thresholds == ""){
+        exit 1, "--gm_thresholds ${params.gm_thresholds}: Cannot pass null or empty string"
+    }
+
+    gm_thresholds_list = params.gm_thresholds.split(',')
+    if (params.pd_distm == 'hamming') {
+        if (gm_thresholds_list.any { it != null && it.contains('.') }) {
+            exit 1, ("'--pd_distm ${params.pd_distm}' is set, but '--gm_thresholds ${params.gm_thresholds}' contains fractions."
+                    + " Please either set '--pd_distm scaled' or remove fractions from distance thresholds.")
+        }
+    } else if (params.pd_distm == 'scaled') {
+        if (gm_thresholds_list.any { it != null && (it as Float < 0 || it as Float > 1) }) {
+            exit 1, ("'--pd_distm ${params.pd_distm}' is set, but '--gm_thresholds ${params.gm_thresholds}' contains thresholds outside of range [0,1]."
+                    + " Please either set '--pd_distm hamming' or adjust the threshold values.")
+        }
+    } else {
+        exit 1, "'--pd_distm ${params.pd_distm}' is an invalid value. Please set to either 'hamming' or 'scaled'."
+    }
+
     called_data = GAS_CALL(expected_clusters.text, distances.results)
     ch_versions = ch_versions.mix(called_data.versions)