-
Notifications
You must be signed in to change notification settings - Fork 577
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial Spark dataflow commit, basic skeleton with some tests.
- Loading branch information
1 parent
921e730
commit 2310062
Showing
26 changed files
with
1,295 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
src/main/java/org/broadinstitute/hellbender/engine/spark/AddContextDataToReadSpark.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package org.broadinstitute.hellbender.engine.spark; | ||
|
||
import com.google.common.collect.Iterables; | ||
import com.google.common.collect.Lists; | ||
import org.apache.spark.api.java.JavaPairRDD; | ||
import org.apache.spark.api.java.JavaRDD; | ||
import org.broadinstitute.hellbender.engine.dataflow.datasources.ReadContextData; | ||
import org.broadinstitute.hellbender.engine.dataflow.datasources.ReferenceDataflowSource; | ||
import org.broadinstitute.hellbender.utils.read.GATKRead; | ||
import org.broadinstitute.hellbender.utils.reference.ReferenceBases; | ||
import org.broadinstitute.hellbender.utils.variant.Variant; | ||
import scala.Tuple2; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* AddContextDataToRead pairs reference bases and overlapping variants with each GATKRead in the PCollection input. | ||
* The variants are obtained from a local file (later a GCS Bucket). The reference bases come from the Google Genomics API. | ||
* | ||
* This transform is intended for direct use in pipelines. | ||
* | ||
* The reference bases paired with each read can be customized by passing in a reference window function | ||
* inside the {@link ReferenceDataflowSource} argument to {@link #add}. See | ||
* {@link org.broadinstitute.hellbender.engine.dataflow.datasources.RefWindowFunctions} for examples. | ||
*/ | ||
public class AddContextDataToReadSpark { | ||
public static JavaPairRDD<GATKRead, ReadContextData> add( | ||
final JavaRDD<GATKRead> reads, final ReferenceDataflowSource referenceDataflowSource, | ||
final JavaRDD<Variant> variants) { | ||
// Join Reads and Variants, Reads and ReferenceBases | ||
JavaPairRDD<GATKRead, Iterable<Variant>> readiVariants = JoinReadsWithVariants.join(reads, variants); | ||
JavaPairRDD<GATKRead, ReferenceBases> readRefBases = JoinReadsWithRefBases.addBases(referenceDataflowSource, reads); | ||
|
||
// For testing we want to know that the reads from the KVs coming back from JoinReadsWithVariants.Join | ||
// and JoinReadsWithRefBases.Pair are the same reads from "reads". | ||
boolean assertsEnabled = false; | ||
assert assertsEnabled = true; // Intentional side-effect!!! | ||
// Now assertsEnabled is set to the correct value | ||
if (assertsEnabled) { | ||
assertSameReads(reads, readRefBases, readiVariants); | ||
} | ||
|
||
JavaPairRDD<GATKRead, Tuple2<Iterable<Iterable<Variant>>, Iterable<ReferenceBases>>> cogroup = readiVariants.cogroup(readRefBases); | ||
return cogroup.mapToPair(in -> { | ||
List<Iterable<Variant>> liVariants = Lists.newArrayList(in._2()._1()); | ||
List<Variant> lVariants = Lists.newArrayList(); | ||
if (!liVariants.isEmpty()) { | ||
final Iterable<Variant> iVariant = Iterables.getOnlyElement(in._2()._1()); | ||
// It's possible for the iVariant to contain only a null variant, we don't | ||
// want to add that to the ReadContextData. | ||
final Variant next = iVariant.iterator().next(); | ||
if (next != null) { | ||
lVariants = Lists.newArrayList(iVariant); | ||
} | ||
} | ||
|
||
ReferenceBases refBases = Iterables.getOnlyElement(in._2()._2()); | ||
ReadContextData readContextData = new ReadContextData(refBases, lVariants); | ||
return new Tuple2<>(in._1(), readContextData); | ||
}); | ||
} | ||
|
||
private static void assertSameReads(final JavaRDD<GATKRead> reads, | ||
final JavaPairRDD<GATKRead, ReferenceBases> readRefBases, | ||
final JavaPairRDD<GATKRead, Iterable<Variant>> readiVariants) { | ||
List<GATKRead> vReads = reads.collect(); | ||
Map<GATKRead, ReferenceBases> vReadRef = readRefBases.collectAsMap(); | ||
Map<GATKRead, Iterable<Variant>> vReadVariant = readiVariants.collectAsMap(); | ||
|
||
// This assumes all rdds doesn't have any duplicates. | ||
JavaRDD<GATKRead> refBasesReads = readRefBases.keys(); | ||
JavaRDD<GATKRead> variantsReads = readiVariants.keys(); | ||
JavaRDD<GATKRead> distinctReads = reads.intersection(refBasesReads).intersection(variantsReads); | ||
|
||
long counts = reads.count(); | ||
assert counts == distinctReads.count(); | ||
assert counts == refBasesReads.count(); | ||
assert counts == variantsReads.count(); | ||
|
||
assert vReadRef.size() == vReads.size(); | ||
assert vReadVariant.size() == vReads.size(); | ||
} | ||
|
||
} | ||
|
27 changes: 27 additions & 0 deletions
27
src/main/java/org/broadinstitute/hellbender/engine/spark/GATKRegistrator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package org.broadinstitute.hellbender.engine.spark; | ||
|
||
import com.esotericsoftware.kryo.Kryo; | ||
import com.google.api.services.genomics.model.Read; | ||
import de.javakaffee.kryoserializers.UnmodifiableCollectionsSerializer; | ||
import org.apache.spark.serializer.KryoRegistrator; | ||
|
||
import java.util.Collections; | ||
|
||
/** | ||
* GATKRegistrator registers Serializers for our project. We need a JsonSerializer for the Google Genomics classes | ||
* and UnmodifiableCollectionsSerializer from a bug in the version of Kryo we're on. | ||
*/ | ||
public class GATKRegistrator implements KryoRegistrator { | ||
@SuppressWarnings("unchecked") | ||
@Override | ||
public void registerClasses(Kryo kryo) { | ||
|
||
// JsonSerializer is needed for the Google Genomics classes like Read and Reference. | ||
kryo.register(Read.class, new JsonSerializer<Read>()); | ||
// htsjdk.variant.variantcontext.CommonInfo has a Map<String, Object> that defaults to | ||
// a Collections.unmodifiableMap. This can't be handled by the version of kryo used in Spark, it's fixed | ||
// in newer versions, but we can't use those because of incompatibility with Spark. We just include the fix | ||
// here. | ||
kryo.register(Collections.unmodifiableMap(Collections.EMPTY_MAP).getClass(), new UnmodifiableCollectionsSerializer()); | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
src/main/java/org/broadinstitute/hellbender/engine/spark/JoinReadsWithRefBases.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package org.broadinstitute.hellbender.engine.spark; | ||
|
||
import com.google.common.collect.Lists; | ||
import org.apache.spark.api.java.JavaPairRDD; | ||
import org.apache.spark.api.java.JavaRDD; | ||
import org.broadinstitute.hellbender.engine.dataflow.datasources.ReferenceDataflowSource; | ||
import org.broadinstitute.hellbender.engine.dataflow.datasources.ReferenceShard; | ||
import org.broadinstitute.hellbender.utils.SimpleInterval; | ||
import org.broadinstitute.hellbender.utils.read.GATKRead; | ||
import org.broadinstitute.hellbender.utils.reference.ReferenceBases; | ||
import scala.Tuple2; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* RefBasesForReads queries the Google Genomics API for reference bases overlapping all of the reads. | ||
* | ||
* step 1: key reads by reference shards | ||
* | ||
* |--------- shard 0 ----------|---------- shard 1 ----------|--------- shard 2 ----------|--------- shard 3 ----------| | ||
* |------ read a -----| |-- read b --| |---- read c ----| | ||
* | ||
* step 2: group reads by the shard they start in | ||
* | ||
* |--------- shard 0 ----------| | ||
* |------ read a -----| | ||
* | ||
* | ||
* |--------- shard 2 ----------| | ||
* |-- read b --| |---- read c ----| | ||
* | ||
* step 3: query the Google Genomics API for all bases needed for each shard | ||
* | ||
* |--- ref bases 1 ---| |--------- ref bases 2 -----------| | ||
* |------ read a -----| |-- read b --| |---- read c ----| | ||
* | ||
* step 4: pair the ref bases needed for each read with the read | ||
* | ||
* |------ read a -----| |-- read b --| |---- read c ----| | ||
* |-- ref bases 1a ---| |ref bases 2b| |- ref bases 2c -| | ||
* | ||
* or in code, | ||
* KV<read a, ref bases 1a> | ||
* KV<read b, ref bases 2b> | ||
* KV<read c, ref bases 2c> | ||
* | ||
* The reference bases paired with each read can be customized by passing in a reference window function | ||
* inside the {@link ReferenceDataflowSource} argument to {@link #addBases}. See {@link org.broadinstitute.hellbender.engine.dataflow.datasources.RefWindowFunctions} for examples. | ||
*/ | ||
public class JoinReadsWithRefBases { | ||
public static JavaPairRDD<GATKRead, ReferenceBases> addBases(final ReferenceDataflowSource referenceDataflowSource, | ||
final JavaRDD<GATKRead> reads) { | ||
|
||
JavaPairRDD<ReferenceShard, GATKRead> shardRead = reads.mapToPair(gatkRead -> { | ||
ReferenceShard shard = ReferenceShard.getShardNumberFromInterval(gatkRead); | ||
return new Tuple2<>(shard, gatkRead); | ||
}); | ||
|
||
JavaPairRDD<ReferenceShard, Iterable<GATKRead>> shardiRead = shardRead.groupByKey(); | ||
|
||
return shardiRead.flatMapToPair(in -> { | ||
List<Tuple2<GATKRead, ReferenceBases>> out = Lists.newArrayList(); | ||
Iterable<GATKRead> iReads = in._2(); | ||
SimpleInterval interval = SimpleInterval.getSpanningInterval(iReads); | ||
ReferenceBases bases = referenceDataflowSource.getReferenceBases(null, interval); | ||
for (GATKRead r : iReads) { | ||
final ReferenceBases subset = bases.getSubset(new SimpleInterval(r)); | ||
out.add(new Tuple2<>(r, subset)); | ||
} | ||
return out; | ||
}); | ||
} | ||
} |
Oops, something went wrong.