Skip to content

Commit

Permalink
Merge pull request #15 from jhsiao999/dev
Browse files Browse the repository at this point in the history
Dev merge
  • Loading branch information
jhsiao999 authored Sep 12, 2019
2 parents b3c9baf + 32ecbd8 commit 1fb1c59
Show file tree
Hide file tree
Showing 25 changed files with 678 additions and 264 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
.Rhistory
data-raw/
.Rproj.user
peco.Rcheck/
peco*tar.gz
Expand Down
5 changes: 4 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Authors@R: c(person("Chiaowen Joyce","Hsiao",email="joyce.hsiao1@gmail.com",
role="ctb", comment = c(ORCID = "0000-0003-2634-9879")),
person("Peter","Carbonetto", email = "peter.carbonetto@gmail.com",
role="ctb", comment = c(ORCID = "0000-0003-1144-6780")))
Maintainer: Chiaowen Joyce Hsiao <joyce.hsiao1@gmail.com>
Description: Our approach provides a way to assign continuous cell cycle phase
using scRNA-seq data, and consequently, allows to identify cyclic trend
of gene expression levels along the cell cycle. This package provides method
Expand All @@ -23,16 +24,18 @@ Description: Our approach provides a way to assign continuous cell cycle phase
URL: https://github.com/jhsiao999/peco
BugReports: https://github.com/jhsiao999/peco/issues
License: GPL (>= 3)
Depends: R (>= 3.2)
Depends: R (>= 3.5)
Imports:
assertthat,
Biobase,
circular,
conicfit,
genlasso,
ggplot2,
graphics,
MASS,
Matrix,
parallel,
stats
Suggests:
knitr,
Expand Down
4 changes: 1 addition & 3 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@

export(circ_dist)
export(cycle_npreg_insample)
export(cycle_npreg_loglik)
export(cycle_npreg_mstep)
export(cycle_npreg_outsample)
export(data_transform_quantile)
export(fit_bspline)
export(fit_cyclical_many)
export(fit_loess)
export(fit_trendfilter_generic)
export(initialize_grids)
export(intensity2circle)
export(rotation)
export(shift_origin)
importFrom(assertthat,assert_that)
importFrom(circular,coord2rad)
importFrom(conicfit,AtoG)
Expand Down
59 changes: 42 additions & 17 deletions R/circ_dist.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,29 @@
#' the differences in both clockwise and counterclockwise directions.
#'
#' @param y1 A vector of angles.
#'
#' @param y2 A vector of angles.
#'
#' @return A vector of distances between angles.
#'
#' @author Joyce Hsiao, Matthew Stephens
#' @examples
#' # a vector of angles
#' theta_ref <- seq(0,2*pi, length.out=100)
#'
#' # shift the origin of theta_ref to pi
#' theta_compare <- shift_origin(theta_ref, origin = pi)
#' mean(circ_dist(theta_ref, theta_compare))
#'
#' # after rotation of angles, difference is 0 between the original
#' # and the shifted angles
#' theta_compare_rotated <- rotation(ref_var=theta_ref, shift_var=theta_compare)
#' mean(circ_dist(theta_ref, theta_compare_rotated))
#'
#'
#' @author Joyce Hsiao, Matthew Stephens
#' @export
#'
#'
circ_dist <- function(y1,y2) {
pmin(abs(y2-y1), abs(2*pi-(abs(y2-y1))))
pmin(abs(y2-y1), abs(2*pi-(abs(y2-y1))))
}

#' @title Rotate circular variable shift_var to minimize distance
Expand All @@ -27,29 +39,42 @@ circ_dist <- function(y1,y2) {
#' distance between the output value and ref_var.
#'
#' @param ref_var A vector of reference angles.
#'
#' @param shift_var A vector of angles to be compared to ref_var.
#'
#' @return The transformed values of shift_var after rotation and
#' shifting.
#'
#' @author Matthew Stephens
#' @examples
#' # a vector of angles
#' theta_ref <- seq(0,2*pi, length.out=100)
#'
#' # shift the origin of theta_ref to pi
#' theta_compare <- shift_origin(theta_ref, origin = pi)
#'
#' # rotate theta_compare in a such a way that the distance
#' # between theta_ref and thet_compare is minimized
#' theta_compare_rotated <- rotation(ref_var=theta_ref, shift_var=theta_compare)
#'
#' par(mofrow=c(1,2))
#' plot(x=theta_ref, y = theta_compare)
#' plot(x=theta_ref, y = theta_compare_rotated)
#'
#' @author Matthew Stephens
#' @export
#'
#'
rotation <- function(ref_var,shift_var) {

df <- data.frame(flip=rep(c(1,-1), each=length(shift_var)),
shift = rep(shift_var, 2))
df <- data.frame(flip=rep(c(1,-1), each=length(shift_var)),
shift = rep(shift_var, 2))

for (i in 1:nrow(df)) {
shift_var_tmp <- df$flip[i]*((shift_var+df$shift[i])%%(2*pi))
df$dis[i] <- mean(circ_dist(ref_var, shift_var_tmp))
}
which_cutoff <- which.min(df$dis)
for (i in seq_len(nrow(df))) {
shift_var_tmp <- df$flip[i]*((shift_var+df$shift[i])%%(2*pi))
df$dis[i] <- mean(circ_dist(ref_var, shift_var_tmp))
}
which_cutoff <- which.min(df$dis)

shift_var_new <-
df$flip[which_cutoff]*((shift_var+df$shift[which_cutoff])%%(2*pi))
shift_var_new <-
df$flip[which_cutoff]*((shift_var+df$shift[which_cutoff])%%(2*pi))

return(shift_var_new)
return(shift_var_new)
}
Loading

0 comments on commit 1fb1c59

Please sign in to comment.