-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidationLevel0.py
1338 lines (1140 loc) · 64.7 KB
/
validationLevel0.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
# **************************************************************************
# *
# * Authors: Carlos Oscar Sorzano ([email protected])
# *
# * Unidad de Bioinformatica of Centro Nacional de Biotecnologia , CSIC
# *
# * This program is free software; you can redistribute it and/or modify
# * it under the terms of the GNU General Public License as published by
# * the Free Software Foundation; either version 2 of the License, or
# * (at your option) any later version.
# *
# * This program is distributed in the hope that it will be useful,
# * but WITHOUT ANY WARRANTY; without even the implied warranty of
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# * GNU General Public License for more details.
# *
# * You should have received a copy of the GNU General Public License
# * along with this program; if not, write to the Free Software
# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
# * 02111-1307 USA
# *
# * All comments concerning this program package may be sent to the
# * e-mail address '[email protected]'
# *
# **************************************************************************
import math
import numpy as np
import os
import scipy
import subprocess
from datetime import datetime
from random import randint
from time import sleep
from scipion.utils import getScipionHome
import pyworkflow.plugin as pwplugin
from pwem import emlib
from validationReport import readMap, readGuinier, latexEnumerate, calculateSha256, reportPlot, reportMultiplePlots,\
reportHistogram, isHomogeneous
import xmipp3
from resourceManager import sendToSlurm, waitOutput, skipSlurm, waitOutputFile, waitUntilFinishes, createScriptForSlurm, checkIfJobFinished
import configparser
from tools.utils import saveIntermediateData
from resources.constants import *
# used by the ProtImportVolumes protocol, volumes will be downloaded from EMDB
IMPORT_FROM_EMDB = 1
config = configparser.ConfigParser()
config.read(os.path.join(os.path.dirname(__file__), 'config.yaml'))
useSlurm = config['QUEUE'].getboolean('USE_SLURM')
gpuIdSkipSlurm = config['QUEUE'].getint('GPU_ID_SKIP_SLURM')
N_THREADS = config['SCIPION'].get('N_THREADS')
def importMap(project, report, label, fnMap, fnMap1, fnMap2, Ts, mapCoordX, mapCoordY, mapCoordZ, priority=False):
Prot = pwplugin.Domain.importFromPlugin('pwem.protocols',
'ProtImportVolumes', doRaise=True)
fnDir, fnBase = os.path.split(fnMap)
if mapCoordX is not None and mapCoordY is not None and mapCoordZ is not None:
prot = project.newProtocol(Prot,
objLabel=label,
filesPath=os.path.join(fnDir,fnMap),
samplingRate=Ts,
setOrigCoord=True,
x=mapCoordX,
y=mapCoordY,
z=mapCoordZ)
else:
prot = project.newProtocol(Prot,
objLabel=label,
filesPath=os.path.join(fnDir,fnMap),
samplingRate=Ts,
setOrigCoord=False)
if fnMap1 is not None and fnMap2 is not None:
prot.setHalfMaps.set(True)
prot.half1map.set(fnMap1)
prot.half2map.set(fnMap2)
if useSlurm:
sendToSlurm(prot, priority=True if priority else False)
project.launchProtocol(prot)
#waitOutput(project, prot, 'outputVolume')
waitUntilFinishes(project, prot)
saveIntermediateData(report.fnReportDir, 'inputData', True, 'map', str(prot.filesPath), 'map from EMDB')
if fnMap1 is not None and fnMap2 is not None:
saveIntermediateData(report.fnReportDir, 'inputData', True, 'map', str(prot.half1map), 'halfmap1 from EMDB')
saveIntermediateData(report.fnReportDir, "inputData", True, "map", str(prot.half2map), 'halfmap2 from EMDB')
return prot
def createMask(project, label, map, Ts, threshold, smooth=False, priority=False):
Prot = pwplugin.Domain.importFromPlugin('xmipp3.protocols.protocol_preprocess',
'XmippProtCreateMask3D', doRaise=True)
prot = project.newProtocol(Prot,
objLabel=label,
inputVolume=map,
threshold=threshold,
doBig=True,
doMorphological=True,
doSmooth=True if smooth else False,
sigmaConvolution=2.0 if smooth else None,
elementSize=math.ceil(2/Ts) if Ts else 1) # Dilation by 2A
if useSlurm:
sendToSlurm(prot, priority=True if priority else False)
project.launchProtocol(prot)
#waitOutput(project, prot, 'outputMask')
waitUntilFinishes(project, prot)
return prot
def createMaskedMap(report, map, mask):
V = xmipp3.Image(map.getFileName()).getData()
M = xmipp3.Image(mask.getFileName()).getData()
save = xmipp3.Image()
save.setData(np.multiply(V,M))
fnMaskedMap = os.path.join(report.getReportDir(),"maskedMap.mrc")
save.write(fnMaskedMap)
return fnMaskedMap
def createResizedMaskedMap(report, resizedMap, resizedMask):
V = xmipp3.Image(resizedMap.getFileName()).getData()
M = xmipp3.Image(resizedMask.getFileName()).getData()
save = xmipp3.Image()
save.setData(np.multiply(V,M))
fnResizedMaskedMap = os.path.join(report.getReportDir(),"resizedMaskedMap.mrc")
save.write(fnResizedMaskedMap)
return fnResizedMaskedMap
def resizeMap(project, protMap, resolution, priority=False):
Xdim = protMap.outputVolume.getDim()[0]
Ts = protMap.outputVolume.getSamplingRate()
AMap = Xdim * Ts
Prot = pwplugin.Domain.importFromPlugin('xmipp3.protocols',
'XmippProtCropResizeVolumes', doRaise=True)
if resolution:
TsTarget = resolution / 2
Xdimp = AMap / TsTarget
Xdimp = int(2 * math.floor(Xdimp / 2))
protResizeMap = project.newProtocol(Prot,
objLabel="Resize Volume Ts=%2.1f"%TsTarget,
doResize=True,
resizeSamplingRate=TsTarget,
doWindow=True,
windowOperation=1,
windowSize=Xdimp)
else:
TsTarget = None
protResizeMap = project.newProtocol(Prot,
objLabel="Resize Volume Factor=1",
doResize=True,
resizeOption=2,
resizeFactor=1)
protResizeMap.inputVolumes.set(protMap.outputVolume)
if useSlurm:
sendToSlurm(protResizeMap, priority=True if priority else False)
project.launchProtocol(protResizeMap)
#waitOutput(project, protResizeMap, 'outputVol')
waitUntilFinishes(project, protResizeMap)
projectPath = os.path.join(project.getPath())
subprocess.call(['chmod', '-R', 'o+r', projectPath])
return protResizeMap, TsTarget
def properMask(mask):
M = readMap(mask.getFileName()).getData()
totalMass = np.sum(M)
if totalMass > 0:
return True
else:
return False
def massAnalysis(report, volume, mask, Ts):
V = readMap(volume.getFileName()).getData()
M = readMap(mask.getFileName()).getData()
Z,Y,X = M.shape
ix = np.where(np.sum(M,axis=(0,1))>0)[0]
iy = np.where(np.sum(M,axis=(0,2))>0)[0]
iz = np.where(np.sum(M,axis=(1,2))>0)[0]
x0 = Ts*(ix[0]) # Left space
xF = Ts*(X-ix[-1]) # Right space
y0 = Ts*(iy[0])
yF = Ts*(Y-iy[-1])
z0 = Ts*(iz[0])
zF = Ts*(Z-iz[-1])
dx = abs(xF-x0)/(Ts*X)*100
dy = abs(yF-y0)/(Ts*Y)*100
dz = abs(zF-z0)/(Ts*Z)*100
secLabel = "sec:massAnalysis"
toWrite=\
"""
\\subsection{Level 0.a Mass analysis}
\\label{%s}
\\textbf{Explanation:}\\\\
The reconstructed map must be relatively well centered in the box, and there should be at least 30\AA~(the exact size
depends on the CTF) on each side to make sure that the CTF can be appropriately corrected.
\\\\
\\\\
"""%(secLabel)
#test
totalMass = np.sum(M)
if totalMass > 0:
ix = np.where(np.sum(M,axis=(0,1))>0)[0]
iy = np.where(np.sum(M,axis=(0,2))>0)[0]
iz = np.where(np.sum(M,axis=(1,2))>0)[0]
x0 = Ts*(ix[0]) # Left space
xF = Ts*(X-ix[-1]) # Right space
y0 = Ts*(iy[0])
yF = Ts*(Y-iy[-1])
z0 = Ts*(iz[0])
zF = Ts*(Z-iz[-1])
dx = abs(xF-x0)/(Ts*X)*100
dy = abs(yF-y0)/(Ts*Y)*100
dz = abs(zF-z0)/(Ts*Z)*100
toWrite+= \
"""
\\textbf{Results:}\\\\
The space from the left and right in X are %6.2f and %6.2f \AA, respectively.
There is a decentering ratio (abs(Right-Left)/Size)\\%% of %5.2f\\%%\\\\
\\\\
The space from the left and right in Y are %6.2f and %6.2f \AA, respectively.
There is a decentering ratio (abs(Right-Left)/Size)\\%% of %5.2f\\%%\\\\
\\\\
The space from the left and right in Z are %6.2f and %6.2f \AA, respectively.
There is a decentering ratio (abs(Right-Left)/Size)\\%% of %5.2f\\%%\\\\
\\\\
"""%(x0, xF, dx, y0, yF, dy, z0, zF, dz)
saveIntermediateData(report.fnReportDir, "massAnalysis", False, "leftSpaceX", x0, ['\u212B', 'Space to the left of the reconstructed map in the box on the x-axis in Angstroms'])
saveIntermediateData(report.fnReportDir, "massAnalysis", False, "rightSpaceX", xF, ['\u212B', 'Space to the right of the reconstructed map in the box on the x-axis in Angstroms'])
saveIntermediateData(report.fnReportDir, "massAnalysis", False, "decenteringRatioX", dx, ['%', '(abs(Right-Left)/Size) %']) # 'details': [units, description]
saveIntermediateData(report.fnReportDir, "massAnalysis", False, "leftSpaceY", y0, ['\u212B', 'Space to the left of the reconstructed map in the box on the y-axis in Angstroms'])
saveIntermediateData(report.fnReportDir, "massAnalysis", False, "rightSpaceY", yF, ['\u212B', 'Space to the right of the reconstructed map in the box on the y-axis in Angstroms'])
saveIntermediateData(report.fnReportDir, "massAnalysis", False, "decenteringRatioY", dy, ['%', '(abs(Right-Left)/Size) %'])
saveIntermediateData(report.fnReportDir, "massAnalysis", False, "leftSpaceZ", z0, ['\u212B', 'Space to the left of the reconstructed map in the box on the z-axis in Angstroms'])
saveIntermediateData(report.fnReportDir, "massAnalysis", False, "rightSpaceZ", zF, ['\u212B', 'Space to the right of the reconstructed map in the box on the z-axis in Angstroms'])
saveIntermediateData(report.fnReportDir, "massAnalysis", False, "decenteringRatioZ", dz, ['%', '(abs(Right-Left)/Size) %'])
# Analysis of Center of mass
cz, cy, cx = scipy.ndimage.measurements.center_of_mass(V)
dcx = abs(cx-X/2)/X*100
dcy = abs(cy-Y/2)/Y*100
dcz = abs(cz-Z/2)/Z*100
toWrite += \
"""
The center of mass is at (x,y,z)=(%6.2f,%6.2f,%6.2f). The decentering of the center of mass (abs(Center)/Size)\\%% is
%5.2f, %5.2f, and %5.2f, respectively.\\\\
"""%(cx,cy,cz,dcx,dcy,dcz)
saveIntermediateData(report.fnReportDir, "massAnalysis", False, "centerOfMass", [cx,cy,cz], ['\u212B', '(x,y,z)'])
saveIntermediateData(report.fnReportDir, "massAnalysis", False, "decenteringCenterOfMass", [dcx,dcy,dcz], ['%', '(abs(Right-Left)/Size) %'])
warnings=[]
testWarnings = False
if dx>20 or testWarnings:
warnings.append("{\\color{red} \\textbf{The volume might be significantly decentered in X.}}")
if dy>20 or testWarnings:
warnings.append("{\\color{red} \\textbf{The volume might be significantly decentered in Y.}}")
if dz>20 or testWarnings:
warnings.append("{\\color{red} \\textbf{The volume might be significantly decentered in Z.}}")
if x0<20 or testWarnings:
warnings.append("{\\color{red} \\textbf{There could be little space from X left to effectively correct for the CTF.}}")
if y0<20 or testWarnings:
warnings.append("{\\color{red} \\textbf{There could be little space from Y left to effectively correct for the CTF.}}")
if z0<20 or testWarnings:
warnings.append("{\\color{red} \\textbf{There could be little space from Z left to effectively correct for the CTF.}}")
if xF<20 or testWarnings:
warnings.append("{\\color{red} \\textbf{There could be little space from X right to effectively correct for the CTF.}}")
if yF<20 or testWarnings:
warnings.append("{\\color{red} \\textbf{There could be little space from Y right to effectively correct for the CTF.}}")
if zF<20 or testWarnings:
warnings.append("{\\color{red} \\textbf{There could be little space from Z right to effectively correct for the CTF.}}")
if dcx>20 or testWarnings:
warnings.append("{\\color{red} \\textbf{The center of mass in X may be significantly shifted. This is common when the refinement is applied exclusively to one protein region.}}")
if dcy>20 or testWarnings:
warnings.append("{\\color{red} \\textbf{The center of mass in Y may be significantly shifted. This is common when the refinement is applied exclusively to one protein region.}}")
if dcz>20 or testWarnings:
warnings.append("{\\color{red} \\textbf{The center of mass in Z may be significantly shifted. This is common when the refinement is applied exclusively to one protein region.}}")
else:
warnings = []
warnings.append("{\\color{red} \\textbf{Threshold parameter is too high, try to lower it.}}")
report.write(toWrite)
msg=\
"""\\textbf{Automatic criteria}: The validation is OK if 1) the decentering and center of mass less than 20\\% of the map
dimensions in all directions, and 2) the extra space on each direction is more than 20\\% of the map dimensions. For local
and focused refinement, or similar, warnings are expected.
\\\\
"""
report.write(msg)
report.writeWarningsAndSummary(warnings, "0.a Mass analysis", secLabel)
if len(warnings)==0:
report.writeAbstract("The map seems to be well centered. ")
else:
report.writeAbstract("The map seems to have some problem in its centering or extra space (see Sec. "\
"\\ref{%s}). "%secLabel)
def maskAnalysis(report, volume, mask, Ts, threshold):
V = readMap(volume.getFileName()).getData()
Ts3 = math.pow(Ts,3)
# Analysis of the raw mask
rawM = np.where(V>=threshold,1,0)
# Connected components
structure = np.ones((3, 3, 3), dtype=np.int64)
labeled, ncomponents = scipy.ndimage.measurements.label(rawM, structure)
sumRawM=np.sum(rawM)
secLabel="sec:maskAnalysis"
toWrite=\
"""
\\subsection{Level 0.b Mask analysis}
\\label{%s}
\\textbf{Explanation:}\\\\
The map at the suggested threshold should have most of its mass concentrated in a single connected component.
It is normal that after thresholding there are a few thousands of very small, disconnected noise blobs. However,
there total mass should not exceed 10\\%%.
The raw mask (just thresholding) and the mask constructed for the analysis (thresholding +
largest connected component + dilation) should significantly overlap. Overlap is defined by the overlapping coefficient
(size(Raw AND Constructed)/size(Raw)) that is a number between 0 and 1, the closer to 1, the more they
agree.
\\\\
\\\\
\\textbf{Results:}\\\\
\\\\
\\underline{Raw mask}: At threshold %f, there are %d connected components with a total number of voxels of %d and
a volume of %5.2f \\AA$^3$ (see Fig. \\ref{fig:rawMask}).
The size and percentage of the total number of voxels for the raw mask are listed below (up to 95\\%% of the mass or
the first 100 clusters, whatever happens first),
the list contains (No. voxels (volume in \AA$^3$), percentage, cumulated percentage):\\\\
\\\\
"""%(secLabel,threshold, ncomponents, sumRawM, sumRawM*Ts3)
saveIntermediateData(report.fnReportDir, "maskAnalysis", False, "connectedComponents", ncomponents, ['', ''])
saveIntermediateData(report.fnReportDir, "maskAnalysis", False, "sizeCC", int(sumRawM), ['voxels', 'Size in terms of number of voxels of connected components']) # convert to int() since int64 is not json serializable
saveIntermediateData(report.fnReportDir, "maskAnalysis", False, "volumeCC", sumRawM*Ts3, ['\u212B\u00B3', 'Connected components volume in cubic Angstroms'])
# individualMass = [np.sum(labeled==i) for i in range(1,ncomponents+1)]
individualMass = np.zeros(ncomponents+1)
for l in np.nditer(labeled):
individualMass[l]+=1
idx = np.argsort(-np.asarray(individualMass)) # Minus is for sorting i descending order
cumulatedMass = 0
i = 0
toWrite2 = ""
nvoxels95 = []
volume95 = []
percentage95 = []
cumulatedPercentage = []
while cumulatedMass/sumRawM<0.95:
if idx[i]>0:
massi = individualMass[idx[i]]
cumulatedMass += massi
if i<100:
if len(toWrite2)>0:
toWrite2 += ", "
toWrite+="(%d (%5.2f), %5.2f, %5.2f)"%(massi, massi*Ts3, 100.0*massi/sumRawM, 100.0*cumulatedMass/sumRawM)
nvoxels95.append(massi)
volume95.append(massi*Ts3)
percentage95.append(100.0*massi/sumRawM)
cumulatedPercentage.append(100.0*cumulatedMass/sumRawM)
i+=1
toWrite += toWrite2
ncomponents95 = i -1
toWrite+="\\\\ \\\\Number of components to reach 95\\%% of the mass: %d\\\\ \\\\"%ncomponents95
ncomponentsRemaining = ncomponents-ncomponents95
voxelsRemaining = sumRawM-cumulatedMass
avgVolumeRemaining = voxelsRemaining/voxelsRemaining*Ts3
maxVolumeRemaining = individualMass[idx[ncomponents95]]*Ts3
minVolumeRemaining = individualMass[idx[-1]]*Ts3
toWrite+="The average size of the remaining %d components is %5.2f voxels (%5.2f \AA$^3$). "\
"Their size go from %d voxels (%5.2f \AA$^3$) to %d voxels (%5.2f \AA$^3$). \\\\ \\\\"%\
(ncomponentsRemaining, voxelsRemaining/ncomponentsRemaining, avgVolumeRemaining,
int(individualMass[idx[ncomponents95]]), maxVolumeRemaining,
int(individualMass[idx[-1]]), minVolumeRemaining)
report.write(toWrite)
saveIntermediateData(report.fnReportDir, "maskAnalysis", False, "size95", nvoxels95, ['voxels', 'Sizes in terms of number of voxels for the list components to reach the 95% of the mass or the first 100 clusters (whatever happens first)'])
saveIntermediateData(report.fnReportDir, "maskAnalysis", False, "volume95", volume95, ['\u212B\u00B3', 'Volumes in cubic Angstroms for the list components to reach the 95% of the mass or the first 100 clusters (whatever happens first)'])
saveIntermediateData(report.fnReportDir, "maskAnalysis", False, "percentage95", percentage95, ['%', 'Percentages for the list components to reach the 95% of the mass or the first 100 clusters (whatever happens first)'])
saveIntermediateData(report.fnReportDir, "maskAnalysis", False, "cumulatedPercentage", cumulatedPercentage, ['%', 'Cumulated percentages for the list components to reach the 95% of the mass or the first 100 clusters (whatever happens first)'])
saveIntermediateData(report.fnReportDir, "maskAnalysis", False, "connectedComponents95", ncomponents95, ['', 'Number of components to reach 95% of the mass'])
saveIntermediateData(report.fnReportDir, "maskAnalysis", False, "ncomponentsRemaining", ncomponentsRemaining, ['', 'Number of remaining components to reach 100% of the mass'])
saveIntermediateData(report.fnReportDir, "maskAnalysis", False, "averageSizeComponentsRemaining", voxelsRemaining/ncomponentsRemaining, ['voxels', 'Average size in terms of number of voxels for the remaining components'])
saveIntermediateData(report.fnReportDir, "maskAnalysis", False, "volumeComponentsRemaining", avgVolumeRemaining, ['\u212B\u00B3', 'Average volume in cubic Angstroms of the remaining components'])
saveIntermediateData(report.fnReportDir, "maskAnalysis", False, "maxSizeComponentsRemaining", int(individualMass[idx[ncomponents95]]), ['voxels', 'Max size in terms of number of voxels of the remaining components'])
saveIntermediateData(report.fnReportDir, "maskAnalysis", False, "maxVolumeComponentsRemaining", maxVolumeRemaining, ['\u212B\u00B3', 'Max volume in cubic Angstroms of the remaining components'])
saveIntermediateData(report.fnReportDir, "maskAnalysis", False, "minSizeComponentsRemaining", int(individualMass[idx[-1]]), ['voxels', 'Min size in terms of number of voxels of the remaining components'])
saveIntermediateData(report.fnReportDir, "maskAnalysis", False, "minVolumeComponentsRemaining", minVolumeRemaining, ['\u212B\u00B3', 'Min volume in cubic Angstroms of the remaining components'])
msg = "The slices of the raw mask can be seen in Fig. \\ref{fig:rawMask}.\\\\"
report.orthogonalSlices("rawMask", msg, "Maximum variance slices in the three dimensions of the raw mask", rawM,
"fig:rawMask", maxVar=True)
# Analysis of different thresholds
maxVal = np.max(V)
stepGray = maxVal/25
g = np.arange(stepGray, maxVal, stepGray)
w = np.zeros(g.size)
toWrite="\nThe following table shows the variation of the mass enclosed at different thresholds "\
"(see Fig. \\ref{fig:mass}):\n\n"
toWrite+="\\begin{small}\n"
toWrite+="\\begin{center}\n"
toWrite+="\\begin{tabular}{|c|c|c|c|}\n"
toWrite+="\\hline\n"
toWrite+="\\textbf{Threshold} & \\textbf{Voxel mass} & \\textbf{Molecular mass(kDa)} & \\textbf{\\# Aminoacids}\\\\ \n"
toWrite+="\\hline\n"
mm = []
aa = []
for i in range(g.size):
w[i] = np.sum(V>g[i])
toWrite+="%5.4f & %5.2f & %5.2f & %5.2f \\\\ \n"%(g[i], w[i], (w[i]*Ts3/(1.207*1000)), w[i]*Ts3/(110*1.207))
mm.append(w[i]*Ts3/(1.207*1000))
aa.append(w[i]*Ts3/(110*1.207))
toWrite+="\\hline\n"
toWrite += "\\end{tabular}\n"
toWrite += "\\end{center}\n\n"
toWrite += "\\end{small}\n"
fnFigMass = os.path.join(report.getReportDir(), "mass.png")
toWrite +=\
"""
\\begin{figure}[H]
\centering
\includegraphics[width=10cm]{%s}
\\caption{Voxel mass as a function of the gray level.}
\\label{fig:mass}
\\end{figure}
"""%fnFigMass
report.write(toWrite)
reportPlot(g,w, 'Gray level', 'Voxel mass', fnFigMass, yscale="log")
saveIntermediateData(report.fnReportDir, "maskAnalysis", False, "grayLevel", g.tolist(), ['', 'List of thresholds in table and plot'])
saveIntermediateData(report.fnReportDir, "maskAnalysis", False, "voxelMass", w.tolist(), ['voxels', 'List of voxel mass in table and plot'])
saveIntermediateData(report.fnReportDir, "maskAnalysis", False, "molecularMass", mm, ['kDa', ''])
saveIntermediateData(report.fnReportDir, "maskAnalysis", False, "Aminoacids", aa, ['', ''])
saveIntermediateData(report.fnReportDir, "maskAnalysis", True, "massPlot", fnFigMass, 'Voxel mass as a function of the gray level')
# Constructed mask
M = readMap(mask.getFileName()).getData()
sumM = np.sum(M)
overlap = np.sum(np.multiply(M,rawM))/sumRawM
toWrite=\
"""
\\underline{Constructed mask}: After keeping the largest component of the previous mask and dilating it by 2\AA,
there is a total number of voxels of %d and a volume of %5.2f \\AA$^3$. The overlap between the
raw and constructed mask is %5.2f.\\\\
"""%(sumM, sumM*Ts3, overlap)
report.write(toWrite)
saveIntermediateData(report.fnReportDir, "maskAnalysis", False, "sizeConstructedMask", float(sumM), ['voxels', 'Size in terms of number of voxels of constructued mask'])
saveIntermediateData(report.fnReportDir, "maskAnalysis", False, "volumeConstructedMask", sumM*Ts3, ['\u212B\u00B3', 'Volume in cubic Angstroms of the constructed mask'])
saveIntermediateData(report.fnReportDir, "maskAnalysis", False, "overlap", overlap, ['', 'Overlap between raw and constructed mask'])
# Warnings
warnings=[]
testWarnings = False
if ncomponents95>5 or testWarnings:
warnings.append("{\\color{red} \\textbf{There might be a problem of connectivity at this threshold because "\
"more than 5 connected components are needed to reach 95\\% of the total mask. Probably a "\
"smaller threshold will not cause this issue.}}")
if avgVolumeRemaining>5 or testWarnings:
warnings.append("{\\color{red} \\textbf{There might be a problem with noise and artifacts, because the "\
"average noise blob has a volume of %f \AA$^3$.}}"%avgVolumeRemaining)
if overlap<0.75 or testWarnings:
warnings.append("{\\color{red} \\textbf{There might be a problem in the construction of the mask, because the "\
"overlap is smaller than 0.75. A common reason is that the suggested threshold causes too many "\
"disconnected components.}}")
msg = \
"""\\textbf{Automatic criteria}: The validation is OK if 1) to keep 95\\% of the mass we need to keep at most 5
connected components; and 2) the average volume of the blobs outside the given threshold has a size smaller than
5\\AA$^3$; and 3) the overlap between the raw mask and the mask constructed for the analysis is larger than 75\\%.
\\\\
"""
report.write(msg)
report.writeWarningsAndSummary(warnings, "0.b Mask analysis", secLabel)
if len(warnings)==0:
report.writeAbstract("There is no problem with the suggested threshold. ")
else:
report.writeAbstract("There seems to be a problem with the suggested threshold (see Sec. "\
"\\ref{%s}). "%secLabel)
def backgroundAnalysis(report, volume, mask):
V = readMap(volume.getFileName()).getData()
secLabel = "sec:bgAnalysis"
toWrite=\
"""
\\subsection{Level 0.c Background analysis}
\\label{%s}
\\textbf{Explanation:}\\\\
Background is defined as the region outside the macromolecule mask. The background mean should be zero, and the number
of voxels with a very low or very high value (below 5 standard deviations of the noise) should be very small and
they should be randomly distributed without any specific structure. Sometimes, you can see some structure due to
the symmetry of the structure.
\\\\
\\\\
\\textbf{Results:}\\\\
\\\\
"""%secLabel
M = 1-readMap(mask.getFileName()).getData() # Background mask
Vbg = V[M>0]
[t,p] = scipy.stats.ttest_1samp(Vbg,0)
meanBg = np.mean(Vbg)
stdBg = np.std(Vbg)
fractionLarge = np.sum(np.abs(Vbg)>5*stdBg)/Vbg.size
cdf5 = 2*scipy.stats.norm.cdf(-5)
cdf5Ratio = fractionLarge/cdf5
toWrite+="The null hypothesis that the background mean is 0 was tested with a one-sample Student's t-test. The "\
"resulting t-statistic and p-value were %5.2f and %f, respectively.\\\\ \n"\
"\\\\ \n"\
"The mean and standard deviation (sigma) of the background were %f and %f. "\
"The percentage of background voxels whose absolute value is larger than 5 times the standard "\
"deviation is %5.2f \\%% (see Fig. \\ref{fig:sigma5}). The same percentage from a Gaussian would be "\
"%f\\%% (ratio between the two percentages: %f).\\\\ \n\\\\ \n"%\
(t,p,meanBg, stdBg,fractionLarge*100,cdf5*100,cdf5Ratio)
saveIntermediateData(report.fnReportDir, "backgroundAnalysis", False, "t-statistic", t, ['', "t-statistics after testing with a a one-sample Student's t-test the null hypothesis that the background mean is 0"])
saveIntermediateData(report.fnReportDir, "backgroundAnalysis", False, "p-value", p, ['', "p-value after testing with a a one-sample Student's t-test the null hypothesis that the background mean is 0"])
saveIntermediateData(report.fnReportDir, "backgroundAnalysis", False, "mean", float(meanBg), ['', 'The mean of the background (gray level)'])
saveIntermediateData(report.fnReportDir, "backgroundAnalysis", False, "standardDeviation", float(stdBg), ['', 'The standard deviation (sigma) of the background (gray level)'])
saveIntermediateData(report.fnReportDir, "backgroundAnalysis", False, "percentageVoxelsLarger5", fractionLarge*100, ['%', 'The percentage of background voxels whose absolute value is larger than 5 times the standard deviation'])
saveIntermediateData(report.fnReportDir, "backgroundAnalysis", False, "percentageGaussian", cdf5*100, ['%', 'The same percentage from a Gaussian'])
saveIntermediateData(report.fnReportDir, "backgroundAnalysis", False, "percentageRatio", cdf5Ratio, ['', 'Ration between the two percentages'])
report.write(toWrite)
Vshooting = np.where(np.logical_and(M, np.abs(V)>5*stdBg),V,0)
msg = "Slices of the background beyond 5*sigma can be seen in Fig. \\ref{fig:sigma5}.\\\\"
report.orthogonalSlices("sigma5", msg, "Maximum variance slices in the three dimensions of the parts of the "\
"background beyond 5*sigma", Vshooting, "fig:sigma5", maxVar=True)
# Warnings
warnings=[]
testWarnings = False
if p<0.001 or testWarnings:
warnings.append("{\\color{red} \\textbf{The null hypothesis that the background mean is 0 has been rejected "\
"because the p-value of the comparison is smaller than 0.001}}")
if cdf5Ratio>20 or testWarnings:
warnings.append("{\\color{red} \\textbf{There is a significant proportion of outlier values in the background "\
"(cdf5 ratio=%5.2f})}"%cdf5Ratio)
msg = \
"""\\textbf{Automatic criteria}: The validation is OK if 1) the p-value of the null hypothesis that the background
has 0 mean is larger than 0.001; and 2) the number of voxels above or below 5 sigma is smaller than 20 times the
amount expected for a Gaussian with the same standard deviation whose mean is 0.
\\\\
"""
report.write(msg)
report.writeWarningsAndSummary(warnings, "0.c Background analysis", secLabel)
if len(warnings)==0:
report.writeAbstract("There is no problem with its background. ")
else:
report.writeAbstract("There seems to be a problem with the map's background (see Sec. "\
"\\ref{%s}). "%secLabel)
def bFactorAnalysis(project, report, map, resolution, priority=False):
secLabel = "sec:bfactor"
msg = \
"""\\subsection{Level 0.d B-factor analysis}
\\label{%s}
\\textbf{Explanation:}\\\\
The B-factor line (see this \\href{%s}{link} for more details) fitted between 15\AA and the resolution reported should have a slope that
is between 0 and 300 \AA$^2$.
\\\\
\\textbf{Results:}\\\\
\\\\
""" % (secLabel, BFACTOR_GUINIER_DOI)
report.write(msg)
if not resolution:
report.writeSummary("0.d B-factor analysis", secLabel, NOT_APPLY_MESSAGE)
report.write(NOT_APPY_NO_RESOLUTION + STATUS_NOT_APPLY)
return None
if resolution>8:
toWrite = NOT_APPLY_WORSE_RESOLUTION % 8 + STATUS_NOT_APPLY
report.write(toWrite)
report.writeSummary("0.d B-factor analysis", secLabel, NOT_APPLY_MESSAGE)
return
fnIn = os.path.join(project.getPath(), map.getFileName())
if fnIn.endswith(".mrc"):
fnIn+=":mrc"
fnOut = os.path.join(report.getReportDir(), "sharpenedMap.mrc")
Ts = map.getSamplingRate()
args = "-i %s -o %s --sampling %f --maxres %s --auto"%(fnIn, fnOut, Ts, resolution)
scipionHome = getScipionHome()
scipion3 = os.path.join(scipionHome,'scipion3')
cmd = '%s run xmipp_volume_correct_bfactor %s'%(scipion3, args)
if not useSlurm:
p = subprocess.Popen(cmd, shell=True, stderr=subprocess.PIPE)
outputLines = p.stderr.read().decode('utf-8').split('\n')
p.wait()
sleep(120)
else:
randomInt = int(datetime.now().timestamp()) + randint(0, 1000000)
slurmScriptPath = createScriptForSlurm('xmipp_volume_correct_bfactor_level0_' + str(randomInt), report.getReportDir(), cmd, priority=priority)
# send job to queue
subprocess.Popen('sbatch %s' % slurmScriptPath, shell=True)
# check if job has finished
while True:
if checkIfJobFinished('xmipp_volume_correct_bfactor_level0_' + str(randomInt)):
break
sleep(120)
with open(slurmScriptPath.replace('.sh', '.job.err'), 'r') as slurmOutputFile:
outputLines = slurmOutputFile.read().split('\n')
for line in outputLines:
if "Fitted slope=" in line and "intercept=" in line:
tokens = line.split()
a = float(tokens[2])
b = float(tokens[4])
if "Applying B-factor of" in line:
tokens = line.split()
bfactor = float(tokens[3])
dinv2, lnF, lnFc = readGuinier(fnOut+".guinier")
fitted = a*dinv2 + b
fnPlot = os.path.join(report.getReportDir(),'Bfactor.png')
reportMultiplePlots(dinv2, [lnF, fitted, lnFc], '1/Resolution^2 (1/A^2)', 'log Structure factor', fnPlot,
['Experimental', 'Fitted', 'Corrected'])
msg=\
"""
Fig. \\ref{fig:Bfactor} shows the logarithm (in natural units) of the structure factor (the module squared of the
Fourier transform) of the experimental map, its fitted line, and the corrected map. The estimated B-factor was
%5.1f. The fitted line was $\\log(|F|^2)=%4.1f/R^2 + (%4.1f)$.
\\begin{figure}[H]
\centering
\includegraphics[width=10cm]{%s}
\\caption{Guinier plot. The X-axis is the square of the inverse of the resolution in \\AA.}
\\label{fig:Bfactor}
\\end{figure}
"""%(bfactor, a, b, fnPlot)
report.write(msg)
saveIntermediateData(report.fnReportDir, "bFactorAnalysis", False, "bfactor", bfactor, ['\u212B\u207B\u00B2', 'The estimated B-factor'])
saveIntermediateData(report.fnReportDir, "bFactorAnalysis", False, "a", a, ['', ''])
saveIntermediateData(report.fnReportDir, "bFactorAnalysis", False, "b", b, ['', ''])
saveIntermediateData(report.getReportDir(), 'bFactorAnalysis', True, 'sharpenedMap.mrc.guinier', os.path.join(report.getReportDir(), 'sharpenedMap.mrc.guinier'), 'sharpenedMap.mrc.guinier file which contain the data to create the guinier plot')
saveIntermediateData(report.getReportDir(), 'bFactorAnalysis', True, 'guinierPlot', fnPlot, 'guinier plot for B-Factor Analysis')
msg = "\\underline{\\textbf{Orthogonal slices of maximum variance of the B-factor corrected map}}\\\\"\
"\\textbf{Results}:\\\\"\
"See Fig. \\ref{fig:maxVarBfactor}.\\\\"
report.orthogonalSlices("maxVarSlicesBfactor", "", "Slices of maximum variation in the three dimensions of the "\
"B-factor corrected map", fnOut, "fig:maxVarBfactor", maxVar=True)
# Warnings
warnings=[]
testWarnings = False
if bfactor<-300 or bfactor>0 or testWarnings:
warnings.append("{\\color{red} \\textbf{The B-factor is out of the interval [-300,0]. %s}}" % ("It is oversharpened." if bfactor>0 else ""))
msg = \
"""\\textbf{Automatic criteria}: The validation is OK if the B-factor is in the range [-300,0].
\\\\
"""
report.write(msg)
report.writeWarningsAndSummary(warnings, "0.d B-factor analysis", secLabel)
if len(warnings)>0:
report.writeAbstract("There seems to be a problem with its B-factor (see Sec. \\ref{%s}). "%secLabel)
return bfactor
def xmippDeepRes(project, report, label, map, mask, resolution, fnMaskedMap, priority=False):
secLabel = "sec:deepres"
msg = \
"""
\\subsection{Level 0.e Local resolution with DeepRes}
\\label{%s}
\\textbf{Explanation}:\\\\
DeepRes (see this \\href{%s}{link} for more details) measures the local resolution using a neural network that has been trained on
the appearance of atomic structures at different resolutions. Then, by comparing the local appearance of the
input map to the appearance of the atomic structures a local resolution label can be assigned.\\\\
\\\\
\\textbf{Results:}\\\\
\\\\
""" % (secLabel, DEEPRES_DOI)
report.write(msg)
if not resolution:
report.writeSummary("0.e DeepRes", secLabel, NOT_APPLY_MESSAGE)
report.write(NOT_APPY_NO_RESOLUTION + STATUS_NOT_APPLY)
return None
if resolution<2:
report.writeSummary("0.e DeepRes", secLabel, NOT_APPLY_MESSAGE)
report.write(NOT_APPLY_BETTER_RESOLUTION % 2 + STATUS_NOT_APPLY)
return None
if resolution>13:
report.writeSummary("0.e DeepRes", secLabel, NOT_APPLY_MESSAGE)
report.write(NOT_APPLY_WORSE_RESOLUTION % 13 + STATUS_NOT_APPLY)
return None
Prot = pwplugin.Domain.importFromPlugin('xmipp3.protocols',
'XmippProtDeepRes', doRaise=True)
prot = project.newProtocol(Prot,
objLabel=label,
inputVolume=map,
Mask=mask)
if useSlurm:
sendToSlurm(prot, GPU=True, priority=True if priority else False)
project.launchProtocol(prot)
#waitOutput(project, prot, 'resolution_Volume')
waitUntilFinishes(project, prot)
if prot.isFailed():
report.writeSummary("0.e DeepRes", secLabel, ERROR_MESSAGE)
report.write(ERROR_MESSAGE_PROTOCOL_FAILED)
deepresStderr = open(os.path.join(project.getPath(), prot.getStderrLog()), "r").read()
if "ran out of memory trying to allocate" in deepresStderr:
report.write("{\\color{red} \\textbf{REASON: %s.}}\\\\ \n" % "System ran out of memory. Try to launch it again.")
report.write(STATUS_ERROR_MESSAGE)
return prot
if prot.isAborted():
print(PRINT_PROTOCOL_ABORTED + ": " + NAME_DEEPRES)
report.writeSummary("0.e DeepRes", secLabel, ERROR_ABORTED_MESSAGE)
report.write(ERROR_MESSAGE_ABORTED + STATUS_ERROR_ABORTED_MESSAGE)
return prot
fnRes = os.path.join(project.getPath(), prot._getExtraPath("deepRes_resolution.vol"))
if not os.path.exists(fnRes):
report.writeSummary("0.e DeepRes", secLabel, ERROR_MESSAGE)
report.write(ERROR_MESSAGE_PROTOCOL_FAILED + STATUS_ERROR_MESSAGE)
return
fnResOriginal = os.path.join(project.getPath(), prot._getExtraPath("deepRes_resolution_originalSize.vol"))
saveIntermediateData(report.getReportDir(), 'deepRes', True, 'deepRes_resolution_originalSize.vol', fnResOriginal, 'deepRes output volume map')
Vres = xmipp3.Image(fnRes).getData()
R = Vres[Vres >0]
fnHist = os.path.join(report.getReportDir(), "deepresHist.png")
reportHistogram(R, "Local resolution (A)", fnHist)
Rpercentiles = np.percentile(R, np.array([0.025, 0.25, 0.5, 0.75, 0.975])*100)
resolutionP = np.sum(R < resolution) / R.size * 100
report.addResolutionEstimate(Rpercentiles[2])
toWrite = \
"""
Fig. \\ref{fig:histDeepres} shows the histogram of the local resolution according to DeepRes. Some representative
percentiles are:
\\begin{center}
\\begin{tabular}{|c|c|}
\\hline
\\textbf{Percentile} & \\textbf{Resolution(\AA)} \\\\
\\hline
2.5\\%% & %5.2f \\\\
\\hline
25\\%% & %5.2f \\\\
\\hline
50\\%% & %5.2f \\\\
\\hline
75\\%% & %5.2f \\\\
\\hline
97.5\\%% & %5.2f \\\\
\\hline
\\end{tabular}
\\end{center}
The reported resolution, %5.2f \AA, is at the percentile %4.1f.
Fig. \\ref{fig:deepresColor} shows some representative views of the local resolution.
\\begin{figure}[H]
\centering
\includegraphics[width=10cm]{%s}
\\caption{Histogram of the local resolution according to deepres.}
\\label{fig:histDeepres}
\\end{figure}
""" % (Rpercentiles[0], Rpercentiles[1], Rpercentiles[2], Rpercentiles[3], Rpercentiles[4], resolution,
resolutionP, fnHist)
report.write(toWrite)
saveIntermediateData(report.getReportDir(), 'deepRes', False, 'resolutionPercentiles', Rpercentiles.tolist(), ['\u212B', 'List of local resolution in Angstroms at percentiles 2.5%, 25%, 50%, 75% and 97.5 %'])
saveIntermediateData(report.getReportDir(), 'deepRes', False, 'resolutionPercentile', resolutionP, ['%', 'The percentile at which the reported resolution is'])
saveIntermediateData(report.getReportDir(), 'deepRes', False, 'resolutionList', R.tolist(), ['\u212B', 'List of local resolution in Angstroms obtained from DeepRes to create the histogram'])
saveIntermediateData(report.getReportDir(), 'deepRes', False, 'estimatedResolution', Rpercentiles[2], ['\u212B', 'The estimated resolution (median) in Angstroms obtained from DeepRes'])
saveIntermediateData(report.getReportDir(), 'deepRes', True, 'deepResHist', fnHist, 'deepRes histogram')
Ts = 1 # Res volume and original volume are at different scales
report.colorIsoSurfaces("", "Local resolution according to DeepRes.", "fig:deepresColor",
project, "deepresViewer",
os.path.join(project.getPath(), prot._getExtraPath("originalVolume.vol")),
Ts,
os.path.join(project.getPath(), prot._getExtraPath("chimera_resolution.vol")),
Rpercentiles[0], Rpercentiles[-1])
saveIntermediateData(report.getReportDir(), 'deepRes', True, 'deepResViewer',
[os.path.join(report.getReportDir(), 'deepresViewer1.jpg'),
os.path.join(report.getReportDir(), 'deepresViewer2.jpg'),
os.path.join(report.getReportDir(), 'deepresViewer3.jpg')], 'deepRes views')
# Warnings
warnings = []
testWarnings = False
RperHomogeneous = isHomogeneous(Rpercentiles[0], Rpercentiles[-1])
if RperHomogeneous:
warnings.append("{\\color{red} \\textbf{Program output seems to be too homogeneous. There might " \
"be some program issues analyzing the data.}}")
if resolutionP < 0.1 or testWarnings:
warnings.append("{\\color{red} \\textbf{The reported resolution, %5.2f \\AA, is particularly high with respect " \
"to the local resolution distribution. It occupies the %5.2f percentile}}" % \
(resolution, resolutionP))
msg = \
"""\\textbf{Automatic criteria}: The validation is OK if the percentile of the user provided resolution is larger than
0.1\\% of the percentile of the local resolution as estimated by DeepRes.
\\\\
"""
report.write(msg)
report.writeWarningsAndSummary(warnings, "0.e DeepRes", secLabel)
return prot
def locBfactor(project, report, label, map, mask, resolution, fnResizedMaskedMap, priority=False):
secLabel = "sec:locbfactor"
msg = \
"""
\\subsection{Level 0.f Local B-factor}
\\label{%s}
\\textbf{Explanation}:\\\\
LocBfactor (see this \\href{%s}{link} for more details) estimates a local resolution B-factor by decomposing the input map into a
local magnitude and phase term using the spiral transform.\\\\
\\\\
\\textbf{Results:}\\\\
\\\\
""" % (secLabel, LOCBFACTOR_LOCOCCUPANCY_DOI)
report.write(msg)
if not resolution:
report.writeSummary("0.f Local B-factor", secLabel, NOT_APPLY_MESSAGE)
report.write(NOT_APPY_NO_RESOLUTION + STATUS_NOT_APPLY)
return None
Prot = pwplugin.Domain.importFromPlugin('ucm.protocols',
'ProtLocBFactor', doRaise=True)
prot = project.newProtocol(Prot,
objLabel=label,
vol=map,
mask_in_molecule=mask,
max_res=resolution,
numberOfThreads=N_THREADS)
if useSlurm:
sendToSlurm(prot, priority=True if priority else False)
project.launchProtocol(prot)
#waitOutput(project, prot, 'bmap')
#waitOutputFile(project, prot, "bmap.mrc")
waitUntilFinishes(project, prot)
fnBfactor = prot._getExtraPath("bmap.mrc")
if prot.isFailed() or not os.path.exists(fnBfactor):
report.writeSummary("0.f LocBfactor", secLabel, ERROR_MESSAGE)
report.write(ERROR_MESSAGE_PROTOCOL_FAILED + STATUS_ERROR_MESSAGE)
return prot
if prot.isAborted():
print(PRINT_PROTOCOL_ABORTED + ": " + NAME_LOCBFACTOR)
report.writeSummary("0.f LocBfactor", secLabel, ERROR_ABORTED_MESSAGE)
report.write(ERROR_MESSAGE_ABORTED + STATUS_ERROR_ABORTED_MESSAGE)
return prot
fnBfactorAbs = os.path.join(project.getPath(), fnBfactor)
saveIntermediateData(report.getReportDir(), 'locBfactor', True, 'bmap.mrc', fnBfactorAbs, 'Local b factor output volume map')
V = xmipp3.Image(fnBfactor+":mrc").getData()
M = xmipp3.Image(mask.getFileName()).getData()
B = V[M>0.5]
fnHist = os.path.join(report.getReportDir(),"locBfactorHist.png")
reportHistogram(B, "Local B-factor (A^-2)", fnHist)
Bpercentiles = np.percentile(B, np.array([0.025, 0.25, 0.5, 0.75, 0.975])*100)
toWrite = \
"""
Fig. \\ref{fig:histLocBfactor} shows the histogram of the local B-factor according to LocBfactor. Some representative
percentiles are:
\\begin{center}
\\begin{tabular}{|c|c|}
\\hline
\\textbf{Percentile} & \\textbf{Local B-factor (\AA$^{-2}$)} \\\\
\\hline
2.5\\%% & %5.2f \\\\
\\hline
25\\%% & %5.2f \\\\
\\hline
50\\%% & %5.2f \\\\
\\hline
75\\%% & %5.2f \\\\
\\hline
97.5\\%% & %5.2f \\\\
\\hline
\\end{tabular}
\\end{center}
Fig. \\ref{fig:locBfactorColor} shows some representative views of the local B-factor.
\\begin{figure}[H]
\centering
\includegraphics[width=10cm]{%s}
\\caption{Histogram of the local B-factor according to LocBfactor.}
\\label{fig:histLocBfactor}
\\end{figure}
""" % (Bpercentiles[0], Bpercentiles[1], Bpercentiles[2], Bpercentiles[3], Bpercentiles[4], fnHist)
report.write(toWrite)
saveIntermediateData(report.getReportDir(), 'locBfactor', False, 'bfactorPercentiles', Bpercentiles.tolist(), ['\u212B\u207B\u00B2', 'List of local resolution B-factor in Angstroms^-2 at percentiles 2.5%, 25%, 50%, 75% and 97.5 %'])
saveIntermediateData(report.getReportDir(), 'locBfactor', False, 'bfactorList', B.tolist(), ['\u212B\u207B\u00B2', 'List of local resolution B-factor in Angstroms^-2 to create the histogram'])
saveIntermediateData(report.getReportDir(), 'locBfactor', True, 'locBfactorHist', fnHist, 'locBfactor histogram')
Ts = map.getSamplingRate()
report.colorIsoSurfaces("", "Local B-factor according to LocBfactor.", "fig:locBfactorColor",
project, "locBfactorViewer",
fnResizedMaskedMap, Ts,
fnBfactor, Bpercentiles[0], Bpercentiles[-1])
saveIntermediateData(report.getReportDir(), 'locBfactor', True, 'locBfactorViewer',
[os.path.join(report.getReportDir(), 'locBfactorViewer1.jpg'),
os.path.join(report.getReportDir(), 'locBfactorViewer2.jpg'),
os.path.join(report.getReportDir(), 'locBfactorViewer3.jpg')], 'locBfactor views')
# Warnings
warnings=[]
testWarnings = False
BperHomogeneous = isHomogeneous(Bpercentiles[0], Bpercentiles[-1])
if BperHomogeneous:
warnings.append("{\\color{red} \\textbf{Program output seems to be too homogeneous. There might " \
"be some program issues analyzing the data.}}")
if Bpercentiles[2]<-300 or Bpercentiles[2]>0 or testWarnings:
warnings.append("{\\color{red} \\textbf{The median B-factor is out of the interval [-300,0]}}")
msg = \
"""\\textbf{Automatic criteria}: The validation is OK if the median B-factor is in the range [-300,0].
\\\\
"""
report.write(msg)
report.writeWarningsAndSummary(warnings, "0.f LocBfactor", secLabel)
if len(warnings)>0:
report.writeAbstract("There seems to be a problem with its local B-factor (see Sec. \\ref{%s}). "%secLabel)