Skip to content

Commit

Permalink
chore: move dupliated code to a private helper
Browse files Browse the repository at this point in the history
  • Loading branch information
clintval committed Apr 29, 2022
1 parent c08b560 commit eeb9e83
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions src/main/scala/com/fulcrumgenomics/bam/api/SamSource.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,22 @@ object SamSource {
var DefaultUseAsyncIo: Boolean = false
var DefaultValidationStringency: ValidationStringency = ValidationStringency.STRICT

/**
* Constructs a [[SamSource]] to read from the provided path.
/** Configure a [[SAMRecordFactory]] with a variety of parameters. */
private def buildSamRecordFactory(
factory: SAMRecordFactory,
ref: Option[PathToFasta],
async: Boolean,
stringency: ValidationStringency,
): SamReaderFactory = {
val fac = SamReaderFactory.make()
fac.samRecordFactory(factory)
fac.setUseAsyncIo(async)
fac.validationStringency(stringency)
ref.foreach(fac.referenceSequence)
fac
}

/** Constructs a [[SamSource]] to read from the provided path.
*
* @param path the path to read the SAM/BAM/CRAM from
* @param index an optional path to read the index from
Expand All @@ -53,16 +67,9 @@ object SamSource {
async: Boolean = DefaultUseAsyncIo,
stringency: ValidationStringency = DefaultValidationStringency,
factory: SAMRecordFactory = SamRecord.Factory): SamSource = {
// Configure the factory
val fac = SamReaderFactory.make()
fac.samRecordFactory(factory)
fac.setUseAsyncIo(async)
fac.validationStringency(stringency)
ref.foreach(r => fac.referenceSequence(r.toFile))

// Open the input(s)
val fac = buildSamRecordFactory(factory = factory, ref = ref, async = async, stringency = stringency)
val input = SamInputResource.of(path)
index.foreach(i => input.index(i))
index.foreach(input.index)
new SamSource(fac.open(input))
}

Expand All @@ -81,11 +88,7 @@ object SamSource {
stringency: ValidationStringency,
factory: SAMRecordFactory,
): SamSource = {
val fac = SamReaderFactory.make()
fac.samRecordFactory(factory)
fac.setUseAsyncIo(async)
fac.validationStringency(stringency)
ref.foreach(fasta => fac.referenceSequence(fasta.toFile))
val fac = buildSamRecordFactory(factory = factory, ref = ref, async = async, stringency = stringency)
new SamSource(fac.open(SamInputResource.of(stream)), closer = Some(() => stream.close()))
}
}
Expand Down

0 comments on commit eeb9e83

Please sign in to comment.