comparison change_title_to_metadata_value.py @ 10:34fb5da28776 draft

"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 6c84abdd07f292048bf2194073e2e938e94158c4"
author bgruening
date Wed, 25 Mar 2020 16:44:49 -0400
parents c9bee3591df9
children 354c048550f7
comparison
equal deleted inserted replaced
9:76bbd52058ab 10:34fb5da28776
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