annotate calculate_density.R @ 3:1069776f7ae2 draft default tip

planemo upload for repository https://github.com/kavonrtep/galaxy_packages commit 3b9f93ed06cc32dbfa271789739e7a1e8fac528c
author petr-novak
date Tue, 30 Apr 2024 08:27:27 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
1069776f7ae2 planemo upload for repository https://github.com/kavonrtep/galaxy_packages commit 3b9f93ed06cc32dbfa271789739e7a1e8fac528c
petr-novak
parents:
diff changeset
1 #!/usr/bin/env Rscript
1069776f7ae2 planemo upload for repository https://github.com/kavonrtep/galaxy_packages commit 3b9f93ed06cc32dbfa271789739e7a1e8fac528c
petr-novak
parents:
diff changeset
2 library(optparse)
1069776f7ae2 planemo upload for repository https://github.com/kavonrtep/galaxy_packages commit 3b9f93ed06cc32dbfa271789739e7a1e8fac528c
petr-novak
parents:
diff changeset
3
1069776f7ae2 planemo upload for repository https://github.com/kavonrtep/galaxy_packages commit 3b9f93ed06cc32dbfa271789739e7a1e8fac528c
petr-novak
parents:
diff changeset
4 get_density <- function(x, chr_size=NULL, tw=1000000){
1069776f7ae2 planemo upload for repository https://github.com/kavonrtep/galaxy_packages commit 3b9f93ed06cc32dbfa271789739e7a1e8fac528c
petr-novak
parents:
diff changeset
5 cvg <- coverage(x)
1069776f7ae2 planemo upload for repository https://github.com/kavonrtep/galaxy_packages commit 3b9f93ed06cc32dbfa271789739e7a1e8fac528c
petr-novak
parents:
diff changeset
6 bins <- tileGenome(chr_size, tilewidth = tw)
1069776f7ae2 planemo upload for repository https://github.com/kavonrtep/galaxy_packages commit 3b9f93ed06cc32dbfa271789739e7a1e8fac528c
petr-novak
parents:
diff changeset
7 d <- binnedAverage(unlist(bins), cvg, "score")
1069776f7ae2 planemo upload for repository https://github.com/kavonrtep/galaxy_packages commit 3b9f93ed06cc32dbfa271789739e7a1e8fac528c
petr-novak
parents:
diff changeset
8 d
1069776f7ae2 planemo upload for repository https://github.com/kavonrtep/galaxy_packages commit 3b9f93ed06cc32dbfa271789739e7a1e8fac528c
petr-novak
parents:
diff changeset
9 }
1069776f7ae2 planemo upload for repository https://github.com/kavonrtep/galaxy_packages commit 3b9f93ed06cc32dbfa271789739e7a1e8fac528c
petr-novak
parents:
diff changeset
10 max_chr_length <- function(g){
1069776f7ae2 planemo upload for repository https://github.com/kavonrtep/galaxy_packages commit 3b9f93ed06cc32dbfa271789739e7a1e8fac528c
petr-novak
parents:
diff changeset
11 x <- split(g, ~seqnames)
1069776f7ae2 planemo upload for repository https://github.com/kavonrtep/galaxy_packages commit 3b9f93ed06cc32dbfa271789739e7a1e8fac528c
petr-novak
parents:
diff changeset
12 L <- sapply(x, function(x)max(end(x), na.rm=TRUE))
1069776f7ae2 planemo upload for repository https://github.com/kavonrtep/galaxy_packages commit 3b9f93ed06cc32dbfa271789739e7a1e8fac528c
petr-novak
parents:
diff changeset
13 L
1069776f7ae2 planemo upload for repository https://github.com/kavonrtep/galaxy_packages commit 3b9f93ed06cc32dbfa271789739e7a1e8fac528c
petr-novak
parents:
diff changeset
14 }
1069776f7ae2 planemo upload for repository https://github.com/kavonrtep/galaxy_packages commit 3b9f93ed06cc32dbfa271789739e7a1e8fac528c
petr-novak
parents:
diff changeset
15
1069776f7ae2 planemo upload for repository https://github.com/kavonrtep/galaxy_packages commit 3b9f93ed06cc32dbfa271789739e7a1e8fac528c
petr-novak
parents:
diff changeset
16 # get input arguments
1069776f7ae2 planemo upload for repository https://github.com/kavonrtep/galaxy_packages commit 3b9f93ed06cc32dbfa271789739e7a1e8fac528c
petr-novak
parents:
diff changeset
17 # bed file
1069776f7ae2 planemo upload for repository https://github.com/kavonrtep/galaxy_packages commit 3b9f93ed06cc32dbfa271789739e7a1e8fac528c
petr-novak
parents:
diff changeset
18 # window size
1069776f7ae2 planemo upload for repository https://github.com/kavonrtep/galaxy_packages commit 3b9f93ed06cc32dbfa271789739e7a1e8fac528c
petr-novak
parents:
diff changeset
19 # output bigwig file
1069776f7ae2 planemo upload for repository https://github.com/kavonrtep/galaxy_packages commit 3b9f93ed06cc32dbfa271789739e7a1e8fac528c
petr-novak
parents:
diff changeset
20 option_list <- list(
1069776f7ae2 planemo upload for repository https://github.com/kavonrtep/galaxy_packages commit 3b9f93ed06cc32dbfa271789739e7a1e8fac528c
petr-novak
parents:
diff changeset
21 make_option(c("-b", "--bed"), type="character", default=NULL, help="BED or GFF file"),
1069776f7ae2 planemo upload for repository https://github.com/kavonrtep/galaxy_packages commit 3b9f93ed06cc32dbfa271789739e7a1e8fac528c
petr-novak
parents:
diff changeset
22 make_option(c("-w", "--window"), type="integer", default=1000000, help="Window size"),
1069776f7ae2 planemo upload for repository https://github.com/kavonrtep/galaxy_packages commit 3b9f93ed06cc32dbfa271789739e7a1e8fac528c
petr-novak
parents:
diff changeset
23 make_option(c("-o", "--output"), type="character", default=NULL, help="Output BigWig file"),
1069776f7ae2 planemo upload for repository https://github.com/kavonrtep/galaxy_packages commit 3b9f93ed06cc32dbfa271789739e7a1e8fac528c
petr-novak
parents:
diff changeset
24 make_option(c("-f", "--format"), type="character", default="gff3", help="Input format (gff3 or bed)"),
1069776f7ae2 planemo upload for repository https://github.com/kavonrtep/galaxy_packages commit 3b9f93ed06cc32dbfa271789739e7a1e8fac528c
petr-novak
parents:
diff changeset
25 make_option(c("-m", "--merge"), type="logical", action="store_true", default=FALSE, help="Merge overlapping regions")
1069776f7ae2 planemo upload for repository https://github.com/kavonrtep/galaxy_packages commit 3b9f93ed06cc32dbfa271789739e7a1e8fac528c
petr-novak
parents:
diff changeset
26
1069776f7ae2 planemo upload for repository https://github.com/kavonrtep/galaxy_packages commit 3b9f93ed06cc32dbfa271789739e7a1e8fac528c
petr-novak
parents:
diff changeset
27
1069776f7ae2 planemo upload for repository https://github.com/kavonrtep/galaxy_packages commit 3b9f93ed06cc32dbfa271789739e7a1e8fac528c
petr-novak
parents:
diff changeset
28 )
1069776f7ae2 planemo upload for repository https://github.com/kavonrtep/galaxy_packages commit 3b9f93ed06cc32dbfa271789739e7a1e8fac528c
petr-novak
parents:
diff changeset
29
1069776f7ae2 planemo upload for repository https://github.com/kavonrtep/galaxy_packages commit 3b9f93ed06cc32dbfa271789739e7a1e8fac528c
petr-novak
parents:
diff changeset
30 opt_parser <- OptionParser(option_list=option_list)
1069776f7ae2 planemo upload for repository https://github.com/kavonrtep/galaxy_packages commit 3b9f93ed06cc32dbfa271789739e7a1e8fac528c
petr-novak
parents:
diff changeset
31 opt <- parse_args(opt_parser)
1069776f7ae2 planemo upload for repository https://github.com/kavonrtep/galaxy_packages commit 3b9f93ed06cc32dbfa271789739e7a1e8fac528c
petr-novak
parents:
diff changeset
32
1069776f7ae2 planemo upload for repository https://github.com/kavonrtep/galaxy_packages commit 3b9f93ed06cc32dbfa271789739e7a1e8fac528c
petr-novak
parents:
diff changeset
33 # check mandatory arguments
1069776f7ae2 planemo upload for repository https://github.com/kavonrtep/galaxy_packages commit 3b9f93ed06cc32dbfa271789739e7a1e8fac528c
petr-novak
parents:
diff changeset
34 if (is.null(opt$bed) || is.null(opt$output)){
1069776f7ae2 planemo upload for repository https://github.com/kavonrtep/galaxy_packages commit 3b9f93ed06cc32dbfa271789739e7a1e8fac528c
petr-novak
parents:
diff changeset
35 stop("Please provide bed file and output file")
1069776f7ae2 planemo upload for repository https://github.com/kavonrtep/galaxy_packages commit 3b9f93ed06cc32dbfa271789739e7a1e8fac528c
petr-novak
parents:
diff changeset
36 }
1069776f7ae2 planemo upload for repository https://github.com/kavonrtep/galaxy_packages commit 3b9f93ed06cc32dbfa271789739e7a1e8fac528c
petr-novak
parents:
diff changeset
37
1069776f7ae2 planemo upload for repository https://github.com/kavonrtep/galaxy_packages commit 3b9f93ed06cc32dbfa271789739e7a1e8fac528c
petr-novak
parents:
diff changeset
38 suppressPackageStartupMessages(library(rtracklayer))
1069776f7ae2 planemo upload for repository https://github.com/kavonrtep/galaxy_packages commit 3b9f93ed06cc32dbfa271789739e7a1e8fac528c
petr-novak
parents:
diff changeset
39 print(opt)
1069776f7ae2 planemo upload for repository https://github.com/kavonrtep/galaxy_packages commit 3b9f93ed06cc32dbfa271789739e7a1e8fac528c
petr-novak
parents:
diff changeset
40 g <- import(opt$bed, format=opt$format)
1069776f7ae2 planemo upload for repository https://github.com/kavonrtep/galaxy_packages commit 3b9f93ed06cc32dbfa271789739e7a1e8fac528c
petr-novak
parents:
diff changeset
41 if (opt$merge){
1069776f7ae2 planemo upload for repository https://github.com/kavonrtep/galaxy_packages commit 3b9f93ed06cc32dbfa271789739e7a1e8fac528c
petr-novak
parents:
diff changeset
42 g <- reduce(g)
1069776f7ae2 planemo upload for repository https://github.com/kavonrtep/galaxy_packages commit 3b9f93ed06cc32dbfa271789739e7a1e8fac528c
petr-novak
parents:
diff changeset
43 }
1069776f7ae2 planemo upload for repository https://github.com/kavonrtep/galaxy_packages commit 3b9f93ed06cc32dbfa271789739e7a1e8fac528c
petr-novak
parents:
diff changeset
44 chr_size <- max_chr_length(g)
1069776f7ae2 planemo upload for repository https://github.com/kavonrtep/galaxy_packages commit 3b9f93ed06cc32dbfa271789739e7a1e8fac528c
petr-novak
parents:
diff changeset
45 d <- get_density(g, chr_size, opt$window)
1069776f7ae2 planemo upload for repository https://github.com/kavonrtep/galaxy_packages commit 3b9f93ed06cc32dbfa271789739e7a1e8fac528c
petr-novak
parents:
diff changeset
46
1069776f7ae2 planemo upload for repository https://github.com/kavonrtep/galaxy_packages commit 3b9f93ed06cc32dbfa271789739e7a1e8fac528c
petr-novak
parents:
diff changeset
47 export(d, opt$output, format="bigwig")