[Biojava-l] NullPointerException when using Viterbi algorithm in HMM.

Thomas Covello thomas.covello at gmail.com
Mon May 11 20:33:43 UTC 2009


Hello,
I am trying to use a Hidden Markov Model to classify a sequence, and I
am encountering a NullPointerException when I try to use the Viterbi
algorithm. My test case follows - if you want the code I am actually
using I can post it. The stack trace:

java.lang.NullPointerException
        at org.biojava.bio.dp.onehead.SingleDP.viterbi(SingleDP.java:648)
        at org.biojava.bio.dp.onehead.SingleDP.viterbi(SingleDP.java:512)
        at oxytricha_crap.Main.main(Main.java:69)

File: Main.java

package oxytricha_crap;

import org.biojava.bio.seq.*;
import org.biojava.bio.*;
import org.biojava.bio.symbol.*;
import org.biojava.bio.dp.*;
import org.biojava.bio.dist.*;
import java.util.*;
import org.biojava.bio.symbol.SymbolList;

public class Main {
    private static Alphabet DNA = DNATools.getDNA();
    private static State IES;
    private static State MDS;
    private static MarkovModel hmm = new SimpleMarkovModel(1, DNA, "MyHMM");

    static {
        try {
            Distribution IES_dist =
                DistributionFactory.DEFAULT.createDistribution(DNA);
            Distribution MDS_dist =
                DistributionFactory.DEFAULT.createDistribution(DNA);
            IES = new SimpleEmissionState("IES", Annotation.EMPTY_ANNOTATION,
                new int [] {1}, IES_dist);
            MDS = new SimpleEmissionState("MDS", Annotation.EMPTY_ANNOTATION,
                new int [] {1}, MDS_dist);
            hmm.addState(IES);
            hmm.addState(MDS);
            hmm.createTransition(MDS, hmm.magicalState());
            hmm.createTransition(hmm.magicalState(), MDS);
            hmm.createTransition(MDS, IES);
            hmm.createTransition(IES, MDS);
            hmm.createTransition(MDS, MDS);
            hmm.createTransition(IES, IES);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        try {
            State currState;
            State lastState = hmm.magicalState();
            Iterator<State> states = Arrays.asList(new State [] {
                MDS, MDS, MDS, IES, IES, IES, MDS, MDS, IES, IES, MDS, MDS,
                MDS, MDS
            }).iterator();
            SymbolList sl = DNATools.createDNA("acgtgtctgagaga");

            HMMTrainer trainer = new SimpleHMMTrainer(hmm);
            trainer.startCycle();
            for (Symbol sym : (List<Symbol>)sl.toList()) {
                currState = states.next();
                if (currState != lastState && lastState != null)
                    trainer.recordTransition(lastState, currState, 1.0);
                trainer.recordEmittedSymbol(currState, sym, 1.0);
                lastState = currState;
            }
            trainer.recordTransition(MDS, hmm.magicalState(), 1.0);

            trainer.completeCycle();

            DP dp = DPFactory.DEFAULT.createDP(hmm);
            SymbolList unknown = DNATools.createDNA("acgtcgtacgtacgtacacaacga");
            StatePath spath = dp.viterbi(new SymbolList [] { unknown },
                ScoreType.PROBABILITY);
        } catch (Exception e) {
            e.printStackTrace();
        }

        System.exit(0);
    }
}



More information about the Biojava-l mailing list