001/*
002 * To change this template, choose Tools | Templates
003 * and open the template in the editor.
004 */
005
006package org.biojava.bio.molbio;
007
008import java.util.Iterator;
009import org.biojava.bio.dist.Distribution;
010import org.biojava.bio.dist.DistributionTrainerContext;
011import org.biojava.bio.symbol.IllegalSymbolException;
012import org.biojava.bio.symbol.Symbol;
013import org.biojava.bio.symbol.SymbolList;
014
015/**
016 * Computes composition statistics about a <code>SymbolList</code>.
017 * Essentially a conveniece wrapper around a Distribution.
018 * @author Mark Schreiber
019 * @since 1.6
020 */
021public class Composition {
022    private SymbolList symbolList;
023    private Distribution distribution;
024    private DistributionTrainerContext dtc;
025
026    
027    /**
028     * Set the <code>SymbolList</code> to calculation the composition of.
029     * @param symbolList a <code>SymbolList</code> from the DNA Alphabet.
030     * @throws org.biojava.bio.symbol.IllegalSymbolException if <code>symbolList</code>
031     * is not DNA.
032     */
033    public synchronized void setSymbolList(final SymbolList symbolList) throws IllegalSymbolException {
034        this.symbolList = symbolList;
035        train(symbolList);
036    }
037
038    private synchronized void train(final SymbolList symbolList) throws IllegalSymbolException {
039        dtc.registerDistribution(getDistribution());
040        for(Iterator i = symbolList.iterator(); i.hasNext();){
041            dtc.addCount(getDistribution(), (Symbol)i.next(), 1.0);
042        }
043        dtc.train();
044        dtc.clearCounts();
045    }
046
047    /**
048     * Returns the distribution backing this class.
049     * @return a <code>Distribution</code>
050     */
051    public Distribution getDistribution() {
052        return distribution;
053    }
054}