Mercurial > repos > bgruening > openbabel
comparison subsearch/subsearch.py @ 7:12822efbd4a4
Uploaded
author | bgruening |
---|---|
date | Sat, 11 May 2013 17:11:45 -0400 |
parents | 6493d130f018 |
children |
comparison
equal
deleted
inserted
replaced
6:9d1f982555ba | 7:12822efbd4a4 |
---|---|
15 import shutil | 15 import shutil |
16 | 16 |
17 def parse_command_line(): | 17 def parse_command_line(): |
18 parser = argparse.ArgumentParser() | 18 parser = argparse.ArgumentParser() |
19 parser.add_argument('-i', '--infile', required=True, help='Molecule file.') | 19 parser.add_argument('-i', '--infile', required=True, help='Molecule file.') |
20 parser.add_argument('--iformat', help='Input format.') | |
20 parser.add_argument('--fastsearch-index', dest="fastsearch_index", | 21 parser.add_argument('--fastsearch-index', dest="fastsearch_index", |
21 required=True, help='Path to the openbabel fastsearch index.') | 22 required=True, help='Path to the openbabel fastsearch index.') |
22 parser.add_argument('-o', '--outfile', required=True, help='Path to the output file.') | 23 parser.add_argument('-o', '--outfile', required=True, help='Path to the output file.') |
23 parser.add_argument('--oformat', | 24 parser.add_argument('--oformat', |
24 default='smi', help='Output file format') | 25 default='smi', help='Output file format') |
43 opts = '-osmi -xt' | 44 opts = '-osmi -xt' |
44 else: | 45 else: |
45 opts = '-o%s' % args.oformat | 46 opts = '-o%s' % args.oformat |
46 | 47 |
47 tmp = tempfile.NamedTemporaryFile(delete=False) | 48 tmp = tempfile.NamedTemporaryFile(delete=False) |
48 cmd = 'obabel %s -O %s %s -ifs -s%s -al %s' % (args.fastsearch_index, tmp.name, opts, query, args.max_candidates) | 49 cmd = 'obabel -ifs %s -O %s %s -s%s -al %s' % (args.fastsearch_index, tmp.name, opts, query, args.max_candidates) |
49 | 50 |
50 child = subprocess.Popen(cmd.split(), | 51 child = subprocess.Popen(cmd.split(), |
51 stdout=subprocess.PIPE, stderr=subprocess.PIPE) | 52 stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
52 | 53 |
53 stdout, stderr = child.communicate() | 54 stdout, stderr = child.communicate() |
62 sys.stdout.write(stdout) | 63 sys.stdout.write(stdout) |
63 sys.stdout.write(stderr) | 64 sys.stdout.write(stderr) |
64 return (tmp.name, query) | 65 return (tmp.name, query) |
65 | 66 |
66 | 67 |
68 def get_smiles_or_smarts( args ): | |
69 """ | |
70 Wrapper to retrieve a striped SMILES or SMARTS string from different input formats. | |
71 """ | |
72 if args.iformat in ['smi', 'text', 'tabular']: | |
73 with open( args.infile ) as text_file: | |
74 for line in text_file: | |
75 yield line.split('\t')[0].strip() | |
76 else: | |
77 # inchi or sdf files | |
78 for mol in pybel.readfile( args.iformat, args.infile ): | |
79 yield mol.wrtie('smiles').split('\t')[0] | |
80 | |
67 def substructure_search( args ): | 81 def substructure_search( args ): |
68 | 82 |
69 pool = multiprocessing.Pool( args.processors ) | 83 pool = multiprocessing.Pool( args.processors ) |
70 for query in open( args.infile ): | 84 for query in get_smiles_or_smarts( args ): |
71 pool.apply_async(mp_helper, args=(query.strip(), args), callback=mp_callback) | 85 pool.apply_async(mp_helper, args=(query, args), callback=mp_callback) |
72 #mp_callback( mp_helper(query.strip(), args) ) | 86 #mp_callback( mp_helper(query, args) ) |
73 pool.close() | 87 pool.close() |
74 pool.join() | 88 pool.join() |
75 | 89 |
76 if args.oformat == 'names': | 90 if args.oformat == 'names': |
77 out_handle = open( args.outfile, 'w' ) | 91 out_handle = open( args.outfile, 'w' ) |