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.*;
26 * Encode a sequence as a numeric vector using either classic residue binary
27 * encoding or convolved with residue substitution matrix.
32 public class BinarySequence extends Sequence
34 public class InvalidSequenceTypeException extends Exception
37 public InvalidSequenceTypeException(String string)
51 * Creates a new BinarySequence object.
56 public BinarySequence(String s, boolean isNa)
58 super("", s, 0, s.length());
63 * clear the dbinary matrix
65 * @return nores - dimension of sequence symbol encoding for this sequence
67 private int initMatrixGetNoRes()
69 int nores = (isNa) ? ResidueProperties.maxNucleotideIndex
70 : ResidueProperties.maxProteinIndex;
71 // Set all matrix to 0
72 dbinary = new double[getSequence().length * nores];
74 for (int i = 0; i < dbinary.length; i++)
81 private int[] getSymbolmatrix()
83 return (isNa) ? ResidueProperties.nucleotideIndex
84 : ResidueProperties.aaIndex;
92 int nores = initMatrixGetNoRes();
93 final int[] sindex = getSymbolmatrix();
94 for (int i = 0; i < getSequence().length; i++)
96 int aanum = nores - 1;
100 aanum = sindex[getCharAt(i)];
101 } catch (NullPointerException e)
111 dbinary[(i * nores) + aanum] = 1.0;
116 * ancode using substitution matrix given in matrix
120 public void matrixEncode(final ScoreMatrix matrix)
121 throws InvalidSequenceTypeException
123 if (isNa != matrix.isDNA())
125 throw new InvalidSequenceTypeException("matrix "
126 + matrix.getClass().getCanonicalName()
127 + " is not a valid matrix for "
128 + (isNa ? "nucleotide" : "protein") + "sequences");
130 matrixEncode(matrix.isDNA() ? ResidueProperties.nucleotideIndex
131 : ResidueProperties.aaIndex, matrix.getMatrix());
134 private void matrixEncode(final int[] aaIndex, final int[][] matrix)
136 // Set all matrix to 0
137 // dbinary = new double[getSequence().length * 21];
139 int nores = initMatrixGetNoRes();
141 // for (int i = 0; i < dbinary.length; i++) {
144 for (int i = 0, iSize = getSequence().length; i < iSize; i++)
146 int aanum = nores - 1;
150 aanum = aaIndex[getCharAt(i)];
151 } catch (NullPointerException e)
161 // Do the blosum^H^H^H^H^H score matrix summation thing
163 for (int j = 0; j < nores; j++)
165 dbinary[(i * nores) + j] = matrix[aanum][j];
173 * @return DOCUMENT ME!
175 public String toBinaryString()
179 for (int i = 0; i < binary.length; i++)
181 out += (new Integer(binary[i])).toString();
183 if (i < (binary.length - 1))
195 * @return DOCUMENT ME!
197 public double[] getDBinary()