def test_search(universe):
    """simply check that for a centered protein in a large box pkdtree
    and kdtree return the same result"""
    ns = NeighborSearch.AtomNeighborSearch(universe.atoms)
    pns = NeighborSearch.AtomNeighborSearch(universe.atoms,
                                            universe.atoms.dimensions)

    ns_res = ns.search(universe.atoms[20], 20)
    pns_res = pns.search(universe.atoms[20], 20)
    assert_equal(ns_res, pns_res)
Exemple #2
0
 def notwithin_coordinates(cutoff=cutoff):
     # acts as '<solvent> WITHIN <cutoff> OF <protein>'
     # must update every time step
     ns_w = NS.AtomNeighborSearch(
         solvent)  # build kd-tree on solvent (N_w > N_protein)
     group = ns_w.search(protein,
                         cutoff)  # solvent within CUTOFF of protein
     return group.positions
Exemple #3
0
 def notwithin_coordinates(cutoff=cutoff):
     # must update every time step
     ns_w = NS.AtomNeighborSearch(solvent)  # build kd-tree on solvent (N_w > N_protein)
     solvation_shell = ns_w.search(protein, cutoff)  # solvent within CUTOFF of protein
     # Find indices in solvent NOT in solvation shell
     uniq_idx = np.setdiff1d(solvent.ix, solvation_shell.ix)
     # Then reselect these from Universe.atoms (as these indices are global)
     group = universe.atoms[uniq_idx]
     return group.positions
Exemple #4
0
 def notwithin_coordinates(cutoff=cutoff):
     # must update every time step
     ns_w = NS.AtomNeighborSearch(
         solvent)  # build kd-tree on solvent (N_w > N_protein)
     solvation_shell = ns_w.search_list(
         protein, cutoff)  # solvent within CUTOFF of protein
     group = MDAnalysis.core.AtomGroup.AtomGroup(
         set_solvent - set(solvation_shell))  # bulk
     return group.coordinates()
Exemple #5
0
    def find_clashing_rotamers(self, fitted_rotamers, protein, site_resid):
        """Detect any rotamer that clashes with the protein."""
        # make a KD tree of the protein neighbouring atoms
        proteinNotSite = protein.select_atoms("protein and not name H* and not (resid " + str(site_resid) +
                                             " or (resid " + str(site_resid-1) + " and (name C or name O)) "
                                                                                 "or (resid " + str(site_resid+1)
                                             + " and name N))")
        proteinNotSiteLookup = KDNS.AtomNeighborSearch(proteinNotSite)

        rotamerSel = fitted_rotamers.select_atoms("not name H*")

        rotamer_clash = []
        for rotamer in fitted_rotamers.trajectory:
            bumps = proteinNotSiteLookup.search(rotamerSel, self.clashDistance)
            rotamer_clash.append(bool(bumps))
        return rotamer_clash, np.sum(rotamer_clash)
Exemple #6
0
def analysis(lUniverses, searchAtoms, radius, fileName):
    #Inputs:
    #       a list of MD Analysis Universes,
    #       a search string (in MD Analysis atom selection grammar)
    #       Radius (in Angstroms) around the search atoms in which to search
    #       string to call the analysis result file
    #Outputs:
    #       a pickle file containing a list of dictionaries with the results in the following format:
    #       [{information about a residue that is whithin the search radius}, {}, {}, {}]

    resList = []
    for u in lUniverses:
        SearchClass = NSearch.AtomNeighborSearch(u.atoms)
        nr = []
        for ts in u.trajectory:
            nr.append(
                SearchClass.search(u.select_atoms(searchAtoms), radius, 'R'))
        nr = np.array(nr)
        for a in range(len(nr)):
            for i in range(len(nr[a])):
                resList.append(nr[a][i])
    resList = np.array(resList)
    unRes = np.unique(resList, return_counts=True)
    unRes = np.array(unRes)
    unRes = unRes.T
    unRes = unRes[np.argsort(unRes[:,
                                   1])]  #sorts into ascending order of counts
    unRes = np.flip(unRes, axis=0)  # flips to descending order of counts
    lResidues = []
    for i in range(1, len(unRes)):
        dRes = {}
        dRes['segid'] = unRes[i][0].segid
        dRes['resname'] = unRes[i][0].resname
        dRes['resid'] = unRes[i][0].resid
        dRes['count'] = unRes[i][1]
        dRes['countPercentage'] = unRes[i][1] / len(lUniverses[0].trajectory)
        dRes['full_string'] = str(dRes['segid']) + " " + str(
            dRes['resname']) + " " + str(dRes['resid'])
        lResidues.append(dRes)
    save(lResidues, fileName)
Exemple #7
0
def find_clashing_rotamers(clash_distance, rotamer_site, protein, resid,
                           chainid):
    """
    Detect any rotamer that clashes with the protein.

    Parameters
    ----------
    clash_distance: float
        distance between atoms of the spin-spin label and the protein
    rotamer_site:
	trajectory of rotamer states attached to spin-label position
    protein: pdb
    resid: int
        spin-labeled position
    chainid: string
        chain of the spin-labeled position

    Returns
    -------
    rotamer_clash: array
        For each spin-label position, an array with boolean entries
        of each rotameric state (True: clash, False: no clash)
    """

    # make a KD tree of the protein neighbouring atoms
    proteinNotSite = protein.select_atoms(
        "protein and not name H* and not (resid " + str(resid) +
        " or (resid " + str(resid - 1) + " and (name C or name O)) "
        "or (resid " + str(resid + 1) + " and name N))")
    proteinNotSiteLookup = KDNS.AtomNeighborSearch(proteinNotSite)

    rotamerSel = rotamer_site.select_atoms("not name H*")

    rotamer_clash = []
    for rotamer in rotamer_site.trajectory:
        bumps = proteinNotSiteLookup.search(rotamerSel, clash_distance)
        rotamer_clash.append(bool(bumps))
    return rotamer_clash
def analyze_clusters(frame):
    u = MDAnalysis.Universe(gro,xtc)
    print('Frame %i of %i......'%(frame,u.trajectory.n_frames))
    start = time.time()
    u.trajectory[frame]

    ######## Discriminate between leaflets in this frame to construct leaflet-separated groups
    #Lipid Residue names
    lipid1 ='DPPC'
    lipid2 ='DIPC'
    lipid3 ='CHOL'

    lpd1_atoms = u.select_atoms('resname %s and name PO4'%lipid1)
    lpd2_atoms = u.select_atoms('resname %s and name PO4'%lipid2)
    lpd3_atoms = u.select_atoms('resname %s and name ROH'%lipid3)
    num_lpd1 = lpd1_atoms.n_atoms
    num_lpd2 = lpd2_atoms.n_atoms
    # atoms in the upper leaflet as defined by insane.py or the CHARMM-GUI membrane builders
    # select cholesterol headgroups within 1.5 nm of lipid headgroups in the selected leaflet
    lpd1i_up = lpd1_atoms[:((num_lpd1)/2)]
    lpd2i_up = lpd2_atoms[:((num_lpd2)/2)]
    lipids_up = lpd1i_up + lpd2i_up
    ns_lipids_up = NS.AtomNeighborSearch(lpd3_atoms)
    lpd3i_up = ns_lipids_up.search(lipids_up,15.0)
    lpd3i_up_resids = np.unique(lpd3i_up.resids)

    lpd1i_down = lpd1_atoms[((num_lpd1)/2):]
    lpd2i_down = lpd2_atoms[((num_lpd2)/2):]
    lipids_down = lpd1i_down + lpd2i_down
    ns_lipids_down = NS.AtomNeighborSearch(lpd3_atoms)
    lpd3i_down = ns_lipids_down.search(lipids_down,15.0)
    lpd3i_down_resids = np.unique(lpd3i_down.resids)

    # ID center of geometry coordinates for cholesterol on indicated bilayer side
    lpd3_up_coords = np.zeros((len(lpd3i_up.resnums),3))
    for i in np.arange(len(lpd3i_up.resnums)):
        resnum = lpd3i_up.resnums[i]
        group = u.select_atoms('resnum %i and (name R1 or name R2 or name R3 or name R4 or name R5)'%resnum)
        group_cog = group.center_of_geometry()
        lpd3_up_coords[i] = group_cog

    lpd3_down_coords = np.zeros((len(lpd3i_down.resnums),3))
    for i in np.arange(len(lpd3i_down.resnums)):
        resnum = lpd3i_down.resnums[i]
        group = u.select_atoms('resnum %i and (name R1 or name R2 or name R3 or name R4 or name R5)'%resnum)
        group_cog = group.center_of_geometry()
        lpd3_down_coords[i] = group_cog

    # ID coordinates for lipids on indicated bilayer side, renaming variables
    lpd1_atoms = u.select_atoms('resname %s and (name C2A or name C2B)'%lipid1)
    lpd2_atoms = u.select_atoms('resname %s and (name D2A or name D2B)'%lipid2)
    num_lpd1 = lpd1_atoms.n_atoms
    num_lpd2 = lpd2_atoms.n_atoms

    # select lipid tail atoms beloging to the selected bilayer side
    lpd1i_up = lpd1_atoms[:((num_lpd1)/2)]
    lpd2i_up = lpd2_atoms[:((num_lpd2)/2)]

    lpd1i_down = lpd1_atoms[((num_lpd1)/2):]
    lpd2i_down = lpd2_atoms[((num_lpd2)/2):]

    # assign lpd1 coordinates, completing the assignment of all coordinates from which psi6 will be computed
    lpd1_up_coords = lpd1i_up.coordinates()
    lpd2_up_coords = lpd2i_up.coordinates()

    lpd1_down_coords = lpd1i_down.coordinates()
    lpd2_down_coords = lpd2i_down.coordinates()

    lpd_up_coords = np.vstack((lpd1_up_coords,lpd3_up_coords))
    lpd_up_coords = lpd_up_coords.astype('float32')

    lpd_up_full_coords = np.vstack((lpd1_up_coords,lpd3_up_coords,lpd2_up_coords)) # un-evaluated indices will go last
    lpd_up_full_coords = lpd_up_full_coords.astype('float32')

    lpd_down_coords = np.vstack((lpd1_down_coords,lpd3_down_coords))
    lpd_down_coords = lpd_down_coords.astype('float32')

    lpd_down_full_coords = np.vstack((lpd1_down_coords,lpd3_down_coords,lpd2_down_coords)) # un-evaluated indices will go last
    lpd_down_full_coords = lpd_down_full_coords.astype('float32')

    # Select tail atoms for M inter-leaflet cluster analysis
    lpd1_tail_atoms = u.select_atoms('resname %s and (name C4A or name C4B)'%lipid1)
    num_tail_lpd1 = lpd1_tail_atoms.n_atoms
    lpd1i_tail_up = lpd1_tail_atoms[:((num_tail_lpd1)/2)]
    lpd1i_tail_down = lpd1_tail_atoms[((num_tail_lpd1)/2):]

    lpd3i_tail_up = []
    for resid in lpd3i_up_resids:
        sel = u.select_atoms('resid %i and name C2'%resid)
        lpd3i_tail_up.append(sel)
    lpd3i_tail_up = np.sum(lpd3i_tail_up)

    lpd3i_tail_down = []
    for resid in lpd3i_down_resids:
        sel = u.select_atoms('resid %i and name C2'%resid)
        lpd3i_tail_down.append(sel)
    lpd3i_tail_down = np.sum(lpd3i_tail_down)

    tail_up = lpd1i_tail_up + lpd3i_tail_up
    tail_down = lpd1i_tail_down + lpd3i_tail_down
    ########################################################################################  

    resids_up = np.concatenate([lpd1i_up.resids, np.unique(lpd3i_up.resids)])
    resids_down = np.concatenate([lpd1i_down.resids, np.unique(lpd3i_down.resids)])

    c = u.trajectory.ts.triclinic_dimensions[0][0] # box length
    box = u.trajectory.ts.triclinic_dimensions

    up_cluster_groups   = extract_clusters(lpd_up_coords, c, resids_up)
    down_cluster_groups = extract_clusters(lpd_down_coords, c, resids_down)

    N_absPsi6s = []
    N_p2s = []
    N_c_sizes = []
    #Compute structural parameters for n -- intra-leaflet cluster parameters on both sides
    haystack = lpd1i_up.indices
    for group in up_cluster_groups:
        if group.select_atoms('resname DPPC').n_residues != 0: # only evaluate DPPC-containing clusters
            # do structural analysis and save the cluster size
            p2 = compute_p2s(group) # mean P2 for the cluster
            c_size = group.select_atoms('name C4A or name C4B or name C2').n_atoms # the number of carbon chains in the cluster
            N_p2s.append(p2)
            N_c_sizes.append(c_size)
            # do psi6 ONLY for dppc molecules
            packed = group.select_atoms('resname %s and (name C2A or name C2B)'%lipid1)
            needles = packed.indices
            st = set(needles)
            atomindices = [i for i, e in enumerate(haystack) if e in st]
            absPsi6 = np.mean(np.abs(get_psi6(lpd_up_full_coords, atomindices, box)))
            N_absPsi6s.append(absPsi6)

    print('finished upper leaflet intra-leaflet clustering, n!')
    haystack = lpd1i_down.indices
    for group in down_cluster_groups:
        if group.select_atoms('resname DPPC').n_residues != 0: # only evaluate DPPC-containing clusters
            # do structural analysis and save the cluster size
            p2 = compute_p2s(group) # mean P2 for the cluster
            c_size = group.select_atoms('name C4A or name C4B or name C2').n_atoms # the number of carbon chains in the cluster
            N_p2s.append(p2)
            N_c_sizes.append(c_size)
            # do psi6 ONLY for dppc molecules
            packed = group.select_atoms('resname %s and (name C2A or name C2B)'%lipid1)
            needles = packed.indices
            st = set(needles)
            atomindices = [i for i, e in enumerate(haystack) if e in st]
            absPsi6 = np.mean(np.abs(get_psi6(lpd_down_full_coords, atomindices, box)))
            N_absPsi6s.append(absPsi6)

    print('finished lower leaflet intra-leaflet clustering, n!')
    M_absPsi6s = []
    M_p2s = []
    M_c_sizes = []
    # Compute structural parameters for m -- inter-leaflet cluster parameters on both sides
    haystack = lpd1i_down.indices
    for group in up_cluster_groups:
        if group.select_atoms('resname DPPC').n_residues != 0: # only evaluate DPPC-containing clusters
            # find the residue ids of the inter-leaflet DPPC molecules within M_cutoff ang of molecules in the cluster            
            ns_inter_lipids = NS.AtomNeighborSearch(lpd1i_tail_down) # using inter-leaflet DPPC molecules....
            inter_lipids = ns_inter_lipids.search(group, M_cutoff) # find inter-leaflet DPPC molecules
            # find the residue ids of the inter-leaflet DPPC and CHOL molecules within M_cutoff ang of molecules in the cluster            
            ns_inter_atoms = NS.AtomNeighborSearch(tail_down) # using inter-leaflet DPPC and CHOL molecules....
            inter_atoms = ns_inter_atoms.search(group, M_cutoff) # find inter-leaflet DPPC and CHOL molecules
            if inter_atoms != []:
                c_size = inter_atoms.n_atoms
                M_c_sizes.append(c_size)
            else:
                M_c_sizes.append(0)

            if inter_lipids != []:
                inter_lipids_resids = np.unique(inter_lipids.resids)
                inter_lipid_groups = []
                for resid in inter_lipids_resids:
                    sel = u.select_atoms('resid %i'%resid)
                    inter_lipid_groups.append(sel)
                inter_group = np.sum(inter_lipid_groups)
                p2 = compute_p2s(inter_group)
                M_p2s.append(p2)
                # do psi6 ONLY for dppc molecules
                packed = inter_group.select_atoms('resname %s and (name C2A or name C2B)'%lipid1)
                needles = packed.indices
                st = set(needles)
                atomindices = [i for i, e in enumerate(haystack) if e in st]
                absPsi6 = np.mean(np.abs(get_psi6(lpd_down_full_coords, atomindices, box)))
                M_absPsi6s.append(absPsi6)
            else:
                M_p2s.append(0)
                M_absPsi6s.append(0)

    print('finished upper leaflet inter-leaflet clustering, m!')
    haystack = lpd1i_up.indices
    for group in down_cluster_groups:
        if group.select_atoms('resname DPPC').n_residues != 0: # only evaluate DPPC-containing clusters
            # find the residue ids of the inter-leaflet DPPC molecules within 10 ang of molecules in the cluster            
            ns_inter_lipids = NS.AtomNeighborSearch(lpd1i_tail_up) # using inter-leaflet DPPC molecules....
            inter_lipids = ns_inter_lipids.search(group, M_cutoff) # find inter-leaflet DPPC molecules within 10 ang of the cluster
            # find the residue ids of the inter-leaflet DPPC and CHOL molecules within M_cutoff ang of molecules in the cluster            
            ns_inter_atoms = NS.AtomNeighborSearch(tail_up) # using inter-leaflet DPPC and CHOL molecules....
            inter_atoms = ns_inter_atoms.search(group, M_cutoff) # find inter-leaflet DPPC and CHOL molecules
            if inter_atoms != []:
                c_size = inter_atoms.n_atoms
                M_c_sizes.append(c_size)
            else:
                M_c_sizes.append(0)

            if inter_lipids != []:
                inter_lipids_resids = np.unique(inter_lipids.resids)
                inter_lipid_groups = []
                for resid in inter_lipids_resids:
                    sel = u.select_atoms('resid %i'%resid)
                    inter_lipid_groups.append(sel)
                inter_group = np.sum(inter_lipid_groups)
                p2 = compute_p2s(inter_group)
                M_p2s.append(p2)
                # do psi6 ONLY for dppc molecules
                packed = inter_group.select_atoms('resname %s and (name C2A or name C2B)'%lipid1)
                needles = packed.indices
                st = set(needles)
                atomindices = [i for i, e in enumerate(haystack) if e in st]
                absPsi6 = np.mean(np.abs(get_psi6(lpd_up_full_coords, atomindices, box)))
                M_absPsi6s.append(absPsi6)
            else:
                M_p2s.append(0)
                M_absPsi6s.append(0)

    print('finished lower leaflet inter-leaflet clustering, m!')
    N_c_sizes  = np.array(N_c_sizes)
    N_p2s      = np.array(N_p2s)
    N_absPsi6s = np.array(N_absPsi6s)
    M_c_sizes  = np.array(M_c_sizes)
    M_p2s      = np.array(M_p2s)
    M_absPsi6s = np.array(M_absPsi6s)

    #results.append( [N_c_sizes, M_c_sizes, N_p2s, M_p2s, N_absPsi6s, M_absPsi6s] )
    print('This whole process ran for', time.time()-start)
    return N_c_sizes, M_c_sizes, N_p2s, M_p2s, N_absPsi6s, M_absPsi6s
    def cluster_analysis(self,
                         cut_off=7.5,
                         times=None,
                         style="atom",
                         measure="b2b",
                         algorithm="dynamic",
                         work_in="Residue",
                         traj_pbc_style=None,
                         pbc=True):
        """High level function clustering molecules together

        Example
        -------
        No Example yet

        Note
        ----
        Not all of the functionality is actually coded yet

        Parameters
        ----------
        cut_off : float, optional
            Minimal distance for two particles to be in the
            same cluster, in Angstroem. Results still depend
            on the measure parameter.
        time : list of floats, optional
            If None, do for whole trajectory. If an interval
            is given like this (t_start, t_end) only do from start
            to end.
        style : string, optional
            "atom" or "molecule". Dependent on this, the
            cluster_objects attribute is interpreted as molecule
            or atoms within a molecule.
        measure : string, optional
            "b2b (bead to bead), COM or COG(center of geometry)
        algorithm : string, optional
            "dynamic" or "static". The static one is slower. I
            loops over all atoms and then merges cluster, whereas
            the dynamic algorithm grows clusters dynamically.
        work_in : string, optional
            "Residue" or "Atom". Either work in (and output)
            ResidueGroups or AtomGroups.
            The former may be faster for systems in which all
            parts of the same molecule are always in the same cluster,
            whereas the latter is useful for systems in which different
            parts of the same molecule can be in different clusters
            (i.e. block copolymers).
        traj_pbc_style : string, optional
            Gromacs pbc definitions: mol or atom, by default
            None
        pbc : bool, optional
            Whether to consider periodic boundary conditions in the 
            neighbour search (for determining whether atoms belong to
            the same cluster), by default True. Note that if work_in is 
            set to "Residue" periodic boundary conditions are taken into
            account implicitly for atoms in molecules passing across the 
            boundaries.

        Raises
        ------
        NotImplementedError
            If an unspecified algorithm or work_in is choosen
        ValueError
            If pbc is not boolean
        
        ToDo
        ----
        -Make static and dynamic algorithms store the same arguments
        -Add plotting capabilities
        -Add capabilities to only look at certain time windows
        -Get rid of traj and coord attributes
        """

        self._set_pbc_style(traj_pbc_style)

        self.universe = self._get_universe(self._coord, traj=self._traj)

        self.style = style

        self.aggregate_species = self._select_species(self.universe,
                                                      style=self.style)

        self.cluster_list = []

        # Initialise the neighboursearch object

        if pbc == True:
            self.neighbour_search = NeighborSearch.AtomNeighborSearch(
                self.aggregate_species,
                box=self.universe.dimensions,
                bucket_size=10)
        elif pbc == False:
            if work_in == "Residue" and traj_pbc_style != "mol":
                warnings.warn('work_in = "Residue" implicitly enforces pbc '\
                              'for atoms in the same molecule if pbc_style '\
                              '= "atom"', UserWarning)
                print('Warning')
            self.neighbour_search = NeighborSearch.AtomNeighborSearch(
                self.aggregate_species, box=None, bucket_size=10)
        else:
            raise ValueError("pbc has to be boolean")

        if work_in == "Residue":
            self.search_level = "R"
        elif work_in == "Atom":
            self.search_level = "A"
        else:
            raise NotImplementedError(
                "{:s} is unspecified work_in variable".format(work_in))

        if algorithm == "static":
            cluster_algorithm = self._get_cluster_list_static
        elif algorithm == "dynamic":
            cluster_algorithm = self._get_cluster_list_dynamic
        else:
            raise NotImplementedError(
                "{:s} is unspecified algorithm".format(algorithm))
        # Loop over all trajectory times
        for time in self.universe.trajectory:
            if times is not None:
                if time.time > max(times) or time.time < min(times):
                    continue
            self.cluster_list.append(cluster_algorithm())
            print("****TIME: {:8.2f}".format(time.time))
            print("---->Number of clusters {:d}".format(
                len(self.cluster_list[-1])))

        # Rewind Trajectory to beginning for other analysis
        self.universe.trajectory.rewind()
def voronoi_tessel(ts):
    # set the time step
    print 'Frame %i in %i'%(ts, end_f)
    u = MDAnalysis.Universe(top,traj)
    u.trajectory[0]

    # Select atoms within this particular frame
    lpd1_atoms = u.select_atoms(sel1)
    lpd2_atoms = u.select_atoms(sel2)
    lpd3_atoms = u.select_atoms(sel3)

    zmean = np.mean(np.concatenate([lpd1_atoms.positions[:,2], lpd2_atoms.positions[:,2], lpd3_atoms.positions[:,2]]))

    num_lpd1 = lpd1_atoms.n_atoms
    num_lpd2 = lpd2_atoms.n_atoms

    # select atoms in the upper leaflet using positions in the first frame  of simulation
    # select cholesterol headgroups within 1.5 nm of lipid headgroups in the selected leaflet in the current frame
    if side == 'up':
        lpd1i=[]
        zpos = lpd1_atoms.positions[:,2]
        for i in range(num_lpd1):
            if zpos[i] > zmean:
                lpd1i.append(lpd1_atoms[i])
        lpd1i = np.sum(np.array(lpd1i))

        lpd2i=[]
        zpos = lpd2_atoms.positions[:,2]
        for i in range(num_lpd2):
            if zpos[i] > zmean:
                lpd2i.append(lpd2_atoms[i])
        lpd2i = np.sum(np.array(lpd2i))

        lipids = lpd1i + lpd2i
        u.trajectory[ts] # now move to the frame currently being analyzed
        ns_lipids = NS.AtomNeighborSearch(lpd3_atoms)

        lpd3i = ns_lipids.search(lipids,15.0)
    elif side == 'down':
        lpd1i=[]
        zpos = lpd1_atoms.positions[:,2]
        for i in range(num_lpd1):
            if zpos[i] < zmean:
                lpd1i.append(lpd1_atoms[i])
        lpd1i = np.sum(np.array(lpd1i))

        lpd2i=[]
        zpos = lpd2_atoms.positions[:,2]
        for i in range(num_lpd2):
            if zpos[i] < zmean:
                lpd2i.append(lpd2_atoms[i])
        lpd2i = np.sum(np.array(lpd2i))


        lipids = lpd1i + lpd2i

        u.trajectory[ts] # now move to the frame currently being analyzed
        ns_lipids = NS.AtomNeighborSearch(lpd3_atoms)

        lpd3i = ns_lipids.search(lipids,15.0)
    lpd_atms = lpd1i + lpd2i + lpd3i


#Extracting the coordinates
    Pxyz = lpd_atms.positions
    Pxy = []
    for l in range(0,len(Pxyz)) :
        Pxy.append([Pxyz[l][0],Pxyz[l][1]])
#Extracting xy coordinates and residue names

    atm_list = []
    for a in range(0, len(Pxyz)):
        atm_list.append([Pxyz[a][0],Pxyz[a][1],lpd_atms[a].resname])

#Introducing PBC
    x_box = u.dimensions[0]
    y_box = u.dimensions[1]

    xplus   = []
    xminus  = []
    xyplus  = []
    xyminus = []

    for atm in range(0 ,len(atm_list)):
        xplus.append([atm_list[atm][0]+x_box,atm_list[atm][1],atm_list[atm][2]])
        xminus.append([atm_list[atm][0]-x_box,atm_list[atm][1],atm_list[atm][2]])

    atm_list_px = atm_list + xplus + xminus

    for atm in range(0 ,len(atm_list_px)):
        xyplus.append([atm_list_px[atm][0],atm_list_px[atm][1]+y_box,atm_list_px[atm][2]])
        xyminus.append([atm_list_px[atm][0],atm_list_px[atm][1]-y_box,atm_list_px[atm][2]])

    atm_list_p = atm_list_px + xyplus + xyminus


    atm_xy = []
    for i in range(0,len(atm_list_p)) :
        atm_xy.append([atm_list_p[i][0],atm_list_p[i][1]])


    vor = Voronoi(atm_xy)
    vor_s = Voronoi(Pxy)
    vertices       = vor.vertices

    ridge_points = vor.ridge_points
    Lpd1_Lpd1_I = 0
    Lpd2_Lpd2_I = 0
    Lpd3_Lpd3_I = 0
    Lpd1_Lpd2_I = 0
    Lpd1_Lpd3_I = 0
    Lpd2_Lpd3_I = 0

    Lpd1_Lpd1_E = 0
    Lpd2_Lpd2_E = 0
    Lpd3_Lpd3_E = 0
    Lpd1_Lpd2_E = 0
    Lpd1_Lpd3_E = 0
    Lpd2_Lpd3_E = 0

    r_length  = len(ridge_points)


  
    for k in range (0,r_length) :
        ridge_k = ridge_points[k]
        Li = atm_list_p[int(ridge_k[0])]
        Lj = atm_list_p[int(ridge_k[1])]

#Lipids INSIDE the box 

        if 0 < Li[0] < x_box and 0 < Li[1] < y_box and 0 < Lj[0] < x_box and 0 < Lj[1] < y_box :
        
            if Li[2] == lipid1 and Lj[2] == lipid1:
                Lpd1_Lpd1_I = Lpd1_Lpd1_I + 1
                
            if Li[2] == lipid2 and Lj[2] == lipid2:
                Lpd2_Lpd2_I = Lpd2_Lpd2_I + 1
                
            if Li[2] == lipid3 and Lj[2] == lipid3:
                Lpd3_Lpd3_I = Lpd3_Lpd3_I + 1
                
            if Li[2] == lipid1 and Lj[2] == lipid2:
                Lpd1_Lpd2_I  = Lpd1_Lpd2_I + 1
                
            if Li[2] == lipid2 and Lj[2] == lipid1:
                Lpd1_Lpd2_I = Lpd1_Lpd2_I + 1

            if Li[2] == lipid1 and Lj[2] == lipid3:
                Lpd1_Lpd3_I  = Lpd1_Lpd3_I + 1
                
            if Li[2] == lipid3 and Lj[2] == lipid1:
                Lpd1_Lpd3_I = Lpd1_Lpd3_I + 1

            if Li[2] == lipid2 and Lj[2] == lipid3:
                Lpd2_Lpd3_I  = Lpd2_Lpd3_I + 1
                
            if Li[2] == lipid3 and Lj[2] == lipid2:
                Lpd2_Lpd3_I = Lpd2_Lpd3_I + 1                
                
#Lipids at the EDGE of the box                
                
        if 0 <= Li[0] < x_box and 0 <= Li[1] < y_box or 0 <= Lj[0] < x_box and 0 <= Lj[1] < y_box :

            if Li[2] == lipid1 and Lj[2] == lipid1:
                Lpd1_Lpd1_E = Lpd1_Lpd1_E + 1
                
            if Li[2] == lipid2 and Lj[2] == lipid2:
                Lpd2_Lpd2_E = Lpd2_Lpd2_E + 1
                
            if Li[2] == lipid3 and Lj[2] == lipid3:
                Lpd3_Lpd3_E = Lpd3_Lpd3_E + 1
                
            if Li[2] == lipid1 and Lj[2] == lipid2:
                Lpd1_Lpd2_E  = Lpd1_Lpd2_E + 1
                
            if Li[2] == lipid2 and Lj[2] == lipid1:
                Lpd1_Lpd2_E = Lpd1_Lpd2_E + 1

            if Li[2] == lipid1 and Lj[2] == lipid3:
                Lpd1_Lpd3_E  = Lpd1_Lpd3_E + 1
                
            if Li[2] == lipid3 and Lj[2] == lipid1:
                Lpd1_Lpd3_E = Lpd1_Lpd3_E + 1

            if Li[2] == lipid2 and Lj[2] == lipid3:
                Lpd2_Lpd3_E  = Lpd2_Lpd3_E + 1
                
            if Li[2] == lipid3 and Lj[2] == lipid2:
                Lpd2_Lpd3_E = Lpd2_Lpd3_E + 1    

                
#Total = LipidsInside + (Lipids including EDGES - Lipids Inside)/2 -----> Correction for over counting the lipids in periodic images
    Lpd1_Lpd1 = Lpd1_Lpd1_I + (Lpd1_Lpd1_E - Lpd1_Lpd1_I)/2
    Lpd2_Lpd2 = Lpd2_Lpd2_I + (Lpd2_Lpd2_E - Lpd2_Lpd2_I)/2
    Lpd3_Lpd3 = Lpd3_Lpd3_I + (Lpd3_Lpd3_E - Lpd3_Lpd3_I)/2
    Lpd1_Lpd2 = Lpd1_Lpd2_I + (Lpd1_Lpd2_E - Lpd1_Lpd2_I)/2
    Lpd1_Lpd3 = Lpd1_Lpd3_I + (Lpd1_Lpd3_E - Lpd1_Lpd3_I)/2
    Lpd2_Lpd3 = Lpd2_Lpd3_I + (Lpd2_Lpd3_E - Lpd2_Lpd3_I)/2

    sum_bonds = Lpd1_Lpd1 + Lpd2_Lpd2 + Lpd3_Lpd3 + Lpd1_Lpd2 + Lpd1_Lpd3 + Lpd2_Lpd3

#Considering only Similar Lipid (SL) and Dissimilar Lipid (DL) Bonds
    SL = Lpd1_Lpd1 + Lpd2_Lpd2 + Lpd3_Lpd3
    DL = Lpd1_Lpd2 + Lpd1_Lpd3 + Lpd2_Lpd3

    #Calculating Fractions    
    X_SL = float(SL)/float(sum_bonds)  #Similar Lipid
    X_DL  = float(DL)/float(sum_bonds) #Dissimilar Lipid

#Mixing Entropy
    mix_entropy = -(X_SL * np.log2(X_SL)) +( X_DL * np.log2(X_DL))

#Calculating Averages
    sum_bonds = Lpd1_Lpd1 + Lpd1_Lpd2 + Lpd2_Lpd2
    avg_bonds = float(sum_bonds)/float(len(atm_list))

    return Lpd1_Lpd1, Lpd2_Lpd2, Lpd3_Lpd3, Lpd1_Lpd2, Lpd1_Lpd3, Lpd2_Lpd3, sum_bonds, avg_bonds, mix_entropy
Exemple #11
0
def extract_data(frame):
    #for frame in frames:
    u = MDAnalysis.Universe(psf, trr)
    print 'Frame %i of %i......' % (frame, u.trajectory.n_frames)
    u.trajectory[frame]
    XBOX = u.dimensions[0]
    YBOX = u.dimensions[1]
    ##################################################################################
    ### Determining side of the bilayer CHOL belongs to in this frame
    #Lipid Residue names
    lpd1_atoms = u.select_atoms(sel1)
    lpd2_atoms = u.select_atoms(sel2)
    lpd3_atoms = u.select_atoms(sel3)
    #Protein atom selction
    prot_atoms = u.select_atoms(selprot)

    num_lpd1 = lpd1_atoms.n_atoms
    num_lpd2 = lpd2_atoms.n_atoms
    num_lpd3 = lpd3_atoms.n_atoms
    # atoms in the upper leaflet as defined by insane.py or the MARTINI-GUI membrane builders
    # select cholesterol headgroups within 1.5 nm of lipid headgroups in the selected leaflet
    if side == 'up':
        lpd1i = lpd1_atoms[:int((num_lpd1) / 2)]
        lpd2i = lpd2_atoms[:int((num_lpd2) / 2)]
        phospholipids = lpd1i + lpd2i
        ns_lipids = NS.AtomNeighborSearch(lpd3_atoms)  #CHOL
        lpd3i = ns_lipids.search(phospholipids, 15.0)
        #proti = prot_atoms.select_atoms(protsel_up)

    elif side == 'down':
        lpd1i = lpd1_atoms[int((num_lpd1) / 2):]
        lpd2i = lpd2_atoms[int((num_lpd2) / 2):]
        phospholipids = lpd1i + lpd2i
        ns_lipids = NS.AtomNeighborSearch(lpd3_atoms)  #CHOL
        lpd3i = ns_lipids.search(phospholipids, 15.0)
        #proti = prot_atoms.select_atoms(protsel_down)

    #Using tail definitions
    groups1 = []
    for i in np.arange(len(lpd1i.resnums)):
        resnum = lpd1i.resnums[i]
        group = u.select_atoms(
            'resname DPPC and resnum %i and (name C2A or name C2B)' % resnum)
        groups1.append(group)
    lpd1i = np.sum(groups1)

    groups2 = []
    for i in np.arange(len(lpd2i.resnums)):
        resnum = lpd2i.resnums[i]
        group = u.select_atoms(
            'resname DIPC and resnum %i and (name D2A or name D2B)' % resnum)
        groups2.append(group)
    lpd2i = np.sum(groups2)

    groups3 = []
    for i in np.arange(len(lpd3i.resnums)):
        resnum = lpd3i.resnums[i]
        group = u.select_atoms('resname CHOL and resnum %i and (name R3)' %
                               resnum)
        groups3.append(group)
    lpd3i = np.sum(groups3)

    if protein == 'YES':
        #Selecting Protein atoms that are on the tail plane.
        phospholipidtails = lpd1i + lpd2i  #Phospholipid tail atoms that defines the plane of interest.
        ns_PROT = NS.AtomNeighborSearch(prot_atoms)  #Protein
        proti = ns_PROT.search(
            phospholipidtails, 10.0
        )  #Protein atoms that are within 1.0nm of the Phospholipid tail atoms.

        u_leaflet = lpd1i + lpd2i + lpd3i + proti
    else:
        u_leaflet = lpd1i + lpd2i + lpd3i

##################################################################################

#Collecting coordinates of the interface atoms
    mem_plane = []
    mem_plane_composition = []

    if protein == 'YES':
        for selatm in proti:
            mem_plane.append(selatm.position)
            mem_plane_composition.append('PROT')

    for selatm in lpd1i:
        mem_plane.append(selatm.position)
        mem_plane_composition.append(lipid1)

    for selatm in lpd2i:
        mem_plane.append(selatm.position)
        mem_plane_composition.append(lipid2)

    for selatm in lpd3i:
        mem_plane.append(selatm.position)
        mem_plane_composition.append(lipid3)

    mem_plane_data = [mem_plane, mem_plane_composition, XBOX, YBOX]

    return mem_plane_data
def calcAPL(frame):
    print frame
    u = MDAnalysis.Universe(psf, trr)
    u.trajectory[frame]

    #Lipid Residue names
    lpd1_atoms = u.select_atoms('resname %s and name PO4' % lipid1)
    lpd2_atoms = u.select_atoms('resname %s and name PO4' % lipid2)
    lpd3_atoms = u.select_atoms('resname %s and name ROH' % lipid3)

    num_lpd1 = lpd1_atoms.n_atoms
    num_lpd2 = lpd2_atoms.n_atoms

    #Separating upper and lower leaflets
    # atoms in the upper leaflet as defined by insane.py or the CHARMM-GUI membrane builders
    # select cholesterol headgroups within 1.5 nm of lipid headgroups in the selected leaflet
    if side == 'up':
        lpd1i = lpd1_atoms[:((num_lpd1) / 2)]
        lpd2i = lpd2_atoms[:((num_lpd2) / 2)]

        lipids = lpd1i + lpd2i
        ns_lipids = NS.AtomNeighborSearch(lpd3_atoms)

        lpd3i = ns_lipids.search(lipids, 15.0)
    elif side == 'down':
        lpd1i = lpd1_atoms[((num_lpd1) / 2):]
        lpd2i = lpd2_atoms[((num_lpd2) / 2):]

        lipids = lpd1i + lpd2i
        ns_lipids = NS.AtomNeighborSearch(lpd3_atoms)

        lpd3i = ns_lipids.search(lipids, 15.0)

    lpd_atms = lpd1i + lpd2i + lpd3i

    nlipid = lpd_atms.n_atoms

    frm_l1_lpd_area = []
    frm_l2_lpd_area = []
    frm_l3_lpd_area = []

    #Extracting the coordinates
    Pxyz = lpd_atms.positions
    Pxy = []
    for l in range(0, len(Pxyz)):
        Pxy.append([Pxyz[l][0], Pxyz[l][1]])
    #Extracting xy coordinates and residue names

    atmlist = []
    for a in range(0, len(Pxyz)):
        atmlist.append([Pxyz[a][0], Pxyz[a][1], lpd_atms[a].resname])

    xbox = u.dimensions[0]
    ybox = u.dimensions[1]

    atm_list_pbc = introducePBC(xbox, ybox, atmlist)

    atm_xy = []
    for i in range(0, len(atm_list_pbc)):
        atm_xy.append([atm_list_pbc[i][0], atm_list_pbc[i][1]])

    vor = Voronoi(atm_xy)
    vor_s = Voronoi(Pxy)
    vertices = vor.vertices

    #Selecting the Index of the Voronoi region for each input point in siede the box
    point_region_box = vor.point_region[:nlipid]
    point_region_pbc = vor.point_region

    for k in range(len(point_region_box)):
        point_region_k = point_region_box[k]
        vor_vert = vor.regions[point_region_k]
        vor_vert_coor = calcVerticeCoordinates(vor_vert, vor)
        lpd_area = calcAreaofPolygon(vor_vert_coor)

        if atm_list_pbc[k][2] == lipid1:
            frm_l1_lpd_area.append(lpd_area)
        elif atm_list_pbc[k][2] == lipid2:
            frm_l2_lpd_area.append(lpd_area)
        elif atm_list_pbc[k][2] == lipid3:
            frm_l3_lpd_area.append(lpd_area)
        else:
            frm_GM_lpd_area.append(lpd_area)

    return frm_l1_lpd_area, frm_l2_lpd_area, frm_l3_lpd_area, frm_GM_lpd_area
Exemple #13
0
def get_side_coordinates_and_box(u, time_ts):
    """Assign lipids to leaflets, retrieve their coordinates, resIDs and the director order parameter u=3/2cosˆ2(theta)-1/2"""

    x, y, z = u.trajectory.ts.triclinic_dimensions[0][
        0], u.trajectory.ts.triclinic_dimensions[1][
            1], u.trajectory.ts.triclinic_dimensions[2][2]
    box = np.array([x, y, z])

    ### Determining side of the bilayer CHOL belongs to in this frame
    #Lipid Residue names
    lipid1 = 'DSPC'
    #lipid2 ='DAPC'
    lipid2 = 'DLIP'
    lipid3 = 'CHL'

    lpd1_atoms = u.select_atoms('resname %s and name P' % lipid1)
    lpd2_atoms = u.select_atoms('resname %s and name P' % lipid2)
    lpd3_atoms = u.select_atoms('resname %s and name O2' % lipid3)
    num_lpd1 = lpd1_atoms.n_atoms
    num_lpd2 = lpd2_atoms.n_atoms
    # atoms in the upper leaflet as defined by insane.py or the CHARMM-GUI membrane builders
    # select cholesterol headgroups within 1.5 nm of lipid headgroups in the selected leaflet
    # this must be done because CHOL rapidly flip-flops between leaflets in the MARTINI model
    # so we must assign CHOL to each leaflet at every time step, and in large systems
    # with substantial membrane undulations, a simple cut-off in the z-axis just will not cut it
    if side == 'up':
        lpd1i = lpd1_atoms[:int((num_lpd1) / 2)]
        lpd2i = lpd2_atoms[:int((num_lpd2) / 2)]
        lipids = lpd1i + lpd2i
        ns_lipids = NS.AtomNeighborSearch(lpd3_atoms)
        lpd3i = ns_lipids.search(lipids, 15.0)
    elif side == 'down':
        lpd1i = lpd1_atoms[int((num_lpd1) / 2):]
        lpd2i = lpd2_atoms[int((num_lpd2) / 2):]
        lipids = lpd1i + lpd2i
        ns_lipids = NS.AtomNeighborSearch(lpd3_atoms)
        lpd3i = ns_lipids.search(lipids, 15.0)

        #define the cholesterol
        # ID center of geometry coordinates for cholesterol on indicated bilayer side
    #print(lpd3i)
    lpd3_coords = np.zeros((len(lpd3i.resnums), 3))
    lpd3_res = np.zeros((len(lpd3i.resnums), 1))
    lpd3_u = np.zeros((len(lpd3i.resnums), 1))
    for i in np.arange(len(lpd3i.resnums)):

        resnum = lpd3i.resnums[i]
        head_CHL = u.select_atoms('resnum %i and (name C1)' %
                                  resnum).center_of_geometry()
        tail_CHL = u.select_atoms('resnum %i and (name C65)' %
                                  resnum).center_of_geometry()
        vect_CHL = head_CHL - tail_CHL
        theta_CHL = np.arccos(
            np.dot(vect_CHL, vref) / (norm(vect_CHL) * norm(vref)))

        u_CHL = 1.5 * ((pow(np.cos(theta_CHL), 2))) - 0.5
        group = u.select_atoms('resnum %i' % resnum)
        group_cog = group.center_of_geometry()
        lpd3_coords[i] = group_cog
        lpd3_res[i] = resnum
        lpd3_u[i] = u_CHL

    # ID coordinates for lipids on indicated bilayer side, renaming variables
    lpd1_coordsA = np.zeros((len(lpd1i.resnums), 3))
    lpd1_coordsB = np.zeros((len(lpd1i.resnums), 3))
    lpd1_res = np.zeros((len(lpd1i.resnums), 1))
    lpd1_u = np.zeros((len(lpd1i.resnums), 1))
    for i in np.arange(len(lpd1i.resnums)):
        resnum_A = lpd1i.resnums[i]
        resnum_B = lpd1i.resnums[i]
        ###test the lipids-end-tails
        head_DSPC_A = u.select_atoms('resnum %i and (name C22)' %
                                     resnum_A).center_of_geometry()
        tail_DSPC_A = u.select_atoms('resnum %i and (name 6C21)' %
                                     resnum_A).center_of_geometry()
        vect_DSPC_A = head_DSPC_A - tail_DSPC_A
        theta_DSPC_A = np.arccos(
            np.dot(vect_DSPC_A, vref) / (norm(vect_DSPC_A) * norm(vref)))

        head_DSPC_B = u.select_atoms('resnum %i and (name C32)' %
                                     resnum_B).center_of_geometry()
        tail_DSPC_B = u.select_atoms('resnum %i and (name 6C31)' %
                                     resnum_B).center_of_geometry()
        vect_DSPC_B = head_DSPC_B - tail_DSPC_B
        theta_DSPC_B = np.arccos(
            np.dot(vect_DSPC_B, vref) / (norm(vect_DSPC_B) * norm(vref)))

        theta_DSPC = (theta_DSPC_A + theta_DSPC_B) / 2
        u_DSPC = ((3 / 2) * (pow(np.cos(theta_DSPC), 2))) - 0.5
        #print(u_DPPC)
        lpd1_u[i] = u_DSPC
        group_lpd1_chainA = u.select_atoms(
            'resnum %i and (name C26 or name C27 or name C28 or name C29 )' %
            resnum_A)
        group_lpd1_chainB = u.select_atoms(
            'resnum %i and (name C36 or name C37 or name C38 or name C39)' %
            resnum_B)
        group_cog_lpd1A = group_lpd1_chainA.center_of_geometry()
        group_cog_lpd1B = group_lpd1_chainB.center_of_geometry()
        lpd1_coordsA[i] = group_cog_lpd1A
        lpd1_coordsB[i] = group_cog_lpd1B
        lpd1_res[i] = resnum_A

    lpd2_coordsA = np.zeros((len(lpd2i.resnums), 3))
    lpd2_coordsB = np.zeros((len(lpd2i.resnums), 3))
    lpd2_res = np.zeros((len(lpd2i.resnums), 1))
    lpd2_u = np.zeros((len(lpd2i.resnums), 1))
    for i in np.arange(len(lpd2i.resnums)):
        resnumB_A = lpd2i.resnums[i]
        resnumB_B = lpd2i.resnums[i]

        #head_DAPC_A = u.select_atoms('resnum %i and (name C24)'%resnumB_A).center_of_geometry()
        #tail_DAPC_A = u.select_atoms('resnum %i and (name 6C21)'%resnumB_A).center_of_geometry()
        #head_DLIP_A = u.select_atoms('resnum %i and (name C24)'%resnumB_A).center_of_geometry()
        #print(head_DLIP_A)
        head_DLIP_A = u.select_atoms(
            'resnum %i and (name C24)' %
            resnumB_A).positions  #changed for positions
        tail_DLIP_A = u.select_atoms('resnum %i and (name 6C21)' %
                                     resnumB_A).center_of_geometry()

        #print(pos_DLIP_A)
        #  vect_DAPC_A = head_DAPC_A - tail_DAPC_A
        #  theta_DAPC_A = np.arccos(np.dot(vect_DAPC_A, vref)/(norm(vect_DAPC_A)*norm(vref)))
        vect_DLIP_A = head_DLIP_A - tail_DLIP_A
        theta_DLIP_A = np.arccos(
            np.dot(vect_DLIP_A, vref) / (norm(vect_DLIP_A) * norm(vref)))

        #  head_DAPC_B = u.select_atoms('resnum %i and (name C34)'%resnumB_B).center_of_geometry()
        #  tail_DAPC_B = u.select_atoms('resnum %i and (name 6C31)'%resnumB_B).center_of_geometry()
        head_DLIP_B = u.select_atoms('resnum %i and (name C34)' %
                                     resnumB_B).center_of_geometry()
        tail_DLIP_B = u.select_atoms('resnum %i and (name 6C31)' %
                                     resnumB_B).center_of_geometry()

        #  vect_DAPC_B = head_DAPC_B - tail_DAPC_B
        #  theta_DAPC_B = np.arccos(np.dot(vect_DAPC_B, vref)/(norm(vect_DAPC_B)*norm(vref)))
        #  theta_DAPC = (theta_DAPC_A + theta_DAPC_B)/2
        vect_DLIP_B = head_DLIP_B - tail_DLIP_B
        theta_DLIP_B = np.arccos(
            np.dot(vect_DLIP_B, vref) / (norm(vect_DLIP_B) * norm(vref)))
        theta_DLIP = (theta_DLIP_A + theta_DLIP_B) / 2

        #        u_DAPC= ((3/2)*(pow(np.cos(theta_DAPC), 2))) - 0.5
        #        lpd2_u[i] = u_DAPC
        u_DLIP = ((3 / 2) * (pow(np.cos(theta_DLIP), 2))) - 0.5
        lpd2_u[i] = u_DLIP
        group_lpd2_chainA = u.select_atoms(
            'resnum %i and (name C26 or name C27 or name C28 or name C29)' %
            resnumB_A)
        group_lpd2_chainB = u.select_atoms(
            'resnum %i and (name C36 or name C37 or name C38 or name C39)' %
            resnumB_B)
        group_cog_lpd2A = group_lpd2_chainA.center_of_geometry()
        lpd2_coordsA[i] = group_cog_lpd2A
        group_cog_lpd2B = group_lpd2_chainB.center_of_geometry()
        lpd2_coordsB[i] = group_cog_lpd2B
        lpd2_res[i] = resnumB_A

    lpd_coords = np.vstack((lpd1_coordsA, lpd1_coordsB, lpd2_coordsA,
                            lpd2_coordsB, lpd3_coords))  #append
    lpd_resids = np.vstack((lpd1_res, lpd1_res, lpd2_res, lpd2_res, lpd3_res))
    lpd_us = np.vstack((lpd1_u, lpd1_u, lpd2_u, lpd2_u, lpd3_u))
    #print(time_ts)
    file = open("testfile.txt", "a")
    file.write(str(time_ts.frame) + "\n")
    file.close()
    return lpd_coords, lpd_resids, lpd_us, box
def get_side_coordinates_and_box(frame):
    u = MDAnalysis.Universe(top, traj)
    u.trajectory[frame]

    x, y, z = u.trajectory.ts.triclinic_dimensions[0][
        0], u.trajectory.ts.triclinic_dimensions[1][
            1], u.trajectory.ts.triclinic_dimensions[2][2]
    box = np.array([x, y, z])

    ### Determining side of the bilayer CHOL belongs to in this frame
    #Lipid Residue names
    lipid1 = 'DPPC'
    lipid2 = 'DFPC'
    lipid3 = 'CHOL'

    lpd1_atoms = u.select_atoms('resname %s and name PO4' % lipid1)
    lpd2_atoms = u.select_atoms('resname %s and name PO4' % lipid2)
    lpd3_atoms = u.select_atoms('resname %s and name ROH' % lipid3)
    num_lpd1 = lpd1_atoms.n_atoms
    num_lpd2 = lpd2_atoms.n_atoms
    # atoms in the upper leaflet as defined by insane.py or the CHARMM-GUI membrane builders
    # select cholesterol headgroups within 1.5 nm of lipid headgroups in the selected leaflet
    # this must be done because CHOL rapidly flip-flops between leaflets in the MARTINI model
    # so we must assign CHOL to each leaflet at every time step, and in large systems
    # with substantial membrane undulations, a simple cut-off in the z-axis just will not cut it
    if side == 'up':
        lpd1i = lpd1_atoms[:((num_lpd1) / 2)]
        lpd2i = lpd2_atoms[:((num_lpd2) / 2)]
        lipids = lpd1i + lpd2i
        ns_lipids = NS.AtomNeighborSearch(lpd3_atoms)
        lpd3i = ns_lipids.search(lipids, 15.0)
    elif side == 'down':
        lpd1i = lpd1_atoms[((num_lpd1) / 2):]
        lpd2i = lpd2_atoms[((num_lpd2) / 2):]
        lipids = lpd1i + lpd2i
        ns_lipids = NS.AtomNeighborSearch(lpd3_atoms)
        lpd3i = ns_lipids.search(lipids, 15.0)

    # ID center of geometry coordinates for cholesterol on indicated bilayer side
    lpd3_coords = np.zeros((len(lpd3i.resnums), 3))
    for i in np.arange(len(lpd3i.resnums)):
        resnum = lpd3i.resnums[i]
        group = u.select_atoms(
            'resnum %i and (name R1 or name R2 or name R3 or name R4 or name R5)'
            % resnum)
        group_cog = group.center_of_geometry()
        lpd3_coords[i] = group_cog

    # ID coordinates for lipids on indicated bilayer side, renaming variables
    lpd1_atoms = u.select_atoms('resname %s and (name C2A or name C2B)' %
                                lipid1)
    lpd2_atoms = u.select_atoms('resname %s and (name D2A or name D2B)' %
                                lipid2)
    num_lpd1 = lpd1_atoms.n_atoms
    num_lpd2 = lpd2_atoms.n_atoms

    # select lipid tail atoms beloging to the selected bilayer side
    if side == 'up':
        lpd1i = lpd1_atoms[:((num_lpd1) / 2)]
        lpd2i = lpd2_atoms[:((num_lpd2) / 2)]

    elif side == 'down':
        lpd1i = lpd1_atoms[((num_lpd1) / 2):]
        lpd2i = lpd2_atoms[((num_lpd2) / 2):]

    # assign lpd1 and lpd2 coordinates, completing the assignment of all coordinates from which psi6 will be computed
    lpd1_coords = lpd1i.positions
    lpd2_coords = lpd2i.positions

    #lpd_coords = np.vstack((lpd1_coords,lpd2_coords,lpd3_coords))
    #lpd_coords = np.vstack((lpd1_coords,lpd2_coords,lpd3_coords))
    lpd_coords = np.vstack((lpd1_coords, lpd3_coords))
    lpd_coords = lpd_coords.astype('float32')
    return lpd_coords, box
def voronoi_tessel(frame):
    # set the time step
    print 'Frame %i in %i' % (frame, end_f)
    u = MDAnalysis.Universe(psf, trr)
    u.trajectory[int(frame)]

    # Select atoms within this particular frame
    lpd1_atoms = u.select_atoms('resname %s and name PO4' % lipid1)
    lpd2_atoms = u.select_atoms('resname %s and name PO4' % lipid2)
    lpd3_atoms = u.select_atoms('resname %s and name ROH' % lipid3)

    prot_atoms = u.select_atoms(selprot)

    num_lpd1 = lpd1_atoms.n_atoms
    num_lpd2 = lpd2_atoms.n_atoms

    #Separating upper and lower leaflets
    # atoms in the upper leaflet as defined by insane.py or the CHARMM-GUI membrane builders
    # select cholesterol headgroups within 1.5 nm of lipid headgroups in the selected leaflet
    if side == 'up':
        lpd1i = lpd1_atoms[:((num_lpd1) / 2)]
        lpd2i = lpd2_atoms[:((num_lpd2) / 2)]

        lipids = lpd1i + lpd2i
        ns_lipids = NS.AtomNeighborSearch(lpd3_atoms)

        lpd3i = ns_lipids.search(lipids, 15.0)
    elif side == 'down':
        lpd1i = lpd1_atoms[((num_lpd1) / 2):]
        lpd2i = lpd2_atoms[((num_lpd2) / 2):]

        lipids = lpd1i + lpd2i
        ns_lipids = NS.AtomNeighborSearch(lpd3_atoms)

        lpd3i = ns_lipids.search(lipids, 15.0)

    if plane_of_interest == 'TAIL':
        #Using tail definitions
        groups1 = []
        for i in np.arange(len(lpd1i.resnums)):
            resnum = lpd1i.resnums[i]
            group = u.select_atoms(
                'resname %s and resnum %i and (name C2A or name C2B)' %
                (lipid1, resnum))
            groups1.append(group)
        lpd1i = np.sum(groups1)

        groups2 = []
        for i in np.arange(len(lpd2i.resnums)):
            resnum = lpd2i.resnums[i]
            group = u.select_atoms(
                'resname %s and resnum %i and (name D2A or name D2B)' %
                (lipid2, resnum))
            groups2.append(group)
        lpd2i = np.sum(groups2)

        groups3 = []
        for i in np.arange(len(lpd3i.resnums)):
            resnum = lpd3i.resnums[i]
            group = u.select_atoms('resname CHOL and resnum %i and (name R3)' %
                                   resnum)
            groups3.append(group)
        lpd3i = np.sum(groups3)

    if protein == 'YES':
        #Selecting Protein atoms that are on the membrane plane.
        phospholipidgrid = lpd1i + lpd2i  #Phospholipid tail atoms that defines the plane of interest.
        ns_PROT = NS.AtomNeighborSearch(prot_atoms)  #Protein
        proti = ns_PROT.search(
            phospholipidgrid,
            10.0)  #Protein atoms that are within 1.0nm of the membrane
        u_leaflet = lpd1i + lpd2i + lpd3i + proti

    else:
        u_leaflet = lpd1i + lpd2i + lpd3i

#Extracting the coordinates
    lpd_atms = u_leaflet
    Pxyz = lpd_atms.positions
    Pxy = []
    for l in range(0, len(Pxyz)):
        Pxy.append([Pxyz[l][0], Pxyz[l][1]])
#Extracting xy coordinates and residue names

    atm_list = []
    for a in range(0, len(Pxyz)):
        #print lpd_atms[a].resname
        atm_list.append(
            [Pxyz[a][0], Pxyz[a][1], lpd_atms[a].resname, lpd_atms[a].segid])

#Introducing PBC
    x_box = u.dimensions[0]
    y_box = u.dimensions[1]

    xplus = []
    xminus = []
    xyplus = []
    xyminus = []

    for atm in range(0, len(atm_list)):
        xplus.append([
            atm_list[atm][0] + x_box, atm_list[atm][1], atm_list[atm][2],
            atm_list[atm][3]
        ])
        xminus.append([
            atm_list[atm][0] - x_box, atm_list[atm][1], atm_list[atm][2],
            atm_list[atm][3]
        ])

    atm_list_px = atm_list + xplus + xminus

    for atm in range(0, len(atm_list_px)):
        xyplus.append([
            atm_list_px[atm][0], atm_list_px[atm][1] + y_box,
            atm_list_px[atm][2], atm_list_px[atm][3]
        ])
        xyminus.append([
            atm_list_px[atm][0], atm_list_px[atm][1] - y_box,
            atm_list_px[atm][2], atm_list_px[atm][3]
        ])

    atm_list_p = atm_list_px + xyplus + xyminus

    atm_xy = []
    for i in range(0, len(atm_list_p)):
        atm_xy.append([atm_list_p[i][0], atm_list_p[i][1]])

    vor = Voronoi(atm_xy)
    vor_s = Voronoi(Pxy)
    vertices = vor.vertices

    ridge_points = vor.ridge_points

    #Plotting Voroni Diagrams
    vor_rgns = vor.regions
    l_vor_rgns = len(vor_rgns)

    vor_points = vor.point_region
    l_vor_points = len(vor_points)
    if PRINT_VOR == 'YES' and np.mod(frame, PRINT_FREQ) == 0:
        plt.clf()
        #voronoi_plot_2d(vor_s)

        for p in range(0, l_vor_points):
            rgn = vor_rgns[vor_points[p]]
            L = atm_list_p[p]
            if not -1 in rgn and 0 <= L[0] < x_box and 0 <= L[1] < y_box:
                if L[2] == lipid1:
                    polygon = [vor.vertices[i] for i in rgn]
                    plt.plot(*zip(*polygon), color='black')
                    plt.fill(*zip(*polygon), color='blue')
                elif L[2] == lipid2:
                    polygon = [vor.vertices[i] for i in rgn]
                    plt.plot(*zip(*polygon), color='black')
                    plt.fill(*zip(*polygon), color='red')
                elif L[2] == lipid3:
                    polygon = [vor.vertices[i] for i in rgn]
                    plt.plot(*zip(*polygon), color='black')
                    plt.fill(*zip(*polygon), color='green')
                else:
                    polygon = [vor.vertices[i] for i in rgn]
                    plt.plot(*zip(*polygon), color='black')
                    plt.fill(*zip(*polygon), color=PROT_color)
        plt.axis('equal')
        if side == 'up':
            plt.savefig('VT/img' + str('%03d' % frame) + '.up.png')
        if side == 'down':
            plt.savefig('VT/img' + str('%03d' % frame) + '.down.png')

    Lpd1_Lpd1_I = 0
    Lpd2_Lpd2_I = 0
    Lpd3_Lpd3_I = 0
    Lpd1_Lpd2_I = 0
    Lpd1_Lpd3_I = 0
    Lpd2_Lpd3_I = 0

    Lpd1_Lpd1_E = 0
    Lpd2_Lpd2_E = 0
    Lpd3_Lpd3_E = 0
    Lpd1_Lpd2_E = 0
    Lpd1_Lpd3_E = 0
    Lpd2_Lpd3_E = 0

    Prot_Lpd1_I = 0
    Prot_Lpd2_I = 0
    Prot_Lpd3_I = 0
    Prot_Prot_I = 0

    Prot_Lpd1_E = 0
    Prot_Lpd2_E = 0
    Prot_Lpd3_E = 0
    Prot_Prot_E = 0

    r_length = len(ridge_points)

    for k in range(0, r_length):
        ridge_k = ridge_points[k]
        Li = atm_list_p[int(ridge_k[0])]
        Lj = atm_list_p[int(ridge_k[1])]

        #print 'Li', Li[3], Li[2]
        #print 'Lj', Lj[3], Lj[2]

        #Lipids INSIDE the box

        if 0 < Li[0] < x_box and 0 < Li[1] < y_box and 0 < Lj[
                0] < x_box and 0 < Lj[1] < y_box:
            #Lipid Lipid contacts

            if Li[2] == lipid1 and Lj[2] == lipid1:
                Lpd1_Lpd1_I = Lpd1_Lpd1_I + 1

            if Li[2] == lipid2 and Lj[2] == lipid2:
                Lpd2_Lpd2_I = Lpd2_Lpd2_I + 1

            if Li[2] == lipid3 and Lj[2] == lipid3:
                Lpd3_Lpd3_I = Lpd3_Lpd3_I + 1

            if Li[2] == lipid1 and Lj[2] == lipid2:
                Lpd1_Lpd2_I = Lpd1_Lpd2_I + 1

            if Li[2] == lipid2 and Lj[2] == lipid1:
                Lpd1_Lpd2_I = Lpd1_Lpd2_I + 1

            if Li[2] == lipid1 and Lj[2] == lipid3:
                Lpd1_Lpd3_I = Lpd1_Lpd3_I + 1

            if Li[2] == lipid3 and Lj[2] == lipid1:
                Lpd1_Lpd3_I = Lpd1_Lpd3_I + 1

            if Li[2] == lipid2 and Lj[2] == lipid3:
                Lpd2_Lpd3_I = Lpd2_Lpd3_I + 1

            if Li[2] == lipid3 and Lj[2] == lipid2:
                Lpd2_Lpd3_I = Lpd2_Lpd3_I + 1

#Protein lipid contacts
            if Li[3] in PROT_seg and Lj[2] == lipid1:
                Prot_Lpd1_I = Prot_Lpd1_I + 1

            if Li[2] == lipid1 and Lj[3] in PROT_seg:
                Prot_Lpd1_I = Prot_Lpd1_I + 1

            if Li[3] in PROT_seg and Lj[2] == lipid2:
                Prot_Lpd2_I = Prot_Lpd2_I + 1

            if Li[2] == lipid2 and Lj[3] in PROT_seg:
                Prot_Lpd2_I = Prot_Lpd2_I + 1

            if Li[3] in PROT_seg and Lj[2] == lipid3:
                Prot_Lpd3_I = Prot_Lpd3_I + 1

            if Li[2] == lipid3 and Lj[3] in PROT_seg:
                Prot_Lpd3_I = Prot_Lpd3_I + 1

            if Li[3] in PROT_seg and Lj[
                    3] in PROT_seg and Li[3] != Lj[3]:  # Avoid self counts
                Prot_Prot_I = Prot_Prot_I + 1

#Lipids at the EDGE of the box
#Lipid Lipid contacts
        if 0 <= Li[0] < x_box and 0 <= Li[1] < y_box or 0 <= Lj[
                0] < x_box and 0 <= Lj[1] < y_box:

            if Li[2] == lipid1 and Lj[2] == lipid1:
                Lpd1_Lpd1_E = Lpd1_Lpd1_E + 1

            if Li[2] == lipid2 and Lj[2] == lipid2:
                Lpd2_Lpd2_E = Lpd2_Lpd2_E + 1

            if Li[2] == lipid3 and Lj[2] == lipid3:
                Lpd3_Lpd3_E = Lpd3_Lpd3_E + 1

            if Li[2] == lipid1 and Lj[2] == lipid2:
                Lpd1_Lpd2_E = Lpd1_Lpd2_E + 1

            if Li[2] == lipid2 and Lj[2] == lipid1:
                Lpd1_Lpd2_E = Lpd1_Lpd2_E + 1

            if Li[2] == lipid1 and Lj[2] == lipid3:
                Lpd1_Lpd3_E = Lpd1_Lpd3_E + 1

            if Li[2] == lipid3 and Lj[2] == lipid1:
                Lpd1_Lpd3_E = Lpd1_Lpd3_E + 1

            if Li[2] == lipid2 and Lj[2] == lipid3:
                Lpd2_Lpd3_E = Lpd2_Lpd3_E + 1

            if Li[2] == lipid3 and Lj[2] == lipid2:
                Lpd2_Lpd3_E = Lpd2_Lpd3_E + 1

#Protein lipid contacts
            if Li[3] in PROT_seg and Lj[2] == lipid1:
                Prot_Lpd1_E = Prot_Lpd1_E + 1

            if Li[2] == lipid1 and Lj[3] in PROT_seg:
                Prot_Lpd1_E = Prot_Lpd1_E + 1

            if Li[3] in PROT_seg and Lj[2] == lipid2:
                Prot_Lpd2_E = Prot_Lpd2_E + 1

            if Li[2] == lipid2 and Lj[3] in PROT_seg:
                Prot_Lpd2_E = Prot_Lpd2_E + 1

            if Li[3] in PROT_seg and Lj[2] == lipid3:
                Prot_Lpd3_E = Prot_Lpd3_E + 1

            if Li[2] == lipid3 and Lj[3] in PROT_seg:
                Prot_Lpd3_E = Prot_Lpd3_E + 1

            if Li[3] in PROT_seg and Lj[
                    3] in PROT_seg and Li[3] != Lj[3]:  # Avoid self counts
                Prot_Prot_E = Prot_Prot_E + 1


#Total = LipidsInside + (Lipids including EDGES - Lipids Inside)/2 -----> Correction for over counting the lipids in periodic images
    Lpd1_Lpd1 = Lpd1_Lpd1_I + (Lpd1_Lpd1_E - Lpd1_Lpd1_I) / 2
    Lpd2_Lpd2 = Lpd2_Lpd2_I + (Lpd2_Lpd2_E - Lpd2_Lpd2_I) / 2
    Lpd3_Lpd3 = Lpd3_Lpd3_I + (Lpd3_Lpd3_E - Lpd3_Lpd3_I) / 2
    Lpd1_Lpd2 = Lpd1_Lpd2_I + (Lpd1_Lpd2_E - Lpd1_Lpd2_I) / 2
    Lpd1_Lpd3 = Lpd1_Lpd3_I + (Lpd1_Lpd3_E - Lpd1_Lpd3_I) / 2
    Lpd2_Lpd3 = Lpd2_Lpd3_I + (Lpd2_Lpd3_E - Lpd2_Lpd3_I) / 2

    Prot_Lpd1 = Prot_Lpd1_I + (Prot_Lpd1_E - Prot_Lpd1_I) / 2
    Prot_Lpd2 = Prot_Lpd2_I + (Prot_Lpd2_E - Prot_Lpd2_I) / 2
    Prot_Lpd3 = Prot_Lpd3_I + (Prot_Lpd3_E - Prot_Lpd3_I) / 2
    Prot_Prot = Prot_Prot_I + (Prot_Prot_E - Prot_Prot_I) / 2

    return Lpd1_Lpd1, Lpd2_Lpd2, Lpd3_Lpd3, Lpd1_Lpd2, Lpd1_Lpd3, Lpd2_Lpd3, Prot_Lpd1, Prot_Lpd2, Prot_Lpd3, Prot_Prot