Usage
This page includes a practical guide to using MembraneCurvature with the surface derivation method of choice (Fourier, binning, or binning nearest). The available methods are introduced using membrane-only systems, followed by examples of membrane-protein systems with position restraints, and with no position restraints.
Note
Examples in this page use data files from MDAnalysisTests and files from
membrane_curvature.tests.datafiles. To run these examples, MDAnalysisTests
and MembraneCurvature must be installed.
1. Membrane-only systems
The examples in this section use a system that comprises a lipid bilayer of
DPPC:CHOL using the Martini force field. Since we have a bilayer, we select
atoms of phospholipid head groups in the upper leaflet only using the
select parameter. Coordinate wrapping is applied only where required
by the selected surface method.
Some points to keep in mind when calculating membrane curvature in membrane-only systems are addressed in this blog post.
Warning
MembraneCurvature does not identify leaflets. It relies on the user to pass
the correct AtomGroup with select. Be aware
of the selection you provide: if both leaflets are included in the selection,
they are treated as a single surface, hence producing incorrect results.
1.1 Fourier surface method (default)
We can calculate membrane curvature using the Fourier surface method by either
setting surface_method='fourier' with default values fourier_m=2,
fourier_n=2, or omitting surface_method, fourier_m, and fourier_n
to rely on the defaults:
import MDAnalysis as mda
from membrane_curvature.base import MembraneCurvature
from MDAnalysis.tests.datafiles import Martini_membrane_gro
universe = mda.Universe(Martini_membrane_gro)
# run with all default values
curvature_upper_leaflet = MembraneCurvature(universe,
select='resid 1-225 and name PO4',
).run()
mean_upper_leaflet = curvature_upper_leaflet.results.average_mean
gaussian_upper_leaflet = curvature_upper_leaflet.results.average_gaussian
Warning
Use fourier_m=2 and fourier_n=2, the default values, unless you need shorter
wavelengths. Increasing fourier_m and fourier_n may introduce noise in the fit
rather than improving curvature results.
Tip
When using the Fourier method, do not set wrap=True. Because periodic boundary
conditions are handled by the Fourier fit, passing wrap=True raises a ValueError.
The code above is equivalent to running with explicit parameters for surface_method,
fourier_m, fourier_n, and curvature_on with the default values:
curvature_upper_leaflet = MembraneCurvature(universe,
select='resid 1-225 and name PO4',
surface_method='fourier',
fourier_m=2,
fourier_n=2,
curvature_on='per_frame',
).run()
Note that by default, MembraneCurvature runs with curvature_on='per_frame' when
the parameter curvature_on is omitted. See section 1.3 Average curvature mode (curvature_on) for the
difference between 'per_frame' and 'average_surface'.
Important
The optional parameters padding and fft_filter are not available with
surface_method='fourier'.
Advanced: Tune the Fourier least-squares cutoff (fourier_rcond)
The Fourier surface is fit by solving a linear least-squares system with singular-value truncation SVD.
The optional cutoff fourier_rcond controls which singular values are treated as “effectively zero” and
therefore removed from the solve. Smaller values keep more directions, and potentially noisier if
the system is underdetermined. Larger values regularize more aggressively.
In MembraneCurvature, pass this cutoff as fourier_rcond:
curvature_upper_leaflet = MembraneCurvature(universe,
select='resid 1-225 and name PO4',
surface_method='fourier',
fourier_m=2,
fourier_n=2,
fourier_rcond=1e-12
).run()
If the effective rank of the design matrix is smaller than the number of
fitted parameters, a UserWarning is emitted. The solver still returns a
valid least squares solution of smallest Euclidean norm, but the coefficients are
not uniquely determined by the data.
Warning
fourier_rcond controls how aggressively we ignore poorly constrained combinations of
Fourier coefficients. We strongly recommend using fourier_rcond with its
default value None.
Larger values keep fewer singular-value directions (more stable / more
regularized). Smaller values keep more directions (closer fit but potentially noisier).
Rough intuition:
fourier_rcond=None: sensible default. Uses NumPy’s heuristic cutoff.fourier_rcond=0: truncate only exactly zero singular values.fourier_rcond=1e-12or1e-10: more aggressive truncation. It can reduce noise when the fit is underdetermined.
Note
All Fourier least-squares steps (design matrix, SVD, coefficients) use
64-bit floating point numpy.float64(). The cutoff defined by fourier_rcond is
a relative threshold: singular values with \(s \le rcond \cdot s_{\max}\) are dropped.
The meaningful scale is therefore relative to the largest singular value \(s_{\max}\), not
absolute coordinates or heights.
With fourier_rcond=None, the cutoff scales with the size of the least-squares problem, typically
the number of atoms in the selection, or the number of fitted coefficients if that is larger.
Passing a value much smaller than \(\sim 10^{-16}\), or much smaller than that automatic
cutoff, usually has no visible effect. To smooth an underdetermined fit on purpose, use larger
values such as 1e-12 or 1e-10 (see warning above).
1.2 Binning methods
Binning methods calculate curvature using finite differences on the derived surfaces.
Two binning methods are available: 'binning' and 'binning_nearest'.
For these methods, the optional parameter padding is available to reduce edge artifacts at the
simulation box boundaries, particularly for Gaussian curvature given that second order derivatives
amplify edge effects. Section 1.2.3 Periodic edge padding includes a usage example.
The optional parameter fft_filter is also available for binning methods, with a different
purpose. It applies a brick-wall filter in reciprocal space to remove high-frequency noise from
the averaged height field, and it is not a fix for edge artifacts. See section
1.2.4 FFT filtering for a practical example.
1.2.1 Binning
Set surface_method='binning' to run membrane curvature with raw binning by
providing the values for the grid n_x_bins and n_y_bins.
import MDAnalysis as mda
from membrane_curvature.base import MembraneCurvature
from MDAnalysis.tests.datafiles import Martini_membrane_gro
universe = mda.Universe(Martini_membrane_gro)
# run with binning and coordinate wrapping
curvature_upper_leaflet = MembraneCurvature(universe,
select='resid 1-225 and name PO4',
surface_method='binning',
n_x_bins=8,
n_y_bins=8,
wrap=True,
).run()
mean_upper_leaflet = curvature_upper_leaflet.results.average_mean
gaussian_upper_leaflet = curvature_upper_leaflet.results.average_gaussian
Tip
When passing a raw trajectory with the surface_method='binning', set
wrap=True. With this setting, the \(x\) and \(y\) coordinates of the
AtomGroup are wrapped into the primary unit cell. Atoms outside the primary cell
are otherwise skipped during binning, which can leave bins empty or build the surface
from fewer lipids than expected.
Warning
With surface_method='binning', empty bins are stored as
numpy.nan and can propagate through the finite difference
calculation of curvature. Edge and corner grid points also have fewer
neighbours, which can introduce artifacts that are strongest in Gaussian
curvature because it uses second derivatives.
If Gaussian curvature is important for your analysis, consider running with
padding=True to reduce edge artifacts at the simulation box boundaries.
Padding does not fill bins that have no reference atoms.
Empty bins remain as numpy.nan when the grid is too coarse or the
sampling is too sparse. See Periodic Edge Padding for a
practical example.
1.2.2 Binning nearest
Set surface_method='binning_nearest' to run membrane curvature using a binning method
that assigns each grid point the \(z\) coordinate of the nearest lipid in the \(xy\)
plane. The nearest lipid is searched over the whole selection, and the reference point of each
lipid is the center of its selected atoms.
Tip
When running with surface_method='binning_nearest', omit the wrap parameter,
or set it to False. Coordinate wrapping is not valid with this method, because
distances between grid points and lipids are calculated to the closest periodic image.
Passing wrap=True raises a ValueError.
import MDAnalysis as mda
from membrane_curvature.base import MembraneCurvature
from MDAnalysis.tests.datafiles import Martini_membrane_gro
universe = mda.Universe(Martini_membrane_gro)
# run with binning nearest - no coordinate wrapping
curvature_upper_leaflet = MembraneCurvature(universe,
select='resid 1-225 and name PO4',
surface_method='binning_nearest',
n_x_bins=8,
n_y_bins=8,
).run()
mean_upper_leaflet = curvature_upper_leaflet.results.average_mean
gaussian_upper_leaflet = curvature_upper_leaflet.results.average_gaussian
With this method, grid points sit at the bin corners, and not at the bin centers as in the
binning and fourier methods. Therefore, for the same values of n_x_bins and
n_y_bins, the maps derived with binning_nearest are offset by half a bin with respect to
the maps derived with binning and fourier. Because every grid point is assigned the
\(z\) of a lipid, this method does not produce empty bins.
The details of the difference between the two binning methods are described in the 2.3. Binning nearest method section of the Algorithm page.
Important
Unlike raw binning, binning_nearest does not produce empty bins. Every grid point
is assigned the \(z\) of the nearest lipid in the selection, so the generated surface
has a value at every bin.
However, edge artifacts are still present in Gaussian curvature because second derivatives
are calculated via finite differences. To reduce edge artifacts, consider running with
padding=True.
1.2.3 Periodic edge padding
With the parameter padding, MembraneCurvature adds a buffer around the
primary grid, calculates curvature on the expanded grid, and then clips the
results back to the initial n_x_bins x n_y_bins size.
Warning
Padding is supported only for surface_method='binning' and
surface_method='binning_nearest', and only when the simulation box
is orthorhombic.
Padding is optional and is set to False by default. To apply edge periodic padding,
run with padding=True:
import MDAnalysis as mda
from membrane_curvature.base import MembraneCurvature
from MDAnalysis.tests.datafiles import Martini_membrane_gro
universe = mda.Universe(Martini_membrane_gro)
# run with binning and padding
curvature_upper_leaflet = MembraneCurvature(universe,
select='resid 1-225 and name PO4',
surface_method='binning', # or 'binning_nearest'
n_x_bins=8,
n_y_bins=8,
wrap=False,
padding=True,
).run()
In this example, we use surface_method='binning' with wrap=False,
because the periodic images added in the padded buffer provide the lateral
periodic boundary conditions. For this reason, MembraneCurvature does not
raise the usual wrap == False warning when padding is enabled.
Tip
Running with padding=True is particularly useful for analysis of Gaussian curvature,
where edge artifacts can be significant.
Padding runs with edge_pad_bins=2 by default. A buffer of 2 bins on each side
is usually enough to fix edge artifacts in the calculation of second derivatives of
Gaussian curvature. Values above edge_pad_bins=4 are unlikely to change the curvature
values and mainly increase runtime. In that case MembraneCurvature emits a
UserWarning. If edge_pad_bins is larger than n_x_bins or
n_y_bins, a ValueError is raised.
Alternatively, use edge_pad_bins if you need to change the width of the default buffer
regions of 2 bins on each side.
For example, if you need to change the width of the buffer regions to 3 bins on each side, you can do:
curvature_upper_leaflet = MembraneCurvature(universe,
select='resid 1-225 and name PO4',
surface_method='binning',
n_x_bins=8,
n_y_bins=8,
wrap=False,
padding=True,
edge_pad_bins=3,
).run()
See Periodic Edge Padding in the Algorithm page for more details.
1.2.4 FFT filtering
FFT filtering is available for the binning methods surface_method='binning' and
surface_method='binning_nearest' via the parameter fft_filter. This option
applies a brick-wall passband to the averaged surface array.
Three filtering modes are available:
fft_filter=None(default): no FFT filtering applied.fft_filter='auto': low-pass with pass band(0, 0.5 * q_Nyq), where \(q_{\mathrm{Nyq}} = \min(\pi/\Delta x,\ \pi/\Delta y)\) from bin widthsdxanddy.fft_filter={'q': (q_low, q_high)}: custom pass band in the reciprocal space(q_low, q_high)range in rad/Å (advanced).
An example of running an analysis with the auto filtering mode (fft_filter='auto') looks like this:
import MDAnalysis as mda
from membrane_curvature.base import MembraneCurvature
from MDAnalysis.tests.datafiles import Martini_membrane_gro
universe = mda.Universe(Martini_membrane_gro)
fft_auto_upper_leaflet = MembraneCurvature(universe,
select='resid 1-225 and name PO4',
surface_method='binning',
n_x_bins=8,
n_y_bins=8,
wrap=True,
fft_filter='auto').run()
Tip
You can combine fft_filter with padding=True. Enable fft_filter
only if you want to filter the averaged height field in reciprocal space to remove
high-frequency noise.
Important
When fft_filter is enabled and the parameter curvature_on is omitted,
MembraneCurvature selects curvature_on='average_surface'. Therefore,
average_mean and
average_gaussian
are calculated from the filtered average surface.
With an explicit curvature_on='per_frame', the filtered surface is still
stored in
average_z_surface,
but the average curvature maps remain the averages of the unfiltered
per-frame curvature arrays.
Advanced: Tuning the brick-wall filter
The brick-wall filter can be configured with custom cutoff values by passing a tuple
(q_low, q_high) in rad/Å as fft_filter={'q': (q_low, q_high)} to define a custom
filter band. These values define the filter passband directly in reciprocal space.
Warning
Use custom fft_filter={'q': (q_low, q_high)} values with caution. The
pass band you choose directly affects the filtered average surface and the
curvature maps derived from it. Poorly chosen bounds can produce inaccurate
curvature values.
We recommend fft_filter='auto' by default. The manual mode is available
for flexibility, but you should only use it when you can justify the chosen
bounds for your system.
To set the pass band relative to the Nyquist magnitude \(q_{\mathrm{Nyq}}\), compute
it with nyquist_q() from the bin widths dx and dy,
then scale to set the pass band. For example, to set the passband to (0, 0.25 * q_Nyq):
import MDAnalysis as mda
from membrane_curvature import nyquist_q
from membrane_curvature.base import MembraneCurvature
from MDAnalysis.tests.datafiles import Martini_membrane_gro
universe = mda.Universe(Martini_membrane_gro)
# set the number of bins
n_x_bins = 8
n_y_bins = 8
# calculate the bin widths
dx = universe.dimensions[0] / n_x_bins
dy = universe.dimensions[1] / n_y_bins
# calculate the Nyquist magnitude
q_Nyq = nyquist_q(dx, dy)
# run with binning and manual filter band
curvature_upper_leaflet = MembraneCurvature(universe,
select='resid 1-225 and name PO4',
surface_method='binning',
n_x_bins=n_x_bins,
n_y_bins=n_y_bins,
wrap=True,
fft_filter={'q': (0, 0.25 * q_Nyq)},
).run()
The bin widths are also stored in the analysis instance as curvature_upper_leaflet.dx and
curvature_upper_leaflet.dy. You can use them to calculate the Nyquist magnitude of an
existing analysis with
nyquist_q(curvature_upper_leaflet.dx, curvature_upper_leaflet.dy).
1.3 Average curvature mode (curvature_on)
The parameter curvature_on controls how
average_mean and
average_gaussian
are obtained. It is available for all surface methods.
Two modes are available:
curvature_on='per_frame'(default): mean and Gaussian curvature are calculated for every frame, and the average maps are the time averages of those per-frame arrays. Passingcurvature_on=Noneis equivalent to'per_frame'.curvature_on='average_surface': the average surface is built first, and mean and Gaussian curvature are calculated once from that average surface.
The scientific difference between the two paths is described in the Algorithm page, section 4. Calculate curvature.
To run with the default per-frame averaging mode, you can omit curvature_on,
or set it explicitly:
import MDAnalysis as mda
from membrane_curvature.base import MembraneCurvature
from MDAnalysis.tests.datafiles import Martini_membrane_gro
universe = mda.Universe(Martini_membrane_gro)
curvature_upper_leaflet = MembraneCurvature(universe,
select='resid 1-225 and name PO4',
curvature_on='per_frame',
).run()
mean_upper_leaflet = curvature_upper_leaflet.results.average_mean
gaussian_upper_leaflet = curvature_upper_leaflet.results.average_gaussian
To calculate curvature from the average surface, set the parameter curvature_on='average_surface'
explicitly:
curvature_upper_leaflet = MembraneCurvature(universe,
select='resid 1-225 and name PO4',
curvature_on='average_surface',
).run()
mean_upper_leaflet = curvature_upper_leaflet.results.average_mean
gaussian_upper_leaflet = curvature_upper_leaflet.results.average_gaussian
Important
In both examples above, the per-frame arrays
mean and
gaussian are still
stored. The choice of curvature_on changes how the average maps are
obtained, not whether per-frame results are stored.
The per-frame arrays are always stored in every run.
2. Membrane-protein systems
Tip
When passing raw trajectories in membrane-protein systems:
With position restraints: set
wrap=Truewhen running withsurface_method='binning'.With no position restraints: set
wrap=Falseand preprocess the trajectory with a rotational and translational fit. The preprocessing steps are described in section 2.2 Protein without position restraints.
Some points to keep in mind when calculating membrane curvature in membrane-protein systems with position restraints are addressed in this blog post.
2.1 Protein with position restraints
In this example, we have a simulation box comprising a copy of the Yiip transporter, embedded in a lipid bilayer of POPE:POPG. Similar to the example for membrane-only, we select the atoms for the upper leaflet to run the analysis.
2.1.1 Fourier surface method (default)
We can calculate membrane curvature using the default values with:
import MDAnalysis as mda
from membrane_curvature.base import MembraneCurvature
from MDAnalysis.tests.datafiles import XTC_MEMPROT, GRO_MEMPROT
universe = mda.Universe(GRO_MEMPROT, XTC_MEMPROT)
curvature_upper_leaflet = MembraneCurvature(universe,
select='resid 297-517 and name P'
).run()
mean_upper_leaflet = curvature_upper_leaflet.results.average_mean
gaussian_upper_leaflet = curvature_upper_leaflet.results.average_gaussian
2.1.2 Binning surface method
The same trajectory and selection can use the binning method by setting
surface_method='binning' with the values for n_x_bins and n_y_bins
and apply coordinate wrapping with wrap=True.
import MDAnalysis as mda
from membrane_curvature.base import MembraneCurvature
from MDAnalysis.tests.datafiles import XTC_MEMPROT, GRO_MEMPROT
universe = mda.Universe(GRO_MEMPROT, XTC_MEMPROT)
curvature_upper_leaflet = MembraneCurvature(universe,
select='resid 297-517 and name P',
surface_method='binning',
n_x_bins=8,
n_y_bins=8,
wrap=True
).run()
mean_upper_leaflet = curvature_upper_leaflet.results.average_mean
gaussian_upper_leaflet = curvature_upper_leaflet.results.average_gaussian
For edge padding and for surface_method='binning_nearest', the same options
described in sections 1.2.3 Periodic edge padding and 1.2.4 FFT filtering apply.
2.2 Protein without position restraints
For membrane-protein systems where the simulation setup has no position restraints on the protein, a trajectory preprocessing by the user is required. If the goal is to assess membrane curvature induced by the protein, the preprocessed trajectory should have the protein centered in the simulation box with translational and rotational fit.
For example, in Gromacs, the trajectory would be preprocessed with:
gmx trjconv -pbc whole -ur compact -c
gmx trjconv -fit rot+trans
gmx trjconv -fit transxy
After you have preprocessed the trajectory, use wrap=False (the trajectory is
already fitted and centered).
2.2.1 Fourier surface method (default)
With the default surface_method='fourier' and fourier_m=2, fourier_n=2:
import MDAnalysis as mda
from membrane_curvature.base import MembraneCurvature
from membrane_curvature.tests.datafiles import XTC_MEMBPROT_FIT, GRO_MEMBPROT_FIT
universe = mda.Universe(GRO_MEMBPROT_FIT, XTC_MEMBPROT_FIT)
curvature_lower_leaflet = MembraneCurvature(universe,
select='resid 2583-3042 and name PO4'
).run()
mean_lower_leaflet = curvature_lower_leaflet.results.average_mean
gaussian_lower_leaflet = curvature_lower_leaflet.results.average_gaussian
2.2.2 Binning nearest surface method with padding
Set surface_method='binning_nearest' with padding=True and the values
for n_x_bins and n_y_bins. Keep wrap=False, because coordinate
wrapping is not valid with surface_method='binning_nearest':
import MDAnalysis as mda
from membrane_curvature.base import MembraneCurvature
from membrane_curvature.tests.datafiles import XTC_MEMBPROT_FIT, GRO_MEMBPROT_FIT
universe = mda.Universe(GRO_MEMBPROT_FIT, XTC_MEMBPROT_FIT)
curvature_lower_leaflet = MembraneCurvature(universe,
select='resid 2583-3042 and name PO4',
surface_method='binning_nearest',
n_x_bins=10,
n_y_bins=10,
wrap=False,
padding=True,
).run()
mean_lower_leaflet = curvature_lower_leaflet.results.average_mean
gaussian_lower_leaflet = curvature_lower_leaflet.results.average_gaussian
Note
With surface_method='binning_nearest', distances between grid corners and
lipid reference points are calculated using the periodic simulation box.
Therefore, a lipid across the opposite box boundary can be selected as the
nearest lipid. Padding then adds periodic neighbors around the primary grid
before finite differences are calculated.
More information on how to visualize the results of the MDAnalysis Membrane Curvature tool can be found in the Visualization page.