2017-02-23 36 views
1

我想製作一個部分複製模型,使得我從圖G中的s> = 1個頂點開始。然後引入一個新頂點「v」並在G中隨機選擇一個頂點「u」 。以概率q,連接vu。獨立於彼此,以概率p將u的每個鄰居連接到v。我想重複一遍,這取決於我的s在Python中製作網絡

我有一個文本文件,它有三列:蛋白1,蛋白2和組合分數。這個文件有超過1000個這樣的條目。因此每條線表示圖中從「蛋白質1」到「蛋白質2」的重量爲「combined_score」的邊緣。我正在使用此文件來實現算法(如上所述)。我篩選出只有那些線在我的combined_score大於990

4932.Q0010 4932.Q0017 951 
4932.Q0010 4932.Q0032 951 
4932.Q0010 4932.Q0045 313 
4932.Q0010 4932.Q0085 263 
4932.Q0010 4932.Q0105 410 
4932.Q0010 4932.Q0143 930 

代碼:

import networkx as nx 
import matplotlib.pyplot as plt 
import random 

def partial_duplication_model(G,p,q,s,max_score): 

    k=G.number_of_nodes() 
    list=[] 

    for i in range(s): 
     #random.randint(1,k) 
     node = random.choice(G.nodes()) 
     if node not in list: 
      v = max_score + i 
      G.add_node(v) 
      list.append(node) 

      G.add_edge(v,node,weight = q) 

     #for j in range(k): 
      for j in G.neighbors(node): 
       if not j==v: 
        G.add_edge(j,node,weight = p) 


    print(G.number_of_nodes()) 

    return(G) 


if __name__ == '__main__': 
     f=open("4932.protein.links.v10.txt","r").readlines() 
     G=nx.Graph() 
     max_score=0 

     for x in f[1:]: 
       y=x.split(" ") 
       for node in y[:1]: 
         if int(y[2])>=990: 
           G.add_node(node) 
       if int(y[2])>=990: 
         G.add_edge(y[0], y[1], weight=int(y[2])) 
       temp=int(y[2]) 
     #print(type(temp)) 
       max_score=max(max_score,temp) 

     p = 0.3 
     q = 0.7 
     s = 2 
     res = partial_duplication_model(G,p,q,s,max_score) 
     print("making a plot") 
     stuff = nx.degree_histogram(res) 
     plt.loglog(stuff) 
     plt.show() 
     #print("Average shortest path length : " , nx.average_shortest_path_length(res))` 

此代碼不能正常工作,因爲當我嘗試計算平均最短路徑長度,它說該圖未連接。

+0

的注意事項:避免命名變量'list','str',或其他預先定義的標識符。通過這種方式,你可以對預定義的事物進行投影,而像「list(something)」這樣的無害表達式可能會以一種神祕的錯誤結束。 – 9000

回答

0
  • 我會做的第一件事是擺脫單個字母的變量名稱。
  • 快捷方式:如果添加一個節點不存在的邊,它將添加該節點。
  • 您不能使用列表作爲變量。 (OK,你可以,但不要指望了良好的效果)

    import networkx as nx 
    import matplotlib.pyplot as plt 
    import random 
    
    def partial_duplication_model(G,p,q,s,max_score): 
        k=G.number_of_nodes() 
        my_list=[] 
    
        for i in range(s): 
         #random.randint(1,k) 
         node = random.choice(G.nodes()) 
         if node not in my_list: 
          v = max_score + i 
          G.add_edge(v,node,weight = q) # I don't understand what you are doing here. 
          list.append(node) 
    
         #are you sure your spacing is correct in the next 5 lines?  
         #for j in range(k): 
          for j in G.neighbors(node): 
           if not j==v: 
            G.add_edge(j,node,weight = p) 
    
    
        print(G.number_of_nodes()) 
        return(G) 
    
    if __name__ == '__main__': 
        G= nx.Graph() 
        max_score = 0 
        with open('4932.protein.links.v10.txt','r') as f: 
         for x in f.readlines()[1:]: 
          node1, node2, val=x.split(" ") 
          val = int(val) 
          if val>=990: 
           G.add_edge(node1, node2, weight=val) 
          max_score=max(max_score,val) 
          p = 0.3 
          q = 0.7 
          s = 2 
          res = partial_duplication_model(G,p,q,s,max_score) 
          print("making a plot") 
          stuff = nx.degree_histogram(res) 
          plt.loglog(stuff) 
          plt.show() 
          #print("Average shortest path length : " , nx.average_shortest_path_length(res)) 
    
+0

是的間距是正確的 –