Skip to content

Commit

Permalink
[ADAM-582] Eliminate .get on option in FragmentCoverter.
Browse files Browse the repository at this point in the history
Resolves #582. In the FragmentConverter, we had code that called .get on an
optional ReferenceRegion. If a NucleotideContigFragment didn't have a contig
associated with it, then we would call .get on the empty option, which would
throw an error. This commit changes that code to call map on the
Option[ReferenceRegion], and then changes the usage of that method so that it
is called in a flatMap. Additionally, I've added a test to cover this case.
  • Loading branch information
fnothaft authored and heuermh committed Jul 20, 2016
1 parent 9c72620 commit 7c2df60
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ private object FragmentCollector extends Serializable {
* @return Returns key value pair where the key is the contig metadata and the
* value is a Fragment Collector object.
*/
def apply(fragment: NucleotideContigFragment): (Contig, FragmentCollector) = {
(
fragment.getContig,
FragmentCollector(Seq((ReferenceRegion(fragment).get, fragment.getFragmentSequence)))
)
def apply(fragment: NucleotideContigFragment): Option[(Contig, FragmentCollector)] = {
ReferenceRegion(fragment).map(rr => {
(fragment.getContig,
FragmentCollector(Seq((rr, fragment.getFragmentSequence))))
})
}
}

Expand Down Expand Up @@ -153,7 +153,7 @@ private[adam] object FragmentConverter extends Serializable {
* @return Returns an RDD of reads that represent aligned contigs.
*/
def convertRdd(rdd: RDD[NucleotideContigFragment]): RDD[AlignmentRecord] = {
rdd.map(FragmentCollector(_))
rdd.flatMap(FragmentCollector(_))
.reduceByKey(mergeFragments)
.flatMap(convertFragment)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ import org.bdgenomics.formats.avro._
class FragmentConverterSuite extends ADAMFunSuite {

test("build a fragment collector and convert to a read") {
val (builtContig, builtFragment) = FragmentCollector(NucleotideContigFragment.newBuilder()
val fcOpt = FragmentCollector(NucleotideContigFragment.newBuilder()
.setContig(Contig.newBuilder().setContigName("ctg").build())
.setFragmentSequence("ACACACAC")
.setFragmentStartPosition(0L)
.build())
assert(fcOpt.isDefined)
val (builtContig, builtFragment) = fcOpt.get

assert(builtContig.getContigName === "ctg")
assert(builtFragment.fragments.length === 1)
Expand All @@ -46,6 +48,11 @@ class FragmentConverterSuite extends ADAMFunSuite {
assert(convertedRead.getEnd === 8L)
}

test("if a fragment isn't associated with a contig, don't get a fragment collector") {
val fcOpt = FragmentCollector(NucleotideContigFragment.newBuilder().build())
assert(fcOpt.isEmpty)
}

sparkTest("convert an rdd of discontinuous fragments, all from the same contig") {
val rdd = sc.parallelize(Seq(NucleotideContigFragment.newBuilder()
.setContig(Contig.newBuilder().setContigName("ctg").build())
Expand Down

1 comment on commit 7c2df60

@InvisibleTech
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay. This is pretty cool. I was thinking about exception handling, instead I needed to think FP style and go back to Option and sort it out without a failure. Sorry I didn't think that way sooner. I tend to use Optional in Java 8 freely but didn't see it here.

Please sign in to comment.