ShivaVariantcallingTest.scala 8.49 KB
Newer Older
bow's avatar
bow committed
1
/**
Peter van 't Hof's avatar
Peter van 't Hof committed
2 3 4 5
 * Due to the license issue with GATK, this part of Biopet can only be used inside the
 * LUMC. Please refer to https://git.lumc.nl/biopet/biopet/wikis/home for instructions
 * on how to use this protected part of biopet or contact us at sasc@lumc.nl
 */
Peter van 't Hof's avatar
Peter van 't Hof committed
6
package nl.lumc.sasc.biopet.pipelines.shiva
7

Peter van 't Hof's avatar
Peter van 't Hof committed
8
import java.io.{ File, FileOutputStream }
Peter van 't Hof's avatar
Peter van 't Hof committed
9 10

import com.google.common.io.Files
Peter van 't Hof's avatar
Peter van 't Hof committed
11
import nl.lumc.sasc.biopet.core.BiopetPipe
Peter van 't Hof's avatar
Peter van 't Hof committed
12
import nl.lumc.sasc.biopet.extensions.Freebayes
Peter van 't Hof's avatar
Peter van 't Hof committed
13
import nl.lumc.sasc.biopet.extensions.bcftools.{ BcftoolsCall, BcftoolsMerge }
14
import nl.lumc.sasc.biopet.utils.config.Config
Peter van 't Hof's avatar
Peter van 't Hof committed
15
import nl.lumc.sasc.biopet.extensions.gatk.CombineVariants
Peter van 't Hof's avatar
Peter van 't Hof committed
16 17
import nl.lumc.sasc.biopet.extensions.gatk.broad.{ HaplotypeCaller, UnifiedGenotyper }
import nl.lumc.sasc.biopet.extensions.tools.{ MpileupToVcf, VcfFilter, VcfStats }
18
import nl.lumc.sasc.biopet.pipelines.shiva.ShivaVariantcalling
Peter van 't Hof's avatar
Peter van 't Hof committed
19
import nl.lumc.sasc.biopet.utils.ConfigUtils
20
import org.apache.commons.io.FileUtils
Peter van 't Hof's avatar
Peter van 't Hof committed
21 22 23
import org.broadinstitute.gatk.queue.QSettings
import org.scalatest.Matchers
import org.scalatest.testng.TestNGSuite
Peter van 't Hof's avatar
Peter van 't Hof committed
24
import org.testng.annotations.{ AfterClass, DataProvider, Test }
Peter van 't Hof's avatar
Peter van 't Hof committed
25

Peter van 't Hof's avatar
Peter van 't Hof committed
26 27
import scala.collection.mutable.ListBuffer

Peter van 't Hof's avatar
Peter van 't Hof committed
28
/**
Peter van 't Hof's avatar
Peter van 't Hof committed
29 30 31 32
 * Class for testing ShivaVariantcalling
 *
 * Created by pjvan_thof on 3/2/15.
 */
