Debian Codegraph @

python/dynamic_atom_overlaps_and_other_outliers.py_origish

raw
1 #
2 def make_quick_test_validation_info(imol):
3
4 def find_rama_baddies():
5 rs = all_molecule_ramachandran_score(imol)
6 # print "rs:", rs
7 scored_residues = rs[5]
8 interesting = filter(lambda item: item[2] < 0.02, scored_residues)
9
10 # remove phi-psi and res-names from the return value
11 munged = map(lambda item: [item[1], item[2]], interesting)
12 return ["Ramachandran Improbables", munged]
13
14 # a list of atom specs
15 def find_chiral_volume_baddies():
16 r = chiral_volume_errors(imol)
17 if not isinstance(r, list):
18 return []
19 else:
20 return r
21
22 def find_c_beta_baddies():
23 try:
24 c_beta_deviations
25 return c_beta_deviations(imol)
26 except:
27 return []
28
29 def rotamer_score_residues(imol):
30
31 residues = all_residues_sans_water(imol)
32 ret = map(lambda residue_spec: [residue_spec,
33 rotamer_score(imol,
34 residue_spec_to_chain_id(residue_spec),
35 residue_spec_to_res_no(residue_spec),
36 residue_spec_to_ins_code(residue_spec),
37 "")],
38 residues)
39 return ret
40
41 def filter_rotamer_baddies(baddies):
42
43 het_groups_in_mol = het_group_residues(imol)
44
45 ret = []
46
47 for baddie in baddies:
48 spec, score = baddie
49 res_name = residue_name(imol,
50 residue_spec_to_chain_id(spec),
51 residue_spec_to_res_no(spec),
52 residue_spec_to_ins_code(spec))
53
54 # print "filter-rotamers testing baddie:", baddie
55 if res_name in ["ALA", "GLY", "UNK", "HOH"]:
56 pass
57 else:
58 # if spec is a het-group then no rotamers for that (return False)
59 is_het = any(map(lambda item: residue_specs_match_qm(item, spec),
60 het_groups_in_mol))
61 if is_het:
62 pass
63 else:
64 if res_name == "LEU":
65 # there is something strange with LEU rotamers, D 427?
66 if score < 1.1:
67 ret += [baddie]
68 else:
69 if score < 0.021:
70 ret += [baddie]
71 return ret
72
73 def molecule_atom_overlap_baddies():
74 return molecule_atom_overlaps(imol)
75
76 def filter_molecule_atom_overlap_baddies(mao_baddies):
77 baddie_limit = 2.0 # more than this is marked as a baddie, was 2.2. Is 2.0 good?
78 return filter(lambda mao_item: mao_item['overlap-volume'] > baddie_limit, mao_baddies)
79
80 def non_pro_cis_peptide_baddies(): # do the filter here - just for consistency
81 cis_peps = cis_peptides(imol)
82 return filter(lambda peptide: "PRO" !=
83 residue_spec_to_residue_name(imol,
84 peptide[1]),
85 cis_peps)
86
87 def sort_buttons_inner(buttons_with_spec):
88 def sort_fn(b_1, b_2):
89 spec_1 = b_1[0]
90 spec_2 = b_2[0]
91 return residue_spec_less_than(b_1, b_2)
92
93 if buttons_with_spec:
94 ret = sorted(buttons_with_spec, key=lambda x: (x[0][0], x[0][1]))
95 else:
96 ret = []
97 return ret
98
99 def sort_buttons(buttons):
100 def remove_from_list(ls):
101 del ls[0][0]
102 return ls
103 buttons_1 = map(lambda button: button if len(button[0]) == 3
104 else remove_from_list(button), buttons)
105 buttons_2 = sort_buttons_inner(buttons_1)
106 return buttons_2
107
108 def twisted_trans_peptide_baddies():
109 return twisted_trans_peptides(imol)
110
111 def destroy_buttons_with_label(label_fragment_txt, dialog_vbox):
112 current_buttons = dialog_vbox.get_children()
113 for button in current_buttons:
114 label = button.get_label()
115 if label_fragment_txt in label:
116 button.destroy()
117
118 # now do something....
119 frb = find_rama_baddies()
120 fcbb = find_c_beta_baddies()
121 maob = molecule_atom_overlap_baddies()
122 filtered_mao_baddies = filter_molecule_atom_overlap_baddies(maob)
123
124 # rama
125 baddies = filter(lambda baddie: baddie[1] < 0.002, frb[1])
126 def get_rama_prob(val):
127 return val[1]
128 baddies.sort(key=get_rama_prob)
129 sorted_filtered_rama_baddies = baddies
130
131 # c-beta
132 c_beta_baddies = filter(lambda baddie: baddie[1][0][1] > 0.25, fcbb)
133 def get_c_beta_score(val):
134 return val[1][0][1]
135 c_beta_baddies.sort(key=get_c_beta_score, reverse=True)
136 sorted_filtered_c_beta_baddies = c_beta_baddies
137
138 # Rotamers
139 #
140 rotamer_baddies = rotamer_score_residues(imol)
141 filtered_rotamer_baddies = filter_rotamer_baddies(rotamer_baddies)
142 def get_rotamer_score(val):
143 if val[1] == 0.0:
144 # score things with score 0.0 (meaning missing sidechain)
145 # as if they are better than low probability outliers
146 # give high score in sorting
147 return 10
148 else:
149 return val[1]
150 sorted_filtered_rotamer_baddies = filtered_rotamer_baddies.sort(key=get_rotamer_score)
151
152 rama_buttons = []
153 for baddie in sorted_filtered_rama_baddies:
154 spec, rama_prob = baddie
155 baddie_score = 0.5
156 score_string = '{:6.2f} %'.format(100 * rama_prob)
157 button_label = "Ramachandran Outlier " + \
158 residue_spec_to_chain_id(spec) + \
159 " " + \
160 str(residue_spec_to_res_no(spec)) + \
161 residue_spec_to_ins_code(spec) + \
162 " " + \
163 residue_spec_to_residue_name(imol, spec) + \
164 " " + \
165 score_string
166 rama_buttons.append([spec, baddie_score, button_label,
167 [[set_go_to_atom_molecule, imol],
168 [set_go_to_atom_from_res_spec, spec]]])
169
170 c_beta_buttons = []
171 for baddie in sorted_filtered_c_beta_baddies:
172 spec = baddie[0]
173 score = baddie[1][0][1] # only the first score
174 score_string = '{:6.2f}'.format(score)
175 baddie_score = 0.5
176 button_label = "C-beta deviant " + \
177 residue_spec_to_string(spec) + \
178 " " + \
179 residue_spec_to_residue_name(imol, spec) + \
180 " " + \
181 score_string + u'\u212B'.encode('utf-8')
182 c_beta_buttons.append([spec, baddie_score, button_label,
183 [[set_go_to_atom_molecule, imol],
184 [set_go_to_atom_from_res_spec, spec]]])
185
186 non_pro_cis_peptide_buttons = []
187 for baddie in non_pro_cis_peptide_baddies():
188 spec_1 = baddie[0]
189 spec_2 = baddie[1]
190 omega = baddie[2]
191 baddie_score = 0.5
192 button_label = "Non-PRO cis-peptide " + \
193 residue_spec_to_string(spec_1) + \
194 " - " + \
195 residue_spec_to_string(spec_2)
196 non_pro_cis_peptide_buttons.append([spec_1, baddie_score, button_label,
197 [[set_go_to_atom_molecule, imol],
198 [set_go_to_atom_from_res_spec, spec_1]]])
199
200 twisted_trans_peptide_buttons = []
201 for baddie in twisted_trans_peptide_baddies():
202 spec_1 = baddie[0]
203 spec_2 = baddie[1]
204 omega = baddie[2]
205 baddie_score = 1.0
206 button_label = "Twisted trans-peptide " + \
207 residue_spec_to_string(spec_1) + \
208 " - " + \
209 residue_spec_to_string(spec_2)
210 twisted_trans_peptide_buttons.append([spec_1, baddie_score, button_label,
211 [[set_go_to_atom_molecule, imol],
212 [set_go_to_atom_from_res_spec, spec_1]]])
213
214 rota_buttons = []
215 for baddie in sorted_filtered_rama_baddies:
216 spec, score = baddie
217
218 # Paul is not sure that he likes a score of
219 # 0.0 meaning "Missing sidechain"
220 # we have lost some information on the way
221 #
222 score_string = '{:6.2f} %'.format(score)
223 ms_string = "Missing Sidechain" if score == 0.0 else "Rotamer Outlier"
224 baddie_score = 0.5
225 rot_name = get_rotamer_name(imol,
226 residue_spec_to_chain_id(spec),
227 residue_spec_to_res_no(spec),
228 residue_spec_to_ins_code(spec))
229 button_label = ms_string + " " + \
230 residue_spec_to_string(spec) + \
231 " " + \
232 residue_spec_to_residue_name(imol, spec) + \
233 " "
234 button_label += rot_name if isinstance(rot_name, str) else " "
235 button_label += "" if score == 0.0 else score_string
236 rota_buttons.append([spec, baddie_score, button_label,
237 [[set_go_to_atom_molecule, imol],
238 [set_go_to_atom_from_res_spec, spec]]])
239
240 chiral_volume_buttons = []
241 for baddie_atom_spec_6 in find_chiral_volume_baddies():
242 # strip off leading, incorrect imol
243 baddie_atom_spec = baddie_atom_spec_6[1:]
244 baddie_score = 1.0
245 residue_spec = atom_spec_to_residue_spec(baddie_atom_spec)
246 button_label = "Chiral Volume Error " + \
247 atom_spec_to_string(baddie_atom_spec)
248 chiral_volume_buttons.append([residue_spec, baddie_score, button_label,
249 [[set_go_to_atom_molecule, imol],
250 [set_go_to_atom_from_atom_spec, baddie_atom_spec]]])
251
252 atom_overlap_buttons = []
253 for baddie in filtered_mao_baddies:
254 atom_spec_1 = baddie['atom-1-spec']
255 atom_spec_2 = baddie['atom-2-spec']
256 overlap = baddie['overlap-volume']
257 baddie_score = 0.5
258 residue_spec = atom_spec_to_residue_spec(atom_spec_1)
259 button_label = "Atom Overlap " + \
260 atom_spec_to_string(atom_spec_1) + \
261 " on " + \
262 atom_spec_to_string(atom_spec_2) + \
263 " OV: " + \
264 '{:5.2f}'.format(overlap)
265 atom_overlap_buttons.append([residue_spec, baddie_score, button_label,
266 [[set_go_to_atom_molecule, imol],
267 [set_go_to_atom_from_atom_spec, atom_spec_1]]])
268
269
270 # This gives a list in "baddie-type" order.
271 # If we want a list in Chain/Residue order,
272 # each baddie now is associated with (prefixed by)
273 # a residue spec - and use those to sort residues.
274
275 # these buttons have 3 fields - spec label func
276
277 buttons = chiral_volume_buttons + \
278 rama_buttons + \
279 rota_buttons + \
280 non_pro_cis_peptide_buttons + \
281 twisted_trans_peptide_buttons + \
282 c_beta_buttons + \
283 atom_overlap_buttons
284
285 sorted_buttons = sort_buttons(buttons)
286 sorted_buttons.reverse()
287 return sorted_buttons
288
289 # find bad things in the structure - rama, C-beta, rotamer, atom clashes baddies
290 #
291 global dialog_vbox, window, missing_sidechains_checkbutton
292 dialog_vbox = False
293 window = False
294 missing_sidechains_checkbutton = False
295
296 def quick_test_validation_outliers_dialog(imol):
297
298 global dialog_vbox, window, missing_sidechains_checkbutton
299
300 def make_window_title(n):
301 return "Coot Interesting/Outliers/Problems: " + str(n)
302
303 def regenerate_button_fn(*args):
304
305 def cb_func(button, callback):
306 for item in callback:
307 item[0](*item[1:])
308
309 if dialog_vbox:
310 buttons = make_quick_test_validation_buttons(imol)
311 old_buttons = dialog_vbox.get_children()
312 for button_spec in buttons:
313 button = gtk.Button(button_spec[0])
314 button.connect("clicked", cb_func, button_spec[1])
315 dialog_vbox.pack_start(button, False, False, 2)
316 button.show()
317 if window:
318 window.set_title(make_window_title(len(buttons)))
319 for butt in old_buttons:
320 butt.destroy()
321 else:
322 quick_test_validation_outliers_dialog(imol)
323
324 def ok_to_have_missing_sidechain_buttons_qm():
325 if missing_sidechains_checkbutton:
326 return missing_sidechains_checkbutton.get_active()
327 else:
328 return True
329
330 def missing_sidechains_checkbutton_toggled(widget):
331 state = widget.get_active()
332 if not state:
333 # i.e. no buttons with "Missing Sidechain"
334 # Mmmh, really?!?
335 destroy_buttons_with_label("Missing Sidechain", dialog_vbox)
336
337 # main line
338
339 # return the update button so that the caller can emit a "clicked" event
340 buttons = make_quick_test_validation_buttons(imol)
341
342 dialog_vbox, window = dialog_box_of_buttons(make_window_title(len(buttons)),
343 [360, 400], buttons, " Close ")
344
345 window_bits = window.get_children()
346 vbox_outer = window_bits[0]
347 control_button_vbox_1 = gtk.HBox(False, 2)
348 missing_sidechains_checkbutton_local = gtk.CheckButton("Missing Sidechains")
349 regenerate_button_local = gtk.Button("Update")
350
351 missing_sidechains_checkbutton = missing_sidechains_checkbutton_local
352
353 vbox_outer.pack_start(regenerate_button_local, False, False, 6)
354 vbox_outer.pack_start(control_button_vbox_1, False, False, 2)
355 regenerate_button_local.connect("clicked", regenerate_button_fn)
356 missing_sidechains_checkbutton.connect("toggled",
357 missing_sidechains_checkbutton_toggled)
358
359 control_button_vbox_1.show()
360 missing_sidechains_checkbutton.show()
361 regenerate_button_local.show()
362 return regenerate_button_local
363
364 def make_quick_test_validation_buttons(imol):
365 i = make_quick_test_validation_info(imol)
366 # remove the spec (and the baddie score for now)
367 return map(lambda x: x[2:], i)
368
369 if (have_coot_python):
370 if coot_python.main_menubar():
371 menu = coot_menubar_menu("Validate")
372
373 def make_quick_test_validation_dialog_func():
374 with UsingActiveAtom() as [aa_imol, aa_chain_id, aa_res_no,
375 aa_ins_code, aa_atom_name, aa_alt_conf]:
376 update_button = quick_test_validation_outliers_dialog(aa_imol)
377 global post_manipulation_script
378 def post_manipulation_script(*args):
379 update_button.emit("clicked")
380
381 add_simple_coot_menu_menuitem(
382 menu, "Overlaps, Peptides, CBeta, Rama & Rota Outliers",
383 lambda func: make_quick_test_validation_dialog_func()
384 )
385
386