Exemplo n.º 1
0
def summpbsa():
    import numpy as np
    import subprocess as sp
    from collections import defaultdict
    from mdanaly import cmap

    ref = "complex_1/em-c1.pdb"
    sp.check_output("echo \"1 0 \" | trjconv -f complex_1/em-c1.gro -s complex_1/em-c1.tpr -o complex_1/em-c1.pdb",
                    shell=True)
    CMAP = cmap.ContactMap("./complex_1/em-c1.pdb")

    at = CMAP.findAtomType('non-hydrogen', ref)
    rd = defaultdict(list)
    rd["A"] = range(1, 300)
    ld = defaultdict(list)
    ld["B"] = range(1, 10)
    rndx = CMAP.findAtomNdx(ref, rd, "A", at, False)
    lndx = CMAP.findAtomNdx(ref, ld, "B", at, False)

    energy = np.loadtxt("./mmpbsa_summary.dat")

    f_idx = [int(x) for x in energy[:, 0]]

    result = np.array([])

    for mmpbsa in energy:
        i = int(mmpbsa[0])
        sp.check_output(
            "echo \"1 0 \" | trjconv -f complex_%d/em-c%d.gro -s complex_%d/em-c%d.tpr -o complex_%d/em-c%d.pdb"
            % (i, i, i, i, i, i), shell=True)
        fn = ["complex_%d/em-c%d.pdb" % (i, i)]

        c = CMAP.cmap_ca(fn, 0.35, False, [rndx, lndx], 0, False,
                         len(rndx) * len(rndx), perAtom=[False, False],
                         ccutoff=2.0, NbyN=False)

        e = mmpbsa[-1]

        if not result.shape[0]:
            result = c * (2.71828 ** (-1.0 * e / 2.5))
        else:
            result += c * (2.71828 ** (-1.0 * e / 2.5))

    MAX = np.max(result)
    cmap = np.reshape(result, (len(lndx), len(rndx)))

    np.savetxt("cmap_energy_normalized.dat", cmap / MAX, delimiter=" ", fmt="%.4f")
Exemplo n.º 2
0
def gen_cmap(args):
    """Load a trajectory file and calculate the atom contactmap.

    Parameters
    ----------
    args: argparse object
        the argument options

    Returns
    -------
    cmap_dat: pd.DataFrame, shape = [ N, M]
        the contactmap along the time. N is number of frames,
        M is N_res * N_res (N_res is number of residues for cmap
        calculations. )

    """

    # TODO: add a residue based selection module here
    # select atoms indices for distance calculations
    atoms_selections = args.select.split()
    if len(atoms_selections) == 2:
        atoms_selections = atoms_selections
    else:
        atoms_selections = [atoms_selections[0]] * 2

    top = mt.load(args.s).topology

    atom_grp_a = top.select("name %s" % atoms_selections[0])
    atom_grp_b = top.select("name %s" % atoms_selections[1])

    # load the trajectory file by chunks iteratively
    print("Iterloading xtc trajectory file ...... ")
    trajs = gmxcli.read_xtc(xtc=args.f, top=args.s, chunk=1000, stride=int(args.dt/args.ps))

    cmap_dat = np.array([])
    print("Computing contactmap ...... ")
    for i, traj in enumerate(trajs):
        contmap = cmap.ContactMap(traj=traj, group_a=atom_grp_a, group_b=atom_grp_b, cutoff=args.cutoff)
        contmap.generate_cmap(shape="array", switch=args.switch)
        if i == 0:
            cmap_dat = contmap.cmap_
        else:
            cmap_dat = np.concatenate((cmap_dat, contmap.cmap_), axis=0)

    cmap_dat = pd.DataFrame(cmap_dat)

    return cmap_dat
