SageTest.scala 5.77 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.
 */
Peter van 't Hof's avatar
Peter van 't Hof committed
15
16
package nl.lumc.sasc.biopet.pipelines.sage

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

import com.google.common.io.Files
Peter van 't Hof's avatar
Peter van 't Hof committed
20
import nl.lumc.sasc.biopet.utils.{ConfigUtils, Logging}
Peter van 't Hof's avatar
Peter van 't Hof committed
21
import nl.lumc.sasc.biopet.utils.config.Config
Peter van 't Hof's avatar
Peter van 't Hof committed
22
import org.apache.commons.io.FileUtils
Peter van 't Hof's avatar
Peter van 't Hof committed
23
24
25
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
26
import org.testng.annotations.{AfterClass, DataProvider, Test}
Peter van 't Hof's avatar
Peter van 't Hof committed
27
28

/**
Peter van 't Hof's avatar
Peter van 't Hof committed
29
30
 * Created by pjvanthof on 28/09/16.
 */
Peter van 't Hof's avatar
Peter van 't Hof committed
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
class SageTest extends TestNGSuite with Matchers {
  def initPipeline(map: Map[String, Any]): Sage = {
    new Sage() {
      override def configNamespace = "sage"

      override def globalConfig = new Config(ConfigUtils.mergeMaps(map, SageTest.config))

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

  @DataProvider(name = "sageOptions")
  def sageOptions = {
    for (
      s1 <- sample1; s2 <- sample2
    ) yield Array("", s1, s2)
  }

  def sample1 = Array(false, true)
  def sample2 = Array(false, true)
Peter van 't Hof's avatar
Peter van 't Hof committed
52
53
54
55
  def transcriptome = true
  def countBed = true
  def tagsLibrary = false
  def libraryCounts: Option[Boolean] = None
Peter van 't Hof's avatar
Peter van 't Hof committed
56

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

Peter van 't Hof's avatar
Peter van 't Hof committed
59
60
  @Test(dataProvider = "sageOptions")
  def testSage(f: String, sample1: Boolean, sample2: Boolean): Unit = {
Peter van 't Hof's avatar
Peter van 't Hof committed
61
62
    val outputDir = SageTest.outputDir
    dirs :+= outputDir
Peter van 't Hof's avatar
Peter van 't Hof committed
63
    val map = {
Peter van 't Hof's avatar
Peter van 't Hof committed
64
      var m: Map[String, Any] = SageTest.config(outputDir)
Peter van 't Hof's avatar
Peter van 't Hof committed
65
66
      if (sample1) m = ConfigUtils.mergeMaps(SageTest.sample1, m)
      if (sample2) m = ConfigUtils.mergeMaps(SageTest.sample2, m)
Peter van 't Hof's avatar
Peter van 't Hof committed
67
68
69
70
71
72
      ConfigUtils.mergeMaps(
        (if (transcriptome) Map[String, Any]("transcriptome" -> SageTest.inputTouch("trans.fa")) else Map[String, Any]()) ++
          (if (countBed) Map[String, Any]("count_bed" -> SageTest.inputTouch("count.bed")) else Map[String, Any]()) ++
          (if (tagsLibrary) Map[String, Any]("tags_library" -> SageTest.inputTouch("tablib")) else Map[String, Any]()) ++
          libraryCounts.map("library_counts" -> _),
        m)
Peter van 't Hof's avatar
Peter van 't Hof committed
73
74
75

    }

Peter van 't Hof's avatar
Peter van 't Hof committed
76
    if ((!sample1 && !sample2) || !countBed || (!transcriptome && !tagsLibrary)) {
Peter van 't Hof's avatar
Peter van 't Hof committed
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
      // When no samples
      intercept[IllegalStateException] {
        initPipeline(map).script()
      }
      Logging.errors.clear()
    } else {
      val pipeline = initPipeline(map)
      pipeline.script()

      val numberLibs = (if (sample1) 1 else 0) + (if (sample2) 2 else 0)
      val numberSamples = (if (sample1) 1 else 0) + (if (sample2) 1 else 0)

      pipeline.summaryFiles shouldBe Map()
      pipeline.summarySettings shouldBe Map()

Peter van 't Hof's avatar
Peter van 't Hof committed
92
93
94
95
96
97
98
99
100
101
102
      pipeline.samples.foreach {
        case (sampleId, sample) =>
          sample.summaryFiles shouldBe Map()
          sample.summaryStats shouldBe Map()
          sample.summarySettings shouldBe Map()
          sample.libraries.foreach {
            case (libId, lib) =>
              lib.summaryFiles shouldBe Map()
              lib.summaryStats shouldBe Map()
              lib.summarySettings shouldBe Map()
          }
Peter van 't Hof's avatar
Peter van 't Hof committed
103
104
105
106
      }

    }
  }
Peter van 't Hof's avatar
Peter van 't Hof committed
107
108
109
110
111

  // remove temporary run directory all tests in the class have been run
  @AfterClass def removeTempOutputDir() = {
    dirs.foreach(FileUtils.deleteDirectory)
  }
Peter van 't Hof's avatar
Peter van 't Hof committed
112
113
}

Peter van 't Hof's avatar
Peter van 't Hof committed
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
class SageNoBedTest extends SageTest {
  override def sample1 = Array(true)
  override def sample2 = Array(false)
  override def countBed = false
}

class SageNoLibTest extends SageTest {
  override def sample1 = Array(true)
  override def sample2 = Array(false)
  override def transcriptome = false
  override def tagsLibrary = false
}

class SageLibraryCountsTest extends SageTest {
  override def sample1 = Array(true)
  override def sample2 = Array(false)
  override def libraryCounts = Some(true)
}

Peter van 't Hof's avatar
Peter van 't Hof committed
133
object SageTest {
Peter van 't Hof's avatar
Peter van 't Hof committed
134
135
136
  def outputDir = Files.createTempDir()
  val intputDir = Files.createTempDir()

Peter van 't Hof's avatar
Peter van 't Hof committed
137
  def inputTouch(name: String): String = {
Peter van 't Hof's avatar
Peter van 't Hof committed
138
    val file = new File(intputDir, name)
Peter van 't Hof's avatar
Peter van 't Hof committed
139
140
141
142
143
144
145
146
    Files.touch(file)
    file.getAbsolutePath
  }

  inputTouch("ref.ebwt")

  private def copyFile(name: String): Unit = {
    val is = getClass.getResourceAsStream("/" + name)
Peter van 't Hof's avatar
Peter van 't Hof committed
147
    val os = new FileOutputStream(new File(intputDir, name))
Peter van 't Hof's avatar
Peter van 't Hof committed
148
149
150
151
152
153
154
155
    org.apache.commons.io.IOUtils.copy(is, os)
    os.close()
  }

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

Peter van 't Hof's avatar
Peter van 't Hof committed
156
  def config(outputDir: File): Map[String, Any] = Map(
157
    "skip_write_dependencies" -> true,
Peter van 't Hof's avatar
Peter van 't Hof committed
158
    "reference_fasta" -> (intputDir + File.separator + "ref.fa"),
Peter van 't Hof's avatar
Peter van 't Hof committed
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
    "output_dir" -> outputDir.getAbsolutePath,
    "fastqc" -> Map("exe" -> "test"),
    "seqtk" -> Map("exe" -> "test"),
    "md5sum" -> Map("exe" -> "test"),
    "bedtools" -> Map("exe" -> "test"),
    "bowtie" -> Map("exe" -> "test"),
    "bowtie_index" -> inputTouch("ref")
  )

  val sample1 = Map(
    "samples" -> Map("sample1" -> Map("libraries" -> Map(
      "lib1" -> Map(
        "R1" -> inputTouch("1_1_R1.fq"),
        "R2" -> inputTouch("1_1_R2.fq")
      )
    )
    )))

  val sample2 = Map(
    "samples" -> Map("sample3" -> Map("libraries" -> Map(
      "lib1" -> Map(
        "R1" -> inputTouch("2_1_R1.fq"),
        "R2" -> inputTouch("2_1_R2.fq")
      ),
      "lib2" -> Map(
        "R1" -> inputTouch("2_2_R1.fq"),
        "R2" -> inputTouch("2_2_R2.fq")
      )
    )
    )))

}