diff --git a/protected/biopet-gatk-pipelines/pom.xml b/protected/biopet-gatk-pipelines/pom.xml index f3a9ead7159eaaaccf28e80872cb20ebaea760a9..eec886f27f83120a2611f0b7330fa573587e61d1 100644 --- a/protected/biopet-gatk-pipelines/pom.xml +++ b/protected/biopet-gatk-pipelines/pom.xml @@ -44,5 +44,17 @@ <artifactId>Shiva</artifactId> <version>${project.version}</version> </dependency> + <dependency> + <groupId>org.testng</groupId> + <artifactId>testng</artifactId> + <version>6.8</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.scalatest</groupId> + <artifactId>scalatest_2.11</artifactId> + <version>2.2.1</version> + <scope>test</scope> + </dependency> </dependencies> </project> diff --git a/protected/biopet-gatk-pipelines/src/main/scala/nl/lumc/sasc/biopet/pipelines/gatk/ShivaVariantcallingGatk.scala b/protected/biopet-gatk-pipelines/src/main/scala/nl/lumc/sasc/biopet/pipelines/gatk/ShivaVariantcallingGatk.scala index 22551875c0a5175669fe4ecb5c3c036ec8c8859b..6dfeb9661f7d4006401ae8bb4da8018ccda320fd 100644 --- a/protected/biopet-gatk-pipelines/src/main/scala/nl/lumc/sasc/biopet/pipelines/gatk/ShivaVariantcallingGatk.scala +++ b/protected/biopet-gatk-pipelines/src/main/scala/nl/lumc/sasc/biopet/pipelines/gatk/ShivaVariantcallingGatk.scala @@ -13,19 +13,18 @@ class ShivaVariantcallingGatk(val root: Configurable) extends QScript with Shiva qscript => def this() = this(null) - override def callers = { + override def callersList = { new HaplotypeCallerGvcf :: new HaplotypeCallerAllele :: new UnifiedGenotyperAllele :: new UnifiedGenotyper :: new HaplotypeCaller :: - super.callers + super.callersList } class HaplotypeCaller extends Variantcaller { val name = "haplotypecaller" protected val defaultPrio = 1 - protected val defaultUse = true def outputFile = new File(outputDir, namePrefix + ".haplotypecaller.vcf.gz") @@ -40,7 +39,6 @@ class ShivaVariantcallingGatk(val root: Configurable) extends QScript with Shiva class UnifiedGenotyper extends Variantcaller { val name = "unifiedgenotyper" protected val defaultPrio = 20 - protected val defaultUse = false def outputFile = new File(outputDir, namePrefix + ".unifiedgenotyper.vcf.gz") @@ -55,7 +53,6 @@ class ShivaVariantcallingGatk(val root: Configurable) extends QScript with Shiva class HaplotypeCallerAllele extends Variantcaller { val name = "haplotypecaller_allele" protected val defaultPrio = 5 - protected val defaultUse = false def outputFile = new File(outputDir, namePrefix + ".haplotypecaller_allele.vcf.gz") @@ -72,7 +69,6 @@ class ShivaVariantcallingGatk(val root: Configurable) extends QScript with Shiva class UnifiedGenotyperAllele extends Variantcaller { val name = "unifiedgenotyper_allele" protected val defaultPrio = 9 - protected val defaultUse = false def outputFile = new File(outputDir, namePrefix + ".unifiedgenotyper_allele.vcf.gz") @@ -89,7 +85,6 @@ class ShivaVariantcallingGatk(val root: Configurable) extends QScript with Shiva class HaplotypeCallerGvcf extends Variantcaller { val name = "haplotypecaller_gvcf" protected val defaultPrio = 5 - protected val defaultUse = false def outputFile = new File(outputDir, namePrefix + ".haplotypecaller_gvcf.vcf.gz") diff --git a/protected/biopet-gatk-pipelines/src/test/scala/nl/lumc/sasc/biopet/pipelines/gatk/ShivaVariantcallingTest.scala b/protected/biopet-gatk-pipelines/src/test/scala/nl/lumc/sasc/biopet/pipelines/gatk/ShivaVariantcallingTest.scala new file mode 100644 index 0000000000000000000000000000000000000000..f20e929586fb0ce500ba71da2db5929b09c8a981 --- /dev/null +++ b/protected/biopet-gatk-pipelines/src/test/scala/nl/lumc/sasc/biopet/pipelines/gatk/ShivaVariantcallingTest.scala @@ -0,0 +1,119 @@ +package nl.lumc.sasc.biopet.pipelines.gatk + +import java.io.File + +import com.google.common.io.Files +import nl.lumc.sasc.biopet.core.config.Config +import nl.lumc.sasc.biopet.extensions.gatk.CombineVariants +import nl.lumc.sasc.biopet.extensions.gatk.broad.{UnifiedGenotyper, HaplotypeCaller} +import nl.lumc.sasc.biopet.tools.{VcfStats, MpileupToVcf, VcfFilter} +import nl.lumc.sasc.biopet.utils.ConfigUtils +import org.apache.commons.io.FileUtils +import org.broadinstitute.gatk.queue.QSettings +import org.scalatest.Matchers +import org.scalatest.testng.TestNGSuite +import org.testng.annotations.{DataProvider, Test, AfterClass} + +import scala.collection.mutable.ListBuffer + +/** + * Created by pjvan_thof on 3/2/15. + */ +class ShivaVariantcallingTest extends TestNGSuite with Matchers { + def initPipeline(map: Map[String, Any]): ShivaVariantcallingGatk = { + new ShivaVariantcallingGatk() { + override def configName = "shivavariantcalling" + override def globalConfig = new Config(ConfigUtils.mergeMaps(map, ShivaVariantcallingTest.config)) + qSettings = new QSettings + qSettings.runName = "test" + } + } + + @DataProvider(name = "shivaVariantcallingOptions") + def shivaVariantcallingOptions = { + val raw = Array(true, false) + val bcftools = Array(true, false) + val haplotypeCallerGvcf = Array(true, false) + val haplotypeCallerAllele = Array(true, false) + val unifiedGenotyperAllele = Array(true, false) + val unifiedGenotyper = Array(true, false) + val haplotypeCaller = Array(true, false) + + (for ( + bams <- 0 to 2; + raw <- raw; + bcftools <- bcftools; + haplotypeCallerGvcf <- haplotypeCallerGvcf; + haplotypeCallerAllele <- haplotypeCallerAllele; + unifiedGenotyperAllele <- unifiedGenotyperAllele; + unifiedGenotyper <- unifiedGenotyper; + haplotypeCaller <- haplotypeCaller + ) yield Array[Any](bams, raw, bcftools, unifiedGenotyper, haplotypeCaller, haplotypeCallerGvcf, haplotypeCallerAllele, unifiedGenotyperAllele) + ).toArray + } + + @Test(dataProvider = "shivaVariantcallingOptions") + def testShivaVariantcalling(bams: Int, + raw: Boolean, + bcftools: Boolean, + unifiedGenotyper: Boolean, + haplotypeCaller: Boolean, + haplotypeCallerGvcf: Boolean, + haplotypeCallerAllele: Boolean, + unifiedGenotyperAllele: Boolean) = { + val callers: ListBuffer[String] = ListBuffer() + if (raw) callers.append("raw") + if (bcftools) callers.append("bcftools") + if (unifiedGenotyper) callers.append("unifiedgenotyper") + if (haplotypeCallerGvcf) callers.append("haplotypecaller_gvcf") + if (haplotypeCallerAllele) callers.append("haplotypecaller_allele") + if (unifiedGenotyperAllele) callers.append("unifiedgenotyper_allele") + if (haplotypeCaller) callers.append("haplotypecaller") + val map = Map("variantcallers"-> callers.toList) + val pipeline = initPipeline(map) + + pipeline.inputBams = (for (n <- 1 to bams) yield new File("bam_" + n + ".bam")).toList + + val illegalArgumentException = pipeline.inputBams.isEmpty || + (!raw && !bcftools && + !haplotypeCaller && !unifiedGenotyper && + !haplotypeCallerGvcf && !haplotypeCallerAllele && !unifiedGenotyperAllele) + + if (illegalArgumentException) intercept[IllegalArgumentException] { + pipeline.script() + } + + if (!illegalArgumentException) { + pipeline.script() + + pipeline.functions.count(_.isInstanceOf[CombineVariants]) shouldBe 1 + (if (raw) 1 else 0) + //pipeline.functions.count(_.isInstanceOf[Bcftools]) shouldBe (if (bcftools) 1 else 0) + //FIXME: Can not check for bcftools because of piping + pipeline.functions.count(_.isInstanceOf[MpileupToVcf]) shouldBe (if (raw) bams else 0) + pipeline.functions.count(_.isInstanceOf[VcfFilter]) shouldBe (if (raw) bams else 0) + pipeline.functions.count(_.isInstanceOf[HaplotypeCaller]) shouldBe (if (haplotypeCaller) 1 else 0) + + (if (haplotypeCallerAllele) 1 else 0) + (if (haplotypeCallerGvcf) bams else 0) + pipeline.functions.count(_.isInstanceOf[UnifiedGenotyper]) shouldBe (if (unifiedGenotyper) 1 else 0) + + (if (unifiedGenotyperAllele) 1 else 0) + pipeline.functions.count(_.isInstanceOf[VcfStats]) shouldBe (1 + callers.size) + } + } + + @AfterClass def removeTempOutputDir() = { + FileUtils.deleteDirectory(ShivaVariantcallingTest.outputDir) + } +} + +object ShivaVariantcallingTest { + val outputDir = Files.createTempDir() + + val config = Map( + "name_prefix" -> "test", + "output_dir" -> outputDir, + "reference" -> "test", + "gatk_jar" -> "test", + "samtools" -> Map("exe" -> "test"), + "bcftools" -> Map("exe" -> "test"), + "input_alleles" -> "test" + ) +} \ No newline at end of file diff --git a/public/shiva/src/main/scala/nl/lumc/sasc/biopet/pipelines/shiva/ShivaVariantcallingTrait.scala b/public/shiva/src/main/scala/nl/lumc/sasc/biopet/pipelines/shiva/ShivaVariantcallingTrait.scala index d71379a9ff3d925f88991140bd831a2d6c7594e9..e8d530f00e63fe7587851130998d199e7dfdc409 100644 --- a/public/shiva/src/main/scala/nl/lumc/sasc/biopet/pipelines/shiva/ShivaVariantcallingTrait.scala +++ b/public/shiva/src/main/scala/nl/lumc/sasc/biopet/pipelines/shiva/ShivaVariantcallingTrait.scala @@ -38,10 +38,11 @@ trait ShivaVariantcallingTrait extends SummaryQScript with SampleLibraryTag { def finalFile = new File(outputDir, namePrefix + ".final.vcf.gz") def biopetScript: Unit = { - val callers = usedCallers.sortBy(_.prio) + val configCallers: Set[String] = config("variantcallers") + val callers = callersList.filter(x => configCallers.contains(x.name)).sortBy(_.prio) require(!inputBams.isEmpty, "No input bams found") - require(callers.exists(_.use), "must select atleast 1 variantcaller") + require(!callers.isEmpty, "must select atleast 1 variantcaller, possible to use: " + callersList.map(_.name).mkString(", ")) val cv = new CombineVariants(qscript) cv.outputFile = finalFile @@ -69,15 +70,11 @@ trait ShivaVariantcallingTrait extends SummaryQScript with SampleLibraryTag { addSummaryJobs } - def callers: List[Variantcaller] = List(new RawVcf, new Bcftools) - - def usedCallers: List[Variantcaller] = callers.filter(_.use) + protected def callersList: List[Variantcaller] = List(new RawVcf, new Bcftools) trait Variantcaller { val name: String def outputDir = new File(qscript.outputDir, name) - protected val defaultUse: Boolean - lazy val use: Boolean = config("use_" + name, default = defaultUse) protected val defaultPrio: Int lazy val prio: Int = config("prio_" + name, default = defaultPrio) def addJobs() @@ -87,7 +84,6 @@ trait ShivaVariantcallingTrait extends SummaryQScript with SampleLibraryTag { class Bcftools extends Variantcaller { val name = "bcftools" protected val defaultPrio = 8 - protected val defaultUse = true def outputFile = new File(outputDir, namePrefix + ".bcftools.vcf.gz") @@ -117,7 +113,6 @@ trait ShivaVariantcallingTrait extends SummaryQScript with SampleLibraryTag { class RawVcf extends Variantcaller { val name = "raw" protected val defaultPrio = 999 - protected val defaultUse = true def outputFile = new File(outputDir, namePrefix + ".raw.vcf.gz") @@ -154,5 +149,8 @@ trait ShivaVariantcallingTrait extends SummaryQScript with SampleLibraryTag { def summarySettings = Map() - def summaryFiles: Map[String, File] = usedCallers.map(x => (x.name -> x.outputFile)).toMap + ("final" -> finalFile) + def summaryFiles: Map[String, File] = { + val callers: Set[String] = config("variantcallers") + callersList.filter(x => callers.contains(x.name)).map(x => (x.name -> x.outputFile)).toMap + ("final" -> finalFile) + } } \ No newline at end of file diff --git a/public/shiva/src/test/scala/nl/lumc/sasc/biopet/pipelines/shiva/ShivaVariantcallingTest.scala b/public/shiva/src/test/scala/nl/lumc/sasc/biopet/pipelines/shiva/ShivaVariantcallingTest.scala index 8fa8d8badf6e88b3b1c309b37694a9a39425d67f..567bd022a99a7898db75b8b94866e0d1c9ff6f47 100644 --- a/public/shiva/src/test/scala/nl/lumc/sasc/biopet/pipelines/shiva/ShivaVariantcallingTest.scala +++ b/public/shiva/src/test/scala/nl/lumc/sasc/biopet/pipelines/shiva/ShivaVariantcallingTest.scala @@ -13,6 +13,8 @@ import org.scalatest.Matchers import org.scalatest.testng.TestNGSuite import org.testng.annotations.{ AfterClass, Test, DataProvider } +import scala.collection.mutable.ListBuffer + /** * Created by pjvan_thof on 3/2/15. */ @@ -36,17 +38,20 @@ class ShivaVariantcallingTest extends TestNGSuite with Matchers { raw <- raw; bcftools <- bcftools ) yield Array[Any](bams, raw, bcftools) - ).toArray + ).toArray } @Test(dataProvider = "shivaVariantcallingOptions") def testShivaVariantcalling(bams: Int, raw: Boolean, bcftools: Boolean) = { - val map = Map("use_raw" -> raw, "use_bcftools" -> bcftools) + val callers: ListBuffer[String] = ListBuffer() + if (raw) callers.append("raw") + if (bcftools) callers.append("bcftools") + val map = Map("variantcallers"-> callers.toList) val pipeline = initPipeline(map) pipeline.inputBams = (for (n <- 1 to bams) yield new File("bam_" + n + ".bam")).toList - val illegalArgumentException = pipeline.inputBams.isEmpty || !raw && !bcftools + val illegalArgumentException = pipeline.inputBams.isEmpty || (!raw && !bcftools) if (illegalArgumentException) intercept[IllegalArgumentException] { pipeline.script() @@ -66,7 +71,6 @@ class ShivaVariantcallingTest extends TestNGSuite with Matchers { @AfterClass def removeTempOutputDir() = { FileUtils.deleteDirectory(ShivaVariantcallingTest.outputDir) } - } object ShivaVariantcallingTest {