Beispiel #1
0
import mdtraj
import numpy as np
import sys

if len(sys.argv) == 1:
    print("Run as 'python lammpstrj2xtc.py *.lammpstrj'")
    sys.exit(0)
frames = mdtraj.load_lammpstrj(sys.argv[1], "system.gro", unit_set="real")

if frames.xyz[0].mean(axis=0).mean() < 1:
    frames.xyz = np.einsum('ijk, ikl->ijl', frames.xyz,
                           frames.unitcell_vectors) * 10  #ultra-fast
    #for index,line in enumerate(frames.unitcell_vectors):
    #frames.xyz[index] = np.matmul(frames.xyz[index],frames.unitcell_vectors[index])*10

frames.save_xtc("system.xtc")
Beispiel #2
0
import mdtraj as md
import numpy as np

t = md.load_lammpstrj('/scratch2/yli25/ch2oh.1g.lammpstrj',
                      top='project/ch2oh33.psf')

hbLen = 0.0
#i = 50
for i in range(len(t)):
    hbonds = md.baker_hubbard(t[i],
                              freq=0.0,
                              exclude_water=False,
                              periodic=True)

    hbondList = []
    for hbond in hbonds:
        if (t[i].topology.atom(hbond[0]).residue.name != t[i].topology.atom(
                hbond[2]).residue.name):
            hbondList.append([hbond[0], hbond[1], hbond[2]])

    #print(hbondList)
    hbLen += len(hbondList)

averageHB = hbLen / (float(len(t)))
print(averageHB)
def calc_number_lammps(lammpstrj,
                       top_file,
                       area,
                       dim,
                       box_range,
                       n_bins,
                       frame_range=None,
                       maxs=None,
                       mins=None):
    """
    Calculate a 1-dimensional number density profile for each residue

    Parameters
    ----------
    lammpstrj : str
        LAMMPS 'lammpstrj' dump file to load
    top_file : str
        LAMMPS-compatible top file to load
    area : int or float
        Area of box in dimensions where number density isn't calculated
    dim: int
        Dimension to calculate number density profile (0,1 or 2)
    box_range : array
        Range of coordinates in 'dim' to evaluate
    frame_range : Python range() (optional)
        Range of frames to calculate number density function over
    maxs : array (optional)
        Maximum coordinate to evaluate 
    mins : array (optional)
        Minimum coordinate to evalute
    
    Attributes
    ----------
    """
    trj = md.load_lammpstrj(lammpstrj, top=top_file)
    trj.unitcell_lengths *= 10
    trj.xyz[:, :, 0] *= trj.unitcell_lengths[0][0]
    trj.xyz[:, :, 1] *= trj.unitcell_lengths[0][1]
    trj.xyz[:, :, 2] *= trj.unitcell_lengths[0][2]
    com_trj = make_comtrj(trj)
    resnames = np.unique([x.name for x in com_trj.topology.residues])
    rho_list = list()

    for resname in resnames:
        sliced = com_trj.topology.select('resname {}'.format(resname))
        trj_slice = com_trj.atom_slice(sliced)
        if frame_range:
            trj_slice = trj_slice[frame_range]
        for frame in trj_slice:
            frame.xyz = frame.xyz * 10
            if maxs is None:
                indices = [[atom.index for atom in compound.atoms]
                           for compound in list(frame.topology.residues)]
            else:
                indices = np.intersect1d(
                    np.intersect1d(np.where(frame.xyz[-1, :, 0] > mins[0]),
                                   np.where(frame.xyz[-1, :, 0] < maxs[0])),
                    np.intersect1d(
                        np.where(frame.xyz[-1, :, 1] > box_range[0]),
                        np.where(frame.xyz[-1, :, 1] < box_range[1])))

            if frame_range:
                if frame.time == frame_range[0]:
                    x = np.histogram(frame.xyz[0, indices, dim].flatten(),
                                     bins=n_bins,
                                     range=(box_range[0], box_range[1]))
                    rho = x[0]
                    bins = x[1]
                else:
                    rho += np.histogram(frame.xyz[0, indices, dim].flatten(),
                                        bins=n_bins,
                                        range=(box_range[0], box_range[1]))[0]
            else:
                if frame.time == 0:
                    x = np.histogram(frame.xyz[0, indices, dim].flatten(),
                                     bins=n_bins,
                                     range=(box_range[0], box_range[1]))
                    rho = x[0]
                    bins = x[1]
                else:
                    rho += np.histogram(frame.xyz[0, indices, dim].flatten(),
                                        bins=n_bins,
                                        range=(box_range[0], box_range[1]))[0]
        rho = np.divide(rho, trj_slice.n_frames * area * 2 / n_bins)
        rho_list.append(rho)

    bin_list = bins[:-1]

    return (rho_list, bin_list)
