| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
|
| 21 |
|
package jalview.analysis; |
| 22 |
|
|
| 23 |
|
import java.util.ArrayList; |
| 24 |
|
import java.util.BitSet; |
| 25 |
|
import java.util.List; |
| 26 |
|
import java.util.Vector; |
| 27 |
|
|
| 28 |
|
import jalview.datamodel.AlignmentAnnotation; |
| 29 |
|
import jalview.datamodel.BinaryNode; |
| 30 |
|
import jalview.datamodel.ContactListI; |
| 31 |
|
import jalview.datamodel.ContactMatrixI; |
| 32 |
|
import jalview.math.Matrix; |
| 33 |
|
import jalview.viewmodel.AlignmentViewport; |
| 34 |
|
|
| 35 |
|
|
| 36 |
|
|
| 37 |
|
|
| 38 |
|
|
| |
|
| 94.5% |
Uncovered Elements: 10 (182) |
Complexity: 44 |
Complexity Density: 0.39 |
|
| 39 |
|
public class AverageDistanceEngine extends TreeEngine |
| 40 |
|
{ |
| 41 |
|
ContactMatrixI cm; |
| 42 |
|
|
| 43 |
|
AlignmentViewport av; |
| 44 |
|
|
| 45 |
|
AlignmentAnnotation aa; |
| 46 |
|
|
| 47 |
|
|
| 48 |
|
|
| 49 |
|
|
| 50 |
|
|
| 51 |
|
|
| 52 |
|
|
| 53 |
|
int mode = 1; |
| 54 |
|
|
| 55 |
|
|
| 56 |
|
|
| 57 |
|
|
| 58 |
|
|
| 59 |
|
@param |
| 60 |
|
@param |
| 61 |
|
|
| 62 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
| 63 |
3 |
public AverageDistanceEngine(AlignmentViewport av, AlignmentAnnotation aa,... |
| 64 |
|
ContactMatrixI cm, boolean cosineOrDifference) |
| 65 |
|
{ |
| 66 |
3 |
this.av = av; |
| 67 |
3 |
this.aa = aa; |
| 68 |
3 |
this.cm = cm; |
| 69 |
3 |
mode = (cosineOrDifference) ? 1 : 0; |
| 70 |
3 |
calculate(cm); |
| 71 |
|
|
| 72 |
|
} |
| 73 |
|
|
| |
|
| 93.9% |
Uncovered Elements: 4 (66) |
Complexity: 13 |
Complexity Density: 0.3 |
|
| 74 |
3 |
public void calculate(ContactMatrixI cm)... |
| 75 |
|
{ |
| 76 |
3 |
this.cm = cm; |
| 77 |
3 |
node = new Vector<BinaryNode>(); |
| 78 |
3 |
clusters = new Vector<BitSet>(); |
| 79 |
3 |
distances = new Matrix(new double[cm.getWidth()][cm.getWidth()]); |
| 80 |
3 |
noseqs = cm.getWidth(); |
| 81 |
3 |
done = new BitSet(); |
| 82 |
3 |
double moduli[] = new double[cm.getWidth()]; |
| 83 |
3 |
double max; |
| 84 |
3 |
if (mode == 0) |
| 85 |
|
{ |
| 86 |
1 |
max = 1; |
| 87 |
|
} |
| 88 |
|
else |
| 89 |
|
{ |
| 90 |
2 |
max = cm.getMax() * cm.getMax(); |
| 91 |
|
} |
| 92 |
|
|
| 93 |
78 |
for (int i = 0; i < cm.getWidth(); i++) |
| 94 |
|
{ |
| 95 |
|
|
| 96 |
75 |
BinaryNode cnode = new BinaryNode(); |
| 97 |
75 |
cnode.setElement(Integer.valueOf(i)); |
| 98 |
75 |
cnode.setName("c" + i); |
| 99 |
75 |
node.addElement(cnode); |
| 100 |
75 |
BitSet bs = new BitSet(); |
| 101 |
75 |
bs.set(i); |
| 102 |
75 |
clusters.addElement(bs); |
| 103 |
|
|
| 104 |
|
|
| 105 |
75 |
ContactListI ith = cm.getContactList(i); |
| 106 |
75 |
distances.setValue(i, i, 0); |
| 107 |
75 |
if (ith == null) |
| 108 |
|
{ |
| 109 |
0 |
continue; |
| 110 |
|
} |
| 111 |
1851 |
for (int j = 0; j < i; j++) |
| 112 |
|
{ |
| 113 |
1776 |
ContactListI jth = cm.getContactList(j); |
| 114 |
1776 |
if (jth == null) |
| 115 |
|
{ |
| 116 |
0 |
break; |
| 117 |
|
} |
| 118 |
1776 |
double prd = 0; |
| 119 |
103380 |
for (int indx = 0; indx < cm.getHeight(); indx++) |
| 120 |
|
{ |
| 121 |
101604 |
if (mode == 0) |
| 122 |
|
{ |
| 123 |
100949 |
if (j == 0) |
| 124 |
|
{ |
| 125 |
3422 |
moduli[i] += ith.getContactAt(indx) * ith.getContactAt(indx); |
| 126 |
|
} |
| 127 |
100949 |
prd += ith.getContactAt(indx) * jth.getContactAt(indx); |
| 128 |
|
} |
| 129 |
|
else |
| 130 |
|
{ |
| 131 |
655 |
prd += Math |
| 132 |
|
.abs(ith.getContactAt(indx) - jth.getContactAt(indx)); |
| 133 |
|
} |
| 134 |
|
} |
| 135 |
1776 |
if (mode == 0) |
| 136 |
|
{ |
| 137 |
1711 |
if (j == 0) |
| 138 |
|
{ |
| 139 |
58 |
moduli[i] = Math.sqrt(moduli[i]); |
| 140 |
|
} |
| 141 |
1711 |
prd = (moduli[i] != 0 && moduli[j] != 0) |
| 142 |
|
? prd / (moduli[i] * moduli[j]) |
| 143 |
|
: 0; |
| 144 |
1711 |
prd = 1 - prd; |
| 145 |
|
} |
| 146 |
|
else |
| 147 |
|
{ |
| 148 |
65 |
prd /= cm.getHeight(); |
| 149 |
|
} |
| 150 |
1776 |
distances.setValue(i, j, prd); |
| 151 |
1776 |
distances.setValue(j, i, prd); |
| 152 |
|
} |
| 153 |
|
} |
| 154 |
|
|
| 155 |
3 |
noClus = clusters.size(); |
| 156 |
3 |
cluster(); |
| 157 |
|
} |
| 158 |
|
|
| 159 |
|
|
| 160 |
|
|
| 161 |
|
|
| 162 |
|
|
| 163 |
|
|
| 164 |
|
|
| 165 |
|
@param |
| 166 |
|
@param |
| 167 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (16) |
Complexity: 5 |
Complexity Density: 0.5 |
|
| 168 |
72 |
@Override... |
| 169 |
|
protected void findClusterDistance(int i, int j) |
| 170 |
|
{ |
| 171 |
72 |
int noi = clusters.elementAt(i).cardinality(); |
| 172 |
72 |
int noj = clusters.elementAt(j).cardinality(); |
| 173 |
|
|
| 174 |
|
|
| 175 |
72 |
double[] newdist = new double[noseqs]; |
| 176 |
|
|
| 177 |
3624 |
for (int l = 0; l < noseqs; l++) |
| 178 |
|
{ |
| 179 |
3552 |
if ((l != i) && (l != j)) |
| 180 |
|
{ |
| 181 |
3408 |
newdist[l] = ((distances.getValue(i, l) * noi) |
| 182 |
|
+ (distances.getValue(j, l) * noj)) / (noi + noj); |
| 183 |
|
} |
| 184 |
|
else |
| 185 |
|
{ |
| 186 |
144 |
newdist[l] = 0; |
| 187 |
|
} |
| 188 |
|
} |
| 189 |
|
|
| 190 |
3624 |
for (int ii = 0; ii < noseqs; ii++) |
| 191 |
|
{ |
| 192 |
3552 |
distances.setValue(i, ii, newdist[ii]); |
| 193 |
3552 |
distances.setValue(ii, i, newdist[ii]); |
| 194 |
|
} |
| 195 |
|
} |
| 196 |
|
|
| 197 |
|
|
| 198 |
|
@inheritDoc |
| 199 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (17) |
Complexity: 6 |
Complexity Density: 0.67 |
|
| 200 |
69 |
@Override... |
| 201 |
|
protected double findMinDistance() |
| 202 |
|
{ |
| 203 |
69 |
double min = Double.MAX_VALUE; |
| 204 |
|
|
| 205 |
3477 |
for (int i = 0; i < (noseqs - 1); i++) |
| 206 |
|
{ |
| 207 |
101460 |
for (int j = i + 1; j < noseqs; j++) |
| 208 |
|
{ |
| 209 |
98052 |
if (!done.get(i) && !done.get(j)) |
| 210 |
|
{ |
| 211 |
34457 |
if (distances.getValue(i, j) < min) |
| 212 |
|
{ |
| 213 |
355 |
mini = i; |
| 214 |
355 |
minj = j; |
| 215 |
|
|
| 216 |
355 |
min = distances.getValue(i, j); |
| 217 |
|
} |
| 218 |
|
} |
| 219 |
|
} |
| 220 |
|
} |
| 221 |
69 |
return min; |
| 222 |
|
} |
| 223 |
|
|
| 224 |
|
|
| 225 |
|
@inheritDoc |
| 226 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (16) |
Complexity: 3 |
Complexity Density: 0.25 |
|
| 227 |
72 |
@Override... |
| 228 |
|
protected void findNewDistances(BinaryNode nodei, BinaryNode nodej, |
| 229 |
|
double dist) |
| 230 |
|
{ |
| 231 |
72 |
double ih = 0; |
| 232 |
72 |
double jh = 0; |
| 233 |
|
|
| 234 |
72 |
BinaryNode sni = nodei; |
| 235 |
72 |
BinaryNode snj = nodej; |
| 236 |
|
|
| 237 |
218 |
while (sni != null) |
| 238 |
|
{ |
| 239 |
146 |
ih = ih + sni.dist; |
| 240 |
146 |
sni = (BinaryNode) sni.left(); |
| 241 |
|
} |
| 242 |
|
|
| 243 |
203 |
while (snj != null) |
| 244 |
|
{ |
| 245 |
131 |
jh = jh + snj.dist; |
| 246 |
131 |
snj = (BinaryNode) snj.left(); |
| 247 |
|
} |
| 248 |
|
|
| 249 |
72 |
nodei.dist = ((dist / 2) - ih); |
| 250 |
72 |
nodej.dist = ((dist / 2) - jh); |
| 251 |
|
} |
| 252 |
|
|
| 253 |
|
|
| 254 |
|
|
| 255 |
|
|
| 256 |
|
|
| 257 |
|
|
| 258 |
|
|
| 259 |
|
|
| 260 |
|
|
| 261 |
|
|
| 262 |
|
|
| 263 |
|
|
| 264 |
|
|
| 265 |
|
|
| 266 |
|
@param |
| 267 |
|
@see |
| 268 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
| 269 |
3 |
public List<BinaryNode> groupNodes(float threshold)... |
| 270 |
|
{ |
| 271 |
3 |
List<BinaryNode> groups = new ArrayList<BinaryNode>(); |
| 272 |
3 |
_groupNodes(groups, getTopNode(), threshold); |
| 273 |
3 |
return groups; |
| 274 |
|
} |
| 275 |
|
|
| |
|
| 80% |
Uncovered Elements: 2 (10) |
Complexity: 3 |
Complexity Density: 0.5 |
|
| 276 |
11 |
protected void _groupNodes(List<BinaryNode> groups, BinaryNode nd,... |
| 277 |
|
float threshold) |
| 278 |
|
{ |
| 279 |
11 |
if (nd == null) |
| 280 |
|
{ |
| 281 |
0 |
return; |
| 282 |
|
} |
| 283 |
|
|
| 284 |
11 |
if ((nd.height / maxheight) > threshold) |
| 285 |
|
{ |
| 286 |
7 |
groups.add(nd); |
| 287 |
|
} |
| 288 |
|
else |
| 289 |
|
{ |
| 290 |
4 |
_groupNodes(groups, nd.left(), threshold); |
| 291 |
4 |
_groupNodes(groups, nd.right(), threshold); |
| 292 |
|
} |
| 293 |
|
} |
| 294 |
|
|
| 295 |
|
|
| 296 |
|
|
| 297 |
|
|
| 298 |
|
@param |
| 299 |
|
|
| 300 |
|
|
| 301 |
|
@return |
| 302 |
|
|
| |
|
| 90.9% |
Uncovered Elements: 2 (22) |
Complexity: 6 |
Complexity Density: 0.43 |
|
| 303 |
294 |
public double findHeight(BinaryNode nd)... |
| 304 |
|
{ |
| 305 |
294 |
if (nd == null) |
| 306 |
|
{ |
| 307 |
0 |
return maxheight; |
| 308 |
|
} |
| 309 |
|
|
| 310 |
294 |
if ((nd.left() == null) && (nd.right() == null)) |
| 311 |
|
{ |
| 312 |
150 |
nd.height = ((BinaryNode) nd.parent()).height + nd.dist; |
| 313 |
|
|
| 314 |
150 |
if (nd.height > maxheight) |
| 315 |
|
{ |
| 316 |
6 |
return nd.height; |
| 317 |
|
} |
| 318 |
|
else |
| 319 |
|
{ |
| 320 |
144 |
return maxheight; |
| 321 |
|
} |
| 322 |
|
} |
| 323 |
|
else |
| 324 |
|
{ |
| 325 |
144 |
if (nd.parent() != null) |
| 326 |
|
{ |
| 327 |
138 |
nd.height = ((BinaryNode) nd.parent()).height + nd.dist; |
| 328 |
|
} |
| 329 |
|
else |
| 330 |
|
{ |
| 331 |
6 |
maxheight = 0; |
| 332 |
6 |
nd.height = (float) 0.0; |
| 333 |
|
} |
| 334 |
|
|
| 335 |
144 |
maxheight = findHeight((BinaryNode) (nd.left())); |
| 336 |
144 |
maxheight = findHeight((BinaryNode) (nd.right())); |
| 337 |
|
} |
| 338 |
|
|
| 339 |
144 |
return maxheight; |
| 340 |
|
} |
| 341 |
|
|
| 342 |
|
|
| 343 |
|
|
| 344 |
|
|
| 345 |
|
@param |
| 346 |
|
|
| 347 |
|
|
| 348 |
|
@return |
| 349 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
| 350 |
7 |
public Vector<BinaryNode> findLeaves(BinaryNode top2)... |
| 351 |
|
{ |
| 352 |
7 |
Vector<BinaryNode> leaves = new Vector<BinaryNode>(); |
| 353 |
7 |
findLeaves(top2, leaves); |
| 354 |
7 |
return leaves; |
| 355 |
|
} |
| 356 |
|
|
| 357 |
|
|
| 358 |
|
|
| 359 |
|
|
| 360 |
|
@param |
| 361 |
|
|
| 362 |
|
@param |
| 363 |
|
|
| 364 |
|
|
| 365 |
|
@return |
| 366 |
|
|
| |
|
| 83.3% |
Uncovered Elements: 2 (12) |
Complexity: 4 |
Complexity Density: 0.5 |
|
| 367 |
143 |
Vector<BinaryNode> findLeaves(BinaryNode nd, Vector<BinaryNode> leaves)... |
| 368 |
|
{ |
| 369 |
143 |
if (nd == null) |
| 370 |
|
{ |
| 371 |
0 |
return leaves; |
| 372 |
|
} |
| 373 |
|
|
| 374 |
143 |
if ((nd.left() == null) && (nd.right() == null)) |
| 375 |
|
|
| 376 |
|
{ |
| 377 |
75 |
leaves.addElement(nd); |
| 378 |
|
|
| 379 |
75 |
return leaves; |
| 380 |
|
} |
| 381 |
|
else |
| 382 |
|
{ |
| 383 |
|
|
| 384 |
|
|
| 385 |
|
|
| 386 |
|
|
| 387 |
68 |
findLeaves(nd.left(), leaves); |
| 388 |
68 |
findLeaves(nd.right(), leaves); |
| 389 |
|
} |
| 390 |
|
|
| 391 |
68 |
return leaves; |
| 392 |
|
} |
| 393 |
|
|
| 394 |
|
} |