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 RMSD matrix calculations; '
34 'only for cpu_omp', default=1)
36 '--subunit',
'-su', dest=
"subunit",
37 help=
'calculate RMSD/sampling and cluster precision/densities '
38 'etc over this subunit only', default=
None)
40 '--align',
'-a', dest=
"align",
41 help=
'boolean flag to allow superposition of models',
42 default=
False, action=
'store_true')
44 '--ambiguity',
'-amb', dest=
"symmetry_groups",
45 help=
'file containing symmetry groups', default=
None)
47 '--scoreA',
'-sa', dest=
"scoreA",
48 help=
'name of the file having the good-scoring scores for sample A',
49 default=
"scoresA.txt")
51 '--scoreB',
'-sb', dest=
"scoreB",
52 help=
'name of the file having the good-scoring scores for sample B',
53 default=
"scoresB.txt")
55 '--rmfA',
'-ra', dest=
"rmf_A",
56 help=
'RMF file with conformations from Sample A', default=
None)
58 '--rmfB',
'-rb', dest=
"rmf_B",
59 help=
'RMF file with conformations from Sample B', default=
None)
61 '--gridsize',
'-g', dest=
"gridsize", type=float,
62 help=
'grid size for calculating sampling precision', default=10.0)
64 '--skip',
'-s', dest=
"skip_sampling_precision",
65 help=
"This option will bypass the calculation of sampling "
66 "precision. This option needs to be used with the clustering "
67 "threshold option. Otherwise by default, sampling precision "
68 "is calculated and the clustering threshold is the "
69 "calculated sampling precision.", default=
False,
72 '--cluster_threshold',
'-ct', dest=
"cluster_threshold", type=float,
73 help=
'final clustering threshold to visualize clusters. Assumes '
74 'that the user has previously calculated sampling precision '
75 'and wants clusters defined at a threshold higher than the '
76 'sampling precision for ease of analysis (lesser number of '
77 'clusters).', default=30.0)
79 '--voxel',
'-v', dest=
"voxel", type=float,
80 help=
'voxel size for the localization densities', default=5.0)
82 '--density_threshold',
'-dt', type=float,
83 dest=
"density_threshold",
84 help=
'threshold for localization densities', default=20.0)
86 '--density',
'-d', dest=
"density",
87 help=
'file containing dictionary of density custom ranges',
90 '--gnuplot',
'-gp', dest=
"gnuplot",
91 help=
"plotting automatically with gnuplot", default=
False,
94 '--selection',
'-sn', dest=
"selection",
95 help=
'file containing dictionary'
96 'of selected subunits and residues'
97 'for RMSD and clustering calculation'
98 "each entry in the dictionary takes the form"
99 "'selection name': [(residue_start, residue_end, protein name)",
101 return parser.parse_args()
104 def make_cluster_centroid(infname, frame, outfname, cluster_index,
105 cluster_size, precision, density, path):
109 if hasattr(RMF.NodeHandle,
'replace_child'):
110 print(infname, outfname)
111 inr = RMF.open_rmf_file_read_only(infname)
112 outr = RMF.create_rmf_file(outfname)
113 cpf = RMF.ClusterProvenanceFactory(outr)
114 RMF.clone_file_info(inr, outr)
115 RMF.clone_hierarchy(inr, outr)
116 RMF.clone_static_frame(inr, outr)
117 inr.set_current_frame(RMF.FrameID(frame))
119 RMF.clone_loaded_frame(inr, outr)
120 rn = outr.get_root_node()
121 children = rn.get_children()
122 if len(children) == 0:
125 prov = [c
for c
in rn.get_children()
if c.get_type() == RMF.PROVENANCE]
130 newp = rn.replace_child(
131 prov,
"cluster.%d" % cluster_index, RMF.PROVENANCE)
133 cp.set_members(cluster_size)
134 cp.set_precision(precision)
135 cp.set_density(os.path.abspath(density))
139 print(infname, frame, outfname)
140 subprocess.call([
'rmf_slice', path + infname,
'-f', str(frame),
154 from IMP.sampcon import scores_convergence, clustering_rmsd
155 from IMP.sampcon import rmsd_calculation, precision_rmsd
159 idfile_A =
"Identities_A.txt"
160 idfile_B =
"Identities_B.txt"
166 with open(os.path.join(args.path, args.scoreA),
'r') as f:
168 score_A.append(float(line.strip(
"\n")))
170 with open(os.path.join(args.path, args.scoreB),
'r') as f:
172 score_B.append(float(line.strip(
"\n")))
174 scores = score_A + score_B
177 scores_convergence.get_top_scorings_statistics(scores, 0, args.sysname)
180 scores_convergence.get_scores_distributions_KS_Stats(
181 score_A, score_B, 100, args.sysname)
184 if args.extension ==
"pdb":
187 conforms, masses, radii, models_name = \
188 rmsd_calculation.get_pdbs_coordinates(
189 args.path, idfile_A, idfile_B)
191 args.extension =
"rmf3"
192 if args.selection
is not None:
193 rmsd_custom_ranges = \
194 precision_rmsd.parse_custom_ranges(args.selection)
196 rmsd_custom_ranges =
None
198 if args.rmf_A
is not None:
199 (ps_names, masses, radii, conforms, symm_groups, models_name,
200 n_models) = rmsd_calculation.get_rmfs_coordinates_one_rmf(
201 args.path, args.rmf_A, args.rmf_B, args.subunit,
202 args.symmetry_groups,
208 (ps_names, masses, radii, conforms,
209 models_name) = rmsd_calculation.get_rmfs_coordinates(
210 args.path, idfile_A, idfile_B, args.subunit,
211 selection=rmsd_custom_ranges)
213 print(
"Size of conformation matrix", conforms.shape)
215 if not args.skip_sampling_precision:
218 numpy.save(
"conforms", conforms)
219 inner_data = rmsd_calculation.get_rmsds_matrix(
220 conforms, args.mode, args.align, args.cores, symm_groups)
221 print(
"Size of RMSD matrix (flattened):", inner_data.shape)
223 conforms = numpy.load(
"conforms.npy")
224 os.unlink(
'conforms.npy')
226 from pyRMSD.matrixHandler
import MatrixHandler
227 mHandler = MatrixHandler()
228 mHandler.loadMatrix(
"Distances_Matrix.data")
230 rmsd_matrix = mHandler.getMatrix()
231 distmat = rmsd_matrix.get_data()
233 distmat_full = sp.spatial.distance.squareform(distmat)
234 print(
"Size of RMSD matrix (unpacked, N x N):", distmat_full.shape)
237 if args.rmf_A
is not None:
238 sampleA_all_models = list(range(n_models[0]))
239 sampleB_all_models = list(range(n_models[0],
240 n_models[1] + n_models[0]))
241 total_num_models = n_models[1] + n_models[0]
244 sampleB_all_models) = clustering_rmsd.get_sample_identity(
246 total_num_models = len(sampleA_all_models) + len(sampleB_all_models)
247 all_models = list(sampleA_all_models) + list(sampleB_all_models)
248 print(
"Size of Sample A:", len(sampleA_all_models),
249 " ; Size of Sample B: ", len(sampleB_all_models),
250 "; Total", total_num_models)
252 if not args.skip_sampling_precision:
254 print(
"Calculating sampling precision")
258 gridSize = args.gridsize
261 cutoffs_list = clustering_rmsd.get_cutoffs_list(distmat, gridSize)
262 print(
"Clustering at thresholds:", cutoffs_list)
265 pvals, cvs, percents = clustering_rmsd.get_clusters(
266 cutoffs_list, distmat_full, all_models, total_num_models,
267 sampleA_all_models, sampleB_all_models, args.sysname)
271 (sampling_precision, pval_converged, cramersv_converged,
272 percent_converged) = clustering_rmsd.get_sampling_precision(
273 cutoffs_list, pvals, cvs, percents)
276 with open(
"%s.Sampling_Precision_Stats.txt"
277 % args.sysname,
'w+')
as fpv:
278 print(
"The sampling precision is defined as the largest allowed "
279 "RMSD between the cluster centroid and a ", args.sysname,
280 "model within any cluster in the finest clustering for "
281 "which each sample contributes models proportionally to "
282 "its size (considering both significance and magnitude of "
283 "the difference) and for which a sufficient proportion of "
284 "all models occur in sufficiently large clusters. The "
285 "sampling precision for our ", args.sysname,
286 " modeling is %.3f" % (sampling_precision),
" A.", file=fpv)
288 print(
"Sampling precision, P-value, Cramer's V and percentage "
289 "of clustered models below:", file=fpv)
290 print(
"%.3f\t%.3f\t%.3f\t%.3f"
291 % (sampling_precision, pval_converged, cramersv_converged,
292 percent_converged), file=fpv)
295 final_clustering_threshold = sampling_precision
298 final_clustering_threshold = args.cluster_threshold
301 print(
"Clustering at threshold %.3f" % final_clustering_threshold)
302 (cluster_centers, cluster_members) = clustering_rmsd.precision_cluster(
303 distmat_full, total_num_models, final_clustering_threshold)
305 (ctable, retained_clusters) = clustering_rmsd.get_contingency_table(
306 len(cluster_centers), cluster_members, all_models,
307 sampleA_all_models, sampleB_all_models)
308 print(
"Contingency table:", ctable)
310 with open(
"%s.Cluster_Population.txt" % args.sysname,
'w+')
as fcp:
311 for rows
in range(len(ctable)):
312 print(rows, ctable[rows][0], ctable[rows][1], file=fcp)
315 density_custom_ranges = precision_rmsd.parse_custom_ranges(args.density)
318 fpc = open(
"%s.Cluster_Precision.txt" % args.sysname,
'w+')
322 for i
in range(len(retained_clusters)):
323 clus = retained_clusters[i]
327 conform_0 = conforms[all_models[cluster_members[clus][0]]]
330 if not os.path.exists(
"./cluster.%s" % i):
331 os.mkdir(
"./cluster.%s" % i)
332 os.mkdir(
"./cluster.%s/Sample_A/" % i)
333 os.mkdir(
"./cluster.%s/Sample_B/" % i)
335 shutil.rmtree(
"./cluster.%s" % i)
336 os.mkdir(
"./cluster.%s" % i)
337 os.mkdir(
"./cluster.%s/Sample_A/" % i)
338 os.mkdir(
"./cluster.%s/Sample_B/" % i)
342 gmd1 = precision_rmsd.GetModelDensity(
343 custom_ranges=density_custom_ranges,
344 resolution=args.density_threshold, voxel=args.voxel,
346 gmd2 = precision_rmsd.GetModelDensity(
347 custom_ranges=density_custom_ranges,
348 resolution=args.density_threshold, voxel=args.voxel,
350 gmdt = precision_rmsd.GetModelDensity(
351 custom_ranges=density_custom_ranges,
352 resolution=args.density_threshold, voxel=args.voxel,
356 both_file = open(
'cluster.'+str(i)+
'.all.txt',
'w')
357 sampleA_file = open(
'cluster.'+str(i)+
'.sample_A.txt',
'w')
358 sampleB_file = open(
'cluster.'+str(i)+
'.sample_B.txt',
'w')
363 for pi
in range(len(conform_0)):
372 cluster_precision = 0.0
377 for mem
in cluster_members[clus]:
379 model_index = all_models[mem]
383 if args.symmetry_groups:
384 rmsd, superposed_ps, trans = \
385 precision_rmsd.get_particles_from_superposed_amb(
386 conforms[model_index], conform_0, args.align, ps,
389 rmsd, superposed_ps, trans = \
390 precision_rmsd.get_particles_from_superposed(
391 conforms[model_index], conform_0, args.align,
396 cluster_precision += rmsd
399 gmdt.add_subunits_density(superposed_ps)
400 print(model_index, file=both_file)
402 if model_index
in sampleA_all_models:
404 gmd1.add_subunits_density(superposed_ps)
405 print(model_index, file=sampleA_file)
408 gmd2.add_subunits_density(superposed_ps)
409 print(model_index, file=sampleB_file)
411 cluster_precision /= float(len(cluster_members[clus]) - 1.0)
413 print(
"Cluster precision (average distance to cluster centroid) "
414 "of cluster ", str(i),
" is %.3f" % cluster_precision,
"A",
422 density = gmdt.write_mrc(path=
"./cluster.%s" % i, file_prefix=
"LPD")
423 gmd1.write_mrc(path=
"./cluster.%s/Sample_A/" % i, file_prefix=
"LPD")
424 gmd2.write_mrc(path=
"./cluster.%s/Sample_B/" % i, file_prefix=
"LPD")
427 cluster_center_index = cluster_members[clus][0]
428 if args.rmf_A
is not None:
429 cluster_center_model_id = cluster_center_index
430 if cluster_center_index < n_models[0]:
431 make_cluster_centroid(
432 os.path.join(args.path, args.rmf_A),
433 cluster_center_index,
434 os.path.join(
"cluster.%d" % i,
435 "cluster_center_model.rmf3"),
436 i, len(cluster_members[clus]),
437 cluster_precision, density, args.path)
439 make_cluster_centroid(
440 os.path.join(args.path, args.rmf_B),
441 cluster_center_index - n_models[0],
442 os.path.join(
"cluster.%d" % i,
443 "cluster_center_model.rmf3"),
444 i, len(cluster_members[clus]),
445 cluster_precision, density, args.path)
448 cluster_center_model_id = all_models[cluster_center_index]
449 outfname = os.path.join(
"cluster.%d" % i,
450 "cluster_center_model." + args.extension)
451 if 'rmf' in args.extension:
452 make_cluster_centroid(
453 models_name[cluster_center_model_id], 0, outfname,
454 i, len(cluster_members[clus]),
455 cluster_precision, density, args.path)
457 shutil.copy(models_name[cluster_center_model_id], outfname)
467 for filename
in sorted(glob.glob(os.path.join(gnuplotdir,
"*.plt"))):
468 cmd = [
'gnuplot',
'-e',
'sysname="%s"' % args.sysname, filename]
470 subprocess.check_call(cmd)
473 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.