annotate plotwithscale.R @ 53:3d2e03e9c526 draft

Deleted selected files
author fabio
date Wed, 31 May 2017 15:26:42 -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 adjustedpvalue=args_values$adjustedpvalue
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 iwtomicsrdata=args_values$iwtomicsrdata
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
14 iwtomicstests=args_values$iwtomicstests
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
15 iwtomicsselectedfeatures=args_values$iwtomicsselectedfeatures
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
16 test_subset=paste0('c(',strsplit(args_values$test_subset,'\\|')[[1]],')')
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
17 feature_subset=paste0('c(',strsplit(args_values$feature_subset,'\\|')[[1]],')')
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
18 # read parameters (from test_subset on)
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
19 i_scale_subset=which(args_names=='scale_subset')
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
20 for(i in i_scale_subset:length(args)){
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
21 eval(parse(text=args[[i]]))
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
22 }
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
23
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
24 # load RData
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
25 load(iwtomicsrdata)
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
26 # read testids and featureids and check them
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
27 unlisted=lapply(seq_along(test_subset),
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
28 function(i){
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
29 test_subset_i=eval(parse(text=test_subset[i]))
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
30 feature_subset_i=eval(parse(text=feature_subset[i]))
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
31 test_subset_i=rep(test_subset_i,each=length(feature_subset_i))
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
32 feature_subset_i=rep(feature_subset_i,length.out=length(test_subset_i))
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
33 scale_subset_i=rep(scale_subset[i],length(test_subset_i))
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
34 return(list(test_subset=test_subset_i,feature_subset=feature_subset_i,scale_subset=scale_subset_i))
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
35 })
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
36 test_subset=unlist(lapply(unlisted,function(l) l$test_subset))
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
37 feature_subset=unlist(lapply(unlisted,function(l) l$feature_subset))
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
38 scale_subset=unlist(lapply(unlisted,function(l) l$scale_subset))
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
39 testids=as.character(read.delim(iwtomicstests,header=FALSE,sep='\t',stringsAsFactors=FALSE))
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
40 featureids=as.character(read.delim(iwtomicsselectedfeatures,header=FALSE,sep='\t',stringsAsFactors=FALSE))
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
41 id_features_subset=featureids[feature_subset]
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
42 if(sum(testids!=paste(testInput(regionsFeatures_test)$id_region1,'vs',testInput(regionsFeatures_test)$id_region2))){
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
43 quit(save="no", status=10)
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
44 stop('Wrong test ids')
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
45 }
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
46 if(sum(featureids!=idFeatures(regionsFeatures_test))){
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
47 quit(save="no", status=20)
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
48 stop('Wrong feature ids')
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
49 }
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
50 # retrieve test and features_subset ids
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
51 id_features_subset=featureids[feature_subset]
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
52 if(sum(duplicated(paste0(test_subset,id_features_subset)))){
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
53 quit(save="no", status=30)
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
54 stop('Two scale thresholds selected for the same test and feature.')
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
55 }
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
56 # If scale_subset=0, do not change the threshold
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
57 default=(scale_subset==0)
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
58 scale_subset=scale_subset[!default]
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
59 test_subset=test_subset[!default]
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
60 id_features_subset=id_features_subset[!default]
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
61
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
62 # get scale threshold
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
63 scale_threshold=lapply(regionsFeatures_test@test$result,
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
64 function(result) unlist(lapply(result,function(feature) feature$max_scale)))
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
65 for(i in seq_along(test_subset)){
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
66 if(scale_threshold[[test_subset[i]]][id_features_subset[i]]<scale_subset[i]){
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
67 quit(save="no", status=40)
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
68 stop('Scale threshold too high.')
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
69 }
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
70 scale_threshold[[test_subset[i]]][id_features_subset[i]]=scale_subset[i]
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
71 }
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
72
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
73 # create adjustedvalue output
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
74 pval=adjusted_pval(regionsFeatures_test,scale_threshold=scale_threshold)
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
75 for(test in seq_along(pval)){
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
76 for(id_feature in idFeatures(regionsFeatures_test)){
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
77 write(paste0('Test: ',testids[test],', on feature ',id_feature),
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
78 file=adjustedpvalue,append=TRUE,sep='\t')
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
79 pval_i=as.data.frame(t(pval[[test]][[id_feature]]))
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
80 row.names(pval_i)=paste('Scale',scale_threshold[[test]][[id_feature]])
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
81 write.table(pval_i,file=adjustedpvalue,append=TRUE,sep='\t',quote=FALSE,row.names=TRUE,col.names=FALSE)
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
82 write('',file=adjustedpvalue,append=TRUE,sep='\t')
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
83 }
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
84 }
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
85
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
86
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
87 # plot test results
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
88 pdf(iwtomicsrespdf,width=5,height=7)
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
89 if(plottype=='boxplot'){
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
90 # fix repeated probs
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
91 probs=sort(unique(probs))
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
92 }else{
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
93 probs=c(0.25,0.5,0.75)
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
94 }
1e677d6b1aaf IWTomics v1.0 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)
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
96 dev.off()
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
97
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
98 # plot summary results
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
99 if(groupby!='none'){
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
100 tryCatch({
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
101 pdf(iwtomicssumpdf,width=15,height=10)
1e677d6b1aaf IWTomics v1.0 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)
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
103 dev.off()
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
104 }, error = function(err) {
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
105 if (grepl('selected features with different resolution',err$message)) {
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
106 quit(save="no", status=50) #error: groupby 'test' but selected features with different resolution.
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
107 stop(err)
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
108 }
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
109 quit(save="no", status=60) #error
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
110 stop(err)
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
111 })
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
112 }
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
113
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
114 }else{
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
115 quit(save="no", status=255)
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
116 stop("Missing IWTomics package")
1e677d6b1aaf IWTomics v1.0 uploaded
fabio
parents:
diff changeset
117 }