def grid_analysis_routine():
    curr_path = os.getcwd().split('/')[-2:]
    grofile = glob.glob("Stage4_*.gro")[0]
    trajfile = "trajectory.lammps"
    traj = mdtraj.load_lammpstrj(trajfile, top=grofile)
    # In case we have some false frames stuck on the front, just look at the
    # last 26 frames because that should be a continuous trjaectory
    traj = traj[-26:]
    traj = _wrap_trj(traj)
    grotraj = mdtraj.load(grofile)
    tracers = np.loadtxt('tracers.out', dtype=int)

    # Water density profile over a region
    water_indices = traj.topology.select('water')
    headgroup_indices = _get_headgroup_indices(traj)
    water_p, bins = calc_density_profile(traj,
                                         traj.topology,
                                         water_indices,
                                         l_x=traj.unitcell_lengths[0, 0],
                                         l_y=traj.unitcell_lengths[0, 1])
    fig2 = plt.figure(2)
    plt.plot(bins, water_p[0])
    plt.savefig("water_profile.jpg", transparent=True)
    plt.close()

    # Identification of top and bottom interfaces
    rho_water = 984  # SPC water
    #hho_interface = rho_water / np.e # Definition of water interface
    rho_interface = 100
    #z_interface_bot, z_interface_top = _find_interface_water(water_p, bins, rho_interface=rho_interface)
    z_interface_bot, z_interface_top = _find_interface_lipid(
        traj, headgroup_indices)
    leaflet_interfaces = [z_interface_bot, z_interface_top]

    # Generate 2D histogram of density
    all_indices = [a.index for a in traj.topology.atoms if a.residue.is_water]
    thickness = 0.5
    grid_size = 1.0
    for i, z_interface in enumerate(leaflet_interfaces):
        if i == 0:
            name = "botwater"
        else:
            name = "topwater"

        # Given location of z_interface, find the atoms around it
        atoms_z = _find_atoms_around(traj,
                                     all_indices,
                                     thickness=thickness,
                                     z=z_interface)

        # Given that subset of atoms, then use the 2d histogram
        density_surface, xbin_centers, ybin_centers, xedges, yedges = calc_density_surface(
            traj, atoms_z, grid_size=grid_size, thickness=thickness)

        _surface_plot(
            _normalize(density_surface),
            xbin_centers,
            ybin_centers,
            num_ticks=5,
            title="Deviation from interfacial water density ({:.0f} kg/m$^3$)".
            format(rho_interface),
            filename='{}_waterdensity.jpg'.format(name))

    xbin_width = xbin_centers[1] - xbin_centers[0]
    ybin_width = ybin_centers[1] - ybin_centers[0]

    # Using the 2D hist bins, find the z-interface in each grid
    interface_bot_surface = np.zeros_like(density_surface)
    interface_top_surface = np.zeros_like(density_surface)

    for x, y in itertools.product(xbin_centers, ybin_centers):
        #atoms_xy = _find_atoms_within(traj, x=x, y=y, atom_indices=water_indices,
        atoms_xy = _find_atoms_within(traj,
                                      x=x,
                                      y=y,
                                      atom_indices=headgroup_indices,
                                      xbin_width=xbin_width,
                                      ybin_width=ybin_width)
        if len(atoms_xy) > 0:
            profile_xy, bins = calc_density_profile(traj,
                                                    traj.topology,
                                                    atoms_xy,
                                                    l_x=xbin_width,
                                                    l_y=ybin_width)

            fig = plt.figure(1)
            plt.plot(bins, np.mean(profile_xy, axis=0))
            plt.xlabel("Z (nm)", fontsize=20)
            plt.ylabel("Density (kg/m$^3$)", fontsize=20)
            plt.savefig('{:.2f}_{:.2f}_waterp.jpg'.format(float(x), float(y)))
            plt.close()

            #z_interface_bot, z_interface_top = _find_interface_water(profile_xy, bins,
            #        rho_interface=rho_interface)
            z_interface_bot, z_interface_top = _find_interface_lipid(
                traj, atoms_xy)
            if z_interface_bot is not None:
                interface_bot_surface[
                    0,
                    int(np.floor(x / xbin_width)),
                    int(np.floor(y / ybin_width))] = z_interface_bot
            else:
                interface_bot_surface[0,
                                      int(np.floor(x / xbin_width)),
                                      int(np.floor(y / ybin_width))] = -100

            if z_interface_top is not None:
                interface_top_surface[
                    0,
                    int(np.floor(x / xbin_width)),
                    int(np.floor(y / ybin_width))] = z_interface_top
            else:
                interface_top_surface[0,
                                      int(np.floor(x / xbin_width)),
                                      int(np.floor(y / ybin_width))] = 100

        else:
            print("No atoms found around ({}, {})".format(x, y))
            interface_bot_surface[0,
                                  int(np.floor(x / xbin_width)),
                                  int(np.floor(y / ybin_width))] = -100
            interface_top_surface[0,
                                  int(np.floor(x / xbin_width)),
                                  int(np.floor(y / ybin_width))] = 100

    fig = plt.figure(1)
    plt.hist(_normalize(interface_bot_surface[0],
                        mean=leaflet_interfaces[0]).flatten(),
             label="Bottom",
             alpha=0.4)
    plt.hist(_normalize(interface_top_surface[0],
                        mean=leaflet_interfaces[1]).flatten(),
             label="Top",
             alpha=0.4)
    plt.xlabel("Interface location (mean set to 0) (nm)", fontsize=20)
    plt.ylabel("Frequency", fontsize=20)
    plt.legend()
    plt.savefig("Zinterface_histogram.jpg", transparent=True)
    plt.close()

    _surface_plot(_normalize(interface_bot_surface,
                             reverse=True,
                             mean=leaflet_interfaces[0]),
                  xbin_centers,
                  ybin_centers,
                  num_ticks=5,
                  title="Deviation from interface location (nm)",
                  filename='interface_bot_surface.jpg')

    _surface_plot(_normalize(interface_top_surface,
                             mean=leaflet_interfaces[1]),
                  xbin_centers,
                  ybin_centers,
                  num_ticks=5,
                  title="Deviation from interface location (nm)",
                  filename='interface_top_surface.jpg')

    # Based on the xbins and ybins, figure out which tracers belong where
    all_tracer_outputs = []
    n_tracers = len(tracers)
    n_sims = _get_n_sims()

    for i, tracer in enumerate(tracers):
        # grid_output_dict will neatly contain all information and gets returned
        grid_output_dict = {}

        res = traj.topology.residue(tracer - 1)
        tracer_oxygen = res.atom(0)
        xyz = traj.xyz[0, tracer_oxygen.index, :]

        # Identify which xy region we're in
        bin_x = np.digitize(xyz[0], xedges) - 1
        bin_y = np.digitize(xyz[1], yedges) - 1

        # Identify the z interface for this xy region
        (interface_bot, interface_top) = (interface_bot_surface[0, bin_x,
                                                                bin_y],
                                          interface_top_surface[0, bin_x,
                                                                bin_y])

        # Find distance from local interface and leaflet interface
        if abs(interface_bot - xyz[2]) < abs(interface_top - xyz[2]):
            d_from_local_i = interface_bot - xyz[2]
            d_from_leaflet_i = leaflet_interfaces[0] - xyz[2]
            closest_interface = interface_bot
        else:
            d_from_local_i = xyz[2] - interface_top
            d_from_leaflet_i = xyz[2] - leaflet_interfaces[1]
            closest_interface = interface_top

        # Find forceout file, do anallysis
        dz = 2  # Angstroms
        kB = 1.987e-3  # kcal/mol k
        T = 305
        RT2 = (kB * T)**2

        sim_number = int(curr_path[-1].replace('Sim', ''))
        forceout_index = sim_number + (i * n_sims)

        meanforce_file = 'meanforce{}.dat'.format(forceout_index)
        meanforce = np.loadtxt(os.path.join('..', meanforce_file))
        dG = -meanforce * dz

        fcorr_file = 'fcorr{}.dat'.format(forceout_index)
        fcorr = np.loadtxt(os.path.join('..', fcorr_file))
        int_F, int_F_val, FACF = prm.integrate_acf_over_time(os.path.join(
            '..', fcorr_file),
                                                             timestep=1)

        diff_coeff = RT2 / int_F_val

        resist = np.exp(dG / (kB * T)) / diff_coeff

        P = 1 / (resist * dz * 1e-8)  # Convert z from \AA to cm

        grid_output_dict['leaflet_interfaces'] = leaflet_interfaces
        grid_output_dict['thickness'] = thickness
        grid_output_dict['grid_size'] = grid_size
        grid_output_dict['xbin_centers'] = xbin_centers
        grid_output_dict['ybin_centers'] = ybin_centers
        grid_output_dict['xedges'] = xedges
        grid_output_dict['yedges'] = yedges
        grid_output_dict['interface_bot'] = interface_bot_surface
        grid_output_dict['interface_top'] = interface_top_surface
        grid_output_dict['path'] = curr_path
        grid_output_dict['tracer_id'] = tracer
        grid_output_dict['tracer_xyz'] = xyz
        grid_output_dict['forceout_index'] = forceout_index
        grid_output_dict['local_interface'] = closest_interface
        grid_output_dict['d_from_local_i'] = d_from_local_i
        grid_output_dict['d_from_leaflet_i'] = d_from_leaflet_i
        grid_output_dict['meanforce'] = meanforce
        grid_output_dict['fcorr'] = fcorr
        grid_output_dict['free_energy'] = dG
        grid_output_dict['diffusion'] = diff_coeff
        grid_output_dict['permeability'] = P

        all_tracer_outputs.append(grid_output_dict)

        #all_G.append(dG)
        #all_D.append(diff_coeff)
        #all_P.append(P)

        #if interface_bot - 0.1 <= xyz[2] <= interface_bot + 0.1:
        #    sampled_interfaces.append(leaflet_interfaces[0] - interface_bot)
        #    analyze_tracer = True
        #elif interface_top - 0.1 <= xyz[2] <= interface_top + 0.1:
        #    sampled_interfaces.append(interface_top - leaflet_interfaces[1])
        #    analyze_tracer = True
        #else:
        #    analyze_tracer = False

        #if analyze_tracer:
        #    # Find forceout file
        #    n_tracers = len(tracers)
        #    n_sims = _get_n_sims()
        #    dz = 2 # Angstroms
        #    kB = 1.987e-3
        #    T = 305
        #    RT2 = (kB*T)**2

        #    sim_number = int(curr_path[-1].replace('Sim', ''))
        #    forceout_index = sim_number + (i * n_sims)

        #    meanforce_file = '../meanforce{}.dat'.format(forceout_index)
        #    meanforce = np.loadtxt(meanforce_file)
        #    dG = meanforce * dz

        #    fcorr_file = '../fcorr{}.dat'.format(forceout_index)
        #    int_F, int_F_val, FACF = prm.integrate_acf_over_time(fcorr_file,
        #            timestep=1)

        #    diff_coeff = RT2 / int_F_val

        #    resist = np.exp(dG/ (kB * T)) / diff_coeff

        #    P = 1 / (resist * dz * 1e-8) # Convert z from \AA to cm

        #    all_G.append(dG)
        #    all_D.append(diff_coeff)
        #    all_P.append(P)

    return all_tracer_outputs