comparison color_to_gray.py @ 6:6070780813d9 draft default tip

planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools commit 57a0433defa3cbc37ab34fbb0ebcfaeb680db8d5
author bgruening
date Sun, 05 Nov 2023 09:36:34 +0000
parents 670975e92458
children
comparison
equal deleted inserted replaced
5:670975e92458 6:6070780813d9
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 2
3 import argparse 3 import argparse
4 import json 4 import json
5 5
6 from cp_common_functions import get_json_value 6 from cp_common_functions import (get_json_value,
7 from cp_common_functions import get_pipeline_lines 7 get_pipeline_lines,
8 from cp_common_functions import get_total_number_of_modules 8 get_total_number_of_modules,
9 from cp_common_functions import INDENTATION 9 INDENTATION, update_module_count,
10 from cp_common_functions import update_module_count 10 write_pipeline)
11 from cp_common_functions import write_pipeline
12 11
13 MODULE_NAME = "ColorToGray" 12 MODULE_NAME = "ColorToGray"
14 OUTPUT_FILENAME = "output.cppipe" 13 OUTPUT_FILENAME = "output.cppipe"
15 14
16 15
17 def build_ctg_header(module_name, module_number): 16 def build_ctg_header(module_name, module_number):
18 """Creates the first line of a module given the name and module number""" 17 """Creates the first line of a module given the name and module number"""
19 result = "|".join([f"{module_name}:[module_num:{module_number}", 18 result = "|".join(
20 "svn_version:\\'Unknown\\'", 19 [
21 "variable_revision_number:4", 20 f"{module_name}:[module_num:{module_number}",
22 "show_window:True", 21 "svn_version:\\'Unknown\\'",
23 "notes:\\x5B\\'Convert the color image to grayscale.\\'\\x5D", 22 "variable_revision_number:4",
24 "batch_state:array(\\x5B\\x5D, dtype=uint8)", 23 "show_window:True",
25 "enabled:True", 24 "notes:\\x5B\\'Convert the color image to grayscale.\\'\\x5D",
26 "wants_pause:False]\n"]) 25 "batch_state:array(\\x5B\\x5D, dtype=uint8)",
26 "enabled:True",
27 "wants_pause:False]\n",
28 ]
29 )
27 return result 30 return result
28 31
29 32
30 def build_main_block(input_params): 33 def build_main_block(input_params):
31 """Creates the main block of the CP pipeline with the parameters that don't depend on conditional choices""" 34 """Creates the main block of the CP pipeline with the parameters that don't depend on conditional choices"""
32 result = INDENTATION.join([f"{INDENTATION}Select the input image:{get_json_value(input_params,'name_input_image')}\n", 35 result = INDENTATION.join(
33 f"Conversion method:{get_json_value(input_params,'con_conversion_method.conversion_method')}\n", 36 [
34 f"Image type:{get_json_value(input_params,'con_conversion_method.con_image_type.image_type')}\n", 37 f"{INDENTATION}Select the input image:{get_json_value(input_params,'name_input_image')}\n",
35 ]) 38 f"Conversion method:{get_json_value(input_params,'con_conversion_method.conversion_method')}\n",
36 39 f"Image type:{get_json_value(input_params,'con_conversion_method.con_image_type.image_type')}\n",
37 conversion_method = get_json_value(input_params, 'con_conversion_method.conversion_method') 40 ]
38 41 )
39 image_type = get_json_value(input_params, 'con_conversion_method.con_image_type.image_type') 42
43 conversion_method = get_json_value(
44 input_params, "con_conversion_method.conversion_method"
45 )
46
47 image_type = get_json_value(
48 input_params, "con_conversion_method.con_image_type.image_type"
49 )
40 rgb_comb_name_out = "OrigGray" 50 rgb_comb_name_out = "OrigGray"
41 combine_w_red = 1.0 51 combine_w_red = 1.0
42 combine_w_green = 1.0 52 combine_w_green = 1.0
43 combine_w_blue = 1.0 53 combine_w_blue = 1.0
44 54
57 value_output_name = "OrigValue" 67 value_output_name = "OrigValue"
58 68
59 channel_count = 1 69 channel_count = 1
60 if conversion_method == "Combine": 70 if conversion_method == "Combine":
61 if image_type == "RGB" or image_type == "HSV": 71 if image_type == "RGB" or image_type == "HSV":
62 rgb_comb_name_out = get_json_value(input_params, 'con_conversion_method.name_output_image') 72 rgb_comb_name_out = get_json_value(
63 combine_w_red = get_json_value(input_params, 'con_conversion_method.con_image_type.weight_red_channel') 73 input_params, "con_conversion_method.name_output_image"
64 combine_w_green = get_json_value(input_params, 'con_conversion_method.con_image_type.weight_green_channel') 74 )
65 combine_w_blue = get_json_value(input_params, 'con_conversion_method.con_image_type.weight_blue_channel') 75 combine_w_red = get_json_value(
76 input_params, "con_conversion_method.con_image_type.weight_red_channel"
77 )
78 combine_w_green = get_json_value(
79 input_params,
80 "con_conversion_method.con_image_type.weight_green_channel",
81 )
82 combine_w_blue = get_json_value(
83 input_params, "con_conversion_method.con_image_type.weight_blue_channel"
84 )
66 elif conversion_method == "Split": 85 elif conversion_method == "Split":
67 if image_type == "RGB": 86 if image_type == "RGB":
68 split_red = get_json_value(input_params, 'con_conversion_method.con_image_type.con_convert_red.yes_no') 87 split_red = get_json_value(
69 red_output_name = get_json_value(input_params, 'con_conversion_method.con_image_type.con_convert_red.name_output_image') 88 input_params,
70 split_green = get_json_value(input_params, 'con_conversion_method.con_image_type.con_convert_green.yes_no') 89 "con_conversion_method.con_image_type.con_convert_red.yes_no",
71 green_output_name = get_json_value(input_params, 'con_conversion_method.con_image_type.con_convert_green.name_output_image') 90 )
72 split_blue = get_json_value(input_params, 'con_conversion_method.con_image_type.con_convert_blue.yes_no') 91 red_output_name = get_json_value(
73 blue_output_name = get_json_value(input_params, 'con_conversion_method.con_image_type.con_convert_blue.name_output_image') 92 input_params,
93 "con_conversion_method.con_image_type.con_convert_red.name_output_image",
94 )
95 split_green = get_json_value(
96 input_params,
97 "con_conversion_method.con_image_type.con_convert_green.yes_no",
98 )
99 green_output_name = get_json_value(
100 input_params,
101 "con_conversion_method.con_image_type.con_convert_green.name_output_image",
102 )
103 split_blue = get_json_value(
104 input_params,
105 "con_conversion_method.con_image_type.con_convert_blue.yes_no",
106 )
107 blue_output_name = get_json_value(
108 input_params,
109 "con_conversion_method.con_image_type.con_convert_blue.name_output_image",
110 )
74 elif image_type == "HSV": 111 elif image_type == "HSV":
75 split_hue = get_json_value(input_params, 'con_conversion_method.con_image_type.con_convert_hue.yes_no') 112 split_hue = get_json_value(
76 hue_output_name = get_json_value(input_params, 'con_conversion_method.con_image_type.con_convert_hue.name_output_image') 113 input_params,
77 split_saturation = get_json_value(input_params, 'con_conversion_method.con_image_type.con_convert_saturation.yes_no') 114 "con_conversion_method.con_image_type.con_convert_hue.yes_no",
78 saturation_output_name = get_json_value(input_params, 'con_conversion_method.con_image_type.con_convert_saturation.name_output_image') 115 )
79 split_value = get_json_value(input_params, 'con_conversion_method.con_image_type.con_convert_value.yes_no') 116 hue_output_name = get_json_value(
80 value_output_name = get_json_value(input_params, 'con_conversion_method.con_image_type.con_convert_value.name_output_image') 117 input_params,
118 "con_conversion_method.con_image_type.con_convert_hue.name_output_image",
119 )
120 split_saturation = get_json_value(
121 input_params,
122 "con_conversion_method.con_image_type.con_convert_saturation.yes_no",
123 )
124 saturation_output_name = get_json_value(
125 input_params,
126 "con_conversion_method.con_image_type.con_convert_saturation.name_output_image",
127 )
128 split_value = get_json_value(
129 input_params,
130 "con_conversion_method.con_image_type.con_convert_value.yes_no",
131 )
132 value_output_name = get_json_value(
133 input_params,
134 "con_conversion_method.con_image_type.con_convert_value.name_output_image",
135 )
81 136
82 result += INDENTATION.join( 137 result += INDENTATION.join(
83 [f"{INDENTATION}Name the output image:{rgb_comb_name_out}\n", 138 [
84 f"Relative weight of the red channel:{str(combine_w_red)}\n", 139 f"{INDENTATION}Name the output image:{rgb_comb_name_out}\n",
85 f"Relative weight of the green channel:{str(combine_w_green)}\n", 140 f"Relative weight of the red channel:{str(combine_w_red)}\n",
86 f"Relative weight of the blue channel:{str(combine_w_blue)}\n", 141 f"Relative weight of the green channel:{str(combine_w_green)}\n",
87 142 f"Relative weight of the blue channel:{str(combine_w_blue)}\n",
88 f"Convert red to gray?:{split_red}\n", 143 f"Convert red to gray?:{split_red}\n",
89 f"Name the output image:{red_output_name}\n", 144 f"Name the output image:{red_output_name}\n",
90 f"Convert green to gray?:{split_green}\n", 145 f"Convert green to gray?:{split_green}\n",
91 f"Name the output image:{green_output_name}\n", 146 f"Name the output image:{green_output_name}\n",
92 f"Convert blue to gray?:{split_blue}\n", 147 f"Convert blue to gray?:{split_blue}\n",
93 f"Name the output image:{blue_output_name}\n", 148 f"Name the output image:{blue_output_name}\n",
94 149 f"Convert hue to gray?:{split_hue}\n",
95 f"Convert hue to gray?:{split_hue}\n", 150 f"Name the output image:{hue_output_name}\n",
96 f"Name the output image:{hue_output_name}\n", 151 f"Convert saturation to gray?:{split_saturation}\n",
97 f"Convert saturation to gray?:{split_saturation}\n", 152 f"Name the output image:{saturation_output_name}\n",
98 f"Name the output image:{saturation_output_name}\n", 153 f"Convert value to gray?:{split_value}\n",
99 f"Convert value to gray?:{split_value}\n", 154 f"Name the output image:{value_output_name}\n",
100 f"Name the output image:{value_output_name}\n" 155 ]
101 ]) 156 )
102 157
103 channel_count = 1 158 channel_count = 1
104 if image_type == "Channels": 159 if image_type == "Channels":
105 channels = input_params['con_conversion_method']['con_image_type']['rpt_channel'] 160 channels = input_params["con_conversion_method"]["con_image_type"][
161 "rpt_channel"
162 ]
106 channel_count = len(channels) 163 channel_count = len(channels)
107 result += INDENTATION.join( 164 result += INDENTATION.join([f"{INDENTATION}Channel count:{channel_count}\n"])
108 [f"{INDENTATION}Channel count:{channel_count}\n"
109 ])
110 165
111 for ch in channels: 166 for ch in channels:
112 rel_weight_ch = 1.0 167 rel_weight_ch = 1.0
113 image_name = "Channel1" 168 image_name = "Channel1"
114 if conversion_method == "Combine": 169 if conversion_method == "Combine":
115 rel_weight_ch = get_json_value(ch, 'weight_of_channel') 170 rel_weight_ch = get_json_value(ch, "weight_of_channel")
116 else: 171 else:
117 image_name = get_json_value(ch, 'image_name') 172 image_name = get_json_value(ch, "image_name")
118 result += INDENTATION.join( 173 result += INDENTATION.join(
119 [f"{INDENTATION}Channel number:{get_json_value(ch,'channel_no')}\n", 174 [
120 f"Relative weight of the channel:{str(rel_weight_ch)}\n", 175 f"{INDENTATION}Channel number:{get_json_value(ch,'channel_no')}\n",
121 f"Image name:{image_name}\n" 176 f"Relative weight of the channel:{str(rel_weight_ch)}\n",
122 ]) 177 f"Image name:{image_name}\n",
178 ]
179 )
123 else: 180 else:
124 result += INDENTATION.join( 181 result += INDENTATION.join(
125 [f"{INDENTATION}Channel count:{channel_count}\n", 182 [
126 "Channel number:Red\\x3A 1\n", 183 f"{INDENTATION}Channel count:{channel_count}\n",
127 "Relative weight of the channel:1.0\n", 184 "Channel number:Red\\x3A 1\n",
128 "Image name:Channel1\n" 185 "Relative weight of the channel:1.0\n",
129 ]) 186 "Image name:Channel1\n",
187 ]
188 )
130 result = result.rstrip("\n") 189 result = result.rstrip("\n")
131 return result 190 return result
132 191
133 192
134 if __name__ == "__main__": 193 if __name__ == "__main__":
135 parser = argparse.ArgumentParser() 194 parser = argparse.ArgumentParser()
136 parser.add_argument( 195 parser.add_argument("-p", "--pipeline", help="CellProfiler pipeline")
137 '-p', '--pipeline', 196 parser.add_argument("-i", "--inputs", help="JSON inputs from Galaxy")
138 help='CellProfiler pipeline'
139 )
140 parser.add_argument(
141 '-i', '--inputs',
142 help='JSON inputs from Galaxy'
143 )
144 args = parser.parse_args() 197 args = parser.parse_args()
145 198
146 pipeline_lines = get_pipeline_lines(args.pipeline) 199 pipeline_lines = get_pipeline_lines(args.pipeline)
147 inputs_galaxy = json.load(open(args.inputs, "r")) 200 inputs_galaxy = json.load(open(args.inputs, "r"))
148 201