Exemple #1
0
def calc_ClosenessCentrality(Graph, node_to_g):
    prot_to_closeness_centrality = {}
    for NI in Graph.Nodes():
        my_prot = node_to_g[NI.GetId()]
        CloseCentr = snap.GetClosenessCentr(Graph, NI.GetId())
        prot_to_closeness_centrality[my_prot] = CloseCentr
    return prot_to_closeness_centrality
def ClosenessCentrality(G):
    centrality = {}
    for NI in G.Nodes():
        centr = snap.GetClosenessCentr(G, NI.GetId())
        centrality[NI.GetId()] = round(centr, 6)

    return centrality
    def form_closeness_centrality_index(self):
        index = {}
        for node in self._graph.Nodes():
            nid = node.GetId()
            index[nid] = snap.GetClosenessCentr(self._graph, nid)

        self._closeness_index = index
def Closeness(d, e):
    f = open(d)
    s = f.read()
    s1 = re.split('\n', s)
    G1 = snap.PUNGraph.New()

    a = re.split(' ', s1[0])

    for i in range(0, int(a[0])):
        G1.AddNode(i)

    for i in range(1, int(a[1]) + 1):
        b = re.split(' ', s1[i])
        G1.AddEdge(int(b[0]), int(b[1]))

    CloseCentr = dict()

    for NI in G1.Nodes():
        CloseCentr[NI.GetId()] = snap.GetClosenessCentr(G1, NI.GetId())
        # print "node: %d centrality: %f" % (NI.GetId(), CloseCentr)

    EdgePara = dict()

    for i in range(1, int(a[1]) + 1):
        c = re.split(' ', s1[i])
        if CloseCentr[int(c[0])] == 0 and CloseCentr[int(c[1])] == 0:
            EdgePara[(int(c[0]), int(c[1]))] = 0
            EdgePara[(int(c[1]), int(c[0]))] = 0
        else:
            EdgePara[(int(c[0]), int(c[1]))] = e * CloseCentr[int(
                c[0])] / (CloseCentr[int(c[0])] + CloseCentr[int(c[1])])
            EdgePara[(int(c[1]), int(c[0]))] = e * CloseCentr[int(
                c[1])] / (CloseCentr[int(c[0])] + CloseCentr[int(c[1])])

    return EdgePara
Exemple #5
0
def CalculateClosenessCentrality(graph):
    output = []
    for NI in graph.Nodes():
        CloseCentr = snap.GetClosenessCentr(graph, NI.GetId())
        output.append([NI.GetId(), CloseCentr])
        # print "node: %d centrality: %f" % (NI.GetId(), CloseCentr)
    return output
Exemple #6
0
def maxClosenessNode(apiGraph):
    node_centralities = [(node.GetId(),
                          snap.GetClosenessCentr(apiGraph, node.GetId(), True,
                                                 False))
                         for node in apiGraph.Nodes()
                         if node.GetId() in package_names]
    max_id = max(node_centralities, key=lambda x: x[1])[0]
    return package_names[max_id]
Exemple #7
0
 def GetMaxKClosenessCentrality(self, k):
     lstColseness = []
     nodesId = []
     for NI in self.graph.Nodes():
         closecenter = snap.GetClosenessCentr(self.graph, NI.GetId())
         nodesId.append(NI.GetId())
         lstColseness.append(closecenter)
     return self.GetMaxK(lstColseness, nodesId, k)
Exemple #8
0
    def getNodeAttributes(self,UGraph):

        attriList=[]
        for index in range(UGraph.GetNodes()):
            nodelist=[]
            attriList.append(nodelist)
            
            #page rank
        PRankH = snap.TIntFltH()
        snap.GetPageRank(UGraph, PRankH)
        counter=0
        for item in PRankH:
            attriList[counter].append(PRankH[item])
            counter+=1
            #HIN
        counter=0
        NIdHubH = snap.TIntFltH()
        NIdAuthH = snap.TIntFltH()
        snap.GetHits(UGraph, NIdHubH, NIdAuthH)
        for item in NIdHubH:
            attriList[counter].append(NIdHubH[item])
            attriList[counter].append(NIdAuthH[item])
            counter+=1

            # Betweenness Centrality 
        counter=0
        Nodes = snap.TIntFltH()
        Edges = snap.TIntPrFltH()
        snap.GetBetweennessCentr(UGraph, Nodes, Edges, 1.0)
        for node in Nodes:
            attriList[counter].append(Nodes[node])
            counter+=1

            # closeness centrality 
        counter=0
        for NI in UGraph.Nodes():
            CloseCentr = snap.GetClosenessCentr(UGraph, NI.GetId())
            attriList[counter].append(CloseCentr)
            counter+=1

            # farness centrality 
        counter=0
        for NI in UGraph.Nodes():
            FarCentr = snap.GetFarnessCentr(UGraph, NI.GetId())
            attriList[counter].append(FarCentr)
            counter+=1

            # node eccentricity
        counter=0
        for NI in UGraph.Nodes():
            attriList[counter].append(snap.GetNodeEcc(UGraph, NI.GetId(), True))
            counter+=1

        atrriMarix=np.array(attriList)

        return atrriMarix
