Mercurial > repos > kyost > atac_primer_tool
annotate make_window_bed.R @ 3:fbfe7b785ea7 draft
Add test for 0 standard deviation.
author | kyost |
---|---|
date | Wed, 02 May 2018 13:20:01 -0400 |
parents | fd3ea97a96bc |
children | 72571a30f17b |
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 |
fd3ea97a96bc
planemo upload commit 103cb51ec368438642504c3f98b76c363db478bb
kyost
parents:
diff
changeset
|
18 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
|
19 #generates bed file with overlapping windows spanning each peak of specified size |
fd3ea97a96bc
planemo upload commit 103cb51ec368438642504c3f98b76c363db478bb
kyost
parents:
diff
changeset
|
20 return_bed <- data.frame() |
fd3ea97a96bc
planemo upload commit 103cb51ec368438642504c3f98b76c363db478bb
kyost
parents:
diff
changeset
|
21 for (i in 1:nrow(a)) { |
fd3ea97a96bc
planemo upload commit 103cb51ec368438642504c3f98b76c363db478bb
kyost
parents:
diff
changeset
|
22 line <- data.frame(a[i,],stringsAsFactors=FALSE) |
fd3ea97a96bc
planemo upload commit 103cb51ec368438642504c3f98b76c363db478bb
kyost
parents:
diff
changeset
|
23 peak_name <- as.character(line[1,4]) |
fd3ea97a96bc
planemo upload commit 103cb51ec368438642504c3f98b76c363db478bb
kyost
parents:
diff
changeset
|
24 line <- rbind(line |
fd3ea97a96bc
planemo upload commit 103cb51ec368438642504c3f98b76c363db478bb
kyost
parents:
diff
changeset
|
25 , data.frame(V1=as.character(line[1,1]) |
fd3ea97a96bc
planemo upload commit 103cb51ec368438642504c3f98b76c363db478bb
kyost
parents:
diff
changeset
|
26 , V2=line[1,2] |
fd3ea97a96bc
planemo upload commit 103cb51ec368438642504c3f98b76c363db478bb
kyost
parents:
diff
changeset
|
27 , V3=line[1,2]+size |
fd3ea97a96bc
planemo upload commit 103cb51ec368438642504c3f98b76c363db478bb
kyost
parents:
diff
changeset
|
28 , V4=as.character(paste(as.character(line[1,4]) |
fd3ea97a96bc
planemo upload commit 103cb51ec368438642504c3f98b76c363db478bb
kyost
parents:
diff
changeset
|
29 , "_window1" |
fd3ea97a96bc
planemo upload commit 103cb51ec368438642504c3f98b76c363db478bb
kyost
parents:
diff
changeset
|
30 , sep = "") |
fd3ea97a96bc
planemo upload commit 103cb51ec368438642504c3f98b76c363db478bb
kyost
parents:
diff
changeset
|
31 ) |
fd3ea97a96bc
planemo upload commit 103cb51ec368438642504c3f98b76c363db478bb
kyost
parents:
diff
changeset
|
32 ) |
fd3ea97a96bc
planemo upload commit 103cb51ec368438642504c3f98b76c363db478bb
kyost
parents:
diff
changeset
|
33 ) |
fd3ea97a96bc
planemo upload commit 103cb51ec368438642504c3f98b76c363db478bb
kyost
parents:
diff
changeset
|
34 count <- as.numeric(line[1,2])+size |
fd3ea97a96bc
planemo upload commit 103cb51ec368438642504c3f98b76c363db478bb
kyost
parents:
diff
changeset
|
35 nline <- 2 |
fd3ea97a96bc
planemo upload commit 103cb51ec368438642504c3f98b76c363db478bb
kyost
parents:
diff
changeset
|
36 while (count < as.numeric(line[1,3])) { |
fd3ea97a96bc
planemo upload commit 103cb51ec368438642504c3f98b76c363db478bb
kyost
parents:
diff
changeset
|
37 line <- rbind(line |
fd3ea97a96bc
planemo upload commit 103cb51ec368438642504c3f98b76c363db478bb
kyost
parents:
diff
changeset
|
38 , data.frame(V1=as.character(line[nline,1]) |
fd3ea97a96bc
planemo upload commit 103cb51ec368438642504c3f98b76c363db478bb
kyost
parents:
diff
changeset
|
39 , V2=line[nline,2]+floor(size/4) |
fd3ea97a96bc
planemo upload commit 103cb51ec368438642504c3f98b76c363db478bb
kyost
parents:
diff
changeset
|
40 , V3=line[nline,2]+floor(size/4)+size |
fd3ea97a96bc
planemo upload commit 103cb51ec368438642504c3f98b76c363db478bb
kyost
parents:
diff
changeset
|
41 , V4=as.character(paste(as.character(line[1,4]) |
fd3ea97a96bc
planemo upload commit 103cb51ec368438642504c3f98b76c363db478bb
kyost
parents:
diff
changeset
|
42 , "_window" |
fd3ea97a96bc
planemo upload commit 103cb51ec368438642504c3f98b76c363db478bb
kyost
parents:
diff
changeset
|
43 , as.character(nline) |
fd3ea97a96bc
planemo upload commit 103cb51ec368438642504c3f98b76c363db478bb
kyost
parents:
diff
changeset
|
44 , sep = "") |
fd3ea97a96bc
planemo upload commit 103cb51ec368438642504c3f98b76c363db478bb
kyost
parents:
diff
changeset
|
45 ) |
fd3ea97a96bc
planemo upload commit 103cb51ec368438642504c3f98b76c363db478bb
kyost
parents:
diff
changeset
|
46 ) |
fd3ea97a96bc
planemo upload commit 103cb51ec368438642504c3f98b76c363db478bb
kyost
parents:
diff
changeset
|
47 ) |
fd3ea97a96bc
planemo upload commit 103cb51ec368438642504c3f98b76c363db478bb
kyost
parents:
diff
changeset
|
48 nline <- nline + 1 |
fd3ea97a96bc
planemo upload commit 103cb51ec368438642504c3f98b76c363db478bb
kyost
parents:
diff
changeset
|
49 count <- as.numeric(line[nline,3]) |
fd3ea97a96bc
planemo upload commit 103cb51ec368438642504c3f98b76c363db478bb
kyost
parents:
diff
changeset
|
50 } |
fd3ea97a96bc
planemo upload commit 103cb51ec368438642504c3f98b76c363db478bb
kyost
parents:
diff
changeset
|
51 return_bed <- rbind(return_bed,line) |
fd3ea97a96bc
planemo upload commit 103cb51ec368438642504c3f98b76c363db478bb
kyost
parents:
diff
changeset
|
52 } |
fd3ea97a96bc
planemo upload commit 103cb51ec368438642504c3f98b76c363db478bb
kyost
parents:
diff
changeset
|
53 return(return_bed) |
fd3ea97a96bc
planemo upload commit 103cb51ec368438642504c3f98b76c363db478bb
kyost
parents:
diff
changeset
|
54 } |
fd3ea97a96bc
planemo upload commit 103cb51ec368438642504c3f98b76c363db478bb
kyost
parents:
diff
changeset
|
55 |
fd3ea97a96bc
planemo upload commit 103cb51ec368438642504c3f98b76c363db478bb
kyost
parents:
diff
changeset
|
56 output <- make_windows_bed(qPCR_table, window_size) |
fd3ea97a96bc
planemo upload commit 103cb51ec368438642504c3f98b76c363db478bb
kyost
parents:
diff
changeset
|
57 |
fd3ea97a96bc
planemo upload commit 103cb51ec368438642504c3f98b76c363db478bb
kyost
parents:
diff
changeset
|
58 |
fd3ea97a96bc
planemo upload commit 103cb51ec368438642504c3f98b76c363db478bb
kyost
parents:
diff
changeset
|
59 write.table(output |
fd3ea97a96bc
planemo upload commit 103cb51ec368438642504c3f98b76c363db478bb
kyost
parents:
diff
changeset
|
60 , output_file |
fd3ea97a96bc
planemo upload commit 103cb51ec368438642504c3f98b76c363db478bb
kyost
parents:
diff
changeset
|
61 , sep = "\t" |
fd3ea97a96bc
planemo upload commit 103cb51ec368438642504c3f98b76c363db478bb
kyost
parents:
diff
changeset
|
62 , col.names = FALSE |
fd3ea97a96bc
planemo upload commit 103cb51ec368438642504c3f98b76c363db478bb
kyost
parents:
diff
changeset
|
63 , row.names = FALSE |
fd3ea97a96bc
planemo upload commit 103cb51ec368438642504c3f98b76c363db478bb
kyost
parents:
diff
changeset
|
64 , quote = FALSE) |