Skip to content

Commit

Permalink
Merge pull request #6 from CougPhenomics/fix-video-rootdir
Browse files Browse the repository at this point in the history
find .here to establish root dir for paths. also define column types and label gifs with treatment
  • Loading branch information
dschneiderch authored Nov 23, 2019
2 parents 8fc2321 + f0b8eec commit 03bc648
Showing 1 changed file with 26 additions and 21 deletions.
47 changes: 26 additions & 21 deletions scripts/makeVideos.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

# Get command line arguments
args = commandArgs(trailingOnly = T)
# args = c('output/from_diy_data', 'diy_data')
# args = c('output/from_diy_data', 'diy_data/genotype_map.csv')

# test if there are two arguments: if not, return an error
if (length(args)<2) {
Expand All @@ -23,22 +23,24 @@ load_install = function(pkg){
}

# load all packages
libs = c('magick','tidyverse','lubridate','av')
libs = c('magick','tidyverse','lubridate','av','rprojroot')
tmp = sapply(libs, FUN = load_install)


# setup directories
root = rprojroot::find_root('.here')
datadir = args[1]
indir = file.path(datadir, 'pseudocolor_images')
outdir = file.path(datadir, 'timelapse')
indir = file.path(root, datadir, 'pseudocolor_images')
outdir = file.path(root, datadir, 'timelapse')
dir.create(outdir, show = F, rec = T)

# get genotype info
gmap = read_csv(args[2])
gmap = read_csv(file.path(root,args[2]))

# get data processed
output = read_csv(file.path(datadir,'output_psII_level0.csv')) %>%
select(treatment, sampleid, roi, gtype)
output = read_csv(file.path(root, datadir,'output_psII_level0.csv'),
col_types = cols(treatment = col_character(), gtype = col_character())) %>%
dplyr::select(treatment, sampleid, roi, gtype)

# filter gmap for available output files
gmap = inner_join(gmap, output) %>% distinct(treatment, sampleid, roi, gtype)
Expand Down Expand Up @@ -75,24 +77,26 @@ cntrl_ids = unique(gmap %>% filter(treatment == 'control') %>% pull(sampleid))
l = cross2(fluc_ids, cntrl_ids)

# test values
# sampleid0 = 'tray2'
# sampleid1 = 'tray5'
# sampleid_c = 'tray2'
# sampleid_t = 'tray5'
# parameter_string = 'FvFm_YII'#'FvFm_YII' #'t300_ALon_YII'
# il = l[[1]]

# define gif making function
arrange_gif = function(il, parameter_string) {
uil = unlist(il, rec = F)
sampleid1 = uil[1]
sampleid0 = uil[2]
print(paste(sampleid0, sampleid1, sep = ' x '))
sampleid_t = uil[1]
sampleid_c = uil[2]
gtype_treatment_label_t = get_treatment(sampleid_t)
print(paste(sampleid_c, sampleid_t, sep = ' x '))
print(gtype_treatment_label_t)
print(parameter_string)

# get images
fns0 = dir(file.path(indir, sampleid0),
fns0 = dir(file.path(indir, sampleid_c),
pattern = parameter_string,
full.names = T)
fns1 = dir(file.path(indir, sampleid1),
fns1 = dir(file.path(indir, sampleid_t),
pattern = parameter_string,
full.names = T)

Expand All @@ -110,8 +114,8 @@ arrange_gif = function(il, parameter_string) {
fns1 <- fns1[elements1]

# get genotypes
g0 = gmap %>% filter(sampleid == sampleid0) %>% pull(gtype)
g1 = gmap %>% filter(sampleid == sampleid1) %>% pull(gtype)
g0 = gmap %>% filter(sampleid == sampleid_c) %>% pull(gtype)
g1 = gmap %>% filter(sampleid == sampleid_t) %>% pull(gtype)
# crossing(dtes0,dtes1) #TODO: filter dates and filenames for common dates

stopifnot(all(dtes0 == dtes1))
Expand All @@ -123,7 +127,7 @@ arrange_gif = function(il, parameter_string) {
# annotate with genotype
imgs0a = image_annotate(
imgs0,
get_treatment(sampleid0),
get_treatment(sampleid_c),
size = 24,
font = 'Arial',
weight = 700,
Expand All @@ -144,7 +148,7 @@ arrange_gif = function(il, parameter_string) {
)

coords %>%
filter(sampleid == sampleid0) %>%
filter(sampleid == sampleid_c) %>%
group_by(xpos, ypos, roi) %>%
group_walk(
keep = T,
Expand Down Expand Up @@ -178,7 +182,7 @@ arrange_gif = function(il, parameter_string) {

imgs1a = image_annotate(
imgs1,
get_treatment(sampleid1),
gtype_treatment_label_t,
size = 24,
font = 'Arial',
weight = 700,
Expand All @@ -198,7 +202,7 @@ arrange_gif = function(il, parameter_string) {
)

coords %>%
filter(sampleid == sampleid1) %>%
filter(sampleid == sampleid_t) %>%
group_by(xpos, ypos, roi) %>%
group_walk(
keep = T,
Expand Down Expand Up @@ -240,7 +244,8 @@ arrange_gif = function(il, parameter_string) {
}
}
# newgif
outfn = paste0(parameter_string, '_', sampleid0, '_x_', sampleid1, '.gif')
treatmentlabel = strsplit(gtype_treatment_label_t,' ')[[1]][2]
outfn = paste0(parameter_string, '_', sampleid_c, '_x_', sampleid_t, '_',treatmentlabel,'.gif')
image_write_video(newgif, file.path(outdir, outfn), framerate = 2)
# image_write_gif(newgif,file.path(outdir, outfn), delay=0.5)
}
Expand Down

0 comments on commit 03bc648

Please sign in to comment.