# 1. Quality Controls

# Generation of Quality Controls

SeSAMe provides a set of quality control steps.

The SeSAMe QC function returns an `sesameQC` object which can be directly printed onto the screen.

    sesameQC(ssets[[1]])

    ##
    ## =======================
    ## =      Intensities    =
    ## =======================
    ## No. probes                      485577
    ## mean (M/U) (in-band InfI):      5529.506
    ## mean (M+U) (in-band InfI):      11059.01
    ##
    ## -- Infinium II --
    ## No. probes:                    350076 (72.095%)
    ## Mean Intensity:                 5160.813
    ##
    ## -- Infinium I (Red) --
    ## No. probes:                     89203 (18.371%)
    ## No. Probes Consistent Channel:  88799
    ## No. Porbes Swapped Channel:     162
    ## No. Probes Low Intensity:       242
    ## Mean Intensity (in-band):       6527.3
    ## Mean Intensity (out-of-band):   928.2117
    ##
    ## -- Infinium I (Grn) --
    ## No. probes:                     46298 (9.535%)
    ## No. Probes Consistent Channel:  46000
    ## No. Probes Swapped Channel:     254
    ## No. Probes Low Intensity:       44
    ## Mean Intensity (in-band):       6394.865
    ## Mean Intensity (out-of-band):   640.0676
    ##
    ## =======================
    ## =      Beta Values    =
    ## =======================
    ## No. probes:                     485577
    ## No. probes with NA:             64144 (13.210%)
    ## Mean Betas:                     0.5072937
    ## Median Betas:                   0.6090879
    ##
    ## -- cg probes --
    ## No. Probes:                     482421
    ## No. Probes with NA:             63563 (13.176%)
    ## Mean Betas:                     0.5098332
    ## Median Betas:                   0.6173473
    ## % Unmethylated (Beta < 0.3):    40.023%
    ## % Methylated (Beta > 0.7):      45.962%
    ##
    ## -- ch probes --
    ## No. Probes:                     3091
    ## No. Probes with NA:             581 (18.797%)
    ## Mean Betas:                     0.08337478
    ## Median Betas:                   0.07236062
    ## % Unmethylated (Beta < 0.3):    99.801%
    ## % Methylated (Beta > 0.7):      0.000%
    ##
    ## -- rs probes --
    ## No. Probes:                     65
    ## No. Probes with NA:             0 (0.000%)
    ## Mean Betas:                     0.5123969
    ## Median Betas:                   0.5321602
    ## % Unmethylated (Beta < 0.3):    30.769%
    ## % Methylated (Beta > 0.7):      30.769%
    ##
    ## =======================
    ## =      Inferences     =
    ## =======================
    ## Sex:                            MALE
    ## Ethnicity:                      WHITE
    ## Age:                            57.4364
    ## Bisulfite Conversion (GCT):     1.10858

The `sesameQC` object can be coerced into data.frame and linked using the following code

    qc10 <- do.call(rbind, lapply(ssets, function(x)
        as.data.frame(sesameQC(x))))
    qc10$sample_name <- names(ssets)

    qc10[,c('mean_beta_cg','frac_meth_cg','frac_unmeth_cg','sex','age')]

    ##          mean_beta_cg frac_meth_cg frac_unmeth_cg  sex      age
    ## WB_105      0.5098332     45.96235       40.02335 MALE 57.43640
    ## WB_218      0.5096105     47.06082       41.04828 MALE 37.95232
    ## WB_261      0.5157003     47.57101       40.73839 MALE 21.93794
    ## PBMC_105    0.5180599     46.11539       38.77543 MALE 55.49295
    ## PBMC_218    0.5203244     47.78827       39.92857 MALE 38.26392
    ## PBMC_261    0.5260290     48.75566       39.61534 MALE 18.05454
    ## Gran_105    0.4973380     46.31641       43.06686 MALE 60.07713
    ## Gran_218    0.4976376     46.47685       43.20653 MALE 39.87609
    ## Gran_261    0.5061924     47.36164       42.82382 MALE 23.75574
    ## CD4+_105    0.5203645     47.25563       39.20422 MALE 48.97698

# Background

The background level is given by `mean_oob_grn` and `mean_oob_red`

    library(ggplot2)

    ## Registered S3 methods overwritten by 'ggplot2':
    ##   method         from
    ##   [.quosures     rlang
    ##   c.quosures     rlang
    ##   print.quosures rlang

    ggplot(qc10,
        aes(x = mean_oob_grn, y= mean_oob_red, label = sample_name)) +
        geom_point() + geom_text(hjust = -0.1, vjust = 0.1) +
        geom_abline(intercept = 0, slope = 1, linetype = 'dotted') +
        xlab('Green Background') + ylab('Red Background') +
        xlim(c(500,1200)) + ylim(c(500,1200))

# Mean Intensity

The mean {M,U} intensity can be reached by `mean_intensity`. Similarly, the mean M+U intensity can be reached by `mean_intensity_total`. Low intensities are symptomatic of low input or poor hybridization.

    library(wheatmap)
    p1 <- ggplot(qc10) +
        geom_bar(aes(sample_name, mean_intensity), stat='identity') +
        xlab('Sample Name') + ylab('Mean Intensity') +
        ylim(0,18000) +
        theme(axis.text.x = element_text(angle=90, vjust=0.5, hjust=1))
    p2 <- ggplot(qc10) +
        geom_bar(aes(sample_name, mean_intensity_total), stat='identity') +
        xlab('Sample Name') + ylab('Mean M+U Intensity') +
        ylim(0,18000) +
        theme(axis.text.x = element_text(angle=90, vjust=0.5, hjust=1))
    WGG(p1) + WGG(p2, RightOf())

# Fraction of color channel switch

The fraction of color channel switch can be found in `InfI_switch_G2R` and `InfI_switch_R2G`. These numbers are symptomatic of how Infinium I probes are affected by SNP-induced color channel switching.

    ggplot(qc10) +
        geom_point(aes(InfI_switch_G2R, InfI_switch_R2G))

# Fraction of NA

The fraction of NAs are signs of masking due to variety of reasons including failed detection, high background, putative low quality probes etc. This number can be reached in `frac_na_cg` and `num_na_cg` (the cg stands for CpG probes, so we also have `num_na_ch` and `num_na_rs`)

    p1 <- ggplot(qc10) +
        geom_bar(aes(sample_name, num_na_cg), stat='identity') +
        xlab('Sample Name') + ylab('Number of NAs') +
        theme(axis.text.x = element_text(angle=90, vjust=0.5, hjust=1))
    p2 <- ggplot(qc10) +
        geom_bar(aes(sample_name, frac_na_cg), stat='identity') +
        xlab('Sample Name') + ylab('Fraction of NAs (%)') +
        theme(axis.text.x = element_text(angle=90, vjust=0.5, hjust=1))
    WGG(p1) + WGG(p2, RightOf())