ShivaTest.scala 5.36 KB
Newer Older
bow's avatar
bow committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
 * Biopet is built on top of GATK Queue for building bioinformatic
 * pipelines. It is mainly intended to support LUMC SHARK cluster which is running
 * SGE. But other types of HPC that are supported by GATK Queue (such as PBS)
 * should also be able to execute Biopet tools and pipelines.
 *
 * Copyright 2014 Sequencing Analysis Support Core - Leiden University Medical Center
 *
 * Contact us at: sasc@lumc.nl
 *
 * A dual licensing mode is applied. The source code within this project that are
 * not part of GATK Queue is freely available for non-commercial use under an AGPL
 * license; For commercial users or users who do not want to follow the AGPL
 * license, please contact us to obtain a separate license.
 */
16
17
package nl.lumc.sasc.biopet.pipelines.shiva

18
19
import java.io.{ File, FileOutputStream }

20
import com.google.common.io.Files
Peter van 't Hof's avatar
Peter van 't Hof committed
21
import nl.lumc.sasc.biopet.utils.config.Config
22
import nl.lumc.sasc.biopet.extensions.bwa.BwaMem
Peter van 't Hof's avatar
Peter van 't Hof committed
23
import nl.lumc.sasc.biopet.extensions.picard.{ MarkDuplicates, SortSam }
24
import nl.lumc.sasc.biopet.extensions.tools.VcfStats
25
26
27
28
import nl.lumc.sasc.biopet.utils.ConfigUtils
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
29
import org.testng.annotations.{ DataProvider, Test }
30
31

/**
Peter van 't Hof's avatar
Peter van 't Hof committed
32
33
 * Test class for [[Shiva]]
 *
34
35
36
37
38
39
 * Created by pjvan_thof on 3/2/15.
 */
class ShivaTest extends TestNGSuite with Matchers {
  def initPipeline(map: Map[String, Any]): Shiva = {
    new Shiva() {
      override def configName = "shiva"
40
      override def globalConfig = new Config(ConfigUtils.mergeMaps(map, ShivaTest.config))
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
      qSettings = new QSettings
      qSettings.runName = "test"
    }
  }

  @DataProvider(name = "shivaOptions")
  def shivaOptions = {
    val bool = Array(true, false)

    for (s1 <- bool; s2 <- bool; s3 <- bool; multi <- bool; single <- bool; library <- bool)
      yield Array("", s1, s2, s3, multi, single, library)
  }

  @Test(dataProvider = "shivaOptions")
  def testShiva(f: String, sample1: Boolean, sample2: Boolean, sample3: Boolean,
                multi: Boolean, single: Boolean, library: Boolean): Unit = {
    val map = {
      var m: Map[String, Any] = ShivaTest.config
Peter van 't Hof's avatar
Peter van 't Hof committed
59
60
61
      if (sample1) m = ConfigUtils.mergeMaps(ShivaTest.sample1, m)
      if (sample2) m = ConfigUtils.mergeMaps(ShivaTest.sample2, m)
      if (sample3) m = ConfigUtils.mergeMaps(ShivaTest.sample3, m)
Peter van 't Hof's avatar
Peter van 't Hof committed
62
      ConfigUtils.mergeMaps(Map("multisample_variantcalling" -> multi,
63
        "single_sample_variantcalling" -> single,
Peter van 't Hof's avatar
Peter van 't Hof committed
64
        "library_variantcalling" -> library), m)
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
    }

    if (!sample1 && !sample2 && !sample3) { // When no samples
      intercept[IllegalArgumentException] {
        initPipeline(map).script()
      }
    } else {
      val pipeline = initPipeline(map)
      pipeline.script()

      val numberLibs = (if (sample1) 1 else 0) + (if (sample2) 1 else 0) + (if (sample3) 2 else 0)
      val numberSamples = (if (sample1) 1 else 0) + (if (sample2) 1 else 0) + (if (sample3) 1 else 0)

      pipeline.functions.count(_.isInstanceOf[MarkDuplicates]) shouldBe (numberLibs + (if (sample3) 1 else 0))

      pipeline.functions.count(_.isInstanceOf[VcfStats]) shouldBe (if (multi) 2 else 0) +
        (if (single) numberSamples * 2 else 0) + (if (library) numberLibs * 2 else 0)
    }
  }
}

object ShivaTest {
  val outputDir = Files.createTempDir()
Peter van 't Hof's avatar
Peter van 't Hof committed
88
89
90
91
92
93
  new File(outputDir, "input").mkdirs()
  def inputTouch(name: String): String = {
    val file = new File(outputDir, "input" + File.separator + name)
    Files.touch(file)
    file.getAbsolutePath
  }
94

95
96
97
98
99
100
101
102
103
104
105
  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")

106
107
108
  val config = Map(
    "name_prefix" -> "test",
    "output_dir" -> outputDir,
Peter van 't Hof's avatar
Peter van 't Hof committed
109
110
111
    "cache" -> true,
    "dir" -> "test",
    "vep_script" -> "test",
Peter van 't Hof's avatar
Peter van 't Hof committed
112
    "reference_fasta" -> (outputDir + File.separator + "ref.fa"),
113
114
115
116
117
118
119
120
121
122
123
124
125
126
    "gatk_jar" -> "test",
    "samtools" -> Map("exe" -> "test"),
    "bcftools" -> Map("exe" -> "test"),
    "fastqc" -> Map("exe" -> "test"),
    "input_alleles" -> "test",
    "variantcallers" -> "raw",
    "fastqc" -> Map("exe" -> "test"),
    "seqtk" -> Map("exe" -> "test"),
    "sickle" -> Map("exe" -> "test"),
    "cutadapt" -> Map("exe" -> "test"),
    "bwa" -> Map("exe" -> "test"),
    "samtools" -> Map("exe" -> "test"),
    "macs2" -> Map("exe" -> "test"),
    "igvtools" -> Map("exe" -> "test"),
127
128
129
130
    "wigtobigwig" -> Map("exe" -> "test"),
    "md5sum" -> Map("exe" -> "test"),
    "bgzip" -> Map("exe" -> "test"),
    "tabix" -> Map("exe" -> "test")
131
132
133
134
135
  )

  val sample1 = Map(
    "samples" -> Map("sample1" -> Map("libraries" -> Map(
      "lib1" -> Map(
Peter van 't Hof's avatar
Peter van 't Hof committed
136
137
        "R1" -> inputTouch("1_1_R1.fq"),
        "R2" -> inputTouch("1_1_R2.fq")
138
139
140
141
142
143
144
      )
    )
    )))

  val sample2 = Map(
    "samples" -> Map("sample2" -> Map("libraries" -> Map(
      "lib1" -> Map(
Peter van 't Hof's avatar
Peter van 't Hof committed
145
146
        "R1" -> inputTouch("2_1_R1.fq"),
        "R2" -> inputTouch("2_1_R2.fq")
147
148
149
150
151
152
153
      )
    )
    )))

  val sample3 = Map(
    "samples" -> Map("sample3" -> Map("libraries" -> Map(
      "lib1" -> Map(
Peter van 't Hof's avatar
Peter van 't Hof committed
154
155
        "R1" -> inputTouch("3_1_R1.fq"),
        "R2" -> inputTouch("3_1_R2.fq")
156
157
      ),
      "lib2" -> Map(
Peter van 't Hof's avatar
Peter van 't Hof committed
158
159
        "R1" -> inputTouch("3_2_R1.fq"),
        "R2" -> inputTouch("3_2_R2.fq")
160
161
162
163
      )
    )
    )))
}