Skip to content

Commit 6c35a55

Browse files
author
Ubuntu
committed
outputing empty pca plots when batch set to False
1 parent d716802 commit 6c35a55

File tree

2 files changed

+59
-16
lines changed

2 files changed

+59
-16
lines changed

src/preprocess/batch_removal.R

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
suppressMessages(library(sva))
22
suppressMessages(library(limma))
33
suppressMessages(library(optparse))
4-
4+
suppressMessages(library(ggplot2))
5+
suppressMessages(library(ggfortify))
6+
suppressMessages(library(RColorBrewer))
7+
suppressMessages(library(ggpubr))
58

69

710
# make option list and parse command line
@@ -20,6 +23,22 @@ option_list <- list(
2023
help="Output files [Required]"))
2124
)
2225

26+
pca_plot <- function(exprTable, annot,title,Batch) {
27+
batch_n <- length(unique(as.character(annot[colnames(exprTable),"Batch"])))
28+
print(paste("there are ", batch_n, " batches in your data"))
29+
df <- cbind.data.frame(t(exprTable),batch = as.character(annot[colnames(exprTable),"Batch"]))
30+
pca_plot <- autoplot(prcomp(t(exprTable)), data = df, col = Batch, size = 1, frame = TRUE, frame.type = 'norm')+
31+
labs(title=title)+
32+
scale_color_manual(values = brewer.pal(name = "Set1", n = 9)[1:batch_n])+
33+
theme_bw()+
34+
theme(plot.title = element_text(hjust=0.5,vjust = 0.5, margin = margin(l=100,r=50,t=10,b=10),face = "bold", colour = "black"),
35+
axis.text.x=element_blank(),
36+
axis.text.y=element_text(size=12,face = "bold",hjust=1),
37+
axis.title.x = element_text(size = 12, face = "bold"),
38+
axis.title.y = element_text(size = 12, face = "bold"))
39+
return(pca_plot)
40+
}
41+
2342

2443
opt_parser <- OptionParser(option_list=option_list);
2544
opts <- parse_args(opt_parser);
@@ -69,19 +88,32 @@ exprZero <- subset(exprZero, !(rownames(exprZero) %in% names(filt_genes)))
6988

7089

7190
if(opts$covariates == "False"){
72-
print('No batches used !')
73-
expr.limma <- expr.dat
74-
} else {
75-
print('Running limma for batch removal')
91+
print('No batches used !')
92+
expr.limma <- expr.dat
93+
expr.limma = rbind(expr.limma,exprZero)
94+
writeDF(ssgsvaFormat(expr.limma),opts$output_after)
95+
96+
} else {
97+
samples$Batch <- samples[,opts$covariates]
98+
print(samples)
99+
100+
print('Running limma for batch removal')
76101
expr.limma = tryCatch(
77102
removeBatchEffect(as.matrix(expr.dat),
78-
samples$opts$covariates),
103+
samples$Batch),
79104
error = function(e){
80105
print(e)
81106
})
82-
}
83-
print(head(expr.limma))
84-
expr.limma = rbind(expr.limma,exprZero)
85-
writeDF(ssgsvaFormat(expr.limma),opts$output_after)
107+
expr.limma = rbind(expr.limma,exprZero)
108+
writeDF(ssgsvaFormat(expr.limma),opts$output_after)
109+
110+
#png(file = paste(opts$out_before,'.png',sep=''), res = 300, height = 1200, width = 1500)
111+
#pca_plot(expr.dat, annot = samples, title = "PCA plot Before Batch Removal",Batch=Batch)
112+
#dev.off()
113+
114+
#png(file = paste(opt$out_after,'.png',sep=''), res = 300, height = 1200, width = 1500)
115+
#pca_plot(expr.limma, annot = samples, title = "PCA plot Before Batch Removal",Batch=Batch)
116+
#dev.off()
86117

118+
}
87119

src/preprocess/pca.R

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,23 @@ pca_plot <- function(exprTable, annot,title, Batch) {
5858

5959

6060
###print pca plot
61-
png(file = opt$out_before, res = 300, height = 1200, width = 1500)
62-
pca_plot(exprTable = expr_before, annot = annot, title = "PCA plot Before Batch Removal",Batch=Batch)
63-
dev.off()
6461

65-
png(file = opt$out_after, res = 300, height = 1200, width = 1500)
66-
pca_plot(exprTable = expr_after, annot = annot, title = "PCA plot After Batch Removal",Batch = Batch)
67-
dev.off()
62+
if (opt$column != "False") {
63+
png(file = opt$out_before, res = 300, height = 1200, width = 1500)
64+
pca_plot(exprTable = expr_before, annot = annot, title = "PCA plot Before Batch Removal",Batch=Batch)
65+
dev.off()
6866

67+
png(file = opt$out_after, res = 300, height = 1200, width = 1500)
68+
pca_plot(exprTable = expr_after, annot = annot, title = "PCA plot After Batch Removal",Batch = Batch)
69+
dev.off()
70+
} else
71+
{
72+
png(file = opt$out_before, res = 300, height = 1200, width = 1500)
73+
plot.new()
74+
dev.off()
75+
76+
png(file = opt$out_after, res = 300, height = 1200, width = 1500)
77+
plot.new()
78+
dev.off()
79+
}
6980
###

0 commit comments

Comments
 (0)