1
1
suppressMessages(library(sva ))
2
2
suppressMessages(library(limma ))
3
3
suppressMessages(library(optparse ))
4
-
4
+ suppressMessages(library(ggplot2 ))
5
+ suppressMessages(library(ggfortify ))
6
+ suppressMessages(library(RColorBrewer ))
7
+ suppressMessages(library(ggpubr ))
5
8
6
9
7
10
# make option list and parse command line
@@ -20,6 +23,22 @@ option_list <- list(
20
23
help = " Output files [Required]" ))
21
24
)
22
25
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
+
23
42
24
43
opt_parser <- OptionParser(option_list = option_list );
25
44
opts <- parse_args(opt_parser );
@@ -69,19 +88,32 @@ exprZero <- subset(exprZero, !(rownames(exprZero) %in% names(filt_genes)))
69
88
70
89
71
90
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' )
76
101
expr.limma = tryCatch(
77
102
removeBatchEffect(as.matrix(expr.dat ),
78
- samples $ opts $ covariates ),
103
+ samples $ Batch ),
79
104
error = function (e ){
80
105
print(e )
81
106
})
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()
86
117
118
+ }
87
119
0 commit comments