src/scBONITA/keggParser.py
def find_pathways_kegg(
    geneList, preDefList=[], writeGraphml=True, organism="hsa", minimumOverlap=1
):

    """
    geneList = the list of genes included in dataset
    writeGraphml = whether or not to write out a graphml (usually true)
    organism = organism code from kegg. Eg human = 'hsa', mouse = 'mus'
    """
    koDict = parseKEGGdict()  # parse the dictionary of ko codes
    try:  # try to retrieve and parse the dictionary containing organism gene names to codes conversion
        url = requests.get("http://rest.kegg.jp/list/" + organism, stream=True)
        # reads KEGG dictionary of identifiers between numbers and actual protein names and saves it to a python dictionary
        aliasDict = {}
        orgDict = {}
        for line in url.iter_lines():
            line = line.decode("utf-8")
            line_split = line.split("\t")
            k = line_split[0].split(":")[1]
            nameline = line_split[1].split(";")
            name = nameline[0]
            if "," in name:
                nameline = name.split(",")
                name = nameline[0]
                for entry in range(1, len(nameline)):
                    aliasDict[nameline[entry].strip()] = name.upper()
            orgDict[k] = name
    except:
        print("Could not get library: " + organism)
    k = KEGG()  # read KEGG from bioservices
    k.organism = organism
    if len(preDefList) == 0:
        pathwayList = list(k.pathwayIds)
    else:
        pathwayList = list(preDefList)
    print(pathwayList)
    pathwayDict = {}
    for x in pathwayList:
        x = x.replace("path:", "")
        code = str(x)
        code = re.sub(
            "[a-zA-Z]+", "", code
        )  # eliminate org letters - retain only numbers from KEGG pathway codes
        origCode = code
        coder = str("ko" + code)  # add ko
        graph = nx.DiGraph()  # open a graph object
        # get ko pathway
        for code in [coder]:
            print(code)
            try:
                url = requests.get(
                    "http://rest.kegg.jp/get/" + code + "/kgml", stream=True
                )
                text = [line.decode("utf-8") for line in url.iter_lines()]
            except:
                print("could not read code: " + code)
                continue
            # read kegg
            graph = readKEGG(text, graph, koDict, orgDict)
        coder = str(organism + origCode)  # set up with org letters
        # get org pathway
        text = []
        for code in [coder]:
            try:
                url = requests.get(
                    "http://rest.kegg.jp/get/" + code + "/kgml", stream=True
                )
                text = []
                for line in url.iter_lines():
                    line = line.decode("utf-8")
                    text.append(line)
            except:
                print("could not read code: " + code)
                continue
            # read kegg
            graph = readKEGG(text, graph, koDict, orgDict)

        # remove complexes and rewire components
        removeNodeList = [x for x in graph.nodes() if "-" in x]
        for rm in removeNodeList:
            for start in graph.predecessors(rm):
                edge1 = graph.get_edge_data(start, rm)["signal"]
                if edge1 == "i":
                    for element in rm.split("-"):
                        graph.add_edge(start, element, signal="i")
                else:
                    for element in rm.split("-"):
                        graph.add_edge(start, element, signal="a")
            for finish in graph.successors(rm):
                edge2 = graph.get_edge_data(rm, finish)["signal"]
                if edge2 == "i":
                    for element in rm.split("-"):
                        graph.add_edge(element, finish, signal="i")
                else:
                    for element in rm.split("-"):
                        graph.add_edge(element, finish, signal="a")
            graph.remove_node(rm)
        # remove dependence of nodes on complexes that include that node
        for node in list(graph.nodes()):
            predlist = graph.predecessors(node)
            for pred in predlist:
                if "-" in pred:
                    genes = pred.split("-")
                    flag = True
                    for gene in genes:
                        if not gene in predlist:
                            flag = False
                    if flag:
                        graph.remove_edge(pred, node)
        # remove self edges
        for edge in list(graph.edges()):
            if edge[0] == edge[1]:
                graph.remove_edge(edge[0], edge[1])
        # check to see if there is a connected component, simplify graph and print if so
        allNodes = set(graph.nodes())
        test = len(allNodes.intersection(geneList))
        print("Pathway: ", x, " Overlap: ", test, " Edges: ", len(graph.edges()))
        # nx.write_graphml(graph,coder+'.graphml')
        if (
            test > minimumOverlap and len(graph.edges()) > 0
        ):  # if there are at least minimumOverlap genes shared between the network and the genes in the dataset
            # nx.write_graphml(graph,coder+'_processed.graphml') # write graph out
            nx.write_graphml(graph, coder + ".graphml")
            print(
                "nodes: ",
                str(len(graph.nodes())),
                ",   edges:",
                str(len(graph.edges())),
            )
            # print(graph.nodes())
            pathwayDict[code] = graph
    return pathwayDict

    geneList = the list of genes included in dataset
    writeGraphml = whether or not to write out a graphml (usually true)
    organism = organism code from kegg. Eg human = 'hsa', mouse = 'mus'
    
