R/chmi.phen.R

Defines functions chmi.phen

#----------------------
# Get phenotype data
#----------------------

#' @export
chmi.phen <- function(
	data_type = c('ab_data', 'cyto_data', 'pbmc_data', 'hemo_data', 'none'),
	aim_data = c('aim_1', 'aim_2', 'aim_3', 'aim_4', 'none'),
	fold_change = FALSE,
	fc_traits = TRUE,
	group_tr = c('ab_full', 'ab_select', 'pbmc_full',  'none'),
	traits,
	filter,
	dir)
{
### require packages & theme ggplot2
  chmi.required_packages()


### argument dir
  if(missing(dir)){
    dir <- chmi.phendir()
  }
  stopifnot(file.exists(dir))


### argument filter
  if(!missing(filter)){
    stopifnot(class(filter) == 'character')
  }


### list of files
  dat_file <- list.files(
  	dir, pattern = '.csv',
    full.names = TRUE,
    recursive = TRUE)

 	# read data in 'dat_file'
		options(datatable.fread.dec.locale = 'es_ES.UTF-8')	## alert to ```locale``` config!!


### read data in 'dat.file'
  dat_imp <- lapply(dat_file, fread, sep = ';', dec = '.', na.strings = c('', 'NA'))

 	# update 'dat_imp'
  	dat_imp <- chmi.update_phenos.dat_imp(dat_imp)


### argument 'data_type'
  if(data_type == 'ab_data') {
  	# left_join()
			dat <- chmi.02_phen.argument_ab_data(dat_imp)
		
		# update by 'aim_data'
			dat <- chmi.03_phen.argument_aims(dat, data_type, aim_data)

	} else if(data_type == 'cyto_data') {
		# left_join()
			dat <- chmi.02_phen.argument_cyto_data(dat_imp)

  } else if(data_type == 'pbmc_data') {
  	# left_join()
			dat <- chmi.02_phen.argument_pbmc_data(dat_imp)
		
		# update by 'aim_data'
			dat <- chmi.03_phen.argument_aims(dat, data_type, aim_data)

  } else if(data_type == 'hemo_data') {
  	# left_join()
			dat <- chmi.02_phen.argument_hemo_data(dat_imp)

  } else if(data_type == 'none') {
  	# no join()
  		dat <- dat_imp[[1]]
  	
  }


### define 'l_vars' in '01_chmi_clinical' folder
	l_vars <- chmi.03_phen.argument_clinical_tr(data_type, aim_data)
	

### argument to select 'group_tr' and individual 'traits'
	if(group_tr != 'none' | !missing(traits)) {
  # 'group_tr' & 'traits' selections
  	dat <- chmi.stat.select_traits(dat, l_vars, group_tr, traits)
  }
  

### argument for 'fold_change'  
### full argument for aim_ab_data in 'chmi.04_phen.argument_fold.R'
	if(data_type == 'ab_data' & fold_change == TRUE) {
		# fold_change for 'ab_data'
			dat <- chmi.04_phen.argument_fc_ab(dat)
			
			# filter_if() 'L1_005' bacause 't_point == C_1' doesn't exist
				dat <- dat %>%
					filter_at(vars(original_id), any_vars(. != 'L1_005'))
					
	} else if(data_type == 'pbmc_data' & fold_change == TRUE) {
		# ratio for 'pbmc'
			dat <- chmi.04_phen.argument_fc_pbmc(dat)
	
	}


### argument for a 'fc_traits'
	if(fold_change == TRUE & fc_traits == TRUE) {
	# avoid ends_with '_c1'
		dat <- dat %>%
			select(-ends_with('_c1'))
	}


### define class of variables
	dat <- chmi.05_phen.arg_class_ab_data(dat)


### update 'dat' as_tibble()
	dat <- dat %>%
		as_tibble()


### returns
  return(dat)
}
mvazquezs/chmitools documentation built on May 1, 2020, 2:06 a.m.