def test_with_save_traj(self):
        path = pkg_resources.resource_filename(__name__, 'data') + os.path.sep

        pdb_file = os.path.join(path, 'bpti_ca.pdb')
        traj_files = [
            os.path.join(path, 'bpti_001-033.xtc'),
            os.path.join(path, 'bpti_034-066.xtc'),
            os.path.join(path, 'bpti_067-100.xtc')
        ]

        source_frag = coor.source([traj_files], top=pdb_file)
        full_data = source_frag.get_output()[0]
        last_frame_fragment_0 = [0,32]
        first_frame_fragment_1 = [0,33]
        first_frame_fragment_2 = [0,66]

        reshape = lambda f: f.xyz.reshape((f.xyz.shape[0],f.xyz.shape[1] * f.xyz.shape[2])).squeeze()

        # Frames in the first fragment:
        frames = coor.save_traj(source_frag, [last_frame_fragment_0], None)
        np.testing.assert_equal(reshape(frames), full_data[32])

        # Frames the first and second fragments
        frames = coor.save_traj(source_frag, [last_frame_fragment_0, first_frame_fragment_1], None)
        np.testing.assert_equal(reshape(frames), full_data[np.array([32, 33])])

        # Frames only in the second fragment
        frames = coor.save_traj(source_frag, [first_frame_fragment_1], None)
        np.testing.assert_equal(reshape(frames), full_data[33])

        # Frames only in the second and third fragment
        frames = coor.save_traj(source_frag, [first_frame_fragment_1, first_frame_fragment_2], None)
        np.testing.assert_equal(reshape(frames), full_data[np.array([33, 66])])
Example #2
0
def SaveEVFrames(dt, ev_traj, c, d, traj_inp=None, filename=None, topfile=None, nframes=None):
    ''' Save frames that correspond to eigenvector centers from md-trajectories
    to separate trajectory.
    
    Parameters:
    --------------
    traj_inp: List of underlying md-trajectories.
    ev_traj: List of eigenfunction trajectories.
    dt: Physical time step.
    c: ndarray, shape(nc,M), centers.
    d: ndarray, shape(nc,). admissible distances to the centers.
    filename: str, name of the center-trajectories
    topfile:str, topology-file
    nframes: int, number of frames per center and per trajectory.
    '''
    # Get the number of trajectories:
    ntraj = len(ev_traj)
    # Get the number of centers and eigenfunctions:
    nc, M = c.shape
    # Create a reader of eigenfunction data:
    ef = pco.source(ev_traj)
    ef.chunksize = np.min(ef.trajectory_lengths())
    # Get the output into memory, leaving out the first ef:
    psidata = ef.get_output(dimensions=np.arange(1, M + 1, dtype=int))
    cindices = []
    # Write out frames to a trajectory file:
    # Loop over the centers:
    for i in range(nc):
        # Create a list of possible frames:
        indices = []
        # Loop over the trajectory files:
        for m in range(ntraj):
            # Get the data for this traj:
            mdata = psidata[m]
            # Get the admissible frames for this trajectory:
            mind = np.where(np.any(np.abs(mdata - c[i, :]) <= d[i], axis=1))[0]
            # Make a random selection:
            if not (nframes is None):
                mind = dt * np.random.choice(mind, (nframes,))
            else:
                mind = dt * mind
            # Put the information together:
            mindices = np.zeros((mind.shape[0], 2), dtype=int)
            mindices[:, 0] = m
            mindices[:, 1] = mind
            indices.append(mindices)
        # Save to traj:
        if not (traj_inp is None) and not (filename is None) and not (topfile is None):
            pco.save_traj(traj_inp, indices, outfile=filename + "Center%d.xtc" % i, topfile=topfile)
        cindices.append(indices)
    return cindices
Example #3
0
fig1, ax1 = mplt.plot_free_energy(
    np.vstack(tica1)[:, 0],
    np.vstack(tica1)[:, 2])
plt.scatter(cl.clustercenters[:, 0], cl.clustercenters[:, 2], c='k')
plt.xlabel('tIC 1', fontsize=18)
plt.ylabel('tIC 3', fontsize=18)
plt.savefig('cluster_centers1v3.pdf')
plt.clf()
a = np.arange(0.8, 15.5, 0.1)
b = list(range(num_clu))
ind = cl.sample_indexes_by_cluster(b, 1)
ind_traj = [i[0][0] for i in ind]
short_list = [a[i] for i in ind_traj]
topfile = '/scratch/shn13007/pgfolder/protein.pdb'
## create list of trajectories and Colvar files
traj_list = [indir + "/p60win{:2.1f}.xtc".format(i) for i in short_list]
f = coor.featurizer(topfile)
inp = coor.load(traj_list, f)
inp2 = [i[20000::10] for i in inp]
frame_list = [i[0][1] for i in ind]
#inp3 = inp2.tolist()
#frame_list2 = frame_list.tolist()
#coor.save_traj(inp2, , 'pg_cluster_centers.xtc', top=topfile)
for i, (j, k) in enumerate(zip(inp2, frame_list)):
    coor.save_traj(j,
                   k,
                   'pg_cluster_centers' + str(i) + '_' + str(j) + '_' +
                   str(k) + '.pdb',
                   top=topfile)
Example #4
0
                s=100,
                alpha=(state_1_colors[i]))
    plt.scatter(clkmeans.clustercenters[i, 0],
                clkmeans.clustercenters[i, 1],
                color='cyan',
                s=100,
                alpha=(state_2_colors[i]))
plt.xlabel('tic 1')
plt.ylabel('tic 2')

plt.savefig('hmm_clusters_on_tic1_tic2_fade.png')

hmm_samples = HMM.sample_by_observation_probabilities(100)

for i in range(n_macrostates):
    coor.save_traj(src, hmm_samples[i], './hmm%s_100samples.xtc' % i)
    t = md.load('./hmm%s_100samples.xtc' % i, top=top)
    t[0].save_pdb('./hmm%s_100samples.pdb' % i)

statdist = MSM.stationary_distribution
relative_counts = MSM.count_matrix_active.sum(0) / np.sum(
    MSM.count_matrix_active)

plt.clf()
plt.figure()
plt.scatter(statdist, relative_counts)
plt.xlabel('MSM stationary distribution')
plt.ylabel('Relative counts')

plt.savefig('msm_sanity_check.png')