Skip to content

Instantly share code, notes, and snippets.

@antortjim
Last active October 30, 2024 13:32
Show Gist options
  • Save antortjim/7cebdce07acab983e4e1c9e589886e79 to your computer and use it in GitHub Desktop.
Save antortjim/7cebdce07acab983e4e1c9e589886e79 to your computer and use it in GitHub Desktop.
Script to load ethoscope data using rethomics and generate plots and tables that quantify behavior and make it easy to select the flies that sleep rebound the best
genotype sex motor reason selected schedule region_id machine_name date reference_hour node motility age_min age_max incubator food age food framerate vial batch
2u MAL YES OK NO 2024-10-11 23:00:00 > 2024-10-12 11:00:00 1 ETHOSCOPE_006 2024-10-10 11 cv1 OK 8 9 T1 0 cornmeal 2 cornmeal AX
2u MAL YES OK NO 2024-10-11 23:00:00 > 2024-10-12 11:00:00 3 ETHOSCOPE_006 2024-10-10 11 cv1 OK 8 8 T1 0 cornmeal 2 cornmeal AX
2u MAL FAIL trace NO 2024-10-11 23:00:00 > 2024-10-12 11:00:00 5 ETHOSCOPE_006 2024-10-10 11 cv1 OK 8 8 T1 0 cornmeal 2 cornmeal AX
2u MAL YES OK NO 2024-10-11 23:00:00 > 2024-10-12 11:00:00 7 ETHOSCOPE_006 2024-10-10 11 cv1 OK 8 8 T1 0 cornmeal 2 cornmeal AX
2u MAL YES OK 2u2 2024-10-11 23:00:00 > 2024-10-12 11:00:00 9 ETHOSCOPE_006 2024-10-10 11 cv1 OK 8 8 T1 0 cornmeal 2 cornmeal AX
2u MAL YES OK 2u1 2024-10-11 23:00:00 > 2024-10-12 11:00:00 12 ETHOSCOPE_006 2024-10-10 11 cv1 OK 8 8 T1 0 cornmeal 2 cornmeal AX
2u MAL YES OK NO 2024-10-11 23:00:00 > 2024-10-12 11:00:00 14 ETHOSCOPE_006 2024-10-10 11 cv1 OK 8 8 T1 0 cornmeal 2 cornmeal AX
2u VFEM LQ OK NO 2024-10-11 23:00:00 > 2024-10-12 11:00:00 16 ETHOSCOPE_006 2024-10-10 11 cv1 OK 9 9 T1 0 cornmeal 2 cornmeal AX
2u VFEM YES OK NO 2024-10-11 23:00:00 > 2024-10-12 11:00:00 18 ETHOSCOPE_006 2024-10-10 11 cv1 OK 9 9 T1 0 cornmeal 2 cornmeal AX
2u VFEM YES OK 2u1 2024-10-11 23:00:00 > 2024-10-12 11:00:00 20 ETHOSCOPE_006 2024-10-10 11 cv1 OK 9 9 T1 0 cornmeal 2 cornmeal AX
2u VFEM YES OK NO 2024-10-11 23:00:00 > 2024-10-12 11:00:00 1 ETHOSCOPE_019 2024-10-10 11 cv1 OK 14 14 T4 0 cornmeal 2 cornmeal AX
2u VFEM YES OK 2u1 2024-10-11 23:00:00 > 2024-10-12 11:00:00 3 ETHOSCOPE_019 2024-10-10 11 cv1 OK 14 14 T4 0 cornmeal 2 cornmeal AX
2u VFEM YES OK 2u2 2024-10-11 23:00:00 > 2024-10-12 11:00:00 5 ETHOSCOPE_019 2024-10-10 11 cv1 OK 14 14 T4 0 cornmeal 2 cornmeal AX
2u VFEM YES OK NO 2024-10-11 23:00:00 > 2024-10-12 11:00:00 7 ETHOSCOPE_019 2024-10-10 11 cv1 OK 14 14 T4 0 cornmeal 2 cornmeal AX
2u VFEM YES OK NO 2024-10-11 23:00:00 > 2024-10-12 11:00:00 9 ETHOSCOPE_019 2024-10-10 11 cv1 OK 14 14 T4 0 cornmeal 2 cornmeal AX
2u VFEM YES OK NO 2024-10-11 23:00:00 > 2024-10-12 11:00:00 12 ETHOSCOPE_019 2024-10-10 11 cv1 OK 14 14 T4 0 cornmeal 2 cornmeal AX
2u VFEM YES OK NO 2024-10-11 23:00:00 > 2024-10-12 11:00:00 14 ETHOSCOPE_019 2024-10-10 11 cv1 OK 14 14 T4 0 cornmeal 2 cornmeal AX
2u VFEM YES OK NO 2024-10-11 23:00:00 > 2024-10-12 11:00:00 16 ETHOSCOPE_019 2024-10-10 11 cv1 OK 14 14 T4 0 cornmeal 2 cornmeal AX
2u MAL YES OK 2u2 2024-10-11 23:00:00 > 2024-10-12 11:00:00 18 ETHOSCOPE_019 2024-10-10 11 cv1 OK 14 14 T4 0 cornmeal 2 cornmeal AX
2u MAL YES OK NO 2024-10-11 23:00:00 > 2024-10-12 11:00:00 20 ETHOSCOPE_019 2024-10-10 11 cv1 OK 14 14 T4 0 cornmeal 2 cornmeal AX
2u MAL YES OK NO 2024-10-11 23:00:00 > 2024-10-12 11:00:00 1 ETHOSCOPE_043 2024-10-10 11 cv1 OK 14 14 T4 0 cornmeal 2 cornmeal AX
2u VFEM LQ OK NO 2024-10-11 23:00:00 > 2024-10-12 11:00:00 3 ETHOSCOPE_043 2024-10-10 11 cv1 OK 13 13 T4 0 cornmeal 2 cornmeal AX
2u VFEM YES OK NO 2024-10-11 23:00:00 > 2024-10-12 11:00:00 5 ETHOSCOPE_043 2024-10-10 11 cv1 OK 13 13 T4 0 cornmeal 2 cornmeal AX
2u VFEM LQ OK NO 2024-10-11 23:00:00 > 2024-10-12 11:00:00 7 ETHOSCOPE_043 2024-10-10 11 cv1 OK 13 13 T4 0 cornmeal 2 cornmeal AX
2u VFEM LQ OK NO 2024-10-11 23:00:00 > 2024-10-12 11:00:00 9 ETHOSCOPE_043 2024-10-10 11 cv1 OK 13 13 T4 0 cornmeal 2 cornmeal AX
2u VFEM YES OK 2u2 2024-10-11 23:00:00 > 2024-10-12 11:00:00 12 ETHOSCOPE_043 2024-10-10 11 cv1 OK 8 8 T4 0 cornmeal 2 cornmeal AX
2u VFEM FAIL trace NO 2024-10-11 23:00:00 > 2024-10-12 11:00:00 14 ETHOSCOPE_043 2024-10-10 11 cv1 OK 8 8 T4 0 cornmeal 2 cornmeal AX
2u VFEM LQ OK NO 2024-10-11 23:00:00 > 2024-10-12 11:00:00 16 ETHOSCOPE_043 2024-10-10 11 cv1 OK 8 8 T4 0 cornmeal 2 cornmeal AX
2u VFEM LQ OK NO 2024-10-11 23:00:00 > 2024-10-12 11:00:00 18 ETHOSCOPE_043 2024-10-10 11 cv1 OK 8 8 T4 0 cornmeal 2 cornmeal AX
2u VFEM LQ OK NO 2024-10-11 23:00:00 > 2024-10-12 11:00:00 20 ETHOSCOPE_043 2024-10-10 11 cv1 OK 8 8 T4 0 cornmeal 2 cornmeal AX
2u MAL YES OK NO 2024-10-11 23:00:00 > 2024-10-12 11:00:00 14 ETHOSCOPE_002 2024-10-10 11 cv1 OK 13 13 T1 0 cornmeal 2 cornmeal AX
2u MAL YES OK NO 2024-10-11 23:00:00 > 2024-10-12 11:00:00 16 ETHOSCOPE_002 2024-10-10 11 cv1 OK 13 13 T1 0 cornmeal 2 cornmeal AX
2u MAL YES OK 2u1 2024-10-11 23:00:00 > 2024-10-12 11:00:00 18 ETHOSCOPE_002 2024-10-10 11 cv1 OK 13 13 T1 0 cornmeal 2 cornmeal AX
2u MAL YES OK NO 2024-10-11 23:00:00 > 2024-10-12 11:00:00 20 ETHOSCOPE_002 2024-10-10 11 cv1 OK 13 13 T1 0 cornmeal 2 cornmeal AX
id_labeller <- function(id, with_date = FALSE) {
if (with_date) {
paste(
substr(id, 1, 10),
substr(id, 22, 23),
substr(id, 28, 29)
)
} else {
paste(
substr(id, 22, 23),
substr(id, 28, 29)
)
}
}
SUMMARY_TIME_WINDOW_MINS <- 30
VELOCITY_CORRECTION_COEF <- 0.0048
TIME_WINDOW_LENGTH <- 10
MIN_TIME_IMMOBILE <- 300
ETHOSCOPE_DATABASE <- "/ethoscope_data/results"
ETHOSCOPE_CACHE <- "/ethoscope_data/cache"
ZT_END_OF_EXPERIMENT <- 60 # ZT when later data is not needed in the plots
filename: "2024-10-10_metadata.csv"
loading_dates:
- "2024-10-10"
library(data.table)
library(scopr)
library(ggetho)
library(ggprism)
library(yaml)
theme_set(ggprism::theme_prism(base_size = 20) + theme(
text = element_text(size = 10)
))
source("constants.R")
PARAMETERS_FILE <- "parameters.yaml"
stopifnot(file.exists(PARAMETERS_FILE))
parameters <- yaml::read_yaml(PARAMETERS_FILE)
METADATA_FILENAME <- parameters$filename
LOADING_DATES <- parameters$loading_dates
# Load metadata and link to database
# Metadata must have the following fields:
# region_id: Integer identifying the position in the arena where the fly was loaded
# date: Character describing the day when the fly was loaded in an ethoscope,
# in GMT time and format YYYY-MM-DD
# machine_name: Name of the ethoscope where the fly was loaded, typically ETHOSCOPE_XXX
# reference_hour: Numeric describing when does the fly experience ZT0, in GMT
# motor: One of YES, NO, or FAIL depending on whether the fly was intended to be sleep deprived or not.
# If it was intended, whether it was succesful or not
# sex: Character describing what is the sex of the fly. Recommended: MAL, FEM and VFEM (for virgins)
# Other useful fields you want to have in your metadata but not required by this code
# genotype
# food
# schedule
metadata <- data.table::fread(METADATA_FILENAME)
metadata[, date := as.character(date)]
metadata[, region_id := as.integer(region_id)]
metadata <- metadata[
date %in% LOADING_DATES &
motor == "YES",
]
metadata <- scopr::link_ethoscope_metadata(metadata, result_dir = ETHOSCOPE_DATABASE)
metadata[, date := substr(as.character(datetime), 1, 10)]
metadata[, fly_no := paste0(substr(machine_name, 11, 13), "_", region_id)]
# Load ethoscope data using linked metadata
dt <- scopr::load_ethoscope(
metadata,
reference_hour = NA,
verbose = TRUE,
cache = ETHOSCOPE_CACHE,
FUN = sleepr::sleep_annotation,
time_window_length = TIME_WINDOW_LENGTH,
velocity_correction_coef = VELOCITY_CORRECTION_COEF,
min_time_immobile = MIN_TIME_IMMOBILE
)
# Quantify behavior in bins of SUMMARY_TIME_WINDOW_MINS minutes
dt_bin <- behavr::bin_apply_all(
dt,
y = "asleep",
x_bin_length = behavr::mins(SUMMARY_TIME_WINDOW_MINS),
FUN = mean
)
# Project rebound data on baseline for nice visualization
df0 <- behavr::rejoin(dt_bin)
df0_baseline <- data.table::copy(df0)
df0_baseline[, trace := "Baseline"]
df0_rebound <- data.table::copy(df0)
df0_rebound[, t := t - behavr::days(1)]
df0_rebound[, trace := "Rebound"]
df <- rbind(df0_baseline, df0_rebound)
df[, trace := factor(trace, levels = c("Baseline", "Rebound"))]
# Plot per fly
options(repr.plot.height = 10, repr.plot.width = 30)
gg <- ggplot(
data = df[t < behavr::hours(ZT_END_OF_EXPERIMENT)][
(trace == "Baseline") | (t >= behavr::days(1) & t < behavr::days(1.5)),
],
mapping = aes(x = t, y = asleep * 30, linetype = trace)
) +
stat_pop_etho(aes(color = sex)) +
scale_y_continuous(name = paste0("Sleep per ", SUMMARY_TIME_WINDOW_MINS, " min bin (min)"), limits = c(0, 30), breaks = seq(0, 30, 5)) +
stat_ld_annotations(height = 1, color = NA, alpha = 0.2) +
guides(linetype = "none") +
scale_x_hours(name = "ZT", breaks = behavr::days(seq(0, 2.5, 0.5))) +
facet_wrap("id", labeller = labeller(id = id_labeller))
suppressWarnings({
print(gg)
ggplot2::ggsave(
plot = gg,
filename = "plot_per_fly.svg",
width = 30, height = 10
)
})
# # Plot per sex
options(repr.plot.width = 20, repr.plot.height = 6)
gg_sd <- ggplot(
data = df[t < behavr::hours(ZT_END_OF_EXPERIMENT), ][
(trace == "Baseline") | (t >= behavr::days(1) & t < behavr::days(1.5)),
],
mapping = aes(x = t, y = asleep * 30, linetype = trace)
) +
stat_pop_etho(color = "black") +
scale_y_continuous(
name = paste0("Sleep per ", SUMMARY_TIME_WINDOW_MINS, " min bin (min)"),
limits = c(0, 30), breaks = seq(0, 30, 5)
) +
stat_ld_annotations(height = 1, color = NA, alpha = 0.2) +
guides(linetype = "none") +
scale_x_hours(name = "ZT", breaks = behavr::days(seq(0, 2.5, 0.5)))
gg <- gg_sd + facet_grid(sex ~ .)
suppressWarnings({
print(gg)
ggplot2::ggsave(
plot = gg,
filename = "plot_per_sex.svg",
width = 20, height = 6
)
})
@antortjim
Copy link
Author

Put all three files in a directory in a computer with an ethocope database. Modify the parameters.yaml file to load the flies you want and execute sleep_rebound_selection.R

@antortjim
Copy link
Author

The provided 2024-10-10_metadata.csv file is the example metadata that generates the example figures here
plot_per_fly
plot_per_sex

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment