forked from LBME/slider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRJB_lib.py
executable file
·7474 lines (6827 loc) · 344 KB
/
RJB_lib.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#to import personal my library
#import RJB_lib
#import #
from __future__ import print_function
from builtins import range
from builtins import bytes, str
import string
import sys
import os
import subprocess
import time
import numpy
import math
from math import acos, asin, atan, sqrt, degrees, atan2
import numpy
def cosdeg (angle_degree):
return math.cos(math.radians(angle_degree))
def sindeg (angle_degree):
return math.sin(math.radians(angle_degree))
import itertools
#import ConfigParser
from collections import defaultdict
#sys.path.insert(0, "/cri4/rafael/Git_Scripts/git-tools/tools")
#sys.path.insert(0, "/cri4/rafael/Git_Scripts/git-tools/tools/Brasil")
#sys.path.insert(0, "/cri4/rafael/Git_Scripts/SLIDER/seq_slider")
#sys.path.insert(0, "/cri4/rafael/Git_Scripts/REPO_EXTERNAL/ARCIMBOLDO/borges-arcimboldo/ARCIMBOLDO_FULL")
#sys.path.insert(0, "/cri4/rafael/Git_Scripts/REPO_EXTERNAL/ARCIMBOLDO_FULL")
#sys.path.insert(0, "/cri4/rafael/Git_Scripts/REPO_EXTERNAL/borges-arcimboldo/ARCIMBOLDO_FULL")
#sys.path.insert(0, "/home/rborges/Dropbox/Git_Scripts/git-tools/tools")
#sys.path.insert(0, "/home/rborges/Dropbox/Git_Scripts/git-tools/tools/Brasil")
## sys.path.insert(0, "/home/rborges/Dropbox/Git_Scripts/REPO_EXTERNAL/ARCIMBOLDO/borges-arcimboldo/ARCIMBOLDO_FULL")
## sys.path.insert(0, "/home/rborges/Dropbox/Git_Scripts/REPO_EXTERNAL/borges-arcimboldo/ARCIMBOLDO_FULL")
## sys.path.insert(0, "/home/rborges/Dropbox/Git_Scripts/REPO_EXTERNAL/ARCIMBOLDO_FULL")
#sys.path.insert(0, "/home/rborges/Dropbox/Git_Scripts/SLIDER/seq_slider")
#sys.path.insert(0, "/home/rborges/REPO-ARCIMBOLDO/borges-arcimboldo/ARCIMBOLDO_FULL")
#import BORGES_MATRIX
from operator import itemgetter, attrgetter, methodcaller
from termcolor import colored
import traceback
# import Grid
from collections import defaultdict
##from alixe_library import generate_fake_ins_for_shelxe , read_cell_and_sg_from_pdb
# import SELSLIB2
import datetime
from math import pi
import Bio.PDB
from Bio import pairwise2
from Bio.SubsMat import MatrixInfo as MatList
amino_acid_list= ['A', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'W', 'Y', 'M' ]
amino_acid_list_3L= ['ALA','CYS','ASP','GLU','PHE','GLY','HIS','ILE','LYS','LEU','MET','ASN','PRO','GLN','ARG','SER','THR','VAL','TRP','TYR','MSE']
amino_acid_list_numb_atoms=[ 5, 6, 8 , 9 , 11 , 4 , 10 , 8 , 9 , 8 , 8 , 8 , 7 , 9 , 11 , 6 , 7 , 7 , 14 , 12 , 8 ]
DicHydrophobic={'ARG':('CB','CG'),'LYS':('CB','CG','CD'),'HIS':('CB'),'ASP':('CB'),'GLU':('CB','CG'),'PRO':('CB','CG'),
'MET':('CB','CG','CE'),'ALA':('CB'),'VAL':('CB','CG1','CG2'),'LEU':('CB','CG','CD1','CD2'),
'ILE':('CB','CG1','CG2','CD1'),'TYR':('CB','CG','CD1','CD2','CE1','CE2'),
'PHE':('CB','CG','CD1','CD2','CE1','CE2','CZ'),'TRP':('CG','CD2','CE3','CZ3','CH2','CZ2'),'CIS':('CB'),
'GLN':('CB','CG'),'ASN':('CB'),'THR':('CG2'),'GLY':(),'SER':()}
#alphabet=['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
alphabet='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqstuvwxyz'
def generate_HLC_from_FOM ( mtz_input , mtz_output ):
os.system ('chltofom -mtzin '+ mtz_input + ' -colin-phifom "/*/*/[PHIC,FOM]" -mtzout ' + mtz_output + ' -colout HL' )
def extract_labels_mtz_to_list ( file_input_mtz ):
os.system ( 'mtzinfo ' + file_input_mtz + ' | grep LABELS > log_labels_mtz.txt' )
labels_file=open('log_labels_mtz.txt','r')
labels_list=labels_file.read()
labels_file.close()
labels_list=labels_list[:-1].split()
os.system('rm log_labels_mtz.txt')
return labels_list
def extract_types_mtz_to_list ( file_input_mtz ):
os.system('mtzinfo '+file_input_mtz+' | grep TYPES > log_types_mtz.txt')
types_file=open('log_types_mtz.txt','r')
types_list=types_file.read()
types_file.close()
types_list=types_list[:-1].split()
os.system('rm log_types_mtz.txt')
return types_list
def extract_unit_cell_space_group_from_mtz_to_list ( file_input_mtz ):
os.system ( 'mtzinfo ' + file_input_mtz + ' | grep XDATA > log_xdata_mtz.txt' )
xdata_file=open('log_xdata_mtz.txt','r')
xdata_list=xdata_file.read()
xdata_file.close()
try:
xdata_list=xdata_list[:-1].split()
unit_cell=xdata_list[1:7]
space_group=xdata_list[9]
resolution=xdata_list[7:9]
os.system('rm log_xdata_mtz.txt')
return unit_cell,space_group
except:
try:
os.system ( 'mtzdmp ' + file_input_mtz + ' | grep " * Number of Columns" -B3 > log_cell_mtz.txt' )
cell_file=open('log_cell_mtz.txt','r')
unit_cell=cell_file.readlines()[0].split()
#cell_line=cell_list[0].split()
cell_file.close()
os.system ( 'mtzdmp ' + file_input_mtz + ' | grep " * Space group " > log_space_group_mtz.txt' )
space_group_file=open('log_space_group_mtz.txt','r')
space_group_list=space_group_file.read().split()
space_group=space_group_list[-1][:-1]
space_group_file.close()
return unit_cell,space_group
except:
print ('Unfortunately, problem extracting information from both "mtzinfo/mtzdmp '+file_input_mtz+'"')
exit()
def phs2mtz ( file_input_phs , file_input_mtz , file_output_mtz , printt=False):
unit_cell,space_group=extract_unit_cell_space_group_from_mtz_to_list ( file_input_mtz )
phs2mtz_job_descr=open('phs2mtz_job_instr.txt','w')
phs2mtz_job_descr.write('CELL\t')
unit_cell_string=''
for item in unit_cell:
unit_cell_string+=item+' '
phs2mtz_job_descr.write(unit_cell_string+'\n')
phs2mtz_job_descr.write('SYMM\t'+space_group+'\n')
phs2mtz_job_descr.write('labout\tH K L F FOM PHI SIGF\n')
phs2mtz_job_descr.write('CTYPOUT\tH H H F W P Q\n')
phs2mtz_job_descr.close()
phs2mtz_job_instr=open('phs2mtz_job_instr.txt','r')
phs2mtz_job = subprocess.Popen([ 'f2mtz','HKLIN',file_input_phs,'HKLOUT',file_output_mtz], stdin=phs2mtz_job_instr, stdout=subprocess.PIPE, stderr=subprocess.PIPE , text=True)
out, err = phs2mtz_job.communicate()
if printt: print (out)
phs2mtz_job_instr.close()
#os.system('phs2mtz_job_instr.txt')
##http://shelx.uni-ac.gwdg.de/~tg/teaching/anl-ccp4/tutorial.pdf
##A .phs-file can be converted to .mtz-format with the CCP4-program f2mtz and a short script like the
##following:
###!/bin/bash
##f2mtz hklin $1 hklout ${1%phs}mtz << eof
##CELL 92.8 92.8 129.2 90.000 90.000 120.00
##SYMM P6122
##title My short title
##labout H K L F FOM PHI sigF
##CTYPOUT H H H F W P Q
##pname CCP4 Workshop 2010
##dname phs to mtz conversion
##END
##eof
def check_add_HLC_coefficients ( file_input_mtz ):
types_list_mtz=extract_types_mtz_to_list ( file_input_mtz )
if 'A' not in types_list_mtz:
os.system ('mv ' + file_input_mtz + ' ' + file_input_mtz+'_bk')
#time.sleep(5)
generate_HLC_from_FOM ( file_input_mtz+'_bk' , file_input_mtz )
def mtz2hlc_script ( file_input_mtz , file_output_hlc ):
labels_list=extract_labels_mtz_to_list ( file_input_mtz )
types_list=extract_types_mtz_to_list ( file_input_mtz )
mtz2hlc_job_descr=open('mtz2hlc_job_instr.txt','w')
mtz2hlc_job_descr.write('LABIN FP=')
if 'FP' in labels_list :
mtz2hlc_job_descr.write('FP')
elif 'F' in labels_list :
mtz2hlc_job_descr.write('F')
elif 'FOBS' in labels_list :
mtz2hlc_job_descr.write('FOBS')
elif 'F-obs' in labels_list :
mtz2hlc_job_descr.write('F-obs')
else:
print ('mtz2hlc_script was unable to find FP/F from mtz file:'+file_input_mtz)
quit()
mtz2hlc_job_descr.write(' DUM1=')
FOM=labels_list[types_list.index('W')]
mtz2hlc_job_descr.write(FOM)
mtz2hlc_job_descr.write(' DUM2=PHIC SIGFP=')
if 'SIGFP' in labels_list :
mtz2hlc_job_descr.write('SIGFP \\\n')
elif 'SIGF' in labels_list :
mtz2hlc_job_descr.write('SIGF \\\n')
elif 'SIGFOBS' in labels_list :
mtz2hlc_job_descr.write('SIGFOBS \\\n')
elif 'SIGF-obs' in labels_list:
mtz2hlc_job_descr.write('SIGF-obs')
else:
print ('mtz2hlc_script was unable to find SIGFP/SIGF from mtz file:'+file_input_mtz)
quit()
## HLC=[]
##for i, j in enumerate(['A']):
## HLC.append(i)
## for label in range(len(labels_list)):
## if labels_list[label]=='A':
## HLC.append(label)
## mtz2hlc_job_descr.write('HLA=HL.ABCD.A HLB=HL.ABCD.B \\\n')
## mtz2hlc_job_descr.write('HLC=HL.ABCD.C HLD=HL.ABCD.D \n')
mtz2hlc_job_descr.write('HLA=HLA HLB=HLB \\\n')
mtz2hlc_job_descr.write('HLC=HLC HLD=HLD \n')
mtz2hlc_job_descr.write("OUTPUT USER '(3I4,F9.2,F6.3,F7.1,F8.2,4F9.4)' \n")
mtz2hlc_job_descr.write('END')
mtz2hlc_job_descr.close()
mtz2hlc_job_instr=open('mtz2hlc_job_instr.txt','r')
mtz2hlc_job = subprocess.Popen([ 'mtz2various','HKLIN',file_input_mtz,'HKLOUT',file_output_hlc], stdin=mtz2hlc_job_instr, stdout=subprocess.PIPE, stderr=subprocess.PIPE , text=True)
out, err = mtz2hlc_job.communicate()
print (out)
mtz2hlc_job_instr.close()
os.system('rm mtz2hlc_job_instr.txt')
##mtz2various HKLIN tempppp.mtz HKLOUT zzz.hlc << EOF
##LABIN FP=FP DUM1=FOM DUM2=PHIC SIGFP=SIGFP \
##HLA=HL.ABCD.A HLB=HL.ABCD.B \
##HLC=HL.ABCD.C HLD=HL.ABCD.D
##OUTPUT USER '(3I4,F9.2,F6.3,F7.1,F8.2,4F9.4)'
##END
##EOF
def mtz2phi_script ( file_input_mtz , file_output_phi , mtz_f=False , mtz_sigf=False , mtz_fom=False, mtz_strf=False , count=0, printt=False): #change file to variable
if printt: print ('Converting',file_input_mtz,'into',file_output_phi)
mtz2phi_job_descr = open('mtz2phi_job_instr' + str(count) + '.txt', 'w')
if mtz_f!=False and mtz_sigf!=False and mtz_fom!=False and mtz_strf!=False:
mtz2phi_job_descr.write('LABIN FP='+mtz_f)
mtz2phi_job_descr.write(' DUM1='+mtz_fom)
mtz2phi_job_descr.write(' DUM2='+mtz_strf)
mtz2phi_job_descr.write(' SIGFP='+mtz_sigf+' \n')
else:
labels_list=extract_labels_mtz_to_list ( file_input_mtz )
types_list=extract_types_mtz_to_list ( file_input_mtz )
mtz2phi_job_descr.write('LABIN FP=')
if mtz_f==False:
if 'FP' in labels_list :
mtz2phi_job_descr.write('FP')
elif 'F' in labels_list :
mtz2phi_job_descr.write('F')
elif 'FOBS' in labels_list :
mtz2phi_job_descr.write('FOBS')
elif 'F-obs' in labels_list:
mtz2phi_job_descr.write('F-obs')
elif 'F-obs-filtered' in labels_list:
mtz2phi_job_descr.write('F-obs-filtered')
else:
print ('mtz2phi_script was unable to find F/FP/FOBS/F-obs/F-obs-filtered from mtz file:'+file_input_mtz)
quit()
else:
mtz2phi_job_descr.write(mtz_f)
mtz2phi_job_descr.write(' DUM1=')
if mtz_fom==False:
try:
FOM=labels_list[types_list.index('W')]
mtz2phi_job_descr.write(FOM)
except:
mtz2phi_job_descr.write('FOM')
else: mtz2phi_job_descr.write(mtz_fom)
mtz2phi_job_descr.write(' DUM2=')
if mtz_fom==False:
if 'PHIC' in labels_list :
mtz2phi_job_descr.write('PHIC SIGFP=')
elif 'PHIFCALC' in labels_list :
mtz2phi_job_descr.write('PHIFCALC SIGFP=')
elif 'PHIF-model' in labels_list :
mtz2phi_job_descr.write('PHIF-model SIGFP=')
else:
print ('mtz2phi_script was unable to find PHIC/PHIFCALC from mtz file:' + file_input_mtz)
quit()
else: mtz2phi_job_descr.write(mtz_strf)
if mtz_sigf==False:
if 'SIGFP' in labels_list :
mtz2phi_job_descr.write('SIGFP \n')
elif 'SIGF' in labels_list :
mtz2phi_job_descr.write('SIGF \n')
elif 'SIGFOBS' in labels_list :
mtz2phi_job_descr.write('SIGFOBS \n')
elif 'SIGF-obs' in labels_list:
mtz2phi_job_descr.write('SIGF-obs \n')
elif 'SIGF-obs-filtered' in labels_list:
mtz2phi_job_descr.write('SIGF-obs-filtered \n')
else:
print ('mtz2phi_script was unable to find SIGFP/SIGF/SIGFOBS/SIGF-obs/SIGF-obs-filtered from mtz file:'+file_input_mtz)
quit()
else:
mtz2phi_job_descr.write(mtz_sigf+' \n')
mtz2phi_job_descr.write("OUTPUT USER '(3I4,F9.2,F6.3,F7.1,F8.2)' \n")
mtz2phi_job_descr.write('END')
mtz2phi_job_descr.close()
mtz2phi_job_instr=open('mtz2phi_job_instr'+str(count)+'.txt','r')
# print 'mtz2various','HKLIN',file_input_mtz,'HKLOUT',file_output_phi
# exit()
mtz2phi_job = subprocess.Popen([ 'mtz2various','HKLIN',file_input_mtz,'HKLOUT',file_output_phi], stdin=mtz2phi_job_instr, stdout=subprocess.PIPE, stderr=subprocess.PIPE , text=True)
out, err = mtz2phi_job.communicate()
#print out
mtz2phi_job_instr.close()
if not os.path.isfile(file_output_phi):
print ('\n\n\n\n\n\nFAILURE IN FUNCTION RJB_LIB.mtz2phi_script CONVERTING',file_input_mtz,'TO',file_output_phi)
print ('Command used:')
print ('mtz2various', 'HKLIN', file_input_mtz, 'HKLOUT', file_output_phi, mtz2phi_job_instr)
print ('Instruction file:,mtz2phi_job_instr'+str(count)+'.txt')
print ('out:')
print (out)
print ('err')
print (err)
mtz2phi_job_instr.close()
quit()
else:
mtz2phi_job_instr.close()
os.system('rm mtz2phi_job_instr'+str(count)+'.txt')
##mtz2various HKLIN ${1} HKLOUT $output.phi > /dev/null << EOF
##LABIN FP=F DUM1=FOM DUM2=PHIC SIGFP=SIGF
##OUTPUT USER '(3I4,F9.2,F6.3,F7.1,F8.2,4F9.4)'
##END
##EOF
def phs2mtz_script ( file_input_phs , instructions , file_output_mtz ):
if instructions.endswith('.mtz'):
labels_list=extract_labels_mtz_to_list ( file_input_mtz )
types_list=extract_types_mtz_to_list ( file_input_mtz )
mtz2phi_job_descr=open('mtz2phi_job_instr.txt','w')
mtz2phi_job_descr.write('LABIN FP=')
if 'FP' in labels_list :
mtz2phi_job_descr.write('FP')
elif 'F' in labels_list :
mtz2phi_job_descr.write('F')
elif 'FOBS' in labels_list :
mtz2phi_job_descr.write('FOBS')
else:
print ('mtz2phi_script was unable to find F/FP/FOBS from mtz file:'+file_input_mtz)
quit()
mtz2phi_job_descr.write(' DUM1=')
FOM=labels_list[types_list.index('W')]
mtz2phi_job_descr.write(FOM)
mtz2phi_job_descr.write(' DUM2=PHIC SIGFP=')
if 'SIGFP' in labels_list :
mtz2phi_job_descr.write('SIGFP \n')
elif 'SIGF' in labels_list :
mtz2phi_job_descr.write('SIGF \n')
elif 'SIGFOBS' in labels_list :
mtz2phi_job_descr.write('SIGFOBS \n')
else:
print ('mtz2phi_script was unable to find SIGFP/SIGF from mtz file:'+file_input_mtz )
quit()
mtz2phi_job_descr.write("OUTPUT USER '(3I4,F9.2,F6.3,F7.1,F8.2)' \n")
mtz2phi_job_descr.write('END')
mtz2phi_job_descr.close()
mtz2phi_job_instr=open('mtz2phi_job_instr.txt','r')
mtz2phi_job = subprocess.Popen([ 'mtz2various','HKLIN',file_input_mtz,'HKLOUT',file_output_phi], stdin=mtz2phi_job_instr, stdout=subprocess.PIPE, stderr=subprocess.PIPE , text=True)
out, err = mtz2phi_job.communicate()
print (out)
mtz2phi_job_instr.close()
#os.system('rm mtz2phi_job_instr.txt')
def extract_table_for_dictionary ( input_file , label_index_to_write_key_in_output_dictionary ):
input_fileee=open( input_file , 'r' )
input_file_list=input_fileee.readlines()
input_fileee.close()
labels=input_file_list[0].split()
dictio_all={}
for line in input_file_list[1:]:
dictio_var={}
line=line.split()
for i in range(len(labels)):
dictio_var[labels[i]]=line[i]
dictio_all[line[label_index_to_write_key_in_output_dictionary]]=dictio_var
return dictio_all
def extract_table_for_dictionary_of_list ( input_file , label_index_to_write_key_in_output_dictionary ):
input_fileee=open( input_file , 'r' )
input_file_list=input_fileee.readlines()
input_fileee.close()
labels=input_file_list[0].split()
dictio_all={}
for line in input_file_list[1:]:
dictio_var={}
line=line.split()
for i in range(len(labels)):
dictio_var[labels[i]]=[]
dictio_all[line[label_index_to_write_key_in_output_dictionary]]=dictio_var
## dictionary created with number of residues and lists
for line in input_file_list[1:]:
line=line.split()
for i in range(len(labels)):
dictio_all[line[label_index_to_write_key_in_output_dictionary]][labels[i]].append(line[i])
return dictio_all
def extract_table_to_list_of_dict_with_first_line_as_key ( input_file ): #script generated in Jul7,2016 based on extract_table_for_dictionary to be more general, I will create in list instead of chosen a dic, thus it has no down side!
input_fileee=open( input_file , 'r' )
input_file_list=input_fileee.readlines()
input_fileee.close()
labels=input_file_list[0].split()
list_dic_all=[]
for line in input_file_list[1:]:
dictio_var={}
line=line.split()
for i in range(len(labels)):
dictio_var[labels[i]]=line[i]
list_dic_all.append(dictio_var)
return list_dic_all
def from_list_dic_generate_dic_dic_lists_chosen_index ( list_dic_all , chosen_key ):
dic_dic_list_all={}
if not chosen_key in list_dic_all[0]:
print ('Chosen key does not exist in given list of dics. Failure in RJB_lib.from_list_dic_generate_dic_dic_lists_chosen_index.')
print ('\nExisting keys are:')
for key in sorted(list_dic_all[0]):
print (key )
exit()
for dic in list_dic_all:
try:
dic_dic_list_all[dic[chosen_key]]
except:
dic_dic_list_all[dic[chosen_key]]=defaultdict(list)
for key in dic:
if not key==chosen_key:
dic_dic_list_all [dic[chosen_key]] [key] .append( dic[key] )
return dic_dic_list_all
def given_dic_of_dic_of_lists_return_list_dic_mean_stdev ( dic_dic_list_all , list_reject_keys ):
list_dic_summ=[]
for chain in sorted(dic_dic_list_all):
dic_var={}
dic_var['chain']=chain
dic=dic_dic_list_all[chain]
for key in dic:
if not key in list_reject_keys :
lista=dic[key]
while 'n/a' in lista: lista.remove('n/a')
if len( lista)==0:
mean='n/a'
std='n/a'
else:
lista=map(float,lista)
mean=numpy.mean(lista)
std=numpy.std(lista)
mean=mean_dev_return_good_value ( mean , 'mean' )
std=mean_dev_return_good_value ( std , 'std' )
dic_var[key+'_m']=mean
dic_var[key+'_sd']=std
list_dic_summ.append(dic_var)
return list_dic_summ
def given_out_edstats_return_list_mean_stdev_by_chain (input_file):
list_dic_all=extract_table_to_list_of_dict_with_first_line_as_key ( input_file )
dic_dic_list_all=from_list_dic_generate_dic_dic_lists_chosen_index ( list_dic_all , 'CI' )
list_dic_summ=given_dic_of_dic_of_lists_return_list_dic_mean_stdev ( dic_dic_list_all , ['RT','MN','CP','NR'] )
return list_dic_summ
def generate_deviation_by_resid ( out_input_file , amino_acid_list_3L , out_output_file ): #corrected in 2 October 2015
out_fileee=open( out_input_file , 'r' )
out_file_list=out_fileee.readlines()
## print out_file_list
try:
columns=out_file_list[0].split() #column of first line
except:
print ("\n\n*******" + out_input_file, "should contain more values:",out_file_list , "*******\n\n")
return None
#part that writes in a list, columns that should be saved in new table file
columns_chosen=[] #columns to be inserted in
columns_chosen.append(columns[2])
columns_chosen.append(columns[3])
for label in columns:
if label.endswith('s') or label.endswith('a') or label.endswith('m'):
columns_chosen.append(label)
#
dictio_table={}
## #generate dictionary (containing all) of dictionary of possible amino_acids containing empty lists
## for amino_acid in amino_acid_list:
## dictio_var={}
## for label2 in columns_chosen:
## dictio_var[label2]=[]
## dictio_table[amino_acid]=dictio_var
for amino_acid in amino_acid_list_3L :
## if amino_acid!='GLY' and amino_acid!='ALA':
dic_amino_acid={}
for label in columns_chosen:
var_list=[]
for line in out_file_list:
#print line
line_list=line.split()
#print line_list
if line_list[1]==amino_acid:
for index_list_line in range(len(line_list) ) :
#print out_file_list[0][index_list_line]
if columns[index_list_line]==label:
#if (amino_acid!='GLY' and amino_acid!='ALA' and label.endswith("s") ) or not label.endswith("s"):
if line_list[index_list_line]!="n/a":
var_list.append(line_list[index_list_line])
else:
var_list.append("NaN")
dic_amino_acid[label]=var_list
dictio_table[amino_acid]=dic_amino_acid
#for item in dictio_table:
# print dictio_table
out_output_file2=open(out_output_file,'w')
out_output_file2.write('RT\t#models\tCI\tRN\t')
for item in columns_chosen[2:]:
out_output_file2.write(item+'(mean)\t'+item+'(dev)\t')
out_output_file2.write('\n')
for amino_acid in dictio_table:
if not len(dictio_table[amino_acid]['CI'])==0:
out_output_file2.write(amino_acid+'\t')
numb_models=str(len(dictio_table[amino_acid]['CI']))
out_output_file2.write(numb_models+'\t')
for key in columns_chosen:
if key=='CI' or key=='RN':
out_output_file2.write(dictio_table[amino_acid][key][0]+'\t')
else:
if key=='NPm':
list=[int(i) for i in dictio_table[amino_acid][key]]
else:
list=[float(i) for i in dictio_table[amino_acid][key]]
mean=numpy.mean(list)
mean=mean_dev_return_good_value ( mean , 'mean' )
std=numpy.std(list)
std=mean_dev_return_good_value ( std , 'std' )
out_output_file2.write(mean+'\t')
out_output_file2.write(std+'\t')
out_output_file2.write('\n')
out_fileee.close()
out_output_file2.close()
def generate_deviation_by_resid_old ( out_input_file , amino_acid_list_3L , out_output_file ):
out_fileee=open( out_input_file , 'r' )
out_file_list=out_fileee.readlines()
columns=out_file_list[0].split()
columns_chosen=[]
columns_chosen.append(columns[2])
columns_chosen.append(columns[3])
for label in columns:
if label.endswith('s') or label.endswith('a') or label.endswith('m'):
columns_chosen.append(label)
dictio_table={}
## #generate dictionary (containing all) of dictionary of possible amino_acids containing empty lists
## for amino_acid in amino_acid_list:
## dictio_var={}
## for label2 in columns_chosen:
## dictio_var[label2]=[]
## dictio_table[amino_acid]=dictio_var
for amino_acid in amino_acid_list_3L :
## if amino_acid!='GLY' and amino_acid!='ALA':
dic_amino_acid={}
for label in columns_chosen:
var_list=[]
for line in out_file_list:
line_list=line.split()
if line_list[1]==amino_acid:
for index_list_line in range(len(line_list) ) :
#print out_file_list[0][index_list_line]
if columns[index_list_line]==label:
if (amino_acid!='GLY' and amino_acid!='ALA' and label.endswith("s") ) or not label.endswith("s"):
var_list.append(line_list[index_list_line])
else:
var_list.append("1000")
dic_amino_acid[label]=var_list
dictio_table[amino_acid]=dic_amino_acid
#for item in dictio_table:
# print dictio_table
out_output_file2=open(out_output_file,'w')
out_output_file2.write('RT\t#models\tCI\tRN\t')
for item in columns_chosen[2:]:
out_output_file2.write(item+'(mean)\t'+item+'(dev)\t')
out_output_file2.write('\n')
for amino_acid in dictio_table:
if not len(dictio_table[amino_acid]['CI'])==0:
out_output_file2.write(amino_acid+'\t')
numb_models=str(len(dictio_table[amino_acid]['CI']))
out_output_file2.write(numb_models+'\t')
for key in columns_chosen:
if key=='CI' or key=='RN':
out_output_file2.write(dictio_table[amino_acid][key][0]+'\t')
else:
if key=='NPm':
list=[int(i) for i in dictio_table[amino_acid][key]]
else:
list=[float(i) for i in dictio_table[amino_acid][key]]
mean=numpy.mean(list)
mean=mean_dev_return_good_value ( mean , 'mean' )
std=numpy.std(list)
std=mean_dev_return_good_value ( std , 'std' )
out_output_file2.write(mean+'\t')
out_output_file2.write(std+'\t')
out_output_file2.write('\n')
out_output_file2.close()
def mean_dev_return_good_value ( input , type_of_variable ) :
if type_of_variable=='mean':
if input<1 and input>-1:
input='%.3f'%(input)
else:
input='%.1f'%(input)
elif type_of_variable=='std':
if input<1 and input>-1:
input='%.4f'%(input)
else:
input='%.1f'%(input)
else:
print ('exception with value: '+input)
print ('quitting')
exit()
return input
def retrieve_res_statistic_to_table ( out_input_file, min_res , max_res , output_file ):
out_res_file_new_overall=open(output_file , 'w') # new table overall main chain protein
out_res_file_new_overall.write('R#\tBAm(mean)\tBAm(dev)\tNPm(mean)\tNPm(dev)\tRm(mean)\tRm(dev)\tRGm(mean)\tRGm(dev)\tSRGm(mean)\tSRGm(dev)\tCCSm(mean)\tCCSm(dev)\tCCPm(mean)\tCCPm(dev)\tZCCm(mean)\tZCCm(dev)\tZOm(mean)\tZOm(dev)\tZDm(mean)\tZDm(dev)\tZD-m(mean)\tZD-m(dev)\tZD+m(mean)\tZD+m(dev)\n')
for i in range (min_res , max_res+1):
i_str=str(i)
out_fileee=open( out_input_file+i_str, 'r' )
out_file_list=out_fileee.readlines()
out_res_file_new_overall.write(i_str+'\t')
list_of_list=[]
for column in range (4, 16 ):
list=[]
for line in range (1, len(out_file_list) ):
line2=out_file_list[line].split('\t')
if line2[column]!='n/a':
list.append( float(line2[column] ) )
list_of_list.append(list)
for listaa in list_of_list:
mean=numpy.mean(listaa)
if mean<1 and mean>-1:
mean='%.3f'%(mean)
else:
mean='%.1f'%(mean)
out_res_file_new_overall.write(mean+'\t')
std=numpy.std(listaa)
if std<1 and std>-1:
std='%.4f'%(std)
else:
std='%.1f'%(std)
out_res_file_new_overall.write(std+'\t')
out_res_file_new_overall.write('\n')
out_res_file_new_overall.close()
def extract_protein_chainID_res_number (pdb_input) :
file=open(pdb_input,'r')
file_list=file.readlines()
file.close()
DicChResNResType={}
listChResNCA=[]
chain_ID=[]
res=[]
number=0
res_check = 'FirstOccurrence'
chain_check=0
res_string=''
for line in file_list:
if line.startswith('ATOM') or (line.startswith('HETATM') and line[17:20]=='MSE'):
#line2=line.split()
if line[12:15]==' CA' and [line[21],int(line[22:26])] not in listChResNCA: listChResNCA.append([line[21],int(line[22:26])]) #Constructing a list of lists [ [chain1,resnumb1] , [chain2,resnumb2] ]
if res_check=='FirstOccurrence':
res_check=int(line[22:26])
chain_check=line[21]
chain_ID.append(chain_check)
residue=amino_acid_list[ amino_acid_list_3L.index(line[17:20]) ]
DicChResNResType[chain_check]={res_check:residue}
res_string=res_string+residue
else:
if not res_check==int(line[22:26]):
if chain_check==line[21]:
if int(line[22:26])==res_check+1:
res_check=res_check+1
residue=amino_acid_list[ amino_acid_list_3L.index(line[17:20]) ]
DicChResNResType[chain_check][res_check]=residue
res_string=res_string+residue
else:
res_check=int(line[22:26])
residue=amino_acid_list[ amino_acid_list_3L.index(line[17:20]) ]
DicChResNResType[chain_check][res_check] = residue
res_string=res_string+'/'+residue
else:
res.append(res_string)
res_string=''
chain_check=line[21]
chain_ID.append(chain_check)
residue=amino_acid_list[ amino_acid_list_3L.index(line[17:20]) ]
res_string=residue
res_check=int(line[22:26])
if chain_check not in DicChResNResType: DicChResNResType[chain_check] = {res_check: residue}
else: DicChResNResType[chain_check][res_check] = residue
res.append(res_string)
count=0
for chain in res:
count=count+len(chain.replace('/',''))
## print 'verifying fn'
## print 'chain_ID',chain_ID
## print 'res',res
## print 'count',count
return chain_ID , res , count , DicChResNResType , listChResNCA
def extract_minimum_maximum_residue_number (pdb_input) : #script that extracts minimum and maximum residue number from a PDB
file=open(pdb_input,'r')
file_list=file.readlines()
file.close()
for line in file_list:
if line.startswith('ATOM'):
#taking minimum number
try:
minimum_res_numb
if minimum_res_numb>int(line[23:26]):
minimum_res_numb=int(line[23:26])
except:
minimum_res_numb=int(line[23:26])
#taking maximum number
try:
maximum_res_numb
if maximum_res_numb<int(line[23:26]):
maximum_res_numb=int(line[23:26])
except:
maximum_res_numb=int(line[23:26])
return minimum_res_numb , maximum_res_numb
def rewrite_seq_variables (seq_input, seq_output): #script that given one sequence in a file containing just the sequence in one letter code, generates variation based on a dictionary. Such a dictionary was made by me looking at similarities of charge and chemical bonds of each residue
amino_acid_group={"A":["G","S","T","V"],"C":["-"],"D":["T","N","G","A","L"],"E":["D","L","N","A","G"],"F":["Y","W","A","G"],"G":["A","V","T"],"H":["Y","W","I","A","G"],"I":["V","N","L","A","G"],"K":["R","Q","M","G","A"],"L":["I","V","N","A","G"],"M":["K","I","C","A","G"],"N":["Q","T","D","A","G"],"P":["F","V","A","G"],"Q":["N","E","H","G","A"],"R":["K","H","S","G","A"],"S":["T","N","Q","A","G"],"T":["S","N","Q","A","G"],"V":["T","S","A","G"],"W":["F","H","M","A","G"],"Y":["W","F","A","G","H"] }
seq_inputtt=open(seq_input,"r")
seq_input_string=seq_inputtt.read()
seq_input_string=seq_input_string[:-1]
list_write=["","","","","",""]
for character in seq_input_string:
list_write[0]=list_write[0]+character
for i in range (5):
try:
list_write[i+1]=list_write[i+1]+amino_acid_group[character][i]
except:
list_write[i+1]=list_write[i+1]+"-"
file_output=open(seq_output,"w")
for seq in list_write:
file_output.write(seq)
file_output.write("\n")
file_output.close()
def plot_line_error (input_file , output_file ): #output_file_without_.png
input_fileee=open(input_file,"r")
input_file_list=input_fileee.readlines()
labels=input_file_list[0].split()
gnuplot_instr=open("gnuplot_instr","w")
gnuplot_instr.write("set terminal png font \"default\"\n")
gnuplot_instr.write('set nokey\n')
if input_file_list[0].startswith("RT"):
gnuplot_instr.write("set style line 1 lc rgb \'grey30\' ps 0 lt 1 lw 2\n")
gnuplot_instr.write("set style line 2 lc rgb 'grey70' lt 1 lw 2\n")
gnuplot_instr.write("set label '*' at 3,0.8 center\n")
gnuplot_instr.write("set label '*' at 4,0.8 center\n")
#gnuplot_instr.write("set label '*' at 4,0.8 center\n")
gnuplot_instr.write("set border 3\n")
gnuplot_instr.write("set xtics nomirror scale 0\n")
gnuplot_instr.write("set xtics rotate\n")
for label in labels:
if label.endswith("(mean)") and not label.startswith("ZDm"):
gnuplot_instr.write('set ylabel \"'+label[:-6]+'\" \n')
residue_number_table=output_file.split('_')[-1]
gnuplot_instr.write('set xlabel \"Residue #'+residue_number_table+'\" \n')
gnuplot_instr.write("set output \""+output_file+"_"+label[:-6]+".png\n")
index_label=labels.index(label)
index_label+=1
gnuplot_instr.write("plot \""+input_file+"\" using 0:"+str(index_label)+":"+str(index_label+1)+" with yerrorbars ls 1, \"\" using 0:"+str(index_label)+":(0.7):xtic(1) with boxes ls 2\n")
else:
try:
xrange_f=int( input_file_list[-1].split()[0] )
xrange_f+=1
xrange_i=int( input_file_list[1].split()[0] )
xrange_i-=1
gnuplot_instr.write("set xrange [")
gnuplot_instr.write(str(xrange_i)+":"+str(xrange_f)+"]\n")
except:
blabla=0
for label in labels:
if label.endswith("(mean)") and not label.startswith("ZDm"):
gnuplot_instr.write('set ylabel \"'+label[:-6]+'\" \n')
gnuplot_instr.write('set xlabel \"Residues number\" \n')
gnuplot_instr.write("set output \""+output_file+"_"+label[:-6]+".png\n")
index_label=labels.index(label)
index_label+=1
gnuplot_instr.write("plot \""+input_file+"\" using 1:"+str(index_label)+" with lines lw 3 lt -1 , \"\" using 1:"+str(index_label)+":"+str(index_label+1)+" with yerrorbars \n")
gnuplot_instr.close()
os.system("gnuplot gnuplot_instr > /dev/null")
def plot_line (input_file , output_file , column_to_be_labeled , columns_to_be_evaluated): #script generated August 13rd, number given should not account for 0 as first column
input_fileee=open(input_file,"r")
input_file_list=input_fileee.readlines()
labels=input_file_list[0].split()
gnuplot_instr=open("gnuplot_instr","w")
gnuplot_instr.write("set terminal png font \"default\"\n")
gnuplot_instr.write('set nokey\n')
try:
xrange_f=int( input_file_list[-1].split()[column_to_be_labeled-1] )
xrange_f+=1
xrange_i=int( input_file_list[1].split()[column_to_be_labeled-1] )
xrange_i-=1
gnuplot_instr.write("set xrange [")
gnuplot_instr.write(str(xrange_i)+":"+str(xrange_f)+"]\n")
except:
pass
for label_number in columns_to_be_evaluated:
gnuplot_instr.write('set ylabel \"'+labels[label_number-1]+'\" \n')
gnuplot_instr.write('set xlabel \"Residues number\" \n')
gnuplot_instr.write("set output \""+output_file+"_"+labels[label_number-1]+".png\n")
gnuplot_instr.write("plot \""+input_file+"\" using "+str(column_to_be_labeled)+":"+str(label_number)+" with lines lw 3 lt -1 \n")#+", \"\" using 1:"+str(index_label)+":"+str(index_label+1)+" with yerrorbars \n")
gnuplot_instr.close()
os.system("gnuplot gnuplot_instr > /dev/null")
def generate_mark_true_res_SC_in_txt ( txt_input , true_res , txt_output ):
txt_input_file=open(txt_input,'r')
txt_input_list=txt_input_file.readlines()
txt_input_file.close()
txt_output_file=open(txt_output,'w')
for line in txt_input_list:
if line.startswith(true_res) and line[3]!="!":
line_post_mortem=line[:3]+'!'+line[3:]
txt_output_file.write(line_post_mortem)
else:
txt_output_file.write(line)
txt_output_file.close()
def return_low_high_value_from_out_file ( statistic , txt_input ):
txt_input_file=open(txt_input,'r')
txt_input_list=txt_input_file.readlines()
txt_input_file.close()
labels=txt_input_list[0].split()
index_statistic=labels.index(statistic)
var_list=[]
for line_number in range(1,len(txt_input_list)):
var_list.append(float(txt_input_list[line_number].split()[index_statistic]))
if statistic.startswith('CC') or statistic.startswith('ZCC') or statistic=='ZOs(mean)' or statistic=='ZD-s(mean)' :
value=max(var_list)
elif statistic.startswith('R') or statistic.startswith('BA') or statistic.startswith('ZD+'):
value=min(var_list)
value='%.3f'%value
return value
def mtzfix (file_input,file_output,type_refinement_program):
if not os.path.isfile( file_output ):
if 'sigmaa' in type_refinement_program or 'sigmaa' in file_input:
mtzfix_job = subprocess.Popen([ 'mtzfix','FLABEL F SIGF FC PHIC FC_ALL PHIC_ALL 2FOFCWT PH2FOFCWT FOFCWT PHFOFCWT FOM'.split(),'HKLIN',file_input,'HKLOUT',file_output], stdout=subprocess.PIPE, stderr=subprocess.PIPE , text=True)
#mtzfix_job = subprocess.Popen([ 'mtzfix','FLABEL F SIGF FC PHIC FC_ALL PHIC_ALL 2FOFCWT PH2FOFCWT FOFCWT PHFOFCWT FOM'.split(),'HKLIN',file_input,'HKLOUT',file_output], stdin=fft_job_instr, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
else:
#mtzfix_job = subprocess.Popen([ 'mtzfix','HKLIN',file_input,'HKLOUT',file_output], stdin=fft_job_instr, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
mtzfix_job = subprocess.Popen([ 'mtzfix','HKLIN',file_input,'HKLOUT',file_output], stdout=subprocess.PIPE, stderr=subprocess.PIPE , text=True)
out, err = mtzfix_job.communicate()
#print out[-110:]
if not os.path.isfile( file_output ):
print ("mtzfix did not generate mtzfixfile, previous file",file_input," being used.")
os.system("cp "+file_input+" "+file_output)
else:
print ("mtzfix performed correctly in file:",file_input)
def RSZD_calculation (file_mtz, file_pdb , file_out , type_refinement_program): #script generated ?, upgraded for evaluating coot output mtz in May, 4th 2016
#type_refinement_program = refmac buster sigmaa (coot)
file_mtz_fix=file_mtz[:-4]+'_mtzfix.mtz'
file_map_fo=file_mtz[:-4]+'_fo.map'
file_map_df=file_mtz[:-4]+'_df.map'
## print 'mtz to be fixed:',file_mtz_fix
## print 'map fo:',file_map_fo
## print 'map fc:',file_map_df
mtzfix (file_mtz,file_mtz_fix,type_refinement_program)
#correct mtz through mtzfix line: mtzfix [FLABEL <string>] HKLIN in.mtz HKLOUT out.mtz [FLABEL <string>]=not necessary
#generate maps through fft
if 'BUSTER' in file_mtz or 'buster' in type_refinement_program or 'sigmaa' in type_refinement_program or 'sigmaa' in file_pdb or 'phenix.refine' in type_refinement_program:
fft_script ( file_mtz_fix , file_map_fo , '2FOFCWT' , 'PH2FOFCWT' )
fft_script ( file_mtz_fix , file_map_df , 'FOFCWT' , 'PHFOFCWT' )
elif 'refmac' in file_mtz or 'refmac' in type_refinement_program:
fft_script ( file_mtz_fix , file_map_fo , 'FWT' , 'PHWT' )
fft_script ( file_mtz_fix , file_map_df , 'DELFWT' , 'PHDELWT' )
# elif 'phenix' in file_mtz or 'phenix' in type_refinement_program:
# print 'phenix.refine is under development, it may be advisable to use phenix.model_vs_data'
# exit()
# fft_script ( file_mtz_fix , file_map_fo , 'FWT' , 'PHWT' )
# fft_script ( file_mtz_fix , file_map_df , 'DELFWT' , 'PHDELWT' )
else:
print ('Unable to find if mtz file '+file_mtz+' was coming from BUSTER or REFMAC or sigmaa (coot>get-eds-pdb-and-mtz), thus unable to generate edstats .out file')
#extract resolution from mtz with mtzdmp
low_res,high_res=extract_resolution_from_mtz(file_mtz)
#generate out through edstats
os.system('echo resl=' + low_res + ',resh=' + high_res + ' | edstats MAPIN1 ' + file_map_fo + ' MAPIN2 ' + file_map_df + ' XYZIN ' + file_pdb + ' OUT ' + file_out + ' > ' + file_out[:-3] +'log')
os.system('rm '+file_map_fo+' '+file_map_df+' '+file_mtz_fix)
def RSZD_calculation_delete (file_mtz, file_pdb , file_out , type_refinement_program): #script generated ?, upgraded for evaluating coot output mtz in May, 4th 2016
#type_refinement_program = refmac buster sigmaa (coot)
file_mtz_fix=file_mtz[:-4]+'_mtzfix.mtz'
file_map_fo=file_pdb[:-4]+'_fo.map'
file_map_df=file_pdb[:-4]+'_df.map'
if 'BUSTER' in file_mtz or 'buster' in type_refinement_program or 'sigmaa' in type_refinement_program or 'sigmaa' in file_pdb or 'phenix.refine' in type_refinement_program :
fft_script ( file_mtz_fix , file_map_fo , '2FOFCWT' , 'PH2FOFCWT' )
fft_script ( file_mtz_fix , file_map_df , 'FOFCWT' , 'PHFOFCWT' )
elif 'refmac' in file_mtz or 'refmac' in type_refinement_program:
fft_script ( file_mtz_fix , file_map_fo , 'FWT' , 'PHWT' )
fft_script ( file_mtz_fix , file_map_df , 'DELFWT' , 'PHDELWT' )
else:
print ('Unable to find if mtz file '+file_mtz+' was coming from BUSTER or REFMAC or sigmaa (coot>get-eds-pdb-and-mtz), thus unable to generate edstats .out file')
#extract resolution from mtz with mtzdmp
low_res,high_res=extract_resolution_from_mtz(file_mtz)
#generate out through edstats
os.system('echo resl=' + low_res + ',resh=' + high_res + ' | edstats MAPIN1 ' + file_map_fo + ' MAPIN2 ' + file_map_df + ' XYZIN ' + file_pdb + ' OUT ' + file_out + ' > ' + file_out[:-3] +'log')
def extract_resolution_from_mtz ( file_input_mtz ):
os.system('mtzdmp ' + file_input_mtz + ' | grep " * Resolution Range :" -A2 | grep A > '+file_input_mtz[:-4]+'_res.log')
b=open(file_input_mtz[:-4]+'_res.log', 'r')
c=b.read().split()
b.close()
low_res=c[3]
high_res=c[5]
os.system('rm '+file_input_mtz[:-4]+'_res.log')
return low_res,high_res
def extract_resolution_from_mtz_delete ( file_input_mtz ):
os.system('mtzdmp ' + file_input_mtz + ' | grep " * Resolution Range :" -A2 | grep A > '+file_input_mtz[:-4]+'_res.log')
b=open(file_input_mtz[:-4]+'_res.log', 'r')
c=b.read().split()
b.close()
low_res=c[3]
high_res=c[5]
#os.system('rm '+file_input_mtz[:-4]+'_res.log')
return low_res,high_res
def fft_script ( file_input_mtz , file_output_map , F , PHI ):
#writting fft_job_description_file
fft_job_description=open(file_output_map+'_fft_job.txt','w')
fft_job_description.write('XYZLIM ASU\n')
fft_job_description.write('GRID SAMPLE 4.5\n')
fft_job_description.write('LABIN -\n')
fft_job_description.write(' F1='+F+' PHI='+PHI)
fft_job_description.write('\nEND')
fft_job_description.close()
fft_job_instr=open(file_output_map+'_fft_job.txt', 'r')
fft_job = subprocess.Popen([ 'fft','HKLIN',file_input_mtz,'MAPOUT',file_output_map], stdin=fft_job_instr, stdout=subprocess.PIPE, stderr=subprocess.PIPE , text=True)
out, err = fft_job.communicate()
fft_job_instr.close()
os.system('rm '+file_output_map+'_fft_job.txt')
def extract_list_mean_from_outfile ( input_outfile ): #script generated in 6 July 2015 to extract from out_file all the statistics´ label and mean
input_outfileee=open ( input_outfile , "r")
input_outfile_list=input_outfileee.readlines()
input_outfileee.close()
labels=[]
final_list=[]
first_out_line=input_outfile_list[0].split()
for column in range ( 3,len( first_out_line ) ):
labels.append ( first_out_line[column] )
var_list=[]
for line in range (1,len(input_outfile_list)):
var_list.append ( float(input_outfile_list[line].split()[column]) )
var_mean=numpy.mean(var_list)
final_list.append (var_mean)
return labels , final_list
def extract_list_mean_std_from_outfile ( input_outfile ): #script generated in 16 July 2015 to extract from out_file all the statistics´ label and mean
input_outfileee=open ( input_outfile , "r")
input_outfile_list=input_outfileee.readlines()
input_outfileee.close()
labels=[]
final_list_mean=[]
final_list_std=[]
first_out_line=input_outfile_list[0].split()
for column in range ( 3,len( first_out_line ) ):
labels.append ( first_out_line[column] )
var_list=[]
for line in range (1,len(input_outfile_list)):
var_list.append ( float(input_outfile_list[line].split()[column]) )
var_mean=numpy.mean(var_list)
var_std=numpy.std(var_list)
if var_mean<1 and var_mean>-1:
var_mean='%.3f'%(var_mean)
else:
var_mean='%.1f'%(var_mean)