R Package MADSEQ

Yu Kong

2018-10-30

The MADSEQ package is a group of hierarchical Bayesian model for the detection and quantification of potential mosaic aneuploidy in sample using massive parallel sequencing data.

The MADSEQ package takes two pieces of information for the detection and quantification of mosaic aneuploidy:

  1. The distribution of the alternative allele frequencies (AAF) of the sites that are genotyped as heterozygous.
  2. The average sequencing coverage for regions. (for targeted sequencing it’s each targeted region; for whole genome sequencing, bin the genome into bins. And because sequencing coverage are usually biased by GC content, normalization is necessary, the normalization function is provided in the package).

MADSEQ works on the whole chromosome resolution. It applies all of the five models (normal, monosomy, mitotic trisomy, meiotic trisomy, loss of heterozygosity) to fit the distribution of the AAF of all the heterozygous sites, and fit the distribution of the coverage from that chromosome. After fitting the same data using all models, it does model comparison using BIC (Bayesian Information Criteria) to select the best model. The model selected tells us whether the chromosome is aneuploid or not, and also the type of mosaic aneuploidy. Then, from the posterior distribution of the best model, we could get the estimation of the fraction of aneuploidy cells.

Data You Need

Note: Currently our package only supports one bam and one vcf file per sample. If you have more than one sample, please prepare multiple bam and vcf files for each of them.

Example Data

There are two sets of example data come with the package:

  1. A bam file named aneuploidy.bam and a vcf file named aneuploidy.vcf.gz for a sample containing trisomy in chromosome 18.
  2. A bam file named normal.bam and a vcf file names normal.vcf.gz for a normal sample.
  3. A bed file named target.bed containing the targeted regions.

To access the data use

Note:This is just a set of example data, only contains a very little region of the genome.

Use The Package

We will start with the bam file, vcf file and bed file in the example data to show you each step for the analysis.

Prepare coverage information

Started with bam file and bed file, you can use prepareCoverageGC function to get the coverage and GC information for each targeted regions.

## load the package
suppressMessages(library("MADSEQ"))

## get path to the location of example data
aneuploidy_bam = system.file("extdata","aneuploidy.bam",package="MADSEQ")
normal_bam = system.file("extdata","normal.bam",package="MADSEQ")
target = system.file("extdata","target.bed",package="MADSEQ")

## Note: for your own data, just specify the path to the location 
## of your file using character.

## prepare coverage and GC content for each targeted region
# aneuploidy sample
aneuploidy_cov = prepareCoverageGC(target_bed=target, 
                                    bam=aneuploidy_bam, 
                                    "hg19")
#> 309 non-repeats regions from 24 chromosomes in the bed file.
#> calculating depth from BAM...
#> 
#> Attaching package: 'BiocGenerics'
#> The following objects are masked from 'package:parallel':
#> 
#>     clusterApply, clusterApplyLB, clusterCall, clusterEvalQ,
#>     clusterExport, clusterMap, parApply, parCapply, parLapply,
#>     parLapplyLB, parRapply, parSapply, parSapplyLB
#> The following objects are masked from 'package:stats':
#> 
#>     IQR, mad, sd, var, xtabs
#> The following objects are masked from 'package:base':
#> 
#>     Filter, Find, Map, Position, Reduce, anyDuplicated, append,
#>     as.data.frame, basename, cbind, colMeans, colSums, colnames,
#>     dirname, do.call, duplicated, eval, evalq, get, grep, grepl,
#>     intersect, is.unsorted, lapply, lengths, mapply, match, mget,
#>     order, paste, pmax, pmax.int, pmin, pmin.int, rank, rbind,
#>     rowMeans, rowSums, rownames, sapply, setdiff, sort, table,
#>     tapply, union, unique, unsplit, which, which.max, which.min
#> 
#> Attaching package: 'S4Vectors'
#> The following object is masked from 'package:base':
#> 
#>     expand.grid
#> 
#> Attaching package: 'Biostrings'
#> The following object is masked from 'package:base':
#> 
#>     strsplit
#> calculating GC content...

# normal sample
normal_cov = prepareCoverageGC(target_bed=target, 
                                bam=normal_bam, 
                                "hg19")
#> 309 non-repeats regions from 24 chromosomes in the bed file.
#> calculating depth from BAM...
#> calculating GC content...

## view the first two rows of prepared coverage data (A GRanges Object)
aneuploidy_cov[1:2]
#> GRanges object with 2 ranges and 2 metadata columns:
#>       seqnames            ranges strand |     depth                GC
#>          <Rle>         <IRanges>  <Rle> | <numeric>         <numeric>
#>   [1]    chr14 21538016-21538117      * |        21 0.647058823529412
#>   [2]    chr14 22377854-22377955      * |        24 0.362745098039216
#>   -------
#>   seqinfo: 24 sequences from an unspecified genome; no seqlengths

normal_cov[1:2]
#> GRanges object with 2 ranges and 2 metadata columns:
#>       seqnames            ranges strand |     depth                GC
#>          <Rle>         <IRanges>  <Rle> | <numeric>         <numeric>
#>   [1]    chr14 21538016-21538117      * |         4 0.647058823529412
#>   [2]    chr14 22377854-22377955      * |        32 0.362745098039216
#>   -------
#>   seqinfo: 24 sequences from an unspecified genome; no seqlengths

Normalize coverage information

The normalization function takes prepared coverage GRanges object from prepareCoverageGC function, normalize the coverage and calculate the expected coverage for the sample. If there is only one sample, the function will correct the coverage by GC content, and take the average coverage for the whole genome as expected coverage. If there are more than one samples given, the function will first quantile normalize coverage across samples, then correct the coverage by GC for each sample. If control sample is not specified, the expected coverage is the median coverage across all samples, if a normal control is specified, the average coverage for control sample is taken as expected coverage for further analysis.

Note:

  1. If you have more than one samples, please make sure they are targeted by the same regions.
  2. If you have more than one samples, please separate female samples and male samples, which means normalize them separately; because sex chromosomes can bias the normalization.
  3. One sample is good to go, however multiple samples will help you get a better normalization.

If you only have one sample

If you choose to write the output to file (recommended)

If you don’t want to write output to file

If you have more than one samples, without a normal control

If you choose to write the output to file (recommended)

If you don’t want to write output to file


## a GRangesList object will be produced by the function
length(normed_without_control)
#> [1] 2
names(normed_without_control)
#> [1] "aneuploidy_cov" "normal_cov"

## subsetting
normed_without_control[["aneuploidy_cov"]]
#> GRanges object with 154 ranges and 5 metadata columns:
#>         seqnames            ranges strand |     depth quantiled_depth
#>            <Rle>         <IRanges>  <Rle> | <numeric>       <numeric>
#>     [1]    chr14 21538016-21538117      * |        21              16
#>     [2]    chr14 22377854-22377955      * |        24              19
#>     [3]    chr14 26201540-26201641      * |        45              36
#>     [4]    chr14 26475535-26475636      * |         8               6
#>     [5]    chr14 29080232-29080333      * |         6               5
#>     ...      ...               ...    ... .       ...             ...
#>   [150]    chr18 58507475-58507576      * |        57              46
#>   [151]    chr18 59650666-59650767      * |        37              30
#>   [152]    chr18 60388772-60388873      * |        40              32
#>   [153]    chr18 63107118-63107219      * |        93              76
#>   [154]    chr18 69135718-69135819      * |        14              11
#>                        GC normed_depth        ref_depth
#>                 <numeric>    <numeric>        <numeric>
#>     [1] 0.647058823529412           26 35.5733117483811
#>     [2] 0.362745098039216           19 35.5733117483811
#>     [3] 0.362745098039216           36 35.5733117483811
#>     [4] 0.264705882352941           20 35.5733117483811
#>     [5] 0.323529411764706           18 35.5733117483811
#>     ...               ...          ...              ...
#>   [150] 0.441176470588235           43 31.7051282051282
#>   [151] 0.372549019607843           28 31.7051282051282
#>   [152] 0.245098039215686           41 31.7051282051282
#>   [153] 0.450980392156863           74 31.7051282051282
#>   [154] 0.323529411764706           24 31.7051282051282
#>   -------
#>   seqinfo: 24 sequences from an unspecified genome; no seqlengths
normed_without_control[["normal_cov"]]
#> GRanges object with 153 ranges and 5 metadata columns:
#>         seqnames            ranges strand |     depth quantiled_depth
#>            <Rle>         <IRanges>  <Rle> | <numeric>       <numeric>
#>     [1]    chr14 21538016-21538117      * |         4               5
#>     [2]    chr14 22377854-22377955      * |        32              42
#>     [3]    chr14 26201540-26201641      * |        24              31
#>     [4]    chr14 26475535-26475636      * |        12              16
#>     [5]    chr14 29080232-29080333      * |        11              14
#>     ...      ...               ...    ... .       ...             ...
#>   [149]    chr18 58507475-58507576      * |        18              24
#>   [150]    chr18 59650666-59650767      * |        23              30
#>   [151]    chr18 60388772-60388873      * |        22              28
#>   [152]    chr18 63107118-63107219      * |        45              61
#>   [153]    chr18 69135718-69135819      * |        13              18
#>                        GC normed_depth        ref_depth
#>                 <numeric>    <numeric>        <numeric>
#>     [1] 0.647058823529412           16 35.5733117483811
#>     [2] 0.362745098039216           40 35.5733117483811
#>     [3] 0.362745098039216           29 35.5733117483811
#>     [4] 0.264705882352941           30 35.5733117483811
#>     [5] 0.323529411764706           22 35.5733117483811
#>     ...               ...          ...              ...
#>   [149] 0.441176470588235           19 31.7051282051282
#>   [150] 0.372549019607843           26 31.7051282051282
#>   [151] 0.245098039215686           40 31.7051282051282
#>   [152] 0.450980392156863           56 31.7051282051282
#>   [153] 0.323529411764706           26 31.7051282051282
#>   -------
#>   seqinfo: 24 sequences from an unspecified genome; no seqlengths

Prepare heterozygous sites

Having vcf.gz file and target bed file ready, use prepareHetero function to process the heterozygous sites.

## specify the path to vcf.gz file
aneuploidy_vcf = system.file("extdata","aneuploidy.vcf.gz",package="MADSEQ")

## target bed file specified before

## If you choose to write the output to file (recommended)
prepareHetero(aneuploidy_vcf, target, genome="hg19", 
              writeToFile=TRUE, destination=".",plot = FALSE)
#> Warning in scan(file = file, what = what, sep = sep, quote = quote, dec =
#> dec, : EOF within quoted string

#> Warning in scan(file = file, what = what, sep = sep, quote = quote, dec =
#> dec, : EOF within quoted string
#> reading vcf file
#> Scanning file to determine attributes.
#> File attributes:
#>   meta lines: 116
#>   header_line: 117
#>   variant count: 387
#>   column count: 10
#> 
Meta line 116 read in.
#> All meta lines processed.
#> gt matrix initialized.
#> Character matrix gt created.
#>   Character matrix gt rows: 387
#>   Character matrix gt cols: 10
#>   skip: 0
#>   nrows: 387
#>   row_num: 0
#> 
Processed variant: 387
#> All variants processed
#> processing vcf file
#> filtering vcf file
#> Warning in scan(file = file, what = what, sep = sep, quote = quote, dec =
#> dec, : EOF within quoted string

#> Warning in scan(file = file, what = what, sep = sep, quote = quote, dec =
#> dec, : EOF within quoted string
#> Warning in .Seqinfo.mergexy(x, y): The 2 combined objects have no sequence levels in common. (Use
#>   suppressWarnings() to suppress this warning.)

