-
Notifications
You must be signed in to change notification settings - Fork 2
/
cumLines.R
executable file
·477 lines (449 loc) · 15.5 KB
/
cumLines.R
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
#!/bin/env Rscript
args <- commandArgs()
scriptPath = strsplit(args[4], '=', fixed = T)[[1]][2]
scriptName = basename(scriptPath)
scriptDir = dirname(scriptPath)
args = args[-(1:5)]
source(paste0(scriptDir, '/common.R'))
usage = function(){
cat(paste0("Usage: ", scriptName) )
cat(" -p=outputName.pdf input1.data inpu2.data [input3.data ...]
Option:
Common:
-p|pdf FILE The output figure in pdf[figure.pdf]
-w|width INT The figure width
-m|main STR The main title
-mainS DOU The size of main title[22 for ggplot]
-x|xlab STR The xlab
-y|ylab STR The ylab
-xl|xlog INT Transform the X scale to INT base log
-yl|ylog INT Transform the Y scale to INT base log
-x1 INT The xlim start
-x2 INT The xlim end
-y1 INT The ylim start
-y2 INT The ylim end
-ng|noGgplot Draw figure in the style of R base rather than ggplot
-ho|-horizontal DOU Draw a horizontal dash line at y=DOU
-sort Sort the values before cumulating
-f|-freq Cumulate frequency rather sum
-frac Draw the Y axis in fraction
-h|help Show help
ggplot specific:
-legendT STR The title shown on legend[Group]
-a|alpha DOU The alpha of bar body
-alphaV STR The column name to apply alpha (V3, V4, ...)
-alphaT STR The title of alpha legend[Alpha]
-alphaTP POS The title position of alpha legend[horizontal: top, vertical:right]
-alphaLP POS The label position of alpha legend[horizontal: top, vertical:right]
-alphaD STR The direction of alpha legend (horizontal, vertical)
-c|color STR The color of line
-colorT STR The title of color legend[Color]
-colorTP POS The title position of color legend[horizontal: top, vertical:right]
-colorLP POS The label position of color legend[horizontal: top, vertical:right]
-colorD STR The direction of color legend (horizontal, vertical)
-l|linetype INT The line type
-linetypeV STR The column name to apply linetype (V3, V4,...)
-linetypeT STR The title of linetype legend[Line Type]
-linetypeTP POS The title position of linetype legend[horizontal: top, vertical:right]
-linetypeLP POS The label position of linetype legend[horizontal: top, vertical:right]
-linetypeD STR The direction of linetype legend (horizontal, vertical)
-s|size DOU The size of bar boundary
-sizeV STR The column name to apply size (V3, V4,...)
-sizeT STR The title of size legend[Size]
-sizeTP POS The title position of size legend[horizontal: top, vertical:right]
-sizeLP POS The label position of size legend[horizontal: top, vertical:right]
-sizeD STR The direction of size legend (horizontal, vertical)
-fp|flip Flip the Y axis to horizontal
-facet STR The facet type (facet_wrap, facet_grid)
-facetM STR The facet model (eg: '. ~ V3', 'V3 ~ .', 'V3 ~ V4', '. ~ V3 + V4', ...)
-facetScl STR The axis scale in each facet ([fixed], free, free_x or free_y)
-noGuide Don't show the legend guide
-lgPos POS The legend position[horizontal: top, vertical:right]
-lgPosX [0,1] The legend relative postion on X
-lgPosY [0,1] The legend relative postion on Y
-lgTtlS INT The legend title size[22]
-lgTxtS INT The legend text size[20]
-lgBox STR The legend box style (horizontal, vertical)
-xPer Show X label in percentage
-yPer Show Y label in percentage
-xComma Show X label number with comma seperator
-yComma Show Y label number with comma seperator
-axisRatio DOU The fixed aspect ratio between y and x units
-annoTxt STRs The comma-seperated texts to be annotated
-annoTxtX INTs The comma-seperated X positions of text
-annoTxtY INTs The comma-seperated Y positions of text
Skill:
Legend title of alpha, color, etc can be set as the same to merge their guides
")
q(save='no')
}
if(length(args) == 0) usage()
myPdf = 'figure.pdf'
alphaT = 'Alpha'
linetypeT = 'Line Type'
sizeT = 'Size'
lgTtlS = 22
lgTxtS = 20
showGuide = TRUE
mainS = 22
myLegendTitle = 'Group'
for(i in 1:length(args)){
arg = args[i]
if(arg == '-s' || arg == '-sort'){
toSort = TRUE
args[i] = NA
next
}
if(arg == '-f' || arg == '-freq'){
freq = TRUE
args[i] = NA
next
}
if(arg == '-frac'){
fraction = TRUE
args[i] = NA
next
}
tmp = parseArgAsNum(arg, 'ho(rizontal)?', 'ho')
if(!is.null(tmp)){
myHorizontal = tmp
args[i] = NA
next
}
tmp = parseArg(arg, 'legendT', 'legendT')
if(!is.null(tmp)){
myLegendTitle = tmp
args[i] = NA
next
}
tmp = parseArgAsNum(arg, 'a(lpha)?', 'a')
if(!is.null(tmp)){
alpha = tmp
args[i] = NA
next
}
tmp = parseArg(arg, 'c(olor)?', 'c')
if(!is.null(tmp)){
color = tmp
args[i] = NA
next
}
tmp = parseArgAsNum(arg, 'l(inetype)?', 'l')
if(!is.null(tmp)){
linetype = tmp
args[i] = NA
next
}
tmp = parseArgAsNum(arg, 's(ize)?', 's')
if(!is.null(tmp)){
size = tmp
args[i] = NA
next
}
if(arg == '-noGuide'){
showGuide = FALSE
}
tmp = parseArg(arg, 'lgPos', 'lgPos')
if(!is.null(tmp)){
lgPos = tmp
args[i] = NA
next
}
tmp = parseArgAsNum(arg, 'lgPosX', 'lgPosX')
if(!is.null(tmp)){
lgPosX = tmp
args[i] = NA
next
}
tmp = parseArgAsNum(arg, 'lgPosY', 'lgPosY')
if(!is.null(tmp)){
lgPosY = tmp
args[i] = NA
next
}
tmp = parseArgAsNum(arg, 'lgTtlS', 'lgTtlS')
if(!is.null(tmp)){
lgTtlS = tmp
args[i] = NA
next
}
tmp = parseArgAsNum(arg, 'lgTxtS', 'lgTxtS')
if(!is.null(tmp)){
lgTxtS = tmp
args[i] = NA
next
}
tmp = parseArg(arg, 'lgBox', 'lgBox')
if(!is.null(tmp)){
lgBox = tmp
args[i] = NA
next
}
if(arg == '-fp' || arg =='-flip'){
flip = TRUE
args[i] = NA
next
}
tmp = parseArg(arg, 'facet', 'facet')
if(!is.null(tmp)){
myFacet = tmp
args[i] = NA
next
}
tmp = parseArg(arg, 'facetM', 'facetM')
if(!is.null(tmp)){
facetM = tmp
args[i] = NA
next
}
tmp = parseArg(arg, 'facetScl', 'facetScl')
if(!is.null(tmp)){
facetScl = tmp
args[i] = NA
next
}
if(arg == '-xPer'){
xPer = TRUE
args[i] = NA
next
}
if(arg == '-yPer'){
yPer = TRUE
args[i] = NA
next
}
if(arg == '-xComma'){
xComma = TRUE
args[i] = NA
next
}
if(arg == '-yComma'){
yComma = TRUE
args[i] = NA
next
}
tmp = parseArgAsNum(arg, 'axisRatio', 'axisRatio')
if(!is.null(tmp)){
axisRatio = tmp
args[i] = NA
next
}
tmp = parseArg(arg, 'annoTxt', 'annoTxt')
if(!is.null(tmp)){
annoTxt = tmp
args[i] = NA
next
}
tmp = parseArg(arg, 'annoTxtX', 'annoTxtX')
if(!is.null(tmp)){
annoTxtX = tmp
args[i] = NA
next
}
tmp = parseArg(arg, 'annoTxtY', 'annoTxtY')
if(!is.null(tmp)){
annoTxtY = tmp
args[i] = NA
next
}
if(arg == '-h' || arg == '-help') usage()
tmp = parseArg(arg, 'p(df)?', 'p')
if(!is.null(tmp)){
myPdf = tmp
args[i] = NA
next
}
tmp = parseArgAsNum(arg, 'w(idth)?', 'w')
if(!is.null(tmp)){
width = tmp
args[i] = NA
next
}
if(arg == '-ng' || arg == '-noGgplot'){
noGgplot = TRUE
args[i] = NA
next
}
tmp = parseArgAsNum(arg, 'x1', 'x1')
if(!is.null(tmp)){
x1 = tmp
args[i] = NA
next
}
tmp = parseArgAsNum(arg, 'x2', 'x2')
if(!is.null(tmp)){
x2 = tmp
args[i] = NA
next
}
tmp = parseArgAsNum(arg, 'y1', 'y1')
if(!is.null(tmp)){
y1 = tmp
args[i] = NA
next
}
tmp = parseArgAsNum(arg, 'y2', 'y2')
if(!is.null(tmp)){
y2 = tmp
args[i] = NA
next
}
tmp = parseArgAsNum(arg, 'xl(og)?', 'xl')
if(!is.null(tmp)){
xLog = tmp
args[i] = NA
next
}
tmp = parseArgAsNum(arg, 'yl(og)?', 'yl')
if(!is.null(tmp)){
yLog = tmp
args[i] = NA
next
}
tmp = parseArg(arg, 'm(ain)?', 'm')
if(!is.null(tmp)){
main = tmp
args[i] = NA
next
}
tmp = parseArgAsNum(arg, 'mainS', 'mainS')
if(!is.null(tmp)){
mainS = tmp
args[i] = NA
next
}
tmp = parseArg(arg, 'x(lab)?', 'x')
if(!is.null(tmp)){
xLab = tmp
args[i] = NA
next
}
tmp = parseArg(arg, 'y(lab)?', 'y')
if(!is.null(tmp)){
yLab = tmp
args[i] = NA
next
}
}
args = args[!is.na(args)]
if(length(args) < 2) stop('Please specify two input files at least')
if(exists('width')){
pdf(myPdf, width = width)
}else{
pdf(myPdf)
}
library(tools)
dir.create('cum', showWarnings = F)
fileNames = basename(file_path_sans_ext(args))
data = read.delim(args[1], header = F)
if(exists('toSort')){
if(exists('freq')){
cum = cumsum(table(sort(data$V1)))/nrow(data)
index = as.numeric(names(cum))
}else{
index = sort(data$V1)
cum = cumsum(index)
}
}else{
index = 1:nrow(data)
cum = cumsum(data$V1)
}
data = data.frame(V1 = index, V2 = cum)
if(exists('fraction')) data$V2 = data$V2/sum(data$V2)
write.table(data, paste0('cum/', fileNames[1], '.cum'), row.names = F, col.names = F, sep = "\t", quote = F)
if(exists('noGgplot')){
myColors = rainbow(length(args))
myCmd = 'plot(data, type = "l", col = myColors[1]'
if(exists('x1') && exists('x2')) myCmd = paste0(myCmd, ', xlim = c(x1, x2)')
if(exists('main')) myCmd = paste0(myCmd, ', main = main')
if(exists('xLab')) myCmd = paste0(myCmd, ', xlab = xLab')
if(exists('yLab')) myCmd = paste0(myCmd, ', ylab = yLab')
myCmd = paste0(myCmd, ')')
eval(parse(text = myCmd))
for(i in 2:length(args)){
file = args[i]
data = read.delim(file, header = F)
if(exists('toSort')){
if(exists('freq')){
cum = cumsum(table(sort(data$V1)))/nrow(data)
index = as.numeric(names(cum))
}else{
index = sort(data$V1)
cum = cumsum(index)
}
}else{
index = 1:nrow(data)
cum = cumsum(data$V1)
}
data = data.frame(V1 = index, V2 = cum)
if(exists('fraction')) data$V2 = data$V2/sum(data$V2)
write.table(data, paste0('cum/', fileNames[i], '.cum'), row.names = F, col.names = F, sep = "\t", quote = F)
lines(data, col = myColors[i])
}
legend('topright', legend = fileNames, col = myColors, lty = 1, lwd = 1.5)
}else{
library(ggplot2)
data = cbind(data, Group = fileNames[1])
for(i in 2:length(args)){
file = args[i]
fileName = fileNames[i]
newData = read.delim(file, header = F)
if(exists('toSort')){
if(exists('freq')){
cum = cumsum(table(sort(newData$V1)))/nrow(newData)
index = as.numeric(names(cum))
}else{
index = sort(newData$V1)
cum = cumsum(index)
}
}else{
index = 1:nrow(newData)
cum = cumsum(newData$V1)
}
newData = data.frame(V1 = index, V2 = cum)
if(exists('fraction')) newData$V2 = newData$V2/sum(newData$V2)
write.table(newData, paste0('cum/', fileNames[i], '.cum'), row.names = F, col.names = F, sep = "\t", quote = F)
newData = cbind(newData, Group = fileName)
data = rbind(data, newData)
}
p = ggplot(data, aes(V1, V2, color = Group)) + guides(color = guide_legend(title = myLegendTitle))
myCmd = 'p = p + geom_line(show_guide = showGuide'
if(exists('alpha')) myCmd = paste0(myCmd, ', alpha = alpha')
if(exists('color')) myCmd = paste0(myCmd, ', color = color')
if(exists('size')) myCmd = paste0(myCmd, ', size = size')
myCmd = paste0(myCmd, ')')
if(exists('myFacet')){
myCmd = paste0(myCmd, ' + ', myFacet, '("' + facetM + '"')
if(exists('facetScl')) myCmd = paste0(myCmd, ', scale = facetScl')
myCmd = paste0(myCmd, ')')
}
eval(parse(text = myCmd))
if(exists('lgPos')) p = p + theme(legend.position = lgPos)
if(exists('lgPosX') && exists('lgPosY')) p = p + theme(legend.position = c(lgPosX, lgPosY))
p = p + theme(legend.title = element_text(size = lgTtlS), legend.text = element_text(size = lgTxtS))
if(exists('lgBox')) p = p + theme(legend.box = lgBox)
if(exists('xPer')) p = p + scale_x_continuous(labels = percent)
if(exists('yPer')) p = p + scale_y_continuous(labels = percent)
if(exists('xComma')) p = p + scale_x_continuous(labels = comma)
if(exists('yComma')) p = p + scale_y_continuous(labels = comma)
if(exists('axisRatio')) p = p + coord_fixed(ratio = axisRatio)
if(exists('annoTxt')) p = p + annotate('text', x = as.numeric(strsplit(annoTxtX, ',', fixed = T)),
y = as.numeric(strsplit(annoTxtY, ',', fixed = T)),
label = strsplit(annoTxt, ',', fixed = T))
if(exists('x1') && exists('x2')) p = p + coord_cartesian(xlim = c(x1, x2))
if(exists('y1') && exists('y2')) p = p + coord_cartesian(ylim = c(y1, y2))
if(exists('x1') && exists('x2') && exists('y1') && exists('y2')) p = p + coord_cartesian(xlim = c(x1, x2), ylim = c(y1, y2))
if(exists('xLog') || exists('yLog')){
library(scales)
if(exists('xLog')) p = p + scale_x_continuous(trans = log_trans(xLog)) + annotation_logticks(sides = 'b')
if(exists('yLog')) p = p + scale_y_continuous(trans = log_trans(yLog)) + annotation_logticks(sides = 'l')
p = p + theme(panel.grid.minor = element_blank())
}
if(exists('main')) p = p + ggtitle(main)
p = p + theme(plot.title = element_text(size = mainS))
p = p + xlab(xLab) + theme(axis.title.x = element_text(size = mainS*0.8), axis.text.x = element_text(size = mainS*0.7))
if(exists('yLab')) p = p + ylab(yLab)
p = p + theme(axis.title.y = element_text(size = mainS*0.8), axis.text.y = element_text(size = mainS*0.7))
if(exists('myHorizontal')) p = p + geom_hline(yintercept = myHorizontal, linetype = "longdash", size = 0.3)
p
}