-
Notifications
You must be signed in to change notification settings - Fork 309
/
alignmentRecordRdd_test.py
361 lines (215 loc) · 10.3 KB
/
alignmentRecordRdd_test.py
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#
# 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.
#
from bdgenomics.adam.adamContext import ADAMContext
from bdgenomics.adam.models import ReferenceRegion
from bdgenomics.adam.rdd import AlignmentRecordRDD, CoverageRDD
from bdgenomics.adam.test import SparkTestCase
from pyspark.sql.types import DoubleType
from pyspark.storagelevel import StorageLevel
class AlignmentRecordRDDTest(SparkTestCase):
def test_save_sorted_sam(self):
testFile = self.resourceFile("sorted.sam")
ac = ADAMContext(self.ss)
reads = ac.loadAlignments(testFile)
tmpPath = self.tmpFile() + ".sam"
sortedReads = reads.sortReadsByReferencePosition()
sortedReads.saveAsSam(tmpPath,
isSorted=True,
asSingleFile=True)
self.checkFiles(testFile, tmpPath)
def test_save_unordered_sam(self):
testFile = self.resourceFile("unordered.sam")
ac = ADAMContext(self.ss)
reads = ac.loadAlignments(testFile)
tmpPath = self.tmpFile() + ".sam"
reads.saveAsSam(tmpPath,
asSingleFile=True)
self.checkFiles(testFile, tmpPath)
def test_union(self):
testFile1 = self.resourceFile("sorted.sam")
testFile2 = self.resourceFile("unordered.sam")
ac = ADAMContext(self.ss)
reads1 = ac.loadAlignments(testFile1)
reads2 = ac.loadAlignments(testFile2)
unionReads = reads1.union([reads2])
self.assertEqual(unionReads.toDF().count(), 13)
def test_save_as_bam(self):
testFile = self.resourceFile("sorted.sam")
ac = ADAMContext(self.ss)
reads = ac.loadAlignments(testFile)
tmpPath = self.tmpFile() + ".bam"
reads.saveAsSam(tmpPath,
isSorted=True,
asSingleFile=True)
bamReads = ac.loadAlignments(tmpPath)
self.assertEqual(bamReads._jvmRdd.jrdd().count(),
reads._jvmRdd.jrdd().count())
def test_count_kmers(self):
testFile = self.resourceFile("small.sam")
ac = ADAMContext(self.ss)
reads = ac.loadAlignments(testFile)
kmers = reads.countKmers(6)
self.assertEqual(kmers.count(), 1040)
def test_pipe_as_sam(self):
reads12Path = self.resourceFile("reads12.sam")
ac = ADAMContext(self.ss)
reads = ac.loadAlignments(reads12Path)
pipedRdd = reads.pipe(["tee", "/dev/null"],
"org.bdgenomics.adam.rdd.read.SAMInFormatter",
"org.bdgenomics.adam.rdd.read.AnySAMOutFormatter",
"org.bdgenomics.adam.api.java.AlignmentRecordsToAlignmentRecordsConverter")
self.assertEqual(reads.toDF().count(), pipedRdd.toDF().count())
def test_transform(self):
readsPath = self.resourceFile("unsorted.sam")
ac = ADAMContext(self.ss)
reads = ac.loadAlignments(readsPath)
transformedReads = reads.transform(lambda x: x.filter(x.contigName == "1"))
self.assertEqual(transformedReads.toDF().count(), 1)
def test_transmute_to_coverage(self):
readsPath = self.resourceFile("unsorted.sam")
ac = ADAMContext(self.ss)
reads = ac.loadAlignments(readsPath)
readsAsCoverage = reads.transmute(lambda x: x.select(x.contigName,
x.start,
x.end,
x.mapq.cast(DoubleType()).alias("count"),
x.recordGroupSample.alias("optSampleId")),
CoverageRDD)
assert(isinstance(readsAsCoverage, CoverageRDD))
self.assertEqual(readsAsCoverage.toDF().count(), 5)
def test_to_coverage(self):
readsPath = self.resourceFile("unsorted.sam")
ac = ADAMContext(self.ss)
reads = ac.loadAlignments(readsPath)
coverage = reads.toCoverage()
self.assertEqual(coverage.toDF().count(), 5)
# 5 reads: contig 3 has 8 bp, chr2 (2 strands) has 20bp, contig 4 has 8bp, contig 1 has 10 bp
# 8 + 20 + 8 + 10 = 46
coverage = reads.toCoverage(collapse = False)
self.assertEqual(coverage.toDF().count(), 46)
def test_to_fragments(self):
readsPath = self.resourceFile("unsorted.sam")
ac = ADAMContext(self.ss)
reads = ac.loadAlignments(readsPath)
fragments = reads.toFragments()
self.assertEqual(fragments.toDF().count(), 5)
def test_load_indexed_bam(self):
readsPath = self.resourceFile("indexed_bams/sorted.bam")
ac = ADAMContext(self.ss)
querys = [ReferenceRegion("chr2", 100, 101), ReferenceRegion("3", 10, 17)]
reads = ac.loadIndexedBam(readsPath, querys)
self.assertEqual(reads.toDF().count(), 2)
def test_filterByOverlappingRegion(self):
readsPath = self.resourceFile("unsorted.sam")
ac = ADAMContext(self.ss)
reads = ac.loadAlignments(readsPath)
query = ReferenceRegion("chr2", 1, 400)
filtered = reads.filterByOverlappingRegion(query)
self.assertEqual(filtered.toDF().count(), 1)
def test_filterByOverlappingRegions(self):
readsPath = self.resourceFile("unsorted.sam")
ac = ADAMContext(self.ss)
reads = ac.loadAlignments(readsPath)
querys = [ReferenceRegion("chr2", 1, 400),
ReferenceRegion("3", 1, 100)]
filtered = reads.filterByOverlappingRegions(querys)
self.assertEqual(filtered.toDF().count(), 2)
def test_caching(self):
readsPath = self.resourceFile("unsorted.sam")
ac = ADAMContext(self.ss)
reads = ac.loadAlignments(readsPath)
cachedReads = reads.cache()
cached = self.sc._jsc.getPersistentRDDs()
self.assertEqual(cached.isEmpty(), False)
cachedReads.unpersist()
cached = self.sc._jsc.getPersistentRDDs()
self.assertEqual(cached.isEmpty(), True)
def test_persisting(self):
readsPath = self.resourceFile("unsorted.sam")
ac = ADAMContext(self.ss)
reads = ac.loadAlignments(readsPath)
persistedReads = reads.persist(StorageLevel.DISK_ONLY)
cached = self.sc._jsc.getPersistentRDDs()
self.assertEqual(cached.isEmpty(), False)
persistedReads.unpersist()
cached = self.sc._jsc.getPersistentRDDs()
self.assertEqual(cached.isEmpty(), True)
def test_broadcast_inner_join(self):
readsPath = self.resourceFile("small.1.sam")
targetsPath = self.resourceFile("small.1.bed")
ac = ADAMContext(self.ss)
reads = ac.loadAlignments(readsPath)
targets = ac.loadFeatures(targetsPath)
jRdd = reads.broadcastRegionJoin(targets)
self.assertEqual(jRdd.toDF().count(), 5)
def test_broadcast_right_outer_join(self):
readsPath = self.resourceFile("small.1.sam")
targetsPath = self.resourceFile("small.1.bed")
ac = ADAMContext(self.ss)
reads = ac.loadAlignments(readsPath)
targets = ac.loadFeatures(targetsPath)
jRdd = reads.rightOuterBroadcastRegionJoin(targets)
self.assertEqual(jRdd.toDF().count(), 6)
def test_shuffle_inner_join(self):
readsPath = self.resourceFile("small.1.sam")
targetsPath = self.resourceFile("small.1.bed")
ac = ADAMContext(self.ss)
reads = ac.loadAlignments(readsPath)
targets = ac.loadFeatures(targetsPath)
jRdd = reads.shuffleRegionJoin(targets)
self.assertEqual(jRdd.toDF().count(), 5)
def test_shuffle_right_outer_join(self):
readsPath = self.resourceFile("small.1.sam")
targetsPath = self.resourceFile("small.1.bed")
ac = ADAMContext(self.ss)
reads = ac.loadAlignments(readsPath)
targets = ac.loadFeatures(targetsPath)
jRdd = reads.rightOuterShuffleRegionJoin(targets)
self.assertEqual(jRdd.toDF().count(), 6)
def test_shuffle_left_outer_join(self):
readsPath = self.resourceFile("small.1.sam")
targetsPath = self.resourceFile("small.1.bed")
ac = ADAMContext(self.ss)
reads = ac.loadAlignments(readsPath)
targets = ac.loadFeatures(targetsPath)
jRdd = reads.leftOuterShuffleRegionJoin(targets)
self.assertEqual(jRdd.toDF().count(), 20)
def test_shuffle_full_outer_join(self):
readsPath = self.resourceFile("small.1.sam")
targetsPath = self.resourceFile("small.1.bed")
ac = ADAMContext(self.ss)
reads = ac.loadAlignments(readsPath)
targets = ac.loadFeatures(targetsPath)
jRdd = reads.fullOuterShuffleRegionJoin(targets)
self.assertEqual(jRdd.toDF().count(), 21)
def test_shuffle_inner_join_groupBy_left(self):
readsPath = self.resourceFile("small.1.sam")
targetsPath = self.resourceFile("small.1.bed")
ac = ADAMContext(self.ss)
reads = ac.loadAlignments(readsPath)
targets = ac.loadFeatures(targetsPath)
jRdd = reads.shuffleRegionJoinAndGroupByLeft(targets)
self.assertEqual(jRdd.toDF().count(), 5)
def test_shuffle_right_outer_join_groupBy_left(self):
readsPath = self.resourceFile("small.1.sam")
targetsPath = self.resourceFile("small.1.bed")
ac = ADAMContext(self.ss)
reads = ac.loadAlignments(readsPath)
targets = ac.loadFeatures(targetsPath)
jRdd = reads.rightOuterShuffleRegionJoinAndGroupByLeft(targets)
self.assertEqual(jRdd.toDF().count(), 21)