#> Warning in .Seqinfo.mergexy(x, y): The 2 combined objects have no sequence levels in common. (Use
#>   suppressWarnings() to suppress this warning.)
#> filtered heterozygous sites for sample aneuploidy.vcf.gz is written to ./aneuploidy.vcf.gz_filtered_heterozygous.txt

## If you don't want to write output to file
aneuploidy_hetero = prepareHetero(aneuploidy_vcf, target,
                                  genome="hg19", writeToFile=FALSE,plot = FALSE)
#> Warning in scan(file = file, what = what, sep = sep, quote = quote, dec =
#> dec, : EOF within quoted string
#> Warning in scan(file = file, what = what, sep = sep, quote = quote, dec =
#> dec, : EOF within quoted string
#> reading vcf file
#> Scanning file to determine attributes.
#> File attributes:
#>   meta lines: 116
#>   header_line: 117
#>   variant count: 387
#>   column count: 10
#> 
Meta line 116 read in.
#> All meta lines processed.
#> gt matrix initialized.
#> Character matrix gt created.
#>   Character matrix gt rows: 387
#>   Character matrix gt cols: 10
#>   skip: 0
#>   nrows: 387
#>   row_num: 0
#> 
Processed variant: 387
#> All variants processed
#> processing vcf file
#> filtering vcf file
#> Warning in scan(file = file, what = what, sep = sep, quote = quote, dec =
#> dec, : EOF within quoted string

#> Warning in scan(file = file, what = what, sep = sep, quote = quote, dec =
#> dec, : EOF within quoted string
#> Warning in .Seqinfo.mergexy(x, y): The 2 combined objects have no sequence levels in common. (Use
#>   suppressWarnings() to suppress this warning.)

#> Warning in .Seqinfo.mergexy(x, y): The 2 combined objects have no sequence levels in common. (Use
#>   suppressWarnings() to suppress this warning.)

Run MadSeq model to detect potential mosaic aneuploidy

The function runMadSeq will run the models and select the best model for the input data.

Note:

  1. Among three normalized coverage sets listed above (one sample, two samples without control, two samples with a control), we will use the two samplew with a control case for the following analysis.
  2. Because these models are based on MCMC sampling, the running process can be very long. Running different chromosomes parallel in background or High Performance Computer Cluster is highly recommended.

If the processed data have been written into files

## specify the path to processed files
aneuploidy_hetero = "./aneuploidy.vcf.gz_filtered_heterozygous.txt"
aneuploidy_normed_cov = "./aneuploidy_cov_normed_depth.txt"

## run the model
aneuploidy_chr18 = runMadSeq(hetero=aneuploidy_hetero, 
                             coverage=aneuploidy_normed_cov, 
                             target_chr="chr18",
                             nChain=1, nStep=1000, thinSteps=1,
                             adapt=100,burnin=200)
#> total number of heterozygous site: 28
#> total number of coverage 39
#> module mix loaded
#> 1. running normal model
#> Compiling model graph
#>    Resolving undeclared variables
#>    Allocating nodes
#> Graph information:
#>    Observed stochastic nodes: 67
#>    Unobserved stochastic nodes: 30
#>    Total graph size: 201
#> 
#> Initializing model
#> 2. running monosomy model
#> Compiling model graph
#>    Resolving undeclared variables
#>    Allocating nodes
#> Graph information:
#>    Observed stochastic nodes: 69
#>    Unobserved stochastic nodes: 29
#>    Total graph size: 248
#> 
#> Initializing model
#> 3. running mitotic trisomy model
#> Compiling model graph
#>    Resolving undeclared variables
#>    Allocating nodes
#> Graph information:
#>    Observed stochastic nodes: 69
#>    Unobserved stochastic nodes: 29
#>    Total graph size: 248
#> 
#> Initializing model
#> 4. running meiotic trisomy model
#> Compiling model graph
#>    Resolving undeclared variables
#>    Allocating nodes
#> Graph information:
#>    Observed stochastic nodes: 69
#>    Unobserved stochastic nodes: 30
#>    Total graph size: 278
#> 
#> Initializing model
#> 5. running loss of heterozygosity model
#> Compiling model graph
#>    Resolving undeclared variables
#>    Allocating nodes
#> Graph information:
#>    Observed stochastic nodes: 67
#>    Unobserved stochastic nodes: 33
#>    Total graph size: 1770
#> 
#> Initializing model
#> models done, comparing models

#> Order and delta BIC of the preference of models
#>          BIC_normal BIC_mitotic_trisomy BIC_meiotic_trisomy 
#>            0.000000            9.907088           16.302748 
#>             BIC_LOH        BIC_monosomy 
#>           16.655443           20.587893 
#> model selected: normal

## An MadSeq object will be returned
aneuploidy_chr18
#> MadSeq object with the posterior distribution from normal model
#>         kappa    m_cov        mu     p_cov    r_cov
#> [1,] 3.827819 28.28205 0.4740872 0.1346740 4.401645
#> [2,] 3.289960 28.28205 0.4740872 0.2212278 8.034156
#> [3,] 3.343753 28.28205 0.4740872 0.2039310 7.245083
#> [4,] 6.223678 28.28205 0.4740872 0.1655468 5.610865
#> [5,] 3.851463 28.28205 0.4740872 0.1074200 3.403680
#> [6,] 3.579869 28.28205 0.4740872 0.1863579 6.477768
#> ------
#>          BIC_normal BIC_mitotic_trisomy BIC_meiotic_trisomy 
#>            0.000000            9.907088           16.302748 
#>             BIC_LOH        BIC_monosomy 
#>           16.655443           20.587893

Note: In order to save time, we only run 1 chain with a much less steps compared with default settings. For real cases, the default settings are recommended.

If the processed data have NOT been written into files

The MadSeq object from the runMadSeq function contains:

  • The posterior distribution of the selected model
  • The delta BIC value between selected model and other models

Note: The value of delta BIC suggests the strength of the confidence of the selected model against other models. In our model, you can set a threshold to get high confidence result, usually it’s 20 in our testing cases. We summarize it as follows

deltaBIC Evidence against higher BIC
[0,10] Probably noisy data
(10,20] Could be positive
>20 High confidence

Visualize the selected model

There are a group of plot functions to plot the output MadSeq object from the runMadSeq.

Description of All Parameters

parameters description
f Fraction of mosaic aneuploidy
m The midpoint of the alternative allele frequency (AAF) for all heterozygous sites
mu[1] Mean AAF of mixture 1: the AAFs of this mixture shifted from midpoint to some higher values
mu[2] Mean AAF of mixture 2: the AAFs of this mixture shifted from midpoint to some lower values
mu[3] (LOH model) Mean AAF of mixture 3: In LOH model, mu[3] indicates normal sites without loss of heterozygosity
mu[3] (meiotic trisomy model) Mean AAF of mixture 3: In meiotic model, the AAFs of this mixture shifted from 0 to some higher value
mu[4] Mean AAF of mixture 4: the AAFs of this mixture shifted from 1 to some lower value (only in meiotic model)
kappa Indicate variance of the AAF mixtures: larger kappa means smaller variance
p[1] Weight of mixture 1: indicate the proportion of heterozygous sites in the mixture 1
p[2] Weight of mixture 2: indicate the proportion of heterozygous sites in the mixture 2
p[3] Weight of mixture 3: indicate the proportion of heterozygous sites in the mixture 3 (only in LOH and meiotic model)
p[4] Weight of mixture 4: indicate the proportion of heterozygous sites in the mixture 4 (only in meiotic model)
p[5] Weight of outlier component: the AAF of 1% sites might not well behaved, so these sites are treated as noise.
m_cov Mean coverage of all the sites from the chromosome, estimated from a negative binomial distribution
p_cov Prob of the negative binomial distribution for the coverage
r_cov Another parameter (r) for the negative binomial disbribution of the coverage, small r means large variance