-
Notifications
You must be signed in to change notification settings - Fork 1
/
bamplot.R
executable file
·91 lines (75 loc) · 2.33 KB
/
bamplot.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
#!/usr/bin/Rscript
print("Script is plotting coverage from bam file and selected reference sequence ID")
exit <- function() {
.Internal(.invokeRestart(list(NULL, NULL), NULL))
}
.unlist <- function (x){
x1 <- x[[1L]]
if (is.factor(x1)){
structure(unlist(x), class = "factor", levels = levels(x1))
} else {
do.call(c, x)
}
}
check_pos <- function(x){
if (intToBits(x)[3] == 1){
return(F)
} else if (intToBits(x)[5] != 1){
return(T)
} else {
return(F)
}
}
check_neg <- function(x){
if (intToBits(x)[5] == 1){
return(T)
} else {
return(F)
}
}
library(Rsamtools, quietly=TRUE)
args <- commandArgs()
print(paste("Bam file is " ,args[6], ", Scaffold name is ", args[7], sep = ""))
bam_path <- args[6]
scaffold_name <- args[7]
if(is.na(bam_path) == TRUE){
print("Bam file is required to use bamplot! Typical usage: ./bamplot.R bamfile.bam scaffold_name")
exit()
}
if(is.na(scaffold_name) == TRUE){
print("Scaffold name is required to use bamplot! Typical usage: ./bamplot.R bamfile.bam scaffold_name")
exit()
}
print("Reading BAM file")
bam <- scanBam(bam_path)
print("Parsing bam file...")
bam_field <- names(bam[[1]])
list <- lapply(bam_field, function(y) .unlist(lapply(bam, "[[", y)))
bam_df <- do.call("DataFrame", list)
names(bam_df) <- bam_field
print("Starting getting data from subsetted query...")
sequence_name = scaffold_name
test <- subset(bam_df, rname == sequence_name)
sequence_name_neg <- bam_df[bam_df$rname == sequence_name &
apply(as.data.frame(bam_df$flag), 1, check_neg),
'pos'
]
sequence_name_pos <- bam_df[bam_df$rname == sequence_name &
apply(as.data.frame(bam_df$flag), 1, check_pos),
'pos'
]
sequence_name_neg_density <- density(sequence_name_neg)
sequence_name_pos_density <- density(sequence_name_pos)
sequence_name_neg_density$y <- sequence_name_neg_density$y * -1
print("Plotting...")
png(paste(sequence_name, ".png", sep = ""))
plot(sequence_name_pos_density,
ylim = range(c(sequence_name_neg_density$y, sequence_name_pos_density$y)),
main = paste("Coverage of ", sequence_name, sep = ""),
xlab = paste(sequence_name, sep = ""),
col = 'blue',
type='h'
)
lines(sequence_name_neg_density, type='h', col = 'red')
dev.off()
print("Plotting done!")