comparison chartskit.r @ 31:7774e0097ff4 draft

Uploaded
author guerler
date Mon, 07 Apr 2014 19:23:58 -0400
parents 4aeb334de0e3
children 0197da753d1e
comparison
equal deleted inserted replaced
30:533b6c8f8584 31:7774e0097ff4
375 return(options) 375 return(options)
376 } 376 }
377 377
378 # get options, using the spec as defined by the enclosed list. 378 # get options, using the spec as defined by the enclosed list.
379 spec = matrix(c( 379 spec = matrix(c(
380 'workdir', 'w', 1, 'character', 'Work directory',
380 'module', 'm', 1, 'character', 'Module name', 381 'module', 'm', 1, 'character', 'Module name',
381 'input', 'i', 1, 'character', 'Input tabular file', 382 'input', 'i', 1, 'character', 'Input tabular file',
382 'columns', 'c', 1, 'character', 'Columns string', 383 'columns', 'c', 1, 'character', 'Columns string',
383 'settings', 's', 1, 'character', 'Settings string', 384 'settings', 's', 1, 'character', 'Settings string',
384 'output', 'o', 1, 'character', 'Output tabular file', 385 'output', 'o', 1, 'character', 'Output tabular file',
386 'verbose', 'v', 0, '', 'Verbose' 387 'verbose', 'v', 0, '', 'Verbose'
387 ), byrow=TRUE, ncol=5); 388 ), byrow=TRUE, ncol=5);
388 opt = getopt(spec); 389 opt = getopt(spec);
389 390
390 # show help 391 # show help
391 if ( !is.null(opt$help) || is.null(opt$module) || is.null(opt$input) || is.null(opt$columns) || is.null(opt$output)) { 392 if ( !is.null(opt$help) ||
392 cat(getopt(spec, usage=TRUE)); 393 is.null(opt$module) ||
394 is.null(opt$input) ||
395 is.null(opt$columns) ||
396 is.null(opt$output)) {
397 cat(getopt(spec, usage=TRUE))
393 q(status=1); 398 q(status=1);
394 } 399 }
395 400
396 # read columns/settings 401 # read columns/settings
397 columns = split(opt$columns) 402 columns = split(opt$columns)
398 settings = split(opt$settings) 403 settings = split(opt$settings)
399 404
400 # read table 405 # read table
401 table <- read.table(opt$input) 406 table <- read.table(opt$input)
402 407
408 # identify module file
409 module_file = paste(opt$workdir, opt$module, '.r', sep='')
410
403 # source module 411 # source module
404 source(opt$module) 412 source(module_file)
405 413
406 # run module 414 # run module
407 l = wrapper (table, columns, settings) 415 l = wrapper (table, columns, settings)
408 416
417 # header
418 header_title <- '# title\t\tCharts Toolkit (chartskit)'
419 header_date <- paste('# date\t\t', Sys.time(), sep='')
420 header_module <- paste('# module\t', opt$module, sep='')
421 header_settings <- paste('# settings\t', opt$settings, sep='')
422 header_columns <- paste('# columns\t', opt$columns, sep='')
423
409 # fill gaps 424 # fill gaps
410 if (length(l) > 0) { 425 if (length(l) > 0) {
411 n <- max(sapply(l, length))
412 ll <- lapply(l, function(X) {
413 c(as.character(X), rep("", times = n - length(X)))
414 })
415 out <- do.call(cbind, ll)
416
417 # print details 426 # print details
418 if (!is.null(opt$verbose)) { 427 if (!is.null(opt$verbose)) {
419 print ('Columns:') 428 print ('Columns:')
420 print (columns) 429 print (columns)
421 print ('Settings:') 430 print ('Settings:')
422 print (settings) 431 print (settings)
423 print ('Result:') 432 print ('Result:')
424 print (out) 433 print (l)
425 } 434 }
426 435
436 # create output file
437 output <- file(opt$output, open='wt')
438
439 # write header
440 writeLines('#', output)
441 writeLines(header_title, output)
442 writeLines(header_date, output)
443 writeLines(header_module, output)
444 writeLines(header_settings, output)
445 writeLines(header_columns, output)
446 writeLines('#', output)
447
427 # write table 448 # write table
428 write.table(out, file=opt$output, row.names=FALSE, col.names = FALSE, quote=FALSE, sep='\t') 449 write.table(l, file=output, row.names=FALSE, col.names = FALSE, quote=FALSE, sep='\t')
450
451 # close file
452 close(output)
429 } else { 453 } else {
430 print ('Columns:') 454 print ('Columns:')
431 print (columns) 455 print (columns)
432 print ('Settings:') 456 print ('Settings:')
433 print (settings) 457 print (settings)