annotate iwtomics-1.0/testandplot.R @ 82:8e397b62df4c draft

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