forked from anigwe/ord_abund_raretaxa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCRT_Functions_v1.1.R
134 lines (98 loc) · 4 KB
/
CRT_Functions_v1.1.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#####
#16 Oct 2014 bug fix. ALS. MaxRel filter was updated. Also added option: can discover of CRT based on MaxRel calculated from dataset with all OTUs OR dataset with only non-singleton OTUs.
####
#function to make relative abundance table - load into workspace
makeRFtable.f=function(data){
cSum1<-colSums(data)
#define an empty matrix to put your RF values into
newdata<-matrix(0,dim(data)[1], dim(data)[2])
#Assign the same column and row names to the new matrix.
colnames(newdata)<-colnames(data)
rownames(newdata)<-rownames(data)
#Each cell will be divided by the column sum.
for (i in 1:length(data)){
newdata[,i] <- data[,i]/cSum1[i]
}
return(newdata)
}
###
#Rare to Prevalent OTUs
SimpleRareToPrev.f=function(otu_fp,abund_thresh = 0.005, abund_thresh_ALL=FALSE,b_thresh = 0.90, rdp_lastcol=TRUE){
#Read in files
otu=read.table(otu_fp, header=TRUE, check.names=FALSE, row.names=1, sep="\t")
#If provided, remove rdp ids and save
if(rdp_lastcol==TRUE){
rdp=otu[,ncol(otu)]
otu=otu[,-ncol(otu)]
}
#remove empty rows
tmp=otu[rowSums(otu)>0,]
no.otus=nrow(tmp)
if(rdp_lastcol==TRUE){
rdp2=rdp[rowSums(otu)>0]
}
#Remove singletons
otu.nosigs=tmp[rowSums(tmp)>1,]
if(rdp_lastcol==TRUE){
rdp3=rdp2[rowSums(tmp)>1]
}else{
rdp3=NULL
}
#how many are left after singletons?
no.sigs=nrow(otu.nosigs)
#Make a rel abundance table - with the full dataset
otu.rel=makeRFtable.f(tmp)
#reduce the rel. abundance table to omit singletons
otu.rel2=otu.rel[is.element(row.names(otu.rel), row.names(otu.nosigs)),]
#loop for each OTU to calculate Coefficent of bimodality
#For efficiency, loops through singleton-omitted dataset (singletons will not have rare-to-prevalent dynamics)
out=NULL
for(j in 1:nrow(otu.nosigs)){
x=as.numeric(otu.nosigs[j,])
k=kurtosis(x)
s=skewness(x)
#calculate the coefficient of bimodality for each OTU
b=(1+(s^2))/(k+3)
#determine whether OTU max and median relative abundance (based on full dataset, otu.rel)
x2=as.numeric(otu.rel[j,])
mx.rel.all=max(x2)
med.rel.all=median(x2)
x3=as.numeric(otu.rel2[j,])
mx.rel.ns=max(x3)
med.rel.ns=median(x3)
out=rbind(out,c(row.names(otu.nosigs)[j],b,mx.rel.all, med.rel.all, mx.rel.ns, med.rel.ns))
}
#print(dim(out))
#print(head(out))
out=as.data.frame(out)
colnames(out)=c("OTUID","CoefficientOfBimodality","MaxRel_All", "MedianRel_All", "MaxRel_NoSingletons", "MedianRel_NoSingletons")
if(rdp_lastcol==TRUE){
out=cbind(out, rdp3)
colnames(out)[7]="TaxonomicAssignment"
}
#Filter 1: for at least one rel. abundance greater than abund_thresh (default = 0.005). The default uses the whole dataset MaxRel (abund_thresh_ALL=TRUE), another option is the singleton-removed dataset.
if(abund_thresh_ALL==TRUE){
at="ALL"
out.filter=out[as.numeric(as.vector(out[,"MaxRel_All"])) >= abund_thresh,]
print(dim(out.filter))
}else{
at="NOSIG"
out.filter=out[as.numeric(as.vector(out[,"MaxRel_NoSingletons"])) >= abund_thresh,]
print(dim(out.filter))
}
#Filter 2: for coefficient of bimodality greater than b_thresh (default = 0.90)
out.filter=out.filter[as.numeric(as.vector(out.filter[,"CoefficientOfBimodality"])) >= b_thresh,]
print(dim(out.filter))
write.table(out.filter, paste("ResultsFile_ConditionallyRareOTUID_", abund_thresh, "_", b_thresh, "_", at, ".txt", sep=""), quote=FALSE, sep="\t", row.names=FALSE)
print("No. conditionally rare OTUs")
print(nrow(out.filter))
print("No. total OTUs")
print(no.otus)
print("Proportion conditional rare / total OTUs")
print(nrow(out.filter)/no.otus)
print("No singleton OTUs")
print(no.sigs)
print("Proportion conditionally rare / non-singletonOTUs")
print(nrow(out.filter)/no.sigs)
return(out.filter)
}