updated to jalview 2.1 and begun ArchiveClient/VamsasClient/VamsasStore updates.
[jalview.git] / src / jalview / datamodel / BinarySequence.java
1 /*
2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2006 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
25 /**
26  * DOCUMENT ME!
27  *
28  * @author $author$
29  * @version $Revision$
30  */
31 public class BinarySequence extends Sequence
32 {
33     int[] binary;
34     double[] dbinary;
35
36     /**
37      * Creates a new BinarySequence object.
38      *
39      * @param s 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 = ((Integer) ResidueProperties.getAAHash().get(getSequence()
68                                                                          .substring(i,
69                             i + 1))).intValue();
70             }
71             catch (NullPointerException e)
72             {
73                 aanum = 20;
74             }
75
76             if (aanum > 20)
77             {
78                 aanum = 20;
79             }
80
81             dbinary[(i * nores) + aanum] = 1.0;
82         }
83     }
84
85     /**
86      * DOCUMENT ME!
87      */
88     public void blosumEncode()
89     {
90         // Set all matrix to 0
91         dbinary = new double[getSequence().length() * 21];
92
93         int nores = 21;
94
95         //for (int i = 0; i < dbinary.length; i++) {
96         //  dbinary[i] = 0.0;
97         //}
98         for (int i = 0; i < getSequence().length(); i++)
99         {
100             int aanum = 20;
101
102             try
103             {
104                 aanum = ((Integer) ResidueProperties.getAAHash().get(getSequence()
105                                                                          .substring(i,
106                             i + 1))).intValue();
107             }
108             catch (NullPointerException e)
109             {
110                 aanum = 20;
111             }
112
113             if (aanum > 20)
114             {
115                 aanum = 20;
116             }
117
118             // Do the blosum thing
119             for (int j = 0; j < 20; j++)
120             {
121                 dbinary[(i * nores) + j] = ResidueProperties.getBLOSUM62()[aanum][j];
122             }
123         }
124     }
125
126     /**
127      * DOCUMENT ME!
128      *
129      * @return DOCUMENT ME!
130      */
131     public String toBinaryString()
132     {
133         String out = "";
134
135         for (int i = 0; i < binary.length; i++)
136         {
137             out += (new Integer(binary[i])).toString();
138
139             if (i < (binary.length - 1))
140             {
141                 out += " ";
142             }
143         }
144
145         return out;
146     }
147
148     /**
149      * DOCUMENT ME!
150      *
151      * @return DOCUMENT ME!
152      */
153     public double[] getDBinary()
154     {
155         return dbinary;
156     }
157
158 }