Esempio n. 1
0
def WriteTiltDict(lipid_tilt_dict, outdir, filename_basis=''):
    """
    This function writes out the *lipid_tilt_dict* to the firectory *outdir*.
    It writes out one file for every key in *lipid_tilt_dict*, i.e. for every lipid type.
    Filenames are preceded by the *filename_basis*.

    :param lipid_tilt_dict: the lipid tilt dictionary as produced by :func:`AnalyzeLipidTiltAndSplay`.
    :param outdir: the directory to which the files will be written
    :param filename_basis: Will be prepended to all file names.

    :type lipid_tilt_dict: :class:`dict`
    :type outdir: :class:`str`
    :type filename_basis: :class:`str`
    """
    for ln in lipid_tilt_dict:
        tl = [180. * el / math.pi for lt in lipid_tilt_dict[ln][0]
              for el in lt]
        dl = [el for lt in lipid_tilt_dict[ln][1] for el in lt]
        if len(dl) != 0:
            if len(dl) != len(tl):
                print ('not the same number of tilts and distances', len(tl), len(dl))
            ll = [tl, dl]
            titles = ['tilt', 'dist from prot']
        else:
            ll = [tl]
            titles = ['tilt']
        file_utilities.WriteListOfListsInColumns(titles, ll, os.path.join(
            outdir, filename_basis + 'tilt_' + ln + '.txt'))
Esempio n. 2
0
def FitTiltDistribution(tilt_list, nbins=90, x_range=None, outdir='', filename_basis='', title_complement='', degrees=False):
    """
    This function extracts the tilt modulus from a list of lipid tilts.
    It will first fit a gaussian y=A exp[(x-mu)/sigma^2] to the distribution of tilts to determine
    the fitting range used to fit the potential of mean force (PMF).
    Different fitting ranges are used to estimate the error on the extracted tilt modulus

    :param tilt_list: A list of lipid splays.
    :param nbins:   The number of bins used when determining the distribution of splays
    :param x_range:  The range in which the distribution will be calculated. Defaults to [mean-3*std,mean+3*std].
    :param outdir:  the directory to which output files will be written, i.e. plots of splay distribution and PMF.
                    if it is not defined, plots will not be generated
    :param filename_basis: Will be prepended to all file names.
    :param title_complement: will be added in the title of the plots
    :param degrees: Whether plots should be in degrees or radians.

    :type tilt_list: :class:`list`
    :type nbins: :class:`int`
    :type x_range: :class:`tuple` of 2 floats
    :type outdir: :class:`str`
    :type filename_basis: :class:`str`
    :type title_complement: :class:`str`
    :type degrees: :class:`bool`

    :return: A tuple **(Chi, DeltaChi,Chi_list)**, containing the tilt modulus *Chi*,
             the estimated uncertainty on *Chi*, and the list of *Chi* values obtained from the different fitting ranges.
    :rtype: (:class:`float`,:class:`float`,:class:`list`)
    """
    if degrees:
        ac = math.pi / 180.
    else:
        ac = 1
    if x_range is None:
        x_range = [0, math.pi / (2. * ac)]
    pa = npy.histogram(tilt_list, nbins, range=x_range, density=True)
    bincenters = 0.5 * (pa[1][1:] + pa[1][:-1])
    pa = pa[0]
    pa2 = pa / npy.sin(bincenters * ac)
    fa = -npy.log(pa2)
    A, mu, sigma = _FitGaussian(bincenters, pa)
    ranges = [(max(mu - i * sigma, 0), mu + i * sigma)
              for i in [1, 1.25, 1.5, 1.75, 2.0]]
    print ("Gaussian y=A exp[(x-mu)/sigma^2] fitted to tilt distribution with A={0},mu={1} and sigma={2}".format(round(A, 2), round(mu, 2), round(sigma, 2)))
    print ("Using the following ranges to fit the PMF:", ranges)
    res_list = [_FitParabole(bincenters, fa, fitting_range, centered=True)
                for fitting_range in ranges]
    K_list = [2. * r[1] / (ac * ac) for r in res_list]
    DeltaK = npy.std(K_list)
    K = K_list[0]
    if outdir:
        file_utilities.WriteListOfListsInColumns(['bin', 'distribution', 'pmf'], [bincenters, pa, fa], os.path.join(
            outdir, '_'.join([filename_basis, 'tilt', 'distribution']) + '.txt'), separator=' ')
        title = 'Tilt distribution {0}'.format(title_complement)
        outfile = os.path.join(outdir, '_'.join(
            [filename_basis, 'tilt', 'distribution']) + '.png')
        _PlotGaussian(bincenters, pa, A, mu, sigma,
                      outfile, title=title, xlabel='Tilt')
        outfile = os.path.join(outdir, '_'.join(
            [filename_basis, 'tilt', 'fit']) + '.png')
        a, b = res_list[0]
        r = [el for el in ranges[0]]
        title = 'Tilt PMF {0}'.format(title_complement)
        _PlotParabola(bincenters, fa, a, b, 0.0, r, outfile, title, 'Tilt', r'$-\log\left[\frac{P(\alpha)}{\sin(\alpha)}\right]$', '$\chi={0}\pm {1} k_BT$'.format(
            round(K, int(-math.log10(K)) + 1), round(DeltaK, int(-math.log10(DeltaK)) + 1)))
    return K, DeltaK, K_list
