annotate chemfp_clustering/nxn_clustering.py @ 21:7c84cfa515e0

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