a351b04f18bbd219eb7e08a0ec65929c53548b13
[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.analysis.PCA;\r
22 \r
23 import jalview.io.*;\r
24 \r
25 import jalview.jbgui.*;\r
26 \r
27 import jalview.schemes.*;\r
28 \r
29 import java.awt.*;\r
30 \r
31 \r
32 public class BinarySequence extends Sequence {\r
33     int[] binary;\r
34     double[] dbinary;\r
35 \r
36     public BinarySequence(SequenceI s) {\r
37         super(s.getName(), s.getSequence(), s.getStart(), s.getEnd());\r
38     }\r
39 \r
40     public BinarySequence(String name, String sequence, int start, int end) {\r
41         super(name, sequence, start, end);\r
42     }\r
43 \r
44     public void encode() {\r
45         // Set all matrix to 0\r
46         dbinary = new double[getSequence().length() * 21];\r
47 \r
48         int nores = 21;\r
49 \r
50         for (int i = 0; i < dbinary.length; i++) {\r
51             dbinary[i] = 0.0;\r
52         }\r
53 \r
54         for (int i = 0; i < getSequence().length(); i++) {\r
55             int aanum = 20;\r
56 \r
57             try {\r
58                 aanum = ((Integer) ResidueProperties.getAAHash().get(getSequence()\r
59                                                                          .substring(i,\r
60                             i + 1))).intValue();\r
61             } catch (NullPointerException e) {\r
62                 aanum = 20;\r
63             }\r
64 \r
65             if (aanum > 20) {\r
66                 aanum = 20;\r
67             }\r
68 \r
69             dbinary[(i * nores) + aanum] = 1.0;\r
70         }\r
71     }\r
72 \r
73     public void blosumEncode() {\r
74         // Set all matrix to 0\r
75         dbinary = new double[getSequence().length() * 21];\r
76 \r
77         int nores = 21;\r
78 \r
79         //for (int i = 0; i < dbinary.length; i++) {\r
80         //  dbinary[i] = 0.0;\r
81         //}\r
82         for (int i = 0; i < getSequence().length(); i++) {\r
83             int aanum = 20;\r
84 \r
85             try {\r
86                 aanum = ((Integer) ResidueProperties.getAAHash().get(getSequence()\r
87                                                                          .substring(i,\r
88                             i + 1))).intValue();\r
89             } catch (NullPointerException e) {\r
90                 aanum = 20;\r
91             }\r
92 \r
93             if (aanum > 20) {\r
94                 aanum = 20;\r
95             }\r
96 \r
97             // Do the blosum thing\r
98             for (int j = 0; j < 20; j++) {\r
99                 dbinary[(i * nores) + j] = ResidueProperties.getBLOSUM62()[aanum][j];\r
100             }\r
101         }\r
102     }\r
103 \r
104     public String toBinaryString() {\r
105         String out = "";\r
106 \r
107         for (int i = 0; i < binary.length; i++) {\r
108             out += (new Integer(binary[i])).toString();\r
109 \r
110             if (i < (binary.length - 1)) {\r
111                 out += " ";\r
112             }\r
113         }\r
114 \r
115         return out;\r
116     }\r
117 \r
118     public double[] getDBinary() {\r
119         return dbinary;\r
120     }\r
121 \r
122     public static void printMemory(Runtime rt) {\r
123         System.out.println("DEBUG: Free memory = " + rt.freeMemory()); // log.\r
124     }\r
125 }\r