---
title: "Package Dependencies"
author: "Brian S. Yandell"
date: "2/10/2017"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```

## DOQTL2 packages

The following packages were split off from Yandell's R/doqtl2 package and are now repositories at <http://github.com/byandell>:

- DOread: read derived data from RDS files
- CCSanger: CCcolors, SQLite, genes, features, snpinfo
- qtl2pattern: SDP patterns, summary methods
- qtl2ggplot: ggplot2 version of R/qtl2 plot routines
- qtl2shiny: shiny app

## Multiple Trait Strategy

Here is a strategy to look at multiple traits in a small interval:

- use analyses table (or other approach) to identify region of interest
    + say 1-5Mb window
- identify set of traits in region
- estimate allele effects at peak for each trait
    + useful plots?
- find best SNP(s) and pattern(s)
    + identify SDP and pattern for each
    + useful plots?
- do same for full effects
    + 36 diplotypes
    + 3-level SNPs
    
Some challenges

- don't have useful thresholds yet
- would like to think about inference across related patterns
- multiple peaks in small region
- pleiotropy vs close linkage

How to incorporate this into shiny tool?

- quick computation: limit size of region
- do we have to run genome scan?
    + or just consider flanking markers for each peak?
    
## Imports

All packages are now set up to formally import functions or data from other packages. Thus, no packages are actually loaded, which should speed up processes. All packages import from some <http://tidyverse.org> package.

### CCSanger imports

- ggplot2, dplyr, readr

### DOread imports

- dplyr
- feather:  `read_feather`
- broman:   `winsorize`
- qlt2scan: `interp_map`

### qtl2pattern imports

- dplyr, tidyr, stringr
- qtl2scan: `scan1`, `scan1coef`
- CCSanger: `get_snpinfo`, `get_svs8`, `convert_bp`, `get_gene_exon_snp`, `sdp_to_logical`, `sdp_to_pattern`

### qtl2ggplot imports

- ggplot2, dplyr, tidyr
- RColorBrewer
- grid, graphics
- Rcpp: sourceCpp
- qtl2scan: `scan1`, `scan1coef`
- qtl2pattern: 
- CCSanger: `sdp_to_pattern`, `CCcolors`, `convert_bp`

### qtl2shiny imports

- ggplot2, dplyr, tidyr
- shiny, shinydashboard
- gdata, grid, GGally, plotly
- qtl2scan: `scan1`
- qtl2ggplot
- qtl2pattern: `scan_pattern`
- CCSanger: `get_gene_exon_snp`, `sdp_to_pattern`, `get_mgi_features`, `CCcolors`
- CausalMST: `comediate1_test`, `mediate1_test`

### CausalMST imports

- dplyr, purrr, stringr, ggplot2, 
- mnormt: `pmnorm`
- corpcor: `is.positive.definite`, `make.positive.definite`
- qtl2scan: `get_common_ids`, `fit1`, `decomp_kinship`

## Package functions and data

### DOread functions

These routines assume data are in RDS files in folder `datapath`.
The read data routines use `analyses_tbl` created for data entry workflow.
Typically one would first do steps indicated in 
`system.file(file.path("doqtl2", "setup.R"), package='qtl2shiny')`.
These include

```
covar <- readRDS(file.path(datapath, "covar.rds"))
analyses_tbl <- readRDS(file.path(datapath, "analyses.rds")
```

- `read_pheno_tbl`: read phenotype table from RDS
- `get_pheno`: get phenotypes from phenotype table
- `get_covar`: get covariates from covariate matrix

The read genotype probability routines assume `qtl2geno::calc_genoprob` has been run.
For quicker access, the chromosomes are stored in separate RDS file.

- `read_probs`: read 8-allele genotype probabilites
- `read_probs36`: read 36-diplotype genotype probabilites

### CCSanger functions and data

Here are the main components, including CC colors and access to SQL databases with gene, SNP and SVS features.

- `CCcolors`, `CCoricolors`: CC founder colors (from `qtl2plot`)
- `get_mgi_features`: Pull MGI gene tbl from SQLite database
- `get_snpinfo`: Get SNP and InDel information in window around peak
- `get_snpprobs`:	Get SNP genotype probabilities in window around peak
- `get_svs8`:	Get SVS information in window around peak

The following pull features. Some have `subset`, `summary` or `plot` methods.

- `get_feature_snp`:	Match features with SNPs
- `get_gene_exon`:	Get exons for set of genes
- `get_gene_exon_snp`:	Get exons for set of genes
- `get_gene_snp`:	Match genes with SNPs

The rest are mostly utilities used in various places. 

- `sdp_to_logical`:	Convert sdp to pattern
- `sdp_to_pattern`:	Convert sdp to pattern
- `check_interval`:	Check chr, start_bp, end_bp for validity
- `convert_bp`:	Convert to bp if in Mb

### qtl2pattern functions

- `listof_scan1coef`:	List of scan1coef objects
- `merge_feature`:	Merge SNP lod peaks with SNP feature information
- `scan_pattern`:	Genome scan by pattern set
- `snpscan_pattern`:	Plot coefficients by pattern set
- `top_snps_all`:	Top SNPs for all phenotypes scanned

This routine will be folded into `plot_snpasso` use, but has a few aspects not yet captured.

- `topsnp_pattern`:	Fine Mapping scans by allele pattern

Following are utility routines

- `genoprob_to_patternprob`:	Collapse genoprob according to pattern
- `pattern_diplos`:	Extract pattern of diplotypes
- `pattern_haplos`:	Extract pattern of diplotypes
- `snpprob_collapse`:	Collapse genoprob according to pattern

### qtl2ggplot functions

Plot methods for `scan1`, `scan1coef`, `genes` and `listof_scan1coef` objects.

The following are utility routines:

- `ggplot_scan1`: create ggplot2 object for `scan1` and `scan1coef`
- `ggplot_genes`: create ggplot2 object for `genes`
- `color_patterns_get`:	Set up col, pattern and group for plotting
- `color_patterns_pheno`:	Set up col, pattern and group for plotting
- `color_patterns_set`:	Set up colors for patterns or points

### qtl2shiny functions

- `doqtl2_app`: run shiny app

## Function migration

The following tables show functions from R/doqtl2 used by shiny app. Some of them will stay, but some of them will be simplified, for instance to fit with R/qtl2ggplot, etc.

file              | function
----------------- | --------
shinyGeneExon.R   | plot_gene_exon
shinyGeneRegion.R | get_mgi_features
shinyPattern.R    | scan_pattern
shinyProbs.R      | get_snpprobs read_probs read_probs36
shinySNPAllele.R  | get_gene_exon_snp get_top_snps_tbl snpprob_collapse
shinyScan1Plot.R  | listof_scan1coefCC
shinySetup.R      | get_pheno
shinyTopFeature.R | merge_feature

function          | file | use
----------------- | -------- | --------
get_gene_exon_snp | gene_exon.R | call get_mgi_features
get_mgi_features  | get_mgi_features.R | extract from SQLite
get_pheno         | get_traits.R | get selected phenotypes
get_snpprobs      | snpinfo.R   | snpprobs for SNPs, InDels, SVs
get_top_snps_tbl  | top_snps_tbl.R | get top SNP info based on LMMs
listof_scan1coefCC | listof_scan1coefCC.R | create list of scan1coefCC objects
merge_feature     | merge_feature.R | merge SNP LOD and other information
plot_gene_exon    | gene_exon.R | plot genes and exons
read_probs        | read_probs.R | read genoprob object for RDS
read_probs36      | read_probs.R | read genoprob object for RDS
scan_pattern      | scan_pattern.R | genome scan by pattern set
snpprob_collapse  | genoprob_to_patternprob.R | collapse from alleles to SNPs

Want `scan_pattern` to look like `plot_snpasso` with `pattern="all"`.

Need to work on geno and exon stuff to meld with `plot_genes`.
