Skip to content

Instantly share code, notes, and snippets.

View tomsing1's full-sized avatar

Thomas Sandmann tomsing1

View GitHub Profile
@tomsing1
tomsing1 / exon_level_read_counting_with_featurecounts.sh
Created September 17, 2024 03:28
A script that retrieves BAM files from S3 and quantifies exon-level expression with featurecounts
#!/usr/bin/env bash
set -e
set -o nounset
# This script uses a GTF file (e.g. generated by Stringtie) and
# 1. retrieves BAM files for individual samples from AWS S3
# 2. quantifies the exons defined in the GTF file with featureCounts,
# e.g. for differential splicing analysis with limma::diffSplice
#
# Alignments are expected to be reverse-stranded and paired-end.
@tomsing1
tomsing1 / awswrangler_in_colab.py
Created August 21, 2024 19:54
Wrangling files on AWS S3 with the awswrangler python module (in Colab)
# Commented out IPython magic to ensure Python compatibility.
# %%capture
# !pip install awswrangler --quiet
import awswrangler as wr
import pandas as pd
import boto3
from google.colab import data_table, userdata
data_table.enable_dataframe_formatter()
@tomsing1
tomsing1 / boto3_in_colab.py
Created August 21, 2024 19:41
Accessing AWS within a Google Colab using the built-in secret manager
import boto3
from google.colab import userdata
# refresh the temporary credentials via the key symbol in the left hand tab
s3_resource = boto3.resource('s3',
aws_access_key_id=userdata.get('AWS_ACCESS_KEY_ID'),
aws_secret_access_key=userdata.get('AWS_SECRET_ACCESS_KEY'),
aws_session_token=userdata.get('AWS_SESSION_TOKEN')
)
@tomsing1
tomsing1 / glue_sql_quoting.R
Created April 19, 2024 17:33
Controlling quoting by the glue::glue_sql() function
library(glue)
library(RSQLite)
con <- dbConnect(RSQLite::SQLite(), ":memory:")
var = "test"
glue_sql("{var}", .con = con) # <SQL> 'test'
glue_sql("{`var`}", .con = con) # <SQL> `test`
glue_sql("{DBI::SQL(var)}", .con = con) # <SQL> test (unquoted)
glue_sql("`{var}`", .con = con) # <SQL> `'test'` NOT USEFUL
@tomsing1
tomsing1 / conditionalPanel_usage_in_module.R
Last active April 7, 2024 19:33
Modularized shiny app using conditional panels (modified from the shiny::conditionalPanel help page)
# The following code is based on the shiny::conditionalPanel() help page. The `ui` and `server`
# components have been compartmentalized into the `mod_histogram` module. The `histogramApp`
# function shows an example of using this module.
# Note that the `ns = ns` argument needs to be passed ot the conditionalPanel() call.
library(shiny)
mod_histogram_ui <- function(id){
ns <- NS(id)
fluidPage(
@tomsing1
tomsing1 / biosample_attributes.R
Created January 25, 2024 17:22
Retrieve sample attributes from EBI's ENA repository in JSON format
library(glue)
library(httr)
library(jsonlite)
library(xml2)
#' Query ENA's REST API for information about records
#'
#' @param accessions Character vector of one or more ENA record identifiers
#' @return An `xml_document` object
get_records <- function(accessions) {
@tomsing1
tomsing1 / order_input.R
Created January 16, 2024 04:54
Reordering the levels of categorical variables in a shiny app with shinyjqui
library(palmerpenguins)
library(shiny)
library(shinyjqui)
categories <- colnames(penguins)[vapply(penguins, is.factor, logical(1))]
server <- function(input, output) {
lapply(categories, \(category) {
output[[category]] <- renderPrint({ print(input[[category]]) })
})
@tomsing1
tomsing1 / image_placeholder.R
Created January 8, 2024 19:09
Create an image tag pointing to a random picture from Lorem Picsum
#' Create an image tag with an example image
#'
#' @param width Scalar integer, the width of the image
#' @param height Scalar integer, the height of the image
#' @param title Scalar character, the title of the image
#' @return A `shiny.tag` with the URL to a random image from
#' [Lorem Picsum](https://picsum.photos/)
#' @export
#' @importFrom htmltool tags
#' @importFrom checkmate assert_count assert_character
@tomsing1
tomsing1 / pyenv.md
Last active December 28, 2023 00:16
Using pyenv to manage multiple python versions & virtual environments

There is a lot of confusing information about virtual environments in python out there, in part because the tool chain has evolved over many years.

I decided to follow the advice of Real python and The hitchhiker's guide to python and manage both multiple python versions and multiple virtual environments with pyenv

@tomsing1
tomsing1 / quarto_webr_example.qmd
Created November 19, 2023 17:32
Quarto markdown file that uses the quarto-webr extension to run R code in the browser
---
title: "Embedding R into Quarto documents with quarto-webr"
subtitle: "Example: intersecting differential expression results"
author: "Thomas Sandmann"
date: '2023/11/18'
format: html
engine: knitr
webr:
show-startup-message: true
packages: ['ggvenn', 'huxtable']