Skip to content

Commit

Permalink
in process
Browse files Browse the repository at this point in the history
  • Loading branch information
dprada committed Oct 27, 2023
1 parent 20b62d2 commit 410eec0
Show file tree
Hide file tree
Showing 20 changed files with 503 additions and 159 deletions.
26 changes: 26 additions & 0 deletions molsysmt/_private/digestion/argument/file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from ...exceptions import ArgumentError

def digest_file(file, caller=None):

if caller.endswith('write_topology_in_msmh5'):

from h5py._hl.files import File as h5py_File
from molsysmt.native import MSMH5FileHandler

if isinstance(file, h5py_File):
if 'type' in file.attrs:
return file

elif isinstance(file, MSMH5FileHandler):
return file

else:

from molsysmt.form.file_msmh5.is_form import is_form as is_file_msmh5_form
file_is_msmh5 = is_file_msmh5_form(file)
if file_is_msmh5:
return file


raise ArgumentError('file', value=file, caller=caller, message=None)

2 changes: 2 additions & 0 deletions molsysmt/form/openmm_Simulation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from .iterators import StructuresIterator, TopologyIterator

from .to_file_pdb import to_file_pdb
from .to_file_msmpk import to_file_msmpk
from .to_molsysmt_Topology import to_molsysmt_Topology
from .to_openmm_Context import to_openmm_Context
from .to_openmm_Topology import to_openmm_Topology
Expand All @@ -28,6 +29,7 @@
_convert_to={
'openmm.Simulation': extract,
'file:pdb': to_file_pdb,
'file:msmpk': to_file_msmpk,
'molsysmt.Topology': to_molsysmt_Topology,
'openmm.Context': to_openmm_Context,
'openmm.Topology': to_openmm_Topology,
Expand Down
4 changes: 2 additions & 2 deletions molsysmt/form/openmm_Simulation/is_form.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
def is_form(item):

item_fullname = item.__class__.__module__+'.'+item.__class__.__name__
from openmm.app.simulation import Simulation

return 'openmm.Simulation'==item_fullname
return isinstance(item, Simulation)

15 changes: 15 additions & 0 deletions molsysmt/form/openmm_Simulation/to_file_msmpk.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from molsysmt._private.digestion import digest

@digest(form='openmm.Simulation')
def to_file_msmpk(item, atom_indices='all', structure_indices='all', output_filename=None):

from . import to_molsysmt_MolSys as openmm_Simulation_to_molsysmt_MolSys
from ..molsysmt_MolSys import to_file_msmpk as molsysmt_MolSys_to_file_msmpk

tmp_item = openmm_Simulation_to_molsysmt_MolSys(item, atom_indices=atom_indices,
structure_indices=structure_indices)

tmp_item = molsysmt_MolSys_to_file_msmpk(tmp_item, output_filename=output_filename)

return tmp_item

2 changes: 1 addition & 1 deletion molsysmt/form/openmm_Simulation/to_molsysmt_MolSys.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def to_molsysmt_MolSys(item, atom_indices='all', structure_indices='all'):

tmp_item = MolSys()
tmp_item.topology = openmm_Simulation_to_molsysmt_Topology(item, atom_indices=atom_indices)
tmp_item.trajectory = openmm_Simulation_to_molsysmt_Structures(item, atom_indices=atom_indices, structure_indices=structure_indices)
tmp_item.structures = openmm_Simulation_to_molsysmt_Structures(item, atom_indices=atom_indices, structure_indices=structure_indices)

return tmp_item

2 changes: 1 addition & 1 deletion molsysmt/form/openmm_Simulation/to_openmm_Topology.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
@digest(form='openmm.Simulation')
def to_openmm_Topology(item, atom_indices='all'):

from molsysmt.form.openmm_Topology import extract_openmm_Topology
from molsysmt.form.openmm_Topology import extract as extract_openmm_Topology

tmp_item = item.topology
tmp_item = extract_openmm_Topology(tmp_item, atom_indices=atom_indices)
Expand Down
4 changes: 2 additions & 2 deletions molsysmt/form/openmm_Topology/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@
from .set import *
from .iterators import TopologyIterator

from .write_topology_in_msmh5 import write_topology_in_msmh5

from .to_string_pdb_text import to_string_pdb_text
from .to_file_pdb import to_file_pdb
from .to_file_psf import to_file_psf
from .to_file_msmh5 import to_file_msmh5
from .to_molsysmt_MolSys import to_molsysmt_MolSys
from .to_molsysmt_Topology import to_molsysmt_Topology
from .to_mdtraj_Topology import to_mdtraj_Topology
Expand All @@ -39,7 +40,6 @@
'string:pdb_text': to_string_pdb_text,
'file:pdb': to_file_pdb,
'file:psf': to_file_psf,
'file:msmh5': to_file_msmh5,
'molsysmt.MolSys': to_molsysmt_MolSys,
'molsysmt.Topology': to_molsysmt_Topology,
'mdtraj.Topology': to_mdtraj_Topology,
Expand Down
26 changes: 0 additions & 26 deletions molsysmt/form/openmm_Topology/to_file_msmh5.py

This file was deleted.

13 changes: 13 additions & 0 deletions molsysmt/form/openmm_Topology/write_topology_in_msmh5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from molsysmt._private.digestion import digest

@digest(form='openmm.Topology')
def write_topology_in_msmh5(item, file, atom_indices='all'):

from . import to_molsysmt_Topology as openmm_Topology_to_molsysmt_Topology
from ..molsysmt_Topology import write_topology_in_msmh5 as write_molsysmt_Topology_in_msmh5

molsysmt_Topology = openmm_Topology_to_molsysmt_Topology(item, atom_indices=atom_indices)
write_molsysmt_Topology_in_msmh5(molsysmt_Topology, file)

pass

40 changes: 20 additions & 20 deletions molsysmt/native/msmh5_file_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ def __init__(self, filename, io_mode='r', creator='MolSysMT', compression="gzip"
if closed:
self.file.close()

def write_topology(topology, selection='all', syntax='MolSysMT'):
def write_topology(self, topology, selection='all', syntax='MolSysMT'):

from molsysmt.basic import get_form, select, convert
from molsysmt._private.variables import is_all
from molsysmt.form import _dict_modules

if is_all(selection):
atom_indices = 'all'
Expand All @@ -43,11 +44,11 @@ def write_topology(topology, selection='all', syntax='MolSysMT'):

try:
topology_form = get_form(topology)
_dict_modules[topology_form].write_topology_in_msmh5(topology, self.file,
_dict_modules[topology_form].write_topology_in_msmh5(topology, file=self.file,
atom_indices=atom_indices)
except:
aux_topology = convert(topology, to_form='molsysmt.Topology', selection=atom_indices)
_dict_modules['molsysmt.Topology'].write_topology_in_msmh5(aux_topology, self.file,
_dict_modules['molsysmt.Topology'].write_topology_in_msmh5(aux_topology, file=self.file,
atom_indices=atom_indices)
del(aux_topology)

Expand All @@ -73,38 +74,36 @@ def _new_msmfile(filename, creator='MolSysMT', compression="gzip", compression_o
file.attrs['int_precision'] = int_precision
file.attrs['float_precision'] = int_precision

structures_sd=file['structures']

if length_unit is None:
structures_sd.attrs['length_unit']=puw.get_standard_units(dimensionality={'[L]':1}, form='string')
file.attrs['length_unit']=puw.get_standard_units(dimensionality={'[L]':1}, form='string')
else:
structures_sd.attrs['length_unit']=puw.get_unit(length_unit, to_form='string')
file.attrs['length_unit']=puw.get_unit(length_unit, to_form='string')

if time_unit is None:
structures_sd.attrs['time_unit']=puw.get_standard_units(dimensionality={'[T]':1}, form='string')
file.attrs['time_unit']=puw.get_standard_units(dimensionality={'[T]':1}, form='string')
else:
structures_sd.attrs['time_unit']=puw.get_unit(time_unit, to_form='string')
file.attrs['time_unit']=puw.get_unit(time_unit, to_form='string')

if energy_unit is None:
structures_sd.attrs['energy_unit']=puw.get_standard_units(dimensionality={'[L]':2, '[M]':1,
file.attrs['energy_unit']=puw.get_standard_units(dimensionality={'[L]':2, '[M]':1,
'[T]':-2, '[mol]':-1}, form='string')
else:
structures_sd.attrs['energy_unit']=puw.get_unit(energy_unit, to_form='string')
file.attrs['energy_unit']=puw.get_unit(energy_unit, to_form='string')

if temperature_unit is None:
structures_sd.attrs['temperature_unit']=puw.get_standard_units(dimensionality={'[K]':1}, form='string')
file.attrs['temperature_unit']=puw.get_standard_units(dimensionality={'[K]':1}, form='string')
else:
structures_sd.attrs['temperature_unit']=puw.get_unit(temperature_unit, to_form='string')
file.attrs['temperature_unit']=puw.get_unit(temperature_unit, to_form='string')

if temperature_unit is None:
structures_sd.attrs['charge_unit']=puw.get_standard_units(dimensionality={'[A]':1, '[T]':1}, form='string')
file.attrs['charge_unit']=puw.get_standard_units(dimensionality={'[A]':1, '[T]':1}, form='string')
else:
structures_sd.attrs['charge_unit']=puw.get_unit(temperature_unit, to_form='string')
file.attrs['charge_unit']=puw.get_unit(temperature_unit, to_form='string')

if temperature_unit is None:
structures_sd.attrs['mass_unit']=puw.get_standard_units(dimensionality={'[M]':1}, form='string')
file.attrs['mass_unit']=puw.get_standard_units(dimensionality={'[M]':1}, form='string')
else:
structures_sd.attrs['mass_unit']=puw.get_unit(mass_unit, to_form='string')
file.attrs['mass_unit']=puw.get_unit(mass_unit, to_form='string')

global_dataset_options = {
'compression':compression,
Expand Down Expand Up @@ -193,15 +192,16 @@ def _new_msmfile(filename, creator='MolSysMT', compression="gzip", compression_o

# Structures

structures_sd = file.create_group("structures_sd")
structures_sd = file.create_group("structures")
structures_sd.attrs['n_atoms'] = 0
structures_sd.attrs['n_structures_sd'] = 0
structures_sd.attrs['n_structures_sd_to_be_written'] = 0
structures_sd.attrs['n_structures'] = 0
structures_sd.attrs['n_structures_written'] = 0
structures_sd.attrs['constant_time_step']=False
structures_sd.attrs['time_step']=0.0
structures_sd.attrs['constant_id_step']=False
structures_sd.attrs['id_step']=0
structures_sd.attrs['constant_box']=False
structures_sd.attrs['pbc']='pbc' # none, cell, mic, continuous

structures_sd.create_dataset('id', (0,), dtype=int_type, maxshape=(None,), **global_dataset_options)
structures_sd.create_dataset('time', (0,), dtype=float_type, maxshape=(None,), **global_dataset_options)
Expand Down
Loading

0 comments on commit 410eec0

Please sign in to comment.