Skip to content

Instantly share code, notes, and snippets.

@rjungemann
Created September 11, 2011 06:50
Show Gist options
  • Save rjungemann/1209258 to your computer and use it in GitHub Desktop.
Save rjungemann/1209258 to your computer and use it in GitHub Desktop.
Example R code
#!/usr/bin/env Rscript
# -----
# USAGE
# -----
# ./galaxy_column ~/desktop/galaxy1014.txt SVZ_conf_lo DG_conf_hi
# ---------
# FUNCTIONS
# ---------
# read a table in tab-delimited format from a particular file
#
# * file - a file or pipe to read the table from
read.galaxy_tabular = function(file) {
read.table(file, header = TRUE, sep = "\t", fill = TRUE, as.is = TRUE)
}
# write data to a file in Excel-friendly format (CSV) from a particular frame
#
# * file - a file or pipe to read the table from
# * frame - data structure to write
write.excel = function(file, frame) write.csv(frame, file, row.names = FALSE)
# open a pipe representing the clipboard
clipboard.out = function() pipe('pbcopy', 'w')
# given a pipe, use it in the callback and then close it afterwards
#
# * p - pipe or file to use
# * callback - action to perform before closing the pipe
pipe.close_after = function(p, callback) {
callback(p)
close(p)
}
# ----
# MAIN
# ----
# read in a table name and two column names
args = commandArgs(TRUE)
table_name = args[1]
a = args[2]
b = args[3]
# read in a table from the table name
table = read.galaxy_tabular(table_name)
# create a new column by dividing column a and b and returning 'TRUE' if > 2
new_column = table[a] / table[b] > 2.0
# write the new column to the clipboard in Excel-friendly format
pipe.close_after( clipboard.out(), function(p) write.excel(p, new_column) )
cat('output a new column to the clipboard for pasting into excel\n')
#!/usr/bin/env Rscript
# -----
# USAGE
# -----
# ./galaxy_filter ~/desktop/galaxy1014.txt 'M/P > 2'
# ---------
# FUNCTIONS
# ---------
# filter down a list of rows
#
# * f - function with one parameter, "row". If returns false, row is removed
# * x - the table to filter
filter = function(f, x) {
ind = as.logical( sapply(x, f) )
x[ !is.na(ind) & ind ]
}
# read a table in tab-delimited format from a particular file
#
# * file - a file or pipe to read the table from
read.galaxy_tabular = function(file) {
read.table(file, header = TRUE, sep = "\t", fill = TRUE, as.is = TRUE)
}
# write data to a file in Excel-friendly format (CSV) from a particular frame
#
# * file - a file or pipe to read the table from
# * frame - data structure to write
write.excel <- function(file, frame) {
write.csv(frame, file, row.names = FALSE)
}
# open a pipe representing the clipboard
clipboard.out <- function() pipe('pbcopy', 'w');
# given a pipe, use it in the callback and then close it afterwards
#
# * p - pipe or file to use
# * callback - action to perform before closing the pipe
pipe.close_after <- function(p, callback) {
callback(p);
close(p);
}
# ----
# MAIN
# ----
# read in the table name and column to filter by
args = commandArgs(TRUE)
table_name = args[1]
a = args[2]
# read in a table given the provided table name
table = read.galaxy_tabular(table_name)
# filter rows out of the table depending on the values in column
table2 = filter(function(row) { row[a] == 'TRUE' }, table)
# write the new table to the clipboard in Excel format
pipe.close_after( clipboard.out(), function(p) write.excel(p, table2) )
cat('output a new column to the clipboard for pasting into excel\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment