Skip to content

Instantly share code, notes, and snippets.

View ATpoint's full-sized avatar

Alexander Bender ATpoint

View GitHub Profile
@ATpoint
ATpoint / readLoomMatrices.R
Created January 23, 2024 08:17
Standalone function to read loom files from velocyto into R as CsparseMatrix, adapted from velocyto.R.
#' Read loom files into R as CsparseMatrix, depending on Matrix and hdf5r
#' @param path to file loom file on disk
#' @param include.ambiguous logical, whether to also read the "ambiguous" counts from velocyto, default FALSE
#'
readLoomMatrices <- function(file, include.ambiguous=FALSE) {
require(Matrix)
require(hdf5r)
engine='hdf5r'
@ATpoint
ATpoint / BundleAndValidate.sh
Last active January 16, 2024 20:02
Tar or zip a directory and validate integrity by comparison of file list md5sums
#!/bin/bash
# BundleAndValidate <FOLDER_TO_BUNDLE> <tar/tarReplaceLink/zip> <Destination>
#
# Tar a folder and validate integrity of the tarball. For this, compare the file listing of the input folder
# with the file listing of the tarball via md5sum. If identical assume tarball is fine.
# Option tar makes a tarball, tarReplacelink uses tar -h to replace links with pointing file and zip makes a zipball.
# Don't be surprised that it can take some time before you see the tarball at destination after submitting the job.
# The first step is a full listing of the origin file, and with many folders and files that can take a while.
# Uses mbuffer internally to buffer incoming tar stream.
#' Create unique combinations
#'
#' To a given vector of numeric values add or subtract a constant value,
#' and return all possible combinations of that as a matrix
#'
#' @param values a vector with numeri values
#' @param change_value a numeric value too add or subtract
#'
#' @details
#' There are 2^length(values) possible combinations
reprex::reprex({
d <- data.frame(a=1)
# Works
suppressMessages({library(AnnotationDbi); library(dplyr)})
d %>% select(a)
detach("package:AnnotationDbi", unload=TRUE)
detach("package:dplyr", unload=TRUE)
@ATpoint
ATpoint / getREACTOME.R
Created July 7, 2023 12:22
Get REACTOME terms, translate to mouse
# Retrieve REACTOME terms for human directly from the website, then map to mouse.
# Pull human to mouse mappings via biomaRt.
library(biomaRt)
library(data.table)
library(magrittr)
library(rtracklayer)
library(tidyverse)
options(timeout=999)
@ATpoint
ATpoint / retrieveKEGG.R
Last active June 26, 2023 11:15
Query biomaRt and the KEGG API to produce a table connecting KEGG pathways with gene names for mouse and human
library(biomaRt)
library(data.table)
library(dplyr)
library(magrittr)
kegg <- sapply(c("mouse", "human"), function(x){
if(x=="mouse"){
dataset <- "mmusculus_gene_ensembl"
# Murine peripheral blood cell numbers based on:
# https://www.stemcell.com/media/files/wallchart/WA10011-Frequencies_Percentages_Mouse_Immune_Cell_Types.pdf
library(magrittr)
library(patchwork)
library(tidyverse)
options(ggplot2.discrete.fill=c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442",
"#0072B2", "#D55E00", "#CC79A7"))
@ATpoint
ATpoint / convertUCell.R
Last active April 22, 2023 18:33
Convert UCell output to label
#' Take output of UCell::ScoreSignatures_UCell() and return a vector of labels with highest score.
#' If the highest score per row is unique (only one value is the highest value) return that label.
#' If all scores are zero return "NA".
#' If more than one value is the highest value return "ambiguous".
#' library(UCell)
#' data(sample.matrix)
#' gene.sets <- list(Tcell_signature = c("CD2","CD3E","CD3D"),
#' Myeloid_signature = c("SPI1","FCER1G","CSF1R"))
#' scores <- ScoreSignatures_UCell(sample.matrix, features=gene.sets)
#' convertUCell(scores)
@ATpoint
ATpoint / colorblind.R
Created December 19, 2022 22:39
A colorblind-friendly palette including gray and black
cb <- c("#E69F00", "#0072B2", "#009E73",
"#CC79A7", "#56B4E9", "#D55E00",
"#661100", "#F0E442", "#332288",
"gray40", "black")
library(ggplot2)
ggplot(data.frame(x=c(rep(c(1,2,3), each=3), 4,4), y=1:length(l), color=LETTERS[1:length(l)]),
aes(x=x, y=y, color=color)) +
geom_point(size=10, show.legend=FALSE) +
@ATpoint
ATpoint / biostars_461026.Rmd
Last active January 17, 2024 18:59
biostars_461026
---
title: "https://www.biostars.org/p/461026/"
author: "ATpoint"
output: html_document
---
# Load data
```{r loadData}