Mercurial > repos > bgruening > openbabel_remduplicates
annotate change_title_to_metadata_value.py @ 10:e185dfd1dfea draft
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 6c84abdd07f292048bf2194073e2e938e94158c4"
| author | bgruening | 
|---|---|
| date | Wed, 25 Mar 2020 20:38:05 +0000 | 
| parents | 112341e4fc94 | 
| children | 8c4a4e9e173c | 
| rev | line source | 
|---|---|
| 0 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 1 #!/usr/bin/env python | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 2 # -*- coding: UTF-8 -*- | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 3 | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 4 """ | 
| 
112341e4fc94
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 | 
| 
112341e4fc94
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. | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 7 """ | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 8 | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 9 import os | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 10 import sys | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 11 import argparse | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 12 import openbabel | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 13 openbabel.obErrorLog.StopLogging() | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 14 import pybel | 
| 10 
e185dfd1dfea
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 6c84abdd07f292048bf2194073e2e938e94158c4"
 bgruening parents: 
0diff
changeset | 15 import random | 
| 
e185dfd1dfea
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 6c84abdd07f292048bf2194073e2e938e94158c4"
 bgruening parents: 
0diff
changeset | 16 import string | 
| 0 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 17 | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 18 | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 19 def main(): | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 20 parser = argparse.ArgumentParser( | 
| 
112341e4fc94
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 \ | 
| 
112341e4fc94
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.", | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 23 ) | 
| 
112341e4fc94
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', | 
| 
112341e4fc94
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") | 
| 
112341e4fc94
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', | 
| 
112341e4fc94
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") | 
| 
112341e4fc94
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', | 
| 
112341e4fc94
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 
e185dfd1dfea
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 6c84abdd07f292048bf2194073e2e938e94158c4"
 bgruening parents: 
0diff
changeset | 30 parser.add_argument('--random', '-r', | 
| 
e185dfd1dfea
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 6c84abdd07f292048bf2194073e2e938e94158c4"
 bgruening parents: 
0diff
changeset | 31 action="store_true", help="Add random suffix to the title.") | 
| 0 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 32 | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 33 args = parser.parse_args() | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 34 | 
| 
112341e4fc94
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) | 
| 
112341e4fc94
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): | 
| 
112341e4fc94
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: | 
| 
112341e4fc94
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 
e185dfd1dfea
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 6c84abdd07f292048bf2194073e2e938e94158c4"
 bgruening parents: 
0diff
changeset | 39 if args.random: | 
| 
e185dfd1dfea
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 6c84abdd07f292048bf2194073e2e938e94158c4"
 bgruening parents: 
0diff
changeset | 40 suffix = ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(13)) | 
| 
e185dfd1dfea
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 6c84abdd07f292048bf2194073e2e938e94158c4"
 bgruening parents: 
0diff
changeset | 41 mol.title += '__%s' % suffix | 
| 0 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 42 output.write( mol ) | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 43 | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 44 output.close() | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 45 | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 46 | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 47 if __name__ == "__main__": | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 48 main() | 
| 
112341e4fc94
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
 bgruening parents: diff
changeset | 49 | 
