-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathprepare_coverage.R
76 lines (66 loc) · 1.38 KB
/
prepare_coverage.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
### script to prepare annotation BED file to coverage start and stop region
# load libs
source('functions.R')
# load tool locations
tool_path <- load_tool_location()
# get args
get_dir <- commandArgs(trailingOnly = TRUE)
# check input file
in_bed <- get_dir[1]
if(grep('.bed$',in_bed) != 1){
stop(
paste0(in_bed, ' is not a BED file!')
)
}
# check output dir
# out_path <- parse_dir(get_dir[2])
# if(!dir.exists(out_path)){
# dir.create(out_path)
# }
# if(!dir.exists(paste0(out_path,'start_stop_region/'))){
# dir.create(paste0(out_path,'start_stop_region/'))
# }
# load
anno <- read.table(
in_bed,
stringsAsFactors = F,
sep = "\t"
)
# modify annotation
anno <- t(sapply(1:nrow(anno),function(j){
# check for different exons
anno_ <- anno[j,]
idx <- which(anno[,4]==anno_[1,4])
# if only one exon
if(length(idx) == 1){
anno_[1,2] <- anno_[1,2]-50
anno_[1,3] <- anno_[1,3]+50
return(anno_)
# else check if first
} else {
if(anno_[1,2] == min(anno[idx,2])){
anno_[1,2] <- anno_[1,2]-50
return(anno_)
} else {
return(anno_)
}
if(anno_[1,2] == max(anno[idx,2])){
anno_[1,3] <- anno_[1,3]+50
return(anno_)
} else {
return(anno_)
}
}
}))
# write
write.table(
anno,
paste0(
gsub(".bed$","",in_bed),
'_plus_50nt.bed'
),
col.names = F,
row.names = F,
sep = "\t",
quote = F
)