Mercurial > repos > bgruening > openbabel_structure_distance_finder
annotate ob_genProp.py @ 4:2c5c7da26e08 draft
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 327c29cc43f56d7067ab9fa51323ea31951db98b"
| author | bgruening | 
|---|---|
| date | Tue, 10 Nov 2020 20:40:06 +0000 | 
| parents | 49242402887b | 
| children | 8302ab092300 | 
| rev | line source | 
|---|---|
| 0 
c066b5accacf
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 6c84abdd07f292048bf2194073e2e938e94158c4"
 bgruening parents: diff
changeset | 1 #!/usr/bin/env python | 
| 
c066b5accacf
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 6c84abdd07f292048bf2194073e2e938e94158c4"
 bgruening parents: diff
changeset | 2 """ | 
| 
c066b5accacf
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 6c84abdd07f292048bf2194073e2e938e94158c4"
 bgruening parents: diff
changeset | 3 Input: Molecular input file. | 
| 
c066b5accacf
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 6c84abdd07f292048bf2194073e2e938e94158c4"
 bgruening parents: diff
changeset | 4 Output: Physico-chemical properties are computed and stored as metadata in the sdf output file. | 
| 
c066b5accacf
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 6c84abdd07f292048bf2194073e2e938e94158c4"
 bgruening parents: diff
changeset | 5 Copyright 2012, Bjoern Gruening and Xavier Lucas | 
| 
c066b5accacf
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 6c84abdd07f292048bf2194073e2e938e94158c4"
 bgruening parents: diff
changeset | 6 """ | 
| 
c066b5accacf
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 6c84abdd07f292048bf2194073e2e938e94158c4"
 bgruening parents: diff
changeset | 7 import argparse | 
| 3 
49242402887b
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 1fe240ef0064a1a4a66d9be1ccace53824280b75"
 bgruening parents: 
2diff
changeset | 8 import sys | 
| 
49242402887b
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 1fe240ef0064a1a4a66d9be1ccace53824280b75"
 bgruening parents: 
2diff
changeset | 9 | 
| 
49242402887b
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 1fe240ef0064a1a4a66d9be1ccace53824280b75"
 bgruening parents: 
2diff
changeset | 10 import cheminfolib | 
| 0 
c066b5accacf
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 6c84abdd07f292048bf2194073e2e938e94158c4"
 bgruening parents: diff
changeset | 11 import openbabel | 
| 3 
49242402887b
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 1fe240ef0064a1a4a66d9be1ccace53824280b75"
 bgruening parents: 
2diff
changeset | 12 from openbabel import pybel | 
| 0 
c066b5accacf
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 6c84abdd07f292048bf2194073e2e938e94158c4"
 bgruening parents: diff
changeset | 13 openbabel.obErrorLog.StopLogging() | 
| 
c066b5accacf
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 6c84abdd07f292048bf2194073e2e938e94158c4"
 bgruening parents: diff
changeset | 14 | 
| 
c066b5accacf
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 6c84abdd07f292048bf2194073e2e938e94158c4"
 bgruening parents: diff
changeset | 15 | 
| 
c066b5accacf
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 6c84abdd07f292048bf2194073e2e938e94158c4"
 bgruening parents: diff
changeset | 16 def parse_command_line(argv): | 
| 
c066b5accacf
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 6c84abdd07f292048bf2194073e2e938e94158c4"
 bgruening parents: diff
changeset | 17 parser = argparse.ArgumentParser() | 
| 3 
49242402887b
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 1fe240ef0064a1a4a66d9be1ccace53824280b75"
 bgruening parents: 
2diff
changeset | 18 parser.add_argument('--iformat', default='sdf', help='input file format') | 
| 0 
c066b5accacf
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 6c84abdd07f292048bf2194073e2e938e94158c4"
 bgruening parents: diff
changeset | 19 parser.add_argument('-i', '--input', required=True, help='input file name') | 
| 3 
49242402887b
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 1fe240ef0064a1a4a66d9be1ccace53824280b75"
 bgruening parents: 
2diff
changeset | 20 parser.add_argument('--oformat', default='sdf', choices=['sdf', 'table'], help='output file format') | 
| 0 
c066b5accacf
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 6c84abdd07f292048bf2194073e2e938e94158c4"
 bgruening parents: diff
changeset | 21 parser.add_argument('--header', type=bool, help='Include the header as the first line of the output table') | 
| 
c066b5accacf
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 6c84abdd07f292048bf2194073e2e938e94158c4"
 bgruening parents: diff
changeset | 22 parser.add_argument('-o', '--output', required=True, help='output file name') | 
| 
c066b5accacf
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 6c84abdd07f292048bf2194073e2e938e94158c4"
 bgruening parents: diff
changeset | 23 return parser.parse_args() | 
| 
c066b5accacf
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 6c84abdd07f292048bf2194073e2e938e94158c4"
 bgruening parents: diff
changeset | 24 | 
| 3 
49242402887b
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 1fe240ef0064a1a4a66d9be1ccace53824280b75"
 bgruening parents: 
2diff
changeset | 25 | 
| 0 
c066b5accacf
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 6c84abdd07f292048bf2194073e2e938e94158c4"
 bgruening parents: diff
changeset | 26 def compute_properties(args): | 
| 
c066b5accacf
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 6c84abdd07f292048bf2194073e2e938e94158c4"
 bgruening parents: diff
