IMP logo
IMP Reference Guide  develop.63b38c487d,2024/12/22
The Integrative Modeling Platform
flexfit.py
1 import os
2 import typing
3 import pathlib
4 
5 import RMF
6 import IMP.atom
7 
8 
9 def read_angle_file(
10  hier: IMP.atom.Hierarchy,
11  flex_dict: dict
12 ) -> typing.Tuple[
13  typing.List[IMP.atom.Residue],
14  typing.List[IMP.atom.Bond]
15 ]:
16  flexible_residues = list()
17  bonds = list()
18  for fr in flex_dict["Flexible residues"]:
19  chain_id = fr['chain_identifier']
20  residue_id = fr['residue_seq_number']
21  sel = IMP.atom.Selection(
22  hierarchy=hier,
23  chain_ids=[chain_id],
24  residue_indexes=[residue_id]
25  )
26  flexible_residues.append(
27  sel.get_selected_particles(False)[0]
28  )
29  for bnd in flex_dict["Bonds"]:
30  a1 = bnd[0]
31  a2 = bnd[1]
32  sel1 = IMP.atom.Selection(
33  hierarchy=hier,
34  chain_ids=[a1['chain_identifier']],
35  residue_indexes=[a1['residue_seq_number']],
36  atom_type=IMP.atom.AtomType(a1['atom_name'])
37  )
38  sel2 = IMP.atom.Selection(
39  hierarchy=hier,
40  chain_ids=[a2['chain_identifier']],
41  residue_indexes=[a2['residue_seq_number']],
42  atom_type=IMP.atom.AtomType(a2['atom_name'])
43  )
44  p1 = sel1.get_selected_particles()[0]
45  p2 = sel2.get_selected_particles()[0]
46  b1 = IMP.atom.Bonded(p1)
47  b2 = IMP.atom.Bonded(p2)
48  bonds.append(
49  IMP.atom.create_bond(b1, b2, IMP.atom.Bond.SINGLE)
50  )
51  return flexible_residues, bonds
52 
53 
54 class WriteRMFFrame(IMP.OptimizerState):
55 
56  def __init__(
57  self,
58  filename,
59  root_hier: IMP.atom.Hierarchy,
60  restraints: typing.List[IMP.Restraint],
61  name: str = "WriteRMFFrame"
62  ):
63  model = root_hier.get_model()
64  super().__init__(model, name)
65  self.restraints = restraints
66  fileName, fileExtension = os.path.splitext(filename)
67  fn = pathlib.Path(fileName + ".0.rmf3")
68  if pathlib.Path(fn).exists():
69  for i in range(100):
70  fn = pathlib.Path(fileName + ".{:d}.rmf3".format(i))
71  if not fn.exists():
72  break
73  self._rmf_filename = str(fn)
74  self._rh = RMF.create_rmf_file(str(fn))
75  IMP.rmf.add_hierarchies(self._rh, [root_hier])
76  IMP.rmf.add_restraints(self._rh, restraints)
77  IMP.rmf.save_frame(self._rh)
78 
79  def do_update(self, arg0):
80  #print(*[r.evaluate(False) for r in self.restraints], sep="\t")
81  IMP.rmf.save_frame(self._rh)
82 
83 
84 class WritePDBFrame(IMP.OptimizerState):
85 
86  def __init__(
87  self,
88  filename,
89  root_hier: IMP.atom.Hierarchy,
90  restraints: typing.List[IMP.Restraint],
91  restraint_filename: str = None,
92  name: str = "WriteDCDFrame",
93  output_objects: typing.List = None,
94  multi_state: bool = False
95  ):
96  model = root_hier.get_model()
97  super().__init__(model, name)
98 
99  import os
100  self._pdb_basename = filename
101  if restraint_filename is None:
102  restraint_filename = os.path.splitext(filename)[0] + ".rst.txt"
103  self._restraint_filename = restraint_filename
104  self.restraints = restraints
105  self._hier = root_hier
106  self.frame = 0
107  self.output_objects = output_objects
108  self.multi_state = multi_state
109 
110  def do_update(self, arg0):
111  with open(self._restraint_filename, "a+") as fp:
112  fp.write("%s\t" % self.frame)
113  fp.write("\t".join(["{:.3f}".format(r.evaluate(False)) for r in self.restraints]))
114  fp.write("\t")
115  if isinstance(self.output_objects, list):
116  for obj in self.output_objects:
117  fp.write(str(obj) + "\t")
118  fp.write("\n")
119  lead = os.path.splitext(self._pdb_basename)[0]
120  if not self.multi_state:
121  hiers = [self._hier]
122  else:
123  hiers = self._hier.get_children()
124  for i, hier in enumerate(hiers):
125  out_fn = lead + "_state_" + str(i) + "_" + "{:04d}".format(self.frame) + ".pdb"
126  IMP.atom.write_pdb(hier, out=out_fn)
127  self.frame += 1
RMF::FrameID save_frame(RMF::FileHandle file, std::string name="")
Save the current state of the linked objects as a new RMF frame.
A decorator for a particle which has bonds.
void write_pdb(const Selection &mhd, TextOutput out, unsigned int model=1)
The type of an atom.
Bond create_bond(Bonded a, Bonded b, Bond o)
Connect the two wrapped particles by a custom bond.
The standard decorator for manipulating molecular structures.
A decorator for wrapping a particle representing a molecular bond.
void add_hierarchies(RMF::NodeHandle fh, const atom::Hierarchies &hs)
void add_restraints(RMF::NodeHandle fh, const Restraints &hs)
A decorator for a residue.
Definition: Residue.h:137
Shared optimizer state that is invoked upon commitment of new coordinates.
Functionality for loading, creating, manipulating and scoring atomic structures.
Select hierarchy particles identified by the biological name.
Definition: Selection.h:70
A restraint is a term in an IMP ScoringFunction.
Definition: Restraint.h:56