/
FeatureRDD.scala
581 lines (522 loc) · 21.5 KB
/
FeatureRDD.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
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
/**
* 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.feature
import com.google.common.collect.ComparisonChain
import java.util.Comparator
import org.apache.hadoop.fs.{ FileSystem, Path }
import org.apache.parquet.hadoop.metadata.CompressionCodecName
import org.apache.spark.SparkContext
import org.apache.spark.rdd.RDD
import org.apache.spark.sql.{ Dataset, SQLContext }
import org.bdgenomics.adam.instrumentation.Timers._
import org.bdgenomics.adam.models._
import org.bdgenomics.adam.rdd.ADAMContext._
import org.bdgenomics.adam.rdd.{
AvroGenomicDataset,
JavaSaveArgs,
SAMHeaderWriter
}
import org.bdgenomics.adam.serialization.AvroSerializer
import org.bdgenomics.adam.sql.{ Feature => FeatureProduct }
import org.bdgenomics.adam.util.FileMerger
import org.bdgenomics.formats.avro.{ Feature, Strand }
import org.bdgenomics.utils.interval.array.{
IntervalArray,
IntervalArraySerializer
}
import scala.collection.JavaConversions._
import scala.math.max
import scala.reflect.ClassTag
import scala.reflect.runtime.universe._
private[adam] case class FeatureArray(
array: Array[(ReferenceRegion, Feature)],
maxIntervalWidth: Long) extends IntervalArray[ReferenceRegion, Feature] {
def duplicate(): IntervalArray[ReferenceRegion, Feature] = {
copy()
}
protected def replace(arr: Array[(ReferenceRegion, Feature)],
maxWidth: Long): IntervalArray[ReferenceRegion, Feature] = {
FeatureArray(arr, maxWidth)
}
}
private[adam] class FeatureArraySerializer extends IntervalArraySerializer[ReferenceRegion, Feature, FeatureArray] {
protected val kSerializer = new ReferenceRegionSerializer
protected val tSerializer = new AvroSerializer[Feature]
protected def builder(arr: Array[(ReferenceRegion, Feature)],
maxIntervalWidth: Long): FeatureArray = {
FeatureArray(arr, maxIntervalWidth)
}
}
private trait FeatureOrdering[T <: Feature] extends Ordering[T] {
def allowNull(s: java.lang.String): java.lang.Integer = {
if (s == null) {
return null
}
java.lang.Integer.parseInt(s)
}
def compare(x: Feature, y: Feature) = {
val doubleNullsLast: Comparator[java.lang.Double] = com.google.common.collect.Ordering.natural().nullsLast()
val intNullsLast: Comparator[java.lang.Integer] = com.google.common.collect.Ordering.natural().nullsLast()
val strandNullsLast: Comparator[Strand] = com.google.common.collect.Ordering.natural().nullsLast()
val stringNullsLast: Comparator[java.lang.String] = com.google.common.collect.Ordering.natural().nullsLast()
// use ComparisonChain to safely handle nulls, as Feature is a java object
ComparisonChain.start()
// consider reference region first
.compare(x.getContigName, y.getContigName)
.compare(x.getStart, y.getStart)
.compare(x.getEnd, y.getEnd)
.compare(x.getStrand, y.getStrand, strandNullsLast)
// then feature fields
.compare(x.getFeatureId, y.getFeatureId, stringNullsLast)
.compare(x.getFeatureType, y.getFeatureType, stringNullsLast)
.compare(x.getName, y.getName, stringNullsLast)
.compare(x.getSource, y.getSource, stringNullsLast)
.compare(x.getPhase, y.getPhase, intNullsLast)
.compare(x.getFrame, y.getFrame, intNullsLast)
.compare(x.getScore, y.getScore, doubleNullsLast)
// finally gene structure
.compare(x.getGeneId, y.getGeneId, stringNullsLast)
.compare(x.getTranscriptId, y.getTranscriptId, stringNullsLast)
.compare(x.getExonId, y.getExonId, stringNullsLast)
.compare(allowNull(x.getAttributes.get("exon_number")), allowNull(y.getAttributes.get("exon_number")), intNullsLast)
.compare(allowNull(x.getAttributes.get("intron_number")), allowNull(y.getAttributes.get("intron_number")), intNullsLast)
.compare(allowNull(x.getAttributes.get("rank")), allowNull(y.getAttributes.get("rank")), intNullsLast)
.result()
}
}
private object FeatureOrdering extends FeatureOrdering[Feature] {}
object FeatureRDD {
/**
* A GenomicRDD that wraps a dataset of Feature data.
*
* @param ds A Dataset of genomic Features.
* @param sequences The reference genome these data are aligned to.
*/
def apply(ds: Dataset[FeatureProduct],
sequences: SequenceDictionary): FeatureRDD = {
new DatasetBoundFeatureRDD(ds, sequences)
}
/**
* Builds a FeatureRDD with an empty sequence dictionary.
*
* @param rdd The underlying Feature RDD to build from.
* @return Returns a new FeatureRDD.
*/
def apply(rdd: RDD[Feature]): FeatureRDD = {
FeatureRDD(rdd, SequenceDictionary.empty)
}
/**
* Builds a FeatureRDD given a sequence dictionary.
*
* @param rdd The underlying Feature RDD to build from.
* @param sd The sequence dictionary for this FeatureRDD.
* @return Returns a new FeatureRDD.
*/
def apply(rdd: RDD[Feature], sd: SequenceDictionary): FeatureRDD = {
new RDDBoundFeatureRDD(rdd, sd, None)
}
/**
* @param feature Feature to convert to GTF format.
* @return Returns this feature as a GTF line.
*/
private[feature] def toGtf(feature: Feature): String = {
def escape(entry: (Any, Any)): String = {
entry._1 + " \"" + entry._2 + "\""
}
val seqname = feature.getContigName
val source = Option(feature.getSource).getOrElse(".")
val featureType = Option(feature.getFeatureType).getOrElse(".")
val start = feature.getStart + 1 // GTF/GFF ranges are 1-based
val end = feature.getEnd // GTF/GFF ranges are closed
val score = Option(feature.getScore).getOrElse(".")
val strand = Features.asString(feature.getStrand)
val frame = Option(feature.getFrame).getOrElse(".")
val attributes = Features.gatherAttributes(feature).map(escape).mkString("; ")
List(seqname, source, featureType, start, end, score, strand, frame, attributes).mkString("\t")
}
/**
* @param feature Feature to write in IntervalList format.
* @return Feature as a one line interval list string.
*/
private[rdd] def toInterval(feature: Feature): String = {
val sequenceName = feature.getContigName
val start = feature.getStart + 1 // IntervalList ranges are 1-based
val end = feature.getEnd // IntervalList ranges are closed
val strand = Features.asString(feature.getStrand, emptyUnknown = false)
val name = Features.nameOf(feature)
List(sequenceName, start, end, strand, name).mkString("\t")
}
/**
* @param feature Feature to write in the narrow peak format.
* @return Returns this feature as a single narrow peak line.
*/
private[rdd] def toNarrowPeak(feature: Feature): String = {
val chrom = feature.getContigName
val start = feature.getStart
val end = feature.getEnd
val name = Features.nameOf(feature)
val score = Option(feature.getScore).map(_.toInt).getOrElse(".")
val strand = Features.asString(feature.getStrand)
val signalValue = feature.getAttributes.getOrElse("signalValue", "0")
val pValue = feature.getAttributes.getOrElse("pValue", "-1")
val qValue = feature.getAttributes.getOrElse("qValue", "-1")
val peak = feature.getAttributes.getOrElse("peak", "-1")
List(chrom, start, end, name, score, strand, signalValue, pValue, qValue, peak).mkString("\t")
}
/**
* @param feature Feature to write in BED format.
* @return Returns the feature as a single line BED string.
*/
private[rdd] def toBed(feature: Feature): String = {
val chrom = feature.getContigName
val start = feature.getStart
val end = feature.getEnd
val name = Features.nameOf(feature)
val score = Option(feature.getScore).getOrElse(".")
val strand = Features.asString(feature.getStrand)
if (!feature.getAttributes.containsKey("thickStart") &&
!feature.getAttributes.containsKey("itemRgb") &&
!feature.getAttributes.containsKey("blockCount")) {
// write BED6 format
List(chrom, start, end, name, score, strand).mkString("\t")
} else {
// write BED12 format
val thickStart = feature.getAttributes.getOrElse("thickStart", ".")
val thickEnd = feature.getAttributes.getOrElse("thickEnd", ".")
val itemRgb = feature.getAttributes.getOrElse("itemRgb", ".")
val blockCount = feature.getAttributes.getOrElse("blockCount", ".")
val blockSizes = feature.getAttributes.getOrElse("blockSizes", ".")
val blockStarts = feature.getAttributes.getOrElse("blockStarts", ".")
List(chrom, start, end, name, score, strand, thickStart, thickEnd, itemRgb, blockCount, blockSizes, blockStarts).mkString("\t")
}
}
/**
* @param feature Feature to write in GFF3 format.
* @return Returns this feature as a single line GFF3 string.
*/
private[rdd] def toGff3(feature: Feature): String = {
def escape(entry: (Any, Any)): String = {
entry._1 + "=" + entry._2
}
val seqid = feature.getContigName
val source = Option(feature.getSource).getOrElse(".")
val featureType = Option(feature.getFeatureType).getOrElse(".")
val start = feature.getStart + 1 // GFF3 coordinate system is 1-based
val end = feature.getEnd // GFF3 ranges are closed
val score = Option(feature.getScore).getOrElse(".")
val strand = Features.asString(feature.getStrand)
val phase = Option(feature.getPhase).getOrElse(".")
val attributes = Features.gatherAttributes(feature).map(escape).mkString(";")
List(seqid, source, featureType, start, end, score, strand, phase, attributes).mkString("\t")
}
}
case class ParquetUnboundFeatureRDD private[rdd] (
@transient private val sc: SparkContext,
private val parquetFilename: String,
sequences: SequenceDictionary) extends FeatureRDD {
lazy val rdd: RDD[Feature] = {
sc.loadParquet(parquetFilename)
}
protected lazy val optPartitionMap = sc.extractPartitionMap(parquetFilename)
lazy val dataset = {
val sqlContext = SQLContext.getOrCreate(sc)
import sqlContext.implicits._
sqlContext.read.parquet(parquetFilename).as[FeatureProduct]
}
def replaceSequences(newSequences: SequenceDictionary): FeatureRDD = {
copy(sequences = newSequences)
}
def toCoverage(): CoverageRDD = {
ParquetUnboundCoverageRDD(sc, parquetFilename, sequences)
}
}
case class DatasetBoundFeatureRDD private[rdd] (
dataset: Dataset[FeatureProduct],
sequences: SequenceDictionary) extends FeatureRDD {
lazy val rdd = dataset.rdd.map(_.toAvro)
protected lazy val optPartitionMap = None
override def saveAsParquet(filePath: String,
blockSize: Int = 128 * 1024 * 1024,
pageSize: Int = 1 * 1024 * 1024,
compressCodec: CompressionCodecName = CompressionCodecName.GZIP,
disableDictionaryEncoding: Boolean = false) {
log.warn("Saving directly as Parquet from SQL. Options other than compression codec are ignored.")
dataset.toDF()
.write
.format("parquet")
.option("spark.sql.parquet.compression.codec", compressCodec.toString.toLowerCase())
.save(filePath)
saveMetadata(filePath)
}
override def transformDataset(
tFn: Dataset[FeatureProduct] => Dataset[FeatureProduct]): FeatureRDD = {
copy(dataset = tFn(dataset))
}
def replaceSequences(newSequences: SequenceDictionary): FeatureRDD = {
copy(sequences = newSequences)
}
def toCoverage(): CoverageRDD = {
import dataset.sqlContext.implicits._
DatasetBoundCoverageRDD(dataset.toDF
.select("contigName", "start", "end", "score")
.withColumnRenamed("score", "count")
.as[Coverage], sequences)
}
}
case class RDDBoundFeatureRDD private[rdd] (
rdd: RDD[Feature],
sequences: SequenceDictionary,
optPartitionMap: Option[Array[Option[(ReferenceRegion, ReferenceRegion)]]]) extends FeatureRDD {
/**
* A SQL Dataset of reads.
*/
lazy val dataset: Dataset[FeatureProduct] = {
val sqlContext = SQLContext.getOrCreate(rdd.context)
import sqlContext.implicits._
sqlContext.createDataset(rdd.map(FeatureProduct.fromAvro))
}
def replaceSequences(newSequences: SequenceDictionary): FeatureRDD = {
copy(sequences = newSequences)
}
def toCoverage(): CoverageRDD = {
val coverageRdd = rdd.map(f => Coverage(f))
RDDBoundCoverageRDD(coverageRdd, sequences, optPartitionMap)
}
}
sealed abstract class FeatureRDD extends AvroGenomicDataset[Feature, FeatureProduct, FeatureRDD] {
protected val productFn = FeatureProduct.fromAvro(_)
protected val unproductFn = (f: FeatureProduct) => f.toAvro
@transient val uTag: TypeTag[FeatureProduct] = typeTag[FeatureProduct]
protected def buildTree(rdd: RDD[(ReferenceRegion, Feature)])(
implicit tTag: ClassTag[Feature]): IntervalArray[ReferenceRegion, Feature] = {
IntervalArray(rdd, FeatureArray.apply(_, _))
}
def union(rdds: FeatureRDD*): FeatureRDD = {
val iterableRdds = rdds.toSeq
FeatureRDD(rdd.context.union(rdd, iterableRdds.map(_.rdd): _*),
iterableRdds.map(_.sequences).fold(sequences)(_ ++ _))
}
/**
* Applies a function that transforms the underlying RDD into a new RDD using
* the Spark SQL API.
*
* @param tFn A function that transforms the underlying RDD as a Dataset.
* @return A new RDD where the RDD of genomic data has been replaced, but the
* metadata (sequence dictionary, and etc) is copied without modification.
*/
def transformDataset(
tFn: Dataset[FeatureProduct] => Dataset[FeatureProduct]): FeatureRDD = {
DatasetBoundFeatureRDD(tFn(dataset), sequences)
}
/**
* Java friendly save function. Automatically detects the output format.
*
* Writes files ending in .bed as BED6/12, .gff3 as GFF3, .gtf/.gff as
* GTF/GFF2, .narrow[pP]eak as NarrowPeak, and .interval_list as
* IntervalList. If none of these match, we fall back to Parquet.
* These files are written as sharded text files.
*
* @param filePath The location to write the output.
* @param asSingleFile If false, writes file to disk as shards with
* one shard per partition. If true, we save the file to disk as a single
* file by merging the shards.
* @param disableFastConcat If asSingleFile is true, disables the use of the
* fast file concatenation engine.
*/
def save(filePath: java.lang.String,
asSingleFile: java.lang.Boolean,
disableFastConcat: java.lang.Boolean) {
if (filePath.endsWith(".bed")) {
saveAsBed(filePath,
asSingleFile = asSingleFile,
disableFastConcat = disableFastConcat)
} else if (filePath.endsWith(".gtf") ||
filePath.endsWith(".gff")) {
saveAsGtf(filePath,
asSingleFile = asSingleFile,
disableFastConcat = disableFastConcat)
} else if (filePath.endsWith(".gff3")) {
saveAsGff3(filePath,
asSingleFile = asSingleFile,
disableFastConcat = disableFastConcat)
} else if (filePath.endsWith(".narrowPeak") ||
filePath.endsWith(".narrowpeak")) {
saveAsNarrowPeak(filePath,
asSingleFile = asSingleFile,
disableFastConcat = disableFastConcat)
} else if (filePath.endsWith(".interval_list")) {
saveAsIntervalList(filePath,
asSingleFile = asSingleFile,
disableFastConcat = disableFastConcat)
} else {
if (asSingleFile) {
log.warn("asSingleFile = true ignored when saving as Parquet.")
}
saveAsParquet(new JavaSaveArgs(filePath))
}
}
/**
* Converts the FeatureRDD to a CoverageRDD.
*
* @return CoverageRDD containing RDD of Coverage.
*/
def toCoverage(): CoverageRDD
/**
* @param newRdd The RDD to replace the underlying RDD with.
* @return Returns a new FeatureRDD with the underlying RDD replaced.
*/
protected def replaceRdd(newRdd: RDD[Feature],
newPartitionMap: Option[Array[Option[(ReferenceRegion, ReferenceRegion)]]] = None): FeatureRDD = {
new RDDBoundFeatureRDD(newRdd, sequences, newPartitionMap)
}
/**
* @param elem The Feature to get an underlying region for.
* @return Since a feature maps directly to a single genomic region, this
* method will always return a Seq of exactly one ReferenceRegion.
*/
protected def getReferenceRegions(elem: Feature): Seq[ReferenceRegion] = {
Seq(ReferenceRegion.unstranded(elem))
}
/**
* Save this FeatureRDD in GTF format.
*
* @param fileName The path to save GTF formatted text file(s) to.
* @param asSingleFile By default (false), writes file to disk as shards with
* one shard per partition. If true, we save the file to disk as a single
* file by merging the shards.
* @param disableFastConcat If asSingleFile is true, disables the use of the
* parallel file merging engine.
*/
def saveAsGtf(fileName: String,
asSingleFile: Boolean = false,
disableFastConcat: Boolean = false) = {
writeTextRdd(rdd.map(FeatureRDD.toGtf),
fileName,
asSingleFile,
disableFastConcat)
}
/**
* Save this FeatureRDD in GFF3 format.
*
* @param fileName The path to save GFF3 formatted text file(s) to.
* @param asSingleFile By default (false), writes file to disk as shards with
* one shard per partition. If true, we save the file to disk as a single
* file by merging the shards.
* @param disableFastConcat If asSingleFile is true, disables the use of the
* parallel file merging engine.
*/
def saveAsGff3(fileName: String,
asSingleFile: Boolean = false,
disableFastConcat: Boolean = false) = {
val optHeaderPath = if (asSingleFile) {
val headerPath = "%s_head".format(fileName)
GFF3HeaderWriter(headerPath, rdd.context)
Some(headerPath)
} else {
None
}
writeTextRdd(rdd.map(FeatureRDD.toGff3),
fileName,
asSingleFile,
disableFastConcat,
optHeaderPath = optHeaderPath)
}
/**
* Save this FeatureRDD in BED format.
*
* @param fileName The path to save BED formatted text file(s) to.
* @param asSingleFile By default (false), writes file to disk as shards with
* one shard per partition. If true, we save the file to disk as a single
* file by merging the shards.
* @param disableFastConcat If asSingleFile is true, disables the use of the
* parallel file merging engine.
*/
def saveAsBed(fileName: String,
asSingleFile: Boolean = false,
disableFastConcat: Boolean = false) = {
writeTextRdd(rdd.map(FeatureRDD.toBed),
fileName,
asSingleFile,
disableFastConcat)
}
/**
* Save this FeatureRDD in interval list format.
*
* @param fileName The path to save interval list formatted text file(s) to.
* @param asSingleFile By default (false), writes file to disk as shards with
* one shard per partition. If true, we save the file to disk as a single
* file by merging the shards.
* @param disableFastConcat If asSingleFile is true, disables the use of the
* parallel file merging engine.
*/
def saveAsIntervalList(fileName: String,
asSingleFile: Boolean = false,
disableFastConcat: Boolean = false) = {
val intervalEntities = rdd.map(FeatureRDD.toInterval)
if (asSingleFile) {
// get fs
val fs = FileSystem.get(rdd.context.hadoopConfiguration)
// write sam file header
val headPath = new Path("%s_head".format(fileName))
SAMHeaderWriter.writeHeader(fs,
headPath,
sequences)
// write tail entries
val tailPath = new Path("%s_tail".format(fileName))
intervalEntities.saveAsTextFile(tailPath.toString)
// merge
FileMerger.mergeFiles(rdd.context,
fs,
new Path(fileName),
tailPath,
optHeaderPath = Some(headPath),
disableFastConcat = disableFastConcat)
} else {
intervalEntities.saveAsTextFile(fileName)
}
}
/**
* Save this FeatureRDD in NarrowPeak format.
*
* @param fileName The path to save NarrowPeak formatted text file(s) to.
* @param asSingleFile By default (false), writes file to disk as shards with
* one shard per partition. If true, we save the file to disk as a single
* file by merging the shards.
* @param disableFastConcat If asSingleFile is true, disables the use of the
* parallel file merging engine.
*/
def saveAsNarrowPeak(fileName: String,
asSingleFile: Boolean = false,
disableFastConcat: Boolean = false) {
writeTextRdd(rdd.map(FeatureRDD.toNarrowPeak),
fileName,
asSingleFile,
disableFastConcat)
}
/**
* Sorts the RDD by the reference ordering.
*
* @param ascending Whether to sort in ascending order or not.
* @param numPartitions The number of partitions to have after sorting.
* Defaults to the partition count of the underlying RDD.
*/
def sortByReference(ascending: Boolean = true, numPartitions: Int = rdd.partitions.length): FeatureRDD = {
implicit def ord = FeatureOrdering
replaceRdd(rdd.sortBy(f => f, ascending, numPartitions))
}
}