Skip to content

Commit

Permalink
Expand linting to doc notebooks
Browse files Browse the repository at this point in the history
  • Loading branch information
mattwthompson committed Nov 15, 2023
1 parent eb88022 commit 2800c38
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 72 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
ci:
autoupdate_schedule: "quarterly"
files: ^openff|(^examples/((?!deprecated).)*$)
files: ^openff|(^examples/((?!deprecated).)*$)|^docs
repos:
- repo: https://github.com/psf/black
rev: 23.9.1
Expand Down
3 changes: 2 additions & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@

sys.path.insert(0, os.path.abspath("."))

import openff.toolkit
import sphinx

import openff.toolkit

# -- General configuration ------------------------------------------------

# If your documentation needs a minimal Sphinx version, state it here.
Expand Down
156 changes: 86 additions & 70 deletions docs/users/molecule_cookbook.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -40,25 +40,36 @@
"source": [
"# Workaround for https://github.com/conda-forge/qcfractal-feedstock/issues/43\n",
"try:\n",
" import qcportal\n",
" import qcportal # noqa\n",
"except ImportError:\n",
" pass\n",
"\n",
"# Hide tracebacks for simpler errors\n",
"import sys\n",
"ipython = get_ipython()\n",
"\n",
"def hide_traceback(exc_tuple=None, filename=None, tb_offset=None,\n",
" exception_only=False, running_compiled_code=False):\n",
"ipython = get_ipython() # noqa\n",
"\n",
"\n",
"def hide_traceback(\n",
" exc_tuple=None,\n",
" filename=None,\n",
" tb_offset=None,\n",
" exception_only=False,\n",
" running_compiled_code=False,\n",
"):\n",
" etype, value, tb = sys.exc_info()\n",
" value.__cause__ = None # suppress chained exceptions\n",
" return ipython._showtraceback(etype, value, ipython.InteractiveTB.get_exception_only(etype, value))\n",
" return ipython._showtraceback(\n",
" etype, value, ipython.InteractiveTB.get_exception_only(etype, value)\n",
" )\n",
"\n",
"\n",
"ipython.showtraceback = hide_traceback\n",
"\n",
"# Hide NumPy warnings\n",
"import warnings\n",
"warnings.filterwarnings('ignore', r'The value of the smallest subnormal for ')\n"
"\n",
"warnings.filterwarnings(\"ignore\", r\"The value of the smallest subnormal for \")"
]
},
{
Expand Down Expand Up @@ -212,8 +223,8 @@
],
"source": [
"smiles_explicit_h = Molecule.from_smiles(\n",
" \"[H][C]([H])([H])[C@@]([H])([C](=[O])[O-])[N+]([H])([H])[H]\", \n",
" hydrogens_are_explicit=True\n",
" \"[H][C]([H])([H])[C@@]([H])([C](=[O])[O-])[N+]([H])([H])[H]\",\n",
" hydrogens_are_explicit=True,\n",
")\n",
"\n",
"assert zw_l_alanine.is_isomorphic_with(smiles_explicit_h)\n",
Expand Down Expand Up @@ -294,8 +305,10 @@
"\n",
"assert zw_l_alanine.is_isomorphic_with(mapped_smiles)\n",
"\n",
"assert mapped_smiles.atoms[0].atomic_number == 7 # First index is the Nitrogen\n",
"assert all([a.atomic_number==1 for a in mapped_smiles.atoms[6:]]) # Final indices are all H\n",
"assert mapped_smiles.atoms[0].atomic_number == 7 # First index is the Nitrogen\n",
"assert all(\n",
" [a.atomic_number == 1 for a in mapped_smiles.atoms[6:]]\n",
") # Final indices are all H\n",
"\n",
"mapped_smiles.visualize()"
]
Expand Down Expand Up @@ -340,9 +353,7 @@
"from openff.toolkit.utils.exceptions import UndefinedStereochemistryError\n",
"\n",
"try:\n",
" smiles_non_isomeric = Molecule.from_smiles(\n",
" \"CC([NH3+])C(=O)[O-]\"\n",
" )\n",
" smiles_non_isomeric = Molecule.from_smiles(\"CC([NH3+])C(=O)[O-]\")\n",
"except UndefinedStereochemistryError as error:\n",
" print(error)"
]
Expand Down Expand Up @@ -413,8 +424,7 @@
],
"source": [
"smiles_non_isomeric = Molecule.from_smiles(\n",
" \"CC([NH3+])C(=O)[O-]\",\n",
" allow_undefined_stereo=True\n",
" \"CC([NH3+])C(=O)[O-]\", allow_undefined_stereo=True\n",
")\n",
"\n",
"assert not zw_l_alanine.is_isomorphic_with(smiles_non_isomeric)\n",
Expand Down Expand Up @@ -507,45 +517,45 @@
"by_hand.name = \"Zwitterionic l-Alanine\"\n",
"\n",
"by_hand.add_atom(\n",
" atomic_number = 8, # Atomic number 8 is Oxygen\n",
" formal_charge = -1, # Formal negative charge\n",
" is_aromatic = False, # Atom is not part of an aromatic system\n",
" stereochemistry = None, # Optional argument; \"R\" or \"S\" stereochemistry\n",
" name = \"O-\" # Optional argument; descriptive name for the atom\n",
" atomic_number=8, # Atomic number 8 is Oxygen\n",
" formal_charge=-1, # Formal negative charge\n",
" is_aromatic=False, # Atom is not part of an aromatic system\n",
" stereochemistry=None, # Optional argument; \"R\" or \"S\" stereochemistry\n",
" name=\"O-\", # Optional argument; descriptive name for the atom\n",
")\n",
"by_hand.add_atom(6, 0, False, name=\"C\")\n",
"by_hand.add_atom(8, 0, False, name=\"O\")\n",
"by_hand.add_atom(6, 0, False, stereochemistry=\"S\", name=\"CA\")\n",
"by_hand.add_atom(1, 0, False, name=\"CAH\")\n",
"by_hand.add_atom(6, 0, False, name=\"CB\")\n",
"by_hand.add_atom(1, 0, False, name=\"HB1\")\n",
"by_hand.add_atom(1, 0, False, name=\"HB2\")\n",
"by_hand.add_atom(1, 0, False, name=\"HB3\")\n",
"by_hand.add_atom(6, 0, False, name=\"C\")\n",
"by_hand.add_atom(8, 0, False, name=\"O\")\n",
"by_hand.add_atom(6, 0, False, stereochemistry=\"S\", name=\"CA\")\n",
"by_hand.add_atom(1, 0, False, name=\"CAH\")\n",
"by_hand.add_atom(6, 0, False, name=\"CB\")\n",
"by_hand.add_atom(1, 0, False, name=\"HB1\")\n",
"by_hand.add_atom(1, 0, False, name=\"HB2\")\n",
"by_hand.add_atom(1, 0, False, name=\"HB3\")\n",
"by_hand.add_atom(7, +1, False, name=\"N+\")\n",
"by_hand.add_atom(1, 0, False, name=\"HN1\")\n",
"by_hand.add_atom(1, 0, False, name=\"HN2\")\n",
"by_hand.add_atom(1, 0, False, name=\"HN3\")\n",
"\n",
"\n",
"by_hand.add_bond( \n",
" atom1 = 0, # First (zero-indexed) atom specified above (\"O-\") \n",
" atom2 = 1, # Second atom specified above (\"C\")\n",
" bond_order = 1, # Single bond\n",
" is_aromatic = False, # Bond is not aromatic\n",
" stereochemistry = None, # Optional argument; \"E\" or \"Z\" stereochemistry\n",
" fractional_bond_order = None # Optional argument; Wiberg (or similar) bond order\n",
"by_hand.add_atom(1, 0, False, name=\"HN1\")\n",
"by_hand.add_atom(1, 0, False, name=\"HN2\")\n",
"by_hand.add_atom(1, 0, False, name=\"HN3\")\n",
"\n",
"\n",
"by_hand.add_bond(\n",
" atom1=0, # First (zero-indexed) atom specified above (\"O-\")\n",
" atom2=1, # Second atom specified above (\"C\")\n",
" bond_order=1, # Single bond\n",
" is_aromatic=False, # Bond is not aromatic\n",
" stereochemistry=None, # Optional argument; \"E\" or \"Z\" stereochemistry\n",
" fractional_bond_order=None, # Optional argument; Wiberg (or similar) bond order\n",
")\n",
"by_hand.add_bond( 1, 2, 2, False) # C = O\n",
"by_hand.add_bond( 1, 3, 1, False) # C - CA\n",
"by_hand.add_bond( 3, 4, 1, False) # CA - CAH\n",
"by_hand.add_bond( 3, 5, 1, False) # CA - CB\n",
"by_hand.add_bond( 5, 6, 1, False) # CB - HB1\n",
"by_hand.add_bond( 5, 7, 1, False) # CB - HB2\n",
"by_hand.add_bond( 5, 8, 1, False) # CB - HB3\n",
"by_hand.add_bond( 3, 9, 1, False) # CB - N+\n",
"by_hand.add_bond( 9, 10, 1, False) # N+ - HN1\n",
"by_hand.add_bond( 9, 11, 1, False) # N+ - HN2\n",
"by_hand.add_bond( 9, 12, 1, False) # N+ - HN3\n",
"by_hand.add_bond(1, 2, 2, False) # C = O\n",
"by_hand.add_bond(1, 3, 1, False) # C - CA\n",
"by_hand.add_bond(3, 4, 1, False) # CA - CAH\n",
"by_hand.add_bond(3, 5, 1, False) # CA - CB\n",
"by_hand.add_bond(5, 6, 1, False) # CB - HB1\n",
"by_hand.add_bond(5, 7, 1, False) # CB - HB2\n",
"by_hand.add_bond(5, 8, 1, False) # CB - HB3\n",
"by_hand.add_bond(3, 9, 1, False) # CB - N+\n",
"by_hand.add_bond(9, 10, 1, False) # N+ - HN1\n",
"by_hand.add_bond(9, 11, 1, False) # N+ - HN2\n",
"by_hand.add_bond(9, 12, 1, False) # N+ - HN3\n",
"\n",
"assert zw_l_alanine.is_isomorphic_with(by_hand)\n",
"\n",
Expand Down Expand Up @@ -829,7 +839,7 @@
"source": [
"with open(\"zw_l_alanine.sdf\", mode=\"rb\") as file:\n",
" sdf_object = Molecule.from_file(file, file_format=\"SDF\")\n",
" \n",
"\n",
"assert zw_l_alanine.is_isomorphic_with(sdf_object)\n",
"sdf_object.visualize()"
]
Expand Down Expand Up @@ -883,7 +893,7 @@
"source": [
"from openff.toolkit.utils import get_data_file_path\n",
"\n",
"path = get_data_file_path('proteins/T4-protein.pdb')\n",
"path = get_data_file_path(\"proteins/T4-protein.pdb\")\n",
"topology = Topology.from_pdb(path)\n",
"protein = topology.molecule(0)\n",
"protein.visualize(\"nglview\")"
Expand Down Expand Up @@ -922,12 +932,14 @@
],
"source": [
"top_from_pdb_from_smiles = Topology.from_pdb(\n",
" get_data_file_path(\"molecules/po4_phenylphosphate.pdb\"), \n",
" unique_molecules=[Molecule.from_smiles(\"P(=O)([O-])([O-])([O-])\"),\n",
" Molecule.from_smiles(\"C1=CC=CC=C1OP(=O)([O-1])([O-1])\")]\n",
") \n",
" get_data_file_path(\"molecules/po4_phenylphosphate.pdb\"),\n",
" unique_molecules=[\n",
" Molecule.from_smiles(\"P(=O)([O-])([O-])([O-])\"),\n",
" Molecule.from_smiles(\"C1=CC=CC=C1OP(=O)([O-1])([O-1])\"),\n",
" ],\n",
")\n",
"\n",
"top_from_pdb_from_smiles.molecule(0).visualize('nglview')"
"top_from_pdb_from_smiles.molecule(0).visualize(\"nglview\")"
]
},
{
Expand All @@ -954,7 +966,7 @@
}
],
"source": [
"top_from_pdb_from_smiles.molecule(1).visualize('nglview')"
"top_from_pdb_from_smiles.molecule(1).visualize(\"nglview\")"
]
},
{
Expand Down Expand Up @@ -987,12 +999,11 @@
],
"source": [
"from openff.toolkit.utils import get_data_file_path\n",
"import nglview\n",
"\n",
"ligand_path = get_data_file_path('molecules/PT2385.sdf')\n",
"ligand_path = get_data_file_path(\"molecules/PT2385.sdf\")\n",
"ligand = Molecule.from_file(ligand_path)\n",
"\n",
"complex_path = get_data_file_path('proteins/5tbm_complex_solv_box.pdb')\n",
"complex_path = get_data_file_path(\"proteins/5tbm_complex_solv_box.pdb\")\n",
"topology = Topology.from_pdb(complex_path, unique_molecules=[ligand])\n",
"\n",
"molecule_smileses = [mol.to_smiles() for mol in topology.molecules]\n",
Expand Down Expand Up @@ -1095,7 +1106,9 @@
}
],
"source": [
"inchi = Molecule.from_inchi(\"InChI=1S/C3H7NO2/c1-2(4)3(5)6/h2H,4H2,1H3,(H,5,6)/t2-/m0/s1\") \n",
"inchi = Molecule.from_inchi(\n",
" \"InChI=1S/C3H7NO2/c1-2(4)3(5)6/h2H,4H2,1H3,(H,5,6)/t2-/m0/s1\"\n",
")\n",
"\n",
"inchi.visualize()"
]
Expand Down Expand Up @@ -1259,14 +1272,16 @@
}
],
"source": [
"# Note that this mapping is off-by-one from the mapping taken \n",
"# Note that this mapping is off-by-one from the mapping taken\n",
"# by the remap method, as Python indexing is 0-based but SMILES\n",
"# is 1-based\n",
"print(\"Before remapping:\", zw_l_alanine.to_smiles(mapped=True))\n",
"\n",
"# Flip the order of the carbonyl carbon and oxygen\n",
"remapped = zw_l_alanine.remap({0: 0,1: 1, 2: 2, 3: 4, 4: 3, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9, 10: 10, 11: 11, 12: 12})\n",
"# ^--------^ \n",
"remapped = zw_l_alanine.remap(\n",
" {0: 0, 1: 1, 2: 2, 3: 4, 4: 3, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9, 10: 10, 11: 11, 12: 12}\n",
")\n",
"# ^--------^\n",
"\n",
"print(\"After remapping: \", remapped.to_smiles(mapped=True))\n",
"\n",
Expand Down Expand Up @@ -1358,7 +1373,7 @@
"source": [
"from openmm.app.pdbfile import PDBFile\n",
"\n",
"openmm_topology = PDBFile('zw_l_alanine.pdb').getTopology()\n",
"openmm_topology = PDBFile(\"zw_l_alanine.pdb\").getTopology()\n",
"openff_topology = Topology.from_openmm(openmm_topology, unique_molecules=[zw_l_alanine])\n",
"\n",
"from_openmm_topology = openff_topology.molecule(0)\n",
Expand Down Expand Up @@ -1437,7 +1452,7 @@
"source": [
"from mdtraj import load_pdb\n",
"\n",
"mdtraj_topology = load_pdb('zw_l_alanine.pdb').topology\n",
"mdtraj_topology = load_pdb(\"zw_l_alanine.pdb\").topology\n",
"openff_topology = Topology.from_openmm(openmm_topology, unique_molecules=[zw_l_alanine])\n",
"\n",
"from_mdtraj_topology = openff_topology.molecule(0)\n",
Expand Down Expand Up @@ -1544,6 +1559,7 @@
],
"source": [
"from rdkit import Chem\n",
"\n",
"rdmol = Chem.MolFromSmiles(\"C[C@H]([NH3+])C([O-])=O\")\n",
"\n",
"print(\"rdmol is of type\", type(rdmol))\n",
Expand Down Expand Up @@ -1815,7 +1831,7 @@
"record = [*client.query_molecules(molecular_formula=\"C16H20N3O5\")][-1]\n",
"\n",
"from_qcarchive = Molecule.from_qcschema(record)\n",
" \n",
"\n",
"from_qcarchive.visualize()"
]
},
Expand Down Expand Up @@ -1889,7 +1905,7 @@
" dataset_type=\"optimization\",\n",
" dataset_name=\"SMIRNOFF Coverage Set 1\",\n",
")\n",
"dimethoxymethanol_optimization = optimization_dataset.get_entry('coc(o)oc-0')\n",
"dimethoxymethanol_optimization = optimization_dataset.get_entry(\"coc(o)oc-0\")\n",
"\n",
"from_optimisation = Molecule.from_qcschema(dimethoxymethanol_optimization)\n",
"\n",
Expand Down

0 comments on commit 2800c38

Please sign in to comment.