Skip to content

Commit

Permalink
version 1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Tamalunas authored and cran-robot committed Mar 6, 2019
0 parents commit f353f5b
Show file tree
Hide file tree
Showing 40 changed files with 2,321 additions and 0 deletions.
20 changes: 20 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 1,20 @@
Package: GCalcium
Type: Package
Title: A Data Manipulation and Analysis Package for Calcium Indicator
Data
Version: 1.0.0
Author: Andrew Tamalunas
Maintainer: Andrew Tamalunas <[email protected]>
Description: Provides shortcuts in extracting useful data points and summarizing waveform data. It is optimized for speed to work efficiently with large data sets so you can get to the analysis phase more quickly. It also utilizes a user-friendly format for use by both beginners and seasoned R users.
Depends: R (>= 2.10)
Imports: stats, graphics, caTools, dplyr
License: GPL-3
Encoding: UTF-8
LazyData: true
Suggests: knitr, rmarkdown
VignetteBuilder: knitr
RoxygenNote: 6.1.0
NeedsCompilation: no
Packaged: 2019-03-04 22:59:12 UTC; Tamu
Repository: CRAN
Date/Publication: 2019-03-06 16:53:27 UTC
39 changes: 39 additions & 0 deletions MD5
Original file line number Diff line number Diff line change
@@ -0,0 1,39 @@
6608c38e2a4fe9fc4b1f10aefc7a7ace *DESCRIPTION
bd20720d287cf5cb848af1af88451b8a *NAMESPACE
0f638a5a158da4c492c345c27ab88d9a *R/GCaMP-data.R
90b054e9139d01d39b48d2470c11383f *R/averaged_trials.R
85b390f236d6c38bf55a60aee47c1b95 *R/avg_curve_slope.R
79ae4cd0e655b7937122712295da7d03 *R/between_trial_change.R
96e7fcb38f9d9ca9695cf50e27fc289b *R/centered_AUC.R
976bf15a2d5352210c974e8eaf7cfc8a *R/consecutive_trial_change.R
05252ed0ad5572aeb9e084d1b3aac028 *R/find_peaks.R
9858e6c28ffbe2fd9499352933fae2cd *R/format_data.R
fc9cf0ee06429894d631990f98073c9b *R/inflect_points.R
50de5f697cc9b6be626228c4101fa0f6 *R/inflect_points_df.R
c3e95a0dfe00558c3bc9ed70f531a3a8 *R/moving_window.R
e98520c7d07bf10ee544d47010da84be *R/perc_baseline.R
130d5f87b48f75c4b05af45b35af51e3 *R/plot_trials.R
d9ec7af657968b443ccf349125f5dd56 *R/within_trial_change.R
68bc4af1b4d530f0aa2da6eafa13a93a *R/z_score.R
78212c11b9f91769970cb4722f820b8b *README.md
e84cd79bb3d24fde1bd985907f9055c8 *build/vignette.rds
5b7e17f525d304025fad16aee3e9ad14 *data/GCaMP.rda
84a648e4e81f16483c783bf70511af8f *inst/doc/Examples.R
ae0c0c4faa504ca58bf405dda7444f9c *inst/doc/Examples.Rmd
eec618723e3094930a91ce0d5cb5b288 *inst/doc/Examples.html
72274034ddcf64920c1f057237a1c359 *man/GCaMP.Rd
098bee6fc2a09f2761f28c9bcc50e80e *man/averaged_trials.Rd
833215eb7a54c8794f6b8c3af3fb1254 *man/avg_curve_slope.Rd
1b1b0cf3fb734bf9525aff9dd5d59b47 *man/between_trial_change.Rd
3f22d1091c7925b9980d75c8556776ca *man/centered_AUC.Rd
55da8ad9e3ef8271d5f3bb16dd27de1d *man/consecutive_trial_change.Rd
66c31e311744bceac7ac89a37d5603be *man/find_peaks.Rd
77c4f665f54ccb0f4d1cf11bf88f3aba *man/format_data.Rd
0842070dbc510b8525d37779708e0477 *man/inflect_points.Rd
08d08e50a8873bf2bb7502affd8028cd *man/inflect_points_df.Rd
954c37bbbc3365cb9bd65537bc8411c1 *man/moving_window.Rd
b9773ec47032491a33c77c3a7e5b0521 *man/perc_baseline.Rd
e3919d8dec61b247a04a8faccba65146 *man/plot_trials.Rd
dc71a591b876d9b20a5d0d7ff2bab2c8 *man/within_trial_change.Rd
ab1bdda610e82d1bc7d1d0c4fe36a3e4 *man/z_score.Rd
ae0c0c4faa504ca58bf405dda7444f9c *vignettes/Examples.Rmd
24 changes: 24 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -0,0 1,24 @@
# Generated by roxygen2: do not edit by hand

export(averaged_trials)
export(avg_curve_slope)
export(between_trial_change)
export(centered_AUC)
export(consecutive_trial_change)
export(find_peaks)
export(format_data)
export(inflect_points)
export(inflect_points_df)
export(moving_window)
export(perc_baseline)
export(plot_trials)
export(within_trial_change)
export(z_score)
importFrom(caTools,trapz)
importFrom(dplyr,case_when)
importFrom(graphics,legend)
importFrom(graphics,lines)
importFrom(graphics,par)
importFrom(graphics,plot)
importFrom(stats,lm)
importFrom(stats,sd)
18 changes: 18 additions & 0 deletions R/GCaMP-data.R
Original file line number Diff line number Diff line change
@@ -0,0 1,18 @@
#' Pre-filtered GCaMP calcium activity waveforms
#'
#' A dataset containing calcium activity and
#' observed times during a behavioral paradigm pilot study;
#' organized into 10 individual trials
#'
#' @docType data
#'
#' @usage data(GCaMP)
#'
#' @format A data frame with 11 rows and 814 columns
#' \describe{
#' \item{Time}{observed times of Trial.n values, in seconds}
#' \item{Trial.n}{values of calcium activity signals for trial n}
#' ...
#' }
#' @source Dr. David Root, Maier-Watkins laboratory
"GCaMP"
29 changes: 29 additions & 0 deletions R/averaged_trials.R
Original file line number Diff line number Diff line change
@@ -0,0 1,29 @@
#' @title Averages signals across trials
#'
#' @description `averaged_trials` averages values over each time point, across the specified trials
#'
#' @param Dataframe a GCalcium-format data frame or matrix
#' @param Trials numbers of trials to be averaged across
#' @return a data frame with observation times and averaged values
#' @examples
#' ### Format data frame
#' df.new <- format_data(GCaMP)
#'
#' ### Plot the average fluorescence signal across trials 1-5
#' df.1thru5 <- averaged_trials(df.new, 1:5)
#'
#' plot(x = df.1thru5$Time, df.1thru5$Values)
#' @export

averaged_trials <- function(Dataframe, Trials){

trialinds <- Trials 1

TrialMeans <- rowMeans(Dataframe[trialinds])
return.df <- as.data.frame(cbind(Dataframe[,1], TrialMeans))

colnames(return.df) <- c("Time", "Values")

return(return.df)

}
32 changes: 32 additions & 0 deletions R/avg_curve_slope.R
Original file line number Diff line number Diff line change
@@ -0,0 1,32 @@
#' @title Get average curve slopes
#'
#' @description `avg_curve_slope` finds the average curve slopes for a trial using inflect_points_df and lm
#'
#' @note curves of a single value will result in the average slope being labeled as NA
#' @importFrom stats lm
#' @param Dataframe a GCalcium-format data frame or matrix
#' @param Trial a single trial number
#' @return vector of average rate of change for each curve
#' @examples
#' df.new <- format_data(data = GCaMP)
#' avg_curve_slope(Dataframe = df.new, Trial = 1)
#' @export

avg_curve_slope <- function(Dataframe, Trial){

Summaryframe <- inflect_points_df(Dataframe = Dataframe, Trial = Trial)
Summaryframe$Curve.Num <- as.factor(Summaryframe$Curve.Num)

### Get all info
curveline <- with(Summaryframe,
by(Summaryframe, Curve.Num,
function(Summaryframe) lm(Raw.Values ~ Time, data = Summaryframe)))

### Extract slopes
ROCs <- sapply(1:nrow(curveline), function(curvenum){
curveline[[curvenum]]$coefficients[[2]]
})

return(ROCs)

}
39 changes: 39 additions & 0 deletions R/between_trial_change.R
Original file line number Diff line number Diff line change
@@ -0,0 1,39 @@
#' @title Compare mean activity between trials
#'
#' @description `between_trial_change` finds the difference in means during same time range between sets of trials.
#'
#' @param Dataframe a GCaMP-format data frame or matrix
#' @param TrialRange1 range of trial numbers to be compared to second set of trials
#' @param TrialRange2 range of trial numbers to be compared to first set of trials
#' @param Time.period range of time to be compared between sets of trials
#' @return Number representing mean difference of trial set 2 and trial set 1
#' @examples
#' # How much did the mean value change 2s after epoc between trials 1-5 and trials 6-10?
#' df.new <- format_data(data = GCaMP)
#' between_trial_change(Dataframe = df.new, TrialRange1 = c(1, 5),
#' TrialRange2 = c(6, 10), Time.period = c(0, 2))
#' @export

between_trial_change <- function(Dataframe, TrialRange1, TrialRange2, Time.period = c(min(Dataframe[1]), max(Dataframe[1]))){

trial1ind <- TrialRange1 1
trial2ind <- TrialRange2 1

start.time <- Time.period[1]
stop.time <- Time.period[2]

### Subsets based on time and trials
sub1 <- Dataframe[Dataframe[1] >= start.time & Dataframe[1] <= stop.time, ]

t1.sub <- as.matrix(sub1[,trial1ind])
t2.sub <- as.matrix(sub1[,trial2ind])

### Trial numbers
t1.mean <- mean(t1.sub)
t2.mean <- mean(t2.sub)

mean.change <- t2.mean - t1.mean

return(mean.change)

}
32 changes: 32 additions & 0 deletions R/centered_AUC.R
Original file line number Diff line number Diff line change
@@ -0,0 1,32 @@
#' @title Compute centered area under curve
#'
#' @description `centered_AUC` computes the area under the curve (AUC) after centering values using a specified function, such as mean or min. Computed using trapezoidal integration.
#'
#' @importFrom caTools trapz
#' @importFrom stats sd
#' @param Dataframe a GCalcium-format data frame or matrix
#' @param Trial a single trial number
#' @param FUN a function to apply to each window
#' @return Data frame of AUCs and curve number for each curve
#' @examples
#' # Get AUCs for trial 2, centered at the mean
#' df.new <- format_data(data = GCaMP)
#' centered_AUC(Dataframe = df.new, Trial = 2, FUN = mean)
#' @export

centered_AUC <- function(Dataframe, Trial, FUN = mean){

Summaryframe <- inflect_points_df(Dataframe = Dataframe, Trial = Trial)
Summaryframe$Raw.Values.c <- Summaryframe$Raw.Values - FUN(Summaryframe$Raw.Values)
Summaryframe$Curve.Num <- as.factor(Summaryframe$Curve.Num)

### Get all info
curve.AUCS <- sapply(split(Summaryframe, Summaryframe$Curve.Num), function(df) trapz(df$Time, df$Raw.Values.c))

### Add curve numbers and put into data frame
AUC.df <- data.frame(Curve.Num = 1:length(curve.AUCS),
AUC = curve.AUCS)

return(AUC.df)

}
53 changes: 53 additions & 0 deletions R/consecutive_trial_change.R
Original file line number Diff line number Diff line change
@@ -0,0 1,53 @@
#' @title Compare mean activity in consecutive trials
#'
#' @description `consecutive_trial_change` finds the change in means between consecutive trials.
#'
#' @param Dataframe a GCaMP-format data frame or matrix
#' @param Trials range of trial numbers to be compared
#' @param Time.period range of time to be compared
#' @return Data frame with the "Mean.Change" column representing differences in means between trial n and trial n 1 for the user-inputted range of trials.
#' @examples
#' ### How much did the mean value change after epoc between consecutive trials in trials 1-10?
#' df.new <- format_data(data = GCaMP)
#' consecutive_trial_change(Dataframe = df.new, Trials = c(1, 10), Time.period = c(0, 4))
#' @export

consecutive_trial_change <- function(Dataframe, Trials, Time.period){

blank.frame <- data.frame()

### Create indices
ind.start1 <- Trials[1] 1
ind.start2 <- ind.start1 1
ind.stop <- Trials[2] 1

### Repeat across consecutive trials
while(ind.start1 < ind.stop){

trialnum <- (ind.start1 - 1) .5

# Subsets
t1.sub <- as.matrix(Dataframe[,ind.start1])
t2.sub <- as.matrix(Dataframe[,ind.start2])

# Means
t1.mean <- mean(t1.sub)
t2.mean <- mean(t2.sub)

mean.change <- t2.mean - t1.mean

# Fin
final.vector <- c(trialnum, mean.change)

blank.frame <- rbind(blank.frame, final.vector)

ind.start1 <- ind.start1 1
ind.start2 <- ind.start2 1

}

colnames(blank.frame) <- c("Trial", "Mean.Change")

return(blank.frame)

}
36 changes: 36 additions & 0 deletions R/find_peaks.R
Original file line number Diff line number Diff line change
@@ -0,0 1,36 @@
#' @title Get peaks/valleys in waveform data
#'
#' @description `find_peaks` finds peaks or valleys in waveform by using inflection points, with filter of 'n' increasing/decreasing points on both sides of each inflection point.
#'
#' @param x vector of numbers
#' @param n.points the number of decreasing (peaks) or increasing (valleys) data points on left and right of inflection point required to be considered a "peak". A positive number as an input finds peaks, and a negative number finds valleys.
#' @return a numeric vector of indices
#' @examples
#' ### Format data frame
#' df.new <- format_data(GCaMP)
#'
#' ### How many peaks are there in trial 1 with 10 decreasing data points on each side?
#' peak.indices <- find_peaks(df.new$Trial1, n.points = 10)
#'
#' ### When do they occur?
#' data.frame(times = df.new$Time[peak.indices], vals = df.new$Trial1[peak.indices])
#' @export

find_peaks <- function (x, n.points = 3){

# define curves and peaks
curves <- diff(sign(diff(x, na.pad = FALSE)))
peakz <- sapply(which(curves < 0), FUN = function(i){

b <- i - n.points 1
b <- ifelse(b > 0, b, 1)
a <- i n.points 1
a <- ifelse(a < length(x), a, length(x))
if(all(x[c(b : i, (i 2) : a)] <= x[i 1])) return(i 1) else return(numeric(0))

})

peakz <- unlist(peakz)
return(peakz)

}
32 changes: 32 additions & 0 deletions R/format_data.R
Original file line number Diff line number Diff line change
@@ -0,0 1,32 @@
#' @title Reformat and label data into GCalcium format
#'
#' @description `format_data` changes the format of data from wide time series to long format with labeled columns. If data is already in long format, the function simply re-labels the columns
#'
#' @param data A data set with observation times in the first row or column, and observed values from trials in each following row or column.
#' @return Data frame with labeled time and trial columns
#' @examples
#' df.new <- format_data(GCaMP)
#' @export

format_data <- function(data) {

### Transpose data if not in long format
if(ncol(data) > nrow(data)){
data <- t(data)
data <- as.data.frame(data)
row.names(data) <- c()
}

### Label time
colnames(data)[1] <- "Time"

### label iters
trial.nums <- 1:(ncol(data) - 1)

### Label trials
colnames(data)[2:ncol(data)] <- sprintf("Trial%d", trial.nums)

return(data)

}

23 changes: 23 additions & 0 deletions R/inflect_points.R
Original file line number Diff line number Diff line change
@@ -0,0 1,23 @@
#' @title Find inflection pooints
#'
#' @description `inflect_points` finds inflection points of waveform data
#'
#' @param x A vector of numbers
#' @return Inflection points of the input vector
#' @examples
#' df.new <- format_data(GCaMP)
#' inflect_points(df.new$Trial1)
#' @export

inflect_points <- function (x){

# Find inflection points
inf.pts <- diff(sign(diff(x, na.pad = TRUE)))

# Add NAs since we removed 2 points by doing first difference twice
inf.pts <- append(inf.pts, 0)
inf.pts <- append(inf.pts, 0)

return(inf.pts)

}
Loading

0 comments on commit f353f5b

Please sign in to comment.