-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhmmCopy.R
94 lines (77 loc) · 3.32 KB
/
hmmCopy.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env Rscript
# runs HMM copy on tumor normal wig files
suppressPackageStartupMessages(library("optparse"));
suppressPackageStartupMessages(library("HMMcopy"));
suppressPackageStartupMessages(library("hwriter"));
optionList <- list(make_option(c("-g", "--gcWig"), default = NULL, help = "GC content wig file"),
make_option(c("-m", "--mapWig"), default = NULL, help = "Mappability wig file"),
make_option(c("-n", "--normalWig"), default = NULL, help = "Normal wig file"),
make_option(c("-o", "--outPrefix"), default = NULL, help = "Output file"));
posArgs <- c('tumor_wig')
parser <- OptionParser(usage = paste('%prog [options]', paste(posArgs, collapse=' ')), option_list=optionList)
arguments <- parse_args(parser, positional_arguments = TRUE)
opt <- arguments$options
if (length(arguments$args) != 1) {
print_help(parser)
print(arguments$args)
stop('Incorrect number of required positional arguments')
} else if (is.null(opt$gcWig)) {
print_help(parser)
print(arguments$args)
stop('Need GC content wig file')
} else if (is.null(opt$outPrefix)) {
print_help(parser)
print(arguments$args)
stop('Need output prefix')
}
cmdArgs <- arguments$args
for (i in 1:length(cmdArgs)){
assign(posArgs[i], cmdArgs[i])
}
tumorWigFile <- cmdArgs[1]
normalWigFile <- opt$normalWig
gcWigFile <- opt$gcWig
mapWigFile <- opt$mapWig
tumorData <- wigsToRangedData(tumorWigFile, gcWigFile, mapWigFile)
tumorData <- correctReadcount(tumorData)
if (!is.null(normalWigFile)) {
normalData <- wigsToRangedData(normalWigFile, gcWigFile, mapWigFile)
normalData <- correctReadcount(normalData)
tumorData$copy <- tumorData$copy - normalData$copy
}
param <- HMMsegment(tumorData, getparam = TRUE)
param$mu <- log(c(1, 1.4, 2, 2.7, 3, 4.5) / 2, 2) # Gavin's parameters
param$m <- param$mu
tumorSeg <- HMMsegment(tumorData, param)
fn <- paste(opt$outPrefix, '.hmmcopy_seg.txt', sep = '')
write.table(as.data.frame(tumorSeg$segs), file = fn, sep = '\t', quote = F)
dir.create('graphics', showWarnings = F)
fn <- paste(opt$outPrefix, 'hmmcopy.html', sep = '')
pg <- openPage(basename(fn), dirname = dirname(fn))
gfn <- paste(opt$outPrefix, '.seg_plot.png', sep = '')
png(gfn, height = 800, width = 1000)
plotSegments(tumorData, tumorSeg, pch = '.', ylab = "Tumour Copy Number", xlab = "Chromosome Position")
null <- dev.off()
hwriteImage(basename(gfn), pg)
gfn <- paste(opt$outPrefix, '.bias_plot.png', sep = '')
png(gfn, height = 1000, width = 1000)
plotBias(tumorData, pch = 19)
null <- dev.off()
hwriteImage(basename(gfn), pg)
for (chr in paste("chr", 1:21, sep = '')) {
gfn <- paste(opt$outPrefix, '.seg_plot_', chr, '.png', sep = '')
png(gfn, height = 1000, width = 1000)
plotSegments(tumorData, tumorSeg, pch = '.', chr = chr, ylab = "Tumour Copy Number", xlab = "Chromosome Position", main = chr)
cols <- stateCols()
legend("topleft", c("HOMD", "HETD", "NEUT", "GAIN", "AMPL", "HLAMP"), fill = cols, horiz = T, bty = "n", cex = 0.5)
null <- dev.off()
hwriteImage(basename(gfn), pg)
}
for (chr in paste("chr", 1:21, sep = '')) {
gfn <- paste(opt$outPrefix, '.cor_plot_', chr, '.png', sep = '')
png(gfn, height = 1000, width = 1000)
plotCorrection(tumorData, pch = '.', chr = chr)
null <- dev.off()
hwriteImage(basename(gfn), pg)
}
null <- closePage(pg)