-
Notifications
You must be signed in to change notification settings - Fork 24
Fix merge external #118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AtaJadidAhari
wants to merge
5
commits into
master
Choose a base branch
from
fix_merge_external
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Fix merge external #118
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| #' Merge two FraserDataSet objects with different splice sites and junctions | ||
| #' | ||
| #' This function merges two FraserDataSet objects that have different | ||
| #' splice sites and junctions but no overlapping samples. It combines | ||
| #' only the rawCountsJ and rawCountsSS assays and recalculates startID | ||
| #' and endID for the merged rowRanges. | ||
| #' | ||
| #' @param fds1 First FraserDataSet object. | ||
| #' @param fds2 Second FraserDataSet object. | ||
| #' @param join_type Type of join for rowRanges: "outer" (default) or "inner". | ||
| #' @param fds_name Name for the merged FraserDataSet. | ||
| #' @param workingDir Directory where to store HDF5 and RDS files. Defaults to | ||
| #' \code{FRASER_output} in the current working directory. | ||
| #' @return A merged FraserDataSet object containing combined samples and assays. | ||
| #' @examples | ||
| #' # merged_fds <- mergeFDS(fds1, fds2, join_type="outer") | ||
| #' @export | ||
| #' @importFrom GenomeInfoDb seqlevelsInUse | ||
| #' @importFrom SummarizedExperiment rowRanges rowData assays | ||
| mergeFDS <- function(fds1, fds2, join_type = c("outer", "inner"), fds_name="merged_fds", workingDir="FRASER_output") { | ||
| join_type <- match.arg(join_type) | ||
| # create a True/False flag for join types | ||
| all_flag <- if (join_type == "outer") TRUE else FALSE | ||
|
|
||
| create_junction_counts <- function(fds){ | ||
| chr_levels <- seqlevelsInUse(fds) | ||
|
|
||
| row_ranges <- as.data.table(rowRanges(fds)) | ||
|
|
||
| junction_counts <- as.data.table(assays(fds)$rawCountsJ) | ||
| junction_counts <- cbind(junction_counts, row_ranges[, c("seqnames", "start", "end", "width", "strand", "startID", "endID")]) | ||
| # Enforce order | ||
| junction_counts[, seqnames := factor(as.character(seqnames), levels = chr_levels)] | ||
| setorder(junction_counts, seqnames, start, end) | ||
| return (junction_counts) | ||
| } | ||
|
|
||
| create_split_counts <- function(fds){ | ||
|
|
||
| chr_levels <- seqlevelsInUse(fds) | ||
|
|
||
| row_ranges <- as.data.table(rowRanges(fds)) | ||
|
|
||
| splice_sites <- rowData(nonSplicedReads(fds)) | ||
| splice_site_counts <- as.data.table(assays(fds)$rawCountsSS) | ||
| splice_site_counts <- as.data.table(cbind(splice_sites, splice_site_counts)) | ||
|
|
||
| splice_site_lookup <- unique(rbind( | ||
| row_ranges[, .(spliceSiteID = startID, seqnames, position = start - 1)], # donor positions | ||
| row_ranges[, .(spliceSiteID = endID, seqnames, position = end)] # acceptor positions | ||
| )) | ||
|
|
||
| splice_site_lookup[, start := position] | ||
| splice_site_lookup[, end := start + 1] | ||
| splice_site_lookup[, width := 2] | ||
| splice_site_lookup[, position := NULL] | ||
|
|
||
| splice_site_counts <- merge(splice_site_counts, splice_site_lookup, by = "spliceSiteID", all.x = TRUE) | ||
| splice_site_counts[, seqnames := factor(as.character(seqnames), levels = chr_levels)] | ||
| setorder(splice_site_counts, seqnames, start) | ||
|
|
||
| return (splice_site_counts) | ||
|
|
||
| } | ||
|
|
||
| junction_counts_1 <- create_junction_counts(fds1) | ||
| junction_counts_2 <- create_junction_counts(fds2) | ||
|
|
||
| # Join the junction counts based on join_type | ||
| junction_counts_merged <- merge(junction_counts_1, junction_counts_2, by=c("start", "end", "seqnames", "width", "strand"), all=all_flag) | ||
|
|
||
| # If outer join, missing numeric counts should become 0. | ||
| # For inner join we do NOT coerce NAs to 0 (there shouldn't be rows | ||
| # coming from only one side when all=FALSE), so skip replacement. | ||
| if (all_flag) { | ||
| junction_counts_merged[is.na(junction_counts_merged)] <- 0 | ||
| } | ||
| positions <- unique(rbind( | ||
AtaJadidAhari marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| junction_counts_merged[, .(seqnames, pos = start)], | ||
| junction_counts_merged[, .(seqnames, pos = end)] | ||
| )) | ||
| setorder(positions, seqnames, pos) | ||
| positions[, spliceSiteID := .I] | ||
|
|
||
| junction_counts_merged <- merge(junction_counts_merged, positions, by.x = c("seqnames", "start"), by.y = c("seqnames", "pos"), all.x = TRUE) | ||
| setnames(junction_counts_merged, "spliceSiteID", "startID") | ||
|
|
||
| junction_counts_merged <- merge(junction_counts_merged, positions, by.x = c("seqnames", "end"), by.y = c("seqnames", "pos"), all.x = TRUE) | ||
| setnames(junction_counts_merged, "spliceSiteID", "endID") | ||
|
|
||
| junction_counts_merged[ ,c("startID.x","startID.y", "endID.x", "endID.y") := NULL] | ||
|
|
||
| splice_site_counts_1 <- create_split_counts(fds1) | ||
| splice_site_counts_2 <- create_split_counts(fds2) | ||
|
|
||
| splice_site_counts_merged <- merge(splice_site_counts_1, splice_site_counts_2, by=c("start", "end", "seqnames", "width", "type"), all=all_flag) | ||
| splice_site_counts_merged[ ,c("spliceSiteID.x","spliceSiteID.y") := NULL] | ||
|
|
||
| # When outer, fill NA -> 0 (samples absent in one side) | ||
| if (all_flag) { | ||
| splice_site_counts_merged[is.na(splice_site_counts_merged)] <- 0 | ||
| } | ||
|
|
||
| splice_site_counts_merged <- merge(splice_site_counts_merged, positions, by.x = c("seqnames", "start"), by.y = c("seqnames", "pos"), all.x = TRUE) | ||
| positions[, pos := pos - 1] # to merge donor values | ||
| splice_site_counts_merged <- merge(splice_site_counts_merged, positions, by.x = c("seqnames", "start"), by.y = c("seqnames", "pos"), all.x = TRUE) | ||
|
|
||
| # Combine spliceSiteID.x and spliceSiteID.y into a single column. | ||
| # For each row, use spliceSiteID.x if it’s not NA; otherwise take spliceSiteID.y. | ||
| # This keeps whichever ID exists after merging. | ||
| splice_site_counts_merged[, spliceSiteID := fcoalesce(spliceSiteID.x, spliceSiteID.y)] | ||
AtaJadidAhari marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| # Set the rest to NULL | ||
| splice_site_counts_merged[ ,c("spliceSiteID.x","spliceSiteID.y") := NULL] | ||
|
|
||
| # when using inner join, ensure we only keep splice sites that are in positions | ||
| if (!all_flag) { | ||
| splice_site_counts_merged <- splice_site_counts_merged[spliceSiteID %in% positions$spliceSiteID] | ||
| junction_counts_merged <- junction_counts_merged[startID %in% positions$spliceSiteID & endID %in% positions$spliceSiteID] | ||
| } | ||
|
|
||
| # Subset both colData | ||
| # --- Combine colData --- | ||
| # Get intersection of column names | ||
| shared_cols <- intersect(colnames(colData(fds1)), colnames(colData(fds2))) | ||
|
|
||
| cd1 <- colData(fds1)[, shared_cols, drop = FALSE] | ||
| cd2 <- colData(fds2)[, shared_cols, drop = FALSE] | ||
|
|
||
| merged_colData <- rbind(cd1, cd2) | ||
|
|
||
| # --- Create merged FraserDataSet --- | ||
| merged_fds <- FraserDataSet( | ||
| colData = as.data.table(merged_colData), | ||
| junctions = junction_counts_merged, | ||
| spliceSites = splice_site_counts_merged, | ||
| name = fds_name, | ||
| workingDir = workingDir | ||
| ) | ||
|
|
||
| merged_fds <- calculatePSIValues(merged_fds, types=fitMetrics(merged_fds)) | ||
|
|
||
| return(merged_fds) | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| context("Test merge fds function") | ||
|
|
||
| test_that("mergeFDS", { | ||
| fds <- createTestFraserDataSet() | ||
|
|
||
| fds_1 <- fds[, 1] | ||
| fds_2 <- fds[, c(2,3)] | ||
|
|
||
| merged_fds <- mergeFDS(fds_1, fds_2) | ||
|
|
||
| # Check number of samples | ||
| expect_equal(ncol(merged_fds), ncol(fds)) | ||
vyepez88 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| # Check number of junctions (features) | ||
| expect_equal(nrow(merged_fds), nrow(fds)) | ||
|
|
||
| # Check number of splice sites | ||
| expect_equal(nrow(assay(merged_fds, "rawCountsSS")), nrow(assay(fds, "rawCountsSS"))) | ||
| }) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does it get saved already? if not, maybe add how to save it