File: test_Cluster.py

package info (click to toggle)
python-biopython 1.45-3
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 18,192 kB
  • ctags: 12,310
  • sloc: python: 83,505; xml: 13,834; ansic: 7,015; cpp: 1,855; sql: 1,144; makefile: 179
file content (517 lines) | stat: -rw-r--r-- 18,373 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
from Numeric import *

def print_row(data):
  print "[",
  for number in data[:-1]: print "%7.3f," % number,
  print "%7.3f]" % data[-1]

def print_matrix(data, mask):
  n,m = shape(data)
  for i in range(n):
    print "[",
    for j in range(m):
      if mask[i,j]: print "%7.3f" % data[i,j],
      else: print "   X   ",
    print "]"

def test_mean_median(module):
  if module=='Bio.Cluster':
    from Bio.Cluster import mean, median
  elif module=='Pycluster':
    from Pycluster import mean, median
  else:
    raise ValueError('Unknown module name: ' + module)
  print "test_mean_median:"
  data1 = array([ 34.3, 3, 2 ])
  data2 = [ 5, 10 ,15, 20]
  data3 = [ 1, 2, 3, 5, 7, 11, 13, 17]
  data4 = [ 100, 19, 3, 1.5, 1.4, 1, 1, 1]

  for data in [data1, data2, data3, data4]:
    print "data =",
    print_row(data)
    print "mean is %7.3f; median is %7.3f" % (mean(data), median(data))
  print

def test_matrix_parse(module):
  if module=='Bio.Cluster':
    from Bio.Cluster import treecluster
  elif module=='Pycluster':
    from Pycluster import treecluster
  else:
    raise ValueError('Unknown module name: ' + module)
  print "test_matrix_parse:"
  # Normal matrix, no errors
  data1 = array([[ 1.1, 1.2 ],
                 [ 1.4, 1.3 ],
                 [ 1.1, 1.5 ],
                 [ 2.0, 1.5 ],
                 [ 1.7, 1.9 ],
                 [ 1.7, 1.9 ],
                 [ 5.7, 5.9 ],
                 [ 5.7, 5.9 ],
                 [ 3.1, 3.3 ],
                 [ 5.4, 5.3 ],
                 [ 5.1, 5.5 ],
                 [ 5.0, 5.5 ],
                 [ 5.1, 5.2 ]])

  # Another normal matrix, no errors; written as a list
  data2 =  [[  1.1, 2.2, 3.3, 4.4, 5.5 ], 
            [  3.1, 3.2, 1.3, 2.4, 1.5 ], 
            [  4.1, 2.2, 0.3, 5.4, 0.5 ], 
            [ 12.1, 2.0, 0.0, 5.0, 0.0 ]]

  # Ragged matrix
  data3 =  [[ 91.1, 92.2, 93.3, 94.4, 95.5], 
            [ 93.1, 93.2, 91.3, 92.4 ], 
            [ 94.1, 92.2, 90.3 ], 
            [ 12.1, 92.0, 90.0, 95.0, 90.0 ]]

  # Matrix with bad cells
  data4 =  [ [ 7.1, 7.2, 7.3, 7.4, 7.5, ],
             [ 7.1, 7.2, 7.3, 7.4, 'snoopy' ], 
             [ 7.1, 7.2, 7.3, None, None]] 

  # Matrix with a bad row
  data5 =  [ [ 23.1, 23.2, 23.3, 23.4, 23.5], 
             None,
             [ 23.1, 23.0, 23.0, 23.0, 23.0]]

  # Various references that don't point to matrices at all
  data6 = "snoopy"
  data7 = {'a': [[2.3,1.2],[3.3,5.6]]}
  data8 = []
  data9 = [None]
  data10 = [[None]]

  try:
    result = treecluster(data1)
    print "Read data1 (correct)"
  except: "Error: treecluster failed to accept matrix data1"
  try:
    result = treecluster(data2)
    print "Read data2 (correct)"
  except: "Error: treecluster failed to accept matrix data2"
  try:
    result = treecluster(data3)
    print "Error: treecluster incorrectly accepted data3"
  except: print "Refused incorrect matrix data3"
  try:
    result = treecluster(data4)
    print "Error: treecluster incorrectly accepted data4"
  except: print "Refused incorrect matrix data4"
  try:
    result = treecluster(data5)
    print "Error: treecluster incorrectly accepted data5"
  except: print "Refused incorrect matrix data5"
  try:
    result = treecluster(data6)
    print "Error: treecluster incorrectly accepted data6"
  except: print "Refused incorrect matrix data6"
  try:
    result = treecluster(data7)
    print "Error: treecluster incorrectly accepted data7"
  except: print "Refused incorrect matrix data7"
  try:
    result = treecluster(data8)
    print "Error: treecluster incorrectly accepted data8"
  except: print "Refused incorrect matrix data8"
  try:
    result = treecluster(data9)
    print "Error: treecluster incorrectly accepted data9"
  except: print "Refused incorrect matrix data9"
  try:
    result = treecluster(data10)
    print "Error: treecluster incorrectly accepted data10"
  except: print "Refused incorrect matrix data10"
  print

def test_kcluster(module):
  if module=='Bio.Cluster':
    from Bio.Cluster import kcluster
  elif module=='Pycluster':
    from Pycluster import kcluster
  else:
    raise ValueError('Unknown module name: ' + module)
  print "test_kcluster"
  nclusters = 3
  # First data set
  weight1 =  array([1,1,1,1,1])
  data1   =  array([[ 1.1, 2.2, 3.3, 4.4, 5.5],
                    [ 3.1, 3.2, 1.3, 2.4, 1.5], 
                    [ 4.1, 2.2, 0.3, 5.4, 0.5], 
                    [12.1, 2.0, 0.0, 5.0, 0.0]]) 
  mask1 =  array([[ 1, 1, 1, 1, 1], 
                  [ 1, 1, 1, 1, 1], 
                  [ 1, 1, 1, 1, 1], 
                  [ 1, 1, 1, 1, 1]]) 
  weight2 =  array([1,1])

  # Second data set
  data2 = array([[ 1.1, 1.2 ],
                 [ 1.4, 1.3 ],
                 [ 1.1, 1.5 ],
                 [ 2.0, 1.5 ],
                 [ 1.7, 1.9 ],
                 [ 1.7, 1.9 ],
                 [ 5.7, 5.9 ],
                 [ 5.7, 5.9 ],
                 [ 3.1, 3.3 ],
                 [ 5.4, 5.3 ],
                 [ 5.1, 5.5 ],
                 [ 5.0, 5.5 ],
                 [ 5.1, 5.2 ]])
  mask2 = array([[ 1, 1 ],
                 [ 1, 1 ],
                 [ 1, 1 ],
                 [ 1, 1 ],
                 [ 1, 1 ],
                 [ 1, 1 ],
                 [ 1, 1 ],
                 [ 1, 1 ],
                 [ 1, 1 ],
                 [ 1, 1 ],
                 [ 1, 1 ],
                 [ 1, 1 ],
                 [ 1, 1 ]])

  # test first data set
  print "First data set"
  clusterid, error, nfound = kcluster (data1, nclusters=nclusters, mask=mask1, weight=weight1, transpose=0, npass=100, method='a', dist='e')
  print "Number of cluster ids is %d (should be %d)" % (len(clusterid), len(data1))
  correct = [0,1,1,2]
  mapping = [clusterid[correct.index(i)] for i in range(nclusters)]
  same = 1
  for i in range(len(clusterid)):
    if clusterid[i]!=mapping[correct[i]]: same = 0
  if same: print "Correct clustering solution found."
  else: print "Wrong clustering solution found."

  # test second data set
  print "Second data set"
  clusterid, error, nfound = kcluster (data2, nclusters=3, mask=mask2, weight=weight2, transpose=0, npass=100, method='a', dist='e')
  print "Number of cluster ids is %d (should be %d)" % (len(clusterid), len(data2))
  correct = [0, 0, 0, 0, 0, 0, 1, 1, 2, 1, 1, 1, 1]
  mapping = [clusterid[correct.index(i)] for i in range(nclusters)]
  same = 1
  for i in range(len(clusterid)):
    if clusterid[i]!=mapping[correct[i]]: same = 0
  if same: print "Correct clustering solution found."
  else: print "Wrong clustering solution found."
  print

