|
| 1 | +#' Gene annotation plot |
| 2 | +#' |
| 3 | +#' Plot read coverage and location of gene annotations that match the keywords and |
| 4 | +#' search criteria for contig/chunk currently being assessed |
| 5 | +#' |
| 6 | +#' @param geneAnnotSubset Subset of gene annotations to be plotted |
| 7 | +#' @param keywords The key-word(s) used for the search. |
| 8 | +#' @param pileupSubset A subset of the pileup associated with the contig/chunk being assessed |
| 9 | +#' @param colIdx The column index 'gene' or 'product' column |
| 10 | +#' @param startbpRange The basepair at which the search is started if a 'specific' search is used |
| 11 | +#' @param endbpRange The basepair at which the search is ended if a 'specific' search is used |
| 12 | +#' @param elevRatio The maximum/minimum values of the pattern-match |
| 13 | +#' @param pattern The pattern-match information associated with the contig/chunk being assessed |
| 14 | +#' @param windowSize The number of basepairs to average read coverage values over. |
| 15 | +#' @keywords internal |
| 16 | +#' @importFrom stringr str_which |
| 17 | +geneAnnotationPlot <- function(geneAnnotSubset, keywords, pileupSubset, |
| 18 | + colIdx, startbpRange, endbpRange, elevRatio, |
| 19 | + pattern, windowSize, chunkSize, mode){ |
| 20 | + position <- coverage <- start <- NULL |
| 21 | + classification <- pattern[[7]] |
| 22 | + refName <- pileupSubset[1, 1] |
| 23 | + if(mode == "genome"){ |
| 24 | + chunkNumber <- as.numeric(str_extract(refName, "(?<=\\_)\\d+$")) - 1 |
| 25 | + startPos <- (pattern[[4]] * windowSize) + (chunkNumber * chunkSize) |
| 26 | + endPos <- (pattern[[5]] * windowSize) + (chunkNumber * chunkSize) |
| 27 | + } else { |
| 28 | + startPos <- pattern[[4]] * windowSize |
| 29 | + endPos <- pattern[[5]] * windowSize |
| 30 | + } |
| 31 | + matchIdxs <- str_which(geneAnnotSubset[, colIdx], regex(paste(keywords, collapse = "|"), ignore_case = TRUE)) |
| 32 | + geneAnnotMatches <- geneAnnotSubset[matchIdxs,] |
| 33 | + geneStartPos <- geneAnnotMatches$start |
| 34 | + geneAnnotLabels <- paste0("#", c(1:nrow(geneAnnotMatches)), ": ", geneAnnotMatches[, colIdx], |
| 35 | + sep = " ", collapse = " \n ") |
| 36 | + plot <- ggplot(data = pileupSubset, aes(x = position, y = coverage)) + |
| 37 | + geom_area(fill = "#009E73") + |
| 38 | + geom_vline(xintercept = geneStartPos, linewidth = 1) + |
| 39 | + geom_vline(xintercept = c(startPos, endPos), color = "#D55E00", linewidth = 1) + |
| 40 | + geom_vline(xintercept = c(startbpRange, endbpRange), color = "#D55E00", linewidth = 1, linetype = "dotted") + |
| 41 | + geom_label(data = geneAnnotMatches, aes(x = start, y = (max(pileupSubset$coverage) / 2), |
| 42 | + label = paste0("#", c(1 : nrow(geneAnnotMatches)))), |
| 43 | + size = 2.75) + |
| 44 | + scale_x_continuous(expand = c(0, 0)) + |
| 45 | + theme(panel.grid.major = element_blank(), |
| 46 | + panel.grid.minor = element_blank(), |
| 47 | + panel.background = element_blank(), |
| 48 | + axis.line = element_line(colour = "black"), |
| 49 | + text = element_text(size = 15), |
| 50 | + plot.margin = margin( |
| 51 | + t = 0, |
| 52 | + r = 10, |
| 53 | + b = 0, |
| 54 | + l = 2 |
| 55 | + )) + |
| 56 | + labs(title = paste(refName, classification), |
| 57 | + subtitle = paste("elevation ratio:", round(elevRatio, digits = 4)), |
| 58 | + x = "Basepair position", |
| 59 | + caption = geneAnnotLabels, |
| 60 | + y = "Read coverage") |
| 61 | + return(plot) |
| 62 | +} |
0 commit comments