BamMetricsTest.scala 4.38 KB
Newer Older
Peter van 't Hof's avatar
Peter van 't Hof committed
1 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.
 */
15 16
package nl.lumc.sasc.biopet.pipelines.bammetrics

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

import com.google.common.io.Files
import nl.lumc.sasc.biopet.extensions.picard._
21
import nl.lumc.sasc.biopet.extensions.tools.BiopetFlagstat
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
Peter van 't Hof's avatar
Peter van 't Hof committed
28
import org.testng.annotations.{ AfterClass, DataProvider, Test }
29 30

/**
Peter van 't Hof's avatar
Peter van 't Hof committed
31 32
 * Test class for [[BamMetrics]]
 *
33 34 35 36 37 38
 * 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
39
      override def configNamespace = "bammetrics"
40 41 42 43 44 45 46 47 48
      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
49
    val bool = Array(true, false)
50 51 52

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

  @Test(dataProvider = "bammetricsOptions")
Peter van 't Hof's avatar
Peter van 't Hof committed
60 61
  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
62
      Map(BamMetricsTest.executables.toSeq: _*)) ++
Peter van 't Hof's avatar
Peter van 't Hof committed
63
      (if (amplicon) Map("amplicon_bed" -> BamMetricsTest.ampliconBed.getAbsolutePath) else Map()) ++
Peter van 't Hof's avatar
Peter van 't Hof committed
64
      (if (rna) Map("annotation_refflat" -> "transcripts.refFlat") else Map()) ++
Peter van 't Hof's avatar
Peter van 't Hof committed
65
      Map("regions_of_interest" -> (1 to rois).map(BamMetricsTest.roi(_).getAbsolutePath).toList)
66 67
    val bammetrics: BamMetrics = initPipeline(map)

Peter van 't Hof's avatar
Peter van 't Hof committed
68
    bammetrics.inputBam = BamMetricsTest.bam
69 70 71 72 73 74 75
    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
76
    bammetrics.functions.count(_.isInstanceOf[CollectWgsMetrics]) shouldBe (if (wgs) 1 else 0)
77 78 79
    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
80
    bammetrics.functions.count(_.isInstanceOf[BiopetFlagstat]) shouldBe 1
81 82 83 84 85 86 87 88 89 90 91
  }

  // 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
92 93 94 95
  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
96 97 98 99 100 101 102 103
  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
  }
104

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

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