我想製作一個部分複製模型,使得我從圖G中的s> = 1個頂點開始。然後引入一個新頂點「v」並在G中隨機選擇一個頂點「u」 。以概率q,連接v和u。獨立於彼此,以概率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))`
此代碼不能正常工作,因爲當我嘗試計算平均最短路徑長度,它說該圖未連接。
的注意事項:避免命名變量'list','str',或其他預先定義的標識符。通過這種方式,你可以對預定義的事物進行投影,而像「list(something)」這樣的無害表達式可能會以一種神祕的錯誤結束。 – 9000