comparison stack_max_projs.groovy @ 0:a02156aa8bda draft

planemo upload for repository https://github.com/lldelisle/tools-lldelisle/tree/master/tools/max_projections_stack_and_upload_omero commit 69a86e0a8ef82ce3c2232b99f72717a184f4d4cf
author lldelisle
date Thu, 12 Dec 2024 12:42:20 +0000
parents
children 9be687213bc9
comparison
equal deleted inserted replaced
-1:000000000000 0:a02156aa8bda
1 /**
2 *
3 * The purpose of this script is to combine a series of time-lapse images into
4 * one file per image with possibly multiple channels and multiple time points
5 *
6 * To make the script run
7 * 1. Create a parent folder (base_dir) and a output folder (output_dir)
8 * 2. The struction of the base_dir must be: one directory per final image and per channel. All the directories should be: `unique_identifier` `suffix specific to channel`.
9 * 3. The image names will be sorted before being merged.
10 * 4. The images must be regular tif.
11 *
12 * The expected outputs are:
13 * 1. In the output_dir one tiff per `unique_identifier` (potentially multi-T and potentially multi-C)
14 */
15
16 #@ File(style="directory", label="Directory with one directory per final image and per channel") base_dir
17 #@ File(style="directory", label="Output directory (must exist)") output_dir
18 #@ String(label="Suffix for white channel directory", value="_BF_max", help="Leave empty if you are not interested") suffix_white
19 #@ String(label="Suffix for fluo channel(s) directory", value="_Fluo_max", help="Leave empty if you are not interested") suffix_fluo
20 #@ String(label="Pattern for green channel images", value="_H2B-GFP", help="Leave empty if you are not interested") pattern_green
21 #@ String(label="Pattern for red channel images", value="_RFP670", help="Leave empty if you are not interested") pattern_red
22
23
24 /**
25 * *****************************************************************************************************************
26 * ********************************************* Final Variables **************************************************
27 * ********************************************* DO NOT MODIFY ****************************************************
28 * ****************************************************************************************************************
29 */
30
31 // Version number = date of last modif
32 VERSION = "20241212"
33
34 /**
35 * *****************************************************************************************************************
36 * **************************************** Beginning of the script ***********************************************
37 * ****************************************************************************************************************
38 */
39
40 try {
41
42 println "Beginning of the script"
43
44 IJ.run("Close All", "")
45
46 // Find all directories
47 File[] dir_list = base_dir.listFiles()
48
49 // The images are stored in a TreeMap where
50 // keys are unique_identifier
51 // values are a TreeMap that we call channelMap where:
52 // keys are colors (Green, Grays, Red)
53 // values are an ImagePlus (T-stack)
54 Map<Integer, Map<String, ImagePlus>> samplesMap = new TreeMap<>()
55 List<String> dir_suffix_list = [suffix_white, suffix_fluo]
56 List<String> dir_channels_list = ["Grays", "Fluo"]
57
58 List<String> fluo_pattern_list = [pattern_green, pattern_red]
59 List<String> fluo_channels_list = ["Green", "Red"]
60
61 // Loop over directories:
62 for (File current_directory : dir_list) {
63 // Ignore if it is not a directory
64 if (! current_directory.isDirectory()) {
65 continue
66 }
67 String current_directory_name = current_directory.getName()
68 // Check if it matches one of the suffix
69 String final_color = ""
70 // And find the unique identifier:
71 String unique_identifier = ""
72 for(int i = 0; i < dir_suffix_list.size(); i++){
73 if (dir_suffix_list[i] != "" && current_directory_name.endsWith(dir_suffix_list[i])) {
74 final_color = dir_channels_list[i]
75 unique_identifier = current_directory_name.replace(dir_suffix_list[i], "")
76 continue
77 }
78 }
79 if (final_color == "") {
80 println current_directory_name + " do not match any suffix."
81 continue
82 }
83 if (! samplesMap.containsKey(unique_identifier) ) {
84 // Initiate the Map
85 samplesMap.put(unique_identifier, new TreeMap<>())
86 }
87 // Generate the ImagePlus
88 if (final_color == "Fluo") {
89 for(int i = 0; i < fluo_pattern_list.size(); i++){
90 // Use pattern for each color
91 if (fluo_pattern_list[i] != "") {
92 println "Processing " + unique_identifier + " " + fluo_pattern_list[i]
93 samplesMap.get(unique_identifier).put(
94 fluo_channels_list[i],
95 FolderOpener.open(
96 current_directory.getAbsolutePath(),
97 " filter=" + fluo_pattern_list[i]
98 )
99 )
100 if (!GraphicsEnvironment.isHeadless()){
101 samplesMap.get(unique_identifier).get(
102 fluo_channels_list[i]).show()
103 }
104 }
105 }
106 } else {
107 // It is easy as all images are used
108 println "Processing " + unique_identifier + " Greys"
109 samplesMap.get(unique_identifier).put(final_color, FolderOpener.open(current_directory.getAbsolutePath()))
110 if (!GraphicsEnvironment.isHeadless()){
111 samplesMap.get(unique_identifier).get(
112 final_color).show()
113 }
114 }
115 }
116
117 // Explore the HashMap and save to tiff
118 for(String unique_identifier : samplesMap.keySet()){
119 // get the channel map
120 Map<String, ImagePlus> channelsMap = samplesMap.get(unique_identifier)
121 ArrayList<String> channels = []
122 ArrayList<ImagePlus> current_images = []
123
124 for(String channel : channelsMap.keySet()){
125 channels.add(channel)
126 current_images.add(channelsMap.get(channel))
127 }
128 // Get number of time:
129 int nT = current_images[0].nSlices
130
131 // Merge all
132 ImagePlus merged_imps = Concatenator.run(current_images as ImagePlus[])
133 // Re-order to make a multi-channel, time-lapse image
134 ImagePlus final_imp
135 if (channels.size() == 1 && nT == 1) {
136 final_imp = merged_imps
137 } else {
138 final_imp = HyperStackConverter.toHyperStack(merged_imps, channels.size() , 1, nT, "xytcz", "Color")
139 }
140 // set LUTs
141 (0..channels.size()-1).each{
142 final_imp.setC(it + 1)
143 IJ.run(final_imp, channels[it], "")
144 final_imp.resetDisplayRange()
145 }
146 // Save to tiff
147 final_imp.setTitle(unique_identifier)
148
149 if (!GraphicsEnvironment.isHeadless()){
150 final_imp.show()
151 }
152
153 def fs = new FileSaver(final_imp)
154 File output_path = new File (output_dir ,final_imp.getTitle()+"_merge.tif" )
155 fs.saveAsTiff(output_path.toString() )
156
157 }
158 println "End of the script"
159
160 } catch (Throwable e) {
161 println("Something went wrong: " + e)
162 e.printStackTrace()
163 throw e
164
165 if (GraphicsEnvironment.isHeadless()){
166 // Force to give exit signal of error
167 System.exit(1)
168 }
169
170 }
171
172 return
173
174 /**
175 * ****************************************************************************************************************
176 * ******************************************* End of the script **************************************************
177 *
178 * ****************************************************************************************************************
179 *
180 * *********************************** Helpers and processing methods *********************************************
181 * ***************************************************************************************************************
182 */
183
184 import ij.IJ
185 import ij.ImagePlus
186 import ij.io.FileSaver
187 import ij.io.Opener
188 import ij.plugin.Concatenator
189 import ij.plugin.FolderOpener
190 import ij.plugin.HyperStackConverter
191 import ij.process.LUT
192
193 import java.awt.GraphicsEnvironment
194 import java.io.File