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