Exemplo n.º 1
0
def main(args):
    if not args.calc_BC and not args.calc_L:
        log.error(
            "At least one of the --calc-BC or --calc-L flags must be set.")
        sys.exit(1)

    global traj
    traj_name = os.path.basename(args.trajectory)
    traj, total_frames = load_trajectory(args.trajectory, args.topology,
                                         args.step, args.lazy_load)

    if args.calc_BC:
        calc_centralities(traj, traj_name, total_frames, args)

    if args.calc_L:
        calc_shortest_paths(traj, traj_name, total_frames, args)
Exemplo n.º 2
0
def parse_traj(traj,
               topology=None,
               step=1,
               selected_atoms=["CA"],
               lazy_load=False):
    traj = load_trajectory(traj, topology, step, lazy_load)[0]

    residues = {}

    for frame in traj:
        for atom in frame.topology.atoms:
            if atom.name in selected_atoms:
                res = atom.residue.resSeq

                ac = frame.xyz[0, atom.index]
                co_ords = [ac[0], ac[1], ac[2]]

                if res in residues:
                    residues[res].append(co_ords)
                else:
                    residues[res] = [co_ords]

    return residues
Exemplo n.º 3
0
def main(args):
    residue  = args.residue.upper()
    cutoff = args.threshold / 10
    chain  = args.chain
    prefix = residue

    chainChar = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    edges = []

    log.info("Loading trajectory...\n")

    traj, nframes = load_trajectory(args.trajectory, args.topology, args.step)

    # Get CA and CB atom indices
    atom_indices = [atom.index for atom in traj.topology.atoms if ((atom.name == "CB" and atom.residue.name != "GLY") or \
        (atom.name == "CA" and atom.residue.name == "GLY"))]

    residues = list(map(str, traj.top.residues))

    log.info("Calculating network around %s (chain %s)...\n" % (residue, chain))

    for frame in traj:

        # Takes a frame and updates a list of contact
        for aaidx, atom1_idx in enumerate(atom_indices):

            if residues[aaidx] == residue:
                atom1          = list(frame.top.atoms)[atom1_idx]
                atom1_chain    = chainChar[atom1.residue.chain.index]
                atom1_resname  = atom1.residue.name
                atom1_resid    = atom1.residue.resSeq

                if atom1_chain == chain:

                    for atom2_idx in atom_indices:
                        if atom1_idx != atom2_idx:
                            # Calculate distances and create edge
                            distance = np.linalg.norm(frame.xyz[0,atom1_idx] - frame.xyz[0,atom2_idx])

                            if distance < cutoff:
                                atom2         = list(frame.top.atoms)[atom2_idx]
                                atom2_chain   = chainChar[atom2.residue.chain.index]
                                atom2_resname = atom2.residue.name
                                atom2_resid   = atom2.residue.resSeq

                                # Write contact
                                contact = "{}{}_{},{}{}_{}".format(
                                        atom1_resname, atom1_resid, atom1_chain,
                                        atom2_resname, atom2_resid, atom2_chain
                                    )
                                edges.append(contact)

                    break #Stop looking for other positions

    #Write one variant at a time
    csv_file = "%s_chain%s_network.csv" % (prefix, chain)
    contact_map = "%s_chain%s_contact_map.pdf" % (prefix, chain)

    log.info("Writing network to %s...\n" % csv_file)

    with open(csv_file, 'w') as f_handle:
        edges = "\n".join(edges)
        f_handle.write(edges)

    log.info("Generating contact map: %s...\n" % contact_map)

    script_name = "rscript.R"
    write_rscript(script_name, nframes, csv_file, contact_map)

    sp.call("R CMD BATCH %s" % script_name, shell=True)

    if os.path.exists(contact_map):
        os.remove(script_name)
    else:
        r_out = "rscript.out"
        log.error("Contact map not generated. See contents of %s for details...\n" % r_out)
Exemplo n.º 4
0
def main(args):
    if not args.final:
        log.error("a final co-ordinate file must be supplied via the --final argument\n")
        sys.exit(1)

    initial = md.load_frame(args.trajectory, 0, top=args.topology)
    if not args.initial:
        args.initial = "initial.xyz"

        log.info("Generating initial co-ordinate file: %s\n" % args.initial)
        initial[0].save(args.initial)


    log.info("Loading trajectory...\n")

    if args.num_frames:
        traj, totalframes = load_trajectory(args.trajectory, args.topology, args.step, True)
        totalframes = args.num_frames
    else:
        traj, totalframes = load_trajectory(args.trajectory, args.topology, args.step, False)

    totalres = initial.n_residues

    log.info('- Total number of frames = %d\n- Number of residues = %d\n' % (totalframes, totalres))

    trajectory = trajectory_to_array(traj, totalframes, totalres)

    log.info('- Final trajectory matrix size: %s\n' % str(trajectory.shape))
    del traj


    log.info("Aligning trajectory frames...\n")

    aligned_mat = numpy.zeros((totalframes,3*totalres))
    frame_0 = trajectory[0].reshape(totalres, 3)

    for frame in range(0, totalframes):
        aligned_mat[frame] = align_frame(frame_0, trajectory[frame], args.aln)

    del trajectory


    log.info("- Calculating average structure...\n")

    average_structure_1 = numpy.mean(aligned_mat, axis=0).reshape(totalres, 3)


    log.info("- Aligning to average structure...\n")

    for i in range(0, 10):
        for frame in range(0, totalframes):
            aligned_mat[frame] = align_frame(average_structure_1, aligned_mat[frame], args.aln)

        average_structure_2 = numpy.average(aligned_mat, axis=0).reshape(totalres, 3)

        rmsd = calc_rmsd(average_structure_1, average_structure_2, args.aln)

        log.info('   - %s Angstroms from previous structure\n' % str(rmsd))

        average_structure_1 = average_structure_2
        del average_structure_2

        if rmsd <= 0.000001:
            for frame in range(0, totalframes):
                aligned_mat[frame] = align_frame(average_structure_1, aligned_mat[frame], args.aln)
            break


    log.info("Calculating difference between frame atoms and average atoms...\n")

    meanstructure = average_structure_1.reshape(totalres*3)

    del average_structure_1

    log.info('- Calculating R_mat\n')
    R_mat = numpy.zeros((totalframes, totalres*3))
    for frame in range(0, totalframes):
        R_mat[frame,:] = (aligned_mat[frame,:]) - meanstructure

    log.info('- Transposing\n')

    RT_mat = numpy.transpose(R_mat)

    RT_mat = numpy.mat(RT_mat)
    R_mat = numpy.mat(R_mat)

    log.info('- Calculating corr_mat\n')

    corr_mat = (RT_mat * R_mat)/ (totalframes-1)
    numpy.savetxt("corr_mat.txt", corr_mat)

    del aligned_mat
    del meanstructure
    del R_mat
    del RT_mat


    log.info('Reading initial and final PDB co-ordinates...\n')

    initial = numpy.zeros((totalres, 3))
    final = numpy.zeros((totalres, 3))

    with open(args.initial, 'r') as initial_lines:
        with open(args.final, 'r') as final_lines:

            res_index = 0
            for line_index, initial_line in enumerate(initial_lines):
                final_line = final_lines.readline()

                if line_index >= 2 and res_index < totalres:
                    initial_res = initial_line.strip().split()

                    if initial_res[0] == "CA":
                        final_res = final_line.strip().split()

                        initial[res_index,] = initial_res[1:]
                        final[res_index,] = final_res[1:]
                        res_index += 1


    log.info('Calculating experimental difference between initial and final co-ordinates...\n')

    if args.aln:
        log.info("- Using NTD alignment restrictions\n")
        final_alg = sdrms.superpose3D(final, initial, refmask=mask, targetmask=mask)[0]
    else:
        final_alg = sdrms.superpose3D(final, initial)[0]

    diffE = (final_alg-initial).reshape(totalres*3, 1)

    del final
    del final_alg


    log.info('Implementing perturbations sequentially...\n')

    perturbations = int(args.perturbations)
    diffP = numpy.zeros((totalres, totalres*3, perturbations))
    initial_trans = initial.reshape(1, totalres*3)

    for s in range(0, perturbations):
        for i in range(0, totalres):
            delF = numpy.zeros((totalres*3))
            f = 2 * numpy.random.random((3, 1)) - 1
            j = (i + 1) * 3

            delF[j-3] = round_sig(abs(f[0,0]), 5)* -1 if f[0,0]< 0 else round_sig(abs(f[0,0]), 5)
            delF[j-2] = round_sig(abs(f[1,0]), 5)* -1 if f[1,0]< 0 else round_sig(abs(f[1,0]), 5)
            delF[j-1] = round_sig(abs(f[2,0]), 5)* -1 if f[2,0]< 0 else round_sig(abs(f[2,0]), 5)

            diffP[i,:,s] = numpy.dot((delF), (corr_mat))
            diffP[i,:,s] = diffP[i,:,s] + initial_trans[0]

            if args.aln:
                diffP[i,:,s] = ((sdrms.superpose3D(diffP[i,:,s].reshape(totalres, 3), initial, refmask=mask, targetmask=mask)[0].reshape(1, totalres*3))[0]) - initial_trans[0]
            else:
                diffP[i,:,s] = ((sdrms.superpose3D(diffP[i,:,s].reshape(totalres, 3), initial)[0].reshape(1, totalres*3))[0]) - initial_trans[0]
            del delF

    del initial_trans
    del initial
    del corr_mat


    log.info("Calculating Pearson's correlations coefficient...\n")

    DTarget = numpy.zeros(totalres)
    DIFF = numpy.zeros((totalres, totalres, perturbations))
    RHO = numpy.zeros((totalres, perturbations))

    for i in range(0, totalres):
        DTarget[i] = sqrt(diffE[3*(i+1)-3]**2 + diffE[3*(i+1)-2]**2 + diffE[3*(i+1)-1]**2)

    for j in range(0, perturbations):
        for i in range(0, totalres):
            for k in range(0, totalres):
                DIFF[k,i,j] = sqrt((diffP[i, 3*(k+1)-3, j]**2) + (diffP[i, 3*(k+1)-2, j]**2) + (diffP[i, 3*(k+1)-1, j]**2))

    del diffP

    for i in range(0, perturbations):
        for j in range(0, totalres):
            RHO[j,i] = numpy.corrcoef(numpy.transpose(DIFF[:,j,i]), DTarget)[0,1]

    del DIFF
    del DTarget

    maxRHO = numpy.zeros(totalres)
    for i in range(0, totalres):
        maxRHO[i] = numpy.amax(abs(RHO[i,:]))

    numpy.savetxt("%s.csv" % args.prefix, maxRHO, delimiter=",", header=args.prefix)

    del maxRHO