2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, 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)
47 * Creates a new BinarySequence object.
52 public BinarySequence(String s, boolean isNa)
54 super("", s, 0, s.length());
59 * clear the dbinary matrix
60 * @return nores - dimension of sequence symbol encoding for this sequence
62 private int initMatrixGetNoRes()
64 int nores=(isNa) ? ResidueProperties.maxNucleotideIndex : ResidueProperties.maxProteinIndex;
65 // Set all matrix to 0
66 dbinary = new double[getSequence().length * nores];
68 for (int i = 0; i < dbinary.length; i++)
74 private int[] getSymbolmatrix()
76 return (isNa) ? ResidueProperties.nucleotideIndex : ResidueProperties.aaIndex;
83 int nores=initMatrixGetNoRes();
84 final int[] sindex=getSymbolmatrix();
85 for (int i = 0; i < getSequence().length; i++)
91 aanum = sindex[getCharAt(i)];
92 } catch (NullPointerException e)
102 dbinary[(i * nores) + aanum] = 1.0;
107 * ancode using substitution matrix given in matrix
111 public void matrixEncode(final ScoreMatrix matrix) throws InvalidSequenceTypeException
113 if (isNa!=matrix.isDNA())
115 throw new InvalidSequenceTypeException("matrix "
116 + matrix.getClass().getCanonicalName()
117 + " is not a valid matrix for "
118 + (isNa ? "nucleotide" : "protein") + "sequences");
120 matrixEncode(matrix.isDNA() ? ResidueProperties.nucleotideIndex
121 : ResidueProperties.aaIndex, matrix.getMatrix());
124 private void matrixEncode(final int[] aaIndex, final int[][] matrix)
126 // Set all matrix to 0
127 // dbinary = new double[getSequence().length * 21];
129 int nores = initMatrixGetNoRes();
131 // for (int i = 0; i < dbinary.length; i++) {
134 for (int i = 0,iSize=getSequence().length; i<iSize; i++)
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()