annotate change_title_to_metadata_value.py @ 6:5486f7a2b0cb draft default tip

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