VcfFilterTest.scala 8.43 KB
Newer Older
Peter van 't Hof's avatar
Peter van 't Hof committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
 * 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
 *
 * A dual licensing mode is applied. The source code within this project that are
 * not part of GATK Queue is freely available for non-commercial use under an AGPL
 * license; For commercial users or users who do not want to follow the AGPL
 * license, please contact us to obtain a separate license.
 */
Sander Bollen's avatar
Sander Bollen committed
16
17
package nl.lumc.sasc.biopet.tools

18
import java.io.File
Sander Bollen's avatar
Sander Bollen committed
19
20
import java.nio.file.Paths

Peter van 't Hof's avatar
Peter van 't Hof committed
21
import htsjdk.variant.variantcontext.GenotypeType
22
import htsjdk.variant.vcf.VCFFileReader
Sander Bollen's avatar
Sander Bollen committed
23
24
25
import org.scalatest.Matchers
import org.scalatest.mock.MockitoSugar
import org.scalatest.testng.TestNGSuite
Peter van 't Hof's avatar
Peter van 't Hof committed
26
import org.testng.annotations.Test
Sander Bollen's avatar
Sander Bollen committed
27
28

import scala.util.Random
Sander Bollen's avatar
Sander Bollen committed
29
import scala.collection.JavaConversions._
Sander Bollen's avatar
Sander Bollen committed
30
/**
Peter van 't Hof's avatar
Peter van 't Hof committed
31
32
 * Test class for [[VcfFilter]]
 *
Sander Bollen's avatar
Sander Bollen committed
33
34
35
36
37
38
39
40
41
 * Created by ahbbollen on 9-4-15.
 */
class VcfFilterTest extends TestNGSuite with MockitoSugar with Matchers {

  import VcfFilter._
  private def resourcePath(p: String): String = {
    Paths.get(getClass.getResource(p).toURI).toString
  }

42
43
  val veppedPath = resourcePath("/VEP_oneline.vcf")
  val vepped = new File(veppedPath)
Sander Bollen's avatar
Sander Bollen committed
44
45
46
  val rand = new Random()

  @Test def testOutputTypeVcf() = {
Sander Bollen's avatar
Sander Bollen committed
47
48
    val tmp = File.createTempFile("VcfFilter", ".vcf")
    tmp.deleteOnExit()
49
50
    val tmpPath = tmp.getAbsolutePath
    val arguments: Array[String] = Array("-I", veppedPath, "-o", tmpPath)
Sander Bollen's avatar
Sander Bollen committed
51
52
53
54
    main(arguments)
  }

  @Test def testOutputTypeBcf() = {
Sander Bollen's avatar
Sander Bollen committed
55
56
    val tmp = File.createTempFile("VcfFilter", ".bcf")
    tmp.deleteOnExit()
57
58
    val tmpPath = tmp.getAbsolutePath
    val arguments: Array[String] = Array("-I", veppedPath, "-o", tmpPath)
Sander Bollen's avatar
Sander Bollen committed
59
60
61
62
    main(arguments)
  }

