comparison image_math.py @ 2:534ed7b8ca34 draft

"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools commit 1907942bef43b20edfdbd1d1b5eb1cac3602848b"
author bgruening
date Thu, 16 Apr 2020 05:44:16 -0400
parents
children 20dbb1f7ec7b
comparison
equal deleted inserted replaced
1:c2d573d02afc 2:534ed7b8ca34
1 #!/usr/bin/env python
2
3 import json
4 import sys
5 import os
6 import argparse
7 from cp_common_functions import *
8
9 MODULE_NAME = "ImageMath"
10 OUTPUT_FILENAME = "output.cppipe"
11
12 operator_map = {
13 "add": "Add",
14 "subtract": "Subtract",
15 "multiply": "Multiply",
16 "divide": "Divide",
17 "average": "Average",
18 "minimum": "Minimum",
19 "maximum": "Maximum",
20 "invert": "Invert",
21 "log_2": "Log transform (base 2)",
22 "log_legacy": "Log transform (legacy)",
23 "and": "And",
24 "or": "Or",
25 "not": "Not",
26 "equals": "Equals"
27 }
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 operation = operator_map[get_json_value(
33 input_params, 'operation.operation')]
34 result = INDENTATION.join([f"{INDENTATION}Operation:{operation}\n",
35 f"Raise the power of the result by:{get_json_value(input_params,'operation.op_results.raise_the_power_of_the_result_by')}\n",
36 f"Multiply the result by:{get_json_value(input_params,'operation.op_results.multiply_the_result_by')}\n",
37 f"Add to result:{get_json_value(input_params,'operation.op_results.add_to_result')}\n",
38 f"Set values less than 0 equal to 0?:{get_json_value(input_params,'operation.op_results.set_values_less_than_0_equal_to_0')}\n",
39 f"Set values greater than 1 equal to 1?:{get_json_value(input_params,'operation.op_results.set_values_greater_than_1_equal_to_1')}\n",
40 f"Ignore the image masks?:{get_json_value(input_params,'ignore_the_image_masks')}\n",
41 f"Name the output image:{get_json_value(input_params,'name_output_image')}"
42 ])
43 return result
44
45
46 def build_variable_block(inputs_galaxy):
47 result = ""
48 first_image_block = build_first_image_block(
49 get_json_value(inputs_galaxy, 'operation.first_image'))
50 result += f"\n{first_image_block}"
51 second_image_block = build_second_image_block(
52 get_json_value(inputs_galaxy, 'operation.second_image'))
53 result += f"\n{second_image_block}"
54 return result
55
56
57 def build_first_image_block(input_params):
58 """Creates the block of parameters for the first operator in operations"""
59
60 value_select = get_json_value(
61 input_params, 'image_or_measurement_first.image_or_measurement_first')
62 image_name = get_json_value(
63 input_params, 'image_or_measurement_first.select_the_first_image')
64 value_multiply = get_json_value(
65 input_params, 'multiply_the_first_image_by')
66 category = get_json_value(
67 input_params, 'image_or_measurement_first.category_first.category_first')
68 measurement = get_json_value(
69 input_params, 'image_or_measurement_first.category_first.measurement_first')
70
71 result = INDENTATION.join(
72 [f"{INDENTATION}Image or measurement?:{value_select}\n",
73 f"Select the first image:{image_name}\n",
74 f"Multiply the first image by:{value_multiply}\n",
75 f"Measurement:{concat_conditional(category, measurement)}"
76 ])
77 return result
78
79
80 def build_second_image_block(input_params):
81 """Creates the block of parameters for the second operator in binary operations"""
82
83 value_select = get_json_value(
84 input_params, 'image_or_measurement_second.image_or_measurement_second')
85 image_name = get_json_value(
86 input_params, 'image_or_measurement_second.select_the_second_image')
87 value_multiply = get_json_value(
88 input_params, 'multiply_the_second_image_by')
89 category = get_json_value(
90 input_params, 'image_or_measurement_second.category_second.category_second')
91 measurement = get_json_value(
92 input_params, 'image_or_measurement_second.category_second.measurement_second')
93
94 result = INDENTATION.join(
95 [f"{INDENTATION}Image or measurement?:{value_select}\n",
96 f"Select the second image:{image_name}\n",
97 f"Multiply the second image by:{value_multiply}\n",
98 f"Measurement:{concat_conditional(category, measurement)}"
99 ])
100 return result
101
102
103 if __name__ == "__main__":
104 parser = argparse.ArgumentParser()
105 parser.add_argument(
106 '-p', '--pipeline',
107 help='CellProfiler pipeline'
108 )
109 parser.add_argument(
110 '-i', '--inputs',
111 help='JSON inputs from Galaxy'
112 )
113 args = parser.parse_args()
114
115 pipeline_lines = get_pipeline_lines(args.pipeline)
116 inputs_galaxy = json.load(open(args.inputs, "r"))
117
118 current_module_num = get_total_number_of_modules(pipeline_lines)
119 current_module_num += 1
120 pipeline_lines = update_module_count(pipeline_lines, current_module_num)
121
122 header_block = build_header(MODULE_NAME, current_module_num)
123 main_block = build_main_block(inputs_galaxy)
124 variable_block = build_variable_block(inputs_galaxy)
125
126 module_pipeline = f"\n{header_block}{main_block}{variable_block}\n"
127 pipeline_lines.append(module_pipeline)
128
129 write_pipeline(OUTPUT_FILENAME, pipeline_lines)