Skip to content
Navigation Menu
{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_marker_function.py
More file actions
366 lines (310 loc) · 11.4 KB
/
Copy pathrun_marker_function.py
File metadata and controls
366 lines (310 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
"""Flexible wrapper to call any markeR R function from Python.
This script allows you to call markeR functions directly without writing Python code.
Usage examples:
python run_marker_function.py PlotScores --help-function
python run_marker_function.py CalculateScores \\
--data counts_example --metadata metadata_example \\
--gene_sets genesets_example --method logmedian --verbose
python run_marker_function.py PlotScores \\
--data counts_example --metadata metadata_example \\
--gene_sets genesets_example --Variable "Condition" \\
--method logmedian --nrow 1 --output plot.png
Options:
--help-function Show R documentation for the function
--verbose Print the generated R code before executing
--output FILE Save plot to PNG file
For built-in example data, use the names: counts_example, metadata_example, genesets_example
"""
import sys
import argparse
import json
import os
import re
# Check dependencies
_missing = []
try:
import rpy2.robjects as ro
from rpy2.robjects.packages import importr, isinstalled
except ImportError:
_missing.append("rpy2")
if _missing:
sys.exit(
"The following Python packages are required but not installed: %s.\n"
"Please install them (e.g. `pip install rpy2`)." % ", ".join(_missing)
)
def ensure_bioc_installed() -> None:
"""Install Bioconductor's package manager if it is not already present."""
biocinstaller = "BiocManager"
if not isinstalled(biocinstaller):
ro.r('install.packages("{0}")'.format(biocinstaller))
ro.r('suppressMessages(require({0}))'.format(biocinstaller))
def install_markeR() -> None:
"""Install the markeR package from Bioconductor if not already installed."""
ensure_bioc_installed()
if not isinstalled("markeR"):
ro.r('BiocManager::install("markeR", ask=FALSE, update=FALSE)')
ro.r('library(markeR)')
def load_example_data():
"""Load built-in markeR example datasets into R namespace."""
install_markeR()
# Load the example datasets
ro.r('data("genesets_example", package="markeR")')
ro.r('data("counts_example", package="markeR")')
ro.r('data("metadata_example", package="markeR")')
print("Loaded markeR example datasets: counts_example, metadata_example, genesets_example")
def parse_parameter(value: str):
"""
Parse a parameter value intelligently.
- Numbers become numeric
- "true"/"false" become logical
- "null" becomes NULL
- R object names (e.g., counts_example) are kept as-is
- JSON objects/arrays become R equivalents
- Strings are kept as strings
"""
value_lower = value.lower()
# Handle boolean
if value_lower == "true":
return "TRUE"
if value_lower == "false":
return "FALSE"
if value_lower == "null":
return "NULL"
# Handle numbers
try:
if "." in value:
float(value)
return value
else:
int(value)
return value
except ValueError:
pass
# Check if it's a known R object name (example data)
known_objects = ["counts_example", "metadata_example", "genesets_example"]
if value in known_objects:
return value
# Try JSON parsing for objects/arrays
try:
json.loads(value)
# If it parses as JSON, return as-is (user can provide lists as JSON)
return value
except (json.JSONDecodeError, ValueError):
pass
# Default: treat as string, escaping any internal quotes
value = value.replace('"', '\\"')
return f'"{value}"'
def build_r_call(function_name: str, params: dict, output_file: str = None, width: int = 800, height: int = 600) -> str:
"""
Build an R function call string from parameters.
Parameters
----------
function_name : str
Name of the R function to call
params : dict
Dictionary of parameter names and values
output_file : str
If provided, set up PNG device before the call and close after
width : int
PNG width in pixels (default: 800)
height : int
PNG height in pixels (default: 600)
Returns
-------
str
Complete R code to execute
"""
# Remove output_file from params if present
params = {k: v for k, v in params.items() if k != "output_file"}
# Build parameter list
param_strings = []
for key, value in params.items():
parsed_value = parse_parameter(value)
param_strings.append(f"{key} = {parsed_value}")
param_str = ", ".join(param_strings)
# Build R code
r_code = ""
if output_file:
output_file = os.path.abspath(output_file)
dirname = os.path.dirname(output_file)
if dirname and not os.path.isdir(dirname):
os.makedirs(dirname, exist_ok=True)
r_code += f'png("{output_file}", width={width}, height={height})\n'
r_code += f"result <- {function_name}({param_str})\n"
r_code += "tryCatch(print(result), error=function(e) { invisible(NULL) })\n"
r_code += "dev.off()\n"
else:
# For screen output, try to print the result
r_code += f"result <- {function_name}({param_str})\n"
r_code += "tryCatch(print(result), error=function(e) { cat('Function executed.\\n') })\n"
return r_code
def main():
parser = argparse.ArgumentParser(
description="Call any markeR R function from Python",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
python run_marker_function.py CalculateScores \\
--data counts_example --metadata metadata_example \\
--gene_sets genesets_example --method logmedian
python run_marker_function.py PlotScores \\
--data counts_example --metadata metadata_example \\
--gene_sets genesets_example --Variable "Condition" \\
--method logmedian --output my_plot.png
Built-in example data: counts_example, metadata_example, genesets_example
"""
)
parser.add_argument(
"function_name",
help="Name of the markeR function to call (e.g., CalculateScores, PlotScores)"
)
parser.add_argument(
"--help-function",
action="store_true",
help="Show help for the R function (instead of calling it)"
)
parser.add_argument(
"--verbose",
action="store_true",
help="Print the generated R code before executing"
)
parser.add_argument(
"--output",
help="Save plot output to a PNG file"
)
parser.add_argument(
"--width",
type=int,
default=800,
help="PNG width in pixels (default: 800)"
)
parser.add_argument(
"--height",
type=int,
default=600,
help="PNG height in pixels (default: 600)"
)
# Allow arbitrary parameters
parser.add_argument(
"params",
nargs="*",
help="Parameters as --name value pairs (e.g., --data counts_example --method logmedian)"
)
# Handle --help for specific functions
if len(sys.argv) > 1 and sys.argv[1] not in ["--help", "-h"]:
if "--help-function" in sys.argv:
func_name = sys.argv[1]
print(f"\n{'='*70}")
print(f"Help for markeR::{func_name}")
print(f"{'='*70}\n")
install_markeR()
# Try to display help
try:
# Get function signature and description
ro.r(f'''
library(markeR)
cat("Function: {func_name}\\n\\n")
# Try to get help
tryCatch({{
help_file <- help("{func_name}", package="markeR")
# Get description from help
}}, error = function(e) {{
cat("Help available at: https://diseasetranscriptomicslab.github.io/markeR/reference/{func_name}.html\\n")
}})
''')
except Exception as e:
pass
print(f"\nDocumentation:")
print(f" https://diseasetranscriptomicslab.github.io/markeR/reference/{func_name}.html")
print(f"\nTo use this function:")
print(f" python run_marker_function.py {func_name} --param1 value1 --param2 value2 [--output output.png]")
print(f"\nTip: Use --verbose flag to see generated R code")
print(f" python run_marker_function.py {func_name} --verbose --param1 value1 ...\n")
print(f"{'='*70}\n")
return
# Parse args
if len(sys.argv) < 2:
parser.print_help()
return
func_name = sys.argv[1]
# Validate function name to prevent code injection
if not re.match(r'^[A-Za-z][A-Za-z0-9_.]*$', func_name):
sys.exit(f"Error: invalid function name '{func_name}'. "
"Function names must start with a letter and contain only letters, digits, dots or underscores.")
output_file = None
width = 800
height = 600
# Parse remaining arguments as key-value pairs
params = {}
i = 2
while i < len(sys.argv):
arg = sys.argv[i]
if arg == "--output" and i + 1 < len(sys.argv):
output_file = sys.argv[i + 1]
i += 2
elif arg == "--width" and i + 1 < len(sys.argv):
try:
width = int(sys.argv[i + 1])
except ValueError:
print(f"Error: --width must be a number, got '{sys.argv[i + 1]}'")
sys.exit(1)
i += 2
elif arg == "--height" and i + 1 < len(sys.argv):
try:
height = int(sys.argv[i + 1])
except ValueError:
print(f"Error: --height must be a number, got '{sys.argv[i + 1]}'")
sys.exit(1)
i += 2
elif arg in ["--verbose", "--help-function"]:
# Skip flags that are not parameters
i += 1
elif arg.startswith("--"):
key = arg[2:] # Remove --
if i + 1 < len(sys.argv) and not sys.argv[i + 1].startswith("--"):
value = sys.argv[i + 1]
params[key] = value
i += 2
else:
# Boolean flag
params[key] = "TRUE"
i += 1
else:
i += 1
# Ensure markeR is installed
print("Installing markeR if needed...")
install_markeR()
load_example_data()
# Build and execute the R call
print(f"\nCalling {func_name} with parameters:")
for key, value in params.items():
print(f" {key} = {value}")
if output_file:
print(f" Saving plot to: {output_file}")
print(f" PNG dimensions: {width}x{height} pixels")
r_code = build_r_call(func_name, params, output_file, width, height)
# Show R code if verbose mode
if "--verbose" in sys.argv:
print(f"\n{'='*70}")
print("Generated R code:")
print(f"{'='*70}")
print(r_code)
print(f"{'='*70}\n")
print(f"Executing R code...\n")
print("=" * 60)
try:
ro.r(r_code)
print("=" * 60)
if output_file:
print(f"\n✓ Plot saved to: {output_file}")
else:
print(f"\n✓ Function executed successfully")
except Exception as e:
print("=" * 60)
print(f"\n✗ Error executing function: {e}")
if "--verbose" not in sys.argv:
print("\nTip: Use --verbose flag to see the generated R code")
print(f" python run_marker_function.py {func_name} --verbose [other options]")
sys.exit(1)
if __name__ == "__main__":
main()
You can’t perform that action at this time.
