forked from PennLINC/xcpEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1dTool.R
executable file
·105 lines (93 loc) · 3.86 KB
/
1dTool.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
#! /usr/bin/env Rscript
###################################################################
# ☭ ☭ ☭ ☭ ☭ ☭ ☭ ☭ ☭ ☭ ☭ ☭ ☭ ☭ ☭ ☭ ☭ ☭ ☭ ☭ ☭ #
###################################################################
###################################################################
# Operations on 1D files. Currently this is very simplistic, but
# functionality will be added as needed.
###################################################################
###################################################################
# Load required libraries
###################################################################
suppressMessages(suppressWarnings(library(optparse)))
suppressMessages(suppressWarnings(library(pracma)))
###################################################################
# Parse arguments to script, and ensure that the required arguments
# have been passed.
###################################################################
option_list = list(
make_option(c("-i", "--input"), action="store", default=NA, type='character',
help="Path to the input 1D file"),
make_option(c("-r", "--header"), action="store", default=FALSE, type='logical',
help="Does the input file contain a header?"),
make_option(c("-z", "--zeros"), action="store", default=FALSE, type='logical',
help="Ignore zero values in computation?"),
make_option(c("-f", "--file"), action="store", default=NA, type='character',
help="Write output to file"),
make_option(c("-t", "--title"), action="store", default=NA, type='character',
help="Optional output header"),
make_option(c("-o", "--operation"), action="store", default='none', type='character',
help="What operation should be performed?
min : return minimum value
max : return maximum value
which_min : return index of minimum value
which_max : return index of maximum value
length : return length of 1D file
mean : return mean of 1D file")
)
opt = parse_args(OptionParser(option_list=option_list))
if (is.na(opt$input)) {
cat('User did not specify an input.\n')
cat('Use 1dTool.R -h for an expanded usage menu.\n')
quit()
}
input <- opt$input
header <- opt$header
zeros <- opt$zeros
file <- opt$file
title <- opt$title
operation <- opt$operation
###################################################################
# Read input.
###################################################################
if (header) {
input <- read.table(input, header=T)
} else {
input <- read.table(input, header=F)
}
###################################################################
# Remove zeros or other irrelevant values if necessary.
###################################################################
if (zeros) {
input[input==0] <- NA
}
###################################################################
# Redirect output to file if requested.
###################################################################
if (!is.na(file)) {
sink(file = file)
}
###################################################################
# Operations -- wish R had case switch
###################################################################
if (operation == 'min') {
out <- min(input, na.rm=TRUE)
} else if (operation == 'max') {
out <- max(input, na.rm=TRUE)
} else if (operation == 'which_min') {
out <- which(input==min(input, na.rm=TRUE))
} else if (operation == 'which_max') {
out <- which(input==max(input, na.rm=TRUE))
} else if (operation == 'length') {
out <- nrow(input)
} else if (operation == 'mean') {
out <- apply(input,2,mean,na.rm=TRUE)
}
if (!is.na(title)) {
cat(title,'\n',sep='')
}
cat(out,sep=' ')
cat('\n')
if (!is.na(file)) {
sink(NULL)
}