R/geom_GC.R

Defines functions content_GC geom_GC1 geom_GC

Documented in geom_GC

##' Multiple sequence alignment layer for ggplot2. It plot points of GC content.

##' @title geom_GC
##' @param show.legend logical. Should this layer be included in the legends?
##' @examples
##' library(ggplot2)
##' #plot GC content
##' f <- system.file("extdata/LeaderRepeat_All.fa", package="ggmsa")
##' ggmsa(f, font = NULL, color="Chemistry_NT") + geom_GC()
##' @export
##' @author Lang Zhou
geom_GC <- function(show.legend = FALSE) {
    structure(list(show.legend = show.legend),
              class = "GCcontent")
}


geom_GC1 <- function(tidyData, show.legend = FALSE){
    tidy <- tidyData
    #tidy <- tidy_msa(msa = msa, start = start, end = end)
    GC_pos <- getOption("GC_pos")

    GC <- content_GC(tidy)
    GC <-GC[GC$character == "GC",]
    col_num <- levels(factor(tidy$position))
    col_len <- length(col_num) + GC_pos
    ly_GC <- geom_point(data = GC, mapping = aes_(x = ~col_len, y = ~ypos, size = ~fre),
                        color = "#51a6e9", na.rm = T, show.legend = show.legend)
    return(ly_GC)
}
##' get GC content

##' @title content_GC
##' @param data  Multiple aligned sequence file or object for representing nucleotide sequences
##' @return A data frame
##' @noRd
##' @author Lang Zhou
content_GC<- function(data){
    tidy <- data
    tidy$name <- factor(tidy$name, levels = unique(tidy$name))
    tidy$ypos <- as.numeric(tidy$name)
    seq_num <- levels(factor(tidy$ypos)) # the sequence number
    lchar_num <- lapply(1:length(seq_num),function(j){
        clo <- tidy[tidy$ypos == j, ]
        y <- prop.table(table(clo$character))
        y["GC"] <- y["G"] + y["C"]
        num <-setNames(rep(0,5), c("A", "T", "G", "C", "GC"))
        num[names(y)] <- y
        return(num)
    })

    char_num <- do.call(rbind,lchar_num)
    char_num <- as.data.frame(char_num)
    char_num["ypos"] =  as.numeric(seq_num)
    char_num2 <- gather(char_num,character,fre, "A", "T", "C","G","GC")
    return(char_num2)
}

Try the ggmsa package in your browser

Any scripts or data that you put into this service are public.

ggmsa documentation built on Aug. 3, 2021, 9:06 a.m.