[Biojava-l] Up-to-date Dice.Java

Mark Schreiber mark_s@sanger.otago.ac.nz
Thu, 18 Jan 2001 14:26:22 +1300 (NZDT)


Hi,

I did find that the most recent CVS copy of Dice.java was still a little
out of date and wouldn't compile. So here is the modified source which
seems to work.

If anyone has comments on how I may have done it better please let me
know.

Mark

/*
 *                    BioJava development code
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  If you do not have a copy,
 * see:
 *
 *      http://www.gnu.org/copyleft/lesser.html
 *
 * Copyright for this code is held jointly by the individual
 * authors.  These should be listed in @author doc comments.
 *
 * For more information on the BioJava project and its aims,
 * or to join the biojava-l mailing list, visit the home page
 * at:
 *
 *      http://www.biojava.org/
 *
 */
package biojava.tutorials;

import org.biojava.bio.*;
import org.biojava.bio.symbol.*;
import org.biojava.bio.seq.*;
import org.biojava.bio.dist.*;
import org.biojava.bio.dp.*;

/**
 * This demo file is a simulation of the "The occasionally dishonest
casino" example
 * from the book by R. Durbin, S. Eddy, A. Krogh, G. Mitchison,
 * "Biological Sequence Analysis",
 * Chapter 3 Markov Chains and hidden Markov models, Section 2, pp55-57.
 * <P>
 * Use: <code>Dice</code>
 * <p>
 * The output consists of three lines:  line 1 represents the output
sequence generated
 * by the hidden markov model (f for fair and l for loaded).  Line 2
contains the name of
 * the die which emitted the corresponding output symbol.  Line 3 shows
the state
 * sequence predicted by the Viterbi algorithm.
 * <P>
 *
 * @author Samiul Hasan
 */

public class Dice
{
  public static void main(String[] args)
  throws Exception {
  	Symbol[] rolls=new Symbol[6];

   	//set up the dice alphabet
   	SimpleAlphabet diceAlphabet=new SimpleAlphabet();
   	diceAlphabet.setName("DiceAlphabet");
        AlphabetManager manager = new AlphabetManager();
   	for(int i=1;i<7;i++) {
	    rolls[i-1]=
manager.createSymbol((char)('0'+i),""+i,Annotation.EMPTY_ANNOTATION);
	    diceAlphabet.addSymbol(rolls[i-1]);
    }

    int [] advance = { 1 };
    Distribution fairD =
DistributionFactory.DEFAULT.createDistribution(diceAlphabet);
    EmissionState fairS = new SimpleEmissionState("fair",
Annotation.EMPTY_ANNOTATION, advance, fairD);
    Distribution loadedD =
DistributionFactory.DEFAULT.createDistribution(diceAlphabet);
    EmissionState loadedS = new SimpleEmissionState("loaded",
Annotation.EMPTY_ANNOTATION, advance, loadedD);

	  SimpleMarkovModel casino = new SimpleMarkovModel(1,
diceAlphabet, "Casino");
    casino.addState(fairS);
    casino.addState(loadedS);

	  //set up transitions between states.
    casino.createTransition(casino.magicalState(),fairS);
    casino.createTransition(casino.magicalState(),loadedS);
    casino.createTransition(fairS,casino.magicalState());
    casino.createTransition(loadedS,casino.magicalState());
    casino.createTransition(fairS,loadedS);
    casino.createTransition(loadedS,fairS);
    casino.createTransition(fairS,fairS);
    casino.createTransition(loadedS,loadedS);

	  //set up emission probabilities.
    for(int i=0;i<rolls.length;i++)	{
	    fairD.setWeight(rolls[i],1.0/6.0);
	    loadedD.setWeight(rolls[i], 0.1);
    }
    loadedD.setWeight(rolls[5],0.5);

	  //set up transition scores.
    Distribution dist;

    dist = casino.getWeights(casino.magicalState());
    dist.setWeight(fairS, 0.8);
    dist.setWeight(loadedS, 0.2);

    dist = casino.getWeights(fairS);
    dist.setWeight(loadedS,               0.04);
    dist.setWeight(fairS,                 0.95);
    dist.setWeight(casino.magicalState(), 0.01);

    dist = casino.getWeights(loadedS);
    dist.setWeight(fairS,                 0.09);
    dist.setWeight(loadedS,               0.90);
    dist.setWeight(casino.magicalState(), 0.01);

	  DP dp=DPFactory.DEFAULT.createDP(casino);
    StatePath obs_rolls = dp.generate(300);

	  SymbolList roll_sequence =
obs_rolls.symbolListForLabel(StatePath.SEQUENCE);
    SymbolList[] res_array = {roll_sequence};
    StatePath v = dp.viterbi(res_array, ScoreType.PROBABILITY);

	  //print out obs_sequence, output, state symbols.
    for(int i = 1; i <= obs_rolls.length()/60; i++) {
      for(int j=i*60; j<Math.min((i+1)*60, obs_rolls.length()); j++)  {
        System.out.print(obs_rolls.symbolAt(StatePath.SEQUENCE,
j+1).getToken());
      }
      System.out.print("\n");
      for(int j=i*60; j<Math.min((i+1)*60, obs_rolls.length()); j++)  {
        System.out.print(obs_rolls.symbolAt(StatePath.STATES,
j+1).getToken());
      }
      System.out.print("\n");
      for(int j=i*60; j<Math.min((i+1)*60, obs_rolls.length()); j++)  {
        System.out.print(v.symbolAt(StatePath.STATES, j+1).getToken());
      }
      System.out.print("\n\n");
    }
  }
}


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Mark Schreiber			Ph: 64 3 4797875
Rm 218				email mark_s@sanger.otago.ac.nz
Department of Biochemistry	email m.schreiber@clear.net.nz
University of Otago		
PO Box 56
Dunedin
New Zealand
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~