comparison change_title_to_metadata_value.py @ 10:304324b4865d draft

"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit c4274133a07c323627e3ac5374502da9ecf669fe-dirty"
author bgruening
date Sat, 21 Mar 2020 12:55:05 +0000
parents acb6a13bda36
children 1c79ea18e7b5
comparison
equal deleted inserted replaced
9:2cf1f3b14f0f 10:304324b4865d
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