annotate ob_filter.py @ 7:fac2c28b4c55 draft default tip

planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit d9c51279c061a1da948a2582d5b502ca7573adbf
author bgruening
date Thu, 15 Aug 2024 11:04:16 +0000
parents a5f4b80e6769
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
06340f46ecb8 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 72df8ae9b8fd9910b3d24aa0836b9b3c9d43f4fb
bgruening
parents:
diff changeset
1 #!/usr/bin/env python
06340f46ecb8 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 72df8ae9b8fd9910b3d24aa0836b9b3c9d43f4fb
bgruening
parents:
diff changeset
2 """
06340f46ecb8 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 72df8ae9b8fd9910b3d24aa0836b9b3c9d43f4fb
bgruening
parents:
diff changeset
3 Input: set of molecules with pre-calculated physico-chemical properties
06340f46ecb8 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 72df8ae9b8fd9910b3d24aa0836b9b3c9d43f4fb
bgruening
parents:
diff changeset
4 Output: set of molecules that pass all the filters
06340f46ecb8 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 72df8ae9b8fd9910b3d24aa0836b9b3c9d43f4fb
bgruening
parents:
diff changeset
5 Copyright 2012, Bjoern Gruening and Xavier Lucas
06340f46ecb8 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 72df8ae9b8fd9910b3d24aa0836b9b3c9d43f4fb
bgruening
parents:
diff changeset
6
06340f46ecb8 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 72df8ae9b8fd9910b3d24aa0836b9b3c9d43f4fb
bgruening
parents:
diff changeset
7 TODO: AND/OR conditions?
06340f46ecb8 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 72df8ae9b8fd9910b3d24aa0836b9b3c9d43f4fb
bgruening
parents:
diff changeset
8 """
06340f46ecb8 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 72df8ae9b8fd9910b3d24aa0836b9b3c9d43f4fb
bgruening
parents:
diff changeset
9 import argparse
5
a5f4b80e6769 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 1fe240ef0064a1a4a66d9be1ccace53824280b75"
bgruening
parents: 4
diff changeset
10 import json
a5f4b80e6769 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 1fe240ef0064a1a4a66d9be1ccace53824280b75"
bgruening
parents: 4
diff changeset
11 import shlex
a5f4b80e6769 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 1fe240ef0064a1a4a66d9be1ccace53824280b75"
bgruening
parents: 4
diff changeset
12 import subprocess
a5f4b80e6769 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 1fe240ef0064a1a4a66d9be1ccace53824280b75"
bgruening
parents: 4
diff changeset
13 import sys
a5f4b80e6769 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 1fe240ef0064a1a4a66d9be1ccace53824280b75"
bgruening
parents: 4
diff changeset
14
0
06340f46ecb8 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 72df8ae9b8fd9910b3d24aa0836b9b3c9d43f4fb
bgruening
parents:
diff changeset
15 import cheminfolib
4
de4c80d17527 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 944ea4bb8a9cd4244152a4a4fecd0485fabc2ad0"
bgruening
parents: 3
diff changeset
16 from openbabel import pybel
7
fac2c28b4c55 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit d9c51279c061a1da948a2582d5b502ca7573adbf
bgruening
parents: 5
diff changeset
17
0
06340f46ecb8 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 72df8ae9b8fd9910b3d24aa0836b9b3c9d43f4fb
bgruening
parents:
diff changeset
18 cheminfolib.pybel_stop_logging()
06340f46ecb8 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 72df8ae9b8fd9910b3d24aa0836b9b3c9d43f4fb
bgruening
parents:
diff changeset
19
5
a5f4b80e6769 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 1fe240ef0064a1a4a66d9be1ccace53824280b75"
bgruening
parents: 4
diff changeset
20
0
06340f46ecb8 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 72df8ae9b8fd9910b3d24aa0836b9b3c9d43f4fb
bgruening
parents:
diff changeset
21 def parse_command_line():
06340f46ecb8 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 72df8ae9b8fd9910b3d24aa0836b9b3c9d43f4fb
bgruening
parents:
diff changeset
22 parser = argparse.ArgumentParser()
7
fac2c28b4c55 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit d9c51279c061a1da948a2582d5b502ca7573adbf
bgruening
parents: 5
diff changeset
23 parser.add_argument("-i", "--input", help="Input file name")
fac2c28b4c55 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit d9c51279c061a1da948a2582d5b502ca7573adbf
bgruening
parents: 5
diff changeset
24 parser.add_argument("-iformat", help="Input file format")
fac2c28b4c55 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit d9c51279c061a1da948a2582d5b502ca7573adbf
bgruening
parents: 5
diff changeset
25 parser.add_argument("-oformat", default="smi", help="Output file format")
fac2c28b4c55 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit d9c51279c061a1da948a2582d5b502ca7573adbf
bgruening
parents: 5
diff changeset
26 parser.add_argument("-o", "--output", help="Output file name", required=True)
fac2c28b4c55 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit d9c51279c061a1da948a2582d5b502ca7573adbf
bgruening
parents: 5
diff changeset
27 parser.add_argument("--filters", help="Specify the filters to apply", required=True)
fac2c28b4c55 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit d9c51279c061a1da948a2582d5b502ca7573adbf
bgruening
parents: 5
diff changeset
28 parser.add_argument(
fac2c28b4c55 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit d9c51279c061a1da948a2582d5b502ca7573adbf
bgruening
parents: 5
diff changeset
29 "--list_of_names",
fac2c28b4c55 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit d9c51279c061a1da948a2582d5b502ca7573adbf
bgruening
parents: 5
diff changeset
30 required=False,
fac2c28b4c55 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit d9c51279c061a1da948a2582d5b502ca7573adbf
bgruening
parents: 5
diff changeset
31 help="A file with list of molecule names to extract. Every name is in one line.",
fac2c28b4c55 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit d9c51279c061a1da948a2582d5b502ca7573adbf
bgruening
parents: 5
diff changeset
32 )
0
06340f46ecb8 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 72df8ae9b8fd9910b3d24aa0836b9b3c9d43f4fb
bgruening
parents:
diff changeset
33 return parser.parse_args()
06340f46ecb8 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 72df8ae9b8fd9910b3d24aa0836b9b3c9d43f4fb
bgruening
parents:
diff changeset
34
5
a5f4b80e6769 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 1fe240ef0064a1a4a66d9be1ccace53824280b75"
bgruening
parents: 4
diff changeset
35
0
06340f46ecb8 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 72df8ae9b8fd9910b3d24aa0836b9b3c9d43f4fb
bgruening
parents:
diff changeset
36 def filter_precalculated_compounds(args, filters):
06340f46ecb8 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 72df8ae9b8fd9910b3d24aa0836b9b3c9d43f4fb
bgruening
parents:
diff changeset
37 outfile = pybel.Outputfile(args.oformat, args.output, overwrite=True)
7
fac2c28b4c55 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit d9c51279c061a1da948a2582d5b502ca7573adbf
bgruening
parents: 5
diff changeset
38 for mol in pybel.readfile("sdf", args.input):
0
06340f46ecb8 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 72df8ae9b8fd9910b3d24aa0836b9b3c9d43f4fb
bgruening
parents:
diff changeset
39 for key, elem in filters.items():
06340f46ecb8 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 72df8ae9b8fd9910b3d24aa0836b9b3c9d43f4fb
bgruening
parents:
diff changeset
40 # map the short description to the larger metadata names stored in the sdf file
3
015ccbd1ad68 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit cda909c5e0b88fa3d12abe43fc72b8dd0729417a"
bgruening
parents: 2
diff changeset
41 property = cheminfolib.ColumnNames.get(key, key)
0
06340f46ecb8 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 72df8ae9b8fd9910b3d24aa0836b9b3c9d43f4fb
bgruening
parents:
diff changeset
42 min = elem[0]
06340f46ecb8 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 72df8ae9b8fd9910b3d24aa0836b9b3c9d43f4fb
bgruening
parents:
diff changeset
43 max = elem[1]
7
fac2c28b4c55 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit d9c51279c061a1da948a2582d5b502ca7573adbf
bgruening
parents: 5
diff changeset
44 if float(mol.data[property]) >= float(min) and float(
fac2c28b4c55 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit d9c51279c061a1da948a2582d5b502ca7573adbf
bgruening
parents: 5
diff changeset
45 mol.data[property]
fac2c28b4c55 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit d9c51279c061a1da948a2582d5b502ca7573adbf
bgruening
parents: 5
diff changeset
46 ) <= float(max):
0
06340f46ecb8 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 72df8ae9b8fd9910b3d24aa0836b9b3c9d43f4fb
bgruening
parents:
diff changeset
47 pass
06340f46ecb8 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 72df8ae9b8fd9910b3d24aa0836b9b3c9d43f4fb
bgruening
parents:
diff changeset
48 else:
06340f46ecb8 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 72df8ae9b8fd9910b3d24aa0836b9b3c9d43f4fb
bgruening
parents:
diff changeset
49 # leave the filter loop, because one filter constrained are not satisfied
06340f46ecb8 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 72df8ae9b8fd9910b3d24aa0836b9b3c9d43f4fb
bgruening
parents:
diff changeset
50 break
06340f46ecb8 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 72df8ae9b8fd9910b3d24aa0836b9b3c9d43f4fb
bgruening
parents:
diff changeset
51 else:
06340f46ecb8 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 72df8ae9b8fd9910b3d24aa0836b9b3c9d43f4fb
bgruening
parents:
diff changeset
52 # if the filter loop terminates in a normal way (no break) all filter rules are satisfied, so save the compound
06340f46ecb8 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 72df8ae9b8fd9910b3d24aa0836b9b3c9d43f4fb
bgruening
parents:
diff changeset
53 outfile.write(mol)
06340f46ecb8 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 72df8ae9b8fd9910b3d24aa0836b9b3c9d43f4fb
bgruening
parents:
diff changeset
54 outfile.close()
06340f46ecb8 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 72df8ae9b8fd9910b3d24aa0836b9b3c9d43f4fb
bgruening
parents:
diff changeset
55
5
a5f4b80e6769 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 1fe240ef0064a1a4a66d9be1ccace53824280b75"
bgruening
parents: 4
diff changeset
56
0
06340f46ecb8 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 72df8ae9b8fd9910b3d24aa0836b9b3c9d43f4fb
bgruening
parents:
diff changeset
57 def filter_new_compounds(args, filters):
06340f46ecb8 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 72df8ae9b8fd9910b3d24aa0836b9b3c9d43f4fb
bgruening
parents:
diff changeset
58
06340f46ecb8 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 72df8ae9b8fd9910b3d24aa0836b9b3c9d43f4fb
bgruening
parents:
diff changeset
59 if args.iformat == args.oformat:
06340f46ecb8 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 72df8ae9b8fd9910b3d24aa0836b9b3c9d43f4fb
bgruening
parents:
diff changeset
60 # use the -ocopy option from openbabel to speed up the filtering, additionally no conversion is carried out
06340f46ecb8 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 72df8ae9b8fd9910b3d24aa0836b9b3c9d43f4fb
bgruening
parents:
diff changeset
61 # http://openbabel.org/docs/dev/FileFormats/Copy_raw_text.html#copy-raw-text
7
fac2c28b4c55 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit d9c51279c061a1da948a2582d5b502ca7573adbf
bgruening
parents: 5
diff changeset
62 cmd = "obabel -i%s %s -ocopy -O %s --filter" % (
fac2c28b4c55 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit d9c51279c061a1da948a2582d5b502ca7573adbf
bgruening
parents: 5
diff changeset
63 args.iformat,
fac2c28b4c55 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit d9c51279c061a1da948a2582d5b502ca7573adbf
bgruening
parents: 5
diff changeset
64 args.input,
fac2c28b4c55 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit d9c51279c061a1da948a2582d5b502ca7573adbf
bgruening
parents: 5
diff changeset
65 args.output,
fac2c28b4c55 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit d9c51279c061a1da948a2582d5b502ca7573adbf
bgruening
parents: 5
diff changeset
66 )
0
06340f46ecb8 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 72df8ae9b8fd9910b3d24aa0836b9b3c9d43f4fb
bgruening
parents:
diff changeset
67 else:
7
fac2c28b4c55 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit d9c51279c061a1da948a2582d5b502ca7573adbf
bgruening
parents: 5
diff changeset
68 cmd = "obabel -i%s %s -o%s -O %s --filter" % (
fac2c28b4c55 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit d9c51279c061a1da948a2582d5b502ca7573adbf
bgruening
parents: 5
diff changeset
69 args.iformat,
fac2c28b4c55 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit d9c51279c061a1da948a2582d5b502ca7573adbf
bgruening
parents: 5
diff changeset
70 args.input,
fac2c28b4c55 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit d9c51279c061a1da948a2582d5b502ca7573adbf
bgruening
parents: 5
diff changeset
71 args.oformat,
fac2c28b4c55 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit d9c51279c061a1da948a2582d5b502ca7573adbf
bgruening
parents: 5
diff changeset
72 args.output,
fac2c28b4c55 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit d9c51279c061a1da948a2582d5b502ca7573adbf
bgruening
parents: 5
diff changeset
73 )
fac2c28b4c55 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit d9c51279c061a1da948a2582d5b502ca7573adbf
bgruening
parents: 5
diff changeset
74 filter_cmd = ""
0
06340f46ecb8 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 72df8ae9b8fd9910b3d24aa0836b9b3c9d43f4fb
bgruening
parents:
diff changeset
75 # OBDescriptor stores a mapping from our desc shortcut to the OB name [0] and a long description [1]
06340f46ecb8 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 72df8ae9b8fd9910b3d24aa0836b9b3c9d43f4fb
bgruening
parents:
diff changeset
76 for key, elem in filters.items():
06340f46ecb8 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 72df8ae9b8fd9910b3d24aa0836b9b3c9d43f4fb
bgruening
parents:
diff changeset
77 ob_descriptor_name = cheminfolib.OBDescriptor[key][0]
06340f46ecb8 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 72df8ae9b8fd9910b3d24aa0836b9b3c9d43f4fb
bgruening
parents:
diff changeset
78 min = elem[0]
06340f46ecb8 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 72df8ae9b8fd9910b3d24aa0836b9b3c9d43f4fb
bgruening
parents:
diff changeset
79 max = elem[1]
7
fac2c28b4c55 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit d9c51279c061a1da948a2582d5b502ca7573adbf
bgruening
parents: 5
diff changeset
80 filter_cmd += " %s>=%s %s<=%s " % (
fac2c28b4c55 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit d9c51279c061a1da948a2582d5b502ca7573adbf
bgruening
parents: 5
diff changeset
81 ob_descriptor_name,
fac2c28b4c55 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit d9c51279c061a1da948a2582d5b502ca7573adbf
bgruening
parents: 5
diff changeset
82 min,
fac2c28b4c55 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit d9c51279c061a1da948a2582d5b502ca7573adbf
bgruening
parents: 5
diff changeset
83 ob_descriptor_name,
fac2c28b4c55 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit d9c51279c061a1da948a2582d5b502ca7573adbf
bgruening
parents: 5
diff changeset
84 max,
fac2c28b4c55 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit d9c51279c061a1da948a2582d5b502ca7573adbf
bgruening
parents: 5
diff changeset
85 )
0
06340f46ecb8 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 72df8ae9b8fd9910b3d24aa0836b9b3c9d43f4fb
bgruening
parents:
diff changeset
86
06340f46ecb8 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 72df8ae9b8fd9910b3d24aa0836b9b3c9d43f4fb
bgruening
parents:
diff changeset
87 args = shlex.split('%s "%s"' % (cmd, filter_cmd))
5
a5f4b80e6769 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 1fe240ef0064a1a4a66d9be1ccace53824280b75"
bgruening
parents: 4
diff changeset
88 # print '%s "%s"' % (cmd, filter_cmd)
0
06340f46ecb8 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 72df8ae9b8fd9910b3d24aa0836b9b3c9d43f4fb
bgruening
parents:
diff changeset
89 # calling openbabel with subprocess and pipe potential errors occuring in openbabel to stdout
5
a5f4b80e6769 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 1fe240ef0064a1a4a66d9be1ccace53824280b75"
bgruening
parents: 4
diff changeset
90 child = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
0
06340f46ecb8 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 72df8ae9b8fd9910b3d24aa0836b9b3c9d43f4fb
bgruening
parents:
diff changeset
91
06340f46ecb8 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 72df8ae9b8fd9910b3d24aa0836b9b3c9d43f4fb
bgruening
parents:
diff changeset
92 stdout, stderr = child.communicate()
06340f46ecb8 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 72df8ae9b8fd9910b3d24aa0836b9b3c9d43f4fb
bgruening
parents:
diff changeset
93 return_code = child.returncode
06340f46ecb8 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 72df8ae9b8fd9910b3d24aa0836b9b3c9d43f4fb
bgruening
parents:
diff changeset
94
06340f46ecb8 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 72df8ae9b8fd9910b3d24aa0836b9b3c9d43f4fb
bgruening
parents:
diff changeset
95 if return_code:
7
fac2c28b4c55 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit d9c51279c061a1da948a2582d5b502ca7573adbf
bgruening
parents: 5
diff changeset
96 sys.stdout.write(stdout.decode("utf-8"))
fac2c28b4c55 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit d9c51279c061a1da948a2582d5b502ca7573adbf
bgruening
parents: 5
diff changeset
97 sys.stderr.write(stderr.decode("utf-8"))
0
06340f46ecb8 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 72df8ae9b8fd9910b3d24aa0836b9b3c9d43f4fb
bgruening
parents:
diff changeset
98 sys.stderr.write("Return error code %i from command:\n" % return_code)
06340f46ecb8 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 72df8ae9b8fd9910b3d24aa0836b9b3c9d43f4fb
bgruening
parents:
diff changeset
99 sys.stderr.write("%s\n" % cmd)
06340f46ecb8 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 72df8ae9b8fd9910b3d24aa0836b9b3c9d43f4fb
bgruening
parents:
diff changeset
100 else:
7
fac2c28b4c55 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit d9c51279c061a1da948a2582d5b502ca7573adbf
bgruening
parents: 5
diff changeset
101 sys.stdout.write(stdout.decode("utf-8"))
fac2c28b4c55 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit d9c51279c061a1da948a2582d5b502ca7573adbf
bgruening
parents: 5
diff changeset
102 sys.stdout.write(stderr.decode("utf-8"))
0
06340f46ecb8 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 72df8ae9b8fd9910b3d24aa0836b9b3c9d43f4fb
bgruening
parents:
diff changeset
103
5
a5f4b80e6769 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 1fe240ef0064a1a4a66d9be1ccace53824280b75"
bgruening
parents: 4
diff changeset
104
2
b8838a9aa31a "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 6c84abdd07f292048bf2194073e2e938e94158c4"
bgruening
parents: 0
diff changeset
105 def filter_by_name(args):
b8838a9aa31a "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 6c84abdd07f292048bf2194073e2e938e94158c4"
bgruening
parents: 0
diff changeset
106 outfile = pybel.Outputfile(args.oformat, args.output, overwrite=True)
7
fac2c28b4c55 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit d9c51279c061a1da948a2582d5b502ca7573adbf
bgruening
parents: 5
diff changeset
107 for mol in pybel.readfile("sdf", args.input):
2
b8838a9aa31a "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 6c84abdd07f292048bf2194073e2e938e94158c4"
bgruening
parents: 0
diff changeset
108 for name in open(args.list_of_names):
b8838a9aa31a "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 6c84abdd07f292048bf2194073e2e938e94158c4"
bgruening
parents: 0
diff changeset
109 if mol.title.strip() == name.strip():
b8838a9aa31a "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 6c84abdd07f292048bf2194073e2e938e94158c4"
bgruening
parents: 0
diff changeset
110 outfile.write(mol)
b8838a9aa31a "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 6c84abdd07f292048bf2194073e2e938e94158c4"
bgruening
parents: 0
diff changeset
111 outfile.close()
0
06340f46ecb8 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 72df8ae9b8fd9910b3d24aa0836b9b3c9d43f4fb
bgruening
parents:
diff changeset
112
5
a5f4b80e6769 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 1fe240ef0064a1a4a66d9be1ccace53824280b75"
bgruening
parents: 4
diff changeset
113
0
06340f46ecb8 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 72df8ae9b8fd9910b3d24aa0836b9b3c9d43f4fb
bgruening
parents:
diff changeset
114 def __main__():
06340f46ecb8 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 72df8ae9b8fd9910b3d24aa0836b9b3c9d43f4fb
bgruening
parents:
diff changeset
115 """
7
fac2c28b4c55 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit d9c51279c061a1da948a2582d5b502ca7573adbf
bgruening
parents: 5
diff changeset
116 Select compounds with certain properties from a small library
0
06340f46ecb8 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 72df8ae9b8fd9910b3d24aa0836b9b3c9d43f4fb
bgruening
parents:
diff changeset
117 """
06340f46ecb8 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 72df8ae9b8fd9910b3d24aa0836b9b3c9d43f4fb
bgruening
parents:
diff changeset
118 args = parse_command_line()
5
a5f4b80e6769 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 1fe240ef0064a1a4a66d9be1ccace53824280b75"
bgruening
parents: 4
diff changeset
119
7
fac2c28b4c55 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit d9c51279c061a1da948a2582d5b502ca7573adbf
bgruening
parents: 5
diff changeset
120 if args.filters == "__filter_by_name__":
2
b8838a9aa31a "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 6c84abdd07f292048bf2194073e2e938e94158c4"
bgruening
parents: 0
diff changeset
121 filter_by_name(args)
b8838a9aa31a "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 6c84abdd07f292048bf2194073e2e938e94158c4"
bgruening
parents: 0
diff changeset
122 return
5
a5f4b80e6769 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 1fe240ef0064a1a4a66d9be1ccace53824280b75"
bgruening
parents: 4
diff changeset
123
0
06340f46ecb8 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 72df8ae9b8fd9910b3d24aa0836b9b3c9d43f4fb
bgruening
parents:
diff changeset
124 # Its a small trick to get the parameters in an easy way from the xml file.
06340f46ecb8 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 72df8ae9b8fd9910b3d24aa0836b9b3c9d43f4fb
bgruening
parents:
diff changeset
125 # To keep it readable in the xml file, many white-spaces are included in that string it needs to be removed.
06340f46ecb8 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 72df8ae9b8fd9910b3d24aa0836b9b3c9d43f4fb
bgruening
parents:
diff changeset
126 # Also the last loop creates a ',{' that is not an valid jason expression.
7
fac2c28b4c55 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit d9c51279c061a1da948a2582d5b502ca7573adbf
bgruening
parents: 5
diff changeset
127 filters = json.loads((args.filters).replace(" ", "").replace(",}", "}"))
fac2c28b4c55 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit d9c51279c061a1da948a2582d5b502ca7573adbf
bgruening
parents: 5
diff changeset
128 if args.iformat == "sdf":
0
06340f46ecb8 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 72df8ae9b8fd9910b3d24aa0836b9b3c9d43f4fb
bgruening
parents:
diff changeset
129 # Check if the sdf file contains all of the required metadata to invoke the precalculation filtering
7
fac2c28b4c55 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit d9c51279c061a1da948a2582d5b502ca7573adbf
bgruening
parents: 5
diff changeset
130 mol = next(pybel.readfile("sdf", args.input))
0
06340f46ecb8 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 72df8ae9b8fd9910b3d24aa0836b9b3c9d43f4fb
bgruening
parents:
diff changeset
131 for key, elem in filters.items():
3
015ccbd1ad68 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit cda909c5e0b88fa3d12abe43fc72b8dd0729417a"
bgruening
parents: 2
diff changeset
132 property = cheminfolib.ColumnNames.get(key, key)
5
a5f4b80e6769 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 1fe240ef0064a1a4a66d9be1ccace53824280b75"
bgruening
parents: 4
diff changeset
133 if property not in mol.data:
0
06340f46ecb8 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 72df8ae9b8fd9910b3d24aa0836b9b3c9d43f4fb
bgruening
parents:
diff changeset
134 break
06340f46ecb8 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 72df8ae9b8fd9910b3d24aa0836b9b3c9d43f4fb
bgruening
parents:
diff changeset
135 else:
06340f46ecb8 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 72df8ae9b8fd9910b3d24aa0836b9b3c9d43f4fb
bgruening
parents:
diff changeset
136 # if the for loop finishes in a normal way, we should habe all properties at least in the first molecule
06340f46ecb8 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 72df8ae9b8fd9910b3d24aa0836b9b3c9d43f4fb
bgruening
parents:
diff changeset
137 # assume it is the same for all other molecules and start the precalculated filtering
06340f46ecb8 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 72df8ae9b8fd9910b3d24aa0836b9b3c9d43f4fb
bgruening
parents:
diff changeset
138 filter_precalculated_compounds(args, filters)
06340f46ecb8 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 72df8ae9b8fd9910b3d24aa0836b9b3c9d43f4fb
bgruening
parents:
diff changeset
139 return True
06340f46ecb8 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 72df8ae9b8fd9910b3d24aa0836b9b3c9d43f4fb
bgruening
parents:
diff changeset
140 filter_new_compounds(args, filters)
06340f46ecb8 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 72df8ae9b8fd9910b3d24aa0836b9b3c9d43f4fb
bgruening
parents:
diff changeset
141
06340f46ecb8 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 72df8ae9b8fd9910b3d24aa0836b9b3c9d43f4fb
bgruening
parents:
diff changeset
142
5
a5f4b80e6769 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 1fe240ef0064a1a4a66d9be1ccace53824280b75"
bgruening
parents: 4
diff changeset
143 if __name__ == "__main__":
0
06340f46ecb8 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 72df8ae9b8fd9910b3d24aa0836b9b3c9d43f4fb
bgruening
parents:
diff changeset
144 __main__()