-
Notifications
You must be signed in to change notification settings - Fork 0
/
FRET_Analyser1.4.py
1391 lines (1089 loc) · 49.6 KB
/
FRET_Analyser1.4.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
# @String(label="Experiment Title", description="Set the title of your experiment") Title
# @File(label="Select a file") Experiment
# @File(label="Select Root directory", style="directory") Root
# @File(label="Select Image Classifier") classifier
# @Integer(label="Control series:", description="The number of baseline measurements", value=3) Control_num
# @Integer(label="Stimulation number:", description="The number of stimulation protocols applied", value=1) Stim_num
# @Boolean(label="Advanced settings", description="Set processing parameters", value=True) Adv_set
import os
import sys
import collections
import ConfigParser
import ast
import time
from math import sqrt
from datetime import datetime
import itertools
from itertools import repeat, chain
import json
from jarray import array
from ij import IJ, WindowManager, ImagePlus
from ij.gui import GenericDialog
from ij.gui import Roi
from ij.gui import Plot
from ij.gui import WaitForUserDialog
from ij.measure import ResultsTable
from ij.plugin import ZProjector
from ij.plugin import CompositeConverter
from ij.plugin import ImageCalculator
from ij.plugin import Duplicator
from ij.plugin.filter import ThresholdToSelection
from ij.plugin.filter import EDM
from ij.plugin.filter import Binary
from ij.plugin.filter import Analyzer
from ij.plugin.filter import BackgroundSubtracter
from ij.plugin.frame import RoiManager
from ij.process import ImageConverter
from ij.process import ImageProcessor
from loci.formats import ImageReader
from loci.formats import MetadataTools
from loci.plugins import BF
from loci.plugins.in import ImporterOptions
from loci.common import Region
from fiji.threshold import Auto_Threshold
from register_virtual_stack import Register_Virtual_Stack_MT
import register_virtual_stack.Transform_Virtual_Stack_MT
from trainableSegmentation import WekaSegmentation
import Watershed_Irregular_Features
from ome.units import UNITS
from java.awt import Color
from java.awt import Dimension
from java.awt import Panel
from java.util import Vector
from java.awt import Font
def main():
""" Master method and tabulator. """
# Analysis timer, start.
startTime = datetime.now()
# If advanced settings is checked,
# gets parameters from user and writes to config,
# else; gets parameters from config file.
if Adv_set == True:
parameters = settings()
else:
parameters = config_read()
# Metadata parser
channels, timepoints, timelist, timelist_unsorted, LP, org_size = meta_parser()
# User inputs
if Stim_num >= 1:
Input_data, Input_data_JSON, Stim_List = user_input()
else:
Stim_List = False
pass
# Directory spawner.
dirs = directorator(Title, str(Root))
# Projections
imageprojector(channels, timelist_unsorted, dirs)
# Composite image generator
Compositor(timepoints, channels, dirs)
# Background subtracter.
Backgroundremoval(dirs, parameters)
# Composite image aligner.
Composite_Aligner(channels, dirs, parameters)
# Raw image transformer (registration).
Transformer(channels, dirs)
# Composite image segmentation.
segmentation = Weka_Segm(dirs)
# seFRET/3-channel mode. Calls measurement method,
# performs calculations, writes results to txt table,
# calls for plots, gifs and ratiometric image generation.
if channels == 3:
# Measurements and calculations.
raw_data = Measurements(channels, timelist, dirs, parameters)
FRET_val, cFRET = three_cube(raw_data, LP)
# Tabulator.
results_table = []
table = open(os.path.join(dirs["Tables"], "Resultstable.txt"), "w")
[[results_table.append([a, b, c, d, e, f])for a, b, c, d, e, f in zip(s1,s2,s3,s4,s5,s6)]
for s1,s2,s3,s4,s5,s6 in zip(FRET_val["Raw"], cFRET, FRET_val["dFRET"],
FRET_val["aFRET"], raw_data["Slices"], raw_data["Time"])
]
table.write("\t\t\t".join(map(str,["Raw", "cFRET", "dFRET", "aFRET", "Slice", "Time "])))
table.write(("_")*128)
for line in range (len(results_table)):
table.write("\n")
table.write("\n")
table.write("\t\t\t".join(map(str,results_table[line])))
table.close()
# Plot FRET values from dict.
for FRET_value_ID, FRET_value in FRET_val.iteritems():
plots(FRET_value, timelist, raw_data["Cell_num"],
FRET_value_ID, Stim_List, dirs, parameters
)
# This plot-call returns scale.
max_Y, min_Y = plots(FRET_val["Raw"], timelist, raw_data["Cell_num"],
"Raw", Stim_List, dirs, parameters)
# Corrects donor concentration, plots concentrations.
IDD_list = [[IDD + CFRET for (IDD, CFRET) in zip(x, y)]
for (x, y) in zip(raw_data["IDD"], cFRET)]
plots(IDD_list, timelist, raw_data["Cell_num"],
"Donor concentration", Stim_List, dirs, parameters
)
plots(FRET_val["A_Conc"], timelist, raw_data["Cell_num"],
"Acceptor concentration", Stim_List, dirs, parameters
)
# Ratiometric/2-channel mode. Same as for 3ch with less calculations.
elif channels == 2:
raw_data = Measurements(channels, timelist, dirs, parameters)
FRET_val = Ratiometric(raw_data, parameters)
for FRET_value_ID, FRET_value in FRET_val.iteritems():
plots(FRET_value, timelist, raw_data["Cell_num"],
FRET_value_ID, Stim_List, dirs, parameters
)
# Scale, ROI color coded overlay and gif animation.
Overlayer(org_size, dirs)
# TODO: Ratiometric image generator.
#ratiometric(LP, org_size, max_Y, min_Y)
# Done, prints/logs time used.
print ("Finished analysis in: "+str(datetime.now()-startTime))
IJ.log("Finished analysis in: "+str(datetime.now()-startTime))
def settings():
""" Settings """
# Calls potential config file to set defaults
# for settings dialog. Sets all defaults to 0
# if no config file exists.
con_path = os.path.join(str(Root), "FRET_params.cfg")
if os.path.exists(con_path):
dflt = config_read()
print type(dflt.get("b_sub", "Rolling ball"))
print (dflt.get("b_sub", "Rolling ball"))
print type(int(dflt.get("b_sub", "Rolling ball")))
print (dflt.get("b_sub", "Rolling ball"))
else:
dflt = {}
feat_model_strings = ["Translation", "Rigid",
"Similarity", "Affine
]
reg_model_strings = ["Translation", "Rigid",
"Similarity", "Affine",
"Elastic", "Least Squares"
]
b_sub_strings = ["Rolling Ball", "Manual Selection"]
# Registration parameters dialog.
gd = GenericDialog("Advanced Settings")
gd.addMessage("REGISTRATION PARAMETERS")
gd.addNumericField("Steps per scale octave: ", float(dflt.get("steps", 6)), 0, 7, "")
gd.addNumericField("Max Octave Size: ", float(dflt.get("max_oct", 1024)), 0, 7, "")
gd.addNumericField("Feature Descriptor Size: ", float(dflt.get("fd_size", 12)), 1, 7, "")
gd.addNumericField("Initial Sigma: ", float(dflt.get("sigma", 1.2)), 2, 7, "")
gd.addNumericField("Max Epsilon: ", float(dflt.get("max_eps", 15)), 1, 7, "")
gd.addNumericField("Min Inlier Ratio: ", float(dflt.get("min_inlier", 0.05)), 3, 7, "")
gd.addCheckbox("Use Shrinkage Constraint", ast.literal_eval(dflt.get("shrinkage", "False")))
gd.addChoice("Feature extraction model", feat_model_strings,
feat_model_strings[int(dflt.get("feat_model", 1))]
)
gd.addChoice("Registration model", reg_model_strings,
reg_model_strings[int(dflt.get("reg_model", 1))]
)
# Background removal parameters dialog.
gd.addPanel(Panel())
gd.addMessage("BACKGROUND REMOVAL")
gd.addChoice("Subtraction method:", b_sub_strings,
b_sub_strings[int(dflt.get("b_sub", 0))]
)
gd.addNumericField("Rolling ball size: ",
float(dflt.get("ballsize", 50)), 1, 7, "px"
)
gd.addCheckbox("Create Background", ast.literal_eval(dflt.get("create_b", "False")))
gd.addCheckbox("Light Background", ast.literal_eval(dflt.get("light_b", "False")))
gd.addCheckbox("Use Parabaloid", ast.literal_eval(dflt.get("parab", "False")))
gd.addCheckbox("Do Pre-smoothing", ast.literal_eval(dflt.get("smooth", "False")))
gd.addCheckbox("Correct Corners", ast.literal_eval(dflt.get("corners", "False")))
# Measumrent parameters dialog.
gd.addPanel(Panel())
gd.addMessage("MEASUREMENT PARAMETERS")
gd.addNumericField("Max Cell Area", float(dflt.get("cell_max", 2200)), 0, 7, "px")
gd.addNumericField("Min Cell Area", float(dflt.get("cell_min", 200)), 0, 7, "px")
gd.addNumericField("Ratio Subtraction", float(dflt.get("subtr_ratio", 0.31)), 3, 7, "")
# Plot parameters dialog.
gd.addPanel(Panel())
gd.addMessage("PLOT PARAMETERS")
gd.addNumericField("Max y, d and aFRET", float(dflt.get("p_max", 0.65)), 2, 7, "")
gd.addNumericField("Min y, d and aFRET", float(dflt.get("p_min", 0.0)), 2, 7, "")
gd.addNumericField("Max y, norm. d and aFRET", float(dflt.get("p_max_n", 1.65)), 2, 7, "")
gd.addNumericField("Min y, norm. d and aFRET", float(dflt.get("p_min_n", 0.5)), 2, 7, "")
# Set location of dialog on screen.
#gd.setLocation(0,1000)
gd.showDialog()
# Checks if cancel was pressed, kills script.
if gd.wasCanceled() is True:
sys.exit("Cancel was pressed, script terminated.")
# Parameters dictionary.
parameters = {"steps" : gd.getNextNumber(),
"max_oct" : gd.getNextNumber(),
"fd_size" : gd.getNextNumber(),
"sigma" : gd.getNextNumber(),
"max_eps" : gd.getNextNumber(),
"min_inlier" : gd.getNextNumber(),
"shrinkage" : gd.getNextBoolean(),
"feat_model" : gd.getNextChoiceIndex(),
"reg_model" : gd.getNextChoiceIndex(),
"b_sub" : gd.getNextChoiceIndex(),
"ballsize" : gd.getNextNumber(),
"create_b" : gd.getNextBoolean(),
"light_b" : gd.getNextBoolean(),
"parab" : gd.getNextBoolean(),
"smooth" : gd.getNextBoolean(),
"corners" : gd.getNextBoolean(),
"cell_max" : gd.getNextNumber(),
"cell_min" : gd.getNextNumber(),
"subtr_ratio" : gd.getNextNumber(),
"p_max" : gd.getNextNumber(),
"p_min" : gd.getNextNumber(),
"p_max_n" : gd.getNextNumber(),
"p_min_n" : gd.getNextNumber()
}
parameters = config_write(parameters)
return parameters
def config_write(parameters):
config = ConfigParser.RawConfigParser()
config.add_section("Parameters")
for key, value in parameters.iteritems():
config.set("Parameters", str(key), str(value))
with open(os.path.join(str(Root), "FRET_params.cfg"), "wb") as configfile:
config.write(configfile)
return parameters
def config_read():
""" Config file reader, returns parameters from config file. """
# Launch parser, set path to cfg file.
config = ConfigParser.RawConfigParser()
con_path = os.path.join(str(Root), "FRET_params.cfg")
# Read config if .cfg exists.
if os.path.exists(con_path):
config.read(con_path)
# Read cfg section if cfg section exists.
try:
p_list = config.items("Parameters")
except ConfigParser.NoSectionError:
print ("NoSectionError: "
"Section 'Parameters' not found, please "
"check (" + str(con_path) + ") for the "
"correct section name, or change the "
"section name in config_read()"
)
raise
# Build dict. of parameters.
parameters = {}
for key, value in p_list:
parameters[key] = value
else:
print ("ERROR: Config file not found, check " + str(con_path) + " "
"or select Advanced Settings to generate a new config file "
)
raise IOError(
"ERROR: Config file not found, check " + str(con_path) + " "
"or select Advanced Settings to generate a new config file "
)
return parameters
def meta_parser():
""" Iterates through .lif XML/OME metadata, returns selected values eg. timepoints, channels, series count, laser power.. """
# Get metadata.
reader = ImageReader()
omeMeta = MetadataTools.createOMEXMLMetadata()
reader.setMetadataStore(omeMeta)
reader.setId(str(Experiment))
# Extracts number of image series, channel number
seriesCount = reader.getSeriesCount()
channels = reader.getSizeC()
#reader.close()
# Number of images
imageCount = omeMeta.getImageCount()
# Image size in pixels AND microns (for scalebar).
Physical_x = omeMeta.getPixelsPhysicalSizeX(0)
Pixel_x = omeMeta.getPixelsSizeX(0)
Physical_x = Physical_x.value()
Pixel_x = Pixel_x.getNumberValue()
# Assumes square image (x=y).
org_size = (Physical_x*Pixel_x)*2
# Laser power of donor excitation laser.
if channels == 3:
LP = omeMeta.getChannelLightSourceSettingsAttenuation(0,0)
LP = 1 - LP.getNumberValue()
else:
LP = 0
timelist = []
for timepoint in range (imageCount):
times = omeMeta.getImageAcquisitionDate(timepoint)
timelist.append(times.toString())
# YY.MM... to minutes.
timelist =[ time.mktime(time.strptime(times, u'%Y-%m-%dT%H:%M:%S')) for times in timelist ]
timelist_unsorted =[ (times - timelist[0])/60 for times in timelist ]
timelist = sorted(timelist_unsorted)
# Prints to log.
IJ.log("Total # of image series (from BF reader): " + str(seriesCount))
IJ.log("Total # of image series (from OME metadata): " + str(imageCount))
IJ.log("Total # of channels (from OME metadata): " + str(channels))
IJ.log("Laserpower (from OME metadata): " + str(LP))
return channels, seriesCount, timelist, timelist_unsorted, LP, org_size
def directorator(Title, Root):
""" Creates all required directories, adds (#) if experiment """
""" replicates exist, returns dict of directory paths. """
if not os.path.exists(Root):
os.makedirs(Root)
Exp_root_base = os.path.join(Root, Title)
Exp_root = Exp_root_base
Replicate = 1
while os.path.exists(Exp_root):
Exp_root = Exp_root_base + "(%d)" % (Replicate)
Replicate += 1
os.makedirs(Exp_root)
subdirs = [
"Plots", "Tables", "Projections",
"Projections_C0", "Projections_C1",
"Projections_C2", "Composites",
"Composites_Aligned", "Transformations",
"Aligned_All", "Overlays",
"Gifs"
]
dirs = {}
for Dest in subdirs:
os.makedirs(os.path.join(Exp_root, Dest))
dirs[Dest] = (os.path.join(Exp_root, Dest))
return dirs
def imageprojector(channels, timelist_unsorted, dirs):
""" Projects .lif timepoints and saves in a common directory,
as well as channel separated directories. """
# Defines in path
path = str(Experiment)
# BF Importer
options = ImporterOptions()
try:
options.setId(path)
except Exception(e):
print str(e)
options.setOpenAllSeries(True)
options.setSplitTimepoints(True)
options.setSplitChannels(True)
imps = BF.openImagePlus(options)
timelist = [x for item in timelist_unsorted for x in repeat(item, channels)]
timelist, imps = zip(*sorted(zip(timelist, imps)))
counter_C0 = -1
counter_C1 = -1
counter_C2 = -1
# Opens all images, splits channels, z-projects and saves to disk
for imp in (imps):
# Projection, Sum Intensity
project = ZProjector()
project.setMethod(ZProjector.SUM_METHOD)
project.setImage(imp)
project.doProjection()
impout = project.getProjection()
projection = impout.getTitle()
try:
# Saves channels to disk,
# add more channels here if desired,
# remember to define new counters.
if "C=0" in projection:
counter_C0 += 1
IJ.saveAs(impout, "TIFF", os.path.join(dirs["Projections"],
"Scan" + str(counter_C0).zfill(3) + "C0"))
IJ.saveAs(impout, "TIFF", os.path.join(dirs["Projections_C0"],
"Scan" + str(counter_C0).zfill(3) + "C0"))
elif "C=1" in projection:
counter_C1 += 1
IJ.saveAs(impout, "TIFF", os.path.join(dirs["Projections"],
"Scan" + str(counter_C1).zfill(3) + "C1"))
IJ.saveAs(impout, "TIFF", os.path.join(dirs["Projections_C1"],
"Scan" + str(counter_C1).zfill(3) + "C1"))
elif "C=2" in projection:
counter_C2 += 1
IJ.saveAs(impout, "TIFF", os.path.join(dirs["Projections"],
"Scan" + str(counter_C2).zfill(3) + "C2"))
IJ.saveAs(impout, "TIFF", os.path.join(dirs["Projections_C2"],
"Scan" + str(counter_C2).zfill(3) + "C2"))
except IOException:
print "Directory does not exist"
raise
IJ.log("Images projected and saved to disk")
def Composite_Aligner(channels, dirs, parameters):
""" Aligns composite images, saves to directory. """
# Reference image name (must be within source directory)
reference_name = "Timepoint000.tif"
# Shrinkage option (False = 0)
if parameters["shrinkage"] == "True":
use_shrinking_constraint = 1
print "shrink"
else:
use_shrinking_constraint = 0
print "noshrink"
# Parameters method, RVSS
p = Register_Virtual_Stack_MT.Param()
# SIFT parameters:
# python cannot coerce string "floats" to int directly,
# hence int(float("1.0")) (RVSS wants ints..).
p.sift.maxOctaveSize = int(float(parameters["max_oct"]))
p.sift.fdSize = int(float(parameters["fd_size"]))
p.sift.initialSigma = int(float(parameters["sigma"]))
p.maxEpsilon = int(float(parameters["max_eps"]))
p.sift.steps = int(float(parameters["steps"]))
p.minInlierRatio = int(float(parameters["min_inlier"]))
# 1 = RIGID, 3 = AFFINE
p.featuresModelIndex = int(float(parameters["feat_model"]))
p.registrationModelIndex = int(float(parameters["reg_model"]))
# Opens a dialog to set transformation options, comment out to run in default mode
#IJ.beep()
#p.showDialog()
# Executes alignment.
print ("Registering stack...")
reference_name = "Timepoint000.tif"
Register_Virtual_Stack_MT.exec(dirs["Composites"] + os.sep,
dirs["Composites_Aligned"] + os.sep,
dirs["Transformations"] + os.sep,
reference_name, p, use_shrinking_constraint)
print ("Registration completed.")
# Close alignment window.
imp = WindowManager.getCurrentImage()
imp.close()
def Transformer(channels, dirs):
""" Applies transformation matrices from Composite_Aligner to all raw, 32-bit projections. """
# Executes transformations for each channel.
t = register_virtual_stack.Transform_Virtual_Stack_MT
print "Transforming channel 0..."
t.exec(dirs["Projections_C0"] + os.sep,
dirs["Aligned_All"] + os.sep,
dirs["Transformations"] + os.sep,
True)
print "Channel 0 transformed."
imp = WindowManager.getCurrentImage()
imp.close()
print "Transforming channel 1..."
t.exec(dirs["Projections_C1"] + os.sep,
dirs["Aligned_All"] + os.sep,
dirs["Transformations"] + os.sep,
True)
print "Channel 1 transformed."
imp = WindowManager.getCurrentImage()
imp.close()
if channels == 3:
print "Transforming channel 2..."
t.exec(dirs["Projections_C2"] + os.sep,
dirs["Aligned_All"] + os.sep,
dirs["Transformations"] + os.sep,
True)
print "Channel 2 transformed."
imp = WindowManager.getCurrentImage()
imp.close()
def Weka_Segm(dirs):
""" Loads trained classifier and segments cells """
""" in aligned images according to training. """
# Define reference image for segmentation (default is timepoint000).
w_train = os.path.join(dirs["Composites_Aligned"], "Timepoint000.tif")
trainer = IJ.openImage(w_train)
weka = WekaSegmentation()
weka.setTrainingImage(trainer)
# Select classifier model.
weka.loadClassifier(str(classifier))
weka.applyClassifier(False)
segmentation = weka.getClassifiedImage()
segmentation.show()
# Convert image to 8bit
ImageConverter(segmentation).convertToRGB()
ImageConverter(segmentation).convertToGray8()
# Threshold segmentation to soma only.
hist = segmentation.getProcessor().getHistogram()
lowth = Auto_Threshold.IJDefault(hist)
segmentation.getProcessor().threshold(lowth)
segmentation.getProcessor().setThreshold(0, 0, ImageProcessor.NO_LUT_UPDATE)
segmentation.getProcessor().invert()
segmentation.show()
# Run Watershed Irregular Features plugin, with parameters.
IJ.run(segmentation, "Watershed Irregular Features",
"erosion=20 convexity_treshold=0 separator_size=0-Infinity")
# Make selection and add to RoiManager.
RoiManager()
rm = RoiManager.getInstance()
rm.runCommand("reset")
roi = ThresholdToSelection.run(segmentation)
segmentation.setRoi(roi)
rm.addRoi(roi)
rm.runCommand("Split")
def Measurements(channels, timelist, dirs, parameters):
""" Takes measurements of weka selected ROIs in a generated aligned image stack. """
# Set desired measurements.
an = Analyzer()
an.setMeasurements(an.AREA + an.MEAN + an.MIN_MAX + an.SLICE)
# Opens raw-projections as stack.
test = IJ.run("Image Sequence...",
"open=" + dirs["Aligned_All"]
+ " number=400 starting=1 increment=1 scale=400 file=.tif sort")
# Calls roimanager.
rm = RoiManager.getInstance()
total_rois = rm.getCount()
# Deletes artefact ROIs (too large or too small).
imp = WindowManager.getCurrentImage()
for roi in reversed(range(total_rois)):
rm.select(roi)
size = imp.getStatistics().area
if size < int(float(parameters["cell_min"])):
rm.select(roi)
rm.runCommand('Delete')
elif size > int(float(parameters["cell_max"])):
rm.select(roi)
rm.runCommand('Delete')
else:
rm.runCommand("Deselect")
# Confirm that ROI selection is Ok (comment out for headless run).
WaitForUserDialog("ROI check", "Control ROI selection, then click OK").show()
# Measure each ROI for each channel.
imp = WindowManager.getCurrentImage()
rm.runCommand("Select All")
rm.runCommand("multi-measure measure_all One row per slice")
# Close.
imp = WindowManager.getCurrentImage()
imp.close()
# Get measurement results.
rt = ResultsTable.getResultsTable()
Area = rt.getColumn(0)
Mean = rt.getColumn(1)
Slice = rt.getColumn(27)
# Removes (and counts) artefact ROIs (redundant)
# Area indices without outliers
Area_indices = [index for (index, value) in enumerate(Area, start=0)
if value > 0 and value < 9999999]
# Mean without outliers from area (redundant)
Filtered_mean = [Mean[index] for index in Area_indices]
Filtered_slice = [Slice[index] for index in Area_indices]
# Number of cell selections.
Cell_number = Filtered_slice.count(1.0)
rm = RoiManager.getInstance()
print "Number of selected cells: ", Cell_number
print "Total number of selections: ", rm.getCount()
Cells = [ Filtered_mean [x : x + Cell_number]
for x in xrange (0, len(Filtered_mean), Cell_number) ]
Cells_indices = [ index for (index, value) in enumerate(Cells) ]
time = [ x for item in timelist for x in repeat(item, Cell_number) ]
time = [ time [x : x + Cell_number] for x in xrange (0, len(time), Cell_number) ]
Slices = [ Filtered_slice [x : x + Cell_number]
for x in xrange (0, len(Filtered_slice), Cell_number) ]
# Lists IDD, IDA + IAA if 3ch.
if channels == 3:
IDD_list = [ Cells [index] for index in Cells_indices [0::int(channels)] ]
IDA_list = [ Cells [index] for index in Cells_indices [1::int(channels)] ]
IAA_list = [ Cells [index] for index in Cells_indices [2::int(channels)] ]
raw_data = {"IDD" : IDD_list, "IDA" : IDA_list, "IAA" : IAA_list,
"Cell_num" : Cell_number, "Slices" : Slices,
"Time" : time
}
elif channels == 2:
IDD_list = [ Cells [index] for index in Cells_indices [0::int(channels)] ]
IDA_list = [ Cells [index] for index in Cells_indices [1::int(channels)] ]
raw_data = {"IDD": IDD_list, "IDA" : IDA_list,
"Cell_num" : Cell_number, "Slices" : Slices,
"Time" : time
}
return raw_data
def three_cube(raw_data, LP):
""" Performs calculations on nested list data from three channels,
returns calculations as nested lists. """
# Direct excitation of acceptor, AER, and
# donor emission bleedthrough, DER, cofficients.
# (Determined experimentally.)
AER = ((8.8764*(LP**2)) + (1.8853*LP)) - 0.1035
DER = 0.1586
IDD_list, IDA_list, IAA_list = raw_data["IDD"], raw_data["IDA"], raw_data["IAA"]
Cell_number = raw_data["Cell_num"]
# Calculations.
Raw_ratio = [ [ IDA / IDD for (IDA, IDD) in zip(x, y) ] for (x, y) in zip(IDA_list, IDD_list) ]
cFRET = [ [ IDA - (AER*IAA) - (DER*IDD)
for (IDA, IAA, IDD) in zip(x, y, z) ]
for (x, y, z) in zip(IDA_list, IAA_list, IDD_list) ]
dFRET = [ [ (IDA - (AER*IAA) - (DER*IDD)) / (IDD + CFRET)
for (IDA, IDD, IAA, CFRET) in zip(x, y, z, c) ]
for (x, y, z, c) in zip(IDA_list, IDD_list, IAA_list, cFRET) ]
aFRET = [ [ ((IDA - (AER*IAA) - (DER*IDD)) / (IAA*AER))/4.66
for (IDA, IDD, IAA) in zip(x, y, z) ]
for (x, y, z) in zip(IDA_list, IDD_list, IAA_list) ]
dtoa = [ [ ((IDD + CFRET)/4.66) / (IAA*AER)
for (IDD, CFRET, IAA) in zip(x, y, z) ]
for (x, y, z) in zip(IDD_list, cFRET, IAA_list) ]
# Flattens nested lists for normalization function.
flat_aFRET = [item for sublist in aFRET for item in sublist]
flat_dFRET = [item for sublist in dFRET for item in sublist]
flat_raw = [item for sublist in Raw_ratio for item in sublist]
aFRET_base = flat_aFRET [0:Cell_number]
aFRET_base_mean = standard_deviation(aFRET_base)
dFRET_base = flat_dFRET [0:Cell_number]
dFRET_base_mean = standard_deviation(dFRET_base)
""" Calculates baseline-normalized values. """
norm_aFRET, norm_aFRET_self = [], []
norm_dFRET, norm_dFRET_self = [], []
norm_raw, norm_raw_self = [], []
baseline = Cell_number * Control_num
for cell in range(Cell_number):
# Divides value 0->max by baseline point 0-Cell_number for each cell.
norm_aFRET.append([ (flat_aFRET[v]) / ((sum(flat_aFRET[cell:baseline:Cell_number]))
/len(flat_aFRET[cell:baseline:Cell_number]))
for v in range(cell, len(flat_aFRET), Cell_number)])
norm_dFRET.append([ (flat_dFRET[v]) / ((sum(flat_dFRET[cell:baseline:Cell_number]))
/len(flat_dFRET[cell:baseline:Cell_number]))
for v in range(cell, len(flat_dFRET), Cell_number)])
norm_raw.append([ (flat_raw[v]) / ((sum(flat_raw[cell:baseline:Cell_number]))
/len(flat_raw[cell:baseline:Cell_number]))
for v in range(cell, len(flat_raw), Cell_number)])
norm_aFRET_self.append([ (flat_aFRET[v]) / (flat_aFRET[cell])
for v in range(cell, len(flat_aFRET), Cell_number)])
norm_dFRET_self.append([ (flat_dFRET[v]) / (flat_dFRET[cell])
for v in range(cell, len(flat_dFRET), Cell_number)])
norm_raw_self.append([ (flat_raw[v]) / (flat_raw[cell])
for v in range(cell, len(flat_raw), Cell_number)])
# Zips [[cell1],[cell2]] to [[time1],[time2]].
norm_aFRET = list(chain.from_iterable(zip(*norm_aFRET)))
norm_dFRET = list(chain.from_iterable(zip(*norm_dFRET)))
norm_raw = list(chain.from_iterable(zip(*norm_raw)))
norm_aFRET_self = list(chain.from_iterable(zip(*norm_aFRET_self)))
norm_dFRET_self = list(chain.from_iterable(zip(*norm_dFRET_self)))
norm_raw_self = list(chain.from_iterable(zip(*norm_raw_self)))
# Rounds final outputs.
Raw_ratio = [ [round(float(i), 3) for i in nested] for nested in Raw_ratio ]
dFRET = [ [round(float(i), 3) for i in nested] for nested in dFRET ]
aFRET = [ [round(float(i), 3) for i in nested] for nested in aFRET ]
cFRET = [ [int(i) for i in nested] for nested in cFRET ]
dtoa = [ [round(float(i), 3) for i in nested] for nested in dtoa ]
norm_aFRET = [round(float(i), 5) for i in norm_aFRET]
norm_dFRET = [round(float(i), 5) for i in norm_dFRET]
norm_raw = [round(float(i), 5) for i in norm_raw]
A_Conc = [ [ (IAA * AER) for (IAA) in x ] for x in IAA_list ]
# TODO: REMOVE UNWANTED PLOT VALUES, cFRET GOES SOLO
FRET_val = {"dFRET" : dFRET, "aFRET" : aFRET,
"Raw" : Raw_ratio, "DtoA" : dtoa,
"A_Conc" : A_Conc, "Normalized aFRET" : norm_aFRET,
"Normalized dFRET" : norm_dFRET,
"Normalized aFRET mean" : norm_aFRET_self,
"Normalized dFRET mean" : norm_dFRET_self,
}
return FRET_val, cFRET
def Ratiometric(raw_data, parameters):
""" Performs calculations on nested list data from two channels,
returns calculations as nested lists. """
IDD_list, IDA_list = raw_data["IDD"], raw_data["IDA"]
Cell_number = raw_data["Cell_num"]
# Calculations
Raw_ratio = [ [ IDA / IDD for (IDA, IDD) in zip(x, y) ]
for (x, y) in zip(IDA_list, IDD_list) ]
Sergei_ratio = [[(IDA / IDD) - (float(parameters["subtr_ratio"]))
for (IDA, IDD) in zip(x, y)]
for (x, y) in zip(IDA_list, IDD_list)]
baseline = Cell_number * Control_num
norm_raw, norm_Sergei = [], []
flat_raw = [item for sublist in Raw_ratio for item in sublist]
flat_Sergei = [item for sublist in Sergei_ratio for item in sublist]
for cell in range(Cell_number):
norm_raw.append([ (flat_raw[v]) / ((sum(flat_raw[cell:baseline:Cell_number]))
/len(flat_raw[cell:baseline:Cell_number]))
for v in range(cell, len(flat_raw), Cell_number)])
norm_Sergei.append([ (flat_Sergei[v]) / ((sum(flat_Sergei[cell:baseline:Cell_number]))
/len(flat_Sergei[cell:baseline:Cell_number]))
for v in range(cell, len(flat_Sergei), Cell_number)])
norm_raw = list(chain.from_iterable(zip(*norm_raw)))
norm_Sergei = list(chain.from_iterable(zip(*norm_Sergei)))
# Round.
Raw_ratio = [ [round(float(i), 3) for i in nested] for nested in Raw_ratio ]
Sergei_ratio = [ [round(float(i), 3) for i in nested] for nested in Sergei_ratio ]
norm_raw = [round(float(i), 5) for i in norm_raw]
norm_Sergei = [round(float(i), 5) for i in norm_Sergei]
FRET_val = {"Raw" : Raw_ratio, "Sergei" : Sergei_ratio,
"Normalized raw" : norm_raw, "Normalized sergei" : norm_Sergei
}
return FRET_val
def Compositor(timepoints, channels, dirs):
""" Creates composite images of all channels for each timepoint. """
# Creates composite stack from HDD images.
for time in range (1, (timepoints*channels), channels):
stack = IJ.run(
"Image Sequence...", "open="
+ dirs["Projections"] + " number=" + str(channels)
+ " starting=" + str(time) + " increment=1 scale=400 file=.tif sort")
comp = IJ.run("Make Composite", "display=Composite")
comp = IJ.run("Stack to RGB", comp)
IJ.saveAs(comp, "Tiff", os.path.join(dirs["Composites"],
"Timepoint"+str(time/channels).zfill(3)))
# Close windows.
for i in range (2):
try:
imp = WindowManager.getCurrentImage()
imp.close()
except:
pass
def Backgroundremoval(dirs, parameters):
""" Runs rolling ball background subtraction on all channels. """
# Processes channel 1.. etc
for root, directories, filenames in os.walk(dirs["Projections_C0"]):
for filename in filenames:
# Check for file extension
if filename.endswith(".tif"):
process(dirs["Projections_C0"], root, filename, parameters)
for root, directories, filenames in os.walk(dirs["Projections_C1"]):
for filename in filenames:
# Check for file extension
if filename.endswith(".tif"):
process(dirs["Projections_C1"], root, filename, parameters)
for root, directories, filenames in os.walk(dirs["Projections_C2"]):
for filename in filenames:
# Check for file extension
if filename.endswith(".tif"):
process(dirs["Projections_C2"], root, filename, parameters)
def process(Destination_Directory, Current_Directory, filename, parameters):
""" Rolling ball method. """
print "Processing:"
# Opening the image
print "Open image file", filename
imp = IJ.openImage(os.path.join(Current_Directory, filename))
ip = imp.getProcessor()
# Parameters: Image processor, Rolling Ball Radius, Create background,
# light background, use parabaloid, do pre smoothing (3x3),
# correct corners
b = BackgroundSubtracter()
b.rollingBallBackground(ip,
float(parameters["ballsize"]),
ast.literal_eval(parameters["create_b"]),
ast.literal_eval(parameters["light_b"]),
ast.literal_eval(parameters["parab"]),
ast.literal_eval(parameters["smooth"]),
ast.literal_eval(parameters["corners"])
)
print "Saving to", Destination_Directory
IJ.saveAs(imp, "Tiff", os.path.join(Destination_Directory, filename))
imp.close()
def Overlayer(org_size, dirs):
""" Overlays ROIs with appropriate color,
saves to .tif and animates aligned images to .gif """
# Get colors.
Colors, Colors_old = colorlist()
# Get ROImanager.
rm = RoiManager().getInstance()
rois = rm.getCount()
# Overlays ROI on aligned images, converts to 8-bit (for gif).
for root, directories, filenames in os.walk(dirs["Composites_Aligned"]):
for filename in filenames:
imp = IJ.openImage(os.path.join(root, filename))
converter = ImageConverter(imp)
converter.setDoScaling(True)
converter.convertToGray8()
# Lookup table and local contrast enhancement for vizualisation.
IJ.run(imp, "Rainbow RGB", "")
IJ.run(imp, "Enhance Local Contrast (CLAHE)",
"blocksize=127 histogram=256 maximum=3 mask=*None*")
for roi in range(rois):
roi_obj = rm.getRoi(roi)
roi_obj.setStrokeWidth(2)
if roi < 19:
roi_obj.setStrokeColor(Color(*Colors[roi][0:3]))
else:
roi_obj.setStrokeColor(eval(Colors_old[roi]))
rm.moveRoisToOverlay(imp)
IJ.saveAs(imp, "Tiff", os.path.join(dirs["Overlays"], filename))