10
|
1 ## summarize hits
|
|
2 output = commandArgs(T)[2] ## output table
|
|
3 filepath = commandArgs(T)[1] ## input dante gff3
|
12
|
4 if (length(commandArgs(T))==2){
|
|
5 summarized_by = NA
|
|
6 }else{
|
|
7 summarized_by = strsplit(commandArgs(T)[-(1:2)], split = ",")[[1]]
|
|
8 }
|
10
|
9
|
|
10 readGFF3fromDante = function(filepath){
|
|
11 dfraw=read.table(filepath, as.is = TRUE)
|
|
12 gff_df = dfraw[,1:8]
|
|
13 colnames(gff_df) = c("seqid", "source", "type", "start", "end", "score",
|
|
14 "strand", "phase")
|
|
15 ## assume same order, same attributes names
|
12
|
16 ## TODO make ti more robust - order can change!
|
|
17 gffattr_list = lapply(
|
|
18 strsplit(dfraw[,9],split=c("=|;")),
|
|
19 function(x)x[c(FALSE,TRUE)]
|
|
20 )
|
|
21 ## some rows are not complete - in case of ambiguous domains
|
|
22 L = sapply(gffattr_list, length)
|
|
23 short = L < max(L)
|
|
24 if (any(short)){
|
|
25 gffattr_list[short] = lapply(gffattr_list[short],function(x) c(x, rep(NA, 13 - length(x))))
|
|
26 }
|
13
|
27 gffattr = as.data.frame(do.call(rbind, gffattr_list), stringsAsFactors = FALSE)
|
12
|
28
|
|
29 ## get attributes names
|
|
30 attrnames = strsplit(dfraw[1,9],split=c("=|;"))[[1]][c(TRUE,FALSE)]
|
|
31 colnames(gffattr) = attrnames
|
|
32
|
|
33 gff_df$Final_Classification = gffattr$Final_Classification
|
|
34 gff_df$Name = gffattr$Name
|
|
35 gff_df$Region_Hits_Classifications = gffattr$Region_Hits_Classifications
|
|
36 gff_df$Best_Hit = gffattr$Best_Hit
|
|
37 gff_df$Best_Hit_DB_Pos = gffattr$Best_Hir_DB_Pos
|
|
38 gff_df$DB_Seq = gffattr$DB_Seq
|
|
39 gff_df$Query_Seq = gffattr$Query_Seq
|
|
40 gff_df$Region_Seq = gffattr$Region_Seq
|
|
41 gff_df$Identity = as.numeric(gffattr$Identity)
|
|
42 gff_df$Similarity = as.numeric(gffattr$Similarity)
|
|
43 gff_df$Relat_Length = as.numeric(gffattr$Relat_Length)
|
|
44 gff_df$Relat_Interruptions = as.numeric(gffattr$Relat_Interruptions)
|
|
45 gff_df$Hit_to_DB_Length = as.numeric(gffattr$Hit_to_DB_Length)
|
10
|
46 return(gff_df)
|
|
47 }
|
|
48
|
|
49 gff = readGFF3fromDante(filepath)
|
12
|
50 # summarized_by = c("Final_Classification", "Name", "seqid")
|
|
51 # summarized_by = c("Final_Classification")
|
|
52
|
10
|
53
|
12
|
54 if (is.na(summarized_by)){
|
|
55 ## export complete table
|
|
56 write.table(gff, file = output, row.names = FALSE, quote = FALSE, sep = "\t")
|
|
57 }else{
|
|
58 ## export summary
|
|
59 tbl = data.frame(table(gff[, summarized_by]))
|
|
60 write.table(tbl, file = output, row.names = FALSE, quote = FALSE, sep = "\t")
|
|
61 }
|