comparison exomedepth.R @ 8:5d60331757d3 draft default tip

"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/exomedepth commit 9eb6d07510ccf27d6499172d62c81661078ec57b"
author iuc
date Wed, 25 Nov 2020 18:37:13 +0000
parents 45af4a9748cf
children
comparison
equal deleted inserted replaced
7:45af4a9748cf 8:5d60331757d3
1 # Load ExomeDepth library (without warnings) 1 # Load ExomeDepth library (without warnings)
2 suppressMessages(library(ExomeDepth)) 2 suppressMessages(library(ExomeDepth))
3 3
4 # Import parameters from xml wrapper (args_file) 4 # Import parameters from xml wrapper (args_file)
5 args <- commandArgs(trailingOnly=TRUE) 5 args <- commandArgs(trailingOnly = TRUE)
6 param <- read.table(args[1],sep="=", as.is=TRUE) 6 param <- read.table(args[1], sep = "=", as.is = TRUE)
7 7
8 # Set common parameters 8 # Set common parameters
9 target <- param[match("target",param[,1]),2] 9 target <- param[match("target", param[, 1]), 2]
10 trans_prob <- as.numeric(param[match("trans_prob",param[,1]),2]) 10 trans_prob <- as.numeric(param[match("trans_prob", param[, 1]), 2])
11 output <- param[match("output",param[,1]),2] 11 output <- param[match("output", param[, 1]), 2]
12 test_vs_ref <- as.logical(param[match("test_vs_ref",param[,1]),2]) 12 test_vs_ref <- as.logical(param[match("test_vs_ref", param[, 1]), 2])
13 13
14 # Create symbolic links for multiple bam and bai 14 # Create symbolic links for multiple bam and bai
15 bam <- param[param[,1]=="bam",2] 15 bam <- param[param[, 1] == "bam", 2]
16 bam_bai <- param[param[,1]=="bam_bai",2] 16 bam_bai <- param[param[, 1] == "bam_bai", 2]
17 bam_label <- param[param[,1]=="bam_label",2] 17 bam_label <- param[param[, 1] == "bam_label", 2]
18 bam_label <- gsub(" ", "_", bam_label) 18 bam_label <- gsub(" ", "_", bam_label)
19 19
20 for(i in 1:length(bam)){ 20 for (i in seq_len(length(bam))) {
21 stopifnot(file.symlink(bam[i], paste(bam_label[i], "bam", sep="."))) 21 stopifnot(file.symlink(bam[i], paste(bam_label[i], "bam", sep = ".")))
22 stopifnot(file.symlink(bam_bai[i], paste(bam_label[i], "bam.bai", sep="."))) 22 stopifnot(file.symlink(bam_bai[i], paste(bam_label[i], "bam.bai", sep = ".")))
23 } 23 }
24 24
25 # Generate read count data 25 # Generate read count data
26 BAMFiles <- paste(bam_label, "bam", sep=".") 26 bam_files <- paste(bam_label, "bam", sep = ".")
27 sink("/dev/null") 27 sink("/dev/null")
28 ExomeCount <- suppressMessages(getBamCounts(bed.file=target, bam.files = BAMFiles)) 28 exome_count <- suppressMessages(getBamCounts(bed.file = target, bam.files = bam_files))
29 sink() 29 sink()
30 30
31 # Convert counts in a data frame 31 # Convert counts in a data frame
32 ExomeCount.dafr <- as(ExomeCount[, colnames(ExomeCount)], 'data.frame') 32 exome_count_dafr <- as(exome_count[, colnames(exome_count)], "data.frame")
33 33
34 # Prepare the main matrix of read count data 34 # Prepare the main matrix of read count data
35 ExomeCount.mat <- as.matrix(ExomeCount.dafr[, grep(names(ExomeCount.dafr), pattern='.bam')]) 35 exome_count_mat <- as.matrix(exome_count_dafr[, grep(names(exome_count_dafr), pattern = ".bam")])
36 36
37 # Remove .bam from sample name 37 # Remove .bam from sample name
38 colnames(ExomeCount.mat) <- gsub(".bam", "", colnames(ExomeCount.mat)) 38 colnames(exome_count_mat) <- gsub(".bam", "", colnames(exome_count_mat))
39 39
40 # Set nsamples == 1 if mode is test vs reference, assuming test is sample 1 40 # Set nsamples == 1 if mode is test vs reference, assuming test is sample 1
41 nsamples <- ifelse(test_vs_ref, 1, ncol(ExomeCount.mat)) 41 nsamples <- ifelse(test_vs_ref, 1, ncol(exome_count_mat))
42 42
43 # Loop over samples 43 # Loop over samples
44 for (i in 1:nsamples){ 44 for (i in 1:nsamples) {
45 # Create the aggregate reference set for this sample
46 my_choice <- suppressWarnings(suppressMessages(select.reference.set(
47 test.counts = exome_count_mat[, i],
48 reference.counts = subset(exome_count_mat, select = -i),
49 bin.length = (exome_count_dafr$end - exome_count_dafr$start) / 1000,
50 n.bins.reduced = 10000
51 )))
45 52
46 # Create the aggregate reference set for this sample 53 my_reference_selected <- apply(
47 my.choice <- suppressWarnings(suppressMessages( 54 X = exome_count_mat[, my_choice$reference.choice, drop = FALSE],
48 select.reference.set(test.counts = ExomeCount.mat[,i], 55 MAR = 1,
49 reference.counts = subset(ExomeCount.mat, select=-i), 56 FUN = sum
50 bin.length = (ExomeCount.dafr$end - ExomeCount.dafr$start)/1000, 57 )
51 n.bins.reduced = 10000)))
52 58
53 my.reference.selected <- apply(X = ExomeCount.mat[, my.choice$reference.choice, drop=FALSE], 59 # Now create the ExomeDepth object for the CNVs call
54 MAR = 1, 60 all.exons <- suppressWarnings(suppressMessages(
55 FUN = sum) 61 new("ExomeDepth",
56 62 test = exome_count_mat[, i],
57 # Now create the ExomeDepth object for the CNVs call 63 reference = my_reference_selected,
58 all.exons<-suppressWarnings(suppressMessages(new('ExomeDepth', 64 formula = "cbind(test,reference)~1"
59 test = ExomeCount.mat[,i], 65 )))
60 reference = my.reference.selected,
61 formula = 'cbind(test,reference)~1')))
62 66
67 # Now call the CNVs
68 result <- try(all.exons <- suppressMessages(CallCNVs(
69 x = all.exons,
70 transition.probability = trans_prob,
71 chromosome = exome_count_dafr$space,
72 start = exome_count_dafr$start,
73 end = exome_count_dafr$end,
74 name = exome_count_dafr$names
75 )), silent = T)
63 76
64 # Now call the CNVs 77 # Next if CNVs are not detected
65 result <- try(all.exons<-suppressMessages(CallCNVs(x=all.exons, 78 if (class(result) == "try-error") {
66 transition.probability = trans_prob, 79 next
67 chromosome = ExomeCount.dafr$space, 80 }
68 start = ExomeCount.dafr$start,
69 end = ExomeCount.dafr$end,
70 name = ExomeCount.dafr$names)), silent=T)
71 81
72 # Next if CNVs are not detected 82 # Compute correlation between ref and test
73 if (class(result)=="try-error"){ 83 my_cor <- cor(all.exons@reference, all.exons@test)
74 next
75 }
76 84
77 # Compute correlation between ref and test 85 # Write results
78 my.cor <- cor(all.exons@reference, all.exons@test) 86 my_results <- cbind(
79 n.call <- nrow(all.exons@CNV.calls) 87 all.exons@CNV.calls[, c(7, 5, 6, 3)],
88 sample = colnames(exome_count_mat)[i],
89 corr = my_cor,
90 all.exons@CNV.calls[, c(4, 9, 12)]
91 )
80 92
81 # Write results 93 # Re-order by chr and position
82 my.results <- cbind(all.exons@CNV.calls[,c(7,5,6,3)], 94 chr_order <- c(paste("chr", 1:22, sep = ""), "chrX", "chrY", "chrM")
83 sample=colnames(ExomeCount.mat)[i], 95 my_results[, 1] <- factor(my_results[, 1], levels = chr_order)
84 corr=my.cor, 96 my_results <- my_results[order(my_results[, 1], my_results[, 2], my_results[, 3]), ]
85 all.exons@CNV.calls[,c(4,9,12)]) 97
86 98 write.table(
87 # Re-order by chr and position 99 sep = "\t",
88 chrOrder<-c(paste("chr",1:22,sep=""),"chrX","chrY","chrM") 100 quote = FALSE,
89 my.results[,1] <- factor(my.results[,1], levels=chrOrder) 101 file = output,
90 my.results <- my.results[order(my.results[,1], my.results[,2], my.results[,3]),] 102 x = my_results,
91 103 row.names = FALSE,
92 write.table(sep='\t', quote=FALSE, file = output, 104 col.names = FALSE,
93 x = my.results, 105 dec = ".",
94 row.names = FALSE, col.names = FALSE, dec=".", append=TRUE) 106 append = TRUE
107 )
95 } 108 }