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