Commit f27f3ab7 authored by Philippe BORDRON's avatar Philippe BORDRON
Browse files

auto adjust legend size on heatmap

parent 8d292bea
......@@ -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=""))
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