def test_clusterdistance(module):
  if module=='Bio.Cluster':
    from Bio.Cluster import clusterdistance
  elif module=='Pycluster':
    from Pycluster import clusterdistance
  else:
    raise ValueError('Unknown module name: ' + module)
  print "test_clusterdistance:"

  # First data set
  weight1 =  array([ 1,1,1,1,1 ])
  data1   =  array([[  1.1, 2.2, 3.3, 4.4, 5.5, ], 
                    [  3.1, 3.2, 1.3, 2.4, 1.5, ], 
                    [  4.1, 2.2, 0.3, 5.4, 0.5, ], 
                    [ 12.1, 2.0, 0.0, 5.0, 0.0, ]])
  mask1   = array([[ 1, 1, 1, 1, 1], 
                   [ 1, 1, 1, 1, 1], 
                   [ 1, 1, 1, 1, 1], 
                   [ 1, 1, 1, 1, 1]])

  # Second data set
  weight2 =  array([ 1,1 ])
  data2   =  array([[ 1.1, 1.2 ],
                    [ 1.4, 1.3 ],
                    [ 1.1, 1.5 ],
                    [ 2.0, 1.5 ],
                    [ 1.7, 1.9 ],
                    [ 1.7, 1.9 ],
                    [ 5.7, 5.9 ],
                    [ 5.7, 5.9 ],
                    [ 3.1, 3.3 ],
                    [ 5.4, 5.3 ],
                    [ 5.1, 5.5 ],
                    [ 5.0, 5.5 ],
                    [ 5.1, 5.2 ]])
  mask2 = array([[ 1, 1 ],
                 [ 1, 1 ],
                 [ 1, 1 ],
                 [ 1, 1 ],
                 [ 1, 1 ],
                 [ 1, 1 ],
                 [ 1, 1 ],
                 [ 1, 1 ],
                 [ 1, 1 ],
                 [ 1, 1 ],
                 [ 1, 1 ],
                 [ 1, 1 ],
                 [ 1, 1 ]])
  # Cluster assignments
  c1 = [0]
  c2 = [1,2]
  c3 = [3]

  print "First data set:"
  print_matrix(data1, mask1)
  print "Clusters are", c1, c2, c3
  distance = clusterdistance(data1, mask=mask1, weight=weight1, index1=c1, index2=c2, dist='e', method='a', transpose=0);
  print "Distance between cluster", c1, "and", c2, "is %7.3f." % distance
  distance = clusterdistance(data1, mask=mask1, weight=weight1, index1=c1, index2=c3, dist='e', method='a', transpose=0);
  print "Distance between cluster", c1, "and", c3, "is %7.3f." % distance
  distance = clusterdistance(data1, mask=mask1, weight=weight1, index1=c2, index2=c3, dist='e', method='a', transpose=0);
  print "Distance between cluster", c2, "and", c3, "is %7.3f." % distance

  # Cluster assignments
  c1 = [ 0, 1, 2, 3 ]
  c2 = [ 4, 5, 6, 7 ]
  c3 = [ 8 ]
  print "Second data set:"
  print_matrix(data2, mask2)
  print "Clusters are", c1, c2, c3
  distance = clusterdistance(data2, mask=mask2, weight=weight2, index1=c1, index2=c2, dist='e', method='a', transpose=0);
  print "Distance between cluster", c1, "and", c2, "is %7.3f." % distance
  distance = clusterdistance(data2, mask=mask2, weight=weight2, index1=c1, index2=c3, dist='e', method='a', transpose=0);
  print "Distance between cluster", c1, "and", c3, "is %7.3f." % distance
  distance = clusterdistance(data2, mask=mask2, weight=weight2, index1=c2, index2=c3, dist='e', method='a', transpose=0);
  print "Distance between cluster", c2, "and", c3, "is %7.3f." % distance
  print

