Using MDTraj in SAMSON

Using MDTraj in SAMSON

MDTraj is a python library that allows users to manipulate molecular dynamics (MD) trajectories.

In this tutorial, we demonstrate how MDTraj can be used to analyze trajectories saved in Path nodes in SAMSON.

Content

Analyzing trajectories

Let us, for example, compute the radius of gyration and the RMSD for a protein with trajectories saved in Path in SAMSON. To use the functionality of MDTraj, we need to export trajectories we would like to analyze from SAMSON into a format supported by MDTraj (e.g., pdb format).

You can find the code in SAMSON Python Scripting samples on github.

Let us first import MDAnalysis modules that we are going to use in this tutorial:

import mdtraj

We will export trajectories from SAMSON (Path in terms of SAMSON) each in a separate pdb-file. For that, we define the following function, which has as parameters a Path and a prefix filename:

def exportTrajectoriesIntoPDB(sbpath, filename):
    '''
    Exports trajectories from a Path node 'sbpath' to xyz files with a name starting with 'filename'
    '''
    sbpath.currentStep = 0                                              # set currentStep for the Path to 0
    trajectory_files = []
 
    for step in range(sbpath.numberOfSteps):                            # loop over steps in the Path
        sbpath.currentStep = step                                       # increment currentStep
 
        fn = filename + str(step) + '.pdb'                              # a name of a file 
        trajectory_files.append(fn)                                     # append list of trajectory files
 
        indexer = SAMSON.getNodes('n.t sm')                             # get a node indexer for all atoms
 
        SAMSON.exportToFile(indexer, fn, '')                            # export current trajectory into a file 'fn'
 
    return trajectory_files                                             # return list of trajectory files

Next, we need to load the exported trajectories into MDTraj. Since we exported trajectories each in a separate file, this can be done using the following function.

def loadTrajectoriesInMDTraj(trajectory_files):
    '''
    Loads trajectories in MDTraj
    '''
    traj = mdtraj.load(trajectory_files[0])                             # load the first trajectory
    for i in range(1, len(trajectory_files)):                           # loop over trajectories
        traj += mdtraj.load(trajectory_files[i])                        # append new trajectory to the previous one
 
    return traj

To compute the radius of gyration and RMSD, we define the following functions, which use the functionality of MDTraj and have as an input parameter a MDTraj trajectory.

def computeRMSD(trajectory):
    '''
    Compute the RMSD
    '''
    rmsd = mdtraj.rmsd(traj[1:], traj[0])                               # get RMSD: 1st arg - target frames, 2nd arg - reference frame
 
    for i in range(rmsd.size):
        print("RMSD [frame: {0}] = {1} nm".format(i, rmsd[i]))
 
def computeRgyr(trajectory):
    '''
    Compute the radius of gyration
    '''
    rgyr = mdtraj.compute_rg(trajectory)                                # compute radius of gyration for each frame
    for i in range(rgyr.size):
        print("Rgyr [frame: {0}] = {1} nm".format(i, rgyr[i]))

Now, we can use these function to compute the desired parameters for a chosen Path. We consider that a Path for which we want to compute these parameters is the first one.

sbpaths = SAMSON.getNodes('n.t path')                                   # get all Path nodes
 
if sbpaths.size != 0:
    sbpath = sbpaths[0]                                                 # get the first Path
 
    trajectory_files = exportTrajectoriesIntoPDB(sbpath, '/path/to/trajectories/traj-')
 
    traj = loadTrajectoriesInMDTraj(trajectory_files)                   # read from a list of trajectories
 
    computeRMSD(traj)
 
    computeRgyr(traj)

Superposition of structure

Let us consider a case, when we have two structures (e.g., two structural models in terms of SAMSON) in the active Document in SAMSON and we want to superimpose them in a way that minimizes the RMSD. For that, we can use MDTraj. In this example, we consider the first molecule to be the reference one, and the second one to be the one which should be superimposed on the first one.

You can find the code in SAMSON Python Scripting samples on github.

Let us import MDTraj modules that we are going to use in this tutorial:

import mdtraj

Next, we need to export structures from SAMSON into files in a format supported by MDTraj (e.g., pdb-format). Note, that in this example each structure is in a separate structural model in SAMSON. In this case, we need to get an indexer of all structural models and export an indexer of each structural model into a separate file.

indexer = SAMSON.getNodes('n.t sm')                                     # get an indexer of structural models            
 
ref = indexer[0]                                                        # get the first structural model    
mob = indexer[1]                                                        # get the second structural model    
ref_indexer = ref.getNodes()                                            # get an indexer of nodes for the first structural model
mob_indexer = mob.getNodes()
 
filename_ref = '/path/to/files/ref.pdb'
filename_mob = '/path/to/files/mob.pdb'
 
SAMSON.exportToFile(ref_indexer, filename_ref, '')                      # export the reference structure
                                                                        # the third input parameter is for options used for importing: '' is for default
SAMSON.exportToFile(mob_indexer, filename_mob, '')                      # export the structure which should be rotated

Now we can superimpose mob on ref using MDTraj, write the resulting structure into a pdb-file and import it into SAMSON:

t_ref = mdtraj.load(filename_ref)                                       # load the reference into MDTraj
t_mob = mdtraj.load(filename_mob)                                       # load the molecule which needs to be superposed
 
t_mob.superpose(t_ref)                                                  # superpose each conformation in the trajectory 'traj' upon a reference
 
filename_mob_superposed = '/path/to/files/mob_superposed_on_ref.pdb'
t_mob.save(filename_mob_superposed)
 
SAMSON.importFromFile(filename_mob_superposed, '')                      # import the superposed structure into SAMSON

Comments are closed.