2 Tidy Cytokine Data Metadata

2.1 Libraries

library(tidyverse)

2.2 Raw file import & cleaning

cyto1 <- 
  read.csv("Input Files/Grey seal cytokine 200_300_800_900.csv") %>% 
  select(-c(1:2)) %>% 
  slice(-c(1:6, 8:18, 37:56)) %>% 
  setNames(c("Sample", .[1,2:14])) %>% 
  slice(-c(1)) %>% 
  mutate(analysis.year="2016",
         batch="1")

cyto_names <-
  gsub("\\s*\\([^\\)]+\\)","", colnames(cyto1)[2:14])

cyto1 <- 
  cyto1 %>% 
  setNames(c("Sample", cyto_names, "analysis.year", "batch"))

cyto2 <- 
  read.csv("Input Files/Results #1349 plate 1 grey seal millipore canine kit.csv") %>% 
  select(-c(2:3, 5:6, 8:10)) %>%
  filter(!Description=="" &
         !Description=="QC1" &
         !Description=="QC2") %>% 
  pivot_wider(id_cols="Description", names_from="Analyte", values_from="Obs.Conc") %>% 
  filter(!Description=="853" &
         !Description=="869" &
         !Description=="896" &
         !Description=="900") %>% 
  mutate(analysis.year="2022",
         batch="2") %>% 
  setNames(c("Sample", cyto_names, "analysis.year", "batch"))

cyto3 <- 
  read.csv("Input Files/Results #1350 plate 2 grey seal millipore canine kit.csv") %>% 
  select(-c(2:3, 5:6, 8:10)) %>%
  filter(!Description=="" &
         !Description=="QC1" &
         !Description=="QC2") %>% 
  pivot_wider(id_cols="Description", names_from="Analyte", values_from="Obs.Conc") %>% 
  mutate(analysis.year="2022",
         batch="3") %>% 
  setNames(c("Sample", cyto_names, "analysis.year", "batch"))

# human error while typing in sample names to computer 1505 -> 1507
cyto3$Sample[22] <- "1507" 

cyto4 <- 
  read.csv("Input Files/Results #1359 4-13-23 2023 Monomoy_Muskeget_GP cytokines.csv") %>% 
  select(-c(1:2,17)) %>% 
  rename("Sample"="X.2") %>% 
  filter(!Sample=="" &
         !Sample=="QC1" &
         !Sample=="QC2" &
         !Sample=="Description") %>% 
  mutate(Sample=gsub("Hg ", "", Sample),
         analysis.year="2023",
         batch="4") %>% 
  setNames(c("Sample", cyto_names, "analysis.year", "batch"))

2.3 Merge cytokine data & tidy

cyto <- 
  bind_rows(cyto1, cyto2, cyto3, cyto4) 


# Change OOR < to 0 & remove *
for (i in 1:nrow(cyto)) {
  for (j in 1:ncol(cyto)) {
    str <- cyto[i,j]
    new_str <- (gsub("\\*","",str))
    cyto[i,j] <- new_str
    {if (cyto[i,j] == "OOR <") {
      cyto[i,j] <- 0
    }
      else{next}
    }
  }
}

# Write out data for all cytokines
write.csv(cyto, "Output Files/cleaned_cytokine_all.csv", row.names=FALSE)

# Remove cytokines with <10% (11) detections
cyto_10 <- 
  cyto %>% 
  select_if(function(x) sum(x>0) > 11) %>% 
  mutate(Sample = cyto$Sample)
  
# Write out reduced cytokine data
write.csv(cyto_10, file="Output Files/cleaned_cytokine.csv", row.names=FALSE)

2.4 Create Cytokine (0/1) Dataset

databin <- cyto
for (i in 1:nrow(databin)) {
  for (j in 2:14) {
  databin[i,j] <- ifelse(databin[i,j] > 0, 1, 0)
  }
}

# Write out binary cytokine data for all
write.csv(databin, "Output Files/cleaned_cytokine_bin_all.csv", row.names=FALSE)


# Remove cytokines with <10% (11) detections
databin_10 <- 
  databin %>% 
  select_if(function(x) sum(x>0) > 11) 


# Write out binary cytokine dataset
write.csv(databin_10, file="Output Files/cleaned_cytokine_bin.csv", row.names=FALSE)

2.5 Tidy Metadata

metadata <- 
  read.csv("Input Files/metadata.csv") %>% 
  mutate(bodcond = (ax.girth*100)/st.length,
         bc_bin = ifelse(bodcond < quantile(bodcond, probs=0.25, na.rm=TRUE), "Poor",
                         ifelse(bodcond > quantile(bodcond, probs=0.25, na.rm=TRUE) & 
                                  bodcond < quantile(bodcond, probs=0.75, na.rm=TRUE), "Average",
                          ifelse(bodcond > quantile(bodcond, probs=0.75, na.rm=TRUE), "Robust", NA))),
         stage = ifelse(iav=="neg" & iavser=="neg","control",
                        ifelse(iav=="pos" & iavser=="neg", "acute",
                               ifelse(iav=="pos" & iavser=="pos", "peak", "late")))) %>% 
  filter(Sample %in% cyto$Sample)

write.csv(x=metadata, file="Output Files/metadata_cyto.csv", row.names=FALSE)