annotate facets_analysis.R @ 4:3f62267c4be7 draft

planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
author artbio
date Sun, 05 Oct 2025 18:42:30 +0000
parents d1914f4d9daf
children 625038b7d764
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
1 #!/usr/bin/env Rscript
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
2
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
3 # Description:
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
4 # This script serves as the backend for the Galaxy FACETS Analysis tool.
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
5 # It takes a SNP pileup file as input and performs allele-specific copy
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
6 # number analysis using the R package 'facets'.
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
7 # ==============================================================================
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
8
3
d1914f4d9daf planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 64ac36125f04497dd51028f307e059fca9ec0503
artbio
parents: 2
diff changeset
9 # --- Load Libraries ---
2
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
10 suppressPackageStartupMessages(library(argparse))
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
11 suppressPackageStartupMessages(library(facets))
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
12
3
d1914f4d9daf planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 64ac36125f04497dd51028f307e059fca9ec0503
artbio
parents: 2
diff changeset
13 # --- Source the external plot_facets_enhanced function ---
d1914f4d9daf planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 64ac36125f04497dd51028f307e059fca9ec0503
artbio
parents: 2
diff changeset
14 # This finds the path of the currently running script and sources
d1914f4d9daf planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 64ac36125f04497dd51028f307e059fca9ec0503
artbio
parents: 2
diff changeset
15 # the R function file relative to it.
d1914f4d9daf planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 64ac36125f04497dd51028f307e059fca9ec0503
artbio
parents: 2
diff changeset
16 initial_opts <- commandArgs(trailingOnly = FALSE)
d1914f4d9daf planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 64ac36125f04497dd51028f307e059fca9ec0503
artbio
parents: 2
diff changeset
17 script_path <- dirname(sub("--file=", "", initial_opts[grep("--file=", initial_opts)]))
d1914f4d9daf planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 64ac36125f04497dd51028f307e059fca9ec0503
artbio
parents: 2
diff changeset
18 source(file.path(script_path, "plot_facets_enhanced-v22.R"))
d1914f4d9daf planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 64ac36125f04497dd51028f307e059fca9ec0503
artbio
parents: 2
diff changeset
19
d1914f4d9daf planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 64ac36125f04497dd51028f307e059fca9ec0503
artbio
parents: 2
diff changeset
20 # --- Define and Parse Arguments ---
2
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
21
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
22 # Create the parser
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
23 parser <- ArgumentParser(description = "Run FACETS algorithm on a SNP pileup file.")
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
24
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
25 # Define arguments
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
26 parser$add_argument("--pileup",
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
27 type = "character", required = TRUE,
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
28 help = "Path to the gzipped pileup CSV file."
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
29 )
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
30 parser$add_argument("--sample_id",
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
31 type = "character", required = TRUE,
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
32 help = "Sample ID used for plot titles and metadata."
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
33 )
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
34
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
35 parser$add_argument("--output_seg",
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
36 type = "character", required = TRUE,
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
37 help = "Path for the output segmentation file (TSV)."
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
38 )
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
39 parser$add_argument("--output_summary",
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
40 type = "character", required = TRUE,
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
41 help = "Path for the output summary file (TSV)."
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
42 )
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
43 parser$add_argument("--output_plots",
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
44 type = "character", required = TRUE,
3
d1914f4d9daf planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 64ac36125f04497dd51028f307e059fca9ec0503
artbio
parents: 2
diff changeset
45 help = "Path for the main output plots file (PNG)."
2
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
46 )
3
d1914f4d9daf planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 64ac36125f04497dd51028f307e059fca9ec0503
artbio
parents: 2
diff changeset
47 parser$add_argument("--output_spider",
d1914f4d9daf planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 64ac36125f04497dd51028f307e059fca9ec0503
artbio
parents: 2
diff changeset
48 type = "character", required = TRUE,
d1914f4d9daf planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 64ac36125f04497dd51028f307e059fca9ec0503
artbio
parents: 2
diff changeset
49 help = "Path for the diagnostic spider plot file (PNG)."
d1914f4d9daf planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 64ac36125f04497dd51028f307e059fca9ec0503
artbio
parents: 2
diff changeset
50 )
4
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
51 parser$add_argument("--output_vcf",
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
52 type = "character", required = TRUE,
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
53 help = "Path for the output VCF file with CNV calls."
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
54 )
2
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
55 parser$add_argument("--cval",
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
56 type = "double", default = 150,
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
57 help = "Critical value for segmentation."
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
58 )
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
59 parser$add_argument("--min_nhet",
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
60 type = "integer", default = 25,
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
61 help = "Minimum number of heterozygous SNPs per segment."
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
62 )
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
63 parser$add_argument("--snp_nbhd",
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
64 type = "integer", default = 300,
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
65 help = "SNP neighborhood size for pre-processing. Crucial for sparse VCFs."
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
66 )
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
67 parser$add_argument("--gbuild",
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
68 type = "character", default = "hg38",
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
69 choices = c("hg19", "hg38", "hg18", "mm9", "mm10"),
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
70 help = "Genome build used for alignment."
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
71 )
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
72
4
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
73 #' Classify CNV segments based on TCN/LCN
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
74 classify_cnv <- function(cncf_df) {
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
75 cncf_df$sv_type <- NA_character_
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
76 cncf_df$sv_type[cncf_df$tcn.em == 2 & (cncf_df$lcn.em == 1 | is.na(cncf_df$lcn.em))] <- "NEUTR"
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
77 cncf_df$sv_type[is.na(cncf_df$sv_type) & cncf_df$tcn.em > 2] <- "DUP"
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
78 cncf_df$sv_type[is.na(cncf_df$sv_type) & cncf_df$tcn.em < 2 & !is.na(cncf_df$lcn.em) & cncf_df$lcn.em > 0] <- "HEMIZYG_DEL"
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
79 cncf_df$sv_type[is.na(cncf_df$sv_type) & cncf_df$tcn.em < 2 & !is.na(cncf_df$lcn.em) & cncf_df$lcn.em == 0] <- "HOMOZYG_DEL"
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
80 cncf_df$sv_type[is.na(cncf_df$sv_type) & cncf_df$tcn.em == 2 & !is.na(cncf_df$lcn.em) & cncf_df$lcn.em == 0] <- "CN_LOH"
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
81
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
82 # Remplacer les NA restants (si tcn.em < 2 mais lcn.em est NA) par un type général
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
83 cncf_df$sv_type[is.na(cncf_df$sv_type) & cncf_df$tcn.em < 2] <- "DEL"
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
84
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
85 return(cncf_df)
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
86 }
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
87
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
88 #' Create a VCF header (explicit version)
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
89 create_vcf_header <- function(sample_id, purity, ploidy) {
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
90 header <- c(
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
91 "##fileformat=VCFv4.2",
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
92 paste0("##fileDate=", format(Sys.Date(), "%Y%m%d")),
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
93 paste0("##source=FACETS_v", packageVersion("facets")),
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
94 "##INFO=<ID=END,Number=1,Type=Integer,Description=\"End position of the variant\">",
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
95 "##INFO=<ID=SVTYPE,Number=1,Type=String,Description=\"Type of structural variant (standard VCF tags: DEL, DUP, CNV)\">",
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
96 "##INFO=<ID=SVLEN,Number=1,Type=Integer,Description=\"Length of the SV\">",
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
97 # --- MODIFICATION ---
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
98 "##INFO=<ID=EVENT,Number=1,Type=String,Description=\"FACETS event classification. Possible values: DUP, HEMIZYG_DEL, HOMOZYG_DEL, CN_LOH\">",
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
99 # --- FIN MODIFICATION ---
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
100 "##INFO=<ID=TCN,Number=1,Type=Integer,Description=\"Total Copy Number (EM fit)\">",
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
101 "##INFO=<ID=LCN,Number=1,Type=Integer,Description=\"Lesser Copy Number (EM fit)\">",
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
102 "##INFO=<ID=NUM_MARK,Number=1,Type=Integer,Description=\"Number of SNPs in the segment\">",
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
103 "##INFO=<ID=NHET,Number=1,Type=Integer,Description=\"Number of heterozygous SNPs in the segment\">",
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
104 paste0("##FACETS_PURITY=", round(purity, 4)),
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
105 paste0("##FACETS_PLOIDY=", round(ploidy, 4)),
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
106 "#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO"
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
107 )
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
108 return(header)
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
109 }
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
110
3
d1914f4d9daf planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 64ac36125f04497dd51028f307e059fca9ec0503
artbio
parents: 2
diff changeset
111 # --- Main Analysis Function ---
2
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
112 main <- function(args) {
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
113 # Set seed for reproducibility
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
114 set.seed(1965)
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
115
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
116 # --- Read the data with readSnpMatrix() from facets ---
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
117 rcmat <- readSnpMatrix(args$pileup)
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
118
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
119 # --- Pre-process sample ---
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
120 xx <- preProcSample(rcmat, gbuild = args$gbuild, snp.nbhd = args$snp_nbhd)
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
121
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
122 # --- Process sample (segmentation) ---
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
123 oo <- procSample(xx, cval = args$cval, min.nhet = args$min_nhet)
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
124
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
125 # --- Estimate ploidy/purity ---
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
126 fit <- emcncf(oo)
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
127
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
128 # Write the main segmentation file
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
129 cncf_output <- fit$cncf
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
130 if (nrow(cncf_output) > 0) {
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
131 cncf_output$purity <- fit$purity
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
132 cncf_output$ploidy <- fit$ploidy
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
133 # Reorder columns to have purity/ploidy first for clarity
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
134 cncf_output <- cncf_output[, c("purity", "ploidy", setdiff(names(cncf_output), c("purity", "ploidy")))]
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
135 }
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
136 write.table(cncf_output, file = args$output_seg, sep = "\t", quote = FALSE, row.names = FALSE)
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
137
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
138 # Write a key-value summary file
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
139 # A NULL value is replaced by NA to preserve vector length.
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
140 summary_df <- data.frame(
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
141 Parameter = c(
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
142 "sample_id", "purity", "ploidy", "dipLogR", "loglik",
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
143 "cval_param", "min_nhet_param", "snp_nbhd_param", "gbuild_param"
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
144 ),
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
145 Value = c(
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
146 args$sample_id,
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
147 ifelse(is.null(fit$purity), NA, fit$purity),
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
148 ifelse(is.null(fit$ploidy), NA, fit$ploidy),
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
149 ifelse(is.null(fit$dipLogR), NA, fit$dipLogR),
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
150 ifelse(is.null(fit$loglik), NA, fit$loglik),
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
151 args$cval,
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
152 args$min_nhet,
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
153 args$snp_nbhd,
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
154 args$gbuild
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
155 )
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
156 )
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
157 write.table(summary_df, file = args$output_summary, sep = "\t", quote = FALSE, row.names = FALSE)
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
158
3
d1914f4d9daf planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 64ac36125f04497dd51028f307e059fca9ec0503
artbio
parents: 2
diff changeset
159 # Generate the plots PNG
d1914f4d9daf planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 64ac36125f04497dd51028f307e059fca9ec0503
artbio
parents: 2
diff changeset
160 png(file = args$output_plots, width = 12, height = 8, units = "in", res = 300)
2
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
161 plotSample(x = oo, emfit = fit, sname = args$sample_id)
3
d1914f4d9daf planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 64ac36125f04497dd51028f307e059fca9ec0503
artbio
parents: 2
diff changeset
162 plot_facets_enhanced(oo, emfit = fit, plot.type = "em", sname = args$sample_id)
d1914f4d9daf planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 64ac36125f04497dd51028f307e059fca9ec0503
artbio
parents: 2
diff changeset
163 dev.off()
d1914f4d9daf planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 64ac36125f04497dd51028f307e059fca9ec0503
artbio
parents: 2
diff changeset
164 png(file = args$output_spider, width = 8, height = 8, units = "in", res = 300)
d1914f4d9daf planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 64ac36125f04497dd51028f307e059fca9ec0503
artbio
parents: 2
diff changeset
165 logRlogORspider(oo$out, oo$dipLogR)
2
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
166 dev.off()
4
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
167
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
168 # --- Generate VCF file ---
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
169
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
170 # Classify segments and define standard SVTYPEs + detailed EVENTs
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
171 cncf_for_vcf <- fit$cncf
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
172 if (nrow(cncf_for_vcf) > 0) {
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
173 cncf_for_vcf$svtype <- NA_character_
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
174 cncf_for_vcf$event <- NA_character_
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
175
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
176 # Duplications
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
177 cncf_for_vcf[cncf_for_vcf$tcn.em > 2, c("svtype", "event")] <- c("DUP", "DUP")
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
178
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
179 # Deletions
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
180 cncf_for_vcf[cncf_for_vcf$tcn.em < 2, c("svtype")] <- "DEL"
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
181 cncf_for_vcf[cncf_for_vcf$tcn.em == 1, c("event")] <- "HEMIZYG_DEL"
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
182 cncf_for_vcf[cncf_for_vcf$tcn.em == 0, c("event")] <- "HOMOZYG_DEL"
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
183
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
184 # Copy-Neutral LOH
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
185 cncf_for_vcf[cncf_for_vcf$tcn.em == 2 & !is.na(cncf_for_vcf$lcn.em) & cncf_for_vcf$lcn.em == 0, c("svtype", "event")] <- c("CNV", "CN_LOH")
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
186
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
187 # Filtrer les segments normaux (ceux où 'svtype' est encore NA)
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
188 cnv_calls <- cncf_for_vcf[!is.na(cncf_for_vcf$svtype), ]
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
189 } else {
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
190 cnv_calls <- data.frame()
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
191 }
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
192
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
193
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
194 if (nrow(cnv_calls) > 0) {
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
195 vcf_header <- create_vcf_header(args$sample_id, fit$purity, fit$ploidy)
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
196
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
197 vcf_body <- apply(cnv_calls, 1, function(seg) {
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
198 alt_allele <- paste0("<", seg["svtype"], ">")
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
199
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
200 info <- paste0(
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
201 "END=", seg["end"],
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
202 ";SVTYPE=", seg["svtype"],
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
203 ";SVLEN=", as.integer(seg["end"]) - as.integer(seg["start"]),
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
204 ";TCN=", seg["tcn.em"],
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
205 ";LCN=", ifelse(is.na(seg["lcn.em"]), ".", seg["lcn.em"]),
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
206 ";EVENT=", seg["event"],
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
207 ";NUM_MARK=", seg["num.mark"],
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
208 ";NHET=", seg["nhet"]
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
209 )
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
210
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
211 paste(seg["chrom"], seg["start"], ".", "N", alt_allele, ".", "PASS", info, sep = "\t")
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
212 })
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
213
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
214 writeLines(c(vcf_header, vcf_body), con = args$output_vcf)
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
215 } else {
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
216 vcf_header <- create_vcf_header(args$sample_id, fit$purity, fit$ploidy)
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
217 writeLines(vcf_header, con = args$output_vcf)
3f62267c4be7 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit e47e0ed100904318ef4aae661b763e049c358edf
artbio
parents: 3
diff changeset
218 }
2
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
219 }
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
220
3
d1914f4d9daf planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 64ac36125f04497dd51028f307e059fca9ec0503
artbio
parents: 2
diff changeset
221 # --- Execution Block ---
2
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
222 if (!interactive()) {
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
223 args <- parser$parse_args()
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
224 main(args)
66a56502199d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/facets commit 0176d2cc4f1caf0ab948ef72efb25ccce735461e
artbio
parents:
diff changeset
225 }