GearsTest.scala 4.83 KB
Newer Older
Wai Yi Leung's avatar
Wai Yi Leung committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
 * 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.
 */
package nl.lumc.sasc.biopet.pipelines.gears

import java.io.File

import com.google.common.io.Files
Peter van 't Hof's avatar
Peter van 't Hof committed
21
import nl.lumc.sasc.biopet.extensions.kraken.{ Kraken, KrakenReport }
Wai Yi Leung's avatar
Wai Yi Leung committed
22
import nl.lumc.sasc.biopet.extensions.picard.SamToFastq
23
import nl.lumc.sasc.biopet.extensions.samtools.SamtoolsView
24
import nl.lumc.sasc.biopet.extensions.tools.KrakenReportToJson
Wai Yi Leung's avatar
Wai Yi Leung committed
25
26
27
28
29
30
31
32
33
34
35
36
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import nl.lumc.sasc.biopet.utils.ConfigUtils
import nl.lumc.sasc.biopet.utils.config.Config
import org.apache.commons.io.FileUtils
import org.broadinstitute.gatk.queue.QSettings
import org.scalatest.Matchers
import org.scalatest.testng.TestNGSuite
import org.testng.annotations._

/**
 * Test class for [[Gears]]
 *
 * Created by wyleung on 10/22/15.
 */

class GearsPipelineTest(val testset: String) extends TestNGSuite with Matchers {
  def initPipeline(map: Map[String, Any]): Gears = {
    new Gears {
      override def configName = "gears"

      override def globalConfig = new Config(map)

      qSettings = new QSettings
      qSettings.runName = "test"
    }
  }

  @DataProvider(name = "gearsOptions")
  def gearsOptions = {
    val startFromBam = Array(true, false)
    val paired = Array(true, false)
    val hasOutputNames = Array(true, false)
    val hasFileExtensions = Array(true, false)

    for (
      fromBam <- startFromBam;
      pair <- paired;
      hasOutputName <- hasOutputNames;
      hasFileExtension <- hasFileExtensions
    ) yield Array(testset, fromBam, pair, hasOutputName, hasFileExtension)
  }

  @Test(dataProvider = "gearsOptions")
  def testGears(testset: String, fromBam: Boolean, paired: Boolean,
                hasOutputName: Boolean, hasFileExtension: Boolean) = {
    val map = ConfigUtils.mergeMaps(Map(
      "output_dir" -> GearsTest.outputDir
    ), Map(GearsTest.executables.toSeq: _*))

    val gears: Gears = initPipeline(map)

    if (fromBam) {
      gears.bamFile = if (hasFileExtension) Some(GearsTest.bam) else Some(GearsTest.bam_noext)
    } else {
      gears.fastqR1 = if (hasFileExtension) Some(GearsTest.r1) else Some(GearsTest.r1_noext)
      gears.fastqR2 = if (paired) if (hasFileExtension) Some(GearsTest.r2) else Some(GearsTest.r2_noext) else None
    }
    if (hasOutputName)
      gears.outputName = "test"

    gears.script()

    if (hasOutputName) {
      gears.outputName shouldBe "test"
    } else {
      // in the following cases the filename should have been determined by the filename
      if (hasFileExtension) {
        gears.outputName shouldBe (if (fromBam) "bamfile" else "R1")
      } else {
        // no real use-case for this one, have this is for sanity check
        gears.outputName shouldBe (if (fromBam) "bamfile" else "R1")
      }
    }

    // SamToFastq should have started if it was started from bam
99
    gears.functions.count(_.isInstanceOf[SamtoolsView]) shouldBe (if (fromBam) 1 else 0)
100
    gears.functions.count(_.isInstanceOf[SamToFastq]) shouldBe (if (fromBam) 1 else 0)
Wai Yi Leung's avatar
Wai Yi Leung committed
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134

    gears.functions.count(_.isInstanceOf[Kraken]) shouldBe 1
    gears.functions.count(_.isInstanceOf[KrakenReport]) shouldBe 1
    gears.functions.count(_.isInstanceOf[KrakenReportToJson]) shouldBe 1
  }

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

object GearsTest {
  val outputDir = Files.createTempDir()
  new File(outputDir, "input").mkdirs()

  val r1 = new File(outputDir, "input" + File.separator + "R1.fq")
  Files.touch(r1)
  val r2 = new File(outputDir, "input" + File.separator + "R2.fq")
  Files.touch(r2)
  val bam = new File(outputDir, "input" + File.separator + "bamfile.bam")
  Files.touch(bam)

  val r1_noext = new File(outputDir, "input" + File.separator + "R1")
  Files.touch(r1_noext)
  val r2_noext = new File(outputDir, "input" + File.separator + "R2")
  Files.touch(r2_noext)
  val bam_noext = new File(outputDir, "input" + File.separator + "bamfile")
  Files.touch(bam_noext)

  val executables = Map(
    "kraken" -> Map("exe" -> "test", "db" -> "test"),
    "krakenreport" -> Map("exe" -> "test", "db" -> "test"),
    "sambamba" -> Map("exe" -> "test"),
Peter van 't Hof's avatar
Peter van 't Hof committed
135
    "samtools" -> Map("exe" -> "test"),
Wai Yi Leung's avatar
Wai Yi Leung committed
136
137
138
    "md5sum" -> Map("exe" -> "test")
  )
}