annotate plotwithscale.R @ 89:e089b19da14c draft

Uploaded 20180221
author fabio
date Wed, 21 Feb 2018 11:07:29 -0500
parents f81d72e482cf
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
83
f81d72e482cf Uploaded
fabio
parents:
diff changeset
1 if (require("IWTomics",character.only = TRUE,quietly = FALSE)) {
f81d72e482cf Uploaded
fabio
parents:
diff changeset
2 args=commandArgs(TRUE)
f81d72e482cf Uploaded
fabio
parents:
diff changeset
3
f81d72e482cf Uploaded
fabio
parents:
diff changeset
4 # get args names and values
f81d72e482cf Uploaded
fabio
parents:
diff changeset
5 args_values=strsplit(args,'=')
f81d72e482cf Uploaded
fabio
parents:
diff changeset
6 args_names=unlist(lapply(args_values,function(arg) arg[1]))
f81d72e482cf Uploaded
fabio
parents:
diff changeset
7 names(args_values)=args_names
f81d72e482cf Uploaded
fabio
parents:
diff changeset
8 args_values=lapply(args_values,function(arg) arg[2])
f81d72e482cf Uploaded
fabio
parents:
diff changeset
9 # read filenames
f81d72e482cf Uploaded
fabio
parents:
diff changeset
10 adjustedpvalue=args_values$adjustedpvalue
f81d72e482cf Uploaded
fabio
parents:
diff changeset
11 iwtomicsrespdf=args_values$iwtomicsrespdf
f81d72e482cf Uploaded
fabio
parents:
diff changeset
12 iwtomicssumpdf=args_values$iwtomicssumpdf
f81d72e482cf Uploaded
fabio
parents:
diff changeset
13 iwtomicsrdata=args_values$iwtomicsrdata
f81d72e482cf Uploaded
fabio
parents:
diff changeset
14 iwtomicstests=args_values$iwtomicstests
f81d72e482cf Uploaded
fabio
parents:
diff changeset
15 iwtomicsselectedfeatures=args_values$iwtomicsselectedfeatures
f81d72e482cf Uploaded
fabio
parents:
diff changeset
16 test_subset=paste0('c(',strsplit(args_values$test_subset,'\\|')[[1]],')')
f81d72e482cf Uploaded
fabio
parents:
diff changeset
17 feature_subset=paste0('c(',strsplit(args_values$feature_subset,'\\|')[[1]],')')
f81d72e482cf Uploaded
fabio
parents:
diff changeset
18 # read parameters (from test_subset on)
f81d72e482cf Uploaded
fabio
parents:
diff changeset
19 i_scale_subset=which(args_names=='scale_subset')
f81d72e482cf Uploaded
fabio
parents:
diff changeset
20 for(i in i_scale_subset:length(args)){
f81d72e482cf Uploaded
fabio
parents:
diff changeset
21 eval(parse(text=args[[i]]))
f81d72e482cf Uploaded
fabio
parents:
diff changeset
22 }
f81d72e482cf Uploaded
fabio
parents:
diff changeset
23
f81d72e482cf Uploaded
fabio
parents:
diff changeset
24 # load RData
f81d72e482cf Uploaded
fabio
parents:
diff changeset
25 load(iwtomicsrdata)
f81d72e482cf Uploaded
fabio
parents:
diff changeset
26 # read testids and featureids and check them
f81d72e482cf Uploaded
fabio
parents:
diff changeset
27 unlisted=lapply(seq_along(test_subset),
f81d72e482cf Uploaded
fabio
parents:
diff changeset
28 function(i){
f81d72e482cf Uploaded
fabio
parents:
diff changeset
29 test_subset_i=eval(parse(text=test_subset[i]))
f81d72e482cf Uploaded
fabio
parents:
diff changeset
30 feature_subset_i=eval(parse(text=feature_subset[i]))
f81d72e482cf Uploaded
fabio
parents:
diff changeset
31 test_subset_i=rep(test_subset_i,each=length(feature_subset_i))
f81d72e482cf Uploaded
fabio
parents:
diff changeset
32 feature_subset_i=rep(feature_subset_i,length.out=length(test_subset_i))
f81d72e482cf Uploaded
fabio
parents:
diff changeset
33 scale_subset_i=rep(scale_subset[i],length(test_subset_i))
f81d72e482cf Uploaded
fabio
parents:
diff changeset
34 return(list(test_subset=test_subset_i,feature_subset=feature_subset_i,scale_subset=scale_subset_i))
f81d72e482cf Uploaded
fabio
parents:
diff changeset
35 })
f81d72e482cf Uploaded
fabio
parents:
diff changeset
36 test_subset=unlist(lapply(unlisted,function(l) l$test_subset))
f81d72e482cf Uploaded
fabio
parents:
diff changeset
37 feature_subset=unlist(lapply(unlisted,function(l) l$feature_subset))
f81d72e482cf Uploaded
fabio
parents:
diff changeset
38 scale_subset=unlist(lapply(unlisted,function(l) l$scale_subset))
f81d72e482cf Uploaded
fabio
parents:
diff changeset
39 testids=as.character(read.delim(iwtomicstests,header=FALSE,sep='\t',stringsAsFactors=FALSE))
f81d72e482cf Uploaded
fabio
parents:
diff changeset
40 featureids=as.character(read.delim(iwtomicsselectedfeatures,header=FALSE,sep='\t',stringsAsFactors=FALSE))
f81d72e482cf Uploaded
fabio
parents:
diff changeset
41 id_features_subset=featureids[feature_subset]
f81d72e482cf Uploaded
fabio
parents:
diff changeset
42 if(sum(testids!=paste(testInput(regionsFeatures_test)$id_region1,'vs',testInput(regionsFeatures_test)$id_region2))){
f81d72e482cf Uploaded
fabio
parents:
diff changeset
43 write("Wrong test ids.", stderr())
f81d72e482cf Uploaded
fabio
parents:
diff changeset
44 quit(save="no", status=10)
f81d72e482cf Uploaded
fabio
parents:
diff changeset
45 }
f81d72e482cf Uploaded
fabio
parents:
diff changeset
46 if(sum(featureids!=idFeatures(regionsFeatures_test))){
f81d72e482cf Uploaded
fabio
parents:
diff changeset
47 write("Wrong feature ids.", stderr())
f81d72e482cf Uploaded
fabio
parents:
diff changeset
48 quit(save="no", status=20)
f81d72e482cf Uploaded
fabio
parents:
diff changeset
49 }
f81d72e482cf Uploaded
fabio
parents:
diff changeset
50 # retrieve test and features_subset ids
f81d72e482cf Uploaded
fabio
parents:
diff changeset
51 id_features_subset=featureids[feature_subset]
f81d72e482cf Uploaded
fabio
parents:
diff changeset
52 if(sum(duplicated(paste0(test_subset,id_features_subset)))){
f81d72e482cf Uploaded
fabio
parents:
diff changeset
53 write("Two scale thresholds selected for the same test and feature.", stderr())
f81d72e482cf Uploaded
fabio
parents:
diff changeset
54 quit(save="no", status=30)
f81d72e482cf Uploaded
fabio
parents:
diff changeset
55 }
f81d72e482cf Uploaded
fabio
parents:
diff changeset
56 # If scale_subset=0, do not change the threshold
f81d72e482cf Uploaded
fabio
parents:
diff changeset
57 default=(scale_subset==0)
f81d72e482cf Uploaded
fabio
parents:
diff changeset
58 scale_subset=scale_subset[!default]
f81d72e482cf Uploaded
fabio
parents:
diff changeset
59 test_subset=test_subset[!default]
f81d72e482cf Uploaded
fabio
parents:
diff changeset
60 id_features_subset=id_features_subset[!default]
f81d72e482cf Uploaded
fabio
parents:
diff changeset
61
f81d72e482cf Uploaded
fabio
parents:
diff changeset
62 # get scale threshold
f81d72e482cf Uploaded
fabio
parents:
diff changeset
63 scale_threshold=lapply(regionsFeatures_test@test$result,
f81d72e482cf Uploaded
fabio
parents:
diff changeset
64 function(result) unlist(lapply(result,function(feature) feature$max_scale)))
f81d72e482cf Uploaded
fabio
parents:
diff changeset
65 for(i in seq_along(test_subset)){
f81d72e482cf Uploaded
fabio
parents:
diff changeset
66 if(scale_threshold[[test_subset[i]]][id_features_subset[i]]<scale_subset[i]){
f81d72e482cf Uploaded
fabio
parents:
diff changeset
67 write("Scale threshold too high.", stderr())
f81d72e482cf Uploaded
fabio
parents:
diff changeset
68 quit(save="no", status=40)
f81d72e482cf Uploaded
fabio
parents:
diff changeset
69 }
f81d72e482cf Uploaded
fabio
parents:
diff changeset
70 scale_threshold[[test_subset[i]]][id_features_subset[i]]=scale_subset[i]
f81d72e482cf Uploaded
fabio
parents:
diff changeset
71 }
f81d72e482cf Uploaded
fabio
parents:
diff changeset
72
f81d72e482cf Uploaded
fabio
parents:
diff changeset
73 # create adjustedvalue output
f81d72e482cf Uploaded
fabio
parents:
diff changeset
74 pval=adjusted_pval(regionsFeatures_test,scale_threshold=scale_threshold)
f81d72e482cf Uploaded
fabio
parents:
diff changeset
75 for(test in seq_along(pval)){
f81d72e482cf Uploaded
fabio
parents:
diff changeset
76 for(id_feature in idFeatures(regionsFeatures_test)){
f81d72e482cf Uploaded
fabio
parents:
diff changeset
77 write(paste0('Test: ',testids[test],', on feature ',id_feature),
f81d72e482cf Uploaded
fabio
parents:
diff changeset
78 file=adjustedpvalue,append=TRUE,sep='\t')
f81d72e482cf Uploaded
fabio
parents:
diff changeset
79 pval_i=as.data.frame(t(pval[[test]][[id_feature]]))
f81d72e482cf Uploaded
fabio
parents:
diff changeset
80 row.names(pval_i)=paste('Scale',scale_threshold[[test]][[id_feature]])
f81d72e482cf Uploaded
fabio
parents:
diff changeset
81 write.table(pval_i,file=adjustedpvalue,append=TRUE,sep='\t',quote=FALSE,row.names=TRUE,col.names=FALSE)
f81d72e482cf Uploaded
fabio
parents:
diff changeset
82 write('',file=adjustedpvalue,append=TRUE,sep='\t')
f81d72e482cf Uploaded
fabio
parents:
diff changeset
83 }
f81d72e482cf Uploaded
fabio
parents:
diff changeset
84 }
f81d72e482cf Uploaded
fabio
parents:
diff changeset
85
f81d72e482cf Uploaded
fabio
parents:
diff changeset
86
f81d72e482cf Uploaded
fabio
parents:
diff changeset
87 # plot test results
f81d72e482cf Uploaded
fabio
parents:
diff changeset
88 pdf(iwtomicsrespdf,width=5,height=7)
f81d72e482cf Uploaded
fabio
parents:
diff changeset
89 if(plottype=='boxplot'){
f81d72e482cf Uploaded
fabio
parents:
diff changeset
90 # fix repeated probs
f81d72e482cf Uploaded
fabio
parents:
diff changeset
91 probs=sort(unique(probs))
f81d72e482cf Uploaded
fabio
parents:
diff changeset
92 }else{
f81d72e482cf Uploaded
fabio
parents:
diff changeset
93 probs=c(0.25,0.5,0.75)
f81d72e482cf Uploaded
fabio
parents:
diff changeset
94 }
f81d72e482cf Uploaded
fabio
parents:
diff changeset
95 plotTest(regionsFeatures_test,alpha=testalpha,type=plottype,probs=probs,average=average,size=size,scale_threshold=scale_threshold,ask=FALSE)
f81d72e482cf Uploaded
fabio
parents:
diff changeset
96 dev.off()
f81d72e482cf Uploaded
fabio
parents:
diff changeset
97
f81d72e482cf Uploaded
fabio
parents:
diff changeset
98 # plot summary results
f81d72e482cf Uploaded
fabio
parents:
diff changeset
99 if(groupby!='none'){
f81d72e482cf Uploaded
fabio
parents:
diff changeset
100 tryCatch({
f81d72e482cf Uploaded
fabio
parents:
diff changeset
101 pdf(iwtomicssumpdf,width=15,height=10)
f81d72e482cf Uploaded
fabio
parents:
diff changeset
102 plotSummary(regionsFeatures_test,alpha=summaryalpha,only_significant=only_significant,groupby=groupby,scale_threshold=scale_threshold,ask=FALSE,append=TRUE)
f81d72e482cf Uploaded
fabio
parents:
diff changeset
103 dev.off()
f81d72e482cf Uploaded
fabio
parents:
diff changeset
104 }, error = function(err) {
f81d72e482cf Uploaded
fabio
parents:
diff changeset
105 if (grepl('selected features with different resolution',err$message)) {
f81d72e482cf Uploaded
fabio
parents:
diff changeset
106 write("Group by 'test' but selected features with different resolution.", stderr())
f81d72e482cf Uploaded
fabio
parents:
diff changeset
107 quit(save="no", status=50) #error: groupby 'test' but selected features with different resolution.
f81d72e482cf Uploaded
fabio
parents:
diff changeset
108 }
f81d72e482cf Uploaded
fabio
parents:
diff changeset
109 write("Summary plot error. Please try again.", stderr())
f81d72e482cf Uploaded
fabio
parents:
diff changeset
110 quit(save="no", status=60) #error
f81d72e482cf Uploaded
fabio
parents:
diff changeset
111 })
f81d72e482cf Uploaded
fabio
parents:
diff changeset
112 }
f81d72e482cf Uploaded
fabio
parents:
diff changeset
113
f81d72e482cf Uploaded
fabio
parents:
diff changeset
114 }else{
f81d72e482cf Uploaded
fabio
parents:
diff changeset
115 write("Missing IWTomics package. Please be sure to have it installed before using this tool.", stderr())
f81d72e482cf Uploaded
fabio
parents:
diff changeset
116 quit(save="no", status=255)
f81d72e482cf Uploaded
fabio
parents:
diff changeset
117 }