KopisuTest.scala 4.62 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
import nl.lumc.sasc.biopet.utils.ConfigUtils
import nl.lumc.sasc.biopet.utils.config.Config
Peter van 't Hof's avatar
Peter van 't Hof committed
23
import org.apache.commons.io.FileUtils
Peter van 't Hof's avatar
Peter van 't Hof committed
24
25
26
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
27
import org.testng.annotations.{ AfterClass, DataProvider, Test }
Peter van 't Hof's avatar
Peter van 't Hof committed
28
29
30
31

import scala.collection.mutable.ListBuffer

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

Peter van 't Hof's avatar
Peter van 't Hof committed
46
47
  private var dirs: List[File] = Nil

Peter van 't Hof's avatar
Peter van 't Hof committed
48
49
50
51
52
  @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
53
54
55
      freec <- bool;
      conifer <- bool
    ) yield Array(bams, freec, conifer)).toArray
Peter van 't Hof's avatar
Peter van 't Hof committed
56
57
58
  }

  @Test(dataProvider = "shivaSvCallingOptions")
Peter van 't Hof's avatar
Peter van 't Hof committed
59
  def testKopisu(bams: Int,
Peter van 't Hof's avatar
Peter van 't Hof committed
60
61
                         freec: Boolean,
                         conifer: Boolean) = {
Peter van 't Hof's avatar
Peter van 't Hof committed
62
63
64
    val outputDir = Files.createTempDir()
    dirs :+= outputDir

Peter van 't Hof's avatar
Peter van 't Hof committed
65
66
    val callers: ListBuffer[String] = ListBuffer()
    val map = Map("sv_callers" -> callers.toList)
Peter van 't Hof's avatar
Peter van 't Hof committed
67
68
69
    val pipeline = initPipeline(map ++ Map(
      "use_freec_method" -> freec,
      "use_conifer_method" -> conifer
Peter van 't Hof's avatar
Peter van 't Hof committed
70
    ), outputDir)
Peter van 't Hof's avatar
Peter van 't Hof committed
71
72
73

    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
74
    val illegalArgumentException = pipeline.inputBams.isEmpty || (!freec && !conifer)
Peter van 't Hof's avatar
Peter van 't Hof committed
75
76
77
78
79
80
81
82
83
84
85

    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
86
      pipeline.summarySettings.get("freec_method") shouldBe Some(freec)
Peter van 't Hof's avatar
Peter van 't Hof committed
87
88
89
90
91
92

      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)
    }
  }
Peter van 't Hof's avatar
Peter van 't Hof committed
93
94
95
96
97

  // remove temporary run directory all tests in the class have been run
  @AfterClass def removeTempOutputDir() = {
    dirs.foreach(FileUtils.deleteDirectory)
  }
Peter van 't Hof's avatar
Peter van 't Hof committed
98
99
100
}

object KopisuTest {
Peter van 't Hof's avatar
Peter van 't Hof committed
101
102
103
  def outputDir = Files.createTempDir()
  val inputDir = Files.createTempDir()

Peter van 't Hof's avatar
Peter van 't Hof committed
104
  private def inputTouch(name: String): File = {
Peter van 't Hof's avatar
Peter van 't Hof committed
105
    val file = new File(inputDir, name).getAbsoluteFile
Peter van 't Hof's avatar
Peter van 't Hof committed
106
107
108
109
110
111
    Files.touch(file)
    file
  }

  private def copyFile(name: String): Unit = {
    val is = getClass.getResourceAsStream("/" + name)
Peter van 't Hof's avatar
Peter van 't Hof committed
112
    val os = new FileOutputStream(new File(inputDir, name))
Peter van 't Hof's avatar
Peter van 't Hof committed
113
114
115
116
117
118
119
120
    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
121
122
123
  val controlDir = Files.createTempDir()
  Files.touch(new File(controlDir, "test.txt"))

Peter van 't Hof's avatar
Peter van 't Hof committed
124
  def config(outputDir: File) = Map(
125
    "skip_write_dependencies" -> true,
Peter van 't Hof's avatar
Peter van 't Hof committed
126
127
    "name_prefix" -> "test",
    "output_dir" -> outputDir,
Peter van 't Hof's avatar
Peter van 't Hof committed
128
    "reference_fasta" -> (inputDir + File.separator + "ref.fa"),
Peter van 't Hof's avatar
Peter van 't Hof committed
129
130
131
132
133
    "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
134
    "freec" -> Map("exe" -> "test", "chrFiles" -> "test", "chrLenFile" -> "test"),
Peter van 't Hof's avatar
Peter van 't Hof committed
135
136
137
138
    "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
139
140
  )
}