Ejemplo n.º 1
0
    print "align_atom_indices:", align_atom_indices
    trajs.superpose(reference=trajs[0], frame=0, atom_indices=align_atom_indices)
    print "Alignment done."
# ===========================================================================

# ===========================================================================
# Just keep the atoms in atom indices, remove other atoms
atom_indices = np.loadtxt('atom_indices', dtype=np.int32).tolist()
print "atom_indices:", atom_indices
trajs_sub_atoms = trajs.atom_slice(atom_indices, inplace=False) #just keep the the atoms in atom indices
print "Trajs:", trajs
print "Sub_atoms_trajs:", trajs_sub_atoms
# ===========================================================================
# do Clustering using KCenters method
#cluster = KCenters(n_clusters=n_clusters, metric="euclidean", random_state=0)
cluster = KCenters(n_clusters=n_clusters, metric="rmsd", random_state=0)
print cluster
#cluster.fit(phi_psi)
cluster.fit(trajs_sub_atoms)

labels = cluster.labels_
print labels
n_microstates = len(set(labels)) - (1 if -1 in labels else 0)
print('Estimated number of clusters: %d' % n_microstates)

cluster_centers_ = cluster.cluster_centers_
# plot micro states
clustering_name = "kcenters_n_" + str(n_microstates)
np.savetxt("assignments_"+clustering_name+".txt", labels, fmt="%d")
np.savetxt("cluster_centers_"+clustering_name+".txt", cluster_centers_, fmt="%d")
trajs[cluster_centers_].save("cluster_centers.pdb")
# ===========================================================================
# Just keep the atoms in atom indices, remove other atoms
atom_indices = np.loadtxt('atom_indices', dtype=np.int32).tolist()
print "atom_indices:", atom_indices
trajs_sub_atoms = trajs.atom_slice(atom_indices, inplace=False) #just keep the the atoms in atom indices
print "Trajs:", trajs
print "Sub_atoms_trajs:", trajs_sub_atoms

# ===========================================================================
# Reading Clustering Centers
centers = md.load("cluster_centers_sub_atoms.pdb")
print "Centers:", centers
# ===========================================================================
# do Assigning using KCenters method
#cluster = KCenters(n_clusters=n_clusters, metric="euclidean", random_state=0)
cluster = KCenters(centers=centers, n_clusters=n_clusters, metric="rmsd", random_state=0)
print cluster
#cluster.fit(phi_psi)
#cluster.fit(trajs_sub_atoms)
cluster.assign(trajs_sub_atoms, cluster)

labels = cluster.labels_
print labels
n_microstates = len(set(labels)) - (1 if -1 in labels else 0)
print('Estimated number of clusters: %d' % n_microstates)

# plot micro states
clustering_name = "kcenters_assign_n_" + str(n_microstates)
np.savetxt("assignments_"+clustering_name+".txt", labels, fmt="%d")