Commit 83131de9 authored by Eric CHARPENTIER's avatar Eric CHARPENTIER 🐍
Browse files

Merge branch 'display-much-conditions' into 'master'

Display much conditions

See merge request !28
parents e587e0d8 208c8b30
......@@ -44,7 +44,7 @@ ecrire<-function(x,file="default.tsv",headRow="Name"){
}
# specific function
unsupervisedClustering<-function(x,transpose=TRUE,method.dist="pearson",method.hclust="ward.D2",bootstrap=FALSE,nboot=10){
unsupervisedClustering<-function(x, transpose=TRUE, method.dist="pearson", method.hclust="ward.D2", bootstrap=FALSE, nboot=10){
if(transpose) x<-t(x)
if(bootstrap){
require(pvclust)
......@@ -92,49 +92,92 @@ plateBatch<-FALSE; if(plateBatchCol %in% colnames(sampleAnnot)) plateBatch<-TRUE
if(batchEffect || plateBatch){
exprDatT=lire(paste(outputDir,"/exprTransformedAdjusted.tsv",sep=""))
}else{
} else {
exprDatT=lire(exprDatTFile)
}
annot<-sampleAnnot
corSample<-cor(exprDatT,method = "pearson")
clustSamples<-unsupervisedClustering(corSample,bootstrap = bootstrap,nboot=nboot,method.dist = "euclidean")
corSample <- cor(exprDatT, method = "pearson")
clustSamples <- unsupervisedClustering(corSample,
bootstrap = bootstrap,
nboot = nboot,
method.dist = "euclidean")
colTopAnnot<-vector("list", ncol(annot))
names(colTopAnnot)<-colnames(annot)
colFun<-c(ggplotColours,rainbow,topo.colors)
i<-1
colTopAnnot <- vector("list", ncol(annot))
names(colTopAnnot) <- colnames(annot)
colFun <- c(ggplotColours, rainbow, topo.colors)
i <- 1
for(col in colnames(annot)){
colTopAnnot[[col]]<-colFun[[i]](nlevels(annot[,col]))
names(colTopAnnot[[col]])<-levels(annot[,col])
i<-i+1
if(i==4) i<-1
colTopAnnot[[col]] <- colFun[[i]](nlevels(annot[,col]))
names(colTopAnnot[[col]]) <- levels(annot[,col])
i <- i+1
if(i==4) i <- 1
}
ha<-HeatmapAnnotation(df = annot, col = colTopAnnot)
annot_legend_params = vector("list", ncol(annot)) #Calcule automatiquement la taille de police selon le nombre de colonnes ou lignes (empirique)
names(annot_legend_params) <- colnames(annot)
default_ft <- 10
for(col in colnames(annot)){
ftsize <- min(default_ft, default_ft*40/nlevels(annot[,col]))
if (ftsize < default_ft){
annot_legend_params[[col]] <- list(labels_gp = gpar(fontsize = ftsize),
grid_height = unit(4*ftsize/default_ft, "mm"), grid_width = unit(4*ftsize/default_ft, "mm")
)
} else {
annot_legend_params[[col]] <- list(labels_gp = gpar(fontsize = ftsize))
}
}
Ht<-Heatmap(matrix = corSample, cluster_rows = clustSamples, cluster_columns = clustSamples, top_annotation = ha,name="Pearson\ncorrelation",row_names_gp = autoGparFontSizeMatrix(ncol(corSample)),column_names_gp = autoGparFontSizeMatrix(ncol(corSample)),row_dend_reorder = FALSE, column_dend_reorder = FALSE,col=heat.colors(100))
Htc<-Heatmap(absSamples[clustSamples$labels,"TotalCount"], name = "Total\nexpression",col=colorRamp2(c(0,max(absSamples$TotalCount)),c("black","green")), show_row_names = FALSE, width = unit(5, "mm"))
ha<-HeatmapAnnotation(df = annot, col = colTopAnnot, annotation_legend_param = annot_legend_params)
pdf(file = paste(outputDir,"/HeatmapCorPearson.pdf",sep=""),width=10,height=9)
Ht<-Heatmap(matrix = corSample,
cluster_rows = clustSamples,
cluster_columns = clustSamples,
top_annotation = ha,
name = "Pearson\ncorrelation",
row_names_gp = autoGparFontSizeMatrix(ncol(corSample)),
column_names_gp = autoGparFontSizeMatrix(ncol(corSample)),
row_dend_reorder = FALSE,
column_dend_reorder = FALSE,
col = heat.colors(100))
Htc<-Heatmap(absSamples[clustSamples$labels,"TotalCount"],
name = "Total\nexpression",
col=colorRamp2(c(0, max(absSamples$TotalCount)), c("black", "green")),
show_row_names = FALSE,
width = unit(5, "mm"))
pdf(file = paste(outputDir,"/HeatmapCorPearson.pdf", sep=""), width=10, height=9)
print(Ht+Htc)
if(batchEffect || plateBatch){
#il y a eu une correction batch
exprDat=lire(paste(outputDir,"/exprTransformed.tsv",sep=""))
corSampleUnadjust<-cor(exprDat,method = "pearson")
clustSamplesUnadjust<-unsupervisedClustering(corSampleUnadjust,nboot=nboot,bootstrap = bootstrap,method.dist = "euclidean")
Htna<-Heatmap(matrix = corSampleUnadjust, cluster_rows = clustSamplesUnadjust, cluster_columns = clustSamplesUnadjust, top_annotation = ha,name="Pearson\ncorrelation\nnon adjusted",row_names_gp = autoGparFontSizeMatrix(ncol(corSampleUnadjust)),column_names_gp = autoGparFontSizeMatrix(ncol(corSampleUnadjust)),row_dend_reorder = FALSE, column_dend_reorder = FALSE,col=heat.colors(100))
corSampleUnadjust <- cor(exprDat,method = "pearson")
clustSamplesUnadjust <- unsupervisedClustering(corSampleUnadjust,
nboot = nboot,
bootstrap = bootstrap,
method.dist = "euclidean")
Htna<-Heatmap(matrix = corSampleUnadjust,
cluster_rows = clustSamplesUnadjust,
cluster_columns = clustSamplesUnadjust,
top_annotation = ha,
name = "Pearson\ncorrelation\nnon adjusted",
row_names_gp = autoGparFontSizeMatrix(ncol(corSampleUnadjust)),
column_names_gp = autoGparFontSizeMatrix(ncol(corSampleUnadjust)),
row_dend_reorder = FALSE,
column_dend_reorder = FALSE,
col = heat.colors(100))
print(Htna+Htc)
}
dev.off()
png(paste(outputDir,"/HeatmapCorPearson.png",sep=""),width=25,height=22,units="cm",res=600)
png(paste(outputDir,"/HeatmapCorPearson.png",sep=""), width=25, height=22, units="cm", res=600)
print(Ht+Htc)
dev.off()
save(ha,batchEffect,plateBatch,sampleAnnot,file=paste(outputDir,"/heatmap.RData",sep=""))
save(ha, batchEffect, plateBatch, sampleAnnot, file=paste(outputDir, "/heatmap.RData", sep=""))
......@@ -47,23 +47,41 @@ ACP<-function(d,transpose=T,scale=F,center=T) {
return(resacp);
}
acp2d<-function(pca, comp=1:2,group=NULL, plotVars = FALSE, pointSize=2, plotText=FALSE,fixedCoord=FALSE,main=NULL,ellipse=FALSE,color=NULL){
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(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)
coor=ifelse(plotVars,"rotation","x")
ngroup <- 0
if(is.null(group)){
d <- data.frame(PC1=pca[[coor]][,comp[1]], PC2=pca[[coor]][,comp[2]]);
graph<-ggplot(data=d, mapping = aes(x=PC1, y=PC2, label = rownames(d)))
}else{
d <- data.frame(PC1=pca[[coor]][,comp[1]], PC2=pca[[coor]][,comp[2]], group=group);
graph<-ggplot(data=d, mapping = aes(x=PC1, y=PC2,colour=group, label = rownames(d)))
graph<-ggplot(data=d, mapping = aes(x=PC1, y=PC2, colour=group, label = rownames(d)))
ngroup <- length(unique(group))
}
theme<-theme(panel.background = element_blank(),panel.border=element_rect(fill=NA),panel.grid.major = element_blank(),panel.grid.minor = element_blank(),strip.background=element_blank(),axis.text.x=element_text(colour="black"),axis.text.y=element_text(colour="black"),axis.ticks=element_line(colour="black"),plot.margin=unit(c(1,1,1,1),"line"))
graph<-graph+theme
graph<-graph+functPlot(size=pointSize)+
default_legend_text_size <- 8
legend_text_size <- ifelse(is.null(group), default_legend_text_size, max(2,min(default_legend_text_size, round(default_legend_text_size*32/ngroup))))
theme <- theme(panel.background = element_blank(),
panel.border=element_rect(fill=NA),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
strip.background=element_blank(),
axis.text.x=element_text(colour="black"),
axis.text.y=element_text(colour="black"),
axis.ticks=element_line(colour="black"),
plot.margin=unit(c(1,1,1,1),"line"),
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(
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
))
graph <- graph + functPlot(size=pointSize)+
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]])
......@@ -96,7 +114,7 @@ plotDistrib<-function(data,type="boxplot",conditions=NULL,main=NULL,conditionNam
graph<-ggplot(data = tabGraph,mapping = aes_string("sample","val",color=conditionName))
}
graph<-graph+theme(axis.text.x = element_text(angle = 90, hjust = 1,vjust=.3))
graph<-graph+theme(axis.text.x = element_text(angle = 90, hjust = 1,vjust=.3))
if(type=="violin") graph<-graph+geom_violin()
if(type=="boxplot") graph<-graph+geom_boxplot()
if(! is.null(main)) graph<-graph+ggtitle(main)
......@@ -108,9 +126,20 @@ plotAllACP<-function(compo=4,acp,sampleAnnot,condCol,main="PCA"){
for(i in 1:(compo-1)){
for(j in (i+1):compo){
if(i == 1 & j ==2){
acp2d(acp,group = sampleAnnot[,condCol],plotText = TRUE,pointSize = 2,comp = c(i,j),main="PCA",fixedCoord = F)
acp2d(acp,
group = sampleAnnot[,condCol],
plotText = TRUE,
pointSize = 2,
comp = c(i,j),
main="PCA",
fixedCoord = F)
}
acp2d(acp,group = sampleAnnot[,condCol],pointSize = 2,comp = c(i,j),main="PCA",fixedCoord = F)
acp2d(acp,
group = sampleAnnot[,condCol],
pointSize = 2,
comp = c(i,j),
main="PCA",
fixedCoord = F)
#acp2d(acp,pointSize = 2,comp = c(i,j),plotVars = TRUE, plotText = TRUE,fixedCoord = F)
}
}
......
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