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