MappingTest.scala 6.43 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.
 */
15
16
package nl.lumc.sasc.biopet.pipelines.mapping

Peter van 't Hof's avatar
Peter van 't Hof committed
17
import java.io.{File, FileOutputStream}
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.core.BiopetCommandLineFunction
Peter van 't Hof's avatar
Peter van 't Hof committed
21
import nl.lumc.sasc.biopet.extensions.centrifuge.Centrifuge
Peter van 't Hof's avatar
Peter van 't Hof committed
22
import nl.lumc.sasc.biopet.pipelines.flexiprep.Fastqc
Peter van 't Hof's avatar
Peter van 't Hof committed
23
import nl.lumc.sasc.biopet.utils.ConfigUtils
24
import nl.lumc.sasc.biopet.utils.config.Config
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, BeforeClass, DataProvider, Test }
30

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

46
47
48
49
50
51
52
  def paired = Array(true, false)
  def chunks = Array(1, 5)
  def skipMarkDuplicates = Array(true, false)
  def skipFlexipreps = Array(true, false)
  def zipped = Array(true, false)
  def unmappedToGears = false

Peter van 't Hof's avatar
Peter van 't Hof committed
53
  @DataProvider(name = "mappingOptions")
54
55
56
57
58
  def mappingOptions = {
    for (
      pair <- paired;
      chunk <- chunks;
      skipMarkDuplicate <- skipMarkDuplicates;
59
60
61
      skipFlexiprep <- skipFlexipreps;
      zipped <- zipped
    ) yield Array(aligner, pair, chunk, skipMarkDuplicate, skipFlexiprep, zipped)
62
63
64
  }

  @Test(dataProvider = "mappingOptions")
65
66
67
68
  def testMapping(aligner: String, paired: Boolean, chunks: Int,
                  skipMarkDuplicate: Boolean,
                  skipFlexiprep: Boolean,
                  zipped: Boolean) = {
Peter van 't Hof's avatar
Peter van 't Hof committed
69
    val map = ConfigUtils.mergeMaps(Map("output_dir" -> outputDir,
70
71
72
      "aligner" -> aligner,
      "number_chunks" -> chunks,
      "skip_markduplicates" -> skipMarkDuplicate,
73
74
      "skip_flexiprep" -> skipFlexiprep,
      "unmapped_to_gears" -> unmappedToGears
Peter van 't Hof's avatar
Peter van 't Hof committed
75
    ), Map(executables.toSeq: _*))
76
77
    val mapping: Mapping = initPipeline(map)

78
    if (zipped) {
Sander van der Zeeuw's avatar
Sander van der Zeeuw committed
79
80
      mapping.inputR1 = r1Zipped
      if (paired) mapping.inputR2 = Some(r2Zipped)
81
    } else {
Sander van der Zeeuw's avatar
Sander van der Zeeuw committed
82
83
      mapping.inputR1 = r1
      if (paired) mapping.inputR2 = Some(r2)
84
    }
85
86
    mapping.sampleId = Some("1")
    mapping.libId = Some("1")
87
88
    mapping.script()

Peter van 't Hof's avatar
Peter van 't Hof committed
89
90
    val pipesJobs = mapping.functions.filter(_.isInstanceOf[BiopetCommandLineFunction])
      .flatMap(_.asInstanceOf[BiopetCommandLineFunction].pipesJobs)
91

92
93
    //Flexiprep
    mapping.functions.count(_.isInstanceOf[Fastqc]) shouldBe (if (skipFlexiprep) 0 else if (paired) 4 else 2)
94

95
    pipesJobs.count(_.isInstanceOf[Centrifuge]) shouldBe (if (unmappedToGears) 1 else 0)
96
  }
97
98

  val outputDir = Files.createTempDir()
Peter van 't Hof's avatar
Peter van 't Hof committed
99
100
101
102
103
104
  new File(outputDir, "input").mkdirs()

  val r1 = new File(outputDir, "input" + File.separator + "R1.fq")
  val r2 = new File(outputDir, "input" + File.separator + "R2.fq")
  val r1Zipped = new File(outputDir, "input" + File.separator + "R1.fq.gz")
  val r2Zipped = new File(outputDir, "input" + File.separator + "R2.fq.gz")
