2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.datamodel;
23 import jalview.analysis.scoremodels.ScoreMatrix;
24 import jalview.schemes.ResidueProperties;
27 * Encode a sequence as a numeric vector using either classic residue binary
28 * encoding or convolved with residue substitution matrix.
33 public class BinarySequence extends Sequence
35 public class InvalidSequenceTypeException extends Exception
38 public InvalidSequenceTypeException(String string)
52 * Creates a new BinarySequence object.
57 public BinarySequence(String s, boolean isNa)
59 super("", s, 0, s.length());
64 * clear the dbinary matrix
66 * @return nores - dimension of sequence symbol encoding for this sequence
68 private int initMatrixGetNoRes()
70 int nores = (isNa) ? ResidueProperties.maxNucleotideIndex
71 : ResidueProperties.maxProteinIndex;
72 dbinary = new double[getLength() * nores];
77 private int[] getSymbolmatrix()
79 return (isNa) ? ResidueProperties.nucleotideIndex
80 : ResidueProperties.aaIndex;
88 int nores = initMatrixGetNoRes();
89 final int[] sindex = getSymbolmatrix();
90 for (int i = 0; i < getLength(); i++)
92 int aanum = nores - 1;
96 aanum = sindex[getCharAt(i)];
97 } catch (NullPointerException e)
107 dbinary[(i * nores) + aanum] = 1.0;
112 * ancode using substitution matrix given in matrix
116 public void matrixEncode(final ScoreMatrix smtrx)
117 throws InvalidSequenceTypeException
119 if (isNa != smtrx.isDNA())
121 throw new InvalidSequenceTypeException("matrix "
122 + smtrx.getClass().getCanonicalName()
123 + " is not a valid matrix for "
124 + (isNa ? "nucleotide" : "protein") + "sequences");
126 matrixEncode(smtrx.isDNA() ? ResidueProperties.nucleotideIndex
127 : ResidueProperties.aaIndex, smtrx.getMatrix());
130 private void matrixEncode(final int[] aaIndex, final float[][] matrix)
132 int nores = initMatrixGetNoRes();
134 for (int i = 0, iSize = getSequence().length; i < iSize; i++)
136 int aanum = nores - 1;
140 aanum = aaIndex[getCharAt(i)];
141 } catch (NullPointerException e)
151 // Do the blosum^H^H^H^H^H score matrix summation thing
153 for (int j = 0; j < nores; j++)
155 dbinary[(i * nores) + j] = matrix[aanum][j];
163 * @return DOCUMENT ME!
165 public String toBinaryString()
169 for (int i = 0; i < binary.length; i++)
171 out += (new Integer(binary[i])).toString();
173 if (i < (binary.length - 1))
185 * @return DOCUMENT ME!
187 public double[] getDBinary()