R/m067.phen.update_spnt.R

Defines functions m067.update.dat_m21_m32 m067.update.dat_04_m21_m32 m067.update.dat_03_m32 m067.update.dat_03_m21 m067.update.dat_02_m32 m067.update.dat_02_m21

#-----------------------
# Update `m067.phen`
#-----------------------


### update 'bagamoyo'-------------------------------------------------------------------------------

# update 'm067_dat' in 'dat_imp[[4]]' (bagamoyo)
m067.update.dat_02_m21 <- function(dat_imp)
{
# stimulators
  l_stim <- c('CSP', 'DMSO', 'PHA', 'HBS', 'AMA1')

# update class of the variable 'dat_imp[[4]]'
  dat_imp[[3]] <- dat_imp[[3]] %>%
    filter(comment != 'non_stimulated') %>%
  	mutate(sid = paste0('B_', sid_ori),
    	t_point = factor(ifelse(t_event == '23', 'M21', 'NA'), levels = c('M21')),
    	site = factor(paste0('bagamoyo')),
    	stimul_spnt = factor(stimul_spnt, levels = c('1', '2', '3', '4', '5')),
    	plt_spnt = as.factor(plt_spnt))

    # check
      stopifnot(!is.na(dat_imp[[3]][['comment']]))

    # create 't_stimul'
      dat_imp[[3]] <- dat_imp[[3]] %>%
        slice(rep(1:n(), times = paste0(stimul_spnt))) %>%
        mutate(t_stimul = factor(
          ifelse(stimul_spnt == '1', l_stim[1],
          ifelse(stimul_spnt == '2', l_stim[1:2],
          ifelse(stimul_spnt == '3', l_stim[1:3],
          ifelse(stimul_spnt == '4', l_stim[1:4],
          ifelse(stimul_spnt == '5', l_stim[1:5], 'NA')))))))

  # select variables
    dat_imp[[3]] <- dat_imp[[3]] %>%
      select(sid, t_stimul, t_point, plt_spnt,
      rows_spnt, cols_spnt, site) %>%
      droplevels()

# return
  return(dat_imp)
}



# update 'm067_dat' in 'dat_imp[[4]]' (bagamoyo)
m067.update.dat_02_m32 <- function(dat_imp)
{
# stimulators
  l_stim <- c('CSP', 'DMSO', 'PHA', 'HBS', 'AMA1')

# update class of the variable 'dat_imp[[4]]'
  dat_imp[[4]] <- dat_imp[[4]] %>%
    filter(!str_detect(comment, 'all_frozen')) %>%
  	mutate(sid = paste0('B_', sid_ori),
    	t_point = factor(ifelse(t_event == '34', 'M32', 'NA'), levels = c('M32')),
    	site = factor(paste0('bagamoyo')),
    	plt_spnt = factor(plt_spnt),
    	stimul_spnt = factor(stimul_spnt, levels = c('2', '3', '4', '5')))

    # check
      stopifnot(any(!is.na(dat_imp[[4]] == FALSE)))

    # create 't_stimul'
      dat_imp[[4]] <- dat_imp[[4]] %>%
        slice(rep(1:n(), times = paste0(stimul_spnt))) %>%
        mutate(t_stimul = factor(
          ifelse(stimul_spnt == '2', l_stim[1:2],
          ifelse(stimul_spnt == '3', l_stim[1:3],
          ifelse(stimul_spnt == '4', l_stim[1:4],
          ifelse(stimul_spnt == '5', l_stim[1:5], 'NA'))))))

  # select variables
    dat_imp[[4]] <- dat_imp[[4]] %>%
      select(sid, t_stimul, t_point, plt_spnt,
      rows_spnt, cols_spnt, site) %>%
      droplevels()

# return
  return(dat_imp)
}


### update 'manhica'--------------------------------------------------------------------------------

# update 'm067_dat' in 'dat_imp[[6]]' (manhiça)
m067.update.dat_03_m21 <- function(dat_imp)
{
# stimulators
  l_stim <- c('CSP', 'DMSO', 'PHA', 'HBS', 'AMA1')

# update class of the variable 'dat_imp[[6]]'
  dat_imp[[6]] <- dat_imp[[6]] %>%
  	mutate(plt_spnt = factor(plt_spnt),
    sid = paste0('M_', sid_ori),
    rows_spnt = factor(rows_spnt),
    cols_spnt = factor(cols_spnt),
    ppos_spnt = factor(ppos_spnt),
    stimul_spnt = factor(stimul_spnt, levels = c('3', '4', '5')),
    agec = factor(agec, levels = c('5_17m', '6_12w')),
    t_point = factor(t_point),
    site = factor(site))

    # create 't_stimul'
    dat_imp[[6]] <- dat_imp[[6]] %>%
      slice(rep(1:n(), times = paste0(stimul_spnt))) %>%
      mutate(t_stimul = factor(
        ifelse(stimul_spnt == '3', l_stim[1:3],
        ifelse(stimul_spnt == '4', l_stim[1:4],
        ifelse(stimul_spnt == '5', l_stim[1:5], 'NA')))))

  # select variables
    dat_imp[[6]] <- dat_imp[[6]] %>%
      select(sid, t_stimul, t_point, plt_spnt,
        rows_spnt, cols_spnt, site)

# return
  return(dat_imp)
}


