Skip to content

Commit

Permalink
Dev (#39)
Browse files Browse the repository at this point in the history
* minor updates

* update some of the hscn columns

* changing Maj/Min to A/B

* change actions

* actions update
  • Loading branch information
marcjwilliams1 authored Jan 21, 2023
1 parent 90660e0 commit 5b920ca
Show file tree
Hide file tree
Showing 12 changed files with 434 additions and 399 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ jobs:
GITHUB_PAT: ${{ secrets.PAT }}
R_KEEP_PKG_SOURCE: yes
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3

- uses: r-lib/actions/setup-r@v2
with:
use-public-rspm: true

- if: runner.os == 'macOS'
uses: Homebrew/actions/setup-homebrew@master
run: brew install --cask xquartz

- uses: r-lib/actions/setup-pandoc@v2
Expand Down
6 changes: 4 additions & 2 deletions .github/workflows/test-coverage.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ on:
push:
branches:
- master
- main
pull_request:
branches:
- master
- main

name: test-coverage

Expand All @@ -16,9 +18,9 @@ jobs:
steps:
- uses: actions/checkout@v2

- uses: r-lib/actions/setup-r@master
- uses: r-lib/actions/setup-r@v2

- uses: r-lib/actions/setup-pandoc@master
- uses: r-lib/actions/setup-pandoc@v2
- name: Install xquartz
run: brew install --cask xquartz
- name: Query dependencies
Expand Down
145 changes: 74 additions & 71 deletions R/callASCN.R
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ switch_alleles <- function(cn) {
phase_cn <- cn %>%
as.data.table() %>%
.[, switch := data.table::fifelse(
Min > Maj,
B > A,
"switch",
"stick"
)] %>%
Expand Down Expand Up @@ -182,17 +182,20 @@ switch_alleles <- function(cn) {
#'
#' @return allele specific copy number object which includes dataframe similar to input with additional columns which include
#'
#' * `Maj` (Major allele copy number)
#' * `Min` (Minor allele copy number)
#' * `state_AS_phased` (phased state of the form Maj|Min )
#' * `state_AS` (mirrored state of the form Maj|Min)
#' * `LOH` (is bin LOH or not)
#' * `state_phase` (state describing which is the dominant allele and whether it is LOH or not)
#' * `state_BAF` (binned discretized BAF value calculated as Min / (Maj + Min))
#'
#' * `A` A allele copy number
#' * `B` B allele copy number
#' * `state_AS_phased` A|B
#' * `state_min` Minor allele copy number
#' * `LOH` =LOH if bin is LOH, NO otherwise
#' * `state_phase` Discretized haplotype specific states
#' * `phase` Whether the A allele or B allele is dominant
#' * `alleleA` Counts for the A allele
#' * `alleleB` Counts for the B allele
#' * `totalcounts` Total number of counts
#' * `BAF` B-allele frequency (alleleB / totalcounts)
#'
#' @details
#' In the allele specific copy number inference Maj is always > Min and state_AS_phased == state_AS
#' In the allele specific copy number inference A is always > B and state_AS_phased == state_AS
#'
#' @export
callAlleleSpecificCN <- function(CNbins,
Expand Down Expand Up @@ -333,47 +336,47 @@ callAlleleSpecificCN <- function(CNbins,
}

alleleCN <- alleleCN %>%
.[, Maj1 := state - state_min] %>%
.[, Min1 := state_min] %>%
# catch edge cases where Min > Maj:
.[, Min := fifelse(Min1 > Maj1, Maj1, Min1)] %>%
.[, Maj := fifelse(Min1 > Maj1, Min1, Maj1)] %>%
.[, Maj1 := NULL] %>%
.[, Min1 := NULL] %>%
.[, A1 := state - state_min] %>%
.[, B1 := state_min] %>%
# catch edge cases where B > A:
.[, B := fifelse(B1 > A1, A1, B1)] %>%
.[, A := fifelse(B1 > A1, B1, A1)] %>%
.[, A1 := NULL] %>%
.[, B1 := NULL] %>%
# catch edge cases of 0|1 and 1|0 states:
.[, state_min := fifelse(state_min < 0, 0, state_min)] %>%
.[, Maj := state - state_min] %>%
.[, Min := state_min] %>%
.[, Min := fifelse(Min < 0, 0, Min)] %>%
.[, Maj := fifelse(Maj < 0, 0, Maj)] %>%
.[, Min := fifelse(Min > state, state, Min)] %>%
.[, Maj := fifelse(Maj > state, state, Maj)] %>%
.[, state_AS_phased := paste0(Maj, "|", Min)] %>%
.[, state_AS := paste0(pmax(state - Min, Min), "|", pmin(state - Min, Min))] %>%
.[, state_min := pmin(Maj, Min)] %>%
.[, A := state - state_min] %>%
.[, B := state_min] %>%
.[, B := fifelse(B < 0, 0, B)] %>%
.[, A := fifelse(A < 0, 0, A)] %>%
.[, B := fifelse(B > state, state, B)] %>%
.[, A := fifelse(A > state, state, A)] %>%
.[, state_AS_phased := paste0(A, "|", B)] %>%
.[, state_AS := paste0(pmax(state - B, B), "|", pmin(state - B, B))] %>%
.[, state_min := pmin(A, B)] %>%
.[, state_AS := ifelse(state > 4, state, state_AS)] %>%
.[, LOH := ifelse(state_min == 0, "LOH", "NO")] %>%
.[, phase := c("Balanced", "A", "B")[1 +
1 * ((Min < Maj)) +
2 * ((Min > Maj))]] %>%
1 * ((B < A)) +
2 * ((B > A))]] %>%
.[, state_phase := c("Balanced", "A-Gained", "B-Gained", "A-Hom", "B-Hom")[1 +
1 * ((Min < Maj) & (Min != 0)) +
2 * ((Min > Maj) & (Maj != 0)) +
3 * ((Min < Maj) & (Min == 0)) +
4 * ((Min > Maj) & (Maj == 0))]] %>%
1 * ((B < A) & (B != 0)) +
2 * ((B > A) & (A != 0)) +
3 * ((B < A) & (B == 0)) +
4 * ((B > A) & (A == 0))]] %>%
.[order(cell_id, chr, start)] %>%
.[, state_BAF := round((Min / state) / 0.1) * 0.1] %>%
.[, state_BAF := round((B / state) / 0.1) * 0.1] %>%
.[, state_BAF := fifelse(is.nan(state_BAF), 0.5, state_BAF)]

