ShivaVariantcallingTest.scala 8.33 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 }
Peter van 't Hof's avatar
Peter van 't Hof committed
18
import nl.lumc.sasc.biopet.utils.ConfigUtils
19
import org.apache.commons.io.FileUtils
Peter van 't Hof's avatar
Peter van 't Hof committed
20 21 22
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
23
import org.testng.annotations.{ AfterClass, DataProvider, Test }
Peter van 't Hof's avatar
Peter van 't Hof committed
24

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

Peter van 't Hof's avatar
Peter van 't Hof committed
27
/**
Peter van 't Hof's avatar
Peter van 't Hof committed
28 29 30 31
 * Class for testing ShivaVariantcalling
 *
 * Created by pjvan_thof on 3/2/15.
 */
Peter van 't Hof's avatar
Peter van 't Hof committed
32
trait ShivaVariantcallingTestTrait extends TestNGSuite with Matchers {
Peter van 't Hof's avatar
Peter van 't Hof committed
33
  def initPipeline(map: Map[String, Any]): ShivaVariantcalling = {
34
    new ShivaVariantcalling() {
Sander Bollen's avatar
Sander Bollen committed
35
      override def configNamespace = "shivavariantcalling"
Peter van 't Hof's avatar
Peter van 't Hof committed
36 37 38 39 40 41
      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
42 43 44 45 46 47 48 49 50 51 52
  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
53
  @DataProvider(name = "shivaVariantcallingOptions")
Peter van 't Hof's avatar
Peter van 't Hof committed
54
  def shivaVariantcallingOptions = {
Peter van 't Hof's avatar
Peter van 't Hof committed
55
    (for (bams <- 0 to 2) yield Array[Any](bams, raw, bcftools, bcftools_singlesample, unifiedGenotyper,
Peter van 't Hof's avatar
Peter van 't Hof committed
56 57
      haplotypeCaller, haplotypeCallerGvcf, haplotypeCallerAllele, unifiedGenotyperAllele,
      freebayes, varscanCnsSinglesample)
Peter van 't Hof's avatar
Peter van 't Hof committed
58
    ).toArray
Peter van 't Hof's avatar
Peter van 't Hof committed
59 60 61
  }

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

87
    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
88

Peter van 't Hof's avatar
Peter van 't Hof committed
89
    val illegalArgumentException = pipeline.inputBams.isEmpty || callers.isEmpty
Peter van 't Hof's avatar
Peter van 't Hof committed
90 91 92 93 94 95 96 97

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

    if (!illegalArgumentException) {
      pipeline.script()

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

Peter van 't Hof's avatar
Peter van 't Hof committed
100
      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
101 102 103 104
      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
105
      pipeline.functions.count(_.isInstanceOf[VcfFilter]) shouldBe (if (raw) bams else 0)
106 107 108 109 110
      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
111 112 113 114
    }
  }
}

Peter van 't Hof's avatar
Peter van 't Hof committed
115 116 117 118 119 120 121 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
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
159 160
object ShivaVariantcallingTest {
  val outputDir = Files.createTempDir()
Peter van 't Hof's avatar
Peter van 't Hof committed
161
  outputDir.deleteOnExit()
Peter van 't Hof's avatar
Peter van 't Hof committed
162
  new File(outputDir, "input").mkdirs()
163
  def inputTouch(name: String): File = {
Peter van 't Hof's avatar
Peter van 't Hof committed
164 165 166 167
    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
168

Peter van 't Hof's avatar
Peter van 't Hof committed
169 170 171 172 173 174 175 176 177 178 179
  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
180 181 182
  val config = Map(
    "name_prefix" -> "test",
    "output_dir" -> outputDir,
Peter van 't Hof's avatar
Peter van 't Hof committed
183 184 185
    "cache" -> true,
    "dir" -> "test",
    "vep_script" -> "test",
Peter van 't Hof's avatar
Peter van 't Hof committed
186
    "reference_fasta" -> (outputDir + File.separator + "ref.fa"),
Peter van 't Hof's avatar
Peter van 't Hof committed
187 188
    "gatk_jar" -> "test",
    "samtools" -> Map("exe" -> "test"),
Peter van 't Hof's avatar
Peter van 't Hof committed
189
    "bcftools" -> Map("exe" -> "test"),
190 191
    "md5sum" -> Map("exe" -> "test"),
    "bgzip" -> Map("exe" -> "test"),
192
    "tabix" -> Map("exe" -> "test"),
Peter van 't Hof's avatar
Peter van 't Hof committed
193 194
    "input_alleles" -> "test.vcf.gz",
    "varscan_jar" -> "test"
Peter van 't Hof's avatar
Peter van 't Hof committed
195 196
  )
}