view change_title_to_metadata_value.py @ 2:30a6c37e95ac draft

planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 78ac0521d7df684e96c1b9c1ba2a17b02e681608
author bgruening
date Sat, 20 May 2017 20:04:59 -0400
parents 98e12cc1f3a8
children fc199b60875d
line wrap: on
line source

#!/usr/bin/env python
# -*- coding: UTF-8 -*-

"""
    Change the title from a molecule file to metadata
    value of a given-id of the same molecule file.
"""

import os
import sys
import argparse
import openbabel
openbabel.obErrorLog.StopLogging()
import pybel


def main():
    parser = argparse.ArgumentParser(
        description="Change the title from a molecule file to metadata \
value of a given-id of the same molecule file.",
    )
    parser.add_argument('--infile', '-i', 
        required=True, help="path to the input file")
    parser.add_argument('--outfile', '-o', 
        required=True, help="path to the output file")
    parser.add_argument('--key', '-k',
        required=True, help="the metadata key from the sdf file which should inlcude the new title")

    args = parser.parse_args()

    output = pybel.Outputfile("sdf", args.outfile, overwrite=True)

    for mol in pybel.readfile("sdf", args.infile):
        if args.key in mol.data:
            mol.title = mol.data[args.key]
        output.write( mol )

    output.close()


if __name__ == "__main__":
    main()