美文网首页
WGCNA衍生:超几何分布识别模块Pivot regulator

WGCNA衍生:超几何分布识别模块Pivot regulator

作者: 挽山 | 来源:发表于2020-06-30 08:01 被阅读0次
    realNet_TF<-read.table(file.choose(),header=T,sep="\t")
    head(realNet_TF);dim(realNet_TF)
    
    realNet_LNC<-read.table(file.choose(),header=T,sep="\t")
    head(realNet_LNC);dim(realNet_LNC)
    
    realNet_miR<-read.table(file.choose(),header=T,sep="\t")
    head(realNet_miR);dim(realNet_miR)
    
    #resOrdered<-as.data.frame(realNet[order(realNet$Score),])
    #head(resOrdered)
    #resOrdered_f<-resOrdered[resOrdered$Methods!='Prediction',] #miR
    #head(resOrdered_f)
    #write.table(resOrdered_f,'RAID_miRNA_mRNA_score-0.5.txt',sep = '\t',row.names = F,quote = F,col.names = colnames(realNet))
    #realNet<-resOrdered_f[,c(6,8)]
    
    realNet_TF<-realNet_TF[,c(2,3)]; head(realNet_TF)
    realNet_LNC<-realNet_LNC[,c(6,8)]; head(realNet_LNC) 
    realNet_miR<-realNet_miR[,c(4,6)]; head(realNet_miR)
    
    realNet_TF<-realNet_TF[,c(1,3)]; head(realNet_TF)
    realNet_LNC<-realNet_LNC[,c(2,8)]; head(realNet_LNC) 
    realNet_miR<-realNet_miR[,c(2,6)]; head(realNet_miR)
    
    net_mRNA_TF<-unique(as.vector(realNet_TF[,2]))
    length(unique(as.vector(realNet_TF[,1])))#543(animal-trrust) 785(TRRUST-TFs) 1371(RAID-lnc) 2596(RAID-miR) 0.5置信node
    length(unique(as.vector(realNet_TF[,2])))#2319(animal-trrust) 7809(RAID-lnc) 15802(RAID-miR) edges
    
    net_mRNA_LNC<-unique(as.vector(realNet_LNC[,2]))
    length(unique(as.vector(realNet_LNC[,1])))
    length(unique(as.vector(realNet_LNC[,2])))
    
    net_mRNA_miR<-unique(as.vector(realNet_miR[,2]))
    length(unique(as.vector(realNet_miR[,1])))
    length(unique(as.vector(realNet_miR[,2])))
    
    library(igraph)
    g_TF<-graph.data.frame(realNet_TF,directed = F)
    g_TF<-simplify(g_TF) 
    net_TF<-get.adjacency(g_TF)
    
    g_LNC<-graph.data.frame(realNet_LNC,directed = F)
    g_LNC<-simplify(g_LNC) 
    net_LNC<-get.adjacency(g_LNC)
    
    g_miR<-graph.data.frame(realNet_miR,directed = F)
    g_miR<-simplify(g_miR) 
    net_miR<-get.adjacency(g_miR)
    
    getNeighbor<-function(gene,net){
      neighbor=c()
      for (i in gene){
        neighbor=c(neighbor,which(net[i,]>0))
      }
      neighbor=colnames(net)[unique(neighbor)]
    }
    
    cc<-c('4','6','17','29')
    pfc<-c('2','6','8')
    
    write.table(paste('sample','module','Gene_id/Mirbase_id', 'Pivot_type','module_links','p_value','target_genes', sep = '\t'),"Pivot regulators of crosstalk modules.txt",append=T,row.names=F,col.names = F,quote = F);
    write.table(paste('sample','module','pivot','m_gene','Pivot_type',sep = '\t'),"all_pivots_edges.txt",append=T,row.names=F,col.names = F,quote = F);
    
    All_module_gene<-read.table(file.choose(),stringsAsFactors = F,header = T) #模块号+基因
    number<-sort(unique(All_module_gene[,1]));number
    head(All_module_gene)
    
    library(tidyr)
    for(j in cc){ #字符时可以in本身,数字时用length
      #j=4
      m<-as.vector(All_module_gene[All_module_gene$module==j,][,2])# 模块基因
      mn<-intersect(net_mRNA_TF,m);length(mn) # 多少在网络里
      m_mi<-getNeighbor(mn,net_TF); length(m_mi) #第一次正向邻居(不同mRNA可能有相同邻居,但R包取邻居不用去重)
      
      nodes=1:nrow(net_TF); #1 to pivot的全部节点数
      names(nodes)=rownames(net_TF);#nodes有两行,上边是名字,下边是本身(序号)
      #mgene=as.data.frame(m);#列名为‘m’
      #modNode=merge(realNet,mgene,by.x ='gene',by.y = 'm' )
      
      for(k in 1:length(m_mi)){
        nodeNei=getNeighbor(c(m_mi[k]),net_TF);# 第二次某pivot反向邻居(调控的全部网络基因)
        sig=intersect(nodeNei,m);#pivot调控的模块基因(一个模块内不用去重)
        neiAndM=length(sig);
        
        if(neiAndM > 1){
          p=1-phyper(neiAndM-1, length(nodeNei),nrow(net_TF)-length(nodeNei),length(mn));
          #(pivot调控模块内基因数-1,pivot反向总调控数,网络总节点数-pivot反向总调控数,模块基因在网数)
          if(p<0.05 & p>0){
            print(neiAndM/length(m))
            
            neiAndM2<-as.data.frame(t(sig)); colnames(neiAndM2)<-c(1:dim(neiAndM2)[2])
            neiAndM3<-unite(neiAndM2,"combine",colnames(neiAndM2), sep="/", remove = F)[,1]
            
            write.table(paste('CC',j,names(nodes[m_mi[k]]),'TF', neiAndM,p,neiAndM3, sep = '\t'),"Pivot regulators of crosstalk modules.txt",append=T,row.names=F,col.names = F,quote = F);
            write.table(paste('CC',j,names(nodes[m_mi[k]]),sig,'TF',sep = '\t'),"all_pivots_edges.txt",append=T,row.names=F,col.names = F,quote = F);
          }
        }
      }
    }
    
    for(j in cc){ #字符时可以in本身,数字时用length
      #j=4
      m<-as.vector(All_module_gene[All_module_gene$module==j,][,2])# 模块基因
      mn<-intersect(net_mRNA_LNC,m);length(mn) # 只是看看多少在网络里
      m_mi<-getNeighbor(mn,net_LNC); length(m_mi) #第一次正向邻居(不同mRNA可能有相同邻居,但R包取邻居不用去重)
      
      nodes=1:nrow(net_LNC);
      names(nodes)=rownames(net_LNC);#nodes有两行,上边是名字,下边是本身(序号)
      #mgene=as.data.frame(m);#列名为‘m’
      #modNode=merge(realNet,mgene,by.x ='gene',by.y = 'm' )
      
      for(k in 1:length(m_mi)){
        nodeNei=getNeighbor(c(m_mi[k]),net_LNC);# 某pivot反向邻居(调控的全部网络基因)
        sig=intersect(nodeNei,m);#调控的模块基因(一个模块内不用去重)
        neiAndM=length(sig);
        
        if(neiAndM > 1){
          p=1-phyper(neiAndM-1, length(nodeNei),nrow(net_LNC)-length(nodeNei),length(mn));
          if(p<0.05 & p>0){
            print(neiAndM/length(m))
            
            neiAndM2<-as.data.frame(t(sig)); colnames(neiAndM2)<-c(1:dim(neiAndM2)[2])
            neiAndM3<-unite(neiAndM2,"combine",colnames(neiAndM2), sep="/", remove = F)[,1]
            
            write.table(paste('CC',j,names(nodes[m_mi[k]]),'LncRNA', neiAndM,p,neiAndM3, sep = '\t'),"Pivot regulators of crosstalk modules.txt",append=T,row.names=F,col.names = F,quote = F);
            write.table(paste('CC',j,names(nodes[m_mi[k]]),sig,'LncRNA',sep = '\t'),"all_pivots_edges.txt",append=T,row.names=F,col.names = F,quote = F);
          }
        }
      }
    }
    
    for(j in cc){ #字符时可以in本身,数字时用length
      #j=4
      m<-as.vector(All_module_gene[All_module_gene$module==j,][,2])# 模块基因
      mn<-intersect(net_mRNA_miR,m);length(mn) # 只是看看多少在网络里
      m_mi<-getNeighbor(mn,net_miR); length(m_mi) #第一次正向邻居(不同mRNA可能有相同邻居,但R包取邻居不用去重)
      
      nodes=1:nrow(net_miR);
      names(nodes)=rownames(net_miR);#nodes有两行,上边是名字,下边是本身(序号)
      #mgene=as.data.frame(m);#列名为‘m’
      #modNode=merge(realNet,mgene,by.x ='gene',by.y = 'm' )
      
      for(k in 1:length(m_mi)){
        nodeNei=getNeighbor(c(m_mi[k]),net_miR);# 某pivot反向邻居(调控的全部网络基因)
        sig=intersect(nodeNei,m);#调控的模块基因(一个模块内不用去重)
        neiAndM=length(sig);
        
        if(neiAndM > 1){
          p=1-phyper(neiAndM-1, length(nodeNei),nrow(net_miR)-length(nodeNei),length(mn));
          if(p<0.05 & p>0){
            print(neiAndM/length(m))
            
            neiAndM2<-as.data.frame(t(sig)); colnames(neiAndM2)<-c(1:dim(neiAndM2)[2])
            neiAndM3<-unite(neiAndM2,"combine",colnames(neiAndM2), sep="/", remove = F)[,1]
            
            write.table(paste('CC',j,names(nodes[m_mi[k]]),'miRNA', neiAndM,p,neiAndM3, sep = '\t'),"Pivot regulators of crosstalk modules.txt",append=T,row.names=F,col.names = F,quote = F);
            write.table(paste('CC',j,names(nodes[m_mi[k]]),sig,'miRNA',sep = '\t'),"all_pivots_edges.txt",append=T,row.names=F,col.names = F,quote = F);
          }
        }
      }
    }
    
    #=================
    All_module_gene<-read.table(file.choose(),stringsAsFactors = F,header = T) #基因+模块号
    number<-sort(unique(All_module_gene[,1]));number
    head(All_module_gene)
    
    for(j in pfc){ #字符时可以in本身,数字时用length
      #j=4
      m<-as.vector(All_module_gene[All_module_gene$module==j,][,2])# 模块基因
      mn<-intersect(net_mRNA_TF,m);length(mn) # 只是看看多少在网络里
      m_mi<-getNeighbor(mn,net_TF); length(m_mi) #第一次正向邻居(不同mRNA可能有相同邻居,但R包取邻居不用去重)
      
      nodes=1:nrow(net_TF);
      names(nodes)=rownames(net_TF);#nodes有两行,上边是名字,下边是本身(序号)
      #mgene=as.data.frame(m);#列名为‘m’
      #modNode=merge(realNet,mgene,by.x ='gene',by.y = 'm' )
      
      for(k in 1:length(m_mi)){
        nodeNei=getNeighbor(c(m_mi[k]),net_TF);# 某pivot反向邻居(调控的全部网络基因)
        sig=intersect(nodeNei,m);#调控的模块基因(一个模块内不用去重)
        neiAndM=length(sig);
        
        if(neiAndM > 1){
          p=1-phyper(neiAndM-1, length(nodeNei),nrow(net_TF)-length(nodeNei),length(mn));
          if(p<0.05 & p>0){
            print(neiAndM/length(m))
            
            neiAndM2<-as.data.frame(t(sig)); colnames(neiAndM2)<-c(1:dim(neiAndM2)[2])
            neiAndM3<-unite(neiAndM2,"combine",colnames(neiAndM2), sep="/", remove = F)[,1]
            
            write.table(paste('PFC',j,names(nodes[m_mi[k]]),'TF', neiAndM,p,neiAndM3, sep = '\t'),"Pivot regulators of crosstalk modules.txt",append=T,row.names=F,col.names = F,quote = F);
            write.table(paste('PFC',j,names(nodes[m_mi[k]]),sig,'TF',sep = '\t'),"all_pivots_edges.txt",append=T,row.names=F,col.names = F,quote = F);
          }
        }
      }
    }
    
    for(j in pfc){ #字符时可以in本身,数字时用length
      #j=5
      m<-as.vector(All_module_gene[All_module_gene$module==j,][,2])# 模块基因
      mn<-intersect(net_mRNA_LNC,m);length(mn) # 只是看看多少在网络里
      m_mi<-getNeighbor(mn,net_LNC); length(m_mi) #第一次正向邻居(不同mRNA可能有相同邻居,但R包取邻居不用去重)
      
      nodes=1:nrow(net_LNC);
      names(nodes)=rownames(net_LNC);#nodes有两行,上边是名字,下边是本身(序号)
      #mgene=as.data.frame(m);#列名为‘m’
      #modNode=merge(realNet,mgene,by.x ='gene',by.y = 'm' )
      
      for(k in 1:length(m_mi)){
        nodeNei=getNeighbor(c(m_mi[k]),net_LNC);# 某pivot反向邻居(调控的全部网络基因)
        sig=intersect(nodeNei,m);#调控的模块基因(一个模块内不用去重)
        neiAndM=length(sig);
        
        if(neiAndM > 1){
          p=1-phyper(neiAndM-1, length(nodeNei),nrow(net_LNC)-length(nodeNei),length(mn));
          if(p<0.05 & p>0){
            print(neiAndM/length(m))
            
            neiAndM2<-as.data.frame(t(sig)); colnames(neiAndM2)<-c(1:dim(neiAndM2)[2])
            neiAndM3<-unite(neiAndM2,"combine",colnames(neiAndM2), sep="/", remove = F)[,1]
            
            write.table(paste('PFC',j,names(nodes[m_mi[k]]),'LncRNA', neiAndM,p,neiAndM3, sep = '\t'),"Pivot regulators of crosstalk modules.txt",append=T,row.names=F,col.names = F,quote = F);
            write.table(paste('PFC',j,names(nodes[m_mi[k]]),sig,'LncRNA',sep = '\t'),"all_pivots_edges.txt",append=T,row.names=F,col.names = F,quote = F);
          }
        }
      }
    }
    
    for(j in pfc){ #字符时可以in本身,数字时用length
      #j=2
      m<-as.vector(All_module_gene[All_module_gene$module==j,][,2])# 模块基因
      mn<-intersect(net_mRNA_miR,m);length(mn) # 只是看看多少在网络里
      m_mi<-getNeighbor(mn,net_miR); length(m_mi) #第一次正向邻居(不同mRNA可能有相同邻居,但R包取邻居不用去重)
      
      nodes=1:nrow(net_miR);
      names(nodes)=rownames(net_miR);#nodes有两行,上边是名字,下边是本身(序号)
      #mgene=as.data.frame(m);#列名为‘m’
      #modNode=merge(realNet,mgene,by.x ='gene',by.y = 'm' )
      
      for(k in 1:length(m_mi)){
        nodeNei=getNeighbor(c(m_mi[k]),net_miR);# 某pivot反向邻居(调控的全部网络基因)
        sig=intersect(nodeNei,m);#调控的模块基因(一个模块内不用去重)
        neiAndM=length(sig);
        
        if(neiAndM > 1){
          p=1-phyper(neiAndM-1, length(nodeNei),nrow(net_miR)-length(nodeNei),length(mn));
          if(p<0.05 & p>0){
            print(neiAndM/length(m))
            
            neiAndM2<-as.data.frame(t(sig)); colnames(neiAndM2)<-c(1:dim(neiAndM2)[2])
            neiAndM3<-unite(neiAndM2,"combine",colnames(neiAndM2), sep="/", remove = F)[,1]
            
            write.table(paste('PFC',j,names(nodes[m_mi[k]]),'miRNA', neiAndM,p,neiAndM3, sep = '\t'),"Pivot regulators of crosstalk modules.txt",append=T,row.names=F,col.names = F,quote = F);
            write.table(paste('PFC',j,names(nodes[m_mi[k]]),sig,'miRNA',sep = '\t'),"all_pivots_edges.txt",append=T,row.names=F,col.names = F,quote = F);
          }
        }
      }
    }
    
    

    相关文章

      网友评论

          本文标题:WGCNA衍生:超几何分布识别模块Pivot regulator

          本文链接:https://www.haomeiwen.com/subject/bhqvqhtx.html