def test_treecluster(module):
  if module=='Bio.Cluster':
    from Bio.Cluster import treecluster
  elif module=='Pycluster':
    from Pycluster import treecluster
  else:
    raise ValueError('Unknown module name: ' + module)
  print "test_treecluster:"
  # First data set
  weight1 =  [ 1,1,1,1,1 ]
  data1   =  array([[  1.1, 2.2, 3.3, 4.4, 5.5], 
                    [  3.1, 3.2, 1.3, 2.4, 1.5], 
                    [  4.1, 2.2, 0.3, 5.4, 0.5], 
                    [ 12.1, 2.0, 0.0, 5.0, 0.0]])
  mask1 = array([[ 1, 1, 1, 1, 1], 
                 [ 1, 1, 1, 1, 1], 
                 [ 1, 1, 1, 1, 1], 
                 [ 1, 1, 1, 1, 1]])

  # Second data set
  weight2 =  [ 1,1 ]
  data2 = array([[ 0.8223, 0.9295 ],
                 [ 1.4365, 1.3223 ],
                 [ 1.1623, 1.5364 ],
                 [ 2.1826, 1.1934 ],
                 [ 1.7763, 1.9352 ],
                 [ 1.7215, 1.9912 ],
                 [ 2.1812, 5.9935 ],
                 [ 5.3290, 5.9452 ],
                 [ 3.1491, 3.3454 ],
                 [ 5.1923, 5.3156 ],
                 [ 4.7735, 5.4012 ],
                 [ 5.1297, 5.5645 ],
                 [ 5.3934, 5.1823 ]])
  mask2 = array([[ 1, 1 ],
                 [ 1, 1 ],
                 [ 1, 1 ],
                 [ 1, 1 ],
                 [ 1, 1 ],
                 [ 1, 1 ],
                 [ 1, 1 ],
                 [ 1, 1 ],
                 [ 1, 1 ],
                 [ 1, 1 ],
                 [ 1, 1 ],
                 [ 1, 1 ],
                 [ 1, 1 ]])

  # test first data set
  print "First data set:"
  print_matrix(data1, mask1)
  print "Pairwise average-linkage clustering"
  tree = treecluster(data=data1, mask=mask1, weight=weight1, transpose=0, method='a', dist='e')
  print "Number of nodes is %d (should be %d)" % (len(tree), len(data1)-1)
  for i in range(len(tree)):
    left = tree[i].left
    right = tree[i].right
    distance = tree[i].distance
    print "Node %3d joins node %3d with node %3d; link distance is %7.3f" % (i, left, right, distance)

  print "Pairwise single-linkage clustering"
  tree = treecluster(data=data1, mask=mask1, weight=weight1, transpose=0, method='s', dist='e')
  print "Number of nodes is %d (should be %d)" % (len(tree), len(data1)-1)
  for i in range(len(tree)):
    left = tree[i].left
    right = tree[i].right
    distance = tree[i].distance
    print "Node %3d joins node %3d with node %3d; link distance is %7.3f" % (i, left, right, distance)

  print "Pairwise centroid-linkage clustering"
  tree = treecluster(data=data1, mask=mask1, weight=weight1, transpose=0, method='c', dist='e')
  print "Number of nodes is %d (should be %d)" % (len(tree), len(data1)-1)
  for i in range(len(tree)):
    left = tree[i].left
    right = tree[i].right
    distance = tree[i].distance
    print "Node %3d joins node %3d with node %3d; link distance is %7.3f" % (i, left, right, distance)

  print "Pairwise maximum-linkage clustering"
  tree = treecluster(data=data1, mask=mask1, weight=weight1, transpose=0, method='m', dist='e')
  print "Number of nodes is %d (should be %d)" % (len(tree), len(data1)-1)
  for i in range(len(tree)):
    left = tree[i].left
    right = tree[i].right
    distance = tree[i].distance
    print "Node %3d joins node %3d with node %3d; link distance is %7.3f" % (i, left, right, distance)

  # Test second data set
  print "Second data set:"
  print "Pairwise average-linkage clustering"
  tree = treecluster(data=data2, mask=mask2, weight=weight2, transpose=0, method='a', dist='e')
  print "Number of nodes is %d (should be %d)" % (len(tree), len(data2)-1)
  for i in range(len(tree)):
    left = tree[i].left
    right = tree[i].right
    distance = tree[i].distance
    print "Node %3d joins node %3d with node %3d; link distance is %7.3f" % (i, left, right, distance)

  print "Pairwise single-linkage clustering"
  tree = treecluster(data=data2, mask=mask2, weight=weight2, transpose=0, method='s', dist='e')
  print "Number of nodes is %d (should be %d)" % (len(tree), len(data2)-1)
  for i in range(len(tree)):
    left = tree[i].left
    right = tree[i].right
    distance = tree[i].distance
    print "Node %3d joins node %3d with node %3d; link distance is %7.3f" % (i, left, right, distance)

  print "Pairwise centroid-linkage clustering"
  tree = treecluster(data=data2, mask=mask2, weight=weight2, transpose=0, method='c', dist='e')
  print "Number of nodes is %d (should be %d)" % (len(tree), len(data2)-1)
  for i in range(len(tree)):
    left = tree[i].left
    right = tree[i].right
    distance = tree[i].distance
    print "Node %3d joins node %3d with node %3d; link distance is %7.3f" % (i, left, right, distance)

  print "Pairwise maximum-linkage clustering"
  tree = treecluster(data=data2, mask=mask2, weight=weight2, transpose=0, method='m', dist='e')
  print "Number of nodes is %d (should be %d)" % (len(tree), len(data2)-1)
  for i in range(len(tree)):
    left = tree[i].left
    right = tree[i].right
    distance = tree[i].distance
    print "Node %3d joins node %3d with node %3d; link distance is %7.3f" % (i, left, right, distance)
  print