Exemple #9
0
    def compute_closeness_centrality(self, graph):
        closeness_centrality = snap.TIntFltH()

        for node in graph.Nodes():
            node_closeness_centrality = snap.GetClosenessCentr(
                graph, node.GetId())
            closeness_centrality.AddDat(node.GetId(),
                                        node_closeness_centrality)

        return closeness_centrality
    def betweenCentral(self,date):

            temp = self.network
            temp.AddFltAttrN("bcentrality")

            # Colour Hash Table
            NIdColorH = snap.TIntStrH()
            purple = 0
            blue = 0
            green = 0
            yellow = 0
            red = 0
            # For every node
            for NI in temp.Nodes():

                # Get ITs betweenes centrality
                CloseCentr = snap.GetClosenessCentr(temp, NI.GetId())
                temp.AddFltAttrDatN(NI.GetId(),CloseCentr,"bcentrality")

                # Determine colour
                if CloseCentr <0.2:
                    NIdColorH[NI.GetId()] = "purple"
                    purple+=1

                elif CloseCentr <0.4:
                    NIdColorH[NI.GetId()] = "blue"
                    blue+=1

                elif CloseCentr <0.6:
                    NIdColorH[NI.GetId()] = "green"
                    green+=1

                elif CloseCentr <0.8:
                    NIdColorH[NI.GetId()] = "yellow"
                    yellow+=1

                else:
                    NIdColorH[NI.GetId()] = "red"
                    red+=1

            print"Purple:\t", purple
            print"Blue:\t" , blue
            print"Green:\t", green
            print"Yellow:", yellow
            print"Red:\t" ,red

            # Draw graph
            #snap.DrawGViz(temp, snap.gvlSfdp, "BetweenesCentrality.png", date, True, NIdColorH)


            """
def GetOverlap(filePathName, Graph, t):
    # l is here the final ranking of the nodes
    # Intially, we just put all the nodes in this
    # and afterwards we sort it
    l = [i for i in range(Graph.GetNodes())]

    # The reference vector whose information is used to sort l
    ref_vect = [0 for i in range(Graph.GetNodes())]

    # if Type 1, then fill ref_vect with closeness centrality measure
    if (t == 1):
        for NI in Graph.Nodes():
            ref_vect[NI.GetId()] = snap.GetClosenessCentr(Graph, NI.GetId())

    # if Type 2, then fill ref_vect with betweenness centrality measure
    if (t == 2):
        Nodes = snap.TIntFltH()
        Edges = snap.TIntPrFltH()

        # Setting NodeFrac parameter as 0.8 as instructed
        snap.GetBetweennessCentr(Graph, Nodes, Edges, 0.8)
        for node in Nodes:
            ref_vect[node] = Nodes[node]

    # if Type 3, then fill ref_vect with PageRank scores
    if (t == 3):
        PRankH = snap.TIntFltH()

        # Taking the limit as 1e-6 as used in gen_centrality.py
        snap.GetPageRank(Graph, PRankH, 0.8, 1e-6, 100)
        for item in PRankH:
            ref_vect[item] = PRankH[item]

    # Now we sort l using the ref_vect
    l.sort(
        key=cmp_to_key(lambda item1, item2: ref_vect[item2] - ref_vect[item1]))

    # make a set containing top 100 nodes of l
    S1 = set(l[:100])

    # make another set containing top 100 nodes from the text files
    S2 = set()
    f = open(filePathName, 'r')
    for _ in range(100):
        s = f.readline()
        a, b = s.split()
        S2.add(int(a))

    # return the number of overlaps in S1 and S2
    return len(S1.intersection(S2))
Exemple #12
0
def snap_cc_compute(FILE_NAME):
    _CSV = ".csv"

    FILE_PATH = "../resultDataset/"
    FILE_OUT_PATH = "../igraph_result/" + FILE_NAME + "_cc" + _CSV

    data = pd.read_csv(FILE_PATH + FILE_NAME + _CSV, header=None)
    csvoutFile = open(FILE_OUT_PATH, "w")
    writer = csv.writer(csvoutFile, lineterminator='\n')

    data0 = np.unique(data.iloc[:, 0].append(data.iloc[:, 1]))
    print("read  %s,  file complete,totol  %d  nodes  %d edges " %
          (FILE_NAME, len(data0), len(data)))
    # print("node list  is  " + data0)
    G2 = snap.TNGraph.New()
    count = 0
    countNum = len(data0)
    print(" start build nodes")
    nodes = data0.tolist()
    # for node in nodes:
    #     # print type (node)
    #     G2.AddNode(nodes.index(node))
    #     count=count+1

    while count != countNum:
        G2.AddNode(count)
        count = count + 1

    count = 0
    edgeNum = len(data)
    print(" start add edges")
    for row in data.itertuples(index=False):
        G2.AddEdge(nodes.index(row[0]), nodes.index(row[1]))
        print('add edges percent: {:.2%}'.format(
            float(count) / float(edgeNum)))
        count += 1

    print(" start compute cc ")

    count = 0
    for NI in G2.Nodes():
        count = count + 1
        CloseCentr = snap.GetClosenessCentr(G2, NI.GetId())
        print('compute percent: {:.2%}'.format(float(count) / float(countNum)))
        writer.writerow([nodes[NI.GetId()], CloseCentr])
        # print "node: %d centrality: %f" % (NI.GetId(), CloseCentr)

    csvoutFile.close()
    print("succesfull")
def nodes_centrality_degree_centrality():
    CloseCentrV = []

    for NI in G.Nodes():
        CloseCentr = snap.GetClosenessCentr(G, NI.GetId())
        CloseCentrV.append((NI.GetId(), CloseCentr))

    def getKey(item):
        return item[1]

    Sorted_CD = sorted(CloseCentrV, key=getKey, reverse=True)
    #print(Sorted_CD[0:5])
    Nodes = [x[0] for x in Sorted_CD]
    Score = [x[1] for x in Sorted_CD]
    for item in Nodes[0:5]:  #top 5
        print("Node", Nodes[item], "DC:", Score[item])
Exemple #14
0
def model_CloseCentr(G):
    print('**************Returns closeness centrality of a given node NId in Graph*************')
    node = []
    centrality = []
    for NI in G.Nodes():
        CloseCentr = snap.GetClosenessCentr(G, NI.GetId())
        node.append(NI.GetId())
        centrality.append(CloseCentr)
        # print "node: %d centrality: %f" % (NI.GetId(), CloseCentr)

    data = pd.DataFrame({'node': node, 'centrality': centrality})
    # print(len(data))
    data = data.sort_values(by=['node', 'centrality'])
    # 写入
    data.to_csv('./data/picture/CloseCentr_centrality.csv', sep='\t')
    print('CloseCentr = ', data[-5:])
    print('###############end_file###############')
    picture(node, centrality, 'CloseCentr', 'node', 'centrality')
Exemple #15
0
def analyzeCloseness(FNGraph):
    t1 = time.time()

    print "Started calculating Closeness scores: \n"

    closeness = dict()

    for NI in FNGraph.Nodes():
        closeness[NI.GetId()] = snap.GetClosenessCentr(FNGraph, NI.GetId())

    nodesSortedByCloseness = sorted(closeness, key=closeness.get, reverse=True)

    # print top 25
    for i in range(0, 25):
        print "\tNode %d \t Closeness: %f" %\
            (nodesSortedByCloseness[i], closeness[nodesSortedByCloseness[i]])

    print "\nFinished calculating in %.3f seconds\n" % (time.time()-t1)
def calculate_closeness_centrality(G,GName,Nodes,nodes,edges):

    print "Initialting Calculations of Closeness Centrality using inbuilt function"
    
    start_time = time.time()

    closeness_centralities = []

    for NI in G.Nodes():
        CloseCentr = snap.GetClosenessCentr(G, NI.GetId())
        closeness_centralities.append([CloseCentr,NI.GetId()])

    closeness_centralities.sort(reverse=True)

    time_taken = time.time() - start_time
    print "Execution for Closeness Centrality completed in ",time_taken//60," mins and ",(time_taken//1)%60, "seconds"

    return closeness_centralities
Exemple #17
0
def _closenessOverlap(elistPath):
    """
    Compute overlap between our values of closeness centrality and SNAP's internal implementation

    Parameters
    ----------
    elistPath: str or pathlib.Path
        Edge list of the graph to compute centralities on
    ----------

    Returns
    ----------
    calculatedNodes: set
        Top 100 nodes by closeness centrality according to our implementation

    SNAPNodes: set
        Top 100 nodes by closeness centrality according to the SNAP implementation (snap.GetClosenessCentr)

    len(overlap): int
        Count of overlapping nodes between the 2 sets
    ----------

    Reads from file our values of closeness centrality and then calls the SNAP function
    Once we have 2 sets of top 100 nodes, perform a set.intersection() call for common elements between both sets
    """

    adjGraph = AdjGraph(elistPath, separator=" ")
    graph = adjGraph.SNAPGraph
    calculatedNodes = readNodes("closeness.txt")

    SNAPCC = {}
    for node in graph.Nodes():
        SNAPCC[node.GetId()] = snap.GetClosenessCentr(graph, node.GetId())

    SNAPCC = {
        k: v
        for k, v in sorted(SNAPCC.items(), key=lambda x: x[1], reverse=True)
    }

    SNAPNodes = list(SNAPCC.keys())[:100]
    SNAPNodes = set([int(node) for node in SNAPNodes])

    overlap = SNAPNodes.intersection(calculatedNodes)
    return (calculatedNodes, SNAPNodes, len(overlap))
def analyze(graph, file1, file2, file3):
    ####### closeness centrality #######
    t0 = time.time()
    closeness_dict = {}

    for node in graph.Nodes():
        closeness_dict[node.GetId()] = snap.GetClosenessCentr(graph,node.GetId())

    closeness_dict = {k:v for k,v in sorted(closeness_dict.items(), key=lambda item: item[1], reverse=True)}

    ans1 = get_overlaps(closeness_dict, file1)

    ######## betweenness centrality #######
    vertices_bt_dict = snap.TIntFltH()
    edge_bt_dict = snap.TIntPrFltH()

    snap.GetBetweennessCentr(graph, vertices_bt_dict, edge_bt_dict, 0.8)

    betweenness_dict = {}
    for node in graph.Nodes():
        betweenness_dict[node.GetId()] = vertices_bt_dict[node.GetId()]

    betweenness_dict = {k:v for k,v in sorted(betweenness_dict.items(), key=lambda item: item[1], reverse=True)}

    ans2 = get_overlaps(betweenness_dict, file2)

    ########## PageRank #############
    pr_dict = snap.TIntFltH()

    snap.GetPageRank(graph, pr_dict, 0.8)

    pagerank = {}
    for node in graph.Nodes():
        pagerank[node.GetId()] = pr_dict[node.GetId()]

    pagerank = {k:v for k,v in sorted(pagerank.items(), key=lambda item: item[1], reverse=True)}

    ans3 = get_overlaps(pagerank, file3)

    print("#overlaps for Closeness Centrality: {}".format(ans1))
    print("#overlaps for Betweenness Centrality: {}".format(ans2))
    print("#overlaps for PageRank Centrality: {}".format(ans3))
    def sample_closeness_centrality(self,
                                    n_node=100,
                                    normalized=True,
                                    isDir=False):
        '''
        Returns closeness centrality sample in Graph. Closeness centrality is equal to 1/farness centrality.
        
        :param n_node: number of nodes to sample
        :param normalized: Output should be normalized (True) or not (False).
        :param isDir: consider direct or not
        
        '''
        snap = self.snap
        ret = []
        for node in self.sample_nodes(n_node):
            CloseCentr = snap.GetClosenessCentr(self.graph, int(node),
                                                normalized, isDir)
            ret.append(CloseCentr)

        return ret
Exemple #20
0
def calculate_closeness_centrality(graph, hashtag):
    start = time.time()
    print("Calculating closeness centrality...")
    closeness_centrality_scores = []
    step = graph.GetNodes()/10
    counter = 0
    for node in graph.Nodes():
        score = snap.GetClosenessCentr(graph, node.GetId())
        if score == 0:
            continue
        closeness_centrality_scores.append((node.GetId(), score))
        counter+=1
        if counter % step == 0:
            print("Progress: %d nodes" %counter)
    print("Saving results to file...")
    with open(hashtag+"_closeness_centrality.csv", "w") as fout:
        fout.write("Node,Closeness Centrality\n")
        for el in closeness_centrality_scores:
            fout.write(",".join([str(el[0]), str(el[1])])+"\n")
    end = time.time()
    print("Completed in: %s" % timedelta(seconds=(int(end-start))))
    def centralityCorrelation(self):
        #Opens cntrltyCrltn data file or creates it.
        #print("centrality noting started\n")
        compiledGraph = open("cntrltyCrltn.dat","w+")
        #stores all centralities.
        #print("Entering centrality method\n")


        #Will act as iterator
        i=0
        #print("Entering centralityCorrelation for loop\n")
        #For each node
        for node in self.network.Nodes():
            #Get's its inward and outward degree
            outDeg = node.GetOutDeg()
            inDeg = node.GetInDeg()
            CloseCentr = snap.GetClosenessCentr(self.network, node.GetId())
            #saves the degrees as coordinates.
            self.saveCoordinates(outDeg,inDeg,compiledGraph,False,CloseCentr)
            #self.saveCoordinates(outDeg,inDeg,compiledGraph,True)
            #self.saveCoordinates(outDeg,compiledGraph,False)
            i+=1
Exemple #22
0
def closeness_test(name):
    if os.path.isfile(DATA_PATH + name + ".closeness"):
        print "Skipping", name
        return

    start = time.time()

    G, coords = osmParser.simpleLoadFromFile(name)

    print "Calculating closeness", name

    ##
    nodeToCloseness = {}
    for node in G.Nodes():
        nodeToCloseness[node.GetId()] = snap.GetClosenessCentr(G, node.GetId())
    ##

    closeOut = open(DATA_PATH + name + ".closeness", 'w')
    pickle.dump(nodeToCloseness, closeOut, 1)

    plotTopK(name, nodeToCloseness, coords, "YlGnBu")

    end = time.time()
    print "took", end - start, "seconds"
Exemple #23
0
import snap
import sys
import numpy as np
import matplotlib
matplotlib.use('Agg')

import matplotlib.pyplot as plt

input_file = sys.argv[1]
SubGraph = snap.LoadEdgeList(snap.PUNGraph, input_file, 0, 1)

cc = set()
closeness = dict()

Graph = snap.GetMxScc(SubGraph)

for node in Graph.Nodes():
    print node.GetId()
    Clcentr = snap.GetClosenessCentr(Graph, node.GetId())
    closeness[node.GetId()] = Clcentr
Exemple #24
0
        top_thirty_authorities = [
            auths_per_node[node_id] for node_id in top_thirty_nodes_ids
        ]
        #
        # Find betweenness
        nodes_betweenness = snap.TIntFltH()
        edge_betweenness = snap.TIntPrFltH()
        betweenness_centrality = snap.GetBetweennessCentr(
            largest_graph, nodes_betweenness, edge_betweenness, 1.0)
        top_thirty_betweenness = [
            nodes_betweenness[node_id] for node_id in top_thirty_nodes_ids
        ]

        # Find closeness
        top_thirty_closeness = [
            snap.GetClosenessCentr(largest_graph, node_id)
            for node_id in top_thirty_nodes_ids
        ]

        measures_df = pandas.DataFrame().from_dict({
            "PageRank":
            top_thirty_nodes_page_rank,
            "Hubs":
            top_thirty_hubs,
            "Authorities":
            top_thirty_authorities,
            "Betweenness":
            top_thirty_betweenness,
            "Closeness":
            top_thirty_closeness,
            "NodeIds":
for line in lines:
    tokens = line.split('||')
    if tokens[2] != '':
        nId = nameToNId[tokens[1]]
        uIdToNId[int(tokens[0])] = nId
graph = snap.ConvertGraph(snap.PUNGraph, network)
degCenters = {}
closeCenters = {}
pageRanks = snap.TIntFltH()
eigenCenters = snap.TIntFltH()
# btwnCenters = snap.TIntFltH()
# edgeHash = snap.TIntPrFltH()
print('Running PageRank...')
snap.GetPageRank(graph, pageRanks)
print('Running Eigenvector centrality...')
snap.GetEigenVectorCentr(graph, eigenCenters)
# print('Running Betweeness...')
# snap.GetBetweennessCentr(graph, btwnCenters, edgeHash)
print('Running Degree and Closeness...')
for uId, nId in uIdToNId.iteritems():
    print uId, nId
    degCenters[uId] = snap.GetDegreeCentr(graph, nId)
    closeCenters[uId] = snap.GetClosenessCentr(graph, nId)

outfile = open('csv/centralities.csv', 'w')
for uId, nId in uIdToNId.iteritems():
    outfile.write(str(uId) + ',' + str(pageRanks[nId]) + ',' +\
        str(eigenCenters[nId]) + ',' +\
        str(degCenters[uId]) + ',' + str(closeCenters[uId]) + '\n')
outfile.close()
def read_and_process_json_files(pdb, path,subdirectory,ari,contacts,contacttype):

    for file in os.listdir(path):
        if file.endswith(".json"):

            if "stats" not in file and "_" in file:

                #  Create graph data structure
                GraphNoWater = snap.TUNGraph.New()
                GraphWithWater = snap.TUNGraph.New()

                json_file = os.path.join(path,file)

                with open(json_file, 'r') as f:
                    pdbjson = json.load(f)


                siffile = file[:-5] + ".sif"
                SIFfilename = os.path.join(subdirectory, siffile)
                sifF = open(SIFfilename,"w")

                for idxlink, ilink in enumerate(pdbjson['links']):

                    ilink["contacttype"] = []

                    aricheck = 0
                    contactscheck = 0

                    atoms1 = []
                    atoms2 = []
                    atoms = []
                    atomtypes = []
                    atomtypes1 = []
                    atomtypes2 = []

                    if 'atomnames' in ilink:
                        atoms = ilink["atomnames"].split(",")[:-1]
                        # print atoms
                        for i in atoms:
                            atoms1.append(i.split("-")[0])
                            atoms2.append(i.split("-")[1])

                    if 'atomtypes' in ilink:
                        atomtypes = ilink["atomtypes"].split(",")[:-1]
                        # print atoms
                        for i in atomtypes:
                            atomtypes1.append(i.split("-")[0])
                            atomtypes2.append(i.split("-")[1])


                    jsonres1 = ilink["name"].split("->")[0]
                    jsonres2 = ilink["name"].split("->")[1].split(",")[0]
                    # value = int(ilink["name"].split("->")[1].split(",")[1])
                    value = ilink["value"]
                    chain1 = ilink["chains"].split("->")[0]
                    chain2 = ilink["chains"].split("->")[1]

                    resname1 = pdbjson['nodes'][ilink["source"]]["name"]
                    resname2 = pdbjson['nodes'][ilink["target"]]["name"]
                    match1 = re.match(r"([a-z]+)([0-9]+)", resname1, re.I)
                    if match1:
                        items1 = match1.groups()
                        resletter1 = items1[0]
                        # print items[0]
                    match2 = re.match(r"([a-z]+)([0-9]+)", resname2, re.I)
                    if match2:
                        items2 = match2.groups()
                        resletter2 = items2[0]
                        # print items[0]

                    if (pdbjson["nodes"][ilink["source"]]["type"] == "ATOM"
                    and "NucleicAcid" not in pdbjson["nodes"][ilink["source"]]["group"]):
                        residueLetter1 = oneLetter[resletter1]

                    else:
                        residueLetter1 = resletter1

                    if (pdbjson["nodes"][ilink["target"]]["type"] == "ATOM"
                    and "NucleicAcid" not in pdbjson["nodes"][ilink["target"]]["group"]):
                        residueLetter2 = oneLetter[resletter2]
                    else:
                        residueLetter2 = resletter2

                    if len(ari)>0:
                        for idx, i in enumerate(ari):
                            for ik in i["type"]:
                                if ik in contacttype:
                                    if (jsonres1 == i["res1"] and jsonres2 == i["res2"] and chain1 == i["chain1"] and chain2 == i["chain2"]):
                                        for adx, a in enumerate(atoms1):
                                            if atoms1[adx] == i["atom1"]:
                                                aricheck += 1
                                                ilink["contacttype"].append(ik)

                                                if atomtypes1[adx] == "L":
                                                    atomtype = "LIG"
                                                else:
                                                    atomtype = atomtypes1[adx] + "C"

                                                sifline = chain1 +":"+jsonres1+":_:"+residueLetter1+"\t"+ ik +":"+atomtype+"_SC"+"\t"+ chain2 +":"+jsonres2+":_:"+residueLetter2 + "\t"+ atoms1[adx]+"\n"
                                                sifF.write(sifline)
                                                break

                                    elif (jsonres1 == i["res2"] and jsonres2 == i["res1"] and chain1 == i["chain2"] and chain2 == i["chain1"]):
                                        for adx, a in enumerate(atoms2):
                                            if atoms2[adx] == i["atom1"]:
                                                aricheck += 1
                                                ilink["contacttype"].append(ik)

                                                if atomtypes2[adx] == "L":
                                                    atomtype = "LIG"
                                                else:
                                                    atomtype = atomtypes2[adx] + "C"

                                                sifline = chain2 +":"+jsonres2+":_:"+residueLetter2+"\t"+ ik +":"+atomtype+"_SC"+"\t"+ chain1 +":"+jsonres1+":_:"+residueLetter1 + "\t"+ atoms2[adx]+"\n"
                                                sifF.write(sifline)
                                                break
                    # if len(ri)>0:
                    #     for idx, i in enumerate(ri):
                    #         if ((jsonres1 == i["res1"] and jsonres2 == i["res2"] and chain1 == i["chain1"] and chain2 == i["chain2"])
                    #         or (jsonres1 == i["res2"] and jsonres2 == i["res1"] and chain1 == i["chain2"] and chain2 == i["chain1"])):
                    #             richeck += 1
                    #             ilink["contacttype"].append("pi-pi")

                    if len(contacts)>0:
                        for idx, i in enumerate(contacts):
                            if (jsonres1 == i["res1"] and jsonres2 == i["res2"]
                            and chain1 == i["chain1"] and chain2 == i["chain2"]):
                                for adx, a in enumerate(atoms1):
                                    if atoms1[adx] == i["atom1"] and atoms2[adx] == i["atom2"]:

                                        contactscheck += 1
                                        ilink["contacttype"].append(i["type"])

                                        if atomtypes1[adx] == "L":
                                            atomtype1 = "LIG"
                                        else:
                                            atomtype1 = atomtypes1[adx] + "C"

                                        if atomtypes2[adx] == "L":
                                            atomtype2 = "LIG"
                                        else:
                                            atomtype2 = atomtypes2[adx] + "C"

                                        for t in i["type"]:
                                            if t == "pi-pi":
                                                t = "pipistack"
                                            sifline = chain1 +":"+jsonres1+":_:"+residueLetter1+"\t"+ t +":"+atomtype1+"_"+atomtype2+"\t"+ chain2 +":"+jsonres2+":_:"+residueLetter2 + "\t"+ atoms1[adx]+ "\t"+ atoms2[adx]+"\n"
                                            sifF.write(sifline)

                                        break

                            elif (jsonres1 == i["res2"] and jsonres2 == i["res1"]
                            and chain1 == i["chain2"] and chain2 == i["chain1"]):
                                for adx, a in enumerate(atoms1):
                                    if atoms1[adx] == i["atom2"] and atoms2[adx] == i["atom1"]:

                                        contactscheck += 1
                                        ilink["contacttype"].append(i["type"])

                                        if atomtypes1[adx] == "L":
                                            atomtype1 = "LIG"
                                        else:
                                            atomtype1 = atomtypes1[adx] + "C"

                                        if atomtypes2[adx] == "L":
                                            atomtype2 = "LIG"
                                        else:
                                            atomtype2 = atomtypes2[adx] + "C"

                                        for t in i["type"]:
                                            if t == "pi-pi":
                                                t = "pipistack"
                                            sifline = chain1 +":"+jsonres1+":_:"+residueLetter1+"\t"+ t +":"+atomtype1+"_"+atomtype2+"\t"+ chain2 +":"+jsonres2+":_:"+residueLetter2 + "\t"+ atoms1[adx]+ "\t"+ atoms2[adx]+"\n"
                                            sifF.write(sifline)
                                        break

                    total = aricheck + contactscheck

                    if total == 0:
                        pdbjson['links'][idxlink] = None

                    else:
                        ilink["value"] = total
                        ilink["name"] = jsonres1 + "->" + jsonres2 + ", " + str(total)

                y = [i for i in pdbjson['links'] if i != None]
                pdbjson['links'] = y

                residuesfilteredLinks[file[:-5]] = y

                # calculating betwenness and closeness and degree for filtered contacts
                for idx, i in enumerate(pdbjson['nodes']):
                    i["weight"] = 0
                    if "HOH" not in i['name']:
                            GraphNoWater.AddNode(idx)
                    GraphWithWater.AddNode(idx)

                for idx, ilink in enumerate(pdbjson['links']):
                    pdbjson['nodes'][ilink["source"]]["weight"] += ilink["value"]
                    pdbjson['nodes'][ilink["target"]]["weight"] += ilink["value"]

                    if "HOH" not in pdbjson['nodes'][ilink["source"]]["name"] and "HOH" not in pdbjson['nodes'][ilink["target"]]["name"]:
                        GraphNoWater.AddEdge(ilink["source"], ilink["target"])
                    GraphWithWater.AddEdge(ilink["source"], ilink["target"])

                BtwHNoW = snap.TIntFltH()
                snap.GetBetweennessCentr(GraphNoWater, BtwHNoW, 1.0)
                BtwHW = snap.TIntFltH()
                snap.GetBetweennessCentr(GraphWithWater, BtwHW, 1.0)

                for idx, i in enumerate(pdbjson['nodes']):
                    for NI in GraphNoWater.Nodes():
                        if NI.GetId() == idx:
                            i["degree"] = NI.GetOutDeg()
                            i["betweeness"] = BtwHNoW.GetDat(NI.GetId())
                            i["closeness"] = round(snap.GetClosenessCentr(GraphNoWater, NI.GetId()), 3)
                            # print i["degree"]
                            break

                    for NI in GraphWithWater.Nodes():
                        if NI.GetId() == idx:
                            i["degreeW"] = NI.GetOutDeg()
                            i["betweenessW"] = BtwHW.GetDat(NI.GetId())
                            i["closenessW"] = snap.GetClosenessCentr(GraphWithWater, NI.GetId())
                            break

                residuesfilteredNodes[file[:-5]] = pdbjson['nodes']

                filename = subdirectory +"/"+ file
                with open(filename, 'w') as outfile:
                    json.dump(pdbjson, outfile, indent=2)

                sifF.close()

        elif file.endswith(".pdb"):
            pdb_file = os.path.join(path, file)
            shutil.copy2(pdb_file, subdirectory)
        elif file.endswith(".zip"):
            zip_file = os.path.join(path, file)
            # shutil.copy2(zip_file, subdirectory)

            foldername = file[:-4]
            zipfolder = os.path.join(subdirectory, foldername)

            if "_txt" in file:
                with zipfile.ZipFile(zip_file,"r") as zip_ref:
                    zip_ref.extractall(subdirectory)

                if not os.path.exists(zipfolder):
                    os.mkdir( zipfolder, 0755 )
            if "_rin" in file:
                shutil.copy2(zip_file, subdirectory)

            if "_sif" in file:
                if not os.path.exists(zipfolder):
                    os.mkdir( zipfolder, 0755 )

    for file in os.listdir(path):
        if "stats" in file:
            # print file + " stats"
            json_file = os.path.join(path, file)

            if "statswith" in file:
                filename = file.replace("statswithWater.json", "")
            else:
                filename = file[:-10]

            with open(json_file, 'r') as f:
                pdbjson = json.load(f)

            for idx, i in enumerate(residuesfilteredNodes[filename]):
                for idx2, i2 in enumerate(pdbjson['nodes']):
                    if i["name"] ==  i2["name"]:
                        pdbjson['nodes'][idx2]["weight"] = i["weight"]
                        if "statswith" in file:
                            pdbjson['nodes'][idx2]["degree"] = i["degreeW"]
                            pdbjson['nodes'][idx2]["betweeness"] = i["betweenessW"]
                            pdbjson['nodes'][idx2]["closeness"] = i["closenessW"]
                        else:
                            pdbjson['nodes'][idx2]["degree"] = i["degree"]
                            pdbjson['nodes'][idx2]["betweeness"] = i["betweeness"]
                            pdbjson['nodes'][idx2]["closeness"] = i["closeness"]


            filename = os.path.join(subdirectory, file)
            with open(filename, 'w') as outfile:
                json.dump(pdbjson, outfile, indent=2)

    # Open and read the main json file , eg 2HPY.json
    name = pdb + ".json"
    json_file = os.path.join(path, name)

    with open(json_file, 'r') as f:
        pdbjson = json.load(f)

    # for rl in residuesfilteredLinks:
    #     print rl
    #     for idx, ilink in enumerate(residuesfilteredLinks[rl]):
    #         if ilink["chains"] == "B->B":
    #             print ilink["chains"] + ilink["name"]

    for idx2, i2 in enumerate(pdbjson['nodes']):
        checkN = 0
        for r in residuesfilteredNodes:
            for idx, i in enumerate(residuesfilteredNodes[r]):
                if i["name"] ==  i2["name"] and i["chain"] ==  i2["chain"]:
                    pdbjson['nodes'][idx2]["weight"] = i["weight"]
                    checkN += 1
                    break;

        if checkN == 0:
            pdbjson['nodes'][idx2] = None

    y = [i for i in pdbjson['nodes'] if i != None]
    pdbjson['nodes'] = y

    for idxlink2, ilink2 in enumerate(pdbjson['links']):
        checkL = 0
        contacts_origin = ilink2["name"].split(",")[0]
        for rl in residuesfilteredLinks:
            for idx, ilink in enumerate(residuesfilteredLinks[rl]):
                contacts_updated = ilink["name"].split(",")[0]
                if ( contacts_origin == contacts_updated
                and ilink["chains"] ==  ilink2["chains"]):
                    # print rl
                    pdbjson['links'][idxlink2]["value"] = ilink["value"]
                    pdbjson['links'][idxlink2]["name"] = ilink["name"]
                    checkL += 1
                    break

        if checkL == 0:
            pdbjson['links'][idxlink2] = None

    y = [i for i in pdbjson['links'] if i != None]
    pdbjson['links'] = y

    filename = os.path.join(subdirectory, name)
    with open(filename, 'w') as outfile:
        json.dump(pdbjson, outfile, indent=2)
def get_closeness_centrality(G, n, directed=False):
    return snap.GetClosenessCentr(G, n, True, directed)
Exemple #28
0
def getAttribute(filename):
    UGraph = snap.LoadEdgeList(snap.PUNGraph, filename, 0, 1)
    UGraph.Dump()

    attributes = pd.DataFrame(np.zeros(shape=(UGraph.GetNodes(), 12)), 
                              columns=['Graph', 'Id', 'Degree', 'DegreeCentrality', 'NodeBetweennessCentrality', 
                                       'ClosenessCentrality', 'FarnessCentrality', 'PageRank', 'HubsScore', 
                                       'AuthoritiesScore', 'NodeEccentricity', 'EigenvectorCentrality'])
    
    attributes['Graph'] = [filename] * UGraph.GetNodes()
    
    # Degree
    id = []
    degree = []
    OutDegV = snap.TIntPrV()
    snap.GetNodeOutDegV(UGraph, OutDegV)
    for item in OutDegV:
        id.append(item.GetVal1())
        degree.append(item.GetVal2())
    attributes['Id'] = id
    attributes['Degree'] = degree

    # Degree, Closeness, Farness Centrality, Node Eccentricity
    degCentr = []
    cloCentr = []
    farCentr = []
    nodeEcc = []
    for NI in UGraph.Nodes():
        degCentr.append(snap.GetDegreeCentr(UGraph, NI.GetId()))
        cloCentr.append(snap.GetClosenessCentr(UGraph, NI.GetId()))
        farCentr.append(snap.GetFarnessCentr(UGraph, NI.GetId()))
        nodeEcc.append(snap.GetNodeEcc(UGraph, NI.GetId(), False))
    attributes['DegreeCentrality'] = degCentr
    attributes['ClosenessCentrality'] = cloCentr
    attributes['FarnessCentrality'] = farCentr
    attributes['NodeEccentricity'] = nodeEcc

    # Betweenness Centrality
    betCentr = []
    Nodes = snap.TIntFltH()
    Edges = snap.TIntPrFltH()
    snap.GetBetweennessCentr(UGraph, Nodes, Edges, 1.0)
    for node in Nodes:
        betCentr.append(Nodes[node])
    attributes['NodeBetweennessCentrality'] = betCentr

    # PageRank
    pgRank = []
    PRankH = snap.TIntFltH()
    snap.GetPageRank(UGraph, PRankH)
    for item in PRankH:
        pgRank.append(PRankH[item])
    attributes['PageRank'] = pgRank

    # Hubs, Authorities score 
    hubs = []
    auth = []
    NIdHubH = snap.TIntFltH()
    NIdAuthH = snap.TIntFltH()
    snap.GetHits(UGraph, NIdHubH, NIdAuthH)
    for item in NIdHubH:
        hubs.append(NIdHubH[item])
    for item in NIdAuthH:
        auth.append(NIdAuthH[item])
    attributes['HubsScore'] = hubs
    attributes['AuthoritiesScore'] = auth

    # Eigenvector Centrality
    eigenCentr = []
    NIdEigenH = snap.TIntFltH()
    snap.GetEigenVectorCentr(UGraph, NIdEigenH)
    for item in NIdEigenH:
        eigenCentr.append(NIdEigenH[item])
    attributes['EigenvectorCentrality'] = eigenCentr

    return attributes
Exemple #29
0
import snap

fbgraph = snap.LoadEdgeList(snap.PUNGraph, "facebook_combined.txt", 0, 1)
n = fbgraph.GetNodes()

close_list = []
for node in fbgraph.Nodes():
    NId = node.GetId()
    closeness_snap = snap.GetClosenessCentr(
        fbgraph, NId, True, False)  #Standard function for closeness centrality
    templist = []
    templist.append(NId)
    templist.append(closeness_snap)
    close_list.append(templist)

close_list.sort(key=lambda x: x[1], reverse=True)

closeness_overlap = 0

closenessfile = open("centralities/closeness.txt", "r")

for i in range(0, 100):
    line = closenessfile.readline()
    for j in range(0, len(line)):
        if line[j] == ' ':
            val = line[:j]
            break
    #Checking for overlaps
    for k in range(0, 100):
        if (int(close_list[k][0]) == int(val)):
            closeness_overlap += 1
filename = "facebook_combined.txt"

G = snap.LoadEdgeList(
    snap.PUNGraph, filename, 0, 1
)  #3rd and 4th parameters are column indices of source and destination nodes

#G is now the required undirected graph

n = G.GetNodes()
os.chdir("centralities")

#for closeness

cls_ib = snap.TIntFltH()  # closeness centrality inbuilt (dict)
for itr in G.Nodes():
    cls_ib[itr.GetId()] = snap.GetClosenessCentr(G, itr.GetId())
    cls_ib[itr.GetId()] = round(cls_ib[itr.GetId()], 6)

cls_ib.SortByDat(False)

cls = snap.TIntFltH()  #closeness centrality dict

filename = "closeness.txt"
f = open(filename, "r")

for line in f:
    l = line.split()
    cls[int(l[0])] = float(l[1])
f.close()

nodes_ib = list()