comparison change_title_to_metadata_value.py @ 0:c066b5accacf draft

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