forked from bigdatagenomics/adam
/
GenotypeRDDSuite.scala
255 lines (207 loc) · 9.59 KB
/
GenotypeRDDSuite.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
100
101
102
103
104
105
106
107
108
109
110
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
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
250
251
252
253
254
255
/**
* Licensed to Big Data Genomics (BDG) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The BDG licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.bdgenomics.adam.rdd.variant
import org.bdgenomics.adam.models.ReferenceRegion
import org.bdgenomics.adam.rdd.ADAMContext._
import org.bdgenomics.adam.util.ADAMFunSuite
class GenotypeRDDSuite extends ADAMFunSuite {
sparkTest("union two genotype rdds together") {
val genotype1 = sc.loadGenotypes(testFile("gvcf_dir/gvcf_multiallelic.g.vcf"))
val genotype2 = sc.loadGenotypes(testFile("small.vcf"))
val union = genotype1.union(genotype2)
assert(union.rdd.count === (genotype1.rdd.count + genotype2.rdd.count))
assert(union.sequences.size === (genotype1.sequences.size + genotype2.sequences.size))
assert(union.samples.size === 4)
}
sparkTest("round trip to parquet") {
val genotypes = sc.loadGenotypes(testFile("small.vcf"))
val outputPath = tmpLocation()
genotypes.saveAsParquet(outputPath)
val unfilteredGenotypes = sc.loadGenotypes(outputPath)
assert(unfilteredGenotypes.rdd.count === 18)
val predicate = ReferenceRegion.createPredicate(ReferenceRegion("1", 14399L, 14400L),
ReferenceRegion("1", 752720L, 757721L),
ReferenceRegion("1", 752790L, 752793L))
val filteredGenotypes = sc.loadParquetGenotypes(outputPath,
optPredicate = Some(predicate))
filteredGenotypes.rdd.foreach(println)
assert(filteredGenotypes.rdd.count === 9)
val starts = filteredGenotypes.rdd.map(_.getStart).distinct.collect.toSet
assert(starts.size === 3)
assert(starts(14396L))
assert(starts(752720L))
assert(starts(752790L))
}
sparkTest("use broadcast join to pull down genotypes mapped to targets") {
val genotypesPath = testFile("small.vcf")
val targetsPath = testFile("small.1.bed")
val genotypes = sc.loadGenotypes(genotypesPath)
val targets = sc.loadFeatures(targetsPath)
val jRdd = genotypes.broadcastRegionJoin(targets)
assert(jRdd.rdd.count === 9L)
}
sparkTest("use right outer broadcast join to pull down genotypes mapped to targets") {
val genotypesPath = testFile("small.vcf")
val targetsPath = testFile("small.1.bed")
val genotypes = sc.loadGenotypes(genotypesPath)
val targets = sc.loadFeatures(targetsPath)
val jRdd = genotypes.rightOuterBroadcastRegionJoin(targets)
val c = jRdd.rdd.collect
assert(c.count(_._1.isEmpty) === 3)
assert(c.count(_._1.isDefined) === 9)
}
sparkTest("use shuffle join to pull down genotypes mapped to targets") {
val genotypesPath = testFile("small.vcf")
val targetsPath = testFile("small.1.bed")
val genotypes = sc.loadGenotypes(genotypesPath)
.transform(_.repartition(1))
val targets = sc.loadFeatures(targetsPath)
.transform(_.repartition(1))
val jRdd = genotypes.shuffleRegionJoin(targets)
val jRdd0 = genotypes.shuffleRegionJoin(targets, optPartitions = Some(4))
// we can't guarantee that we get exactly the number of partitions requested,
// we get close though
assert(jRdd.rdd.partitions.length === 1)
assert(jRdd0.rdd.partitions.length === 4)
assert(jRdd.rdd.count === 9L)
assert(jRdd0.rdd.count === 9L)
}
sparkTest("use right outer shuffle join to pull down genotypes mapped to targets") {
val genotypesPath = testFile("small.vcf")
val targetsPath = testFile("small.1.bed")
val genotypes = sc.loadGenotypes(genotypesPath)
.transform(_.repartition(1))
val targets = sc.loadFeatures(targetsPath)
.transform(_.repartition(1))
val jRdd = genotypes.rightOuterShuffleRegionJoin(targets)
val jRdd0 = genotypes.rightOuterShuffleRegionJoin(targets, optPartitions = Some(4))
// we can't guarantee that we get exactly the number of partitions requested,
// we get close though
assert(jRdd.rdd.partitions.length === 1)
assert(jRdd0.rdd.partitions.length === 4)
val c = jRdd.rdd.collect
val c0 = jRdd0.rdd.collect
assert(c.count(_._1.isEmpty) === 3)
assert(c0.count(_._1.isEmpty) === 3)
assert(c.count(_._1.isDefined) === 9)
assert(c0.count(_._1.isDefined) === 9)
}
sparkTest("use left outer shuffle join to pull down genotypes mapped to targets") {
val genotypesPath = testFile("small.vcf")
val targetsPath = testFile("small.1.bed")
val genotypes = sc.loadGenotypes(genotypesPath)
.transform(_.repartition(1))
val targets = sc.loadFeatures(targetsPath)
.transform(_.repartition(1))
val jRdd = genotypes.leftOuterShuffleRegionJoin(targets)
val jRdd0 = genotypes.leftOuterShuffleRegionJoin(targets, optPartitions = Some(4))
// we can't guarantee that we get exactly the number of partitions requested,
// we get close though
assert(jRdd.rdd.partitions.length === 1)
assert(jRdd0.rdd.partitions.length === 4)
val c = jRdd.rdd.collect
val c0 = jRdd0.rdd.collect
assert(c.count(_._2.isEmpty) === 9)
assert(c0.count(_._2.isEmpty) === 9)
assert(c.count(_._2.isDefined) === 9)
assert(c0.count(_._2.isDefined) === 9)
}
sparkTest("use full outer shuffle join to pull down genotypes mapped to targets") {
val genotypesPath = testFile("small.vcf")
val targetsPath = testFile("small.1.bed")
val genotypes = sc.loadGenotypes(genotypesPath)
.transform(_.repartition(1))
val targets = sc.loadFeatures(targetsPath)
.transform(_.repartition(1))
val jRdd = genotypes.fullOuterShuffleRegionJoin(targets)
val jRdd0 = genotypes.fullOuterShuffleRegionJoin(targets, optPartitions = Some(4))
// we can't guarantee that we get exactly the number of partitions requested,
// we get close though
assert(jRdd.rdd.partitions.length === 1)
assert(jRdd0.rdd.partitions.length === 4)
val c = jRdd.rdd.collect
val c0 = jRdd0.rdd.collect
assert(c.count(t => t._1.isEmpty && t._2.isEmpty) === 0)
assert(c0.count(t => t._1.isEmpty && t._2.isEmpty) === 0)
assert(c.count(t => t._1.isDefined && t._2.isEmpty) === 9)
assert(c0.count(t => t._1.isDefined && t._2.isEmpty) === 9)
assert(c.count(t => t._1.isEmpty && t._2.isDefined) === 3)
assert(c0.count(t => t._1.isEmpty && t._2.isDefined) === 3)
assert(c.count(t => t._1.isDefined && t._2.isDefined) === 9)
assert(c0.count(t => t._1.isDefined && t._2.isDefined) === 9)
}
sparkTest("use shuffle join with group by to pull down genotypes mapped to targets") {
val genotypesPath = testFile("small.vcf")
val targetsPath = testFile("small.1.bed")
val genotypes = sc.loadGenotypes(genotypesPath)
.transform(_.repartition(1))
val targets = sc.loadFeatures(targetsPath)
.transform(_.repartition(1))
val jRdd = genotypes.shuffleRegionJoinAndGroupByLeft(targets)
val jRdd0 = genotypes.shuffleRegionJoinAndGroupByLeft(targets, optPartitions = Some(4))
// we can't guarantee that we get exactly the number of partitions requested,
// we get close though
assert(jRdd.rdd.partitions.length === 1)
assert(jRdd0.rdd.partitions.length === 4)
val c = jRdd.rdd.collect
val c0 = jRdd0.rdd.collect
assert(c.size === 9)
assert(c0.size === 9)
assert(c.forall(_._2.size == 1))
assert(c0.forall(_._2.size == 1))
}
sparkTest("use right outer shuffle join with group by to pull down genotypes mapped to targets") {
val genotypesPath = testFile("small.vcf")
val targetsPath = testFile("small.1.bed")
val genotypes = sc.loadGenotypes(genotypesPath)
.transform(_.repartition(1))
val targets = sc.loadFeatures(targetsPath)
.transform(_.repartition(1))
val jRdd = genotypes.rightOuterShuffleRegionJoinAndGroupByLeft(targets)
val jRdd0 = genotypes.rightOuterShuffleRegionJoinAndGroupByLeft(targets, optPartitions = Some(4))
// we can't guarantee that we get exactly the number of partitions requested,
// we get close though
assert(jRdd.rdd.partitions.length === 1)
assert(jRdd0.rdd.partitions.length === 4)
val c = jRdd.rdd.collect
val c0 = jRdd0.rdd.collect
assert(c.count(_._1.isDefined) === 18)
assert(c0.count(_._1.isDefined) === 18)
assert(c.filter(_._1.isDefined).count(_._2.size == 1) === 9)
assert(c0.filter(_._1.isDefined).count(_._2.size == 1) === 9)
assert(c.filter(_._1.isDefined).count(_._2.isEmpty) === 9)
assert(c0.filter(_._1.isDefined).count(_._2.isEmpty) === 9)
assert(c.count(_._1.isEmpty) === 3)
assert(c0.count(_._1.isEmpty) === 3)
assert(c.filter(_._1.isEmpty).forall(_._2.size == 1))
assert(c0.filter(_._1.isEmpty).forall(_._2.size == 1))
}
sparkTest("convert back to variant contexts") {
val genotypesPath = testFile("small.vcf")
val genotypes = sc.loadGenotypes(genotypesPath)
val variantContexts = genotypes.toVariantContextRDD
assert(variantContexts.sequences.containsRefName("1"))
assert(variantContexts.samples.nonEmpty)
val vcs = variantContexts.rdd.collect
assert(vcs.size === 6)
val vc = vcs.head
assert(vc.position.referenceName === "1")
assert(vc.variant.variant.contigName === "1")
assert(vc.genotypes.nonEmpty)
}
}