annotate R/ltr_utils.R @ 7:c33d6583e548 draft

"planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
author petr-novak
date Fri, 24 Jun 2022 14:19:48 +0000
parents 6ae4a341d1f3
children 9de392f2fc02
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
1 add_coordinates_of_closest_neighbor <- function(gff) {
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
2 gff <- gff[order(seqnames(gff), start(gff))]
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
3 # split to chromosomes:
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
4 gff_parts <- split(gff, seqnames(gff))
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
5 upstreams <- c(sapply(gff_parts, function(x) c(1, head(end(x), -1))))
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
6 downstreams <- c(sapply(gff_parts, function(x) c(start(x)[-1], seqlengths(x)[runValue(seqnames(x[1]))])))
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
7 gff_updated <- unlist(gff_parts)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
8 gff_updated$upstream_domain <- unlist(upstreams)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
9 gff_updated$downstream_domain <- unlist(downstreams)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
10 names(gff_updated) <- NULL
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
11 return(gff_updated)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
12 }
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
13
7
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
14 get_domain_clusters_alt <- function(gff, dist_models, threshold=0.99){
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
15 gff <- sort(gff, by = ~ seqnames * start)
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
16 domain_pairs <- data.frame(
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
17 D1 = paste0(head(gff$Name,-1),"_S"),
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
18 D2 = paste0(gff$Name[-1],"_S"),
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
19 C1 = head(gff$Final_Classification,-1),
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
20 C2 = gff$Final_Classification[-1],
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
21 S1 = head(strand(gff),-1),
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
22 S2 = strand(gff)[-1],
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
23 start1 = head(start(gff),-1),
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
24 start2 = start(gff)[-1],
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
25 chrpos= paste0(seqnames(gff)[-1],"_",start(gff)[-1])
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
26 )
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
27 domain_pairs$D_A <- with(domain_pairs, ifelse(D1 < D2, D1, D2))
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
28 domain_pairs$D_B <- with(domain_pairs, ifelse(D1 > D2, D1, D2))
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
29 domain_pairs$quantile <- 1
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
30 same_element_cluster <- domain_pairs$S1 == domain_pairs$S2 & domain_pairs$C1 == domain_pairs$C2
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
31 domain_pairs$same_element_cluster <- same_element_cluster
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
32
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
33 q_plus_function <- apply(domain_pairs[same_element_cluster,],
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
34 1,
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
35 function(x) dist_models$plus[[x["C1"]]][[x["D_A"]]][[x["D_B"]]])
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
36
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
37 D <- abs(domain_pairs[same_element_cluster,]$start1 - domain_pairs[same_element_cluster,]$start2)
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
38
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
39 q_plus_value <- mapply(function(x, D)if(is.null(x)){0}else{x(D)}, x= q_plus_function, D = D)
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
40
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
41 domain_pairs$quantile[same_element_cluster] <- q_plus_value
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
42
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
43 domain_pairs$split_position <- !same_element_cluster | domain_pairs$quantile > threshold
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
44 # combine clusters based on distances and original cluster
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
45 clusters <- paste(cumsum(c(TRUE, domain_pairs$split_position)), get_domain_clusters(gff))
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
46 return(clusters)
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
47 }
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
48
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
49
0
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
50 get_domain_clusters <- function(gff) {
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
51 # get consecutive domains from same linage
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
52 # must be sorted!
7
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
53 plus_order_split <- c(0, as.numeric(cumsum(head(gff$domain_order, -1) >= gff$domain_order[-1] & strand(gff)[-1] == '+')))
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
54 minus_order_split <- rev(as.numeric(cumsum(rev(c(gff$domain_order[-1] >= head(gff$domain_order,-1) & strand(gff)[-1] == '-', FALSE)))))
0
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
55 # split based on classification - must be same and consecutive
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
56 x <- rle(gff$Final_Classification)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
57 # split on strand change
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
58 s <- rep(seq_along(runLength(strand(gff))), runLength(strand(gff)))
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
59 domain_cluster <- paste0(rep(seq_along(x$lengths), x$lengths), "_", seqnames(gff),
7
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
60 "_", plus_order_split, "_", minus_order_split, "_", s)
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
61 clusters <- factor(domain_cluster, levels = unique(domain_cluster))
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
62 return(clusters)
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
63 }
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
64
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
65 # create partial TE from clusters
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
66 get_partial_te_from_cluster_of_domains <- function (gpart){
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
67 ID <- paste("TE_partial_", gpart$Cluster[1], sep="")
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
68 te_partial <- GRanges(type="transposable_element_partial",
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
69 strand=strand(gpart)[1],
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
70 ID = ID,
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
71 source = "dante_ltr",
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
72 seqnames=seqnames(gpart)[1],
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
73 Final_Classification=gpart$Final_Classification[1],
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
74 Name=gpart$Final_Classification[1],
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
75 IRanges(min(start(gpart)), max(end(gpart))))
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
76 gpart$Parent <- ID
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
77 return(c(te_partial,gpart))
0
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
78 }
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
79
7
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
80 # create partial TE from clusters
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
81 get_partial_te_from_cluster_of_domains <- function(gpart, ID) {
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
82 te_partial <- makeGRangesFromDataFrame(
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
83 data.frame(type = "transposable_element_partial",
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
84 strand = gpart$strand[1],
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
85 ID = ID,
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
86 source = "dante_ltr",
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
87 seqnames = gpart$seqnames[1],
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
88 Final_Classification =
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
89 gpart$Final_Classification[1],
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
90 Name = gpart$Final_Classification[1],
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
91 IRanges(min(gpart$start), max(gpart$end))),
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
92 keep.extra.columns = TRUE)
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
93
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
94
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
95 gpart$Parent <- ID
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
96 gpart_gr = makeGRangesFromDataFrame(gpart, keep.extra.columns = TRUE)
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
97 return(c(te_partial, gpart_gr))
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
98 }
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
99
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
100 # function to count for each element number of occurences:
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
101 count_occurences_for_each_element <- function(x) {
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
102 counts_unique <- table(x)
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
103 counts <- counts_unique[x]
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
104 counts
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
105 }
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
106
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
107
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
108 clean_domain_clusters <- function(gcl, lineage_domain_span, min_domains) {
0
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
109 ## remove clusters wich does not have enough domains or domains
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
110 ## are on different strand
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
111 N_domains <- sapply(gcl, nrow)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
112 N_unique_domains <- sapply(gcl, function(x)length(unique(x$Name)))
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
113 S <- sapply(gcl, function(x)paste(sort(unique(x$strand)), collapse = " "))
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
114 S_OK <- S %in% c("+", "-")
7
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
115 max_span <- lineage_domain_span[sapply(gcl, function(x) x$Final_Classification[1])]
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
116 # set to zero if lineage is not covered in lineage domain span
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
117 max_span[is.na(max_span)] <- 0
0
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
118 span <- sapply(gcl, function(x)max(x$end) - min(x$start))
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
119 cond1 <- S_OK &
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
120 N_unique_domains == N_domains &
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
121 N_domains >= min_domains &
7
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
122 span <= max_span
0
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
123 return(gcl[cond1])
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
124 }
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
125
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
126 check_ranges <- function(gx, s, offset = OFFSET) {
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
127 # check is range is not out of sequence length
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
128 START <- sapply(gx, function(x)min(x$start)) - offset
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
129 END <- sapply(gx, function(x)max(x$end)) + offset
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
130 MAX <- seqlengths(s)[sapply(gx, function(x)as.character(x$seqnames[1]))]
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
131 good_ranges <- (START > 0) & (END <= MAX)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
132 return(good_ranges)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
133 }
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
134
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
135 get_ranges <- function(gx, offset = OFFSET) {
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
136 S <- sapply(gx, function(x)min(x$start))
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
137 E <- sapply(gx, function(x)max(x$end))
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
138 gr <- GRanges(seqnames = sapply(gx, function(x)x$seqnames[1]), IRanges(start = S - offset, end = E + offset))
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
139 }
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
140
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
141 get_ranges_left <- function(gx, offset = OFFSET, offset2 = 300) {
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
142 S <- sapply(gx, function(x)min(x$start))
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
143 max_offset <- S - sapply(gx, function(x)min(x$upstream_domain))
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
144 offset_adjusted <- ifelse(max_offset < offset, max_offset, offset)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
145 gr <- GRanges(seqnames = sapply(gx, function(x)x$seqnames[1]), IRanges(start = S - offset_adjusted, end = S + offset2))
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
146 return(gr)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
147 }
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
148
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
149 get_ranges_right <- function(gx, offset = OFFSET, offset2 = 300) {
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
150 E <- sapply(gx, function(x)max(x$end))
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
151 max_offset <- sapply(gx, function(x)max(x$downstream_domain)) - E
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
152 offset_adjusted <- ifelse(max_offset < offset, max_offset, offset)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
153 gr <- GRanges(seqnames = sapply(gx, function(x)x$seqnames[1]), IRanges(start = E - offset2, end = E + offset_adjusted))
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
154 return(gr)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
155 }
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
156
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
157 firstTG <- function(ss) {
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
158 x <- matchPattern("TG", ss)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
159 if (length(x) == 0) {
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
160 return(0)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
161 }else {
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
162 return(min(start(x)))
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
163 }
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
164 }
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
165
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
166 lastCA <- function(ss) {
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
167 x <- matchPattern("CA", ss)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
168 if (length(x) == 0) {
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
169 return(0)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
170 }else {
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
171 return(max(start(x)))
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
172 }
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
173 }
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
174
7
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
175 domain_distance <- function(d_query, d_reference){
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
176 if (d_query == d_reference){
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
177 return (0)
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
178 }
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
179 d_query_p <- strsplit(d_query," ")[[1]]
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
180 d_reference_p <- strsplit(d_reference," ")[[1]]
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
181 d <- length(d_reference_p) - sum(d_query_p == d_reference_p[d_reference_p %in% d_query_p])
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
182 return(d)
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
183 }
2
f131886ea194 "planemo upload commit 891bfe9acf7349c2b887aff6d7e52a7f4ebf3b3a"
petr-novak
parents: 0
diff changeset
184
f131886ea194 "planemo upload commit 891bfe9acf7349c2b887aff6d7e52a7f4ebf3b3a"
petr-novak
parents: 0
diff changeset
185
0
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
186 trim2TGAC <- function(bl) {
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
187 for (i in 1:nrow(bl)) {
2
f131886ea194 "planemo upload commit 891bfe9acf7349c2b887aff6d7e52a7f4ebf3b3a"
petr-novak
parents: 0
diff changeset
188 cons <- consensusString(c(bl$qseq[i], bl$sseq[i]), ambiguityMap="?")
f131886ea194 "planemo upload commit 891bfe9acf7349c2b887aff6d7e52a7f4ebf3b3a"
petr-novak
parents: 0
diff changeset
189 tg_P <- firstTG(cons)
f131886ea194 "planemo upload commit 891bfe9acf7349c2b887aff6d7e52a7f4ebf3b3a"
petr-novak
parents: 0
diff changeset
190 ca_P <- lastCA(cons)
f131886ea194 "planemo upload commit 891bfe9acf7349c2b887aff6d7e52a7f4ebf3b3a"
petr-novak
parents: 0
diff changeset
191 e_dist <- bl$length[i] - ca_P
f131886ea194 "planemo upload commit 891bfe9acf7349c2b887aff6d7e52a7f4ebf3b3a"
petr-novak
parents: 0
diff changeset
192 max_dist = 50 # was 25
f131886ea194 "planemo upload commit 891bfe9acf7349c2b887aff6d7e52a7f4ebf3b3a"
petr-novak
parents: 0
diff changeset
193 no_match <- any(tg_P == 0, ca_P == 0)
0
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
194 if (!no_match &
2
f131886ea194 "planemo upload commit 891bfe9acf7349c2b887aff6d7e52a7f4ebf3b3a"
petr-novak
parents: 0
diff changeset
195 tg_P < max_dist &
f131886ea194 "planemo upload commit 891bfe9acf7349c2b887aff6d7e52a7f4ebf3b3a"
petr-novak
parents: 0
diff changeset
196 e_dist < max_dist) {
0
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
197 ## trim alignment
2
f131886ea194 "planemo upload commit 891bfe9acf7349c2b887aff6d7e52a7f4ebf3b3a"
petr-novak
parents: 0
diff changeset
198 bl[i,] <- trim_blast_table(bl[i,], tg_P, e_dist - 1)
0
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
199 }
2
f131886ea194 "planemo upload commit 891bfe9acf7349c2b887aff6d7e52a7f4ebf3b3a"
petr-novak
parents: 0
diff changeset
200
0
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
201 }
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
202 return(bl)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
203 }
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
204
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
205 trim_blast_table <- function(b, T1, T2) {
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
206 b$qstart <- b$qstart + T1
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
207 b$sstart <- b$sstart + T1
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
208 b$qend <- b$qend - T2
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
209 b$send <- b$send - T2
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
210 b$sseq <- substring(b$sseq, T1, b$length - T2)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
211 b$qseq <- substring(b$qseq, T1, b$length - T2)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
212 b$length <- nchar(b$sseq)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
213 return(b)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
214 }
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
215
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
216 blast_all2all <- function(seqs, db=NULL, ncpus=1, word_size=20, perc_identity=90, max_target_seq = 5000, task = "megablast", additional_params= ""){
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
217 if (ncpus == 1){
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
218 query <- list(seqs)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
219 }else{
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
220 query <-split(seqs, round(seq(1,ncpus,length.out = length(seqs))))
7
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
221 if (length(query) < ncpus){
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
222 ncpus <- length(query)
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
223 }
0
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
224 }
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
225 if(is.null(db)){
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
226 # search against itself
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
227 db <- seqs
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
228 }
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
229 qf <-tempfile(fileext=paste0("_",1:ncpus,".fasta"))
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
230 outf <-tempfile(fileext=paste0("_",1:ncpus,".csv"))
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
231 dbf <- tempfile()
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
232 script <- tempfile()
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
233 writeXStringSet(db, dbf)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
234 mapply(query, qf, FUN = writeXStringSet)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
235 cols <- "qaccver saccver pident length mismatch gapopen qstart qend sstart send evalue bitscore qlen slen"
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
236 cmd_db <- paste("makeblastdb -dbtype nucl -in ", dbf)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
237 cmd_blast <- paste("blastn -task ", task, " -word_size", word_size,
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
238 "-outfmt \"6 ", cols, "\" ",
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
239 "-perc_identity", perc_identity, " -min_raw_gapped_score 500",
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
240 "-max_target_seqs", max_target_seq, additional_params,
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
241 "-query", qf, "-db", dbf, "-out", outf,
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
242 "&"
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
243 )
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
244
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
245 # TODO - inspect only forward strand??
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
246 system(cmd_db)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
247 cmd_all <- paste(paste(cmd_blast,collapse="\n"),"\nwait")
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
248 cat(cmd_all, file = script)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
249 system(paste("sh ", script))
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
250
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
251 bl_list <- lapply(outf, read.table, stringsAsFactors = FALSE, col.names = unlist(strsplit(cols, " ")), sep="\t", comment.char = "")
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
252 bl_table <- do.call(rbind, bl_list)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
253 unlink(qf)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
254 #unlink(outf)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
255 unlink(dbf)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
256 unlink(script)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
257 return(bl_table)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
258 }
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
259
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
260 identify_conflicts <- function (bl){
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
261 QL <- gsub(".+[|]", "", bl$qaccver)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
262 SL <- gsub(".+[|]", "", bl$saccver)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
263 id_with_conflict <- unique(c(bl$qaccver[QL != SL],bl$saccver[QL != SL]))
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
264 id_ok <- setdiff(unique(c(bl$qaccver,bl$saccver)), id_with_conflict)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
265 single_hit <- sapply(
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
266 sapply(
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
267 split(bl$qaccver, bl$saccver), unique
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
268 ), length) == 1
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
269 id_with_no_hits <- names(single_hit)[single_hit] # except hit to itself
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
270 return(list(
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
271 ok = id_ok,
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
272 conflict = id_with_conflict,
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
273 no_hit = id_with_no_hits)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
274 )
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
275 }
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
276
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
277
7
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
278 analyze_TE <- function(seqs, ncpus = 10, word_size = 20, perc_identity = 90, ...){
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
279 blt <- blast_all2all(seqs, ncpus = ncpus, word_size = word_size, perc_identity = perc_identity, ...)
0
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
280 te_conflict_info <- identify_conflicts(blt)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
281 blt_te_ok <- blast_table_subset(blt, te_conflict_info$ok)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
282 te_ok_lineages <- split(blt_te_ok,
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
283 gsub(
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
284 ".+[|]",
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
285 "",
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
286 blt_te_ok$qaccver))
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
287 gr_representative <- GRangesList(mclapply(te_ok_lineages,
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
288 FUN = get_representative_ranges,
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
289 mc.cores = ncpus
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
290 ))
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
291 seqs_representative <- getSeq(seqs, Reduce(c, gr_representative))
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
292 names(seqs_representative) <- seqnames(Reduce(c, gr_representative))
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
293
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
294 return(
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
295 list(
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
296 seqs_representative = seqs_representative,
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
297 te_conflict_info = te_conflict_info,
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
298 gr_representative = gr_representative,
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
299 blast = blt
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
300 )
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
301 )
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
302 }
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
303
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
304 query_coverage <- function(blt){
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
305 blt <- blt[blt$qaccver != blt$saccver,]
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
306 Q_lengths <- blt$qlen[!duplicated(blt$qaccver)]
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
307 names(Q_lengths) <- blt$qaccver[!duplicated(blt$qaccver)]
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
308 gr <- GRanges(seqnames = blt$qaccver, seqlengths = Q_lengths,
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
309 IRanges(start = blt$qstart, end = blt$qend))
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
310 return(coverage(gr))
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
311 }
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
312
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
313 multiplicity_of_te <- function(blt){
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
314 # exclude self to self hits and calculate coverage + mean_multiplicity of TE
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
315 # assuption is that TE which are 'identical' to other TE from the same lineage are
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
316 # likely correct
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
317 blt_no_self <- blt[blt$qaccver != blt$saccver,]
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
318 cvr <- query_coverage(blt_no_self)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
319 L <- sapply(cvr, function(x) sum(width(x)))
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
320 C1 <- sapply(cvr, function(x) sum(as.numeric(runValue(x) >= 1) * runLength(x)))
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
321 multiplicity <- sapply(cvr, function(x) sum(as.numeric(runValue(x)) * runLength(x)))/L
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
322 data.frame(L = L, C1 = C1, multiplicity = multiplicity )
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
323 }
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
324
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
325 verify_based_on_multiplicity <- function(TE_info, min_coverage=0.99, min_multiplicity=3){
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
326 blt <- TE_info$blast[TE_info$blast$qaccver %in% TE_info$te_conflict_info$ok,]
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
327 mp <- multiplicity_of_te(blt)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
328 id_ok_mp_verified <- rownames(mp)[mp$C1/mp$L > min_coverage & mp$multiplicity >= min_multiplicity]
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
329 return(list(multiplicity = mp,
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
330 id_ok_mp_verified = id_ok_mp_verified))
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
331
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
332 }
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
333
7
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
334 compare_TE_datasets <- function(Q, S, word_size = 20, min_coverage = 0.95, ncpus=10, ...){
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
335 blt <- blast_all2all(seqs = Q, db = S, ncpus = ncpus, word_size = word_size, ...)
0
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
336 QL <- gsub(".+[|]", "", blt$qaccver)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
337 SL <- gsub(".+[|]", "", blt$saccver)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
338 id_with_conflict <- unique(c(blt$qaccver[QL != SL]))
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
339 id_ok <- setdiff(unique(blt$qaccver), id_with_conflict)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
340 # check coverage hits
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
341 blt_ok <- blt[blt$qaccver %in% id_ok,]
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
342 Q_lengths <- blt_ok$qlen[!duplicated(blt_ok$qaccver)]
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
343 names(Q_lengths) <- blt_ok$qaccver[!duplicated(blt_ok$qaccver)]
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
344 gr <- GRanges(seqnames = blt_ok$qaccver, seqlengths = Q_lengths,
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
345 IRanges(start = blt_ok$qstart, end = blt_ok$qend))
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
346 cvr <- coverage(gr)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
347 L <- sapply(cvr, function(x) sum(width(x)))
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
348 C1 <- sapply(cvr, function(x) sum(as.numeric(runValue(x) >= 1) * runLength(x)))
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
349 Max_uncovered <- sapply(cvr, function(x){
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
350 if(any(runValue(x)==0)){
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
351 return(max(runLength(x)[runValue(x) == 0]))
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
352 }else{
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
353 return(0)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
354 }
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
355 })
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
356
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
357 # verified based on hit to reference - S
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
358 C1_prop <- C1/L
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
359 pass <- (C1_prop >= min_coverage & Max_uncovered < 500)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
360 if (any(pass)){
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
361 id_ok_verified <- names(C1_prop)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
362 }else {
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
363 id_ok_verified <- NULL
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
364 }
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
365 return(list(id_with_conflict = id_with_conflict,
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
366 id_ok = id_ok,
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
367 id_ok_verified = id_ok_verified
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
368 ))
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
369 }
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
370
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
371
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
372
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
373 blast_table_subset <- function(bl,id){
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
374 return(bl[bl$qaccver %in% id & bl$saccver %in% id,, drop = FALSE])
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
375 }
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
376
3
6ae4a341d1f3 "planemo upload commit 8bd6029a4de4a8f5031a5cc71303bb06217cc88a"
petr-novak
parents: 2
diff changeset
377 get_representative_ranges <- function(bl, min_length = 200, min_identity = 98){
6ae4a341d1f3 "planemo upload commit 8bd6029a4de4a8f5031a5cc71303bb06217cc88a"
petr-novak
parents: 2
diff changeset
378 bl <- bl[bl$pident>=min_identity, , drop=FALSE]
6ae4a341d1f3 "planemo upload commit 8bd6029a4de4a8f5031a5cc71303bb06217cc88a"
petr-novak
parents: 2
diff changeset
379 bl <- bl[bl$pident>=min_identity & bl$length >= min_length, , drop=FALSE]
0
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
380 score <- sort(unlist(by(bl$bitscore, bl$qaccver, sum, simplify = FALSE)),
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
381 decreasing = TRUE)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
382 L <- bl$qlen[!duplicated(bl$qaccver)]
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
383 names(L) <- bl$qaccver[!duplicated(bl$qaccver)]
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
384 gr <- GRanges(seqnames = bl$qaccver,
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
385 IRanges(start = bl$qstart, end = bl$qend),
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
386 seqlengths = L,
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
387 subject = bl$saccver,
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
388 sstart = ifelse(bl$send < bl$sstart, bl$send, bl$sstart),
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
389 send = ifelse(bl$send > bl$sstart, bl$send, bl$sstart))
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
390 SN <- levels(seqnames(gr))
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
391 inc <- rep(TRUE, length(gr))
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
392 MSK <- GRangesList()
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
393 for (i in names(score)){
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
394 inc[gr$subject %in% i] <- FALSE
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
395 gr_part <- gr[seqnames(gr) %in% i & inc]
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
396 MSK[[i]] <- GRanges(seqnames = factor(gr_part$subject, levels = SN),
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
397 IRanges(start = gr_part$sstart, end = gr_part$send),
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
398 seqlengths = L
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
399 )
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
400 }
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
401 gout <- unlist(MSK)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
402
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
403 full_gr <- GRanges(seqnames = factor(SN, levels = SN),
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
404 IRanges(start = rep(1,length(L)),
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
405 end = L)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
406 )
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
407 unmasked_gr <- GenomicRanges::setdiff(full_gr, gout)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
408 return(unmasked_gr[width(unmasked_gr) >= min_length])
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
409 }
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
410
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
411 expected_diversity <- function(seqs, niter=100, km = 6){
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
412 L <- nchar(seqs)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
413 R <- matrix(ncol = niter, nrow = length(seqs))
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
414 for (i in 1:niter){
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
415 seqs_rnd <- DNAStringSet(sapply(L, function(n) paste(sample(c("A", "C", "T", "G"), n, replace=TRUE), collapse="")))
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
416 R[,i] <- seq_diversity(seqs_rnd, km = km)$richness
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
417 }
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
418 R
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
419
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
420 }
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
421
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
422 seq_diversity <- function (seqs, km=6){
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
423 K <- oligonucleotideFrequency(seqs, width=km)>0
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
424 P <- t(K)/rowSums(K)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
425 # shannon index
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
426 SI <- apply(P, 2, function(x) {x1 <- x[x>0]; -sum(x1*log(x1))})
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
427 # richness
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
428 R <- rowSums(K)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
429 list(richness=R, shannon_index=SI)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
430 }
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
431
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
432
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
433
7
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
434 blast <- function(s1, s2, expected_aln_lenght=NULL) {
0
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
435 tmp1 <- tempfile()
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
436 tmp2 <- tempfile()
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
437 tmp_out <- tempfile()
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
438 writeXStringSet(DNAStringSet(s1), tmp1)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
439 writeXStringSet(DNAStringSet(s2), tmp2)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
440 # alternative blast:
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
441 cmd <- paste("blastn -task blastn -word_size 7 -dust no -gapextend 1 -gapopen 2 -reward 1 -penalty -1",
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
442 " -query ", tmp1, ' -subject ', tmp2, ' -strand plus ',
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
443 '-outfmt "6 qaccver saccver pident length mismatch gapopen qstart qend sstart send evalue bitscore qseq sseq"',
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
444 ' -out', tmp_out)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
445
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
446 system(cmd)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
447 out_raw <- read.table(tmp_out, as.is = TRUE, sep = "\t",
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
448 col.names = strsplit("qaccver saccver pident length mismatch gapopen qstart qend sstart send evalue bitscore qseq sseq", split = ' ')[[1]])
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
449
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
450 if (nrow(out_raw) == 0) {
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
451 return(out_raw)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
452 }
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
453 out <- trim2TGAC(out_raw)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
454 # remove alingment shorted that
7
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
455 # out <- out[out$length > 100,]
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
456 # alngment must be at least 80% of expected LTRmin95
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
457 out <- out[out$length > (expected_aln_lenght *0.8),]
0
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
458 if (nrow(out) == 0) {
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
459 return(out)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
460 }
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
461 ## filter for TGCA
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
462 TG_L <- substring(out$qseq, 1, 2) == "TG"
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
463 TG_R <- substring(out$sseq, 1, 2) == "TG"
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
464 CA_L <- substring(out$qseq, out$length - 1, out$length) == "CA"
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
465 CA_R <- substring(out$sseq, out$length - 1, out$length) == "CA"
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
466 cond <- TG_L & TG_R & CA_L & CA_R
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
467 out <- out[cond, , drop = FALSE]
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
468 unlink(tmp1)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
469 unlink(tmp2)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
470 unlink(tmp_out)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
471 # TODO - detele all temp files!
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
472 # unlink(tmp1, tmp2, tmp_out)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
473 return(out)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
474 }
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
475
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
476 get_correct_coordinates <- function(b) {
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
477 do.call(rbind, strsplit(b$qaccver, split = "_"))
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
478 }
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
479
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
480 evaluate_ltr <- function(GR, GRL, GRR, blast_line, Lseq, Rseq) {
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
481 LTR_L <- makeGRangesFromDataFrame(data.frame(seqnames = seqnames(GR),
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
482 start = start(GRL) + blast_line$qstart - 2,
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
483 end = start(GRL) + blast_line$qend - 1))
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
484 LTR_R <- makeGRangesFromDataFrame(data.frame(seqnames = seqnames(GR),
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
485 start = start(GRR) + blast_line$sstart - 2,
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
486 end = start(GRR) + blast_line$send - 1))
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
487
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
488 TSD_L <- makeGRangesFromDataFrame(data.frame(seqnames = seqnames(GR),
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
489 start = start(GRL) + blast_line$qstart - 3:10,
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
490 end = start(GRL) + blast_line$qstart - 3))
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
491 TSD_R <- makeGRangesFromDataFrame(data.frame(seqnames = seqnames(GR),
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
492 start = start(GRR) + blast_line$send,
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
493 end = start(GRR) + blast_line$send + 0:7))
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
494
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
495 TSD_L_seq <- DNAStringSet(substring(Lseq, blast_line$qstart - 2:9, blast_line$qstart - 2))
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
496 TSD_R_seq <- DNAStringSet(substring(Rseq, blast_line$send + 1, blast_line$send + 1:8))
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
497
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
498 matching_tsd <- TSD_R_seq == TSD_L_seq
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
499 matching_tsd[1:3] <- FALSE # remove short tsd
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
500 p <- which(matching_tsd)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
501 if (length(p) > 0) {
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
502 TSD_Length <- max(p)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
503 TSD_sequence <- TSD_L_seq[TSD_Length]
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
504 TSD_position <- append(TSD_R[TSD_Length], TSD_L[TSD_Length])
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
505 }else {
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
506 TSD_Length <- 0
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
507 TSD_sequence <- ""
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
508 TSD_position <- NA
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
509 }
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
510
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
511 TE_Length <- end(LTR_R) - start(LTR_L)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
512 LTR_Identity <- blast_line$pident
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
513 out <- list(TSD_position = TSD_position, TSD_sequence = TSD_sequence, TSD_Length = TSD_Length,
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
514 LTR_R_position = LTR_R, LTR_L_position = LTR_L, TE_Length = TE_Length, LTR_Identity = LTR_Identity)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
515 return(out)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
516 }
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
517
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
518 get_best_ltr <- function(x) {
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
519 tsd_ok <- sapply(x, function(k)k$TSD_Length > 3)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
520 te_length_ok <- sapply(x, function(k)k$TE_Length < 30000)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
521 ltr_length_ok <- sapply(x, function(k)width(k$LTR_R_position) >= 100 & width(k$LTR_L_position) >= 100)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
522 if (sum(tsd_ok & te_length_ok & ltr_length_ok) >= 1) {
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
523 # return the first one (best bitscore)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
524 return(x[tsd_ok & te_length_ok][1])
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
525 }
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
526 if (any(te_length_ok & ltr_length_ok)) {
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
527 return(x[te_length_ok & ltr_length_ok][1])
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
528 }else {
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
529 return(NULL)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
530 }
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
531 }
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
532
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
533 get_te_gff3 <- function(g, ID) {
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
534 D <- makeGRangesFromDataFrame(g$domain, keep.extra.columns = TRUE)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
535 sn <- seqnames(D)[1]
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
536 S <- strand(D)[1]
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
537 TE <- GRanges(seqnames = sn,
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
538 IRanges(start = start(g$ltr_info[[1]]$LTR_L_position),
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
539 end = end(g$ltr_info[[1]]$LTR_R_position)), strand = S)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
540 TE$type <- "transposable_element"
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
541 TE$ID <- ID
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
542
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
543 if (as.character(S) == "+") {
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
544 LTR_5 <- g$ltr_info[[1]]$LTR_L
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
545 LTR_3 <- g$ltr_info[[1]]$LTR_R
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
546 }else {
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
547 LTR_3 <- g$ltr_info[[1]]$LTR_L
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
548 LTR_5 <- g$ltr_info[[1]]$LTR_R
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
549 }
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
550 LTR_5$LTR <- '5LTR'
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
551 LTR_3$LTR <- '3LTR'
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
552 LTR_5$type <- "long_terminal_repeat"
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
553 LTR_3$type <- "long_terminal_repeat"
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
554 strand(LTR_3) <- S
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
555 strand(LTR_5) <- S
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
556 LTR_3$Parent <- ID
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
557 LTR_5$Parent <- ID
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
558 LTR_3$Final_Classification <- D$Final_Classification[1]
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
559 LTR_5$Final_Classification <- D$Final_Classification[1]
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
560 LTR_5$LTR_Identity <- g$ltr_info[[1]]$LTR_Identity
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
561 LTR_3$LTR_Identity <- g$ltr_info[[1]]$LTR_Identity
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
562
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
563 TE$LTR_Identity <- g$ltr_info[[1]]$LTR_Identity
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
564 TE$LTR5_length <- width(LTR_5)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
565 TE$LTR3_length <- width(LTR_3)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
566
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
567 if (is.na(g$ltr_info[[1]]$TSD_position)[1]) {
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
568 # no TSD found
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
569 TSD <- NULL
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
570 TE$TSD <- 'not_found'
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
571 }else {
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
572 TSD <- g$ltr_info[[1]]$TSD_position
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
573 TSD$type <- "target_site_duplication"
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
574 TSD$Parent <- ID
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
575 TE$TSD <- as.character(g$ltr_info[[1]]$TSD_sequence)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
576 }
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
577
7
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
578 TE$Name <- TE$Final_Classification <- D$Final_Classification[1]
0
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
579
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
580 D$Parent <- ID
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
581 out <- c(TE, LTR_3, LTR_5, D, TSD)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
582 return(out)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
583 }
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
584
7
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
585 get_TE <- function(Lseq, Rseq, domains_gff, GR, GRL, GRR,LTR_length) {
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
586 xx <- blast(Lseq, Rseq, LTR_length)
0
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
587 if (nrow(xx) == 0) {
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
588 return(NULL)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
589 }else {
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
590 ltr_tmp <- list()
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
591 for (j in 1:nrow(xx)) {
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
592 ltr_tmp[[j]] <- evaluate_ltr(GR, GRL, GRR, xx[j, , drop = FALSE], Lseq, Rseq)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
593 }
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
594 ltr <- get_best_ltr(ltr_tmp)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
595 if (length(ltr) == 0) {
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
596 return(NULL)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
597 ## add good ltr
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
598 }else {
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
599 return(list(domain = domains_gff, ltr_info = ltr, blast_out = xx))
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
600 }
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
601 }
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
602 }
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
603
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
604 add_pbs <- function(te, s, trna_db) {
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
605 ltr5 <- te[which(te$LTR == "5LTR")]
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
606 STRAND <- as.character(strand(te)[1])
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
607 if (STRAND == "+") {
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
608 pbs_gr <- GRanges(seqnames(ltr5), IRanges(start = end(ltr5) + 1, end = end(ltr5) + 31))
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
609 pbs_s <- reverseComplement(getSeq(s, pbs_gr))
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
610 }else {
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
611 pbs_gr <- GRanges(seqnames(ltr5), IRanges(end = start(ltr5) - 1, start = start(ltr5) - 30))
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
612 pbs_s <- getSeq(s, pbs_gr)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
613 }
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
614
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
615 names(pbs_s) <- "pbs_region"
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
616 # find trna match
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
617 tmp <- tempfile()
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
618 tmp_out <- tempfile()
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
619 writeXStringSet(DNAStringSet(pbs_s), tmp)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
620 # alternative blast:
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
621 cmd <- paste("blastn -task blastn -word_size 7 -dust no -perc_identity 100",
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
622 " -query ", tmp, ' -db ', trna_db, ' -strand plus ',
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
623 '-outfmt "6 qaccver saccver pident length mismatch gapopen qstart qend sstart send evalue bitscore qseq sseq"',
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
624 ' -out', tmp_out)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
625
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
626 system(cmd)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
627 pbs_exact_gr <- NULL
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
628 out_raw <- read.table(tmp_out, as.is = TRUE, sep = "\t",
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
629 col.names = strsplit(
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
630 "qaccver saccver pident length mismatch gapopen qstart qend sstart send evalue bitscore qseq sseq",
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
631 split = ' ')[[1]])
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
632 if (nrow(out_raw) > 0) {
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
633 cca <- grepl("CCA$", out_raw$qseq)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
634 to_end <- out_raw$send == 23 # align to end of sequence
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
635 max_dist <- out_raw$qend > 25 # max 5 bp from ltr
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
636 min_length <- out_raw$length >= 10
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
637 out_pass <- out_raw[cca & to_end & max_dist & min_length,]
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
638 if (nrow(out_pass) > 0) {
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
639 trna_id <- out_pass$saccver[1]
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
640 if (STRAND == "+") {
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
641 S <- end(ltr5) + 32 - out_pass$qend[1]
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
642 E <- end(ltr5) + 32 - out_pass$qstart[1]
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
643 }else {
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
644 S <- start(ltr5) - 31 + out_pass$qstart[1]
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
645 E <- start(ltr5) - 31 + out_pass$qend[1]
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
646 }
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
647 pbs_exact_gr <- GRanges(seqnames(ltr5), IRanges(start = S, end = E))
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
648 pbs_exact_gr$trna_id <- trna_id
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
649 pbs_exact_gr$Length <- out_pass$Length
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
650 strand(pbs_exact_gr) <- STRAND
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
651 pbs_exact_gr$type <- 'primer_binding_site'
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
652 pbs_exact_gr$Parent <- te[1]$ID
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
653 te$trna_id <- c(trna_id, rep(NA, length(te) - 1))
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
654
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
655 }
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
656 }
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
657 te <- append(te, pbs_exact_gr)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
658 unlink(tmp)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
659 unlink(tmp_out)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
660 return(te)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
661 }
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
662
2
f131886ea194 "planemo upload commit 891bfe9acf7349c2b887aff6d7e52a7f4ebf3b3a"
petr-novak
parents: 0
diff changeset
663
f131886ea194 "planemo upload commit 891bfe9acf7349c2b887aff6d7e52a7f4ebf3b3a"
petr-novak
parents: 0
diff changeset
664 get_te_rank <- function (gr){
f131886ea194 "planemo upload commit 891bfe9acf7349c2b887aff6d7e52a7f4ebf3b3a"
petr-novak
parents: 0
diff changeset
665 DL_id <- gr$ID[gr$type == "transposable_element" &
f131886ea194 "planemo upload commit 891bfe9acf7349c2b887aff6d7e52a7f4ebf3b3a"
petr-novak
parents: 0
diff changeset
666 gr$TSD == "not_found" &
f131886ea194 "planemo upload commit 891bfe9acf7349c2b887aff6d7e52a7f4ebf3b3a"
petr-novak
parents: 0
diff changeset
667 is.na(gr$trna_id)]
f131886ea194 "planemo upload commit 891bfe9acf7349c2b887aff6d7e52a7f4ebf3b3a"
petr-novak
parents: 0
diff changeset
668 DLT_id <- gr$ID[gr$type == "transposable_element" &
f131886ea194 "planemo upload commit 891bfe9acf7349c2b887aff6d7e52a7f4ebf3b3a"
petr-novak
parents: 0
diff changeset
669 gr$TSD != "not_found" &
f131886ea194 "planemo upload commit 891bfe9acf7349c2b887aff6d7e52a7f4ebf3b3a"
petr-novak
parents: 0
diff changeset
670 is.na(gr$trna_id)]
f131886ea194 "planemo upload commit 891bfe9acf7349c2b887aff6d7e52a7f4ebf3b3a"
petr-novak
parents: 0
diff changeset
671 DLTP_id <- gr$ID[gr$type == "transposable_element" &
f131886ea194 "planemo upload commit 891bfe9acf7349c2b887aff6d7e52a7f4ebf3b3a"
petr-novak
parents: 0
diff changeset
672 gr$TSD != "not_found" &
f131886ea194 "planemo upload commit 891bfe9acf7349c2b887aff6d7e52a7f4ebf3b3a"
petr-novak
parents: 0
diff changeset
673 !is.na(gr$trna_id)]
f131886ea194 "planemo upload commit 891bfe9acf7349c2b887aff6d7e52a7f4ebf3b3a"
petr-novak
parents: 0
diff changeset
674 DLP_id <- gr$ID[gr$type == "transposable_element" &
f131886ea194 "planemo upload commit 891bfe9acf7349c2b887aff6d7e52a7f4ebf3b3a"
petr-novak
parents: 0
diff changeset
675 gr$TSD == "not_found" &
f131886ea194 "planemo upload commit 891bfe9acf7349c2b887aff6d7e52a7f4ebf3b3a"
petr-novak
parents: 0
diff changeset
676 !is.na(gr$trna_id)]
f131886ea194 "planemo upload commit 891bfe9acf7349c2b887aff6d7e52a7f4ebf3b3a"
petr-novak
parents: 0
diff changeset
677 Rank <- character(length(gr))
f131886ea194 "planemo upload commit 891bfe9acf7349c2b887aff6d7e52a7f4ebf3b3a"
petr-novak
parents: 0
diff changeset
678 ID <- unlist(ifelse(is.na(gr$ID), gr$Parent, gr$ID))
f131886ea194 "planemo upload commit 891bfe9acf7349c2b887aff6d7e52a7f4ebf3b3a"
petr-novak
parents: 0
diff changeset
679
f131886ea194 "planemo upload commit 891bfe9acf7349c2b887aff6d7e52a7f4ebf3b3a"
petr-novak
parents: 0
diff changeset
680 Rank[ID %in% DL_id] <- "DL"
f131886ea194 "planemo upload commit 891bfe9acf7349c2b887aff6d7e52a7f4ebf3b3a"
petr-novak
parents: 0
diff changeset
681 Rank[ID %in% DLT_id] <- "DLT"
f131886ea194 "planemo upload commit 891bfe9acf7349c2b887aff6d7e52a7f4ebf3b3a"
petr-novak
parents: 0
diff changeset
682 Rank[ID %in% DLP_id] <- "DLP"
f131886ea194 "planemo upload commit 891bfe9acf7349c2b887aff6d7e52a7f4ebf3b3a"
petr-novak
parents: 0
diff changeset
683 Rank[ID %in% DLTP_id] <- "DLTP"
f131886ea194 "planemo upload commit 891bfe9acf7349c2b887aff6d7e52a7f4ebf3b3a"
petr-novak
parents: 0
diff changeset
684 return(Rank)
f131886ea194 "planemo upload commit 891bfe9acf7349c2b887aff6d7e52a7f4ebf3b3a"
petr-novak
parents: 0
diff changeset
685 }
f131886ea194 "planemo upload commit 891bfe9acf7349c2b887aff6d7e52a7f4ebf3b3a"
petr-novak
parents: 0
diff changeset
686
7
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
687
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
688 get_te_statistics <- function(gr, RT){
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
689 Ranks <- c("D", "DL", "DLT", "DLP", "DLTP")
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
690 all_class <- names(sort(table(RT$Final_Classification), decreasing = TRUE))
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
691 RT_domain <- as.integer(table(factor(RT$Final_Classification, levels = all_class)))
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
692 rank_table <- list()
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
693 for (i in 1:length(Ranks)) {
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
694 gr_part <- gr[gr$type == "transposable_element" & gr$Rank == Ranks[i]]
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
695 rank_table[[Ranks[i]]] <- as.integer(table(factor(gr_part$Final_Classification, levels = all_class)))
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
696
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
697 }
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
698 out <- cbind(do.call(cbind, rank_table), RT_domain=RT_domain)
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
699 out <- rbind(out, Total = colSums(out))
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
700 rownames(out) <- c(all_class, "Total")
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
701 return(out)
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
702 }
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
703
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
704
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
705 get_te_statistics_old <- function(gr, RT) {
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
706 DOMAINS <- gr[gr$type == "transposable_element" & gr$Rank == "D"]
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
707 DOMAINS_LTR <- gr[gr$type == "transposable_element" & gr$Rank == "DL"]
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
708 DOMAINS_LTR_TSD <- gr[gr$type == "transposable_element" & gr$Rank == "DLT"]
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
709 DOMAINS_LTR_TSD_PBS <- gr[gr$type == "transposable_element" & gr$Rank == "DLTP"]
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
710 DOMAINS_LTR_PBS <- gr[gr$type == "transposable_element" & gr$Rank == "DLP"]
0
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
711
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
712 all_class <- names(sort(table(RT$Final_Classification), decreasing = TRUE))
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
713 RT_domain <- as.integer(table(factor(RT$Final_Classification, levels = all_class)))
7
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
714 D <- as.integer(table(factor(DOMAINS$Final_Classification, levels = all_class)))
0
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
715 DL <- as.integer(table(factor(DOMAINS_LTR$Final_Classification, levels = all_class)))
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
716 DLT <- as.integer(table(factor(DOMAINS_LTR_TSD$Final_Classification, levels = all_class)))
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
717 DLTP <- as.integer(table(factor(DOMAINS_LTR_TSD_PBS$Final_Classification, levels = all_class)))
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
718 DLP <- as.integer(table(factor(DOMAINS_LTR_PBS$Final_Classification, levels = all_class)))
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
719 out <- data.frame(RT_domain = RT_domain,
7
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
720 DOMAINS = D,
0
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
721 DOMAINS_LTR = DL,
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
722 DOMAINS_LTR_TSD = DLT,
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
723 DOMAINS_LTR_PBS = DLP,
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
724 DOMAINS_LTR_TSD_PBS = DLTP,
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
725 row.names = all_class
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
726 )
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
727 total <- colSums(out)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
728 out <- rbind(out, Total = total)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
729 return(out)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
730 }
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
731
3
6ae4a341d1f3 "planemo upload commit 8bd6029a4de4a8f5031a5cc71303bb06217cc88a"
petr-novak
parents: 2
diff changeset
732 getSeqNamed <- function(s, gr, name = NULL) {
0
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
733 spart <- getSeq(s, gr)
3
6ae4a341d1f3 "planemo upload commit 8bd6029a4de4a8f5031a5cc71303bb06217cc88a"
petr-novak
parents: 2
diff changeset
734 if (is.null(name)){
6ae4a341d1f3 "planemo upload commit 8bd6029a4de4a8f5031a5cc71303bb06217cc88a"
petr-novak
parents: 2
diff changeset
735 id1 <- paste0(seqnames(gr), '_', start(gr), "_", end(gr))
6ae4a341d1f3 "planemo upload commit 8bd6029a4de4a8f5031a5cc71303bb06217cc88a"
petr-novak
parents: 2
diff changeset
736 }else{
6ae4a341d1f3 "planemo upload commit 8bd6029a4de4a8f5031a5cc71303bb06217cc88a"
petr-novak
parents: 2
diff changeset
737 id1 <- mcols(gr)[,name]
6ae4a341d1f3 "planemo upload commit 8bd6029a4de4a8f5031a5cc71303bb06217cc88a"
petr-novak
parents: 2
diff changeset
738 }
0
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
739 id2 <- gr$Final_Classification
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
740 names(spart) <- paste0(id1, "#", id2)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
741 spart
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
742 }
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
743
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
744 get_TE_id <- function (gr){
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
745 gr_te <- gr[gr$type == "transposable_element"]
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
746 ID <- gr_te$ID
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
747 A <- paste0(seqnames(gr_te), '_', start(gr_te), "_", end(gr_te))
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
748 B <- gr_te$Final_Classification
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
749 names(ID) <- paste0(A, "#", B)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
750
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
751 }
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
752
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
753
7
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
754 get_te_sequences <- function (gr, s) {
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
755 Ranks <- c("D", "DL", "DLT", "DLP", "DLTP")
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
756 s_te <- list()
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
757 for (i in 1:length(Ranks)) {
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
758 gr_te <- gr[gr$type == "transposable_element" & gr$Rank == Ranks[i]]
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
759 if (length(gr_te) > 0) {
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
760 s_te[[Ranks[i]]] <- getSeqNamed(s, gr_te)
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
761 }
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
762 }
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
763 return(s_te)
0
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
764 }
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
765
7
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
766
0
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
767 cd_hit_est <- function(seqs, min_identity = 0.9, word_size = 10, ncpu = 2){
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
768 # runs cd-hi-est and return table with cluster membership, and size and if reads was repesentative
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
769 # input sequences must be in the same orientation!!!
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
770 sfile <- tempfile()
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
771 fasta_out <- tempfile()
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
772 clstr <- paste0(fasta_out,".clstr")
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
773 # cdhit is triming names!!
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
774 ori_names <- names(seqs)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
775 names(seqs) <- seq_along(seqs)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
776 writeXStringSet(seqs, sfile)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
777 cmd <- paste("cd-hit-est",
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
778 "-i", sfile,
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
779 "-o", fasta_out,
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
780 "-c", min_identity,
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
781 "-n", word_size,
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
782 "-T", ncpu,
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
783 "-r", 0)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
784 system(cmd)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
785 cls_raw <- grep("^>", readLines(clstr), invert = TRUE, value = TRUE)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
786 unlink(fasta_out)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
787 unlink(clstr)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
788 index <- gsub("\t.+","",cls_raw)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
789 id <- as.numeric(gsub("[.].+","",
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
790 gsub(".+>", "", cls_raw))
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
791 )
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
792 is_representative <- id %in% id[grep("[*]$",cls_raw)]
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
793 membership <- cumsum(index=="0")
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
794 cluster_size <- tabulate(membership)[membership]
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
795 # reorder
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
796 ord <- order(id)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
797 cls_info <- data.frame(
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
798 seq_id = ori_names,
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
799 membership = membership[ord],
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
800 cluster_size = cluster_size[ord],
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
801 is_representative = is_representative[ord]
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
802 )
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
803 return(cls_info)
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
804 }
7b0bbe7477c4 "planemo upload commit 92c684dff3b377c8c08654c7f3d46a133385e3e0-dirty"
petr-novak
parents:
diff changeset
805
7
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
806
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
807 dante2dist <- function(dc){
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
808 # dc - cluster of domains, granges
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
809 dpos <- get_dom_pos(dc)
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
810 D <- c(dist(dpos))
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
811 NN <- combn(names(dpos), 2)
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
812 # order feature in pair alphabeticaly
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
813 N1 <- ifelse(NN[1,]<NN[2,], NN[1,], NN[2, ])
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
814 N2 <- ifelse(NN[1,]>NN[2,], NN[1,], NN[2, ])
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
815 dfd <- data.frame(F1 = N1, F2 = N2, distance = D)
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
816 }
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
817
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
818
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
819
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
820 dante_to_quantiles <- function(dc, model_function, lineage=NULL){
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
821 dfd <- dante2dist(dc)
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
822 if (is.null(lineage)){
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
823 lineage <- dc$Final_Classification[1]
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
824 }
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
825 # check if lineage has defined ecdf
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
826 if (lineage %in% names(model_function$plus)){
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
827 fn <- model_function$plus[[lineage]]
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
828 fnm <- model_function$minus[[lineage]]
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
829 dstat1 <- mapply(mapply(dfd$F1, dfd$F2, FUN=function(a, b)fn[[a]][[b]]), dfd$distance, FUN=function(f, x)f(x))
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
830 dstat2 <- mapply(mapply(dfd$F1, dfd$F2, FUN=function(a, b)fnm[[a]][[b]]), dfd$distance, FUN=function(f, x)f(-x))
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
831 dout <- cbind(dfd, dstat1, dstat2, dstat12 = ifelse(dstat1>dstat2, dstat2, dstat1))
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
832 return(dout)
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
833 }else{
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
834 return(NULL)
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
835 }
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
836 }
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
837
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
838 get_dom_pos <- function(g){
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
839 if (length(g) == 0){
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
840 return(NULL)
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
841 }
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
842 gdf <- data.frame(rexdb = as.character(seqnames(g)),
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
843 domain = g$Name,
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
844 S = start(g),
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
845 E = end(g),
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
846 stringsAsFactors = FALSE)
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
847 gSmat <- xtabs(S ~ rexdb + domain, data = gdf)
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
848 colnames(gSmat) <- paste0(colnames(gSmat),"_S")
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
849 gEmat <- xtabs(E ~ rexdb + domain, data = gdf)
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
850 colnames(gEmat) <- paste0(colnames(gEmat),"_E")
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
851 SEmat <- cbind(gSmat,gEmat)
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
852 # reorder
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
853 dom_position <- colMeans(SEmat)
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
854 SEmat_sorted <- SEmat[,order(dom_position)]
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
855 return(SEmat_sorted)
c33d6583e548 "planemo upload commit 50884f7f0269a0bbde078f24fe5020975693bcd9"
petr-novak
parents: 3
diff changeset
856 }