ShivaSvCallingTest.scala 7.75 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/**
 * 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.
 */
package nl.lumc.sasc.biopet.pipelines.shiva

import java.io.{ File, FileOutputStream }
Peter van 't Hof's avatar
Peter van 't Hof committed
19
import java.nio.file.Paths
20
21

import com.google.common.io.Files
Wai Yi Leung's avatar
Wai Yi Leung committed
22
import nl.lumc.sasc.biopet.extensions.breakdancer.{ BreakdancerCaller, BreakdancerConfig, BreakdancerVCF }
23
24
import nl.lumc.sasc.biopet.extensions.clever.CleverCaller
import nl.lumc.sasc.biopet.extensions.delly.DellyCaller
Wai Yi Leung's avatar
Wai Yi Leung committed
25
import nl.lumc.sasc.biopet.extensions.pindel.{ PindelVCF, PindelConfig, PindelCaller }
26
import nl.lumc.sasc.biopet.utils.ConfigUtils
Wai Yi Leung's avatar
Wai Yi Leung committed
27
import nl.lumc.sasc.biopet.utils.config.Config
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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.{ AfterClass, DataProvider, Test }

import scala.collection.mutable.ListBuffer

/**
 * Test class for [[ShivaVariantcalling]]
 *
 * Created by pjvan_thof on 3/2/15.
 */
class ShivaSvCallingTest extends TestNGSuite with Matchers {
  def initPipeline(map: Map[String, Any]): ShivaSvCalling = {
    new ShivaSvCalling {
      override def configName = "shivasvcalling"
      override def globalConfig = new Config(ConfigUtils.mergeMaps(map, ShivaSvCallingTest.config))
      qSettings = new QSettings
      qSettings.runName = "test"
    }
  }

  @DataProvider(name = "shivaSvCallingOptions")
  def shivaSvCallingOptions = {
    val bool = Array(true, false)
    (for (
      bams <- 0 to 3;
      delly <- bool;
      clever <- bool;
Wai Yi Leung's avatar
Wai Yi Leung committed
58
59
60
      breakdancer <- bool;
      pindel <- bool
    ) yield Array(bams, delly, clever, breakdancer, pindel)).toArray
61
62
63
  }

  @Test(dataProvider = "shivaSvCallingOptions")
64
65
66
  def testShivaSvCalling(bams: Int,
                         delly: Boolean,
                         clever: Boolean,
Wai Yi Leung's avatar
Wai Yi Leung committed
67
68
                         breakdancer: Boolean,
                         pindel: Boolean) = {
69
70
71
72
    val callers: ListBuffer[String] = ListBuffer()
    if (delly) callers.append("delly")
    if (clever) callers.append("clever")
    if (breakdancer) callers.append("breakdancer")
Wai Yi Leung's avatar
Wai Yi Leung committed
73
    if (pindel) callers.append("pindel")
74
75
76
77
78
    val map = Map("sv_callers" -> callers.toList)
    val pipeline = initPipeline(map)

    pipeline.inputBams = (for (n <- 1 to bams) yield n.toString -> ShivaSvCallingTest.inputTouch("bam_" + n + ".bam")).toMap

Wai Yi Leung's avatar
Wai Yi Leung committed
79
    val illegalArgumentException = pipeline.inputBams.isEmpty || (!delly && !clever && !breakdancer && !pindel)
80
81
82
83
84
85
86
87
88
89

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

    if (!illegalArgumentException) {
      pipeline.init()
      pipeline.script()

Peter van 't Hof's avatar
Peter van 't Hof committed
90
91
92
93
94
95
96
      val summaryCallers = pipeline.summarySettings("sv_callers")
      if (delly) assert(summaryCallers.contains("delly"))
      else assert(!summaryCallers.contains("delly"))
      if (clever) assert(summaryCallers.contains("clever"))
      else assert(!summaryCallers.contains("clever"))
      if (breakdancer) assert(summaryCallers.contains("breakdancer"))
      else assert(!summaryCallers.contains("breakdancer"))
Wai Yi Leung's avatar
Wai Yi Leung committed
97
98
      if (pindel) assert(summaryCallers.contains("pindel"))
      else assert(!summaryCallers.contains("pindel"))
Peter van 't Hof's avatar
Peter van 't Hof committed
99

100
      pipeline.functions.count(_.isInstanceOf[BreakdancerConfig]) shouldBe (if (breakdancer) bams else 0)
Wai Yi Leung's avatar
Wai Yi Leung committed
101
      pipeline.functions.count(_.isInstanceOf[BreakdancerCaller]) shouldBe (if (breakdancer) bams else 0)
102
      pipeline.functions.count(_.isInstanceOf[BreakdancerVCF]) shouldBe (if (breakdancer) bams else 0)
Wai Yi Leung's avatar
Wai Yi Leung committed
103
104
105
106
107

      pipeline.functions.count(_.isInstanceOf[PindelConfig]) shouldBe (if (pindel) bams else 0)
      pipeline.functions.count(_.isInstanceOf[PindelCaller]) shouldBe (if (pindel) bams else 0)
      pipeline.functions.count(_.isInstanceOf[PindelVCF]) shouldBe (if (pindel) bams else 0)

108
109
110
111
112
113
      pipeline.functions.count(_.isInstanceOf[CleverCaller]) shouldBe (if (clever) bams else 0)
      pipeline.functions.count(_.isInstanceOf[DellyCaller]) shouldBe (if (delly) (bams * 4) else 0)

    }
  }

114
115
116
117
118
119
120
121
122
123
124
125
126
127
  @DataProvider(name = "dellyOptions")
  def dellyOptions = {
    val bool = Array(true, false)
    (for (
      del <- bool;
      dup <- bool;
      inv <- bool;
      tra <- bool
    ) yield Array(1, del, dup, inv, tra)).toArray
  }