def test_somcluster(module):
  if module=='Bio.Cluster':
    from Bio.Cluster import somcluster
  elif module=='Pycluster':
    from Pycluster import somcluster
  else:
    raise ValueError('Unknown module name: ' + module)
  print "test_somcluster:"

  # First data set
  weight1 = [ 1,1,1,1,1 ]
  data1 = array([[  1.1, 2.2, 3.3, 4.4, 5.5], 
                 [  3.1, 3.2, 1.3, 2.4, 1.5], 
                 [  4.1, 2.2, 0.3, 5.4, 0.5], 
                 [ 12.1, 2.0, 0.0, 5.0, 0.0]])
  mask1 = array([[ 1, 1, 1, 1, 1], 
                 [ 1, 1, 1, 1, 1], 
                 [ 1, 1, 1, 1, 1], 
                 [ 1, 1, 1, 1, 1]])

  # Second data set
  weight2 =  [ 1,1 ]
  data2 = array([[ 1.1, 1.2 ],
                 [ 1.4, 1.3 ],
                 [ 1.1, 1.5 ],
                 [ 2.0, 1.5 ],
                 [ 1.7, 1.9 ],
                 [ 1.7, 1.9 ],
                 [ 5.7, 5.9 ],
                 [ 5.7, 5.9 ],
                 [ 3.1, 3.3 ],
                 [ 5.4, 5.3 ],
                 [ 5.1, 5.5 ],
                 [ 5.0, 5.5 ],
                 [ 5.1, 5.2 ]])
  mask2 = array([[ 1, 1 ],
                 [ 1, 1 ],
                 [ 1, 1 ],
                 [ 1, 1 ],
                 [ 1, 1 ],
                 [ 1, 1 ],
                 [ 1, 1 ],
                 [ 1, 1 ],
                 [ 1, 1 ],
                 [ 1, 1 ],
                 [ 1, 1 ],
                 [ 1, 1 ],
                 [ 1, 1 ]])

  print "First data set:"
  clusterid, celldata = somcluster(data=data1, mask=mask1, weight=weight1, transpose=0, nxgrid=10, nygrid=10, inittau=0.02, niter=100, dist='e')
  print "Number of cluster ids is %d (should be %d)" % (len(clusterid), len(data1))
  print "Grid is %d-dimensional (should be 2-dimensional)" % len(clusterid[0])

  print "Second data set:"
  clusterid, celldata = somcluster(data=data2, mask=mask2, weight=weight2, transpose=0, nxgrid=10, nygrid=10, inittau=0.02, niter=100, dist='e')
  print "Number of cluster ids is %d (should be %d)" % (len(clusterid), len(data2))
  print "Grid is %d-dimensional (should be 2-dimensional)" % len(clusterid[0])
  print

def test_distancematrix_kmedoids(module):
  if module=='Bio.Cluster':
    from Bio.Cluster import distancematrix, kmedoids
  elif module=='Pycluster':
    from Pycluster import distancematrix, kmedoids
  else:
    raise ValueError('Unknown module name: ' + module)

  print "test_distancematrix_kmedoids:"
  data = array([[2.2, 3.3, 4.4],
                [2.1, 1.4, 5.6],
                [7.8, 9.0, 1.2],
                [4.5, 2.3, 1.5],
                [4.2, 2.4, 1.9],
                [3.6, 3.1, 9.3],
                [2.3, 1.2, 3.9],
                [4.2, 9.6, 9.3],
                [1.7, 8.9, 1.1]])
  mask = array([[1, 1, 1],
                [1, 1, 1],
                [0, 1, 1],
                [1, 1, 1],
                [1, 1, 1],
                [0, 1, 0],
                [1, 1, 1],
                [1, 0, 1],
                [1, 1, 1]])
  weight = array([2.0, 1.0, 0.5])
  matrix = distancematrix(data, mask=mask, weight=weight)
  print "Distance matrix:"
  for row in matrix:
      print " ".join(["%7.3f" % number for number in row])
  clusterid, error, nfound = kmedoids(matrix, npass=1000)
  print "Cluster assignments:", clusterid
  print "Within-cluster sum of distances: %7.3f" % error

def run_tests(module="Pycluster"):
  if module==[]: module = "Bio.Cluster"
  test_mean_median(module)
  test_matrix_parse(module)
  test_kcluster(module)
  test_clusterdistance(module)
  test_treecluster(module)
  test_somcluster(module)
  test_distancematrix_kmedoids(module)

if __name__ == "__main__" :
  print "test_Cluster"
  run_tests(module = "Bio.Cluster")