Exemplo n.º 3
0
def run_coord_number():
    """


    Examples:
    gmx_coordnum.py -f test_traj_dt1ns.xtc -s reference.pdb -rc " " 1 40
    -lc " " 50 70 -o coord_result.csv -atomtype all all -byres True
    -v True -cutoff 0.5
    """
    startTime = datetime.now()

    args = arguments()

    resides_a, resides_b = [], []
    group_a, group_b = [], []

    if args.byres:
        ndx = index.PdbIndex(args.s, [args.rc[0]], args.rc[1:])
        ndx.resid_mapper()
        #print(ndx.resid_mapper_)
        s, e = ndx.resid_mt_style(args.rc[0], args.rc[1], args.rc[2])
        #print(s, e)
        resides_a = np.arange(s, e + 1)

        ndx = index.PdbIndex(args.s, [args.lc[0]], args.lc[1:])
        ndx.resid_mapper()
        #print(ndx.resid_mapper_)
        s, e = ndx.resid_mt_style(args.lc[0], args.lc[1], args.lc[2])
        resides_b = np.arange(s, e + 1)

        verbose(args.v,
                "X-axis resids : " + " ".join([str(x) for x in resides_a]))
        verbose(args.v,
                "Y-axis resids : " + " ".join([str(x) for x in resides_b]))

    else:
        # TODO: define a way to select atom slices
        # define the atom indices for receptor
        '''ndx = index.PdbIndex(reference=args.s, atomtype=args.atomtype[0],
                             resSeq=args.rc[1:],
                             chain=[args.rc[0]])
        ndx.prepare_selection()
        ndx.res_index()'''
        group_a = index.gen_atom_index(pdbin=args.s,
                                       chain=[args.rc[0]],
                                       atomtype=args.atomtype[0],
                                       resSeq=args.rc[1:],
                                       style="mdtraj")
        #print(group_a)
        # define the atom indices for ligand
        '''ndx = index.PdbIndex(reference=args.s, atomtype=args.atomtype[1],
                             resSeq=args.lc[1:],
                             chain=[args.lc[0]])
        ndx.prepare_selection()
        ndx.res_index()'''
        group_b = index.gen_atom_index(pdbin=args.s,
                                       chain=[args.lc[0]],
                                       atomtype=args.atomtype[-1],
                                       resSeq=args.lc[1:],
                                       style="mdtraj")
        verbose(args.v, "Atom indexing processing completed ......")

    results = np.array([])

    verbose(args.v, "Loading trajectory xtc file ...... ")
    trajs = gmxcli.read_xtc(args.f,
                            args.s,
                            chunk=1000,
                            stride=int(args.dt / args.ps))

    verbose(args.v, "Performing calculations ...... ")
    for i, traj in enumerate(trajs):
        verbose(args.v,
                "Generate coordinate number for chunk #%d trajectory " % i)
        if args.byres:
            coord = cmap.CmapNbyN(traj,
                                  resids_a=resides_a,
                                  resids_b=resides_b,
                                  cutoff=args.cutoff)
            coord.contact_nbyn()
            if i == 0:
                results = coord.contacts_by_res_
            else:
                results = np.concatenate((results, coord.contacts_by_res_),
                                         axis=0)

        else:
            coord = cmap.ContactMap(traj, group_a, group_b, args.cutoff)
            coord.coord_num()

            if i == 0:
                results = coord.coord_number_
            else:
                results = np.concatenate((results, coord.coord_number_),
                                         axis=0)

    results = pd.DataFrame(results)
    results.index = np.arange(results.shape[0]) * args.dt

    verbose(args.v, "Saving results to output file ...... ")
    results.to_csv(args.o,
                   sep=",",
                   header=False,
                   index=True,
                   float_format="%.1f")

    print("Total Time Usage: ")
    print(datetime.now() - startTime)
Exemplo n.º 4
0
    atom_indices = []
    for i in [0, 1]:
        print("Please select a group for %s: " % sets[i])
        for j, gn in enumerate(ndx.groups):
            print("%d : %s" % (j, gn))

        used_groups.append(ndx.groups[int(input("Your choice: "))])

    rec_ndx = [int(x) - 1 for x in ndx.groupContent(used_groups[0])]
    lig_ndx = [int(x) - 1 for x in ndx.groupContent(used_groups[1])]

    cmaps = np.array([])
    # generate cmap now
    for traj in trajs:
        CMap = cmap.ContactMap(traj, rec_ndx, lig_ndx, cutoff=0.5)
        CMap.generate_atom_pairs()
        CMap.generate_cmap(switch=True)

        c = CMap.cmap_

        if cmaps.shape[0] == 0:
            cmaps = c
        else:
            cmaps = np.concatenate((cmaps, c), axis=0)

    # run PCA now
    pca_obj = pca.PCA()
    pca_obj.fit_transform(cmaps[:, 1:])
    print(pca_obj.eigvalues_ratio_)
    X_transformed = pca_obj.X_transformed_