2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
18 package jalview.datamodel;
20 import jalview.schemes.*;
23 * Encode a sequence as a numeric vector using either classic residue binary
24 * encoding or convolved with residue substitution matrix.
29 public class BinarySequence extends Sequence
31 public class InvalidSequenceTypeException extends Exception
34 public InvalidSequenceTypeException(String string)
48 * Creates a new BinarySequence object.
53 public BinarySequence(String s, boolean isNa)
55 super("", s, 0, s.length());
60 * clear the dbinary matrix
62 * @return nores - dimension of sequence symbol encoding for this sequence
64 private int initMatrixGetNoRes()
66 int nores = (isNa) ? ResidueProperties.maxNucleotideIndex
67 : ResidueProperties.maxProteinIndex;
68 // Set all matrix to 0
69 dbinary = new double[getSequence().length * nores];
71 for (int i = 0; i < dbinary.length; i++)
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 < getSequence().length; 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 matrix)
118 throws InvalidSequenceTypeException
120 if (isNa != matrix.isDNA())
122 throw new InvalidSequenceTypeException("matrix "
123 + matrix.getClass().getCanonicalName()
124 + " is not a valid matrix for "
125 + (isNa ? "nucleotide" : "protein") + "sequences");
127 matrixEncode(matrix.isDNA() ? ResidueProperties.nucleotideIndex
128 : ResidueProperties.aaIndex, matrix.getMatrix());
131 private void matrixEncode(final int[] aaIndex, final int[][] matrix)
133 // Set all matrix to 0
134 // dbinary = new double[getSequence().length * 21];
136 int nores = initMatrixGetNoRes();
138 // for (int i = 0; i < dbinary.length; i++) {
141 for (int i = 0, iSize = getSequence().length; i < iSize; i++)
143 int aanum = nores - 1;
147 aanum = aaIndex[getCharAt(i)];
148 } catch (NullPointerException e)
158 // Do the blosum^H^H^H^H^H score matrix summation thing
160 for (int j = 0; j < nores; j++)
162 dbinary[(i * nores) + j] = matrix[aanum][j];
170 * @return DOCUMENT ME!
172 public String toBinaryString()
176 for (int i = 0; i < binary.length; i++)
178 out += (new Integer(binary[i])).toString();
180 if (i < (binary.length - 1))
192 * @return DOCUMENT ME!
194 public double[] getDBinary()