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