Commit d855bf73 authored by Eric CHARPENTIER's avatar Eric CHARPENTIER 🐍
Browse files

Merge branch 'hovering-pca' into 'master'

Hovering pca

See merge request !29
parents 83131de9 51b8c2d9
#!/usr/bin/env RScript
library(ggplot2)
library(ggiraph)
library(grid)
library(optparse)
......@@ -49,9 +50,10 @@ ACP<-function(d,transpose=T,scale=F,center=T) {
acp2d<-function(pca, comp=1:2, group=NULL, plotVars = FALSE, pointSize=2, plotText=FALSE, fixedCoord=FALSE, main=NULL, ellipse=FALSE, color=NULL){
if(!require("ggplot2")) stop("You must install ggplot2");
if(!require("ggiraph")) stop("You must install ggiraph");
if(length(comp)!=2) stop("You must give a vector of 2 integer for comp parameter");
percentVar <- pca$percentVar
functPlot<-ifelse(plotText,geom_text,geom_point)
functPlot<-ifelse(plotText, geom_text_interactive, geom_point_interactive)
coor=ifelse(plotVars,"rotation","x")
ngroup <- 0
if(is.null(group)){
......@@ -76,12 +78,14 @@ acp2d<-function(pca, comp=1:2, group=NULL, plotVars = FALSE, pointSize=2, plotTe
legend.text = element_text(size = legend_text_size) # manage legend text size according to number of conditions
)
graph <- graph + theme
graph <- graph + guides(col = guide_legend(
graph <- graph + guides(col = guide_legend_interactive(
ncol = ifelse(ngroup == 0, NULL, ceiling(ngroup/24)), # limit the number of columns to keep the plot large enough; maybe take legend_text_size
override.aes = list(size=legend_text_size/(2*pointSize)) # adjust dot size into legend
keywidth=unit(round(legend_text_size/(2*pointSize)) + 8, "points"), # adjust dot box size into legend for ggiraph
keyheight=unit(round(legend_text_size/(2*pointSize)) + 8, "points"), # ggiraph do not use point as std unit
override.aes = list(size=legend_text_size/(2*pointSize)) # adjust dot size into legend
))
graph <- graph + functPlot(size=pointSize)+
graph <- graph + functPlot(size=pointSize, tooltip = rownames(d)) +
xlab(paste0("PC",comp[1],": ",round(percentVar[comp[1]] * 100),"% variance")) +
ylab(paste0("PC",comp[2],": ",round(percentVar[comp[2]] * 100),"% variance"))
if(fixedCoord)graph <- graph + coord_fixed(ratio=percentVar[comp[2]]/percentVar[comp[1]])
......@@ -165,6 +169,7 @@ compo=length(which(acpT$percentVar>0.05))+1
exprDatT<-exprDatT[,c(rownames(sampleAnnot))]
pdf(file = paste(outputDir,"/NormAndPCA.pdf",sep=""),width=10,height=10)
acpTa <- NULL
if(batchEffect || plateBatch){
......@@ -197,9 +202,12 @@ if(batchEffect || plateBatch){
acp2d(acpTa,comp=c(1,2),plotText = TRUE,group = sampleAnnot[,batchEffectCol],main="PCA on normalized/transformed/adjusted data")
}
p <- acp2d(acpTa,comp=c(1,2),group = sampleAnnot[,condCol],main="PCA on normalized/transformed/adjusted data")
htmlwidgets::saveWidget(girafe(ggobj = p, pointsize = 12, width_svg = 6, height_svg = 4), paste(outputDir, "PCA.html", sep="/"))
png(paste(outputDir,"/PCA.png",sep=""),width=15,height=10,units="cm",res=300,pointsize=6)
acp2d(acpTa,comp=c(1,2),group = sampleAnnot[,condCol],main="PCA on normalized/transformed/adjusted data")
dev.off()
print(p)
dev.off()
plotAllACP(compo,acpTa,sampleAnnot,condCol)
......@@ -210,12 +218,16 @@ if(batchEffect || plateBatch){
barplot(acpT$percentVar*100,names.arg = round(acpT$percentVar*100,2),main = "Contribution of each component in the PCA")
plotAllACP(compo,acpT,sampleAnnot,condCol)
p <- acp2d(acpT,comp=c(1,2),group = sampleAnnot[,condCol],main="PCA on normalized/transformed data")
htmlwidgets::saveWidget(girafe(ggobj = p, pointsize = 12, width_svg = 6, height_svg = 4), paste(outputDir, "PCA.html", sep="/"))
png(paste(outputDir,"/PCA.png",sep=""),width=15,height=10,units="cm",res=300,pointsize=6)
acp2d(acpT,comp=c(1,2),group = sampleAnnot[,condCol],main="PCA on normalized/transformed data")
print(p)
dev.off()
}
dev.off()
save(acpT, acpTa, sampleAnnot, file=paste(outputDir, "/PCA.RData", sep=""))
......@@ -205,7 +205,10 @@
<h2 id="ACP">PCA</h2>
</header>
<p>The Principal Component Analysis groups samples according to their transcriptomic profiles. All conditions in the project are shown here.</p>
<img src="./{{config.de_folder}}/PCA.png" style="width:100%">
<div id="PCA" class="content" style="--iframe-aspect-ratio: 18/12; width: 100%;">
<iframe src="./{{config.de_folder}}/PCA.html" width="18" height="12"></iframe><br/>
</div>
<!-- <img src="./{{config.de_folder}}/PCA.png" style="width:100%"> -->
</br></br>
<a href="./{{config.de_folder}}/NormAndPCA.pdf" target="_blank">View full PCA report</a>
<HR align="center" size="6" width="100%">
......
......@@ -193,7 +193,10 @@
<h2 id="ACP">PCA</h2>
</header>
<p>The Principal Component Analysis groups samples according to their transcriptomic profiles. All conditions in the project are shown here.</p>
<img src="./{{config.de_folder}}/PCA.png" style="width:100%">
<div id="PCA" class="content" style="--iframe-aspect-ratio: 18/12; width: 100%;">
<iframe src="./{{config.de_folder}}/PCA.html" width="18" height="12"></iframe><br/>
</div>
<!-- <img src="./{{config.de_folder}}/PCA.png" style="width:100%"> -->
</br></br>
<a href="./{{config.de_folder}}/NormAndPCA.pdf" target="_blank">View full PCA report</a>
<HR align="center" size="6" width="100%">
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment