(JAL-1013) generalise binary sequence encoding scheme for nucleotide and protein...
[jalview.git] / src / jalview / datamodel / BinarySequence.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3  * Copyright (C) 2011 J Procter, AM Waterhouse, 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    * Creates a new BinarySequence object.
48    * 
49    * @param s
50    *          DOCUMENT ME!
51    */
52   public BinarySequence(String s, boolean isNa)
53   {
54     super("", s, 0, s.length());
55     this.isNa=isNa;
56   }
57
58   /**
59    * clear the dbinary matrix 
60    * @return nores - dimension of sequence symbol encoding for this sequence
61    */
62   private int initMatrixGetNoRes()
63   {
64     int nores=(isNa) ? ResidueProperties.maxNucleotideIndex : ResidueProperties.maxProteinIndex;
65     // Set all matrix to 0
66     dbinary = new double[getSequence().length * nores];
67
68     for (int i = 0; i < dbinary.length; i++)
69     {
70       dbinary[i] = 0.0;
71     }
72     return nores;
73   }
74   private int[] getSymbolmatrix()
75   {
76     return (isNa) ? ResidueProperties.nucleotideIndex : ResidueProperties.aaIndex;
77   }
78   /**
79    * DOCUMENT ME!
80    */
81   public void encode()
82   {
83     int nores=initMatrixGetNoRes();
84     final int[] sindex=getSymbolmatrix();
85     for (int i = 0; i < getSequence().length; i++)
86     {
87       int aanum = nores-1;
88
89       try
90       {
91         aanum = sindex[getCharAt(i)];
92       } catch (NullPointerException e)
93       {
94         aanum = nores-1;
95       }
96
97       if (aanum >= nores)
98       {
99         aanum = nores-1;
100       }
101
102       dbinary[(i * nores) + aanum] = 1.0;
103     }
104   }
105
106   /**
107    * ancode using substitution matrix given in matrix
108    * 
109    * @param matrix
110    */
111   public void matrixEncode(final ScoreMatrix matrix) throws InvalidSequenceTypeException
112   {
113     if (isNa!=matrix.isDNA())
114     {
115       throw new InvalidSequenceTypeException("matrix "
116               + matrix.getClass().getCanonicalName()
117               + " is not a valid matrix for "
118               + (isNa ? "nucleotide" : "protein") + "sequences");
119     }
120     matrixEncode(matrix.isDNA() ? ResidueProperties.nucleotideIndex
121             : ResidueProperties.aaIndex, matrix.getMatrix());
122   }
123
124   private void matrixEncode(final int[] aaIndex, final int[][] matrix)
125   {
126     // Set all matrix to 0
127     // dbinary = new double[getSequence().length * 21];
128
129     int nores = initMatrixGetNoRes();
130
131     // for (int i = 0; i < dbinary.length; i++) {
132     // dbinary[i] = 0.0;
133     // }
134     for (int i = 0,iSize=getSequence().length; i<iSize; i++)
135     {
136       int aanum = nores-1;
137
138       try
139       {
140         aanum = aaIndex[getCharAt(i)];
141       } catch (NullPointerException e)
142       {
143         aanum = nores-1;
144       }
145
146       if (aanum >=nores)
147       {
148         aanum = nores-1;
149       }
150
151       // Do the blosum^H^H^H^H^H score matrix summation thing
152
153       for (int j = 0; j < nores; j++)
154       {
155         dbinary[(i * nores) + j] = matrix[aanum][j];
156       }
157     }
158   }
159
160   /**
161    * DOCUMENT ME!
162    * 
163    * @return DOCUMENT ME!
164    */
165   public String toBinaryString()
166   {
167     String out = "";
168
169     for (int i = 0; i < binary.length; i++)
170     {
171       out += (new Integer(binary[i])).toString();
172
173       if (i < (binary.length - 1))
174       {
175         out += " ";
176       }
177     }
178
179     return out;
180   }
181
182   /**
183    * DOCUMENT ME!
184    * 
185    * @return DOCUMENT ME!
186    */
187   public double[] getDBinary()
188   {
189     return dbinary;
190   }
191
192 }