Class |
Line # |
Actions |
|||
---|---|---|---|---|---|
BinarySequence | 33 | 36 | 22 | ||
BinarySequence.InvalidSequenceTypeException | 35 | 1 | 1 |
1 | /* | |
2 | * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$) | |
3 | * Copyright (C) $$Year-Rel$$ The Jalview Authors | |
4 | * | |
5 | * This file is part of Jalview. | |
6 | * | |
7 | * Jalview is free software: you can redistribute it and/or | |
8 | * modify it under the terms of the GNU General Public License | |
9 | * as published by the Free Software Foundation, either version 3 | |
10 | * of the License, or (at your option) any later version. | |
11 | * | |
12 | * Jalview is distributed in the hope that it will be useful, but | |
13 | * WITHOUT ANY WARRANTY; without even the implied warranty | |
14 | * of MERCHANTABILITY or FITNESS FOR A PARTICULAR | |
15 | * PURPOSE. See the GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with Jalview. If not, see <http://www.gnu.org/licenses/>. | |
19 | * The Jalview Authors are detailed in the 'AUTHORS' file. | |
20 | */ | |
21 | package jalview.datamodel; | |
22 | ||
23 | import jalview.analysis.scoremodels.ScoreMatrix; | |
24 | import jalview.schemes.ResidueProperties; | |
25 | ||
26 | /** | |
27 | * Encode a sequence as a numeric vector using either classic residue binary | |
28 | * encoding or convolved with residue substitution matrix. | |
29 | * | |
30 | * @author $author$ | |
31 | * @version $Revision$ | |
32 | */ | |
33 | public class BinarySequence extends Sequence | |
34 | { | |
35 | public class InvalidSequenceTypeException extends Exception | |
36 | { | |
37 | ||
38 | 0 | public InvalidSequenceTypeException(String string) |
39 | { | |
40 | 0 | super(string); |
41 | } | |
42 | ||
43 | } | |
44 | ||
45 | int[] binary; | |
46 | ||
47 | double[] dbinary; | |
48 | ||
49 | boolean isNa = false; | |
50 | ||
51 | /** | |
52 | * Creates a new BinarySequence object. | |
53 | * | |
54 | * @param s | |
55 | * DOCUMENT ME! | |
56 | */ | |
57 | 0 | public BinarySequence(String s, boolean isNa) |
58 | { | |
59 | 0 | super("", s, 0, s.length()); |
60 | 0 | this.isNa = isNa; |
61 | } | |
62 | ||
63 | /** | |
64 | * clear the dbinary matrix | |
65 | * | |
66 | * @return nores - dimension of sequence symbol encoding for this sequence | |
67 | */ | |
68 | 0 | private int initMatrixGetNoRes() |
69 | { | |
70 | 0 | int nores = (isNa) ? ResidueProperties.maxNucleotideIndex |
71 | : ResidueProperties.maxProteinIndex; | |
72 | ||
73 | 0 | dbinary = new double[getLength() * nores]; |
74 | ||
75 | 0 | return nores; |
76 | } | |
77 | ||
78 | 0 | private int[] getSymbolmatrix() |
79 | { | |
80 | 0 | return (isNa) ? ResidueProperties.nucleotideIndex |
81 | : ResidueProperties.aaIndex; | |
82 | } | |
83 | ||
84 | /** | |
85 | * DOCUMENT ME! | |
86 | */ | |
87 | 0 | public void encode() |
88 | { | |
89 | 0 | int nores = initMatrixGetNoRes(); |
90 | 0 | final int[] sindex = getSymbolmatrix(); |
91 | 0 | for (int i = 0; i < getLength(); i++) |
92 | { | |
93 | 0 | int aanum = nores - 1; |
94 | ||
95 | 0 | try |
96 | { | |
97 | 0 | aanum = sindex[getCharAt(i)]; |
98 | } catch (NullPointerException e) | |
99 | { | |
100 | 0 | aanum = nores - 1; |
101 | } | |
102 | ||
103 | 0 | if (aanum >= nores) |
104 | { | |
105 | 0 | aanum = nores - 1; |
106 | } | |
107 | ||
108 | 0 | dbinary[(i * nores) + aanum] = 1.0; |
109 | } | |
110 | } | |
111 | ||
112 | /** | |
113 | * ancode using substitution matrix given in matrix | |
114 | * | |
115 | * @param smtrx | |
116 | */ | |
117 | 0 | public void matrixEncode(final ScoreMatrix smtrx) |
118 | throws InvalidSequenceTypeException | |
119 | { | |
120 | 0 | if (isNa != smtrx.isDNA()) |
121 | { | |
122 | 0 | throw new InvalidSequenceTypeException( |
123 | "matrix " + smtrx.getClass().getCanonicalName() | |
124 | + " is not a valid matrix for " | |
125 | 0 | + (isNa ? "nucleotide" : "protein") + "sequences"); |
126 | } | |
127 | 0 | matrixEncode(smtrx.isDNA() ? ResidueProperties.nucleotideIndex |
128 | : ResidueProperties.aaIndex, smtrx.getMatrix()); | |
129 | } | |
130 | ||
131 | 0 | private void matrixEncode(final int[] aaIndex, final float[][] matrix) |
132 | { | |
133 | 0 | int nores = initMatrixGetNoRes(); |
134 | ||
135 | 0 | for (int i = 0, iSize = getLength(); i < iSize; i++) |
136 | { | |
137 | 0 | int aanum = nores - 1; |
138 | ||
139 | 0 | try |
140 | { | |
141 | 0 | aanum = aaIndex[getCharAt(i)]; |
142 | } catch (NullPointerException e) | |
143 | { | |
144 | 0 | aanum = nores - 1; |
145 | } | |
146 | ||
147 | 0 | if (aanum >= nores) |
148 | { | |
149 | 0 | aanum = nores - 1; |
150 | } | |
151 | ||
152 | // Do the blosum^H^H^H^H^H score matrix summation thing | |
153 | ||
154 | 0 | for (int j = 0; j < nores; j++) |
155 | { | |
156 | 0 | dbinary[(i * nores) + j] = matrix[aanum][j]; |
157 | } | |
158 | } | |
159 | } | |
160 | ||
161 | /** | |
162 | * DOCUMENT ME! | |
163 | * | |
164 | * @return DOCUMENT ME! | |
165 | */ | |
166 | 0 | public String toBinaryString() |
167 | { | |
168 | 0 | String out = ""; |
169 | ||
170 | 0 | for (int i = 0; i < binary.length; i++) |
171 | { | |
172 | 0 | out += (Integer.valueOf(binary[i])).toString(); |
173 | ||
174 | 0 | if (i < (binary.length - 1)) |
175 | { | |
176 | 0 | out += " "; |
177 | } | |
178 | } | |
179 | ||
180 | 0 | return out; |
181 | } | |
182 | ||
183 | /** | |
184 | * DOCUMENT ME! | |
185 | * | |
186 | * @return DOCUMENT ME! | |
187 | */ | |
188 | 0 | public double[] getDBinary() |
189 | { | |
190 | 0 | return dbinary; |
191 | } | |
192 | ||
193 | } |