Mercurial > repos > bgruening > openbabel_remduplicates
annotate subsearch.py @ 13:d44de092fef3 draft
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 1fe240ef0064a1a4a66d9be1ccace53824280b75"
| author | bgruening | 
|---|---|
| date | Mon, 19 Oct 2020 14:36:22 +0000 | 
| parents | 8c4a4e9e173c | 
| children | f3099132512d | 
| rev | line source | 
|---|---|
| 0 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 1 #!/usr/bin/env python | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 2 """ | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 3 Input: Molecules in SDF, SMILES ... | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 4 Output: Moleculs filtered with specified substructures. | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 5 Copyright 2013, Bjoern Gruening and Xavier Lucas | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 6 """ | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 7 import argparse | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 8 import multiprocessing | 
| 13 
d44de092fef3
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 1fe240ef0064a1a4a66d9be1ccace53824280b75"
 bgruening parents: 
12diff
changeset | 9 import os | 
| 
d44de092fef3
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 1fe240ef0064a1a4a66d9be1ccace53824280b75"
 bgruening parents: 
12diff
changeset | 10 import shutil | 
| 0 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 11 import subprocess | 
| 13 
d44de092fef3
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 1fe240ef0064a1a4a66d9be1ccace53824280b75"
 bgruening parents: 
12diff
changeset | 12 import sys | 
| 
d44de092fef3
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 1fe240ef0064a1a4a66d9be1ccace53824280b75"
 bgruening parents: 
12diff
changeset | 13 import tempfile | 
| 0 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 14 | 
| 12 
8c4a4e9e173c
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 944ea4bb8a9cd4244152a4a4fecd0485fabc2ad0"
 bgruening parents: 
0diff
changeset | 15 from openbabel import openbabel, pybel | 
| 
8c4a4e9e173c
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 944ea4bb8a9cd4244152a4a4fecd0485fabc2ad0"
 bgruening parents: 
0diff
changeset | 16 openbabel.obErrorLog.StopLogging() | 
| 
8c4a4e9e173c
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 944ea4bb8a9cd4244152a4a4fecd0485fabc2ad0"
 bgruening parents: 
0diff
changeset | 17 | 
| 13 
d44de092fef3
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 1fe240ef0064a1a4a66d9be1ccace53824280b75"
 bgruening parents: 
12diff
changeset | 18 | 
| 0 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 19 def parse_command_line(): | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 20 parser = argparse.ArgumentParser() | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 21 parser.add_argument('-i', '--infile', required=True, help='Molecule file.') | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 22 parser.add_argument('--iformat', help='Input format.') | 
| 13 
d44de092fef3
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 1fe240ef0064a1a4a66d9be1ccace53824280b75"
 bgruening parents: 
12diff
changeset | 23 parser.add_argument('--fastsearch-index', dest="fastsearch_index", required=True, | 
| 
d44de092fef3
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 1fe240ef0064a1a4a66d9be1ccace53824280b75"
 bgruening parents: 
12diff
changeset | 24 help='Path to the openbabel fastsearch index.') | 
| 0 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 25 parser.add_argument('-o', '--outfile', required=True, help='Path to the output file.') | 
| 13 
d44de092fef3
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 1fe240ef0064a1a4a66d9be1ccace53824280b75"
 bgruening parents: 
12diff
changeset | 26 parser.add_argument('--oformat', default='smi', help='Output file format') | 
| 
d44de092fef3
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 1fe240ef0064a1a4a66d9be1ccace53824280b75"
 bgruening parents: 
12diff
changeset | 27 parser.add_argument("--max-candidates", dest="max_candidates", type=int, default=4000, | 
| 
d44de092fef3
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 1fe240ef0064a1a4a66d9be1ccace53824280b75"
 bgruening parents: 
12diff
changeset | 28 help="The maximum number of candidates.") | 
| 
d44de092fef3
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 1fe240ef0064a1a4a66d9be1ccace53824280b75"
 bgruening parents: 
12diff
changeset | 29 parser.add_argument('-p', '--processors', type=int, | 
| 
d44de092fef3
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 1fe240ef0064a1a4a66d9be1ccace53824280b75"
 bgruening parents: 
12diff
changeset | 30 default=multiprocessing.cpu_count()) | 
| 0 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 31 return parser.parse_args() | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 32 | 
| 13 
d44de092fef3
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 1fe240ef0064a1a4a66d9be1ccace53824280b75"
 bgruening parents: 
12diff
changeset | 33 | 
| 0 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 34 results = list() | 
| 13 
d44de092fef3
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 1fe240ef0064a1a4a66d9be1ccace53824280b75"
 bgruening parents: 
12diff
changeset | 35 | 
| 
d44de092fef3
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 1fe240ef0064a1a4a66d9be1ccace53824280b75"
 bgruening parents: 
12diff
changeset | 36 | 
| 0 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 37 def mp_callback(res): | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 38 results.append(res) | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 39 | 
| 13 
d44de092fef3
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 1fe240ef0064a1a4a66d9be1ccace53824280b75"
 bgruening parents: 
12diff
changeset | 40 | 
| 
d44de092fef3
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 1fe240ef0064a1a4a66d9be1ccace53824280b75"
 bgruening parents: 
12diff
changeset | 41 def mp_helper(query, args): | 
| 0 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 42 """ | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 43 Helper function for multiprocessing. | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 44 That function is a wrapper around the following command: | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 45 obabel file.fs -s"smarts" -Ooutfile.smi -al 999999999 | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 46 """ | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 47 | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 48 if args.oformat == 'names': | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 49 opts = '-osmi -xt' | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 50 else: | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 51 opts = '-o%s' % args.oformat | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 52 | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 53 tmp = tempfile.NamedTemporaryFile(delete=False) | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 54 cmd = 'obabel -ifs %s -O %s %s -s%s -al %s' % (args.fastsearch_index, tmp.name, opts, query, args.max_candidates) | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 55 | 
| 13 
d44de092fef3
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 1fe240ef0064a1a4a66d9be1ccace53824280b75"
 bgruening parents: 
