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