geneList = the list of genes included in dataset
writeGraphml = whether or not to write out a graphml (usually true)
organism = organism code from kegg. Eg human = 'hsa', mouse = 'mus'
geneList = the list of genes included in dataset writeGraphml = whether or not to write out a graphml ( usually true ) organism = organism code from kegg . Eg human = ' hsa ' mouse = ' mus '
def find_pathways_kegg(
    geneList, preDefList=[], writeGraphml=True, organism="hsa", minimumOverlap=1
):
    koDict = parseKEGGdict()  
    try:  
        url = requests.get("http://rest.kegg.jp/list/" + organism, stream=True)
        aliasDict = {}
        orgDict = {}
        for line in url.iter_lines():
            line = line.decode("utf-8")
            line_split = line.split("\t")
            k = line_split[0].split(":")[1]
            nameline = line_split[1].split(";")
            name = nameline[0]
            if "," in name:
                nameline = name.split(",")
                name = nameline[0]
                for entry in range(1, len(nameline)):
                    aliasDict[nameline[entry].strip()] = name.upper()
            orgDict[k] = name
    except:
        print("Could not get library: " + organism)
    k = KEGG()  
    k.organism = organism
    if len(preDefList) == 0:
        pathwayList = list(k.pathwayIds)
    else:
        pathwayList = list(preDefList)
    print(pathwayList)
    pathwayDict = {}
    for x in pathwayList:
        x = x.replace("path:", "")
        code = str(x)
        code = re.sub(
            "[a-zA-Z]+", "", code
        )  
        origCode = code
        coder = str("ko" + code)  
        graph = nx.DiGraph()  
        for code in [coder]:
            print(code)
            try:
                url = requests.get(
                    "http://rest.kegg.jp/get/" + code + "/kgml", stream=True
                )
                text = [line.decode("utf-8") for line in url.iter_lines()]
            except:
                print("could not read code: " + code)
                continue
            graph = readKEGG(text, graph, koDict, orgDict)
        coder = str(organism + origCode)  
        text = []
        for code in [coder]:
            try:
                url = requests.get(
                    "http://rest.kegg.jp/get/" + code + "/kgml", stream=True
                )
                text = []
                for line in url.iter_lines():
                    line = line.decode("utf-8")
                    text.append(line)
            except:
                print("could not read code: " + code)
                continue
            graph = readKEGG(text, graph, koDict, orgDict)
        removeNodeList = [x for x in graph.nodes() if "-" in x]
        for rm in removeNodeList:
            for start in graph.predecessors(rm):
                edge1 = graph.get_edge_data(start, rm)["signal"]
                if edge1 == "i":
                    for element in rm.split("-"):
                        graph.add_edge(start, element, signal="i")
                else:
                    for element in rm.split("-"):
                        graph.add_edge(start, element, signal="a")
            for finish in graph.successors(rm):
                edge2 = graph.get_edge_data(rm, finish)["signal"]
                if edge2 == "i":
                    for element in rm.split("-"):
                        graph.add_edge(element, finish, signal="i")
                else:
                    for element in rm.split("-"):
                        graph.add_edge(element, finish, signal="a")
            graph.remove_node(rm)
        for node in list(graph.nodes()):
            predlist = graph.predecessors(node)
            for pred in predlist:
                if "-" in pred:
                    genes = pred.split("-")
                    flag = True
                    for gene in genes:
                        if not gene in predlist:
                            flag = False
                    if flag:
                        graph.remove_edge(pred, node)
        for edge in list(graph.edges()):
            if edge[0] == edge[1]:
                graph.remove_edge(edge[0], edge[1])
        allNodes = set(graph.nodes())
        test = len(allNodes.intersection(geneList))
        print("Pathway: ", x, " Overlap: ", test, " Edges: ", len(graph.edges()))
        if (
            test > minimumOverlap and len(graph.edges()) > 0
        ):  
            nx.write_graphml(graph, coder + ".graphml")
            print(
                "nodes: ",
                str(len(graph.nodes())),
                ",   edges:",
                str(len(graph.edges())),
            )
            pathwayDict[code] = graph
    return pathwayDict
