-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinspect_modelarray_h5.sh
More file actions
137 lines (111 loc) · 4.16 KB
/
inspect_modelarray_h5.sh
File metadata and controls
137 lines (111 loc) · 4.16 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
#!/usr/bin/env bash
set -euo pipefail
usage() {
cat <<'EOF'
Usage: inspect_modelarray_h5.sh H5_FILE [SCALAR_NAME] [ANALYSIS_NAME] [COHORT_CSV] [CONTAINER]
Quickly inspect a ModelArray HDF5 using the convenience functions highlighted in
the upstream exploring-h5 vignette.
Arguments:
H5_FILE Path to the ModelArray HDF5 file.
SCALAR_NAME Optional scalar to load explicitly.
ANALYSIS_NAME Optional saved analysis to load explicitly.
COHORT_CSV Optional phenotype CSV for exampleElementData().
CONTAINER Optional Singularity image to use when host R lacks ModelArray.
EOF
exit 1
}
[[ $# -ge 1 ]] || usage
H5_FILE="$1"
SCALAR_NAME="${2:-}"
ANALYSIS_NAME="${3:-}"
COHORT_CSV="${4:-}"
CONTAINER="${5:-/data/local/container/modelarray/modelarray_confixel_0.1.5.sif}"
[[ -f "$H5_FILE" ]] || { echo "ERROR: H5 file not found: $H5_FILE" >&2; exit 1; }
if [[ -n "$COHORT_CSV" ]]; then
[[ -f "$COHORT_CSV" ]] || { echo "ERROR: Cohort CSV not found: $COHORT_CSV" >&2; exit 1; }
fi
run_rscript() {
if command -v Rscript >/dev/null 2>&1 && Rscript --vanilla -e 'library(ModelArray)' >/dev/null 2>&1; then
Rscript --vanilla - "$H5_FILE" "$SCALAR_NAME" "$ANALYSIS_NAME" "$COHORT_CSV"
return
fi
command -v singularity >/dev/null 2>&1 || { echo "ERROR: Neither host R+ModelArray nor singularity is available." >&2; exit 1; }
[[ -f "$CONTAINER" ]] || { echo "ERROR: Container not found: $CONTAINER" >&2; exit 1; }
bind_args=( -B "$(dirname "$H5_FILE")":"$(dirname "$H5_FILE")" )
if [[ -n "$COHORT_CSV" ]]; then
bind_args+=( -B "$(dirname "$COHORT_CSV")":"$(dirname "$COHORT_CSV")" )
fi
singularity run --cleanenv "${bind_args[@]}" "$CONTAINER" Rscript - "$H5_FILE" "$SCALAR_NAME" "$ANALYSIS_NAME" "$COHORT_CSV"
}
run_rscript <<'RSCRIPT'
args <- commandArgs(trailingOnly = TRUE)
h5_path <- args[[1]]
scalar_name <- args[[2]]
analysis_name <- args[[3]]
cohort_csv <- args[[4]]
library(ModelArray)
if (exists("h5summary", where = asNamespace("ModelArray"), mode = "function")) {
summary_obj <- h5summary(h5_path)
print(summary_obj)
} else {
cat("h5summary() not available in this ModelArray version; using show() after load instead.\n")
}
scalar_types <- if (nzchar(scalar_name)) scalar_name else NULL
analysis_names <- if (nzchar(analysis_name)) analysis_name else NULL
modelarray <- ModelArray(h5_path, scalar_types = scalar_types, analysis_names = analysis_names)
show(modelarray)
get_scalar_names <- function(x) {
if (exists("scalarNames", where = asNamespace("ModelArray"), mode = "function")) {
scalarNames(x)
} else {
names(scalars(x))
}
}
get_analysis_names <- function(x) {
if (exists("analysisNames", where = asNamespace("ModelArray"), mode = "function")) {
analysisNames(x)
} else {
names(results(x))
}
}
count_elements <- function(x, scalar) {
if (exists("nElements", where = asNamespace("ModelArray"), mode = "function")) {
nElements(x, scalar)
} else {
numElementsTotal(x, scalar)
}
}
count_inputs <- function(x, scalar) {
if (exists("nInputFiles", where = asNamespace("ModelArray"), mode = "function")) {
nInputFiles(x, scalar)
} else {
ncol(scalars(x)[[scalar]])
}
}
scalar_names <- get_scalar_names(modelarray)
analysis_names_loaded <- get_analysis_names(modelarray)
cat("\nScalar names:\n")
print(scalar_names)
cat("\nAnalysis names:\n")
print(analysis_names_loaded)
if (length(scalar_names) > 0) {
scalar <- scalar_names[[1]]
cat("\nSource files for", scalar, ":\n")
print(head(sources(modelarray)[[scalar]]))
cat("\nDimensions for", scalar, ":\n")
print(dim(scalars(modelarray)[[scalar]]))
cat("\nNumber of elements:", count_elements(modelarray, scalar), "\n")
cat("Number of inputs:", count_inputs(modelarray, scalar), "\n")
}
if (length(analysis_names_loaded) > 0) {
analysis <- analysis_names_loaded[[1]]
cat("\nResult columns for", analysis, ":\n")
print(colnames(results(modelarray)[[analysis]]))
}
if (nzchar(cohort_csv) && length(scalar_names) > 0) {
phenotypes <- read.csv(cohort_csv)
scalar <- scalar_names[[1]]
cat("\nexampleElementData() preview for element 1:\n")
print(head(exampleElementData(modelarray, scalar = scalar, i_element = 1, phenotypes = phenotypes)))
}
RSCRIPT