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