rethomics / damr

Read TriKinetics' DAM data in R
http://rethomics.github.io
6 stars 7 forks source link

Plotting Siesta #41

Open Thejaswinisuso1 opened 1 year ago

Thejaswinisuso1 commented 1 year ago

Hello. I was trying to plot siesta (daytime sleep) only from ZT4-ZT8 (the peaking time points). But using rethomics, we can only plot from ZT0-ZT12 since they sum all day of sleep for a single channel. So can you help me to write a script using which I can quantify sleep during ZT4-ZT8? I appreciate any help you can provide. Theja

qgeissmann commented 1 year ago

Hi @Thejaswinisuso1, If you want to quantify:

# first, we can create a zt variable (i.e. t modulus one day)
dt[, zt := t %% behavr::hours(24)]
# then we compute the mean sleep in this interval, for each individual
dt[zt %between% behavr::hours(c(4, 8)) , .(siesta = mean(asleep)), by=key(dt)]

Note, you get siesta as a number between 0 and 1 , which is the proportion of time slept withing your selected time range (you can multiply that by 4h depending on how you want to express things)... Does it make sense? Regarding plotting, the easiest is to just show the time window you want from a larger plot:

pl <- ggetho(dt, aes(y = asleep), time_wrap = hours(24)) + 
         stat_pop_etho() + 
         coord_cartesian(xlim= hours(c(4,8)))
Thejaswinisuso1 commented 1 year ago

instead of dt, I took dt_curated so that I could avoid dead flies

dt_curated[, zt := t %% behavr::hours(24)]

mean Siesta ZT4-ZT8

dt[zt %in% behavr::hours(c(4, 8)) , .(siesta = mean(asleep)), by=key(dt)]

pl <- ggetho(dt, aes(y = asleep), time_wrap = hours(24)) + stat_pop_etho() + coord_cartesian(xlim= hours(c(4,8)))

summary_dt <- rejoin(dt_curated[xmv(genotype) %in% c(var.1) &
t %between% c(days(7),days(9)) , .(

this is where the computation happens

                  sleep_fraction_siesta = mean(asleep)),
                ,by=id])

summary_dt Rplot

summary_dt_melted <- melt(summary_dt, measure.vars = patterns("sleepfraction"), variable.name = "phase", value.name = "sle Rplot01 ep_fraction")

ggplot(summary_dt_melted, aes(x=phase, y=sleep_fraction, fi cs siesta plot ll=genotype)) + geom_boxplot(outlier.colour = NA) + scale_y_continuous(name= "Fraction of time sleeping",labels = scales::percent)

I rewrote it like this, but what I am getting is sleep during the whole day( averaging full day sleep= day time sleep+ Night time sleep), not sleep during ZT4-ZT8, which should be around 90% in my case. I have attached the images for your reference.

qgeissmann commented 1 year ago

oops, i edited my code to use %between% instead of %in%. otherwise, your last plot does not apply coord_cartesian(xlim= hours(c(4,8))) I am unsure i understand

Thejaswinisuso1 commented 1 year ago

metadata1.csv Monitor77.txt

Genotypes

var.1<-"CS"

###############################################################################################################################################################

Find the place of the data

DATA_DIR <- "/Users/thejaswinisusobhanan/Documents/Rethomics/Monitor 05:08:2022"

list.files(DATA_DIR, pattern= ".txt|.csv") setwd(DATA_DIR)

###############################################################################################################################################################

Metadata : number of flies, sex , gentotype

metadata <- fread("metadata1.csv")

metadata <- link_dam_metadata(metadata, result_dir = DATA_DIR)

metadata

###############################################################################################################################################################

Loading the data

dt <- load_dam(metadata) summary(dt)

###############################################################################################################################################################

Calculating sleep amount

dt <- load_dam(metadata, FUN = sleepr::sleep_dam_annotation)

Plotting the data 30 min bin

ggetho(dt, aes(z=asleep)) + stat_ld_annotations(height = 1)+ stat_tile_etho()

ggetho(dt, aes(y=asleep)) + stat_pop_etho() + stat_ld_annotations()

Plotting the data x min bin

