def add(self, item, selection='all', structure_indices='all'): from molsysmt.basic import get step, time, box = get(item, target="system", structure_indices=structure_indices, step=True, time=True, box=True) coordinates = get(item, target="atom", selection=selection, structure_indices=structure_indices, coordinates=True) if self.n_structures == 0: self.append_structures(step, time, coordinates, box) else: if self.n_structures != coordinates.shape[0]: raise ValueError( 'Both items need to have the same n_structures') else: unit = puw.get_unit(self.coordinates) value_coordinates = puw.get_value(coordinates, to_unit=unit) value_self_coordinates = puw.get_value(self.coordinates) self.coordinates = np.hstack( [value_self_coordinates, value_coordinates]) * unit del (value_coordinates, value_self_coordinates) self.n_atoms = self.coordinates.shape[1] pass
def indices_to_selection(molecular_system, indices, target='atom', syntaxis=None): syntaxis = digest_syntaxis(syntaxis) target = digest_target(target) output_string = '' if syntaxis == 'NGLView': if target == 'atom': output_string = '@' + ','.join([str(ii) for ii in indices]) elif target == 'group': from molsysmt import get group_ids, chain_ids = get(molecular_system, target='group', indices=indices, group_id=True, chain_id=True) output_string = ' '.join([ str(ii) + ':' + str(jj) for ii, jj in zip(group_ids, chain_ids) ]) elif target == 'chain': from molsysmt import get chain_ids = get(molecular_system, target='chain', indices=indices, chain_id=True) output_string = ' '.join([':' + ii for ii in chain_ids]) else: raise NotImplementedError elif syntaxis == 'MDTraj': if target == 'atom': output_string = 'index ' + ' '.join([str(ii) for ii in indices]) elif target == 'group': output_string = 'resid ' + ' '.join([str(ii) for ii in indices]) elif target == 'chain': output_string = 'chainid ' + ' '.join([str(ii) for ii in indices]) else: raise NotImplementedError else: raise NotImplementedError return output_string
def unwrap(molecular_system, selection='all', structure_indices='all', syntaxis='MolSysMT', engine='MolSysMT', in_place=False): engine = digest_engine(engine) structure_indices = digest_structure_indices(structure_indices) if engine=='MolSysMT': from molsysmt.basic import select, get, set, extract coordinates= get(molecular_system, target='atom', selection=selection, coordinates=True) n_structures = coordinates.shape[0] n_atoms = coordinates.shape[1] box, box_shape = get(molecular_system, target='system', structure_indices=structure_indices, box=True, box_shape=True) orthogonal = 0 if box_shape is None: raise ValueError("The system has no PBC box. The input argument 'pbc' can not be True.") elif box_shape == 'cubic': orthogonal =1 length_units = puw.get_unit(coordinates) box = puw.convert(box, to_unit=length_units) box = np.asfortranarray(puw.get_value(box), dtype='float64') coordinates = np.asfortranarray(puw.get_value(coordinates), dtype='float64') libbox.unwrap(coordinates, box, orthogonal, n_atoms, n_structures) coordinates=np.ascontiguousarray(coordinates)*length_units else: raise NotImpementedEngineError() if in_place: set(molecular_system, target='atom', selection=selection, structure_indices=structure_indices, syntaxis=syntaxis, coordinates=coordinates) pass else: tmp_molecular_system = extract(molecular_system, selection=selection, structure_indices=structure_indices, syntaxis=syntaxis) set(tmp_molecular_system, target='atom', selection='all', structure_indices='all', syntaxis='MolSysMT', coordinates=coordinates) return tmp_molecular_system
def atomic_radius(molecular_system, selection='all', type='vdw'): from molsysmt.basic import get from molsysmt.physico_chemical_properties.atoms.radius import units from molsysmt._private._digestion import digest_target if type == 'vdw': from molsysmt.physico_chemical_properties.atoms.radius import vdw as values else: raise NotImplementedError() atom_types = get(molecular_system, target='atom', selection=selection, type=True) output = [] for ii in atom_types: var_aux = values[ii.capitalize()] output.append(var_aux) output = puw.quantity(np.array(output), units) return output
def select_bonded_to(molecular_system, selection, syntaxis): from molsysmt.basic import get not_bonded = False if "not bonded to" in selection: selection_1, selection_2 = selection.split(" not bonded to") not_bonded = True else: selection_1, selection_2 = selection.split(" bonded to") atom_indices_1 = select(molecular_system, selection=selection_1, syntaxis=syntaxis) atom_indices_2 = get(molecular_system, 'atom', selection=selection_2, bonded_atoms=True, syntaxis=syntaxis) atom_indices_2 = np.unique(np.concatenate(atom_indices_2).ravel()) if not_bonded: output = np.setdiff1d(atom_indices_1, atom_indices_2, assume_unique=True) else: output = np.intersect1d(atom_indices_1, atom_indices_2, assume_unique=True) return output
def frameslist(molecular_system=None, structure_indices=None): from molsysmt.basic import get if structure_indices is None: structure_indices = list( np.arange(get(molecular_system, n_structures=True), dtype=int)) elif type(structure_indices) == int: structure_indices = np.asarray([structure_indices]) elif type(structure_indices) == list: structure_indices = np.asarray(structure_indices) elif type(structure_indices) == np.ndarray: structure_indices = structure_indices elif frame == 'all': structure_indices = np.arange(get(molecular_system, n_structures=True)) return structure_indices
def atom_indices_to_AmberMask(molecular_system, atom_indices): from molsysmt.basic import get n_atoms = get(molecular_system, element='system', n_atoms=True) mask = np.zeros(n_atoms, dtype=int) mask[atom_indices] = 1 return list(mask)
def complementary_group_indices(molecular_system, group_indices): from molsysmt.basic import get n_groups = get(molecular_system, target='system', n_groups=True) mask = np.ones(n_groups,dtype=bool) mask[atom_indices]=False return list(np.where(mask)[0])
def get_covalent_blocks(molecular_system, selection='all', remove_bonds=None, output_form='sets', syntaxis='MolSysMT'): from molsysmt.topology.get_bondgraph import get_bondgraph G = get_bondgraph(molecular_system, nodes_name='atom_index', selection=selection, syntaxis=syntaxis) if remove_bonds is not None: if type(remove_bonds) in [list, tuple]: remove_bonds = np.array(remove_bonds) if len(remove_bonds.shape) == 1: if remove_bonds.shape[0] == 2: remove_bonds = remove_bonds.reshape([1, 2]) else: raise ValueError( "Input argument bonded_atoms with wrong shape") elif len(remove_bonds.shape) == 2: if remove_bonds.shape[1] != 2: raise ValueError( "Input argument bonded_atoms with wrong shape") else: raise ValueError("Input argument bonded_atoms with wrong shape") for atom_pair in remove_bonds: G.remove_edge(atom_pair[0], atom_pair[1]) components = connected_components(G) del (G) if output_form == 'sets': blocks = list(components) elif output_form == 'array': n_atoms = get(molecular_system, target='system', n_atoms=True) blocks = -np.ones([n_atoms], dtype=int) component_index = 0 for component in components: blocks[list(component)] = component_index component_index += 1 else: raise ValueError('Input argument "output" must be "sets" or "array"') return np.array(blocks)
def component_name_from_component(item, indices='all'): if indices is 'all': from molsysmt.basic import get n_components = get(item, target='system', n_components=True) output = np.full(n_components, None, dtype=object) else: output = np.full(indices.shape[0], None, dtype=object) return output
def append(self, item, selection='all', structure_indices='all'): from molsysmt.basic import get step, time, coordinate, box = get(item, target="atom", selection=selection, structure_indices=structure_indices, frame=True) self.append_structures(step, time, coordinate, box) pass
def has_waters(molecular_system): from molsysmt.basic import get output = False n_waters = get(molecular_system, target='system', n_waters=True) if n_waters>0: output = True return output
def get_center(molecular_system, selection='all', groups_of_atoms=None, weights=None, structure_indices='all', syntaxis='MolSysMT', engine='MolSysMT', parallel=False): molecular_system = digest_molecular_system(molecular_system) engine = digest_engine(engine) if engine == 'MolSysMT': if groups_of_atoms is None: atom_indices = select(molecular_system, selection=selection, syntaxis=syntaxis) groups_of_atoms = [atom_indices] groups_serialized = serialized_lists(groups_of_atoms, dtype='int64') if weights is None: weights = np.ones((groups_serialized.n_values)) elif weights is 'masses': raise NotImplementedError coordinates = get(molecular_system, target='system', structure_indices=structure_indices, coordinates=True) length_units = puw.get_unit(coordinates) coordinates = np.asfortranarray(puw.get_value(coordinates), dtype='float64') n_atoms = coordinates.shape[1] n_structures = coordinates.shape[0] com = libcom.center_of_mass(coordinates, groups_serialized.indices, groups_serialized.values, groups_serialized.starts, weights, n_structures, n_atoms, groups_serialized.n_indices, groups_serialized.n_values) del (coordinates, groups_serialized) return com * length_units else: raise NotImplementedError(NotImplementedMessage)
def mass(molecular_system, target='atom', selection='all'): from molsysmt.basic import get from molsysmt.physico_chemical_properties.atoms.mass import physical, units from molsysmt._private._digestion import digest_target target = digest_target(target) values = physical output = [] if target == 'atom': atom_types = get(molecular_system, target=target, selection=selection, atom_type=True) for ii in atom_types: output.append(values[ii.capitalize()]) elif target in ['group', 'component', 'molecule', 'chain', 'entity']: atom_types_in_target = get(molecular_system, target=target, selection=selection, atom_type=True) for aux in atom_types_in_target: output.append(np.sum([values[ii.capitalize()] for ii in aux])) elif target == 'system': atom_types_in_target = get(molecular_system, target='atom', selection='all', atom_type=True) output.append( np.sum([values[ii.capitalize()] for ii in atom_types_in_target])) if target == 'system': output = output[0] * puw.unit(units) else: output = puw.quantity(np.array(output), units) return output
def hydrophobicity(molecular_system, selection='all', type='eisenberg'): from molsysmt.basic import get if type == 'eisenberg': from .groups.hydrophobicity import eisenberg as values elif type == 'argos': from .groups.hydrophobicity import argos as values elif type == 'sweet': from .groups.hydrophobicity import sweet as values elif type == 'kyte': from .groups.hydrophobicity import kyte as values elif type == 'abraham': from .groups.hydrophobicity import abraham as values elif type == 'bull': from .groups.hydrophobicity import bull as values elif type == 'guy': from .groups.hydrophobicity import guy as values elif type == 'miyazawa': from .groups.hydrophobicity import miyazawa as values elif type == 'roseman': from .groups.hydrophobicity import roseman as values elif type == 'wolfenden': from .groups.hydrophobicity import wolfenden as values elif type == 'chothia': from .groups.hydrophobicity import chothia as values elif type == 'hopp': from .groups.hydrophobicity import hopp as values elif type == 'manavalan': from .groups.hydrophobicity import manavalan as values elif type == 'black': from .groups.hydrophobicity import black as values elif type == 'fauchere': from .groups.hydrophobicity import fauchere as values else: raise NotImplementedError() group_types = get(molecular_system, target='group', selection=selection, name=True) output = [] for ii in group_types: output.append(values[ii.upper()]) output = np.array(output) return output
def n_components_from_system(item, indices='all'): from molsysmt import get component_index_from_atom = get(item, target='atom', indices='all', component_index=True) if component_index_from_atom[0] is None: n_components = 0 else: output = np.unique(component_index_from_atom) n_components = output.shape[0] return n_components
def component_index_from_atom(item, indices='all'): from molsysmt.basic import get from molsysmt.lib import bonds as _libbonds n_atoms, n_bonds = get(item, target='system', n_atoms=True, n_bonds=True) if n_bonds == 0: output = np.full(n_atoms, None, dtype=object) else: atoms_indices = get(item, target='bond', indices='all', atom_index=True) output = _libbonds.component_indices(atoms_indices, n_atoms, n_bonds) output = np.ascontiguousarray(output, dtype=int) if indices is not 'all': output = output[indices] return output
def is_solvated(molecular_system): from molsysmt.basic import get from molsysmt import puw output = False n_waters, volume = get(molecular_system, target='system', n_waters=True, box_volume=True) if (n_waters > 0) and (volume is not None): density_number = puw.get_value((n_waters / volume), to_unit='1/nm**3') if (density_number) > 15: output = True return output
def component_type_from_component(item, indices='all'): from molsysmt.basic import get group_types_from_component = get(item, target='component', indices=indices, group_type=True) output = [] for group_types in group_types_from_component: component_type = _type_from_group_type(group_types) output.append(component_type) output = np.array(output, dtype=object) return output
def mutate(molecular_system, residue_indices=None, to_residue_names=None, engine='PDBFixer', verbose=False): if engine == "PDBFixer": from molsysmt.basic import get, convert, get_form if not hasattr(residue_indices, '__iter__'): residue_indices = [residue_indices] if not hasattr(to_residue_names, '__iter__'): to_residue_names = [to_residue_names] to_residue_names = [name.upper() for name in to_residue_names] form_in = get_form(molecular_system) tmp_molecular_system = convert(molecular_system, to_form="pdbfixer.PDBFixer") from_residue_names, residue_ids, in_chain_ids = get( tmp_molecular_system, target='group', indices=residue_indices, group_name=True, group_id=True, chain_id=True) for residue_id, from_residue_name, to_residue_name, in_chain_id in zip( residue_ids, from_residue_names, to_residue_names, in_chain_ids): mutation_string = "-".join( [from_residue_name, str(residue_id), to_residue_name]) if verbose: print(mutation_string) tmp_molecular_system.applyMutations([mutation_string], in_chain_id) tmp_molecular_system = convert(tmp_molecular_system, to_form=form_in) return tmp_molecular_system else: raise NotImplementedError
def buried_fraction(molecular_system, selection='all', type='janin'): from molsysmt.basic import get if type == 'janin': from .groups.buried_fraction import janin as values else: raise NotImplementedError() group_types = get(molecular_system, target='group', selection=selection, name=True) output = [] for ii in group_types: output.append(values[ii.upper()]) output = np.array(output) return output
def get_bondgraph(molecular_system, nodes_name='atom_index', selection='all', syntaxis='MolSysMT', to_form='networkx.Graph', check=True): # tengo que incluir la forma NetworkX para convertir. # en el caso de convert, lo que obtengo es una red con el nombre de los nodos dado por la # con el indice de atomo empezando por cero (todavía no lo he decidido) # el caso de este método es que nos da un grafo con los nodos nombrados según # nodes_name en ['atom_index', 'short_string', 'long_string'] if check: from molsysmt.tools.molecular_system import is_molecular_system if not is_molecular_system(molecular_system): raise MolecularSystemNeededError() tmp_molecular_system = None to_form = digest_to_form(to_form) if to_form == 'networkx.Graph': G = Graph() if nodes_name is 'atom_index': atom_indices, bonded_atoms = get(molecular_system, target='atom', selection=selection, syntaxis=syntaxis, atom_index=True, inner_bonded_atoms=True, check=False) G.add_nodes_from(atom_indices) G.add_edges_from(bonded_atoms) else: raise NotImplementedError tmp_molecular_system = G else: raise NotImplementedError return tmp_molecular_system
def get_sasa (molecular_system, target='atom', selection='all', structure_indices='all', syntaxis='MolSysMT', engine='MDTraj'): engine = digest_engine(engine) target = digest_target(target) if engine == 'MDTraj': from mdtraj import shrake_rupley tmp_item = convert(molecular_system, structure_indices=structure_indices, to_form='mdtraj.Trajectory') sasa_array = shrake_rupley(tmp_item, mode='atom') # tiene probe_radius y n_sphere_points if target=='atom': if selection is not 'all': atom_indices = select(molecular_system, selection=selection, syntaxis=syntaxis) sasa_array = sasa_array[:,atom_indices] else: sets_atoms = get(molecular_system, target=target, selection=selection, syntaxis=syntaxis, atom_index=True) n_sets = len(sets_atoms) n_structures = sasa_array.shape[0] new_sasa_array = np.empty([n_structures, n_sets], dtype='float') for ii in range(n_sets): new_sasa_array[:,ii] = sasa_array[:,sets_atoms[ii].astype(int)].sum(axis=1) sasa_array = new_sasa_array sasa_array = puw.quantity(sasa_array, 'nm**2') sasa_array = puw.standardize(sasa_array) else: raise NotImplementedError("Engine not implemented yet") return sasa_array
def to_openmm_Simulation(item, molecular_system=None, atom_indices='all', structure_indices='all'): from molsysmt.basic import convert, get from openmm.app import Simulation topology = convert(molecular_system, to_form='openmm.Topology', selection=atom_indices) positions = get(molecular_system, target='atom', selection=atom_indices, structure_indices=structure_indices, coordinates=True) positions = puw.convert(positions[0], to_unit='nm', to_form='openmm.unit') simulation = convert(molecular_system, to_form='molsysmt.Simulation') integrator = simulation.to_openmm_Integrator() platform = simulation.to_openmm_Platform() properties = simulation.get_openmm_Simulation_parameters() tmp_item = Simulation(topology, item, integrator, platform, properties) tmp_item.context.setPositions(positions) if simulation.initial_velocities_to_temperature: temperature = puw.convert(simulation.temperature, to_unit='K', to_form='openmm.unit') tmp_item.context.setVelocitiesToTemperature(temperature) if molecular_system is not None: tmp_molecular_system = molecular_system.combine_with_items( tmp_item, atom_indices=atom_indices, structure_indices=structure_indices) else: tmp_molecular_system = None return tmp_item, tmp_molecular_system
def transmembrane_tendency(molecular_system, selection='all', type='zhao'): from molsysmt.basic import get if type == 'zhao': from .groups.transmembrane_tendency import zhao as values elif type == 'senes': from .groups.transmembrane_tendency import senes as values else: raise NotImplementedError() group_types = get(molecular_system, target='group', selection=selection, name=True) output = [] for ii in group_types: output.append(values[ii.upper()]) output = np.array(output) return output
def volume(molecular_system, selection='all', type='grantham'): from molsysmt.basic import get if type == 'grantham': from .groups.volume import grantham as values else: raise NotImplementedError() group_types = get(molecular_system, target='group', selection=selection, name=True) output = [] for ii in group_types: output.append(values[ii.upper()]) output = np.array(output) return output
def surface_area(molecular_system, selection='all', type='collantes'): from molsysmt.basic import get if type == 'collantes': from .groups.surface_area import collantes as values else: raise NotImplementedError() group_types = get(molecular_system, target='group', selection=selection, name=True) output = [] for ii in group_types: output.append(values[ii.upper()]) output = np.array(output) return output
def polarity(molecular_system, selection='all', type='grantham'): from molsysmt.basic import get if type == 'grantham': from molsysmt.physico_chemical_properties.groups.polarity import grantham as values elif type == 'zimmerman': from molsysmt.physico_chemical_properties.groups.polarity import zimmerman as values else: raise NotImplementedError() group_names = get(molecular_system, target='group', selection=selection, group_name=True) output = [] for ii in group_names: output.append(values[ii.upper()]) output = np.array(output) return output
def translate(molecular_system, translation=None, selection='all', structure_indices='all', syntaxis='MolSysMT', in_place=False, check=True): from molsysmt.basic import get, set, select, copy, is_molecular_system if check: if not is_molecular_system(molecular_system): raise MolecularSystemNeededError() atom_indices = select(molecular_system, selection=selection, syntaxis=syntaxis) coordinates = get(molecular_system, target='atom', indices=atom_indices, structure_indices=structure_indices, coordinates=True) length_units = puw.get_unit(coordinates) coordinates = puw.get_value(coordinates) translation = puw.get_value(translation, to_unit=length_units) n_atoms = coordinates.shape[1] if type(translation) in [list, tuple]: translation = np.array(translation) if type(translation) == np.ndarray: if len(translation.shape) == 1: if translation.shape[0] == 3: translation = np.tile(translation, (n_atoms, 1)) else: raise ValueError('Wrong shape of translation vector') elif len(translation.shape) == 2: if translation.shape[1] != 3: raise ValueError('Wrong shape of translation vector') elif len(translation.shape) == 3: if translation.shape[2] != 3: raise ValueError('Wrong shape of translation vector') if translation.shape[1] == 1: translation = np.tile(translation, (n_atoms, 1)) coordinates += translation coordinates = coordinates * length_units if in_place: return set(molecular_system, target='atom', indices=atom_indices, structure_indices=structure_indices, coordinates=coordinates) else: tmp_molecular_system = copy(molecular_system) set(tmp_molecular_system, target='atom', indices=atom_indices, structure_indices=structure_indices, coordinates=coordinates) return tmp_molecular_system
def elements_to_string(molecular_system, indices=None, target='atom', check=True): from molsysmt.basic import get target = digest_target(target) string = [] if target == 'atom': atom_indices, atom_ids, atom_names = get(molecular_system, target=target, indices=indices, index=True, id=True, name=True) for atom_index, atom_id, atom_name in zip(atom_indices, atom_ids, atom_names): string.append(atom_name + '-' + str(atom_id) + '@' + str(atom_index)) elif target == 'group': group_indices, group_ids, group_names = get(molecular_system, target=target, indices=indices, index=True, id=True, name=True) for group_index, group_id, group_name in zip(group_indices, group_ids, group_names): string.append(group_name + '-' + str(group_id) + '@' + str(group_index)) elif target == 'component': component_indices = get(molecular_system, target=target, indices=indices, index=True) for component_index in component_indices: string.append(str(component_index)) elif target == 'chain': chain_indices, chain_ids, chain_names = get(molecular_system, target=target, indices=indices, index=True, id=True, name=True) for chain_index, chain_id, chain_name in zip(chain_indices, chain_ids, chain_names): if chain_name is None: chain_name = '_' string.append(chain_name + '-' + str(chain_id) + '@' + str(chain_index)) elif target == 'molecule': molecule_indices, molecule_names = get(molecular_system, target=target, indices=indices, index=True, name=True) for molecule_index, molecule_name in zip(molecule_indices, molecule_names): if molecule_name is None: molecule_name = '_' string.append(molecule_name + '@' + str(molecule_index)) elif target == 'entity': entity_indices, entity_names = get(molecular_system, target=target, indices=indices, index=True, name=True) for entity_index, entity_name in zip(entity_indices, entity_names): if entity_name is None: molecule_name = '_' string.append(entity_name + '@' + str(entity_index)) return string