CarpTest.scala 4.53 KB
Newer Older
Peter van 't Hof's avatar
Peter van 't Hof committed
1
2
3
4
package nl.lumc.sasc.biopet.pipelines.carp

import java.io.File

5
6
7
8
9
10
11
import com.google.common.io.Files
import org.apache.commons.io.FileUtils
import org.broadinstitute.gatk.queue.QSettings
import org.testng.annotations.{ AfterClass, Test, DataProvider }
import org.scalatest.Matchers
import org.scalatest.testng.TestNGSuite

Peter van 't Hof's avatar
Peter van 't Hof committed
12
13
14
15
import nl.lumc.sasc.biopet.core.config.Config
import nl.lumc.sasc.biopet.extensions.bwa.BwaMem
import nl.lumc.sasc.biopet.extensions.macs2.Macs2CallPeak
import nl.lumc.sasc.biopet.extensions.picard.{ MergeSamFiles, SortSam }
Peter van 't Hof's avatar
Peter van 't Hof committed
16
17
18
19
20
import nl.lumc.sasc.biopet.utils.ConfigUtils

/**
 * Created by pjvan_thof on 2/13/15.
 */
Peter van 't Hof's avatar
Peter van 't Hof committed
21
class CarpTest extends TestNGSuite with Matchers {
Peter van 't Hof's avatar
Peter van 't Hof committed
22
23
24
25
26
27
28
29
30
31
32
33
34
  def initPipeline(map: Map[String, Any]): Carp = {
    new Carp() {
      override def configName = "carp"
      override def globalConfig = new Config(map)
      qSettings = new QSettings
      qSettings.runName = "test"
    }
  }

  @DataProvider(name = "carpOptions", parallel = true)
  def carpOptions = {
    val bool = Array(true, false)

Peter van 't Hof's avatar
Peter van 't Hof committed
35
    for (s1 <- bool; s2 <- bool; s3 <- bool; t <- bool; c <- bool) yield Array("", s1, s2, s3, t, c)
Peter van 't Hof's avatar
Peter van 't Hof committed
36
37
38
  }

  @Test(dataProvider = "carpOptions")
Peter van 't Hof's avatar
Peter van 't Hof committed
39
  def testCarp(f: String, sample1: Boolean, sample2: Boolean, sample3: Boolean, threatment: Boolean, control: Boolean): Unit = {
Peter van 't Hof's avatar
Peter van 't Hof committed
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
    val map = {
      var m = ConfigUtils.mergeMaps(Map("output_dir" -> CarpTest.outputDir
      ), CarpTest.excutables)

      if (sample1) m = ConfigUtils.mergeMaps(CarpTest.sample1, m.toMap)
      if (sample2) m = ConfigUtils.mergeMaps(CarpTest.sample2, m.toMap)
      if (sample3) m = ConfigUtils.mergeMaps(CarpTest.sample3, m.toMap)
      if (threatment) m = ConfigUtils.mergeMaps(CarpTest.threatment1, m.toMap)
      if (control) m = ConfigUtils.mergeMaps(CarpTest.control1, m.toMap)
      m
    }

    if (!sample1 && !sample2 && !sample3 && !threatment && !control) { // When no samples
      intercept[IllegalArgumentException] {
        initPipeline(map).script()
      }
    } else if (threatment && !control) { // If control of a samples does not exist in samples
      intercept[IllegalStateException] {
        initPipeline(map).script()
      }
    } else { // When samples are correct
      val carp = initPipeline(map)
      carp.script()
Peter van 't Hof's avatar
Peter van 't Hof committed
63
64
65
66
      val numberLibs = (if (sample1) 1 else 0) + (if (sample2) 1 else 0) + (if (sample3) 2 else 0) +
        (if (threatment) 1 else 0) + (if (control) 1 else 0)
      val numberSamples = (if (sample1) 1 else 0) + (if (sample2) 1 else 0) + (if (sample3) 1 else 0) +
        (if (threatment) 1 else 0) + (if (control) 1 else 0)
Peter van 't Hof's avatar
Peter van 't Hof committed
67

Peter van 't Hof's avatar
Peter van 't Hof committed
68
69
70
      carp.functions.count(_.isInstanceOf[BwaMem]) shouldBe numberLibs
      carp.functions.count(_.isInstanceOf[SortSam]) shouldBe numberLibs
      carp.functions.count(_.isInstanceOf[MergeSamFiles]) shouldBe (if (sample3) 1 else 0)
Peter van 't Hof's avatar
Peter van 't Hof committed
71

Peter van 't Hof's avatar
Peter van 't Hof committed
72
      carp.functions.count(_.isInstanceOf[Macs2CallPeak]) shouldBe (numberSamples + (if (threatment) 1 else 0))
Peter van 't Hof's avatar
Peter van 't Hof committed
73
74
    }
  }
75
76
77
78
79

  // remove temporary run directory all tests in the class have been run
  @AfterClass def removeTempOutputDir() = {
    FileUtils.deleteDirectory(CarpTest.outputDir)
  }
Peter van 't Hof's avatar
Peter van 't Hof committed
80
81
82
}

object CarpTest {
83
  val outputDir = Files.createTempDir()
Peter van 't Hof's avatar
Peter van 't Hof committed
84
85
86
87
88
89
90
91
92

  val excutables = Map(
    "reference" -> "test",
    "seqstat" -> Map("exe" -> "test"),
    "fastqc" -> Map("exe" -> "test"),
    "seqtk" -> Map("exe" -> "test"),
    "sickle" -> Map("exe" -> "test"),
    "bwa" -> Map("exe" -> "test"),
    "samtools" -> Map("exe" -> "test"),
Peter van 't Hof's avatar
Peter van 't Hof committed
93
94
95
    "macs2" -> Map("exe" -> "test"),
    "igvtools" -> Map("exe" -> "test"),
    "wigtobigwig" -> Map("exe" -> "test")
Peter van 't Hof's avatar
Peter van 't Hof committed
96
97
98
  )

  val sample1 = Map(
Peter van 't Hof's avatar
Peter van 't Hof committed
99
    "samples" -> Map("sample1" -> Map("libraries" -> Map(
Peter van 't Hof's avatar
Peter van 't Hof committed
100
101
102
103
104
105
106
107
      "lib1" -> Map(
        "R1" -> "1_1_R1.fq",
        "R2" -> "1_1_R2.fq"
      )
    )
    )))

  val sample2 = Map(
Peter van 't Hof's avatar
Peter van 't Hof committed
108
    "samples" -> Map("sample2" -> Map("libraries" -> Map(
Peter van 't Hof's avatar
Peter van 't Hof committed
109
110
111
112
113
      "lib1" -> Map(
        "R1" -> "2_1_R1.fq",
        "R2" -> "2_1_R2.fq"
      )
    )
Peter van 't Hof's avatar
Peter van 't Hof committed
114
    )))
Peter van 't Hof's avatar
Peter van 't Hof committed
115
116

  val sample3 = Map(
Peter van 't Hof's avatar
Peter van 't Hof committed
117
    "samples" -> Map("sample3" -> Map("libraries" -> Map(
Peter van 't Hof's avatar
Peter van 't Hof committed
118
119
120
121
122
123
124
125
126
127
128
129
      "lib1" -> Map(
        "R1" -> "3_1_R1.fq",
        "R2" -> "3_1_R2.fq"
      ),
      "lib2" -> Map(
        "R1" -> "3_2_R1.fq",
        "R2" -> "3_2_R2.fq"
      )
    )
    )))

  val threatment1 = Map(
Peter van 't Hof's avatar
Peter van 't Hof committed
130
    "samples" -> Map("threatment" -> Map("control" -> "control1", "libraries" -> Map(
Peter van 't Hof's avatar
Peter van 't Hof committed
131
132
133
134
135
136
137
138
      "lib1" -> Map(
        "R1" -> "threatment_1_R1.fq",
        "R2" -> "threatment_1_R2.fq"
      )
    )
    )))

  val control1 = Map(
Peter van 't Hof's avatar
Peter van 't Hof committed
139
    "samples" -> Map("control1" -> Map("libraries" -> Map(
Peter van 't Hof's avatar
Peter van 't Hof committed
140
141
142
143
144
145
146
147
      "lib1" -> Map(
        "R1" -> "control_1_R1.fq",
        "R2" -> "control_1_R2.fq"
      )
    )
    )))

}