Commit 59c78525 authored by bow's avatar bow
Browse files

Merge branch 'feature-more_unit_tests' into 'develop'

Adding more unit tests

Adding some random unit tests on this branch

Also fixes #338 

See merge request !391
parents 311c28ef 156dad59
......@@ -45,6 +45,18 @@
<artifactId>BiopetExtensions</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.scalatest</groupId>
<artifactId>scalatest_2.10</artifactId>
<version>2.2.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package nl.lumc.sasc.biopet.pipelines.bamtobigwig
import java.io.File
import java.nio.file.Paths
import org.scalatest.Matchers
import org.scalatest.testng.TestNGSuite
import org.testng.annotations.Test
import scala.io.Source
/**
* Created by pjvanthof on 09/05/16.
*/
class BamToChromSizesTest extends TestNGSuite with Matchers {
private def resourcePath(p: String): String = {
Paths.get(getClass.getResource(p).toURI).toString
}
@Test
def testChromSizes: Unit = {
val bamFile = new File(resourcePath("/empty.bam"))
val bamToChromSizes = new BamToChromSizes(null)
bamToChromSizes.bamFile = bamFile
bamToChromSizes.chromSizesFile = File.createTempFile("chrom.", ".sizes")
bamToChromSizes.chromSizesFile.deleteOnExit()
bamToChromSizes.run()
Source.fromFile(bamToChromSizes.chromSizesFile).getLines().toList shouldBe List("chrQ\t10000", "chrR\t10000")
}
}
......@@ -30,18 +30,16 @@ import scala.collection.mutable.ListBuffer
*/
object WriteDependencies extends Logging with Configurable {
val root: Configurable = null
private val functionNames: mutable.Map[QFunction, String] = mutable.Map()
private def createFunctionNames(functions: Seq[QFunction]): Unit = {
private def createFunctionNames(functions: Seq[QFunction]): Map[QFunction, String] = {
val cache: mutable.Map[String, Int] = mutable.Map()
for (function <- functions) {
(for (function <- functions) yield {
val baseName = function match {
case f: Configurable => f.configNamespace
case f => f.getClass.getSimpleName
}
cache += baseName -> (cache.getOrElse(baseName, 0) + 1)
functionNames += function -> s"$baseName-${cache(baseName)}"
}
function -> s"$baseName-${cache(baseName)}"
}).toMap
}
/**
......@@ -55,7 +53,7 @@ object WriteDependencies extends Logging with Configurable {
val errorOnMissingInput: Boolean = config("error_on_missing_input", false)
createFunctionNames(functions)
val functionNames = createFunctionNames(functions)
case class QueueFile(file: File) {
private val inputJobs: ListBuffer[QFunction] = ListBuffer()
......
package nl.lumc.sasc.biopet.core
import java.io.File
import java.nio.file.Files
import nl.lumc.sasc.biopet.utils.ConfigUtils
import org.broadinstitute.gatk.queue.function.QFunction
import org.scalatest.Matchers
import org.scalatest.testng.TestNGSuite
import org.testng.annotations.Test
import scala.io.Source
/**
* Created by pjvanthof on 09/05/16.
*/
class WriteDependenciesTest extends TestNGSuite with Matchers {
import WriteDependenciesTest._
case class Qfunc(in: Seq[File], out: Seq[File]) extends QFunction {
override def inputs = in
override def outputs = out
override def doneOutputs = out.map(x => new File(x.getParentFile, s".${x.getName}.done"))
override def failOutputs = out.map(x => new File(x.getParentFile, s".${x.getName}.fail"))
jobOutputFile = new File(out.head + ".out")
}
@Test
def testDeps: Unit = {
val outputFile = File.createTempFile("deps.", ".json")
outputFile.deleteOnExit()
val func1 = Qfunc(file1 :: Nil, file2 :: Nil)
val func2 = Qfunc(file2 :: Nil, file3 :: Nil)
WriteDependencies.writeDependencies(func1 :: func2 :: Nil, outputFile)
val deps = ConfigUtils.fileToConfigMap(outputFile)
deps("jobs") shouldBe a[Map[_, _]]
val jobs = deps("jobs").asInstanceOf[Map[String, Map[String, Any]]]
jobs.count(_._1.contains("Qfunc")) shouldBe 2
deps("files") shouldBe a[List[_]]
val files = deps("files").asInstanceOf[List[Map[String, Any]]]
val paths = files.map(x => x.get("path")).flatten
assert(paths.contains(file1.toString))
assert(paths.contains(file2.toString))
assert(paths.contains(file3.toString))
files.find(_.get("path") == Some(file1.toString)).flatMap(_.get("pipeline_input")) shouldBe Some(true)
files.find(_.get("path") == Some(file2.toString)).flatMap(_.get("pipeline_input")) shouldBe Some(false)
files.find(_.get("path") == Some(file3.toString)).flatMap(_.get("pipeline_input")) shouldBe Some(false)
}
}
object WriteDependenciesTest {
val tempDir = Files.createTempDirectory("test").toFile
tempDir.deleteOnExit()
val file1 = new File(tempDir, "file1.txt")
val file2 = new File(tempDir, "file2.txt")
val file3 = new File(tempDir, "file3.txt")
}
\ No newline at end of file
......@@ -61,7 +61,7 @@ class Summary(file: File) {
}
/** Executes given function for each sample */
def getSampleValues(function: (Summary, String) => Option[Any]): Map[String, Option[Any]] = {
def getSampleValues[T](function: (Summary, String) => Option[T]): Map[String, Option[T]] = {
(for (sample <- samples) yield sample -> function(this, sample)).toMap
}
......@@ -95,7 +95,7 @@ class Summary(file: File) {
* @param function Function to execute
* @return (sampleId, libId) -> value
*/
def getLibraryValues(function: (Summary, String, String) => Option[Any]): Map[(String, String), Option[Any]] = {
def getLibraryValues[T](function: (Summary, String, String) => Option[T]): Map[(String, String), Option[T]] = {
(for (sample <- samples; lib <- libraries.getOrElse(sample, Set())) yield {
(sample, lib) -> function(this, sample, lib)
}).toMap
......
......@@ -33,7 +33,6 @@ case class SummaryValue(value: Option[Any]) {
})
}
//TODO: Calculations are not yet used somewhere, needs more testing
def +(that: SummaryValue): SummaryValue = {
(this.value, that.value) match {
case (Some(a: Double), Some(b)) => SummaryValue(Some(a + b.toString.toDouble))
......
package nl.lumc.sasc.biopet.utils
import java.io.{ File, FileNotFoundException, PrintWriter }
import java.nio.file.Files
import org.scalatest.Matchers
import org.scalatest.testng.TestNGSuite
import org.testng.annotations.Test
import scala.io.Source
/**
* Created by pjvanthof on 05/05/16.
*/
class IoUtilsTest extends TestNGSuite with Matchers {
def createTempTestFile(file: File): Unit = {
file.getParentFile.mkdirs()
val writer = new PrintWriter(file)
writer.println("test")
writer.close()
file.deleteOnExit()
}
@Test
def testCopyFile: Unit = {
val temp1 = File.createTempFile("test.", ".txt")
temp1.deleteOnExit()
val temp2 = File.createTempFile("test.", ".txt")
temp2.deleteOnExit()
createTempTestFile(temp1)
IoUtils.copyFile(temp1, temp2)
val reader = Source.fromFile(temp2)
reader.getLines().toList shouldBe List("test")
reader.close()
}
@Test
def testCopyFileNonExistingDir: Unit = {
val temp1 = File.createTempFile("test.", ".txt")
val tempDir = new File(Files.createTempDirectory("test").toFile, "non-exist")
tempDir.deleteOnExit()
tempDir shouldNot exist
val temp2 = new File(tempDir, "test.txt")
createTempTestFile(temp1)
intercept[FileNotFoundException] {
IoUtils.copyFile(temp1, temp2)
}
IoUtils.copyFile(temp1, temp2, true)
val reader = Source.fromFile(temp2)
reader.getLines().toList shouldBe List("test")
reader.close()
}
@Test
def testCopyDir: Unit = {
val tempDir1 = Files.createTempDirectory("test").toFile
tempDir1.deleteOnExit()
val tempDir2 = Files.createTempDirectory("test").toFile
tempDir2.deleteOnExit()
val relativePaths: List[String] = List(
"test1.txt",
"test2.txt",
"dir1" + File.separator + "test1.txt",
"dir1" + File.separator + "test2.txt",
"dir2" + File.separator + "test1.txt",
"dir2" + File.separator + "test2.txt")
relativePaths.foreach { x =>
createTempTestFile(new File(tempDir1, x))
new File(tempDir2, x) shouldNot exist
}
IoUtils.copyDir(tempDir1, tempDir2)
relativePaths.foreach { x =>
val file = new File(tempDir2, x)
file should exist
val reader = Source.fromFile(file)
reader.getLines().toList shouldBe List("test")
reader.close()
}
}
}
package nl.lumc.sasc.biopet.utils.summary
import java.io.{ File, PrintWriter }
import nl.lumc.sasc.biopet.utils.ConfigUtils
import org.scalatest.Matchers
import org.scalatest.testng.TestNGSuite
import org.testng.annotations.Test
/**
* Created by pjvanthof on 06/05/16.
*/
class SummaryTest extends TestNGSuite with Matchers {
@Test
def testSamples: Unit = {
val summary = new Summary(SummaryTest.testSummaryFile)
summary.samples shouldBe Set("sample1")
}
@Test
def testLibraries: Unit = {
val summary = new Summary(SummaryTest.testSummaryFile)
summary.libraries shouldBe Map("sample1" -> Set("lib1"))
}
@Test
def testValue: Unit = {
val summary = new Summary(SummaryTest.testSummaryFile)
summary.getValue("key_1") shouldBe Some("test_1")
summary.getValue("key_x") shouldBe None
summary.getValue("samples", "sample1", "key_2") shouldBe Some("test_2")
summary.getValue("samples", "sample1", "libraries", "lib1", "key_3") shouldBe Some("test_3")
summary.getValue(Some("sample1"), None, "key_2") shouldBe Some("test_2")
summary.getValue(Some("sample1"), None, "key_x") shouldBe None
summary.getValue(Some("sample1"), Some("lib1"), "key_3") shouldBe Some("test_3")
summary.getValue(Some("sample1"), Some("lib1"), "key_x") shouldBe None
summary.getValue(None, None, "key_1") shouldBe Some("test_1")
summary.getValue(None, None, "key_x") shouldBe None
}
@Test
def testSampleValue: Unit = {
val summary = new Summary(SummaryTest.testSummaryFile)
summary.getSampleValue("sample1", "key_2") shouldBe Some("test_2")
summary.getSampleValue("sample1", "key_x") shouldBe None
summary.getSampleValue("samplex", "key_x") shouldBe None
}
@Test
def testSampleValues: Unit = {
val summary = new Summary(SummaryTest.testSummaryFile)
summary.getSampleValues("key_2") shouldBe Map("sample1" -> Some("test_2"))
summary.getSampleValues("key_x") shouldBe Map("sample1" -> None)
summary.getSampleValues((summary, sample) => summary.getSampleValue(sample, "key_2")) shouldBe Map("sample1" -> Some("test_2"))
}
@Test
def testLibraryValue: Unit = {
val summary = new Summary(SummaryTest.testSummaryFile)
summary.getLibraryValue("sample1", "lib1", "key_3") shouldBe Some("test_3")
summary.getLibraryValue("sample1", "lib1", "key_x") shouldBe None
summary.getLibraryValue("samplex", "libx", "key_x") shouldBe None
}
@Test
def testLibraryValues: Unit = {
val summary = new Summary(SummaryTest.testSummaryFile)
summary.getLibraryValues("key_3") shouldBe Map(("sample1", "lib1") -> Some("test_3"))
summary.getLibraryValues("key_x") shouldBe Map(("sample1", "lib1") -> None)
summary.getLibraryValues((summary, sample, lib) => summary.getLibraryValue(sample, lib, "key_3")) shouldBe Map(("sample1", "lib1") -> Some("test_3"))
}
}
object SummaryTest {
val testSummary = Map(
"key_1" -> "test_1",
"samples" -> Map(
"sample1" -> Map(
"key_2" -> "test_2",
"libraries" -> Map(
"lib1" -> Map("key_3" -> "test_3")
)
)
)
)
val testSummaryFile = File.createTempFile("summary.", ".json")
testSummaryFile.deleteOnExit()
val writer = new PrintWriter(testSummaryFile)
writer.println(ConfigUtils.mapToJson(testSummary).nospaces)
writer.close()
}
package nl.lumc.sasc.biopet.utils.summary
import java.io.{ File, PrintWriter }
import nl.lumc.sasc.biopet.utils.ConfigUtils
import org.scalatest.Matchers
import org.scalatest.testng.TestNGSuite
import org.testng.annotations.Test
/**
* Created by pjvanthof on 06/05/16.
*/
class SummaryValueTest extends TestNGSuite with Matchers {
@Test
def testConstructor: Unit = {
val summary = new Summary(SummaryValueTest.testSummaryFile)
new SummaryValue(None).value shouldBe None
new SummaryValue(Some(1)).value shouldBe Some(1)
new SummaryValue(List("key_1"), summary, None, None).value shouldBe Some("test_1")
new SummaryValue(List("key_2"), summary, Some("sample1"), None).value shouldBe Some("test_2")
new SummaryValue(List("key_3"), summary, Some("sample1"), Some("lib1")).value shouldBe Some("test_3")
}
@Test
def testPlus: Unit = {
new SummaryValue(Some(1.0)) + new SummaryValue(Some(1.0)) shouldBe new SummaryValue(Some(2.0))
new SummaryValue(Some(1.0)) + new SummaryValue(Some(1)) shouldBe new SummaryValue(Some(2.0))
new SummaryValue(Some(1)) + new SummaryValue(Some(1)) shouldBe new SummaryValue(Some(2))
new SummaryValue(Some("1")) + new SummaryValue(Some(1)) shouldBe new SummaryValue(Some(2.0))
new SummaryValue(Some("1")) + new SummaryValue(Some(1.0)) shouldBe new SummaryValue(Some(2.0))
new SummaryValue(None) + new SummaryValue(Some(1.0)) shouldBe new SummaryValue(None)
}
@Test
def testMin: Unit = {
new SummaryValue(Some(1.0)) - new SummaryValue(Some(1.0)) shouldBe new SummaryValue(Some(0.0))
new SummaryValue(Some(1.0)) - new SummaryValue(Some(1)) shouldBe new SummaryValue(Some(0.0))
new SummaryValue(Some(1)) - new SummaryValue(Some(1)) shouldBe new SummaryValue(Some(0))
new SummaryValue(Some("1")) - new SummaryValue(Some(1)) shouldBe new SummaryValue(Some(0.0))
new SummaryValue(Some("1")) - new SummaryValue(Some(1.0)) shouldBe new SummaryValue(Some(0.0))
new SummaryValue(None) - new SummaryValue(Some(1.0)) shouldBe new SummaryValue(None)
}
@Test
def testMultiply: Unit = {
new SummaryValue(Some(1.0)) * new SummaryValue(Some(2.0)) shouldBe new SummaryValue(Some(2.0))
new SummaryValue(Some(1.0)) * new SummaryValue(Some(2)) shouldBe new SummaryValue(Some(2.0))
new SummaryValue(Some(1)) * new SummaryValue(Some(2)) shouldBe new SummaryValue(Some(2))
new SummaryValue(Some("1")) * new SummaryValue(Some(2)) shouldBe new SummaryValue(Some(2.0))
new SummaryValue(Some("1")) * new SummaryValue(Some(2.0)) shouldBe new SummaryValue(Some(2.0))
new SummaryValue(None) * new SummaryValue(Some(2.0)) shouldBe new SummaryValue(None)
}
@Test
def testDivide: Unit = {
new SummaryValue(Some(2.0)) / new SummaryValue(Some(1.0)) shouldBe new SummaryValue(Some(2.0))
new SummaryValue(Some(2.0)) / new SummaryValue(Some(1)) shouldBe new SummaryValue(Some(2.0))
new SummaryValue(Some(2)) / new SummaryValue(Some(1)) shouldBe new SummaryValue(Some(2))
new SummaryValue(Some("2")) / new SummaryValue(Some(1)) shouldBe new SummaryValue(Some(2.0))
new SummaryValue(Some("2")) / new SummaryValue(Some(1.0)) shouldBe new SummaryValue(Some(2.0))
new SummaryValue(None) / new SummaryValue(Some(1.0)) shouldBe new SummaryValue(None)
}
@Test
def testLeft: Unit = {
new SummaryValue(Some(2.0)) % new SummaryValue(Some(1.0)) shouldBe new SummaryValue(Some(0))
new SummaryValue(Some(2.0)) % new SummaryValue(Some(1)) shouldBe new SummaryValue(Some(0))
new SummaryValue(Some(2)) % new SummaryValue(Some(1)) shouldBe new SummaryValue(Some(0))
new SummaryValue(Some("2")) % new SummaryValue(Some(1)) shouldBe new SummaryValue(Some(0))
new SummaryValue(Some("2")) % new SummaryValue(Some(1.0)) shouldBe new SummaryValue(Some(0))
new SummaryValue(None) % new SummaryValue(Some(1.0)) shouldBe new SummaryValue(None)
}
}
object SummaryValueTest {
val testSummary = Map(
"key_1" -> "test_1",
"samples" -> Map(
"sample1" -> Map(
"key_2" -> "test_2",
"libraries" -> Map(
"lib1" -> Map("key_3" -> "test_3")
)
)
)
)
val testSummaryFile = File.createTempFile("summary.", ".json")
testSummaryFile.deleteOnExit()
val writer = new PrintWriter(testSummaryFile)
writer.println(ConfigUtils.mapToJson(testSummary).nospaces)
writer.close()
}
......@@ -58,7 +58,7 @@ class Carp(val root: Configurable) extends QScript with MultisampleMappingTrait
override def makeSample(id: String) = new Sample(id)
class Sample(sampleId: String) extends super.Sample(sampleId) {
override def preProcessBam = Some(createFile(".filter.bam"))
override def preProcessBam = Some(createFile("filter.bam"))
val controls: List[String] = config("control", default = Nil)
......
......@@ -58,7 +58,9 @@ trait ShivaTestTrait extends TestNGSuite with Matchers {
def multisampleCalling: Boolean = true
def sampleCalling = false
def libraryCalling = false
def dbsnp: Boolean = true
def dbsnp = true
def svCalling = false
def annotation = false
@Test(dataProvider = "shivaOptions")
def testShiva(f: String, sample1: Boolean, sample2: Boolean,
......@@ -73,7 +75,9 @@ trait ShivaTestTrait extends TestNGSuite with Matchers {
"single_sample_variantcalling" -> sampleCalling,
"library_variantcalling" -> libraryCalling,
"use_indel_realigner" -> realign,
"use_base_recalibration" -> baseRecalibration), m)
"use_base_recalibration" -> baseRecalibration,
"sv_calling" -> svCalling,
"annotation" -> annotation), m)
}
......@@ -96,6 +100,21 @@ trait ShivaTestTrait extends TestNGSuite with Matchers {
pipeline.functions.count(_.isInstanceOf[BaseRecalibrator]) shouldBe (if (dbsnp && baseRecalibration) (numberLibs * 2) else 0)
pipeline.functions.count(_.isInstanceOf[PrintReads]) shouldBe (if (dbsnp && baseRecalibration) numberLibs else 0)
pipeline.summarySettings.get("annotation") shouldBe Some(annotation)
pipeline.summarySettings.get("sv_calling") shouldBe Some(svCalling)
pipeline.samples foreach {
case (sampleId, sample) =>
sample.summarySettings.get("single_sample_variantcalling") shouldBe Some(sampleCalling)
sample.summarySettings.get("use_indel_realigner") shouldBe Some(realign)
sample.libraries.foreach {
case (libId, lib) =>
lib.summarySettings.get("library_variantcalling") shouldBe Some(libraryCalling)
lib.summarySettings.get("use_indel_realigner") shouldBe Some(realign)
lib.summarySettings.get("use_base_recalibration") shouldBe Some(baseRecalibration && dbsnp)
}
}
pipeline.functions.count(_.isInstanceOf[VcfStats]) shouldBe (
(if (multisampleCalling) 2 else 0) +
(if (sampleCalling) numberSamples * 2 else 0) +
......@@ -125,6 +144,20 @@ class ShivaSampleCallingTest extends ShivaTestTrait {
override def baseRecalibrationProvider = Array(false)
override def sampleCalling = true
}
class ShivaWithSvCallingTest extends ShivaTestTrait {
override def sample1 = Array(true)
override def sample2 = Array(false)
override def realignProvider = Array(false)
override def baseRecalibrationProvider = Array(false)
override def svCalling = true
}
class ShivaWithAnnotationTest extends ShivaTestTrait {
override def sample1 = Array(true)
override def sample2 = Array(false)
override def realignProvider = Array(false)
override def baseRecalibrationProvider = Array(false)
override def annotation = true
}
object ShivaTest {
val outputDir = Files.createTempDir()
......@@ -170,7 +203,18 @@ object ShivaTest {
"wigtobigwig" -> Map("exe" -> "test"),
"md5sum" -> Map("exe" -> "test"),
"bgzip" -> Map("exe" -> "test"),
"tabix" -> Map("exe" -> "test")
"tabix" -> Map("exe" -> "test"),
"breakdancerconfig" -> Map("exe" -> "test"),
"breakdancercaller" -> Map("exe" -> "test"),
"pindelconfig" -> Map("exe" -> "test"),
"pindelcaller" -> Map("exe" -> "test"),
"pindelvcf" -> Map("exe" -> "test"),
"clever" -> Map("exe" -> "test"),
"delly" -> Map("exe" -> "test"),
"pysvtools" -> Map(
"exe" -> "test",
"exclusion_regions" -> "test",
"translocations_only" -> false)
)
val sample1 = Map(
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment