-
Notifications
You must be signed in to change notification settings - Fork 30
/
sp_multiBar.sh
executable file
·281 lines (246 loc) · 5.89 KB
/
sp_multiBar.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#!/bin/bash
#set -x
usage()
{
cat <<EOF
${txtcyn}
***CREATED BY Chen Tong ([email protected])***
Usage:
$0 options${txtrst}
${bldblu}Function${txtrst}:
This script is used to do boxplot using ggplot2.
fileformat for -f (suitable for data extracted from one sample)
Gene hmC expr Set
NM_001003918_26622 0 83.1269257376101 1
NM_001011535_3260 0 0 1
NM_001012640_14264 0 0 1
NM_001012640_30427 0 0 1
NM_001003918_266221 0 0 1
NM_001017393_30504 0 0 1
NM_001025241_30464 0 0 1
NM_001017393_30504 0 0 1
fileformat when -m is true
#The name "value" and "variable" shoud not be altered.
#"Set" needs to be the parameter after -a.
#Actually this format is the melted result of last format.
value variable Set
0 var 1
1 vhig 2
${txtbld}OPTIONS${txtrst}:
-f Data file (with header line, the first column is the
colname, tab seperated)${bldred}[NECESSARY]${txtrst}
-m When true, it will skip preprocess. But the format must be
the same as listed before.
${bldred}[Default FALSE, accept TRUE]${txtrst}
-a Name for x-axis variable
[${txtred}Necessary, for example 'Gene' which represents
name of each gene Set${txtrst}]
-b Order of xvariable (parameter for -a).
[${txtred}Optional, format: 'chr1','chr2','chr3',...${txtrst}]
["'chr1','chr2','chr3','chr4','chr5','chr6','chr7','chr8',
'chr9','chr10','chr11','chr12','chr13','chr14','chr15',
'chr16','chr17','chr18','chr19','chrX','chrY','chrM'"]
-l Order for legend.
[${txtred}Optional, format: 'Expected','5hmC',...${txtrst}]
-n Rotation angle for x-axis.
[${txtred}Optional, Default oriental. Accept an integer
like 45,90 (count anti-clockwise). ${txtrst}]
-g Variable name for facet.
[${txtred}Optional, like 'sample' or 'grp' ${txtrst}]
-j Number of columns in one row.
[${txtred}Necessary if -g is given ${txtrst}]
-k Paramter for scales for facet.
[${txtred}Optional, only used when -g is given. Default each
inner graph use same scale [x,y range]. 'free','free_x','free_y'
is accepted. ${txtrst}]
-o Order of facet.
[${txtred}Optional, format:
'F1_6-1','R1','F1_MEF','1422','2737','17-3-15','15-4-6','513','233'${txtrst}]
-p Other columns that needs to be ignored.
[Optional, format (begins with ,) ',col_name1,colname2...']
-t Title of picture[${txtred}Default empty title${txtrst}]
-x xlab of picture[${txtred}Default empty xlab${txtrst}]
-y ylab of picture[${txtred}Default empty ylab${txtrst}]
-s Scale y axis
[${txtred}Default null. Accept TRUE.${txtrst}]
-v If scale is TRUE, give the following
scale_y_log10(), coord_trans(y="log10"), or other legal
#command for ggplot2)${txtrst}]
-w The width of output picture.[${txtred}Default 20${txtrst}]
-u The height of output picture.[${txtred}Default 12${txtrst}]
-E The type of output figures.[${txtred}Default png, accept
eps/ps, tex (pictex), pdf, jpeg, tiff, bmp, svg and wmf)${txtrst}]
-r The resolution of output picture.[${txtred}Default 300 ppi${txtrst}]
-z Is there a header[${bldred}Default TRUE${txtrst}]
-e Execute or not[${bldred}Default TRUE${txtrst}]
-i Install depeneded packages[${bldred}Default FALSE${txtrst}]
EOF
}
file=
title=''
melted='FALSE'
xlab='NULL'
ylab='NULL'
xvariable=''
xvar_order=''
legend_order=''
angle=0
facet='haha'
facet_ncol=1
facet_order=''
col_exclu=''
scales='fixed'
scaleY='FALSE'
scaleY_x='FALSE'
header='TRUE'
execute='TRUE'
ist='FALSE'
uwid=20
vhig=12
res=300
ext='png'
while getopts "hf:m:a:b:l:o:n:g:j:k:p:t:x:y:w:u:r:E:s:z:v:e:i:" OPTION
do
case $OPTION in
h)
usage
exit 1
;;
f)
file=$OPTARG
;;
m)
melted=$OPTARG
;;
a)
xvariable=$OPTARG
;;
b)
xvar_order=$OPTARG
;;
l)
legend_order=$OPTARG
;;
o)
facet_order=$OPTARG
;;
n)
angle=$OPTARG
;;
g)
facet=$OPTARG
;;
j)
facet_ncol=$OPTARG
;;
k)
scales=$OPTARG
;;
p)
col_exclu=$OPTARG
;;
t)
title=$OPTARG
;;
x)
xlab=$OPTARG
;;
y)
ylab=$OPTARG
;;
w)
uwid=$OPTARG
;;
u)
vhig=$OPTARG
;;
r)
res=$OPTARG
;;
E)
ext=$OPTARG
;;
s)
scaleY=$OPTARG
;;
v)
scaleY_x=$OPTARG
;;
z)
header=$OPTARG
;;
e)
execute=$OPTARG
;;
i)
ist=$OPTARG
;;
?)
usage
exit 1
;;
esac
done
if [ -z $file ]; then
usage
exit 1
fi
mid=".multiBarNew"
cat <<END >${file}${mid}.r
if ($ist){
install.packages("ggplot2", repo="http://cran.us.r-project.org")
install.packages("reshape2", repo="http://cran.us.r-project.org")
}
library(ggplot2)
library(reshape2)
if(! $melted){
data <- read.table(file="${file}", sep="\t", header=$header, quote="")
if ("${facet}" != "haha"){
id_vars = c("${xvariable}", "${facet}" ${col_exclu})
#data_m <- melt(data, id.vars=c("${xvariable}", "${facet}"))
} else {
id_vars = c("${xvariable}" ${col_exclu})
#data_m <- melt(data, id.vars=c("${xvariable}"))
}
data_m <- melt(data, id.vars=id_vars)
} else {
data_m <- read.table(file="$file", sep="\t",
header=$header, quote="")
}
if ("${legend_order}" != ""){
data_m\$variable <- factor(data_m\$variable,
levels=c(${legend_order}))
}
if ("${xvar_order}" != ""){
data_m\$${xvariable} <- factor(data_m\$${xvariable},
levels=c(${xvar_order}))
}
if ("${facet_order}" != ""){
data_m\$${facet} <- factor(data_m\$${facet},
levels=c(${facet_order}))
}
p <- ggplot(data_m, aes(factor($xvariable), value)) + xlab($xlab) +
ylab($ylab)
p <- p + geom_bar(aes(fill=factor(variable)), stat="identity",
position="dodge") + theme_bw()
if ($angle != 0) {
p <- p + theme(axis.text.x=element_text(angle=${angle},hjust=1))
}
if ("$facet" != "haha"){
p <- p + facet_wrap(~${facet}, ncol=${facet_ncol},
scales="${scales}")
}
if("$scaleY"){
p <- p + $scaleY_x
}
ggsave(p, filename="${file}${mid}.${ext}", dpi=$res, width=$uwid,
height=$vhig, units=c("cm"))
#png(filename="${file}${mid}.png", width=$uwid, height=$vhig,
#res=$res)
#p
#dev.off()
END
if [ "$execute" == "TRUE" ]; then
Rscript ${file}${mid}.r
if [ "$?" == "0" ]; then /bin/rm -f ${file}${mid}.r; fi
fi