001/*
002 *                    BioJava development code
003 *
004 * This code may be freely distributed and modified under the
005 * terms of the GNU Lesser General Public Licence.  This should
006 * be distributed with the code.  If you do not have a copy,
007 * see:
008 *
009 *      http://www.gnu.org/copyleft/lesser.html
010 *
011 * Copyright for this code is held jointly by the individual
012 * authors.  These should be listed in @author doc comments.
013 *
014 * For more information on the BioJava project and its aims,
015 * or to join the biojava-l mailing list, visit the home page
016 * at:
017 *
018 *      http://www.biojava.org/
019 *
020 */
021
022
023package org.biojava.bio.dp;
024import java.io.Serializable;
025
026import org.biojava.bio.dist.Distribution;
027import org.biojava.bio.symbol.IllegalSymbolException;
028import org.biojava.bio.symbol.Symbol;
029
030/**
031 * This class computes the score that is used to be used
032 * in a DP optimisation.
033 *
034 * @author Matthew Pocock
035 */
036public interface ScoreType {
037  /**
038   * Calculates the score associated with a distribution and a symbol.
039   */
040  double calculateScore(Distribution dist, Symbol sym)
041  throws IllegalSymbolException;
042  
043  public final static ScoreType PROBABILITY = new Probability();
044  
045  /**
046   * In this class, calculateScore returns the probability
047   * of a Symbol being emitted.
048   *
049   * @author Matthew Pocock
050   */
051  public static class Probability implements ScoreType, Serializable {
052    public double calculateScore(Distribution dist, Symbol sym)
053    throws IllegalSymbolException {
054      return dist.getWeight(sym);
055    }
056  };
057  
058  public final static ScoreType ODDS = new Odds();
059  
060  /**
061   * In this class, calculateScore returns the odds ratio
062   * of a symbol being emitted.  That is, the ratio of the
063   * probability of a Symbol being emitted to it being
064   * emitted by the null model.
065   *
066   * @author Matthew Pocock
067   */
068  public static class Odds implements ScoreType, Serializable {
069    public double calculateScore(Distribution dist, Symbol sym)
070    throws IllegalSymbolException {
071      double d = dist.getWeight(sym);
072      double n = dist.getNullModel().getWeight(sym);
073      //System.out.println("Odds for " + sym.getName() + "\t= " + d + " / " + n);
074      return d / n;
075    }
076  };
077  
078  public final static ScoreType NULL_MODEL = new NullModel();
079  
080  /**
081   * In this class, calculateScore returns the probability of
082   * a Symbol being emitted by the null model.
083   *
084   * @author Matthew Pocock
085   */
086  public static class NullModel implements ScoreType, Serializable {
087    public double calculateScore(Distribution dist, Symbol sym)
088    throws IllegalSymbolException {
089      return dist.getNullModel().getWeight(sym);
090    }
091  };
092}
093