---
title: "Quality Check Report"
subtitle: "<h4>GWASinspector package</h4>"
output:
  html_document:
    css: styles_extra.css
---

<style>
.main-container {
    max-width: 90%;
    margin-left: auto;
    margin-right: auto;
}
</style>


```{r, echo=FALSE}
htmltools::img(src = knitr::image_uri(system.file("extdata", "DNA.png", package = "GWASinspector")), 
               alt = 'logo', 
               style = 'position:absolute; top:0; right:20%; padding:10px;')
```

***

<br/>
Start Time:		**`r format(.QC$config$new_items$starttime, "%b %d %Y - %X")`**

End time:		**`r format(.QC$config$new_items$endtime, "%b %d %Y - %X")`**

Script version: **`r .QC$script.version`**

System Information : **`r .QC$r.version`**


<br/>


<h3 class="header1">Input GWAS Files</h3>



`r  report.table <- data.table(sapply(.QC$qc.study.list, 
                                  function(x) 
                                    return(paste0("<a style='font-weight:bold' target='_blank' href='",basename(x$html.report.path),"'>",basename(x$file.path),"</a>"))))
                                    
  report.table <- cbind(sapply(.QC$qc.study.list, function(x) return(x$number)) , report.table)
  colnames(report.table) <- c('File number', 'Input file name')
  row.names(report.table) <- sapply(.QC$qc.study.list, function(x) return(x$number))

kable(report.table, align = "c" ,escape = FALSE, format = "html") %>% 
kableExtra::kable_styling(bootstrap_options = c("striped", "hover", "condensed", "responsive"), 
full_width = FALSE, 
position = "left") %>%
kableExtra::column_spec(1, bold = TRUE,width="10em",extra_css='text-align:center', border_right = T) %>%
kableExtra::column_spec(2, extra_css='text-align:left')
`

***

<h4 class="header2">Summary of the input GWAS files</h4>

`r report.table <- t(data.table(
       "<span class='mainItem'>Sample size (Max)</span>" = sapply(.QC$qc.study.list, function(x) return(thousand.sep(x$MAX_N_TOTAL))),
	   
	    "<span class='mainItem'>Missing columns</span>" = sapply(.QC$qc.study.list, function(x) return(paste(x$missing.Columns, collapse = ' | '))), 
		
       "<span class='mainItem'>Variants in input file</span>" = sapply(.QC$qc.study.list, function(x) return(thousand.sep(x$input.data.rowcount))), 
		"<span class='item'>Missing crucial variable</span>" = sapply(.QC$qc.study.list, function(x) return(calculatePercent(x$missing.crucial.rowcount,
                                                  x$input.data.rowcount,
                                                  pretty = T))),
												  
       "<span class='mainItem'>Variant count after step 1</span>" = sapply(.QC$qc.study.list, function(x) return(calculatePercent(x$rowcount.step1,
                                                     x$input.data.rowcount,
                                                     decimal.place=3,
                                                     pretty = T))),
													 
	   "<span class='item'>Monomorphic variants</span>" = sapply(.QC$qc.study.list, function(x) return(calculatePercent(x$monomorphic.count,x$rowcount.step3,pretty=TRUE))),
	   
	   "<span class='mainItem'>Variant count after step 2</span>" = sapply(.QC$qc.study.list, function(x) return(calculatePercent(x$rowcount.step2,
                                                      x$input.data.rowcount,
                                                      decimal.place=3,
                                                      pretty = T))),
	   "<span class='mainItem'>Variant count after step 3</span>" = sapply(.QC$qc.study.list, function(x) return(calculatePercent(x$rowcount.step3,
                                                       x$input.data.rowcount,
                                                       decimal.place=4,
                                                       pretty = T))),
	   "<span class='mainItem'>SNP variants</span>" = sapply(.QC$qc.study.list, function(x) return(calculatePercent(sum(as.numeric(gsub(x=x$tables$multi_allele_count_preProcess[1:3],
                                                                                                pattern = ",",
                                                                                                replacement = ""))),
                                                       x$rowcount.step3,
                                                       decimal.place=4,
                                                       pretty = T))),
	   "<span class='mainItem'>Non-SNP variants</span>" = sapply(.QC$qc.study.list, function(x) return(calculatePercent(sum(as.numeric(gsub(x=x$tables$multi_allele_count_preProcess[4:6],
                                                                                                pattern = ",",
                                                                                                replacement = ""))),
                                                       x$rowcount.step3,
                                                       decimal.place=4,
                                                       pretty = T))),
	   "<span class='mainItem'>Palindromic variants</span>" = sapply(.QC$qc.study.list, function(x) return(calculatePercent(x$palindromic.rows,x$rowcount.step3,pretty=TRUE))),
       "<span class='mainItem'>Genotyped variants</span>" = sapply(.QC$qc.study.list, function(x) return(ifelse(x$tables$imputed.tbl[IMPUTED=='genotyped',.N] == 0,
                                                 "NA",                                                 calculatePercent(as.numeric(x$tables$imputed.tbl[IMPUTED=="genotyped",N]),x$rowcount.step3,pretty=TRUE)))),
       "<span class='mainItem'>Imputed variants</span>" = sapply(.QC$qc.study.list, function(x) return(ifelse(x$tables$imputed.tbl[IMPUTED=='imputed',.N] == 0,
                                                 "NA",                                                 calculatePercent(as.numeric(x$tables$imputed.tbl[IMPUTED=="imputed",N]),x$rowcount.step3,pretty=TRUE)))),
       "<span class='mainItem'>Negative-strand variants</span>" = sapply(.QC$qc.study.list, function(x) return(calculatePercent(x$neg.strand.count,x$rowcount.step3,pretty=TRUE))),
	   "<span class='mainItem'>Allele frequency correlation</span>" = '',
	   "<span class='item'>Standard reference</span>" = sapply(.QC$qc.study.list, function(x) return(x$AFcor.std_ref)),
	   "<span class='item'>Alternative reference</span>" = sapply(.QC$qc.study.list, function(x) return(x$AFcor.alt_ref)),
       "<span class='mainItem'>Palindromic allele frequency correlation</span>" = '',
       "<span class='item'>Standard reference</span>" = sapply(.QC$qc.study.list, function(x) return(x$AFcor.palindromic.std_ref)),
       "<span class='item'>Alternative reference</span>" = sapply(.QC$qc.study.list, function(x) return(x$AFcor.palindromic.alt_ref)),
       "<span class='mainItem'>Lambda - total</span>" = sapply(.QC$qc.study.list, function(x) return(x$lambda)),
       "<span class='mainItem'>Lambda - genotyped</span>" = sapply(.QC$qc.study.list, function(x) return(x$lambda.gen)),
       "<span class='mainItem'>Lambda - imputed</span>" = sapply(.QC$qc.study.list, function(x) return(x$lambda.imp)),
	   "<span class='mainItem'>P-value correlation (obs. vs exp.)</span>" = sapply(.QC$qc.study.list, function(x) return(x$PVcor)),
       "<span class='mainItem'>Visscher's statistic (HQ variants)</span>" = sapply(.QC$qc.study.list, function(x) return(x$Visschers.stat.HQ)),
              "<span class='mainItem'>Fixed HWE P-value</span>" = sapply(.QC$qc.study.list, function(x) return(x$fixed.hwep)),
	   "<span class='mainItem'>Fixed imputation quality</span>" = sapply(.QC$qc.study.list, function(x) return(x$fixed.impq)),
	   "<span class='mainItem'>Fixed sample size</span>" = sapply(.QC$qc.study.list, function(x) return(x$fixed.n_total)),
	   "<span class='mainItem'>Fixed call rate</span>" = sapply(.QC$qc.study.list, function(x) return(x$fixed.callrate)),
	   "<span class='mainItem'>Effect size</span>" = ' ',
       "<span class='item'>Min.</span>" = sapply(.QC$qc.study.list, function(x) return(x$tables$variable.summary['Min.',  .QC$config$input_parameters$effect_type_string])),
	   "<span class='item'>1st Qu.</span>" = sapply(.QC$qc.study.list, function(x) return(x$tables$variable.summary['1st Qu.',  .QC$config$input_parameters$effect_type_string])),
       "<span class='item'>Median</span>" = sapply(.QC$qc.study.list, function(x) return(x$tables$variable.summary['Median', .QC$config$input_parameters$effect_type_string])),
	   "<span class='item'>Mean</span>" = sapply(.QC$qc.study.list, function(x) return(x$tables$variable.summary['Mean', .QC$config$input_parameters$effect_type_string])),
	   "<span class='item'>3rd Qu.</span>" = sapply(.QC$qc.study.list, function(x) return(x$tables$variable.summary['3rd Qu.', .QC$config$input_parameters$effect_type_string])),
	   "<span class='item'>Max.</span>" = sapply(.QC$qc.study.list, function(x) return(x$tables$variable.summary['Max.', .QC$config$input_parameters$effect_type_string])),
       "<span class='item'>Min. (HQ variants)</span>" = sapply(.QC$qc.study.list, function(x) return(ifelse(nrow(x$tables$variable.summary.HQ) > 0,
																								   x$tables$variable.summary.HQ['Min.',  .QC$config$input_parameters$effect_type_string], 
																								   NA))),
	   "<span class='item'>Max. (HQ variants)</span>" = sapply(.QC$qc.study.list, function(x) return(ifelse(nrow(x$tables$variable.summary.HQ) > 0,
																								   x$tables$variable.summary.HQ['Max.', .QC$config$input_parameters$effect_type_string], 
																								   NA))),
	    "<span class='mainItem'>Standard error (median)</span>" = sapply(.QC$qc.study.list, function(x) return(x$tables$variable.summary['Median','STDERR'])),
		"<span class='mainItem'>Standard error (median) (HQ variants)</span>" = sapply(.QC$qc.study.list, function(x) return(ifelse(nrow(x$tables$variable.summary.HQ) > 0,
																								   x$tables$variable.summary.HQ['Median','STDERR'], 
																								   NA)))
       ))
      
	    colnames(report.table) <- sapply(.QC$qc.study.list, function(x) return(x$number))
		study.col.count <- length(.QC$qc.study.list) + 1

prc <-  sprintf("%s%%", study.col.count * 15)	
	
kable(report.table,escape = FALSE, align = "c" , format = "html") %>% 
kableExtra::kable_styling(bootstrap_options = c("striped", "hover", "condensed", "responsive"), 
full_width = TRUE, 
position = "left") %>%
kableExtra::column_spec(1, bold = TRUE, width="20em", border_right = T,extra_css="vertical-align:middle"  ) %>%
kableExtra::column_spec(2:study.col.count, width="20em" , border_right = T ) %>%
kableExtra::scroll_box(width = prc)

`
<br/>
  <div>step1: removing variants with missing crucial values and duplicated lines.</div>
  <div>step2: removing monomorphic variants and specified chromosomes.</div>
  <div>step3: removing mismatched, ambiguous and multi-allelic variants that could not be verified.</div>


<h3 class="header1">Plots</h3>

`r 
if(file.exists(.QC$config$paths$precisionPlotPath) & .QC$graphic.device != 'tiff')
	htmltools::img(src = knitr::image_uri(.QC$config$paths$precisionPlotPath), 
               alt = 'MAF',width='80%')

`


***


`r 
if(file.exists(.QC$config$paths$skew_kurt) & .QC$graphic.device != 'tiff')
	htmltools::img(src = knitr::image_uri(.QC$config$paths$skew_kurt), 
               alt = 'MAF',width='80%')

`

*** 




<h4 class="header2">Effect-size box plot</h4>
`r paste0("<a target='_blank' href='",basename(.QC$config$paths$effsizePlotPath),"'><font color='blue'>click for full-size plot</font></a>")`

`r 
if(file.exists(.QC$config$paths$effsizePlotPath) & .QC$graphic.device != 'tiff' )
	htmltools::img(src = knitr::image_uri(.QC$config$paths$effsizePlotPath), 
               alt = 'MAF',height='100%')

`

