Mercurial > repos > bgruening > openbabel_genprop
comparison change_title_to_metadata_value.py @ 13:417845394cdf draft
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 1fe240ef0064a1a4a66d9be1ccace53824280b75"
author | bgruening |
---|---|
date | Mon, 19 Oct 2020 14:46:54 +0000 |
parents | a72ae2711a97 |
children | 50eaae9df8d3 |
comparison
equal
deleted
inserted
replaced
12:a72ae2711a97 | 13:417845394cdf |
---|---|
4 """ | 4 """ |
5 Change the title from a molecule file to metadata | 5 Change the title from a molecule file to metadata |
6 value of a given-id of the same molecule file. | 6 value of a given-id of the same molecule file. |
7 """ | 7 """ |
8 | 8 |
9 import os | |
10 import sys | |
11 import argparse | 9 import argparse |
12 import random | 10 import random |
13 import string | 11 import string |
14 | 12 |
15 | |
16 from openbabel import openbabel, pybel | 13 from openbabel import openbabel, pybel |
17 openbabel.obErrorLog.StopLogging() | 14 openbabel.obErrorLog.StopLogging() |
15 | |
18 | 16 |
19 def main(): | 17 def main(): |
20 parser = argparse.ArgumentParser( | 18 parser = argparse.ArgumentParser( |
21 description="Change the title from a molecule file to metadata \ | 19 description="Change the title from a molecule file to metadata \ |
22 value of a given-id of the same molecule file.", | 20 value of a given-id of the same molecule file.", |
23 ) | 21 ) |
24 parser.add_argument('--infile', '-i', | 22 parser.add_argument('--infile', '-i', required=True, |
25 required=True, help="path to the input file") | 23 help="path to the input file") |
26 parser.add_argument('--outfile', '-o', | 24 parser.add_argument('--outfile', '-o', required=True, |
27 required=True, help="path to the output file") | 25 help="path to the output file") |
28 parser.add_argument('--key', '-k', | 26 parser.add_argument('--key', '-k', required=True, |
29 required=True, help="the metadata key from the sdf file which should inlcude the new title") | 27 help="the metadata key from the sdf file which should inlcude the new title") |
30 parser.add_argument('--random', '-r', | 28 parser.add_argument('--random', '-r', action="store_true", |
31 action="store_true", help="Add random suffix to the title.") | 29 help="Add random suffix to the title.") |
32 | 30 |
33 args = parser.parse_args() | 31 args = parser.parse_args() |
34 | 32 |
35 output = pybel.Outputfile("sdf", args.outfile, overwrite=True) | 33 output = pybel.Outputfile("sdf", args.outfile, overwrite=True) |
36 for mol in pybel.readfile("sdf", args.infile): | 34 for mol in pybel.readfile("sdf", args.infile): |
37 if args.key in mol.data: | 35 if args.key in mol.data: |
38 mol.title = mol.data[args.key] | 36 mol.title = mol.data[args.key] |
39 if args.random: | 37 if args.random: |
40 suffix = ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(13)) | 38 suffix = ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(13)) |
41 mol.title += '__%s' % suffix | 39 mol.title += '__%s' % suffix |
42 output.write( mol ) | 40 output.write(mol) |
43 | 41 |
44 output.close() | 42 output.close() |
45 | 43 |
46 | 44 |
47 if __name__ == "__main__": | 45 if __name__ == "__main__": |
48 main() | 46 main() |
49 |