SageTest.scala 3.87 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
16
17
18
19
20
21
22
23
24
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
99
100
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
135
136
137
138
139
140
141
142
package nl.lumc.sasc.biopet.pipelines.sage

import java.io.{File, FileOutputStream}

import com.google.common.io.Files
import nl.lumc.sasc.biopet.utils.{ConfigUtils, Logging}
import nl.lumc.sasc.biopet.utils.config.Config
import org.broadinstitute.gatk.queue.QSettings
import org.scalatest.Matchers
import org.scalatest.testng.TestNGSuite
import org.testng.annotations.{DataProvider, Test}

/**
  * Created by pjvanthof on 28/09/16.
  */
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)


  @Test(dataProvider = "sageOptions")
  def testSage(f: String, sample1: Boolean, sample2: Boolean): Unit = {
    val map = {
      var m: Map[String, Any] = SageTest.config
      if (sample1) m = ConfigUtils.mergeMaps(SageTest.sample1, m)
      if (sample2) m = ConfigUtils.mergeMaps(SageTest.sample2, m)
      ConfigUtils.mergeMaps(Map(
        // Config values
      ), m)

    }

    if (!sample1 && !sample2) {
      // 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.summaryFile shouldBe new File(SageTest.outputDir, "Sage.summary.json")
      pipeline.summaryFiles shouldBe Map()
      pipeline.summarySettings shouldBe Map()

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

    }
  }
}

object SageTest {
  val outputDir = Files.createTempDir()
  outputDir.deleteOnExit()
  new File(outputDir, "input").mkdirs()
  def inputTouch(name: String): String = {
    val file = new File(outputDir, "input" + File.separator + name)
    Files.touch(file)
    file.getAbsolutePath
  }

  inputTouch("ref.ebwt")

  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")

  val config: Map[String, Any] = Map(
    "reference_fasta" -> (outputDir + File.separator + "ref.fa"),
    "output_dir" -> outputDir.getAbsolutePath,
    "transcriptome" -> inputTouch("trans.fa"),
    "count_bed" -> inputTouch("count.bed"),
    "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")
      )
    )
    )))

}