apply gpl development license
[jalview.git] / src / jalview / datamodel / BinarySequence.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Development Version 2.4.1)
3  * Copyright (C) 2009 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4  * 
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18  */
19 package jalview.datamodel;
20
21 import jalview.schemes.*;
22
23 /**
24  * DOCUMENT ME!
25  * 
26  * @author $author$
27  * @version $Revision$
28  */
29 public class BinarySequence extends Sequence
30 {
31   int[] binary;
32
33   double[] dbinary;
34
35   /**
36    * Creates a new BinarySequence object.
37    * 
38    * @param s
39    *                DOCUMENT ME!
40    */
41   public BinarySequence(String s)
42   {
43     super("", s, 0, s.length());
44   }
45
46   /**
47    * DOCUMENT ME!
48    */
49   public void encode()
50   {
51     // Set all matrix to 0
52     dbinary = new double[getSequence().length * 21];
53
54     int nores = 21;
55
56     for (int i = 0; i < dbinary.length; i++)
57     {
58       dbinary[i] = 0.0;
59     }
60
61     for (int i = 0; i < getSequence().length; i++)
62     {
63       int aanum = 20;
64
65       try
66       {
67         aanum = ResidueProperties.aaIndex[getCharAt(i)];
68       } catch (NullPointerException e)
69       {
70         aanum = 20;
71       }
72
73       if (aanum > 20)
74       {
75         aanum = 20;
76       }
77
78       dbinary[(i * nores) + aanum] = 1.0;
79     }
80   }
81
82   /**
83    * ancode using substitution matrix given in matrix
84    * 
85    * @param matrix
86    */
87   public void matrixEncode(ScoreMatrix matrix)
88   {
89     matrixEncode(matrix.isDNA() ? ResidueProperties.nucleotideIndex
90             : ResidueProperties.aaIndex, matrix.getMatrix());
91   }
92
93   /**
94    * DOCUMENT ME!
95    */
96   public void blosumEncode()
97   {
98     matrixEncode(ResidueProperties.aaIndex, ResidueProperties.getBLOSUM62());
99   }
100
101   private void matrixEncode(int[] aaIndex, int[][] matrix)
102   {
103     // Set all matrix to 0
104     dbinary = new double[getSequence().length * 21];
105
106     int nores = 21;
107
108     // for (int i = 0; i < dbinary.length; i++) {
109     // dbinary[i] = 0.0;
110     // }
111     for (int i = 0; i < getSequence().length; i++)
112     {
113       int aanum = 20;
114
115       try
116       {
117         aanum = aaIndex[getCharAt(i)];
118       } catch (NullPointerException e)
119       {
120         aanum = 20;
121       }
122
123       if (aanum > 20)
124       {
125         aanum = 20;
126       }
127
128       // Do the blosum thing
129
130       for (int j = 0; j < 20; j++)
131       {
132         dbinary[(i * nores) + j] = matrix[aanum][j];
133       }
134     }
135   }
136
137   /**
138    * DOCUMENT ME!
139    * 
140    * @return DOCUMENT ME!
141    */
142   public String toBinaryString()
143   {
144     String out = "";
145
146     for (int i = 0; i < binary.length; i++)
147     {
148       out += (new Integer(binary[i])).toString();
149
150       if (i < (binary.length - 1))
151       {
152         out += " ";
153       }
154     }
155
156     return out;
157   }
158
159   /**
160    * DOCUMENT ME!
161    * 
162    * @return DOCUMENT ME!
163    */
164   public double[] getDBinary()
165   {
166     return dbinary;
167   }
168
169 }