---
title: "Extdata Explanations"
author: "Aubrey Odom"
date: "2024-01-06"
output: html_document
---

# Toy Data

`inst/extdata/MAE.RDS`

This file consists of a MultiAssayExperiment object with raw data published in the following paper:

Odom AR, Gill CJ, Pieciak R et al. Characterization of longitudinal nasopharyngeal microbiome patterns in maternally HIV-exposed Zambian infants [version 2; peer review: 2 approved with reservations]. Gates Open Res 2024, 6:143 (https://doi.org/10.12688/gatesopenres.14041.2)

The code to create the package data object is reproduced below.

```{R, eval = FALSE}
dat2 <- readRDS("/restricted/projectnb/infant-microbiome/work/aodom/HIV_Exposed_Infants/data/animalculesFinalHIV.rds")

# Add in pairing data
matched <- matrix(c("0554", "0469",
                    "0620", "0507",
                    "0645", "0539",
                    "0749", "0718",
                    "0798", "0801",
                    "0850", "0839",
                    "0895", "0878",
                    "0915", "0890",
                    "0987", "1040",
                    "1095", "1049"),
                  dimnames = list(c(), c("HIV", "Control")),
                  byrow = TRUE, ncol = 2) %>%
  tibble::as_tibble() %>%
  dplyr::mutate(pairing = seq_len(nrow(.))) %>%
  tidyr::pivot_longer(cols = c(HIV, Control), 
                      names_to = c("HIVStatus"),
                      values_to = "Subject") %>%
  dplyr::mutate(Subject = paste(Subject, "-1", sep = "")) 

# Merge with colData and extract SE objects
se_mgx <- SummarizedExperiment::assay(dat2, "MicrobeGenetics") %>%
  base::data.matrix() %>%
  S4Vectors::SimpleList() %>% magrittr::set_names("MGX")
se_colData <- SummarizedExperiment::colData(dat2[["MicrobeGenetics"]]) %>%
  as.data.frame() %>%
  tibble::rownames_to_column("rn") %>%
  dplyr::left_join(matched, by = c("Subject", "HIVStatus")) %>%
  as.data.frame() %>%
  tibble::column_to_rownames("rn") %>%
  S4Vectors::DataFrame()
se_rowData <- SummarizedExperiment::rowData(dat2[["MicrobeGenetics"]])

microbe_se <- SummarizedExperiment::SummarizedExperiment(
  assays = se_mgx, colData = se_colData, rowData = se_rowData)
MAE <- MultiAssayExperiment::MultiAssayExperiment(
  experiments = S4Vectors::SimpleList(MicrobeGenetics = microbe_se),
  colData = se_colData)

stem2 <- "/restricted/projectnb/infant-microbiome/work/aodom/LMTK/inst/extdata"
saveRDS(MAE, file.path(stem2, "MAE.RDS"))
```

This file is a small-form animalcules-formatted MAE object intended for usage with the examples.
```{R, eval = FALSE}
stem2 <- "/restricted/projectnb/infant-microbiome/work/aodom/LegATo/inst/extdata"

# Creating the sample data
which_sex <- sample(c("Male", "Female"), 10, replace = TRUE)
sample_table <- tibble(Sample = paste0("X-", seq_len(50)),
                       Subject = rep(paste0("S", seq_len(10)), 5),
                       Sex = rep(which_sex, 5),
                       Month = rep(seq_len(5), each = 10),
                       Group = rep(c("A", "B"), 25),
                       Pairing = rep(rep(seq_len(5), each = 2), 5)) %>%
  mutate("HairLength" = rnorm(50, mean = 50, sd = 14.5),
         "Age" = round(runif(50, 21, 80), 1)) %>%
  as.data.frame()
rownames(sample_table) <- sample_table$Sample

# Tax table
data_in_2 <- system.file("extdata/MAE.RDS", package = "LegATo") %>%
  readRDS() %>% filter_MAE(0.02)
tax_dat_pre <- parse_MAE_SE(data_in_2)$tax
ind <- sample(seq_len(nrow(tax_dat_pre)), 100)
tax_dat <- tax_dat_pre[ind, ] %>%
  arrange(species)

# Counts table
data_in <- system.file("extdata/MAE.rds", package = "animalcules") %>%
  readRDS()
counts_dat <- parse_MAE_SE(data_in)$counts
colnames(counts_dat) <- sample_table$Sample
rownames(counts_dat) <- rownames(tax_dat)

se_mgx <- counts_dat %>% base::data.matrix() %>% S4Vectors::SimpleList() %>%
  magrittr::set_names("MGX")
se_rowData <- tax_dat %>% base::data.frame() %>%
    dplyr::mutate_all(as.character) %>% S4Vectors::DataFrame()
se_colData  <- sample_table %>% S4Vectors::DataFrame()

write.csv(counts_dat, file.path(stem2, "counts.csv"))
write.csv(tax_dat, file.path(stem2, "tax.csv"))
write.csv(se_colData, file.path(stem2, "sample.csv"))

microbe_se <- SummarizedExperiment::SummarizedExperiment(
  assays = se_mgx, colData = se_colData, rowData = se_rowData)
MAE_out <- MultiAssayExperiment::MultiAssayExperiment(
  experiments = S4Vectors::SimpleList(MicrobeGenetics = microbe_se),
  colData = se_colData)

saveRDS(MAE_out, file.path(stem2, "MAE_small.RDS"))
```
