RefoldedMembraneTrajectory.py 6.79 KB
Newer Older
1
2
3
4
5
#MDANSE : Molecular Dynamics Analysis for Neutron Scattering Experiments
#------------------------------------------------------------------------------------------
#Copyright (C)
#2015- Eric C. Pellegrini Institut Laue-Langevin
#BP 156
6
7
#71 avenue des Martyrs
#38000 Grenoble Cedex 9
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#France
#pellegrini[at]ill.fr
#goret[at]ill.fr
#aoun[at]ill.fr
#
#This library is free software; you can redistribute it and/or
#modify it under the terms of the GNU Lesser General Public
#License as published by the Free Software Foundation; either
#version 2.1 of the License, or (at your option) any later version.
#
#This library is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#Lesser General Public License for more details.
#
#You should have received a copy of the GNU Lesser General Public
#License along with this library; if not, write to the Free Software
#Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

''' 
Created on Apr 10, 2015

30
:author: Eric C. Pellegrini
31
32
33
'''

import collections
34
import os
35
36
37
38
39

from MMTK.Collections import Collection
from MMTK.ParticleProperties import Configuration
from MMTK.Trajectory import SnapshotGenerator, Trajectory, TrajectoryOutput

40
from MDANSE.Framework.Jobs.IJob import IJob, JobError
41
42
43
44
from MDANSE.Mathematics.Geometry import center

class RefoldedMembraneTrajectory(IJob):
    """
45
46
47
48
    Rebuild the trajectory of a membrane such that the lipids in the upper leaflet are actually in the upper half of the simulation box 
    and therefore the lipids in the lower leaflet are in the lower half of the box i.e. the membrane is centred in the simulation box.
    
    :note: The normal to the membrane is assumed to be parallel to the z axis. 
49
50
51
52
53
54
55
56
    """
    
    type = 'rmt'
    
    label = "Refolded Membrane Trajectory"

    category = ('Macromolecules','Lipids')
    
57
    ancestor = ["mmtk_trajectory"]
58

59
    settings = collections.OrderedDict()
60
    settings['trajectory'] = ('mmtk_trajectory',{'default':os.path.join('..','..','..','Data','Trajectories','MMTK','dmpc_in_periodic_universe.nc')})
61
62
    settings['frames'] = ('frames', {'dependencies':{'trajectory':'trajectory'}})
    settings['axis'] = ('single_choice', {'label':"membrane axis", 'choices':['a','b','c'], 'default':'c'})
63
64
    settings['upper_leaflet'] = ('string', {'label':"name of the lipid of the upper leaflet", 'default':"DMPC"})
    settings['lower_leaflet'] = ('string', {'label':"name of the lipid of the lower leaflet", 'default':"DMPC"})
65
    settings['output_files'] = ('output_files', {'formats':["netcdf"]})
66
67
68
69
70
71
72
73
74
                
    def initialize(self):
        """
        Initialize the input parameters and analysis self variables
        """

        self.numberOfSteps = self.configuration['frames']['number']
        
        self._universe = self.configuration['trajectory']['instance'].universe
75
        
76
77
78
        self._upperLeaflet = Collection([obj for obj in self._universe.objectList() if obj.name == self.configuration["upper_leaflet"]["value"]])
        self._lowerLeaflet = Collection([obj for obj in self._universe.objectList() if obj.name == self.configuration["lower_leaflet"]["value"]])
        self._membrane = Collection(self._upperLeaflet,self._lowerLeaflet)
79
80
81
82
        
        if (not self._membrane):
            raise JobError('No objects matching a lipid membrane could be found in the universe.')
            
83
84
85
        self._upperLeafletIndexes = [at.index for at in self._upperLeaflet.atomList()]
        self._lowerLeafletIndexes = [at.index for at in self._lowerLeaflet.atomList()]
        self._membraneIndexes = [at.index for at in self._membrane.atomList()]
86
        
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
        # The output trajectory is opened for writing.
        self._rmt = Trajectory(self._membrane, self.configuration['output_files']['files'][0], "w")
        
        # The title for the trajectory is set. 
        self._rmt.title = self.__class__.__name__

        # Create the snapshot generator.
        self._snapshot = SnapshotGenerator(self._universe, actions = [TrajectoryOutput(self._rmt, "all", 0, None, 1)])

        self._axis = self.configuration["axis"]["index"]                

    def run_step(self, index):
        """
        Runs a single step of the job.\n
 
        :Parameters:
            #. index (int): The index of the step.
        :Returns:
            #. index (int): The index of the step. 
            #. None
        """

        # get the Frame index
        frameIndex = self.configuration['frames']['value'][index]
              
        # The configuration corresponding to this index is set to the universe.
        self._universe.setFromTrajectory(self.configuration['trajectory']['instance'], frameIndex)

        conf = self._universe.contiguousObjectConfiguration()

        boxCoords = self._universe._realToBoxPointArray(conf.array)

        # Compute the center of gravity of the whole lower leaflet.
        lowerLeafletCenter = center(boxCoords[self._lowerLeafletIndexes,:])[self._axis]
        for lip in self._upperLeaflet:
            idxs = [at.index for at in lip.atomList()]
            currCenter = center(boxCoords[idxs,:])[self._axis]
            if currCenter < lowerLeafletCenter:
                boxCoords[idxs,2] += 1.0 

        upperLeafletCenter = center(boxCoords[self._upperLeafletIndexes,:])[self._axis]
        for lip in self._lowerLeaflet:
            idxs = [at.index for at in lip.atomList()]
            currCenter = center(boxCoords[idxs,:])[self._axis]
            if currCenter > upperLeafletCenter:
                boxCoords[idxs,2] -= 1.0

        conf = Configuration(self._universe, self._universe._boxToRealPointArray(boxCoords))
        
        self._universe.setConfiguration(conf)
                          
        # The times corresponding to the running index.
        t = self.configuration['frames']['time'][index]
        
        # A snapshot of the universe is written to the output trajectory.
        self._snapshot(data = {'time': t})
                                
        return index, None

    def combine(self, index, x):
        """
        Combines returned results of run_step.\n
        :Parameters:
            #. index (int): The index of the step.\n
            #. x (any): The returned result(s) of run_step
        """   
        pass
        
    def finalize(self):
        """
        Finalizes the calculations (e.g. averaging the total term, output files creations ...).
        """ 
        # The input trajectory is closed.
        self.configuration['trajectory']['instance'].close()
                                                    
        # The output trajectory is closed.
        self._rmt.close()