changeset | 27 if args.oformat == 'sdf': | 
| 
c066b5accacf
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 6c84abdd07f292048bf2194073e2e938e94158c4"
 bgruening parents: diff
changeset | 28 outfile = pybel.Outputfile(args.oformat, args.output, overwrite=True) | 
| 
c066b5accacf
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 6c84abdd07f292048bf2194073e2e938e94158c4"
 bgruening parents: diff
changeset | 29 else: | 
| 
c066b5accacf
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 6c84abdd07f292048bf2194073e2e938e94158c4"
 bgruening parents: diff
changeset | 30 outfile = open(args.output, 'w') | 
| 
c066b5accacf
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 6c84abdd07f292048bf2194073e2e938e94158c4"
 bgruening parents: diff
changeset | 31 if args.header: | 
| 
c066b5accacf
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 6c84abdd07f292048bf2194073e2e938e94158c4"
 bgruening parents: diff
changeset | 32 mol = next(pybel.readfile(args.iformat, args.input)) | 
| 
c066b5accacf
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 6c84abdd07f292048bf2194073e2e938e94158c4"
 bgruening parents: diff
changeset | 33 metadata = cheminfolib.get_properties_ext(mol) | 
| 3 
49242402887b
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 1fe240ef0064a1a4a66d9be1ccace53824280b75"
 bgruening parents: 
2diff
changeset | 34 outfile.write('%s\n' % '\t'.join([cheminfolib.ColumnNames[key] for key in metadata])) | 
| 0 
c066b5accacf
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 6c84abdd07f292048bf2194073e2e938e94158c4"
 bgruening parents: diff
changeset | 35 | 
| 
c066b5accacf
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 6c84abdd07f292048bf2194073e2e938e94158c4"
 bgruening parents: diff
changeset | 36 for mol in pybel.readfile(args.iformat, args.input): | 
| 
c066b5accacf
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 6c84abdd07f292048bf2194073e2e938e94158c4"
 bgruening parents: diff
changeset | 37 if mol.OBMol.NumHvyAtoms() > 5: | 
| 
c066b5accacf
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 6c84abdd07f292048bf2194073e2e938e94158c4"
 bgruening parents: diff
changeset | 38 metadata = cheminfolib.get_properties_ext(mol) | 
| 
c066b5accacf
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 6c84abdd07f292048bf2194073e2e938e94158c4"
 bgruening parents: diff
changeset | 39 if args.oformat == 'sdf': | 
| 3 
49242402887b
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 1fe240ef0064a1a4a66d9be1ccace53824280b75"
 bgruening parents: 
2diff
changeset | 40 [mol.data.update({cheminfolib.ColumnNames[key]: metadata[key]}) for key in metadata] | 
| 0 
c066b5accacf
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 6c84abdd07f292048bf2194073e2e938e94158c4"
 bgruening parents: diff
changeset | 41 outfile.write(mol) | 
| 
c066b5accacf
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 6c84abdd07f292048bf2194073e2e938e94158c4"
 bgruening parents: diff
changeset | 42 else: | 
| 3 
49242402887b
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 1fe240ef0064a1a4a66d9be1ccace53824280b75"
 bgruening parents: 
2diff
changeset | 43 outfile.write('%s\n' % ('\t'.join([str(metadata[key]) for key in metadata]))) | 
| 0 
c066b5accacf
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 6c84abdd07f292048bf2194073e2e938e94158c4"
 bgruening parents: diff
changeset | 44 outfile.close() | 
| 
c066b5accacf
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 6c84abdd07f292048bf2194073e2e938e94158c4"
 bgruening parents: diff
changeset | 45 | 
| 3 
49242402887b
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 1fe240ef0064a1a4a66d9be1ccace53824280b75"
 bgruening parents: 
2diff
changeset | 46 | 
| 0 
c066b5accacf
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 6c84abdd07f292048bf2194073e2e938e94158c4"
 bgruening parents: diff
changeset | 47 def __main__(): | 
| 
c066b5accacf
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 6c84abdd07f292048bf2194073e2e938e94158c4"
 bgruening parents: diff
changeset | 48 """ | 
| 
c066b5accacf
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 6c84abdd07f292048bf2194073e2e938e94158c4"
 bgruening parents: diff
changeset | 49 Physico-chemical properties are computed and stored as metadata in the sdf output file | 
| 
c066b5accacf
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 6c84abdd07f292048bf2194073e2e938e94158c4"
 bgruening parents: diff
changeset | 50 """ | 
| 
c066b5accacf
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 6c84abdd07f292048bf2194073e2e938e94158c4"
 bgruening parents: diff
changeset | 51 args = parse_command_line(sys.argv) | 
| 
c066b5accacf
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 6c84abdd07f292048bf2194073e2e938e94158c4"
 bgruening parents: diff
changeset | 52 compute_properties(args) | 
| 
c066b5accacf
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 6c84abdd07f292048bf2194073e2e938e94158c4"
 bgruening parents: diff
changeset | 53 | 
| 3 
49242402887b
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 1fe240ef0064a1a4a66d9be1ccace53824280b75"
 bgruening parents: 
2diff
changeset | 54 | 
| 
49242402887b
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 1fe240ef0064a1a4a66d9be1ccace53824280b75"
 bgruening parents: 
2diff
changeset | 55 if __name__ == "__main__": | 
| 0 
c066b5accacf
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 6c84abdd07f292048bf2194073e2e938e94158c4"
 bgruening parents: diff
changeset | 56 __main__() | 
