/
FragmentRDDSuite.scala
288 lines (232 loc) · 10.5 KB
/
FragmentRDDSuite.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/**
* 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.fragment
import org.bdgenomics.adam.rdd.ADAMContext._
import org.bdgenomics.adam.rdd.read.{
AlignmentRecordRDD,
AnySAMOutFormatter,
QualityScoreBin
}
import org.bdgenomics.adam.util.ADAMFunSuite
import scala.collection.JavaConversions._
class FragmentRDDSuite extends ADAMFunSuite {
sparkTest("don't lose any reads when piping interleaved fastq to sam") {
// write suffixes at end of reads
sc.hadoopConfiguration.setBoolean(FragmentRDD.WRITE_SUFFIXES, true)
val fragmentsPath = testFile("interleaved_fastq_sample1.ifq")
val ardd = sc.loadFragments(fragmentsPath)
val records = ardd.rdd.count
assert(records === 3)
implicit val tFormatter = InterleavedFASTQInFormatter
implicit val uFormatter = new AnySAMOutFormatter
// this script converts interleaved fastq to unaligned sam
val scriptPath = testFile("fastq_to_usam.py")
val pipedRdd: AlignmentRecordRDD = ardd.pipe("python $0",
files = Seq(scriptPath))
val newRecords = pipedRdd.rdd.count
assert(2 * records === newRecords)
}
sparkTest("don't lose any reads when piping tab5 to sam") {
val fragmentsPath = testFile("interleaved_fastq_sample1.ifq")
val ardd = sc.loadFragments(fragmentsPath)
val records = ardd.rdd.count
assert(records === 3)
implicit val tFormatter = Tab5InFormatter
implicit val uFormatter = new AnySAMOutFormatter
// this script converts tab5 to unaligned sam
val scriptPath = testFile("tab5_to_usam.py")
val pipedRdd: AlignmentRecordRDD = ardd.pipe("python $0",
files = Seq(scriptPath))
val newRecords = pipedRdd.rdd.count
assert(2 * records === newRecords)
}
sparkTest("don't lose any reads when piping tab6 to sam") {
// write suffixes at end of reads
sc.hadoopConfiguration.setBoolean(FragmentRDD.WRITE_SUFFIXES, true)
val fragmentsPath = testFile("interleaved_fastq_sample1.ifq")
val ardd = sc.loadFragments(fragmentsPath)
val records = ardd.rdd.count
assert(records === 3)
implicit val tFormatter = Tab6InFormatter
implicit val uFormatter = new AnySAMOutFormatter
// this script converts tab6 to unaligned sam
val scriptPath = testFile("tab6_to_usam.py")
val pipedRdd: AlignmentRecordRDD = ardd.pipe("python $0",
files = Seq(scriptPath))
val newRecords = pipedRdd.rdd.count
assert(2 * records === newRecords)
}
sparkTest("use broadcast join to pull down fragments mapped to targets") {
val fragmentsPath = testFile("small.1.sam")
val targetsPath = testFile("small.1.bed")
val fragments = sc.loadFragments(fragmentsPath)
val targets = sc.loadFeatures(targetsPath)
val jRdd = fragments.broadcastRegionJoin(targets)
assert(jRdd.rdd.count === 5)
}
sparkTest("use right outer broadcast join to pull down fragments mapped to targets") {
val fragmentsPath = testFile("small.1.sam")
val targetsPath = testFile("small.1.bed")
val fragments = sc.loadFragments(fragmentsPath)
val targets = sc.loadFeatures(targetsPath)
val jRdd = fragments.rightOuterBroadcastRegionJoin(targets)
val c = jRdd.rdd.collect
assert(c.count(_._1.isEmpty) === 1)
assert(c.count(_._1.isDefined) === 5)
}
sparkTest("use shuffle join to pull down fragments mapped to targets") {
val fragmentsPath = testFile("small.1.sam")
val targetsPath = testFile("small.1.bed")
val fragments = sc.loadFragments(fragmentsPath)
.transform(_.repartition(1))
val targets = sc.loadFeatures(targetsPath)
.transform(_.repartition(1))
val jRdd = fragments.shuffleRegionJoin(targets)
val jRdd0 = fragments.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 === 5)
assert(jRdd.rdd.count === 5)
assert(jRdd0.rdd.count === 5)
}
sparkTest("use right outer shuffle join to pull down fragments mapped to targets") {
val fragmentsPath = testFile("small.1.sam")
val targetsPath = testFile("small.1.bed")
val fragments = sc.loadFragments(fragmentsPath)
.transform(_.repartition(1))
val targets = sc.loadFeatures(targetsPath)
.transform(_.repartition(1))
val jRdd = fragments.rightOuterShuffleRegionJoin(targets)
val jRdd0 = fragments.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 === 5)
val c = jRdd.rdd.collect
val c0 = jRdd0.rdd.collect
assert(c.count(_._1.isEmpty) === 1)
assert(c0.count(_._1.isEmpty) === 1)
assert(c.count(_._1.isDefined) === 5)
assert(c0.count(_._1.isDefined) === 5)
}
sparkTest("use left outer shuffle join to pull down fragments mapped to targets") {
val fragmentsPath = testFile("small.1.sam")
val targetsPath = testFile("small.1.bed")
val fragments = sc.loadFragments(fragmentsPath)
.transform(_.repartition(1))
val targets = sc.loadFeatures(targetsPath)
.transform(_.repartition(1))
val jRdd = fragments.leftOuterShuffleRegionJoin(targets)
val jRdd0 = fragments.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 === 5)
val c = jRdd.rdd.collect
val c0 = jRdd0.rdd.collect
assert(c.count(_._2.isEmpty) === 15)
assert(c0.count(_._2.isEmpty) === 15)
assert(c.count(_._2.isDefined) === 5)
assert(c0.count(_._2.isDefined) === 5)
}
sparkTest("use full outer shuffle join to pull down fragments mapped to targets") {
val fragmentsPath = testFile("small.1.sam")
val targetsPath = testFile("small.1.bed")
val fragments = sc.loadFragments(fragmentsPath)
.transform(_.repartition(1))
val targets = sc.loadFeatures(targetsPath)
.transform(_.repartition(1))
val jRdd = fragments.fullOuterShuffleRegionJoin(targets)
val jRdd0 = fragments.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 === 5)
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) === 15)
assert(c0.count(t => t._1.isDefined && t._2.isEmpty) === 15)
assert(c.count(t => t._1.isEmpty && t._2.isDefined) === 1)
assert(c0.count(t => t._1.isEmpty && t._2.isDefined) === 1)
assert(c.count(t => t._1.isDefined && t._2.isDefined) === 5)
assert(c0.count(t => t._1.isDefined && t._2.isDefined) === 5)
}
sparkTest("use shuffle join with group by to pull down fragments mapped to targets") {
val fragmentsPath = testFile("small.1.sam")
val targetsPath = testFile("small.1.bed")
val fragments = sc.loadFragments(fragmentsPath)
.transform(_.repartition(1))
val targets = sc.loadFeatures(targetsPath)
.transform(_.repartition(1))
val jRdd = fragments.shuffleRegionJoinAndGroupByLeft(targets)
val jRdd0 = fragments.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 === 5)
val c = jRdd.rdd.collect
val c0 = jRdd0.rdd.collect
assert(c.size === 5)
assert(c0.size === 5)
assert(c.forall(_._2.size == 1))
assert(c0.forall(_._2.size == 1))
}
sparkTest("use right outer shuffle join with group by to pull down fragments mapped to targets") {
val fragmentsPath = testFile("small.1.sam")
val targetsPath = testFile("small.1.bed")
val fragments = sc.loadFragments(fragmentsPath)
.transform(_.repartition(1))
val targets = sc.loadFeatures(targetsPath)
.transform(_.repartition(1))
val jRdd = fragments.rightOuterShuffleRegionJoinAndGroupByLeft(targets)
val jRdd0 = fragments.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 === 5)
val c = jRdd.rdd.collect
val c0 = jRdd0.rdd.collect
assert(c.count(_._1.isDefined) === 20)
assert(c0.count(_._1.isDefined) === 20)
assert(c.filter(_._1.isDefined).count(_._2.size == 1) === 5)
assert(c0.filter(_._1.isDefined).count(_._2.size == 1) === 5)
assert(c.filter(_._1.isDefined).count(_._2.isEmpty) === 15)
assert(c0.filter(_._1.isDefined).count(_._2.isEmpty) === 15)
assert(c.count(_._1.isEmpty) === 1)
assert(c0.count(_._1.isEmpty) === 1)
assert(c.filter(_._1.isEmpty).forall(_._2.size == 1))
assert(c0.filter(_._1.isEmpty).forall(_._2.size == 1))
}
sparkTest("bin quality scores in fragments") {
val fragments = sc.loadFragments(testFile("bqsr1.sam"))
val binnedFragments = fragments.binQualityScores(Seq(QualityScoreBin(0, 20, 10),
QualityScoreBin(20, 40, 30),
QualityScoreBin(40, 60, 50)))
val qualityScoreCounts = binnedFragments.rdd.flatMap(fragment => {
fragment.getAlignments.toSeq
}).flatMap(read => {
read.getQual
}).map(s => s.toInt - 33)
.countByValue
assert(qualityScoreCounts(30) === 92899)
assert(qualityScoreCounts(10) === 7101)
}
}