view ggplot2_heatmap.xml @ 1:fe8d4655ee10 draft

planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ggplot2 commit e7b5919a8e06595b23e8fc31b975fc508f5081b3
author iuc
date Mon, 11 Feb 2019 17:29:14 -0500
parents f44dc95928aa
children 391a4cc45fea
line wrap: on
line source

<tool id="ggplot2_heatmap" name="Heatmap w ggplot" version="@VERSION@">
    <macros>
        <import>macros.xml</import>
    </macros>
    <requirements>
        <requirement type="package" version="0.9.2">r-cowplot</requirement>
        <requirement type="package" version="0.2.0">r-egg</requirement>
        <requirement type="package" version="0.1_20">r-ggdendro</requirement>
        <requirement type="package" version="0.7.4">r-dplyr</requirement>
        <requirement type="package" version="1.4.3">r-reshape2</requirement>
        <requirement type="package" version="1.2.1">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>
            <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" 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>
    <citations>
    </citations>
</tool>