VcfFilterTest.scala 7.68 KB
Newer Older
Sander Bollen's avatar
Sander Bollen committed
1
2
3
4
5
import java.io.File

import nl.lumc.sasc.biopet.extensions.tools.VcfFilter
import org.scalatest.Matchers
import org.scalatest.testng.TestNGSuite
6
import org.testng.annotations.{DataProvider, Test}
Sander Bollen's avatar
Sander Bollen committed
7
8
9
10
11
12
13
14
15
16
17


/**
  * Created by ahbbollen on 2-3-16.
  */
class VcfFilterTest extends TestNGSuite with Matchers {

  def cmd(s: String) = {
    s.replace("'", "").replace("  ", " ").trim
  }

18
  def createFilterer = {
Sander Bollen's avatar
Sander Bollen committed
19
20
21
22
23
24
25
26
27
    val filterer = new VcfFilter(null)

    val iVcf = File.createTempFile("vcfFilter", ".vcf.gz")
    val oVcf = File.createTempFile("vcfFilter", ".vcf.gz")
    iVcf.deleteOnExit()
    oVcf.deleteOnExit()
    filterer.inputVcf = iVcf
    filterer.outputVcf = oVcf

28
29
    filterer
  }
Sander Bollen's avatar
Sander Bollen committed
30

31
32
33
34
35
36
37
38
39
40
  @DataProvider(name = "intArguments")
  def intArguments = {
    Array(
      Array("minSampleDepth", Some(50)),
      Array("minTotalDepth", Some(50)),
      Array("minAlternateDepth", Some(50)),
      Array("minSamplesPass", Some(5)),
      Array("minGenomeQuality", Some(100))
    )
  }
Sander Bollen's avatar
Sander Bollen committed
41

42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
  @DataProvider(name = "stringArguments")
  def stringArguments  = {
    Array(
      Array("resToDom", Some("dummy")),
      Array("trioCompound", Some("dummy")),
      Array("deNovoInSample", Some("dummy")),
      Array("deNovoTrio", Some("dummy")),
      Array("trioLossOfHet", Some("dummy"))
    )
  }

  @DataProvider(name = "listArguments")
  def listArguments = {
    Array(
      Array("mustHaveVariant", List("sample1", "sample2")),
      Array("calledIn", List("sample1", "sample2")),
      Array("mustHaveGenotype", List("sample1:HET", "sample2:HET")),
      Array("diffGenotype", List("sample1:sample2", "sample2:sample3")),
      Array("filterHetVarToHomVar", List("sample1:sample2", "sample2:sample3")),
      Array("id", List("rs01", "rs02"))
    )
  }
Sander Bollen's avatar
Sander Bollen committed
64

65
66
67
  @DataProvider(name = "fileArguments")
  def fileArguments = {
    val invFile = File.createTempFile("vcfFilter", ".vcf")
Sander Bollen's avatar
Sander Bollen committed
68
    val idFile = File.createTempFile("vcfFilter", ".txt")
69
    invFile.deleteOnExit()
Sander Bollen's avatar
Sander Bollen committed
70
    idFile.deleteOnExit()
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
    Array(
      Array("invertedOutputVcf", Some(invFile)),
      Array("idFile", Some(idFile))
    )
  }

  @Test(dataProvider = "intArguments")
  def testIntArguments(attr: String, value: Option[Int]) = {
    val filterer = createFilterer

    attr match {
      case "minSampleDepth" => filterer.minSampleDepth = value
      case "minTotalDepth" => filterer.minTotalDepth = value
      case "minAlternateDepth" => filterer.minAlternateDepth = value
      case "minSamplesPass" => filterer.minSamplesPass = value
      case "minGenomeQuality" => filterer.minGenomeQuality = value
      case _ => throw new IllegalArgumentException
    }

    val cmdString = "--" + attr + " " + (value match {
      case Some(v) => v.toString
      case _ => throw new IllegalArgumentException
    })

Sander Bollen's avatar
Sander Bollen committed
95
    cmd(filterer.cmdLine).contains(cmdString) shouldBe true
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
  }

  @Test(dataProvider = "stringArguments")
  def testStringArguments(attr: String, value: Option[String]) = {
    val filterer = createFilterer

    attr match {
      case "resToDom" => filterer.resToDom = value
      case "trioCompound" => filterer.trioCompound = value
      case "deNovoInSample" => filterer.deNovoInSample = value
      case "deNovoTrio" => filterer.deNovoTrio = value
      case "trioLossOfHet" =>  filterer.trioLossOfHet = value
      case _ => throw new IllegalArgumentException
    }

    val cmdString = "--" + attr + " " + (value match {
      case Some(v) => v
      case _ => throw new IllegalArgumentException
    })

Sander Bollen's avatar
Sander Bollen committed
116
    cmd(filterer.cmdLine).contains(cmdString) shouldBe true
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
  }

