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