MappingTest.scala 5.51 KB
Newer Older
1
2
3
4
package nl.lumc.sasc.biopet.pipelines.mapping

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.scalatest.Matchers
import org.scalatest.testng.TestNGSuite
import org.testng.annotations.{ AfterClass, DataProvider, Test }

12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import nl.lumc.sasc.biopet.core.config.Config
import nl.lumc.sasc.biopet.extensions.bwa.{ BwaSamse, BwaSampe, BwaAln, BwaMem }
import nl.lumc.sasc.biopet.extensions.picard.{ MergeSamFiles, AddOrReplaceReadGroups, MarkDuplicates, SortSam }
import nl.lumc.sasc.biopet.extensions._
import nl.lumc.sasc.biopet.pipelines.flexiprep.Cutadapt
import nl.lumc.sasc.biopet.pipelines.flexiprep.Fastqc
import nl.lumc.sasc.biopet.pipelines.flexiprep.Sickle
import nl.lumc.sasc.biopet.pipelines.flexiprep._
import nl.lumc.sasc.biopet.tools.FastqSync
import nl.lumc.sasc.biopet.utils.ConfigUtils

/**
 * Created by pjvan_thof on 2/12/15.
 */
class MappingTest extends TestNGSuite with Matchers {
  def initPipeline(map: Map[String, Any]): Mapping = {
    new Mapping {
      override def configName = "mapping"
      override def globalConfig = new Config(map)
      qSettings = new QSettings
Peter van 't Hof's avatar
Peter van 't Hof committed
32
      qSettings.runName = "test"
33
34
35
36
37
    }
  }

  @DataProvider(name = "mappingOptions", parallel = true)
  def mappingOptions = {
38
    val aligners = Array("bwa", "bwa-aln", "star", "star-2pass", "bowtie", "stampy")
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
    val paired = Array(true, false)
    val chunks = Array(1, 5, 10, 100)
    val skipMarkDuplicates = Array(true, false)
    val skipFlexipreps = Array(true, false)

    for (
      aligner <- aligners;
      pair <- paired;
      chunk <- chunks;
      skipMarkDuplicate <- skipMarkDuplicates;
      skipFlexiprep <- skipFlexipreps
    ) yield Array(aligner, pair, chunk, skipMarkDuplicate, skipFlexiprep)
  }

  @Test(dataProvider = "mappingOptions")
  def testMapping(aligner: String, paired: Boolean, chunks: Int, skipMarkDuplicate: Boolean, skipFlexiprep: Boolean) = {
    val map = ConfigUtils.mergeMaps(Map("output_dir" -> MappingTest.outputDir,
      "aligner" -> aligner,
      "number_chunks" -> chunks,
      "skip_markduplicates" -> skipMarkDuplicate,
      "skip_flexiprep" -> skipFlexiprep
    ), Map(MappingTest.excutables.toSeq: _*))
    val mapping: Mapping = initPipeline(map)

    mapping.input_R1 = new File(mapping.outputDir, "bla_R1.fq")
    if (paired) mapping.input_R2 = Some(new File(mapping.outputDir, "bla_R2.fq"))
    mapping.sampleId = "1"
    mapping.libId = "1"
    mapping.script()

    //Flexiprep
    mapping.functions.count(_.isInstanceOf[Fastqc]) shouldBe (if (skipFlexiprep) 0 else if (paired) 4 else 2)
    mapping.functions.count(_.isInstanceOf[Zcat]) shouldBe 0
    mapping.functions.count(_.isInstanceOf[SeqtkSeq]) shouldBe ((if (skipFlexiprep) 0 else if (paired) 2 else 1) * chunks)
    mapping.functions.count(_.isInstanceOf[Cutadapt]) shouldBe ((if (skipFlexiprep) 0 else if (paired) 2 else 1) * chunks)
    mapping.functions.count(_.isInstanceOf[FastqSync]) shouldBe ((if (skipFlexiprep) 0 else if (paired && !skipFlexiprep) 1 else 0) * chunks)
    mapping.functions.count(_.isInstanceOf[Sickle]) shouldBe ((if (skipFlexiprep) 0 else 1) * chunks)
    mapping.functions.count(_.isInstanceOf[Gzip]) shouldBe (if (skipFlexiprep) 0 else if (paired) 2 else 1)

    //aligners
    mapping.functions.count(_.isInstanceOf[BwaMem]) shouldBe ((if (aligner == "bwa") 1 else 0) * chunks)
    mapping.functions.count(_.isInstanceOf[BwaAln]) shouldBe ((if (aligner == "bwa-aln") (if (paired) 2 else 1) else 0) * chunks)
    mapping.functions.count(_.isInstanceOf[BwaSampe]) shouldBe ((if (aligner == "bwa-aln") (if (paired) 1 else 0) else 0) * chunks)
    mapping.functions.count(_.isInstanceOf[BwaSamse]) shouldBe ((if (aligner == "bwa-aln") (if (paired) 0 else 1) else 0) * chunks)
    mapping.functions.count(_.isInstanceOf[Star]) shouldBe ((if (aligner == "star") 1 else if (aligner == "star-2pass") 3 else 0) * chunks)
    mapping.functions.count(_.isInstanceOf[Bowtie]) shouldBe ((if (aligner == "bowtie") 1 else 0) * chunks)
    mapping.functions.count(_.isInstanceOf[Stampy]) shouldBe ((if (aligner == "stampy") 1 else 0) * chunks)

    // Sort sam or replace readgroup
    val sort = aligner match {
      case "bwa" | "bwa-aln" | "stampy"     => "sortsam"
      case "star" | "star-2pass" | "bowtie" => "replacereadgroups"
      case _                                => throw new IllegalArgumentException("aligner: " + aligner + " does not exist")
    }

    mapping.functions.count(_.isInstanceOf[SortSam]) shouldBe ((if (sort == "sortsam") 1 else 0) * chunks)
    mapping.functions.count(_.isInstanceOf[AddOrReplaceReadGroups]) shouldBe ((if (sort == "replacereadgroups") 1 else 0) * chunks)

    mapping.functions.count(_.isInstanceOf[MergeSamFiles]) shouldBe (if (skipMarkDuplicate && chunks > 1) 1 else 0)
    mapping.functions.count(_.isInstanceOf[MarkDuplicates]) shouldBe (if (skipMarkDuplicate) 0 else 1)
  }
100
101
102
103
104

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

object MappingTest {
108
  val outputDir = Files.createTempDir()
109
110
111
112
113
114
115
116
117
118

  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"),
    "star" -> Map("exe" -> "test"),
    "bowtie" -> Map("exe" -> "test"),
119
    "stampy" -> Map("exe" -> "test", "genome" -> "test", "hash" -> "test"),
120
121
122
    "samtools" -> Map("exe" -> "test")
  )
}