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