view make_window_bed.R @ 2:6f5ce4e865a5 draft

Deleted selected files
author kyost
date Sat, 28 Apr 2018 19:45:57 -0400
parents fd3ea97a96bc
children 72571a30f17b
line wrap: on
line source

## 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)