library(matools)
example_data <- system.file("extdata/demo_data",
  package = "matools",
  mustWork = TRUE
)
example_parameters <- system.file("extdata/demo_data",
  "demo_data_project_parameters.csv",
  package = "matools",
  mustWork = TRUE
)
 
for (filename in matools_env$files_to_process) {
  message(paste("Processing File:", filename))
  data <- asc_to_tibble(
    file_path = file.path(matools_env$directory_data, paste(filename, ".ASC", sep = ""))
  )
  # set experiment parameters for a single file
  set_experiment_parameters(parameters = matools_env$parameters, filename = filename)
  # modify the data frame
  calculate_stimulus_times(
    stimulus_time_first = matools_env$stimulus_time_of_first,
    stimulus_count = matools_env$stimulus_count,
    stimulus_isi = matools_env$stimulus_isi
  )
  data <- add_sweep_number_to_rows(
    df = data,
    sweep_duration = matools_env$sweep_duration_sec,
    sweep_count = matools_env$sweep_total,
    time_ref = "rec_time_ms"
  )
  data <- insert_rows_for_missing_sweeps(
    df = data,
    sweep_count = matools_env$sweep_total
  )
  data <- standardize_event_time(
    df = data,
    sweep_duration = matools_env$sweep_duration_sec,
    time_ref = "rec_time_ms"
  )
  data <- add_stimulus_index(
    df = data,
    time_of_stim = matools_env$time_of_stimuli,
    isi = matools_env$stimulus_isi,
    sweep_count = matools_env$sweep_total,
    recovery_stim = matools_env$stimulus_time_of_recovery
  )
  data <- add_event_index(data)
  data <- add_event_jitter(
    df = data,
    to_rise = FALSE
  )
  user_parameters <- tibble::tibble(
    condition_start = matools_env$condition_starts,
    condition_end = matools_env$condition_ends,
    condition = matools_env$condition_names
  )
  data <- add_condition_tag(
    df = data,
    parameters = user_parameters
  )
  df_histo <-
    create_histogram_df(
      df = data,
      time_of_stim = matools_env$time_of_stimuli,
      condition_names = matools_env$condition_names,
      bin_size = 1,
      window_size = 50,
      first_bin_on_stim = FALSE,
      one_event_per_bin = FALSE
    )
  # aggregate data for plotting
  if (!exists("data_collection")) {
    data_collection <-
      tibble::tibble(
        cell_id = filename,
        experiment_id = matools_env$experiment_id,
        data_events = list(data),
        data_histogram = list(df_histo)
      )
  } else {
    data_collection <-
      tibble::add_row(
        data_collection,
        cell_id = filename,
        experiment_id = matools_env$experiment_id,
        data_events = list(data),
        data_histogram = list(df_histo)
      )
  }
}
#> Processing File: cell1
#> Processing File: cell2