Ejemplo n.º 1
0
def main_library_compile(args):
    builder = GraphBuilder(args)

    for path_dir in glob(os.path.join(args.library, "*")):
        if os.path.isdir(path_dir):
            path_name = os.path.basename(path_dir)
            xgmml_path = os.path.join(path_dir, "graph.xgmml")
            handle = open(xgmml_path)
            gr = network_convert.read_xgmml(handle)
            handle.close()
            fix_gr = builder.fix_graph(gr)
            spf_path = os.path.join(path_dir, "graph.spf")
            log("Writing: %s" % (spf_path))
            handle = open(spf_path, "w")
            network_convert.write_spf(fix_gr, handle)
            handle.close()
Ejemplo n.º 2
0
def main_library_copy(args):
    builder = GraphBuilder(args)
    out_zip = zipfile.ZipFile(args.out, "w")
    out_base = re.sub( r'.zip$', '', os.path.basename(args.out))
    for path_dir in glob(os.path.join(args.library, "*")):
        if os.path.isdir(path_dir):
            path_name = os.path.basename(path_dir)
            xgmml_path = os.path.join(path_dir, "graph.xgmml")
            handle = open(xgmml_path)
            gr = network_convert.read_xgmml(handle)
            handle.close()
            fix_gr = builder.fix_graph(gr)
            log("Writing: %s" % (path_name))
            text = StringIO()
            network_convert.write_spf(fix_gr, text)
            if len(text.getvalue()) > 0:
                out_zip.writestr( os.path.join(out_base, path_name + ".spf"), text.getvalue())
    out_zip.close()
Ejemplo n.º 3
0
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()
Ejemplo n.º 4
0
        z = zipfile.ZipFile(args.cys)
        for n in z.namelist():
            if n.endswith(".xgmml") and os.path.dirname(n).endswith("networks"):
                handle = z.open(n)
                gr = convert.read_xgmml(handle)
                handle.close()
    """

    if gr is not None:
        if args.out_spf is not None:
            if args.out_spf == "-":
                ohandle = sys.stdout
            else:
                ohandle = open(args.out_spf, "w")
            convert.write_spf(gr, ohandle, 
                node_type_field=args.node_type_field, node_type_default=args.node_type_default, 
                edge_type_field=args.edge_type_field, edge_type_default=args.edge_type_default
            )
        if args.out_xgmml is not None:
            if args.out_xgmml == "-":
                ohandle = sys.stdout
            else:
                ohandle = open(args.out_xgmml, "w")
            convert.write_xgmml(gr, ohandle)
        if args.out_gmt:
            if args.out_gmt == "-":
                ohandle = sys.stdout
            else:
                ohandle = open(args.out_gmt, "w")

            pathmap = {}
            for node in gr.node: