-
Notifications
You must be signed in to change notification settings - Fork 0
/
bedMaker.R
60 lines (43 loc) · 1.64 KB
/
bedMaker.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
#!/usr/bin/env R
# Author: Oliver Stuart
# Date: 3/4/2020
#This script takes a list of contigs and their lengths and spits out a bed file targeting reads on those contigs
##################################
######## Initial Checks ##########
##################################
#Are all required packages installed?
is.installed <- function(mypkg) is.element(mypkg, installed.packages()[,1])
packages <- c("optparse","dplyr")
if(sum(is.installed(packages)) < length(packages)){
print(paste0("Something is missing, check that all required packages (",paste(packages,collapse=", "),") are installed."))
stop()
}
suppressMessages(library("optparse"))
suppressMessages(library("dplyr"))
#Bring in the inputs
option_list = list(
make_option(c("-c", "--contig"), type="character", default=NULL,
help="tab separated table of contigs\tlengths", metavar="character")
);
opt_parser = OptionParser(option_list=option_list);
opt = parse_args(opt_parser);
#Do the inputs actually exist?
if (is.null(opt$contig)){
print_help(opt_parser)
stop("Provide a contig file.", call.=FALSE)
}
##################################
######### Preparing data #########
##################################
#Read table
d <- read.delim(opt$contig,header=T,
colClasses="character")
colnames(d) <- c("contig","lengths")
w <- data.frame(contig=d$contig,
start=rep(0,times=nrow(d)),
stop=as.numeric(d$length)-1)
##################################
########## Writing data ##########
##################################
#Write the bed file
write.table(w,file=paste0(opt$contig,".bed"),col.names=F,row.names=F,quote=F,sep="\t")