annotate chembl.py @ 0:915e9be38994 draft

planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
author bgruening
date Mon, 05 Aug 2019 05:21:58 -0400
parents
children 6f8458d1cf46
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
1 from chembl_webresource_client.new_client import new_client
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
2 import argparse
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
3
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
4 def open_file(filename):
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
5 with open(filename) as f:
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
6 return f.readline()
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
7
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
8 def get_smiles(res):
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
9 """
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
10 Get a list of SMILES from function results
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
11 """
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
12 smiles = set()
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
13 for smi in res:
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
14 smiles.add(smi['molecule_structures']['canonical_smiles'])
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
15 return smiles
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
16
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
17 def sim_search(smiles, tanimoto):
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
18 """
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
19 Return compounds which are within a Tanimoto range of the SMILES input
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
20 """
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
21 similarity = new_client.similarity
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
22 return similarity.filter(smiles=smiles, similarity=tanimoto).only(['molecule_structures'])
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
23
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
24 def substr_search(smiles):
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
25 """
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
26 Return compounds which contain the SMILES substructure input
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
27 """
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
28 substructure = new_client.substructure
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
29 return substructure.filter(smiles=smiles).only(['molecule_structures'])
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
30
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
31 def filter_drugs(mols):
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
32 """
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
33 Return only compounds which are approved drugs
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
34 """
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
35 return mols.filter(max_phase=4)
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
36
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
37 def filter_biotherapeutic(mols):
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
38 """
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
39 Return only biotherapeutic molecules
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
40 """
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
41 return mols.filter(biotherapeutic__isnull=False)
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
42
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
43 def filter_nat_prod(mols):
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
44 """
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
45 Return only natural products
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
46 """
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
47 return mols.filter(natural_product=1)
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
48
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
49 def filter_ro5(mols):
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
50 """
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
51 Return only compounds with no RO5 violations
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
52 """
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
53 return mols.filter(molecule_properties__num_ro5_violations=0)
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
54
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
55 def main():
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
56 parser = argparse.ArgumentParser(description='Search ChEMBL database for compounds')
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
57 parser.add_argument('-i', '--input', help='SMILES input')
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
58 parser.add_argument('-f', '--file', help='SMILES input as file')
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
59 parser.add_argument('-o', '--output', help="SMILES output")
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
60 parser.add_argument('-t', '--tanimoto', type=int, help='Tanimoto similarity score')
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
61 parser.add_argument('-s', '--substructure', action='store_true', help='Substructure search using the SMILES input.')
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
62 parser.add_argument('-d', '--drugs', action='store_true', help='Filter approved drugs')
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
63 parser.add_argument('-b', '--biotherapeutic', action='store_true', help='Filter biotherapeutic molecules')
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
64 parser.add_argument('-n', '--nat-prod', action='store_true', help='Filter natural products')
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
65 parser.add_argument('-r', '--ro5', action='store_true', help='Filter compounds that pass Lipinski RO5')
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
66
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
67 args = parser.parse_args()
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
68
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
69 if args.file: # get SMILES from file rather than -i option
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
70 args.input = open_file(args.file)
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
71
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
72 if len(args.input) < 5:
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
73 raise IOError('SMILES must be at least 5 characters long.')
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
74
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
75 if args.substructure: # specify search type: substructure or similarity
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
76 mols = substr_search(args.input)
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
77 else:
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
78 mols = sim_search(args.input, args.tanimoto)
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
79
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
80 # filter options:
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
81 if args.drugs:
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
82 mols = filter_drugs(mols)
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
83
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
84 if args.biotherapeutic:
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
85 mols = filter_biotherapeutic(mols)
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
86
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
87 if args.nat_prod:
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
88 mols = filter_nat_prod(mols)
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
89
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
90 if args.ro5:
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
91 mols = filter_ro5(mols)
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
92
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
93 # get SMILES from search output
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
94 mols = get_smiles(mols)
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
95
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
96 # write to file
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
97 with open(args.output, 'w') as f:
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
98 f.write('\n'.join(mols))
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
99
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
100
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
101 if __name__ == "__main__":
915e9be38994 planemo upload for repository https://github.com/chembl/chembl_webresource_client commit 2e3c3c2bd7ecdc9c2968a32f91e81136e0cb3835
bgruening
parents:
diff changeset
102 main()