12diff
changeset | 56 child = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE) | 
| 0 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 57 | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 58 stdout, stderr = child.communicate() | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 59 return_code = child.returncode | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 60 | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 61 if return_code: | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 62 sys.stdout.write(stdout) | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 63 sys.stderr.write(stderr) | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 64 sys.stderr.write("Return error code %i from command:\n" % return_code) | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 65 sys.stderr.write("%s\n" % cmd) | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 66 else: | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 67 sys.stdout.write(stdout) | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 68 sys.stdout.write(stderr) | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 69 return (tmp.name, query) | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 70 | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 71 | 
| 13 
d44de092fef3
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 1fe240ef0064a1a4a66d9be1ccace53824280b75"
 bgruening parents: 
12diff
changeset | 72 def get_smiles_or_smarts(args): | 
| 0 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 73 """ | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 74 Wrapper to retrieve a striped SMILES or SMARTS string from different input formats. | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 75 """ | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 76 if args.iformat in ['smi', 'text', 'tabular']: | 
| 13 
d44de092fef3
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 1fe240ef0064a1a4a66d9be1ccace53824280b75"
 bgruening parents: 
12diff
changeset | 77 with open(args.infile) as text_file: | 
| 0 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 78 for line in text_file: | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 79 yield line.split('\t')[0].strip() | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 80 else: | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 81 # inchi or sdf files | 
| 13 
d44de092fef3
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 1fe240ef0064a1a4a66d9be1ccace53824280b75"
 bgruening parents: 
12diff
changeset | 82 for mol in pybel.readfile(args.iformat, args.infile): | 
| 0 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 83 yield mol.write('smiles').split('\t')[0] | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 84 | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 85 | 
| 13 
d44de092fef3
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 1fe240ef0064a1a4a66d9be1ccace53824280b75"
 bgruening parents: 
12diff
changeset | 86 def substructure_search(args): | 
| 
d44de092fef3
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 1fe240ef0064a1a4a66d9be1ccace53824280b75"
 bgruening parents: 
12diff
changeset | 87 pool = multiprocessing.Pool(args.processors) | 
| 
d44de092fef3
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 1fe240ef0064a1a4a66d9be1ccace53824280b75"
 bgruening parents: 
12diff
changeset | 88 for query in get_smiles_or_smarts(args): | 
| 0 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 89 pool.apply_async(mp_helper, args=(query, args), callback=mp_callback) | 
| 13 
d44de092fef3
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 1fe240ef0064a1a4a66d9be1ccace53824280b75"
 bgruening parents: 
12diff
changeset | 90 # mp_callback(mp_helper(query, args)) | 
| 0 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 91 pool.close() | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 92 pool.join() | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 93 | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 94 if args.oformat == 'names': | 
| 13 
d44de092fef3
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 1fe240ef0064a1a4a66d9be1ccace53824280b75"
 bgruening parents: 
12diff
changeset | 95 out_handle = open(args.outfile, 'w') | 
| 0 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 96 for result_file, query in results: | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 97 with open(result_file) as res_handle: | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 98 for line in res_handle: | 
| 13 
d44de092fef3
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 1fe240ef0064a1a4a66d9be1ccace53824280b75"
 bgruening parents: 
12diff
changeset | 99 out_handle.write('%s\t%s\n' % (line.strip(), query)) | 
| 
d44de092fef3
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 1fe240ef0064a1a4a66d9be1ccace53824280b75"
 bgruening parents: 
12diff
changeset | 100 os.remove(result_file) | 
| 0 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 101 out_handle.close() | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 102 else: | 
| 13 
d44de092fef3
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 1fe240ef0064a1a4a66d9be1ccace53824280b75"
 bgruening parents: 
12diff
changeset | 103 out_handle = open(args.outfile, 'wb') | 
| 0 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 104 for result_file, query in results: | 
| 13 
d44de092fef3
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 1fe240ef0064a1a4a66d9be1ccace53824280b75"
 bgruening parents: 
12diff
changeset | 105 res_handle = open(result_file, 'rb') | 
| 
d44de092fef3
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 1fe240ef0064a1a4a66d9be1ccace53824280b75"
 bgruening parents: 
12diff
changeset | 106 shutil.copyfileobj(res_handle, out_handle) | 
| 0 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 107 res_handle.close() | 
| 13 
d44de092fef3
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 1fe240ef0064a1a4a66d9be1ccace53824280b75"
 bgruening parents: 
12diff
changeset | 108 os.remove(result_file) | 
| 0 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 109 out_handle.close() | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 110 | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 111 | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 112 def __main__(): | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 113 """ | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 114 Multiprocessing Open Babel Substructure Search. | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 115 """ | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 116 args = parse_command_line() | 
| 13 
d44de092fef3
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 1fe240ef0064a1a4a66d9be1ccace53824280b75"
 bgruening parents: 
12diff
changeset | 117 substructure_search(args) | 
| 
d44de092fef3
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 1fe240ef0064a1a4a66d9be1ccace53824280b75"
 bgruening parents: 
12diff
changeset | 118 | 
| 0 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 119 | 
| 13 
d44de092fef3
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 1fe240ef0064a1a4a66d9be1ccace53824280b75"
 bgruening parents: 
12diff
changeset | 120 if __name__ == "__main__": | 
| 0 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 121 __main__() | 
