BamMetricsTest.scala 4.49 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
22
23
24
25
26
27
28
29
30
31

import com.google.common.io.Files
import nl.lumc.sasc.biopet.core.config.Config
import nl.lumc.sasc.biopet.extensions.bedtools.{ BedtoolsCoverage, BedtoolsIntersect }
import nl.lumc.sasc.biopet.extensions.picard._
import nl.lumc.sasc.biopet.extensions.samtools.SamtoolsFlagstat
import nl.lumc.sasc.biopet.scripts.CoverageStats
import nl.lumc.sasc.biopet.tools.BiopetFlagstat
import nl.lumc.sasc.biopet.utils.ConfigUtils
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
32
import org.testng.annotations.{ AfterClass, DataProvider, Test }
33
34

/**
Peter van 't Hof's avatar
Peter van 't Hof committed
35
36
 * Test class for [[BamMetrics]]
 *
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
 * Created by pjvan_thof on 4/30/15.
 */
class BamMetricsTest extends TestNGSuite with Matchers {

  def initPipeline(map: Map[String, Any]): BamMetrics = {
    new BamMetrics() {
      override def configName = "bammetrics"
      override def globalConfig = new Config(map)
      qSettings = new QSettings
      qSettings.runName = "test"
    }
  }

  @DataProvider(name = "bammetricsOptions")
  def bammetricsOptions = {
    val rois = Array(0, 1, 2, 3)
    val amplicon = Array(true, false)
    val rna = Array(true, false)

    for (
      rois <- rois;
      amplicon <- amplicon;
      rna <- rna
    ) yield Array(rois, amplicon, rna)
  }

  @Test(dataProvider = "bammetricsOptions")
  def testFlexiprep(rois: Int, amplicon: Boolean, rna: Boolean) = {
bow's avatar
bow committed
65
66
    val map = ConfigUtils.mergeMaps(Map("output_dir" -> BamMetricsTest.outputDir),
      Map(BamMetricsTest.executables.toSeq: _*)) ++
67
      (if (amplicon) Map("amplicon_bed" -> "amplicon.bed") else Map()) ++
bow's avatar
bow committed
68
      (if (rna) Map("transcript_refflat" -> "transcripts.refFlat") else Map()) ++
69
70
71
72
73
74
75
76
77
78
79
      Map("regions_of_interest" -> (1 to rois).map("roi_" + _ + ".bed").toList)
    val bammetrics: BamMetrics = initPipeline(map)

    bammetrics.inputBam = new File("input.bam")
    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)
bow's avatar
bow committed
80
    bammetrics.functions.count(_.isInstanceOf[CollectWgsMetrics]) shouldBe (if (rna) 0 else 1)
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
    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)
    bammetrics.functions.count(_.isInstanceOf[BiopetFlagstat]) shouldBe (1 + (regions * 2))
    bammetrics.functions.count(_.isInstanceOf[SamtoolsFlagstat]) shouldBe (1 + (regions * 2))
    bammetrics.functions.count(_.isInstanceOf[BedtoolsIntersect]) shouldBe (regions * 2)

    bammetrics.functions.count(_.isInstanceOf[BedtoolsCoverage]) shouldBe regions
    bammetrics.functions.count(_.isInstanceOf[CoverageStats]) shouldBe regions
  }

  // 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()

102
103
104
105
106
107
108
109
110
111
112
  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")

113
114
  val executables = Map(
    "refFlat" -> "bla.refFlat",
115
    "reference_fasta" -> (outputDir + File.separator + "ref.fa"),
116
    "samtools" -> Map("exe" -> "test"),
117
118
    "bedtools" -> Map("exe" -> "test"),
    "md5sum" -> Map("exe" -> "test")
119
120
  )
}