1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
package jalview.datamodel; |
22 |
|
|
23 |
|
import java.util.ArrayList; |
24 |
|
import java.util.List; |
25 |
|
import java.util.StringTokenizer; |
26 |
|
|
27 |
|
import jalview.bin.Console; |
28 |
|
|
|
|
| 0% |
Uncovered Elements: 136 (136) |
Complexity: 39 |
Complexity Density: 0.51 |
|
29 |
|
public abstract class ContactMatrix extends GroupSetHolder |
30 |
|
implements ContactMatrixI |
31 |
|
{ |
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
boolean symmetric = true; |
36 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
37 |
0 |
public ContactMatrix(boolean symmetric)... |
38 |
|
{ |
39 |
0 |
this.symmetric = symmetric; |
40 |
|
} |
41 |
|
|
42 |
|
List<List<Float>> contacts = null; |
43 |
|
|
44 |
|
int width = 0, numcontacts = 0; |
45 |
|
|
46 |
|
float min = 0f, max = 0f; |
47 |
|
|
|
|
| 0% |
Uncovered Elements: 29 (29) |
Complexity: 8 |
Complexity Density: 0.47 |
|
48 |
0 |
public void addContact(int left, int right, float strength)... |
49 |
|
{ |
50 |
0 |
if (left < 0 || right < 0) |
51 |
|
{ |
52 |
0 |
throw new Error(new RuntimeException( |
53 |
|
"Cannot have negative indices for contact left=" + left |
54 |
|
+ " right=" + right + " strength=" + strength)); |
55 |
|
} |
56 |
0 |
if (symmetric) |
57 |
|
{ |
58 |
0 |
if (left > right) |
59 |
|
{ |
60 |
|
|
61 |
0 |
int r = right; |
62 |
0 |
right = left; |
63 |
0 |
left = r; |
64 |
|
} |
65 |
|
} |
66 |
0 |
if (contacts == null) |
67 |
|
{ |
68 |
|
|
69 |
0 |
contacts = new ArrayList<List<Float>>(); |
70 |
|
} |
71 |
0 |
List<Float> clist = contacts.get(left); |
72 |
0 |
if (clist == null) |
73 |
|
{ |
74 |
0 |
clist = new ArrayList<Float>(); |
75 |
0 |
contacts.set(left, clist); |
76 |
|
} |
77 |
0 |
Float last = clist.set(right, strength); |
78 |
|
|
79 |
0 |
checkBounds(strength); |
80 |
0 |
if (last == null) |
81 |
|
{ |
82 |
0 |
numcontacts++; |
83 |
|
} |
84 |
|
} |
85 |
|
|
|
|
| 0% |
Uncovered Elements: 8 (8) |
Complexity: 3 |
Complexity Density: 0.75 |
|
86 |
0 |
private void checkBounds(float strength)... |
87 |
|
{ |
88 |
0 |
if (min > strength) |
89 |
|
{ |
90 |
0 |
min = strength; |
91 |
|
} |
92 |
0 |
if (max < strength) |
93 |
|
{ |
94 |
0 |
max = strength; |
95 |
|
} |
96 |
|
} |
97 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 3 |
Complexity Density: 1 |
|
98 |
0 |
@Override... |
99 |
|
public ContactListI getContactList(final int column) |
100 |
|
{ |
101 |
0 |
if (column < 0 || column >= width) |
102 |
|
{ |
103 |
0 |
return null; |
104 |
|
} |
105 |
|
|
106 |
0 |
return new ContactListImpl(new ContactListProviderI() |
107 |
|
{ |
108 |
|
int p = column; |
109 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
110 |
0 |
@Override... |
111 |
|
public int getPosition() |
112 |
|
{ |
113 |
0 |
return p; |
114 |
|
} |
115 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
116 |
0 |
@Override... |
117 |
|
public int getContactHeight() |
118 |
|
{ |
119 |
0 |
return width; |
120 |
|
|
121 |
|
} |
122 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
123 |
0 |
@Override... |
124 |
|
public double getContactAt(int column) |
125 |
|
{ |
126 |
0 |
Float cl = getFloatElementAt(column, p); |
127 |
0 |
if (cl == null) |
128 |
|
{ |
129 |
|
|
130 |
0 |
return Double.NaN; |
131 |
|
} |
132 |
0 |
return cl.doubleValue(); |
133 |
|
} |
134 |
|
}); |
135 |
|
} |
136 |
|
|
|
|
| 0% |
Uncovered Elements: 15 (15) |
Complexity: 3 |
Complexity Density: 0.27 |
|
137 |
0 |
private Float getFloatElementAt(int column, int p)... |
138 |
|
{ |
139 |
|
|
140 |
0 |
List<Float> clist; |
141 |
0 |
Float cl = null; |
142 |
0 |
if (symmetric) |
143 |
|
{ |
144 |
0 |
if (p < column) |
145 |
|
{ |
146 |
0 |
clist = contacts.get(p); |
147 |
0 |
cl = clist.get(column); |
148 |
|
} |
149 |
|
else |
150 |
|
{ |
151 |
0 |
clist = contacts.get(column); |
152 |
0 |
cl = clist.get(p); |
153 |
|
} |
154 |
|
} |
155 |
|
else |
156 |
|
{ |
157 |
0 |
clist = contacts.get(p); |
158 |
0 |
cl = clist.get(column); |
159 |
|
} |
160 |
0 |
return cl; |
161 |
|
} |
162 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
163 |
0 |
@Override... |
164 |
|
public double getElementAt(int column, int row) |
165 |
|
{ |
166 |
0 |
Float cl = getFloatElementAt(column, row); |
167 |
0 |
if (cl != null) |
168 |
|
{ |
169 |
0 |
return cl; |
170 |
|
} |
171 |
0 |
throw (new RuntimeException("Out of Bounds " + column + "," + row)); |
172 |
|
} |
173 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
174 |
0 |
@Override... |
175 |
|
public float getMin() |
176 |
|
{ |
177 |
0 |
return min; |
178 |
|
} |
179 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
180 |
0 |
@Override... |
181 |
|
public float getMax() |
182 |
|
{ |
183 |
0 |
return max; |
184 |
|
} |
185 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
186 |
0 |
@Override... |
187 |
|
public String getAnnotLabel() |
188 |
|
{ |
189 |
0 |
return "Contact Matrix"; |
190 |
|
} |
191 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
192 |
0 |
@Override... |
193 |
|
public String getAnnotDescr() |
194 |
|
{ |
195 |
0 |
return "Contact Matrix"; |
196 |
|
} |
197 |
|
|
|
|
| 0% |
Uncovered Elements: 23 (23) |
Complexity: 6 |
Complexity Density: 0.46 |
|
198 |
0 |
public static String contactToFloatString(ContactMatrixI cm)... |
199 |
|
{ |
200 |
0 |
StringBuilder sb = new StringBuilder(); |
201 |
0 |
for (int c = 0; c < cm.getWidth(); c++) |
202 |
|
{ |
203 |
0 |
ContactListI cl = cm.getContactList(c); |
204 |
0 |
long lastsb = -1; |
205 |
0 |
if (cl != null) |
206 |
|
{ |
207 |
0 |
for (int h = 0; h <= cl.getContactHeight(); h++) |
208 |
|
{ |
209 |
0 |
if (sb.length() > 0) |
210 |
|
{ |
211 |
0 |
if (sb.length() - lastsb > 320) |
212 |
|
{ |
213 |
|
|
214 |
0 |
sb.append('\n'); |
215 |
0 |
lastsb = sb.length(); |
216 |
|
} |
217 |
|
else |
218 |
|
{ |
219 |
0 |
sb.append('\t'); |
220 |
|
} |
221 |
|
} |
222 |
0 |
sb.append(cl.getContactAt(h)); |
223 |
|
} |
224 |
|
} |
225 |
|
} |
226 |
0 |
return sb.toString(); |
227 |
|
} |
228 |
|
|
|
|
| 0% |
Uncovered Elements: 22 (22) |
Complexity: 5 |
Complexity Density: 0.36 |
|
229 |
0 |
public static float[][] fromFloatStringToContacts(String values, int cols,... |
230 |
|
int rows) |
231 |
|
{ |
232 |
0 |
float[][] vals = new float[cols][rows]; |
233 |
0 |
StringTokenizer tabsep = new StringTokenizer(values, "" + '\t' + '\n'); |
234 |
0 |
int c = 0, r = 0; |
235 |
0 |
while (tabsep.hasMoreTokens()) |
236 |
|
{ |
237 |
0 |
double elem = Double.valueOf(tabsep.nextToken()); |
238 |
0 |
vals[c][r++] = (float) elem; |
239 |
0 |
if (r >= vals[c].length) |
240 |
|
{ |
241 |
0 |
r = 0; |
242 |
0 |
c++; |
243 |
|
} |
244 |
0 |
if (c >= vals.length) |
245 |
|
{ |
246 |
0 |
break; |
247 |
|
} |
248 |
|
} |
249 |
0 |
if (tabsep.hasMoreElements()) |
250 |
|
{ |
251 |
0 |
Console.warn( |
252 |
|
"Ignoring additional elements for Float string to contact matrix parsing."); |
253 |
|
} |
254 |
0 |
return vals; |
255 |
|
} |
256 |
|
} |