BamMetricsTest.scala 4.54 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
  * 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 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.
  */
15 16
package nl.lumc.sasc.biopet.pipelines.bammetrics

17
import java.io.{File, FileOutputStream}
18 19 20

import com.google.common.io.Files
import nl.lumc.sasc.biopet.extensions.picard._
Peter van 't Hof's avatar
Peter van 't Hof committed
21
import nl.lumc.sasc.biopet.extensions.tools.BamStats
22
import nl.lumc.sasc.biopet.utils.ConfigUtils
23
import nl.lumc.sasc.biopet.utils.config.Config
24 25 26 27
import org.apache.commons.io.FileUtils
import org.broadinstitute.gatk.queue.QSettings
import org.scalatest.Matchers
import org.scalatest.testng.TestNGSuite
28
import org.testng.annotations.{AfterClass, DataProvider, Test}
29 30

/**
31 32 33 34
  * Test class for [[BamMetrics]]
  *
  * Created by pjvan_thof on 4/30/15.
  */
35 36 37 38
class BamMetricsTest extends TestNGSuite with Matchers {

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

  @DataProvider(name = "bammetricsOptions")
47
  def bammetricsOptions: Array[Array[AnyVal]] = {
48
    val rois = Array(0, 1, 2, 3)
Peter van 't Hof's avatar
Peter van 't Hof committed
49
    val bool = Array(true, false)
50

51 52 53 54
    for (rois <- rois;
         amplicon <- bool;
         rna <- bool;
         wgs <- bool) yield Array(rois, amplicon, rna, wgs)
55 56
  }

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

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

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

    bammetrics.functions.count(_.isInstanceOf[CollectRnaSeqMetrics]) shouldBe (if (rna) 1 else 0)
Peter van 't Hof's avatar
Peter van 't Hof committed
78
    bammetrics.functions.count(_.isInstanceOf[CollectWgsMetrics]) shouldBe (if (wgs) 1 else 0)
79
    bammetrics.functions.count(_.isInstanceOf[CollectMultipleMetrics]) shouldBe 1
80
    bammetrics.functions.count(_.isInstanceOf[CollectHsMetrics]) shouldBe (if (amplicon) 1 else 0)
81 82
    bammetrics.functions.count(_.isInstanceOf[CollectTargetedPcrMetrics]) shouldBe (if (amplicon) 1
                                                                                    else 0)
Peter van 't Hof's avatar
Peter van 't Hof committed
83
    bammetrics.functions.count(_.isInstanceOf[BamStats]) shouldBe 1
84 85 86
  }

  // remove temporary run directory all tests in the class have been run
87
  @AfterClass def removeTempOutputDir(): Unit = {
Peter van 't Hof's avatar
Peter van 't Hof committed
88
    dirs.foreach(FileUtils.deleteDirectory)
89 90 91 92
  }
}

object BamMetricsTest {
93
  val inputDir: File = Files.createTempDir()
Peter van 't Hof's avatar
Peter van 't Hof committed
94

Peter van 't Hof's avatar
Peter van 't Hof committed
95
  val bam = new File(inputDir, "bla.bam")
Peter van 't Hof's avatar
Peter van 't Hof committed
96
  Files.touch(bam)
Peter van 't Hof's avatar
Peter van 't Hof committed
97
  val ampliconBed = new File(inputDir, "amplicon_bed.bed")
Peter van 't Hof's avatar
Peter van 't Hof committed
98 99 100
  Files.touch(ampliconBed)

  def roi(i: Int): File = {
101
    val roi = new File(inputDir, s"roi$i.bed")
Peter van 't Hof's avatar
Peter van 't Hof committed
102 103 104
    Files.touch(roi)
    roi
  }
105

106 107
  private def copyFile(name: String): Unit = {
    val is = getClass.getResourceAsStream("/" + name)
Peter van 't Hof's avatar
Peter van 't Hof committed
108
    val os = new FileOutputStream(new File(inputDir, name))
109 110 111 112 113 114 115 116
    org.apache.commons.io.IOUtils.copy(is, os)
    os.close()
  }

  copyFile("ref.fa")
  copyFile("ref.dict")
  copyFile("ref.fa.fai")

117
  val executables = Map(
118
    "skip_write_dependencies" -> true,
119
    "refFlat" -> "bla.refFlat",
Peter van 't Hof's avatar
Peter van 't Hof committed
120
    "reference_fasta" -> (inputDir + File.separator + "ref.fa"),
121
    "samtools" -> Map("exe" -> "test"),
122 123
    "bedtools" -> Map("exe" -> "test"),
    "md5sum" -> Map("exe" -> "test")
124 125
  )
}