  @Test(dataProvider = "dellyOptions")
  def testShivaDelly(bams: Int, del: Boolean, dup: Boolean, inv: Boolean, tra: Boolean): Unit = {

Peter van 't Hof's avatar
Peter van 't Hof committed
128
    val map = Map("sv_callers" -> List("delly"), "delly" ->
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
      Map("DEL" -> del, "DUP" -> dup, "INV" -> inv, "TRA" -> tra)
    )
    val pipeline = initPipeline(map)

    pipeline.inputBams = Map("bam" -> ShivaVariantcallingTest.inputTouch("bam" + ".bam"))

    if (!del && !dup && !inv && !tra) intercept[IllegalArgumentException] {
      pipeline.init()
      pipeline.script()
    }
    else {
      pipeline.init()
      pipeline.script()

      pipeline.functions.count(_.isInstanceOf[DellyCaller]) shouldBe
        ((if (del) 1 else 0) + (if (dup) 1 else 0) + (if (inv) 1 else 0) + (if (tra) 1 else 0))
    }
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
148
149
150
151
152
153
154
155
156
157
158
159
160
  @Test
  def testWrongCaller(): Unit = {
    val map = Map("sv_callers" -> List("this is not a caller"))
    val pipeline = initPipeline(map)

    pipeline.inputBams = Map("bam" -> ShivaVariantcallingTest.inputTouch("bam" + ".bam"))

    intercept[IllegalArgumentException] {
      pipeline.init()
      pipeline.script()
    }
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
  private def resourcePath(p: String): String = {
    Paths.get(getClass.getResource(p).toURI).toString
  }

  @Test
  def testInputBamsArg(): Unit = {
    val pipeline = initPipeline(Map())

    pipeline.inputBamsArg :+= new File(resourcePath("/paired01.bam"))

    pipeline.init()
    pipeline.script()

    val summaryCallers = pipeline.summarySettings("sv_callers")
    assert(summaryCallers.contains("delly"))
    assert(summaryCallers.contains("clever"))
    assert(summaryCallers.contains("breakdancer"))
  }

180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
  @AfterClass def removeTempOutputDir() = {
    FileUtils.deleteDirectory(ShivaSvCallingTest.outputDir)
  }
}

object ShivaSvCallingTest {
  val outputDir = Files.createTempDir()
  new File(outputDir, "input").mkdirs()
  def inputTouch(name: String): File = {
    val file = new File(outputDir, "input" + File.separator + name).getAbsoluteFile
    Files.touch(file)
    file
  }

  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")

  val config = Map(
    "name_prefix" -> "test",
    "output_dir" -> outputDir,
    "cache" -> true,
    "dir" -> "test",
    "vep_script" -> "test",
    "reference_fasta" -> (outputDir + File.separator + "ref.fa"),
    "gatk_jar" -> "test",
    "samtools" -> Map("exe" -> "test"),
    "md5sum" -> Map("exe" -> "test"),
    "bgzip" -> Map("exe" -> "test"),
    "tabix" -> Map("exe" -> "test"),
    "breakdancerconfig" -> Map("exe" -> "test"),
    "breakdancercaller" -> Map("exe" -> "test"),
Wai Yi Leung's avatar
Wai Yi Leung committed
219
220
221
    "pindelconfig" -> Map("exe" -> "test"),
    "pindelcaller" -> Map("exe" -> "test"),
    "pindelvcf" -> Map("exe" -> "test"),
222
223
224
225
226
    "clever" -> Map("exe" -> "test"),
    "delly" -> Map("exe" -> "test"),
    "varscan_jar" -> "test"
  )
}