#!/usr/bin/env python3 # (C) 2017 OpenEye Scientific Software Inc. All rights reserved. # # TERMS FOR USE OF SAMPLE CODE The software below ("Sample Code") is # provided to current licensees or subscribers of OpenEye products or # SaaS offerings (each a "Customer"). # Customer is hereby permitted to use, copy, and modify the Sample Code, # subject to these terms. OpenEye claims no rights to Customer's # modifications. Modification of Sample Code is at Customer's sole and # exclusive risk. Sample Code may require Customer to have a then # current license or subscription to the applicable OpenEye offering. # THE SAMPLE CODE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, # EXPRESS OR IMPLIED. OPENEYE DISCLAIMS ALL WARRANTIES, INCLUDING, BUT # NOT LIMITED TO, WARRANTIES OF MERCHANTABILITY, FITNESS FOR A # PARTICULAR PURPOSE AND NONINFRINGEMENT. In no event shall OpenEye be # liable for any damages or liability in connection with the Sample Code # or its use. ############################################################################# # Converts a CSV or a SDF file into HTML ############################################################################# import sys from openeye import oechem from openeye import oedepict def main(argv=[__name__]): itf = oechem.OEInterface(InterfaceData) oedepict.OEConfigure2DMolDisplayOptions(itf, oedepict.OE2DMolDisplaySetup_AromaticStyle) if not oechem.OEParseCommandLine(itf, argv): return 1 iname = itf.GetString("-in") oname = itf.GetString("-out") # check input/output files ifs = oechem.oemolistream() if not ifs.open(iname): oechem.OEThrow.Fatal("Cannot open input file!") if ifs.GetFormat() not in [oechem.OEFormat_CSV, oechem.OEFormat_SDF]: oechem.OEThrow.Fatal("Input must be CSV or SDF file!") ext = oechem.OEGetFileExtension(oname) if ext != "html": oechem.OEThrow.Fatal("Output must be HTML format.") ofp = open(oname, "w") # import molecules mollist = [] for mol in ifs.GetOEGraphMols(): mollist.append(oechem.OEGraphMol(mol)) # setup depiction options width, height = 200, 200 opts = oedepict.OE2DMolDisplayOptions(width, height, oedepict.OEScale_AutoScale) oedepict.OESetup2DMolDisplayOptions(opts, itf) # collect data tags tags = CollectDataTags(mollist) # generate html file WriteHTMLFile(ofp, mollist, iname, tags, opts) return 0 def CollectDataTags(mollist): tags = [] for mol in mollist: for dp in oechem.OEGetSDDataIter(mol): if not dp.GetTag() in tags: tags.append(dp.GetTag()) return tags def WriteHTMLFile(ofp, mollist, iname, tags, opts): WriteHTMLHeader(ofp, iname, tags) for mol in mollist: WriteHTMLTableRow(ofp, mol, opts, tags) WriteHTMLFooter(ofp) def WriteHTMLTableRow(ofp, mol, opts, tags): ofp.write("
Molecule | ") # write data tags in table header for tag in tags: ofp.write("%s | " % tag) ofp.write("\n
---|