diff modify/change_title_to_metadata_value.py @ 0:527ecd2fc500 draft

Uploaded
author bgruening
date Thu, 15 Aug 2013 03:25:06 -0400
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/modify/change_title_to_metadata_value.py	Thu Aug 15 03:25:06 2013 -0400
@@ -0,0 +1,42 @@
+#!/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, 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()
+