Converting NetworkX Graph object into Cytoscape format

This is for a SNP distance matrix weighted graph. A symmetrical matrix contains the number of SNP differences from reference for X samples. Data is read in from a tab separated file, inversed to become an adjacency matrix for NetworkX import function from_pandas_adjacency(), and force-directed Fruchterman Reingold layout calculated. Edges are trimmed to avoid symmetrical source/targets and self source/targets. The 2000 multiplier for node position is determined manually to improve graph display and node overlap; automatic method preferred but not implemented.

import networkx as nx
import pandas as pd
import os

def genNetworkCyto(species, ST, date):
    input = date + '/' + genDistName(species, ST)
    if not (os.path.exists(input) and os.path.isfile(input)):
        print("species/st not in selected date")
        return None

    A = pd.read_csv(input, sep='\t', index_col=0, header=0)

    def inverse(x):
        if x == 0:
            return 1
        else:
            return 1/x

    Ai = A.applymap(inverse)
    G = nx.from_pandas_adjacency(Ai)
    pos=nx.fruchterman_reingold_layout(G, iterations=2000, threshold=1e-10)

    nodes = [
        {
            'data': {'id': node, 'label': node},
            'position': {'x': 2000*pos[node][0], 'y': 2000*pos[node][1]},
            'locked': 'true'
        }
        for node in G.nodes
    ]

    edges = []
    for col in A:
        for row, value in A[col].iteritems():
            if {'data': {'source': row, 'target': col}} not in edges and row != col:
                edges.append({'data': {'source': col, 'target': row}})

    for edge in edges:
        edge['data']['weight'] = A.loc[edge['data']['source'], edge['data']['target']]

    elements = nodes + edges

    return elements
1 Like