BamMetricsTest.scala 4.42 KB
Newer Older
Peter van 't Hof's avatar
Peter van 't Hof 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.bammetrics

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

import com.google.common.io.Files
import nl.lumc.sasc.biopet.extensions.picard._
22
import nl.lumc.sasc.biopet.extensions.tools.BiopetFlagstat
23
import nl.lumc.sasc.biopet.utils.ConfigUtils
24
import nl.lumc.sasc.biopet.utils.config.Config
25
26
27
28
import org.apache.commons.io.FileUtils
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.{ AfterClass, DataProvider, Test }
30
31

/**
Peter van 't Hof's avatar
Peter van 't Hof committed
32
33
 * Test class for [[BamMetrics]]
 *
34
35
36
37
38
39
 * Created by pjvan_thof on 4/30/15.
 */
class BamMetricsTest extends TestNGSuite with Matchers {

  def initPipeline(map: Map[String, Any]): BamMetrics = {
    new BamMetrics() {
Sander Bollen's avatar
Sander Bollen committed
40
      override def configNamespace = "bammetrics"
41
42
43
44
45
46
47
48
49
      override def globalConfig = new Config(map)
      qSettings = new QSettings
      qSettings.runName = "test"
    }
  }

  @DataProvider(name = "bammetricsOptions")
  def bammetricsOptions = {
    val rois = Array(0, 1, 2, 3)
Peter van 't Hof's avatar
Peter van 't Hof committed
50
    val bool = Array(true, false)
51
52
53

    for (
      rois <- rois;
Peter van 't Hof's avatar
Peter van 't Hof committed
54
55
56
57
      amplicon <- bool;
      rna <- bool;
      wgs <- bool
    ) yield Array(rois, amplicon, rna, wgs)
58
59
60
  }

  @Test(dataProvider = "bammetricsOptions")
Peter van 't Hof's avatar
Peter van 't Hof committed
61
62
  def testBamMetrics(rois: Int, amplicon: Boolean, rna: Boolean, wgs: Boolean) = {
    val map = ConfigUtils.mergeMaps(Map("output_dir" -> BamMetricsTest.outputDir, "rna_metrics" -> rna, "wgs_metrics" -> wgs),
bow's avatar
bow committed
63
      Map(BamMetricsTest.executables.toSeq: _*)) ++
Peter van 't Hof's avatar
Peter van 't Hof committed
64
      (if (amplicon) Map("amplicon_bed" -> BamMetricsTest.ampliconBed.getAbsolutePath) else Map()) ++
Peter van 't Hof's avatar
Peter van 't Hof committed
65
      (if (rna) Map("annotation_refflat" -> "transcripts.refFlat") else Map()) ++
Peter van 't Hof's avatar
Peter van 't Hof committed
66
      Map("regions_of_interest" -> (1 to rois).map(BamMetricsTest.roi(_).getAbsolutePath).toList)
67
68
    val bammetrics: BamMetrics = initPipeline(map)

Peter van 't Hof's avatar
Peter van 't Hof committed
69
    bammetrics.inputBam = BamMetricsTest.bam
70
71
72
73
74
75
76
    bammetrics.sampleId = Some("1")
    bammetrics.libId = Some("1")
    bammetrics.script()

    var regions: Int = rois + (if (amplicon) 1 else 0)

    bammetrics.functions.count(_.isInstanceOf[CollectRnaSeqMetrics]) shouldBe (if (rna) 1 else 0)
Peter van 't Hof's avatar
Peter van 't Hof committed
77
    bammetrics.functions.count(_.isInstanceOf[CollectWgsMetrics]) shouldBe (if (wgs) 1 else 0)
78
79
80
    bammetrics.functions.count(_.isInstanceOf[CollectMultipleMetrics]) shouldBe 1
    bammetrics.functions.count(_.isInstanceOf[CalculateHsMetrics]) shouldBe (if (amplicon) 1 else 0)
    bammetrics.functions.count(_.isInstanceOf[CollectTargetedPcrMetrics]) shouldBe (if (amplicon) 1 else 0)
Peter van 't Hof's avatar
Peter van 't Hof committed
81
    bammetrics.functions.count(_.isInstanceOf[BiopetFlagstat]) shouldBe 1
82
83
84
85
86
87
88
89
90
91
92
  }

  // remove temporary run directory all tests in the class have been run
  @AfterClass
  def removeTempOutputDir() = {
    FileUtils.deleteDirectory(BamMetricsTest.outputDir)
  }
}

object BamMetricsTest {
  val outputDir = Files.createTempDir()
Peter van 't Hof's avatar
Peter van 't Hof committed
93
94
95
96
  new File(outputDir, "input").mkdirs()

  val bam = new File(outputDir, "input" + File.separator + "bla.bam")
  Files.touch(bam)
Peter van 't Hof's avatar
Peter van 't Hof committed
97
98
99
100
101
102
103
104
  val ampliconBed = new File(outputDir, "input" + File.separator + "amplicon_bed.bed")
  Files.touch(ampliconBed)

  def roi(i: Int): File = {
    val roi = new File(outputDir, "input" + File.separator + s"roi${i}.bed")
    Files.touch(roi)
    roi
  }
105

106
107
108
109
110
111
112
113
114
115
116
  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")

117
118
  val executables = Map(
    "refFlat" -> "bla.refFlat",
119
    "reference_fasta" -> (outputDir + File.separator + "ref.fa"),
120
    "samtools" -> Map("exe" -> "test"),
121
122
    "bedtools" -> Map("exe" -> "test"),
    "md5sum" -> Map("exe" -> "test")
123
124
  )
}