Peter van 't Hof's avatar
Peter van 't Hof committed
33
trait ShivaVariantcallingTestTrait extends TestNGSuite with Matchers {
Peter van 't Hof's avatar
Peter van 't Hof committed
34
  def initPipeline(map: Map[String, Any]): ShivaVariantcalling = {
35
    new ShivaVariantcalling() {
Sander Bollen's avatar
Sander Bollen committed
36
      override def configNamespace = "shivavariantcalling"
Peter van 't Hof's avatar
Peter van 't Hof committed
37 38 39 40 41 42
      override def globalConfig = new Config(ConfigUtils.mergeMaps(map, ShivaVariantcallingTest.config))
      qSettings = new QSettings
      qSettings.runName = "test"
    }
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
43 44 45 46 47 48 49 50 51 52 53
  def raw  : Boolean = false
  def bcftools  : Boolean = false
  def bcftools_singlesample  : Boolean = false
  def haplotypeCallerGvcf  : Boolean = false
  def haplotypeCallerAllele  : Boolean = false
  def unifiedGenotyperAllele  : Boolean = false
  def unifiedGenotyper  : Boolean = false
  def haplotypeCaller  : Boolean = false
  def freebayes  : Boolean = false
  def varscanCnsSinglesample  : Boolean = false

Peter van 't Hof's avatar
Peter van 't Hof committed
54
  @DataProvider(name = "shivaVariantcallingOptions")
Peter van 't Hof's avatar
Peter van 't Hof committed
55
  def shivaVariantcallingOptions = {
Sander van der Zeeuw's avatar
Sander van der Zeeuw committed
56
    (for (
57
      bams <- 0 to 2;
Peter van 't Hof's avatar
Peter van 't Hof committed
58 59 60
    ) yield Array[Any](bams, raw, bcftools, bcftools_singlesample, unifiedGenotyper,
      haplotypeCaller, haplotypeCallerGvcf, haplotypeCallerAllele, unifiedGenotyperAllele,
      freebayes, varscanCnsSinglesample)
Peter van 't Hof's avatar
Peter van 't Hof committed
61
    ).toArray
Peter van 't Hof's avatar
Peter van 't Hof committed
62 63 64
  }

  @Test(dataProvider = "shivaVariantcallingOptions")
65 66 67
  def testShivaVariantcalling(bams: Int,
                              raw: Boolean,
                              bcftools: Boolean,
68
                              bcftoolsSinglesample: Boolean,
69 70 71 72 73
                              unifiedGenotyper: Boolean,
                              haplotypeCaller: Boolean,
                              haplotypeCallerGvcf: Boolean,
                              haplotypeCallerAllele: Boolean,
                              unifiedGenotyperAllele: Boolean,
Peter van 't Hof's avatar
Peter van 't Hof committed
74
                              freebayes: Boolean,
75
                              varscanCnsSinglesample: Boolean) = {
Peter van 't Hof's avatar
Peter van 't Hof committed
76 77 78
    val callers: ListBuffer[String] = ListBuffer()
    if (raw) callers.append("raw")
    if (bcftools) callers.append("bcftools")
79
    if (bcftoolsSinglesample) callers.append("bcftools_singlesample")
80 81 82 83 84
    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")
Peter van 't Hof's avatar
Peter van 't Hof committed
85
    if (freebayes) callers.append("freebayes")
86
    if (varscanCnsSinglesample) callers.append("varscan_cns_singlesample")
87
    val map = Map("variantcallers" -> callers.toList)
Peter van 't Hof's avatar
Peter van 't Hof committed
88 89
    val pipeline = initPipeline(map)

90
    pipeline.inputBams = (for (n <- 1 to bams) yield n.toString -> ShivaVariantcallingTest.inputTouch("bam_" + n + ".bam")).toMap
Peter van 't Hof's avatar
Peter van 't Hof committed
91

Peter van 't Hof's avatar
Peter van 't Hof committed
92
    val illegalArgumentException = pipeline.inputBams.isEmpty || (callers.isEmpty)
Peter van 't Hof's avatar
Peter van 't Hof committed
93 94 95 96 97 98 99 100

    if (illegalArgumentException) intercept[IllegalArgumentException] {
      pipeline.script()
    }

    if (!illegalArgumentException) {
      pipeline.script()

Peter van 't Hof's avatar
Peter van 't Hof committed
101 102
      val pipesJobs = pipeline.functions.filter(_.isInstanceOf[BiopetPipe]).flatMap(_.asInstanceOf[BiopetPipe].pipesJobs)

Peter van 't Hof's avatar
Peter van 't Hof committed
103
      pipeline.functions.count(_.isInstanceOf[CombineVariants]) shouldBe (1 + (if (raw) 1 else 0) + (if (varscanCnsSinglesample) 1 else 0))
Peter van 't Hof's avatar
Peter van 't Hof committed
104 105 106 107
      pipesJobs.count(_.isInstanceOf[BcftoolsCall]) shouldBe (if (bcftools) 1 else 0) + (if (bcftoolsSinglesample) bams else 0)
      pipeline.functions.count(_.isInstanceOf[BcftoolsMerge]) shouldBe (if (bcftoolsSinglesample && bams > 1) 1 else 0)
      pipesJobs.count(_.isInstanceOf[Freebayes]) shouldBe (if (freebayes) 1 else 0)
      pipesJobs.count(_.isInstanceOf[MpileupToVcf]) shouldBe (if (raw) bams else 0)
Peter van 't Hof's avatar
Peter van 't Hof committed
108
      pipeline.functions.count(_.isInstanceOf[VcfFilter]) shouldBe (if (raw) bams else 0)
109 110 111 112 113
      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)
Peter van 't Hof's avatar
Peter van 't Hof committed
114 115
    }
  }
116 117 118 119

  @AfterClass def removeTempOutputDir() = {
    FileUtils.deleteDirectory(ShivaVariantcallingTest.outputDir)
  }
Peter van 't Hof's avatar
Peter van 't Hof committed
120 121
}

