def cyclePartition(graph): a = prt.adj(graph) n = graph.nodes() r = prt.reach(a) l = prt.levels(r,n) c = prt.cycles(graph, l) return c
def distance(graph): #Must be bettered if more than one cycle, works on Yeast adj = prt.adj(graph); B = prt.binAddI(adj); D = prt.binSub(B, np.identity(len(adj),float)) prevS = B for i in range(2,len(adj),1): curS = prt.binPrd(B, prevS) D = D + i*(curS-prevS) prevS = curS return D
def distance(graph): adj = prt.adj(graph); B = prt.binAddI(adj); D = prt.binSub(B, np.identity(len(adj),float)) prevS = B for i in range(2,len(adj),1): curS = prt.binPrd(B, prevS) D = D + i*(curS-prevS) prevS = curS return D
def distance(graph): #Must be bettered if more than one cycle, works on Yeast adj = prt.adj(graph) B = prt.binAddI(adj) D = prt.binSub(B, np.identity(len(adj), float)) prevS = B for i in range(2, len(adj), 1): curS = prt.binPrd(B, prevS) D = D + i * (curS - prevS) prevS = curS return D
def distance(graph): adj = prt.adj(graph); B = prt.binary_add_identity(adj) D = prt.binary_substraction(B, np.identity(len(adj),float)) prev_S = B for i in range(2,len(adj),1): cur_S = prt.binary_product(B, prev_S) D = D + i*(cur_S-prev_S) prev_S = cur_S return D
def SILS(graph): A = prt.adj(graph) A = np.asarray(A) loops = geodetic(graph) nodes = graph.nodes() #make an adjacency matrix for the loop edges = [] #list of adjacency matrices for each loop for loop in loops: # For every loop, add edges to a matrix in adjacency-style l = len(loop) E = np.zeros(A.shape) for j in range(0,l-1): sourceName = loop[j] destName = loop[j+1] source = nodes.index(sourceName) dest = nodes.index(destName) E[source,dest] = 1 firstName = loop[0] lastName = loop[l-1] first = nodes.index(firstName) last = nodes.index(lastName) E[last,first] = 1 edges.append(E) B = np.zeros(A.shape); S = loops SILS = [] # Here comes the construction of the SILS while S: #figure out which loops do not contribute any new edges copy_s = S[:] edges_copy = [] for j, element in enumerate(edges): # Counting the number of contributions the loop can introduce entry = S[j] test = element - B test[test<0] = 0 new_edges = np.sum(test) if new_edges==0: copy_s.remove(entry) else: edges_copy.append(element) S = copy_s edges = edges_copy #of the remaining loops, determine the contribution of edges numberOfNewEdges = [] for element in edges: test = element - B test[test<0] = 0 new_edges = int(np.sum(test)) numberOfNewEdges.append(new_edges) if numberOfNewEdges: # The loop with minimum contribution should be added m = numberOfNewEdges.index(min(numberOfNewEdges)) # Add edges to the SILS edge collection B = B + edges[m] SILS.append(S[m]) # Remove the items from the loop collection S.pop(m) edges.pop(m) return SILS
def SILS(graph): A = prt.adj(graph) A = np.asarray(A) loops = geodetic(graph) nodes = graph.nodes() # Make an adjacency matrix for the loop. edges = [] # List of adjacency matrices for each loop. for loop in loops: # For every loop, add edges to a matrix in adjacency-style. l = len(loop) E = np.zeros(A.shape) for j in range(0,l-1): source_name = loop[j] dest_name = loop[j+1] source = nodes.index(source_name) dest = nodes.index(dest_name) E[source,dest] = 1 first_name = loop[0] last_name = loop[l-1] first = nodes.index(first_name) last = nodes.index(last_name) E[last,first] = 1 edges.append(E) B = np.zeros(A.shape); S = loops SILS = [] # Here comes the construction of the SILS. while S: # Figure out which loops do not contribute any new edges. copy_s = S[:] edges_copy = [] for j, element in enumerate(edges): # Counting the number of contributions the loop can introduce. entry = S[j] test = element - B test[test<0] = 0 new_edges = np.sum(test) if new_edges==0: copy_s.remove(entry) else: edges_copy.append(element) S = copy_s edges = edges_copy # Of the remaining loops, determine the contribution of edges. number_of_new_edges = [] for element in edges: test = element - B test[test<0] = 0 new_edges = int(np.sum(test)) number_of_new_edges.append(new_edges) if number_of_new_edges: # The loop with minimum contribution should be added m = number_of_new_edges.index(min(number_of_new_edges)) # Add edges to the SILS edge collection B = B + edges[m] SILS.append(S[m]) # Remove the items from the loop collection S.pop(m) edges.pop(m) return SILS
def uniqueConsEdges(graph, sils): # Get graph information needed. A = prt.adj(graph) A = np.asarray(A) nodes = graph.nodes() # Make an adjacency matrix for every loop in sils edges = [] # List of adjacency-style matrices for each loop for loop in sils: # For every loop, add unique consecutive edges to a matrix in adjacency-style l = len(loop) E = np.zeros(A.shape) # If length of loop is 2 the else procedure would give problems if l == 2: for node in loop: index = nodes.index(node) E[index, index] = 1 else: for j in range(0, l - 2): sourceName = loop[j] destName = loop[j + 2] # This line is different, plus 2 this time. source = nodes.index(sourceName) dest = nodes.index(destName) E[source, dest] = 1 secondName = loop[1] firstName = loop[0] lastName = loop[l - 1] lastButOneName = loop[l - 2] second = nodes.index(secondName) first = nodes.index(firstName) last = nodes.index(lastName) lastButOne = nodes.index(lastButOneName) E[lastButOne, first] = 1 E[last, second] = 1 edges.append(E) # Sum occurrence of consecutive edges in loops. sumConsEdges = np.zeros(A.shape) for entry in edges: sumConsEdges = np.add(sumConsEdges, entry) # When sum is one, the edge is in only one loop, so get indices. indicesConsEdges = np.where(sumConsEdges == 1) uniqueConsLoops = [] # Construct the loop list and node indices list. for i in range(0, int(len(indicesConsEdges[0] - 1))): rowNo = int(indicesConsEdges[0][i]) columnNo = int(indicesConsEdges[1][i]) j = 0 for edge in edges: if edge[rowNo, columnNo] == 1: uniqueConsLoops.append(sils[j]) j = j + 1 return indicesConsEdges, uniqueConsLoops
def switchChoose(sils, indEdges, uniqLoops, indConsEdges, uniqConsLoops, graph): # This function is used to come to a conclusion about which loop to switch off how. # First try to switch off as many loops as possible by finding unique edges. unique = [] # Make element of the set uniqLoops unique. [unique.append(loop) for loop in uniqLoops if loop not in unique] # Get the right loop numbers. indSource = [] indDest = [] loopNos = [] for loop in unique: loopNo = uniqLoops.index(loop) loopNos.append(loopNo) # Construct the indices list with indices for every loop. [indSource.append(indEdges[0][loopNo]) for loopNo in loopNos] [indDest.append(indEdges[1][loopNo]) for loopNo in loopNos] uniqIndices = [indSource, indDest] # Now switching node indices for variable names. namesUniq = [] for item in uniqIndices: names = prt.nodeNames(item, graph) namesUniq.append(names) # And reordering them into edge style. uniqueEdges = [] for i in range(0, len(namesUniq[0])): uniqueEdge = [namesUniq[0][i], namesUniq[1][i]] uniqueEdges.append(uniqueEdge) # Now eliminating loops from the total collection that needs to be turned off. SILS = sils [SILS.remove(loop) for loop in unique if loop in SILS] # Now deleting which loops can be switched off by the method of consecutive edges. uniqCons = [] [uniqCons.append(loop) for loop in uniqConsLoops if loop not in uniqCons] # Removing all loops that can already be turned off simpler. [uniqCons.remove(loop) for loop in unique if loop in uniqCons] # Constructing indices lists for every loop. indSource = [] indDest = [] loopNos = [] for loop in uniqCons: loopNo = uniqConsLoops.index(loop) loopNos.append(loopNo) # List of indices for every loop with two unique cons edges. [indSource.append(indConsEdges[0][int(loopNo)]) for loopNo in loopNos] [indDest.append(indConsEdges[1][int(loopNo)]) for loopNo in loopNos] uniqConsIndices = [indSource, indDest] # Now switching node indices for variable names. namesCons = [] for item in uniqConsIndices: names2 = prt.nodeNames(item, graph) namesCons.append(names2) # And reordering them into edge style. uniqueConsEdges = [] for i in range(0, len(namesCons[0])): consEdge = [namesCons[0][i], namesCons[1][i]] uniqueConsEdges.append(consEdge) # Eliminating loops again from SILS. [SILS.remove(loop) for loop in uniqCons if loop in SILS] return (unique, uniqueEdges, uniqCons, uniqueConsEdges, SILS)
def uniqueEdges(graph, sils): A = prt.adj(graph) A = np.asarray(A) nodes = graph.nodes() #make an adjacency matrix for every loop in sils edges = [] #list of adjacency matrices for each loop for loop in sils: # For every loop, add edges to a matrix in adjacency-style l = len(loop) E = np.zeros(A.shape) for j in range(0, l - 1): sourceName = loop[j] destName = loop[j + 1] source = nodes.index(sourceName) dest = nodes.index(destName) E[source, dest] = 1 firstName = loop[0] lastName = loop[l - 1] first = nodes.index(firstName) last = nodes.index(lastName) E[last, first] = 1 edges.append(E) # Sum occurrence of edges in loops sumEdges = np.zeros(A.shape) for entry in edges: # sumEdges += entry sumEdges = np.add(sumEdges, entry) # When sum is one, the edge is in only one loop, so get indices. # a = np.argwhere(sumEdges==1) # for entry in a: # row, column = entry indicesEdges = np.where(sumEdges == 1) uniqueLoops = [] for p in range(int(len(indicesEdges[0] - 1))): rowNo = int(indicesEdges[0][p]) columnNo = int(indicesEdges[1][p]) for q, edge in enumerate(edges): if edge[rowNo, columnNo] == 1: uniqueLoops.append(sils[q]) # # When sum is two, the edge can be used to switch off two loops # extraIndicesEdges = np.where(sumEdges == 2) # # extraLoops = [] # for i in range(0,int(len(extraIndicesEdges[0]-1))): # twoLoops = [] # rowNo = int(extraIndicesEdges[0][i]) # columnNo = int(extraIndicesEdges[1][i]) # j = 0 # for edge in edges: # if edge[rowNo,columnNo] == 1: # twoLoops.append(sils[j]) # j = j+1 # extraLoops.append(twoLoops) return indicesEdges, uniqueLoops
def unique_edges(graph,sils): A = prt.adj(graph) A = np.asarray(A) nodes = graph.nodes() # Make an adjacency matrix for every loop in SILS. edges = [] # List of adjacency matrices for each loop for loop in sils: # For every loop, add edges to a matrix in adjacency-style. l = len(loop) E = np.zeros(A.shape) for j in range(0,l-1): source_name = loop[j] dest_name = loop[j+1] source = nodes.index(source_name) dest = nodes.index(dest_name) E[source,dest] = 1 first_name = loop[0] last_name = loop[l-1] first = nodes.index(first_name) last = nodes.index(last_name) E[last,first] = 1 edges.append(E) # Sum occurrence of edges in loops. sum_edges = np.zeros(A.shape) for entry in edges: # sumEdges += entry sum_edges = np.add(sum_edges,entry) # When sum is one, the edge is in only one loop, so get indices. indices_edges = np.where(sum_edges == 1) unique_loops = [] for p in range(int(len(indices_edges[0]-1))): rowNo = int(indices_edges[0][p]) columnNo = int(indices_edges[1][p]) for q,edge in enumerate(edges): if edge[rowNo,columnNo] == 1: unique_loops.append(sils[q]) # # When sum is two, the edge can be used to switch off two loops # extraIndicesEdges = np.where(sumEdges == 2) # # extraLoops = [] # for i in range(0,int(len(extraIndicesEdges[0]-1))): # twoLoops = [] # rowNo = int(extraIndicesEdges[0][i]) # columnNo = int(extraIndicesEdges[1][i]) # j = 0 # for edge in edges: # if edge[rowNo,columnNo] == 1: # twoLoops.append(sils[j]) # j = j+1 # extraLoops.append(twoLoops) return indices_edges,unique_loops
def unique_cons_edges(graph,sils): # Get graph information needed. A = prt.adj(graph) A = np.asarray(A) nodes = graph.nodes() # Make an adjacency matrix for every loop in sils edges = [] # List of adjacency-style matrices for each loop for loop in sils: # For every loop, add unique consecutive edges to a matrix in adjacency-style l = len(loop) E = np.zeros(A.shape) # If length of loop is 2 the else procedure would give problems if l == 2: for node in loop: index = nodes.index(node) E[index,index] = 1 else: for j in range(0,l-2): source_name = loop[j] dest_name = loop[j+2] # This line is different, plus 2 this time. source = nodes.index(source_name) dest = nodes.index(dest_name) E[source,dest] = 1 second_name = loop[1] first_name = loop[0] last_name = loop[l-1] last_but_one_name = loop[l-2] second = nodes.index(second_name) first = nodes.index(first_name) last = nodes.index(last_name) last_but_one = nodes.index(last_but_one_name) E[last_but_one,first] = 1 E[last,second] = 1 edges.append(E) # Sum occurrence of consecutive edges in loops. sum_cons_edges = np.zeros(A.shape) for entry in edges: sum_cons_edges = np.add(sum_cons_edges,entry) # When sum is one, the edge is in only one loop, so get indices. indices_cons_edges = np.where(sum_cons_edges == 1) unique_cons_loops = [] # Construct the loop list and node indices list. for i in range(0,int(len(indices_cons_edges[0]-1))): rowNo = int(indices_cons_edges[0][i]) columnNo = int(indices_cons_edges[1][i]) j = 0 for edge in edges: if edge[rowNo,columnNo] == 1: unique_cons_loops.append(sils[j]) j = j+1 return indices_cons_edges,unique_cons_loops
def switchChoose(sils,ind_edges,uniq_loops,ind_cons_edges,uniq_cons_loops,graph): # This function is used to come to a conclusion about which loop to switch off how. # First try to switch off as many loops as possible by finding unique edges. unique = [] # Make element of the set uniqLoops unique. [unique.append(loop) for loop in uniq_loops if loop not in unique] # Get the right loop numbers. ind_source = [] ind_dest = [] loop_nos = [] for loop in unique: loop_no = uniq_loops.index(loop) loop_nos.append(loop_no) # Construct the indices list with indices for every loop. [ind_source.append(ind_edges[0][loop_no]) for loop_no in loop_nos] [ind_dest.append(ind_edges[1][loop_no]) for loop_no in loop_nos] uniq_indices = [ind_source,ind_dest] # Now switching node indices for variable names. names_uniq = [] for item in uniq_indices: names = prt.node_names(item,graph) names_uniq.append(names) # And reordering them into edge style. unique_edges = [] for i in range(0,len(names_uniq[0])): unique_edge = [names_uniq[0][i],names_uniq[1][i]] unique_edges.append(unique_edge) # Now eliminating loops from the total collection that needs to be turned off. SILS = sils [SILS.remove(loop) for loop in unique if loop in SILS] # Now deleting which loops can be switched off by the method of consecutive edges. uniq_cons = [] [uniq_cons.append(loop) for loop in uniq_cons_loops if loop not in uniq_cons] # Removing all loops that can already be turned off simpler. [uniq_cons.remove(loop) for loop in unique if loop in uniq_cons] # Constructing indices lists for every loop. ind_source = [] ind_dest = [] loop_nos = [] for loop in uniq_cons: loop_no = uniq_cons_loops.index(loop) loop_nos.append(loop_no) # List of indices for every loop with two unique cons edges. [ind_source.append(ind_cons_edges[0][int(loopNo)]) for loopNo in loop_nos] [ind_dest.append(ind_cons_edges[1][int(loopNo)]) for loopNo in loop_nos] uniqConsIndices = [ind_source,ind_dest] # Now switching node indices for variable names. names_cons = [] for item in uniqConsIndices: names2 = prt.node_names(item,graph) names_cons.append(names2) # And reordering them into edge style. unique_cons_edges = [] for i in range(0,len(names_cons[0])): cons_edge = [names_cons[0][i],names_cons[1][i]] unique_cons_edges.append(cons_edge) # Eliminating loops again from SILS. [SILS.remove(loop) for loop in uniq_cons if loop in SILS] return (unique,unique_edges,uniq_cons,unique_cons_edges,SILS)