38
|
1 if (require("IWTomics",character.only = TRUE,quietly = FALSE)) {
|
0
|
2 args=commandArgs(TRUE)
|
|
3
|
|
4 # get args names and values
|
|
5 args_values=strsplit(args,'=')
|
|
6 args_names=unlist(lapply(args_values,function(arg) arg[1]))
|
|
7 names(args_values)=args_names
|
|
8 args_values=lapply(args_values,function(arg) arg[2])
|
|
9 # read filenames
|
|
10 outrdata=args_values$outrdata
|
|
11 outregions=args_values$outregions
|
|
12 outfeatures=args_values$outfeatures
|
|
13 outpdf=args_values$outpdf
|
|
14 regionspaths=unlist(strsplit(args_values$regionspaths,'\\|'))
|
|
15 if("regionsheaderfile" %in% args_names){
|
|
16 # the file regionsheaderfile must contain as first column the (unique) regionsfilenames,
|
|
17 # as second column the corresponding ids and as third column the names
|
|
18 tryCatch({
|
|
19 regionsheader=read.delim(args_values$regionsheaderfile,header=FALSE,stringsAsFactors=FALSE,row.names=1,sep="\t")
|
|
20 regionsfilenames=unlist(strsplit(args_values$regionsfilenames,'\\|'))
|
|
21 if(length(setdiff(regionsfilenames,row.names(regionsheader)))) {
|
|
22 quit(save="no", status=11)
|
|
23 stop('Not all regionsfilenames are present in the first column of regionsheader.')
|
|
24 }
|
|
25 id_regions=regionsheader[regionsfilenames,1]
|
|
26 name_regions=regionsheader[regionsfilenames,2]
|
|
27 }, error = function(err) {
|
|
28 quit(save="no", status=10) #error on header file
|
|
29 stop(err)
|
|
30 })
|
|
31 }else{
|
|
32 eval(parse(text=args[[which(args_names=='regionsgalaxyids')]]))
|
|
33 id_regions=paste0('data_',regionsgalaxyids)
|
|
34 name_regions=paste0('data_',regionsgalaxyids)
|
|
35 }
|
|
36 featurespaths=unlist(strsplit(args_values$featurespaths,'\\|'))
|
|
37 if("featuresheaderfile" %in% args_names){
|
|
38 # the file featuresheaderfile must contain as first column the (unique) featuresfilenames,
|
|
39 # as second column the corresponding ids and as third column the names
|
|
40 tryCatch({
|
|
41 featuresheader=read.delim(args_values$featuresheaderfile,header=FALSE,stringsAsFactors=FALSE,row.names=1,sep="\t")
|
|
42 featuresfilenames=unlist(strsplit(args_values$featuresfilenames,'\\|'))
|
|
43 if(length(setdiff(featuresfilenames,row.names(featuresheader)))) {
|
|
44 quit(save="no", status=21)
|
|
45 stop('Not all featuresfilenames are present in the first column of featuresheader.')
|
|
46 }
|
|
47 id_features=featuresheader[featuresfilenames,1]
|
|
48 name_features=featuresheader[featuresfilenames,2]
|
|
49 }, error = function(err) {
|
|
50 quit(save="no", status=20) #error on header file
|
|
51 stop(err)
|
|
52 })
|
|
53 }else{
|
|
54 eval(parse(text=args[[which(args_names=='featuresgalaxyids')]]))
|
|
55 id_features=paste0('data_',featuresgalaxyids)
|
|
56 name_features=paste0('data_',featuresgalaxyids)
|
|
57 }
|
|
58 # read parameters (from smoothing on)
|
|
59 i_smoothing=which(args_names=='smoothing')
|
|
60 for(i in i_smoothing:length(args)){
|
|
61 eval(parse(text=args[[i]]))
|
|
62 }
|
|
63
|
|
64 # load data
|
|
65 tryCatch({
|
|
66 regionsFeatures=IWTomicsData(regionspaths,featurespaths,alignment,
|
|
67 id_regions,name_regions,id_features,name_features,start.are.0based=start.are.0based)
|
|
68 }, error = function(err) {
|
|
69 if(grepl('invalid format',err$message)){
|
|
70 quit(save="no", status=31) # error, not enough columns in input file
|
|
71 }else if(grepl('duplicated regions',err$message)){
|
|
72 quit(save="no", status=32) # error, duplicated regions in region file
|
|
73 }else if(grepl('duplicated windows',err$message)){
|
|
74 quit(save="no", status=33) # error, duplicated windows in feature file
|
|
75 }else if(grepl('overlapping windows',err$message)){
|
|
76 quit(save="no", status=34) # error, overlapping windows in feature file
|
|
77 }else if(grepl('not all regions in datasets',err$message)){
|
|
78 quit(save="no", status=35) # error, windows in feature files do not cover all regions in region files
|
|
79 }else if(grepl('ifferent size windows',err$message)){
|
|
80 quit(save="no", status=36) # error, all windows in a feature files must have the same size
|
|
81 }
|
|
82 #error loading data
|
|
83
|
|
84 stop(err)
|
|
85
|
|
86 quit(save="no", status=30)
|
|
87
|
|
88 })
|
|
89
|
|
90 # smooth data
|
|
91 if(smoothing!='no'){
|
|
92 tryCatch({
|
|
93 if(smoothing=='locpoly'){
|
|
94 dist_knots=10
|
|
95 }else if(smoothing=='kernel'){
|
|
96 degree=3
|
|
97 dist_knots=10
|
|
98 }else if(smoothing=='splines'){
|
|
99 bandwidth=5
|
|
100 }
|
|
101 if(alignment=='scale'){
|
|
102 if(scale==0){
|
|
103 regionsFeatures=smooth(regionsFeatures,type=smoothing,fill_gaps=fill_gaps,
|
|
104 bandwidth=bandwidth,degree=degree,dist_knots=dist_knots)
|
|
105 }else{
|
|
106 regionsFeatures=smooth(regionsFeatures,type=smoothing,fill_gaps=fill_gaps,
|
|
107 bandwidth=bandwidth,degree=degree,dist_knots=dist_knots,scale_grid=scale)
|
|
108 }
|
|
109 }else{
|
|
110 regionsFeatures=smooth(regionsFeatures,type=smoothing,fill_gaps=fill_gaps,
|
|
111 bandwidth=bandwidth,degree=degree,dist_knots=dist_knots)
|
|
112 }
|
|
113 }, error = function(err) {
|
|
114 quit(save="no", status=40) #error on smoothing
|
|
115 stop(err)
|
|
116 })
|
|
117 }
|
|
118
|
|
119 # plot data
|
|
120 pdf(outpdf,width=10,height=8)
|
|
121 if(plottype=='boxplot'){
|
|
122 # fix repeated probs
|
|
123 probs=sort(unique(probs))
|
|
124 }else{
|
|
125 probs=c(0.25,0.5,0.75)
|
|
126 }
|
|
127 plot(regionsFeatures,type=plottype,probs=probs,average=average,size=size,ask=FALSE)
|
|
128 dev.off()
|
|
129
|
|
130 # create output
|
|
131 #write.table(cbind(unlist(strsplit(args_values$regionsfilenames,'\\|')),idRegions(regionsFeatures),nameRegions(regionsFeatures)),
|
|
132 #file=outregions,quote=FALSE,sep='\t',row.names=FALSE,col.names=FALSE)
|
|
133 write.table(as.data.frame(t(idRegions(regionsFeatures))),file=outregions,quote=FALSE,sep='\t',row.names=FALSE,col.names=FALSE)
|
|
134 #write.table(cbind(unlist(strsplit(args_values$featuresfilenames,'\\|')),idFeatures(regionsFeatures),nameFeatures(regionsFeatures)),
|
|
135 #file=outfeatures,quote=FALSE,sep='\t',row.names=FALSE,col.names=FALSE)
|
|
136 write.table(as.data.frame(t(idFeatures(regionsFeatures))),file=outfeatures,quote=FALSE,sep='\t',row.names=FALSE,col.names=FALSE)
|
|
137 save(regionsFeatures,file=outrdata)
|
38
|
138 }else{
|
|
139 quit(save="no", status=255)
|
|
140 stop("Missing IWTomics package")
|
|
141 } |