Merge branch 'releases/Release_2_11_3_Branch'
[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
73     dbinary = new double[getLength() * nores];
74
75     return nores;
76   }
77
78   private int[] getSymbolmatrix()
79   {
80     return (isNa) ? ResidueProperties.nucleotideIndex
81             : ResidueProperties.aaIndex;
82   }
83
84   /**
85    * DOCUMENT ME!
86    */
87   public void encode()
88   {
89     int nores = initMatrixGetNoRes();
90     final int[] sindex = getSymbolmatrix();
91     for (int i = 0; i < getLength(); i++)
92     {
93       int aanum = nores - 1;
94
95       try
96       {
97         aanum = sindex[getCharAt(i)];
98       } catch (NullPointerException e)
99       {
100         aanum = nores - 1;
101       }
102
103       if (aanum >= nores)
104       {
105         aanum = nores - 1;
106       }
107
108       dbinary[(i * nores) + aanum] = 1.0;
109     }
110   }
111
112   /**
113    * ancode using substitution matrix given in matrix
114    * 
115    * @param smtrx
116    */
117   public void matrixEncode(final ScoreMatrix smtrx)
118           throws InvalidSequenceTypeException
119   {
120     if (isNa != smtrx.isDNA())
121     {
122       throw new InvalidSequenceTypeException(
123               "matrix " + smtrx.getClass().getCanonicalName()
124                       + " is not a valid matrix for "
125                       + (isNa ? "nucleotide" : "protein") + "sequences");
126     }
127     matrixEncode(smtrx.isDNA() ? ResidueProperties.nucleotideIndex
128             : ResidueProperties.aaIndex, smtrx.getMatrix());
129   }
130
131   private void matrixEncode(final int[] aaIndex, final float[][] matrix)
132   {
133     int nores = initMatrixGetNoRes();
134
135     for (int i = 0, iSize = getLength(); i < iSize; i++)
136     {
137       int aanum = nores - 1;
138
139       try
140       {
141         aanum = aaIndex[getCharAt(i)];
142       } catch (NullPointerException e)
143       {
144         aanum = nores - 1;
145       }
146
147       if (aanum >= nores)
148       {
149         aanum = nores - 1;
150       }
151
152       // Do the blosum^H^H^H^H^H score matrix summation thing
153
154       for (int j = 0; j < nores; j++)
155       {
156         dbinary[(i * nores) + j] = matrix[aanum][j];
157       }
158     }
159   }
160
161   /**
162    * DOCUMENT ME!
163    * 
164    * @return DOCUMENT ME!
165    */
166   public String toBinaryString()
167   {
168     String out = "";
169
170     for (int i = 0; i < binary.length; i++)
171     {
172       out += (Integer.valueOf(binary[i])).toString();
173
174       if (i < (binary.length - 1))
175       {
176         out += " ";
177       }
178     }
179
180     return out;
181   }
182
183   /**
184    * DOCUMENT ME!
185    * 
186    * @return DOCUMENT ME!
187    */
188   public double[] getDBinary()
189   {
190     return dbinary;
191   }
192
193 }