Merge branch 'features/JAL-2446NCList' into spikes/mungo
[jalview.git] / src / jalview / datamodel / BinarySequence.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ 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.analysis.scoremodels.ScoreMatrix;
24 import jalview.schemes.ResidueProperties;
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     dbinary = new double[getLength() * nores];
73
74     return nores;
75   }
76
77   private int[] getSymbolmatrix()
78   {
79     return (isNa) ? ResidueProperties.nucleotideIndex
80             : ResidueProperties.aaIndex;
81   }
82
83   /**
84    * DOCUMENT ME!
85    */
86   public void encode()
87   {
88     int nores = initMatrixGetNoRes();
89     final int[] sindex = getSymbolmatrix();
90     for (int i = 0; i < getLength(); i++)
91     {
92       int aanum = nores - 1;
93
94       try
95       {
96         aanum = sindex[getCharAt(i)];
97       } catch (NullPointerException e)
98       {
99         aanum = nores - 1;
100       }
101
102       if (aanum >= nores)
103       {
104         aanum = nores - 1;
105       }
106
107       dbinary[(i * nores) + aanum] = 1.0;
108     }
109   }
110
111   /**
112    * ancode using substitution matrix given in matrix
113    * 
114    * @param smtrx
115    */
116   public void matrixEncode(final ScoreMatrix smtrx)
117           throws InvalidSequenceTypeException
118   {
119     if (isNa != smtrx.isDNA())
120     {
121       throw new InvalidSequenceTypeException("matrix "
122               + smtrx.getClass().getCanonicalName()
123               + " is not a valid matrix for "
124               + (isNa ? "nucleotide" : "protein") + "sequences");
125     }
126     matrixEncode(smtrx.isDNA() ? ResidueProperties.nucleotideIndex
127             : ResidueProperties.aaIndex, smtrx.getMatrix());
128   }
129
130   private void matrixEncode(final int[] aaIndex, final float[][] matrix)
131   {
132     int nores = initMatrixGetNoRes();
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 }