-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvalidationReport.py
More file actions
1080 lines (937 loc) · 37.6 KB
/
validationReport.py
File metadata and controls
1080 lines (937 loc) · 37.6 KB
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
import hashlib
from math import radians
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import os
import scipy
import subprocess
import PIL
import json
import requests
from pyworkflow.protocol import StringParam
from pyworkflow.utils.path import makePath, cleanPath
from pwem.viewers import LocalResolutionViewer
from pwem.emlib.metadata import iterRows
from tools.utils import storeIntermediateData
import xmipp3
import configparser
from resources.constants import *
def get_env_bool(var_name, default=None):
val = os.getenv(var_name)
if val is None:
return default
if val.strip().lower() in ('1', 'true', 'yes', 'on'):
return True
elif val.strip().lower() in ('0', 'false', 'no', 'off'):
return False
def get_env_int(var_name, default=None):
val = os.getenv(var_name)
try:
return int(val)
except (ValueError, TypeError):
return default
config = configparser.ConfigParser()
config.read(os.path.join(os.path.dirname(__file__), 'config.yaml'))
max_mem_to_use = get_env_int('CHIMERA_MAX_MEM_TO_USE') or config['CHIMERA'].getint('MAX_MEM_TO_USE')
max_voxels_to_open = get_env_int('CHIMERA_MAX_VOXELS') or config['CHIMERA'].getint('MAX_VOXELS')
use_virtual_display = get_env_bool('CHIMERA_USE_VIRTUAL_DISPLAY') or config['CHIMERA'].getboolean('USE_VIRTUAL_DISPLAY')
virtual_display_port = get_env_int('CHIMERA_VIRTUAL_DISPLAY_PORT') or config['CHIMERA'].getint('VIRTUAL_DISPLAY_PORT')
def safeNeg(value):
return -value if value is not None else None
def escapeLatexSpecialChars(text):
special_chars = ['#', '$', '%', '&', '_', '{', '}']
# Replace special character to escape them in latex
for char in special_chars:
text = text.replace(char, f"\\{char}")
return text
def isHomogeneous(value1, value2, eps=0.01):
if abs(value1-value2) < eps:
isHomogeneous = True
else:
isHomogeneous = False
return isHomogeneous
def writeAfterReferenceLine(fn, newLines, referenceLine):
with open(fn, 'r') as file:
content = file.readlines()
updated_content = []
for line in content:
updated_content.append(line)
if referenceLine in line:
updated_content.extend(newLines)
with open(fn, 'w') as file:
file.writelines(updated_content)
def readMap(fnMap):
return xmipp3.Image(fnMap)
def readStack(fnXmd):
md = xmipp3.MetaData(fnXmd)
S = None
i=0
for objId in md:
I = xmipp3.Image(md.getValue(xmipp3.MDL_IMAGE, objId))
Xdim, Ydim, Zdim, Ndim = I.getDimensions()
if S is None:
S=np.zeros((md.size(), Ydim, Xdim))
S[i,:,:]=I.getData()
i+=1
return S
def readGuinier(fnGuinier):
fh = open(fnGuinier)
lineNo = 0
content = []
for line in fh.readlines():
if lineNo > 0:
content.append([float(x) for x in line.split()])
lineNo += 1
X = np.array(content)
fh.close()
dinv2 = X[:, 0]
lnF = X[:, 1]
lnFc = X[:, 3]
return dinv2, lnF, lnFc
def writeImage(I, fnOut, scale=True):
if scale:
I = I.astype(np.float64)
I -= np.min(I)
I *= 255/np.max(I)
Iout = PIL.Image.fromarray(I.astype(np.uint8)[::-1])
Iout.save(fnOut)
def latexItemize(itemList):
toWrite="\\begin{itemize}\n"
for item in itemList:
toWrite+=" \item %s\n"%item
toWrite+="\\end{itemize}\n\n"
return toWrite
def latexEnumerate(itemList):
toWrite="\\begin{enumerate}\n"
for item in itemList:
toWrite+=" \item %s\n"%item
toWrite+="\\end{enumerate}\n\n"
return toWrite
def generateChimeraView(fnWorkingDir, fnMap, fnView, isMap=True, threshold=0, angX=0, angY=0, angZ=0, bfactor=False,\
occupancy=False, otherAttribute=[], rainbow=True, legendMin=None, legendMax=None):
chimeraScript=\
"""
windowsize 1300 700
set bgColor white
"""
if isMap:
chimeraScript+=\
"""
volume dataCacheSize %d
volume voxelLimitForOpen %d
volume showPlane false
""" % (max_mem_to_use, max_voxels_to_open)
chimeraScript+=\
"""
open %s
""" % fnMap
if isMap:
chimeraScript+=\
"""show #1 models
volume #1 level %f
volume #1 color #4e9a06
lighting soft
"""%threshold
else:
chimeraScript+=\
"""hide atoms
show cartoons
"""
if bfactor:
chimeraScript+="color bfactor\n"
if occupancy:
chimeraScript += "color byattribute occupancy"
if rainbow:
chimeraScript += " palette rainbow\n"
if legendMin and legendMax:
chimeraScript += " palette bluered range %s,%s\n" % (legendMin, legendMax)
else:
chimeraScript += "\n"
if len(otherAttribute) > 0:
chimeraScript += "open %s\n" % otherAttribute[0]
chimeraScript += "color byattribute %s" % otherAttribute[1]
if legendMin and legendMax:
chimeraScript += " palette bluered range %s,%s\n" % (legendMin, legendMax)
else:
chimeraScript += "\n"
if legendMin and legendMax:
chimeraScript += "key blue:%s white: red:%s fontSize 15 size 0.025,0.4 pos 0.01,0.3\n" %(legendMin, legendMax)
chimeraScript+=\
"""turn x %f
turn y %f
turn z %f
view all
save %s
exit
"""%(angX, angY, angZ, fnView)
fnTmp = os.path.join(fnWorkingDir, "chimeraScript.cxc")
fh = open(fnTmp,"w")
fh.write(chimeraScript)
fh.close()
from chimera import Plugin
args = " chimeraScript.cxc" if use_virtual_display else " --nogui --offscreen chimeraScript.cxc"
Plugin.runChimeraProgram(f"xvfb-run --server-num={virtual_display_port} " + Plugin.getProgram() if use_virtual_display else Plugin.getProgram(),
args, cwd=fnWorkingDir)
#cleanPath(fnTmp)
def generateChimeraColorView(fnWorkingDir, project, fnRoot, fnMap, Ts, fnColor, minVal, maxVal):
viewer = LocalResolutionViewer(project=project)
viewer.colorMap = StringParam()
viewer.colorMap.set("jet")
cmdFile = os.path.join(fnWorkingDir, fnRoot + ".py")
viewer.createChimeraScript(cmdFile, fnColor, fnMap, Ts,
numColors=11,
lowResLimit=maxVal,
highResLimit=minVal)
fn1 = os.path.join(fnWorkingDir, fnRoot + "1.jpg")
fn2 = os.path.join(fnWorkingDir, fnRoot + "2.jpg")
fn3 = os.path.join(fnWorkingDir, fnRoot + "3.jpg")
newLines = [
"run(session, 'volume dataCacheSize %d')\n" % max_mem_to_use,
"run(session, 'volume voxelLimitForOpen %d')\n" % max_voxels_to_open,
"run(session, 'volume showPlane false')\n"
]
referenceLine = "run(session, 'set bgColor white')"
writeAfterReferenceLine(cmdFile, newLines, referenceLine)
fhCmd = open(cmdFile, "a")
toWrite = \
"""
run(session, 'windowsize 1300 700')
run(session, 'view all')
run(session, 'turn y -90')
run(session, 'turn z -90')
run(session, 'save %s')
run(session, 'turn z 90')
run(session, 'turn y 90')
run(session, 'turn x -90')
run(session, 'turn z -90')
run(session, 'view all')
run(session, 'save %s')
run(session, 'turn z 90')
run(session, 'turn x 90')
run(session, 'view all')
run(session, 'save %s')
run(session, 'exit')
""" % (fn1, fn2, fn3)
fhCmd.write(toWrite)
fhCmd.close()
from chimera import Plugin
args = f"--script {cmdFile}" if use_virtual_display else f" --nogui --offscreen --script {cmdFile}"
Plugin.runChimeraProgram(f"xvfb-run --server-num={virtual_display_port} " + Plugin.getProgram() if use_virtual_display else Plugin.getProgram(),
args, cwd=fnWorkingDir)
def formatInv(value, pos):
""" Format function for Matplotlib formatter. """
inv = 999.
if value:
inv = 1 / value
return "1/%0.2f" % inv
def reportPlot(x,y, xlabel, ylabel, fnOut, yscale="linear",grid=True, plotType="plot", barWidth=1,
invertXLabels=False, addMean=False, title=""):
matplotlib.use('Agg')
plt.figure()
if plotType=="plot":
plt.plot(x,y)
if addMean:
ymean = np.mean(y)
plt.plot(x,ymean*np.ones(len(y)),'--')
elif plotType=="bar":
plt.bar(x,y,barWidth)
elif plotType=="scatter":
plt.scatter(x,y)
plt.yscale(yscale)
if invertXLabels:
plt.gca().xaxis.set_major_formatter(matplotlib.ticker.FuncFormatter(formatInv))
plt.grid(grid)
plt.xlabel(xlabel)
plt.ylabel(ylabel)
if title!="":
plt.title(title)
plt.savefig(fnOut, bbox_inches='tight')
plt.close('all')
def reportHistogram(y, ylabel, fnOut, xlim=None):
matplotlib.use('Agg')
plt.figure()
plt.hist(y, bins=25)
plt.grid(True)
plt.xlabel(ylabel)
plt.ylabel("Count")
if xlim:
plt.xlim(xlim)
plt.savefig(fnOut, bbox_inches='tight')
plt.close('all')
def reportMultiplePlots(x,yList, xlabel, ylabel, fnOut, legends, invertXLabels=False, xshade0=None, xshadeF=None):
matplotlib.use('Agg')
plt.figure()
for i in range(len(yList)):
plt.plot(x,yList[i], label=legends[i])
plt.legend()
if invertXLabels:
plt.gca().xaxis.set_major_formatter(matplotlib.ticker.FuncFormatter(formatInv))
plt.grid(True)
plt.xlabel(xlabel)
plt.ylabel(ylabel)
if xshade0 is not None and xshadeF is not None:
plt.gca().axes.axvspan(xshade0, xshadeF, alpha=0.3, color='green')
plt.savefig(fnOut, bbox_inches='tight')
plt.close('all')
def radialPlot(thetas, radii, weights, fnOut, plotType="points"):
max_w = max(weights)
min_w = min(weights)
min_p = 5
max_p = 40
matplotlib.use('Agg')
plt.figure()
fig, ax = plt.subplots(subplot_kw={'projection': 'polar'})
if plotType=="points":
for theta, radius, w in zip(thetas, radii, weights):
pointsize = int((w - min_w) / (max_w - min_w + 0.001) * (max_p - min_p) + min_p)
ax.plot(radians(theta), radius, markerfacecolor='blue', marker='.', markersize=pointsize)
elif plotType=="contour":
thetasp = np.radians(np.linspace(0, 360, 360))
radiip = np.arange(0, np.max(radii)+1, 1)
rmesh, thetamesh = np.meshgrid(radiip, thetasp)
values = np.zeros((len(thetasp), len(radiip)))
for i in range(0, len(thetas)):
values[int(thetas[i]), int(radii[i])] = weights[i]
stp = 0.1
lowlim = max(0.0, values.min())
highlim = values.max() + stp
pc = plt.contourf(thetamesh, rmesh, values, np.arange(lowlim, highlim, stp))
plt.colorbar(pc)
ax.set_rmax(np.max(radii))
ax.set_rticks([15, 30, 45, 60, 75, 90]) # Less radial ticks
ax.set_rlabel_position(-22.5) # Move radial labels away from plotted line
ax.grid(True)
plt.savefig(fnOut, bbox_inches='tight')
plt.close('all')
def calculateSha256(fn):
sha256_hash = hashlib.sha256()
with open(fn, "rb") as f:
# Read and update hash string value in blocks of 4K
for byte_block in iter(lambda: f.read(4096), b""):
sha256_hash.update(byte_block)
return sha256_hash.hexdigest()
def CDFFromHistogram(x,count):
xWidth=x[1]-x[0]
newX = np.array(x+[x[-1]+xWidth])
newX -= xWidth/2
newY = np.array([0]+count)
F = np.cumsum(newY)
F = F/F[-1]
return newX, F
def CDFpercentile(x,F,xp=None,Fp=None):
if xp is not None:
f = scipy.interpolate.interp1d(x, F, bounds_error=False, fill_value=(0.0,1.0))
if type(xp)==list:
return [f(xpp) for xpp in xp]
else:
return f(xp)
if Fp is not None:
f = scipy.interpolate.interp1d(F, x, bounds_error=False, fill_value=(np.min(x),np.max(x)))
if type(Fp)==list:
return [f(Fpp) for Fpp in Fp]
return f(Fp)
return None
def plotMicrograph(fnMic, fnOut, coords=None, boxSize=0, Ts=0):
I = xmipp3.Image()
I.read(fnMic)
Xdim, Ydim, _, _ = I.getDimensions()
I.readPreviewSmooth(fnMic, 800)
Xdimp, Ydimp, _, _ = I.getDimensions()
boxSizep = int(boxSize*Ts/float(Xdim)*Xdimp)
matplotlib.use('Agg')
plt.figure()
plt.imshow(I.getData(), cmap='gray')
plt.axis('off')
Kx = float(Xdimp)/Xdim
Ky = float(Ydimp)/Ydim
if coords is not None:
for x,y in coords:
xp = int(x*Kx)
yp = int(y*Ky)
circle = plt.Circle((xp, yp), boxSizep, color='green', fill=False)
plt.gca().add_artist(circle)
plt.savefig(fnOut, bbox_inches='tight')
plt.close('all')
class ValidationReport:
def __init__(self, fnDir, levels, IS_EMDB_ENTRY, EMDB_ID, FNMAP, PDB_ID, FNMODEL, JOB_NAME, JOB_DESCRIPTION, MAPRESOLUTION):
self.fnProjectDir = fnDir
self.fnReportDir = os.path.join(fnDir,"validationReport")
makePath(self.fnReportDir)
self.fnReport = os.path.join(self.fnReportDir,"report.tex")
self.fh = open(self.fnReport,"w+")
self.fnFrontpage = os.path.join(self.fnReportDir,"frontpage.tex")
self.fnFrontpage = open(self.fnFrontpage,"w")
self.writeFrontpage(levels, IS_EMDB_ENTRY, EMDB_ID, FNMAP, PDB_ID, FNMODEL, JOB_NAME, JOB_DESCRIPTION, MAPRESOLUTION)
self.fnFrontpage.close()
self.fnContext = os.path.join(self.fnReportDir, "context.tex")
self.fnContext = open(self.fnContext, "w")
self.writeContext()
self.fnContext.close()
self.fnAbstract = os.path.join(self.fnReportDir,"abstract.tex")
self.fhAbstract = open(self.fnAbstract,"w")
self.fnSummary = os.path.join(self.fnReportDir,"summary.tex")
self.fhSummary = open(self.fnSummary,"w")
self.fnSummaryWarnings = os.path.join(self.fnReportDir,"summaryWarnings.tex")
self.fhSummaryWarnings = open(self.fnSummaryWarnings,"w")
self.fhSummaryWarnings.write(SUMMARY_WARNINGS_TITLE)
self.writePreamble()
self.resolutionEstimates = []
self.score = 0
self.scoreN = 0
self.abstract = ""
def getReportDir(self):
return self.fnReportDir
def writePreamble(self):
toWrite = \
"""
\\documentclass[12pt, letterpaper]{article}
\\usepackage[utf8]{inputenc}
\\usepackage{graphicx}
\\usepackage{float}
\\usepackage{subfig}
\\usepackage{hyperref}
\\usepackage{xcolor}
\\usepackage[us,12hr]{datetime}
\\usepackage{longtable}
\\usepackage{enumitem}
\\usepackage{tikz}
\\usepackage{fancyhdr}
\\setlist{nosep}
\\usepackage[most]{tcolorbox}
\\usepackage{lastpage}
\\usepackage{refcount}
% Define a new tcolorbox
\\newtcolorbox{mycolorbox}{
colback=red!40!white, % Color de fondo
colframe=red!75!black, % Color del borde
arc=0mm,
}
% Define own colors
\\definecolor{mygreen}{RGB}{116,183,46}
\\definecolor{myblue}{RGB}{37,150,190}
% Set hyperlinks configuration
\\hypersetup{
colorlinks=true,
linkcolor=blue,
filecolor=magenta,
urlcolor=blue,
pdftitle={Validation Report Service},
pdfpagemode=FullScreen,
citecolor=mygreen,
}
% Define footer with pagination
\\fancypagestyle{main}{
\\fancyhf{}
\\fancyfoot[C]{Page \\thepage\\ of \\getpagerefnumber{LastPage}}
\\renewcommand{\\headrulewidth}{0pt}
\\renewcommand{\\footrulewidth}{0pt}
}
\\begin{document}
\\input{frontpage.tex}
% Reactivate pagination
\\newpage
\\pagestyle{main}
\\pagenumbering{arabic}
\\input{context.tex}
\\clearpage
\\renewcommand{\\abstractname}{Summarized overall quality}
\\begin{abstract}
\\input{abstract.tex}
\\clearpage
\\input{summary.tex}
\\clearpage
\\input{summaryWarnings.tex}
\\end{abstract}
\\clearpage
\\tableofcontents
\\clearpage
"""
self.fh.write(toWrite)
toWrite = \
"""
\\begin{tabular}{lrr}
"""
self.fhSummary.write(toWrite)
def write(self,msg):
self.fh.write(msg)
self.fh.flush()
def writeWarningsAndSummary(self, warnings, section, secLabel):
if warnings is None:
toWrite = \
"""
\\textbf{Automatic criteria}: The program ran properly but there are no automatic criteria defined to evaluate the results.
Manual interpretation is needed. Not included as evaluable item in 'Summarized overall quality' section.\\\\
""" + STATUS_OK
self.writeSummary(section, secLabel, OK_MESSAGE)
else:
self.scoreN+=1
if len(warnings) > 0:
countWarnings = len(warnings)
toWrite = "\\textbf{WARNINGS}: %d warnings\\\\ \n%s\n" % (countWarnings, latexEnumerate(warnings))
self.writeSummary(section, secLabel, WARNINGS_MESSAGE % countWarnings)
self.fhSummaryWarnings.write("\\underline{Section \\ref{%s} (%s)}\n" % (secLabel, section))
self.fhSummaryWarnings.write("\\begin{enumerate}\n")
for warning in warnings:
self.fhSummaryWarnings.write("\\item %s\n"%warning)
self.fhSummaryWarnings.write("\\end{enumerate}\n\n\n")
else:
toWrite = STATUS_OK
self.writeSummary(section, secLabel, OK_MESSAGE)
self.score += 1
self.write(toWrite)
def writeSummary(self, test, sec, msg):
toWrite = "%s & Sec. \\ref{%s} & %s \\\\ \n"%(test,sec,msg)
self.fhSummary.write(toWrite)
def writeAbstract(self, msg):
self.fhAbstract.write(msg)
def writeFrontpage(self, levels, IS_EMDB_ENTRY, EMDB_ID, FNMAP, PDB_ID, FNMODEL, JOB_NAME, JOB_DESCRIPTION, MAPRESOLUTION):
ScipionEmValDir = os.path.dirname(__file__)
logo = os.path.join(ScipionEmValDir, 'resources', 'figures', 'i2pc_logo.png')
if IS_EMDB_ENTRY:
# Set parameters for public data-based repots
icon = os.path.join(ScipionEmValDir, 'resources', 'figures', 'webicon.png')
color = 'mygreen'
icon_msg = 'Public Data-based Report'
msg = 'data publicly available at \\href{https://www.ebi.ac.uk/emdb/}{EMDB}.'
if MAPRESOLUTION:
resolution_str = str(MAPRESOLUTION) + ' \AA'
else:
resolution_str = "{\\color{red} (not reported)}"
title = None
authors = None
deposited = None
try:
url_rest_api = 'https://www.ebi.ac.uk/emdb/api/entry/%s' % EMDB_ID
jdata = requests.get(url_rest_api).json()
title = jdata["admin"]["title"]
deposited = jdata["admin"]["key_dates"]["deposition"]
# Set authors depending on if it is a list of dicts or a list of strs
if type(jdata["admin"]["authors_list"]["author"][0]) == dict:
authors_list = [d["valueOf_"] for d in jdata["admin"]["authors_list"]["author"] if "valueOf_" in d]
authors = ", ".join(authors_list)
else:
authors = ", ".join(jdata["admin"]["authors_list"]["author"])
except:
pass
entryInfoList = [EMDB_ID, PDB_ID, title, authors, deposited, resolution_str]
else:
icon = os.path.join(ScipionEmValDir, 'resources', 'figures', 'usericon.png')
color = 'myblue'
icon_msg = 'User Data-based Report'
msg = 'user data provided through the \\href{https://biocomp.cnb.csic.es/EMValidationService/}{VRS website}.'
if MAPRESOLUTION:
resolution_str = str(MAPRESOLUTION) + ' \AA'
else:
resolution_str = "{\\color{red} (not reported)}"
MAP_NAME = os.path.basename(FNMAP) if FNMAP else None
MODEL_NAME = os.path.basename(FNMODEL) if FNMODEL else None
TRIMMED_JOB_DESCRIPTION = JOB_DESCRIPTION[:420] if JOB_DESCRIPTION else None
entryInfoList = [MAP_NAME, MODEL_NAME, JOB_NAME, TRIMMED_JOB_DESCRIPTION, resolution_str]
# Add first part of the frontpage.tex file
toWrite=\
"""
\\pagenumbering{gobble}
\\pagestyle{empty}
\\begin{center}
\\parbox[c][\\textheight][t]{\\textwidth}{
\\vspace{-1cm}
\\begin{tikzpicture}[overlay, remember picture]
\\node[anchor=north east,
inner sep=2cm]
at ([xshift=-2cm, yshift=0.5cm]current page.north east)
{\\includegraphics[width=1cm]{%s}};
\\node[anchor=north east,
inner sep=1cm,
text=%s]
at ([yshift=-2cm, xshift=-1cm]current page.north east)
{%s};
\\end{tikzpicture}
\\begin{center}
\\includegraphics[width=8cm]{%s}\\\\
\\vspace{0.5cm}
{\\Huge \\bf Validation Report Service}\\\\
\\vspace{0.5cm}
{\\Large Cryo-EM Map Validation Report}\\\\
\\vspace{0.5cm}
{Report to assess Cryo-EM Volume Map at Level(s) %s}\\\\
\\vspace{0.5cm}
{\\color{%s}\\rule{\\linewidth}{0.5mm}}\\\\[0.5cm]
{This report has been generated based on %s}\\\\[0.5cm]
\\begin{flushleft}
{\\bf {Basic Entry Information:}}\\\\[0.2cm]
"""%(icon, color, icon_msg, logo, ", ".join(levels), color, msg)
# Add Info entry lines depending on if is a Public Data-based or User Data-based Report and if each value has been specified
entryLine=\
"""
{\\bf {%s: }}{%s}\\\\
"""
entryLineHRef=\
"""
{\\bf {%s: }}\\href{%s}{%s}\\\\
"""
if IS_EMDB_ENTRY:
for item in entryInfoList:
if item is EMDB_ID and EMDB_ID is not None:
toWrite+=entryLineHRef % ('EMDB ID', EMDB_LINK % EMDB_ID, EMDB_ID)
if item is PDB_ID and PDB_ID is not None:
toWrite+=entryLineHRef % ('PDB ID', PDB_LINK % PDB_ID, PDB_ID)
if item is title and title is not None:
toWrite+=entryLine % ('Title', escapeLatexSpecialChars(title))
if item is authors and authors is not None:
toWrite+=entryLineHRef % ('Authors', EMDB_LINK % EMDB_ID, "See EMDB entry link")
if item is deposited and deposited is not None:
toWrite+=entryLine % ('Deposited on', escapeLatexSpecialChars(deposited))
if item is resolution_str and resolution_str is not None:
toWrite+=entryLine % ('Reported Resolution', resolution_str)
else:
for item in entryInfoList:
if item is MAP_NAME and MAP_NAME is not None:
toWrite+=entryLine % ('Volumen Map', escapeLatexSpecialChars(MAP_NAME))
if item is MODEL_NAME and MODEL_NAME is not None:
toWrite+=entryLine % ('Atomic Model', escapeLatexSpecialChars(MODEL_NAME))
if item is JOB_NAME and JOB_NAME is not None:
toWrite+=entryLine % ('Job Name', escapeLatexSpecialChars(JOB_NAME))
if item is TRIMMED_JOB_DESCRIPTION and TRIMMED_JOB_DESCRIPTION is not None:
toWrite+=entryLine % ('Job Description', escapeLatexSpecialChars(TRIMMED_JOB_DESCRIPTION))
if item is resolution_str and resolution_str is not None:
toWrite+=entryLine % ('Reported Resolution', resolution_str)
# Add final part of the frontpage.tex file
toWrite+=\
"""
\\end{flushleft}
{\\color{%s}\\rule{\\linewidth}{0.5mm}}\\\\[0.8cm]
\\begin{flushright}
{\\bf \\large {Contact Us:}}\\\\[0.2cm]
{Instruct Image Processing Center }\\href{http://i2pc.es/}{(I$^2$PC)}\\\\
{Biocomputing Unit }\\href{http://biocomputingunit.es/}{(BCU)}\\\\
{i2pc@cnb.csic.es}\\\\
\\href{https://biocomp.cnb.csic.es/EMValidationService/}{VRS Website}\\\\[1cm]
\\end{flushright}
{National Center for Biotechnology (CNB)}\\\\
{St/ Darwin, 3 (Autonomous University of Madrid)}\\\\
{28049 Cantoblanco, Madrid (Spain)}\\\\[0.5cm]
\\vfill
{Last update:} \\bf \\today, \\currenttime
\\end{center}
}
\\end{center}
"""%(color)
self.fnFrontpage.write(toWrite)
def writeContext(self):
ScipionEmValDir = os.path.dirname(__file__)
scipion_logo = os.path.join(ScipionEmValDir, 'resources', 'figures', 'scipion_logo.png')
toWrite = \
"""
\\begin{center}\\textbf{Context}\\end{center}
Cryo-electron microscopy is currently one of the most active techniques in Structural Biology. The number of maps deposited at the \\href{https://www.ebi.ac.uk/emdb/}{Electron Microscopy Data Bank} is rapidly growing every year and keeping the quality of the submitted maps is essential to maintain the scientific quality of the field. \\\\
The ultimate quality measure is the consistency of the map and an atomic model. However, this is only possible for high resolution maps. Over the years there have been many suggestions about validation measures of 3DEM maps. Unfortunately, most of these measures are not currently in use for their spread in multiple software tools and the associated difficulty to access them. To alleviate this problem, we made available a validation grading system that evaluate the information provided to assess the map. \\\\
This system grades a map from 0 to 5 depending on the amount of information available. In this way, a map could be validated at Level 0 (the deposited map), 1 (two half maps), 2 (2D classes), 3 (particles), 4 (... + angular assignment), 5 (... + micrographs and coordinates). In addition, we can have three optional qualifiers: A (... + atomic model), W (... + image processing workflow), and O (... + other techniques). To know more about this service read this \\href{%s}{paper} \\\\
This Validation Report Service uses Scipion (see this \\href{%s}{link} for more detail) as workflow engine and ChimeraX (see this \\href{%s}{link} for more detail) to generate the 3D views. For more information about the different methods and softwares used for this report, see the references \\href{%s}{here}.\\\\
\\vfill
\\begin{center}
\\includegraphics[width=8cm]{%s}\\
\\end{center}
""" % (VRS_DOI, SCIPION_DOI, CHIMERAX_DOI, HELP_WEBSITE_LINK, scipion_logo)
self.fnContext.write(toWrite)
def addResolutionEstimate(self, R):
self.resolutionEstimates.append(R)
def abstractResolution(self, resolution):
if len(self.resolutionEstimates)>0:
validResolutionEstimates = [x for x in self.resolutionEstimates if x is not None]
if len(validResolutionEstimates)==1:
msg="\n\n\\vspace{0.5cm}The average resolution of the map estimated is %4.1f\\AA~."%\
(validResolutionEstimates[0])
else:
msg="\n\n\\vspace{0.5cm}The average resolution of the map estimated by various methods goes from %4.1f\\AA~to %4.1f\\AA~with an "\
"average of %4.1f\\AA."%\
(np.min(validResolutionEstimates), np.max(validResolutionEstimates), np.mean(validResolutionEstimates))
if not resolution:
msg+=" The resolution was not reported by the user."
else:
msg+=" The resolution reported by the user was %4.1f\\AA." % (resolution)
if resolution<0.8*np.mean(validResolutionEstimates):
msg+=" The resolution reported may be overestimated."
msg+="\n\n\\vspace{0.5cm}"
self.writeAbstract(msg)
def addCitation(self, key, bblEntry):
# Bibliographystyle: apalike
# \begin{thebibliography}{}
#
# \bibitem[Greenwade, 1993] {greenwade93}
# Greenwade, G. ~D.(1993).
# \newblock The {C}omprehensive {T}ex {A}rchive {N}etwork({CTAN}).
# \newblock {\em TUGBoat}, 14(3): 342 - -351.
#
# \end{thebibliography}
if not key in self.citations:
self.citations[key] = bblEntry
def writeFailedSubsection(self, subsection, msg):
toWrite=\
"""
\\subsection{%s}
%s
\\textbf{Failed, it could not be computed}
"""%(subsection, msg)
self.fh.write(toWrite)
def writeSection(self, section, label=False):
if label:
toWrite=\
"""
\\section{%s}
\\label{%s}
"""%(section, label)
else:
toWrite=\
"""
\\section{%s}
"""%section
self.fh.write(toWrite)
def orthogonalProjections(self, fnRoot, msg, caption, fnMap, label):
V = readMap(fnMap).getData()
fnZ = os.path.join(self.fnReportDir,fnRoot+"_Z.jpg")
writeImage(np.sum(V,axis=0), fnZ)
fnY = os.path.join(self.fnReportDir,fnRoot+"_Y.jpg")
writeImage(np.sum(V,axis=1), fnY)
PIL.Image.open(fnY).rotate(-90, expand=True).save(fnY)
fnX = os.path.join(self.fnReportDir,fnRoot+"_X.jpg")
writeImage(np.sum(V,axis=2), fnX)
toWrite="""
%s
\\begin{figure}[H]
\\centering
\\subfloat[X Projection]{\includegraphics[width=6.5cm]{%s}}
\\hspace{0.1cm}
\\subfloat[Y Projection]{\includegraphics[width=6.5cm]{%s}}
\\hspace{0.1cm}
\\end{figure}
\\begin{figure}[H]
\\centering
\\subfloat[Z Projection]{\includegraphics[width=6.5cm]{%s}}
\\caption{%s}
\\label{%s}
\\end{figure}
"""%(msg, fnX, fnY, fnZ, caption, label)
self.fh.write(toWrite)
def isoSurfaces(self, fnRoot, msg, caption, fnMap, threshold, label):
generateChimeraView(self.fnReportDir, fnMap, fnRoot + "1.jpg", True, threshold, 0, -90, -90)
generateChimeraView(self.fnReportDir, fnMap, fnRoot + "2.jpg", True, threshold, -90, 0, -90)
generateChimeraView(self.fnReportDir, fnMap, fnRoot + "3.jpg", True, threshold, 0, 0, 0)
caption += " Views generated by ChimeraX at a the following "\
"X, Y, Z angles: View 1 (0, -90, -90), View 2 (-90, 0, -90), View 3 (0, 0, 0)."
fn1 = os.path.join(self.fnReportDir,fnRoot + "1.jpg")
fn2 = os.path.join(self.fnReportDir,fnRoot + "2.jpg")
fn3 = os.path.join(self.fnReportDir,fnRoot + "3.jpg")
toWrite = \
"""
%s
\\begin{figure}[H]
\\centering
\\subfloat[View 1]{\includegraphics[width=6.5cm]{%s}}
\\hspace{0.1cm}
\\subfloat[View 2]{\includegraphics[width=6.5cm]{%s}}
\\hspace{0.1cm}
\\end{figure}
\\begin{figure}[H]
\\centering
\\subfloat[View 3]{\includegraphics[width=6.5cm]{%s}}
\\caption{%s}
\\label{%s}
\\end{figure}
""" % (msg, fn1, fn2, fn3, caption, label)
self.fh.write(toWrite)
def colorIsoSurfaces(self, msg, caption, label, project, fnRoot, fnMap, Ts, fnColor, minVal, maxVal):
generateChimeraColorView(self.getReportDir(), project, fnRoot, fnMap, Ts, fnColor, minVal, maxVal)
caption += " Views generated by ChimeraX at a the following "\
"X, Y, Z angles: View 1 (0, -90, -90), View 2 (-90, 0, -90), View 3 (0, 0, 0)."
fn1 = os.path.join(self.fnReportDir, fnRoot + "1.jpg")
fn2 = os.path.join(self.fnReportDir, fnRoot + "2.jpg")
fn3 = os.path.join(self.fnReportDir, fnRoot + "3.jpg")
toWrite = \
"""
%s
\\begin{figure}[H]
\\centering
\\subfloat[View 1]{\includegraphics[width=12cm]{%s}}
\\end{figure}
\\begin{figure}[H]
\\centering
\\subfloat[View 2]{\includegraphics[width=12cm]{%s}}
\\end{figure}
\\begin{figure}[H]
\\centering
\\subfloat[View 3]{\includegraphics[width=12cm]{%s}}
\\caption{%s}
\\label{%s}
\\end{figure}
""" % (msg, fn1, fn2, fn3, caption, label)
self.fh.write(toWrite)
def atomicModel(self, fnRoot, msg, caption, fnModel, label, bfactor=False, occupancy=False, otherAttribute=[], rainbow=True, legendMin=None, legendMax=None):
generateChimeraView(self.fnReportDir, fnModel, fnRoot + "1.jpg", False, 0, 0, -90, -90, bfactor=bfactor,
occupancy=occupancy, otherAttribute=otherAttribute, rainbow=rainbow, legendMin=legendMin, legendMax=legendMax)
generateChimeraView(self.fnReportDir, fnModel, fnRoot + "2.jpg", False, 0, -90, 0, -90, bfactor=bfactor,
occupancy=occupancy, otherAttribute=otherAttribute, rainbow=rainbow, legendMin=legendMin, legendMax=legendMax)
generateChimeraView(self.fnReportDir, fnModel, fnRoot + "3.jpg", False, 0, 0, 0, 0, bfactor=bfactor,
occupancy=occupancy, otherAttribute=otherAttribute, rainbow=rainbow, legendMin=legendMin, legendMax=legendMax)
caption += " Views generated by ChimeraX at a the following " \
"X, Y, Z angles: View 1 (0, -90, -90), View 2 (-90, 0, -90), View 3 (0, 0, 0)."
fn1 = os.path.join(self.fnReportDir, fnRoot + "1.jpg")
fn2 = os.path.join(self.fnReportDir, fnRoot + "2.jpg")
fn3 = os.path.join(self.fnReportDir, fnRoot + "3.jpg")
toWrite = \
"""
%s
\\begin{figure}[H]
\\centering
\\subfloat[View 1]{\includegraphics[width=6.5cm]{%s}}
\\hspace{0.1cm}
\\subfloat[View 2]{\includegraphics[width=6.5cm]{%s}}
\\hspace{0.1cm}
\\end{figure}
\\begin{figure}[H]
\\centering
\\subfloat[View 3]{\includegraphics[width=6.5cm]{%s}}
\\caption{%s}
\\label{%s}
\\end{figure}
""" % (msg, fn1, fn2, fn3, caption, label)
self.fh.write(toWrite)
def orthogonalSlices(self, fnRoot, msg, caption, map, label, maxVar=False):
if type(map) is str:
V = readMap(map)
mV = V.getData()
Zdim, Ydim, Xdim = mV.shape
else:
mV = map
Zdim, Ydim, Xdim = mV.shape
if maxVar:
ix = np.argmax([np.var(mV[:, :, i]) for i in range(Xdim)])
iy = np.argmax([np.var(mV[:, i, :]) for i in range(Ydim)])
iz = np.argmax([np.var(mV[i, :, :]) for i in range(Zdim)])
else:
ix = int(Xdim / 2)
iy = int(Ydim / 2)
iz = int(Zdim / 2)
fnZ = os.path.join(self.fnReportDir, fnRoot + "_Z.jpg")
writeImage(mV[iz, :, :], fnZ)
fnY = os.path.join(self.fnReportDir, fnRoot + "_Y.jpg")
writeImage(mV[:, iy, :], fnY)
PIL.Image.open(fnY).rotate(-90, expand=True).save(fnY)
fnX = os.path.join(self.fnReportDir, fnRoot + "_X.jpg")
writeImage(mV[:, :, ix], fnX)
toWrite = msg
toWrite+=\
"""\\begin{figure}[H]
\\centering
\\subfloat[X Slice %d]{\includegraphics[width=6.5cm]{%s}}
\\hspace{0.1cm}
\\subfloat[Y Slice %d]{\includegraphics[width=6.5cm]{%s}}
\\hspace{0.1cm}
\\end{figure}
\\begin{figure}[H]
\\centering
\\subfloat[Z Slice %d]{\includegraphics[width=6.5cm]{%s}}
\\caption{%s}
\\label{%s}
\\end{figure}
""" % (ix, fnX, iy, fnY, iz, fnZ, caption, label)
self.fh.write(toWrite)
def setOfImages(self, fnImgs, mdlLabel, caption, label, fnRoot, width, Ximgs, imgMax=1e6):
toWrite = \
"""\\begin{figure}[H]
\\centering
"""
self.write(toWrite)
md = xmipp3.MetaData(fnImgs)
imgNames = md.getColumnValues(mdlLabel)
toWrite = ""
for i in range(len(imgNames)):
I = xmipp3.Image(imgNames[i])
fnOut = "%s_%04d.jpg"%(fnRoot,i)
I.write(fnOut)
toWrite+=\
"""\\includegraphics[width=%s]{%s}
"""%(width,fnOut)
if (i+1)%Ximgs==0:
toWrite+="\\\\ \n"
if i==imgMax:
break
toWrite+=\
"""\\caption{%s}
\\label{%s}
\\end{figure}
"""%(caption, label)
self.write(toWrite)
def showj(self, md, mdLabelList, renderList, formatList, headers, fnRoot, width):
toWrite = \
"""\\begin{longtable}{%s}
\\centering
"""%('c'*len(headers))
i = 0