pierrepo / grodecoder

GroDecoder extracts and identifies the molecular components of a structure file (PDB or GRO) issued from a molecular dynamics simulation.
https://grodecoder.streamlit.app/
BSD 3-Clause "New" or "Revised" License
0 stars 1 forks source link

Manage periodic condition, if there are still there #80

Open KarinDuong opened 3 months ago

KarinDuong commented 3 months ago

https://www.mdanalysis.org/2020/03/09/on-the-fly-transformations/ For example, with the function unwrap (https://docs.mdanalysis.org/1.0.0/documentation_pages/transformations/wrap.html#MDAnalysis.transformations.wrap.unwrap)

KarinDuong commented 3 months ago

To use the unwrap function on a system with periodic boundary conditions, we have to create segment. To create these segments, we need a topology file. Here the code I use :

import MDAnalysis as mda
from MDAnalysis.core.topologyattrs import TopologyAttr
from MDAnalysis.transformations import unwrap

structure_file = "structural filepath"
topology_file = ".tpr filepath"

u = mda.Universe(topology_file, structure_file)

# Define fragments
frag_array = u.atoms.resids # Fragment indices
u.add_TopologyAttr(TopologyAttr('fragindices', frag_array))

ag = u.atoms

# Define the unpacking transformation
unwrap_transform = unwrap(ag)

# Apply the transformation
u.trajectory.add_transformations(unwrap_transform)

# name of the file we about to create
output_gro_file = 'output_unwrapped.gro'
with mda.Writer(output_gro_file, n_atoms=u.atoms.n_atoms) as W:
    for ts in u.trajectory:
        W.write(u.atoms)

So the unwrap function use and need the same elements that the command : gmx trjconv -f [name GRO file] -s [name tpr file] -o [name for the new file that about to be create] -pbc mol