Skip to content

Commit

Permalink
In process
Browse files Browse the repository at this point in the history
  • Loading branch information
dprada committed Apr 30, 2024
1 parent c38fb18 commit bccc30a
Show file tree
Hide file tree
Showing 15 changed files with 359 additions and 87 deletions.
41 changes: 41 additions & 0 deletions molsysmt/_private/digestion/argument/bond_indices.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from ...exceptions import ArgumentError
from ...variables import is_all
import numpy as np

def digest_bond_indices(bond_indices, caller=None):
""" Checks if bond_indices has the expected type and value.
Parameters
----------
indices : str or int or list or tuple or range.
The indices
caller: str, optional
Name of the function or method that is being digested.
For debugging purposes.
Returns
-------
str or ndarray or None
Either None, 'all' or an numpy array of integers with the indices.
Raises
-------
ArgumentError
If the given indices are not of the correct type.
"""

if bond_indices is None:
return None
elif is_all(bond_indices):
return 'all'
elif isinstance(bond_indices, (int, np.int64, np.int32)):
return np.array([bond_indices], dtype='int64')
elif isinstance(bond_indices, (np.ndarray, list, tuple, range)):
if all(isinstance(ii, (int, np.int64, np.int32)) for ii in bond_indices):
return np.array(bond_indices, dtype='int64')
else:
return [digest_bond_indices(ii) for ii in bond_indices]

raise ArgumentError('bond_indices', caller=caller, message=None)

1 change: 1 addition & 0 deletions molsysmt/build/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@
from .define_new_chain import define_new_chain
from .make_water_box import make_water_box
from .remove_overlapping_molecules import remove_overlapping_molecules
from .remove_bonds import remove_bonds

22 changes: 22 additions & 0 deletions molsysmt/build/remove_bonds.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from molsysmt._private.exceptions import NotImplementedMethodError
from molsysmt._private.digestion import digest
from molsysmt._private.variables import is_all

@digest()
def remove_bonds(molecular_system, bond_indices='all', in_place=True, skip_digestion=False):

from molsysmt.basic import where_is_attribute
from molsysmt.form import _dict_modules

if in_place:

item, form = where_is_attribute(molecular_system, 'bond_index', check_if_None=False,
skip_digestion=True)

add_bonds_function = getattr(_dict_modules[form], f'remove_bonds')
add_bonds_function(item, bond_indices, skip_digestion=True)

else:

raise NotImplementedMethodError

15 changes: 9 additions & 6 deletions molsysmt/element/entity/get_entity_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

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

if redefine_molecules or redefine_indices:

Expand All @@ -12,18 +13,20 @@ def get_entity_index(molecular_system, element='atom', selection='all',
if redefine_molecules:

molecule_name_from_molecules = get_molecule_name(molecular_system, element='molecule',
selection=selection, redefine_molecules=True, syntax=syntax)
selection=selection, redefine_molecules=True, syntax=syntax, skip_digestion=True)

molecule_type_from_molecules = get_molecule_type(molecular_system, element='molecule',
selection=selection, redefine_molecules=True, syntax=syntax)
selection=selection, redefine_molecules=True, syntax=syntax, skip_digestion=True)

else:

molecule_name_from_molecules = get_molecule_name(molecular_system, element='molecule',
selection=selection, redefine_molecules=False, redefine_names=False, syntax=syntax)
selection=selection, redefine_molecules=False, redefine_names=False, syntax=syntax,
skip_digestion=True)

molecule_type_from_molecules = get_molecule_type(molecular_system, element='molecule',
selection=selection, redefine_molecules=False, redefine_types=False, syntax=syntax)
selection=selection, redefine_molecules=False, redefine_types=False, syntax=syntax,
skip_digestion=True)

count = 0
output = []
Expand Down Expand Up @@ -87,7 +90,7 @@ def get_entity_index(molecular_system, element='atom', selection='all',

from molsysmt import get
output = get(molecular_system, element=element, selection=selection, syntax=syntax,
entity_index=True)
entity_index=True, skip_digestion=True)

