Skip to content

Instantly share code, notes, and snippets.

@dominiquesydow
Created April 16, 2019 16:33
Show Gist options
  • Save dominiquesydow/1025167190e07229acd157bf6834a1b7 to your computer and use it in GitHub Desktop.
Save dominiquesydow/1025167190e07229acd157bf6834a1b7 to your computer and use it in GitHub Desktop.
Extract ligand from PDB file
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Extract ligand coordinates by PDB ID and ligand ID"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"from pypdb import *\n",
"import re\n",
"\n",
"def get_ligand_coord(pdb_id, ligand_id, output_path=\"./\"):\n",
" \"\"\"\n",
" Extract ligand coordinates by PDB ID and ligand ID:\n",
" * Get PDB file from PDB (by PDB ID)\n",
" * Extract ligand entry rows in PDB file (by ligand ID) and save as PDB file\n",
" * Also: Return ligand entry rows\n",
" \"\"\"\n",
" \n",
" full_pdb = None # Full content of PDB file\n",
" lig_pdb = None # Ligand coordinates\n",
" \n",
" \n",
" # Get PDB file\n",
" try:\n",
" full_pdb = get_pdb_file(pdb_id)\n",
" except:\n",
" print(\"PDB ID could not be retrieved from PDB.\")\n",
" \n",
" # Extract ligand coordinates\n",
" try:\n",
" lig_pdb = [i for i in full_pdb.split(\"\\n\") if re.search(\"^HETATM\", i) and ligand_id in i]\n",
" except:\n",
" print(\"Ligand ID not found in PDB file.\")\n",
" \n",
" # Write ligand coordinates to file\n",
" with open(output_path + pdb_id + \"_\" + ligand_id + \".pdb\", \"a\") as f:\n",
" for i in lig_pdb:\n",
" f.write(i + \"\\n\")\n",
" \n",
" return lig_pdb"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"# Test with example\n",
"pdb_id = \"3W32\"\n",
"ligand_id = \"W32\""
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"# Save file to current directory\n",
"ligand_coords = get_ligand_coord(pdb_id, ligand_id)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"['HETATM 2547 C1 W32 A1101 17.523 28.990 5.798 1.00 31.51 C ',\n",
" 'HETATM 2548 C2 W32 A1101 16.291 28.418 6.080 1.00 30.31 C ',\n",
" 'HETATM 2549 C3 W32 A1101 17.399 32.400 11.069 1.00 27.58 C ',\n",
" 'HETATM 2550 C4 W32 A1101 17.766 30.265 6.269 1.00 29.96 C ',\n",
" 'HETATM 2551 C5 W32 A1101 17.620 31.995 9.772 1.00 28.42 C ',\n",
" 'HETATM 2552 C6 W32 A1101 15.605 30.408 7.268 1.00 28.58 C ',\n",
" 'HETATM 2553 C7 W32 A1101 15.786 33.991 10.313 1.00 27.15 C ',\n",
" 'HETATM 2554 C8 W32 A1101 12.748 33.566 13.379 1.00 31.64 C ',\n",
" 'HETATM 2555 C9 W32 A1101 14.942 34.492 14.574 1.00 35.19 C ',\n",
" 'HETATM 2556 C10 W32 A1101 15.370 29.134 6.809 1.00 25.91 C ',\n",
" 'HETATM 2557 C11 W32 A1101 16.480 33.398 11.352 1.00 28.93 C ',\n",
" 'HETATM 2558 C12 W32 A1101 16.830 30.965 6.994 1.00 32.48 C ',\n",
" 'HETATM 2559 C13 W32 A1101 16.923 32.597 8.743 1.00 27.90 C ',\n",
" 'HETATM 2560 C14 W32 A1101 15.995 33.592 9.009 1.00 29.12 C ',\n",
" 'HETATM 2561 C15 W32 A1101 13.699 34.562 15.192 1.00 29.35 C ',\n",
" 'HETATM 2562 C16 W32 A1101 14.989 33.935 13.305 1.00 35.49 C ',\n",
" 'HETATM 2563 C17 W32 A1101 16.163 35.029 15.170 1.00 34.55 C ',\n",
" 'HETATM 2564 C18 W32 A1101 16.443 35.196 16.465 1.00 31.64 C ',\n",
" 'HETATM 2565 C19 W32 A1101 17.734 35.864 16.810 1.00 41.85 C ',\n",
" 'HETATM 2566 C20 W32 A1101 15.566 34.815 17.582 1.00 34.23 C ',\n",
" 'HETATM 2567 C21 W32 A1101 14.310 35.640 17.409 1.00 33.52 C ',\n",
" 'HETATM 2568 C22 W32 A1101 23.039 35.053 18.700 1.00 86.61 C ',\n",
" 'HETATM 2569 C23 W32 A1101 20.048 36.322 16.157 1.00 53.53 C ',\n",
" 'HETATM 2570 C24 W32 A1101 20.735 35.538 17.258 1.00 62.21 C ',\n",
" 'HETATM 2571 C25 W32 A1101 14.038 28.522 7.120 1.00 27.55 C ',\n",
" 'HETATM 2572 N26 W32 A1101 12.598 34.091 14.590 1.00 29.72 N ',\n",
" 'HETATM 2573 N27 W32 A1101 13.891 33.461 12.695 1.00 32.32 N ',\n",
" 'HETATM 2574 N28 W32 A1101 13.391 35.060 16.456 1.00 30.86 N ',\n",
" 'HETATM 2575 N29 W32 A1101 16.248 33.857 12.650 1.00 28.23 N ',\n",
" 'HETATM 2576 N30 W32 A1101 18.733 35.791 15.867 1.00 40.18 N ',\n",
" 'HETATM 2577 O31 W32 A1101 17.835 36.482 17.860 1.00 50.74 O ',\n",
" 'HETATM 2578 O32 W32 A1101 23.066 36.437 16.489 1.00 63.85 O ',\n",
" 'HETATM 2579 O33 W32 A1101 21.997 37.443 18.557 1.00 81.42 O ',\n",
" 'HETATM 2580 O34 W32 A1101 17.118 32.249 7.424 1.00 30.16 O ',\n",
" 'HETATM 2581 F35 W32 A1101 14.097 27.611 8.115 1.00 35.13 F ',\n",
" 'HETATM 2582 F36 W32 A1101 13.166 29.438 7.562 1.00 34.11 F ',\n",
" 'HETATM 2583 F37 W32 A1101 13.559 27.856 6.039 1.00 32.07 F ',\n",
" 'HETATM 2584 S38 W32 A1101 22.276 36.305 17.700 1.00 84.11 S ',\n",
" 'HETATM 2585 CL1 W32 A1101 15.137 34.343 7.702 1.00 34.16 CL ']"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Look at ligand PDB lines (those were written to file)\n",
"ligand_coords"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": [
"# Save file to other directory, e.g.\n",
"ligand_coords = get_ligand_coord(pdb_id, ligand_id, \"/home/dominique/Downloads/\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "teachopencadd",
"language": "python",
"name": "teachopencadd"
},
"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.6.7"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment