---
title: "Quarantine Main Paper Tables 1-3, Figure 3, and Other Supporting Tables and Figures"
author: "Jacob Bueno de Mesquita"
date: "04/6/2019"
output:
  html_document: default
  word_document: default
  pdf_document: default
---

# Printing Tables and Figures for Manuscript Main Text

The goal of this Markdown is to print the tables and figures directly from the R code and files used for analysis. The purpose of printing directly into the Markdown, which can be drawn into a manuscript easily, is to prevent errors in translating these results into a format acceptable for publication The analysis was produced by the EMIT_Quarantine_Main_Analysis.R script and a couple of additional supporting tables were produced by the EMIT_Quarantine_Text_Analysis.R script. 

First I will setup the programming environment and note the session information.
Because the output for this step is long and not very useful for immediate use, we will keep the output hidden, however it can be called up if needed. 

We will also read in the data required to make the tables and figure included in the main text.

```{r, include=FALSE}
library(tidyverse)
library(RcppRoll)
library(readxl)
library(knitr)
library(data.table)
library(lubridate)
library(devtools)
library(xtable)
library(DT)
library(kableExtra)
library(arsenal)
library(survival)

Table1 <- read_csv("/Users/jbueno/Box Sync/EMIT/EMIT_Data_Analysis_Jake/EMIT_Quarantine/Analysis Results/Table1_Manuscript.csv") %>%
  select(-X1)

Table2 <- read_csv("/Users/jbueno/Box Sync/EMIT/EMIT_Data_Analysis_Jake/EMIT_Quarantine/Analysis Results/Table2_Manuscript.csv",
                   col_types = cols (
                     X1 = col_integer(),
                     `Quarantine #` = col_character(),
                     `N Subjects` = col_integer(),
                     `N Samples` = col_integer(),
                     `Coarse Positive Subjects (%)` = col_character(),
                     `Coarse Positive Samples (%)` = col_character(),
                     `Mean of Positive Coarse Samples*` = col_character(),
                     `Fine Subjects (%)` = col_character(),
                     `Fine Samples (%)` = col_character(),
                     `Mean of Positive Fine Samples*` = col_character()
                   )
) %>%
  select(-X1)

Table3 <- read_csv("/Users/jbueno/Box Sync/EMIT/EMIT_Data_Analysis_Jake/EMIT_Quarantine/Analysis Results/Table3_Manuscript.csv") %>%
  select(-X1)

Bar <- read_csv("/Users/jbueno/Box Sync/EMIT/EMIT_Data_Analysis_Jake/EMIT_Quarantine/Analysis Results/BarPosBySampDay.csv")

Line <- read_csv("/Users/jbueno/Box Sync/EMIT/EMIT_Data_Analysis_Jake/EMIT_Quarantine/Analysis Results/NPctLineData.csv")
```

```{r, echo = FALSE}
session_info()
```

## Table 1: Symptoms and Infection Status of Donors

Printing a formatted Table 1 with Kable

```{r, echo = FALSE}
kable(Table1, align = "c") %>%
  kable_styling(c("striped", "bordered", "hover", "condensed", "responsive"), 
                full_width = FALSE, position = "center", font_size = 10) %>%
  add_header_above(c(" " = 2, "Clinical Illness (% of Infected)" = 3, 
                     "Laboratory Confirmed Infection Criteria (% of Infected)" = 3)) %>%
  column_spec(1, bold = T) %>%
  row_spec(4, bold = TRUE)
```


## Table 2: Viral Shedding of Infected Donors

Printing a formatted Table 2 with Kable

```{r, echo = FALSE}
kable(Table2, align = "c") %>%
  kable_styling(c("striped", "bordered", "hover", "condensed", "responsive"), 
                full_width = FALSE, position = "center", font_size = 10) %>%
  add_header_above(c(" " = 3, "Coarse Aerosol (>5um)" = 3, 
                     "Fine Aerosol (≤5um)" = 3)) %>%
  column_spec(1, bold = T) %>%
  row_spec(4, bold = TRUE) %>%
  footnote(symbol = c("The arithmetic mean RNA copy number used data from positive samples only. The geometric means (GM) and geometric standard deviations (GSD) were 2.90E+3 (3.22) and 5.24E+3 (6.62) for positive coarse and positive fine aerosol samples, respectively. Quantitative RT-PCR assays for aerosol samples were run in duplicate and in instances where one replicate was positive and the other was negative (i.e., non-detectable), we imputed for the non-detectable replicate using LOQ*(1/sqrt(2)) where LOQ is the limit of quantification computed as the most dilute sample that gave a positive result in all replicates. The LOQ for this assay was 2,000 copies per sample. Such imputed values for non-detectable replicates were used in computing arithmetic mean, GM, and GSD. "))
```


## Table 3: Symptoms and Infection Status of Recipients

Printing a formatted Table 3 with Kable

```{r, echo = FALSE}
kable(Table3, align = "c") %>%
  kable_styling(c("striped", "bordered", "hover", "condensed", "responsive"), 
                full_width = FALSE, position = "center", font_size = 10) %>%
  add_header_above(c(" " = 3, "Clinical Illness (% of Exposed)" = 3, 
                     "Laboratory Confirmed Infection Criteria (% of Exposed)" = 3)) %>%
  kable_styling(full_width = F) %>%
  column_spec(1, bold = T) %>%
  collapse_rows(columns = 1, valign = "top") %>%
  row_spec(7:8, bold = TRUE)
```


## Two Plots for Figure 3

Bar chart of viral shedding (by qPCR) from NP swabs, coarse aerosol, fine aerosol, both coarse and fine aerosol, either coarse or fine aerosol

```{r fig.align="center", echo = FALSE}
p <- ggplot(Bar, aes(fill = `Sample Type`, y = `Fraction Positive`, x = `Study Day`)) +
  geom_bar(position = "dodge", stat = "identity") + theme(legend.position = c(0.2, 0.8)) +
  theme_bw() +
  xlab("Study Day") +
  ylab("Fraction Positive")
p
```

Line chart of the arithmetic mean ct values and 1 arithmetic standard deviation above and below bars for NP swabs that were postiive on study days 1-4.

```{r fig.align="center", echo = FALSE}
p2 <- ggplot(Line, aes(x = `Study Day`, y = `Nasopharyngeal Swab CT Value Mean`, group=1)) +
  geom_line() +
  geom_errorbar(width=.1, aes(ymin=`Nasopharyngeal Swab CT Value Mean` - `Nasopharyngeal Swab CT Value SD`, 
                              ymax=`Nasopharyngeal Swab CT Value Mean` + `Nasopharyngeal Swab CT Value SD`)) +
  geom_point(shape=21, size=3, fill="white") +
  ylim(20,38) +
  theme_bw() +
  xlab("Study Day") +
  ylab("Nasopharyngeal Swab CT Value")
p2
```


# Printing Other Supporting Tables & Figures

There are some additional tables that hold information required for fully describing the results in the paper. These will be produced here. Tables and figures from among this set can be selected for inclusion in manuscript SI. 

## Supporting Analysis: Donor Status

### Donors with Greater than Anticipated Immunity by Quarantine

This table provides output that is used in the footnote for Table 1.

```{r, echo = FALSE, include = FALSE}
Table1_Footnote_Summary <- read_csv("/Users/jbueno/Box Sync/EMIT/EMIT_Data_Analysis_Jake/EMIT_Quarantine/Analysis Results/Table1_Footnote_Reportable_Summary.csv")%>%
  select(-X1)
```

```{r, echo = FALSE}
kable(Table1_Footnote_Summary, align = "c") %>%
  kable_styling(c("striped", "bordered", "hover", "condensed", "responsive"), 
                full_width = FALSE, position = "center", font_size = 10) %>%
  row_spec(4, bold = TRUE) %>%
  column_spec(1, bold = T) %>%
  footnote(number = c("Greater than anticipated immunity by HAI consisted of HAI titer >10 at study baseline (i.e., admission to quarantine), or MN ≥80: indicated by 1 if true and 0 otherwise; ", "4-fold or greater rise in HAI or MN titres between Day -2 (baseline) and Day 28 (followup): indicated by 1 if true and 0 otherwise; " , "≥2 positive NPS test results by qPCR (if a single sample, two positive aliquots required): indicated by 1 if true and 0 otherwise" ))
```

Summary by subject ID of donors with greater than anticipated immunity.

```{r, echo = FALSE, include = FALSE}
Table1_Footnote_SubjectID <- read_csv("/Users/jbueno/Box Sync/EMIT/EMIT_Data_Analysis_Jake/EMIT_Quarantine/Analysis Results/Table1_Footnote_Reportable_Summary_SubjectIDs.csv")%>%
  select(-X1)
```

```{r, echo = FALSE}
kable(Table1_Footnote_SubjectID, align = "c") %>%
  kable_styling(c("striped", "bordered", "hover", "condensed", "responsive"), 
                full_width = FALSE, position = "center", font_size = 10) %>%
  column_spec(1, bold = T) %>%
  collapse_rows(columns = 1, valign = "top") %>%
  footnote(general = "This table provides the information for each of the 10 subject IDs that were aggregated by quarantine in the Summary by Quarantine of Donors with Greater than Anticipated Immunity table.", 
           number = c("Greater than anticipated immunity by HAI consisted of HAI titer >10 at study baseline (i.e., admission to quarantine), or MN ≥80): indicated by 1 if true and 0 otherwise; ", "4-fold or greater rise in HAI or MN titres between Day -2 (baseline) and Day 28 (followup): indicated by 1 if true and 0 otherwise; " , "≥2 positive NPS test results by qPCR (if a single sample, two positive aliquots required): indicated by 1 if true and 0 otherwise" ))
```


### Asymptomatic Infected Donors

The number of asymptomatic, infected donors is mentioned in the Results. This table supports those written findings. 

```{r, echo = FALSE, include = FALSE}
Table1_asymptomatic <-  read_csv("/Users/jbueno/Box Sync/EMIT/EMIT_Data_Analysis_Jake/EMIT_Quarantine/Analysis Results/Asymptomatic_Q.csv",
                                 col_types = cols(
                                    `Quarantine #` = col_character(),
                                    Asymptomatic = col_integer()
                                    )
                                 )
```

```{r, echo = FALSE}
kable(Table1_asymptomatic, align = "c") %>%
  kable_styling(c("striped", "bordered", "hover", "condensed", "responsive"), 
                full_width = FALSE, position = "center", font_size = 10) %>%
  row_spec(4, bold = TRUE) %>%
  column_spec(1, bold = T) %>%
  footnote(general = c("Asymptomatic was defined as volunteers who did not reach symptomatic status, with symptomatic defined as evidence of any respiratory symptom lasting ≥24 hours during study days 1-6. In Q3, one donor had a single instance of low-grade fever at 37.9 degrees C in the morning of study day 3. This donor exhibited no other symptoms and was included as one of the asymptomatic donors"))
```


### Infected Donor PCR Positivity by Study Day

Looking at this in tabular form.

```{r, echo = FALSE, include = FALSE}
Table1_PCR_Positive_Subjects_by_StudyDay_Table <-  read_csv("/Users/jbueno/Box Sync/EMIT/EMIT_Data_Analysis_Jake/EMIT_Quarantine/Analysis Results/PCR_Positive_Study_Day_Table.csv")
```

```{r, echo = FALSE}
kable(Table1_PCR_Positive_Subjects_by_StudyDay_Table, align = "c") %>%
  kable_styling(c("striped", "bordered", "hover", "condensed", "responsive"), 
                full_width = FALSE, position = "center", font_size = 10) %>%
  row_spec(7, bold = TRUE) %>%
  column_spec(1, bold = T) %>%
  footnote(general = c("Only the 36 donors who met laboratory case criteria by PCR (i.e., two or more positive NPS test results by qRT-PCR) were included. Percents were computed out of these 36 for each study day (i.e., one nasopharyngeal swab per infected donor per day, for each study day 1-6."))
```

Plot the pooled data from all three quarantines. 

```{r, echo = FALSE, include = FALSE}
Table1_PCR_Positive_Subjects_by_StudyDay_Line <-  read_csv("/Users/jbueno/Box Sync/EMIT/EMIT_Data_Analysis_Jake/EMIT_Quarantine/Analysis Results/PCR_Positive_Study_Day_line.csv")
```

```{r fig.align="center", echo = FALSE}
p3 <- ggplot(Table1_PCR_Positive_Subjects_by_StudyDay_Line, aes(x = `Study Day`, y = `Percent Positive`, group=1)) +
  geom_line() +
  geom_point(shape=21, size=3, fill="white") +
  theme_bw() +
  xlab("Study Day") +
  ylab("Percent Positive")
p3
```


### Infected Donor PCR Positivity by Study Day, by Quarantine

Looking at PCR positive donor data over the 6 study days, as before, but this time stratified by the 3 Q to see if the Q were similar.

```{r, echo = FALSE, include = FALSE}
Table1_PCR_Positive_Subjects_by_StudyDay_by_Q_Table <-  read_csv("/Users/jbueno/Box Sync/EMIT/EMIT_Data_Analysis_Jake/EMIT_Quarantine/Analysis Results/PCR_Pos_StudyDay_Q123_table.csv")
```

```{r, echo = FALSE}
kable(Table1_PCR_Positive_Subjects_by_StudyDay_by_Q_Table, align = "c") %>%
  kable_styling(c("striped", "bordered", "hover", "condensed", "responsive"), 
                full_width = FALSE, position = "center", font_size = 10) %>%
  row_spec(7, bold = TRUE) %>%
  column_spec(1, bold = T) %>%
  footnote(general = c("Only the donors who met laboratory case criteria by PCR (i.e., two or more positive NPS test results by qRT-PCR) were included. Percents were computed out of the number of total samples collected from positive donors for each study day (i.e., one nasopharyngeal swab per infected donor per day, for each study day 1-6. These totals are 12, 10, 14, and 36 for Q1, Q2, Q3, and all Q together, respectively."))
```

Plot the data by each quarantine, and pooled data on the same plot. 

```{r, echo = FALSE, include = FALSE}
Table1_PCR_Positive_Subjects_by_StudyDay_by_Q_Line <-  read_csv("/Users/jbueno/Box Sync/EMIT/EMIT_Data_Analysis_Jake/EMIT_Quarantine/Analysis Results/PCR_Pos_StudyDay_Q123_line.csv")
```

```{r fig.align="center", echo = FALSE}
p4 <- ggplot(Table1_PCR_Positive_Subjects_by_StudyDay_by_Q_Line, 
            aes(x = `Study Day`, y = `Percent Positive`, group = Quarantine, colour = Quarantine)) +
  geom_line() +
  geom_point() +
  scale_color_manual(values = c("black", "darkorange3", "cyan2", "darkorchid1")) +
  theme_bw() +
  xlab("Study Day") +
  ylab("Percent Positive")
p4
```


### Nasopharyngeal Swab CT Values Over Study Days 1-6

What was the CT value trend for positive nasopharyngeal swabs and did each Q follow a similar trend over the 6 study days?

```{r, echo = FALSE, include = FALSE}
Qdata_pcr_pos2_or_more_days_days1to6_byQ_NPS_CT_Qall_long <-  read_csv("/Users/jbueno/Box Sync/EMIT/EMIT_Data_Analysis_Jake/EMIT_Quarantine/Analysis Results/PCR_CT_StudyDay_Q123_line.csv")
```

```{r fig.align="center", echo = FALSE}
pd <- position_dodge(0.25)
p5 <- ggplot(Qdata_pcr_pos2_or_more_days_days1to6_byQ_NPS_CT_Qall_long, 
            aes(x = `Study Day`, y = `CT`, group = Quarantine, colour = Quarantine)) +
  geom_line() +
  geom_errorbar(width=.1, aes(ymin=`CT` - `SD`, ymax=`CT` + `SD`), position = pd) +
  geom_point(position = pd) +
  scale_color_manual(values = c("black", "darkorange3", "cyan2", "darkorchid1")) +
  theme_bw() +
  xlab("Study Day") +
  ylab("NPS CT Value")
p5
```


### Donor Positivity by Serology and PCR

Looking at the risk of become positive, among donors inoculated, and among donors infected, by: HAI or MN, and PCR; HAI and MN, and PCR; HAI or MN, but not PCR; HAI and MN, but not PCR.

```{r, echo = FALSE, include = FALSE}
Table1_Sero_and_or_PCR_Positive_byQ_Inf_Inoc <- read_csv("/Users/jbueno/Box Sync/EMIT/EMIT_Data_Analysis_Jake/EMIT_Quarantine/Analysis Results/SI_Inf_PCR_and_Sero_by_Q_table1_Inf_Inoc.csv")
```

```{r, echo = FALSE}
kable(Table1_Sero_and_or_PCR_Positive_byQ_Inf_Inoc, align = "c") %>%
  kable_styling(c("striped", "bordered", "hover", "condensed", "responsive"), 
                full_width = FALSE, position = "center", font_size = 10) %>%
  add_header_above(c(" " = 2, 
                     "Positive by PCR" = 2, 
                     "Not Positive by PCR" = 2)) %>%
  row_spec(4, bold = TRUE) %>%
  column_spec(1, bold = T) %>%
  footnote(general = "Seroconversion: 4-fold or greater rise in HAI or MN titres between Day -2 (baseline) and Day 28 (followup): indicated by 1 if true and 0 otherwise; PCR Positivity: ≥2 positive NPS test results by qPCR (if a single sample, two positive aliquots required): indicated by 1 if true and 0 otherwise")
```

If we want to do the same analysis but only include the risk of seroconversion among the infected donors (not intention to treat analysis), then we can produce a new table. 

```{r, echo = FALSE, include = FALSE}
Table1_Sero_and_or_PCR_Positive_byQ_Inf <- read_csv("/Users/jbueno/Box Sync/EMIT/EMIT_Data_Analysis_Jake/EMIT_Quarantine/Analysis Results/SI_Inf_PCR_and_Sero_by_Q_table1_Inf.csv")
```

```{r, echo = FALSE}
kable(Table1_Sero_and_or_PCR_Positive_byQ_Inf, align = "c") %>%
  kable_styling(c("striped", "bordered", "hover", "condensed", "responsive"), 
                full_width = FALSE, position = "center", font_size = 10) %>%
  add_header_above(c(" " = 2, 
                     "Positive by PCR" = 2, 
                     "Not Positive by PCR" = 2)) %>%
  row_spec(4, bold = TRUE) %>%
  column_spec(1, bold = T) %>%
  footnote(general = "Seroconversion: 4-fold or greater rise in HAI or MN titres between Day -2 (baseline) and Day 28 (followup): indicated by 1 if true and 0 otherwise; PCR Positivity: ≥2 positive NPS test results by qPCR (if a single sample, two positive aliquots required): indicated by 1 if true and 0 otherwise")
```


### Table 1 Main Text with Stringent Symptom and ILI Criteria 

If we apply a more stringent criteria to the symptom classification where symptoms that appear before study day 1 are not included in the criteria for symptoms or ILI, then we get a slightly different count for the symptomatic category, but not for the ILI. 

Printing a new Table 1 with the stringent criteria applied.

```{r, echo = FALSE, include = FALSE}
Table1_Stringent_Sympt_ILI_Table <- read_csv("/Users/jbueno/Box Sync/EMIT/EMIT_Data_Analysis_Jake/EMIT_Quarantine/Analysis Results/SI_prior_sympt_Table1_Manuscript.csv") %>%
  select(-X1)
```

```{r, echo = FALSE}
kable(Table1_Stringent_Sympt_ILI_Table, align = "c") %>%
  kable_styling(c("striped", "bordered", "hover", "condensed", "responsive"), 
                full_width = FALSE, position = "center", font_size = 10) %>%
  add_header_above(c(" " = 2, "Laboratory Confirmed Infection & Illness (% of Infected)" = 3, 
                     "Laboratory Confirmed Infection Criteria (% of Infected)" = 3)) %>%
  column_spec(1, bold = T) %>%
  row_spec(4, bold = TRUE)
```

If we wanted to compare just the original Table 1 with this new, more stringent version, by the only variables of interest (sympomatic, febrile, and ILI), we could produce a new comparison table. 

```{r, echo = FALSE}
Table1_Stringent_Sympt_ILI_Table_Compare <- Table1_Stringent_Sympt_ILI_Table %>%
  select(`Quarantine #`, Symptomatic, ILI) %>%
  rename(`Stringent Symptomatic` = Symptomatic,
         `Stringent ILI` = ILI)

Table1_Compare <- Table1 %>%
  select(`Quarantine #`, Symptomatic, ILI) %>%
  rename(`Table1 Symptomatic` = Symptomatic,
         `Table1 ILI` = ILI)

Table1_Stringent_Comparison_Table <- Table1_Compare %>%
  left_join(Table1_Stringent_Sympt_ILI_Table_Compare)

kable(Table1_Stringent_Comparison_Table, align = "c") %>%
  kable_styling(c("striped", "bordered", "hover", "condensed", "responsive"), 
                full_width = FALSE, position = "center", font_size = 10) %>%
  add_header_above(c(" " = 1, "Original Table 1 Values" = 2, "Stringent Criteria Values" = 2)) %>%
  column_spec(1, bold = T) %>%
  row_spec(4, bold = TRUE)
```

It's easy to see here that the more stringent criteria produces very similar values compared with the original Table 1. The minor difference is that the symptomatic group by stringent criteria is 2 less compared with the original Table 1.  


## Supporting Analysis: Infected Donor Viral Shedding

Printing the number of person-day G2 samples by quarantine.

```{r, echo = FALSE, include = FALSE}
G2_PersonDay_Samples_Q_Table <- read_csv("/Users/jbueno/Box Sync/EMIT/EMIT_Data_Analysis_Jake/EMIT_Quarantine/Analysis Results/Exhaled_breath_person_days_by_Q.csv")
```

```{r, echo = FALSE}
kable(G2_PersonDay_Samples_Q_Table, align = "c") %>%
  kable_styling(c("striped", "bordered", "hover", "condensed", "responsive"), 
                full_width = FALSE, position = "center", font_size = 10) %>%
  column_spec(1, bold = T) %>%
  row_spec(4, bold = TRUE)
```

Now examining within each of these quarantines, to see how many subjects gave how many G2 samples.

```{r, echo = FALSE, include = FALSE}
G2_Volunteer_Number_Samples_Q_Table <- read_csv("/Users/jbueno/Box Sync/EMIT/EMIT_Data_Analysis_Jake/EMIT_Quarantine/Analysis Results/G2_visits_by_person_Q.csv")
```

```{r, echo = FALSE}
kable(G2_Volunteer_Number_Samples_Q_Table, align = "c") %>%
  kable_styling(c("striped", "bordered", "hover", "condensed", "responsive"), 
                full_width = FALSE, position = "center", font_size = 10) %>%
  column_spec(1, bold = T) %>%
  row_spec(4, bold = TRUE)
```

Printing the number of volunteers who gave G2 samples over the 4 study days of exhaled breath collection (study days 1-4).

```{r, echo = FALSE, include = FALSE}
G2_Volunteers_StudyDay_Table <- read_csv("/Users/jbueno/Box Sync/EMIT/EMIT_Data_Analysis_Jake/EMIT_Quarantine/Analysis Results/G2_visits_by_studyday.csv")
```

```{r, echo = FALSE}
kable(G2_Volunteers_StudyDay_Table, align = "c") %>%
  kable_styling(c("striped", "bordered", "hover", "condensed", "responsive"), 
                full_width = FALSE, position = "center", font_size = 10) %>%
  column_spec(1, bold = T) %>%
  row_spec(5, bold = TRUE)
```


### Geometric Mean and Geometric Standard Deviation of Positive Fine and Coarse Samples

This information was included in the footnote of Table 2. 

Fine Aerosol Shedding (Among Infected Donors in the Quarantine Trial)
```{r, echo = FALSE, include = FALSE}
Fine_Aerosol_Table2_Footnote <- read_csv("/Users/jbueno/Box Sync/EMIT/EMIT_Data_Analysis_Jake/EMIT_Quarantine/Analysis Results/Table2_Fine_footnote.csv") %>%
  select(-X1)
Fine_Aerosol_Table2_Footnote$Fine_Positive_Samples_GeomMean <- 
  format(Fine_Aerosol_Table2_Footnote$Fine_Positive_Samples_GeomMean, scientific = TRUE)
Fine_Aerosol_Table2_Footnote$Fine_Positive_Samples_Max <- 
  format(Fine_Aerosol_Table2_Footnote$Fine_Positive_Samples_Max, scientific = TRUE)
```

```{r, echo = FALSE}
kable(Fine_Aerosol_Table2_Footnote, align = "c") %>%
  kable_styling(c("striped", "bordered", "hover", "condensed", "responsive"), 
                full_width = FALSE, position = "center", font_size = 10)
```

Coarse Aerosol Shedding (Among Infected Donors in the Quarantine Trial)
```{r, echo = FALSE, include = FALSE}
Coarse_Aerosol_Table2_Footnote <- read_csv("/Users/jbueno/Box Sync/EMIT/EMIT_Data_Analysis_Jake/EMIT_Quarantine/Analysis Results/Table2_Coarse_footnote.csv") %>%
  select(-X1)
Coarse_Aerosol_Table2_Footnote$Coarse_Positive_Samples_Geom_Mean <- 
  format(Coarse_Aerosol_Table2_Footnote$Coarse_Positive_Samples_Geom_Mean, scientific = TRUE)
Coarse_Aerosol_Table2_Footnote$Coarse_Positive_Samples_Max <- 
  format(Coarse_Aerosol_Table2_Footnote$Coarse_Positive_Samples_Max, scientific = TRUE)
```

```{r, echo = FALSE}
kable(Coarse_Aerosol_Table2_Footnote, align = "c") %>%
  kable_styling(c("striped", "bordered", "hover", "condensed", "responsive"), 
                full_width = FALSE, position = "center", font_size = 10)
```


### For Comparison: The Geometric Mean and Geometric Standard Deviation of Fine and Coarse Samples from UMD Population

Fine Aerosol Shedding (Among Influenza Cases in EMIT UMD Population -- 2012-13 flu season among UMD campus community)
```{r, echo = FALSE, include = FALSE}
UMD_Fine_Pos_Samples_Geom_Mean <- read_csv("/Users/jbueno/Box Sync/EMIT/EMIT_Data_Analysis_Jake/EMIT_Quarantine/Analysis Results/UMD_Fine_Pos_Samples_Geom_Mean.csv") %>%
  select(-X1)
UMD_Fine_Pos_Samples_Geom_Mean$Fine_Positive_Samples_Geom_Mean <- 
  format(UMD_Fine_Pos_Samples_Geom_Mean$Fine_Positive_Samples_Geom_Mean, scientific = TRUE)
UMD_Fine_Pos_Samples_Geom_Mean$Fine_Positive_Samples_Max <- 
  format(UMD_Fine_Pos_Samples_Geom_Mean$Fine_Positive_Samples_Max, scientific = TRUE)
```

```{r, echo = FALSE}
kable(UMD_Fine_Pos_Samples_Geom_Mean, align = "c") %>%
  kable_styling(c("striped", "bordered", "hover", "condensed", "responsive"), 
                full_width = FALSE, position = "center", font_size = 10)
```

Coarse Aerosol Shedding (Among Influenza Cases in EMIT UMD Population -- 2012-13 flu season among UMD campus community)
```{r, echo = FALSE, include = FALSE}
UMD_Coarse_Pos_Samples_Geom_Mean <- read_csv("/Users/jbueno/Box Sync/EMIT/EMIT_Data_Analysis_Jake/EMIT_Quarantine/Analysis Results/UMD_Coarse_Pos_Samples_Geom_Mean.csv") %>%
  select(-X1)
UMD_Coarse_Pos_Samples_Geom_Mean$Coarse_Positive_Samples_Geom_Mean <- 
  format(UMD_Coarse_Pos_Samples_Geom_Mean$Coarse_Positive_Samples_Geom_Mean, scientific = TRUE)
UMD_Coarse_Pos_Samples_Geom_Mean$Coarse_Positive_Samples_Max <- 
  format(UMD_Coarse_Pos_Samples_Geom_Mean$Coarse_Positive_Samples_Max, scientific = TRUE)