return output

4 changes: 3 additions & 1 deletion molsysmt/form/molsysmt_MolSys/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@
from .copy import copy
from .add import add
from .merge import merge
from .add_bonds import add_bonds
from .append_structures import append_structures
from .get_topological_attributes import *
from .get_structural_attributes import *
from .get_mechanical_attributes import *
from .set import *
from .iterators import StructuresIterator, TopologyIterator

from .add_bonds import add_bonds
from .remove_bonds import remove_bonds

from .to_mdtraj_Topology import to_mdtraj_Topology
from .to_mdtraj_Trajectory import to_mdtraj_Trajectory
from .to_molsysmt_Topology import to_molsysmt_Topology
Expand Down
8 changes: 8 additions & 0 deletions molsysmt/form/molsysmt_MolSys/remove_bonds.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from molsysmt._private.exceptions import NotImplementedMethodError
from molsysmt._private.digestion import digest
from molsysmt._private.variables import is_all

@digest(form='molsysmt.MolSys')
def remove_bonds(item, bond_indices='all', skip_digestion=False):

return item.topology.remove_bonds(bond_indices=bond_indices, skip_digestion=True)
80 changes: 52 additions & 28 deletions molsysmt/form/molsysmt_PDBFileHandler/to_molsysmt_Topology.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
from molsysmt._private.digestion import digest
from molsysmt._private.variables import is_all
from molsysmt import pyunitwizard as puw
import pandas as pd
import numpy as np

@digest(form='molsysmt.PDBFileHandler')
def to_molsysmt_Topology(item, atom_indices='all', structure_indices=0, get_missing_bonds=True,
skip_digestion=False):

from molsysmt.native import Topology
from molsysmt.build import get_missing_bonds as _get_missing_bonds
from molsysmt.native import MolSys

tmp_item = Topology()
tmp_item = MolSys()

atom_id_array = []
atom_name_array = []
Expand All @@ -22,6 +22,7 @@ def to_molsysmt_Topology(item, atom_indices='all', structure_indices=0, get_miss

occupancy_array = []
alternate_location_array = []
coordinates_array = []

group_index = -1
former_group_id = None
Expand All @@ -30,8 +31,6 @@ def to_molsysmt_Topology(item, atom_indices='all', structure_indices=0, get_miss
former_chain_name = None
aux_dict_chain = {}



for atom_record in item.entry.coordinate.model[0].record:

atom_id_array.append(atom_record.serial)
Expand All @@ -58,6 +57,8 @@ def to_molsysmt_Topology(item, atom_indices='all', structure_indices=0, get_miss

chain_index_array.append(chain_index)

coordinates_array.append([atom_record.x, atom_record.y, atom_record.z])

atom_id_array = np.array(atom_id_array, dtype=int)
atom_name_array = np.array(atom_name_array, dtype=str)
group_index_array = np.array(group_index_array, dtype=int)
Expand All @@ -69,6 +70,8 @@ def to_molsysmt_Topology(item, atom_indices='all', structure_indices=0, get_miss
occupancy_array = np.array(occupancy_array, dtype=float)
alternate_location_array = np.array(alternate_location_array, dtype=str)

coordinates_array = np.array(coordinates_array, dtype=float)

alt_atom_indices = np.where(alternate_location_array!=' ')[0]
aux_dict = {}

Expand Down Expand Up @@ -96,51 +99,72 @@ def to_molsysmt_Topology(item, atom_indices='all', structure_indices=0, get_miss
chosen = np.argmax(alt_occupancy)
chosen = same_atoms.pop(chosen)
atoms_to_be_removed_with_alt_loc += same_atoms

atom_id_array = np.delete(atom_id_array, atoms_to_be_removed_with_alt_loc)
atom_name_array = np.delete(atom_name_array, atoms_to_be_removed_with_alt_loc)
group_index_array = np.delete(group_index_array, atoms_to_be_removed_with_alt_loc)
chain_index_array = np.delete(chain_index_array, atoms_to_be_removed_with_alt_loc)

if len(atoms_to_be_removed_with_alt_loc):
coordinates_array = np.delete(coordinates_array, atoms_to_be_removed_with_alt_loc)

n_atoms = atom_id_array.shape[0]
n_groups = group_name_array.shape[0]
n_chains = chain_name_array.shape[0]

tmp_item.reset_atoms(n_atoms=n_atoms)
tmp_item.reset_groups(n_groups=n_groups)
tmp_item.reset_chains(n_chains=n_chains)
tmp_item.topology.reset_atoms(n_atoms=n_atoms)
tmp_item.topology.reset_groups(n_groups=n_groups)
tmp_item.topology.reset_chains(n_chains=n_chains)

tmp_item.atoms.atom_id = atom_id_array
tmp_item.atoms.atom_name = atom_name_array
tmp_item.atoms.group_index = group_index_array
tmp_item.atoms.chain_index = chain_index_array
tmp_item.topology.atoms.atom_id = atom_id_array
tmp_item.topology.atoms.atom_name = atom_name_array
tmp_item.topology.atoms.group_index = group_index_array
tmp_item.topology.atoms.chain_index = chain_index_array

tmp_item.rebuild_atoms(redefine_ids=False, redefine_types=True)
tmp_item.topology.rebuild_atoms(redefine_ids=False, redefine_types=True)

tmp_item.groups.group_id = group_id_array
tmp_item.groups.group_name = group_name_array
tmp_item.topology.groups.group_id = group_id_array
tmp_item.topology.groups.group_name = group_name_array

tmp_item.rebuild_groups(redefine_ids=False, redefine_types=True)
tmp_item.topology.rebuild_groups(redefine_ids=False, redefine_types=True)

tmp_item.chains.chain_name = chain_name_array
tmp_item.topology.chains.chain_name = chain_name_array

tmp_item.rebuild_chains(redefine_ids=True, redefine_types=False )
tmp_item.topology.rebuild_chains(redefine_ids=True, redefine_types=False )

del(atom_id_array, atom_name_array,
group_index_array, group_id_array, group_name_array,
chain_index_array, chain_name_array,
occupancy_array, alternate_location_array, alt_atom_indices, aux_dict)

if get_missing_bonds:

from molsysmt.build import get_missing_bonds as _get_missing_bonds
from molsysmt.pbc import get_box_from_lengths_and_angles

cryst1 = item.entry.crystallographic_and_coordinate_transformation.cryst1
box_lengths = puw.quantity([[cryst1.a, cryst1.b, cryst1.c]], 'angstroms')
box_angles = puw.quantity([[cryst1.alpha, cryst1.beta, cryst1.gamma]], 'degrees')
box = get_box_from_lengths_and_angles(box_lengths, box_angles, skip_digestion=True)

coordinates = puw.quantity(coordinates_array, 'angstroms')
tmp_item.structures.append(coordinates=coordinates, box=box)

del(coordinates_array, box, box_lengths, box_angles)

bonds = _get_missing_bonds(tmp_item, skip_digestion=True)
bonds = np.array(bonds)
tmp_item.reset_bonds(n_bonds=bonds.shape[0])
tmp_item.bonds.drop(['order', 'type'], axis=1, inplace=True)
tmp_item.bonds.atom1_index=bonds[:,0]
tmp_item.bonds.atom2_index=bonds[:,1]
tmp_item.topology,reset_bonds(n_bonds=bonds.shape[0])
tmp_item.topology.bonds.drop(['order', 'type'], axis=1, inplace=True)
tmp_item.topology.bonds.atom1_index=bonds[:,0]
tmp_item.topology.bonds.atom2_index=bonds[:,1]

tmp_item.rebuild_components()
tmp_item.rebuild_molecules()
tmp_item.rebuild_chains(redefine_ids=False, redefine_types=True)
tmp_item.rebuild_entities()
tmp_item.topology.rebuild_components()
tmp_item.topology.rebuild_molecules()
tmp_item.topology.rebuild_chains(redefine_ids=False, redefine_types=True)
tmp_item.topology.rebuild_entities()

tmp_item = tmp_item.extract(atom_indices=atom_indices, copy_if_all=False, skip_digestion=True)
tmp_item = tmp_item.topology.extract(atom_indices=atom_indices, copy_if_all=False, skip_digestion=True)

return tmp_item

3 changes: 3 additions & 0 deletions molsysmt/form/molsysmt_Topology/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
from .set import *
from .iterators import TopologyIterator

from .add_bonds import add_bonds
from .remove_bonds import remove_bonds

from .to_string_amino_acids_3 import to_string_amino_acids_3
from .to_string_amino_acids_1 import to_string_amino_acids_1
from .to_string_pdb_text import to_string_pdb_text
Expand Down
10 changes: 10 additions & 0 deletions molsysmt/form/molsysmt_Topology/add_bonds.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from molsysmt._private.exceptions import NotImplementedMethodError
from molsysmt._private.digestion import digest
from molsysmt import pyunitwizard as puw
import numpy as np

@digest(form='molsysmt.Topology')
def add_bonds(item, bonded_atom_pairs, skip_digestion=False):

item.add_bonds(bonded_atom_pairs, skip_digestion=True)

8 changes: 8 additions & 0 deletions molsysmt/form/molsysmt_Topology/remove_bonds.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from molsysmt._private.exceptions import NotImplementedMethodError
from molsysmt._private.digestion import digest
from molsysmt._private.variables import is_all

@digest(form='molsysmt.Topology')
def remove_bonds(item, bond_indices='all', skip_digestion=False):

return item.remove_bonds(bond_indices=bond_indices, skip_digestion=True)
6 changes: 3 additions & 3 deletions molsysmt/form/openmm_Modeller/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
form_type = 'class'
form_info = ["", ""]

piped_topological_attribute = None
piped_structural_attribute = None
piped_any_attribute = None
piped_topological_attribute = 'molsysmt.Topology'
piped_structural_attribute = 'molsysmt.Structures'
piped_any_attribute = 'molsysmt.MolSys'

from .is_form import is_form

Expand Down
42 changes: 42 additions & 0 deletions molsysmt/native/topology.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,46 @@ def add_bonds(self, bonded_atom_pairs, skip_digestion=False):
self.bonds._sort_bonds()
self.bonds._remove_empty_columns()

self.rebuild_components()
self.rebuild_molecules()
self.rebuild_entities()

del(df_concatenado, aux_bonds_dataframe)

def remove_bonds(self, bond_indices='all', skip_digestion=False):

if is_all(bond_indices):

self.bonds = Bonds_DataFrame(n_bonds=0)

else:

tmp_dataframe = self.bonds.drop(bond_indices)

n_bonds = bonded_atom_pairs.shape[0]

self.bonds = Bonds_DataFrame(n_bonds=n_bonds)
self.bonds.atom1_index=tmp_dataframe.atom1_index
self.bonds.atom2_index=tmp_dataframe.atom2_index

if 'order' in tmp_dataframe:
self.bonds['order'] = tmp_dataframe['order']
else:
del self.bonds['order']

if 'type' in tmp_dataframe:
self.bonds['type'] = tmp_dataframe['type']
else:
del self.bonds['type']

self.bonds._sort_bonds()

del(tmp_dataframe)

self.rebuild_components()
self.rebuild_molecules()
self.rebuild_entities()


def add_missing_bonds(self, selection='all', syntax='MolSysMT', skip_digestion=False):

Expand All @@ -347,6 +385,10 @@ def add_missing_bonds(self, selection='all', syntax='MolSysMT', skip_digestion=F

self.add_bonds(bonds, skip_digestion=True)

self.rebuild_components()
self.rebuild_molecules()
self.rebuild_entities()

def rebuild_atoms(self, redefine_ids=True, redefine_types=True):

if redefine_ids:
Expand Down
Loading

0 comments on commit bccc30a

Please sign in to comment.