WGCNA衍生:差异基因取一步邻居(蛋白互作)

#在string网络中取差异基因的一步邻居
DEGs<-read.delim(file.choose(),header=T,sep="\t") #file=1.7-protein_coding_?.txt
pc<-DEGs[DEGs$Gene.type=='protein_coding',][,1]
head(pc)
#如果是list,取邻居报错
typeof(pc);pc<-as.vector(unlist(pc));length(pc)

#自定义取一邻居函数
getneighbor<-function(gene,net){
  neighbor=c()
  for (i in gene){
    neighbor=c(neighbor,which(net[i,]>0))
  }
  neighbor=colnames(net)[unique(neighbor)]
}

library(igraph)
string<-read.table(file.choose(),header=F,sep="\t") # file=string_700_si.txt
g<-graph.data.frame(string,directed = F)
#g<-simplify(g0) 

net<-get.adjacency(g)

nodes<-1:nrow(net)
names(nodes)<-rownames(net)

#在PPI中的DEGs
PPI_pc<-intersect(names(nodes),pc);length(PPI_pc)
write.table(paste('PCgene_in_net','Neighbor',sep = '\t'),"8.1-CC_pc-edges_0.05_2.txt",append=T,row.names=F,col.names = F,quote = F)

for (i in 1:length(PPI_pc)) {
  Nei=getneighbor(c(PPI_pc[i]),net)
  write.table(paste(PPI_pc[i],Nei,sep = '\t'),"8.1-CC_pc-edges_0.05_2.txt",append=T,row.names=F,col.names = F,quote = F)
}

Nei<-getneighbor(PPI_pc,net);length(Nei)

wgcna<-union(pc,Nei);length(wgcna)

wgcna<-as.matrix(wgcna);colnames(wgcna)<-'W_gene';head(wgcna)

#WGCNA matrix
matrix<-read.table(file = file.choose(),header = T,sep = '\t')
head(matrix)
co_expression<-merge(wgcna,matrix,by.x = 'W_gene',by.y = 'Symbol')
head(co_expression);dim(co_expression)

write.table(co_expression,"6-CC_PPI_WGCNA.txt",row.names = F,quote = F,sep="\t")

你可能感兴趣的:(WGCNA衍生:差异基因取一步邻居(蛋白互作))