1 from __future__
import print_function
6 import pyRMSD.RMSDCalculator
9 def parse_custom_ranges(ranges_file):
12 with open(ranges_file)
as fh:
15 return d[
'density_custom_ranges']
18 def get_particles_from_superposed(
19 cluster_conform_i, cluster_conform_0, align, ps, trans):
20 def _to_vector3ds(numpy_array):
26 calculator = pyRMSD.RMSDCalculator.RMSDCalculator(
27 "QCP_SERIAL_CALCULATOR",
28 numpy.array([cluster_conform_0, cluster_conform_i]))
30 calculator = pyRMSD.RMSDCalculator.RMSDCalculator(
31 "NOSUP_SERIAL_CALCULATOR",
32 numpy.array([cluster_conform_0, cluster_conform_i]))
34 rmsd, superposed_fit = calculator.pairwise(
35 0, 1, get_superposed_coordinates=
True)
42 _to_vector3ds(superposed_fit[0]), _to_vector3ds(cluster_conform_0))
44 for particle_index
in range(len(superposed_fit[1])):
49 return rmsd, ps, trans
52 def get_particles_from_superposed_amb(
53 cluster_conform_i, cluster_conform_0, align, ps, trans, symm_groups):
55 '''Modified superposed function to work with symmetric copies'''
57 def _to_vector3ds(numpy_array):
64 superposed_final_coords = []
66 for perm
in pyRMSD.symmTools.symm_permutations(symm_groups):
69 new_cluster_conform_i = cluster_conform_i
73 for [particle0, particle1]
in sg:
76 if particle0 > particle1:
77 pyRMSD.symmTools.swap_atoms(
78 new_cluster_conform_i, particle0, particle1)
81 calculator = pyRMSD.RMSDCalculator.RMSDCalculator(
82 "QCP_SERIAL_CALCULATOR",
83 numpy.array([cluster_conform_0, new_cluster_conform_i]))
85 calculator = pyRMSD.RMSDCalculator.RMSDCalculator(
86 "NOSUP_SERIAL_CALCULATOR",
87 numpy.array([cluster_conform_0, new_cluster_conform_i]))
89 rmsd, superposed_fit = calculator.pairwise(
90 0, 1, get_superposed_coordinates=
True)
94 superposed_final_coords = superposed_fit
102 _to_vector3ds(superposed_final_coords[0]),
103 _to_vector3ds(cluster_conform_0))
105 for particle_index
in range(len(superposed_final_coords[1])):
110 superposed_final_coords[1][particle_index]))
112 return min_rmsd, ps, trans
116 """Compute mean density maps from structures.
117 Keeps a dictionary of density maps,
118 keys are in the custom ranges. When you call add_subunits_density, it adds
119 particle coordinates to the existing density maps.
122 def __init__(self, custom_ranges=None, resolution=20.0, voxel=5.0,
125 @param list of particles decorated with mass, radius, and XYZ
126 @param resolution The MRC resolution of the output map
128 @param voxel The voxel size for the output map (lower is slower)
131 self.MRCresolution = resolution
133 self.count_models = 0.0
135 self.bead_names = bead_names
136 self.custom_ranges = custom_ranges
140 self.particle_indices_in_custom_ranges = {}
142 for density_name
in self.custom_ranges:
143 self.particle_indices_in_custom_ranges[density_name] = []
146 for index, beadname
in enumerate(self.bead_names):
147 for density_name
in self.custom_ranges:
149 for domain
in self.custom_ranges[density_name]:
150 if self._is_contained(beadname, domain):
151 self.particle_indices_in_custom_ranges[
152 density_name].append(index)
155 def normalize_density(self):
158 def _create_density_from_particles(self, ps, name,
159 kernel_type=
'GAUSSIAN'):
160 '''Internal function for adding to densities.
161 pass XYZR particles with mass and create a density from them.
162 kernel type options are GAUSSIAN, BINARIZED_SPHERE, and SPHERE.'''
165 dmap.set_was_used(
True)
167 if name
not in self.densities:
168 self.densities[name] = dmap
174 dmap3.set_was_used(
True)
176 dmap3.add(self.densities[name])
177 self.densities[name] = dmap3
179 def _is_contained(self, bead_name, domain):
180 """ domain can be the name of a single protein or a tuple
181 (start_residue,end_residue,protein_name)
182 bead is a string of type moleculeName_startResidue_endResidue
185 (bead_protein, bead_res_start,
186 bead_res_end, bead_copy) = bead_name.split(
"_")
189 if isinstance(domain, tuple):
190 domain_protein = domain[2]
192 domain_protein = domain
194 if "." in domain_protein:
195 spl = domain_protein.split(
".")
196 domain_protein = spl[0]
197 domain_copy = int(spl[1])
199 domain_copy = bead_copy = -1
201 if bead_protein != domain_protein
or int(bead_copy) != domain_copy:
205 if isinstance(domain, tuple):
206 bead_residues = set(range(int(bead_res_start),
207 int(bead_res_end)+1))
208 domain_residues = set(range(int(domain[0]),
210 return not domain_residues.isdisjoint(bead_residues)
215 """Add a frame to the densities.
216 @param ps List of particles decorated with XYZR and Mass.
218 self.count_models += 1.0
220 particles_custom_ranges = {}
221 for density_name
in self.custom_ranges:
222 particles_custom_ranges[density_name] = []
225 for density_name
in self.custom_ranges:
227 in self.particle_indices_in_custom_ranges[density_name]:
228 particles_custom_ranges[density_name].append(
232 for density_name
in self.custom_ranges:
233 self._create_density_from_particles(
234 particles_custom_ranges[density_name], density_name)
236 def get_density_keys(self):
237 return list(self.densities.keys())
240 """Get the current density for some component name"""
241 if name
not in self.densities:
244 return self.densities[name]
246 def write_mrc(self, path=".", file_prefix=""):
247 for density_name
in self.densities:
248 mrc = os.path.join(path, file_prefix +
"_" + density_name +
".mrc")
249 self.densities[density_name].
multiply(1. / self.count_models)
251 self.densities[density_name], mrc,
253 if len(self.densities) == 1:
256 return os.path.join(path, file_prefix +
"_*.mrc")
def get_density
Get the current density for some component name.
Compute mean density maps from structures.
Class for sampling a density map from particles.
DensityMap * multiply(const DensityMap *m1, const DensityMap *m2)
Return a density map for which voxel i contains the result of m1[i]*m2[i].
DensityMap * create_density_map(const IMP::algebra::GridD< 3, S, V, E > &arg)
Create a density map from an arbitrary IMP::algebra::GridD.
def add_subunits_density
Add a frame to the densities.
Basic utilities for handling cryo-electron microscopy 3D density maps.
A decorator for a particle with x,y,z coordinates.
algebra::BoundingBoxD< 3 > get_bounding_box(const DensityMap *m)
Transformation3D get_transformation_aligning_first_to_second(Vector3Ds a, Vector3Ds b)