ggetho(dt, aes(z=asleep), summary_time_window = mins(2)) + stat_ld_annotations(height = 1)+ stat_tile_etho()

ggetho(dt, aes(y=asleep), summary_time_window = mins(2)) + stat_pop_etho() + stat_ld_annotations()

###############################################################################################################################################################

Removing dead flies

dt_curated <- curate_dead_animals(dt) summary(dt_curated)

See which dead flies

setdiff(dt[, id, meta=T], dt_curated[, id, meta=T])

###############################################################################################################################################################

Plotting the data

ggetho(dt_curated, aes(z=asleep)) + stat_ld_annotations(height = 1)+ stat_tile_etho()

ggetho(dt_curated, aes(y=asleep)) + stat_pop_etho() + stat_ld_annotations()

Plotting Sleep of all days chossen in metadata

ggetho(dt_curated[xmv(genotype) %in% c(var.1) & t %between% c(days(08),days(10) ) ], aes(y=asleep, colour=genotype)) + stat_pop_etho() + stat_ld_annotations(period = hours(24), l_duration = hours(12))

Averging the sleep over multiple days chossen in metadata

ggetho(dt_curated[xmv(genotype) %in% c(var.1) & t %between% c(days(08),days(10)) ], aes(y=asleep, colour=genotype), time_wrap = hours(24)) + stat_pop_etho() + stat_ld_annotations(period = hours(24), l_duration = hours(12)) + scale_y_continuous(name= "Fraction of time sleeping",labels = scales::percent)

###############################################################################################################################################################

Define daytime period length and night time period length

dt_curated[, phase := ifelse(t %% hours(24) < hours(12), "L", "D")]

Average all day sleep amount for each fly over multiple days chossen in metadata

summary_dt <- rejoin(dt_curated[xmv(genotype) %in% c(var.1) &
t %between% c(days(08),days(10)) , .(

this is where the computation happens

                  sleep_fraction_all = mean(asleep),
                  sleep_fraction_l = mean(asleep[phase == "L"]),
                  sleep_fraction_d = mean(asleep[phase == "D"])
                ),
                ,by=id])

summary_dt

fraction of time sleeping

summary_dt_melted <- melt(summary_dt, measure.vars = patterns("sleepfraction"), variable.name = "phase", value.name = "sleep_fraction")

ggplot(summary_dt_melted, aes(x=phase, y=sleep_fraction, fill=genotype)) + geom_boxplot(outlier.colour = NA) + scale_y_continuous(name= "Fraction of time sleeping",labels = scales::percent)

Average all day sleep amount for each fly over multiple days chossen in metadata

summary_dt <- rejoin(dt_curated[xmv(genotype) %in% c(var.1) &
t %between% c(days(7),days(9)) , .(

                  sleep_fraction_all = mean(asleep),
                  sleep_fraction_l = mean(asleep[phase == "L"]),
                  sleep_fraction_d = mean(asleep[phase == "D"])
                ),
                ,by=id])

summary_dt

only Siesta plot

Siesta ZT4-ZT8

dt_curated[, zt := t %% behavr::hours(24)]

then we compute the mean sleep in this interval, for each individual

dt[zt %between% behavr::hours(c(4, 8)) , .(siesta = mean(asleep)), by=key(dt)]

how will I specify the days that I want to consider

to plot

pl <- ggetho(dt, aes(y = asleep), time_wrap = hours(24)) + stat_pop_etho() + coord_cartesian(xlim= hours(c(4,8)))

when I run this I get nothing so I run the next step

summary_dt <- rejoin(dt_curated[xmv(genotype) %in% c(var.1) &
t %between% c(days(7),days(9)) , .(sleep_fraction_siesta = mean(asleep)), ,by=id]) summary_dt

summary_dt_melted <- melt(summary_dt, measure.vars = patterns("sleepfraction"), variable.name = "phase", value.name = "sleep_fraction")

ggplot(summary_dt_melted, aes(x=phase, y=sleep_fraction, fill=genotype)) + geom_boxplot(outlier.colour = NA) + scale_y_continuous(name= "Fraction of time sleeping",labels = scales::percent)