From 51bb2077e3e40604383c7c9179da6100d697ee4b Mon Sep 17 00:00:00 2001 From: Diego Prada Date: Thu, 5 Oct 2023 17:08:55 -0600 Subject: [PATCH] in process --- molsysmt/basic/remove.py | 7 ++- .../atom/get_atom_type_from_atom_name.py | 5 ++- molsysmt/element/atom/names.py | 41 +++++++++++------ molsysmt/form/parmed_Structure/__init__.py | 1 + molsysmt/form/parmed_Structure/attributes.py | 41 ++++++++++++++++- molsysmt/form/parmed_Structure/get.py | 45 ++++++++++++++++--- molsysmt/form/parmed_Structure/to_file_cif.py | 14 ++++++ .../parmed_Structure/to_molsysmt_Topology.py | 19 ++++++-- 8 files changed, 148 insertions(+), 25 deletions(-) create mode 100644 molsysmt/form/parmed_Structure/to_file_cif.py diff --git a/molsysmt/basic/remove.py b/molsysmt/basic/remove.py index 1b67d36c3..558f13a95 100644 --- a/molsysmt/basic/remove.py +++ b/molsysmt/basic/remove.py @@ -1,7 +1,7 @@ from molsysmt._private.digestion import digest from molsysmt._private.structure_indices import complementary_structure_indices from molsysmt._private.atom_indices import complementary_atom_indices - +from molsysmt._private.variables import is_all @digest() def remove(molecular_system, selection=None, structure_indices=None, to_form=None, syntax='MolSysMT'): @@ -61,7 +61,7 @@ def remove(molecular_system, selection=None, structure_indices=None, to_form=Non """ - from . import select, extract + from . import select, extract, get atom_indices_to_be_kept = 'all' structure_indices_to_be_kept = 'all' @@ -71,6 +71,9 @@ def remove(molecular_system, selection=None, structure_indices=None, to_form=Non atom_indices_to_be_kept = complementary_atom_indices(molecular_system, atom_indices_to_be_removed) if structure_indices is not None: + if is_all(structure_indices): + n_structures = get(molecular_system, element='system', n_structures=True) + structure_indices= list(range(n_structures)) structure_indices_to_be_kept = complementary_structure_indices(molecular_system, structure_indices) tmp_item = extract(molecular_system, selection=atom_indices_to_be_kept, diff --git a/molsysmt/element/atom/get_atom_type_from_atom_name.py b/molsysmt/element/atom/get_atom_type_from_atom_name.py index a17ab50b1..6930a816c 100644 --- a/molsysmt/element/atom/get_atom_type_from_atom_name.py +++ b/molsysmt/element/atom/get_atom_type_from_atom_name.py @@ -7,5 +7,8 @@ def get_atom_type_from_atom_name(atom_name): To be written soon... """ - return atom_type[atom[atom_name]] + try: + return atom_type[atom[atom_name]] + except: + raise ValueError('Atom name '+atom_name+' has not known atom type.') diff --git a/molsysmt/element/atom/names.py b/molsysmt/element/atom/names.py index 52b1b1dfe..729d73c19 100644 --- a/molsysmt/element/atom/names.py +++ b/molsysmt/element/atom/names.py @@ -1,6 +1,7 @@ atom={ 'H' : 'atH' , 'HN' : 'atH' , # charmm.pdb +'HN1' : 'atH' , # 'H1' : 'atH1' , 'H2' : 'atH2' , '2HH' : 'atH2' , # pymol.pdb @@ -78,22 +79,28 @@ 'H8S' : 'atH8S' , # lipid POPC 'H8X' : 'atH8X' , # lipid POPC 'H8Y' : 'atH8Y' , # lipid POPC +'H9R' : 'atH9R' , # lipid POPC +'H9S' : 'atH9S' , # lipid POPC 'H9X' : 'atH9X' , # lipid POPC 'H9Y' : 'atH9Y' , # lipid POPC +'H10R' : 'atH10R' , # lipid POPC +'H10S' : 'atH10S' , # lipid POPC 'H10X' : 'atH10X' , # lipid POPC 'H10Y' : 'atH10Y' , # lipid POPC 'H11A' : 'atH11A' , # lipid POPC 'H11B' : 'atH11B' , # lipid POPC 'H11R' : 'atH11R' , # lipid POPC +'H11S' : 'atH11S' , # lipid POPC 'H11X' : 'atH11X' , # lipid POPC 'H11Y' : 'atH11Y' , # lipid POPC -'H11S' : 'atH11S' , # lipid POPC 'H12A' : 'atH12A' , # lipid POPC 'H12B' : 'atH12B' , # lipid POPC 'H12R' : 'atH12R' , # lipid POPC 'H12S' : 'atH12S' , # lipid POPC +'H12T' : 'atH12T' , # lipid POPC 'H12X' : 'atH12X' , # lipid POPC 'H12Y' : 'atH12Y' , # lipid POPC +'H12Z' : 'atH12Z' , # lipid POPC 'H13A' : 'atH13A' , # lipid POPC 'H13B' : 'atH13B' , # lipid POPC 'H13C' : 'atH13C' , # lipid POPC @@ -228,6 +235,7 @@ 'HH32' : 'atHH32' , # opls.gro '3HH3' : 'atHH33' , 'HH33' : 'atHH33' , # opls.gro +'HR' : 'atHR' , # lipid 'HS' : 'atHS' , # lipid POPC 'HNT' : 'atHT' , # ACEMD (NME Terminal) 'HX' : 'atHX' , # lipid POPC @@ -500,35 +508,41 @@ 'atH8S' : 'H' , 'atH8X' : 'H' , 'atH8Y' : 'H' , +'atH9R' : 'H' , +'atH9S' : 'H' , 'atH9X' : 'H' , 'atH9Y' : 'H' , +'atH10R' : 'H' , +'atH10S' : 'H' , 'atH10X' : 'H' , 'atH10Y' : 'H' , +'atH11R' : 'H' , +'atH11S' : 'H' , 'atH11X' : 'H' , 'atH11Y' : 'H' , +'atH12S' : 'H' , +'atH12R' : 'H' , +'atH12T' : 'H' , 'atH12X' : 'H' , 'atH12Y' : 'H' , +'atH12Z' : 'H' , +'atH13R' : 'H' , +'atH13S' : 'H' , 'atH13X' : 'H' , 'atH13Y' : 'H' , +'atH14R' : 'H' , +'atH14S' : 'H' , 'atH14X' : 'H' , 'atH14Y' : 'H' , +'atH15R' : 'H' , +'atH15S' : 'H' , 'atH15X' : 'H' , 'atH15Y' : 'H' , +'atH16R' : 'H' , +'atH16S' : 'H' , 'atH16X' : 'H' , 'atH16Y' : 'H' , 'atH16Z' : 'H' , -'atH11R' : 'H' , -'atH11S' : 'H' , -'atH12R' : 'H' , -'atH12S' : 'H' , -'atH13R' : 'H' , -'atH13S' : 'H' , -'atH14R' : 'H' , -'atH14S' : 'H' , -'atH15R' : 'H' , -'atH15S' : 'H' , -'atH16R' : 'H' , -'atH16S' : 'H' , 'atH17R' : 'H' , 'atH17S' : 'H' , 'atH18R' : 'H' , @@ -574,6 +588,7 @@ 'atHH31': 'H' , 'atHH32': 'H' , 'atHH33': 'H' , +'atHR' : 'H' , 'atHS' : 'H' , 'atHT' : 'H' , 'atHT1' : 'H' , diff --git a/molsysmt/form/parmed_Structure/__init__.py b/molsysmt/form/parmed_Structure/__init__.py index df92e36e6..a41b9890c 100644 --- a/molsysmt/form/parmed_Structure/__init__.py +++ b/molsysmt/form/parmed_Structure/__init__.py @@ -19,6 +19,7 @@ from .to_file_mol2 import to_file_mol2 from .to_file_pdb import to_file_pdb from .to_file_psf import to_file_psf +from .to_file_cif import to_file_cif from .to_mdtraj_Topology import to_mdtraj_Topology from .to_mdtraj_Trajectory import to_mdtraj_Trajectory from .to_nglview_NGLWidget import to_nglview_NGLWidget diff --git a/molsysmt/form/parmed_Structure/attributes.py b/molsysmt/form/parmed_Structure/attributes.py index 00855ba73..4cc80358b 100644 --- a/molsysmt/form/parmed_Structure/attributes.py +++ b/molsysmt/form/parmed_Structure/attributes.py @@ -2,6 +2,7 @@ attributes = {ii:False for ii in _all_attributes} +attributes['atom_index'] = True attributes['atom_index'] = True attributes['atom_id'] = True attributes['atom_name'] = True @@ -23,8 +24,46 @@ attributes['chain_id'] = True attributes['chain_name'] = True attributes['chain_type'] = True -attributes['coordinates'] = True + +attributes['n_atoms'] = True +attributes['n_groups'] = True +attributes['n_components'] = True +attributes['n_molecules'] = True +attributes['n_chains'] = True +attributes['n_entities'] = True +attributes['n_aminoacids'] = True +attributes['n_nucleotides'] = True +attributes['n_ions'] = True +attributes['n_waters'] = True +attributes['n_small_molecules'] = True +attributes['n_peptides'] = True +attributes['n_proteins'] = True +attributes['n_dnas'] = True +attributes['n_rnas'] = True +attributes['n_lipids'] = True +attributes['n_oligosaccharides'] = True +attributes['n_saccharides'] = True + +attributes['bond_index'] = True +attributes['bond_id'] = False +attributes['bond_type'] = True +attributes['bond_order'] = True +attributes['bonded_atoms'] = True +attributes['inner_bond_index'] = True +attributes['inner_bonded_atoms'] = True +attributes['n_bonds'] = True +attributes['n_inner_bonds'] = True + + +attributes['structure_index'] = True attributes['box'] = True +attributes['box_shape'] = True +attributes['box_angles'] = True +attributes['box_lengths'] = True +attributes['box_volume'] = True +attributes['coordinates'] = True +attributes['n_structures'] = True +attributes['b_factor'] = True del(_all_attributes) diff --git a/molsysmt/form/parmed_Structure/get.py b/molsysmt/form/parmed_Structure/get.py index fbe9773c0..f24cfe78f 100644 --- a/molsysmt/form/parmed_Structure/get.py +++ b/molsysmt/form/parmed_Structure/get.py @@ -5,6 +5,7 @@ from molsysmt._private.execfile import execfile from molsysmt._private.exceptions import NotImplementedMethodError, NotWithThisFormError from molsysmt._private.digestion import digest +from molsysmt._private.variables import is_all form='parmed.Structure' @@ -63,7 +64,22 @@ def get_n_inner_bonds_from_atom(item, indices='all'): @digest(form=form) def get_coordinates_from_atom(item, indices='all', structure_indices='all'): - raise NotImplementedMethodError() + output = item.coordinates + + if output is None: + return output + + if not is_all(indices): + output = output[:,indices,:] + if not is_all(structure_indices): + output = output[structure_indices,:,:] + + if not puw.is_quantity(output): + output = puw.quantity(output,'angstroms') + + output = puw.standardize(output) + + return output ## From group @@ -191,22 +207,41 @@ def get_n_bonds_from_system(item): @digest(form=form) def get_n_structures_from_system(item, structure_indices='all'): - raise NotImplementedMethodError() + if is_all(structure_indices): + if item.coordinates is None: + return 0 + else: + return item.coordinates.shape[0] + else: + return len(structure_indices) @digest(form=form) def get_box_from_system(item, structure_indices='all'): - raise NotImplementedMethodError() + output = item.box + + if output is None: + return output + + if not is_all(structure_indices): + output = output[structure_indices,:,:] + + if not puw.is_quantity(output): + output = puw.quantity(output,'angstroms') + + output = puw.standardize(output) + + return output @digest(form=form) def get_time_from_system(item, structure_indices='all'): - raise NotImplementedMethodError() + return None @digest(form=form) def get_structure_id_from_system(item, structure_indices='all'): - raise NotImplementedMethodError() + return None ## From bond diff --git a/molsysmt/form/parmed_Structure/to_file_cif.py b/molsysmt/form/parmed_Structure/to_file_cif.py new file mode 100644 index 000000000..1b418510c --- /dev/null +++ b/molsysmt/form/parmed_Structure/to_file_cif.py @@ -0,0 +1,14 @@ +from molsysmt._private.digestion import digest + +@digest(form='parmed.Structure') +def to_file_cif(item, atom_indices='all', structure_indices='all', output_filename=None): + + from . import extract + + tmp_item = extract(item, atom_indices=atom_indices, structure_indices=structure_indices, + copy_if_all=False) + tmp_item.save(output_filename) + tmp_item = output_filename + + return tmp_item + diff --git a/molsysmt/form/parmed_Structure/to_molsysmt_Topology.py b/molsysmt/form/parmed_Structure/to_molsysmt_Topology.py index 3398484ed..754ba598e 100644 --- a/molsysmt/form/parmed_Structure/to_molsysmt_Topology.py +++ b/molsysmt/form/parmed_Structure/to_molsysmt_Topology.py @@ -6,6 +6,7 @@ def to_molsysmt_Topology(item, atom_indices='all'): from molsysmt.native import Topology from numpy import empty, array, arange, reshape, where, unique, nan, sort, zeros from molsysmt.element.group import get_group_type_from_group_name + from molsysmt.element.atom import get_atom_type_from_atom_name from ..molsysmt_Topology import extract tmp_item = Topology() @@ -33,25 +34,37 @@ def to_molsysmt_Topology(item, atom_indices='all'): atom_index = 0 + aux_dict_chains={} + for atom in item.atoms: - atom_index_array[atom_index] = atom._idx + atom_index_array[atom_index] = atom.idx atom_name_array[atom_index] = atom.name atom_id_array[atom_index] = atom.idx - atom_type_array[atom_index] = None + atom_type_array[atom_index] = get_atom_type_from_atom_name(atom.name) atom_partial_charge_array[atom_index] = atom.charge atom_formal_charge_array[atom_index] = atom.formal_charge - group_index_array[atom_index] = atom.residue._idx + group_index_array[atom_index] = atom.residue.idx group_name_array[atom_index] = atom.residue.name group_id_array[atom_index] = atom.residue.idx group_type_array[atom_index] = get_group_type_from_group_name(atom.residue.name) + chain_id = atom.residue.chain + if chain_id not in aux_dict_chains: + aux_dict_chains[chain_id]=len(aux_dict_chains) + chain_index = aux_dict_chains[chain_id] + chain_index_array[atom_index] = chain_index + chain_id_array[atom_index] = chain_id + chain_index_array[atom_index] = 0 chain_id_array[atom_index] = None atom_index+=1 + if len(aux_dict_chains)==1: + if '' in aux_dict_chains: + chain_id_array[:]='A' tmp_item.atoms_dataframe["atom_index"] = atom_index_array tmp_item.atoms_dataframe["atom_name"] = atom_name_array