Getting BMG Labtech CSV files into R

Here is some R code for reading wide-format BMG MARS-style time series CSV files into long-format in R, for either single data files (readBMG) or multiple data files from the same experiment, e.g. using the BMG Microplate stacker (readStack).

require(lubridate)
require(tidyverse)
require(growthcurver)

# Some functions for importing BMG-style CSV files in a tidy way

# BMG MARS CSV exports have a trailing comma, so this drops the empty last column
read_csv_drop = function(...) read_csv(...) %>% select(-ncol(.))

# A function to convert BMG MARS's crazy time format to fractional hours
BMGtime = function(otime){
	missing.s=grep("[0-9] s",otime, value=F,invert=T)
	missing.m=grep("[0-9] min",otime, value=F,invert=T)
	missing.h=grep("[0-9] h",otime, value=F,invert=T)
	otime[missing.s]=sub("$"," 0 s", otime[missing.s])
	otime[missing.h]=sub("^","0 h ", otime[missing.h])
	otime[missing.m]=sub("h", "h 0 min ", otime[missing.m])
	otime=period_to_seconds(hms(gsub("[a-z ]+",":", otime)))/3600
return(otime)
}

# Read a single BMG Mars wide-format time series table into long format
readBMG = function(file){
	time = read_csv(file, n_max=1) %>% t() %>% BMGtime()
	time =  time[4:(length(time)-1)]
	growthdata = read_csv_drop(file, skip=2, col_names=c("row", "col", "content", time)) %>%
		select(-ends_with("_1")) %>%
 		gather("time","value",-row, -col, -content) %>%
		type_convert()
return(growthdata)
}

# Read multiple files from the same experiment (resulting from a stacker run)
readStack = function(pattern, parse.name=F, prefix=NULL, suffix=NULL, into=NULL, sep=NULL){
	stackdata = data_frame(id = list.files(pattern=pattern), data = map(id, ~readBMG(.)))
	if(parse.name){
		stackdata = stackdata %>%
			mutate(id = gsub(suffix, "", gsub(prefix, "", id))) %>%
			separate(id, into=into)
	}
	stackdata = stackdata %>% unnest(data) %>% type_convert()
return(stackdata)}

Leave a comment...

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s