VcfFilterTest.scala 8.33 KB
Newer Older
Peter van 't Hof's avatar
Peter van 't Hof 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
Peter van 't Hof's avatar
Peter van 't Hof 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.
 */
Sander Bollen's avatar
Sander Bollen committed
15
16
package nl.lumc.sasc.biopet.tools

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

Peter van 't Hof's avatar
Peter van 't Hof committed
20
import htsjdk.variant.variantcontext.GenotypeType
21
import htsjdk.variant.vcf.VCFFileReader
Sander Bollen's avatar
Sander Bollen committed
22
23
24
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
25
import org.testng.annotations.Test
Sander Bollen's avatar
Sander Bollen committed
26
27

import scala.util.Random
Sander Bollen's avatar
Sander Bollen committed
28
import scala.collection.JavaConversions._
Sander Bollen's avatar
Sander Bollen committed
29
/**
Peter van 't Hof's avatar
Peter van 't Hof committed
30
31
 * Test class for [[VcfFilter]]
 *
Sander Bollen's avatar
Sander Bollen committed
32
33
34
35
36
37
38
39
40
 * 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
  }

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

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

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

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

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

Peter van 't Hof's avatar
Peter van 't Hof committed
79
    val size = new VCFFileReader(tmp, false).size
Sander Bollen's avatar
Sander Bollen committed
80
81
82
83
    size shouldBe 1

    val tmp2 = File.createTempFile("VcfFilter", ".vcf.gz")
    tmp2.deleteOnExit()
Peter van 't Hof's avatar
Peter van 't Hof committed
84
    val arguments2: Array[String] = Array("-I", veppedPath, "-o", tmp2.getAbsolutePath,
Sander Bollen's avatar
Sander Bollen committed
85
86
87
      "--mustHaveGenotype", "Sample_101:HOM_VAR")
    main(arguments2)

Peter van 't Hof's avatar
Peter van 't Hof committed
88
    val size2 = new VCFFileReader(tmp2, false).size
Sander Bollen's avatar
Sander Bollen committed
89
90
    size2 shouldBe 0

Sander Bollen's avatar
Sander Bollen committed
91
92
  }

93
94
95
96
  @Test def testHasGenotype() = {
    val reader = new VCFFileReader(vepped, false)
    val record = reader.iterator().next()

Sander Bollen's avatar
Sander Bollen committed
97
98
99
100
101
    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
102

Sander Bollen's avatar
Sander Bollen committed
103
104
105
106
107
    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
108

Sander Bollen's avatar
Sander Bollen committed
109
110
    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
111
112
  }

113
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
  @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
  }

170
171
172
173
174
175
176
177
178
  @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
  }

179
180
181
182
  @Test def testMustHaveVariant() = {
    val reader = new VCFFileReader(vepped, false)
    val record = reader.iterator().next()

Sander Bollen's avatar
Sander Bollen committed
183
184
185
    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
186
187
  }

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

Sander Bollen's avatar
Sander Bollen committed
192
193
194
    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
195
196
197
198
199
200
  }

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

Sander Bollen's avatar
Sander Bollen committed
201
202
203
    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
204
205
206
207
208
209
  }

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

Peter van 't Hof's avatar
Peter van 't Hof committed
210
211
212
    uniqueVariantInSample(record, "Sample_101") shouldBe false
    uniqueVariantInSample(record, "Sample_102") shouldBe false
    uniqueVariantInSample(record, "Sample_103") shouldBe false
Sander Bollen's avatar
Sander Bollen committed
213
214
215
216
217
  }

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

    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
226
    val trio = new Trio("Sample_101", "Sample_102", "Sample_103")
Sander Bollen's avatar
Sander Bollen committed
227
228
229
230
231
232
233

    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
234
    val trio = new Trio("Sample_101", "Sample_102", "Sample_103")
Sander Bollen's avatar
Sander Bollen committed
235
236
237
238
239
240
241
242
243
244
245
246

    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
247
}