2 Data Import and Preprocessing

This section preprocess and import raw data (in separate excel .xlsx files provided by Dan Hallinger) into R.

rm(list=ls())
library(plotly)
library(tidyverse)
library(ggthemes)
library(stringr)
library(readxl)
library(devtools)
load_all("./toxplot")
session_info()
##  setting  value                       
##  version  R version 3.3.0 (2016-05-03)
##  system   x86_64, mingw32             
##  ui       RTerm                       
##  language (EN)                        
##  collate  English_United States.1252  
##  tz       America/New_York            
##  date     2018-04-04                  
## 
##  package      * version  date       source                           
##  assertthat     0.1      2013-12-06 CRAN (R 3.3.1)                   
##  backports      1.0.5    2017-01-18 CRAN (R 3.3.2)                   
##  bindr          0.1      2016-11-13 CRAN (R 3.3.3)                   
##  bindrcpp       0.2      2017-06-17 CRAN (R 3.3.3)                   
##  bookdown       0.3.20   2017-05-15 Github (rstudio/bookdown@86026cf)
##  broom          0.4.1    2016-06-24 CRAN (R 3.3.1)                   
##  chron          2.3-47   2015-06-24 CRAN (R 3.3.1)                   
##  colorspace     1.3-0    2016-11-10 CRAN (R 3.3.2)                   
##  commonmark     1.2      2017-03-01 CRAN (R 3.3.3)                   
##  data.table     1.9.6    2015-09-19 CRAN (R 3.3.1)                   
##  DBI            0.5-1    2016-09-10 CRAN (R 3.3.1)                   
##  devtools     * 1.12.0   2016-06-24 CRAN (R 3.3.1)                   
##  digest         0.6.12   2017-01-27 CRAN (R 3.3.3)                   
##  dplyr        * 0.7.1    2017-06-22 CRAN (R 3.3.3)                   
##  evaluate       0.10     2016-10-11 CRAN (R 3.3.2)                   
##  forcats        0.2.0    2017-01-23 CRAN (R 3.3.3)                   
##  foreign        0.8-67   2016-09-13 CRAN (R 3.3.2)                   
##  ggplot2      * 2.2.1    2016-12-30 CRAN (R 3.3.3)                   
##  ggthemes     * 3.4.0    2017-02-19 CRAN (R 3.3.3)                   
##  glue           1.1.1    2017-06-21 CRAN (R 3.3.3)                   
##  gtable         0.2.0    2016-02-26 CRAN (R 3.3.1)                   
##  haven          1.0.0    2016-09-23 CRAN (R 3.3.2)                   
##  hms            0.3      2016-11-22 CRAN (R 3.3.2)                   
##  htmltools      0.3.6    2017-04-28 CRAN (R 3.3.3)                   
##  htmlwidgets    0.8      2016-11-09 CRAN (R 3.3.2)                   
##  httr           1.2.1    2016-07-03 CRAN (R 3.3.1)                   
##  jsonlite       1.4      2017-04-08 CRAN (R 3.3.3)                   
##  knitr          1.15.20  2017-05-15 Github (yihui/knitr@f3a490b)     
##  lattice        0.20-34  2016-09-06 CRAN (R 3.3.2)                   
##  lazyeval       0.2.0    2016-06-12 CRAN (R 3.3.1)                   
##  lubridate      1.6.0    2016-09-13 CRAN (R 3.3.3)                   
##  magrittr       1.5      2014-11-22 CRAN (R 3.3.1)                   
##  memoise        1.0.0    2016-01-29 CRAN (R 3.3.1)                   
##  mnormt         1.5-5    2016-10-15 CRAN (R 3.3.2)                   
##  modelr         0.1.0    2016-08-31 CRAN (R 3.3.2)                   
##  munsell        0.4.3    2016-02-13 CRAN (R 3.3.1)                   
##  nlme           3.1-128  2016-05-10 CRAN (R 3.3.2)                   
##  numDeriv       2016.8-1 2016-08-27 CRAN (R 3.3.1)                   
##  pkgconfig      2.0.1    2017-03-21 CRAN (R 3.3.3)                   
##  plotly       * 4.7.0    2017-05-28 CRAN (R 3.3.3)                   
##  plyr           1.8.4    2016-06-08 CRAN (R 3.3.1)                   
##  psych          1.6.9    2016-09-17 CRAN (R 3.3.2)                   
##  purrr        * 0.2.2    2016-06-18 CRAN (R 3.3.1)                   
##  R6             2.2.2    2017-06-17 CRAN (R 3.3.3)                   
##  RColorBrewer   1.1-2    2014-12-07 CRAN (R 3.3.1)                   
##  Rcpp           0.12.12  2017-07-15 CRAN (R 3.3.3)                   
##  readr        * 1.0.0    2016-08-03 CRAN (R 3.3.1)                   
##  readxl       * 0.1.1    2016-03-28 CRAN (R 3.3.1)                   
##  reshape2       1.4.2    2016-10-22 CRAN (R 3.3.2)                   
##  rlang          0.1.1    2017-05-18 CRAN (R 3.3.3)                   
##  rmarkdown      1.6      2017-06-15 CRAN (R 3.3.3)                   
##  RMySQL         0.10.9   2016-05-08 CRAN (R 3.3.1)                   
##  roxygen2       6.0.1    2017-02-06 CRAN (R 3.3.3)                   
##  rprojroot      1.2      2017-01-16 CRAN (R 3.3.2)                   
##  RSQLite        1.0.0    2014-10-25 CRAN (R 3.3.1)                   
##  rstudioapi     0.6      2016-06-27 CRAN (R 3.3.1)                   
##  rvest          0.3.2    2016-06-17 CRAN (R 3.3.2)                   
##  scales         0.4.1    2016-11-09 CRAN (R 3.3.2)                   
##  stringi        1.1.2    2016-10-01 CRAN (R 3.3.1)                   
##  stringr      * 1.2.0    2017-02-18 CRAN (R 3.3.3)                   
##  tcpl           1.2.2    2016-05-18 CRAN (R 3.3.3)                   
##  tibble       * 1.3.3    2017-05-28 CRAN (R 3.3.3)                   
##  tidyr        * 0.6.3    2017-05-15 CRAN (R 3.3.3)                   
##  tidyverse    * 1.1.1    2017-01-27 CRAN (R 3.3.3)                   
##  toxplot      * 0.1.0    <NA>       local                            
##  viridisLite    0.1.3    2016-03-12 CRAN (R 3.3.1)                   
##  withr          1.0.2    2016-06-20 CRAN (R 3.3.1)                   
##  xml2           1.0.0    2016-06-24 CRAN (R 3.3.2)                   
##  yaml           2.1.14   2016-11-12 CRAN (R 3.3.2)

2.1 Preprocess Single-Concentration Data

For single concentration data, Dan Hallinger has already gathered all the data and made the level0 compatible file already in .xlsx (see file ./raw data files/sc data from Dan.xlsx). Here just need to do some clean up to make the data ready for analysis.

dt_sc <- read_excel("./raw data files/sc data from Dan.xlsx", sheet = 2 )
# rename columns, following ToxCast Pipeline naming requirement.
names(dt_sc) <- c("pid", "rowi", "coli", "spid", "conc", "unit", "rep1", "rep2", "rep3")

dt_sc %<>% gather(rep, rval, rep1:rep3) %>%
  mutate(apid = paste(pid, rep, sep="_"),
         wllq = 1L,
         acsn = "raiu",
         assay = "RAIU",
         srcf = "NIS_ph1_v2_sc_lvl0_for_tcpl.csv"
         ) %>%
  dplyr::select(apid, everything())

# add well type
d1 <- dt_sc %>%
  filter(grepl("^TP*", dt_sc$spid)) %>%
  mutate(wllt = "t")

# clean up spid.
non_t <- dt_sc %>%
  filter(!grepl("^TP*", dt_sc$spid)) %>%
  separate(spid, c("delete", "spid"), sep = " ") %>%
  dplyr::select(-delete)

# continue assign well type
d2 <- non_t %>%
  filter(spid == "NaNO3") %>%
  mutate(wllt = "pr_ec80")
d3 <- non_t %>%
  filter(spid == "NaClO4") %>%
  mutate(wllt = "pr")
d4 <- non_t %>%
  filter(spid == "2,4-D") %>%
  mutate(wllt = "nrc")
d5 <- non_t %>%
  filter(spid == "DMSO") %>%
  mutate(wllt = "n")
d6 <- non_t %>%
  filter(spid == "NaSCN") %>%
  mutate(wllt = "pr_ec20")

dt_sc_update <- rbind(d1, d2, d3, d4, d5,d6)

# convert concentration as numeric
dt_sc_update$conc <- as.numeric(dt_sc_update$conc)

# convert row id from letters to numbers
dt_sc_update$rowi <- stringr::str_replace_all(dt_sc_update$rowi, c("A" = "1",
                                                                   "B" = "2",
                                                                   "C" = "3",
                                                                   "D" = "4",
                                                                   "E" = "5",
                                                                   "F" = "6",
                                                                   "G" = "7",
                                                                   "H" = "8"))

2.1.1 Update Testing Concentration

After we finished the testing, NCCT provided the actual aliquot_concentration in the chemical unblind file, which contains the actual CAS number, chemical name corresponding to each sample id. Some chemicals were not delivered as 20mM as we initially requested. Therefore it is necessary to update the level 0 file with the actual concentration.

spid_chnm_table <-
read_excel("./raw data files/EPA_11700_EPA-SLaws_ph1v2_150ul_20170125_key.xlsx")

spid_chnm_table <-
    spid_chnm_table %>% dplyr::select(EPA_Sample_ID, Aliquot_Concentration, CASRN, Preferred_Name)
#rename the column title to be compatible with tcpl package.
names(spid_chnm_table) <- c("spid", "aliquot_conc", "casn", "chnm")

#calculate the actual cocnentration tested in single-con screening
#the unit will be convert from mM to Molar
spid_chnm_table <- spid_chnm_table %>%
    mutate(test_conc = aliquot_conc / 2E5)

# update singel-con level0 data frame with updated concentration
# the concentration unit is converted from uM to M.
s1 <- filter(dt_sc_update, wllt == "t")
s2 <- filter(dt_sc_update, wllt != "t") %>%
    mutate(conc = conc / 1E6,
           unit = "M")
s1 <-
    left_join(s1, dplyr::select(spid_chnm_table, spid, test_conc), by = "spid") %>%
    mutate(conc = test_conc,
           unit = "M") %>%
    dplyr::select(-test_conc)

dt_sc_update <- bind_rows(s2, s1)

2.1.2 Export Sinlge-Con level0 file with updated concentration

# export to .csv file
write_csv(dt_sc_update, "./input data files/NIS_ph1_v2_sc_lvl0_for_tcpl.csv" )

2.2 Import and Preprocess Multi-Concentration Data

For multi-concentration data, Dan Hallinger made the .xlsx file as the summary file of all three bioreplicates of RAIU/cytotox assay. Therefore to read the data into R, this code needs to specify the row and column position of the data on the excel sheet.

To make the parsing succesful, make sure the excel data file (for example: “Combined RAIU Data (MC Plate 1).xlsx”, see fig below) contains three bioreplicates of the raw readings in 96well layout, with the first one range from Row 1-9, 2nd one from row 11-19, 3rd one from row 21-29. The 12 columns of the 96 well should start from Column C to N. On row 30, col D-M, list sample_id of the chemicals tested. on row 1,2,3 of col O, list MC or SC, Plate number, RAIU or Cytotox repsectively.

The screenshot below shows how the data should be organized and labelled:

summary file layout

summary file layout

2.2.1 Functions for data retrieving

To properly read the raw data into R and ToxCast pipline compatible level 0 file, three functions named: read_parse, create_platemap, and annotate_data are written and described below.

2.2.1.1 read_parse function to read and parse excel data file

This function takes the data file name as the argument, and returns a list object containing all assay info and readouts.

Notice the row index used here is different from what is in the xlsx file, this is because when read_excel read in the file, blank rows are automatically deleted. Therefore row 28, instead of row 30 contains the correct sample_ID.

read_parse <- function(file_name) {
    d <- read_excel(file_name, sheet =1, col_names = FALSE)
    d[] <- lapply(d, as.character)  #convert all columns to character
    rep1 <- data.matrix(d[2:9,3:14])
    rep2 <- data.matrix(d[11:18,3:14])
    rep3 <- data.matrix(d[20:27,3:14])
    assay_form <-d[[1,15]]   #MC or SC (multicon or single con)

    #plate 1,2,3 and go on, this is plate id Dan Hallinger assigned,
    #for the same plate id and assay_form, the plate layout is the same,
    #whether its raiu or cytotox.
    plate_id <- d[[2,15]]

    #name of the assay, RAIU or Cytotox
    #assay_plate_id <- paste(d[[1,15]], d[[2,15]], sep="_")
    #plate_id and assay_type should merge together, such as mc_plate1
    assay_name <- d[[3,15]]
    #this is the sample_id used to identify each tested chemical(blinded id)
    sample_id <- d[28,4:13, drop=TRUE]
    if (is.na(assay_form)|| is.na(plate_id)||is.na(assay_name)||is.na(sample_id)) {
        stop("missing info for assay type, plate id or sample id!\n Go back and check your .xlsx data file.")
        } else {
            results <- list(assay_name, assay_form, plate_id, sample_id, rep1, rep2, rep3)
            }
    return(results)
}

2.2.1.2 create_platemap function to create the assay plate map with correct sample_ID

To add info for each well of the assay, I created a plate map .csv file with 3 blocks of 96-well layout providing:

  • chemical names
  • concentration (unit: M)
  • well type(type of control or sample)

The file for multi concetration assay is named mc_platemap.csv. To read single concentration results, similar plate map needs to be created and named as sc_platemap.csv

The screen shot below is the mc_platemap used in NIS phase 1 data. mc_platemap

Note that there are NAs in the name map (row 1-8), these positions will be filled with the correct sample_id in the processing.

The code below is the function to parse the plate map (for mc or sc) csv file, and fill in the sample_id in to the right position in 96well layout.

create_platemap <- function(map_file, sample_id) {
    #check validity of sample_id
    if (!complete.cases(sample_id)) {stop("missing sample_id, check data file.")}
    #readin chemical name map
    name_map <- read.table(map_file, sep=",", nrows=8, na.strings="na",stringsAsFactors=FALSE)
    #readin concentration map
    conc_map <- read.table(map_file, sep=",", nrows=8, skip=8, na.strings="na")
    #readin welltype map
    welltype_map <- read.table(map_file, sep=",", nrows=8, skip=16, na.strings="na", stringsAsFactors=FALSE)
    #fill in the NAs in name_map with the correct sample_id that was read from the assay data file(.xlsx)
    #The loop here fills NAs on the mc_platemap
    for (i in 1:10) {
        name_map[2:7,i+1] <- rep(sample_id[[i]],6)
        i <- i+1
    }
    #put all three map together in a list.
    map <- list(name_map, conc_map, welltype_map)
    return(map)
}

2.2.1.3 annotate_data function to convert all data into tidy tabular form (level 0 compatible)

This function requires 1) the proper assay map generated by create_platemap function, 2) parsed data generated by read_parse function.

The function convert all the data into a dataframe with columns of:

  • assay: name of the assay. cytotox or raiu
  • apid: assay plate id. apid shoud be a unique id for each 96 well plate, can distinguish replicate, but doesn’t distinguish Cytotox and RAIU assay
  • pid: plate id. used to represent mother plate id, doesn’t distinguish replicate,nor Cytotox or RAIU assay.
  • spid: sample ID, the TP*** ID assigned by NCCT representing each tested chemical sample
  • rowi: row position on 96 well plate
  • coli: column position on 96 well plate
  • repi: replicate id, (1 to 3)
  • conc: molar concentration (M)
  • wllt: well type. define whether a well contains a control or a test sample
annotate_data <- function(map, results) {
    name_map <- map[[1]]
    conc_map <- map[[2]]
    welltype_map <- map[[3]]
    assay_name <- results[[1]]
    assay_form <- results[[2]]
    plate_id <- results[[3]]
    #sample_id <- results[[4]]
    rep1 <- results[[5]]
    rep2 <- results[[6]]
    rep3 <- results[[7]]

    #initialize empty dataframe
    d <- data.frame()
    #loop through 96 wells to gather all variables together
    for (i in 1:8) {
        for (j in 1:12) {
            t <- data.frame(assay=assay_name,
                            pid=plate_id,
                            spid=name_map[[i,j]],
                            rowi=i,
                            coli=j,
                            rep1=rep1[i,j],
                            rep2=rep2[i,j],
                            rep3=rep3[i,j],
                            conc=conc_map[[i,j]],
                            wllt=welltype_map[[i,j]]
                            )
            d <- rbind(d,t)
            j <- j+1
        }
        i <- i+1
    }
    #sort rows by sample_id, then concentration
    d <- arrange(d, spid, desc(conc))
    return(d)
}

2.2.2 Parse all excel data files and generate tidy table

file_directory <- "./raw data files/phase 1 mc data/"
flist <- list.files(file_directory)
print(paste("A total of",length(flist), "files in the directory to be read:"))
## [1] "A total of 36 files in the directory to be read:"
print(flist)
##  [1] "Combined Cytotox Data (MC Plate 1).xlsx" 
##  [2] "Combined Cytotox Data (MC Plate 10).xlsx"
##  [3] "Combined Cytotox Data (MC Plate 11).xlsx"
##  [4] "Combined Cytotox Data (MC Plate 12).xlsx"
##  [5] "Combined Cytotox Data (MC Plate 13).xlsx"
##  [6] "Combined Cytotox Data (MC Plate 14).xlsx"
##  [7] "Combined Cytotox Data (MC Plate 15).xlsx"
##  [8] "Combined Cytotox Data (MC Plate 16).xlsx"
##  [9] "Combined Cytotox Data (MC Plate 17).xlsx"
## [10] "Combined Cytotox Data (MC Plate 18).xlsx"
## [11] "Combined Cytotox Data (MC Plate 2).xlsx" 
## [12] "Combined Cytotox Data (MC Plate 3).xlsx" 
## [13] "Combined Cytotox Data (MC Plate 4).xlsx" 
## [14] "Combined Cytotox Data (MC Plate 5).xlsx" 
## [15] "Combined Cytotox Data (MC Plate 6).xlsx" 
## [16] "Combined Cytotox Data (MC Plate 7).xlsx" 
## [17] "Combined Cytotox Data (MC Plate 8).xlsx" 
## [18] "Combined Cytotox Data (MC Plate 9).xlsx" 
## [19] "Combined RAIU Data (MC Plate 1).xlsx"    
## [20] "Combined RAIU Data (MC Plate 10).xlsx"   
## [21] "Combined RAIU Data (MC Plate 11).xlsx"   
## [22] "Combined RAIU Data (MC Plate 12).xlsx"   
## [23] "Combined RAIU Data (MC Plate 13).xlsx"   
## [24] "Combined RAIU Data (MC Plate 14).xlsx"   
## [25] "Combined RAIU Data (MC Plate 15).xlsx"   
## [26] "Combined RAIU Data (MC Plate 16).xlsx"   
## [27] "Combined RAIU Data (MC Plate 17).xlsx"   
## [28] "Combined RAIU Data (MC Plate 18).xlsx"   
## [29] "Combined RAIU Data (MC Plate 2).xlsx"    
## [30] "Combined RAIU Data (MC Plate 3).xlsx"    
## [31] "Combined RAIU Data (MC Plate 4).xlsx"    
## [32] "Combined RAIU Data (MC Plate 5).xlsx"    
## [33] "Combined RAIU Data (MC Plate 6).xlsx"    
## [34] "Combined RAIU Data (MC Plate 7).xlsx"    
## [35] "Combined RAIU Data (MC Plate 8).xlsx"    
## [36] "Combined RAIU Data (MC Plate 9).xlsx"
#combine directory and file name
fl <- paste(file_directory, flist, sep="")
#name of plate map file
map_file  <- "./raw data files/mc_platemap.csv"

# read all files
df_mc <- data.frame()
for (i in 1:length(fl)) {
    results <- read_parse(fl[[i]])
    print(paste("Reading file", i, ":", flist[[i]]))
    #print(sapply(results[1:4], paste, sep=" "))
    sample_id <- results[[4]]
    map <- create_platemap(map_file, sample_id)
    temp <- annotate_data(map, results)
    df_mc <- rbind(df_mc, temp)

    i <- i+1
}
## [1] "Reading file 1 : Combined Cytotox Data (MC Plate 1).xlsx"
## [1] "Reading file 2 : Combined Cytotox Data (MC Plate 10).xlsx"
## [1] "Reading file 3 : Combined Cytotox Data (MC Plate 11).xlsx"
## [1] "Reading file 4 : Combined Cytotox Data (MC Plate 12).xlsx"
## [1] "Reading file 5 : Combined Cytotox Data (MC Plate 13).xlsx"
## [1] "Reading file 6 : Combined Cytotox Data (MC Plate 14).xlsx"
## [1] "Reading file 7 : Combined Cytotox Data (MC Plate 15).xlsx"
## [1] "Reading file 8 : Combined Cytotox Data (MC Plate 16).xlsx"
## [1] "Reading file 9 : Combined Cytotox Data (MC Plate 17).xlsx"
## [1] "Reading file 10 : Combined Cytotox Data (MC Plate 18).xlsx"
## [1] "Reading file 11 : Combined Cytotox Data (MC Plate 2).xlsx"
## [1] "Reading file 12 : Combined Cytotox Data (MC Plate 3).xlsx"
## [1] "Reading file 13 : Combined Cytotox Data (MC Plate 4).xlsx"
## [1] "Reading file 14 : Combined Cytotox Data (MC Plate 5).xlsx"
## [1] "Reading file 15 : Combined Cytotox Data (MC Plate 6).xlsx"
## [1] "Reading file 16 : Combined Cytotox Data (MC Plate 7).xlsx"
## [1] "Reading file 17 : Combined Cytotox Data (MC Plate 8).xlsx"
## [1] "Reading file 18 : Combined Cytotox Data (MC Plate 9).xlsx"
## [1] "Reading file 19 : Combined RAIU Data (MC Plate 1).xlsx"
## [1] "Reading file 20 : Combined RAIU Data (MC Plate 10).xlsx"
## [1] "Reading file 21 : Combined RAIU Data (MC Plate 11).xlsx"
## [1] "Reading file 22 : Combined RAIU Data (MC Plate 12).xlsx"
## [1] "Reading file 23 : Combined RAIU Data (MC Plate 13).xlsx"
## [1] "Reading file 24 : Combined RAIU Data (MC Plate 14).xlsx"
## [1] "Reading file 25 : Combined RAIU Data (MC Plate 15).xlsx"
## [1] "Reading file 26 : Combined RAIU Data (MC Plate 16).xlsx"
## [1] "Reading file 27 : Combined RAIU Data (MC Plate 17).xlsx"
## [1] "Reading file 28 : Combined RAIU Data (MC Plate 18).xlsx"
## [1] "Reading file 29 : Combined RAIU Data (MC Plate 2).xlsx"
## [1] "Reading file 30 : Combined RAIU Data (MC Plate 3).xlsx"
## [1] "Reading file 31 : Combined RAIU Data (MC Plate 4).xlsx"
## [1] "Reading file 32 : Combined RAIU Data (MC Plate 5).xlsx"
## [1] "Reading file 33 : Combined RAIU Data (MC Plate 6).xlsx"
## [1] "Reading file 34 : Combined RAIU Data (MC Plate 7).xlsx"
## [1] "Reading file 35 : Combined RAIU Data (MC Plate 8).xlsx"
## [1] "Reading file 36 : Combined RAIU Data (MC Plate 9).xlsx"
# add rep, rval, acsn, wllq, unit, apid columns
df_mc <- df_mc %>%
  mutate(acsn = stringr::str_to_lower(assay),
         wllq = 1L,
         unit = 'M') %>%
  gather(rep, rval, rep1:rep3) %>%
  mutate(apid = paste(pid, rep, sep = "_"))

