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
022package org.biojava.bio.program.hmmer;
023
024import org.biojava.bio.Annotation;
025import org.biojava.bio.dist.Distribution;
026import org.biojava.bio.dist.DistributionFactory;
027import org.biojava.bio.dp.DotState;
028import org.biojava.bio.dp.EmissionState;
029import org.biojava.bio.dp.IllegalTransitionException;
030import org.biojava.bio.dp.ProfileHMM;
031import org.biojava.bio.dp.State;
032import org.biojava.bio.symbol.Alphabet;
033import org.biojava.bio.symbol.IllegalAlphabetException;
034import org.biojava.bio.symbol.IllegalSymbolException;
035import org.biojava.bio.symbol.Symbol;
036import org.biojava.utils.ChangeVetoException;
037 
038
039
040
041
042/** This is a class for representing HMMER generated Profile HMM.
043 *  It differs from the normal ProfileHMM only in the states which are connected:
044 *   - there are no insert <-> delete transitions allowed
045 *   - there is no iO initial insert state (between begin and initial match states)
046 *   - there is not iN final insert state (between final match state and end state)
047 *
048 *  @author Lachlan Coin
049 */
050public class HmmerProfileHMM extends ProfileHMM {
051  protected HmmerProfileHMM(
052    Alphabet alpha, int columns,
053    DistributionFactory matchFactory, DistributionFactory insertFactory, String name
054  ) throws IllegalSymbolException, IllegalTransitionException,
055  IllegalAlphabetException {
056      super(alpha,columns, matchFactory,insertFactory,name);
057  }
058
059 
060
061    /** This is called by constructor in setting up the allowed transitions in the model */
062  protected void connectModel() throws 
063        ChangeVetoException, IllegalSymbolException, IllegalTransitionException,IllegalAlphabetException{
064        EmissionState mO = getMatch(0);
065        removeState(getInsert(0));
066        removeState(getInsert(columns()));
067        DotState dO = null;
068        EmissionState iO =null;
069        for(int i = 1; i <= columns(); i++){
070            EmissionState mN = getMatch(i);
071            EmissionState iN = getInsert(i);
072            DotState dN = getDelete(i);
073
074            // from a model state
075            createTransition(mO, mN);
076            if(i < columns())
077                createTransition(mN, iN);
078            createTransition(mO, dN);
079      
080            // from an insert state
081            if(i<columns())
082                createTransition(iN, iN);
083           
084            
085            // from a delete state
086            if(i > 1) {
087                createTransition(dO, dN);
088                createTransition(dO, mN);
089                createTransition(iO, mN);
090            }        
091            mO = mN;
092            iO = iN;
093            dO = dN;
094            // transitions to and from magical states and all match states
095            if(i>1)
096                createTransition(magicalState(),mN);
097            createTransition(mN, magicalState());
098        }
099        // for the transitions to end
100        createTransition(dO, magicalState());
101    }
102
103    public double transScore(State from, State to, Symbol symFrom, Symbol symTo) throws IllegalSymbolException{
104            return log2(getWeights(from).getWeight(to));
105    }
106    
107    protected static double log2(double x){
108        return Math.log(x)/Math.log(2);
109    }
110    
111    protected EmissionState makeNewInsertState(String str, Annotation ann, int[] adv, Distribution dis){
112          return new ProfileEmissionState(str, ann,adv, dis);
113  }
114  
115   protected EmissionState makeNewMatchState(String str, Annotation ann, int[] adv, Distribution dis){
116          return new ProfileEmissionState(str, ann,adv, dis);
117  }
118  
119}