  @Test def testOutputTypeVcfGz() = {
Sander Bollen's avatar
Sander Bollen committed
63
64
    val tmp = File.createTempFile("VcfFilter", ".vcf.gz")
    tmp.deleteOnExit()
65
66
    val tmpPath = tmp.getAbsolutePath
    val arguments: Array[String] = Array("-I", veppedPath, "-o", tmpPath)
Sander Bollen's avatar
Sander Bollen committed
67
68
69
    main(arguments)
  }

Sander Bollen's avatar
Sander Bollen committed
70
71
  @Test def testMustHaveGenotypes() = {
    /**
Peter van 't Hof's avatar
Peter van 't Hof committed
72
73
     * This should simply not raise an exception
     */
Sander Bollen's avatar
Sander Bollen committed
74
75
    val tmp = File.createTempFile("VCfFilter", ".vcf.gz")
    tmp.deleteOnExit()
76
77
    val tmpPath = tmp.getAbsolutePath
    val arguments: Array[String] = Array("-I", veppedPath, "-o", tmpPath,
Sander Bollen's avatar
Sander Bollen committed
78
79
      "--mustHaveGenotype", "Sample_101:HET")
    main(arguments)
Sander Bollen's avatar
Sander Bollen committed
80

81
    val size = new VCFFileReader(new File(tmpPath), false).size
Sander Bollen's avatar
Sander Bollen committed
82
83
84
85
    size shouldBe 1

    val tmp2 = File.createTempFile("VcfFilter", ".vcf.gz")
    tmp2.deleteOnExit()
86
87
    val tmpPath2 = tmp2.getAbsolutePath
    val arguments2: Array[String] = Array("-I", veppedPath, "-o", tmpPath2,
Sander Bollen's avatar
Sander Bollen committed
88
89
90
      "--mustHaveGenotype", "Sample_101:HOM_VAR")
    main(arguments2)

91
    val size2 = new VCFFileReader(new File(tmpPath2), false).size
Sander Bollen's avatar
Sander Bollen committed
92
93
    size2 shouldBe 0

Sander Bollen's avatar
Sander Bollen committed
94
95
  }

96
97
98
99
  @Test def testHasGenotype() = {
    val reader = new VCFFileReader(vepped, false)
    val record = reader.iterator().next()

Sander Bollen's avatar
Sander Bollen committed
100
101
102
103
104
    hasGenotype(record, List(("Sample_101", GenotypeType.HET))) shouldBe true
    hasGenotype(record, List(("Sample_101", GenotypeType.HOM_VAR))) shouldBe false
    hasGenotype(record, List(("Sample_101", GenotypeType.HOM_REF))) shouldBe false
    hasGenotype(record, List(("Sample_101", GenotypeType.NO_CALL))) shouldBe false
    hasGenotype(record, List(("Sample_101", GenotypeType.MIXED))) shouldBe false
105

Sander Bollen's avatar
Sander Bollen committed
106
107
108
109
110
    hasGenotype(record, List(("Sample_103", GenotypeType.HET))) shouldBe false
    hasGenotype(record, List(("Sample_103", GenotypeType.HOM_VAR))) shouldBe false
    hasGenotype(record, List(("Sample_103", GenotypeType.HOM_REF))) shouldBe true
    hasGenotype(record, List(("Sample_103", GenotypeType.NO_CALL))) shouldBe false
    hasGenotype(record, List(("Sample_103", GenotypeType.MIXED))) shouldBe false
111

Sander Bollen's avatar
Sander Bollen committed
112
113
    hasGenotype(record, List(("Sample_103", GenotypeType.HOM_REF), ("Sample_101", GenotypeType.HET))) shouldBe true
    hasGenotype(record, List(("Sample_103", GenotypeType.HET), ("Sample_101", GenotypeType.HOM_REF))) shouldBe false
114
115
  }

116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
  @Test def testMinQualScore() = {
    val reader = new VCFFileReader(vepped, false)
    val record = reader.iterator().next()

    minQualscore(record, 2000) shouldBe false
    minQualscore(record, 1000) shouldBe true

  }

  @Test def testHasNonRefCalls() = {
    val reader = new VCFFileReader(vepped, false)
    val record = reader.iterator().next()

    hasNonRefCalls(record) shouldBe true
  }

  @Test def testHasCalls() = {
    val reader = new VCFFileReader(vepped, false)
    val record = reader.iterator().next()

    hasCalls(record) shouldBe true
  }

  @Test def testHasMinDP() = {
    val reader = new VCFFileReader(vepped, false)
    val record = reader.iterator().next()

    hasMinTotalDepth(record, 100) shouldBe true
    hasMinTotalDepth(record, 200) shouldBe false
  }

  @Test def testHasMinSampleDP() = {
    val reader = new VCFFileReader(vepped, false)
    val record = reader.iterator().next()

    hasMinSampleDepth(record, 30, 1) shouldBe true
    hasMinSampleDepth(record, 30, 2) shouldBe true
    hasMinSampleDepth(record, 30, 3) shouldBe true
    hasMinSampleDepth(record, 40, 1) shouldBe true
    hasMinSampleDepth(record, 40, 2) shouldBe true
    hasMinSampleDepth(record, 40, 3) shouldBe false
    hasMinSampleDepth(record, 50, 1) shouldBe false
    hasMinSampleDepth(record, 50, 2) shouldBe false
    hasMinSampleDepth(record, 50, 3) shouldBe false
  }

