ShivaSvCallingTest.scala 7.73 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
Peter van 't Hof's avatar
Peter van 't Hof committed
25
import nl.lumc.sasc.biopet.extensions.pindel.{ PindelCaller, PindelConfig, PindelVCF }
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
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
31
import org.testng.annotations.{ DataProvider, Test }
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

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
57
58
59
      breakdancer <- bool;
      pindel <- bool
    ) yield Array(bams, delly, clever, breakdancer, pindel)).toArray
60
61
62
  }

  @Test(dataProvider = "shivaSvCallingOptions")
63
64
65
  def testShivaSvCalling(bams: Int,
                         delly: Boolean,
                         clever: Boolean,
Wai Yi Leung's avatar
Wai Yi Leung committed
66
67
                         breakdancer: Boolean,
                         pindel: Boolean) = {
68
69
70
71
    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
72
    if (pindel) callers.append("pindel")
73
74
75
76
77
    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
78
    val illegalArgumentException = pipeline.inputBams.isEmpty || (!delly && !clever && !breakdancer && !pindel)
79
80
81
82
83
84
85
86
87
88

    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
89
90
91
92
93
94
95
      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
96
97
      if (pindel) assert(summaryCallers.contains("pindel"))
      else assert(!summaryCallers.contains("pindel"))
Peter van 't Hof's avatar
Peter van 't Hof committed
98

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

      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)

107
      pipeline.functions.count(_.isInstanceOf[CleverCaller]) shouldBe (if (clever) bams else 0)
Peter van 't Hof's avatar
Peter van 't Hof committed
108
      pipeline.functions.count(_.isInstanceOf[DellyCaller]) shouldBe (if (delly) bams * 4 else 0)
109
110
111
112

    }
  }

113
114
115
  @DataProvider(name = "dellyOptions")
  def dellyOptions = {
    val bool = Array(true, false)
Peter van 't Hof's avatar
Peter van 't Hof committed
116
    for (
117
118
119
120
      del <- bool;
      dup <- bool;
      inv <- bool;
      tra <- bool
Peter van 't Hof's avatar
Peter van 't Hof committed
121
    ) yield Array(1, del, dup, inv, tra)
122
123
124
125
126
  }

  @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
127
    val map = Map("sv_callers" -> List("delly"), "delly" ->
128
129
130
131
      Map("DEL" -> del, "DUP" -> dup, "INV" -> inv, "TRA" -> tra)
    )
    val pipeline = initPipeline(map)

Peter van 't Hof's avatar
Peter van 't Hof committed
132
    pipeline.inputBams = Map("bam" -> ShivaSvCallingTest.inputTouch("bam" + ".bam"))
133
134
135
136
137
138
139
140
141
142
143
144
145
146

    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
147
148
149
150
151
  @Test
  def testWrongCaller(): Unit = {
    val map = Map("sv_callers" -> List("this is not a caller"))
    val pipeline = initPipeline(map)

Peter van 't Hof's avatar
Peter van 't Hof committed
152
    pipeline.inputBams = Map("bam" -> ShivaSvCallingTest.inputTouch("bam" + ".bam"))
Peter van 't Hof's avatar
Peter van 't Hof committed
153
154
155
156
157
158
159

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

Peter van 't Hof's avatar
Peter van 't Hof committed
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
  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"))
  }
178
179
180
181
}

object ShivaSvCallingTest {
  val outputDir = Files.createTempDir()
Peter van 't Hof's avatar
Peter van 't Hof committed
182
  outputDir.deleteOnExit()
183
  new File(outputDir, "input").mkdirs()
Peter van 't Hof's avatar
Peter van 't Hof committed
184
  private def inputTouch(name: String): File = {
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
    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
215
216
217
    "pindelconfig" -> Map("exe" -> "test"),
    "pindelcaller" -> Map("exe" -> "test"),
    "pindelvcf" -> Map("exe" -> "test"),
218
219
    "clever" -> Map("exe" -> "test"),
    "delly" -> Map("exe" -> "test"),
Wai Yi Leung's avatar
Wai Yi Leung committed
220
221
222
223
224
    "varscan_jar" -> "test",
    "pysvtools" -> Map(
      "exe" -> "test",
      "exclusion_regions" -> "test",
      "translocations_only" -> false)
225
226
  )
}