R/GlowwormOutputStacked.R

Defines functions GlowwormOutputStacked

Documented in GlowwormOutputStacked

#' @title Generate a stacked bar plot showing the expression pattern for each gene across all subpopulations.
#' @description This function generates a stacked bar plot showing the expression pattern for each gene across each subpopulations.
#' Genes are ranked based on both specificity and abundance either across the whole population (if using GlowwormScale as input) or
#' across specific subpopulations (if using GlowwormSubpopulation as input). Each broad population in the metadata is given one color,
#' and subpopulations are indicated by the various shades of that color.
#' @param GlowwormOutput Output from GlowwormScale or GlowwormSubpopulation
#' @param ShowTop A numeric value indicating how many of the top ranked genes to show. Default is all.
#' @param Colors A vector of colors to use for the broad populations. 14 colors are set as default. If more than 14 broad populations
#' are detected, the user will need to input a vector of colors.
#' @param Legend Show Legend. Default is true.
#' @param RotateXText Rotate gene names on x-axis. Default is 0, but 45 and 90 are well supported.
#' @param SizeXText Size of gene names. Default is 11.
#' @return
#' @importFrom ggpubr get_legend as_ggplot ggarrange
#' @importFrom tidyr gather
#' @export

GlowwormOutputStacked = function(GlowwormOutput, ShowTop = "All", Colors = "Default", Legend = T, RotateXText = 0, SizeXText = 11){
  if("GlowwormScaleOutput" %in% names(GlowwormOutput)){
    MetaData = GlowwormOutput[["MetaData"]]
    if(Colors == "Default"){Colors = c('#808000', '#fabebe', '#911eb4', '#ffe119', '#e6beff', '#000075', '#3cb44b', '#008080', '#f58231',  '#e6194b', '#4363d8', '#fffac8', '#aaffc3', '#ffd8b1')}
    if(ShowTop == "All"){ShowTop = dim(GlowwormOutput[["GlowwormScaleOutput"]])[2]-2}
    if(length(unique(MetaData[[2]])) > length(Colors)){stop("More than 14 unique broad populations were detected in metadata. Please provide additional colours to the argument")}

    #Stacked Plot
    SubpopTable = as.data.frame(matrix(ncol=3, nrow=0))
    colnames(SubpopTable) = c("Broad", "NSubpops", "Buffer")
    colnames(MetaData) = c("Pop", "Broad")
    for(x in unique(MetaData$Broad)){
      Subs = subset(MetaData, MetaData$Broad %in% x)
      SubsTab = as.data.frame(x)
      colnames(SubsTab) = "Broad"
      SubsTab$NSubpops = length(unique(Subs$Pop))
      SubsTab$Buffer = round(length(unique(Subs$Pop))  + log2(length(unique(Subs$Pop)) *2))
      SubpopTable = rbind(SubpopTable, SubsTab)
    }
    SubpopTable$BROADPOP_Colors = Colors[1:dim(SubpopTable)[1]]

    GeneData = GlowwormOutput[["GlowwormScaleOutput"]]
    GeneData_t = as.data.frame(t(GeneData))
    DataGather = gather(GeneData_t, Pop, Value)
    DataGather$Broad = gsub(".*\\^", "", DataGather$Pop)
    DataGather$Pop = gsub("\\^.*", "", DataGather$Pop)
    DataGather$Gene = row.names(GeneData_t)

    CompileColors = as.data.frame(matrix(ncol = 3, nrow=0))
    colnames(CompileColors) = c("Subpops", "SubpopColors", "Buffer")

    for(x in unique(DataGather$Broad)){
      Subs = subset(DataGather, DataGather$Broad %in% x)
      Subs = Subs[order(Subs$Pop),]
      SubsNandCol = subset(SubpopTable, SubpopTable$Broad %in% x)
      GetColors <- as.data.frame(colorRampPalette(c(SubsNandCol$BROADPOP_Colors, "white"))(SubsNandCol$Buffer))
      colnames(GetColors) = "SubpopColors"
      BufferLevels = SubsNandCol$Buffer - SubsNandCol$NSubpops
      GetColors$Subpops = c(unique(Subs$Pop), rep(NA, BufferLevels))
      GetColors = na.omit(GetColors)
      GetColors$SubpopColors = rev(GetColors$SubpopColors)
      CompileColors = rbind(CompileColors, GetColors)
    }

    SumStats = GlowwormOutput[[grep("SumStats", names(GlowwormOutput), value = T)]]
    SumStats = data.frame(SumStats[order(-SumStats[[grep("RankScore", colnames(SumStats), value = T)]]),])

    GetTopGenes = SumStats %>% top_n(ShowTop, RankScore)

    DataGather2 = merge(DataGather, CompileColors, by.x = "Pop", by.y = "Subpops")
    DataGather2 = DataGather2[order(DataGather2$Broad),]
    DataGather2 = subset(DataGather2, DataGather2$Gene %in% row.names(GetTopGenes))
    DataGather2$Gene = factor(DataGather2$Gene, levels = row.names(GetTopGenes))
    DataGather2$Value = as.numeric(DataGather2$Value)
    DataGather2 = DataGather2[order(DataGather2$Gene),]
    FindMax = DataGather2 %>% group_by(Gene) %>% dplyr::summarise(Height = sum(Value))
    FindMax = max(FindMax$Height)


    if(RotateXText == 90){
      SetVjust = 0.5
      SetHjust = 1
    }else if(RotateXText == 45){
      SetVjust = 1
      SetHjust = 1
    }else{
      SetVjust = 0
      SetHjust =  0.5
    }

    StackedPlot = ggplot(DataGather2, aes(fill=Pop, y=Value, x=Gene)) + theme_classic() +
      geom_bar(position="stack", stat="identity", fill = DataGather2$SubpopColors) + theme(legend.position = "none", axis.text.x = element_text(size=SizeXText, face="italic", angle = RotateXText, vjust = SetVjust, hjust=SetHjust)) + scale_fill_manual(values = unique(DataGather2$SubpopColors)) + ylab("Normalized average counts") + xlab("") + scale_y_continuous(limits = c(0,FindMax*1.1), expand= c(0,0))

    Stack = ggplot(SubpopTable, aes(fill=Broad, y=Buffer, x=Buffer)) + geom_bar(position="stack", stat="identity") + scale_fill_manual(values=SubpopTable$BROADPOP_Colors, name=" ", label = SubpopTable$Broad) #+ theme(legend.position = "bottom")

    LegendOnly <- get_legend(Stack)
    LegendOnly = as_ggplot(LegendOnly)

    if(ShowTop < 5){LegendPos = 1}else if(ShowTop > 5 & ShowTop < 15){LegendPos = 2}else if(ShowTop > 15 & ShowTop < 50){LegendPos = 4}else{LegendPos = 10}


    if(Legend == T){
      STACK = ggarrange(StackedPlot, LegendOnly, ncol = 2, widths = c(ShowTop,LegendPos))
    }else{
      STACK = StackedPlot
    }

    return(STACK)
  }else{
    stop("Need to run GlowwormGlobal or GlowwormSubpopulation")
  }}
Hannahglover/Glowworm documentation built on Jan. 16, 2024, 11:47 p.m.