0
|
1 #! /usr/bin/env Rscript
|
|
2 # repeat masker summary
|
|
3 # analysis of *.out file
|
|
4 # input arguments: <rmsk_result.out> <reads.fas.
|
|
5 # calculates totoal proportion of matched repetetive families and averagee score and print the table
|
|
6
|
14
|
7 opt = list()
|
|
8 opt$fasta = commandArgs(T)[1]
|
|
9 opt$repeat_masker = commandArgs(T)[2]
|
|
10 opt$output = commandArgs(T)[3]
|
0
|
11
|
|
12
|
|
13 RMfiles=system(paste("ls ",opt$repeat_masker,sep=""),intern=T)
|
|
14 fasta=system(paste("ls ",opt$fasta,sep=""),intern=T)
|
|
15
|
|
16
|
|
17
|
|
18 getsubdir=function(x){
|
14
|
19 xx=gsub(".*/","",x)
|
|
20 sdir=gsub(xx,"",x,fixed=T)
|
0
|
21 sdir
|
|
22 }
|
|
23
|
|
24 fasta.summary=function(reads){
|
|
25 suppressPackageStartupMessages(library(Biostrings,quiet=T))
|
|
26 if (exists("readDNAStringSet")){
|
|
27 seqs=readDNAStringSet(reads)
|
|
28 }else{
|
|
29 seqs=read.DNAStringSet(reads)
|
|
30 }
|
|
31
|
|
32 N=length(seqs)
|
|
33 Ls=nchar(seqs)
|
|
34 L=sum(Ls)
|
|
35 M=median(Ls)
|
|
36 A=mean(Ls)
|
|
37 output=c(N,L,M,A)
|
|
38 names(output)=c("N","length","median","mean")
|
|
39 output
|
|
40 }
|
|
41
|
|
42 repCont=function(repname){ # uses external variable!
|
|
43 pos=repname==classfamily
|
|
44 RC=unname(sum(lengths[pos])/totalLength)
|
|
45 RC
|
|
46 }
|
|
47 repScore=function(repname){ # uses external variable!
|
|
48 pos=repname==classfamily
|
|
49 S=unname(mean(score[pos]))
|
|
50 S
|
|
51 }
|
|
52
|
|
53 N=length(RMfiles)
|
|
54
|
|
55 for (i in seq_along(RMfiles)){
|
|
56 cat(paste((RMfiles[i]),"\n"))
|
|
57
|
|
58 sdir=getsubdir(RMfiles[i])
|
|
59 seqs.summary=fasta.summary(fasta[i])
|
|
60 totalLength=seqs.summary["length"]
|
|
61
|
|
62 total.counts=data.frame(c("All_Reads_Length","All_Reads_Number"),c(NA,NA),c(NA,NA),c(totalLength,seqs.summary['N']),c(totalLength,seqs.summary['N']),c(NA,NA))
|
|
63 colnames(total.counts)=c("class/fammily","class","family","hits","content","Mean_Score")
|
|
64
|
|
65
|
|
66 # get RM data if not empty
|
|
67 rmsk_empty=length(grep("There were no repetitive sequences detected",scan(RMfiles[i],what=character(),n=1,sep="\n",quiet=T),fixed=T))>0
|
|
68 rmsk_notperformed = length(grep("No Repbase used with RepetMasker",scan(RMfiles[i],what=character(),n=1,sep="\n",quiet=T),fixed=T))>0
|
|
69 if (!rmsk_empty & !rmsk_notperformed) {
|
|
70 rmsk=read.table(RMfiles[i],header=F,as.is=T,skip=2,comment.char="*",fill=T)
|
|
71 score=rmsk[,1]
|
|
72 Ids=rmsk[,5]
|
|
73 starts=rmsk[,6]
|
|
74 ends=rmsk[,7]
|
|
75 lengths=ends-starts
|
|
76 classfamily=rmsk[,11]
|
|
77 out.table=table(classfamily)
|
|
78 out.table=data.frame(names(out.table),c(out.table),stringsAsFactors=F)
|
|
79 contents=sapply(out.table[,1],repCont)*100
|
|
80 meanScore=sapply(out.table[,1],repScore)
|
|
81 class=gsub("/.*","",out.table[,1])
|
|
82 families=gsub(".*/","",out.table[,1])
|
|
83 out.table=data.frame(out.table[,1],class,families,out.table[,-1],contents,meanScore,stringsAsFactors=F)
|
|
84 colnames(out.table)=c("class/fammily","class","family","hits","content","Mean_Score")
|
|
85 out.table=out.table[order(out.table$content,decreasing=T),]
|
|
86 out.table=rbind(out.table,total.counts)
|
|
87 }else{
|
|
88 out.table=total.counts
|
|
89 }
|
|
90 out.table=out.table[order(out.table$content,decreasing=T),]
|
|
91
|
|
92 write.table(out.table,file=paste(sdir,opt$output,sep=""),row.names=F,quote=F,sep="\t")
|
|
93
|
|
94
|
|
95 # merge all tables:
|
|
96
|
|
97 colnames(out.table)=paste(colnames(out.table),c("",rep(sdir,5)))
|
|
98
|
|
99 if (i==1) {
|
|
100 mergedTable=out.table[,c(1,4,5,6)]
|
|
101 }else{
|
|
102
|
|
103 mergedTable=merge(mergedTable,out.table[,c(1,4,5,6)],by=1,all=T)
|
|
104 }
|
|
105
|
|
106 }
|
|
107 mergedTable=mergedTable[order(rowSums(mergedTable[,seq(2,N*3,by=3),drop=FALSE],na.rm=T),decreasing=T),]
|
|
108 mergedTable[is.na(mergedTable)]<-0
|
|
109
|
|
110 write.table(t(mergedTable),file=paste(opt$output,"summary.csv",sep=""),col.names=F,row.names=T,quote=F,sep="\t")
|
|
111
|
|
112 #save.image("tmp.RData")
|
|
113
|
|
114
|
|
115
|
|
116
|
|
117
|
|
118
|
|
119
|
|
120
|