Exemple #1
0
    private static MarkovModel makeMarkovModel(
        SimpleAlphabet alphabet,
        double[][] transitionMatrix,
        double[] startProbabilities,
        LinkedHashMap<String, double[]> statesAndCorresponingDirichletParameters,
        String modelName)
        throws Exception {

      int[] advance = {1};
      SimpleAlphabet anAlphabet = alphabet;

      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];

      try {
        for (int i = 0; i < numberOfStates; i++) {
          dists[i] = DistributionFactory.DEFAULT.createDistribution(anAlphabet);
          String oneState = stateNames.get(i);
          emissionStates[i] =
              new SimpleEmissionState(oneState, Annotation.EMPTY_ANNOTATION, advance, dists[i]);
        }
      } catch (Exception e) {
        throw new Exception("Can't create distributions!");
      }

      // create HMM
      SimpleMarkovModel mm = new SimpleMarkovModel(1, anAlphabet, modelName);

      // 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
      try {
        State magic = mm.magicalState();
        for (State i : emissionStates) {
          mm.createTransition(magic, i);
          mm.createTransition(i, magic);
          for (State j : emissionStates) {
            mm.createTransition(i, j);
          }
        }
      } catch (Exception e) {
        throw new Exception("Can't create transitions!");
      }

      // set up emission scores
      for (Iterator<?> i = anAlphabet.iterator(); i.hasNext(); ) {
        AtomicSymbol oneSym = (AtomicSymbol) i.next();
        // System.out.println(oneSym.getName());
        double[] symbolsInThisSymbolAsArrayOfDoubles = makeArrayOfDoublesFromASymbol(oneSym);
        for (int d = 0; d < dists.length; d++) {
          double dirichletPar[] = arraysOfDirichletParameters.get(d);
          double oneDensity =
              DirichletDist.density(dirichletPar, symbolsInThisSymbolAsArrayOfDoubles);

          // System.out.print( oneDensity + "  ");

          oneDensity =
              oneDensity
                  + 100; // because some of these scores are zero, this one unit is to be able to
                         // take log! 100

          dists[d].setWeight(oneSym, oneDensity);
        }
        // System.out.println();
      }

      // 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(mm.magicalState(), 0.0001);
        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*/
Exemple #2
0
    public LinkedHashMap<SimpleAlphabet, SimpleSymbolList> getAlphabetAndSimpleSymbolList(
        Matrix2D m, Sequence sequence) throws IllegalSymbolException {
      LinkedHashMap<SimpleAlphabet, SimpleSymbolList> alphabetAndSymbolList =
          new LinkedHashMap<SimpleAlphabet, SimpleSymbolList>();

      SimpleAlphabet alphabet = new SimpleAlphabet();
      List<AtomicSymbol> listOfSymbols = new ArrayList<AtomicSymbol>();
      alphabet.setName("ObservedSequenceAlphabet");
      int numberofRows = m.rows();
      int seqLength = sequence.length();
      if (numberofRows != seqLength) {
        System.err.print(
            "It was assumed your sequence has a length equal to  the number of rows of the matrix, but found a case that is not true!");
      }

      Symbol gap = sequence.getAlphabet().getGapSymbol();

      for (int i = 0; i < numberofRows; i++) {
        List<Symbol> oneListOfSymbol = new ArrayList<Symbol>(3);

        // red is match, green is flanking and blue is background
        double redValue = m.get(i, 0);
        double greenValue = m.get(i, 1);
        double blueValue = m.get(i, 2);
        double onesum = redValue + greenValue + blueValue;
        boolean isGap = false;
        isGap = (sequence.symbolAt(i + 1) == gap);
        if (onesum
            == 0) { // sum of this value is supposed to be one, but for some rows it sums up to
                    // zero, this is to ignore those up to time we found out why these naouthy rows
                    // sums up to zero!
          // continue;
          // note these three lines is only a dummy solution for positions where red, green and blue
          // are summed up to zero!
          redValue = 0.3333;
          greenValue = 0.3333;
          blueValue = 1 - (redValue + greenValue);
        }

        // make one triplet symbol from three symbols
        Symbol redSymbol = AlphabetManager.createSymbol(Double.toString(redValue));
        Symbol greenSymol = AlphabetManager.createSymbol(Double.toString(greenValue));
        Symbol blueSymbol = AlphabetManager.createSymbol(Double.toString(blueValue));
        oneListOfSymbol.add(redSymbol);
        oneListOfSymbol.add(greenSymol);
        oneListOfSymbol.add(blueSymbol);

        // now create symbol and add it to alphabet
        AtomicSymbol oneSym =
            (AtomicSymbol)
                AlphabetManager.createSymbol(
                    Annotation.EMPTY_ANNOTATION, oneListOfSymbol, alphabet);
        alphabet.addSymbol(oneSym);
        listOfSymbols.add(oneSym);
      }

      SimpleSymbolList ssl = new SimpleSymbolList(alphabet, listOfSymbols);

      alphabetAndSymbolList.put(alphabet, ssl);
      return alphabetAndSymbolList;
    } /*getAlphabetAndSimpleSymbolList*/