Remove redundancy in Eclipse
[jalview.git] / src / jalview / datamodel / BinarySequence.java
1 /*\r
2 * Jalview - A Sequence Alignment Editor and Viewer\r
3 * Copyright (C) 2005 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(SequenceI s)\r
42     {\r
43         super(s.getName(), s.getSequence(), s.getStart(), s.getEnd());\r
44     }\r
45 \r
46     /**\r
47      * Creates a new BinarySequence object.\r
48      *\r
49      * @param name DOCUMENT ME!\r
50      * @param sequence DOCUMENT ME!\r
51      * @param start DOCUMENT ME!\r
52      * @param end DOCUMENT ME!\r
53      */\r
54     public BinarySequence(String name, String sequence, int start, int end)\r
55     {\r
56         super(name, sequence, start, end);\r
57     }\r
58 \r
59     /**\r
60      * DOCUMENT ME!\r
61      */\r
62     public void encode()\r
63     {\r
64         // Set all matrix to 0\r
65         dbinary = new double[getSequence().length() * 21];\r
66 \r
67         int nores = 21;\r
68 \r
69         for (int i = 0; i < dbinary.length; i++)\r
70         {\r
71             dbinary[i] = 0.0;\r
72         }\r
73 \r
74         for (int i = 0; i < getSequence().length(); i++)\r
75         {\r
76             int aanum = 20;\r
77 \r
78             try\r
79             {\r
80                 aanum = ((Integer) ResidueProperties.getAAHash().get(getSequence()\r
81                                                                          .substring(i,\r
82                             i + 1))).intValue();\r
83             }\r
84             catch (NullPointerException e)\r
85             {\r
86                 aanum = 20;\r
87             }\r
88 \r
89             if (aanum > 20)\r
90             {\r
91                 aanum = 20;\r
92             }\r
93 \r
94             dbinary[(i * nores) + aanum] = 1.0;\r
95         }\r
96     }\r
97 \r
98     /**\r
99      * DOCUMENT ME!\r
100      */\r
101     public void blosumEncode()\r
102     {\r
103         // Set all matrix to 0\r
104         dbinary = new double[getSequence().length() * 21];\r
105 \r
106         int nores = 21;\r
107 \r
108         //for (int i = 0; i < dbinary.length; i++) {\r
109         //  dbinary[i] = 0.0;\r
110         //}\r
111         for (int i = 0; i < getSequence().length(); i++)\r
112         {\r
113             int aanum = 20;\r
114 \r
115             try\r
116             {\r
117                 aanum = ((Integer) ResidueProperties.getAAHash().get(getSequence()\r
118                                                                          .substring(i,\r
119                             i + 1))).intValue();\r
120             }\r
121             catch (NullPointerException e)\r
122             {\r
123                 aanum = 20;\r
124             }\r
125 \r
126             if (aanum > 20)\r
127             {\r
128                 aanum = 20;\r
129             }\r
130 \r
131             // Do the blosum thing\r
132             for (int j = 0; j < 20; j++)\r
133             {\r
134                 dbinary[(i * nores) + j] = ResidueProperties.getBLOSUM62()[aanum][j];\r
135             }\r
136         }\r
137     }\r
138 \r
139     /**\r
140      * DOCUMENT ME!\r
141      *\r
142      * @return DOCUMENT ME!\r
143      */\r
144     public String toBinaryString()\r
145     {\r
146         String out = "";\r
147 \r
148         for (int i = 0; i < binary.length; i++)\r
149         {\r
150             out += (new Integer(binary[i])).toString();\r
151 \r
152             if (i < (binary.length - 1))\r
153             {\r
154                 out += " ";\r
155             }\r
156         }\r
157 \r
158         return out;\r
159     }\r
160 \r
161     /**\r
162      * DOCUMENT ME!\r
163      *\r
164      * @return DOCUMENT ME!\r
165      */\r
166     public double[] getDBinary()\r
167     {\r
168         return dbinary;\r
169     }\r
170 \r
171     /**\r
172      * DOCUMENT ME!\r
173      *\r
174      * @param rt DOCUMENT ME!\r
175      */\r
176     public static void printMemory(Runtime rt)\r
177     {\r
178         System.out.println("DEBUG: Free memory = " + rt.freeMemory()); // log.\r
179     }\r
180 }\r