Esempio n. 3
0
def FitSplayDistribution(splay_list, lipid_area, nbins=100, x_range=None, outdir='', filename_basis='', title_complement=''):
    """
    This function extracts the bending modulus from a list of splays.
    It will first fit a gaussian y=A exp[(x-mu)/sigma^2] to the distribution of splays to determine
    the fitting range used to fit the potential of mean force (PMF).
    Different fitting ranges are used to estimate the error on the extracted bending rigidity

    :param splay_list: A list of lipid splays.
    :param lipid_area: The area per lipid.
    :param nbins:   The number of bins used when determining the distribution of splays
    :param x_range:  The range in which the distribution will be calculated. Defaults to [mean-3*std,mean+3*std].
    :param outdir:  the directory to which output files will be written, i.e. plots of splay distribution and PMF.
                    if it is not defined, plots will not be generated
    :param filename_basis: Will be prepended to all file names.
    :param title_complement: will be added in the title of the plots

    :type splay_list: :class:`list`
    :type lipid_area: :class:`float`
    :type nbins: :class:`int`
    :type x_range: :class:`tuple` of 2 floats
    :type outdir: :class:`str`
    :type filename_basis: :class:`str`
    :type title_complement: :class:`str`

    :return: A tuple **(K, DeltaK,K_list)**, containing the bending rigidity *K*,
             the estimated uncertainty on *K*, and the list of *K* values obtained from the different fitting ranges.
    :rtype: (:class:`float`,:class:`float`,:class:`list`)
    """
    if not x_range:
        w = npy.std(splay_list)
        m = npy.average(splay_list)
        x_range = [m - 3 * w, m + 3 * w]
    pa = npy.histogram(splay_list, nbins, range=x_range, density=True)
    bincenters = 0.5 * (pa[1][1:] + pa[1][:-1])
    fa = -npy.log(pa[0])
    A, mu, sigma = _FitGaussian(bincenters, pa[0])
    ranges = [(mu - i * sigma, mu + i * sigma)
              for i in [1, 1.25, 1.5, 1.75, 2]]
    print ("Gaussian y=A exp[(x-mu)/sigma^2] fitted to tilt distribution with A={0},mu={1} and sigma={2}".format(round(A, 2), round(mu, 2), round(sigma, 2)))
    print ("Using the following ranges to fit the PMF:", ranges)
    res_list = []
    for fitting_range in ranges:
        try:
            r = _FitParabole(bincenters, fa, fitting_range)
        except:
            r = [0, 0]
        res_list.append(r)
    K_list = [2. * res[1] / lipid_area for res in res_list]
    DeltaK = npy.std([el for el in K_list if not el == 0.0])
    K = K_list[0]
    if outdir:
        file_utilities.WriteListOfListsInColumns(['bin', 'distribution', 'pmf'], [bincenters, pa[0], fa], os.path.join(
            outdir, '_'.join([filename_basis, 'splay', 'distribution']) + '.txt'), separator=' ')
        title = 'Splay distribution {0}'.format(title_complement)
        outfile = os.path.join(outdir, '_'.join(
            [filename_basis, 'splay', 'distribution']) + '.png')
        _PlotGaussian(bincenters, pa[0], A, mu, sigma,
                      outfile, title=title, xlabel='Splay')
        outfile = os.path.join(outdir, '_'.join(
            [filename_basis, 'splay', 'fit']) + '.png')
        a, b, x0 = res_list[0]
        title = 'Splay PMF {0}'.format(title_complement)
        _PlotParabola(bincenters, fa, a, b, x0, ranges[0], outfile, title, 'Splay', r'$-\log\left[P(\alpha)\right]$',
                      '$\chi^{{12}}={0}\pm {1}  k_BT$'.format(round(K, int(-math.log10(K)) + 1), round(DeltaK, int(-math.log10(DeltaK)) + 1)))
    return K, DeltaK, K_list