#!/bin/bash VERSION=22-282 # Copyright 2022 Stijn van Dongen # This program is free software; you can redistribute it and/or modify it # under the terms of version 3 of the GNU General Public License as published # by the Free Software Foundation. It should be shipped with MCL in the top # level directory as the file COPYING. # _ _ __|_ _. __|_ _ _| _ _ _ _|_. _ _ _ _ |. _ | _ _ _ _ _ # | (/__\ | | |(_ | (/_(_| (_(_)| | | |(_|(/_| |(_\/ ||| ||<(_|(_|(/_ (_|(_ # | / | |/ # RCL consensus clustering QC # Author: Stijn van Dongen # # See rcl, github.com/micans/mcl#rcl and this script with no arguments. set -euo pipefail themode= # first argument, mode 'qc' 'qc2' 'heatannot' 'quickmark' projectdir= # second argument, for all modes. infix='vnl' # -x infix, a secondary tag cpu=1 # -p NUM ANNOTATION= # -a FNAME annotation file CLUSTERING= # -c FNAME clustering file (mcl format) HIERARCHY= # -h FNAME hierarchy file (rcl.hm*) COUNTMATRIX= # -d FNAME data file GENENAMES= # -g FNAME row names for COUNTMATRIX do_force=false # -F (for mode 'tree') R_invoke="R --no-echo --quiet --silent --vanilla" # -R (echo R code rather than execute it) function usage() { local e=$1 cat < /dev/null; then echo "Input is not in mcl network format; check file $fname" false fi } function test_absence () { local fname=$1 local action=${2:?Programmer error, need second argument} # return or exit if [[ -f $fname ]]; then if $do_force; then echo "Recreating existing file $fname" else echo "File $fname exists, use -F to force renewal" if [[ $action == 'return' ]]; then return 1 elif [[ $action == 'exit' ]]; then exit 1 fi fi fi } require_mode "${1-}" # themode now set shift 1 if grep -qFw $themode <<< "qc qc2 heatannot quickmark"; then require_tag $themode "${1-}" # projectdir now set shift 1 fi if [[ $themode == 'version' ]]; then echo "rcl-qc version: $VERSION" exit 0 fi pfx=$projectdir/rcl require_file "$pfx.lsocls" "cluster file $pfx.lsocls is missing" require_file "$pfx.nitems" "count file $pfx.nitems is missing" require_file "$pfx.tab" "tab file $pfx.nitems is missing" echo -- "$themode $projectdir $@" >> $pfx.qcline while getopts :a:c:d:g:h:p:x:FR opt do case "$opt" in a) ANNOTATION=$OPTARG ;; c) CLUSTERING=$OPTARG ;; d) COUNTMATRIX=$OPTARG ;; g) GENENAMES=$OPTARG ;; h) HIERARCHY=$OPTARG ;; p) cpu=$OPTARG ;; x) infix=$OPTARG ;; R) R_invoke=cat ;; F) do_force=true ;; :) echo "Flag $OPTARG needs argument"; exit 1 ;; ?) echo "Flag $OPTARG unknown"; exit 1 ;; esac done shift $((OPTIND-1)) function require_opt() { n_req=0 local mode=$1 option=$2 value=$3 description=$4 if [[ -z $value ]]; then echo "Mode $mode requires $description for option $option" (( ++n_req )) fi } ## ## Q C if [[ $themode == 'qc' ]]; then # ____ ___) ______ ______ # # (, / / (, / ) (, / ) # # /---/ _ __ _ /---( _ / / __ _ _ _____ _ # # ) / (___(/_/ (__(/_ ) / ____)_(/_ _/___ /_/ (_(_(_(_/_(_) / (_/_)_ # # (_/ (_/ ( (_/___ / .-/ # # (_/ # # Some things still hardcoded, e.g. cluster size range. # This looks absolutely awful, with perl madness and inline R scripts. # Remember that things like daisies and clover exist. # Most importantly though It Should Work require_file "$pfx.lsocls" "(created in step 'setup or mcl')" require_file "$pfx.nitems" "(created in step 'setup')" export RCLPLOT_PARAM_SCALE=${RCLPLOT_PARAM_SCALE:-2} mapfile -t cls < $pfx.lsocls # Heatmap: out_heat_txt=$pfx.qc-heat.txt out_heat_pdf=${out_heat_txt%.txt}.pdf ( echo -e "d\tP1\tP2"; clm dist "${cls[@]}" | rcldo.pl distwrangle $(cat $pfx.nitems) ) > $out_heat_txt # \ $R_invoke < "$out_gra_txt" $R_invoke < $ecc_file mcxdump -imx $cls --transpose --no-values > $cls_dump if ! diff -q <(cut -f 1 $ecc_file) <(cut -f 1 $cls_dump); then echo "Difference in domains!" false fi paste $ecc_file <(cut -f 2 $cls_dump) \ | sort -nk 3 \ | datamash --group 3 mean 2 count 3 \ | perl -ne 'chomp; print "$_\t$ENV{tag}\n"' \ > $pfx.qc2.$tag.txt done cat $pfx.qc2.*.txt | tac > $pfx.qc2all.txt fi out_qc2_pdf=$pfx.qc2all.pdf $R_invoke < 1,] # remove singletons. d\$V4 <- as.factor(d\$V4) mytheme = theme(plot.title = element_text(hjust = 0.5), plot.margin=grid::unit(c(4,4,4,4), "mm"), legend.spacing.y=unit(0, 'mm'), legend.key.size = unit(5, "mm"), text=element_text(family="serif")) ggplot() + mytheme + geom_point(data=d, aes(x=V2, y=log10(V3), colour=V4)) + expand_limits(x=c(0,${RCL_ECC_X:-10}), y=c(0,${RCL_ECC_Y:-5})) + labs(colour = "Cls", x="Average eccentricity", y="Cluster size (log10)") + scale_color_viridis(discrete=TRUE, labels=as.numeric(levels(d\$V4))/10**${RCLPLOT_PARAM_SCALE:-0}) + ggtitle("${RCLPLOT_ECC_TITLE:-Cluster size / eccentricity}") ggsave("$out_qc2_pdf", width=${RCLPLOT_X:-5}, height=${RCLPLOT_Y:-5}) EOR echo "-- file $out_qc2_pdf created" elif [[ $themode == 'heatannot' ]]; then require_opt $themode -a "$ANNOTATION" "an annotation table with header line, row names as in tab file" if (( n_req )); then exit 1; fi require_file "$ANNOTATION" "an annotation table with header line, row names as in tab file" mybase=$projectdir/hm.$infix # cannot use labelcls to unify outputs - rcldo does lots of stuff for hierarchy including Newick tree. # dispatch_type=none dispatch_file=none if [[ -n $CLUSTERING ]]; then # the old cls mode. dispatch_type=heatannotcls dispatch_file=$mybase.thecls.txt mcxdump -imx $CLUSTERING --dump-rlines --no-values > $dispatch_file elif [[ -n $HIERARCHY ]]; then dispatch_type=heatannot dispatch_file=$HIERARCHY else echo "Need -c cls or -h hierarchy argument" && false fi how=created if test_absence $mybase.sum.txt return; then # rcldo.pl $dispatch_type $ANNOTATION $dispatch_file $projectdir/rcl.tab > $mybase.txt rcldo.pl $dispatch_type $ANNOTATION $dispatch_file $projectdir/rcl.tab $mybase # creates $mybase.{sum.txt,nwk} else how=reused fi echo "-- file $mybase.sum.txt $how" # exit 0 $R_invoke < 0) { cat(sprintf("<> %d NA resulting for transform %s\n", nna, transform), file=stderr()) } if (ninf > 0) { cat(sprintf("<> %d infinite instances for transform %s (%s)\n", ninf, transform, inf_expected), file=stderr()) } obj[is.na(obj)] <- 0 obj[is.infinite(obj) & obj > 0] <- 10 obj[is.infinite(obj) & obj < 0] <- -10 ## the first value is the first residual cluster, usually much larger than the rest. clr_size = colorRamp2(c(0, median(g\$Size[-1]), max(g\$Size[-c(1,2)])), c("white", "lightgreen", "darkgreen")) clr_type = colorRamp2(c(0, median(term_uv_sum), max(term_uv_sum)), c("white", "plum1", "purple4")) size_ha = HeatmapAnnotation("${RCLHM_SET:-Set size}" = each_cls_sz, col=list("${RCLHM_SET:-Set size}"=clr_size), show_annotation_name = TRUE) type_ha = HeatmapAnnotation("${RCLHM_TERM:-Annot}" = term_uv_sum, col=list("${RCLHM_TERM:-Annot}"=clr_type), which='row', show_annotation_name = TRUE) ht <- Heatmap(obj, name = "${RCLHM_VALUE:-Heat}", column_title = "${RCLHM_XTITLE:-Clusters}", # row_title = "${RCLHM_YTITLE:-Annotation}", cluster_rows = ${RCLHM_ROWCLUSTER:-FALSE}, cluster_columns= r, top_annotation = size_ha, right_annotation = type_ha, col=myclr, heatmap_width = unit(${RCLPLOT_X:-14}, "cm"), heatmap_height = unit(${RCLPLOT_Y:-20}, "cm"), row_names_gp = gpar(fontsize = ${RCLPLOT_YFTSIZE:-8}), column_names_gp = gpar(fontsize = ${RCLPLOT_XFTSIZE:-8}, fontfamily="Courier"), show_column_names = ${RCLHM_CNAMES:-FALSE}, column_names_side = "top", show_column_dend = ${RCLHM_CDEND:-TRUE}, show_row_dend = ${RCLHM_RDEND:-TRUE}, show_row_names = ${RCLHM_RNAMES:-TRUE}, row_labels=lapply(rownames(obj), function(x) { substr(x, 1, ${RCLPLOT_YLABELMAX:-20}) })) # options(repr.plot.width = unit(${RCLPLOT_HM_X:-20}, "cm"), repr.plot.height = unit(${RCLPLOT_HM_Y:-40}, "cm"), repr.plot.res = 100) ht = draw(ht, merge_legend = TRUE, annotation_legend_side = "right") cat(sprintf("Heatmap dimensions: %s %s\n", ComplexHeatmap:::width(ht), ComplexHeatmap:::height(ht)), file=stderr()) w = ComplexHeatmap:::width(ht) w = convertX(w, "inch", valueOnly = TRUE) h = ComplexHeatmap:::height(ht) h = convertY(h, "inch", valueOnly = TRUE) pdf(sprintf("$mybase.%s.pdf", transform), width=w, height=h) ht = draw(ht, merge_legend = TRUE, annotation_legend_side = "right") invisible(dev.off()) cat(sprintf("-- file $mybase.%s.pdf created\n", transform), file=stderr()) } EOR elif [[ $themode == 'quickmark' ]]; then if [[ -z ${RCL_SCRIPT_HOME+x} ]]; then echo "-- Please set RCL_SCRIPT_HOME to the path where rcl-qm.R is found" false fi mybase=$projectdir/qm.$infix mycachebase=$projectdir/qmcache.$infix echo "-- Using $mybase as file prefix" require_opt $themode -d "$COUNTMATRIX" "Count matrix in matrix market format (saved by writeMM)" require_opt $themode -g "$GENENAMES" "Gene names - should correpond to row names of count matrix" if (( n_req )); then exit 1; fi # ? todo check file $mybase.cls if [[ -n $CLUSTERING ]]; then # the old cls mode. mcxdump -imx $CLUSTERING -tabr $pfx.tab --dump-rlines --no-values > $mybase.cls elif [[ -n $HIERARCHY ]]; then rcldo.pl labelcls $pfx.tab < $HIERARCHY > $mybase.cls else echo "Need -c cls or -h hierarchy argument" && false fi echo "Starting quickMarkers() analysis [https://github.com/constantAmateur/SoupX/]" 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 echo "Output in $mybase.annot.txt" fi