Skip to content

Commit

Permalink
In process
Browse files Browse the repository at this point in the history
  • Loading branch information
dprada committed Feb 29, 2024
1 parent 5b9ed28 commit 5315cfa
Show file tree
Hide file tree
Showing 5 changed files with 14,674 additions and 42 deletions.
1 change: 0 additions & 1 deletion molsysmt/form/molsysmt_MolSys/to_nglview_NGLWidget.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ def to_nglview_NGLWidget(item, atom_indices='all', structure_indices='all', skip
else:
tmp_item = item


tmp_item = show_molsysmt(tmp_item, skip_digestion=True)

return tmp_item
Expand Down
136 changes: 113 additions & 23 deletions molsysmt/form/molsysmt_MolSys/to_string_pdb_text.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,123 @@
from molsysmt._private.digestion import digest
from molsysmt import pyunitwizard as puw
from molsysmt._private.variables import is_all
from datetime import datetime

@digest(form='molsysmt.MolSys')
def to_string_pdb_text(item, atom_indices='all', structure_indices='all', skip_digestion=False):

now = datetime.now()

from molsysmt.pbc import get_lengths_and_angles_from_box

tmp_item = ""

tp=item.topology
st=item.structures

for ii in range(item.topology.atoms.shape[0]):
line = f"{'ATOM':<6}{[i]:>5} {tp.atoms[i]:<4}" \
f"{' ':1}{residue_names[i]:>3} {' ':1}{chain_ids[i]:>1}" \
f"{residue_numbers[i]:>4}{' ':1} {x_coords[i]:>8.3f}" \
f"{y_coords[i]:>8.3f}{z_coords[i]:>8.3f}{occupancies[i]:>6.2f}" \
f"{temp_factors[i]:>6.2f} {element_symbols[i]:>2}"
file.write(line + '\n')
line = f"ATOM {atom_numbers[i]:5d} {atom_names[i]:^4}{residue_names[i]:>3} {chain_ids[i]}{residue_numbers[i]:4d} {x_coords[i]:8.3f}{y_coords[i]:8.3f}{z_coords[i]:8.3f}{occupancies[i]:6.2f}{temp_factors[i]:6.2f} {atom_names[i][0]}"
# Escribe la línea en el archivo
file.write(line + '\n')

from . import to_openmm_Topology
from . import get_coordinates_from_atom
from ..openmm_Topology import to_string_pdb_text as openmm_Topology_to_string_pdb_text

coordinates = get_coordinates_from_atom(item, indices=atom_indices, structure_indices=structure_indices,
skip_digestion=True)
tmp_item = to_openmm_Topology(item, atom_indices=atom_indices, structure_indices=structure_indices,
skip_digestion=True)
tmp_item = openmm_Topology_to_string_pdb_text(tmp_item, coordinates=coordinates, skip_digestion=True)
description = "MOLECULAR SYSTEM"
date = now.strftime('%d-%b-%y').upper()
pdb_id = ''

line = f"HEADER {description:<40}{date:>9} {pdb_id:<4}\n"
tmp_item += line


line = f"REMARK 1 Created by MolSysMT version 1.0 on {now.strftime('%d-%b-%Y').upper()} at {now.strftime('%H:%M:%S')}\n"
tmp_item += line


with_multiple_models = False

if is_all(structure_indices):
if item.structures.coordinates.shape[0]>1:
with_multiple_models = True
if item.structures.structure_id is not None:
model_index = item.structures.structure_id
else:
model_index = list(range(item.structures.coordinates.shape[0]))
else:
if len(structure_indices)>1:
with_multiple_models = True
if item.structures.structure_id is not None:
model_index = item.structures.structure_id[structure_indices]
else:
model_index = list(range(len(structure_indices)))

if item.structures.box is not None:

if is_all(structure_indices):
lengths, angles = get_lengths_and_angles_from_box(item.structures.box[0])
else:
lengths, angles = get_lengths_and_angles_from_box(item.structures.box[structure_indices[0]])

a,b,c = puw.get_value(lengths[0], to_unit='angstrom')
alpha,beta,gamma = puw.get_value(angles[0], to_unit='degrees')

line = f"CRYST1{a:>9.3f}{b:>9.3f}{c:>9.3f}{alpha:>7.2f}{beta:>7.2f}{gamma:>7.2f}\n"
tmp_item += line

if is_all(atom_indices):
aux_df = item.topology.atoms
else:
aux_df = item.topology.atoms.iloc[atom_indices]
aux_df.reset_index(drop=True, inplace=True)

st_ii=0

if is_all(atom_indices):
aux_coors = puw.get_value(item.structures.coordinates[st_ii, :, :], to_unit='angstroms')
else:
aux_coors = puw.get_value(item.structures.coordinates[st_ii, atom_indices, :], to_unit='angstroms')

if with_multiple_models:

line = f"MODEL {model_index[st_ii]:>4}"
tmp_item += line

for atom in aux_df.itertuples():

head = 'ATOM'

atom_id = atom.atom_id
atom_name = atom.atom_name
group_name = item.topology.groups.iloc[atom.group_index,1]
group_id = item.topology.groups.iloc[atom.group_index,0]
chain_id = item.topology.chains.iloc[atom.chain_index,1]

x,y,z = aux_coors[atom.Index, :]

occupancy = 0.0
temp_factor = 0.0

element_symbol = atom.atom_type

line = (
f"{head:<6}"
f"{atom_id:>5}"
f"{' ':1}"
f"{atom_name:<4}"
f"{' ':1}"
f"{group_name:>3}"
f"{' ':1}"
f"{chain_id:>1}"
f"{group_id:>4}"
f"{' ':1}"
f"{x:>8.3f}"
f"{y:>8.3f}"
f"{z:>8.3f}"
f"{occupancy:>6.2f}"
f"{temp_factor:>6.2f}"
f"{' ':10}"
f"{element_symbol:>2}"
f"\n"
)
tmp_item += line

if with_multiple_models:

line = f"ENDMDL"
tmp_item += line


tmp_item += 'END\n'

return tmp_item

13 changes: 7 additions & 6 deletions molsysmt/form/openmm_Topology/to_molsysmt_Topology.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,23 +119,24 @@ def to_molsysmt_Topology(item, atom_indices='all', skip_digestion=False):

# components

tmp_item.rebuild_components()
tmp_item.rebuild_components(redefine_indices=True, redefine_ids=True, redefine_names=True,
redefine_types=True)

# molecules

tmp_item.rebuild_molecules()
tmp_item.rebuild_molecules(redefine_indices=True, redefine_ids=True, redefine_names=True,
redefine_types=True)

# chain types

tmp_item.rebuild_chain_types()
tmp_item.rebuild_chains(redefine_ids=True, redefine_types=True)

# entity

tmp_item.rebuild_entities()
tmp_item.rebuild_entities(redefine_indices=True, redefine_ids=True, redefine_names=True,
redefine_types=True)

# nan to None

tmp_item._fix_null_values()
tmp_item.bonds._sort_bonds()

tmp_item = tmp_item.extract(atom_indices=atom_indices, copy_if_all=False, skip_digestion=True)
Expand Down
Loading

0 comments on commit 5315cfa

Please sign in to comment.