Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
nifets committed Mar 24, 2023
0 parents commit f368db4
Show file tree
Hide file tree
Showing 42 changed files with 47,833 additions and 0 deletions.
2,001 changes: 2,001 additions & 0 deletions Manifest.toml

Large diffs are not rendered by default.

121 changes: 121 additions & 0 deletions PDBbuilder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
# https://bioinformatics.stackexchange.com/questions/19570/read-pdb-file-extract-dihedral-angles-modify-dihedral-angles-reconstruct-cart#
# ^ reference for this code


from Bio import PDB
import math

from Bio.PDB import PDBIO
import Bio.PDB.internal_coords as ic

class ChainSelect(PDB.Select):
def accept_chain(self, chain):
if chain.id == 'A':
return True
else:
return False

def pdb_from_dihedral_angles(structure: PDB.Structure.Structure,
output_path: str,
phi_angles,
psi_angles):
# Internally calculate dihedral angles for the structure
structure.atom_to_internal_coordinates()

# Assume we care about the first chain only
# internal_coords extends the chain class to include dihedral angles (and other info)
chain: ic.IC_Chain = list(structure.get_chains())[0].internal_coord

# Dictionary of all dihedral angles in the chain
all_angles = chain.dihedra

# Find the phi and psi dihedral angles
# key[i]: ic.AtomKey
# key[i].akl[3]: gives the atom name - //kind of confusing representation
phi_keys = [k for k in all_angles.keys() if k[0].akl[3] == 'C' and
k[1].akl[3] == 'N' and
k[2].akl[3] == 'CA' and
k[3].akl[3] == 'C']
psi_keys = [k for k in all_angles.keys() if k[0].akl[3] == 'N' and
k[1].akl[3] == 'CA' and
k[2].akl[3] == 'C' and
k[3].akl[3] == 'N']


assert(len(phi_keys) == len(phi_angles))
assert(len(psi_keys) == len(psi_angles))

# Update phi angles
# Note that angles are stored as degrees in ic
phi_angles_degrees = list(map(math.degrees, phi_angles))
for i, k in enumerate(phi_keys):
all_angles[k].angle = phi_angles_degrees[i]

# Update psi angles
psi_angles_degrees = list(map(math.degrees, psi_angles))
for i, k in enumerate(psi_keys):
all_angles[k].angle = psi_angles_degrees[i]

# Now that the dihedral angles have been changed, need to recompute cartesian coords
structure.internal_to_atom_coordinates()




# Write PDB file
io = PDBIO()
io.set_structure(structure)
io.save(output_path, ChainSelect())


parser = PDB.PDBParser()
structure = parser.get_structure("1A3N", "data/1A3N.pdb")
pdb_from_dihedral_angles(structure, "output/n1A3N.pdb", [0] * 140, [0] * 140)

# structure.atom_to_internal_coordinates()

# chain = list(structure.get_chains())[0]
# ic_chain: PDB.internal_coords.IC_Chain = chain.internal_coord
# d: Dict[Tuple[PDB.internal_coords.AtomKey,
# PDB.internal_coords.AtomKey,
# PDB.internal_coords.AtomKey,
# PDB.internal_coords.AtomKey],
# PDB.internal_coords.Dihedron] = ic_chain.dihedra



# cnt = 1
# for key in d:


# if w == 'N':
# if key[1].akl[3] == 'CA':
# if key[2].akl[3] == 'C':
# if key[3].akl[3] == 'N':

# print ('\n',cnt,' : ', [x.akl[3] for x in key], d[key].angle)

# d[key].angle += 45

# cnt += 1

# cnt = 1
# for key in d:


# if key[0].akl[3] == 'N':
# if key[1].akl[3] == 'CA':
# if key[2].akl[3] == 'C':
# if key[3].akl[3] == 'N':

# print ('\n',cnt,' : ', [x.akl[3] for x in key], d[key].angle)

# cnt += 1

# structure.internal_to_atom_coordinates(verbose = True)

# io = PDBIO()

# io.set_structure(structure)

# io.save('output/atom_coord.pdb', preserve_atom_numbering=True)
35 changes: 35 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name = "TorusEvol"
uuid = "bb045c0a-7050-477d-931f-b94f09181689"
authors = ["Stefan Manolache <[email protected]>"]
version = "0.1.0"

[deps]
Bio3DView = "99c8bb3a-9d13-5280-9740-b4880ed9c598"
BioAlignments = "00701ae9-d1dc-5365-b64a-a3a3ebf5695e"
BioSequences = "7e6ae17a-c86d-528c-b3b9-7f778a29fe59"
BioStructures = "de9282ab-8554-53be-b2d6-f6c222edabfc"
BioSymbols = "3c28c6f8-a34d-59c4-9654-267d177fcfa9"
Blink = "ad839575-38b3-5650-b840-f874b8c74a25"
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
DelimitedFiles = "8bb1440f-4735-579b-a4ab-409b98df4dab"
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
DistributionsAD = "ced4e74d-a319-5a8a-b0ac-84af2272839c"
FastaIO = "a0c94c4b-ebed-5953-b5fc-82fe598ac79f"
Formatting = "59287772-0a20-5a39-b81b-1366585eb4c0"
HypothesisTests = "09f84164-cd44-5f33-b23f-e6b0d136a0d5"
IJulia = "7073ff75-c697-5162-941a-fcdaad2a7d2a"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
LogExpFunctions = "2ab3a3ac-af41-5b50-aa03-7779005ae688"
MCMCChains = "c7f686f2-ff18-58e9-bc7b-31028e88f75d"
Memoization = "6fafb56a-5788-4b4e-91ca-c0cea6611c73"
OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"
PDMats = "90014a1f-27ba-587c-ab20-58faa44d9150"
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
StatsPlots = "f3b207a7-027a-5e70-b257-86293d7955fd"
SubstitutionModels = "8365b1bb-bd83-58ee-a267-f2965fc81c73"
Turing = "fce5fe82-541a-59a6-adf8-730c64b5f9a0"
WebIO = "0f1e0344-ec1d-5b48-a673-e5cf874b6c29"
Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"
148 changes: 148 additions & 0 deletions ProteinViewer.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "5c7b2f8e3a274d1c89c6ce833bbaac71",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"A Jupyter Widget"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"<Chain id=A>\n"
]
},
{
"ename": "ValueError",
"evalue": "I/O operation on closed file",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn [1], line 11\u001b[0m\n\u001b[0;32m 8\u001b[0m chain \u001b[39m=\u001b[39m \u001b[39mlist\u001b[39m(structure\u001b[39m.\u001b[39mget_chains())[\u001b[39m0\u001b[39m]\n\u001b[0;32m 9\u001b[0m \u001b[39mprint\u001b[39m(chain)\n\u001b[1;32m---> 11\u001b[0m nv\u001b[39m.\u001b[39;49mshow_biopython(structure)\n",
"File \u001b[1;32mc:\\Users\\stefa\\Dropbox\\Jotun Hein - Part C Projects\\Manolache\\Code\\Evolutionary Toroidal Difussions\\TorusEvol\\.venv\\lib\\site-packages\\nglview\\show.py:452\u001b[0m, in \u001b[0;36mshow_biopython\u001b[1;34m(entity, **kwargs)\u001b[0m\n\u001b[0;32m 437\u001b[0m \u001b[39m'''Show NGL widget with Biopython structural entity.\u001b[39;00m\n\u001b[0;32m 438\u001b[0m \n\u001b[0;32m 439\u001b[0m \u001b[39mTakes a Structure, Model, Chain, Residue or Atom\u001b[39;00m\n\u001b[1;32m (...)\u001b[0m\n\u001b[0;32m 449\u001b[0m \u001b[39m... w\u001b[39;00m\n\u001b[0;32m 450\u001b[0m \u001b[39m'''\u001b[39;00m\n\u001b[0;32m 451\u001b[0m entity \u001b[39m=\u001b[39m BiopythonStructure(entity)\n\u001b[1;32m--> 452\u001b[0m \u001b[39mreturn\u001b[39;00m NGLWidget(entity, \u001b[39m*\u001b[39m\u001b[39m*\u001b[39mkwargs)\n",
"File \u001b[1;32mc:\\Users\\stefa\\Dropbox\\Jotun Hein - Part C Projects\\Manolache\\Code\\Evolutionary Toroidal Difussions\\TorusEvol\\.venv\\lib\\site-packages\\nglview\\widget.py:245\u001b[0m, in \u001b[0;36mNGLWidget.__init__\u001b[1;34m(self, structure, representations, parameters, **kwargs)\u001b[0m\n\u001b[0;32m 243\u001b[0m \u001b[39melse\u001b[39;00m:\n\u001b[0;32m 244\u001b[0m \u001b[39mif\u001b[39;00m structure \u001b[39mis\u001b[39;00m \u001b[39mnot\u001b[39;00m \u001b[39mNone\u001b[39;00m:\n\u001b[1;32m--> 245\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39madd_structure(structure, \u001b[39m*\u001b[39m\u001b[39m*\u001b[39mkwargs)\n\u001b[0;32m 247\u001b[0m \u001b[39mif\u001b[39;00m representations:\n\u001b[0;32m 248\u001b[0m \u001b[39m# If initial representations are provided,\u001b[39;00m\n\u001b[0;32m 249\u001b[0m \u001b[39m# we need to set defaultRepresentation to False\u001b[39;00m\n\u001b[0;32m 250\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mrepresentations \u001b[39m=\u001b[39m representations\n",
"File \u001b[1;32mc:\\Users\\stefa\\Dropbox\\Jotun Hein - Part C Projects\\Manolache\\Code\\Evolutionary Toroidal Difussions\\TorusEvol\\.venv\\lib\\site-packages\\nglview\\widget.py:1113\u001b[0m, in \u001b[0;36mNGLWidget.add_structure\u001b[1;34m(self, structure, **kwargs)\u001b[0m\n\u001b[0;32m 1111\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39mnot\u001b[39;00m \u001b[39misinstance\u001b[39m(structure, Structure):\n\u001b[0;32m 1112\u001b[0m \u001b[39mraise\u001b[39;00m \u001b[39mValueError\u001b[39;00m(\u001b[39mf\u001b[39m\u001b[39m'\u001b[39m\u001b[39m{\u001b[39;00mstructure\u001b[39m}\u001b[39;00m\u001b[39m is not an instance of Structure\u001b[39m\u001b[39m'\u001b[39m)\n\u001b[1;32m-> 1113\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_load_data(structure, \u001b[39m*\u001b[39m\u001b[39m*\u001b[39mkwargs)\n\u001b[0;32m 1114\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_ngl_component_ids\u001b[39m.\u001b[39mappend(structure\u001b[39m.\u001b[39mid)\n\u001b[0;32m 1115\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mn_components \u001b[39m>\u001b[39m \u001b[39m1\u001b[39m:\n",
"File \u001b[1;32mc:\\Users\\stefa\\Dropbox\\Jotun Hein - Part C Projects\\Manolache\\Code\\Evolutionary Toroidal Difussions\\TorusEvol\\.venv\\lib\\site-packages\\nglview\\widget.py:1231\u001b[0m, in \u001b[0;36mNGLWidget._load_data\u001b[1;34m(self, obj, **kwargs)\u001b[0m\n\u001b[0;32m 1229\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39mnot\u001b[39;00m is_url:\n\u001b[0;32m 1230\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39mhasattr\u001b[39m(obj, \u001b[39m'\u001b[39m\u001b[39mget_structure_string\u001b[39m\u001b[39m'\u001b[39m):\n\u001b[1;32m-> 1231\u001b[0m blob \u001b[39m=\u001b[39m obj\u001b[39m.\u001b[39;49mget_structure_string()\n\u001b[0;32m 1232\u001b[0m kwargs2[\u001b[39m'\u001b[39m\u001b[39mext\u001b[39m\u001b[39m'\u001b[39m] \u001b[39m=\u001b[39m obj\u001b[39m.\u001b[39mext\n\u001b[0;32m 1233\u001b[0m passing_buffer \u001b[39m=\u001b[39m \u001b[39mTrue\u001b[39;00m\n",
"File \u001b[1;32mc:\\Users\\stefa\\Dropbox\\Jotun Hein - Part C Projects\\Manolache\\Code\\Evolutionary Toroidal Difussions\\TorusEvol\\.venv\\lib\\site-packages\\nglview\\adaptor.py:200\u001b[0m, in \u001b[0;36mBiopythonStructure.get_structure_string\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m 198\u001b[0m io_str \u001b[39m=\u001b[39m StringIO()\n\u001b[0;32m 199\u001b[0m io_pdb\u001b[39m.\u001b[39msave(io_str)\n\u001b[1;32m--> 200\u001b[0m \u001b[39mreturn\u001b[39;00m io_str\u001b[39m.\u001b[39;49mgetvalue()\n",
"\u001b[1;31mValueError\u001b[0m: I/O operation on closed file"
]
}
],
"source": [
"import nglview as nv\n",
"from Bio.PDB import PDBParser\n",
"\n",
"parser = PDBParser()\n",
"\n",
"with open(\"data/m1A3N.pdb\") as handle:\n",
" record = next(SeqIO.parse(handle, \"fasta\"))\n",
" human_a = record.seq\n",
" print(human_a)\n",
"structure = parser.get_structure(\"m1A3N\", \"m1A3N.pdb\")\n",
"\n",
"chain = list(structure.get_chains())[0]\n",
"print(chain)\n",
"\n",
"nv.show_biopython(structure)\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"import nglview as nv\n",
"\n",
"\n",
"w = nv.show_structure_file(\"output/n1A3N.pdb\")\n",
"w.clear()\n",
"w.add_representation(\"backbone\")\n",
"\n",
"for i in range(10):\n",
" if i % 2 == 0:\n",
" w = nv.show_structure_file(\"output/n1A3N.pdb\")\n",
" else:\n",
" w = nv.show_structure_file(\"output/m1A3N.pdb\")\n",
" w.clear()\n",
" w.add_representation(\"backbone\")\n",
" w"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "abbeed12461a445698b89c1caa5e72ec",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"A Jupyter Widget"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"u = nv.show_structure_file(\"output/n1A3N.pdb\")\n",
"u"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.9.6 ('.venv': venv)",
"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.6 (tags/v3.9.6:db3ff76, Jun 28 2021, 15:26:21) [MSC v.1929 64 bit (AMD64)]"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "9f23c5b2d7923a9bf6ddcca76ea127105863b859cb6fc4879d608de940540fb9"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
1,279 changes: 1,279 additions & 0 deletions archive/.ipynb_checkpoints/hello_bio-checkpoint.ipynb

Large diffs are not rendered by default.

Loading

0 comments on commit f368db4

Please sign in to comment.