Skip to content

Commit

Permalink
Some bugs fixed
Browse files Browse the repository at this point in the history
dprada committed Dec 17, 2024
1 parent b9204c5 commit 8ca7065
Showing 8 changed files with 292 additions and 55 deletions.
17 changes: 9 additions & 8 deletions molsysmt/basic/view.py
Original file line number Diff line number Diff line change
@@ -134,23 +134,24 @@ def view(molecular_system=None, selection='all', structure_indices='all',

if with_water_as == 'surface':
if viewer=='NGLView':
from molsysmt.thirds.nglview import show_system_as_transparent_surface
show_system_as_transparent_surface(tmp_item)
from molsysmt.thirds.nglview import show_as_surface
show_as_surface(tmp_item, selection='molecule_type=="water"',
opacity=0.2, color='lightblue', skip_digestion=True)
elif with_water_as == 'licorice':
if viewer=='NGLView':
from molsysmt.thirds.nglview import show_water_as_licorice
show_water_as_licorice(tmp_item)
from molsysmt.thirds.nglview import show_as_licorice
show_as_licorice(tmp_item, selection='molecule_type=="water"')

if with_ions_as is not None:

if with_ions_as == 'licorice':
if viewer=='NGLView':
from molsysmt.thirds.nglview import show_ions_as_licorice
show_ions_as_licorice(tmp_item)
from molsysmt.thirds.nglview import show_as_licorice
show_as_licorice(tmp_item, selection='molecule_type=="ion"')
elif with_ions_as in ['balls and sticks', 'balls']:
if viewer=='NGLView':
from molsysmt.thirds.nglview import show_ions_as_ball_and_stick
show_ions_as_ball_and_stick(tmp_item)
from molsysmt.thirds.nglview import show_as_balls_and_sticks
show_as_balls_and_sticks(tmp_item)


return tmp_item
13 changes: 13 additions & 0 deletions molsysmt/build/solvate.py
Original file line number Diff line number Diff line change
@@ -116,6 +116,19 @@ def solvate (molecular_system, box_shape="truncated octahedral", clearance='14.0
ionicStrength=ionic_strength, positiveIon=cation,
negativeIon=anion)

# fixing a bug of OpenMM
list_atoms = list(modeller.topology.atoms())
atom_id = int(list_atoms[0].id)
for atom in list_atoms[1:]:
atom_id += 1
atom.id = str(atom_id)
list_residues = list(modeller.topology.residues())
residue_id = int(list_residues[0].id)
for residue in list_residues[1:]:
residue_id += 1
residue.id = str(residue_id)
#

tmp_item = convert(modeller, to_form=to_form)

del(modeller)
9 changes: 7 additions & 2 deletions molsysmt/element/entity/get_entity_index.py
Original file line number Diff line number Diff line change
@@ -8,7 +8,10 @@ def get_entity_index(molecular_system, element='atom', selection='all',

if redefine_molecules or redefine_indices:

from ..molecule import get_molecule_name, get_molecule_type
from ..molecule import get_molecule_name, get_molecule_type, get_molecule_index

molecule_index_from_atoms = get_molecule_index(molecular_system, element='atom',
selection=selection, redefine_indices=redefine_molecules, syntax=syntax, skip_digestion=True)

molecule_name_from_molecules = get_molecule_name(molecular_system, element='molecule',
selection=selection, redefine_indices=redefine_molecules, syntax=syntax, skip_digestion=True)
@@ -74,7 +77,9 @@ def get_entity_index(molecular_system, element='atom', selection='all',

output.append(entity_index)

if element=='molecule':
if element=='atom':
output=[output[ii] for ii in molecule_index_from_atoms]
elif element=='molecule':
output=output
elif element=='entity':
output=np.unique(output).tolist()
2 changes: 1 addition & 1 deletion molsysmt/element/molecule/get_molecule_index.py
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@

@digest()
def get_molecule_index(molecular_system, element='atom', selection='all',
redefine_components=False, redefine_indices=False, syntax='MolSysMT'):
redefine_components=False, redefine_indices=False, syntax='MolSysMT', skip_digestion=False):

if redefine_indices:

7 changes: 4 additions & 3 deletions molsysmt/thirds/nglview/show_as_balls_and_sticks.py
Original file line number Diff line number Diff line change
@@ -4,8 +4,8 @@
# https://github.com/arose/ngl/blob/master/doc/usage/selection-language.md


@digest()
def show_as_balls_and_sticks(view, selection='all'):
@digest(form='nglview.NGLWidget')
def show_as_balls_and_sticks(view, selection='all', skip_digestion=False):

from molsysmt import select

@@ -18,7 +18,8 @@ def show_as_balls_and_sticks(view, selection='all'):
nglview_selection='ion'

if nglview_selection is None:
nglview_selection = select(view, element='atom', selection=selection, to_syntax='NGLView')
nglview_selection = select(view, element='atom', selection=selection, to_syntax='NGLView',
skip_digestion=True)

view.add_ball_and_stick(selection=nglview_selection)

7 changes: 4 additions & 3 deletions molsysmt/thirds/nglview/show_as_licorice.py
Original file line number Diff line number Diff line change
@@ -4,8 +4,8 @@
# https://github.com/arose/ngl/blob/master/doc/usage/selection-language.md


@digest()
def show_as_licorice(view, selection='all'):
@digest(form='nglview.NGLWidget')
def show_as_licorice(view, selection='all', skip_digestion=False):

from molsysmt import select

@@ -18,7 +18,8 @@ def show_as_licorice(view, selection='all'):
nglview_selection='ion'

if nglview_selection is None:
nglview_selection = select(view, element='atom', selection=selection, to_syntax='NGLView')
nglview_selection = select(view, element='atom', selection=selection, to_syntax='NGLView',
skip_digestion=True)

view.add_licorice(selection=nglview_selection)

9 changes: 6 additions & 3 deletions molsysmt/thirds/nglview/show_as_surface.py
Original file line number Diff line number Diff line change
@@ -4,8 +4,8 @@
# https://github.com/arose/ngl/blob/master/doc/usage/selection-language.md


@digest()
def show_as_surface(view, selection='all', opacity=0.3, color='lightblue'):
@digest(form='nglview.NGLWidget')
def show_as_surface(view, selection='all', opacity=0.3, color='lightblue', skip_digestion=False):

from molsysmt import select

@@ -14,9 +14,12 @@ def show_as_surface(view, selection='all', opacity=0.3, color='lightblue'):
if isinstance(selection, str):
if selection=='molecule_type=="water"' or selection=='group_type=="water"':
nglview_selection='water'
elif selection=='molecule_type=="ion"' or selection=='group_type=="ion"':
nglview_selection='ion'

if nglview_selection is None:
nglview_selection = select(view, element='atom', selection=selection, to_syntax='NGLView')
nglview_selection = select(view, element='atom', selection=selection, to_syntax='NGLView',
skip_digestion=True)

view.add_surface(selection=nglview_selection, opacity=opacity, color=color)

283 changes: 248 additions & 35 deletions sandbox/Tests.ipynb
Original file line number Diff line number Diff line change
@@ -12,13 +12,13 @@
{
"cell_type": "code",
"execution_count": 1,
"id": "89627ab5-532e-4815-ac94-3c3295fb685c",
"id": "1f40cd08-c65b-4252-bd33-013ee3d34f68",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "48a31e76db0a4b61b2eb70c441b239af",
"model_id": "0619b99e074e46f8a04fb6a7dab35a3e",
"version_major": 2,
"version_minor": 0
},
@@ -29,72 +29,285 @@
}
],
"source": [
"import molsysmt as msm"
"from openmm import app\n",
"from openmm import *\n",
"from openmm import unit as u\n",
"import molsysmt as msm\n",
"from sys import stdout"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "0e199614-dae1-4654-9144-fc343bca8f35",
"execution_count": 2,
"id": "9ad5a98b-4549-41cf-bf96-975de0523b97",
"metadata": {},
"outputs": [],
"source": [
"molsys = msm.convert(msm.systems['chicken villin HP35']['chicken_villin_HP35_solvated.h5msm'])"
"pdb = app.PDBFile('V5.pdb')"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "988e1738-da74-4fc2-8dc1-e0c4c290d13f",
"metadata": {},
"outputs": [],
"source": [
"modeller = app.Modeller(pdb.topology, pdb.positions)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "12e927f6-6450-4c4a-81c1-55e79288b398",
"metadata": {},
"outputs": [],
"source": [
"forcefield = app.ForceField(\"amber14-all.xml\", \"amber14/tip3p.xml\")"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "8e2fb113-efb1-4766-b312-dac76a767d8a",
"metadata": {},
"outputs": [],
"source": [
"modeller.addSolvent(forcefield, model='tip3p', padding=1.4*u.nanometers, boxShape='cube')"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "ebf4eea3-0855-4897-a99f-6343342faba2",
"metadata": {},
"outputs": [],
"source": [
"aa = list(modeller.topology.atoms())"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "a211cb38-8e95-4c68-8dc3-9031804953eb",
"id": "c86fa1f8-cb3d-434f-bcdf-704c7283b568",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1236"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
"ename": "AttributeError",
"evalue": "property 'symbol' of 'Element' object has no setter",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[11], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43maa\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m-\u001b[39;49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m]\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43melement\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msymbol\u001b[49m\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mCL\u001b[39m\u001b[38;5;124m'\u001b[39m\n",
"\u001b[0;31mAttributeError\u001b[0m: property 'symbol' of 'Element' object has no setter"
]
}
],
"source": [
"msm.element.component.get_n_components(molsys, selection='all', redefine_components=False)"
"aa[-1].element.symbol='CL'"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "1968e865-1cdb-422a-bd3a-d483b2894bb5",
"execution_count": null,
"id": "c33deeab-1265-4ea2-9443-946d91814064",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1236"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"app.PDBFile.writeFile(modeller.topology, modeller.positions, file=\"V5_solvated.pdb\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "897596e5-8f57-436e-8e4a-131545a37db6",
"metadata": {},
"outputs": [],
"source": [
"msm.view(modeller, standard=True, with_water_as='licorice', with_ions_as='balls')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1f1211f1-9065-466f-93e6-008575fb6bc0",
"metadata": {},
"outputs": [],
"source": [
"msm.info(modeller)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "db7fd8f0-2769-41dc-9d2e-ac72afe48a97",
"metadata": {},
"outputs": [],
"source": [
"system = forcefield.createSystem(modeller.topology, nonbondedMethod=app.PME, nonbondedCutoff=1.2*u.nanometer, constraints=app.HBonds)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "62ce2c8c-d38d-4973-984e-10c317a18b0b",
"metadata": {},
"outputs": [],
"source": [
"friction = 1 / u.picosecond\n",
"timestep = 2 * u.femtoseconds\n",
"temperature = 500 * u.kelvin\n",
"integrator = LangevinIntegrator(temperature, friction, timestep)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6d258d16-0c94-4e06-bf66-8263ba13ee78",
"metadata": {},
"outputs": [],
"source": [
"platform = Platform.getPlatformByName(\"CUDA\") # Usa \"CPU\" si no tienes GPU"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0aff61fa-da0a-4678-8f56-bde71e1fa2ea",
"metadata": {},
"outputs": [],
"source": [
"simulation = app.Simulation(modeller.topology, system, integrator, platform)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cface5cf-a7fa-4df4-91da-fdc3e11d6fd2",
"metadata": {},
"outputs": [],
"source": [
"simulation.context.setPositions(modeller.positions)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "de4b19ec-35ac-47d3-898b-6f909dfa817d",
"metadata": {},
"outputs": [],
"source": [
"simulation.minimizeEnergy()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3ea1a731-ef78-4daa-be9f-9ef91a701762",
"metadata": {},
"outputs": [],
"source": [
"reporter1 = app.DCDReporter('traj.dcd', 2000)\n",
"reporter2 = app.StateDataReporter(stdout, 10000, step=True, potentialEnergy=True, temperature=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "386fd3bd-b22f-428d-8bb8-c106b58cfcdc",
"metadata": {},
"outputs": [],
"source": [
"simulation.reporters = [reporter1, reporter2]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3343a7f6-739e-4690-81b1-1a3861944051",
"metadata": {},
"outputs": [],
"source": [
"simulation.step(50000)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "82d070fd-5922-4989-ae19-bcea3fca0974",
"metadata": {},
"outputs": [],
"source": [
"molsys = msm.convert(['V5_solvated.pdb','traj.dcd'])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6fab4260-b78c-4fd5-a61d-f928037bfcaf",
"metadata": {},
"outputs": [],
"source": [
"msm.element.component.get_n_components(molsys, selection='all', redefine_components=True)"
"msm.info(molsys)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ced1720f-6835-4840-b8f8-168a246e68cd",
"id": "3e626d48-d01f-4284-9b64-9a393db977a4",
"metadata": {},
"outputs": [],
"source": [
"msm.view(molsys, standard=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d8593a28-3501-4a87-8650-42187e9a278c",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "189e9904-d8ca-4f25-86e6-c22b8b4b906d",
"metadata": {},
"outputs": [],
"source": [
" molsys = msm.convert(msm.systems['chicken villin HP35']['chicken_villin_HP35_solvated.h5msm'])\n",
" n_components_1 = msm.element.component.get_n_components(molsys, selection='all', redefine_components=False)\n",
" n_components_2 = msm.element.component.get_n_components(molsys, selection='all', redefine_components=True)\n"
"msm.element.entity.get_entity_index(pdb.topology, element='atom', selection='all', redefine_molecules=True,\n",
" redefine_indices=True, skip_digestion=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0605093c-a56b-4aa1-8a68-e82741f92e6d",
"metadata": {},
"outputs": [],
"source": [
"msm.get(pdb.topology, element='atom', entity_index=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2d623284-58f8-466e-a6c3-8f861d1b3195",
"metadata": {},
"outputs": [],
"source": [
"msm.info(pdb.topology)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0102bbb3-cb2c-4f53-a0d5-32ac2395afc4",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {

0 comments on commit 8ca7065

Please sign in to comment.