annotate exomedepth.R @ 4:9ccde1867fbb draft

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