<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://wiki.docking.org/index.php?action=history&amp;feed=atom&amp;title=Create_ChemDraw_files_with_Python</id>
	<title>Create ChemDraw files with Python - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://wiki.docking.org/index.php?action=history&amp;feed=atom&amp;title=Create_ChemDraw_files_with_Python"/>
	<link rel="alternate" type="text/html" href="http://wiki.docking.org/index.php?title=Create_ChemDraw_files_with_Python&amp;action=history"/>
	<updated>2026-06-20T15:22:42Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.39.1</generator>
	<entry>
		<id>http://wiki.docking.org/index.php?title=Create_ChemDraw_files_with_Python&amp;diff=17004&amp;oldid=prev</id>
		<title>Iamkaant: Created page with &quot;== Complete Manual: Displaying Chemical Structures in ChemDraw Format Using pycdxml ==  === Overview ===  The pycdxml package enables platform-independent manipulation of ChemDraw files (CDX and CDXML formats) through Python. The CDXMLSlideGenerator module creates professional structure sheets by arranging molecules in grids with associated properties, outputting standard ChemDraw documents that can be further edited by users.  === Installation ===  Create a conda enviro...&quot;</title>
		<link rel="alternate" type="text/html" href="http://wiki.docking.org/index.php?title=Create_ChemDraw_files_with_Python&amp;diff=17004&amp;oldid=prev"/>
		<updated>2025-12-05T22:39:24Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;== Complete Manual: Displaying Chemical Structures in ChemDraw Format Using pycdxml ==  === Overview ===  The pycdxml package enables platform-independent manipulation of ChemDraw files (CDX and CDXML formats) through Python. The CDXMLSlideGenerator module creates professional structure sheets by arranging molecules in grids with associated properties, outputting standard ChemDraw documents that can be further edited by users.  === Installation ===  Create a conda enviro...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;== Complete Manual: Displaying Chemical Structures in ChemDraw Format Using pycdxml ==&lt;br /&gt;
&lt;br /&gt;
=== Overview ===&lt;br /&gt;
&lt;br /&gt;
The pycdxml package enables platform-independent manipulation of ChemDraw files (CDX and CDXML formats) through Python. The CDXMLSlideGenerator module creates professional structure sheets by arranging molecules in grids with associated properties, outputting standard ChemDraw documents that can be further edited by users.&lt;br /&gt;
&lt;br /&gt;
=== Installation ===&lt;br /&gt;
&lt;br /&gt;
Create a conda environment with required dependencies:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
name: pycdxml&lt;br /&gt;
channels:  &lt;br /&gt;
  - conda-forge &lt;br /&gt;
  - defaults   &lt;br /&gt;
dependencies:&lt;br /&gt;
  - python&amp;gt;=3.8  &lt;br /&gt;
  - rdkit&amp;gt;=2020.09.1 &lt;br /&gt;
  - numpy&lt;br /&gt;
  - pyyaml&lt;br /&gt;
  - lxml&lt;br /&gt;
  - fonttools&lt;br /&gt;
  - matplotlib&lt;br /&gt;
  - pip&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Install the environment and package:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
conda env create -f environment.yml&lt;br /&gt;
conda activate pycdxml&lt;br /&gt;
python -m pip install -e /path/to/PyCDXML&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Core Workflow for Structure Display ===&lt;br /&gt;
&lt;br /&gt;
The typical workflow consists of four main steps:&lt;br /&gt;
&lt;br /&gt;
# Read molecular structures from an SD file using RDKit&lt;br /&gt;
# Convert to CDXML format using the cdxml_converter module&lt;br /&gt;
# Extract properties from molecules to display alongside structures&lt;br /&gt;
# Generate slide using CDXMLSlideGenerator with customizable layout&lt;br /&gt;
&lt;br /&gt;
=== Detailed Implementation ===&lt;br /&gt;
&lt;br /&gt;
==== Step 1: Import Required Modules ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
import sys&lt;br /&gt;
from rdkit import Chem&lt;br /&gt;
from pycdxml import cdxml_slide_generator, cdxml_converter&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Step 2: Load Molecular Structures ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
# Read from command line or specify path directly&lt;br /&gt;
input_file = sys.argv[1]  # or use explicit path&lt;br /&gt;
suppl = Chem.SDMolSupplier(input_file)&lt;br /&gt;
molecules = [x for x in suppl]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Important considerations:&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* Works best with standard small molecules&lt;br /&gt;
* May have issues with organometallics, polymers, or complex structures&lt;br /&gt;
* Each molecule treated as a separate entity (similar to MOL file concept)&lt;br /&gt;
&lt;br /&gt;
==== Step 3: Convert Molecules to CDXML Format ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
cdxmls = []&lt;br /&gt;
for mol in molecules:    &lt;br /&gt;
    cdxml = cdxml_converter.mol_to_document(mol).to_cdxml()&lt;br /&gt;
    cdxmls.append(cdxml)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;mol_to_document()&amp;lt;/code&amp;gt; method creates a ChemDraw document object, and &amp;lt;code&amp;gt;to_cdxml()&amp;lt;/code&amp;gt; converts it to the CDXML XML string format.&lt;br /&gt;
