77
88# Get command line arguments
99args = commandArgs(trailingOnly = T )
10- # args = c('output/from_diy_data', 'diy_data')
10+ # args = c('output/from_diy_data', 'diy_data/genotype_map.csv ')
1111
1212# test if there are two arguments: if not, return an error
1313if (length(args )< 2 ) {
@@ -23,22 +23,24 @@ load_install = function(pkg){
2323}
2424
2525# load all packages
26- libs = c(' magick' ,' tidyverse' ,' lubridate' ,' av' )
26+ libs = c(' magick' ,' tidyverse' ,' lubridate' ,' av' , ' rprojroot ' )
2727tmp = sapply(libs , FUN = load_install )
2828
2929
3030# setup directories
31+ root = rprojroot :: find_root(' .here' )
3132datadir = args [1 ]
32- indir = file.path(datadir , ' pseudocolor_images' )
33- outdir = file.path(datadir , ' timelapse' )
33+ indir = file.path(root , datadir , ' pseudocolor_images' )
34+ outdir = file.path(root , datadir , ' timelapse' )
3435dir.create(outdir , show = F , rec = T )
3536
3637# get genotype info
37- gmap = read_csv(args [2 ])
38+ gmap = read_csv(file.path( root , args [2 ]) )
3839
3940# get data processed
40- output = read_csv(file.path(datadir ,' output_psII_level0.csv' )) %> %
41- select(treatment , sampleid , roi , gtype )
41+ output = read_csv(file.path(root , datadir ,' output_psII_level0.csv' ),
42+ col_types = cols(treatment = col_character(), gtype = col_character())) %> %
43+ dplyr :: select(treatment , sampleid , roi , gtype )
4244
4345# filter gmap for available output files
4446gmap = inner_join(gmap , output ) %> % distinct(treatment , sampleid , roi , gtype )
@@ -75,24 +77,26 @@ cntrl_ids = unique(gmap %>% filter(treatment == 'control') %>% pull(sampleid))
7577l = cross2(fluc_ids , cntrl_ids )
7678
7779# test values
78- # sampleid0 = 'tray2'
79- # sampleid1 = 'tray5'
80+ # sampleid_c = 'tray2'
81+ # sampleid_t = 'tray5'
8082# parameter_string = 'FvFm_YII'#'FvFm_YII' #'t300_ALon_YII'
8183# il = l[[1]]
8284
8385# define gif making function
8486arrange_gif = function (il , parameter_string ) {
8587 uil = unlist(il , rec = F )
86- sampleid1 = uil [1 ]
87- sampleid0 = uil [2 ]
88- print(paste(sampleid0 , sampleid1 , sep = ' x ' ))
88+ sampleid_t = uil [1 ]
89+ sampleid_c = uil [2 ]
90+ gtype_treatment_label_t = get_treatment(sampleid_t )
91+ print(paste(sampleid_c , sampleid_t , sep = ' x ' ))
92+ print(gtype_treatment_label_t )
8993 print(parameter_string )
9094
9195 # get images
92- fns0 = dir(file.path(indir , sampleid0 ),
96+ fns0 = dir(file.path(indir , sampleid_c ),
9397 pattern = parameter_string ,
9498 full.names = T )
95- fns1 = dir(file.path(indir , sampleid1 ),
99+ fns1 = dir(file.path(indir , sampleid_t ),
96100 pattern = parameter_string ,
97101 full.names = T )
98102
@@ -110,8 +114,8 @@ arrange_gif = function(il, parameter_string) {
110114 fns1 <- fns1 [elements1 ]
111115
112116 # get genotypes
113- g0 = gmap %> % filter(sampleid == sampleid0 ) %> % pull(gtype )
114- g1 = gmap %> % filter(sampleid == sampleid1 ) %> % pull(gtype )
117+ g0 = gmap %> % filter(sampleid == sampleid_c ) %> % pull(gtype )
118+ g1 = gmap %> % filter(sampleid == sampleid_t ) %> % pull(gtype )
115119 # crossing(dtes0,dtes1) #TODO: filter dates and filenames for common dates
116120
117121 stopifnot(all(dtes0 == dtes1 ))
@@ -123,7 +127,7 @@ arrange_gif = function(il, parameter_string) {
123127 # annotate with genotype
124128 imgs0a = image_annotate(
125129 imgs0 ,
126- get_treatment(sampleid0 ),
130+ get_treatment(sampleid_c ),
127131 size = 24 ,
128132 font = ' Arial' ,
129133 weight = 700 ,
@@ -144,7 +148,7 @@ arrange_gif = function(il, parameter_string) {
144148 )
145149
146150 coords %> %
147- filter(sampleid == sampleid0 ) %> %
151+ filter(sampleid == sampleid_c ) %> %
148152 group_by(xpos , ypos , roi ) %> %
149153 group_walk(
150154 keep = T ,
@@ -178,7 +182,7 @@ arrange_gif = function(il, parameter_string) {
178182
179183 imgs1a = image_annotate(
180184 imgs1 ,
181- get_treatment( sampleid1 ) ,
185+ gtype_treatment_label_t ,
182186 size = 24 ,
183187 font = ' Arial' ,
184188 weight = 700 ,
@@ -198,7 +202,7 @@ arrange_gif = function(il, parameter_string) {
198202 )
199203
200204 coords %> %
201- filter(sampleid == sampleid1 ) %> %
205+ filter(sampleid == sampleid_t ) %> %
202206 group_by(xpos , ypos , roi ) %> %
203207 group_walk(
204208 keep = T ,
@@ -240,7 +244,8 @@ arrange_gif = function(il, parameter_string) {
240244 }
241245 }
242246 # newgif
243- outfn = paste0(parameter_string , ' _' , sampleid0 , ' _x_' , sampleid1 , ' .gif' )
247+ treatmentlabel = strsplit(gtype_treatment_label_t ,' ' )[[1 ]][2 ]
248+ outfn = paste0(parameter_string , ' _' , sampleid_c , ' _x_' , sampleid_t , ' _' ,treatmentlabel ,' .gif' )
244249 image_write_video(newgif , file.path(outdir , outfn ), framerate = 2 )
245250 # image_write_gif(newgif,file.path(outdir, outfn), delay=0.5)
246251}
0 commit comments