def find_pathways_kegg ( geneList , preDefList = [ ] , writeGraphml = True , organism = "hsa" , minimumOverlap = 1 ) : koDict = parseKEGGdict ( ) try : url = requests . get ( "http://rest.kegg.jp/list/" + organism , stream = True ) aliasDict = { } orgDict = { } for line in url . iter_lines ( ) : line = line . decode ( "utf-8" ) line_split = line . split ( "\t" ) k = line_split [ 0 ] . split ( ":" ) [ 1 ] nameline = line_split [ 1 ] . split ( ";" ) name = nameline [ 0 ] if "," in name : nameline = name . split ( "," ) name = nameline [ 0 ] for entry in range ( 1 , len ( nameline ) ) : aliasDict [ nameline [ entry ] . strip ( ) ] = name . upper ( ) orgDict [ k ] = name except : print ( "Could not get library: " + organism ) k = KEGG ( ) k . organism = organism if len ( preDefList ) == 0 : pathwayList = list ( k . pathwayIds ) else : pathwayList = list ( preDefList ) print ( pathwayList ) pathwayDict = { } for x in pathwayList : x = x . replace ( "path:" , "" ) code = str ( x ) code = re . sub ( "[a-zA-Z]+" , "" , code ) origCode = code coder = str ( "ko" + code ) graph = nx . DiGraph ( ) for code in [ coder ] : print ( code ) try : url = requests . get ( "http://rest.kegg.jp/get/" + code + "/kgml" , stream = True ) text = [ line . decode ( "utf-8" ) for line in url . iter_lines ( ) ] except : print ( "could not read code: " + code ) continue graph = readKEGG ( text , graph , koDict , orgDict ) coder = str ( organism + origCode ) text = [ ] for code in [ coder ] : try : url = requests . get ( "http://rest.kegg.jp/get/" + code + "/kgml" , stream = True ) text = [ ] for line in url . iter_lines ( ) : line = line . decode ( "utf-8" ) text . append ( line ) except : print ( "could not read code: " + code ) continue graph = readKEGG ( text , graph , koDict , orgDict ) removeNodeList = [ x for x in graph . nodes ( ) if "-" in x ] for rm in removeNodeList : for start in graph . predecessors ( rm ) : edge1 = graph . get_edge_data ( start , rm ) [ "signal" ] if edge1 == "i" : for element in rm . split ( "-" ) : graph . add_edge ( start , element , signal = "i" ) else : for element in rm . split ( "-" ) : graph . add_edge ( start , element , signal = "a" ) for finish in graph . successors ( rm ) : edge2 = graph . get_edge_data ( rm , finish ) [ "signal" ] if edge2 == "i" : for element in rm . split ( "-" ) : graph . add_edge ( element , finish , signal = "i" ) else : for element in rm . split ( "-" ) : graph . add_edge ( element , finish , signal = "a" ) graph . remove_node ( rm ) for node in list ( graph . nodes ( ) ) : predlist = graph . predecessors ( node ) for pred in predlist : if "-" in pred : genes = pred . split ( "-" ) flag = True for gene in genes : if not gene in predlist : flag = False if flag : graph . remove_edge ( pred , node ) for edge in list ( graph . edges ( ) ) : if edge [ 0 ] == edge [ 1 ] : graph . remove_edge ( edge [ 0 ] , edge [ 1 ] ) allNodes = set ( graph . nodes ( ) ) test = len ( allNodes . intersection ( geneList ) ) print ( "Pathway: " , x , " Overlap: " , test , " Edges: " , len ( graph . edges ( ) ) ) if ( test > minimumOverlap and len ( graph . edges ( ) ) > 0 ) : nx . write_graphml ( graph , coder + ".graphml" ) print ( "nodes: " , str ( len ( graph . nodes ( ) ) ) , ",   edges:" , str ( len ( graph . edges ( ) ) ) , ) pathwayDict [ code ] = graph return pathwayDict
geneList = the list of genes included in dataset
writeGraphml = whether or not to write out a graphml (usually true)
organism = organism code from kegg.
geneList = the list of genes included in dataset writeGraphml = whether or not to write out a graphml ( usually true ) organism = organism code from kegg .
"""
    geneList = the list of genes included in dataset
    writeGraphml = whether or not to write out a graphml (usually true)
    organism = organism code from kegg. Eg human = 'hsa', mouse = 'mus'
    """ # parse the dictionary of ko codes
 # try to retrieve and parse the dictionary containing organism gene names to codes conversion
 # reads KEGG dictionary of identifiers between numbers and actual protein names and saves it to a python dictionary
 # read KEGG from bioservices
 # eliminate org letters - retain only numbers from KEGG pathway codes
 # add ko
 # open a graph object
 # get ko pathway
 # read kegg
 # set up with org letters
 # get org pathway
 # read kegg
 # remove complexes and rewire components
 # remove dependence of nodes on complexes that include that node
 # remove self edges
 # check to see if there is a connected component, simplify graph and print if so
 # nx.write_graphml(graph,coder+'.graphml')
 # if there are at least minimumOverlap genes shared between the network and the genes in the dataset
 # nx.write_graphml(graph,coder+'_processed.graphml') # write graph out
 # print(graph.nodes())

geneList
preDefList
writeGraphml
organism
minimumOverlap