diff --git a/src/main/java/org/broadinstitute/gatk/nativebindings/smithwaterman/SWAlignerArguments.java b/src/main/java/org/broadinstitute/gatk/nativebindings/smithwaterman/SWAlignerArguments.java deleted file mode 100644 index 8565bdb..0000000 --- a/src/main/java/org/broadinstitute/gatk/nativebindings/smithwaterman/SWAlignerArguments.java +++ /dev/null @@ -1,72 +0,0 @@ -package org.broadinstitute.gatk.nativebindings.smithwaterman; - -/** - * Struct used to pass arguments to the Smith-Waterman aligner - */ -public final class SWAlignerArguments { - /** how to treat overhangs **/ - public final OverhangStrategy strategy; - - /** match value **/ - public final int w_match; - - /** mismatch penalty **/ - public final int w_mismatch; - - /** gap open penalty **/ - public final int w_open; - - /** gap extension penalty **/ - public final int w_extend; - - public SWAlignerArguments(final OverhangStrategy strategy, final int w_extend, final int w_match, final int w_mismatch, final int w_open) - { - if ( strategy == null ) { - throw new IllegalArgumentException("strategy must not be null"); - } - if( w_extend > 0 ) { - throw new IllegalArgumentException("w_extend must be <= 0 but was passed as " + w_extend); - } - if( w_mismatch > 0 ) { - throw new IllegalArgumentException("w_mismatch must be <= 0 but was passed as " + w_mismatch); - } - if( w_open > 0 ) { - throw new IllegalArgumentException("w_open must be <= 0 but was passed as " + w_open); - } - - this.strategy = strategy; - this.w_extend = w_extend; - this.w_match = w_match; - this.w_mismatch = w_mismatch; - this.w_open = w_open; - } - - /** - * How overhangs should be treated during Smith-Waterman alignment - */ - public enum OverhangStrategy { - /* - * Add softclips for the overhangs - */ - SOFTCLIP, - - /* - * Treat the overhangs as proper insertions/deletions - */ - INDEL, - - /* - * Treat the overhangs as proper insertions/deletions for leading (but not trailing) overhangs. - * This is useful e.g. when we want to merge dangling tails in an assembly graph: because we don't - * expect the dangling tail to reach the end of the reference path we are okay ignoring trailing - * deletions - but leading indels are still very much relevant. - */ - LEADING_INDEL, - - /* - * Just ignore the overhangs - */ - IGNORE - } - -} diff --git a/src/main/java/org/broadinstitute/gatk/nativebindings/smithwaterman/SWAlignerNativeBinding.java b/src/main/java/org/broadinstitute/gatk/nativebindings/smithwaterman/SWAlignerNativeBinding.java index e520ba8..6336d17 100644 --- a/src/main/java/org/broadinstitute/gatk/nativebindings/smithwaterman/SWAlignerNativeBinding.java +++ b/src/main/java/org/broadinstitute/gatk/nativebindings/smithwaterman/SWAlignerNativeBinding.java @@ -3,22 +3,18 @@ import org.broadinstitute.gatk.nativebindings.NativeLibrary; +import java.io.Closeable; + /** * Provides access to the native kernel of the Smith-Waterman computation. - * GATK will call {@link #initialize(SWAlignerArguments)} to set the - * parameter values and will call {@link #align(byte[], byte[])} to perform + * GATK will call {@link #align(byte[], byte[], SWParameters, SWOverhangStrategy)} to perform * an alignment. * + * Subclasses may override {@link #close()} if they need to release resources. + * * Only one thread will call methods on any given object of classes that implement this interface. */ -public interface SWAlignerNativeBinding extends NativeLibrary { - - /** - * Initialize the native Smith-Waterman - * - * @param args arguments used for Smith-Waterman - */ - void initialize(SWAlignerArguments args); +public interface SWAlignerNativeBinding extends NativeLibrary, Closeable { /** * Perform a Smith-Waterman alignment and return the result @@ -27,6 +23,17 @@ public interface SWAlignerNativeBinding extends NativeLibrary { * @param alt alternate sequence * @return alignment result */ - SWAlignmentResult align(byte[] ref, byte[] alt); + SWNativeAlignerResult align(byte[] ref, byte[] alt, SWParameters parameters, SWOverhangStrategy overhangStrategy); + + + /** + * Subclasses may optionally implement close in order to release any native resources that they are holding. + * Subclasses that rely on close to recover resources should fail with {@link IllegalStateException} if + * {@link #align(byte[], byte[], SWParameters, SWOverhangStrategy)} is called after close. + */ + @Override + default void close() { + //do nothing by default + } } diff --git a/src/main/java/org/broadinstitute/gatk/nativebindings/smithwaterman/SWAlignmentResult.java b/src/main/java/org/broadinstitute/gatk/nativebindings/smithwaterman/SWNativeAlignerResult.java similarity index 61% rename from src/main/java/org/broadinstitute/gatk/nativebindings/smithwaterman/SWAlignmentResult.java rename to src/main/java/org/broadinstitute/gatk/nativebindings/smithwaterman/SWNativeAlignerResult.java index 047fad8..32f624f 100644 --- a/src/main/java/org/broadinstitute/gatk/nativebindings/smithwaterman/SWAlignmentResult.java +++ b/src/main/java/org/broadinstitute/gatk/nativebindings/smithwaterman/SWNativeAlignerResult.java @@ -1,9 +1,9 @@ package org.broadinstitute.gatk.nativebindings.smithwaterman; /** - * Struct used to hold results of the Smith-Waterman alignment result + * Struct used to hold results of the Smith-Waterman alignment */ -public final class SWAlignmentResult { +public final class SWNativeAlignerResult { // CIGAR string of the alignment public final String cigar; @@ -11,7 +11,7 @@ public final class SWAlignmentResult { public final int alignment_offset; - public SWAlignmentResult(final String cigar, final int alignment_offset) + public SWNativeAlignerResult(final String cigar, final int alignment_offset) { this.cigar = cigar; this.alignment_offset = alignment_offset; diff --git a/src/main/java/org/broadinstitute/gatk/nativebindings/smithwaterman/SWOverhangStrategy.java b/src/main/java/org/broadinstitute/gatk/nativebindings/smithwaterman/SWOverhangStrategy.java new file mode 100644 index 0000000..8ba22a4 --- /dev/null +++ b/src/main/java/org/broadinstitute/gatk/nativebindings/smithwaterman/SWOverhangStrategy.java @@ -0,0 +1,29 @@ +package org.broadinstitute.gatk.nativebindings.smithwaterman; + +/** + * How overhangs should be treated during Smith-Waterman alignment + */ +public enum SWOverhangStrategy { + /* + * Add softclips for the overhangs + */ + SOFTCLIP, + + /* + * Treat the overhangs as proper insertions/deletions + */ + INDEL, + + /* + * Treat the overhangs as proper insertions/deletions for leading (but not trailing) overhangs. + * This is useful e.g. when we want to merge dangling tails in an assembly graph: because we don't + * expect the dangling tail to reach the end of the reference path we are okay ignoring trailing + * deletions - but leading indels are still very much relevant. + */ + LEADING_INDEL, + + /* + * Just ignore the overhangs + */ + IGNORE +} diff --git a/src/main/java/org/broadinstitute/gatk/nativebindings/smithwaterman/SWParameters.java b/src/main/java/org/broadinstitute/gatk/nativebindings/smithwaterman/SWParameters.java new file mode 100644 index 0000000..c364d5a --- /dev/null +++ b/src/main/java/org/broadinstitute/gatk/nativebindings/smithwaterman/SWParameters.java @@ -0,0 +1,57 @@ +package org.broadinstitute.gatk.nativebindings.smithwaterman; + +/** + * a set of parameters to configure Smith-Waterman assembly + */ +public class SWParameters { + private final int matchValue; + private final int mismatchPenalty; + private final int gapOpenPenalty; + private final int gapExtendPenalty; + + /** + * Create a new set of parameters for Smith-Waterman alignment + * @param matchValue how much to reward a match during alignment >= 0 + * @param mismatchPenalty how much to penalize a mismatch during alignment <= 0 + * @param gapOpenPenalty how much penalize the creation of a new gap in the alignment <= 0 + * @param gapExtendPenalty how much to penalize extending an already open gap in the alignment <= 0 + */ + public SWParameters(final int matchValue, final int mismatchPenalty, final int gapOpenPenalty, final int gapExtendPenalty) { + if( matchValue < 0 ) { + throw new IllegalArgumentException("matchValue must be >= 0 but was passed as " + matchValue); + } + if( mismatchPenalty > 0 ) { + throw new IllegalArgumentException("mismatchPenalty must be <= 0 but was passed as " + mismatchPenalty); + } + if( gapOpenPenalty > 0 ) { + throw new IllegalArgumentException("gapOpenPenalty must be <= 0 but was passed as " + gapOpenPenalty); + } + if( gapExtendPenalty > 0 ) { + throw new IllegalArgumentException("gapExtendPenalty must be <= 0 but was passed as " + gapExtendPenalty); + } + this.matchValue = matchValue; + this.mismatchPenalty = mismatchPenalty; + this.gapOpenPenalty = gapOpenPenalty; + this.gapExtendPenalty = gapExtendPenalty; + } + + /** gap extension penalty **/ + public int getGapExtendPenalty() { + return gapExtendPenalty; + } + + /** match value **/ + public int getMatchValue() { + return matchValue; + } + + /** mismatch penalty **/ + public int getMismatchPenalty() { + return mismatchPenalty; + } + + /** gap open penalty **/ + public int getGapOpenPenalty() { + return gapOpenPenalty; + } +}