CarpTest.scala 6.02 KB
Newer Older
bow's avatar
bow 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
bow's avatar
bow 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.
 */
Peter van 't Hof's avatar
Peter van 't Hof committed
15
16
package nl.lumc.sasc.biopet.pipelines.carp

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.config.Config
Peter van 't Hof's avatar
Peter van 't Hof committed
21
22
import nl.lumc.sasc.biopet.extensions.bwa.BwaMem
import nl.lumc.sasc.biopet.extensions.macs2.Macs2CallPeak
Peter van 't Hof's avatar
Peter van 't Hof committed
23
24
import nl.lumc.sasc.biopet.extensions.picard.{ MergeSamFiles, SortSam }
import nl.lumc.sasc.biopet.utils.{ ConfigUtils, Logging }
Peter van 't Hof's avatar
Peter van 't Hof committed
25
26
27
28
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
29
import org.testng.annotations.{ AfterClass, DataProvider, Test }
Peter van 't Hof's avatar
Peter van 't Hof committed
30
31

/**
Peter van 't Hof's avatar
Peter van 't Hof committed
32
33
 * Test class for [[Carp]]
 *
Peter van 't Hof's avatar
Peter van 't Hof committed
34
35
 * Created by pjvan_thof on 2/13/15.
 */
Peter van 't Hof's avatar
Peter van 't Hof committed
36
class CarpTest extends TestNGSuite with Matchers {
Peter van 't Hof's avatar
Peter van 't Hof committed
37
38
  def initPipeline(map: Map[String, Any]): Carp = {
    new Carp() {
Sander Bollen's avatar
Sander Bollen committed
39
      override def configNamespace = "carp"
Peter van 't Hof's avatar
Peter van 't Hof committed
40
41
42
43
44
45
      override def globalConfig = new Config(map)
      qSettings = new QSettings
      qSettings.runName = "test"
    }
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
46
  @DataProvider(name = "carpOptions")
Peter van 't Hof's avatar
Peter van 't Hof committed
47
48
49
  def carpOptions = {
    val bool = Array(true, false)

Peter van 't Hof's avatar
Peter van 't Hof committed
50
    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
51
52
53
  }

  @Test(dataProvider = "carpOptions")
Peter van 't Hof's avatar
Peter van 't Hof committed
54
  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
55
56
    val map = {
      var m = ConfigUtils.mergeMaps(Map("output_dir" -> CarpTest.outputDir
57
      ), CarpTest.executables)
Peter van 't Hof's avatar
Peter van 't Hof committed
58

Peter van 't Hof's avatar
Peter van 't Hof committed
59
60
61
62
63
      if (sample1) m = ConfigUtils.mergeMaps(CarpTest.sample1, m)
      if (sample2) m = ConfigUtils.mergeMaps(CarpTest.sample2, m)
      if (sample3) m = ConfigUtils.mergeMaps(CarpTest.sample3, m)
      if (threatment) m = ConfigUtils.mergeMaps(CarpTest.threatment1, m)
      if (control) m = ConfigUtils.mergeMaps(CarpTest.control1, m)
Peter van 't Hof's avatar
Peter van 't Hof committed
64
65
66
67
      m
    }

    if (!sample1 && !sample2 && !sample3 && !threatment && !control) { // When no samples
Peter van 't Hof's avatar
Peter van 't Hof committed
68
      intercept[IllegalStateException] {
Peter van 't Hof's avatar
Peter van 't Hof committed
69
70
        initPipeline(map).script()
      }
Peter van 't Hof's avatar
Peter van 't Hof committed
71
      Logging.errors.clear()
Peter van 't Hof's avatar
Peter van 't Hof committed
72
73
74
75
    } else if (threatment && !control) { // If control of a samples does not exist in samples
      intercept[IllegalStateException] {
        initPipeline(map).script()
      }
Peter van 't Hof's avatar
Peter van 't Hof committed
76
      Logging.errors.clear()
Peter van 't Hof's avatar
Peter van 't Hof committed
77
78
79
    } else { // When samples are correct
      val carp = initPipeline(map)
      carp.script()
Peter van 't Hof's avatar
Peter van 't Hof committed
80
81
82
83
      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
84

Peter van 't Hof's avatar
Peter van 't Hof committed
85
86
      //carp.functions.count(_.isInstanceOf[BwaMem]) shouldBe numberLibs
      //carp.functions.count(_.isInstanceOf[SortSam]) shouldBe numberLibs
Peter van 't Hof's avatar
Peter van 't Hof committed
87
      carp.functions.count(_.isInstanceOf[MergeSamFiles]) shouldBe (if (sample3) 1 else 0)
Peter van 't Hof's avatar
Peter van 't Hof committed
88

Peter van 't Hof's avatar
Peter van 't Hof committed
89
      carp.functions.count(_.isInstanceOf[Macs2CallPeak]) shouldBe (numberSamples + (if (threatment) 1 else 0))
Peter van 't Hof's avatar
Peter van 't Hof committed
90
91
    }
  }
92
93
94
95
96

