[Biojava-l] NullPointerException in Hidden Markov Model

Hashem Koohy hk3 at sanger.ac.uk
Wed Sep 21 08:36:35 UTC 2011


Hi,
I have set up a HMM model and I am trying to get the viterbi path printed
out, instead I get the following error message. I feel it must be some thing
silly but I cannot  spot it. I really appreciate any clue.


Exception in thread "main" 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 hmmwithdirichletprior.VITERBI2.main(VITERBI2.java:188)




This is how I call my HMM model from main function:


            MarkovModel mm = block.makeMarkovModel(observedSeqAlphabet,
tranProb, strProb, statesAndDirPar, "dirichletMM");
             DP dp    = new SingleDP(mm);
            SymbolList [] symList = {symbolList};
            StatePath viterbiPath = dp.viterbi(symList,
ScoreType.PROBABILITY);

And here is my makeMarkovModel method:


        private static MarkovModel makeMarkovModel(
                SimpleAlphabet alphabet,  double [][] transitionMatrix,
double [] startProbabilities, LinkedHashMap<String ,
                double []> statesAndCorresponingDirichletParameters, String
modelName ) throws Exception{
            
            SimpleMarkovModel mm  = new SimpleMarkovModel(1, alphabet,
modelName );
            int [] advance = { 1 };
            
            int numberOfStates =
statesAndCorresponingDirichletParameters.size();
            ArrayList<String > stateNames = new ArrayList<String>();
            ArrayList<double []> arraysOfDirichletParameters = new
ArrayList<double []>();
            
            for(Map.Entry<String, double[]>
me:statesAndCorresponingDirichletParameters.entrySet() ){
                double oneDirichletPar [] = me.getValue();
                arraysOfDirichletParameters.add(oneDirichletPar);
                String oneState           = me.getKey();
                stateNames.add(oneState);
            }
                   
            //Distribution initiation
            Distribution     [] dists          = new
Distribution[numberOfStates];
            EmissionState   [] emissionStates = new
SimpleEmissionState[numberOfStates];
            
            for(int i = 0; i< numberOfStates;i++){
                dists[i] =
DistributionFactory.DEFAULT.createDistribution(alphabet);
                String oneState = stateNames.get(i);
                emissionStates[i] =  new SimpleEmissionState(oneState,
Annotation.EMPTY_ANNOTATION,advance,dists[i] );
            }
            
            //add states to the model
            for(State s:emissionStates ){
                try{
                    mm.addState(s);
                }
                catch(Exception e){
                    throw new Exception("Can't add states to model!");
                }
            }
            
            
            //create transitions
            State magic = mm.magicalState();
            for(State i:emissionStates ){
                mm.createTransition(magic, i);
                for(State j: emissionStates){
                    mm.createTransition(i, j);
                }
            }
            
            //set up emission scores
            for(Iterator<?> i = alphabet.iterator(); i.hasNext();){
                AtomicSymbol oneSym = (AtomicSymbol) i.next();
                double [] symbolsInThisSymbolAsArrayOfDoubles =
makeArrayOfDoublesFromASymbol(oneSym);
                for(int d =0 ; d< dists.length; d++){
                    double dirichletPar [] =
arraysOfDirichletParameters.get(d);
                    double oneDensity         =
DirichletDist.density(dirichletPar, symbolsInThisSymbolAsArrayOfDoubles);
                    dists[d].setWeight(oneSym,oneDensity );
                   
                }
            }
            
                //set  transition scores
                Distribution transDist;
                
                //magical to others
                transDist = mm.getWeights(mm.magicalState());
                for(int i=0; i<emissionStates.length; i++){
                    transDist.setWeight(emissionStates[i],
startProbabilities[i]);
                }
                
                //from each state to others
                for(int i =0; i<emissionStates.length;i++){
                    transDist = mm.getWeights(emissionStates[i]);
                    transDist.setWeight(emissionStates[i],
transitionMatrix[i][i]);
                    for(int j =0; j<emissionStates.length; j++){
                        if(i !=j){
                            transDist.setWeight(emissionStates[j],
transitionMatrix[i][j]);
                        }
                    }
                }  
            System.out.println("One HMM set up!");
            return mm;
        }/*makeMarkovModel*/



In the mailing list I see Thomas Covello posted similar problem on May 2009
but don¹t see any treat to it.
Thanks 
Hashem
-------------------------------
Hashem Koohy
PhD
Postdoctoral Fellow,
Sanger Institute,
Cambridge
Mobile: 07515425433





-- 
 The Wellcome Trust Sanger Institute is operated by Genome Research 
 Limited, a charity registered in England with number 1021457 and a 
 company registered in England with number 2742969, whose registered 
 office is 215 Euston Road, London, NW1 2BE. 





More information about the Biojava-l mailing list