Formatting
[jalview.git] / src / jalview / datamodel / BinarySequence.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer
3  * Copyright (C) 2007 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  * DOCUMENT ME!
25  *
26  * @author $author$
27  * @version $Revision$
28  */
29 public class BinarySequence
30     extends Sequence
31 {
32   int[] binary;
33   double[] dbinary;
34
35   /**
36    * Creates a new BinarySequence object.
37    *
38    * @param s DOCUMENT ME!
39    */
40   public BinarySequence(String s)
41   {
42     super("", s, 0, s.length());
43   }
44
45   /**
46    * DOCUMENT ME!
47    */
48   public void encode()
49   {
50     // Set all matrix to 0
51     dbinary = new double[getSequence().length * 21];
52
53     int nores = 21;
54
55     for (int i = 0; i < dbinary.length; i++)
56     {
57       dbinary[i] = 0.0;
58     }
59
60     for (int i = 0; i < getSequence().length; i++)
61     {
62       int aanum = 20;
63
64       try
65       {
66         aanum = ResidueProperties.aaIndex[getCharAt(i)];
67       }
68       catch (NullPointerException e)
69       {
70         aanum = 20;
71       }
72
73       if (aanum > 20)
74       {
75         aanum = 20;
76       }
77
78       dbinary[ (i * nores) + aanum] = 1.0;
79     }
80   }
81
82   /**
83    * ancode using substitution matrix given in matrix
84    * @param matrix
85    */
86   public void matrixEncode(ScoreMatrix matrix)
87   {
88     matrixEncode(matrix.isDNA() ? ResidueProperties.nucleotideIndex :
89                  ResidueProperties.aaIndex, matrix.getMatrix());
90   }
91
92   /**
93    * DOCUMENT ME!
94    */
95   public void blosumEncode()
96   {
97     matrixEncode(ResidueProperties.aaIndex, ResidueProperties.getBLOSUM62());
98   }
99
100   private void matrixEncode(int[] aaIndex, int[][] matrix)
101   {
102     // Set all matrix to 0
103     dbinary = new double[getSequence().length * 21];
104
105     int nores = 21;
106
107     //for (int i = 0; i < dbinary.length; i++) {
108     //  dbinary[i] = 0.0;
109     //}
110     for (int i = 0; i < getSequence().length; i++)
111     {
112       int aanum = 20;
113
114       try
115       {
116         aanum = aaIndex[getCharAt(i)];
117       }
118       catch (NullPointerException e)
119       {
120         aanum = 20;
121       }
122
123       if (aanum > 20)
124       {
125         aanum = 20;
126       }
127
128       // Do the blosum thing
129
130       for (int j = 0; j < 20; j++)
131       {
132         dbinary[ (i * nores) + j] = matrix[aanum][j];
133       }
134     }
135   }
136
137   /**
138    * DOCUMENT ME!
139    *
140    * @return DOCUMENT ME!
141    */
142   public String toBinaryString()
143   {
144     String out = "";
145
146     for (int i = 0; i < binary.length; i++)
147     {
148       out += (new Integer(binary[i])).toString();
149
150       if (i < (binary.length - 1))
151       {
152         out += " ";
153       }
154     }
155
156     return out;
157   }
158
159   /**
160    * DOCUMENT ME!
161    *
162    * @return DOCUMENT ME!
163    */
164   public double[] getDBinary()
165   {
166     return dbinary;
167   }
168
169 }