data=read.table("all_gene.abu",row.names=1,header=1,sep='\t')
head(data)
niche=read.table("positive_select_gene.tsv",row.names=1,header=1)
library(reshape2)
niche[is.na(niche)]=0
data2=apply(data,2,as.numeric)
rownames(data2)=rownames(data)
colnames(data2)=colnames(data)
sum=apply(data2,1,sum)
data=data2[sum>5,]
head(data)
data=as.data.frame(data)
data$gene=rownames(data)
niche$gene=rownames(niche)
data_melt=as.data.frame(melt(data))
niche_melt=as.data.frame(melt(niche))
rownames(data_melt)=paste(data_melt[,1],data_melt[,2],sep='_')
rownames(niche_melt)=paste(niche_melt[,1],niche_melt[,2],sep='_')
niche_melt=niche_melt[niche_melt[,3]>0,]
data_melt=data_melt[data_melt[,3]>5,]
head(niche_melt)
head(data_melt)
abu_and_niche=data.frame(niche_melt,data_melt[as.character(rownames(niche_melt)),3])
colnames(abu_and_niche)[4]="rpkm"
write.table(abu_and_niche,"postive_niche_abundance.tsv",sep='\t',quote=F)