  // 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
97
98
99
}

object CarpTest {
100
  val outputDir = Files.createTempDir()
Peter van 't Hof's avatar
Peter van 't Hof committed
101
102
103
104
105
106
  new File(outputDir, "input").mkdirs()
  def inputTouch(name: String): String = {
    val file = new File(outputDir, "input" + File.separator + name)
    Files.touch(file)
    file.getAbsolutePath
  }
Peter van 't Hof's avatar
Peter van 't Hof committed
107

108
109
110
111
112
113
114
115
116
117
118
  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")

119
  val executables = Map(
Peter van 't Hof's avatar
Peter van 't Hof committed
120
    "reference_fasta" -> (outputDir + File.separator + "ref.fa"),
Peter van 't Hof's avatar
Peter van 't Hof committed
121
122
123
    "fastqc" -> Map("exe" -> "test"),
    "seqtk" -> Map("exe" -> "test"),
    "sickle" -> Map("exe" -> "test"),
124
    "cutadapt" -> Map("exe" -> "test"),
Peter van 't Hof's avatar
Peter van 't Hof committed
125
126
    "bwa" -> Map("exe" -> "test"),
    "samtools" -> Map("exe" -> "test"),
Peter van 't Hof's avatar
Peter van 't Hof committed
127
    "macs2" -> Map("exe" -> "test"),
128
    "igvtools" -> Map("exe" -> "test", "igvtools_jar" -> "test"),
129
130
    "wigtobigwig" -> Map("exe" -> "test"),
    "md5sum" -> Map("exe" -> "test")
Peter van 't Hof's avatar
Peter van 't Hof committed
131
132
133
  )

  val sample1 = Map(
Peter van 't Hof's avatar
Peter van 't Hof committed
134
    "samples" -> Map("sample1" -> Map("libraries" -> Map(
Peter van 't Hof's avatar
Peter van 't Hof committed
135
      "lib1" -> Map(
Peter van 't Hof's avatar
Peter van 't Hof committed
136
137
        "R1" -> inputTouch("1_1_R1.fq"),
        "R2" -> inputTouch("1_1_R2.fq")
Peter van 't Hof's avatar
Peter van 't Hof committed
138
139
140
141
142
      )
    )
    )))

  val sample2 = Map(
Peter van 't Hof's avatar
Peter van 't Hof committed
143
    "samples" -> Map("sample2" -> Map("libraries" -> Map(
Peter van 't Hof's avatar
Peter van 't Hof committed
144
      "lib1" -> Map(
Peter van 't Hof's avatar
Peter van 't Hof committed
145
146
        "R1" -> inputTouch("2_1_R1.fq"),
        "R2" -> inputTouch("2_1_R2.fq")
Peter van 't Hof's avatar
Peter van 't Hof committed
147
148
      )
    )
Peter van 't Hof's avatar
Peter van 't Hof committed
149
    )))
Peter van 't Hof's avatar
Peter van 't Hof committed
150
151

  val sample3 = Map(
Peter van 't Hof's avatar
Peter van 't Hof committed
152
    "samples" -> Map("sample3" -> Map("libraries" -> Map(
Peter van 't Hof's avatar
Peter van 't Hof committed
153
      "lib1" -> Map(
Peter van 't Hof's avatar
Peter van 't Hof committed
154
155
        "R1" -> inputTouch("3_1_R1.fq"),
        "R2" -> inputTouch("3_1_R2.fq")
Peter van 't Hof's avatar
Peter van 't Hof committed
156
157
      ),
      "lib2" -> Map(
Peter van 't Hof's avatar
Peter van 't Hof committed
158
159
        "R1" -> inputTouch("3_2_R1.fq"),
        "R2" -> inputTouch("3_2_R2.fq")
Peter van 't Hof's avatar
Peter van 't Hof committed
160
161
162
163
164
      )
    )
    )))

  val threatment1 = Map(
Peter van 't Hof's avatar
Peter van 't Hof committed
165
    "samples" -> Map("threatment" -> Map("control" -> "control1", "libraries" -> Map(
Peter van 't Hof's avatar
Peter van 't Hof committed
166
      "lib1" -> Map(
Peter van 't Hof's avatar
Peter van 't Hof committed
167
168
        "R1" -> inputTouch("threatment_1_R1.fq"),
        "R2" -> inputTouch("threatment_1_R2.fq")
Peter van 't Hof's avatar
Peter van 't Hof committed
169
170
171
172
173
      )
    )
    )))

  val control1 = Map(
Peter van 't Hof's avatar
Peter van 't Hof committed
174
    "samples" -> Map("control1" -> Map("libraries" -> Map(
Peter van 't Hof's avatar
Peter van 't Hof committed
175
      "lib1" -> Map(
Peter van 't Hof's avatar
Peter van 't Hof committed
176
177
        "R1" -> inputTouch("control_1_R1.fq"),
        "R2" -> inputTouch("control_1_R2.fq")
Peter van 't Hof's avatar
Peter van 't Hof committed
178
179
180
181
182
      )
    )
    )))

}