34e7cd098e7b0e0a5d3753e064eda6a8522cbd65
[jalview.git] / src / jalview / datamodel / BinarySequence.java
1 /*
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
4  * 
5  * This file is part of Jalview.
6  * 
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.
10  *  
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.
15  * 
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  */
18 package jalview.datamodel;
19
20 import jalview.schemes.*;
21
22 /**
23  * Encode a sequence as a numeric vector using either classic residue binary
24  * encoding or convolved with residue substitution matrix.
25  * 
26  * @author $author$
27  * @version $Revision$
28  */
29 public class BinarySequence extends Sequence
30 {
31   public class InvalidSequenceTypeException extends Exception
32   {
33
34     public InvalidSequenceTypeException(String string)
35     {
36       super(string);
37     }
38
39   }
40
41   int[] binary;
42
43   double[] dbinary;
44
45   boolean isNa = false;
46
47   /**
48    * Creates a new BinarySequence object.
49    * 
50    * @param s
51    *          DOCUMENT ME!
52    */
53   public BinarySequence(String s, boolean isNa)
54   {
55     super("", s, 0, s.length());
56     this.isNa = isNa;
57   }
58
59   /**
60    * clear the dbinary matrix
61    * 
62    * @return nores - dimension of sequence symbol encoding for this sequence
63    */
64   private int initMatrixGetNoRes()
65   {
66     int nores = (isNa) ? ResidueProperties.maxNucleotideIndex
67             : ResidueProperties.maxProteinIndex;
68     // Set all matrix to 0
69     dbinary = new double[getSequence().length * nores];
70
71     for (int i = 0; i < dbinary.length; i++)
72     {
73       dbinary[i] = 0.0;
74     }
75     return nores;
76   }
77
78   private int[] getSymbolmatrix()
79   {
80     return (isNa) ? ResidueProperties.nucleotideIndex
81             : ResidueProperties.aaIndex;
82   }
83
84   /**
85    * DOCUMENT ME!
86    */
87   public void encode()
88   {
89     int nores = initMatrixGetNoRes();
90     final int[] sindex = getSymbolmatrix();
91     for (int i = 0; i < getSequence().length; i++)
92     {
93       int aanum = nores - 1;
94
95       try
96       {
97         aanum = sindex[getCharAt(i)];
98       } catch (NullPointerException e)
99       {
100         aanum = nores - 1;
101       }
102
103       if (aanum >= nores)
104       {
105         aanum = nores - 1;
106       }
107
108       dbinary[(i * nores) + aanum] = 1.0;
109     }
110   }
111
112   /**
113    * ancode using substitution matrix given in matrix
114    * 
115    * @param matrix
116    */
117   public void matrixEncode(final ScoreMatrix matrix)
118           throws InvalidSequenceTypeException
119   {
120     if (isNa != matrix.isDNA())
121     {
122       throw new InvalidSequenceTypeException("matrix "
123               + matrix.getClass().getCanonicalName()
124               + " is not a valid matrix for "
125               + (isNa ? "nucleotide" : "protein") + "sequences");
126     }
127     matrixEncode(matrix.isDNA() ? ResidueProperties.nucleotideIndex
128             : ResidueProperties.aaIndex, matrix.getMatrix());
129   }
130
131   private void matrixEncode(final int[] aaIndex, final int[][] matrix)
132   {
133     // Set all matrix to 0
134     // dbinary = new double[getSequence().length * 21];
135
136     int nores = initMatrixGetNoRes();
137
138     // for (int i = 0; i < dbinary.length; i++) {
139     // dbinary[i] = 0.0;
140     // }
141     for (int i = 0, iSize = getSequence().length; i < iSize; i++)
142     {
143       int aanum = nores - 1;
144
145       try
146       {
147         aanum = aaIndex[getCharAt(i)];
148       } catch (NullPointerException e)
149       {
150         aanum = nores - 1;
151       }
152
153       if (aanum >= nores)
154       {
155         aanum = nores - 1;
156       }
157
158       // Do the blosum^H^H^H^H^H score matrix summation thing
159
160       for (int j = 0; j < nores; j++)
161       {
162         dbinary[(i * nores) + j] = matrix[aanum][j];
163       }
164     }
165   }
166
167   /**
168    * DOCUMENT ME!
169    * 
170    * @return DOCUMENT ME!
171    */
172   public String toBinaryString()
173   {
174     String out = "";
175
176     for (int i = 0; i < binary.length; i++)
177     {
178       out += (new Integer(binary[i])).toString();
179
180       if (i < (binary.length - 1))
181       {
182         out += " ";
183       }
184     }
185
186     return out;
187   }
188
189   /**
190    * DOCUMENT ME!
191    * 
192    * @return DOCUMENT ME!
193    */
194   public double[] getDBinary()
195   {
196     return dbinary;
197   }
198
199 }