-
Notifications
You must be signed in to change notification settings - Fork 2
Output_cn_plots_restyling #66
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
adfa920
add module for plotting wisecondorx cnv
SaraPotente a0fe366
add R script for plotting wisecondorx cnv
SaraPotente 1647996
add plot_wisecondorx_cnv to liquid_biopsy wf
SaraPotente 5ad65ac
update config
SaraPotente f21ee72
update comments
SaraPotente d5fcede
update nextflow_schema
SaraPotente 932308a
update nextflow.config
SaraPotente a25d289
fix params description
SaraPotente 443dd33
update modules.config
SaraPotente d367313
fix schema value
SaraPotente cbba41d
update parameter name and description
SaraPotente 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| #!/usr/bin/env Rscript | ||
|
|
||
| suppressPackageStartupMessages({ | ||
| library(argparser) | ||
| library(readr) | ||
| library(dplyr) | ||
| library(ggplot2) | ||
| }) | ||
|
|
||
| ## arguments | ||
|
|
||
| p <- arg_parser("Plot WisecondorX output (bin-level log2 ratio + segments)") | ||
| p <- add_argument(p, "--id", help = "Sample ID") | ||
| p <- add_argument(p, "--seg_file", help = "Segmented file with calls from WisecondorX", nargs = Inf) | ||
| p <- add_argument(p, "--binfile", help = "Bin-level file from WisecondorX") | ||
| p <- add_argument(p, "--outdir", help = "Output directory", default = ".") | ||
| p <- add_argument(p, "--ratio_limit", help = "Log2ratio limit of additional WisecondorX plots (application-generated plots are unaffected)", default = 1) | ||
| argv <- parse_args(p) | ||
|
|
||
| sample_id <- argv$id | ||
| bin_path <- argv$binfile | ||
| output_dir <- argv$outdir | ||
| ratio_limit <- argv$ratio_limit | ||
|
|
||
| # take the right seg_file pattern | ||
| seg_candidates <- argv$seg_file | ||
| if (length(seg_candidates) > 1) { | ||
| seg_candidates <- seg_candidates[!grepl("_gistic\\.seg$", seg_candidates)] | ||
| } | ||
| seg_path <- seg_candidates[1] | ||
|
|
||
| dir.create(output_dir, showWarnings = FALSE, recursive = TRUE) | ||
|
|
||
|
|
||
| # Fix column names | ||
| read_and_normalize <- function(path) { | ||
| df <- read_tsv(path, show_col_types = FALSE) %>% | ||
| rename_with(tolower) | ||
|
|
||
| if ("chrom" %in% names(df)) df <- rename(df, chr = chrom) | ||
| if ("seg.mean.adj" %in% names(df)) df <- rename(df, ratio = `seg.mean.adj`) | ||
|
|
||
| df | ||
| } | ||
|
|
||
| # Remap WisecondorX gain/loss/neut calls to readable labels for the plot | ||
|
|
||
| classify_segments <- function(df) { | ||
| call_map <- c(gain = "GAIN", loss = "LOSS", neut = "NEUTRAL") | ||
| df %>% mutate(call = recode(tolower(call), !!!call_map, .default = "NEUTRAL")) | ||
| } | ||
|
|
||
| # Plot style formatting, genomic coordinates | ||
| compute_genomic_coords <- function(bins, segs) { | ||
| chr_order <- c(as.character(1:22), "X", "Y") | ||
| present_chr <- intersect(chr_order, unique(c(bins$chr, segs$chr))) | ||
|
|
||
| chr_lengths <- bins %>% | ||
| filter(chr %in% present_chr) %>% | ||
| group_by(chr) %>% | ||
| summarise(len = max(end), .groups = "drop") %>% | ||
| mutate(chr = factor(chr, levels = present_chr)) %>% | ||
| arrange(chr) %>% | ||
| mutate(offset = lag(cumsum(len), default = 0)) | ||
|
|
||
| add_offset <- function(df) { | ||
| df %>% | ||
| filter(chr %in% present_chr) %>% | ||
| mutate(chr = factor(chr, levels = present_chr)) %>% | ||
| left_join(chr_lengths %>% select(chr, offset), by = "chr") %>% | ||
| mutate(start_g = start + offset, end_g = end + offset) | ||
| } | ||
|
|
||
| list( | ||
| bins = add_offset(bins) %>% mutate(pos_g = (start_g + end_g) / 2), | ||
| segs = add_offset(segs), | ||
| chr_lengths = chr_lengths %>% mutate(xmax = offset + len) | ||
| ) | ||
| } | ||
|
|
||
| # Plot wisecondorx plot | ||
| plot_wisecondorx_cnv <- function(bins, segs, chr_lengths, sample_id, ratio_limit, output_dir) { | ||
| chr_ticks <- chr_lengths %>% mutate(mid = offset + len / 2) | ||
| chr_boundaries <- chr_lengths$xmax[-nrow(chr_lengths)] | ||
|
|
||
| color_mapping <- c( | ||
| "NEUTRAL" = "#377eb8", | ||
| "GAIN" = "#e41a1c", | ||
| "LOSS" = "#4daf4a" | ||
| ) | ||
|
|
||
| p <- ggplot() + | ||
| geom_vline(xintercept = chr_boundaries, color = "grey85", linewidth = 0.4, linetype = "dotted") + | ||
| geom_hline(yintercept = 0, color = "grey75", linewidth = 0.4) + | ||
| geom_point( | ||
| data = bins, | ||
| aes(x = pos_g, y = pmin(pmax(ratio, -ratio_limit), ratio_limit)), | ||
| color = "grey75", size = 0.25, alpha = 0.35 | ||
| ) + | ||
| geom_segment( | ||
| data = segs, | ||
| aes(x = start_g, xend = end_g, y = ratio, yend = ratio, color = call), | ||
| linewidth = 1.6, lineend = "round" | ||
| ) + | ||
| scale_color_manual( | ||
| values = color_mapping, | ||
| name = "Copy Number Call", | ||
| guide = guide_legend(override.aes = list(linewidth = 4)) | ||
| ) + | ||
| scale_x_continuous(breaks = chr_ticks$mid, labels = chr_ticks$chr, expand = c(0.01, 0.01)) + | ||
| scale_y_continuous(limits = c(-ratio_limit, ratio_limit)) + | ||
| labs( | ||
| x = "Chromosome", y = expression(log[2](ratio)), | ||
| title = paste0("Copy Number Profile", if (!is.null(sample_id)) paste0(" - ", sample_id) else "") | ||
| ) + | ||
| theme_minimal(base_size = 13) + | ||
| theme( | ||
| panel.grid.minor = element_blank(), | ||
| panel.grid.major.x = element_blank(), | ||
| panel.grid.major.y = element_line(color = "grey93", linewidth = 0.3), | ||
| panel.background = element_rect(fill = "white", color = NA), | ||
| plot.background = element_rect(fill = "white", color = NA), | ||
| axis.text = element_text(color = "grey30"), | ||
| axis.title = element_text(color = "grey20"), | ||
| plot.title = element_text(face = "bold"), | ||
| plot.subtitle = element_text(color = "grey40", size = 10), | ||
| legend.position = "bottom", | ||
| legend.title = element_text(face = "bold") | ||
| ) | ||
|
|
||
| out_png <- file.path(output_dir, paste0(sample_id, ".copy_number.png")) | ||
| out_svg <- file.path(output_dir, paste0(sample_id, ".copy_number.svg")) | ||
| ggsave(out_png, plot = p, width = 14, height = 6, dpi = 300) | ||
| ggsave(out_svg, plot = p, width = 14, height = 6) | ||
| message("Plot (PNG) saved to: ", out_png) | ||
| message("Plot (SVG) saved to: ", out_svg) | ||
| } | ||
|
|
||
| # ---- main ------------------------------------------------------------- | ||
|
|
||
| message("Processing sample: ", sample_id) | ||
|
|
||
| bins <- read_and_normalize(bin_path) %>% | ||
| filter(!is.na(ratio)) | ||
| segs <- read_and_normalize(seg_path) %>% | ||
| classify_segments() | ||
|
|
||
| message("Data loaded - Bins: ", nrow(bins), " Segments: ", nrow(segs)) | ||
|
|
||
| coords <- compute_genomic_coords(bins, segs) | ||
|
|
||
| plot_wisecondorx_cnv(coords$bins, | ||
| coords$segs, | ||
| coords$chr_lengths, | ||
| sample_id, | ||
| ratio_limit, | ||
| output_dir) | ||
|
|
||
| message("Plot generation completed!") |
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,43 @@ | ||
| process PLOT_WISECONDORX_CNV { | ||
| tag "Plotting WisecondorX results for $meta.id" | ||
| label 'process_low' | ||
| container "community.wave.seqera.io/library/procps-ng_r-argparser_r-dplyr_r-ggplot2_pruned:10da72fa04bcba1a" | ||
|
|
||
| input: | ||
| tuple val(meta), path(seg_file) | ||
| tuple val(meta2), path(bins) | ||
|
|
||
| output: | ||
| tuple val(meta), path("*.copy_number.png"), emit: plot_png | ||
| tuple val(meta), path("*.copy_number.svg"), emit: plot_svg | ||
| path "versions.yml" , emit: versions | ||
|
|
||
| when: | ||
| task.ext.when == null || task.ext.when | ||
|
|
||
| script: | ||
| def args = task.ext.args ?: '' | ||
| def prefix = task.ext.prefix ?: "${meta.id}" | ||
| def VERSION = '0.1' | ||
| """ | ||
| plot_wisecondorx_cnv.R \\ | ||
| --id ${prefix} \\ | ||
| --seg_file ${seg_file} \\ | ||
| --binfile ${bins} \\ | ||
| --outdir . \\ | ||
| ${args} | ||
|
|
||
| cat <<-END_VERSIONS > versions.yml | ||
| "${task.process}": | ||
| plot_wisecondorx_cnv: $VERSION | ||
| END_VERSIONS | ||
| """ | ||
|
|
||
| stub: | ||
| def prefix = task.ext.prefix ?: "${meta.id}" | ||
| """ | ||
| touch ${prefix}.copy_number.png | ||
| touch ${prefix}.copy_number.svg | ||
| touch versions.yml | ||
| """ | ||
| } |
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
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.
Uh oh!
There was an error while loading. Please reload this page.