diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/make_window_bed.R	Sat Apr 28 15:07:26 2018 -0400
@@ -0,0 +1,64 @@
+## Command to run tool:
+# Rscript --vanilla make_window_bed.R qPCR_peaks.bed window_size output_file
+
+# Set up R error handling to go to stderr
+options(show.error.messages=F, error=function(){cat(geterrmessage(),file=stderr());q("no",1,F)})
+
+# Avoid crashing Galaxy with an UTF8 error on German LC settings
+loc <- Sys.setlocale("LC_MESSAGES", "en_US.UTF-8")
+
+args <- commandArgs(TRUE)
+
+qPCR_bed <- args[1]
+window_size <- strtoi(args[2])
+output_file <- args[3]
+
+qPCR_table <- read.delim(qPCR_bed, header=FALSE, stringsAsFactors=FALSE)
+
+make_windows_bed <- function(a, size) { #a is a bed file containing peaks of interest and coordinates
+  #generates bed file with overlapping windows spanning each peak of specified size
+  return_bed <- data.frame()
+  for (i in 1:nrow(a)) {
+    line <- data.frame(a[i,],stringsAsFactors=FALSE)
+    peak_name <- as.character(line[1,4])
+    line <- rbind(line
+                  , data.frame(V1=as.character(line[1,1])
+                               , V2=line[1,2]
+                               , V3=line[1,2]+size
+                               , V4=as.character(paste(as.character(line[1,4])
+                                                       , "_window1"
+                                                       , sep = "")
+                               )
+                  )
+    )
+    count <- as.numeric(line[1,2])+size
+    nline <- 2
+    while (count < as.numeric(line[1,3])) {
+      line <- rbind(line
+					, data.frame(V1=as.character(line[nline,1])
+								, V2=line[nline,2]+floor(size/4)
+								, V3=line[nline,2]+floor(size/4)+size
+								, V4=as.character(paste(as.character(line[1,4])
+												 , "_window"
+												 , as.character(nline)
+												 , sep = "")
+												 )
+								)
+                    )
+      nline <- nline + 1
+      count <- as.numeric(line[nline,3])
+    }
+    return_bed <- rbind(return_bed,line)
+  }
+  return(return_bed)
+}
+
+output <- make_windows_bed(qPCR_table, window_size)
+
+
+write.table(output
+            , output_file
+            , sep = "\t"
+			, col.names = FALSE
+			, row.names = FALSE
+            , quote = FALSE)