annotate modify/change_title_to_metadata_value.py @ 2:125da3a296ca draft default tip

Uploaded
author bgruening
date Wed, 15 Jul 2015 12:13:08 -0400
parents 527ecd2fc500
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
1 #!/usr/bin/env python
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
2 # -*- coding: UTF-8 -*-
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
3
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
4 """
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
5 Change the title from a molecule file to metadata
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
6 value of a given-id of the same molecule file.
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
7 """
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
8
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
9 import os, sys
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
10 import argparse
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
11 import openbabel
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
12 openbabel.obErrorLog.StopLogging()
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
13 import pybel
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
14
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
15
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
16 def main():
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
17 parser = argparse.ArgumentParser(
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
18 description="Change the title from a molecule file to metadata \
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
19 value of a given-id of the same molecule file.",
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
20 )
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
21 parser.add_argument('--infile', '-i',
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
22 required=True, help="path to the input file")
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
23 parser.add_argument('--outfile', '-o',
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
24 required=True, help="path to the output file")
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
25 parser.add_argument('--key', '-k',
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
26 required=True, help="the metadata key from the sdf file which should inlcude the new title")
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
27
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
28 args = parser.parse_args()
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
29
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
30 output = pybel.Outputfile("sdf", args.outfile, overwrite=True)
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
31
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
32 for mol in pybel.readfile("sdf", args.infile):
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
33 if args.key in mol.data:
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
34 mol.title = mol.data[args.key]
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
35 output.write( mol )
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
36
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
37 output.close()
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
38
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
39
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
40 if __name__ == "__main__":
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
41 main()
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
42