diff --git a/public/java/src/org/broadinstitute/sting/utils/R/RScriptExecutor.java b/public/java/src/org/broadinstitute/sting/utils/R/RScriptExecutor.java index 868ea89b5..d5e9dd9b3 100644 --- a/public/java/src/org/broadinstitute/sting/utils/R/RScriptExecutor.java +++ b/public/java/src/org/broadinstitute/sting/utils/R/RScriptExecutor.java @@ -32,6 +32,7 @@ import org.broadinstitute.sting.commandline.ArgumentCollection; import org.broadinstitute.sting.gatk.walkers.recalibration.Covariate; import org.broadinstitute.sting.utils.PathUtils; import org.broadinstitute.sting.utils.Utils; +import org.broadinstitute.sting.utils.exceptions.UserException; import java.io.File; import java.io.IOException; @@ -53,11 +54,11 @@ public class RScriptExecutor { public static class RScriptArgumentCollection { @Advanced @Argument(fullName = "path_to_Rscript", shortName = "Rscript", doc = "The path to your implementation of Rscript. For Broad users this is maybe /broad/tools/apps/R-2.6.0/bin/Rscript", required = false) - private String PATH_TO_RSCRIPT = "Rscript"; + public String PATH_TO_RSCRIPT = "Rscript"; @Advanced @Argument(fullName = "path_to_resources", shortName = "resources", doc = "Path to resources folder holding the Sting R scripts.", required = false) - private List PATH_TO_RESOURCES = Arrays.asList("public/R/", "private/R/"); + public List PATH_TO_RESOURCES = Arrays.asList("public/R/", "private/R/"); } final RScriptArgumentCollection myArgs; @@ -98,7 +99,7 @@ public class RScriptExecutor { } } - generateException("Couldn't find script: " + scriptName + " in " + myArgs.PATH_TO_RSCRIPT); + generateException("Couldn't find script: " + scriptName + " in " + myArgs.PATH_TO_RESOURCES); return null; } @@ -112,7 +113,7 @@ public class RScriptExecutor { private void generateException(String msg, Throwable e) { if ( exceptOnError ) - throw new RuntimeException(msg, e); + throw new UserException(msg, e); else logger.warn(msg + (e == null ? "" : ":" + e.getMessage())); } diff --git a/public/java/test/org/broadinstitute/sting/utils/R/RScriptExecutorUnitTest.java b/public/java/test/org/broadinstitute/sting/utils/R/RScriptExecutorUnitTest.java new file mode 100644 index 000000000..1bbf74db9 --- /dev/null +++ b/public/java/test/org/broadinstitute/sting/utils/R/RScriptExecutorUnitTest.java @@ -0,0 +1,106 @@ +/* + * Copyright (c) 2011, The Broad Institute + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following + * conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + */ + +package org.broadinstitute.sting.utils.R; + +import org.apache.commons.io.FileUtils; +import org.broadinstitute.sting.BaseTest; +import org.broadinstitute.sting.gatk.walkers.diffengine.DiffElement; +import org.broadinstitute.sting.gatk.walkers.diffengine.DiffEngine; +import org.broadinstitute.sting.gatk.walkers.diffengine.DiffNode; +import org.broadinstitute.sting.gatk.walkers.diffengine.Difference; +import org.broadinstitute.sting.utils.exceptions.UserException; +import org.testng.Assert; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +/** + * Basic unit test for RScriptExecutor in reduced reads + */ +public class RScriptExecutorUnitTest extends BaseTest { + final static String testrscript = "print(\"hello, world\")\n"; + final static String publicRScript = "plot_Tranches.R"; + final static String privateRScript = "variantCallQC.R"; + + // -------------------------------------------------------------------------------- + // + // Difference testing routines + // + // -------------------------------------------------------------------------------- + + private void testOne(String script, String pathToRscript, String anotherSearchPath, boolean exceptOnError) { + RScriptExecutor.RScriptArgumentCollection collection = + new RScriptExecutor.RScriptArgumentCollection(); + if ( pathToRscript != null ) + collection.PATH_TO_RSCRIPT = pathToRscript; + if ( anotherSearchPath != null ) { + List x = new ArrayList(collection.PATH_TO_RESOURCES); + x.add(anotherSearchPath); + collection.PATH_TO_RESOURCES = x; + } + RScriptExecutor executor = new RScriptExecutor(collection, exceptOnError); + executor.callRScripts(script); + } + + @Test + public void testPublic() { testOne(publicRScript, null, null, true); } + + @Test + public void testPrivate() { testOne(privateRScript, null, null, true); } + + // make sure we don't break finding something in private by adding another directory + @Test + public void testPrivateWithAdditionalPath1() { testOne(privateRScript, null, "dist", true); } + + // make sure we don't break finding something in private by adding another directory + @Test + public void testPrivateWithAdditionalPath2() { testOne(privateRScript, null, "doesNotExist", true); } + + @Test(expectedExceptions = UserException.class) + public void testNonExistantScriptException() { testOne("does_not_exist.R", null, null, true); } + + @Test() + public void testNonExistantScriptNoException() { testOne("does_not_exist.R", null, null, false); } + + @Test(expectedExceptions = UserException.class) + public void testNonExistantRScriptException() { testOne(publicRScript, "badRScriptValue", null, true); } + + @Test() + public void testNonExistantRScriptNoException() { testOne(publicRScript, "badRScriptValue", null, false); } + + @Test() + public void testScriptInNewPath() throws IOException { + File t = createTempFile("myTestScript", ".R"); + FileUtils.writeStringToFile(t, testrscript); + testOne(t.getName(), null, t.getParent(), true); + } +} \ No newline at end of file