-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCoot.py
1300 lines (1183 loc) · 45.7 KB
/
Coot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import division
# TODO: lots of Windows fixes
# TODO: separate window for controling visibility of many models?
# this module is designed to be run only by Coot
# Coot 0.6-pre-1 or greater is required!
from random import random
import subprocess
import traceback
import cPickle
import string
import sys
import os
# =============================================================================
# Copy of code from libtbx.utils for handling unicode
# UnicodeDecodeError is passed instead of Sorry
def to_unicode(text, codec='utf8'):
'''
Function for handling text when it is first encountered
Changes str type to unicode type
The input is returned unmodified if it is already unicode
Will convert other types (e.g. int, float) to str
'''
if (isinstance(text, unicode)):
return text
elif (isinstance(text, str)):
new_text = text
try:
new_text = text.decode(codec)
except UnicodeDecodeError:
raise
finally:
return new_text
elif (text is not None):
return unicode(text)
else:
return None
def to_str(text, codec='utf8'):
'''
Function for handling text when it is passed to cctbx functions that expect
the str type
Changes unicode type to str type
The input is returned unmodified if it is already str
Will convert other types (e.g. int, float) to str
'''
if (isinstance(text, str)):
return text
elif (isinstance(text, unicode)):
new_text = text
try:
new_text = text.encode(codec)
except UnicodeDecodeError:
raise
finally:
return new_text
elif (text is not None):
return str(text)
else:
return None
# =============================================================================
#if "COOT_PYTHON_DIR" in os.environ :
# sys.path.append(os.environ["COOT_PYTHON_DIR"])
phenix_dir = to_unicode(os.environ.get("COOT_PHENIX_PATH", None))
phenix_build = to_unicode(os.environ.get("COOT_PHENIX_BUILD_PATH", None))
if (sys.platform != "win32") :
home_dir = to_unicode(os.environ.get("HOME"))
if (phenix_build is not None) :
os.environ["PATH"]+=":%s:%s:%s" % (
os.path.join(phenix_build, "probe", "exe"),
os.path.join(phenix_build, "reduce", "exe"),
os.path.join(phenix_build, "bin"))
else :
home_dir = to_unicode(to_unicode(os.environ.get("HOMEPATH")))
if (phenix_build is not None) :
os.environ["PATH"]+=";%s;%s;%s" % (
os.path.join(phenix_build, "probe", "exe"),
os.path.join(phenix_build, "reduce", "exe"),
os.path.join(phenix_build, "bin"))
import gtk
import gobject
import coot # XXX: coot_utils broken?
try :
import coot_python
except Exception, e :
print "Could not import coot_python module!"
print "PHENIX GUI extensions will be disabled."
class empty (object) :
def main_menubar (self) :
return None
def main_toolbar (self) :
return None
coot_python = empty()
import socket
#socket.setdefaulttimeout(0.01)
from SimpleXMLRPCServer import SimpleXMLRPCServer
from xmlrpclib import ServerProxy
def coot_log (f) :
def log_wrapper (self, *args, **kwds) :
_args = list(args)
_kwds = dict(kwds)
str_args = ", ".join([ str(arg) for arg in _args ])
str_kwds = ", ".join([ "%s=%s" % (kwd, str(val))
for kwd, val in _kwds.iteritems() ])
call_signature = []
if str_args != "" : call_signature.append(str_args)
if str_kwds != "" : call_signature.append(str_kwds)
try :
print "%s.%s(%s)" % (self.__class__.__name__, f.__name__,
", ".join(call_signature))
sys.stdout.flush()
except IOError, e :
pass
return f(self, *args, **kwds)
return log_wrapper
def coot_startup () :
print "Loading PHENIX Coot extensions..."
gui = coot_phenix_interface()
set_console_display_commands_hilights(0, 0, 0)
set_find_hydrogen_torsions(1)
#set_do_probe_dots_on_rotamers_and_chis(1)
#set_do_probe_dots_post_refine(1)
set_rotamer_lowest_probability(0.5)
try :
set_nomenclature_errors_on_read("ignore")
except Exception :
pass
########################################################################
# PHENIX INTERFACE
#--- XML-RPC server object
class coot_xmlrpc_server (SimpleXMLRPCServer) :
def __init__ (self, addr, phenix_interface) :
self.phenix_interface = phenix_interface
SimpleXMLRPCServer.__init__(self, addr, logRequests=0)
def _dispatch (self, method, params) :
if not self.phenix_interface.enable_xmlrpc :
return -1
result = -1
func = None
if hasattr(self.phenix_interface, method) :
func = getattr(self.phenix_interface, method)
elif hasattr(coot, method) :
func = getattr(coot, method)
if not hasattr(func, "__call__") :
print "%s is not a callable object!" % method
else :
try :
result = func(*params)
except Exception, e :
traceback_str = "\n".join(traceback.format_tb(sys.exc_info()[2]))
raise Exception("%s\nOriginal traceback:%s" % (to_str(e), traceback_str))
else :
if result is None :
result = -1
return result
#--- manager for communication with phenix
class coot_phenix_interface (object) :
def __init__ (self) :
self._client_id = 0
self._guis = {}
self._btns = {}
self._menu = None
self._results = {}
self._molprobity_gui = None
self._molprobity_btn = None
self._molprobity_results = None
self._project_list = []
self._current_project = None
self._update_btn = None
self._probe_file = None
self.enable_xmlrpc = True
self.xmlrpc_server = None
self._start_model = None
self._current_model = None
self._current_maps = None
self._compared_models_and_maps = []
self._highlighted_mol = None
self._tmp_dir = None
self.phenix_server = None
self.settings = { "map_colour" : (0.0, 0.5, 1.0),
"diff_map_colour" : (0.0, 1.0, 0.0),
"n_map_colour" : (0.25, 0.0, 1.0),
"n_diff_map_colour" : (0.5, 1.0, 0.0),
"anom_map_colour" : (1.0, 1.0, 0.0),
"iso_diff_map_colour" : (0.5, 0.0, 1.0), }
set_tip_of_the_day_flag(0)
menubar = coot_python.main_menubar()
toolbar = coot_python.main_toolbar()
# add GUI objects
if menubar is not None :
menu = coot_menubar_menu("PHENIX")
#add_simple_coot_menu_menuitem(menu, "AutoBuild...",
# self.launch_autobuild)
add_simple_coot_menu_menuitem(menu, "Reload MolProbity to-do list",
self.reload_molprobity_gui)
#add_simple_coot_menu_menuitem(menu,
# "Reload real-space correlation to-do list", self.reload_rsc_gui)
self._menu = menu
if toolbar is not None :
try :
toolbar.insert(gtk.SeparatorToolItem(), -1)
except Exception, e :
print "Error: %s" % to_str(e)
self.h_button = gtk.ToggleToolButton()
self.h_button.set_label("Hydrogens off")
self.h_button.set_is_important(True)
toolbar.insert(self.h_button, -1)
self.h_button.connect("clicked", self.toggle_hydrogens)
self.h_button.set_active(True)
self.h_button.show()
# start XML-RPC server
if "CCTBX_COOT_PORT" in os.environ :
port = string.atoi(os.environ["CCTBX_COOT_PORT"])
try :
self.xmlrpc_server = coot_xmlrpc_server(("127.0.0.1", port), self)
self.xmlrpc_server.socket.settimeout(0.01)
except Exception, e :
print "Error starting XML-RPC server:"
print str(e)
else :
print "xml-rpc server running on port %d" % port
# timeout used to be set to whatever the Phenix preferences have it as,
# but 250ms seems to be a universally good choice, and much shorter
# intervals screw up the Coot GUI (at least on Windows)
gobject.timeout_add(250, self.timeout_func)
if toolbar is not None :
self._update_btn = gtk.ToggleToolButton()
self._update_btn.set_label("Connected to PHENIX")
self._update_btn.set_is_important(True)
toolbar.insert(self._update_btn, -1)
self._update_btn.connect("clicked", self.toggle_update)
self._update_btn.set_active(True)
self._update_btn.show()
if False : # FIXME #"CCTBX_XMLRPC_PORT" in os.environ :
port = string.atoi(os.environ["CCTBX_XMLRPC_PORT"])
self.phenix_server = ServerProxy(uri="http://127.0.0.1:%d/RPC2" % port)
if self._menu is not None :
add_simple_coot_menu_menuitem(self._menu, "Run phenix.refine...",
self.launch_phenix_refine)
if toolbar is not None :
pass
#phenix_refine_button = gtk.ToolButton(icon_widget=None,
# label="Refine in PHENIX")
#phenix_refine_button.set_is_important(True)
#toolbar.insert(phenix_refine_button, -1)
#phenix_refine_button.connect("clicked", self.launch_phenix_refine)
#phenix_refine_button.show()
#---------------------------------------------------------------------
# EVENT HANDLERS
def toggle_hydrogens (self, *args) :
if self.h_button.get_active() :
self.h_button.set_label("Hydrogens on")
for imol in model_molecule_list() :
set_draw_hydrogens(imol, True)
else :
self.h_button.set_label("Hydrogens off")
for imol in model_molecule_list() :
set_draw_hydrogens(imol, False)
def toggle_update (self, *args) :
if self._update_btn is not None :
if self._update_btn.get_active() :
self._update_btn.set_label("Connected to PHENIX")
self.enable_xmlrpc = True
else :
self._update_btn.set_label("Connect to PHENIX")
self.enable_xmlrpc = False
def launch_phenix_refine (self, *args) :
molecule_chooser_gui("Select model for refinement",
self._launch_phenix_refine)
def _launch_phenix_refine (self, pdb_mol) :
if pdb_mol is not None :
out_file = strip_extension(molecule_name(pdb_mol))
out_file += "_coot.pdb"
write_pdb_file(pdb_mol, out_file)
try :
print "starting phenix.refine via XML-RPC..."
self.phenix_server.Refine_refine_new_model(out_file)
except Exception, e :
print e
else :
show_message(
"""Coot has saved your model as %s and sent it back to PHENIX for \
refinement. Any previously specified non-PDB input files will be left alone. \
You should now return to the phenix.refine window and adjust the settings, \
then click the "Run" button.""" % out_file)
def launch_autobuild (self, *args) :
pass # TODO
# we have to handle requests to remove them from the queue, but if
# self.enable_xmlrpc is False, they will simply be ignored.
def timeout_func (self, *args) :
if self.xmlrpc_server is not None :
self.xmlrpc_server.handle_request()
return True
def reload_molprobity_gui (self, *args) :
if self._results.get("molprobity") is not None :
gui = self._guis.get("molprobity")
if gui is None or gui.window is None :
self.load_molprobity_gui(self._results.get("molprobity"))
def reload_rsc_gui (self, *args) :
pass
#---------------------------------------------------------------------
# XML-RPC methods
@coot_log
def is_alive (self) :
return True
@coot_log
def quit (self) :
gtk.main_quit()
@coot_log
def clear (self) :
pass # TODO: reset everything
def set_client_id (self, client_id) :
self._client_id = client_id
def update_settings (self, settings_pkl) :
self.settings = cPickle.loads(settings_pkl)
def set_projects (self, projects) :
self._project_list = projects.split(";")
def set_current_project (self, project_id) :
self._current_project = project_id
def set_tmp_dir (self, tmp_dir) :
self._tmp_dir = to_unicode(tmp_dir)
@coot_log
def clear_refinement (self) :
self.close_tmp_model()
for object_number in range(number_of_generic_objects()) :
set_display_generic_object(object_number, 0)
molprobity_gui = self._guis.get("molprobity")
if molprobity_gui is not None and molprobity_gui.window is not None :
molprobity_gui.destroy_window()
try :
self._guis.pop("molprobity", None)
self._results.pop("molprobity", None)
except KeyError : # FIXME why does this happen???
pass
return True
def read_pdb_no_recenter (self, file_name) :
file_name = to_str(file_name)
return handle_read_draw_molecule_with_recentre(file_name, 0)
def read_pdb_recenter (self, file_name) :
file_name = to_str(file_name)
return handle_read_draw_molecule_with_recentre(file_name, 1)
def read_pdb_as_trace (self, file_name) :
imol = read_pdb(to_str(file_name))
graphics_to_ca_representation(imol)
return imol
def load_alt_orig_ref_model (self, file_name) :
imol = self.read_pdb_as_trace(file_name)
set_show_unit_cell(imol, 1)
@coot_log
def update_model (self, pdb_out) :
pdb_out = to_unicode(pdb_out)
if not os.path.isfile(pdb_out) :
print "***error: %s not found" % pdb_out
return False
if self._current_model is None :
imol = read_pdb(to_str(pdb_out))
set_molecule_bonds_colour_map_rotation(imol, 140)
self._current_model = imol
else :
clear_and_update_model_molecule_from_file(
self._current_model, to_str(pdb_out))
return True
def get_newest_model (self) :
all_mols = molecule_number_list()
current_mol = None
for imol in old_mols :
if is_valid_model_molecule(imol) :
current_mol = imol
return current_mol
@coot_log
def close_maps (self) :
old_maps = map_molecule_list()
for imol in old_maps :
close_molecule(imol)
return True
def close_existing_map (self, file_name, f_label) :
for imol in map_molecule_list() :
map_name = molecule_name(imol)
fields = map_name.split()
if fields[0] == file_name and fields[1] == f_label :
close_molecule(imol)
@coot_log
def close_all (self, maps=False) :
old_mols = molecule_number_list()
for imol in old_mols :
if (not have_unsaved_changes_p(imol)) :
close_molecule(imol)
else :
print "Molecule %d has been modified, will not close" % imol
print molecule_name(imol)
self._current_model = None
self._start_model = None
if maps :
self.close_maps()
return True
@coot_log
def recenter_and_zoom (self, x, y, z) :
set_rotation_centre(x, y, z)
set_zoom(30)
# rotation_center()
# spin_zoom_trans(axis, nstep, stepsize, zoom_by, x_rel, y_rel, z_rel)
graphics_draw()
def highlight_model_selection (self, selection) :
if self._highlighted_mol is not None :
close_molecule(self._highlighted_mol)
self._highlighted_mol = None
imol = self.get_newest_model()
imol2 = new_molecule_by_atom_selection(imol, "// /")
#additional_representation_by_attributes(imol, chain_id, resno_start,
# resno_end, ins_code, representation_type, bonds_box_type=0,
# bond_width, draw_hydrogens_flag)
#--- phenix.refine stuff
@coot_log
def show_start_model (self, file_name) :
if self._start_model is None :
imol = read_pdb(to_str(file_name))
set_molecule_bonds_colour_map_rotation(imol, 280)
self._start_model = imol
else :
clear_and_update_model_molecule_from_file(
self._start_model, to_str(file_name))
@coot_log
def close_tmp_model (self) :
if self._current_model is not None :
close_molecule(self._current_model)
self._current_model = None
if (self._current_maps is not None) :
for imol in self._current_maps :
close_molecule(imol)
self._current_maps = None
return True
return False
@coot_log
def auto_load_maps (self, map_file, f="2FOFCWT", delf="FOFCWT", phi="PH",
use_filled=True) :
map_file = to_str(map_file)
set_colour_map_rotation_for_map(0)
imol1 = make_and_draw_map(map_file, f, "%s%s" % (phi, f), "", 0, 0)
imol2 = make_and_draw_map(map_file, delf, "%s%s" % (phi, delf), "", 0, 1)
set_scrollable_map(imol1)
return (imol1, imol2)
@coot_log
def auto_load_anom_maps (self, map_file, f="2FOFCWT", fphi="PH",
anomf="ANOM", anomphi="PANOM", use_filled=True) :
map_file = to_str(map_file)
set_colour_map_rotation_for_map(0)
imol1 = make_and_draw_map(map_file, f, "%s%s" % (fphi, f), "", 0, 0)
imol2 = make_and_draw_map(map_file, anomf, anomphi, "", 0, 1)
set_scrollable_map(imol1)
return (imol1, imol2)
@coot_log
def load_phenix_refine_temp_files (self, tmp_dir, run_name) :
tmp_dir = to_unicode(tmp_dir)
run_name = to_unicode(run_name)
if None in [tmp_dir, run_name] or not os.path.isdir(tmp_dir) :
return False
pdb_tmp = os.path.join(tmp_dir, "tmp.refine.pdb")
map_tmp = os.path.join(tmp_dir, "tmp.refine_maps.mtz")
if (not os.path.isfile(pdb_tmp)) or (not os.path.isfile(map_tmp)) :
print "One or more output files not found:"
print " %s" % to_str(pdb_tmp)
print " %s" % to_str(map_tmp)
return False
self.update_model(to_str(pdb_tmp))
self._current_maps = self.auto_load_maps(to_str(map_tmp), phi="PH")
set_colour_map_rotation_for_map(0)
graphics_draw()
add_status_bar_text("Current model and maps for %s" % to_str(run_name))
return True
@coot_log
def load_phenix_refine_final_pdb (self, output_dir, file_base) :
output_dir = to_unicode(output_dir)
file_base = to_unicode(file_base)
output_base = os.path.join(output_dir, file_base)
pdb_out = "%s.pdb" % output_base
if (not os.path.isfile(pdb_out)) :
print "ERROR: refinement output file not found!"
print " expected %s" % pdb_out
print " directory contents:"
subprocess.call("ls -l %s/" % to_str(output_dir))
return -1
pdb_mol = read_pdb(to_str(pdb_out))
set_molecule_bonds_colour_map_rotation(pdb_mol, 30)
return True
@coot_log
def load_phenix_refine_final_files (self, output_dir, file_base) :
output_dir = to_unicode(output_dir)
file_base = to_unicode(file_base)
if (None in [output_dir, file_base]) or (not os.path.isdir(output_dir)) :
return False
if (not os.path.isdir(output_dir)) :
raise RuntimeError("Output directory does not exist!")
self.load_phenix_refine_final_pdb(output_dir, file_base)
output_base = os.path.join(output_dir, file_base)
map_out_old = "%s_map_coeffs.mtz" % output_base
map_out_new = output_base + ".mtz"
map_out = None
if os.path.isfile(map_out_new) :
map_out = map_out_new
elif os.path.isfile(map_out_old) :
map_out = map_out_old
if (map_out is not None) :
self.load_refinement_maps(map_out)
else :
print "ERROR: can't find map coefficients!"
print "listing %s:" % output_dir
for file_name in os.listdir(output_dir) :
print " %s" % file_name
graphics_draw()
add_status_bar_text("Showing refinement results in %s" % to_str(output_dir))
return True
def load_refinement_maps (self, map_file) :
map_file = to_unicode(map_file)
(imol1, imol2) = self.auto_load_maps(map_file, phi="PH")
fcol = self.settings["map_colour"]
dcol = self.settings["diff_map_colour"]
set_map_colour(imol1, *fcol)
set_map_colour(imol2, *dcol)
set_colour_map_rotation_for_map(0)
set_scrollable_map(imol1)
set_imol_refinement_map(imol1)
@coot_log
def load_phenix_refine_final_xn_files (self, output_dir, file_base) :
output_dir = to_unicode(output_dir)
file_base = to_unicode(file_base)
self.load_phenix_refine_final_pdb(output_dir, file_base)
output_base = os.path.join(output_dir, file_base)
map_out_old = "%s_map_coeffs.mtz" % output_base
map_out_new = output_base + ".mtz"
map_out = None
if os.path.isfile(map_out_new) :
map_out = map_out_new
elif os.path.isfile(map_out_old) :
map_out = map_out_old
if (map_out is not None) :
(imol1, imol2) = self.auto_load_maps(map_out, f="2FOFCWT_xray",
delf="FOFCWT_xray", phi="PH")
fcol = self.settings["map_colour"]
dcol = self.settings["diff_map_colour"]
set_map_colour(imol1, *fcol)
set_map_colour(imol2, *dcol)
(imol3, imol4) = self.auto_load_maps(map_out, f="2FOFCWT_neutron",
delf="FOFCWT_neutron", phi="PH")
fcol = self.settings["n_map_colour"]
dcol = self.settings["n_diff_map_colour"]
set_map_colour(imol3, *fcol) #dcol[0], dcol[1], dcol[2]) # purple
set_map_colour(imol4, *dcol)
set_scrollable_map(imol1)
else :
print "Sorry, can't find map coefficients!"
graphics_draw()
add_status_bar_text("Showing refinement results in %s" % to_str(output_dir))
return True
@coot_log
def load_ensemble_refinement (self, file_base) :
file_base = to_unicode(file_base)
pdb_file = file_base + ".pdb"
mtz_file = file_base + ".mtz"
imol = read_pdb(to_str(pdb_file))
graphics_to_b_factor_representation(imol)
self.load_refinement_maps(mtz_file)
@coot_log
def load_molprobity_gui (self, tmp_dir) :
tmp_dir = to_unicode(tmp_dir)
molprobity_gui = self._guis.get("molprobity")
if getattr(molprobity_gui, "window", None) is not None :
molprobity_gui.destroy_window()
# data_file = os.path.join(tmp_dir, "molprobity.pkl")
probe_file = os.path.join(tmp_dir, "probe.txt")
self.load_probe_results(probe_file, force_reload=True)
# self._guis["molprobity"] = coot_molprobity_todo_list_gui(self, data_file)
self._results["molprobity"] = tmp_dir
return True
@coot_log
def load_probe_results (self, probe_file, force_reload=False) :
probe_file = to_unicode(probe_file)
if (self._probe_file is None) or force_reload :
if os.path.isfile(probe_file) :
self._probe_file = probe_file
handle_read_draw_probe_dots_unformatted(to_str(probe_file), 0, 0)
self.show_probe_dots(True, True)
@coot_log
def show_probe_dots (self, show_dots, overlaps_only) :
n_objects = number_of_generic_objects()
sys.stdout.flush()
if show_dots :
for object_number in range(n_objects) :
obj_name = generic_object_name(object_number)
if overlaps_only and not obj_name in ["small overlap", "bad overlap"] :
sys.stdout.flush()
set_display_generic_object(object_number, 0)
else :
set_display_generic_object(object_number, 1)
else :
sys.stdout.flush()
for object_number in range(n_objects) :
set_display_generic_object(object_number, 0)
#--- wizard stuff
@coot_log
def load_solve_map (self, map_file) :
map_file = to_unicode(map_file)
if not os.path.isfile(map_file) :
return False
self.close_existing_map(map_file, "/crystal/dataset/FP")
set_colour_map_rotation_for_map(0)
imol = make_and_draw_map(to_str(map_file), "/crystal/dataset/FP", "/crystal/dataset/PHIB", "/crystal/dataset/FOM", 1, 0)
#set_map_colour(imol, 0, 0.5, 1)
graphics_draw()
return True
@coot_log
def load_resolve_map (self, map_file, difference_map=0, contour_level=1.0,
prefix="/crystal/dataset") :
map_file = to_unicode(map_file)
if not os.path.isfile(map_file) :
return False
f_name = "%s/FP" % prefix
phi_name = "%s/PHIM" % prefix
fom_name = "%s/FOMM" % prefix
self.close_existing_map(map_file, f_name)
set_colour_map_rotation_for_map(0)
imol = make_and_draw_map(to_str(map_file), f_name, phi_name, fom_name, 1,
difference_map)
set_contour_level_in_sigma(imol, contour_level)
#set_map_colour(imol, 0, 0.5, 1)
graphics_draw()
return True
@coot_log
def load_ccp4_style_map (self,
map_file,
difference_map=0,
contour_level=1.0,
prefix="/crystal/dataset") :
map_file = to_unicode(map_file)
if (not os.path.isfile(map_file)) :
return False
self.close_existing_map(map_file, "%s/FWT" % prefix)
set_colour_map_rotation_for_map(0)
imol = make_and_draw_map(to_str(map_file), "%s/FWT" % prefix, "%s/PHWT" % prefix,
"", 0, difference_map)
set_contour_level_in_sigma(imol, contour_level)
graphics_draw()
return True
def load_new_resolve_map (self, *args, **kwds) :
return self.load_ccp4_style_map(*args, **kwds)
@coot_log
def load_autobuild_map (self, map_file) :
map_file = to_unicode(map_file)
return self.load_resolve_map(map_file)
# phenix.phaser (EP), phenix.autosol
@coot_log
def load_phaser_map (self, map_file, phi_name="PHIFWT") :
map_file = to_unicode(map_file)
if not os.path.isfile(map_file) :
return False
set_colour_map_rotation_for_map(0)
map1 = self.load_any_map(map_file, "FWT", phi_name)
graphics_draw()
add_status_bar_text("Showing weighted F(obs) map from Phaser")
return True
# phenix.phaser (MR), phenix.automr
@coot_log
def load_phaser_mr_maps (self, map_file) :
map_file = to_unicode(map_file)
if not os.path.isfile(map_file) :
return False
set_colour_map_rotation_for_map(0)
map1 = self.load_any_map(map_file, "FWT", "PHWT")
map2 = self.load_any_map(map_file, "DELFWT", "PHDELWT", is_diff_map=1)
# set_contour_level_in_sigma(map2, 3.0)
graphics_draw()
add_status_bar_text("Showing 2mFo-DFC and mFo-DFC maps from Phaser")
return True
@coot_log
def load_autobuild_overall_best (self, output_dir) :
self.load_current_overall_best(output_dir)
# phenix.autosol, phenix.autobuild
@coot_log
def load_current_overall_best (self, output_dir) :
output_dir = to_unicode(output_dir)
run_name = os.path.basename(output_dir)
pdb_out = os.path.join(output_dir, "overall_best.pdb")
pdb_out_fallback = os.path.join(output_dir, "working_best.pdb")
map_out = os.path.join(output_dir, "overall_best_denmod_map_coeffs.mtz")
self.close_maps()
if os.path.exists("%s/FINISHED" % output_dir) :
if self._current_model is not None :
close_molecule(self._current_model)
self._current_model = None
if os.path.isfile(pdb_out) :
pdb_mol = read_pdb(to_str(pdb_out))
set_molecule_bonds_colour_map_rotation(pdb_mol, 30)
elif (os.path.isfile(pdb_out_fallback)) :
pdb_mol = read_pdb(to_str(pdb_out_fallback))
set_molecule_bonds_colour_map_rotation(pdb_mol, 30)
elif os.path.isfile(pdb_out) :
self.update_model(pdb_out)
elif (os.path.isfile(pdb_out_fallback)) :
self.update_model(pdb_out_fallback)
self.load_ccp4_style_map(map_out)
if os.path.isfile(pdb_out) :
try :
(r_work, r_free) = extract_phenix_refine_r_factors(to_str(pdb_out))
add_status_bar_text(
"Showing current best map and RESOLVE model from %s (R-free = %s)" %
(to_str(run_name), str(r_free)))
except AssertionError, e :
pass
return True
# phenix.phase_and_build
def load_current_build (self, output_dir, pdb_file, map_file,
program_name="phenix.phase_and_build") :
output_dir = to_unicode(output_dir)
pdb_file = to_unicode(pdb_file)
run_name = os.path.basename(output_dir)
self.close_maps()
self.update_model(pdb_file)
self.load_resolve_map(map_file)
add_status_bar_text("Loaded current best model from %s at %s" %
(to_str(run_name), time.strftime("%H:%M:%S", time.localtime())))
@coot_log
def load_fobs_map (self, map_file, f_col, phi_col, fom_col="", contour=1.0,
colour=None) :
map_file = to_unicode(map_file)
imol = self.load_any_map(map_file, f_col, phi_col, fom_col)
if imol is not None :
set_contour_level_in_sigma(imol, contour)
if (colour is None) :
colour = self.settings["map_colour"]
set_map_colour(imol, *colour)
return imol
def load_comparison_map (self, *args, **kwds) :
kwds['colour'] = (0.5, 0., 1.)
return self.load_fobs_map(*args, **kwds)
# phenix.development.fem
def load_fem_map (self, map_file, f_col) :
map_file = to_unicode(map_file)
self.load_fobs_map(map_file=map_file, f_col=f_col, phi_col="PHI"+f_col)
self.load_comparison_map(map_file=map_file, f_col="2mFoDFc",
phi_col="PHI2mFoDFc")
def load_difference_map_coeffs (self, map_file, f_col, phi_col) :
map_file = to_unicode(map_file)
imol = self.load_any_map(map_file, f_col, phi_col, is_diff_map=1)
if imol is not None :
set_contour_level_in_sigma(imol, 3.0)
set_map_colour(imol, *self.settings["diff_map_colour"])
return imol
def load_anom_map_coeffs (self, map_file, f_col, phi_col) :
map_file = to_unicode(map_file)
imol = self.load_any_map(map_file, f_col, phi_col)
if imol is not None :
set_contour_level_in_sigma(imol, 3.0)
set_map_colour(imol, *self.settings["anom_map_colour"])
return imol
# phenix.fobs_minus_fobs_map
def load_iso_diff_map_coeffs (self, map_file, f_col, phi_col) :
map_file = to_unicode(map_file)
imol = self.load_any_map(map_file, f_col, phi_col, is_diff_map=1)
if imol is not None :
set_contour_level_in_sigma(imol, 3.0)
set_map_colour(imol, *self.settings["iso_diff_map_colour"])
return imol
@coot_log
def load_any_map (self, map_file, f_col, phi_col, fom_col="", is_diff_map=0) :
map_file = to_unicode(map_file)
if not os.path.isfile(map_file) :
return None
use_weights = 0
if fom_col != "" :
use_weights = 1
imol = make_and_draw_map(to_str(map_file), f_col, phi_col, fom_col,
use_weights, is_diff_map)
return imol
def load_ccp4_map (self, map_file, is_difference_map=False) :
map_file = to_unicode(map_file)
imol = handle_read_ccp4_map(to_str(map_file), 0)
if (is_difference_map) :
set_contour_level_in_sigma(imol, 3.0)
set_map_colour(imol, *self.settings["diff_map_colour"])
else :
set_contour_level_in_sigma(imol, 1.5)
set_map_colour(imol, *self.settings["map_colour"])
def update_color_setting (self, color_name, *rgb) :
self.settings[color_name] = tuple(rgb)
def start_sculptor_gui (self) :
script_file = os.path.join(phenix_dir,"phaser","phaser","coot_sculptor.py")
if os.path.isfile(script_file) :
run_script(script_file)
def load_superposed_models_and_maps (self, output_dir) :
output_dir = to_unicode(output_dir)
pickle_file = os.path.join(output_dir, ".output.pickle")
assert os.path.isfile(pickle_file)
output = cPickle.load(open(pickle_file, "rb"))
(pdb_1, map_1, pdb_2, map_2, is_difference_map) = output
read_pdb(to_str(os.path.join(output_dir, pdb_1)))
read_pdb(to_str(os.path.join(output_dir, pdb_2)))
if is_difference_map :
for map_file in [map_1, map_2] :
self.load_ccp4_map(os.path.join(output_dir, map_file), True)
else :
for map_file in [map_1, map_2] :
self.load_ccp4_map(os.path.join(output_dir, map_file), False)
def recalculate_probe_dots (self, file_name) :
file_name = to_unicode(file_name)
for pdb_mol in molecule_number_list() :
mol_name = molecule_name(pdb_mol)
if (mol_name == file_name) :
tmp_dir = self._tmp_dir
if (tmp_dir is None) :
if ("PHENIX_TMP" in os.environ) :
tmp_dir = os.environ["PHENIX_TMP"]
else :
tmp_dir = os.path.join(home_dir, ".phenix", "tmp")
if (not os.path.isdir(tmp_dir)) :
raise RuntimeError("Temporary directory (%s) does not exist." %
to_str(tmp_dir))
out_file = os.path.join(tmp_dir, "%d.pdb" % int(random() * 1000000))
safe_delete(out_file)
write_pdb_file(pdb_mol, out_file)
probe_file = os.path.join(tmp_dir, "probe.txt")
subprocess.call(args="""phenix.reduce %s | phenix.probe -u -q -mc -het -once "alta ogt33 not water" "alta ogt33" - > %s""" %
(to_str(out_file), to_str(probe_file)), shell=True)
if (not os.path.isfile(probe_file)) :
print "Missing output file from PROBE:"
print probe_file
raise RuntimeError("Missing output file from PROBE (%s)" %
to_str(probe_file))
safe_delete(out_file)
self.load_probe_results(probe_file, force_reload=True)
#---------------------------------------------------------------------
# methods for structure comparison GUI
def clear_compared_models (self) :
self._compared_models_and_maps = []
def load_partial_model_and_map (self, pdb_file, map_file=None,
diff_map_file=None) :
pdb_file = to_unicode(pdb_file)
map_file = to_unicode(map_file)
diff_map_file = to_unicode(diff_map_file)
files_and_ids = []
pdb_mol = self.read_pdb_recenter(to_str(pdb_file))
map_mol1 = map_mol2 = None
if (map_file is not None) and (map_file != "") :
map_mol1 = handle_read_ccp4_map(to_str(map_file), 0)
set_contour_level_in_sigma(map_mol1, 1.5)
set_map_colour(map_mol1, *self.settings["map_colour"])
if (diff_map_file is not None) and (diff_map_file != "") :
map_mol2 = handle_read_ccp4_map(to_str(diff_map_file), 1)
set_contour_level_in_sigma(map_mol2, 3.0) # ???
set_map_colour(map_mol2, *self.settings["diff_map_colour"])
structure = comparison_structure(
pdb_file=pdb_file,
pdb_mol=pdb_mol,
map_mol1=map_mol1,
map_mol2=map_mol2)
self._compared_models_and_maps.append(structure)
def load_partial_model_and_map_coeffs (self, pdb_file, mtz_file, phi) :
pdb_file = to_str(pdb_file)
mtz_file = to_str(mtz_file)
files_and_ids = []
pdb_mol = self.read_pdb_recenter(pdb_file)
imol1 = imol2 = None
if mtz_file is not None :
(imol1, imol2) = self.auto_load_maps(mtz_file, phi=phi)
structure = comparison_structure(
pdb_file=pdb_file,
pdb_mol=pdb_mol,
map_mol1=imol1,
map_mol2=imol2)
self._compared_models_and_maps.append(structure)
def set_active_map_for_comparison (self, pdb_file) :
pdb_file = to_str(pdb_file)
for structure in self._compared_models_and_maps :
if (structure.pdb_file == pdb_file) :
structure.set_active_map()
break
def set_compared_model_visibility (self, file_name, model, maps) :
file_name = to_str(file_name)
for structure in self._compared_models_and_maps :
if (structure.pdb_file == file_name) :
structure.set_visibility(model, maps)
break
else :
print "Can't find %s" % file_name
active = []
for other in self._compared_models_and_maps :
if (other.maps_visible) :
active.append(other)
if (len(active) == 1) :
active[0].set_active_map()
def save_compared_models (self, save_dir=None) :
save_dir = to_unicode(save_dir)
files = []
for structure in self._compared_models_and_maps :
imol = structure.pdb_mol
file_name = structure.pdb_file
file_base, ext = os.path.splitext(os.path.basename(file_name))
if save_dir is None :
save_dir = os.path.dirname(file_name)
out_file = os.path.join(save_dir, "%s_coot.pdb" % file_base)
write_pdb_file(imol, to_str(out_file))
files.append(to_str(out_file))
return ";".join(files)
class comparison_structure (object) :
def __init__ (self,
pdb_file,
pdb_mol,
map_mol1=None,
map_mol2=None) :
self.pdb_file = to_unicode(pdb_file)
self.pdb_mol = pdb_mol
self.map_mol1 = map_mol1
self.map_mol2 = map_mol2
self.maps_visible = True
def set_visibility (self, model, maps) :
set_mol_displayed(self.pdb_mol, model)