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;
73 dbinary = new double[getLength() * nores];
78 private int[] getSymbolmatrix()
80 return (isNa) ? ResidueProperties.nucleotideIndex
81 : ResidueProperties.aaIndex;
89 int nores = initMatrixGetNoRes();
90 final int[] sindex = getSymbolmatrix();
91 for (int i = 0; i < getLength(); i++)
93 int aanum = nores - 1;
97 aanum = sindex[getCharAt(i)];
98 } catch (NullPointerException e)
108 dbinary[(i * nores) + aanum] = 1.0;
113 * ancode using substitution matrix given in matrix
117 public void matrixEncode(final ScoreMatrix smtrx)
118 throws InvalidSequenceTypeException
120 if (isNa != smtrx.isDNA())
122 throw new InvalidSequenceTypeException(
123 "matrix " + smtrx.getClass().getCanonicalName()
124 + " is not a valid matrix for "
125 + (isNa ? "nucleotide" : "protein") + "sequences");
127 matrixEncode(smtrx.isDNA() ? ResidueProperties.nucleotideIndex
128 : ResidueProperties.aaIndex, smtrx.getMatrix());
131 private void matrixEncode(final int[] aaIndex, final float[][] matrix)
133 int nores = initMatrixGetNoRes();
135 for (int i = 0, iSize = getLength(); i < iSize; i++)
137 int aanum = nores - 1;
141 aanum = aaIndex[getCharAt(i)];
142 } catch (NullPointerException e)
152 // Do the blosum^H^H^H^H^H score matrix summation thing
154 for (int j = 0; j < nores; j++)
156 dbinary[(i * nores) + j] = matrix[aanum][j];
164 * @return DOCUMENT ME!
166 public String toBinaryString()
170 for (int i = 0; i < binary.length; i++)
172 out += (Integer.valueOf(binary[i])).toString();
174 if (i < (binary.length - 1))
186 * @return DOCUMENT ME!
188 public double[] getDBinary()