2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.1)
3 * Copyright (C) 2014 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 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/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
19 package jalview.datamodel;
21 import jalview.schemes.*;
24 * Encode a sequence as a numeric vector using either classic residue binary
25 * encoding or convolved with residue substitution matrix.
30 public class BinarySequence extends Sequence
32 public class InvalidSequenceTypeException extends Exception
35 public InvalidSequenceTypeException(String string)
49 * Creates a new BinarySequence object.
54 public BinarySequence(String s, boolean isNa)
56 super("", s, 0, s.length());
61 * clear the dbinary matrix
63 * @return nores - dimension of sequence symbol encoding for this sequence
65 private int initMatrixGetNoRes()
67 int nores = (isNa) ? ResidueProperties.maxNucleotideIndex
68 : ResidueProperties.maxProteinIndex;
69 // Set all matrix to 0
70 dbinary = new double[getSequence().length * nores];
72 for (int i = 0; i < dbinary.length; i++)
79 private int[] getSymbolmatrix()
81 return (isNa) ? ResidueProperties.nucleotideIndex
82 : ResidueProperties.aaIndex;
90 int nores = initMatrixGetNoRes();
91 final int[] sindex = getSymbolmatrix();
92 for (int i = 0; i < getSequence().length; i++)
94 int aanum = nores - 1;
98 aanum = sindex[getCharAt(i)];
99 } catch (NullPointerException e)
109 dbinary[(i * nores) + aanum] = 1.0;
114 * ancode using substitution matrix given in matrix
118 public void matrixEncode(final ScoreMatrix matrix)
119 throws InvalidSequenceTypeException
121 if (isNa != matrix.isDNA())
123 throw new InvalidSequenceTypeException("matrix "
124 + matrix.getClass().getCanonicalName()
125 + " is not a valid matrix for "
126 + (isNa ? "nucleotide" : "protein") + "sequences");
128 matrixEncode(matrix.isDNA() ? ResidueProperties.nucleotideIndex
129 : ResidueProperties.aaIndex, matrix.getMatrix());
132 private void matrixEncode(final int[] aaIndex, final int[][] matrix)
134 // Set all matrix to 0
135 // dbinary = new double[getSequence().length * 21];
137 int nores = initMatrixGetNoRes();
139 // for (int i = 0; i < dbinary.length; i++) {
142 for (int i = 0, iSize = getSequence().length; i < iSize; i++)
144 int aanum = nores - 1;
148 aanum = aaIndex[getCharAt(i)];
149 } catch (NullPointerException e)
159 // Do the blosum^H^H^H^H^H score matrix summation thing
161 for (int j = 0; j < nores; j++)
163 dbinary[(i * nores) + j] = matrix[aanum][j];
171 * @return DOCUMENT ME!
173 public String toBinaryString()
177 for (int i = 0; i < binary.length; i++)
179 out += (new Integer(binary[i])).toString();
181 if (i < (binary.length - 1))
193 * @return DOCUMENT ME!
195 public double[] getDBinary()