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 '--resolution',
'-r', dest=
"resolution", type=int,
37 help=
'resolution at which to select proteins in a multiscale system',
40 '--subunit',
'-su', dest=
"subunit",
41 help=
'calculate RMSD/sampling and cluster precision/densities '
42 'etc over this subunit only', default=
None)
44 '--align',
'-a', dest=
"align",
45 help=
'boolean flag to allow superposition of models',
46 default=
False, action=
'store_true')
48 '--ambiguity',
'-amb', dest=
"symmetry_groups",
49 help=
'file containing symmetry groups', default=
None)
51 '--scoreA',
'-sa', dest=
"scoreA",
52 help=
'name of the file having the good-scoring scores for sample A',
53 default=
"scoresA.txt")
55 '--scoreB',
'-sb', dest=
"scoreB",
56 help=
'name of the file having the good-scoring scores for sample B',
57 default=
"scoresB.txt")
59 '--rmfA',
'-ra', dest=
"rmf_A",
60 help=
'RMF file with conformations from Sample A', default=
None)
62 '--rmfB',
'-rb', dest=
"rmf_B",
63 help=
'RMF file with conformations from Sample B', default=
None)
65 '--gridsize',
'-g', dest=
"gridsize", type=float,
66 help=
'grid size for calculating sampling precision', default=10.0)
68 '--skip',
'-s', dest=
"skip_sampling_precision",
69 help=
"This option will bypass the calculation of sampling "
70 "precision. This option needs to be used with the clustering "
71 "threshold option. Otherwise by default, sampling precision "
72 "is calculated and the clustering threshold is the "
73 "calculated sampling precision.", default=
False,
76 '--cluster_threshold',
'-ct', dest=
"cluster_threshold", type=float,
77 help=
'final clustering threshold to visualize clusters. Assumes '
78 'that the user has previously calculated sampling precision '
79 'and wants clusters defined at a threshold higher than the '
80 'sampling precision for ease of analysis (lesser number of '
81 'clusters).', default=30.0)
83 '--voxel',
'-v', dest=
"voxel", type=float,
84 help=
'voxel size for the localization densities', default=5.0)
86 '--density_threshold',
'-dt', type=float,
87 dest=
"density_threshold",
88 help=
'threshold for localization densities', default=20.0)
90 '--density',
'-d', dest=
"density",
91 help=
'file containing dictionary of density custom ranges',
94 '--gnuplot',
'-gp', dest=
"gnuplot",
95 help=
"plotting automatically with gnuplot", default=
False,
98 '--selection',
'-sn', dest=
"selection",
99 help=
'file containing dictionary'
100 'of selected subunits and residues'
101 'for RMSD and clustering calculation'
102 "each entry in the dictionary takes the form"
103 "'selection name': [(residue_start, residue_end, protein name)",
105 return parser.parse_args()
108 def make_cluster_centroid(infname, frame, outfname, cluster_index,
109 cluster_size, precision, density, path):
113 if hasattr(RMF.NodeHandle,
'replace_child'):
114 print(infname, outfname)
115 inr = RMF.open_rmf_file_read_only(infname)
116 outr = RMF.create_rmf_file(outfname)
117 cpf = RMF.ClusterProvenanceFactory(outr)
118 RMF.clone_file_info(inr, outr)
119 RMF.clone_hierarchy(inr, outr)
120 RMF.clone_static_frame(inr, outr)
121 inr.set_current_frame(RMF.FrameID(frame))
123 RMF.clone_loaded_frame(inr, outr)
124 rn = outr.get_root_node()
125 children = rn.get_children()
126 if len(children) == 0:
129 prov = [c
for c
in rn.get_children()
if c.get_type() == RMF.PROVENANCE]
134 newp = rn.replace_child(
135 prov,
"cluster.%d" % cluster_index, RMF.PROVENANCE)
137 cp.set_members(cluster_size)
138 cp.set_precision(precision)
139 cp.set_density(os.path.abspath(density))
143 print(infname, frame, outfname)
144 subprocess.call([
'rmf_slice', path + infname,
'-f', str(frame),
158 from IMP.sampcon import scores_convergence, clustering_rmsd
159 from IMP.sampcon import rmsd_calculation, precision_rmsd
163 idfile_A =
"Identities_A.txt"
164 idfile_B =
"Identities_B.txt"
170 with open(os.path.join(args.path, args.scoreA),
'r') as f:
172 score_A.append(float(line.strip(
"\n")))
174 with open(os.path.join(args.path, args.scoreB),
'r') as f:
176 score_B.append(float(line.strip(
"\n")))
178 scores = score_A + score_B
181 scores_convergence.get_top_scorings_statistics(scores, 0, args.sysname)
184 scores_convergence.get_scores_distributions_KS_Stats(
185 score_A, score_B, 100, args.sysname)
188 if args.extension ==
"pdb":
191 conforms, masses, radii, models_name = \
192 rmsd_calculation.get_pdbs_coordinates(
193 args.path, idfile_A, idfile_B)
195 args.extension =
"rmf3"
196 if args.selection
is not None:
197 rmsd_custom_ranges = \
198 precision_rmsd.parse_custom_ranges(args.selection)
200 rmsd_custom_ranges =
None
202 if args.rmf_A
is not None:
203 (ps_names, masses, radii, conforms, symm_groups, models_name,
204 n_models) = rmsd_calculation.get_rmfs_coordinates_one_rmf(
205 args.path, args.rmf_A, args.rmf_B, args.subunit,
206 args.symmetry_groups,
213 (ps_names, masses, radii, conforms,
214 models_name) = rmsd_calculation.get_rmfs_coordinates(
215 args.path, idfile_A, idfile_B, args.subunit,
216 selection=rmsd_custom_ranges,
217 resolution=args.resolution)
219 print(
"Size of conformation matrix", conforms.shape)
221 if not args.skip_sampling_precision:
224 numpy.save(
"conforms", conforms)
225 inner_data = rmsd_calculation.get_rmsds_matrix(
226 conforms, args.mode, args.align, args.cores, symm_groups)
227 print(
"Size of RMSD matrix (flattened):", inner_data.shape)
229 conforms = numpy.load(
"conforms.npy")
230 os.unlink(
'conforms.npy')
232 from pyRMSD.matrixHandler
import MatrixHandler
233 mHandler = MatrixHandler()
234 mHandler.loadMatrix(
"Distances_Matrix.data")
236 rmsd_matrix = mHandler.getMatrix()
237 distmat = rmsd_matrix.get_data()
239 distmat_full = sp.spatial.distance.squareform(distmat)
240 print(
"Size of RMSD matrix (unpacked, N x N):", distmat_full.shape)
243 if args.rmf_A
is not None:
244 sampleA_all_models = list(range(n_models[0]))
245 sampleB_all_models = list(range(n_models[0],
246 n_models[1] + n_models[0]))
247 total_num_models = n_models[1] + n_models[0]
250 sampleB_all_models) = clustering_rmsd.get_sample_identity(
252 total_num_models = len(sampleA_all_models) + len(sampleB_all_models)
253 all_models = list(sampleA_all_models) + list(sampleB_all_models)
254 print(
"Size of Sample A:", len(sampleA_all_models),
255 " ; Size of Sample B: ", len(sampleB_all_models),
256 "; Total", total_num_models)
258 if not args.skip_sampling_precision:
260 print(
"Calculating sampling precision")
264 gridSize = args.gridsize
267 cutoffs_list = clustering_rmsd.get_cutoffs_list(distmat, gridSize)
268 print(
"Clustering at thresholds:", cutoffs_list)
271 pvals, cvs, percents = clustering_rmsd.get_clusters(
272 cutoffs_list, distmat_full, all_models, total_num_models,
273 sampleA_all_models, sampleB_all_models, args.sysname)
277 (sampling_precision, pval_converged, cramersv_converged,
278 percent_converged) = clustering_rmsd.get_sampling_precision(
279 cutoffs_list, pvals, cvs, percents)
282 with open(
"%s.Sampling_Precision_Stats.txt"
283 % args.sysname,
'w+')
as fpv:
284 print(
"The sampling precision is defined as the largest allowed "
285 "RMSD between the cluster centroid and a ", args.sysname,
286 "model within any cluster in the finest clustering for "
287 "which each sample contributes models proportionally to "
288 "its size (considering both significance and magnitude of "
289 "the difference) and for which a sufficient proportion of "
290 "all models occur in sufficiently large clusters. The "
291 "sampling precision for our ", args.sysname,
292 " modeling is %.3f" % (sampling_precision),
" A.", file=fpv)
294 print(
"Sampling precision, P-value, Cramer's V and percentage "
295 "of clustered models below:", file=fpv)
296 print(
"%.3f\t%.3f\t%.3f\t%.3f"
297 % (sampling_precision, pval_converged, cramersv_converged,
298 percent_converged), file=fpv)
301 final_clustering_threshold = sampling_precision
304 final_clustering_threshold = args.cluster_threshold
307 print(
"Clustering at threshold %.3f" % final_clustering_threshold)
308 (cluster_centers, cluster_members) = clustering_rmsd.precision_cluster(
309 distmat_full, total_num_models, final_clustering_threshold)
311 (ctable, retained_clusters) = clustering_rmsd.get_contingency_table(
312 len(cluster_centers), cluster_members, all_models,
313 sampleA_all_models, sampleB_all_models)
314 print(
"Contingency table:", ctable)
316 with open(
"%s.Cluster_Population.txt" % args.sysname,
'w+')
as fcp:
317 for rows
in range(len(ctable)):
318 print(rows, ctable[rows][0], ctable[rows][1], file=fcp)
321 density_custom_ranges = precision_rmsd.parse_custom_ranges(args.density)
324 fpc = open(
"%s.Cluster_Precision.txt" % args.sysname,
'w+')
328 for i
in range(len(retained_clusters)):
329 clus = retained_clusters[i]
333 conform_0 = conforms[all_models[cluster_members[clus][0]]]
336 if not os.path.exists(
"./cluster.%s" % i):
337 os.mkdir(
"./cluster.%s" % i)
338 os.mkdir(
"./cluster.%s/Sample_A/" % i)
339 os.mkdir(
"./cluster.%s/Sample_B/" % i)
341 shutil.rmtree(
"./cluster.%s" % i)
342 os.mkdir(
"./cluster.%s" % i)
343 os.mkdir(
"./cluster.%s/Sample_A/" % i)
344 os.mkdir(
"./cluster.%s/Sample_B/" % i)
348 gmd1 = precision_rmsd.GetModelDensity(
349 custom_ranges=density_custom_ranges,
350 resolution=args.density_threshold, voxel=args.voxel,
352 gmd2 = precision_rmsd.GetModelDensity(
353 custom_ranges=density_custom_ranges,
354 resolution=args.density_threshold, voxel=args.voxel,
356 gmdt = precision_rmsd.GetModelDensity(
357 custom_ranges=density_custom_ranges,
358 resolution=args.density_threshold, voxel=args.voxel,
362 both_file = open(
'cluster.'+str(i)+
'.all.txt',
'w')
363 sampleA_file = open(
'cluster.'+str(i)+
'.sample_A.txt',
'w')
364 sampleB_file = open(
'cluster.'+str(i)+
'.sample_B.txt',
'w')
369 for pi
in range(len(conform_0)):
378 cluster_precision = 0.0
383 for mem
in cluster_members[clus]:
385 model_index = all_models[mem]
389 if args.symmetry_groups:
390 rmsd, superposed_ps, trans = \
391 precision_rmsd.get_particles_from_superposed_amb(
392 conforms[model_index], conform_0, args.align, ps,
395 rmsd, superposed_ps, trans = \
396 precision_rmsd.get_particles_from_superposed(
397 conforms[model_index], conform_0, args.align,
402 cluster_precision += rmsd
405 gmdt.add_subunits_density(superposed_ps)
406 print(model_index, file=both_file)
408 if model_index
in sampleA_all_models:
410 gmd1.add_subunits_density(superposed_ps)
411 print(model_index, file=sampleA_file)
414 gmd2.add_subunits_density(superposed_ps)
415 print(model_index, file=sampleB_file)
417 cluster_precision /= float(len(cluster_members[clus]) - 1.0)
419 print(
"Cluster precision (average distance to cluster centroid) "
420 "of cluster ", str(i),
" is %.3f" % cluster_precision,
"A",
428 density = gmdt.write_mrc(path=
"./cluster.%s" % i, file_prefix=
"LPD")
429 gmd1.write_mrc(path=
"./cluster.%s/Sample_A/" % i, file_prefix=
"LPD")
430 gmd2.write_mrc(path=
"./cluster.%s/Sample_B/" % i, file_prefix=
"LPD")
433 cluster_center_index = cluster_members[clus][0]
434 if args.rmf_A
is not None:
435 cluster_center_model_id = cluster_center_index
436 if cluster_center_index < n_models[0]:
437 make_cluster_centroid(
438 os.path.join(args.path, args.rmf_A),
439 cluster_center_index,
440 os.path.join(
"cluster.%d" % i,
441 "cluster_center_model.rmf3"),
442 i, len(cluster_members[clus]),
443 cluster_precision, density, args.path)
445 make_cluster_centroid(
446 os.path.join(args.path, args.rmf_B),
447 cluster_center_index - n_models[0],
448 os.path.join(
"cluster.%d" % i,
449 "cluster_center_model.rmf3"),
450 i, len(cluster_members[clus]),
451 cluster_precision, density, args.path)
454 cluster_center_model_id = all_models[cluster_center_index]
455 outfname = os.path.join(
"cluster.%d" % i,
456 "cluster_center_model." + args.extension)
457 if 'rmf' in args.extension:
458 make_cluster_centroid(
459 models_name[cluster_center_model_id], 0, outfname,
460 i, len(cluster_members[clus]),
461 cluster_precision, density, args.path)
463 shutil.copy(models_name[cluster_center_model_id], outfname)
473 for filename
in sorted(glob.glob(os.path.join(gnuplotdir,
"*.plt"))):
474 cmd = [
'gnuplot',
'-e',
'sysname="%s"' % args.sysname, filename]
476 subprocess.check_call(cmd)
479 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.