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