105
  val hisat2Index = new File(outputDir, "ref.1.ht2")
Peter van 't Hof's avatar
Peter van 't Hof committed
106
107
108
109
110
111
112

  @BeforeClass
  def createTempFiles: Unit = {
    Files.touch(r1)
    Files.touch(r2)
    Files.touch(r1Zipped)
    Files.touch(r2Zipped)
113
    Files.touch(hisat2Index)
Peter van 't Hof's avatar
Peter van 't Hof committed
114
115
116
117
118
119
120
121

    copyFile("ref.fa")
    copyFile("ref.dict")
    copyFile("ref.fa.fai")
    copyFile("ref.1.bt2")
    copyFile("ref.1.ebwt")

  }
122

123
124
125
126
127
128
129
  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()
  }

130
  val executables = Map(
Peter van 't Hof's avatar
Peter van 't Hof committed
131
    "reference_fasta" -> (outputDir + File.separator + "ref.fa"),
132
    "db" -> "test",
Peter van 't Hof's avatar
Peter van 't Hof committed
133
    "bowtie_index" -> (outputDir + File.separator + "ref"),
134
    "hisat2_index" -> (outputDir + File.separator + "ref"),
135
136
    "fastqc" -> Map("exe" -> "test"),
    "seqtk" -> Map("exe" -> "test"),
137
138
    "gsnap" -> Map("exe" -> "test"),
    "tophat" -> Map("exe" -> "test"),
139
    "sickle" -> Map("exe" -> "test"),
140
    "cutadapt" -> Map("exe" -> "test"),
141
142
143
    "bwa" -> Map("exe" -> "test"),
    "star" -> Map("exe" -> "test"),
    "bowtie" -> Map("exe" -> "test"),
144
    "bowtie2" -> Map("exe" -> "test"),
145
    "hisat2" -> Map("exe" -> "test"),
146
    "stampy" -> Map("exe" -> "test", "genome" -> "test", "hash" -> "test"),
147
    "samtools" -> Map("exe" -> "test"),
148
149
    "kraken" -> Map("exe" -> "test", "db" -> "test"),
    "krakenreport" -> Map("exe" -> "test", "db" -> "test"),
Peter van 't Hof's avatar
Peter van 't Hof committed
150
151
152
    "centrifuge" -> Map("exe" -> "test"),
    "centrifugekreport" -> Map("exe" -> "test"),
    "centrifuge_index" -> "test",
153
    "md5sum" -> Map("exe" -> "test")
154
  )
Peter van 't Hof's avatar
Peter van 't Hof committed
155
156
157
158
159
160
161
162
163
164
165
166
167

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

class MappingBwaMemTest extends AbstractTestMapping("bwa-mem")
class MappingBwaAlnTest extends AbstractTestMapping("bwa-aln")
class MappingStarTest extends AbstractTestMapping("star")
class MappingStar2PassTest extends AbstractTestMapping("star-2pass")
class MappingBowtieTest extends AbstractTestMapping("bowtie")
class MappingBowtie2Test extends AbstractTestMapping("bowtie2")
168
class MappingHisat2Test extends AbstractTestMapping("hisat2")
Peter van 't Hof's avatar
Peter van 't Hof committed
169
170
171
class MappingStampyTest extends AbstractTestMapping("stampy")
class MappingGsnapTest extends AbstractTestMapping("gsnap")
class MappingTophatTest extends AbstractTestMapping("tophat")
172
173
174
175
176
177
178
179
180
181

class MappingGearsTest extends AbstractTestMapping("bwa-mem") {
  override def unmappedToGears = true

  override def paired = Array(true)
  override def chunks = Array(1)
  override def skipMarkDuplicates = Array(true)
  override def skipFlexipreps = Array(true)
  override def zipped = Array(true)
}