All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.citec.tcs.alignment.SoftAffineAlignmentFullAlgorithm Maven / Gradle / Ivy

/* 
 * TCS Alignment Toolbox
 * 
 * Copyright (C) 2013-2015
 * Benjamin Paaßen, Georg Zentgraf
 * AG Theoretical Computer Science
 * Centre of Excellence Cognitive Interaction Technology (CITEC)
 * University of Bielefeld
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see .
 */
package de.citec.tcs.alignment;

import de.citec.tcs.alignment.sequence.Sequence;
import java.util.EnumMap;

/**
 * This implements a local affine alignment similar to Smith-Waterman
 * and Gotoh. More information about this alignment can be found in the
 * AbstractAffineAlignmentAlgorithm.
 *
 * Instead of the strict minimum it uses a differentiable function (softmin)
 * that takes into account all possible alignment choices at each point. The
 * degree by which other choices are considered can be set with the beta
 * ("strictness") parameter. More information about softmin can be found in the
 * Softmin class.
 *
 * Note that the normalization of the end result is considerably more difficult
 * here. In principle the end result is normalized by the worst possible
 * alignment, meaning by the costs to skip both input sequences entirely. But
 * the soft alignment allows for even worse alignments. So it is possible
 * to get a distance bigger than 1, which we prevent by cutting off such
 * distances and setting them to 1, such that the output of this algorithm
 * remains to be reliable.
 *
 * This Algorithm returns a SoftAffinePathModel that is able to calculate the
 * soft derivative of this alignment. The derivative methodology is shown there.
 *
 * @author Benjamin Paassen - [email protected]
 */
public class SoftAffineAlignmentFullAlgorithm
		extends AbstractAffineAlignmentAlgorithm {

	public static final double DEFAULTBETA = 1;
	private double beta = DEFAULTBETA;
	private double approxThreshold = Softmin.DEFAULTAPPROXTHRESHOLD;

	public SoftAffineAlignmentFullAlgorithm(
			AlignmentSpecification alignmentSpecification) {
		super(SoftAffinePathModel.class, alignmentSpecification);
	}

	/**
	 * Sets the parameter defining the "softness" of the alignment.
	 *
	 * @param beta The parameter defining the "softness" of the alignment. For
	 * beta towards infinity this alignment becomes closer to the strict
	 * alignment. For beta = 0 all possible alignments are equally considered
	 * and softmin returns the average. Please note that a low beta value might
	 * lead to a very rough approximation and that for higher sequence lengths
	 * beta has to be higher, too.
	 */
	public void setBeta(double beta) {
		this.beta = beta;
	}

	/**
	 * Returns the parameter defining the "softness" of the alignment.
	 *
	 * @return The parameter defining the "softness" of the alignment. For beta
	 * towards infinity this alignment becomes closer to the strict alignment.
	 * For beta = 0 all possible alignments are equally considered and softmin
	 * returns the average. Please note that a low beta value might lead to a
	 * very rough approximation and that for higher sequence lengths beta has to
	 * be higher, too.
	 */
	public double getBeta() {
		return beta;
	}

	/**
	 * For more information on the ApproxThreshold have a look at the
	 * Softmin class.
	 *
	 * @return the approximation threshold for the Softmin function.
	 */
	public double getApproxThreshold() {
		return approxThreshold;
	}

	/**
	 * For more information on the ApproxThreshold have a look at the
	 * Softmin class.
	 *
	 * @param approxThreshold the approximation threshold for the Softmin
	 * function.
	 */
	public void setApproxThreshold(double approxThreshold) {
		this.approxThreshold = approxThreshold;
	}

	/**
	 * This implements the strict maximum of the given input doubles.
	 *
	 * @return the strict maximum of the given input doubles.
	 */
	@Override
	public double choice(double... choices) {
		if (choices == null || choices.length == 0) {
			throw new IllegalArgumentException("Choice input was invalid!");
		}
		return Softmin.softmin(beta, approxThreshold, choices);
	}

	/**
	 * {@inheritDoc }
	 */
	@Override
	public SoftAffinePathModel transformToResult(EnumMap dp_tables,
			double[][] compareMatrix,
			double[] deletionMatrix, double[] insertionMatrix,
			double[] skipDeletionMatrix, double[] skipInsertionMatrix,
			Sequence a, Sequence b) {
		final double accum_score = dp_tables.get(Recurrence.SKIPDEL_START)[0][0];
		final double score;
		if (accum_score > 0) {
			//normalize it by the cost for skipping both sequences entirely.
			double worstScore = 0;
			for (int i = 0; i < skipDeletionMatrix.length; i++) {
				worstScore += skipDeletionMatrix[i];
			}
			for (int j = 0; j < skipInsertionMatrix.length; j++) {
				worstScore += skipInsertionMatrix[j];
			}
			score = Math.min(1, accum_score / worstScore);
		} else {
			score = 0;
		}
		return new SoftAffinePathModel(beta, getSpecification(),
				getMinMiddleSkips(), score,
				dp_tables,
				compareMatrix, deletionMatrix, insertionMatrix,
				skipDeletionMatrix, skipInsertionMatrix,
				a, b);
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy