Skip to content

Instantly share code, notes, and snippets.

@stephenturner
Last active April 7, 2025 10:25
Show Gist options
  • Save stephenturner/2e7ee7443645b7048aa8338d79c55d04 to your computer and use it in GitHub Desktop.
Save stephenturner/2e7ee7443645b7048aa8338d79c55d04 to your computer and use it in GitHub Desktop.
Demonstration using reticulate to access the Hugging Face transformers library and pyBigWig in R using uv
# Demo 1: NLP -------------------------------------------------------------
# Load reticulate, declare that we'll use pytorch and huggingface transformers
library(reticulate)
py_require("torch")
py_require("transformers")
# Use the transformers python, and use the sentiment analysis pipeline
transformers <- import("transformers")
analyzer <- transformers$pipeline("sentiment-analysis")
# Try it out
analyzer("It was the best of times")
analyzer("It was the worst of times")
# R function using the HF transformers sentiment pipeline to return a DF
analyze_sentiment <- function(text) {
result <- analyzer(text)[[1]]
data.frame(label = result$label, score = result$score)
}
analyze_sentiment("it was the age of wisdom, it was the age of foolishness,
it was the epoch of belief, it was the epoch of incredulity,
it was the season of Light, it was the season of Darkness,
it was the spring of hope, it was the winter of despair")
# Create some text snippets
mytexts <- c("I love using R and Python together!",
"This is the worst API I've ever worked with.",
"Results are fine, but the code is a mess",
"This package manager is super fast.")
# Now, run sentiment analysis on all your text snippets
library(dplyr)
library(tidyr)
tibble(text=mytexts) |>
mutate(sentiment = lapply(text, analyze_sentiment)) |>
unnest_wider(sentiment)
# Demo 2: pyBigWig --------------------------------------------------------
library(reticulate)
py_require("pyBigWig")
download.file("http://genome.ucsc.edu/goldenPath/help/examples/bigWigExample.bw",
destfile = "bigWigExample.bw",
mode = "wb")
# Import the pyBigWig module
pybw <- import("pyBigWig")
# Open a BigWig file
bw <- pybw$open("bigWigExample.bw")
# Get list of chromosomes
chroms <- bw$chroms()
print(chroms)
# Query values near the end of chromosome 21
chrom <- "chr21"
start <- chroms[[1]]-100000L
end <- start+1000L
# Get values (one per base)
values <- bw$values(chrom, start, end)
# Close the file
bw$close()
# Wrap into data frame
df <- data.frame(position = start:(end - 1),
signal = unlist(values))
# Plot the result
library(ggplot2)
ggplot(df, aes(x = position, y = signal)) +
geom_line(color = "steelblue") +
theme_minimal() +
labs(title = paste("Signal at", chrom, "from", start, "to", end),
x = "Genomic position",
y = "Signal")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment