Mercurial > repos > bgruening > cp_save_images
comparison starting_modules.py @ 3:a45d360ae9d9 draft
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools commit c3917e27eb1c1deeb381aa0dc8161c07699562fb"
author | bgruening |
---|---|
date | Mon, 11 May 2020 07:51:22 -0400 |
parents | |
children | 4ecf93628f97 |
comparison
equal
deleted
inserted
replaced
2:609911f19ab2 | 3:a45d360ae9d9 |
---|---|
1 import json | |
2 import sys | |
3 import os | |
4 | |
5 FOURSPACES = " " | |
6 | |
7 input_json_path = sys.argv[1] | |
8 | |
9 params = json.load(open(input_json_path, "r")) | |
10 | |
11 | |
12 def write_images(): | |
13 filter_images = params['images']['filter_images'] | |
14 | |
15 _str = "\nImages:[module_num:1|svn_version:\\'Unknown\\'|variable_revision_number:2|show_window:False|notes:\\x5B\\'To begin creating your project, use the Images module to compile a list of files and/or folders that you want to analyze. You can also specify a set of rules to include only the desired files in your selected folders.\\'\x5D|batch_state:array(\x5B\x5D, dtype=uint8)|enabled:True|wants_pause:False]\n" | |
16 _str += FOURSPACES+":\n" | |
17 _str += FOURSPACES + "Filter images?:%s\n" % filter_images | |
18 _str += FOURSPACES + "Select the rule criteria:and (extension does isimage) (directory doesnot startwith \".\")\n" | |
19 | |
20 return _str | |
21 | |
22 | |
23 def write_metadata(): | |
24 metadata_extraction = params['metadata']['con_metadata_extraction'] | |
25 extract = metadata_extraction['extract'] | |
26 | |
27 if 'extraction_method' in metadata_extraction: | |
28 method_count = len(metadata_extraction['extraction_method']) | |
29 else: | |
30 method_count = 1 | |
31 | |
32 _str = "\nMetadata:[module_num:2|svn_version:\\'Unknown\\'|variable_revision_number:4|show_window:False|notes:\\x5B\\'The Metadata module optionally allows you to extract information describing your images (i.e, metadata) which will be stored along with your measurements. This information can be contained in the file name and/or location, or in an external file.\\'\x5D|batch_state:array(\x5B\x5D, dtype=uint8)|enabled:True|wants_pause:False]\n" | |
33 _str += FOURSPACES + "Extract metadata?:%s\n" % extract | |
34 | |
35 if extract == "No": | |
36 _str += FOURSPACES + "Metadata data type:Text\n" | |
37 _str += FOURSPACES + "Metadata types:{}\n" | |
38 _str += FOURSPACES + "Extraction method count:%d\n" % method_count | |
39 _str += FOURSPACES + "Metadata extraction method:Extract from file/folder names\n" | |
40 _str += FOURSPACES + "Regular expression to extract from file name:^(?P<Plate>.*)_(?P<Well>\x5BA-P\x5D\x5B0-9\x5D{2})_s(?P<Site>\x5B0-9\x5D)_w(?P<ChannelNumber>\x5B0-9\x5D)\n" | |
41 _str += FOURSPACES + "Regular expression to extract from folder name:(?P<Date>\x5B0-9\x5D{4}_\x5B0-9\x5D{2}_\x5B0-9\x5D{2})$\n" | |
42 _str += FOURSPACES + "Extract metadata from:All images\n" | |
43 _str += FOURSPACES + "Select the filtering criteria:and (file does contain \"\")\n" | |
44 _str += FOURSPACES + "Metadata file location:\n" | |
45 _str += FOURSPACES + "Match file and image metadata:\x5B\x5D\n" | |
46 _str += FOURSPACES + "Use case insensitive matching?:No\n" | |
47 else: | |
48 _str += FOURSPACES + "Metadata data type:Text\n" #default Text,not possible to select in Galaxy | |
49 _str += FOURSPACES + "Metadata types:{}\n" | |
50 _str += FOURSPACES + "Extraction method count:%d\n" % method_count | |
51 | |
52 for methods in metadata_extraction["extraction_method"]: | |
53 _str += FOURSPACES + "Metadata extraction method:%s\n" % methods["metadata_extraction_method"] | |
54 _str += FOURSPACES + "Metadata source:%s\n" % methods["con_metadata_source"]["metadata_source"] | |
55 | |
56 if "file_name_regex" in methods["con_metadata_source"]: | |
57 file_regex = methods["con_metadata_source"]["file_name_regex"] | |
58 folder_regex = "(?P<folderField1>.*)" | |
59 elif "folder_name_regex" in methods["con_metadata_source"]: | |
60 file_regex = "(?P<field1>.*)_(?P<field2>[a-zA-Z0-9]+)" | |
61 folder_regex = methods["con_metadata_source"]["folder_name_regex"] | |
62 else: | |
63 # default regex | |
64 file_regex = "(?P<field1>.*)_(?P<field2>[a-zA-Z0-9]+)" | |
65 folder_regex = "(?P<folderField1>.*)" | |
66 | |
67 _str += FOURSPACES + "Regular expression to extract from file name:%s\n" % file_regex | |
68 _str += FOURSPACES + "Regular expression to extract from folder name:%s\n" % folder_regex | |
69 | |
70 _str += FOURSPACES + "Extract metadata from:%s\n" % methods["con_metadata_extract_from"]["extract_metadata_from"] | |
71 | |
72 if methods["con_metadata_extract_from"]["extract_metadata_from"] == "Images matching a rule": | |
73 rule_str ="" | |
74 for r in methods["con_metadata_extract_from"]["r_match"]: | |
75 if r["con_match"]["rule_type"] == "extension": | |
76 rule_str += " (" + r["con_match"]["rule_type"] + " " + r["con_match"]["operator"] + " " + \ | |
77 r["con_match"]["match_type"]+")" | |
78 else: | |
79 rule_str +=" (" + r["con_match"]["rule_type"] + " " + r["con_match"]["operator"] + " " +\ | |
80 r["con_match"]["contain"] + " \"" + r["con_match"]["match_value"] +"\")" | |
81 | |
82 | |
83 _str += FOURSPACES + "Select the filtering criteria:" + methods["con_metadata_extract_from"]["match_all_any"] + rule_str +"\n" | |
84 else: | |
85 _str += FOURSPACES + "Select the filtering criteria:and (file does contain \"\")\n" #this line is required even if it's not used | |
86 | |
87 _str += FOURSPACES + "Metadata file location:\n" | |
88 _str += FOURSPACES + "Match file and image metadata:\x5B\x5D\n" | |
89 _str += FOURSPACES + "Use case insensitive matching?:No\n" | |
90 | |
91 return _str | |
92 | |
93 | |
94 def write_nameandtypes(): | |
95 nameandtypes = params['nameandtypes'] | |
96 assign_a_name = nameandtypes['con_assign_a_name_to']['assign_a_name_to'] | |
97 | |
98 if "con_select_image_type" in nameandtypes['con_assign_a_name_to']: | |
99 con_set_intensity = nameandtypes['con_assign_a_name_to']['con_select_image_type']['con_set_intensity'] | |
100 max_intensity = con_set_intensity['maximum_intensity'] if "maximum_intensity" in con_set_intensity else 255.0 | |
101 else: | |
102 max_intensity = 255.0 | |
103 | |
104 pixel_space = nameandtypes['pixel_space'] | |
105 | |
106 rule_count = len(nameandtypes['con_assign_a_name_to']['r_match_rule']) if "r_match_rule" in nameandtypes['con_assign_a_name_to'] else 1 | |
107 | |
108 process_3d = nameandtypes['pixel_space']['process_3d'] | |
109 x_spacing = 1.0 if "x_spacing" not in pixel_space else pixel_space["x_spacing"] | |
110 y_spacing = 1.0 if "y_spacing" not in pixel_space else pixel_space["y_spacing"] | |
111 z_spacing = 1.0 if "z_spacing" not in pixel_space else pixel_space["z_spacing"] | |
112 | |
113 _str = "\nNamesAndTypes:[module_num:3|svn_version:\\'Unknown\\'|variable_revision_number:8|show_window:False|notes:\\x5B\\'The NamesAndTypes module allows you to assign a meaningful name to each image by which other modules will refer to it.\\'\\x5D|batch_state:array(\\x5B\\x5D, dtype=uint8)|enabled:True|wants_pause:False]\n" | |
114 | |
115 _str += FOURSPACES + "Assign a name to:%s\n" % assign_a_name | |
116 | |
117 if assign_a_name == "All images": | |
118 _str += FOURSPACES + "Select the image type:%s\n" % nameandtypes['con_assign_a_name_to']['con_select_image_type']['select_image_type'] | |
119 _str += FOURSPACES + "Name to assign these images:%s\n" % nameandtypes['con_assign_a_name_to']['name_to_assign'] | |
120 _str += FOURSPACES + "Match metadata:[]\n" | |
121 | |
122 _str += FOURSPACES + "Image set matching method:Order\n" | |
123 _str += FOURSPACES + "Set intensity range from:%s\n" % con_set_intensity['set_intensity_range_from'] | |
124 _str += FOURSPACES + "Assignments count:%s\n" % rule_count | |
125 _str += FOURSPACES + "Single images count:0\n" | |
126 _str += FOURSPACES + "Maximum intensity:%.1f\n" % max_intensity | |
127 _str += FOURSPACES + "Process as 3D?:%s\n" % process_3d | |
128 | |
129 else: | |
130 #the below lines are not relevant to "images matching rules", but needed in pipeline file | |
131 _str += FOURSPACES + "Select the image type:Grayscale image\n" | |
132 _str += FOURSPACES + "Name to assign these images:DNA\n" | |
133 _str += FOURSPACES + "Match metadata:[]\n" | |
134 | |
135 _str += FOURSPACES + "Image set matching method:%s\n" % nameandtypes['con_assign_a_name_to']['matching_method'] | |
136 _str += FOURSPACES + "Set intensity range from:Image metadata\n" | |
137 _str += FOURSPACES + "Assignments count:%d\n" % rule_count | |
138 _str += FOURSPACES + "Single images count:0\n" | |
139 _str += FOURSPACES + "Maximum intensity:%.1f\n" % max_intensity | |
140 _str += FOURSPACES + "Process as 3D?:%s\n" % process_3d | |
141 | |
142 _str += FOURSPACES + "Relative pixel spacing in X:%.1f\n" % x_spacing | |
143 _str += FOURSPACES + "Relative pixel spacing in Y:%.1f\n" % y_spacing | |
144 _str += FOURSPACES + "Relative pixel spacing in Z:%.1f\n" % z_spacing | |
145 | |
146 if assign_a_name == "Images matching rules": | |
147 for rule in nameandtypes["con_assign_a_name_to"]["r_match_rule"]: | |
148 | |
149 rule_str = "" | |
150 if len(rule["r_match"]) >0 : | |
151 for r in rule["r_match"]: | |
152 if r["con_match"]["rule_type"] == "file" or r["con_match"]["rule_type"] == "directory": | |
153 rule_str += " (" + r["con_match"]["rule_type"] + " "+r["con_match"]["operator"]+" "+\ | |
154 r["con_match"]["contain"]+" \"" + r["con_match"]["match_value"] +"\")" | |
155 else: | |
156 rule_str += " ("+ r["con_match"]["rule_type"] + " " + r["con_match"]["operator"] + " " + \ | |
157 r["con_match"]["match_type"] + ")" | |
158 else: | |
159 rule_str = " (file does contain \"\")" #need to have a value even if it is not used | |
160 | |
161 _str += FOURSPACES + "Select the rule criteria:" + rule["match_all_any"] + rule_str +"\n" | |
162 | |
163 img_or_obj = rule["con_select_image_type"]["select_image_type"] | |
164 | |
165 if img_or_obj == "Objects": | |
166 _str += FOURSPACES + "Name to assign these images:DNA\n" | |
167 _str += FOURSPACES + "Name to assign these objects:%s\n" % rule["con_select_image_type"]["name_to_assign"] | |
168 else: | |
169 _str += FOURSPACES + "Name to assign these images:%s\n" % rule["con_select_image_type"]["name_to_assign"] | |
170 _str += FOURSPACES + "Name to assign these objects:Cell\n" | |
171 | |
172 _str += FOURSPACES + "Select the image type:%s\n" % img_or_obj | |
173 | |
174 | |
175 intensity_range="Image metadata" #default value | |
176 if img_or_obj == "Grayscale image" or img_or_obj == "Color image": | |
177 intensity_range = rule["con_select_image_type"]["con_set_intensity"]["set_intensity_range_from"] | |
178 | |
179 _str += FOURSPACES + "Set intensity range from:%s\n" % intensity_range | |
180 | |
181 if intensity_range == "Manual": | |
182 _str += FOURSPACES + "Maximum intensity:%s\n" % rule["con_select_image_type"]["con_set_intensity"]["maximum_intensity"] | |
183 else: | |
184 _str += FOURSPACES + "Maximum intensity:255.0\n" | |
185 | |
186 | |
187 return _str | |
188 | |
189 | |
190 def write_groups(): | |
191 groups = params['groups'] | |
192 | |
193 _str = "\nGroups:[module_num:4|svn_version:\\'Unknown\\'|variable_revision_number:2|show_window:False|notes:\\x5B\\\'The Groups module optionally allows you to split your list of images into image subsets (groups) which will be processed independently of each other. Examples of groupings include screening batches, microtiter plates, time-lapse movies, etc.\\'\\x5D|batch_state:array(\\x5B\\x5D, dtype=uint8)|enabled:True|wants_pause:False]\n" | |
194 | |
195 group_images = groups["con_groups"]["group_images"] | |
196 | |
197 _str += FOURSPACES + "Do you want to group your images?:%s\n" % group_images | |
198 _str += FOURSPACES + "grouping metadata count:1\n" | |
199 | |
200 if group_images == "Yes": | |
201 _str += FOURSPACES + "Metadata category:%s\n" % groups["con_groups"]["group_category"] | |
202 else: | |
203 _str += FOURSPACES + "Metadata category:None\n" | |
204 | |
205 return _str | |
206 | |
207 | |
208 with open("output.cppipe", "w") as f: | |
209 headers = ["CellProfiler Pipeline: http://www.cellprofiler.org\n", | |
210 "Version:3\n", | |
211 "DateRevision:319\n", | |
212 "GitHash:\n", | |
213 "ModuleCount:4\n", | |
214 "HasImagePlaneDetails:False", | |
215 "\n"] | |
216 | |
217 f.writelines(headers) | |
218 | |
219 img_str = write_images() | |
220 metadata_str = write_metadata() | |
221 nameandtypes_str = write_nameandtypes() | |
222 groups_str = write_groups() | |
223 | |
224 output_str = img_str + metadata_str + nameandtypes_str + groups_str | |
225 | |
226 f.write(output_str) | |
227 f.close() |