comparison change_title_to_metadata_value.py @ 10:976a5975b952 draft

"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 6c84abdd07f292048bf2194073e2e938e94158c4"
author bgruening
date Wed, 25 Mar 2020 16:45:37 -0400
parents ada6daa717d2
children 43167f164076
comparison
equal deleted inserted replaced
9:0691e5e97cd5 10:976a5975b952
10 import sys 10 import sys
11 import argparse 11 import argparse
12 import openbabel 12 import openbabel
13 openbabel.obErrorLog.StopLogging() 13 openbabel.obErrorLog.StopLogging()
14 import pybel 14 import pybel
15 import random
16 import string
15 17
16 18
17 def main(): 19 def main():
18 parser = argparse.ArgumentParser( 20 parser = argparse.ArgumentParser(
19 description="Change the title from a molecule file to metadata \ 21 description="Change the title from a molecule file to metadata \
23 required=True, help="path to the input file") 25 required=True, help="path to the input file")
24 parser.add_argument('--outfile', '-o', 26 parser.add_argument('--outfile', '-o',
25 required=True, help="path to the output file") 27 required=True, help="path to the output file")
26 parser.add_argument('--key', '-k', 28 parser.add_argument('--key', '-k',
27 required=True, help="the metadata key from the sdf file which should inlcude the new title") 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.")
28 32
29 args = parser.parse_args() 33 args = parser.parse_args()
30 34
31 output = pybel.Outputfile("sdf", args.outfile, overwrite=True) 35 output = pybel.Outputfile("sdf", args.outfile, overwrite=True)
32
33 for mol in pybel.readfile("sdf", args.infile): 36 for mol in pybel.readfile("sdf", args.infile):
34 if args.key in mol.data: 37 if args.key in mol.data:
35 mol.title = mol.data[args.key] 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
36 output.write( mol ) 42 output.write( mol )
37 43
38 output.close() 44 output.close()
39 45
40 46