annotate change_title_to_metadata_value.py @ 10:fc199b60875d draft

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