Skip to content

Instantly share code, notes, and snippets.

View stemangiola's full-sized avatar

Stefano Mangiola stemangiola

View GitHub Profile
# PREMISE
# these implementation of HPCL is highly experimental.
# Therefore the interface, function names, arguments names, types of input
# are subject to change and to be extended.
# At this stage, the input is a named list of Seurat objects.
# In another branch (soon to be integrated), we accept SCE, H5 SCE, and anndata
BiocManager::install("MangiolaLaboratory/HPCell@grammar-hpc", upgrade_dependencies=FALSE)
BiocManager::install("crew", upgrade_dependencies=FALSE)
BiocManager::install("crew.cluster", upgrade_dependencies=FALSE)
git remote set-url origin [email protected]:USERNAME/REPOSITORY.git
@stemangiola
stemangiola / color_cell_types.R
Last active July 28, 2023 04:27
Color cell types
get_cell_type_color = function(cell_type_vector){
cell_type_vector |>
enframe(value = "harmonised_cell_type") |>
left_join(
tribble(
~harmonised_cell_type, ~palette,
# Theme
friendly_cols <- dittoSeq::dittoColors()
# scale_fill_manual(values = friendly_cols)
# scale_color_manual(values = friendly_cols)
theme_multipanel =
theme_bw() +
theme(
panel.border = element_blank(),
@stemangiola
stemangiola / aggregate_cells.R
Last active November 25, 2020 01:27
code for ABACBS 2020
#' Convert array of quosure (e.g. c(col_a, col_b)) into character vector
#'
#' @keywords internal
#'
#' @importFrom rlang quo_name
#' @importFrom rlang quo_squash
#' @importFrom purrr when
#' @importFrom magrittr equals
#'
#' @param v A array of quosures (e.g. c(col_a, col_b))
@stemangiola
stemangiola / tidy_data_tree.R
Last active June 7, 2019 05:54
tidy_data_tree
ToDataFrameTypeColFull = function(tree, ...){
tree %>%
Clone() %>%
{
t = (.)
foreach(l=1:(t %$% Get("level") %>% max), .combine = bind_rows) %do% {
data.tree::Clone(t) %>%
{ data.tree::Prune(., function(x) x$level <= l + 1); . } %>%
data.tree::ToDataFrameTypeCol(...) %>%
as_tibble
@stemangiola
stemangiola / stan_tidy_tools.R
Created April 16, 2019 23:41
Some useful functions about bayes object parsing
get_bimodality = function(fit) {
fit %>%
tidybayes::gather_draws(exposure_rate[S]) %>% summarise(
bimodal = diptest::dip.test(`.value`) %$% `p.value`
)
}
@stemangiola
stemangiola / rstan_installation.txt
Last active March 21, 2019 04:47
Installation of rstan for big machines
####################################################################
# From within R
####################################################################
####################################################################
# Initialisation of makefiles
####################################################################
dotR <- file.path(Sys.getenv("HOME"), ".R")
if (!file.exists(dotR)) dir.create(dotR)
generate_gamma_sum <- function(N, k, theta) {
n_means = length(k)
sums = numeric(N)
for(n in 1:N) sums[n] = sum(rgamma(n_means, shape = k, scale = theta))
list(sums = sums, shape = k, scale = theta)
}
k=c(3,4,5)
theta=c(1, 2, 1)
generate_gamma_sum(10000, c(3,4,5), c(1, 2, 1)) %$%
@stemangiola
stemangiola / approximated_modified_bessel_second_kind_log.R
Created March 11, 2019 05:10
approximated modified bessel second kind log
approximated_modified_bessel_second_kind_log = function(z, v, s = max(0,v-10)){
# s = 0 not approximated
0.5 * ( log(pi) - log(2) ) + (-z - 0.5 * log(z)) +
foreach(j = s:floor(v-0.5), .combine = c) %do% {
lgamma(j + abs(v) - 0.5 + 1) -