comparison color_to_gray.py @ 0:4395ea2a01f4 draft

"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools commit 35da2dcd86747c9bff138e100dbe08c6106f3780"
author bgruening
date Sat, 06 Feb 2021 10:00:10 +0000
parents
children e8f822eeb9fd
comparison
equal deleted inserted replaced
-1:000000000000 0:4395ea2a01f4
1 #!/usr/bin/env python
2
3 import argparse
4 import json
5
6 from cp_common_functions import get_json_value
7 from cp_common_functions import get_pipeline_lines
8 from cp_common_functions import get_total_number_of_modules
9 from cp_common_functions import INDENTATION
10 from cp_common_functions import update_module_count
11 from cp_common_functions import write_pipeline
12
13 MODULE_NAME = "ColorToGray"
14 OUTPUT_FILENAME = "output.cppipe"
15
16
17 def build_ctg_header(module_name, module_number):
18 """Creates the first line of a module given the name and module number"""
19 result = "|".join([f"{module_name}:[module_num:{module_number}",
20 "svn_version:\\'Unknown\\'",
21 "variable_revision_number:4",
22 "show_window:True",
23 "notes:\\x5B\\'Convert the color image to grayscale.\\'\\x5D",
24 "batch_state:array(\\x5B\\x5D, dtype=uint8)",
25 "enabled:True",
26 "wants_pause:False]\n"])
27 return result
28
29
30 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"""
32 result = INDENTATION.join([f"{INDENTATION}Select the input image:{get_json_value(input_params,'name_input_image')}\n",
33 f"Conversion method:{get_json_value(input_params,'con_conversion_method.conversion_method')}\n",
34 f"Image type:{get_json_value(input_params,'con_conversion_method.con_image_type.image_type')}\n",
35 ])
36
37 conversion_method = get_json_value(input_params, 'con_conversion_method.conversion_method')
38
39 image_type = get_json_value(input_params, 'con_conversion_method.con_image_type.image_type')
40 rgb_comb_name_out = "OrigGray"
41 combine_w_red = 1.0
42 combine_w_green = 1.0
43 combine_w_blue = 1.0
44
45 split_red = "Yes"
46 split_green = "Yes"
47 split_blue = "Yes"
48 red_output_name = "OrigRed"
49 green_output_name = "OrigGreen"
50 blue_output_name = "OrigBlue"
51
52 split_hue = "Yes"
53 split_saturation = "Yes"
54 split_value = "Yes"
55 hue_output_name = "OrigHue"
56 saturation_output_name = "OrigSaturation"
57 value_output_name = "OrigValue"
58
59 channel_count = 1
60 if conversion_method == "Combine":
61 if image_type == "RGB" or image_type == "HSV":
62 rgb_comb_name_out = get_json_value(input_params, 'con_conversion_method.name_output_image')
63 combine_w_red = get_json_value(input_params, 'con_conversion_method.con_image_type.weight_red_channel')
64 combine_w_green = get_json_value(input_params, 'con_conversion_method.con_image_type.weight_green_channel')
65 combine_w_blue = get_json_value(input_params, 'con_conversion_method.con_image_type.weight_blue_channel')
66 elif conversion_method == "Split":
67 if image_type == "RGB":
68 split_red = get_json_value(input_params, 'con_conversion_method.con_image_type.con_convert_red.yes_no')
69 red_output_name = get_json_value(input_params, 'con_conversion_method.con_image_type.con_convert_red.name_output_image')
70 split_green = get_json_value(input_params, 'con_conversion_method.con_image_type.con_convert_green.yes_no')
71 green_output_name = get_json_value(input_params, 'con_conversion_method.con_image_type.con_convert_green.name_output_image')
72 split_blue = get_json_value(input_params, 'con_conversion_method.con_image_type.con_convert_blue.yes_no')
73 blue_output_name = get_json_value(input_params, 'con_conversion_method.con_image_type.con_convert_blue.name_output_image')
74 elif image_type == "HSV":
75 split_hue = get_json_value(input_params, 'con_conversion_method.con_image_type.con_convert_hue.yes_no')
76 hue_output_name = get_json_value(input_params, 'con_conversion_method.con_image_type.con_convert_hue.name_output_image')
77 split_saturation = get_json_value(input_params, 'con_conversion_method.con_image_type.con_convert_saturation.yes_no')
78 saturation_output_name = get_json_value(input_params, 'con_conversion_method.con_image_type.con_convert_saturation.name_output_image')
79 split_value = get_json_value(input_params, 'con_conversion_method.con_image_type.con_convert_value.yes_no')
80 value_output_name = get_json_value(input_params, 'con_conversion_method.con_image_type.con_convert_value.name_output_image')
81
82 result += INDENTATION.join(
83 [f"{INDENTATION}Name the output image:{rgb_comb_name_out}\n",
84 f"Relative weight of the red channel:{str(combine_w_red)}\n",
85 f"Relative weight of the green channel:{str(combine_w_green)}\n",
86 f"Relative weight of the blue channel:{str(combine_w_blue)}\n",
87
88 f"Convert red to gray?:{split_red}\n",
89 f"Name the output image:{red_output_name}\n",
90 f"Convert green to gray?:{split_green}\n",
91 f"Name the output image:{green_output_name}\n",
92 f"Convert blue to gray?:{split_blue}\n",
93 f"Name the output image:{blue_output_name}\n",
94
95 f"Convert hue to gray?:{split_hue}\n",
96 f"Name the output image:{hue_output_name}\n",
97 f"Convert saturation to gray?:{split_saturation}\n",
98 f"Name the output image:{saturation_output_name}\n",
99 f"Convert value to gray?:{split_value}\n",
100 f"Name the output image:{value_output_name}\n"
101 ])
102
103 channel_count = 1
104 if image_type == "Channels":
105 channels = input_params['con_conversion_method']['con_image_type']['rpt_channel']
106 channel_count = len(channels)
107 result += INDENTATION.join(
108 [f"{INDENTATION}Channel count:{channel_count}\n"
109 ])
110
111 for ch in channels:
112 rel_weight_ch = 1.0
113 image_name = "Channel1"
114 if conversion_method == "Combine":
115 rel_weight_ch = get_json_value(ch, 'weight_of_channel')
116 else:
117 image_name = get_json_value(ch, 'image_name')
118 result += INDENTATION.join(
119 [f"{INDENTATION}Channel number:{get_json_value(ch,'channel_no')}\n",
120 f"Relative weight of the channel:{str(rel_weight_ch)}\n",
121 f"Image name:{image_name}\n"
122 ])
123 else:
124 result += INDENTATION.join(
125 [f"{INDENTATION}Channel count:{channel_count}\n",
126 "Channel number:Red\\x3A 1\n",
127 "Relative weight of the channel:1.0\n",
128 "Image name:Channel1\n"
129 ])
130
131 return result
132
133
134 if __name__ == "__main__":
135 parser = argparse.ArgumentParser()
136 parser.add_argument(
137 '-p', '--pipeline',
138 help='CellProfiler pipeline'
139 )
140 parser.add_argument(
141 '-i', '--inputs',
142 help='JSON inputs from Galaxy'
143 )
144 args = parser.parse_args()
145
146 pipeline_lines = get_pipeline_lines(args.pipeline)
147 inputs_galaxy = json.load(open(args.inputs, "r"))
148
149 current_module_num = get_total_number_of_modules(pipeline_lines)
150 current_module_num += 1
151 pipeline_lines = update_module_count(pipeline_lines, current_module_num)
152
153 header_block = build_ctg_header(MODULE_NAME, current_module_num)
154 main_block = build_main_block(inputs_galaxy)
155
156 module_pipeline = f"\n{header_block}{main_block}\n"
157 pipeline_lines.append(module_pipeline)
158
159 write_pipeline(OUTPUT_FILENAME, pipeline_lines)