# mirror BAF
alleleCN <- alleleCN[, switch := data.table::fifelse(
Min > Maj,
B > A,
"switch",
"stick"
)] %>%
.[, alleleB := totalcounts - alleleA] %>%
.[, distA := abs(BAF - (Min / state))] %>%
.[, distB := abs(BAF - (Maj / state))] %>%
.[, distA := abs(BAF - (B / state))] %>%
.[, distB := abs(BAF - (A / state))] %>%
.[, switch := fifelse((distB < distA) & (BAF != 0.5), "switch", "stick")] %>%
.[, alleleA := data.table::fifelse(switch == "switch", alleleB, alleleA)] %>%
.[, alleleB := totalcounts - alleleA] %>%
Expand All @@ -385,7 +388,7 @@ callAlleleSpecificCN <- function(CNbins,

if (fillmissing){
alleleCN <- dplyr::left_join(CNbins, alleleCN)
alleleCN <- tidyr::fill(alleleCN, c("state_min", "Maj", "Min", "state_phase", "state_AS",
alleleCN <- tidyr::fill(alleleCN, c("state_min", "A", "B", "state_phase", "state_AS",
"state_AS_phased", "LOH", "state_BAF", "phase"), .direction = "downup")
}

Expand Down Expand Up @@ -416,16 +419,16 @@ callAlleleSpecificCN <- function(CNbins,
#'
#' @return allele specific copy number object which includes dataframe similar to input with additional columns which include
#'
#' * `Maj` (Major allele copy number)
#' * `Min` (Minor allele copy number)
#' * `state_AS_phased` (phased state of the form Maj|Min )
#' * `state_AS` (mirrored state of the form Maj|Min)
#' * `A` (Major allele copy number)
#' * `B` (Minor allele copy number)
#' * `state_AS_phased` (phased state of the form A|B )
#' * `state_AS` (mirrored state of the form A|B)
#' * `LOH` (is bin LOH or not)
#' * `state_phase` (state describing which is the dominant allele and whether it is LOH or not)
#' * `state_BAF` (binned discretized BAF value calculated as Min / (Maj + Min))
#' * `state_BAF` (binned discretized BAF value calculated as B / (A + B))
#'
#' @details
#' In the allele specific copy number inference Maj is always > Min and state_AS_phased == state_AS
#' In the allele specific copy number inference A is always > B and state_AS_phased == state_AS
#'
#' @export
callAlleleSpecificCNfromHSCN <- function(hscn,
Expand Down Expand Up @@ -502,49 +505,49 @@ callAlleleSpecificCNfromHSCN <- function(hscn,
}

alleleCN <- alleleCN %>%
.[, Maj1 := state - state_min] %>%
.[, Min1 := state_min] %>%
# catch edge cases where Min > Maj:
.[, Min := fifelse(Min1 > Maj1, Maj1, Min1)] %>%
.[, Maj := fifelse(Min1 > Maj1, Min1, Maj1)] %>%
.[, Maj1 := NULL] %>%
.[, Min1 := NULL] %>%
.[, A1 := state - state_min] %>%
.[, B1 := state_min] %>%
# catch edge cases where B > A:
.[, B := fifelse(B1 > A1, A1, B1)] %>%
.[, A := fifelse(B1 > A1, B1, A1)] %>%
.[, A1 := NULL] %>%
.[, B1 := NULL] %>%
# catch edge cases of 0|1 and 1|0 states:
.[, state_min := Min] %>%
.[, state_min := B] %>%
.[, state_min := fifelse(state_min < 0, 0, state_min)] %>%
.[, Maj := state - state_min] %>%
.[, Min := state_min] %>%
.[, Min := fifelse(Min < 0, 0, Min)] %>%
.[, Maj := fifelse(Maj < 0, 0, Maj)] %>%
.[, Min := fifelse(Min > state, state, Min)] %>%
.[, Maj := fifelse(Maj > state, state, Maj)] %>%
.[, state_AS_phased := paste0(Maj, "|", Min)] %>%
.[, state_AS := paste0(pmax(state - Min, Min), "|", pmin(state - Min, Min))] %>%
.[, state_min := pmin(Maj, Min)] %>%
.[, A := state - state_min] %>%
.[, B := state_min] %>%
.[, B := fifelse(B < 0, 0, B)] %>%
.[, A := fifelse(A < 0, 0, A)] %>%
.[, B := fifelse(B > state, state, B)] %>%
.[, A := fifelse(A > state, state, A)] %>%
.[, state_AS_phased := paste0(A, "|", B)] %>%
.[, state_AS := paste0(pmax(state - B, B), "|", pmin(state - B, B))] %>%
.[, state_min := pmin(A, B)] %>%
.[, state_AS := ifelse(state > 4, state, state_AS)] %>%
.[, LOH := ifelse(state_min == 0, "LOH", "NO")] %>%
.[, phase := c("Balanced", "A", "B")[1 +
1 * ((Min < Maj)) +
2 * ((Min > Maj))]] %>%
1 * ((B < A)) +
2 * ((B > A))]] %>%
.[, state_phase := c("Balanced", "A-Gained", "B-Gained", "A-Hom", "B-Hom")[1 +
1 * ((Min < Maj) & (Min != 0)) +
2 * ((Min > Maj) & (Maj != 0)) +
3 * ((Min < Maj) & (Min == 0)) +
4 * ((Min > Maj) & (Maj == 0))]] %>%
1 * ((B < A) & (B != 0)) +
2 * ((B > A) & (A != 0)) +
3 * ((B < A) & (B == 0)) +
4 * ((B > A) & (A == 0))]] %>%
.[order(cell_id, chr, start)] %>%
.[, state_BAF := round((Min / state) / 0.1) * 0.1] %>%
.[, state_BAF := round((B / state) / 0.1) * 0.1] %>%
.[, state_BAF := fifelse(is.nan(state_BAF), 0.5, state_BAF)]

# mirror BAF
alleleCN <- alleleCN[, switch := data.table::fifelse(
Min > Maj,
B > A,
"switch",
"stick"
)] %>%
.[, alleleB := totalcounts - alleleA] %>%
.[, origBAF := BAF] %>%
.[, distA := abs(BAF - (Min / state))] %>%
.[, distB := abs(BAF - (Maj / state))] %>%
.[, distA := abs(BAF - (B / state))] %>%
.[, distB := abs(BAF - (A / state))] %>%
.[, switch := fifelse((distB < distA) & (BAF != 0.5), "switch", "stick")] %>%
.[, alleleA := data.table::fifelse(switch == "switch", alleleB, alleleA)] %>%
.[, alleleB := totalcounts - alleleA] %>%
Expand All @@ -555,7 +558,7 @@ callAlleleSpecificCNfromHSCN <- function(hscn,

if (fillmissing){
alleleCN <- dplyr::left_join(CNbins, alleleCN)
alleleCN <- tidyr::fill(alleleCN, c("state_min", "Maj", "Min", "state_phase", "state_AS",
alleleCN <- tidyr::fill(alleleCN, c("state_min", "A", "B", "state_phase", "state_AS",
"state_AS_phased", "LOH", "state_BAF", "phase"), .direction = "downup")
}

Expand Down
Loading

0 comments on commit 5b920ca

Please sign in to comment.