### narrowPeak2bed.r ### Ivory Blakley ### 5 November 2014 ### usage: Rscript narrowPeak2bed.r prefix outDir ### prefix (required) sample name of narrowPeak file ### outDir (optional) a directory where the bed files should be saved ################################################### ### code chunk number 0: options and variables ################################################### options(echo=TRUE) date() args=commandArgs(trailingOnly=T) prefix=args[1] prefix=gsub(".narrowPeak", "", prefix) #the prefix should not include the file extension outDir=NULL if (length(args)>1) { outDir = paste0(args[2], "/") outDir = gsub("//", "/", outDir) } print(prefix) ################################################### ### code chunk number 1: read in narrowPeak file ################################################### fname.in=paste(prefix, "narrowPeak", sep=".") peaks = read.table(fname.in) fname.out=paste0(outDir, prefix, ".bed") ################################################### ### code chunk number 2: convert from narrowPeak format to bed format ################################################### peaks$V4 = paste("spp", prefix, 1:nrow(peaks), sep="_") peaks$V5 = peaks$V9 peaks$V7 = peaks$V10 + peaks$V2 peaks$V8 = peaks$V7 + 1 peaks = peaks[,1:8] peaks$V9 = NA peaks$V10 = 1 peaks$V11 = peaks$V3 - peaks$V2 peaks$V12 = 0 ################################################### ### code chunk number 3: write out bed file ################################################### write.table(peaks, file=fname.out, sep="\t", row.names=F, col.names=F, quote=F) ################################################### ###code chunk number 4: session Info ################################################### sessionInfo()