Peter van 't Hof's avatar
Peter van 't Hof committed
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
class ShivaVariantcallingNoVariantcallersTest extends ShivaVariantcallingTestTrait
class ShivaVariantcallingAllTest extends ShivaVariantcallingTestTrait {
  override def raw  : Boolean = true
  override def bcftools  : Boolean = true
  override def bcftools_singlesample  : Boolean = true
  override def haplotypeCallerGvcf  : Boolean = true
  override def haplotypeCallerAllele  : Boolean = true
  override def unifiedGenotyperAllele  : Boolean = true
  override def unifiedGenotyper  : Boolean = true
  override def haplotypeCaller  : Boolean = true
  override def freebayes  : Boolean = true
  override def varscanCnsSinglesample  : Boolean = true
}
class ShivaVariantcallingRawTest extends ShivaVariantcallingTestTrait {
  override def raw  : Boolean = true
}
class ShivaVariantcallingBcftoolsTest extends ShivaVariantcallingTestTrait {
  override def bcftools  : Boolean = true
}
class ShivaVariantcallingBcftoolsSinglesampleTest extends ShivaVariantcallingTestTrait {
  override def bcftools_singlesample  : Boolean = true
}
class ShivaVariantcallingHaplotypeCallerGvcfTest extends ShivaVariantcallingTestTrait {
  override def haplotypeCallerGvcf  : Boolean = true
}
class ShivaVariantcallingHaplotypeCallerAlleleTest extends ShivaVariantcallingTestTrait {
  override def haplotypeCallerAllele  : Boolean = true
}
class ShivaVariantcallingUnifiedGenotyperAlleleTest extends ShivaVariantcallingTestTrait {
  override def unifiedGenotyperAllele  : Boolean = true
}
class ShivaVariantcallingUnifiedGenotyperTest extends ShivaVariantcallingTestTrait {
  override def unifiedGenotyper  : Boolean = true
}
class ShivaVariantcallingHaplotypeCallerTest extends ShivaVariantcallingTestTrait {
  override def haplotypeCaller  : Boolean = true
}
class ShivaVariantcallingFreebayesTest extends ShivaVariantcallingTestTrait {
  override def freebayes  : Boolean = true
}
class ShivaVariantcallingVarscanCnsSinglesampleTest extends ShivaVariantcallingTestTrait {
  override def varscanCnsSinglesample  : Boolean = true
}

Peter van 't Hof's avatar
Peter van 't Hof committed
166 167
object ShivaVariantcallingTest {
  val outputDir = Files.createTempDir()
Peter van 't Hof's avatar
Peter van 't Hof committed
168
  new File(outputDir, "input").mkdirs()
169
  def inputTouch(name: String): File = {
Peter van 't Hof's avatar
Peter van 't Hof committed
170 171 172 173
    val file = new File(outputDir, "input" + File.separator + name).getAbsoluteFile
    Files.touch(file)
    file
  }
Peter van 't Hof's avatar
Peter van 't Hof committed
174

Peter van 't Hof's avatar
Peter van 't Hof committed
175 176 177 178 179 180 181 182 183 184 185
  private def copyFile(name: String): Unit = {
    val is = getClass.getResourceAsStream("/" + name)
    val os = new FileOutputStream(new File(outputDir, name))
    org.apache.commons.io.IOUtils.copy(is, os)
    os.close()
  }

  copyFile("ref.fa")
  copyFile("ref.dict")
  copyFile("ref.fa.fai")

Peter van 't Hof's avatar
Peter van 't Hof committed
186 187 188
  val config = Map(
    "name_prefix" -> "test",
    "output_dir" -> outputDir,
Peter van 't Hof's avatar
Peter van 't Hof committed
189 190 191
    "cache" -> true,
    "dir" -> "test",
    "vep_script" -> "test",
Peter van 't Hof's avatar
Peter van 't Hof committed
192
    "reference_fasta" -> (outputDir + File.separator + "ref.fa"),
Peter van 't Hof's avatar
Peter van 't Hof committed
193 194
    "gatk_jar" -> "test",
    "samtools" -> Map("exe" -> "test"),
Peter van 't Hof's avatar
Peter van 't Hof committed
195
    "bcftools" -> Map("exe" -> "test"),
196 197
    "md5sum" -> Map("exe" -> "test"),
    "bgzip" -> Map("exe" -> "test"),
198
    "tabix" -> Map("exe" -> "test"),
Peter van 't Hof's avatar
Peter van 't Hof committed
199 200
    "input_alleles" -> "test.vcf.gz",
    "varscan_jar" -> "test"
Peter van 't Hof's avatar
Peter van 't Hof committed
201 202
  )
}