annotate testandplot.R @ 54:d49031ef33d5 draft

Uploaded 20170531
author fabio
date Wed, 31 May 2017 16:25:59 -0400
parents 1e677d6b1aaf
children 2bb6b44093ba
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
1 if (require("IWTomics",character.only = TRUE,quietly = FALSE)) {
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
2 args=commandArgs(TRUE)
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
3
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
4 # get args names and values
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
5 args_values=strsplit(args,'=')
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
6 args_names=unlist(lapply(args_values,function(arg) arg[1]))
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
7 names(args_values)=args_names
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
8 args_values=lapply(args_values,function(arg) arg[2])
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
9 # read filenames
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
10 adjustedpvaluematrix=args_values$adjustedpvaluematrix
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
11 iwtomicsrespdf=args_values$iwtomicsrespdf
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
12 iwtomicssumpdf=args_values$iwtomicssumpdf
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
13 regionids=args_values$regionids
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
14 featureids=args_values$featureids
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
15 rdatafile=args_values$rdatafile
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
16 iwtomicsrdata=args_values$iwtomicsrdata
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
17 iwtomicstests=args_values$iwtomicstests
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
18 iwtomicsselectedfeatures=args_values$iwtomicsselectedfeatures
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
19 # read parameters (from region1 on)
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
20 i_region1=which(args_names=='region1')
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
21 for(i in i_region1:length(args)){
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
22 eval(parse(text=args[[i]]))
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
23 }
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
24
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
25 # load RData
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
26 load(rdatafile)
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
27 # read regionids and featureids
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
28 regionids=as.character(read.delim(regionids,header=FALSE,sep='\t',stringsAsFactors=FALSE))
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
29 featureids=as.character(read.delim(featureids,header=FALSE,sep='\t',stringsAsFactors=FALSE))
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
30 # retrieve region1, region2 and features_subset ids and check they are in the RData
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
31 id_region1=regionids[region1]
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
32 id_region2=regionids[region2]
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
33 id_features_subset=featureids[features_subset]
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
34 if(length(setdiff(c(id_region1,id_region2),idRegions(regionsFeatures)))!=0){
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
35 quit(save="no", status=10)
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
36 stop('Wrong region ids')
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
37 }
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
38 if(length(setdiff(id_features_subset,idFeatures(regionsFeatures)))!=0){
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
39 quit(save="no", status=20)
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
40 stop('Wrong feature ids')
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
41 }
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
42 if(sum(duplicated(paste0(id_region1,id_region2)))){
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
43 quit(save="no", status=30)
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
44 stop('Same test repeated multiple times.')
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
45 }
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
46
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
47 # perform test
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
48 tryCatch({
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
49 # fix repeated probs
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
50 if(statistics=='quantile'){
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
51 # fix repeated probs
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
52 testprobs=sort(unique(testprobs))
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
53 }else{
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
54 testprobs=0.5
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
55 }
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
56 regionsFeatures_test=IWTomicsTest(regionsFeatures,id_region1,id_region2,id_features_subset,
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
57 statistics=statistics,probs=testprobs,B=B)
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
58 # create adjustedvaluematrix output
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
59 for(test in seq_along(id_region1)){
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
60 for(id_feature in id_features_subset){
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
61 write(paste0('Test: ',id_region1[test],' vs ',id_region2[test],', on feature ',id_feature),
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
62 file=adjustedpvaluematrix,append=TRUE,sep='\t')
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
63 pval=regionsFeatures_test@test$result[[test]][[id_feature]]$adjusted_pval_matrix
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
64 row.names(pval)=paste('Scale',rev(seq_len(nrow(pval))))
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
65 write.table(pval,file=adjustedpvaluematrix,append=TRUE,sep='\t',quote=FALSE,row.names=TRUE,col.names=FALSE)
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
66 write('',file=adjustedpvaluematrix,append=TRUE,sep='\t')
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
67 }
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
68 }
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
69 }, error = function(err) {
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
70 quit(save="no", status=40) #error testing
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
71 stop(err)
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
72 })
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
73
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
74 # plot test results
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
75 pdf(iwtomicsrespdf,width=5,height=7)
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
76 if(plottype=='boxplot'){
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
77 # fix repeated probs
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
78 probs=sort(unique(probs))
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
79 }else{
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
80 probs=c(0.25,0.5,0.75)
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
81 }
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
82 plotTest(regionsFeatures_test,alpha=testalpha,type=plottype,probs=probs,average=average,size=size,ask=FALSE)
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
83 dev.off()
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
84
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
85 # plot summary results
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
86 if(groupby!='none'){
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
87 pdf(iwtomicssumpdf,width=15,height=10)
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
88 plotSummary(regionsFeatures_test,alpha=summaryalpha,only_significant=only_significant,groupby=groupby,ask=FALSE,append=TRUE)
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
89 dev.off()
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
90 }
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
91
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
92 # create output
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
93 write.table(as.data.frame(t(paste(id_region1,'vs',id_region2))),file=iwtomicstests,quote=FALSE,sep='\t',row.names=FALSE,col.names=FALSE)
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
94 write.table(as.data.frame(t(idFeatures(regionsFeatures_test))),file=iwtomicsselectedfeatures,quote=FALSE,sep='\t',row.names=FALSE,col.names=FALSE)
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
95 save(regionsFeatures_test,file=iwtomicsrdata)
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
96 }else{
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
97 quit(save="no", status=255)
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
98 stop("Missing IWTomics package")
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
99 }