1 from __future__
import print_function
2 from IMP
import ArgumentParser
5 __doc__ =
"Perform analysis to determine sampling convergence."
16 parser = ArgumentParser(
17 description=
"First stages of analysis for assessing sampling "
20 '--sysname',
'-n', dest=
"sysname",
21 help=
'name of the system', default=
"")
23 '--path',
'-p', dest=
"path",
24 help=
'path to the good-scoring models', default=
"./")
26 '--extension',
'-e', dest=
"extension",
27 help=
'extension of the file', choices=[
'rmf',
'pdb'], default=
"rmf")
29 '--mode',
'-m', dest=
"mode", help=
'pyRMSD calculator',
30 choices=[
'cuda',
'cpu_omp',
'cpu_serial'], default=
"cuda")
32 '--cores',
'-c', dest=
"cores", type=int,
33 help=
'number of cores for parallel clustering at '
34 'different thresholds and RMSD matrix calculations; '
35 'only for cpu_omp', default=1)
37 '--resolution',
'-r', dest=
"resolution", type=int,
38 help=
'resolution at which to select proteins in a multiscale system',
41 '--subunit',
'-su', dest=
"subunit",
42 help=
'calculate RMSD/sampling and cluster precision/densities '
43 'etc over this subunit only', default=
None)
45 '--align',
'-a', dest=
"align",
46 help=
'boolean flag to allow superposition of models',
47 default=
False, action=
'store_true')
49 '--ambiguity',
'-amb', dest=
"symmetry_groups",
50 help=
'file containing symmetry groups', default=
None)
52 '--scoreA',
'-sa', dest=
"scoreA",
53 help=
'name of the file having the good-scoring scores for sample A',
54 default=
"scoresA.txt")
56 '--scoreB',
'-sb', dest=
"scoreB",
57 help=
'name of the file having the good-scoring scores for sample B',
58 default=
"scoresB.txt")
60 '--rmfA',
'-ra', dest=
"rmf_A",
61 help=
'RMF file with conformations from Sample A', default=
None)
63 '--rmfB',
'-rb', dest=
"rmf_B",
64 help=
'RMF file with conformations from Sample B', default=
None)
66 '--gridsize',
'-g', dest=
"gridsize", type=float,
67 help=
'grid size for calculating sampling precision', default=10.0)
69 '--skip',
'-s', dest=
"skip_sampling_precision",
70 help=
"This option will bypass the calculation of sampling "
71 "precision. This option needs to be used with the clustering "
72 "threshold option. Otherwise by default, sampling precision "
73 "is calculated and the clustering threshold is the "
74 "calculated sampling precision.", default=
False,
77 '--cluster_threshold',
'-ct', dest=
"cluster_threshold", type=float,
78 help=
'final clustering threshold to visualize clusters. Assumes '
79 'that the user has previously calculated sampling precision '
80 'and wants clusters defined at a threshold higher than the '
81 'sampling precision for ease of analysis (lesser number of '
82 'clusters).', default=30.0)
84 '--voxel',
'-v', dest=
"voxel", type=float,
85 help=
'voxel size for the localization densities', default=5.0)
87 '--density_threshold',
'-dt', type=float,
88 dest=
"density_threshold",
89 help=
'threshold for localization densities', default=20.0)
91 '--density',
'-d', dest=
"density",
92 help=
'file containing dictionary of density custom ranges',
95 '--gnuplot',
'-gp', dest=
"gnuplot",
96 help=
"plotting automatically with gnuplot", default=
False,
99 '--selection',
'-sn', dest=
"selection",
100 help=
'file containing dictionary'
101 'of selected subunits and residues'
102 'for RMSD and clustering calculation'
103 "each entry in the dictionary takes the form"
104 "'selection name': [(residue_start, residue_end, protein name)",
106 return parser.parse_args()
109 def make_cluster_centroid(infname, frame, outfname, cluster_index,
110 cluster_size, precision, density, path):
114 if hasattr(RMF.NodeHandle,
'replace_child'):
115 print(infname, outfname)
116 inr = RMF.open_rmf_file_read_only(infname)
117 outr = RMF.create_rmf_file(outfname)
118 cpf = RMF.ClusterProvenanceFactory(outr)
119 RMF.clone_file_info(inr, outr)
120 RMF.clone_hierarchy(inr, outr)
121 RMF.clone_static_frame(inr, outr)
122 inr.set_current_frame(RMF.FrameID(frame))
124 RMF.clone_loaded_frame(inr, outr)
125 rn = outr.get_root_node()
126 children = rn.get_children()
127 if len(children) == 0:
130 prov = [c
for c
in rn.get_children()
if c.get_type() == RMF.PROVENANCE]
135 newp = rn.replace_child(
136 prov,
"cluster.%d" % cluster_index, RMF.PROVENANCE)
138 cp.set_members(cluster_size)
139 cp.set_precision(precision)
140 cp.set_density(os.path.abspath(density))
144 print(infname, frame, outfname)
145 subprocess.call([
'rmf_slice', path + infname,
'-f', str(frame),
159 from IMP.sampcon import scores_convergence, clustering_rmsd
160 from IMP.sampcon import rmsd_calculation, precision_rmsd
164 idfile_A =
"Identities_A.txt"
165 idfile_B =
"Identities_B.txt"
171 with open(os.path.join(args.path, args.scoreA),
'r') as f:
173 score_A.append(float(line.strip(
"\n")))
175 with open(os.path.join(args.path, args.scoreB),
'r') as f:
177 score_B.append(float(line.strip(
"\n")))
179 scores = score_A + score_B
182 scores_convergence.get_top_scorings_statistics(scores, 0, args.sysname)
185 scores_convergence.get_scores_distributions_KS_Stats(
186 score_A, score_B, 100, args.sysname)
189 if args.extension ==
"pdb":
192 conforms, masses, radii, models_name = \
193 rmsd_calculation.get_pdbs_coordinates(
194 args.path, idfile_A, idfile_B)
196 args.extension =
"rmf3"
197 if args.selection
is not None:
198 rmsd_custom_ranges = \
199 precision_rmsd.parse_custom_ranges(args.selection)
201 rmsd_custom_ranges =
None
203 if args.rmf_A
is not None:
204 (ps_names, masses, radii, conforms, symm_groups, models_name,
205 n_models) = rmsd_calculation.get_rmfs_coordinates_one_rmf(
206 args.path, args.rmf_A, args.rmf_B,
208 args.symmetry_groups,
216 (ps_names, masses, radii, conforms,
217 models_name) = rmsd_calculation.get_rmfs_coordinates(
218 args.path, idfile_A, idfile_B, args.subunit,
219 selection=rmsd_custom_ranges,
220 resolution=args.resolution)
222 print(
"Size of conformation matrix", conforms.shape)
224 if not args.skip_sampling_precision:
227 numpy.save(
"conforms", conforms)
228 inner_data = rmsd_calculation.get_rmsds_matrix(
229 conforms, args.mode, args.align, args.cores, symm_groups)
230 print(
"Size of RMSD matrix (flattened):", inner_data.shape)
232 conforms = numpy.load(
"conforms.npy")
233 os.unlink(
'conforms.npy')
235 from pyRMSD.matrixHandler
import MatrixHandler
236 mHandler = MatrixHandler()
237 mHandler.loadMatrix(
"Distances_Matrix.data")
239 rmsd_matrix = mHandler.getMatrix()
240 distmat = rmsd_matrix.get_data()
242 distmat_full = sp.spatial.distance.squareform(distmat)
243 print(
"Size of RMSD matrix (unpacked, N x N):", distmat_full.shape)
246 if args.rmf_A
is not None:
247 sampleA_all_models = list(range(n_models[0]))
248 sampleB_all_models = list(range(n_models[0],
249 n_models[1] + n_models[0]))
250 total_num_models = n_models[1] + n_models[0]
253 sampleB_all_models) = clustering_rmsd.get_sample_identity(
255 total_num_models = len(sampleA_all_models) + len(sampleB_all_models)
256 all_models = list(sampleA_all_models) + list(sampleB_all_models)
257 print(
"Size of Sample A:", len(sampleA_all_models),
258 " ; Size of Sample B: ", len(sampleB_all_models),
259 "; Total", total_num_models)
261 if not args.skip_sampling_precision:
263 print(
"Calculating sampling precision")
267 gridSize = args.gridsize
270 cutoffs_list = clustering_rmsd.get_cutoffs_list(distmat, gridSize)
271 print(
"Clustering at thresholds:", cutoffs_list)
274 pvals, cvs, percents = clustering_rmsd.get_clusters(
275 cutoffs_list, distmat_full, all_models, total_num_models,
276 sampleA_all_models, sampleB_all_models, args.sysname,
281 (sampling_precision, pval_converged, cramersv_converged,
282 percent_converged) = clustering_rmsd.get_sampling_precision(
283 cutoffs_list, pvals, cvs, percents)
286 with open(
"%s.Sampling_Precision_Stats.txt"
287 % args.sysname,
'w+')
as fpv:
288 print(
"The sampling precision is defined as the largest allowed "
289 "RMSD between the cluster centroid and a ", args.sysname,
290 "model within any cluster in the finest clustering for "
291 "which each sample contributes models proportionally to "
292 "its size (considering both significance and magnitude of "
293 "the difference) and for which a sufficient proportion of "
294 "all models occur in sufficiently large clusters. The "
295 "sampling precision for our ", args.sysname,
296 " modeling is %.3f" % (sampling_precision),
" A.", file=fpv)
298 print(
"Sampling precision, P-value, Cramer's V and percentage "
299 "of clustered models below:", file=fpv)
300 print(
"%.3f\t%.3f\t%.3f\t%.3f"
301 % (sampling_precision, pval_converged, cramersv_converged,
302 percent_converged), file=fpv)
305 final_clustering_threshold = sampling_precision
308 final_clustering_threshold = args.cluster_threshold
311 print(
"Clustering at threshold %.3f" % final_clustering_threshold)
312 (cluster_centers, cluster_members) = clustering_rmsd.precision_cluster(
313 distmat_full, total_num_models, final_clustering_threshold)
315 (ctable, retained_clusters) = clustering_rmsd.get_contingency_table(
316 len(cluster_centers), cluster_members, all_models,
317 sampleA_all_models, sampleB_all_models)
318 print(
"Contingency table:", ctable)
320 with open(
"%s.Cluster_Population.txt" % args.sysname,
'w+')
as fcp:
321 for rows
in range(len(ctable)):
322 print(rows, ctable[rows][0], ctable[rows][1], file=fcp)
325 density_custom_ranges = precision_rmsd.parse_custom_ranges(args.density)
328 fpc = open(
"%s.Cluster_Precision.txt" % args.sysname,
'w+')
332 for i
in range(len(retained_clusters)):
333 clus = retained_clusters[i]
337 conform_0 = conforms[all_models[cluster_members[clus][0]]]
340 if not os.path.exists(
"./cluster.%s" % i):
341 os.mkdir(
"./cluster.%s" % i)
342 os.mkdir(
"./cluster.%s/Sample_A/" % i)
343 os.mkdir(
"./cluster.%s/Sample_B/" % i)
345 shutil.rmtree(
"./cluster.%s" % i)
346 os.mkdir(
"./cluster.%s" % i)
347 os.mkdir(
"./cluster.%s/Sample_A/" % i)
348 os.mkdir(
"./cluster.%s/Sample_B/" % i)
352 gmd1 = precision_rmsd.GetModelDensity(
353 custom_ranges=density_custom_ranges,
354 resolution=args.density_threshold, voxel=args.voxel,
356 gmd2 = precision_rmsd.GetModelDensity(
357 custom_ranges=density_custom_ranges,
358 resolution=args.density_threshold, voxel=args.voxel,
360 gmdt = precision_rmsd.GetModelDensity(
361 custom_ranges=density_custom_ranges,
362 resolution=args.density_threshold, voxel=args.voxel,
366 both_file = open(
'cluster.'+str(i)+
'.all.txt',
'w')
367 sampleA_file = open(
'cluster.'+str(i)+
'.sample_A.txt',
'w')
368 sampleB_file = open(
'cluster.'+str(i)+
'.sample_B.txt',
'w')
373 for pi
in range(len(conform_0)):
382 cluster_precision = 0.0
387 for mem
in cluster_members[clus]:
389 model_index = all_models[mem]
393 rmsd, superposed_ps, trans = \
394 precision_rmsd.get_particles_from_superposed(
395 conforms[model_index], conform_0, args.align,
396 ps, trans, symm_groups)
400 cluster_precision += rmsd
403 gmdt.add_subunits_density(superposed_ps)
404 print(model_index, file=both_file)
406 if model_index
in sampleA_all_models:
408 gmd1.add_subunits_density(superposed_ps)
409 print(model_index, file=sampleA_file)
412 gmd2.add_subunits_density(superposed_ps)
413 print(model_index, file=sampleB_file)
415 cluster_precision /= float(len(cluster_members[clus]) - 1.0)
417 print(
"Cluster precision (average distance to cluster centroid) "
418 "of cluster ", str(i),
" is %.3f" % cluster_precision,
"A",
426 density = gmdt.write_mrc(path=
"./cluster.%s" % i, file_prefix=
"LPD")
427 gmd1.write_mrc(path=
"./cluster.%s/Sample_A/" % i, file_prefix=
"LPD")
428 gmd2.write_mrc(path=
"./cluster.%s/Sample_B/" % i, file_prefix=
"LPD")
431 cluster_center_index = cluster_members[clus][0]
432 if args.rmf_A
is not None:
433 cluster_center_model_id = cluster_center_index
434 if cluster_center_index < n_models[0]:
435 make_cluster_centroid(
436 os.path.join(args.path, args.rmf_A),
437 cluster_center_index,
438 os.path.join(
"cluster.%d" % i,
439 "cluster_center_model.rmf3"),
440 i, len(cluster_members[clus]),
441 cluster_precision, density, args.path)
443 make_cluster_centroid(
444 os.path.join(args.path, args.rmf_B),
445 cluster_center_index - n_models[0],
446 os.path.join(
"cluster.%d" % i,
447 "cluster_center_model.rmf3"),
448 i, len(cluster_members[clus]),
449 cluster_precision, density, args.path)
452 cluster_center_model_id = all_models[cluster_center_index]
453 outfname = os.path.join(
"cluster.%d" % i,
454 "cluster_center_model." + args.extension)
455 if 'rmf' in args.extension:
456 make_cluster_centroid(
457 models_name[cluster_center_model_id], 0, outfname,
458 i, len(cluster_members[clus]),
459 cluster_precision, density, args.path)
461 shutil.copy(models_name[cluster_center_model_id], outfname)
471 for filename
in sorted(glob.glob(os.path.join(gnuplotdir,
"*.plt"))):
472 cmd = [
'gnuplot',
'-e',
'sysname="%s"' % args.sysname, filename]
474 subprocess.check_call(cmd)
477 if __name__ ==
'__main__':
def get_data_path
Return the full path to one of this module's data files.
static XYZR setup_particle(Model *m, ParticleIndex pi)
static XYZ setup_particle(Model *m, ParticleIndex pi)
Class for storing model, its restraints, constraints, and particles.
static Mass setup_particle(Model *m, ParticleIndex pi, Float mass)
Class to handle individual particles of a Model object.
Sampling exhaustiveness protocol.