1 package edu.uci.iotproject.comparison.seqalignment;
3 import java.util.function.ToIntBiFunction;
4 import java.util.function.ToIntFunction;
7 * Provides a generic implementation for the calculation of the cost of aligning two elements of a sequence as part of
8 * the sequence alignment algorithm (the algorithm is implemented in {@link SequenceAlignment}).
10 * @param <T> The type of the elements that are being aligned.
12 * @author Janus Varmarken {@literal <jvarmark@uci.edu>}
13 * @author Rahmadi Trimananda {@literal <rtrimana@uci.edu>}
15 public class AlignmentPricer<T> {
18 * A function that provides the cost of aligning a {@link T} with a gap.
20 private final ToIntFunction<T> mGapCostFunction;
23 * A function that provides the cost of aligning a {@link T} with some other {@link T}.
25 private final ToIntBiFunction<T,T> mAlignmentCostFunction;
28 * Constructs a new {@link AlignmentPricer}.
30 * @param alignmentCostFunction A function that specifies the cost of aligning a {@link T} with some other {@link T}
31 * (e.g., based on the values of the properties of the two instances).
32 * @param gapCostFunction A function that specifies the cost of aligning a {@link T} with a gap. Note that the
33 * function is free to specify <em>different</em> gap costs for different {@link T}s.
35 public AlignmentPricer(ToIntBiFunction<T,T> alignmentCostFunction, ToIntFunction<T> gapCostFunction) {
36 mAlignmentCostFunction = alignmentCostFunction;
37 mGapCostFunction = gapCostFunction;
41 * Calculate the cost of aligning {@code item1} with {@code item2}. If either of the two arguments is set to
42 * {@code null}, the cost of aligning the other argument with a gap will be returned. Note that both arguments
43 * cannot be {@code null} at the same time as that translates to aligning a gap with a gap, which is pointless.
45 * @param item1 The first of the two aligned objects. Set to {@code null} to calculate the cost of aligning
46 * {@code item2} with a gap.
47 * @param item2 The second of the two aligned objects. Set to {@code null} to calculate the cost of aligning
48 * {@code item2} with a gap.
49 * @return The cost of aligning {@code item1} with {@code item2}.
51 public int alignmentCost(T item1, T item2) {
52 // If both arguments are null, the caller is aligning a gap with a gap which is pointless might as well remove
53 // both gaps in that case!)
54 if (item1 == null && item2 == null) {
55 throw new IllegalArgumentException("Both arguments cannot be null: you are aligning a gap with a gap!");
57 // If one item is null, it means we're aligning an int with a gap.
58 // Invoke the provided gap cost function to get the gap cost.
60 return mGapCostFunction.applyAsInt(item2);
63 return mGapCostFunction.applyAsInt(item1);
65 // If both arguments are present, we simply delegate the task of calculating the cost of aligning the two items
66 // to the provided alignment cost function.
67 return mAlignmentCostFunction.applyAsInt(item1, item2);