view ggplot2_heatmap.xml @ 8:8b8a26a5fdf2 draft default tip

planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ggplot2 commit 57b86418a4f032a5664b8dc1c9585a11be629158
author iuc
date Thu, 15 May 2025 13:01:32 +0000
parents 48fc850095ba
children
line wrap: on
line source

<tool id="ggplot2_heatmap" name="Heatmap w ggplot" version="@TOOL_VERSION@+galaxy@VERSION_SUFFIX@" profile="23.0">
    <macros>
        <import>macros.xml</import>
    </macros>
    <expand macro="bio_tools"/>
    <requirements>
        <requirement type="package" version="1.1.1">r-cowplot</requirement>
        <requirement type="package" version="0.4.5">r-egg</requirement>
        <requirement type="package" version="0.1.23">r-ggdendro</requirement>
        <requirement type="package" version="1.0.10">r-dplyr</requirement>
        <requirement type="package" version="1.4.4">r-reshape2</requirement>
        <requirement type="package" version="2.1.0">r-svglite</requirement>
    </requirements>
    <command detect_errors="exit_code"><![CDATA[
cat '$script' &&
Rscript '$script'
    ]]></command>
    <configfiles>
        <configfile name="script"><![CDATA[
@R_INIT@

## Import library

library(ggplot2)
library(cowplot)
library(egg)
library(dplyr)
library(ggdendro)
library(reshape2)

input <- '$input1'
header <- ${inputdata.header}
rowname_index <- as.integer('$inputdata.row_names_index')

transform <- '$adv.transform'

## read table with or with out header or row_names
if(rowname_index > 0){
    df <- read.table(input, header = header, row.names = rowname_index, sep = "\t")
}else{
    df <- read.table(input, header = header, sep = "\t")
}

hclust_fun = function(x) hclust(x, method="complete")
dist_fun = function(x) dist(x, method="maximum")
distfun=dist_fun
hclustfun=hclust_fun

plot_mat <- df

## transform dataset
if(transform == "log2"){
    plot_mat <- log2(plot_mat)
    cat("\n ", transform, " transformed")
}else if(transform == "log2plus1"){
    plot_mat <- log2(plot_mat+1)
    cat("\n ", transform, " transformed")
}else if(transform == "log10"){
    plot_mat <- log10(plot_mat)
    cat("\n ", transform, " transformed")
}else if(transform == "log10plus1"){
    plot_mat <- log10(plot_mat+1)
    cat("\n ", transform, " transformed")
}else{
    plot_mat <- plot_mat
}

#if $adv.colorscheme == "whrd"
    colorscale = scale_fill_gradient(low="white", high="red", guide="colorbar")
#elif $adv.colorscheme == "whblu"
    colorscale = scale_fill_gradient(low="white", high="blue", guide="colorbar")
#elif $adv.colorscheme == "blwhre"
    colorscale = scale_fill_gradient2(low="blue", mid="white", high="red", guide="colorbar")
#end if

plot_mat["rows"] <- row.names(plot_mat)
plot_mat.melt <- melt(plot_mat, id.vars = "rows")
names(plot_mat.melt)[2] <- "cols"

#if $adv.cluster:

plot_mat.dendo <- as.dendrogram(hclust(d = dist(x = subset(plot_mat, select = -rows))))

plot_mat.dendo.order <- order.dendrogram(plot_mat.dendo)

gg_rows = ggdendrogram(data = plot_mat.dendo, rotate = FALSE) +
    theme(axis.text.y = element_text(size = 6), axis.text.x = element_blank())

plot_mat.melt[,"rows"] <- factor(x = plot_mat.melt[,"rows"],
       levels = unique(plot_mat.melt[,"rows"])[plot_mat.dendo.order],
       ordered = TRUE)

plot_mat.dendo <- as.dendrogram(hclust(d = dist(x = t(subset(plot_mat, select = -rows)))))
plot_mat.dendo.order <- order.dendrogram(plot_mat.dendo)

gg_cols = ggdendrogram(data = plot_mat.dendo, rotate = TRUE) +
    theme(axis.text.x = element_text(size = 6), axis.text.y = element_blank())

plot_mat.melt[,"cols"] <- factor(x = plot_mat.melt[,"cols"],
                     levels = unique(plot_mat.melt[,"cols"])[plot_mat.dendo.order],
                     ordered = TRUE)

## plot the heatmap
gg_hm = plot_mat.melt %>%
  ggplot(aes(x = rows, y = cols, fill = value)) +
  geom_tile() +
  theme(legend.position = "bottom", axis.title.x = element_blank(), axis.title.y = element_blank(),
        axis.text.x = element_text(angle = 90, hjust = 1)) +
  colorscale

gg_empty = plot_mat.melt %>%
    ggplot(aes(x = cols, y = value)) +
    geom_blank() +
    theme(axis.text = element_blank(),
        axis.title = element_blank(),
        line = element_blank(),
        panel.background = element_blank())

plot_out <- ggarrange(
    gg_rows, gg_empty, gg_hm, gg_cols,
    nrow = 2, ncol = 2, widths = c(3, 1), heights = c(1, 3), newpage =F)

#else

## plot the heatmap
gg_hm = plot_mat.melt %>%
    ggplot(aes(x = rows, y = cols, fill = value)) +
    geom_tile() + ggtitle('$title') +
    theme(legend.position = "bottom", axis.title.x = element_blank(), axis.title.y = element_blank(), axis.text.x = element_text(angle = 90, hjust = 1)) +
    colorscale

plot_out <- gg_hm

#end if

@SAVE_OUTPUT@
        ]]></configfile>
    </configfiles>
    <inputs>
        <expand macro="read_complex_input"/>
        <expand macro="title"/>
        <!-- Advanced Options  -->
        <section name="adv" title="Advanced Options" expanded="false">
            <expand macro="transform" />
            <param name="cluster" type="boolean" truevalue="true" falsevalue="false" checked="false" label="Enable data clustering" />
            <param name="colorscheme" type="select" label="Heatmap colorscheme" >
                <option value="whrd" selected="true">White to red</option>
                <option value="whblu">White to blue</option>
                <option value="blwhre">Blue to white to red</option>
            </param>
        </section>
        <!-- Output Options  -->
        <section name="out" title="Output Options" expanded="true">
            <expand macro="dimensions" />
        </section>
    </inputs>
    <outputs>
         <expand macro="additional_output" />
    </outputs>
    <tests>
        <test expect_num_outputs="2">
            <param name="input1" value="mtcars.txt" ftype="tabular"/>
            <conditional name="inputdata">
                <param name="input_type" value="with_header_rownames"/>
                <param name="header" value="TRUE"/>
                <param name="row_names_index" value="1"/>
                <param name="sample_name_orientation" value="TRUE"/>
            </conditional>
            <param name="transform" value="log10plus1"/>
            <param name="cluster" value="true"/>
            <param name="colorscheme" value="blwhre"/>
            <param name="additional_output_format" value="pdf"/>
            <output name="output2" file="ggplot_heatmap_result1.pdf" ftype="pdf" compare="sim_size"/>
        </test>
    </tests>
    <help><![CDATA[
This tool will generate a clustered heatmap of your data. More customization options will be added, for now the heatmap uses a red coloring scheme and clustering is performed using the "maximum" similarity measure and the "complete" hierarchical clustering measure.

Input data should have row labels in the first column and column labels. For example, the row labels (the first column) should represent gene IDs and the column labels should represent sample IDs.
    ]]></help>
    <expand macro="citations"/>
</tool>