Skip to content

Commit 3c76b2e

Browse files
authored
Merge pull request #384 from GoekeLab/bugFixPlotBambu
Bug fix plot bambu
2 parents e74f2fd + 1197ad0 commit 3c76b2e

1 file changed

Lines changed: 39 additions & 23 deletions

File tree

R/plotBambu_utilityFunctions.R

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -31,41 +31,57 @@ plotAnnotation <- function(se, gene_id, transcript_id) {
3131
#' @inheritParams plotAnnotation
3232
#' @noRd
3333
plotAnnotation_withExpression <- function(se, gene_id, transcript_id) {
34+
seGene <- transcriptToGeneExpression(se)
3435
if (!is.null(transcript_id)) {
3536
if (!all(transcript_id %in% rownames(se))) stop("all(transcript_id
3637
%in% rownames(se)) condition is not satisfied!")
3738
txRanges <- rowRanges(se)[transcript_id]
38-
names(txRanges) <- paste0(transcript_id, ":", unlist(lapply(
39-
strand(txRanges),
40-
function(x) unique(as.character(x)))))
41-
p_annotation <- ggbio::autoplot(txRanges,
42-
group.selfish = TRUE)
43-
p_expression <-
44-
ggbio::autoplot(as.matrix(log2(assays(se)$CPM[transcript_id, ] +
45-
1)), axis.text.angle = 45)
46-
p <- gridExtra::grid.arrange(p_annotation@ggplot, p_expression,
47-
heights = c(1, 1))
39+
gene_id <- unique(mcols(txRanges)$GENEID)
40+
geneRange <- rowRanges(seGene)[gene_id]
41+
names(txRanges) <- labelFeature(transcript_id, txRanges)
42+
names(geneRange) <- labelFeature(gene_id, geneRange)
43+
p <- plotAnnotation_plotFunction(geneRange, txRanges,se,transcript_id)
4844
return(p)
4945
} else {
5046
if (!all(gene_id %in% rowData(se)$GENEID)) stop("all(gene_id %in%
5147
rowData(se)$GENEID) condition is not satisfied!")
5248
p <- lapply(gene_id, function(g) {
5349
txVec <- rowData(se)[rowData(se)$GENEID == g, ]$TXNAME
5450
txRanges <- rowRanges(se)[txVec]
55-
names(txRanges) <- paste0(txVec, ":", unlist(lapply(
56-
strand(txRanges), function(x) unique(as.character(x)))))
57-
p_annotation <- ggbio::autoplot(txRanges, group.selfish = TRUE)
58-
p_expression <-
59-
ggbio::autoplot(as.matrix(log2(assays(se)$CPM[txVec, ] +
60-
1)), axis.text.angle = 45, hjust = 1)
61-
p <- gridExtra::grid.arrange(p_annotation@ggplot, p_expression,
62-
top = g, heights = c(1, 1) )
51+
geneRange <- rowRanges(seGene)[gene_id]
52+
names(geneRange) <- labelFeature(gene_id, geneRange)
53+
names(txRanges) <-labelFeature(txVec, txRanges)
54+
p <- plotAnnotation_plotFunction(geneRange, txRanges,se,txVec)
6355
return(p)
6456
})
6557
return(p)
6658
}
6759
}
6860

61+
#' label feature wtih strand added
62+
#' @inheritParams plotAnnotation_withExpression
63+
#' @noRd
64+
labelFeature <- function(feature_id,featureRange){
65+
paste0(feature_id, ":", as.character(unlist(unique(strand(featureRange)))))
66+
}
67+
68+
#' the plotting function
69+
#' @inheritParams plotAnnotation_withExpression
70+
#' @noRd
71+
plotAnnotation_plotFunction <- function(geneRange, txRanges,se,txVec){
72+
p_annotation_gene <- ggbio::autoplot(geneRange,
73+
group.selfish = TRUE)
74+
p_annotation <- ggbio::autoplot(txRanges, group.selfish = TRUE)
75+
p_expression <-
76+
ggbio::autoplot(as.matrix(log2(assays(se)$CPM[txVec, ] +
77+
1)), axis.text.angle = 45, hjust = 1)
78+
p <- gridExtra::grid.arrange(Gene = p_annotation_gene@ggplot,
79+
Transcript = p_annotation@ggplot, p_expression,
80+
top = "", heights = c(1,min(length(txVec),4),
81+
min(length(txVec),5)))
82+
return(p)
83+
}
84+
6985
#' plot PCA
7086
#' @param se a SummarizedExperiment object
7187
#' @param count.data a dataframe of log2CPM
@@ -88,9 +104,9 @@ plotPCA <- function(se, count.data, group.variable) {
88104
p <- ggplot2::ggplot(plotData, ggplot2::aes(x = PC1, y = PC2)) +
89105
ggplot2::geom_point(ggplot2::aes(col = groupVar)) +
90106
ggplot2::ylab(paste0("PC2 (",
91-
round(pca_result$sdev[2] / sum(pca_result$sdev) * 100, 1), "%)")) +
107+
round(pca_result$sdev[2]^2 / sum(pca_result$sdev^2) * 100, 1), "%)")) +
92108
ggplot2::xlab(paste0("PC1 (",
93-
round(pca_result$sdev[1] / sum(pca_result$sdev) * 100, 1), "%)")) +
109+
round(pca_result$sdev[1]^2 / sum(pca_result$sdev^2) * 100, 1), "%)")) +
94110
ggplot2::theme_minimal()
95111
} else {
96112
pca_result <- prcomp(t(as.matrix(count.data)))
@@ -99,9 +115,9 @@ plotPCA <- function(se, count.data, group.variable) {
99115
p <- ggplot2::ggplot(plotData, ggplot2::aes(x = PC1, y = PC2)) +
100116
ggplot2::geom_point(ggplot2::aes(col = runname)) +
101117
ggplot2::ylab(paste0("PC2 (",
102-
round(pca_result$sdev[2] / sum(pca_result$sdev) * 100, 1), "%)")) +
118+
round(pca_result$sdev[2]^2 / sum(pca_result$sdev^2) * 100, 1), "%)")) +
103119
ggplot2::xlab(paste0("PC1 (",
104-
round(pca_result$sdev[1] / sum(pca_result$sdev) * 100, 1), "%)")) +
120+
round(pca_result$sdev[1]^2 / sum(pca_result$sdev^2) * 100, 1), "%)")) +
105121
ggplot2::theme_minimal()
106122
}
107123
return(p)
@@ -140,4 +156,4 @@ plotHeatmap <- function(se, count.data, group.variable) {
140156
show_row_names = FALSE, column_names_gp = grid::gpar(fontsize = 9))
141157
}
142158
return(p)
143-
}
159+
}

0 commit comments

Comments
 (0)