-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBrainSegmenterGUI.m
2902 lines (2557 loc) · 101 KB
/
BrainSegmenterGUI.m
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
function varargout = BrainSegmenterGUI(varargin)
%% BrainSegmenterGUI MATLAB code for BrainSegmenterGUI.fig
% Lilia Mesina, Polaris/CCBN, November2013
% ==
% Last Modified by LM, 15Aug2015
%% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @BrainSegmenterGUI_OpeningFcn, ...
'gui_OutputFcn', @BrainSegmenterGUI_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', @BrainSegmenterGUI_Callback);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before BrainSegmenterGUI is made visible.
function BrainSegmenterGUI_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to BrainSegmenterGUI (see VARARGIN)
% Choose default command line output for BrainSegmenterGUI
global homeGUI
%batchPath batchName nTiles
%global redTresh greenTresh
%global bigWidth bigHeight
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
movegui(hObject,'northwest');
home = pwd;
homeGUI = which('BrainSegmenterGUI');
[homeGUI_ fn ex] = fileparts(homeGUI);
homeGUI = homeGUI_;
addpath(genpath(homeGUI));
fprintf(2, 'BrainSegmenterGUI Path Loaded!');
% --- Outputs from this function are returned to the command line.
function varargout = BrainSegmenterGUI_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
%% Set Menu
set_menu(hObject,eventdata,handles);
set(hObject, 'KeyPressFcn',@fKeyPress);
function BrainSegmenterGUI_Callback(hObject, eventdata, handles, varargin)
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% --- Executes on button press
function openAnalysis_Callback(hObject, eventdata, handles, sect)
global homeGUI imgFN
global imgName batchRoot batchName
global iTiles jTiles nTiles
global redTresh greenTresh
global maxRedTresh maxGreenTresh flipS
global bigWidth bigHeight nCells
global tileWidth tileHeight
global imageAnalysisStatus
global step0 step1 step2 step3 step4 step5
global rotate dataCompute
global verGUI sectName regisRoot
global brainRoot
verGUI = '2.0.4';
if nargin < 4
[fn, dn, ext] = uigetfile({'*.ndpi';'*.*'},'Select the NDPI Image for Analysis');
else
dn = batchRoot;
ndpi_files = dir(fullfile(batchRoot,'*.ndpi'));
fn = ndpi_files(1).name;
end
hFig2 = findobj('Name','allSectionsView');
close(hFig2);
[fn3 dn3 ex3] = fileparts(fn);
imgName = dn3;
fullFN = fullfile(dn, fn);
if isequal(fn,0)
disp('User selected Cancel');
return
else
disp(['User selected: ' fullFN]);
end
strT = ['BrainSegmenterGUI v' verGUI ' >> %s'];
caption = sprintf(strT, fullFN);
set(gcf, 'Name', caption );
homeGUI = which('BrainSegmenterGUI');
[homeGUI f e] = fileparts(homeGUI);
%% Create a default XML Analysis file
%% init globals
batchRoot = dn;
[dn1 fn1 ext] = fileparts(batchRoot);
[dn1 fn1 ext] = fileparts(dn1);
sectName = fn1;
regisRoot = [dn1 '\ndpi4regis\'];
[dn fn ext] = fileparts(dn1);
brainRoot = dn;
%set brainID
[dn2 fn2 ext] = fileparts(brainRoot);
brainID = fn2;
hObj = findobj('Tag', 'tabBrain');
set(hObj, 'Title', ['Brain: ' brainID]);
hObj = findobj('Tag', 'tabSection');
set(hObj, 'Title', ['Section: ' sectName]);
xmlFile = [batchRoot 'ImageAnalysisProgress.xml'];
if exist(xmlFile, 'file')
hM = msgbox('Progress XML file exists. Loading analysis...');
updateControls(xmlFile);
pause(1);
close(hM);
else
copyfile([homeGUI '\lib\ImageAnalysisProgress.xml'], xmlFile);
dnTmp = batchRoot;
imgNameTmp = imgName;
loadProgressFromXML(xmlFile);
batchRoot = dnTmp;
imgName = imgNameTmp;
batchName = ['batch_' imgName];
para = [];
para = {{'batchRoot' '' batchRoot}; {'batchName' '' batchName}; {'imgName' '' imgName}};
saveProgressToXML(para);
% Check if Step0 is Done
tList = dir([batchRoot '\*.jpg']);
nT = length(tList);
%nP = length(para);
if nT
hObj = findobj('Tag', 'step0');
set(hObj,'Value', 1);
step0 = 'Done';
para = [];
para{1} = {'step0' 'Status' 'Done'};
saveProgressToXML(para);
end
if exist([batchRoot '\' batchName],'dir')
strIn = 'i1j1'; %default
tileFN = [batchRoot '\' batchName '\' strIn '\' strIn '.tif'];
if ~exist(tileFN,'file')
tileFN = [batchRoot '\' batchName '\' strIn '_processed\' strIn '_blue_RAW.tif'];
end
while ~exist(tileFN,'file')
x = inputdlg('In the batch folder can''t find tile image (\i1j1\i1j1.tif) to set bigWidth|bigHeight and other parameters. What other tile do you want to use?',...
'Input', 1, {'i1j1'});
strIn = x{:};
tileFN = [batchRoot '\' batchName '\' strIn '\' strIn '.tif'];
end
hObj = findobj('Tag', 'step0');
set(hObj,'Value', 1);
step0 = 'Done';
para = [];
para{1} = {'step0' 'Status' 'Done'};
hObj = findobj('Tag', 'step1');
set(hObj,'Value', 1);
step1 = 'Done';
para{2} = {'step1' 'Status' 'Done'};
saveProgressToXML(para);
if strcmp(imageAnalysisStatus,'None')
para = [];
imageAnalysisStatus = 'InProgress';
para{1} = {'ImageAnalysis' 'Status' imageAnalysisStatus};
imgInfo = imfinfo(tileFN);
imgInfo = imgInfo(1);
tileWidth = imgInfo.Width;
tileHeight = imgInfo.Height;
para{2} = {'Tile' 'tileWidth' num2str(tileWidth)};
para{3} = {'Tile' 'tileHeight' num2str(tileHeight)};
l = dir([batchRoot '\' batchName '\i*']);
nFiles = length(l);
foundTiles = 0;
for i =1:nFiles
l2 = l(i).name;
if length(l2) ~= 4, continue; end
foundTiles = 1;
a(i,1) = str2num(l2(2));
a(i,2) = str2num(l2(4));
end
if ~foundTiles %check processed tiles instead
l = dir([batchRoot '\' batchName '\i*_processed']);
nFiles = length(l);
for i =1:nFiles
l2 = l(i).name;
%if length(l2) ~= 4, continue; end
a(i,1) = str2num(l2(2));
a(i,2) = str2num(l2(4));
end
end
iTiles = max(a(:,1));
jTiles = max(a(:,2));
nTiles = iTiles*jTiles;
hObj = findobj('Tag','niTiles');
strVal = num2str(iTiles);
set(hObj,'String', strVal);
hObj = findobj('Tag','njTiles');
strVal = num2str(jTiles);
set(hObj,'String', strVal);
hObj = findobj('Tag','nTiles_value');
strVal = num2str(nTiles);
set(hObj,'String', strVal);
para{4} = {'Tiles' 'iTiles' num2str(iTiles)};
para{5} = {'Tiles' 'jTiles' num2str(jTiles)};
para{6} = {'Tiles' 'nTiles' num2str(nTiles)};
bigWidth = jTiles*tileWidth;
bigHeight = iTiles*tileHeight;
para{7} = {'Section' 'bigWidth' num2str(bigWidth)};
para{8} = {'Section' 'bigHeight' num2str(bigHeight)};
para{9} = {'step0' 'Status' 'Done'};
para{10} = {'step1' 'Status' 'Done'};
hObj = findobj('Tag', 'step0');
set(hObj,'Value', 1);
hObj = findobj('Tag', 'step1');
set(hObj,'Value', 1);
step0 = 'Done';
step1 = 'Done';
strNew = [batchRoot '\' batchName '\' strIn '_processed\'];
if exist([strNew strIn '_blue_RAW_BG.tif'],'file')
step2 = 'Done';
nP = length(para);
para{nP+1} = {'step2' 'Status' 'Done'};
hObj = findobj('Tag', 'step2');
set(hObj,'Value', 1);
end
if exist([strNew 'Results_Image_nuc.tif'],'file')
step3 = 'Done';
nP = length(para);
para{nP+1} = {'step3' 'Status' 'Done'};
hObj = findobj('Tag', 'step3');
set(hObj,'Value', 1);
end
if exist([strNew 'results_table_raw_edit.txt'],'file')
step4 = 'Done';
step5 = 'Done';
nP = length(para);
para{nP+1} = {'step4' 'Status' 'Done'};
para{nP+2} = {'step5' 'Status' 'Done'};
hObj = findobj('Tag', 'step4');
set(hObj,'Value', 1);
hObj = findobj('Tag', 'step5');
set(hObj,'Value', 1);
end
end
end
saveProgressToXML(para);
end
hObj = findobj('Tag', 'redTreshold');
strVal = num2str(redTresh);
set(hObj,'String', strVal);
hObj = findobj('Tag', 'greenTreshold');
strVal = num2str(greenTresh);
set(hObj,'String', strVal);
hObj = findobj('Tag', 'maxRedTresh');
strVal = num2str(maxRedTresh);
set(hObj,'String', strVal);
hObj = findobj('Tag', 'maxGreenTresh');
strVal = num2str(maxGreenTresh);
set(hObj,'String', strVal);
hObj = findobj('Tag', 'flipS');
set(hObj,'Value', flipS);
% --- Executes on button press in runAllSteps.
function runAllSteps_Callback(hObject, eventdata, handles)
% hObject handle to runAllSteps (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global batchRoot batchName
global step0 step1 step2 step3 step4 step5
xmlFile = [batchRoot 'ImageAnalysisProgress.xml'];
loadProgressFromXML(xmlFile);
if isempty(batchRoot)
set(hObject,'Value',0);
errordlg('No Image Analysis Project opened.');
return
end
if strcmp(step0,'None')
%hC = guidata(hObject);
BrainSegmenterGUI('step0_Callback',hObject,eventdata,guidata(hObject));
loadProgressFromXML(xmlFile);
%waitfor(hC);
end
if strcmp(step1,'None')
BrainSegmenterGUI('step1_Callback',hObject,eventdata,guidata(hObject));
global imgName batchRoot batchName
global iTiles nTiles
global redTresh greenTresh
global maxRedTresh maxGreenTresh flipS
global bigWidth bigHeight nCells
global tileWidth tileHeight
global imageAnalysisStatus
global step0 step1 step2 step3 step4 step5
loadProgressFromXML(xmlFile);
end
if strcmp(step2,'None')
BrainSegmenterGUI('step2_Callback',hObject,eventdata,guidata(hObject));
%step3 = 'None';
%try to avoid error "Reference to a cleared variable step3."
global imgName batchRoot batchName
global iTiles jTiles nTiles
global redTresh greenTresh
global maxRedTresh maxGreenTresh flipS
global bigWidth bigHeight nCells
global tileWidth tileHeight
global imageAnalysisStatus
global step0 step1 step2 step3 step4 step5
loadProgressFromXML(xmlFile);
end
if strcmp(step3,'None')
BrainSegmenterGUI('step3_Callback',hObject,eventdata,guidata(hObject));
loadProgressFromXML(xmlFile);
end
if strcmp(step4,'None')
BrainSegmenterGUI('step4_Callback',hObject,eventdata,guidata(hObject));
loadProgressFromXML(xmlFile);
end
if strcmp(step5,'None')
BrainSegmenterGUI('step5_Callback',hObject,eventdata,guidata(hObject));
loadProgressFromXML(xmlFile);
end
function step0_Callback(hObject, eventdata, handles)
%% Split NDPI image in tiles
global imgName batchRoot nTiles step
step = 'Step0'
startTime = datetime
if isempty(batchRoot)
set(hObject,'Value',0);
errordlg('No Image Analysis Project opened.');
return
end
sel = get(hObject,'Value');
if sel
tic
hB = waitbar(0, 'Step0 in progress (1-3min) ...');
%set(hB, 'WindowStyle','modal', 'CloseRequestFcn','');
%batch.nSlices = 1;
waitbar(0.3, hB, 'Step0 in progress (1-3min) ...');
imgFN = [batchRoot imgName '.ndpi'];
[status nTiles0] = split_ndpi(imgFN);
%status = 1; nTiles0 = 8; %test
waitbar(0.8, hB, 'Step0 in progress (1-3min) ...');
if ~status
set(hObject,'Enable','on','Value', 0);
errordlg('Step0: process failed.');
return
end
nTiles = nTiles0;
valT = num2str(nTiles);
h = findobj('Tag','nTiles_value');
set(h,'String',valT);
waitbar(1, hB, 'Step0 in progress (1-3min) ...');
close(hB);
disp('Step0 Done.');
saveProgressToXML({{'step0' 'Status' 'Done'}; {'Tiles' 'nTiles' valT}});
set(hObject,'Value',1);
toc
[dn fn ex] = fileparts(imgFN);
tList = dir([dn '\' fn '_x2.5_z*']);
imgS = [dn '\' tList(1).name];
figure; imshow(imgS);
hM = msgbox('Step0 Done. Select Rotate parameter ...');
%pause(3); close(hM); %test
end
saveRunInfo(startTime);
function step1_Callback(hObject, eventdata, handles)
%% Convert tiles to Tif
global imgName batchRoot batchName
global iTiles jTiles nTiles
global tileWidth tileHeight
global bigWidth bigHeight rotate step
step = 'Step1'
startTime = datetime
if isempty(batchRoot)
set(hObject,'Value',0);
errordlg('No Image Analysis Project opened.');
return
end
sel = get(hObject,'Value');
if sel
tic
hB = waitbar(0.1, 'Step1 in progress (approx 30-60min) ...');
toc
xmlFile = [batchRoot 'ImageAnalysisProgress.xml'];
loadProgressFromXML(xmlFile);
batchRoot_ = batchRoot;
batchName_ = batchName;
imgFN = [batchRoot imgName '.ndpi'];
[status iTiles0, jTiles0] = img2tif(imgFN,hB);
%status = 1; iTiles0 = 1; jTiles0 = 2;
hB = waitbar(0.9, hB, 'Step1 in progress (approx 30-60min) ...');
if ~status
batch.img(1).step1.status = 'Error';
set(hObject,'Enable','on','Value', 0);
errordlg('Step1: process failed.');
return
end
loadProgressFromXML(xmlFile);
iTiles = iTiles0;
jTiles = jTiles0;
updatePara;
set(hObject,'Value',1);
hB = waitbar(1, hB, 'Step1 in progress (approx 30-60min) ...');
close(hB);
disp('Step1 Done.');
toc
hM = msgbox('Step1 Done.');
pause(3);
close(hM);
end
saveRunInfo(startTime)
function step2_Callback(hObject, eventdata, handles)
%% Preprocess tiles
global batchRoot batchName homeGUI step
step = 'Step2'
startTime = datetime
if isempty(batchRoot)
set(hObject,'Value',0);
errordlg('No Image Analysis Project opened.');
return
end
sel = get(hObject,'Value');
%test
%bigWidth = 16346*4; %61080; %## get image info just before saving i1j1.tif
%bigHeight = 11114*4; %39888;
if sel
tic
hB = waitbar(0, 'Step2 in progress (approx 40min) ...');
toc
%[dn fn ex] = fileparts(imgFN); %test
str1 = [batchRoot batchName '\'];
xmlFN = [batchRoot 'ImageAnalysisProgress.xml'];
homeGUI = which('BrainSegmenterGUI');
[homeGUI f e] = fileparts(homeGUI);
str2 = [homeGUI '\lib\ProjectDef_seg.xml'];
tList = dir([str1 'i*']);
nFiles = length(tList);
Miji;
MIJ.run('Refresh Jython Scripts');
waitbar(0.1, hB, 'Step2 in progress (approx 40min) ...');
%hBar = waitbar(0,'Step2 in Progress ...');
for i=1:nFiles
dn = tList(i).name;
if length(dn)>5, continue; end
str3 = [str1 dn];
paraPy = ['choose=' str3 '\ blue=1 red=1 green=1'];
MIJ.run('Farsight PreProcess', paraPy);
MIJ.run('Dispose All Windows', '/all non-image');
%MIJ.run('Select Window', 'Exception');
%batch()
%wait(jobStep2);
disp([dn ' preprocessed']);
%waitbar(i/nFiles,hBar);
% LM, 19Aug2015; delete intermediary images to save memory space
rmdir(str3,'s')
%
waitbar(i/nFiles, hB, ['Step2 in progress (approx 40min) ' dn '...']);
end
%MIJ.run('Close All'); % doesn't close Exception window
%close(hBar); %close all;
MIJ.exit;
waitbar(1, hB, 'Step2 in progress (approx 40min) ...');
close(hB);
disp('Step2 Done.');
loadProgressFromXML(xmlFN);
saveProgressToXML({{'step2' 'Status' 'Done'}});
toc
hObj2 = findobj('Tag', 'step2');
set(hObj2,'Value',1); %err deleted obj
hM = msgbox('Step2 Done.');
pause(3);
close(hM);
end
saveRunInfo(startTime)
function step3_Callback(hObject, eventdata, handles)
%% Run FTK cell segmentation on tiles
global batchRoot batchName homeGUI step
step = 'Step3'
startTime = datetime
if isempty(batchRoot)
if ~isempty(homeGUI)
[batch fileN ext] = fileparts(homeGUI);
else
set(hObject,'Value',0);
errordlg('No Image Analysis Project opened.');
return
end
end
sel = get(hObject,'Value');
if sel
tic
hB = waitbar(0, 'Step3 in progress (approx 50min) ...');
str1 = [batchRoot batchName '\'];
xmlFN = [batchRoot '\ImageAnalysisProgress.xml'];
homeGUI = which('BrainSegmenterGUI');
[homeGUI f e] = fileparts(homeGUI);
str2 = [homeGUI '\lib\ProjectDef_seg.xml'];
strLib3 = [homeGUI '\lib\3rdparty\Farsight0.4.5\bin\'];
tList = dir([str1 'i*_processed']);
nFiles = length(tList);
waitbar(0.2, hB, 'Step3 in progress (approx 50min) ...');
for i=1:nFiles
fn = tList(i).name;
dn = [str1 fn '\'];
listJpg = dir([dn '*_blue_RAW_BG.tif']);
if ~length(listJpg)
disp('Tile is clear, skip.');
continue;
end
fnNuc = [dn 'Results_Image_nuc.tif'];
if exist(fnNuc, 'file')
disp('Tile is segmented, skip.');
continue;
end
copyfile(str2, [dn 'ProjectDef_seg.xml']);
createInputImageXML(dn);
%addpath(genpath(dn));
%may need to go from 10 to 12-15 outside cell
inParam = [dn 'ProjectDef_seg.xml'];
inIDImg = [dn 'Results_Image_nuc.tif'];
inImg = [dn 'Input_Image.xml'];
outTable = [dn 'results_table_raw.txt']; %?raw
[status] = dos(['projproc ' inImg ' ' inIDImg ' ' outTable ' ' inParam], '-echo');
%save Command Window output to runSegmLog.txt
%# use try/catch and rename to i1j1c %test
if status
h = msgbox(['Folder ' fn '. Segmentation failed, continue.' ]);
pause(2);
close(h);
continue
end
waitbar(i/nFiles, hB, ['Step3 in progress (approx 50min) ' fn '...']);
h = msgbox(['Segmentation done! Folder ' fn]);
pause(2);
close(h);
end
disp('Step3 Done.');
toc
waitbar(1, hB, 'Step3 in progress (approx 50min) ...');
close(hB);
loadProgressFromXML(xmlFN);
saveProgressToXML({{'step3' 'Status' 'Done'}});
set(hObject,'Value',1);
hB = msgbox('Step3 Done.');
pause(3);
close(hB);
end
saveRunInfo(startTime)
function step4_Callback(hObject, eventdata, handles)
%% Run FTK associative rules for cell classification
global batchRoot batchName nTile
global homeGUI step
step = 'Step4'
startTime = datetime
if isempty(batchRoot)
set(hObject,'Value',0);
errordlg('No Image Analysis Project opened.');
return
end
sel = get(hObject,'Value');
if sel
tic
hB = waitbar(0, 'Step4 in progress (approx 30-60min) ...');
%[dn fn ex] = fileparts(imgFN);
str1 = [batchRoot batchName '\'];
homeGUI = which('BrainSegmenterGUI');
[homeGUI f e] = fileparts(homeGUI);
str2 = [homeGUI '\lib\HistoProjectDef_asso_feat.xml'];
strLib3 = [homeGUI '\lib\3rdparty\Farsight0.4.5\bin\'];
tList = dir([str1 'i*_processed']);
nFiles = length(tList);
waitbar(0.2, hB, 'Step4 in progress (approx 30-60min) ...');
for i=1:nFiles
fn = tList(i).name;
dn = [str1 fn '\'];
if ~exist([dn 'ProjectDef_seg.xml'],'file')
h = msgbox(['Skip tile ' dn '. XML definition file absent.']);
continue;
end
disp(['=== Get RAW ASSOCIATIONS in: ' fn]);
copyfile(str2, [dn 'HistoProjectDef_asso_feat.xml']);
%may need to go from 10 to 12-15 outside cell
inParam = [dn 'HistoProjectDef_asso_feat.xml'];
inIDImg = [dn 'Results_Image_nuc.tif'];
inImg = [dn 'Input_Image.xml'];
outTable = [dn 'results_table_raw.txt'];
if exist(inImg, 'file') & exist(outTable, 'file')
[status] = dos(['projproc ' inImg ' ' inIDImg ' ' outTable ' ' inParam], '-echo');
disp(['=== RAW ASSOCIATIONS done in: ' fn]);
%save Command Window output to runSegmLog.txt %test
else
status = 1;
end
if status
h = msgbox(['Folder ' fn '. FTK RAW ASSOCIATIONS failed, continue.']);
continue
end
waitbar(i/nFiles, hB, ['Step4 in progress (approx 30-60min) ' fn '...']);
h = msgbox(['FTK RAW ASSOCIATIONS done! Folder ' fn]);
pause(2);
close(h);
end
disp('Step4 Done.');
toc
waitbar(1, hB, 'Step4 in progress (approx 30-60min) ...');
close(hB);
saveProgressToXML({{'step4' 'Status' 'Done'}});
set(hObject,'Value',1);
hM = msgbox('Step4 Done.');
pause(3);
close(hM);
end
saveRunInfo(startTime)
function step5_Callback(hObject, eventdata, handles)
%% Compute min/max intensity
% And append as 4 columns to result text file.
global batchRoot batchName nTiles
global redTresh greenTresh step
global maxRedTresh maxGreenTresh flipS
step = 'Step5'
startTime = datetime
if isempty(batchRoot)
set(hObject,'Value',0);
errordlg('No Image Analysis Project opened.');
return
end
disp(['redTresh: ' num2str(redTresh)]);
disp(['greenTresh: ' num2str(greenTresh)]);
disp(['maxRedTresh: ' num2str(maxRedTresh)]);
disp(['maxGreenTresh: ' num2str(maxGreenTresh)]);
sel = get(hObject,'Value');
if sel
tic
hB = waitbar(0, 'Step5 in progress (approx 15-20min) ...');
%% Load ij tile objects
batchFullName = [batchRoot batchName];
dirList = dir(batchFullName);
j1 = 0;
xy = cell(nTiles,1);
waitbar(0.2, hB, 'Step5 in progress (approx 15-20min) ...');
nFiles = length(dirList);
for i1 = 1:nFiles
%dirList = FindFiles('*_processed','',dn3);
processedF = dirList(i1).name;
%disp(processedF); %test
if ~isempty(strfind(processedF,'_processed'))
fn = [batchFullName '\' processedF '\results_table_raw.txt'];
if exist(fn, 'file')
%disp(fn); %test
found = 1;
j1 = j1 +1;
%disp(processedF(2)); %test
xyTemp = importdata(fn); %use textscan() for Ascii
iTile = str2num(processedF(2));
jTile = str2num(processedF(4));
k = jTile + 4*(iTile-1);
disp(['===== Loading tile: i' num2str(iTile) 'j' num2str(jTile) 'k' num2str(k)]);
xy{k}.data = xyTemp.data(:,1:3);
xy{k}.iTile = iTile;
xy{k}.jTile = jTile;
dnImg = [batchFullName '\' processedF '\'];
fnImg = dir([dnImg '*blue*_RAW.tif']);
if exist([dnImg fnImg.name], 'file')
imgInfo = imfinfo([dnImg fnImg.name]);
imgInfo = imgInfo(1);
xy{k}.width = imgInfo.Width;
xy{k}.height = imgInfo.Height;
else
disp(['Can''t open RAW blue channel to get image info. Continue.']);
end
%% Check if exists red_TOTAL column, then treshold data
findRed = strfind(xyTemp.colheaders,'red_TOTAL');
xy{k}.tracerRed = [];
col = find(~cellfun(@isempty,findRed));
if ~isempty(col)
tempM = xyTemp.data(:,col); %34);
tempM(tempM < redTresh)=0;
xy{k}.tracerRed = tempM;
disp('=== Compute min/max Red:');
imnuc = [dnImg 'Results_Image_nuc.tif'];
img = dir([dnImg '*red*_RAW.tif']); %on RAW
img = [dnImg img(1).name];
ids = xyTemp.data(:,1);
tracer = xy{k}.tracerRed(:);
nIDs = length(ids);
fprintf('The tile has a total of %d cells \n',nIDs);
nTr = find(tracer);
nTr = length(nTr);
if nTr
fprintf('There are only %d cells with red tracer \n',nTr);
minRed = []; maxRed = [];
[minRed maxRed] = getMinMaxIntensity(imnuc, img, ids, tracer)
xy{k}.minRed = minRed;
xy{k}.maxRed = maxRed;
else
fprintf('There are no cells with red tracer. Skip. \n');
xy{k}.minRed = zeros(nIDs,1); xy{k}.maxRed = zeros(nIDs,1);
end
end
%% Check if exists green_TOTAL, then treshold data
findGreen = strfind(xyTemp.colheaders,'green_TOTAL');
xy{k}.tracerGreen = [];
col = find(~cellfun(@isempty,findGreen));
if ~isempty(col)
tempM = xyTemp.data(:,col); %35);
tempM(tempM < greenTresh)=0;
xy{k}.tracerGreen = tempM;
disp('=== Compute min/max Green:');
imnuc = [dnImg 'Results_Image_nuc.tif'];
img = dir([dnImg '*green*_RAW.tif']); %on RAW !
img = [dnImg img(1).name];
ids = xyTemp.data(:,1);
tracer = xy{k}.tracerGreen(:);
nIDs = length(ids);
fprintf('The tile has a total of %d cells \n',nIDs);
nTr = find(tracer);
nTr = length(nTr);
if nTr
fprintf('There are only %d cells with green tracer \n',nTr);
minGreen = []; maxGreen = [];
[minGreen maxGreen] = getMinMaxIntensity(imnuc, img, ids, tracer)
xy{k}.minGreen = minGreen; xy{k}.maxGreen = maxGreen;
else
fprintf('There are no cells with green tracer. Skip. \n');
xy{k}.minGreen = zeros(nIDs,1); xy{k}.maxGreen = zeros(nIDs,1);
end
end
%% Min/Max column append
%test
%{
% if ~isempty(xy{k}.minRed)
% colNames = {'minRed' 'maxRed'};
% colData = [xy{k}.minRed xy{k}.maxRed];
% farsight_results_append(fn,colNames,colData);
% end
% if ~isempty(xy{k}.minGreen)
% colNames = { 'minGreen' 'maxGreen'};
% colData = [xy{k}.minGreen xy{k}.maxGreen];
% farsight_results_append(fn,colNames,colData);
% end
%}
colNames = {'minRed' 'maxRed' 'minGreen' 'maxGreen'};
colData = [xy{k}.minRed xy{k}.maxRed xy{k}.minGreen xy{k}.maxGreen];
farsight_results_append(fn,colNames,colData);
else
disp(['File ' processedF ' doesn''t exist. Continue.']);
end
end
waitbar(i1/nFiles, hB, ['Step5 in progress (approx 15-20min) ' processedF '...']);
end
if j1 == 0
errordlg(['Could not find processed tiles in directory ' batchFullName],'File Error');
return;
end;
%% step done
xyTemp = []; xBig = []; yBig = []; cBig = []; %xy{nTiles} = [];
disp('Step5 Done.');
toc
waitbar(1, hB, 'Step5 in progress (approx 15-20min) ...');
close(hB);
saveProgressToXML({{'step5' 'Status' 'Done'}});
set(hObject,'Value',1);
hM = msgbox('Step5 Done.');
pause(3);
close(hM);
end
saveRunInfo(startTime)
function step6_Callback(hObject, eventdata, handles)
%% Load Image Analysis Results
global batchRoot batchName nTiles
global redTresh greenTresh refROI
global maxRedTresh maxGreenTresh flipS
global xBig yBig cBig nCells step3
global bigWidth bigHeight
global regisRoot sectName flipS
if isempty(batchRoot)
set(hObject,'Value',0);
errordlg('No Image Analysis Project opened.');
return
end
if ~redTresh
msgbox('Step6: No Red Treshold defined. Error.');
end
%if ~redTresh
sel = get(hObject,'Value');
if sel
roi40 = [];
if strcmp(step3,'None')
set(hObject,'Value',0);
errordlg('Step3 not completed.');
return
end
%% if needed, create folders for regitration files
[dn fn ex] = fileparts(batchRoot);
[dn fn ex] = fileparts(dn);
dnBFI = [dn '\bfi4regis']
if ~exist(dnBFI, 'dir')
mkdir(dn,'bfi4regis');
end
dnRegis = [dn '\ndpi4regis']
if ~exist(dnRegis, 'dir')
mkdir(dn,'ndpi4regis');
end
dnSection = [dnRegis '\' sectName]
if ~exist(dnSection, 'dir')
mkdir(dnRegis,sectName);
end
%% Load ij tile objects
batchFullName = [batchRoot batchName];
dirList = dir([batchFullName '\i*_processed']);
%nTiles, get from xml
nTiles = length(dirList);
j1 = 0; k=0;
xy = cell(nTiles,1);
hB = waitbar(0, 'Step6 in progress, loading tiles...');
for i1 = 1:nTiles
%dirList = FindFiles('*_processed','',dn3);
processedF = dirList(i1).name;
%disp(processedF); %if ~isempty(strfind(processedF,'_processed')) %test
fileFound = 0;
dnTile = [batchFullName '\' processedF];
fn = [dnTile '\results_table_raw_edit.txt'];
if ~exist(fn, 'file')
fn = [dnTile '\results_table_raw.txt'];
if ~exist(fn, 'file')
disp(['No results*.txt file in folder ' ...
dnTile ', need to run Step4/5. Skip.']);
fileFound = 0;
continue;
else
fileFound = 1;
end
else
fileFound = 1;
end
if ~fileFound, continue; end
found = 1;
%disp(fn); %j1 = j1 +1; %disp(processedF(2)); %test
xyTemp = importdata(fn);
iTile = str2num(processedF(2));
jTile = str2num(processedF(4));
k = k+1; %jTile + 2*(iTile-1); %iMax
disp(['Loading tile: i' num2str(iTile) 'j' num2str(jTile) 'k' num2str(k)]);
xy{k}.data = xyTemp.data(:,1:3);
xy{k}.iTile = iTile;
xy{k}.jTile = jTile;
%% init Tracer r/g info
findRed = strfind(xyTemp.colheaders,'red_TOTAL');
xy{k}.tracerRed = [];
if ~isempty(findRed)
col = find(~cellfun(@isempty,findRed));
tempM = xyTemp.data(:,col); %34); %test
tempM(tempM < redTresh)=0;
xy{k}.tracerRed = tempM;
end
findGreen = strfind(xyTemp.colheaders,'green_TOTAL');
xy{k}.tracerGreen = [];
if ~isempty(findGreen)
col = find(~cellfun(@isempty,findGreen));
tempM = xyTemp.data(:,col); %34); %test
tempM(tempM < greenTresh)=0;
xy{k}.tracerGreen = tempM;
end
findTracer = strfind(xyTemp.colheaders,'minRed');
xy{k}.tracerMinMax = [];
col = find(~cellfun(@isempty,findTracer));
if ~isempty(col)
colLast = col+3;
tempM = xyTemp.data(:,col:colLast); %4 columns
xy{k}.tracerMinMax = tempM;
% else
% errordlg(['Min/Max tracer info not found for tile i' num2str(iTile) 'j' num2str(jTile) ', might need to run Step5. Skip.']);
% return
end
dnImg = [batchFullName '\' processedF '\'];
fnImg = dir([dnImg '*blue*_RAW.tif']);
if exist([dnImg fnImg.name], 'file')
imgInfo = imfinfo([dnImg fnImg.name]);
imgInfo = imgInfo(1);
xy{k}.width = imgInfo.Width;
xy{k}.height = imgInfo.Height;
else
disp('Can''t open RAW blue channel to get image info. Continue.');
end
waitbar(i1/nTiles, hB, 'Step6 in progress, loading tiles...');
end
%% Compute tile information and offset to get the big image points/dots
disp('======= Combine all tiles into one big figure:');
width = xy{1}.width;
height = xy{1}.height;
xPlot = []; yPlot = []; cPlot = [];
xBig = []; yBig = []; cBig = [];
tileTracerInfo = []; tracerInfo = [];
waitbar(0, hB, 'Step6 in progress, processing tiles...');
for i = 1:nTiles
if isempty(xy{i}), continue; end
iTile = xy{i}.iTile;
jTile = xy{i}.jTile;
offsetX = (iTile-1) * height; %test 9972; %15270;
offsetY = (jTile-1) * width; %test 15270; %9972;
disp(['processing i' num2str(iTile) 'j' num2str(jTile) ...
', offX=' num2str(offsetX/(iTile-1)) ...
', offY=' num2str(offsetY/(jTile-1))]);
%k = jTile + 4*(iTile-1); %test
xPlot = xy{i}.data(:,2);
xPlot = xPlot + offsetY;
yPlot = xy{i}.data(:,3);%test abs(.. -15270); %*offsetY; axis ij
yPlot = yPlot + offsetX;
cPlot = zeros(length(xPlot),3);
%% Set all points by default to blue color [0 0 1]
cPlot(:,1) = 0; cPlot(:,2) = 0; cPlot(:,3) = 1;
n2 = length(xPlot);
%% Set red color [r 0 0]
% green last, because red+ can show green+ also
if ~isempty(xy{i}.tracerMinMax)
for k1 = 1:n2
if (xy{i}.tracerMinMax(k1,2) >= maxRedTresh)
% t1 = logical(xy{i}.tracerGreen(i2));
% t2 = logical(xy{i}.tracerRed(i2));
% if ~t1 & t2;
if cPlot(k1,2) %for test only
%disp();
errordlg(['Cell b' num2str(i) 'k' num2str(k1) ' double marked: Green and now Red. Test...']);
end