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.schemes.ResidueProperties;
24 import jalview.schemes.ScoreMatrix;
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 // Set all matrix to 0
73 dbinary = new double[getSequence().length * nores];
75 for (int i = 0; i < dbinary.length; i++)
82 private int[] getSymbolmatrix()
84 return (isNa) ? ResidueProperties.nucleotideIndex
85 : ResidueProperties.aaIndex;
93 int nores = initMatrixGetNoRes();
94 final int[] sindex = getSymbolmatrix();
95 for (int i = 0; i < getSequence().length; i++)
97 int aanum = nores - 1;
101 aanum = sindex[getCharAt(i)];
102 } catch (NullPointerException e)
112 dbinary[(i * nores) + aanum] = 1.0;
117 * ancode using substitution matrix given in matrix
121 public void matrixEncode(final ScoreMatrix matrix)
122 throws InvalidSequenceTypeException
124 if (isNa != matrix.isDNA())
126 throw new InvalidSequenceTypeException("matrix "
127 + matrix.getClass().getCanonicalName()
128 + " is not a valid matrix for "
129 + (isNa ? "nucleotide" : "protein") + "sequences");
131 matrixEncode(matrix.isDNA() ? ResidueProperties.nucleotideIndex
132 : ResidueProperties.aaIndex, matrix.getMatrix());
135 private void matrixEncode(final int[] aaIndex, final int[][] matrix)
137 // Set all matrix to 0
138 // dbinary = new double[getSequence().length * 21];
140 int nores = initMatrixGetNoRes();
142 // for (int i = 0; i < dbinary.length; i++) {
145 for (int i = 0, iSize = getSequence().length; i < iSize; i++)
147 int aanum = nores - 1;
151 aanum = aaIndex[getCharAt(i)];
152 } catch (NullPointerException e)
162 // Do the blosum^H^H^H^H^H score matrix summation thing
164 for (int j = 0; j < nores; j++)
166 dbinary[(i * nores) + j] = matrix[aanum][j];
174 * @return DOCUMENT ME!
176 public String toBinaryString()
180 for (int i = 0; i < binary.length; i++)
182 out += (new Integer(binary[i])).toString();
184 if (i < (binary.length - 1))
196 * @return DOCUMENT ME!
198 public double[] getDBinary()