KopisuTest.scala 4.34 KB
Newer Older
Peter van 't Hof's avatar
Peter van 't Hof committed
1
/**
Peter van 't Hof's avatar
Peter van 't Hof committed
2
3
4
5
6
7
8
9
10
 * 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
 *
11
 * A dual licensing mode is applied. The source code within this project is freely available for non-commercial use under an AGPL
Peter van 't Hof's avatar
Peter van 't Hof committed
12
13
14
 * license; For commercial users or users who do not want to follow the AGPL
 * license, please contact us to obtain a separate license.
 */
Peter van 't Hof's avatar
Peter van 't Hof committed
15
16
package nl.lumc.sasc.biopet.pipelines.kopisu

Peter van 't Hof's avatar
Peter van 't Hof committed
17
import java.io.{ File, FileOutputStream }
Peter van 't Hof's avatar
Peter van 't Hof committed
18
19

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

import scala.collection.mutable.ListBuffer

/**
Peter van 't Hof's avatar
Peter van 't Hof committed
31
32
33
34
 * Test class for [[Kopisu]]
 *
 * Created by pjvan_thof on 3/2/15.
 */
Peter van 't Hof's avatar
Peter van 't Hof committed
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
class KopisuTest extends TestNGSuite with Matchers {
  def initPipeline(map: Map[String, Any]): Kopisu = {
    new Kopisu() {
      override def configNamespace = "kopisu"
      override def globalConfig = new Config(ConfigUtils.mergeMaps(map, KopisuTest.config))
      qSettings = new QSettings
      qSettings.runName = "test"
    }
  }

  @DataProvider(name = "shivaSvCallingOptions")
  def shivaSvCallingOptions = {
    val bool = Array(true, false)
    (for (
      bams <- 0 to 3;
Peter van 't Hof's avatar
Peter van 't Hof committed
50
51
52
      freec <- bool;
      conifer <- bool
    ) yield Array(bams, freec, conifer)).toArray
Peter van 't Hof's avatar
Peter van 't Hof committed
53
54
55
56
  }

  @Test(dataProvider = "shivaSvCallingOptions")
  def testShivaSvCalling(bams: Int,
Peter van 't Hof's avatar
Peter van 't Hof committed
57
58
                         freec: Boolean,
                         conifer: Boolean) = {
Peter van 't Hof's avatar
Peter van 't Hof committed
59
60
    val callers: ListBuffer[String] = ListBuffer()
    val map = Map("sv_callers" -> callers.toList)
Peter van 't Hof's avatar
Peter van 't Hof committed
61
62
63
64
    val pipeline = initPipeline(map ++ Map(
      "use_freec_method" -> freec,
      "use_conifer_method" -> conifer
    ))
Peter van 't Hof's avatar
Peter van 't Hof committed
65
66
67

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

Peter van 't Hof's avatar
Peter van 't Hof committed
68
    val illegalArgumentException = pipeline.inputBams.isEmpty || (!freec && !conifer)
Peter van 't Hof's avatar
Peter van 't Hof committed
69
70
71
72
73
74
75
76
77
78
79

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

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

      pipeline.freecMethod.isDefined shouldBe freec
Peter van 't Hof's avatar
Peter van 't Hof committed
80
      pipeline.summarySettings.get("freec_method") shouldBe Some(freec)
Peter van 't Hof's avatar
Peter van 't Hof committed
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109

      pipeline.functions.count(_.isInstanceOf[FreeC]) shouldBe (if (freec) bams else 0)
      pipeline.functions.count(_.isInstanceOf[FreeCAssessSignificancePlot]) shouldBe (if (freec) bams else 0)
      pipeline.functions.count(_.isInstanceOf[FreeCCNVPlot]) shouldBe (if (freec) bams else 0)
    }
  }
}

object KopisuTest {
  val outputDir = Files.createTempDir()
  outputDir.deleteOnExit()
  new File(outputDir, "input").mkdirs()
  private 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")

Peter van 't Hof's avatar
Peter van 't Hof committed
110
111
112
113
  val controlDir = Files.createTempDir()
  controlDir.deleteOnExit()
  Files.touch(new File(controlDir, "test.txt"))

Peter van 't Hof's avatar
Peter van 't Hof committed
114
  val config = Map(
115
    "skip_write_dependencies" -> true,
Peter van 't Hof's avatar
Peter van 't Hof committed
116
117
118
119
120
121
122
123
    "name_prefix" -> "test",
    "output_dir" -> outputDir,
    "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"),
Peter van 't Hof's avatar
Peter van 't Hof committed
124
    "freec" -> Map("exe" -> "test", "chrFiles" -> "test", "chrLenFile" -> "test"),
Peter van 't Hof's avatar
Peter van 't Hof committed
125
126
127
128
    "controls_dir" -> controlDir.getAbsolutePath,
    "conifer" -> Map("script" -> "/usr/bin/test"),
    "probe_file" -> "test",
    "rscript" -> Map("exe" -> "test")
Peter van 't Hof's avatar
Peter van 't Hof committed
129
130
  )
}