annotate clean_rm_output.R @ 8:2557cad81607 draft

Uploaded
author petr-novak
date Wed, 03 May 2023 11:12:41 +0000
parents 7f1032da7a0a
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
1 #!/usr/bin/env Rscript
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
2 suppressPackageStartupMessages(library(rtracklayer))
1
814cba36e435 Uploaded
mvdbeek
parents: 0
diff changeset
3 suppressPackageStartupMessages(library(parallel))
814cba36e435 Uploaded
mvdbeek
parents: 0
diff changeset
4
0
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
5
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
6 gff_cleanup = function(gff){
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
7 ## remove overlapin annotation track - assign new annot
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
8 gff_disjoin = disjoin(gff, with.revmap=TRUE)
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
9 ## append annotation:
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
10 gff_names = mclapply(as.list(gff_disjoin$revmap), FUN = function(x)gff$Name[x], mc.cores = 8)
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
11 gff_strands = mclapply(as.list(gff_disjoin$revmap), FUN = function(x)strand(gff[x]), mc.cores = 8)
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
12 new_annot = sapply(sapply(gff_names, unique), paste, collapse="|")
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
13 new_annot_uniq = unique(new_annot)
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
14 lca_annot = sapply(strsplit(new_annot_uniq, "|", fixed = TRUE), resolve_name)
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
15 names(lca_annot) = new_annot_uniq
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
16 new_annot_lca = lca_annot[new_annot]
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
17 #new_annot_lca = sapply(sapply(gff_names, unique), resolve_name)
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
18 strand_attribute = sapply(sapply(gff_strands, unique), paste, collapse="|")
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
19 gff_disjoin$source="RM"
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
20 gff_disjoin$type="repeat"
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
21 gff_disjoin$score=NA
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
22 gff_disjoin$phase=NA
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
23 gff_disjoin$Name=new_annot_lca
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
24 gff_disjoin$Original_names=new_annot
2
7f1032da7a0a Uploaded
petr-novak
parents: 1
diff changeset
25 gff_disjoin$strands=strand_attribute
0
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
26 gff_disjoin$revmap=NULL
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
27 return(gff_disjoin)
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
28 }
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
29
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
30 resolve_name=function(x){
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
31 if (length(x)==1){
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
32 # no conflict
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
33 return(x)
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
34 } else{
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
35 y = sapply(x, strsplit, split="/", fixed = TRUE)
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
36 ny = table(unlist(sapply(y, function(x)paste(seq_along(x),x))))
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
37 if (max(ny)<length(x)){
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
38 return("Unknown")
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
39 }else{
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
40 k = which(ny==length(x))
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
41 r = max(as.numeric((gsub(" .+","",names(k)))))
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
42 out = paste(y[[1]][1:r], collapse="/")
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
43 return(out)
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
44 }
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
45 }
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
46 }
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
47
2
7f1032da7a0a Uploaded
petr-novak
parents: 1
diff changeset
48 convert_names <- function(n, old_sep = "|" , new_sep = "\""){
7f1032da7a0a Uploaded
petr-novak
parents: 1
diff changeset
49 # remove all characters which are new_sep with -
7f1032da7a0a Uploaded
petr-novak
parents: 1
diff changeset
50 n_new = gsub(old_sep, new_sep,
7f1032da7a0a Uploaded
petr-novak
parents: 1
diff changeset
51 gsub(new_sep,"-", n, fixed = TRUE),
7f1032da7a0a Uploaded
petr-novak
parents: 1
diff changeset
52 fixed = TRUE)
7f1032da7a0a Uploaded
petr-novak
parents: 1
diff changeset
53 return(n_new)
7f1032da7a0a Uploaded
petr-novak
parents: 1
diff changeset
54 }
0
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
55
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
56
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
57 infile = commandArgs(T)[1]
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
58 outfile = commandArgs(T)[2]
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
59
2
7f1032da7a0a Uploaded
petr-novak
parents: 1
diff changeset
60
0
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
61 ## infile = "./test_data/raw_rm.out"
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
62
1
814cba36e435 Uploaded
mvdbeek
parents: 0
diff changeset
63 rm_out = read.table(infile, as.is=TRUE, sep="", skip = 2, fill=TRUE, header=FALSE, col.names=paste0("V",1:16))
0
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
64
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
65 gff = GRanges(seqnames = rm_out$V5, ranges = IRanges(start = rm_out$V6, end=rm_out$V7))
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
66
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
67 # repeat class after # symbol - syntax 1
2
7f1032da7a0a Uploaded
petr-novak
parents: 1
diff changeset
68 # detect separator
7f1032da7a0a Uploaded
petr-novak
parents: 1
diff changeset
69 # if "|" is present replace "|" -> "/" and "/" -> "-"
7f1032da7a0a Uploaded
petr-novak
parents: 1
diff changeset
70 if (any(grepl("|", rm_out$V11, fixed = TRUE))){
7f1032da7a0a Uploaded
petr-novak
parents: 1
diff changeset
71 gff$Name <- convert_names(rm_out$V11, old_sep = "|", new_sep = "/")
7f1032da7a0a Uploaded
petr-novak
parents: 1
diff changeset
72 message('replacing classification separator character "|" with "/"')
7f1032da7a0a Uploaded
petr-novak
parents: 1
diff changeset
73 print(gff)
7f1032da7a0a Uploaded
petr-novak
parents: 1
diff changeset
74 }else{
7f1032da7a0a Uploaded
petr-novak
parents: 1
diff changeset
75 gff$Name <- rm_out$V11
7f1032da7a0a Uploaded
petr-novak
parents: 1
diff changeset
76 }
0
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
77
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
78 ## is repeat type is specifies by double underscore:
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
79 ## then rm_out$V11 is unspecified
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
80 if (any(rm_out$V11 == "Unspecified")){
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
81 ## set Name from prefix
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
82 ## TODO
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
83 inc = rm_out$V11 == "Unspecified"
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
84 Name = gsub("__.+","",rm_out$V10)
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
85 # chanche Usnpsecified to new name
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
86 gff$Name[inc] = Name
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
87 }
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
88
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
89
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
90 ## join neighbors with the same annotation, disregard strand!
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
91 result <- unlist(reduce(split(gff, gff$Name)))
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
92
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
93 result$Name <- names(result)
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
94
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
95 result_clean = gff_cleanup(result)
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
96
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
97 ## TODO
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
98 ## identify conflicting annotation, replace by LCA but keep origin list of classifications
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
99
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
100 gff_out = sortSeqlevels(result_clean)
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
101 gff_out = sort(gff_out)
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
102 gff_out$type = "repeat_region"
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
103 gff_out$source = "RepeatMasker_parsed"
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
104 gff_out$ID=paste0(gff_out$Name, "_", seq_along(gff_out$Name))
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
105 export(gff_out, format = "gff3", con=outfile)
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
106
ea6a3059a6af Uploaded
petr-novak
parents:
diff changeset
107