# update 'm067_dat' in 'dat_imp[[7]]' (manhica)
m067.update.dat_03_m32 <- function(dat_imp)
{
# stimulators
  l_stim <- c('CSP', 'DMSO', 'PHA', 'HBS', 'AMA1')

# update class of the variable 'dat_imp[[7]]'
  dat_imp[[7]] <- dat_imp[[7]] %>%
  	mutate(plt_spnt = factor(plt_spnt),
    sid = paste0('M_', sid_ori),
    rows_spnt = factor(rows_spnt),
    cols_spnt = factor(cols_spnt),
    ppos_spnt = factor(ppos_spnt),
    stimul_spnt = factor(stimul_spnt, levels = c('3', '5')),
    agec = factor(agec, levels = c('5_17m', '6_12w')),
    t_point = factor(t_point),
    site = factor(site))

    # create 't_stimul'
    dat_imp[[7]] <- dat_imp[[7]] %>%
      slice(rep(1:n(), times = paste0(stimul_spnt))) %>%
      mutate(t_stimul = factor(
        ifelse(stimul_spnt == '3', l_stim[1:3],
        ifelse(stimul_spnt == '5', l_stim[1:5], 'NA'))))

  # select variables
    dat_imp [[7]] <- dat_imp[[7]] %>%
      select(sid, t_stimul, t_point, plt_spnt,
      rows_spnt, cols_spnt, site)

# return
  return(dat_imp)
}



### update 'lambarene'------------------------------------------------------------------------------

# update 'm067_dat' in 'dat_imp[[9]]' (lambarene)
m067.update.dat_04_m21_m32 <- function(dat_imp)
{
# update class of the variable 'dat_imp[[9]]'
  dat_imp[[9]] <- dat_imp[[9]] %>%
    filter(t_stimul != '-') %>%
  	mutate(t_point = factor(
      ifelse(visit == '23', 'M21',
      ifelse(visit == '34', 'M32',
      'NA')),
      levels = c('M21', 'M32')),
    sid = paste0('L_', brady),
    t_stimul = factor(t_stimul, levels = c('CSP', 'DMSO', 'PHA', 'HBS', 'AMA1', 'check_plt')),
    site = factor(paste0('lambarene')))

  # avoid duplicated 'sid' & 't_stimul'
    dat_imp[[9]] <- dat_imp[[9]] %>%
      distinct(sid, t_stimul, .keep_all = TRUE) %>%
      select(sid, t_stimul, t_point, plt_spnt,
      rows_spnt, cols_spnt, site)

# return
  return(dat_imp)
}


### update 'dat_m21_m32'----------------------------------------------------------------------------

# update 'dat_m21_m32' (all sites)
m067.update.dat_m21_m32 <- function(dat_m21_m32)
{
# 'l_vars' to filter
	l_vars <- c('plt_spnt', 'rows_spnt', 'cols_spnt')

 	# complete.cases only & 'complete.cases()' avoid NA in 'plt_spnt'
 	  dat_m21_m32 <- dat_m21_m32 %>%
      filter_at(vars(l_vars), all_vars(complete.cases(.))) %>%
      mutate(sid_stimul = factor(paste0(sid, '_', t_stimul)),
        plt_spnt = factor(
        ifelse(site == 'bagamoyo', paste0('B_', plt_spnt),
        ifelse(site == 'manhica', paste0('M_', plt_spnt),
        ifelse(site == 'lambarene', paste0('L_', plt_spnt),
        'NA')))),
      rows_spnt = factor(rows_spnt),
      cols_spnt = factor(cols_spnt),
      t_point = factor(ifelse(!is.na(t_point), paste0(t_point), 'check_point'),
        levels = c('M21', 'M32', 'check_point')))

   # ensure 'B_NA' out
   dat_m21_m32 <- dat_m21_m32 %>%
      filter(!plt_spnt == 'B_NA') %>%
      droplevels()

# return
	return(dat_m21_m32)
}
mvazquezs/m067tools documentation built on April 19, 2019, 7:07 p.m.