R/plotCopynumber.R

Defines functions plotCopynumber

Documented in plotCopynumber

#' Print Genome wide copynumber plot
#'
#' This function will take a dataframe with sample, chr, start.pos, end.pos and call.
#' It will output a full plot of all the segments
#' @title plotCopynumber
#' @param ReturnClass Class of type CNV. REQUIRED
#' @param setColours A vector of colours for the plot
#' @keywords Copynumber CNV
#' @export
#' @examples
#' Plot_Copynumber(CNVvault_Object)

plotCopynumber <- function(ReturnClass, setColours = c(), ...) {

        ReturnClass@Plot$plot <- ggplot(ReturnClass@Segments, aes(xmin=start, xmax=end, ymin=Ystart, ymax=Yend, fill=calls)) +
                geom_rect() +
                ## Draw lines between the chromosomes
                geom_vline(xintercept = ReturnClass@Chr_Starts,
                           colour="grey",
                           alpha=0.25) +
                ## draw lines between the samples
                geom_hline(yintercept = 0:ReturnClass@NumberOfSamples,
                           colour="grey",
                           alpha=0.25) +
                ## Start x at 0. break halfway through each chromosome (this means the label is centered)
                 scale_x_continuous(expand = c(0, 0),
                                    breaks = ReturnClass@Chr_Starts[-length(ReturnClass@Chr_Starts)] +
                                            (ReturnClass@Chr_Sizes/2),
                                    ## set the labels to the chr names
                                    labels = gsub("chr","",
                                                  names(ReturnClass@Chr_Starts[-length(ReturnClass@Chr_Starts)]))) +
                ## Break y between each sample
                scale_y_continuous(expand=c(0,0),
                                   breaks = 0.5:(ReturnClass@NumberOfSamples-0.5), labels=levels(ReturnClass@Segments$sampleID)) +
                ## set the colours for the calls and remove the name
                {if(length(setColours)>0)
                        scale_fill_manual(name="",
                                          values=setColours) } +
                ## Set axis labels
                labs(x="Chromosome", y="Sample") +
                theme_classic() +
                theme(axis.text.x = element_text(size=8,
                                                 face="bold"),
                      axis.text.y = element_text(size=10,
                                                 face="bold"),
                      axis.ticks = element_blank(),
                      legend.position = "top")
        # if (plot) {
        #         print(ReturnClass@Plot$plot)
        #         }
        ReturnClass
}
findlaycopley/PlotCNV documentation built on Feb. 27, 2025, 1:55 a.m.