2.2.3 Update Testing Concentrations

After we finished the testing, NCCT provided the actual aliquot_concentration in the chemical unblind file, which contains the actual CAS number, chemical name corresponding to each sample id. Some chemicals were not delivered as 20mM as we initially requested due to solubility limit. Therefore it is necessary to update the level 0 file with the actual concentration.

mc_conc_ratio <- spid_chnm_table %>%
  mutate(conc_ratio = aliquot_conc/2E5/1E-4) %>%
  dplyr::select(spid, conc_ratio)

# update cyto data
mc_t_cyto <- df_mc %>%
    filter(assay == "Cytotox") %>%
    filter(wllt == "t")
mc_non_t_cyto <- df_mc %>%
    filter(assay == "Cytotox") %>%
    filter(wllt != "t")

mc_t_cyto_update <- data.frame()
for (id in unique(mc_t_cyto$spid)) {
    d_spid <- filter(mc_t_cyto, spid == id)
    ratio <- filter(mc_conc_ratio, spid == id)[[2]]
    d_spid <- d_spid %>% mutate(conc = conc * ratio)
    mc_t_cyto_update <- bind_rows(mc_t_cyto_update, d_spid)
}
# Here is the udpated mc_lvl0 data table.
mc_lvl0_cyto_update <- bind_rows(mc_non_t_cyto, mc_t_cyto_update)

#update raiu data
mc_t_raiu <- df_mc %>%
    filter(assay == "RAIU") %>%
    filter(wllt == "t")
mc_non_t_raiu <- df_mc %>%
    filter(assay == "RAIU") %>%
    filter(wllt != "t")

mc_t_raiu_update <- data.frame()
for (id in unique(mc_t_raiu$spid)) {
    #print(id)
    d_spid <- filter(mc_t_raiu, spid == id)
    ratio <- filter(mc_conc_ratio, spid == id)[[2]]
    d_spid <- d_spid %>% mutate(conc = conc * ratio)
    mc_t_raiu_update <- bind_rows(mc_t_raiu_update, d_spid)
}
#Here is the udpated mc_lvl0 data table.
mc_lvl0_raiu_update <- bind_rows(mc_non_t_raiu, mc_t_raiu_update)

2.2.4 Export Multi-conc data to file

mc_lvl0_raiu_update %>%
  mutate(srcf = "NIS_ph1_v2_mc_lvl0_for_tcpl_raiu.csv") %>%
  write_csv("./input data files/NIS_ph1_v2_mc_lvl0_for_tcpl_raiu.csv")

mc_lvl0_cyto_update %>%
  mutate(srcf = "NIS_ph1_v2_mc_lvl0_for_tcpl_cytotox.csv") %>%
  write_csv("./input data files/NIS_ph1_v2_mc_lvl0_for_tcpl_cytotox.csv")