Mercurial > repos > bgruening > chemfp
annotate nxn_clustering.py @ 5:57a1a58056a6 draft
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit d786052cd04f8b25eb4aff80b1b9724f62031b61
author | bgruening |
---|---|
date | Sat, 20 May 2017 12:57:06 -0400 |
parents | 70b071de9bee |
children | 0d88631bb7de |
rev | line source |
---|---|
2
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
1 #!/usr/bin/env python |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
2 """ |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
3 Modified version of code examples from the chemfp project. |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
4 http://code.google.com/p/chem-fingerprints/ |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
5 Thanks to Andrew Dalke of Andrew Dalke Scientific! |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
6 """ |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
7 import matplotlib |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
8 matplotlib.use('Agg') |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
9 import argparse |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
10 import os |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
11 import chemfp |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
12 import scipy.cluster.hierarchy as hcluster |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
13 import pylab |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
14 import numpy |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
15 |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
16 def distance_matrix(arena, tanimoto_threshold = 0.0): |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
17 n = len(arena) |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
18 # Start off a similarity matrix with 1.0s along the diagonal |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
19 try: |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
20 similarities = numpy.identity(n, "d") |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
21 except: |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
22 raise Exception('Input dataset is to large!') |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
23 chemfp.set_num_threads( args.processors ) |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
24 |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
25 ## Compute the full similarity matrix. |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
26 # The implementation computes the upper-triangle then copies |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
27 # the upper-triangle into lower-triangle. It does not include |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
28 # terms for the diagonal. |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
29 results = chemfp.search.threshold_tanimoto_search_symmetric(arena, threshold=tanimoto_threshold) |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
30 |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
31 # Copy the results into the NumPy array. |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
32 for row_index, row in enumerate(results.iter_indices_and_scores()): |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
33 for target_index, target_score in row: |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
34 similarities[row_index, target_index] = target_score |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
35 |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
36 # Return the distance matrix using the similarity matrix |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
37 return 1.0 - similarities |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
38 |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
39 |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
40 if __name__ == "__main__": |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
41 parser = argparse.ArgumentParser(description="""NxN clustering for fps files. |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
42 For more details please see the chemfp documentation: |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
43 https://chemfp.readthedocs.org |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
44 """) |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
45 |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
46 parser.add_argument("-i", "--input", dest="input_path", |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
47 required=True, |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
48 help="Path to the input file.") |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
49 |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
50 parser.add_argument("-c", "--cluster", dest="cluster_image", |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
51 help="Path to the output cluster image.") |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
52 |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
53 parser.add_argument("-s", "--smatrix", dest="similarity_matrix", |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
54 help="Path to the similarity matrix output file.") |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
55 |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
56 parser.add_argument("-t", "--threshold", dest="tanimoto_threshold", |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
57 type=float, default=0.0, |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
58 help="Tanimoto threshold [0.0]") |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
59 |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
60 parser.add_argument("--oformat", default='png', help="Output format (png, svg)") |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
61 |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
62 parser.add_argument('-p', '--processors', type=int, |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
63 default=4) |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
64 |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
65 args = parser.parse_args() |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
66 |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
67 targets = chemfp.open( args.input_path, format='fps' ) |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
68 arena = chemfp.load_fingerprints( targets ) |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
69 distances = distance_matrix( arena, args.tanimoto_threshold ) |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
70 |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
71 if args.similarity_matrix: |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
72 distances.tofile( args.similarity_matrix ) |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
73 |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
74 if args.cluster_image: |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
75 linkage = hcluster.linkage( distances, method="single", metric="euclidean" ) |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
76 |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
77 hcluster.dendrogram(linkage, labels=arena.ids) |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
78 |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
79 pylab.savefig( args.cluster_image, format=args.oformat ) |
70b071de9bee
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/chemfp commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
bgruening
parents:
diff
changeset
|
80 |