/
ReadSets.scala
338 lines (291 loc) · 10.3 KB
/
ReadSets.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
package org.hammerlab.genomics.readsets
import grizzled.slf4j.Logging
import hammerlab.path._
import htsjdk.samtools.ValidationStringency
import org.apache.spark.SparkContext
import org.apache.spark.rdd.RDD
import org.bdgenomics.adam.models.SequenceDictionary
import org.bdgenomics.adam.rdd.ADAMContext
import org.hammerlab.bam
import org.hammerlab.genomics.loci.parsing.All
import org.hammerlab.genomics.loci.set.LociSet
import org.hammerlab.genomics.reads.Read
import org.hammerlab.genomics.readsets.args.Base
import org.hammerlab.genomics.readsets.io.{ Config, Input, Sample }
import org.hammerlab.genomics.readsets.rdd.ReadsRDD
import org.hammerlab.genomics.reference.{ ContigLengths, ContigName, Locus }
import org.hammerlab.spark.Context
import spark_bam._
/**
* A [[ReadSets]] contains reads from multiple inputs as well as [[SequenceDictionary]] / contig-length information
* merged from them.
*/
case class ReadSets(readsRDDs: PerSample[ReadsRDD],
sequenceDictionary: SequenceDictionary,
contigLengths: ContigLengths) {
def samples: PerSample[Sample] = readsRDDs.map(_.sample)
def numSamples: NumSamples = readsRDDs.length
def sampleNames: PerSample[String] = samples.map(_.name)
def sc = readsRDDs.head.reads.sparkContext
lazy val mappedReadsRDDs = readsRDDs.map(_.mappedReads)
lazy val allMappedReads = sc.union(mappedReadsRDDs).setName("unioned reads")
lazy val sampleIdxKeyedMappedReads: RDD[SampleRead] =
sc.union(
for {
(mappedReadsRDD, sampleId) ← mappedReadsRDDs.zipWithIndex
} yield
mappedReadsRDD.map(r ⇒ (sampleId → r): SampleRead)
)
}
object ReadSets extends Logging {
implicit def toRDDs(readsets: ReadSets): PerSample[ReadsRDD] = readsets.readsRDDs
import hammerlab.shapeless._
def apply(args: Base)(
implicit
sc: SparkContext,
cf: ContigName.Factory
): (ReadSets, LociSet) = {
val config = args.readFilterArgs.parseConfig(sc.hadoopConfiguration)
val readsets =
apply(
args.inputs,
config,
!args.noSequenceDictionaryArgs.noSequenceDictionary
)
(
readsets,
LociSet(
config.loci,
readsets.contigLengths
)
)
}
/**
* Load reads from multiple files, merging their sequence dictionaries and verifying that they are consistent.
*/
def apply(inputs: Inputs,
config: Config,
contigLengthsFromDictionary: Boolean = true)(
implicit
sc: SparkContext,
cf: ContigName.Factory
): ReadSets =
apply(
inputs.map((_, config)),
contigLengthsFromDictionary
)
/**
* Load reads from multiple files, allowing different filters to be applied to each file.
*/
def apply(inputsAndFilters: PerSample[(Input, Config)],
contigLengthsFromDictionary: Boolean)(
implicit
sc: SparkContext,
cf: ContigName.Factory
): ReadSets = {
val (inputs, _) = inputsAndFilters.unzip
val (readsRDDs, sequenceDictionaries) =
(for {
(Input(id, _, path), config) ← inputsAndFilters
} yield
load(path, id, config)
)
.unzip
val sequenceDictionary = mergeSequenceDictionaries(inputs, sequenceDictionaries)
val contigLengths: ContigLengths =
if (contigLengthsFromDictionary)
getContigLengthsFromSequenceDictionary(sequenceDictionary)
else
sc.union(readsRDDs)
.flatMap(_.asMappedRead)
.map(read ⇒ read.contigName → read.end)
.reduceByKey(_ max _)
.collectAsMap()
.toMap
ReadSets(
(for {
(reads, input) ← readsRDDs.zip(inputs)
} yield
ReadsRDD(reads, input)
)
.toVector,
sequenceDictionary,
contigLengths
)
}
def apply(readsRDDs: PerSample[ReadsRDD], sequenceDictionary: SequenceDictionary): ReadSets =
ReadSets(
readsRDDs,
sequenceDictionary,
getContigLengths(sequenceDictionary)
)
/**
* Given a filename and a spark context, return a pair (RDD, SequenceDictionary), where the first element is an RDD
* of Reads, and the second element is the Sequence Dictionary giving info (e.g. length) about the contigs in the BAM.
*
* @param path name of file containing reads
* @param sc spark context
* @param config config to apply
* @return
*/
private[readsets] def load(path: Path,
sampleId: Int,
config: Config)(
implicit
sc: SparkContext,
cf: ContigName.Factory
): (RDD[Read], SequenceDictionary) = {
val (allReads, sequenceDictionary) =
if (path.toString.endsWith(".bam") || path.toString.endsWith(".sam"))
loadFromBAM(path, sampleId, config)
else
loadFromADAM(path, sampleId, config)
val reads = filterRDD(allReads, config, sequenceDictionary)
(reads, sequenceDictionary)
}
/** Returns an RDD of Reads and SequenceDictionary from reads in BAM format **/
private def loadFromBAM(path: Path,
sampleId: Int,
config: Config)(
implicit
sc: Context,
cf: ContigName.Factory
): (RDD[Read], SequenceDictionary) = {
val contigLengths = bam.header.ContigLengths(path)
val sequenceDictionary = SequenceDictionary(contigLengths)
implicit val splitSize = config.maxSplitSize
val reads =
config
.overlapsLoci
.filterNot(_ == All)
.fold {
sc
.loadReads(
path,
splitSize = config.maxSplitSize
)
} {
loci ⇒
sc
.loadBamIntervals(
path,
LociSet(
loci,
contigLengths.values.toMap
)
)
}
.map(Read(_))
(reads, sequenceDictionary)
}
/** Returns an RDD of Reads and SequenceDictionary from reads in ADAM format **/
private def loadFromADAM(path: Path,
sampleId: Int,
config: Config)(
implicit
sc: SparkContext,
cf: ContigName.Factory
): (RDD[Read], SequenceDictionary) = {
logger.info(s"Using ADAM to read: $path")
import ADAMContext._
val alignmentRDD = sc.loadAlignments(path, stringency = ValidationStringency.LENIENT)
val sequenceDictionary = alignmentRDD.sequences
(alignmentRDD.rdd.map(Read(_, sampleId)), sequenceDictionary)
}
/** Extract the length of each contig from a sequence dictionary */
private def getContigLengths(sequenceDictionary: SequenceDictionary): ContigLengths = {
val builder = Map.newBuilder[ContigName, Locus]
sequenceDictionary.records.foreach(record => builder += ((record.name.toString, record.length)))
builder.result
}
/**
* SequenceDictionaries store information about the contigs that will be found in a given set of reads: names,
* lengths, etc.
*
* When loading/manipulating multiple sets of reads, we generally want to understand the set of all contigs that
* are referenced by the reads, perform some consistency-checking (e.g. verifying that each contig is listed as having
* the same length in each set of reads in which it appears), and finally pass the downstream user a
* SequenceDictionary that encapsulates all of this.
*
* This function performs all of the above.
*
* @param inputs Input files, each containing a set of reads.
* @param dicts SequenceDictionaries that have been parsed from @filenames.
* @return a SequenceDictionary that has been merged and validated from the inputs.
*/
private[readsets] def mergeSequenceDictionaries(inputs: Inputs,
dicts: Seq[SequenceDictionary]): SequenceDictionary = {
val records =
(for {
(input, dict) ← inputs.zip(dicts)
record ← dict.records
} yield
input → record
)
.groupBy(_._2.name)
.values
.map { values ⇒
val (input, record) = values.head
// Verify that all records for a given contig are equal.
values.tail.toList.filter(_._2 != record) match {
case Nil ⇒
case mismatched ⇒
throw new IllegalArgumentException(
(
s"Conflicting sequence records for ${record.name}:" ::
s"$input: $record" ::
mismatched.map { case (otherFile, otherRecord) => s"$otherFile: $otherRecord" }
)
.mkString("\n\t")
)
}
record
}
new SequenceDictionary(records.toVector).sorted
}
/**
* Apply filters to an RDD of reads.
*/
private def filterRDD(reads: RDD[Read], config: Config, sequenceDictionary: SequenceDictionary): RDD[Read] = {
/* Note that the InputFilter properties are public, and some loaders directly apply
* the filters as the reads are loaded, instead of filtering an existing RDD as we do here. If more filters
* are added, be sure to update those implementations.
*
* This is implemented as a static function instead of a method in InputConfig because the overlapsLoci
* attribute cannot be serialized.
*/
var result = reads
config
.overlapsLoci
.foreach { overlapsLoci ⇒
val contigLengths = getContigLengths(sequenceDictionary)
val loci = LociSet(overlapsLoci, contigLengths)
val broadcastLoci = reads.sparkContext.broadcast(loci)
result = result.filter(_.asMappedRead.exists(broadcastLoci.value.intersects))
}
if (config.nonDuplicate) result = result.filter(!_.isDuplicate)
if (config.passedVendorQualityChecks) result = result.filter(!_.failedVendorQualityChecks)
if (config.isPaired) result = result.filter(_.isPaired)
config.minAlignmentQuality.foreach(
minAlignmentQuality ⇒
result =
result.filter(
_.asMappedRead
.forall(_.alignmentQuality >= minAlignmentQuality)
)
)
result
}
/**
* Construct a map from contig name -> length of contig, using a SequenceDictionary.
*/
private def getContigLengthsFromSequenceDictionary(sequenceDictionary: SequenceDictionary): ContigLengths = {
val builder = Map.newBuilder[ContigName, Locus]
for {
record <- sequenceDictionary.records
} {
builder += ((record.name, record.length))
}
builder.result
}
}