comparison make_window_bed.R @ 0:fd3ea97a96bc draft

planemo upload commit 103cb51ec368438642504c3f98b76c363db478bb
author kyost
date Sat, 28 Apr 2018 15:07:26 -0400
parents
children 72571a30f17b
comparison
equal deleted inserted replaced
-1:000000000000 0:fd3ea97a96bc
1 ## Command to run tool:
2 # Rscript --vanilla make_window_bed.R qPCR_peaks.bed window_size output_file
3
4 # Set up R error handling to go to stderr
5 options(show.error.messages=F, error=function(){cat(geterrmessage(),file=stderr());q("no",1,F)})
6
7 # Avoid crashing Galaxy with an UTF8 error on German LC settings
8 loc <- Sys.setlocale("LC_MESSAGES", "en_US.UTF-8")
9
10 args <- commandArgs(TRUE)
11
12 qPCR_bed <- args[1]
13 window_size <- strtoi(args[2])
14 output_file <- args[3]
15
16 qPCR_table <- read.delim(qPCR_bed, header=FALSE, stringsAsFactors=FALSE)
17
18 make_windows_bed <- function(a, size) { #a is a bed file containing peaks of interest and coordinates
19 #generates bed file with overlapping windows spanning each peak of specified size
20 return_bed <- data.frame()
21 for (i in 1:nrow(a)) {
22 line <- data.frame(a[i,],stringsAsFactors=FALSE)
23 peak_name <- as.character(line[1,4])
24 line <- rbind(line
25 , data.frame(V1=as.character(line[1,1])
26 , V2=line[1,2]
27 , V3=line[1,2]+size
28 , V4=as.character(paste(as.character(line[1,4])
29 , "_window1"
30 , sep = "")
31 )
32 )
33 )
34 count <- as.numeric(line[1,2])+size
35 nline <- 2
36 while (count < as.numeric(line[1,3])) {
37 line <- rbind(line
38 , data.frame(V1=as.character(line[nline,1])
39 , V2=line[nline,2]+floor(size/4)
40 , V3=line[nline,2]+floor(size/4)+size
41 , V4=as.character(paste(as.character(line[1,4])
42 , "_window"
43 , as.character(nline)
44 , sep = "")
45 )
46 )
47 )
48 nline <- nline + 1
49 count <- as.numeric(line[nline,3])
50 }
51 return_bed <- rbind(return_bed,line)
52 }
53 return(return_bed)
54 }
55
56 output <- make_windows_bed(qPCR_table, window_size)
57
58
59 write.table(output
60 , output_file
61 , sep = "\t"
62 , col.names = FALSE
63 , row.names = FALSE
64 , quote = FALSE)