annotate exomedepth-a9701c421408/exomedepth.R @ 0:6ea78cede7aa draft

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