1 from __future__
import print_function, division
7 from collections
import defaultdict
12 """Take iterable of TempResidues and return compatified string"""
15 idxs = [r.get_index()
for r
in res]
18 cur_range=[idxs[0],idxs[0]]
20 if idx!=cur_range[1]+1:
21 all_ranges.append(cur_range)
24 all_ranges.append(cur_range)
26 for nr,r
in enumerate(all_ranges):
27 ret+=
'%i-%i'%(r[0],r[1])
28 if nr<len(all_ranges)-1:
32 def get_structure(mdl,pdb_fn,chain_id,res_range=None,offset=0,model_num=None,ca_only=False):
33 """read a structure from a PDB file and return a list of residues
34 @param mdl The IMP model
35 @param pdb_fn The file to read
36 @param chain_id Chain ID to read
37 @param res_range Add only a specific set of residues.
38 res_range[0] is the starting and res_range[1] is the ending residue index
39 The ending residue can be "END", that will take everything to the end of the sequence.
41 @param offset Apply an offset to the residue indexes of the PDB file
42 @param model_num Read multi-model PDB and return that model
43 @param ca_only Read only CA atoms (by default, all non-waters are read)
54 if model_num>=len(mhs):
55 raise Exception(
"you requested model num "+str(model_num)+\
56 " but the PDB file only contains "+str(len(mhs))+
" models")
59 if res_range==[]
or res_range
is None:
70 atom_type=IMP.atom.AT_P)
73 if sel_p.get_selected_particles():
74 "WARNING: detected nucleotides. Selecting phosphorous instead of CA"
77 for p
in sel.get_selected_particles():
79 res.set_index(res.get_index() + offset)
82 print(
"WARNING: no residues selected from %s in range %s"
83 % (pdb_fn, res_range))
86 def build_bead(mdl,residues,input_coord=None):
87 """Generates a single bead"""
95 if ds_frag[0] == ds_frag[-1]:
96 rt = residues[0].get_residue_type()
98 h.set_name(
'%i_bead' % (ds_frag[0]))
99 prt.set_name(
'%i_bead' % (ds_frag[0]))
106 ptem.set_radius(radius)
109 h.set_name(
'%i-%i_bead' % (ds_frag[0], ds_frag[-1]))
110 prt.set_name(
'%i-%i_bead' % (ds_frag[0], ds_frag[-1]))
111 h.set_residue_indexes(range(ds_frag[0], ds_frag[-1] + 1))
113 radius = 0.8 * (3.0 / 4.0 / pi * volume) ** (1.0 / 3.0)
114 ptem.set_radius(radius)
118 if tuple(input_coord)
is not None:
119 ptem.set_coordinates(input_coord)
124 def build_necklace(mdl,residues, resolution, input_coord=None):
125 """Generates a string of beads with given length"""
128 out_hiers.append(build_bead(mdl,chunk, input_coord=input_coord))
131 def build_ca_centers(mdl,residues):
132 """Create a bead on the CA position with coarsened size and mass"""
134 for tempres
in residues:
135 residue = tempres.get_hierarchy()
137 rp1.set_name(
"Residue_%i"%residue.get_index())
138 rt = residue.get_residue_type()
150 get_selected_particles()
152 get_selected_particles()
155 central_atom=calpha[0]
159 raise(
"build_ca_centers: weird selection (no Ca, no nucleotide P or ambiguous selection found)")
164 out_hiers.append(this_res)
167 def setup_bead_as_gaussian(mh):
168 """Setup bead as spherical gaussian, using radius as variance"""
169 p = mh.get_particle()
178 def show_representation(node):
182 resolutions = repr.get_resolutions()
183 for r
in resolutions:
184 print(
'---- resolution %i ----' %r)
190 def build_representation(parent,rep,coord_finder):
191 """Create requested representation.
192 For beads, identifies continuous segments and sets up as Representation.
193 If any volume-based representations (e.g.,densities) are requested,
194 will instead create a single Representation node.
195 All reps are added as children of the passed parent.
196 @param parent The Molecule to which we'll add add representations
197 @param rep What to build. An instance of pmi::topology::_Representation
198 @param coord_finder A _FindCloseStructure object to help localize beads
203 mdl = parent.get_model()
204 if rep.color
is not None:
205 if type(rep.color)
is float:
207 elif type(rep.color)
is str:
209 elif hasattr(rep.color,
'__iter__')
and len(rep.color)==3:
214 raise Exception(
"Color must be float or (r,g,b) tuple")
220 primary_resolution = min(rep.bead_resolutions)
225 if rep.density_residues_per_component:
227 num_components = len(rep.residues)//rep.density_residues_per_component+1
228 rep_dict = defaultdict(list)
232 built_reps.append(root_representation)
233 res_nums = [r.get_index()
for r
in rep.residues]
236 density_frag.get_particle().set_name(
"Densities %i"%rep.density_residues_per_component)
239 if os.path.exists(rep.density_prefix+
'.txt')
and not rep.density_force_compute:
243 if len(density_ps)!=num_components
or not os.path.exists(rep.density_prefix+
'.txt')
or rep.density_force_compute:
246 for r
in rep.residues:
258 mass_multiplier=total_mass)
261 if rep.density_voxel_size>0.0:
263 rep.density_voxel_size,fast=
True)
265 for n, d
in enumerate(density_ps):
266 d.set_name(
'Density #%d' % n)
267 density_frag.add_child(d)
268 root_representation.add_representation(density_frag,
270 rep.density_residues_per_component)
274 rsort = sorted(list(rep.residues),key=
lambda r:r.get_index())
276 prev_structure = rsort[0].get_has_structure()
279 for nr,r
in enumerate(rsort):
280 if r.get_index()!=prev_idx+1
or r.get_has_structure()!=prev_structure
or force_break:
281 segments.append(cur_seg)
285 prev_idx = r.get_index()
286 prev_structure = r.get_has_structure()
287 if r.get_index()-1
in rep.bead_extra_breaks:
290 segments.append(cur_seg)
295 for frag_res
in segments:
296 res_nums = [r.get_index()
for r
in frag_res]
297 rrange =
"%i-%i"%(res_nums[0],res_nums[-1])
298 name =
"Frag_"+rrange
300 name_all +=rrange+
','
308 built_reps.append(this_representation)
309 for resolution
in rep.bead_resolutions:
312 this_resolution.set_name(
"%s: Res %i"%(name,resolution))
313 if frag_res[0].get_has_structure():
315 if resolution==atomic_res:
316 for residue
in frag_res:
317 this_resolution.add_child(residue.get_hierarchy())
318 elif resolution==ca_res
and rep.bead_ca_centers:
319 beads = build_ca_centers(mdl,frag_res)
321 this_resolution.add_child(bead)
324 for residue
in frag_res:
325 tempc.add_child(IMP.atom.create_clone(residue.hier))
327 for bead
in beads.get_children():
328 this_resolution.add_child(bead)
333 input_coord = coord_finder.find_nearest_coord(min(r.get_index()
for r
in frag_res))
334 if input_coord
is None:
335 input_coord = rep.bead_default_coord
336 beads = build_necklace(mdl,
341 this_resolution.add_child(bead)
351 rep_dict[resolution]+=this_resolution.get_children()
353 if resolution==primary_resolution:
354 this_representation.add_child(this_resolution)
356 this_representation.add_representation(this_resolution,
360 if rep.setup_particles_as_densities:
362 setup_bead_as_gaussian(p)
363 this_resolution.set_name(this_resolution.get_name()+
' Densities %i'%resolution)
364 this_representation.add_representation(this_resolution,
369 root_representation.set_name(name_all.strip(
',')+
": Base")
370 d = root_representation.get_representations(IMP.atom.DENSITIES)
371 d[0].set_name(
'%s: '%name_all + d[0].get_name())
372 for resolution
in rep.bead_resolutions:
375 [r.get_index()
for r
in rep.residues])
376 this_resolution.set_name(
"%s: Res %i"%(name_all,resolution))
377 for hier
in rep_dict[resolution]:
378 this_resolution.add_child(hier)
379 if resolution==primary_resolution:
380 root_representation.add_child(this_resolution)
382 root_representation.add_representation(this_resolution,
double get_volume_from_residue_type(ResidueType rt)
Return an estimate for the volume of a given residue.
static Gaussian setup_particle(Model *m, ParticleIndex pi)
void show_molecular_hierarchy(Hierarchy h)
Print out the molecular hierarchy.
static Fragment setup_particle(Model *m, ParticleIndex pi)
double get_mass(const Selection &s)
Get the total mass of a hierarchy, in Daltons.
static XYZR setup_particle(Model *m, ParticleIndex pi)
Select atoms which are selected by both selectors.
double get_mass(ResidueType c)
Get the mass from the residue type.
Color get_rgb_color(double f)
Return the color for f from the RGB color map.
double get_mass_from_number_of_residues(unsigned int num_aa)
Estimate the mass of a protein from the number of amino acids.
double get_ball_radius_from_volume_3d(double volume)
Return the radius of a sphere with a given volume.
static Residue setup_particle(Model *m, ParticleIndex pi, ResidueType t, int index, int insertion_code)
static Representation setup_particle(Model *m, ParticleIndex pi)
GenericHierarchies get_leaves(Hierarchy mhd)
Get all the leaves of the bit of hierarchy.
void read_pdb(TextInput input, int model, Hierarchy h)
A Gaussian distribution in 3D.
A decorator for a representation.
double get_volume_from_mass(double m, ProteinDensityReference ref=ALBER)
Estimate the volume of a protein from its mass.
Ints get_index(const ParticlesTemp &particles, const Subset &subset, const Subsets &excluded)
A decorator for a particle representing an atom.
static Mass setup_particle(Model *m, ParticleIndex pi, Float mass)
PDBSelector * get_default_pdb_selector()
A decorator for a particle with x,y,z coordinates.
static Colored setup_particle(Model *m, ParticleIndex pi, Color color)
A decorator for a residue.
static bool get_is_setup(const IMP::ParticleAdaptor &p)
Hierarchies read_multimodel_pdb(TextInput input, Model *model, PDBSelector *selector=get_default_pdb_selector())
Hierarchy create_simplified_along_backbone(Chain input, const IntRanges &residue_segments, bool keep_detailed=false)
Rotation3D get_identity_rotation_3d()
Return a rotation that does not do anything.
Class to handle individual particles of a Model object.
Select all CA ATOM records.
Python classes to represent, score, sample and analyze models.
Functionality for loading, creating, manipulating and scoring atomic structures.
static Chain setup_particle(Model *m, ParticleIndex pi, std::string id)
An exception for an invalid value being passed to IMP.
Select hierarchy particles identified by the biological name.
Select all ATOM and HETATM records with the given chain ids.
A decorator for a particle with x,y,z coordinates and a radius.