comparison change_title_to_metadata_value.py @ 15:9adf3fae2771 draft default tip

planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit d9c51279c061a1da948a2582d5b502ca7573adbf
author bgruening
date Thu, 15 Aug 2024 11:04:41 +0000
parents bd678d7db2ae
children
comparison
equal deleted inserted replaced
14:c19608db6b15 15:9adf3fae2771
9 import argparse 9 import argparse
10 import random 10 import random
11 import string 11 import string
12 12
13 from openbabel import openbabel, pybel 13 from openbabel import openbabel, pybel
14
14 openbabel.obErrorLog.StopLogging() 15 openbabel.obErrorLog.StopLogging()
15 16
16 17
17 def main(): 18 def main():
18 parser = argparse.ArgumentParser( 19 parser = argparse.ArgumentParser(
19 description="Change the title from a molecule file to metadata \ 20 description="Change the title from a molecule file to metadata \
20 value of a given-id of the same molecule file.", 21 value of a given-id of the same molecule file.",
21 ) 22 )
22 parser.add_argument('--infile', '-i', required=True, 23 parser.add_argument("--infile", "-i", required=True, help="path to the input file")
23 help="path to the input file") 24 parser.add_argument(
24 parser.add_argument('--outfile', '-o', required=True, 25 "--outfile", "-o", required=True, help="path to the output file"
25 help="path to the output file") 26 )
26 parser.add_argument('--key', '-k', required=True, 27 parser.add_argument(
27 help="the metadata key from the sdf file which should inlcude the new title") 28 "--key",
28 parser.add_argument('--random', '-r', action="store_true", 29 "-k",
29 help="Add random suffix to the title.") 30 required=True,
31 help="the metadata key from the sdf file which should inlcude the new title",
32 )
33 parser.add_argument(
34 "--random", "-r", action="store_true", help="Add random suffix to the title."
35 )
30 36
31 args = parser.parse_args() 37 args = parser.parse_args()
32 38
33 output = pybel.Outputfile("sdf", args.outfile, overwrite=True) 39 output = pybel.Outputfile("sdf", args.outfile, overwrite=True)
34 for mol in pybel.readfile("sdf", args.infile): 40 for mol in pybel.readfile("sdf", args.infile):
35 if args.key in mol.data: 41 if args.key in mol.data:
36 mol.title = mol.data[args.key] 42 mol.title = mol.data[args.key]
37 if args.random: 43 if args.random:
38 suffix = ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(13)) 44 suffix = "".join(
39 mol.title += '__%s' % suffix 45 random.choice(string.ascii_lowercase + string.digits)
46 for _ in range(13)
47 )
48 mol.title += "__%s" % suffix
40 output.write(mol) 49 output.write(mol)
41 50
42 output.close() 51 output.close()
43 52
44 53