&lt;br /&gt;
==== Step 4: Extract and Configure Properties ====&lt;br /&gt;
&lt;br /&gt;
Properties appear as text annotations below each structure. The &amp;lt;code&amp;gt;TextProperty&amp;lt;/code&amp;gt; class defines how each property displays:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
all_props = []&lt;br /&gt;
for mol in molecules:&lt;br /&gt;
    props = [&lt;br /&gt;
        cdxml_slide_generator.TextProperty(&amp;#039;SOURCE_ID&amp;#039;, &lt;br /&gt;
                                          mol.GetProp(&amp;quot;SOURCE_ID&amp;quot;), &lt;br /&gt;
                                          color=&amp;#039;#3f6eba&amp;#039;),&lt;br /&gt;
        cdxml_slide_generator.TextProperty(&amp;#039;MG_ID&amp;#039;, &lt;br /&gt;
                                          mol.GetProp(&amp;quot;MG_ID&amp;quot;))&lt;br /&gt;
    ]&lt;br /&gt;
    all_props.append(props)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;TextProperty parameters:&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;name&amp;#039;&amp;#039;&amp;#039;: Property label (displayed if show_name=True)&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;value&amp;#039;&amp;#039;&amp;#039;: The actual property value from molecule data&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;color&amp;#039;&amp;#039;&amp;#039;: Hex color code (e.g., &amp;#039;#3f6eba&amp;#039; for blue)&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;show_name&amp;#039;&amp;#039;&amp;#039;: Boolean to display/hide the property name&lt;br /&gt;
&lt;br /&gt;
==== Step 5: Generate the Slide ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
sg = cdxml_slide_generator.CDXMLSlideGenerator(&lt;br /&gt;
    style=&amp;quot;ACS 1996&amp;quot;,           # ChemDraw style template&lt;br /&gt;
    number_of_properties=2,      # Properties per molecule&lt;br /&gt;
    columns=5,                   # Grid columns&lt;br /&gt;
    rows=10,                     # Grid rows&lt;br /&gt;
    slide_width=50,              # Page width (cm)&lt;br /&gt;
    slide_height=70              # Page height (cm)&lt;br /&gt;
)&lt;br /&gt;
&lt;br /&gt;
slide = sg.generate_slide(cdxmls, all_props)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;CDXMLSlideGenerator parameters:&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;style&amp;#039;&amp;#039;&amp;#039;: ChemDraw style name (e.g., &amp;quot;ACS 1996&amp;quot;) - determines bond length, font sizes, display preferences&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;number_of_properties&amp;#039;&amp;#039;&amp;#039;: Must match the number of TextProperty objects per molecule&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;columns&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;rows&amp;#039;&amp;#039;&amp;#039;: Grid layout (5×10 = 50 structures per page)&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;slide_width&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;slide_height&amp;#039;&amp;#039;&amp;#039;: Document dimensions in centimeters&lt;br /&gt;
&lt;br /&gt;
==== Step 6: Save Output File ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
output_file = sys.argv[2]  # or specify path&lt;br /&gt;
with open(output_file, &amp;quot;w&amp;quot;, encoding=&amp;#039;UTF-8&amp;#039;) as xf:&lt;br /&gt;
    xf.write(slide)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Critical encoding note:&amp;#039;&amp;#039;&amp;#039; Always use &amp;lt;code&amp;gt;encoding=&amp;#039;UTF-8&amp;#039;&amp;lt;/code&amp;gt; to ensure proper character handling, especially for special characters in compound names or properties.&lt;br /&gt;
&lt;br /&gt;
=== Command-Line Usage ===&lt;br /&gt;
&lt;br /&gt;
Create a script (e.g., &amp;lt;code&amp;gt;generate_structures.py&amp;lt;/code&amp;gt;) and run:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
python generate_structures.py input_structures.sdf output_slide.cdxml&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Advanced Features ===&lt;br /&gt;
&lt;br /&gt;
==== Style Application ====&lt;br /&gt;
&lt;br /&gt;
Apply consistent ChemDraw styles across all structures. The styler module can convert existing CDXML files to a target style:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from pycdxml import cdxml_styler&lt;br /&gt;
&lt;br /&gt;
styler = cdxml_styler.CDXMLStyler(style_source=&amp;quot;/path/to/ACS 1996.cdxml&amp;quot;)&lt;br /&gt;
styler.apply_style_to_file(&amp;#039;input.cdxml&amp;#039;, outpath=&amp;#039;output.cdxml&amp;#039;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Style affects:&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* Bond lengths and widths&lt;br /&gt;
* Atom label font sizes&lt;br /&gt;
* Hydrogen display (implicit vs explicit)&lt;br /&gt;
* Stereochemistry indicators&lt;br /&gt;
* Overall visual presentation&lt;br /&gt;
&lt;br /&gt;
==== Format Conversions ====&lt;br /&gt;
&lt;br /&gt;
Convert between CDXML (XML text) and CDX (binary) formats:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
# CDX to CDXML&lt;br /&gt;
doc = cdxml_converter.read_cdx(&amp;#039;/path/to/structure.cdx&amp;#039;)    &lt;br /&gt;
cdxml_converter.write_cdxml_file(doc, &amp;#039;/path/to/structure.cdxml&amp;#039;)&lt;br /&gt;
&lt;br /&gt;
# CDXML to base64-encoded CDX&lt;br /&gt;
doc = cdxml_converter.read_cdxml(&amp;#039;/path/to/structure.cdxml&amp;#039;)&lt;br /&gt;
b64_cdx = cdxml_converter.to_b64_cdx(doc)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Property Annotations for SD File Export ====&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Important feature:&amp;#039;&amp;#039;&amp;#039; All visible properties are automatically annotated to the molecules in the CDXML file. If you open the generated CDXML in ChemDraw and save as an SD file, all displayed properties will be included in the SD file output.&lt;br /&gt;
&lt;br /&gt;
=== Practical Tips ===&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;For Large Datasets:&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* Process in batches if dealing with thousands of structures&lt;br /&gt;
* Calculate optimal rows/columns based on page size and readability&lt;br /&gt;
* Consider multiple pages rather than overcrowding single page&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Property Display:&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* Limit to 2-4 properties per structure for readability&lt;br /&gt;
* Use color coding to highlight important values (e.g., red for high activity)&lt;br /&gt;
* Include units in property names when showing numerical values&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Layout Optimization:&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* Standard 5×10 grid (50 structures) works well for A3/tabloid size printouts&lt;br /&gt;
* For presentations, consider 4×3 or 5×4 grids for better visibility&lt;br /&gt;
* Adjust slide_width and slide_height to match intended output format&lt;br /&gt;
&lt;br /&gt;
=== Output and Compatibility ===&lt;br /&gt;
&lt;br /&gt;
The generated CDXML file is a fully valid ChemDraw document that can be:&lt;br /&gt;
* Opened and edited in ChemDraw Desktop&lt;br /&gt;
* Converted to PDF, PNG, or other formats via ChemDraw&lt;br /&gt;
* Modified by end users (chemists) to adjust layouts, add annotations, or change styles&lt;br /&gt;
* Saved as SD files with all property annotations preserved&lt;br /&gt;
&lt;br /&gt;
=== Known Limitations ===&lt;br /&gt;
&lt;br /&gt;
* Very old CDX files (~ChemDraw 7 era) may fail to parse correctly&lt;br /&gt;
* Complex molecules (organometallics, polymers) may have conversion issues&lt;br /&gt;
* Package focuses on &amp;quot;file-level&amp;quot; operations, not chemical validation&lt;br /&gt;
* Style translation may not perfectly position additional drawing elements (brackets, arrows) relative to molecules&lt;br /&gt;
&lt;br /&gt;
=== Complete Example Script ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
#!/usr/bin/env python&lt;br /&gt;
&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
Generate ChemDraw structure sheet from SD file&lt;br /&gt;
Usage: python script.py input.sdf output.cdxml&lt;br /&gt;
&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
import sys&lt;br /&gt;
from rdkit import Chem&lt;br /&gt;
from pycdxml import cdxml_slide_generator, cdxml_converter&lt;br /&gt;
&lt;br /&gt;
# Read input file&lt;br /&gt;
input_sdf = sys.argv[1]&lt;br /&gt;
output_cdxml = sys.argv[2]&lt;br /&gt;
&lt;br /&gt;
# Load molecules&lt;br /&gt;
suppl = Chem.SDMolSupplier(input_sdf)&lt;br /&gt;
molecules = [mol for mol in suppl if mol is not None]&lt;br /&gt;
&lt;br /&gt;
# Convert to CDXML&lt;br /&gt;
cdxmls = []&lt;br /&gt;
for mol in molecules:    &lt;br /&gt;
    cdxml = cdxml_converter.mol_to_document(mol).to_cdxml()&lt;br /&gt;
    cdxmls.append(cdxml)&lt;br /&gt;
&lt;br /&gt;
# Extract properties&lt;br /&gt;
all_props = []&lt;br /&gt;
for mol in molecules:&lt;br /&gt;
    props = [&lt;br /&gt;
        cdxml_slide_generator.TextProperty(&amp;#039;ID&amp;#039;, &lt;br /&gt;
                                          mol.GetProp(&amp;quot;SOURCE_ID&amp;quot;), &lt;br /&gt;
                                          color=&amp;#039;#3f6eba&amp;#039;),&lt;br /&gt;
        cdxml_slide_generator.TextProperty(&amp;#039;Activity&amp;#039;, &lt;br /&gt;
                                          mol.GetProp(&amp;quot;ACTIVITY&amp;quot;), &lt;br /&gt;
                                          show_name=True)&lt;br /&gt;
    ]&lt;br /&gt;
    all_props.append(props)&lt;br /&gt;
&lt;br /&gt;
# Generate slide&lt;br /&gt;
generator = cdxml_slide_generator.CDXMLSlideGenerator(&lt;br /&gt;
    style=&amp;quot;ACS 1996&amp;quot;,&lt;br /&gt;
    number_of_properties=2,&lt;br /&gt;
    columns=5,&lt;br /&gt;
    rows=10,&lt;br /&gt;
    slide_width=50,&lt;br /&gt;
    slide_height=70&lt;br /&gt;
)&lt;br /&gt;
&lt;br /&gt;
slide = generator.generate_slide(cdxmls, all_props)&lt;br /&gt;
&lt;br /&gt;
# Save output&lt;br /&gt;
with open(output_cdxml, &amp;quot;w&amp;quot;, encoding=&amp;#039;UTF-8&amp;#039;) as f:&lt;br /&gt;
    f.write(slide)&lt;br /&gt;
&lt;br /&gt;
print(f&amp;quot;Generated {output_cdxml} with {len(molecules)} structures&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== References ===&lt;br /&gt;
&lt;br /&gt;
* pycdxml GitHub repository: https://github.com/kienerj/pycdxml&lt;br /&gt;
* Official ChemDraw CDX format specification: https://www.cambridgesoft.com/services/documentation/sdk/chemdraw/cdx/IntroCDX.htm&lt;br /&gt;
* RDKit documentation: https://www.rdkit.org/docs/&lt;br /&gt;
&lt;br /&gt;
[[Category:Cheminformatics]]&lt;br /&gt;
[[Category:Python]]&lt;br /&gt;
[[Category:Molecular Visualization]]&lt;/div&gt;</summary>
		<author><name>Iamkaant</name></author>
	</entry>
</feed>