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
022
023package org.biojava.bio.dp;
024
025import java.io.Serializable;
026
027import org.biojava.bio.dist.Distribution;
028import org.biojava.bio.dist.DistributionFactory;
029import org.biojava.bio.symbol.Alphabet;
030import org.biojava.bio.symbol.IllegalAlphabetException;
031
032/**
033 * @author Matthew Pocock
034 */
035public class SimpleWeightMatrix implements WeightMatrix, Serializable {
036  private static final long serialVersionUID = 73394340224964858L;
037    
038  private final Distribution [] columns;
039  private final Alphabet alpha;
040
041  public Alphabet getAlphabet() {
042    return alpha;
043  }
044
045  public int columns() {
046    return this.columns.length;
047  }
048  
049  public Distribution getColumn(int column) {
050    return columns[column];
051  }
052
053  public SimpleWeightMatrix(Alphabet alpha, int columns, DistributionFactory dFact)
054  throws IllegalAlphabetException {
055    this.alpha = alpha;
056    this.columns = new Distribution[columns];
057    for(int i = 0; i < columns; i++) {
058      this.columns[i] = dFact.createDistribution(alpha);
059    }
060  }
061  
062  public SimpleWeightMatrix(Distribution[] columns)
063  throws IllegalAlphabetException {
064    this.alpha = columns[0].getAlphabet();
065    for(int c = 0; c < columns.length; c++) {
066      if(columns[c].getAlphabet() != alpha) {
067        throw new IllegalAlphabetException(
068          "All columns must emit the same alphabet. Expecting " +
069          alpha.getName() + ", but found " + columns[c].getAlphabet().getName()
070        );
071      }
072    }
073    this.columns = columns;
074  }
075  
076  public int hashCode() {
077      int hc = 0;
078      for (int c = 0; c < columns.length; ++c) {
079          hc = (23 * hc) + columns[c].hashCode();
080      }
081      return hc;
082  }
083  
084  public boolean equals(Object o) {
085      if (o instanceof WeightMatrix) {
086          WeightMatrix wm = (WeightMatrix) o;
087          if (wm.columns() != this.columns()) {
088              return false;
089          }
090          if (wm.getAlphabet() != this.getAlphabet()) {
091              return false;
092          }
093          
094          for (int c = 0; c < columns(); ++c) {
095              if (! this.getColumn(c).equals(wm.getColumn(c))) {
096                  return false;
097              }
098          }
099          return true;
100      }
101      return false;
102  }
103}