| 1 |
#!/bin/bash |
| 2 |
VERSION=22-282 |
| 3 |
|
| 4 |
# Copyright 2022 Stijn van Dongen |
| 5 |
|
| 6 |
# This program is free software; you can redistribute it and/or modify it |
| 7 |
# under the terms of version 3 of the GNU General Public License as published |
| 8 |
# by the Free Software Foundation. It should be shipped with MCL in the top |
| 9 |
# level directory as the file COPYING. |
| 10 |
|
| 11 |
# _ _ __|_ _. __|_ _ _| _ _ _ _|_. _ _ _ _ |. _ | _ _ _ _ _ |
| 12 |
# | (/__\ | | |(_ | (/_(_| (_(_)| | | |(_|(/_| |(_\/ ||| ||<(_|(_|(/_ (_|(_ |
| 13 |
# | / | |/ |
| 14 |
# RCL consensus clustering QC |
| 15 |
# Author: Stijn van Dongen |
| 16 |
# |
| 17 |
# See rcl, github.com/micans/mcl#rcl and this script with no arguments. |
| 18 |
|
| 19 |
set -euo pipefail |
| 20 |
|
| 21 |
themode= # first argument, mode 'qc' 'qc2' 'heatannot' 'quickmark' |
| 22 |
projectdir= # second argument, for all modes. |
| 23 |
infix='vnl' # -x infix, a secondary tag |
| 24 |
cpu=1 # -p NUM |
| 25 |
ANNOTATION= # -a FNAME annotation file |
| 26 |
CLUSTERING= # -c FNAME clustering file (mcl format) |
| 27 |
HIERARCHY= # -h FNAME hierarchy file (rcl.hm*) |
| 28 |
COUNTMATRIX= # -d FNAME data file |
| 29 |
GENENAMES= # -g FNAME row names for COUNTMATRIX |
| 30 |
do_force=false # -F (for mode 'tree') |
| 31 |
R_invoke="R --no-echo --quiet --silent --vanilla" |
| 32 |
# -R (echo R code rather than execute it) |
| 33 |
|
| 34 |
function usage() { |
| 35 |
local e=$1 |
| 36 |
cat <<EOH |
| 37 |
This program provides qc/reporting modes additional to rcl, where TAG is an rcl project |
| 38 |
directory. It has some in-line R code to create plots (not pretty, but there are reasons, e.g. file |
| 39 |
transformations are needed before the R code can be invoked). Should you find it useful and wish to |
| 40 |
change the R code, you can save R scripts by using the -E option and make changes in the |
| 41 |
saved copy. Several options can be changed from the command line: |
| 42 |
|
| 43 |
RCLPLOT_YFTSIZE=6 RCLHM_XTITLE="My Title" rcl-qc heatannot TAG -a file1 -c file2 |
| 44 |
|
| 45 |
Options that can not be set this way currently are colour schemes, some dimensions as well as many |
| 46 |
other things. To find out what options can be set I'm afraid the best solution is to look inside |
| 47 |
the script. Suggestions/patches / wholesale better ideas are welcome. |
| 48 |
|
| 49 |
rcl-qc qc TAG create (1) heat map of clustering discrepancies and (2) granularity plot. |
| 50 |
|
| 51 |
rcl-qc qc2 TAG create scatter plot of cluster sizes versus induced mean eccentricity of nodes. |
| 52 |
This is compute intensive and benefits from multiple CPUs (-p option). |
| 53 |
|
| 54 |
rcl-qc heatannot TAG -a annotationfile -h rclhierarchyfile (output from rcl select, TAG/rcl.hm*) |
| 55 |
rcl-qc heatannot TAG -a annotationfile -c clustering (in mcl matrix format) |
| 56 |
|
| 57 |
This mode creates a heatmap from an annotation file, where each |
| 58 |
node is scored for the same list of traits. Scores are added for each trait and |
| 59 |
each cluster by adding all scores for that trait for all nodes in the cluster. |
| 60 |
|
| 61 |
Annotation file should be rectangular tab-separated, with a header line |
| 62 |
for the trait/class names and with row names the same as the labels in TAG/rcl.tab. |
| 63 |
Thus row names represent nodes/cells/barcodes, and A[i,j] is the score for |
| 64 |
class j for node i. |
| 65 |
|
| 66 |
Additional options: |
| 67 |
-x infix adds infix to the output files (use to distinguish versions/parameters) |
| 68 |
|
| 69 |
rcl-qc quickmark TAG -d countmatrixfile -g generownamesfile -h rclhierarchyfile |
| 70 |
rcl-qc quickmark TAG -d countmatrixfile -g generownamesfile -c clustering |
| 71 |
|
| 72 |
Additional options: |
| 73 |
-x infix adds infix to the output files (use to distinguish versions/parameters) |
| 74 |
|
| 75 |
Additional options: |
| 76 |
-F force re-run (various modes will indicate if this can be used) |
| 77 |
-E output R code rather than execute it (various modes) |
| 78 |
|
| 79 |
R libraries required: |
| 80 |
qc and qc2: ggplot2 viridis |
| 81 |
heatannot: circlize ComplexHeatmap DECIPHER |
| 82 |
|
| 83 |
MCL sibling programs required: |
| 84 |
mcx mcxdump rcldo.pl clxdo |
| 85 |
EOH |
| 86 |
exit $e |
| 87 |
} |
| 88 |
|
| 89 |
MODES="qc qc2 heatannot quickmark version" |
| 90 |
|
| 91 |
function require_mode() { |
| 92 |
local mode=$1 |
| 93 |
[[ -z $mode ]] && echo "Please provide a mode" && usage 0 |
| 94 |
if ! grep -qFw -- $mode <<< "$MODES"; then |
| 95 |
echo "Need a mode, one of { $MODES }"; usage 1 |
| 96 |
fi |
| 97 |
themode=$mode |
| 98 |
} |
| 99 |
function require_tag() { |
| 100 |
local mode=$1 tag=$2 |
| 101 |
if [[ -z $tag ]]; then |
| 102 |
echo "Mode $mode requires a project directory tag" |
| 103 |
false |
| 104 |
elif [[ $tag =~ ^- ]]; then |
| 105 |
echo "Project directory not allowed to start with a hyphen" |
| 106 |
false |
| 107 |
fi |
| 108 |
projectdir=$tag |
| 109 |
} |
| 110 |
function require_file() { |
| 111 |
local fname=$1 response=$2 |
| 112 |
if [[ ! -f $fname ]]; then |
| 113 |
echo "Expected file $fname not found; $response" |
| 114 |
false |
| 115 |
fi |
| 116 |
} |
| 117 |
function require_imx() { |
| 118 |
local fname=$1 response=$2 |
| 119 |
require_file "$fname" "$response" |
| 120 |
if ! mcx query -imx $fname --dim > /dev/null; then |
| 121 |
echo "Input is not in mcl network format; check file $fname" |
| 122 |
false |
| 123 |
fi |
| 124 |
} |
| 125 |
function test_absence () { |
| 126 |
local fname=$1 |
| 127 |
local action=${2:?Programmer error, need second argument} # return or exit |
| 128 |
if [[ -f $fname ]]; then |
| 129 |
if $do_force; then |
| 130 |
echo "Recreating existing file $fname" |
| 131 |
else |
| 132 |
echo "File $fname exists, use -F to force renewal" |
| 133 |
if [[ $action == 'return' ]]; then |
| 134 |
return 1 |
| 135 |
elif [[ $action == 'exit' ]]; then |
| 136 |
exit 1 |
| 137 |
fi |
| 138 |
fi |
| 139 |
fi |
| 140 |
} |
| 141 |
|
| 142 |
require_mode "${1-}" # themode now set |
| 143 |
shift 1 |
| 144 |
if grep -qFw $themode <<< "qc qc2 heatannot quickmark"; then |
| 145 |
require_tag $themode "${1-}" # projectdir now set |
| 146 |
shift 1 |
| 147 |
fi |
| 148 |
|
| 149 |
if [[ $themode == 'version' ]]; then |
| 150 |
echo "rcl-qc version: $VERSION" |
| 151 |
exit 0 |
| 152 |
fi |
| 153 |
|
| 154 |
pfx=$projectdir/rcl |
| 155 |
require_file "$pfx.lsocls" "cluster file $pfx.lsocls is missing" |
| 156 |
require_file "$pfx.nitems" "count file $pfx.nitems is missing" |
| 157 |
require_file "$pfx.tab" "tab file $pfx.nitems is missing" |
| 158 |
|
| 159 |
echo -- "$themode $projectdir $@" >> $pfx.qcline |
| 160 |
|
| 161 |
|
| 162 |
while getopts :a:c:d:g:h:p:x:FR opt |
| 163 |
do |
| 164 |
case "$opt" in |
| 165 |
a) ANNOTATION=$OPTARG ;; |
| 166 |
c) CLUSTERING=$OPTARG ;; |
| 167 |
d) COUNTMATRIX=$OPTARG ;; |
| 168 |
g) GENENAMES=$OPTARG ;; |
| 169 |
h) HIERARCHY=$OPTARG ;; |
| 170 |
p) cpu=$OPTARG ;; |
| 171 |
x) infix=$OPTARG ;; |
| 172 |
R) R_invoke=cat ;; |
| 173 |
F) do_force=true ;; |
| 174 |
:) echo "Flag $OPTARG needs argument"; exit 1 ;; |
| 175 |
?) echo "Flag $OPTARG unknown"; exit 1 ;; |
| 176 |
esac |
| 177 |
done |
| 178 |
|
| 179 |
shift $((OPTIND-1)) |
| 180 |
|
| 181 |
function require_opt() { |
| 182 |
n_req=0 |
| 183 |
local mode=$1 option=$2 value=$3 description=$4 |
| 184 |
if [[ -z $value ]]; then |
| 185 |
echo "Mode $mode requires $description for option $option" |
| 186 |
(( ++n_req )) |
| 187 |
fi |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
## |
| 192 |
## Q C |
| 193 |
|
| 194 |
if [[ $themode == 'qc' ]]; then |
| 195 |
|
| 196 |
# ____ ___) ______ ______ # |
| 197 |
# (, / / (, / ) (, / ) # |
| 198 |
# /---/ _ __ _ /---( _ / / __ _ _ _____ _ # |
| 199 |
# ) / (___(/_/ (__(/_ ) / ____)_(/_ _/___ /_/ (_(_(_(_/_(_) / (_/_)_ # |
| 200 |
# (_/ (_/ ( (_/___ / .-/ # |
| 201 |
# (_/ # |
| 202 |
# Some things still hardcoded, e.g. cluster size range. |
| 203 |
# This looks absolutely awful, with perl madness and inline R scripts. |
| 204 |
# Remember that things like daisies and clover exist. |
| 205 |
# Most importantly though It Should Work |
| 206 |
|
| 207 |
require_file "$pfx.lsocls" "(created in step 'setup or mcl')" |
| 208 |
require_file "$pfx.nitems" "(created in step 'setup')" |
| 209 |
|
| 210 |
export RCLPLOT_PARAM_SCALE=${RCLPLOT_PARAM_SCALE:-2} |
| 211 |
mapfile -t cls < $pfx.lsocls |
| 212 |
|
| 213 |
# Heatmap: |
| 214 |
out_heat_txt=$pfx.qc-heat.txt |
| 215 |
out_heat_pdf=${out_heat_txt%.txt}.pdf |
| 216 |
( echo -e "d\tP1\tP2"; clm dist "${cls[@]}" | rcldo.pl distwrangle $(cat $pfx.nitems) ) > $out_heat_txt # \ |
| 217 |
|
| 218 |
$R_invoke <<EOR |
| 219 |
library(ggplot2, warn.conflicts=FALSE) |
| 220 |
library(viridis, warn.conflicts=FALSE) |
| 221 |
|
| 222 |
mytheme = theme(plot.title = element_text(hjust = 0.5), plot.margin=grid::unit(c(5,5,5,5), "mm"), |
| 223 |
axis.text.x=element_text(size=rel(1.0), angle=0), axis.text.y=element_text(size=rel(1.0), angle=0), |
| 224 |
legend.position="bottom", legend.text=element_text(size=rel(0.6)), legend.title=element_text(size=rel(0.8)), |
| 225 |
text=element_text(family="serif")) |
| 226 |
|
| 227 |
h <- read.table("$out_heat_txt",header=T, colClasses=c("double", "character", "character")) |
| 228 |
h\$d <- 100 * h\$d |
| 229 |
|
| 230 |
p1 <- ggplot(h, aes(x=P1, y=P2, fill=d)) + |
| 231 |
geom_tile() + mytheme + ggtitle("Cluster discrepancies") + |
| 232 |
guides(shape = guide_legend(override.aes = list(size = 0.3))) + |
| 233 |
labs(x="Granularity parameter", y="Granularity parameter") + |
| 234 |
geom_text(aes(label=round(d)), color="white", size=4) + scale_fill_viridis(option = "A", |
| 235 |
name = "Distance to GCS as percentage of nodes", direction = -1, limits=c(0,100), |
| 236 |
guide = guide_colourbar(direction = "horizontal", |
| 237 |
draw.ulim = F, |
| 238 |
title.hjust = 0.5, |
| 239 |
barheight = unit(3, "mm"), |
| 240 |
label.hjust = 0.5, title.position = "top")) |
| 241 |
ggsave("$out_heat_pdf", width=${RCLPLOT_X:-6}, height=${RCLPLOT_Y:-5}); |
| 242 |
EOR |
| 243 |
echo "-- $out_heat_pdf created" |
| 244 |
|
| 245 |
# Granularity: |
| 246 |
out_gra_txt="$pfx.qc-gra.txt" |
| 247 |
out_gra_pdf="${out_gra_txt%.txt}.pdf" |
| 248 |
for fname in "${cls[@]}"; do |
| 249 |
clxdo gra $fname | tr -s ' ' '\n' | tail -n +2 | rcldo.pl granul $fname $(cat $pfx.nitems) |
| 250 |
done > "$out_gra_txt" |
| 251 |
$R_invoke <<EOR |
| 252 |
library(ggplot2, warn.conflicts=FALSE) |
| 253 |
|
| 254 |
mytheme = theme(plot.title = element_text(hjust = 0.5), |
| 255 |
plot.margin=grid::unit(c(4,4,4,4), "mm"), legend.spacing.y=unit(0, 'mm'), legend.key.size = unit(5, "mm"), |
| 256 |
text=element_text(family="serif")) |
| 257 |
|
| 258 |
a <- read.table("$out_gra_txt", colClasses=c("factor", "double", "double")) |
| 259 |
xLabels <- c(1,10,100,1000,10000) |
| 260 |
ggplot(a) + geom_line(aes(x = V2, y = V3, group=V1, colour=V1)) + |
| 261 |
mytheme + scale_color_viridis_d() + |
| 262 |
geom_point(aes(x = V2, y = V3, group=V1, colour=V1)) + |
| 263 |
labs(col="${RCLPLOT_GRA_COLOUR:-Granularity parameter}", x="Cluster size x", y=expression("Fraction of nodes in clusters of size "<="x")) + |
| 264 |
scale_x_continuous(breaks = c(0,10,20,30,40),labels= xLabels) + |
| 265 |
ggtitle("${RCLPLOT_GRA_TITLE:-Granularity signatures across inflation}") |
| 266 |
ggsave("$out_gra_pdf", width=${RCLPLOT_X:-6}, height=${RCLPLOT_Y:-4}) |
| 267 |
EOR |
| 268 |
echo "-- $out_gra_pdf created" |
| 269 |
|
| 270 |
|
| 271 |
elif [[ $themode == 'qc2' ]]; then |
| 272 |
|
| 273 |
require_file "$pfx.lsocls" "(created in step 'tree')" |
| 274 |
export MCLXIOVERBOSITY=2 |
| 275 |
mapfile -t cls < $pfx.lsocls |
| 276 |
|
| 277 |
# if $do_force || [[ ! -f $pfx.qc2all.txt ]]; then |
| 278 |
if ! test_absence $pfx.qc2all.txt return; then |
| 279 |
echo "-- reusing $pfx.qc2all.txt" |
| 280 |
else |
| 281 |
|
| 282 |
for cls in ${cls[@]}; do |
| 283 |
|
| 284 |
export tag=$(rcldo.pl clstag $cls) |
| 285 |
ecc_file=$pfx.ecc.$tag.txt |
| 286 |
cls_dump=$pfx.clsdump.$tag |
| 287 |
|
| 288 |
echo "-- computing cluster-wise eccentricity for $cls [$tag]" |
| 289 |
mcx alter -icl $cls -imx $pfx.input --block | mcx diameter -imx - -t $cpu --progress | tail -n +2 > $ecc_file |
| 290 |
|
| 291 |
mcxdump -imx $cls --transpose --no-values > $cls_dump |
| 292 |
|
| 293 |
if ! diff -q <(cut -f 1 $ecc_file) <(cut -f 1 $cls_dump); then |
| 294 |
echo "Difference in domains!" |
| 295 |
false |
| 296 |
fi |
| 297 |
|
| 298 |
paste $ecc_file <(cut -f 2 $cls_dump) \ |
| 299 |
| sort -nk 3 \ |
| 300 |
| datamash --group 3 mean 2 count 3 \ |
| 301 |
| perl -ne 'chomp; print "$_\t$ENV{tag}\n"' \ |
| 302 |
> $pfx.qc2.$tag.txt |
| 303 |
|
| 304 |
done |
| 305 |
cat $pfx.qc2.*.txt | tac > $pfx.qc2all.txt |
| 306 |
fi |
| 307 |
|
| 308 |
out_qc2_pdf=$pfx.qc2all.pdf |
| 309 |
|
| 310 |
$R_invoke <<EOR |
| 311 |
library(ggplot2, warn.conflicts=FALSE) |
| 312 |
library(viridis, warn.conflicts=FALSE) |
| 313 |
d <- read.table("$pfx.qc2all.txt") |
| 314 |
d <- d[d\$V3 > 1,] # remove singletons. |
| 315 |
d\$V4 <- as.factor(d\$V4) |
| 316 |
mytheme = theme(plot.title = element_text(hjust = 0.5), |
| 317 |
plot.margin=grid::unit(c(4,4,4,4), "mm"), legend.spacing.y=unit(0, 'mm'), legend.key.size = unit(5, "mm"), |
| 318 |
text=element_text(family="serif")) |
| 319 |
ggplot() + mytheme + |
| 320 |
geom_point(data=d, aes(x=V2, y=log10(V3), colour=V4)) + |
| 321 |
expand_limits(x=c(0,${RCL_ECC_X:-10}), y=c(0,${RCL_ECC_Y:-5})) + |
| 322 |
labs(colour = "Cls", x="Average eccentricity", y="Cluster size (log10)") + |
| 323 |
scale_color_viridis(discrete=TRUE, labels=as.numeric(levels(d\$V4))/10**${RCLPLOT_PARAM_SCALE:-0}) + |
| 324 |
ggtitle("${RCLPLOT_ECC_TITLE:-Cluster size / eccentricity}") |
| 325 |
ggsave("$out_qc2_pdf", width=${RCLPLOT_X:-5}, height=${RCLPLOT_Y:-5}) |
| 326 |
EOR |
| 327 |
echo "-- file $out_qc2_pdf created" |
| 328 |
|
| 329 |
|
| 330 |
elif [[ $themode == 'heatannot' ]]; then |
| 331 |
|
| 332 |
require_opt $themode -a "$ANNOTATION" "an annotation table with header line, row names as in tab file" |
| 333 |
if (( n_req )); then exit 1; fi |
| 334 |
require_file "$ANNOTATION" "an annotation table with header line, row names as in tab file" |
| 335 |
|
| 336 |
mybase=$projectdir/hm.$infix |
| 337 |
|
| 338 |
# cannot use labelcls to unify outputs - rcldo does lots of stuff for hierarchy including Newick tree. |
| 339 |
# |
| 340 |
dispatch_type=none |
| 341 |
dispatch_file=none |
| 342 |
|
| 343 |
if [[ -n $CLUSTERING ]]; then # the old cls mode. |
| 344 |
dispatch_type=heatannotcls |
| 345 |
dispatch_file=$mybase.thecls.txt |
| 346 |
mcxdump -imx $CLUSTERING --dump-rlines --no-values > $dispatch_file |
| 347 |
elif [[ -n $HIERARCHY ]]; then |
| 348 |
dispatch_type=heatannot |
| 349 |
dispatch_file=$HIERARCHY |
| 350 |
else |
| 351 |
echo "Need -c cls or -h hierarchy argument" && false |
| 352 |
fi |
| 353 |
|
| 354 |
how=created |
| 355 |
if test_absence $mybase.sum.txt return; then |
| 356 |
# rcldo.pl $dispatch_type $ANNOTATION $dispatch_file $projectdir/rcl.tab > $mybase.txt |
| 357 |
rcldo.pl $dispatch_type $ANNOTATION $dispatch_file $projectdir/rcl.tab $mybase |
| 358 |
# creates $mybase.{sum.txt,nwk} |
| 359 |
else |
| 360 |
how=reused |
| 361 |
fi |
| 362 |
echo "-- file $mybase.sum.txt $how" |
| 363 |
|
| 364 |
# exit 0 |
| 365 |
|
| 366 |
$R_invoke <<EOR |
| 367 |
suppressPackageStartupMessages(library(circlize, warn.conflicts=FALSE)) |
| 368 |
suppressPackageStartupMessages(library(ComplexHeatmap, warn.conflicts=FALSE)) |
| 369 |
suppressPackageStartupMessages(library(DECIPHER, warn.conflicts=FALSE)) # For newick read |
| 370 |
col_mv = colorRamp2(${RCLHM_MVSCALE:-1} * rev(c(0, 1, 2, 3, 4, 5)), c("darkred", "orange", "lightgoldenrod", "white", "lightblue", "darkblue")) |
| 371 |
col_logit = colorRamp2(c(-2, -1, 0, 1, 2, 4, 6)-4, c("darkblue", "lightblue", "white", "lightgoldenrod", "orange", "red", "darkred")) |
| 372 |
col_freq = colorRamp2(${RCLHM_FRSCALE:-1} * c(-5,-3,0,1,2,3,5), c("darkblue", "lightblue", "white", "lightgoldenrod", "orange", "red", "darkred")) |
| 373 |
col_zs = colorRamp2(${RCLHM_ZSSCALE:-1} * c(-2, -1, 0, 1, 2), c("darkblue", "lightblue", "white", "orange", "darkred")) |
| 374 |
|
| 375 |
g <- read.table("$mybase.sum.txt", header=T, sep="\t", row.names=4, check.names=FALSE, as.is=TRUE) |
| 376 |
each_cls_sz <- g[3:nrow(g),"Size"] |
| 377 |
universe_sz <- g\$Size[1] |
| 378 |
|
| 379 |
g2 <- as.matrix(g[3:nrow(g),4:ncol(g)]) |
| 380 |
term_uv_sum <- as.numeric(g[1,4:ncol(g)]) # vector over terms, sum score over universe. |
| 381 |
term_uv_sdev <- as.numeric(g[2,4:ncol(g)]) # vector over terms, sdev for each term. |
| 382 |
term_uv_mean <- term_uv_sum / universe_sz # vector over terms, mean score over universe. |
| 383 |
|
| 384 |
g3 <- apply(g2, 2, function(x) { x / (each_cls_sz/universe_sz)}) |
| 385 |
g4 <- apply(g3, 1, function(y) { log(y / term_uv_sum) }) |
| 386 |
h4 <- t(apply(g2, 2, function(x) { ((x / each_cls_sz)) })) # { -log10(x / (each_cls_sz)) })) |
| 387 |
|
| 388 |
z3 <- apply(g2, 2, "/", each_cls_sz) # average score for cluster. |
| 389 |
z4 <- apply(z3, 1, function(y) { (y - term_uv_mean ) / term_uv_sdev }) |
| 390 |
|
| 391 |
# h4: mean value in cluster. |
| 392 |
# g4: logarithm of frequency(cluster)/frequency(universe) |
| 393 |
# z4: z-score transformed data. |
| 394 |
|
| 395 |
r <- FALSE # Either FALSE or a dendrogram ... |
| 396 |
if ("$dispatch_type" == 'heatannot') { |
| 397 |
r <- ReadDendrogram("$mybase.nwk") |
| 398 |
if (nobs(r) != ncol(g4)) { |
| 399 |
stop(sprintf("Dendrogram has %d elements, table $mybase.sum.txt has %d elements", nobs(r), ncol(g4))) |
| 400 |
} |
| 401 |
# used to have/need this: complete dendrogram/matrix ordering understanding still pending. |
| 402 |
# g4 <- g4[,order(order.dendrogram(r))] |
| 403 |
} |
| 404 |
|
| 405 |
ls_transform = c("rf", "mv", "zs") |
| 406 |
ls_transform = c("zs") |
| 407 |
# rf relative frequency |
| 408 |
# mv mean value |
| 409 |
# zs Z score |
| 410 |
for (transform in ls_transform) { |
| 411 |
myclr <- col_freq |
| 412 |
obj <- g4 |
| 413 |
if (transform == "mv") { myclr <- col_mv; obj <- h4 } |
| 414 |
if (transform == "zs") { myclr <- col_zs; obj <- z4 } |
| 415 |
|
| 416 |
inf_expected <- ifelse(transform == "rf", "expected", "unexpected") |
| 417 |
nna <- sum(is.na(obj)) |
| 418 |
ninf <- sum(is.infinite(obj)) |
| 419 |
if (nna > 0) { cat(sprintf("<> %d NA resulting for transform %s\n", nna, transform), file=stderr()) } |
| 420 |
if (ninf > 0) { cat(sprintf("<> %d infinite instances for transform %s (%s)\n", ninf, transform, inf_expected), file=stderr()) } |
| 421 |
|
| 422 |
obj[is.na(obj)] <- 0 |
| 423 |
obj[is.infinite(obj) & obj > 0] <- 10 |
| 424 |
obj[is.infinite(obj) & obj < 0] <- -10 |
| 425 |
|
| 426 |
## the first value is the first residual cluster, usually much larger than the rest. |
| 427 |
clr_size = colorRamp2(c(0, median(g\$Size[-1]), max(g\$Size[-c(1,2)])), c("white", "lightgreen", "darkgreen")) |
| 428 |
clr_type = colorRamp2(c(0, median(term_uv_sum), max(term_uv_sum)), c("white", "plum1", "purple4")) |
| 429 |
size_ha = HeatmapAnnotation("${RCLHM_SET:-Set size}" = each_cls_sz, col=list("${RCLHM_SET:-Set size}"=clr_size), show_annotation_name = TRUE) |
| 430 |
type_ha = HeatmapAnnotation("${RCLHM_TERM:-Annot}" = term_uv_sum, col=list("${RCLHM_TERM:-Annot}"=clr_type), which='row', show_annotation_name = TRUE) |
| 431 |
ht <- Heatmap(obj, name = "${RCLHM_VALUE:-Heat}", |
| 432 |
column_title = "${RCLHM_XTITLE:-Clusters}", # row_title = "${RCLHM_YTITLE:-Annotation}", |
| 433 |
cluster_rows = ${RCLHM_ROWCLUSTER:-FALSE}, |
| 434 |
cluster_columns= r, |
| 435 |
top_annotation = size_ha, |
| 436 |
right_annotation = type_ha, |
| 437 |
col=myclr, |
| 438 |
heatmap_width = unit(${RCLPLOT_X:-14}, "cm"), |
| 439 |
heatmap_height = unit(${RCLPLOT_Y:-20}, "cm"), |
| 440 |
row_names_gp = gpar(fontsize = ${RCLPLOT_YFTSIZE:-8}), |
| 441 |
column_names_gp = gpar(fontsize = ${RCLPLOT_XFTSIZE:-8}, fontfamily="Courier"), |
| 442 |
show_column_names = ${RCLHM_CNAMES:-FALSE}, |
| 443 |
column_names_side = "top", |
| 444 |
show_column_dend = ${RCLHM_CDEND:-TRUE}, show_row_dend = ${RCLHM_RDEND:-TRUE}, |
| 445 |
show_row_names = ${RCLHM_RNAMES:-TRUE}, |
| 446 |
row_labels=lapply(rownames(obj), function(x) { substr(x, 1, ${RCLPLOT_YLABELMAX:-20}) })) |
| 447 |
|
| 448 |
# options(repr.plot.width = unit(${RCLPLOT_HM_X:-20}, "cm"), repr.plot.height = unit(${RCLPLOT_HM_Y:-40}, "cm"), repr.plot.res = 100) |
| 449 |
|
| 450 |
ht = draw(ht, merge_legend = TRUE, annotation_legend_side = "right") |
| 451 |
cat(sprintf("Heatmap dimensions: %s %s\n", ComplexHeatmap:::width(ht), ComplexHeatmap:::height(ht)), file=stderr()) |
| 452 |
w = ComplexHeatmap:::width(ht) |
| 453 |
w = convertX(w, "inch", valueOnly = TRUE) |
| 454 |
h = ComplexHeatmap:::height(ht) |
| 455 |
h = convertY(h, "inch", valueOnly = TRUE) |
| 456 |
|
| 457 |
pdf(sprintf("$mybase.%s.pdf", transform), width=w, height=h) |
| 458 |
ht = draw(ht, merge_legend = TRUE, annotation_legend_side = "right") |
| 459 |
invisible(dev.off()) |
| 460 |
|
| 461 |
cat(sprintf("-- file $mybase.%s.pdf created\n", transform), file=stderr()) |
| 462 |
} |
| 463 |
EOR |
| 464 |
|
| 465 |
|
| 466 |
elif [[ $themode == 'quickmark' ]]; then |
| 467 |
|
| 468 |
if [[ -z ${RCL_SCRIPT_HOME+x} ]]; then |
| 469 |
echo "-- Please set RCL_SCRIPT_HOME to the path where rcl-qm.R is found" |
| 470 |
false |
| 471 |
fi |
| 472 |
|
| 473 |
mybase=$projectdir/qm.$infix |
| 474 |
mycachebase=$projectdir/qmcache.$infix |
| 475 |
|
| 476 |
echo "-- Using $mybase as file prefix" |
| 477 |
|
| 478 |
require_opt $themode -d "$COUNTMATRIX" "Count matrix in matrix market format (saved by writeMM)" |
| 479 |
require_opt $themode -g "$GENENAMES" "Gene names - should correpond to row names of count matrix" |
| 480 |
if (( n_req )); then exit 1; fi |
| 481 |
|
| 482 |
# ? todo check file $mybase.cls |
| 483 |
if [[ -n $CLUSTERING ]]; then # the old cls mode. |
| 484 |
mcxdump -imx $CLUSTERING -tabr $pfx.tab --dump-rlines --no-values > $mybase.cls |
| 485 |
elif [[ -n $HIERARCHY ]]; then |
| 486 |
rcldo.pl labelcls $pfx.tab < $HIERARCHY > $mybase.cls |
| 487 |
else |
| 488 |
echo "Need -c cls or -h hierarchy argument" && false |
| 489 |
fi |
| 490 |
|
| 491 |
echo "Starting quickMarkers() analysis [https://github.com/constantAmateur/SoupX/]" |
| 492 |
R --no-echo --silent --quiet --vanilla --args $COUNTMATRIX $GENENAMES $mybase.cls $mybase.annot.txt $mycachebase.qm.txt $mycachebase.data.txt ${RCL_QM_N:-2} ${RCL_QM_TFIDF:-1.0} < $RCL_SCRIPT_HOME/rcl-qm.R |
| 493 |
echo "Output in $mybase.annot.txt" |
| 494 |
|
| 495 |
fi |
| 496 |
|
| 497 |
|