Mercurial > repos > petr-novak > repeat_annotation_pipeline3
comparison clean_rm_output.R @ 0:ea6a3059a6af draft
Uploaded
author | petr-novak |
---|---|
date | Mon, 18 Oct 2021 11:01:20 +0000 |
parents | |
children | 814cba36e435 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:ea6a3059a6af |
---|---|
1 #!/usr/bin/env Rscript | |
2 suppressPackageStartupMessages(library(rtracklayer)) | |
3 | |
4 gff_cleanup = function(gff){ | |
5 ## remove overlapin annotation track - assign new annot | |
6 gff_disjoin = disjoin(gff, with.revmap=TRUE) | |
7 ## append annotation: | |
8 gff_names = mclapply(as.list(gff_disjoin$revmap), FUN = function(x)gff$Name[x], mc.cores = 8) | |
9 gff_strands = mclapply(as.list(gff_disjoin$revmap), FUN = function(x)strand(gff[x]), mc.cores = 8) | |
10 new_annot = sapply(sapply(gff_names, unique), paste, collapse="|") | |
11 new_annot_uniq = unique(new_annot) | |
12 lca_annot = sapply(strsplit(new_annot_uniq, "|", fixed = TRUE), resolve_name) | |
13 names(lca_annot) = new_annot_uniq | |
14 new_annot_lca = lca_annot[new_annot] | |
15 #new_annot_lca = sapply(sapply(gff_names, unique), resolve_name) | |
16 strand_attribute = sapply(sapply(gff_strands, unique), paste, collapse="|") | |
17 gff_disjoin$strands=strand_attribute | |
18 gff_disjoin$source="RM" | |
19 gff_disjoin$type="repeat" | |
20 gff_disjoin$score=NA | |
21 gff_disjoin$phase=NA | |
22 gff_disjoin$Name=new_annot_lca | |
23 gff_disjoin$Original_names=new_annot | |
24 gff_disjoin$revmap=NULL | |
25 return(gff_disjoin) | |
26 } | |
27 | |
28 resolve_name=function(x){ | |
29 if (length(x)==1){ | |
30 # no conflict | |
31 return(x) | |
32 } else{ | |
33 y = sapply(x, strsplit, split="/", fixed = TRUE) | |
34 ny = table(unlist(sapply(y, function(x)paste(seq_along(x),x)))) | |
35 if (max(ny)<length(x)){ | |
36 return("Unknown") | |
37 }else{ | |
38 k = which(ny==length(x)) | |
39 r = max(as.numeric((gsub(" .+","",names(k))))) | |
40 out = paste(y[[1]][1:r], collapse="/") | |
41 return(out) | |
42 } | |
43 } | |
44 } | |
45 | |
46 | |
47 | |
48 infile = commandArgs(T)[1] | |
49 outfile = commandArgs(T)[2] | |
50 | |
51 ## infile = "./test_data/raw_rm.out" | |
52 | |
53 rm_out = read.table(infile, as.is=TRUE, sep="", skip = 2, fill=TRUE, header=FALSE) | |
54 | |
55 gff = GRanges(seqnames = rm_out$V5, ranges = IRanges(start = rm_out$V6, end=rm_out$V7)) | |
56 | |
57 # repeat class after # symbol - syntax 1 | |
58 gff$Name=rm_out$V11 | |
59 | |
60 ## is repeat type is specifies by double underscore: | |
61 ## then rm_out$V11 is unspecified | |
62 if (any(rm_out$V11 == "Unspecified")){ | |
63 ## set Name from prefix | |
64 ## TODO | |
65 inc = rm_out$V11 == "Unspecified" | |
66 Name = gsub("__.+","",rm_out$V10) | |
67 # chanche Usnpsecified to new name | |
68 gff$Name[inc] = Name | |
69 } | |
70 | |
71 | |
72 ## join neighbors with the same annotation, disregard strand! | |
73 result <- unlist(reduce(split(gff, gff$Name))) | |
74 | |
75 result$Name <- names(result) | |
76 | |
77 result_clean = gff_cleanup(result) | |
78 | |
79 ## TODO | |
80 ## identify conflicting annotation, replace by LCA but keep origin list of classifications | |
81 | |
82 gff_out = sortSeqlevels(result_clean) | |
83 gff_out = sort(gff_out) | |
84 gff_out$type = "repeat_region" | |
85 gff_out$source = "RepeatMasker_parsed" | |
86 gff_out$ID=paste0(gff_out$Name, "_", seq_along(gff_out$Name)) | |
87 export(gff_out, format = "gff3", con=outfile) | |
88 | |
89 |