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