Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
378 changes: 378 additions & 0 deletions notebooks/API_Training_Exercises/01_Simple_Report.ipynb
Copy link
Contributor

@Alex-AMC Alex-AMC Nov 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please remove the output of all the cells ( I think the last one in particular is a bit too big) Cell 3 less so.

A suggestion and not necessary - I can see that you've added the dependencies as googletrans, might be nice to have a cell that installs it using magic commands

!conda install googletrans 
or 
!pip install googletrans 

That way, the user doesn't have to leave the notebook :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did try that but for me it proved somewhat problematic (basically just failed)

Original file line number Diff line number Diff line change
@@ -0,0 +1,378 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"```\n",
"This script can be used for any purpose without limitation subject to the\n",
"conditions at http://www.ccdc.cam.ac.uk/Community/Pages/Licences/v2.aspx\n",
"\n",
"This permission notice and the following statement of attribution must be\n",
"included in all copies or substantial portions of this script.\n",
"\n",
"2024-09-11: Made available by the Cambridge Crystallographic Data Centre.\n",
"\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# International Report\n",
"\n",
"This notebook talks through how to create a report that reports a CSD entry ... but uses google translate to translate headings into the language of user choice.\n",
"You could use the ideas her to convert the 'simple_report.py script into an international report\n",
"\n",
"#### Prerequisites\n",
"\n",
"First install the CSD Python API and googletrans into your conda or pip environment. This should only be needed the before you run the script."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now we are ready to start some python coding. We need to import some modules. We are writing a script that ultimately will be used in Mercury so we will want some of \n",
"the special utilities available for writing reports."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from ccdc.utilities import html_table\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can create a translator for the script to use, and define our language of choice. Lets write a little function to do the translation"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"from googletrans import Translator\n",
"\n",
"translator = Translator()\n",
"def tr(text, lang):\n",
" if lang is None:\n",
" return text\n",
" try:\n",
" return translator.translate(str(text),src=\"en\",dest=lang).text\n",
" except Exception as e:\n",
" return text"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's try it out:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'ماري لديها خروف صغير'"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\n",
"tr(\"Mary had a little lamb\", \"ar\") # Lets try arabic!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"So now we can create a report on a CSD entry that is international. The following function will work in a Mercury API script. Note how it defines an interface object"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"def get_coordinates(molecule, round_digits=None):\n",
" \"\"\"Yield the label and fractional coordinates of all atoms in the molecule.\n",
"\n",
" :param molecule: (:obj:`ccdc.molecule.Molecule`) The molecule for which to return coordinates.\n",
" :param round_digits: (:obj:`int`) How many decimal digits coordinates should be rounded to.\n",
" :returns: (:obj:`list`) List of the label and fractional x/y/z coordinates for each atom\n",
" in the molecule in format ['Atom label', 'X coordinate', 'Y coordinate', 'Z coordinate'].\n",
" \"\"\"\n",
" for atom in molecule.atoms:\n",
" try:\n",
" x, y, z = atom.fractional_coordinates\n",
" yield [atom.label,\n",
" x if round_digits is None else round(x, round_digits),\n",
" y if round_digits is None else round(y, round_digits),\n",
" z if round_digits is None else round(z, round_digits)]\n",
" except TypeError:\n",
" continue\n",
"\n",
"\n",
"def main(interface=None, lang=None):\n",
" \"\"\"Generate a simple report based on the entry currently selected in the Mercury UI.\n",
"\n",
" :param interface: (:obj:`ccdc.utility.ApplicationInterface`) An ApplicationInterface instance.\n",
" \"\"\"\n",
" if interface is None:\n",
" from ccdc.utilities import ApplicationInterface\n",
" interface = ApplicationInterface()\n",
"\n",
" entry = interface.current_entry\n",
"\n",
" # Open a HTML report. This will create the file, copy the CSD Python API\n",
" # default template for reports and fill in the headline/page title.\n",
" with interface.html_report(title=tr('Simple report on ',lang)+ ' ' + entry.identifier) as report:\n",
"\n",
" # Write the section header for Entry Details\n",
" report.write_section_header(tr('Entry Details',lang))\n",
" # Assemble a list of information and labels to go into the \"Entry Details\" table\n",
" entry_details = [\n",
" ['<b>'+tr('Chemical name',lang)+'</b>', entry.chemical_name],\n",
" ['<b>'+tr('Synonyms',lang)+'</b>', entry.synonyms],\n",
" ['<b>'+tr('Formula',lang)+'</b>', entry.formula],\n",
" ['<b>'+tr('R-factor',lang)+'</b>', entry.r_factor],\n",
" ['<b>'+tr('Disorder',lang)+'</b>', tr(entry.disorder_details,lang)],\n",
" ['<b>'+tr('Polymorphism',lang)+'</b>', tr(entry.polymorph,lang)],\n",
" ['<b>'+tr('3D structure',lang)+'</b>', tr(entry.has_3d_structure,lang)],\n",
" ['<b>'+tr('Organic',lang)+'</b>', tr(entry.is_organic,lang)],\n",
" ['<b>'+tr('Polymeric',lang)+'</b>', tr(entry.is_polymeric,lang)],\n",
" ['<b>'+tr('Bioactivity',lang)+'</b>', tr(entry.bioactivity,lang)],\n",
" ['<b>'+tr('Source',lang)+'</b>', tr(entry.source,lang)],\n",
" ['<b>'+tr('Habit',lang)+'</b>', tr(entry.habit,lang)],\n",
" ]\n",
" # Generate a HTML table from the entry details and write it to the report\n",
" report.write(html_table(data=entry_details, table_id='entry_details'))\n",
"\n",
" # Write the section header for Fractional Coordinates\n",
" report.write_section_header(tr('Fractional Coordinates',lang))\n",
" # Get the coordinates of all the atoms from the entry and write them to a HTML table\n",
" report.write(html_table(data=list(get_coordinates(entry.molecule, round_digits=3)),\n",
" table_id='fractional_coordinates',\n",
" header=[tr('Atom',lang), 'x', 'y', 'z']))\n",
"\n",
" # Write the section header for Publication Details\n",
" report.write_section_header(tr('Publication Details',lang))\n",
" # Assemble a list of information and labels for the Publication Details table\n",
" publication_details = [\n",
" ['<b>'+tr('Reference',lang)+'</b>', '%s Volume %s, %s' % (getattr(entry.publication, 'journal_name', ''),\n",
" entry.publication.volume,\n",
" entry.publication.year)],\n",
" ['<b>'+tr('Authors', lang)+'</b>', entry.publication.authors],\n",
" ['<b>'+tr('Document Object Identifier',lang)+'</b>', entry.publication.doi]\n",
" ]\n",
" # Write the publication details to a HTML table\n",
" report.write(html_table(publication_details, table_id='publication_details'))\n",
"\n",
" # Write the section header for Basic Crystallographic Information\n",
" report.write_section_header(tr('Basic Crystallographic Information',lang))\n",
" # Assemble a list of basic crystal information and labels for the table\n",
" crystallographic_data = [\n",
" ['<b>'+tr('Crystal System',lang)+'</b>', entry.crystal.crystal_system],\n",
" ['<b>'+tr('Space Group',lang) + '</b>', entry.crystal.spacegroup_symbol],\n",
" ['<b>'+tr('Cell Volume',lang)+ '</b>', '%s ų' % round(entry.crystal.cell_volume, 3)],\n",
" ['<b>Z, Z\\'</b>', entry.crystal.z_prime],\n",
" ]\n",
" # Write the crystallographic details to a HTML table\n",
" report.write(html_table(crystallographic_data, table_id='crystallographic_information'))\n",
"\n",
" # Once the HTMLReport is closed (e.g. when the with: branch above ends),\n",
" # it will automatically write the appropriate HTML footer."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Lets use our function. As we are in a notebook, we will have to define a dummy interface file:"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"import time\n",
"from ccdc.utilities import ApplicationInterface\n",
"interface = ApplicationInterface(parse_commandline=False)\n",
"interface.identifier = \"AABHTZ\"\n",
"interface.output_html_file = f'{interface.identifier}_{time.strftime(\"%H%M%S\", time.gmtime())}_report.html'\n",
"\n",
"main(interface,\"ar\") # Arabic "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Finally use Jupyter to display it:"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<!DOCTYPE html>\n",
"<html>\n",
"\t<head>\n",
"\t\t<style type=\"text/css\">\n",
"\t\t\tbody { font-family: Calibri, Verdana, sans-serif; padding: 1.5em; font-size: 1.2em}\n",
"\t\t\t#ccdc_logo { float: right; margin: -2em 1.5em 1.5em 1.5em; }\n",
"\t\t\tp { text-align: justify; }\n",
"\t\t\th1 { text-align: left; font-size: 1.8em; font-weight: bold; }\n",
"\t\t\th2 { text-align: left; font-size: 1.5em; font-weight: bold; }\n",
"\t\t\ttable { width: 100%; margin:auto; border: 1px solid; border-collapse:collapse; }\n",
"\t\t\tth { padding: .2em; font-weight: bold; }\n",
"\t\t\ttd { padding: .2em; }\n",
"\t\t\ttable, th, td { border: 1px solid; }\n",
"\t\t\ttr:nth-child(even) { background: #ccc; }\n",
" </style>\n",
"\t\t<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>\n",
"\t\t<title>تقرير بسيط عن AABHTZ</title>\n",
" </head>\n",
"\t<body>\n",
"<img src=\"ccdc_logo_180x180_with_text.png\" id=\"ccdc_logo\" alt=\"CCDC\" />\n",
"<h1 id=\"report_header\">تقرير بسيط عن AABHTZ</h1>\n",
"<h2 id=\"\">تفاصيل الدخول</h2>\n",
"<table id=\"entry_details\">\n",
" <tr><td><b>الاسم الكيميائي</b></td><td>4-Acetoamido-3-(1-acetyl-2-(2,6-dichlorobenzylidene)hydrazine)-1,2,4-triazole</td></tr>\n",
" <tr><td><b>المرادفات</b></td><td>()</td></tr>\n",
" <tr><td><b>صيغة</b></td><td>C13 H12 Cl2 N6 O2</td></tr>\n",
" <tr><td><b>عامل r</b></td><td>4.1</td></tr>\n",
" <tr><td><b>اضطراب</b></td><td>لا أحد</td></tr>\n",
" <tr><td><b>تعدد الأشكال</b></td><td>لا أحد</td></tr>\n",
" <tr><td><b>هيكل ثلاثي الأبعاد</b></td><td>حقيقي</td></tr>\n",
" <tr><td><b>عضوي</b></td><td>حقيقي</td></tr>\n",
" <tr><td><b>البوليمرية</b></td><td>خطأ شنيع</td></tr>\n",
" <tr><td><b>النشاط الحيوي</b></td><td>لا أحد</td></tr>\n",
" <tr><td><b>مصدر</b></td><td>لا أحد</td></tr>\n",
" <tr><td><b>عادة</b></td><td>لا أحد</td></tr>\n",
"</table>\n",
"<h2 id=\"\">إحداثيات كسرية</h2>\n",
"<table id=\"fractional_coordinates\">\n",
" <tr><th>ذرة</th><th>x</th><th>y</th><th>z</th></tr>\n",
" <tr><td>Cl1</td><td>-0.336</td><td>0.1</td><td>0.106</td></tr>\n",
" <tr><td>Cl2</td><td>-0.641</td><td>-0.308</td><td>0.327</td></tr>\n",
" <tr><td>C1</td><td>-0.478</td><td>0.039</td><td>0.231</td></tr>\n",
" <tr><td>C2</td><td>-0.573</td><td>0.134</td><td>0.342</td></tr>\n",
" <tr><td>C3</td><td>-0.685</td><td>0.092</td><td>0.448</td></tr>\n",
" <tr><td>C4</td><td>-0.702</td><td>-0.045</td><td>0.445</td></tr>\n",
" <tr><td>C5</td><td>-0.607</td><td>-0.139</td><td>0.33</td></tr>\n",
" <tr><td>C6</td><td>-0.49</td><td>-0.101</td><td>0.218</td></tr>\n",
" <tr><td>C7</td><td>-0.384</td><td>-0.194</td><td>0.089</td></tr>\n",
" <tr><td>N1</td><td>-0.367</td><td>-0.298</td><td>0.135</td></tr>\n",
" <tr><td>N2</td><td>-0.263</td><td>-0.38</td><td>0.01</td></tr>\n",
" <tr><td>C8</td><td>-0.186</td><td>-0.365</td><td>-0.175</td></tr>\n",
" <tr><td>N3</td><td>-0.22</td><td>-0.386</td><td>-0.335</td></tr>\n",
" <tr><td>N4</td><td>-0.115</td><td>-0.36</td><td>-0.484</td></tr>\n",
" <tr><td>C9</td><td>-0.025</td><td>-0.326</td><td>-0.406</td></tr>\n",
" <tr><td>N5</td><td>-0.064</td><td>-0.327</td><td>-0.21</td></tr>\n",
" <tr><td>N6</td><td>0.002</td><td>-0.284</td><td>-0.074</td></tr>\n",
" <tr><td>C10</td><td>-0.031</td><td>-0.161</td><td>0.074</td></tr>\n",
" <tr><td>C11</td><td>0.041</td><td>-0.124</td><td>0.222</td></tr>\n",
" <tr><td>O1</td><td>-0.11</td><td>-0.088</td><td>0.079</td></tr>\n",
" <tr><td>C12</td><td>-0.239</td><td>-0.486</td><td>0.072</td></tr>\n",
" <tr><td>C13</td><td>-0.331</td><td>-0.509</td><td>0.255</td></tr>\n",
" <tr><td>O2</td><td>-0.145</td><td>-0.554</td><td>-0.03</td></tr>\n",
" <tr><td>H1</td><td>-0.558</td><td>0.232</td><td>0.347</td></tr>\n",
" <tr><td>H2</td><td>-0.752</td><td>0.157</td><td>0.531</td></tr>\n",
" <tr><td>H3</td><td>-0.784</td><td>-0.078</td><td>0.53</td></tr>\n",
" <tr><td>H4</td><td>-0.326</td><td>-0.175</td><td>-0.032</td></tr>\n",
" <tr><td>H5</td><td>0.057</td><td>-0.296</td><td>-0.469</td></tr>\n",
" <tr><td>H6</td><td>0.046</td><td>-0.34</td><td>-0.057</td></tr>\n",
" <tr><td>H7</td><td>0.081</td><td>-0.036</td><td>0.217</td></tr>\n",
" <tr><td>H8</td><td>0.105</td><td>-0.198</td><td>0.189</td></tr>\n",
" <tr><td>H9</td><td>-0.006</td><td>-0.107</td><td>0.333</td></tr>\n",
" <tr><td>H10</td><td>-0.313</td><td>-0.598</td><td>0.275</td></tr>\n",
" <tr><td>H11</td><td>-0.329</td><td>-0.451</td><td>0.374</td></tr>\n",
" <tr><td>H12</td><td>-0.413</td><td>-0.525</td><td>0.243</td></tr>\n",
"</table>\n",
"<h2 id=\"\">تفاصيل النشر</h2>\n",
"<table id=\"publication_details\">\n",
" <tr><td><b>مرجع</b></td><td> Volume 5, 1976</td></tr>\n",
" <tr><td><b>المؤلفون</b></td><td>P.-E.Werner</td></tr>\n",
" <tr><td><b>معرف كائن المستند</b></td><td>None</td></tr>\n",
"</table>\n",
"<h2 id=\"\">المعلومات البلورية الأساسية</h2>\n",
"<table id=\"crystallographic_information\">\n",
" <tr><td><b>نظام البلورة</b></td><td>triclinic</td></tr>\n",
" <tr><td><b>مجموعة الفضاء</b></td><td>P-1</td></tr>\n",
" <tr><td><b>حجم الخلية</b></td><td>769.978 ų</td></tr>\n",
" <tr><td><b>Z, Z'</b></td><td>1.0</td></tr>\n",
"</table>\n",
"\n",
"</body>\n",
"</html>\n"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from IPython.display import HTML\n",
"HTML(interface.output_html_file)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.20"
}
},
"nbformat": 4,
"nbformat_minor": 4
}