comparison Analyse.R @ 0:1024245abc70 draft

planemo upload for repository https://github.com/sblanck/smagexp/tree/master/smagexp_tools commit 5974f806f344dbcc384b931492d7f023bfbbe03b
author sblanck
date Thu, 22 Feb 2018 08:38:22 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:1024245abc70
1 #!/usr/bin/env Rscript
2 # setup R error handling to go to stderr
3 options( show.error.messages=F, error = function () { cat( geterrmessage(), file=stderr() ); q( "no", 1, F ) } )
4
5 # we need that to not crash galaxy with an UTF8 error on German LC settings.
6 loc <- Sys.setlocale("LC_MESSAGES", "en_US.UTF-8")
7
8 library("optparse")
9
10 ##### Read options
11 option_list=list(
12 make_option("--rdatainput",type="character",default="NULL",help="rdata object containing eset object"),
13 make_option("--conditions",type="character",default=NULL,help="Text file summarizing conditions of the experiment (required)"),
14 make_option("--selectcondition1",type="character",default=NULL,help="log2 transformation"),
15 # make_option("--condition1",type="character",default=NULL,help="A table containing the expression data"),
16 make_option("--selectcondition2",type="character",default="NULL",help="rdata object containing eset object"),
17 # make_option("--condition2",type="character",default=NULL,help="Text file summarizing conditions of the experiment (required)"),
18 make_option("--nbresult",type="character",default=NULL,help="number of result displayed results"),
19 make_option("--rdataoutput",type="character",default="NULL",help="output rdata object containing eset object"),
20 make_option("--htmloutput",type="character",default=NULL,help="Output html report"),
21 make_option("--htmloutputpath",type="character",default="NULL",help="Path of output html report"),
22 make_option("--tabularoutput",type="character",default=NULL,help="Output text file"),
23 make_option("--htmltemplate",type="character",default=NULL,help="html template)"),
24 make_option("--tooldirectory",type="character",default=NULL,help="tool directory)")
25
26
27 );
28
29 opt_parser = OptionParser(option_list=option_list);
30 opt = parse_args(opt_parser);
31
32 if(is.null(opt$rdatainput)){
33 print_help(opt_parser)
34 stop("rData input required.", call.=FALSE)
35 }
36
37 if(is.null(opt$conditions)){
38 print_help(opt_parser)
39 stop("conditions input required.", call.=FALSE)
40 }
41
42
43 #loading libraries
44 suppressPackageStartupMessages(require(GEOquery))
45 suppressPackageStartupMessages(require(Biobase))
46 suppressPackageStartupMessages(require(GEOquery))
47 suppressPackageStartupMessages(require(GEOmetadb))
48 suppressPackageStartupMessages(require(limma))
49 suppressPackageStartupMessages(require(jsonlite))
50 suppressPackageStartupMessages(require(affy))
51 suppressPackageStartupMessages(require(dplyr))
52
53 load(opt$rdatainput)
54 targetFile=opt$conditions
55 condition1Name=opt$selectcondition1
56 #condition1=opt$condition1
57 condition2Name=opt$selectcondition2
58 #condition2=opt$condition2
59 nbresult=opt$nbresult
60 result_export_eset=opt$rdataoutput
61 result=opt$htmloutput
62 result.path=opt$htmloutputpath
63 result.tabular=opt$tabularoutput
64 result.template=opt$htmltemplate
65 tooldirectory=opt$tooldirectory
66
67 targets <- read.table(targetFile,sep="\t",stringsAsFactors=FALSE)
68
69 #condition1_tmp <- strsplit(condition1,",")
70 condition1 <-targets[which(targets$V2==condition1Name),1]
71
72 #condition2_tmp <- strsplit(condition2,",")
73 #condition2<-unlist(condition2_tmp)
74 condition2 <-targets[which(targets$V2==condition2Name),1]
75
76 conditions=c(condition1,condition2)
77
78 dir.create(result.path, showWarnings = TRUE, recursive = FALSE)
79
80 eset=eset[,which(rownames(eset@phenoData@data) %in% conditions)]
81
82 eset@phenoData@data$source_name_ch1=""
83 eset@phenoData@data$source_name_ch1[which(rownames(eset@phenoData@data) %in% condition1)]=condition1Name
84 eset@phenoData@data$source_name_ch1[which(rownames(eset@phenoData@data) %in% condition2)]=condition2Name
85
86 condNames=paste0("G",as.numeric(as.character(pData(eset)["source_name_ch1"][,1])!=condition1Name))
87
88 f <- as.factor(condNames)
89
90 design <- model.matrix(~ 0+f)
91
92 colnames(design) <- levels(f)
93
94 fit <- lmFit(eset, design)
95
96 cont.matrix <- makeContrasts(G0-G1, levels=design)
97
98 fit2 <- contrasts.fit(fit, cont.matrix)
99 fit2 <- eBayes(fit2)
100
101 tT <- topTable(fit2, adjust="fdr", sort.by="B", number=nbresult)
102
103 #head(exprs(eset))
104
105 gpl <- annotation(eset)
106 if (substr(x = gpl,1,3)!="GPL"){
107 #if the annotation info does not start with "GPL" we retrieve the corresponding GPL annotation
108 mapping=read.csv(paste0(tooldirectory,"/gplToBioc.csv"),stringsAsFactors=FALSE)
109 gpl=mapping[which(mapping$bioc_package==annotation(eset)),]$gpl
110 gpl=gpl[1]
111
112 annotation(eset)=gpl
113
114 platf <- getGEO(gpl, AnnotGPL=TRUE)
115 ncbifd <- data.frame(attr(dataTable(platf), "table"))
116
117 fData(eset)["ID"]=row.names(fData(eset))
118 fData(eset)=merge(x=fData(eset),y=ncbifd,all.x = TRUE, by = "ID")
119 colnames(fData(eset))[4]="ENTREZ_GENE_ID"
120 row.names(fData(eset))=fData(eset)[,"ID"]
121
122 tT <- add_rownames(tT, "ID")
123
124 } else {
125
126 gpl <- annotation(eset)
127 platf <- getGEO(gpl, AnnotGPL=TRUE)
128 ncbifd <- data.frame(attr(dataTable(platf), "table"))
129
130 if (!("ID" %in% colnames(tT))){
131 tT <- add_rownames(tT, "ID")}
132
133 }
134
135 tT <- merge(tT, ncbifd, by="ID")
136 tT <- tT[order(tT$P.Value), ]
137 tT <- subset(tT, select=c("Platform_SPOTID","ID","adj.P.Val","P.Value","t","B","logFC","Gene.symbol","Gene.title","Gene.ID","Chromosome.annotation","GO.Function.ID"))
138 tT<-format(tT, digits=2, nsmall=2)
139 head(tT)
140 colnames(tT)=gsub(pattern = "\\.",replacement = "_",colnames(tT))
141 matrixtT=as.matrix(tT)
142 datajson=toJSON(matrixtT,pretty = TRUE)
143
144 htmlfile=readChar(result.template, file.info(result.template)$size)
145 htmlfile=gsub(x=htmlfile,pattern = "###DATAJSON###",replacement = datajson, fixed = TRUE)
146 dir.create(result.path, showWarnings = TRUE, recursive = FALSE)
147
148 boxplot="boxplot.png"
149 png(boxplot,width=800,height = 400)
150 par(mar=c(7,5,1,1))
151 boxplot(exprs(eset),las=2,outline=FALSE)
152 dev.off()
153 htmlfile=gsub(x=htmlfile,pattern = "###BOXPLOT###",replacement = boxplot, fixed = TRUE)
154 file.copy(boxplot,result.path)
155
156 histopvalue="histopvalue.png"
157
158 png(histopvalue,width=800,height = 400)
159 par(mfrow=c(1,2))
160 hist(fit2$F.p.value,nclass=100,main="Histogram of p-values", xlab="p-values",ylab="frequency")
161 volcanoplot(fit2,coef=1,highlight=10,main="Volcano plot")
162 htmlfile=gsub(x=htmlfile,pattern = "###HIST###",replacement = histopvalue, fixed = TRUE)
163 dev.off()
164 file.copy(histopvalue,result.path)
165
166 saveConditions=c(condition1Name,condition2Name)
167 save(eset,saveConditions,file=result_export_eset)
168 write.table(x=tT[,-1],file=result.tabular,quote=FALSE,row.names=FALSE,col.names=TRUE,sep="\t")
169 write(htmlfile,result)
170