```

```{r, echo = FALSE}
kable(UMD_Coarse_Pos_Samples_Geom_Mean, align = "c") %>%
  kable_styling(c("striped", "bordered", "hover", "condensed", "responsive"), 
                full_width = FALSE, position = "center", font_size = 10)
```


### Boxplots of Fine and Coarse Aerosol Shedding
There were 14 positive fine and 6 poisitive coarse samples.
Previously we plotted these without using log base 10 ont he y-axis and had issues, so we are going to plot these boxplots using log10 on the y-axis.

```{r, echo = FALSE, include = FALSE}
Table_2_Copies <- read_csv("/Users/jbueno/Box Sync/EMIT/EMIT_Data_Analysis_Jake/EMIT_Quarantine/Analysis Results/Donor_Aerosol_Copies.csv") %>%
  select(-X1)
```

```{r, echo = FALSE}
Table_2_Copies_log <- Table_2_Copies %>%
  mutate(log10.copies = log10(final.copies.replicate.avg))

f <- ggplot(Table_2_Copies_log, aes(G2.pcr.copies.sample.type, log10.copies))
f + geom_boxplot() + theme_bw() + labs(x = "Aerosol Fraction", y = "Log10 RNA Copies")
```

## Supporting Analysis: Recipient Status

### Table 3 Footnote: Greater than Anticipated Immunity Upon Entry to Quarantine

Greater than anticipated immunity for Intervention Receipients (IR).

```{r, echo = FALSE, include = FALSE}
Table3_IR_Footnote <- read_csv("/Users/jbueno/Box Sync/EMIT/EMIT_Data_Analysis_Jake/EMIT_Quarantine/Analysis Results/Table3_IR_Footnote_Reportable_Summary.csv") %>%
  select(-X1)
```

```{r, echo = FALSE}
kable(Table3_IR_Footnote, align = "c") %>%
  kable_styling(c("striped", "bordered", "hover", "condensed", "responsive"), 
                full_width = FALSE, position = "center", font_size = 10)
```

Greater than anticipated immunity for Control Receipients (CR).
```{r, echo = FALSE, include = FALSE}
Table3_CR_Footnote <- read_csv("/Users/jbueno/Box Sync/EMIT/EMIT_Data_Analysis_Jake/EMIT_Quarantine/Analysis Results/Table3_CR_Footnote_Reportable_Summary.csv") %>%
  select(-X1)
```

```{r, echo = FALSE}
kable(Table3_CR_Footnote, align = "c") %>%
  kable_styling(c("striped", "bordered", "hover", "condensed", "responsive"), 
                full_width = FALSE, position = "center", font_size = 10)
```


### Table 3 Footnote: Greater than Anticipated Immunity Upon Entry to Quarantine

If we apply a more stringent criteria to the symptom classification where symptoms that appear before study day 1 are not included in the criteria for symptomatic and ILI, then we gets slightly different counts.

Printing a new Table 3 with the stringent criteria applied.

```{r, echo = FALSE, include = FALSE}
Table3_Stringent_Sympt_ILI_Table <- read_csv("/Users/jbueno/Box Sync/EMIT/EMIT_Data_Analysis_Jake/EMIT_Quarantine/Analysis Results/SI_prior_sympt_Table3_Manuscript.csv") %>%
  select(-X1)
```

```{r, echo = FALSE}
kable(Table3_Stringent_Sympt_ILI_Table, align = "c") %>%
  kable_styling(c("striped", "bordered", "hover", "condensed", "responsive"), 
                full_width = FALSE, position = "center", font_size = 10) %>%
  add_header_above(c(" " = 3, "Clinical Illness (% of Exposed)" = 3, 
                     "Laboratory Confirmed Infection Criteria (% of Exposed)" = 3)) %>%
  kable_styling(full_width = F) %>%
  column_spec(1, bold = T) %>%
  collapse_rows(columns = 1, valign = "top") %>%
  row_spec(7:8, bold = TRUE)
```

If we wanted to compare just the original Table 3 with this new, more stringent version, by the only variables of interest (sympomatic and ILI), we could produce a new comparison table. 

```{r, echo = FALSE}
Table3_Stringent_Sympt_ILI_Table_Compare <- Table3_Stringent_Sympt_ILI_Table %>%
  select(`Quarantine #`, `Recipient Classification`, Symptomatic, ILI) %>%
  rename(`Stringent Symptomatic` = Symptomatic,
         `Stringent ILI` = ILI)

Table3_Compare <- Table3 %>%
  select(`Quarantine #`, `Recipient Classification`, Symptomatic, ILI) %>%
  rename(`Table3 Symptomatic` = Symptomatic,
         `Table3 ILI` = ILI)

Table3_Stringent_Comparison_Table <- bind_cols(Table3_Compare, Table3_Stringent_Sympt_ILI_Table_Compare) %>%
  select(-`Quarantine #1`, -`Recipient Classification1`)

kable(Table3_Stringent_Comparison_Table, align = "c") %>%
  kable_styling(c("striped", "bordered", "hover", "condensed", "responsive"), 
                full_width = FALSE, position = "center", font_size = 10) %>%
  add_header_above(c(" " = 2, "Original Table 3 Values" = 2, "Stringent Criteria Values" = 2)) %>%
  column_spec(1, bold = T) %>%
  collapse_rows(columns = 1, valign = "top") %>%
  row_spec(7:8, bold = TRUE)
```

It's easy to see here that the more stringent criteria produces very similar values compared with the original Table 3. The minor difference is that the CR symptomatic group by stringent criteria is 2 less, and the CR ILI group by stringent criteria is 1 less. 

### Serology for the CR that Seroconverted and the 2 CRs with 1 Day of PCR Positivity

## MN Serology for the 3 CR with Some Evidence of Infection

```{r, echo = FALSE, include = FALSE}
Seroconverted_CR_MN_Serology <-  read_csv("/Users/jbueno/Box Sync/EMIT/EMIT_Data_Analysis_Jake/EMIT_Quarantine/Analysis Results/Seroconverted_CR_MN_Serology.csv") %>%
  select(-X1)

PCR_Pos_CR_Serology_Report_MN <- read_csv("/Users/jbueno/Box Sync/EMIT/EMIT_Data_Analysis_Jake/EMIT_Quarantine/Analysis Results/PCR_Pos_CR_Serology_Report_MN.csv") %>%
  select(-X1)
```

```{r, echo = FALSE}
Evidence_Infection_Report_Serology_MN <- rbind(Seroconverted_CR_MN_Serology, PCR_Pos_CR_Serology_Report_MN)

kable(Evidence_Infection_Report_Serology_MN, align = "c") %>%
  kable_styling(c("striped", "bordered", "hover", "condensed", "responsive"), 
                full_width = FALSE, position = "center", font_size = 10) %>%
  kable_styling(full_width = F) %>%
  column_spec(1, bold = T) %>%
  collapse_rows(columns = 1, valign = "top") %>%
  footnote(symbol = c("Volunteer 233 was positive by both MN and HAI. Volunteers 236 and 242 did not seroconvert by either MN or HAI, however they each had a single day of PCR positivity, thus we wanted to explore their serology scores regardless."))
```

## HAI Serology for the 3 CR with Some Evidence of Infection

```{r, echo = FALSE, include = FALSE}
Seroconverted_CR_HAI_Serology <-  read_csv("/Users/jbueno/Box Sync/EMIT/EMIT_Data_Analysis_Jake/EMIT_Quarantine/Analysis Results/Seroconverted_CR_HAI_Serology.csv") %>%
  select(-X1)

PCR_Pos_CR_Serology_Report_HAI <- read_csv("/Users/jbueno/Box Sync/EMIT/EMIT_Data_Analysis_Jake/EMIT_Quarantine/Analysis Results/PCR_Pos_CR_Serology_Report_HAI.csv") %>%
  select(-X1)
```

```{r, echo = FALSE}
Evidence_Infection_Report_Serology_HAI <- rbind(Seroconverted_CR_HAI_Serology, PCR_Pos_CR_Serology_Report_HAI)

kable(Evidence_Infection_Report_Serology_HAI, align = "c") %>%
  kable_styling(c("striped", "bordered", "hover", "condensed", "responsive"), 
                full_width = FALSE, position = "center", font_size = 10) %>%
  kable_styling(full_width = F) %>%
  column_spec(1, bold = T) %>%
  footnote(symbol = c("Volunteer 233 was positive by both MN and HAI. Volunteers 236 and 242 did not seroconvert by either MN or HAI, however they each had a single day of PCR positivity, thus we wanted to explore their serology scores. NDA: No detectable antibody."))
```

## PCR ct values for the NP swabs of Recipients with only one day of detectable InfA RNA

```{r, echo = FALSE, include = FALSE}
PCR_Pos_CR_pcr_Report <-  read_csv("/Users/jbueno/Box Sync/EMIT/EMIT_Data_Analysis_Jake/EMIT_Quarantine/Analysis Results/PCR_Pos_CR_pcr_Report.csv") %>%
  select(-X1)
```

```{r, echo = FALSE}
kable(PCR_Pos_CR_pcr_Report, align = "c") %>%
  kable_styling(c("striped", "bordered", "hover", "condensed", "responsive"), 
                full_width = FALSE, position = "center", font_size = 10) %>%
  kable_styling(full_width = F) %>%
  column_spec(1, bold = T) %>%
  footnote(symbol = c("Volunteers 236 and 242 did not seroconvert by either MN or HAI, however they each had a single day of PCR positivity. CR: Control Recipient"))
```


### Symptoms for the CR that Seroconverted and the 2 CRs with 1 Day of PCR Positivity

Perhaps the easiest way to describe this is through a line plot. Volunteer 233 had some symptoms but volunteers 236 and 242 did not have any symptoms at all. Thus the line plot will plot the symptoms from 233 only. 

The total respiratory score, presented here is a sum of the total "respiratory"" symptom scores for each day of symptoms surveillance. "Respiratory" symptoms include: runny nose, stuffy nose, sneezing, sore throat, cough, and shortness of breath.

Note: We took a mean symptom score for the combined respiratory symptom scores (runnyNose, stuffyNose, sneezing, soreThroat, cough, SOB), by score instance (there were mostly 3 instances of symptoms scoring per day). Then took the mean by study day to get a single mean, total respiratory symtpom score for study days -3 through 10. 

```{r, echo = FALSE, include = FALSE}
Seroconverted_CR_Symptoms_Line <-  read_csv("/Users/jbueno/Box Sync/EMIT/EMIT_Data_Analysis_Jake/EMIT_Quarantine/Analysis Results/Seroconverted_CR_Symptoms_Line.csv") %>%
  select(-X1)
```

```{r, echo = FALSE}
p <- ggplot(Seroconverted_CR_Symptoms_Line, aes(x = StudyDay, y = `Mean Total Respiratory Score`, group=1)) +
  geom_line() +
  geom_point(shape=21, size=3, fill="white") +
  theme_bw() +
  xlab("Study Day") +
  ylab("Total Respiratory Score")
p
```


## Applying Killingley et al, 2012 qRT-PCR infection criteria to Main Q study

### Table 1: Donor Status Using Proof of Concept Study (Killingley et al, 2012) Infection Criteria

```{r, echo = FALSE, include = FALSE}
table1_POC_criteria <-  read_csv("/Users/jbueno/Box Sync/EMIT/EMIT_Data_Analysis_Jake/EMIT_Quarantine/Analysis Results/SI_POC_Criteria_Table1_Manuscript.csv") %>%
  select(-X1)