  @Test(dataProvider = "listArguments")
  def testListArguments(attr: String, value: List[String]) = {
    val filterer = createFilterer

    attr match {
      case "mustHaveVariant" => filterer.mustHaveVariant = value
      case "mustHaveGenotype" => filterer.mustHaveGenotype = value
      case "calledIn" => filterer.calledIn = value
      case "diffGenotype" => filterer.diffGenotype = value
      case "filterHetVarToHomVar" => filterer.filterHetVarToHomVar = value
      case "id" => filterer.id = value
      case _ => throw new IllegalArgumentException
    }

Sander Bollen's avatar
Sander Bollen committed
133
134
135
136
    value.foreach { x =>
      val cmdString = "--" + attr + " " + x
      cmd(filterer.cmdLine).contains(cmdString) shouldBe true
    }
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
  }

  @Test(dataProvider = "fileArguments")
  def testFileArguments(attr: String, value: Option[File])  = {
    val filterer = createFilterer

    attr match {
      case "invertedOutputVcf" => filterer.invertedOutputVcf = value
      case "idFile" => filterer.idFile = value
      case _ => throw new IllegalArgumentException
    }

    val cmdString = "--" + attr + " " + (value match {
      case Some(v) => v.getAbsolutePath
      case _ => throw new IllegalArgumentException
    })

Sander Bollen's avatar
Sander Bollen committed
154
    cmd(filterer.cmdLine).contains(cmdString) shouldBe true
155
156
157
158
159
160
161
162
163
164
  }

  /**
    * The following two tests are for arguments with a so-far unique type
    */
  @Test
  def testMinQual = {
    val filterer = createFilterer
    filterer.minQualScore = Option(50)

Sander Bollen's avatar
Sander Bollen committed
165
    cmd(filterer.cmdLine).contains("--minQualScore 50.0") shouldBe true
166
167
168
169
170
171
172
  }

  @Test
  def testFilterRefCalls = {
    val filterer = createFilterer
    filterer.filterRefCalls = true

Sander Bollen's avatar
Sander Bollen committed
173
    cmd(filterer.cmdLine).contains("--filterRefCalls") shouldBe true
Sander Bollen's avatar
Sander Bollen committed
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
  }

  @Test
  def testBeforeGraph() = {
    val filterer = new VcfFilter(null)
    val iVcf = File.createTempFile("vcfFilter", ".vcf.gz")
    val oVcf = File.createTempFile("vcfFilter", ".vcf.gz")
    iVcf.deleteOnExit()
    oVcf.deleteOnExit()
    filterer.inputVcf = iVcf
    filterer.outputVcf = oVcf

    filterer.beforeGraph()
    filterer.outputVcfIndex.getAbsolutePath shouldBe oVcf.getAbsolutePath + ".tbi"
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
  @DataProvider(name = "functions")
  def functions = {
    Array(
      () => testCommand(minSampleDepth = Some(2)),
      () => testCommand(minTotalDepth = Some(2)),
      () => testCommand(minSampleDepth = Some(2), minTotalDepth = Some(2))
    ).map(Array(_))
  }

  @Test(dataProvider = "functions")
  def executer(function0: Function0[Unit]): Unit = function0()

  protected def testCommand(
    minSampleDepth: Option[Int] = None,
    minTotalDepth: Option[Int] = None,
    minAlternateDepth: Option[Int] = None,
    minSamplesPass: Option[Int] = None,
    minGenomeQuality: Option[Int] = None,
    filterRefCalls: Boolean = false,
    invertedOutputVcf: Option[File] = None,
    resToDom: Option[String] = None,
    trioCompound: Option[String] = None,
    deNovoInSample: Option[String] = None,
    deNovoTrio: Option[String] = None,
    trioLossOfHet: Option[String] = None,
    mustHaveVariant: List[String] = Nil,
    calledIn: List[String] = Nil,
    mustHaveGenotype: List[String] = Nil,
    diffGenotype: List[String] = Nil,
    filterHetVarToHomVar: List[String] = Nil,
    minQualScore: Option[Double] = None,
    id: List[String] = Nil,
    idFile: Option[File] = None
  ): Unit = {
    val vcfFilter = new VcfFilter(null)
    vcfFilter.minSampleDepth = minSampleDepth
    vcfFilter.minTotalDepth = minTotalDepth
    vcfFilter.minAlternateDepth = minAlternateDepth
    vcfFilter.minSamplesPass = minSamplesPass
    vcfFilter.minGenomeQuality = minGenomeQuality
    vcfFilter.filterRefCalls = filterRefCalls
    vcfFilter.invertedOutputVcf = invertedOutputVcf
    vcfFilter.resToDom = resToDom
    vcfFilter.trioCompound = trioCompound
    vcfFilter.deNovoInSample = deNovoInSample
    vcfFilter.deNovoTrio = deNovoTrio
    vcfFilter.trioLossOfHet = trioLossOfHet
    vcfFilter.mustHaveVariant = mustHaveVariant
    vcfFilter.calledIn = calledIn
    vcfFilter.mustHaveGenotype = mustHaveGenotype
    vcfFilter.diffGenotype = diffGenotype
    vcfFilter.filterHetVarToHomVar = filterHetVarToHomVar
    vcfFilter.minQualScore = minQualScore
    vcfFilter.id = id
    vcfFilter.idFile = idFile
    val command = vcfFilter.commandLine

    //TODO: add test on command test
  }

Sander Bollen's avatar
Sander Bollen committed
250
}