comparison compare_gff.R @ 6:b53f5a456d01 draft

"planemo upload commit 3aefb0555456837d10fe69e4ad25de08d5972cb2"
author petr-novak
date Thu, 19 May 2022 08:23:55 +0000
parents
children 5366d5ea04bc
comparison
equal deleted inserted replaced
5:eca9213923b1 6:b53f5a456d01
1 #!/usr/bin/env Rscript
2 # function from questionr R package:
3 wtable <- function(x, y = NULL, weights = NULL, digits = 3, normwt = FALSE, useNA = c("no", "ifany", "always"), na.rm = TRUE, na.show = FALSE, exclude = NULL)
4 {
5 if (is.null(weights)) {
6 warning("no weights argument given, using uniform weights of 1")
7 weights <- rep(1, length(x))
8 }
9 if (length(x) != length(weights)) stop("x and weights lengths must be the same")
10 if (!is.null(y) & (length(x) != length(y))) stop("x and y lengths must be the same")
11 miss.usena <- missing(useNA)
12 useNA <- match.arg(useNA)
13 weights[is.na(weights)] <- 0
14 if (normwt) {
15 weights <- weights * length(x)/sum(weights)
16 }
17
18 if (!missing(na.show) || !missing(na.rm)) {
19 warning("'na.rm' and 'na.show' are ignored when 'useNA' is provided.")
20 }
21 if (useNA != "no" || (na.show && miss.usena)) {
22 if (match(NA, exclude, nomatch = 0L)) {
23 warning("'exclude' containing NA and 'useNA' != \"no\"' are a bit contradicting")
24 }
25 x <- addNA(x)
26 if (!is.null(y)) y <- addNA(y)
27 }
28 if (useNA == "no" || (na.rm && miss.usena)) {
29 s <- !is.na(x) & !is.na(weights)
30 if (!is.null(y)) s <- s & !is.na(y)
31 x <- x[s, drop = FALSE]
32 if (!is.null(y)) y <- y[s, drop = FALSE]
33 weights <- weights[s]
34 }
35 if (!is.null(exclude)) {
36 s <- !(x %in% exclude)
37 if (!is.null(y)) s <- s & !(y %in% exclude)
38 x <- factor(x[s, drop = FALSE])
39 if (!is.null(y)) y <- factor(y[s, drop = FALSE])
40 weights <- weights[s]
41 }
42 if (is.null(y)) {
43 result <- tapply(weights, x, sum, simplify = TRUE)
44 }
45 else {
46 result <- tapply(weights, list(x,y), sum, simplify = TRUE)
47 }
48 result[is.na(result)] <- 0
49 tab <- as.table(result)
50 if (useNA == "ifany") {
51 if (!is.null(y)) {
52 if (sum(tab[,is.na(colnames(tab))]) == 0) tab <- tab[,!is.na(colnames(tab))]
53 if (sum(tab[is.na(rownames(tab)),]) == 0) tab <- tab[!is.na(rownames(tab)),]
54 } else {
55 if (tab[is.na(names(tab))] == 0) tab <- tab[!is.na(names(tab))]
56 }
57 }
58 tab
59 }
60
61 suppressPackageStartupMessages(library(rtracklayer))
62 library(parallel)
63 g1 <- import(commandArgs(T)[1], format = "GFF")
64 g2 <- import(commandArgs(T)[2], format = "GFF")
65 # assume non inrarange overlaps!
66 attribute_name <- commandArgs(T)[3]
67 if (FALSE){
68 g1 <- import("test_data/RM_LTR_TE.gff3")
69 g2 <- import("test_data/RM_RE.gff3")
70 g2 <- import("test_data/RM_RE_CL.gff3")
71 g2 <- import("test_data/RM_RE_v3.gff3")
72 g2 <- import("test_data/RM_RE_v4.gff3")
73
74 attribute_name <- "Name"
75 }
76
77 g1$SOURCE <- 1
78 g2$SOURCE <- 2
79
80 g12 <- append(g1,g2)
81 g12_disjoin <- disjoin(g12, ignore.strand=TRUE, with.revmap=TRUE)
82
83 g12$sel_attr <- mcols(g12)[,attribute_name]
84
85 annot_name <- mclapply(as.list(g12_disjoin$revmap), FUN = function(x)g12$sel_attr[x], mc.cores = 8)
86 annot_source <- mclapply(as.list(g12_disjoin$revmap), FUN = function(x)g12$SOURCE[x], mc.cores = 8)
87
88 c1 <- sapply(annot_source, function (x) 1 %in% x & !(2 %in% x))
89 c2 <- sapply(annot_source, function (x) 2 %in% x & !(1 %in% x))
90 c12 <- !(c1 | c2)
91
92
93 n1 <- cbind(sapply(annot_name[c1], "[", 1 ), "No Annotation")
94 n2 <- cbind("No Annotation", sapply(annot_name[c2], "[", 1))
95
96 n12 <- do.call(rbind, annot_name[c12])
97
98 n12all <- matrix(character(), ncol = 2, nrow = length(g12_disjoin))
99 n12all[c1,] <- n1
100 n12all[c2,] <- n2
101 n12all[c12,] <- n12
102
103 traw <- wtable(n12all[,1], n12all[,2], weights = width(g12_disjoin))
104 tbl_df <- as.data.frame((traw))
105 tbl_df <- tbl_df[order(tbl_df$Freq, decreasing = TRUE),]
106 colnames(tbl_df) <- c("Annotation1", "Annotation2", "Overlap[bp]")
107
108 ## NOTE - if name change - modify it in xml too
109 write.table(tbl_df, file = 'annotation_overlap_long.csv', sep="\t", quote=FALSE, row.names = FALSE)
110
111 tbl <- as.matrix(traw)
112 tbl <- tbl[order(rowSums(tbl), decreasing = TRUE),]
113 tbl <- tbl[, order(colSums(tbl), decreasing = TRUE)]
114 write.table(tbl, file= 'annotation_overlap.csv', sep="\t", quote = FALSE)
115