if opts.network.endswith(".sif"): graph = convert.read_sif(open(opts.network)) else: graph = convert.read_spf(open(opts.network)) heats = parseHeats(opts.heats) if opts.cut_graph and not opts.node_attributes: cut_val = None try: cutoff = float(opts.cut_graph) except: raise Exception( "Error: value supplied to cut_graph must be a positive number") cutG = cutGraph(graph, heats, cutoff) convert.write_sif(cutG, sys.stdout) sys.exit(0) if opts.node_attributes: if not opts.cut_graph: print "Error: please supply a cut value" sys.exit(1) cutoff = None try: cutoff = float(opts.cut_graph) except: raise Exception( "Error: value supplied to cut_graph must be a positive number") cutG = cutGraph(graph, heats, cutoff) nodes = getNodes(cutG)
def main_build(args): gr = networkx.MultiDiGraph() paths = pathway_opener( list( (args.pathways[i],args.pathways[i+1]) for i in range(0, len(args.pathways),2) ) ) builer = GraphBuilder(args) type_count = {} interaction_count = {} duplicate_edges = 0 for cur_path in paths: log("Scanning: %s" % (cur_path.name)) cur_gr = cur_path.read() if args.organism is None or cur_gr.graph.get('organism', args.organism) == args.organism: fix_gr = builer.fix_graph(cur_gr) for node in fix_gr.node: if node in gr.node: if 'type' in gr.node[node] and 'type' in fix_gr.node[node] and gr.node[node]['type'] != fix_gr.node[node]['type']: error("%s failure: Mismatch Node Type: %s :%s --> %s" % (cur_path.name, node, gr.node[node]['type'], fix_gr.node[node]['type'] )) if args.rename_nonprotein or args.all: #because 'protein' is a default node type, if we see something not protein, then change the node to match if gr.node[node]['type'] == 'protein': gr.node[node]['type'] = fix_gr.node[node]['type'] else: log("Merging: %s" % node) gr.add_node(node, attr_dict=fix_gr.node[node]) for src, dst, data in fix_gr.edges(data=True): interaction = data['interaction'] src_node_type = fix_gr.node[src].get('type', None) dst_node_type = fix_gr.node[dst].get('type', None) if src in gr.node and dst in gr.node: has_edge = False if dst in gr.edge[src]: for i in gr.edge[src][dst]: if gr.edge[src][dst][i]['interaction'] == interaction: has_edge = True if not has_edge: if not (args.remove_self or args.all) or src != dst: gr.add_edge(src, dst, attr_dict=data ) else: log("Removing self loop: %s" % (src)) else: duplicate_edges += 1 connect_list = networkx.connected_components(networkx.Graph(gr)) rm_list = [] for group in connect_list: if len(group) < args.min_subgraph: rm_list.extend(group) for r in rm_list: gr.remove_node(r) log("+---------------------------+") log("|Node Count: %15d|" % (len(gr.nodes()))) log("|Edge Count: %15d|" % (len(gr.edges()))) log("|Duplicate Edges: %10d|" % (duplicate_edges)) log("|Connected Components: %5d|" % (networkx.number_connected_components(networkx.Graph(gr)))) log("+---------------------------+") if args.output: handle = open(args.output, "w") else: handle = sys.stdout for n_type in type_count: log("Node Type %s: %d" % (n_type, type_count[n_type])) if args.spf: network_convert.write_spf(gr, handle) elif args.sif: network_convert.write_sif(gr, handle) else: network_convert.write_xgmml(gr, handle) handle.close()
graph = None if opts.network.endswith(".sif"): graph = convert.read_sif(open(opts.network)) else: graph = convert.read_spf(open(opts.network)) heats = parseHeats(opts.heats) if opts.cut_graph and not opts.node_attributes: cut_val = None try: cutoff = float(opts.cut_graph) except: raise Exception("Error: value supplied to cut_graph must be a positive number") cutG = cutGraph(graph, heats, cutoff) convert.write_sif(cutG, sys.stdout) sys.exit(0) if opts.node_attributes: if not opts.cut_graph: print "Error: please supply a cut value" sys.exit(1) cutoff = None try: cutoff = float(opts.cut_graph) except: raise Exception("Error: value supplied to cut_graph must be a positive number") cutG = cutGraph(graph, heats, cutoff) nodes = getNodes(cutG) print "HeatsValue"