Depicting CSV or SDF in PDF


You want to depict molecules along with their associated data read from a CSV file in a multi-page PDF file. See example in drugs.pdf and in Table 1.

Table 1. Example of depiction of CSV in PDF (The pages are reduced here for visualization convenience)

page 1

page 2

../_images/csv2pdf-01.png ../_images/csv2pdf-02.png


Difficulty Level

../_images/chilly.png ../_images/chilly.png


The CSV file format is a text file format containing comma-separated values. In OEChem TK this file format is implemented to enable data exchange with a wide variety of other software. Each line of a CSV file stores data for a molecule that is represented by a SMILES string.

See also

When reading a CSV file, the fields of the file are attached to each molecule as SD data. This data can be accessed by the OEGetSDDataIter function that returns an iterator over all the SD data (tag - value) pairs of a molecule. The CollectDataTags function iterates over a list of molecules and returns the unique tags of the data attached to the molecules.

1def CollectDataTags(mollist):
3    tags = []
4    for mol in mollist:
5        for dp in oechem.OEGetSDDataIter(mol):
6            if not dp.GetTag() in tags:
7                tags.append(dp.GetTag())
9    return tags

The DepictMoleculesWithData function takes a list of molecules read from a CSV file along with the data tags returned by the CollectDataTags function. Each molecule and its corresponding data is rendered into adjacent cells of an OEReport object (lines 3-16). The OEReport class is a layout manager allowing generation of multi-page images in a convenient way. After rendering the molecules, the input filename is rendered into page headers (lines 22-27) while the page number is rendered at the bottom of each page (lines 31-36).

 1def DepictMoleculesWithData(report, mollist, iname, tags, opts):
 3    for mol in mollist:
 5        # render molecule
 7        cell = report.NewCell()
 8        oedepict.OEPrepareDepiction(mol)
 9        disp = oedepict.OE2DMolDisplay(mol, opts)
10        oedepict.OERenderMolecule(cell, disp)
11        oedepict.OEDrawCurvedBorder(cell, oedepict.OELightGreyPen, 10.0)
13        # render corresponding data
15        cell = report.NewCell()
16        RenderData(cell, mol, tags)
18    # add input filnename to headers
20    headerfont = oedepict.OEFont(oedepict.OEFontFamily_Default, oedepict.OEFontStyle_Default,
21                                 12, oedepict.OEAlignment_Center, oechem.OEBlack)
22    headerpos = oedepict.OE2DPoint(report.GetHeaderWidth() / 2.0, report.GetHeaderHeight() / 2.0)
24    for header in report.GetHeaders():
25        header.DrawText(headerpos, iname, headerfont)
27    # add page number to footers
29    footerfont = oedepict.OEFont(oedepict.OEFontFamily_Default, oedepict.OEFontStyle_Default,
30                                 12, oedepict.OEAlignment_Center, oechem.OEBlack)
31    footerpos = oedepict.OE2DPoint(report.GetFooterWidth() / 2.0, report.GetFooterHeight() / 2.0)
33    for pageidx, footer in enumerate(report.GetFooters()):
34        footer.DrawText(footerpos, "- %d -" % (pageidx + 1), footerfont)

The RenderData function shows how ease is to render the (tag - value) tuples using the OEImageTable class.

 1def RenderData(image, mol, tags):
 3    data = []
 4    for tag in tags:
 5        value = "N/A"
 6        if oechem.OEHasSDData(mol, tag):
 7            value = oechem.OEGetSDData(mol, tag)
 8        data.append((tag, value))
10    nrdata = len(data)
12    tableopts = oedepict.OEImageTableOptions(nrdata, 2, oedepict.OEImageTableStyle_LightBlue)
13    tableopts.SetColumnWidths([10, 20])
14    tableopts.SetMargins(2.0)
15    tableopts.SetHeader(False)
16    tableopts.SetStubColumn(True)
17    table = oedepict.OEImageTable(image, tableopts)
19    for row, (tag, value) in enumerate(data):
20        cell = table.GetCell(row + 1, 1)
21        table.DrawText(cell, tag + ":")
22        cell = table.GetBodyCell(row + 1, 1)
23        table.DrawText(cell, value)

Download code and drugs.csv supporting data file


Running the above command will generate the drugs.pdf multi-page pdf file.

prompt > python3 drugs.csv drugs.pdf


Reading the columns of an CSV file into SD data fields, means that the OEChem TK provides a meta-data interchange between sdf files and CSV files. Consequently, the same Python script can be used to generate a pdf file reading an sdf file.


After downloading drugs.sdf supporting data file, the above command will generate the same drugs.pdf multi-page pdf file (apart from the input filename on each page header).

prompt > python3 drugs.sdf drugs.pdf

See also in OEChem TK manual



See also in OEDepict TK manual