Takes a string, not sequence
[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(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 = ((Integer) ResidueProperties.getAAHash().get(getSequence()\r
68                                                                          .substring(i,\r
69                             i + 1))).intValue();\r
70             }\r
71             catch (NullPointerException e)\r
72             {\r
73                 aanum = 20;\r
74             }\r
75 \r
76             if (aanum > 20)\r
77             {\r
78                 aanum = 20;\r
79             }\r
80 \r
81             dbinary[(i * nores) + aanum] = 1.0;\r
82         }\r
83     }\r
84 \r
85     /**\r
86      * DOCUMENT ME!\r
87      */\r
88     public void blosumEncode()\r
89     {\r
90         // Set all matrix to 0\r
91         dbinary = new double[getSequence().length() * 21];\r
92 \r
93         int nores = 21;\r
94 \r
95         //for (int i = 0; i < dbinary.length; i++) {\r
96         //  dbinary[i] = 0.0;\r
97         //}\r
98         for (int i = 0; i < getSequence().length(); i++)\r
99         {\r
100             int aanum = 20;\r
101 \r
102             try\r
103             {\r
104                 aanum = ((Integer) ResidueProperties.getAAHash().get(getSequence()\r
105                                                                          .substring(i,\r
106                             i + 1))).intValue();\r
107             }\r
108             catch (NullPointerException e)\r
109             {\r
110                 aanum = 20;\r
111             }\r
112 \r
113             if (aanum > 20)\r
114             {\r
115                 aanum = 20;\r
116             }\r
117 \r
118             // Do the blosum thing\r
119             for (int j = 0; j < 20; j++)\r
120             {\r
121                 dbinary[(i * nores) + j] = ResidueProperties.getBLOSUM62()[aanum][j];\r
122             }\r
123         }\r
124     }\r
125 \r
126     /**\r
127      * DOCUMENT ME!\r
128      *\r
129      * @return DOCUMENT ME!\r
130      */\r
131     public String toBinaryString()\r
132     {\r
133         String out = "";\r
134 \r
135         for (int i = 0; i < binary.length; i++)\r
136         {\r
137             out += (new Integer(binary[i])).toString();\r
138 \r
139             if (i < (binary.length - 1))\r
140             {\r
141                 out += " ";\r
142             }\r
143         }\r
144 \r
145         return out;\r
146     }\r
147 \r
148     /**\r
149      * DOCUMENT ME!\r
150      *\r
151      * @return DOCUMENT ME!\r
152      */\r
153     public double[] getDBinary()\r
154     {\r
155         return dbinary;\r
156     }\r
157 \r
158 }\r