  @Test def testHasMinSampleAD() = {
    val reader = new VCFFileReader(vepped, false)
    val record = reader.iterator().next()

    minAlternateDepth(record, 0, 3) shouldBe true
    minAlternateDepth(record, 10, 2) shouldBe true
    minAlternateDepth(record, 10, 3) shouldBe false
    minAlternateDepth(record, 20, 1) shouldBe true
    minAlternateDepth(record, 20, 2) shouldBe false
  }

173
174
175
176
177
178
179
180
181
  @Test def testHasMinGQ() = {
    val reader = new VCFFileReader(vepped, false)
    val record = reader.iterator().next()

    minGenomeQuality(record, 99, 1) shouldBe true
    minGenomeQuality(record, 99, 2) shouldBe true
    minGenomeQuality(record, 99, 3) shouldBe true
  }

182
183
184
185
  @Test def testMustHaveVariant() = {
    val reader = new VCFFileReader(vepped, false)
    val record = reader.iterator().next()

Sander Bollen's avatar
Sander Bollen committed
186
187
188
    mustHaveVariant(record, List("Sample_101")) shouldBe true
    mustHaveVariant(record, List("Sample_101", "Sample_102")) shouldBe true
    mustHaveVariant(record, List("Sample_101", "Sample_102", "Sample_103")) shouldBe false
189
190
  }

Sander Bollen's avatar
Sander Bollen committed
191
192
193
194
  @Test def testSameGenotype() = {
    val reader = new VCFFileReader(vepped, false)
    val record = reader.iterator().next()

Sander Bollen's avatar
Sander Bollen committed
195
196
197
    notSameGenotype(record, "Sample_101", "Sample_102") shouldBe false
    notSameGenotype(record, "Sample_101", "Sample_103") shouldBe true
    notSameGenotype(record, "Sample_102", "Sample_103") shouldBe true
Sander Bollen's avatar
Sander Bollen committed
198
199
200
201
202
203
  }

  @Test def testfilterHetVarToHomVar() = {
    val reader = new VCFFileReader(vepped, false)
    val record = reader.iterator().next()

Sander Bollen's avatar
Sander Bollen committed
204
205
206
    filterHetVarToHomVar(record, "Sample_101", "Sample_102") shouldBe true
    filterHetVarToHomVar(record, "Sample_101", "Sample_103") shouldBe true
    filterHetVarToHomVar(record, "Sample_102", "Sample_103") shouldBe true
Sander Bollen's avatar
Sander Bollen committed
207
208
209
210
211
212
  }

  @Test def testDeNovo() = {
    val reader = new VCFFileReader(vepped, false)
    val record = reader.iterator().next()

Sander Bollen's avatar
Sander Bollen committed
213
214
215
    denovoInSample(record, "Sample_101") shouldBe false
    denovoInSample(record, "Sample_102") shouldBe false
    denovoInSample(record, "Sample_103") shouldBe false
Sander Bollen's avatar
Sander Bollen committed
216
217
218
219
220
  }

  @Test def testResToDom() = {
    val reader = new VCFFileReader(vepped, false)
    val record = reader.iterator().next()
Sander Bollen's avatar
Sander Bollen committed
221
    val trio = new Trio("Sample_101", "Sample_102", "Sample_103")
Sander Bollen's avatar
Sander Bollen committed
222
223
224
225
226
227
228

    resToDom(record, List(trio)) shouldBe false
  }

  @Test def testTrioCompound = {
    val reader = new VCFFileReader(vepped, false)
    val record = reader.iterator().next()
Sander Bollen's avatar
Sander Bollen committed
229
    val trio = new Trio("Sample_101", "Sample_102", "Sample_103")
Sander Bollen's avatar
Sander Bollen committed
230
231
232
233
234
235
236

    trioCompound(record, List(trio))
  }

  @Test def testDeNovoTrio = {
    val reader = new VCFFileReader(vepped, false)
    val record = reader.iterator().next()
Sander Bollen's avatar
Sander Bollen committed
237
    val trio = new Trio("Sample_101", "Sample_102", "Sample_103")
Sander Bollen's avatar
Sander Bollen committed
238
239
240
241
242
243
244
245
246
247
248
249

    denovoTrio(record, List(trio))
  }

  @Test def testInIDSet() = {
    val reader = new VCFFileReader(vepped, false)
    val record = reader.iterator().next()

    inIdSet(record, Set("rs199537431")) shouldBe true
    inIdSet(record, Set("dummy")) shouldBe false
  }

Sander Bollen's avatar
Sander Bollen committed
250
}