```

```{r, echo = FALSE}
kable(table1_POC_criteria, align = "c") %>%
  kable_styling(c("striped", "bordered", "hover", "condensed", "responsive"), 
                full_width = FALSE, position = "center", font_size = 10) %>%
  add_header_above(c(" " = 2, "Clinical Illness (% of Infected)" = 3, 
                     "Laboratory Confirmed Infection Criteria (% of Infected)" = 3)) %>%
  column_spec(1, bold = T) %>%
  row_spec(4, bold = TRUE)
```

### Table 3: Recipient Status Using Proof of Concept Study (Killingley et al, 2012) Infection Criteria

```{r, echo = FALSE, include = FALSE}
table3_POC_criteria <-  read_csv("/Users/jbueno/Box Sync/EMIT/EMIT_Data_Analysis_Jake/EMIT_Quarantine/Analysis Results/SI_POC_Criteria_Table3_Manuscript.csv") %>%
  select(-X1)
```

```{r, echo = FALSE}
kable(table3_POC_criteria, align = "c") %>%
  kable_styling(c("striped", "bordered", "hover", "condensed", "responsive"), 
                full_width = FALSE, position = "center", font_size = 10) %>%
  add_header_above(c(" " = 3, "Clinical Illness (% of Exposed)" = 3, 
                     "Laboratory Confirmed Infection Criteria (% of Exposed)" = 3)) %>%
  kable_styling(full_width = F) %>%
  column_spec(1, bold = T) %>%
  collapse_rows(columns = 1, valign = "top") %>%
  row_spec(7:8, bold = TRUE)
```

# Group Comparisons with Respect to SAR Outcomes
```{r, echo = TRUE}

## Making some comparisons between the Proof of Concept study (PoC) and the Main Q study

# Comparing PoC with PoC criteria versus Main Q with Main Q criteria:
fisher.test(matrix(c(3, 12-3, 1, 75-1), ncol = 2))

# Comparing PoC with PoC criteria versus Main Q CR with Main Q criteria:
fisher.test(matrix(c(3, 12-3, 1, 35-1), ncol = 2))

# But the correct comparison (applying the more stringent Main Q criteria to both studies) between Main Q overall and PoC would be:
fisher.test(matrix(c(1, 12-1, 1, 75-1), ncol = 2))

# Correct comparison (stringent Main Q criteria applied to both studies) between Main Q CR and PoC:
fisher.test(matrix(c(1, 12-1, 1, 35-1), ncol = 2))

# Correct comparison (stringent Main Q criteria applied to both studies) between Main Q IR and PoC:
fisher.test(matrix(c(1, 12-1, 0, 40-0), ncol = 2))

# Correct comparison (stringent Main Q criteria) of Main Q CR to Main Q IR:
fisher.test(matrix(c(1, 35-1, 0, 40-0), ncol = 2))

# Correct comparison (with less stringent PoC criteria) of Main Q CR to Main Q IR
fisher.test(matrix(c(3, 35-3, 0, 40-0), ncol = 2))

## Additional computations using PoC criteria to compare between both studies:

# Comparing PoC with IR
fisher.test(matrix(c(3, 12-3, 0, 40-0), ncol = 2))
# Comparing PoC with CR
fisher.test(matrix(c(3, 12-3, 3, 35-3), ncol = 2))
# Comparing PoC with overall Main Q
fisher.test(matrix(c(3, 12-3, 3, 75-3), ncol = 2))

## Using the Main Q criteria for infection status in both studies: 
## We got 1/75 (overall, and 1/35 for CR group, and 0/40 for IR group) but we were expecting at least .16 (double the infecteds and recipients, double the time)
# Comparing the theoretical ~16% expected versus the 1/75 actual (overall study)
n_successes = 1
n = 75
p = 0.16
binom.test(n_successes, n, p)$p.value
# Comparing the theoretical ~16% expected versus the 1/35 actual (CR group only)
n_successes = 1
n = 35
p = 0.16
binom.test(n_successes, n, p)$p.value

## Using the POC criteria for infection status in both studies:
## We got 3/75 (overall, and 3/35 for the CR group, and 0/40 for IR group) and we were expecting at least .5 (double the infecteds and recipients, double the time)
# Comparing the theoretical ~50% expected versus the 1/75 actual (overall study)
n_successes = 3
n = 75
p = 0.50
binom.test(n_successes, n, p)$p.value
# Comparing the theoretical ~50% expected versus the 1/35 actual (CR group only)
n_successes = 3
n = 35
p = 0.50
binom.test(n_successes, n, p)$p.value
```

# Baseline Characteristics Table
```{r, echo = FALSE, results = 'asis'}
Qdata <- read.csv("/Users/jbueno/Box Sync/EMIT/EMIT_Data_Analysis_Jake/EMIT_Quarantine/Curated Data/Analytical Datasets/QuarantineMergedData.csv")

Baseline_vars <- Qdata %>%
  select(SubjectID, Randomization_DorIRorCR, Randomization_DorR, QuarantineNumber, Gender, Age, Microneut_VisitType, Microneutralization.Titer.to.A.Wisconsin.67.2005, HAI_dayminus2, HAI_dayminus2_recodeNDA) %>%
  filter(Microneut_VisitType == "Q baseline") %>%
  distinct(SubjectID, Randomization_DorIRorCR, .keep_all = TRUE)

tab2 <- tableby(Randomization_DorIRorCR ~ Gender + Age, data = Baseline_vars,
                control = tableby.control(numeric.stats = "medianq1q3", digits = 0L, total = FALSE, test = FALSE))
summary(tab2)
```

