{ "cells": [ { "cell_type": "markdown", "id": "58c8dfd7", "metadata": {}, "source": [ "## User defined fragmentation models\n", "\n", "Users may use the `ugropy` API to define their own fragmentation models. The\n", "basic models API is the `FragmentationModel` class. Instances of this class\n", "have all the methods and attributes necessary to define a fragmentation model." ] }, { "cell_type": "code", "execution_count": 1, "id": "74aa71ac", "metadata": {}, "outputs": [], "source": [ "from ugropy import FragmentationModel" ] }, { "cell_type": "markdown", "id": "944ec03b", "metadata": {}, "source": [ "Groups are defined on a `pandas` DataFrame. This is because is convenient\n", "to store the data in a tabular format, and simply read it from a file. You can\n", "check examples on the repository (https://github.com/ipqa-research/ugropy/blob/main/ugropy/groupscsv/unifac/unifac_subgroups.csv)\n", "\n", "We can define a simplified UNIFAC fragmentation model as follows:" ] }, { "cell_type": "code", "execution_count": 8, "id": "a6223244", "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "\n", "# We define a simple fragmentation model with some common groups.\n", "df = pd.DataFrame(\n", " {\n", " \"group\": [\"CH3\", \"CH2\", \"CH\", \"C\", \"AC\", \"ACH\", \"ACCH3\", \"ACCH2\"],\n", " \"smarts\": [\n", " \"[CX4H3]\",\n", " \"[CX4H2]\",\n", " \"[CX4H]\",\n", " \"[CX4H0]\",\n", " \"[cH0]\",\n", " \"[cH]\",\n", " \"[cH0][CX4H3]\",\n", " \"[cH0][CX4H2]\",\n", " ]\n", " }\n", ")\n", "\n", "# Set the group column as the index\n", "df.set_index(\"group\", inplace=True)\n", "\n", "\n", "# Define a fragmentation model using the defined groups. UNIFAC-like models\n", "# doesn't allow overlappin groups or atoms that not belong to any group.\n", "mymodel = FragmentationModel(\n", " subgroups=df,\n", " allow_overlapping=False,\n", " allow_free_atoms=False, \n", ")" ] }, { "cell_type": "markdown", "id": "631e0088", "metadata": {}, "source": [ "With this instance we can detect fragments and solve groups as any other\n", "`ugropy` model. For example:" ] }, { "cell_type": "code", "execution_count": 9, "id": "9becb507", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'ACH': 5, 'ACCH3': 1}" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sol = mymodel.get_groups(\"toluene\")\n", "\n", "sol.subgroups" ] }, { "cell_type": "code", "execution_count": 10, "id": "01c0c4e2", "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "ACH: 10ACCH2: 2" ], "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sol = mymodel.get_groups(\"bibenzyl\")\n", "\n", "sol.draw(width=800)\n" ] }, { "cell_type": "markdown", "id": "ac292ee8", "metadata": {}, "source": [ "The `FragmentationModel.get_groups` method returns a `FragmentationResult`\n", "object. To extend the behavior of the base `FragmentationModel` class, you can\n", "inherit from it and inherit from `FragmentationResult` as well.\n", "\n", "An example, could be the `GibbsModel` class and the `GibbsFragmentationResult`\n", "class. The differences between these classes are that receives an extra argument\n", "(the groups' information dataframe). These models work exactly the same but\n", "also calculates the R and Q of the molecule once the fragments are detected.\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "id": "1a9e94dd", "metadata": {}, "outputs": [], "source": [ "from ugropy import GibbsFragmentationResult, GibbsModel" ] }, { "cell_type": "markdown", "id": "818c6b74", "metadata": {}, "source": [ "You may check these classes source code in the API documentation, it's very\n", "simple to follow and understand how to extend the basic behavior of the base\n", "``FragmentationModel`` class.\n" ] } ], "metadata": { "kernelspec": { "display_name": "ugropy", "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.10.12" } }, "nbformat": 4, "nbformat_minor": 5 }