def readEdgesFromNX(nxGraph, sqg): for edge in nxGraph.edges(): edgeAttrib = nxGraph[edge[0]][edge[1]] edgeArrayDict = edgeAttrib.copy() if "directed" in edgeAttrib: del edgeArrayDict['directed'] edgeArrayDict["outNode"] = edge[0] edgeArrayDict["inNode"] = edge[1] edgeType = 'directedEdge' else: edgeArrayDict["node1"] = edge[0] edgeArrayDict["node2"] = edge[1] edgeType = 'edge' if 'type' in edgeAttrib: edgeType = edgeAttrib['type'] del edgeArrayDict['type'] edgeArrayList = sqg.getArrayList(edgeType) if edgeArrayList is None: variables = None if edgeType not in AbstractArrayList._arrayListTypes: variables = getSQGVariablesFromNXAttributes(edgeArrayDict) edgeArrayList = InMemoryArrayList(type=edgeType, variables=variables) sqg.setArrayList(edgeArrayList) edgeArrayList.addDict(edgeArrayDict)
def readSubgraphsFromNX(nxGraph, nxSubgraphList, sqg): for nxSubgraph in nxSubgraphList: subgraphType = 'subgraph' edgeTypes = None subgraphAttrib = nxSubgraph.graph subgraphArrayDict = subgraphAttrib.copy() if 'type' in subgraphAttrib: subgraphType = subgraphAttrib['type'] del subgraphArrayDict['type'] if 'edgeTypes' in subgraphAttrib: edgeTypes = subgraphAttrib['edgeTypes'] del subgraphArrayDict['edgeTypes'] subgraphArrayDict['nodes'] = nxSubgraph.nodes() subgraphArrayList = sqg.getArrayList(subgraphType) if subgraphArrayList is None: variables, sharedVariables = None, None if subgraphType not in AbstractArrayList._arrayListTypes: variables = getSQGVariablesFromNXAttributes(subgraphArrayDict) if edgeTypes is not None: sharedVariables = dict() sharedVariables["edges"] = edgeTypes subgraphArrayList = InMemoryArrayList(type=subgraphType, sharedVariables=sharedVariables, variables=variables) sqg.setArrayList(subgraphArrayList) subgraphArrayList.addDict(subgraphArrayDict)
def readNodesFromNX(nxGraph, sqg): for node in nxGraph.nodes(): nodeType = 'node' nodeAttrib = nxGraph.node[node] nodeArrayDict = nodeAttrib.copy() if 'type' in nodeAttrib: nodeType = nodeAttrib['type'] del nodeArrayDict['type'] nodeArrayDict['nodeName'] = node nodeArrayList = sqg.getArrayList(nodeType) if nodeArrayList is None: variables = None if nodeType not in AbstractArrayList._arrayListTypes: variables = getSQGVariablesFromNXAttributes(nodeArrayDict) nodeArrayList = InMemoryArrayList(type=nodeType, variables=variables) sqg.setArrayList(nodeArrayList) nodeArrayList.addDict(nodeArrayDict)
from pysqg.sqg import Sqg sqg = Sqg(includes=[ "ungappedOverlapGraph" ], \ sharedVariables={ "Created":"Assemblathon File Format Working Group using Richard Durbin's example", "Date":"24/11/2011"}) from pysqg.arrayList import InMemoryArrayList nodes = InMemoryArrayList(type="node") sqg.setArrayList(nodes) sqg.getArrayList("node") nodes.getArrayNames() nodes.getArrayTypes() _1 = 0 nodes.addArray([ _1 ]) _2 = 1 nodes.addDict({ "nodeName":_2 }) from pysqg.arrayList import OnDiskArrayList segmentEdges = OnDiskArrayList(file="./segmentEdges", type="multiLabelledSegment") sqg.setArrayList(segmentEdges) segmentEdges.getArrayNames() segmentEdges.getArrayTypes() aL, aR = 2, 3 nodes.addArray([ aL ]) nodes.addArray([ aR ]) segmentEdges.addDict({ "inNode":aL, "outNode":aR, "length":10, \ "sequence":"acggtcagca", "degree":1 }) b1L, b1R = 4, 5 nodes.addArray([ b1L ]) nodes.addArray([ b1R ]) segmentEdges.addDict({ "inNode":b1L, "outNode":b1R, "length":6, \ "sequence":"catact", "degree":2 }) b2L, b2R = 6, 7 nodes.addArray([ b2L ]) nodes.addArray([ b2R ])
#Add them to the graph inheritanceGraph.setArrayList(arrayLists) inheritanceGraph.setArrayList(sqgs) inheritanceGraph.setArrayList(inheritEdges) inheritanceGraph.setArrayList(graphEdges) #Build the nodes and connect the graphs to the array types typeStringsToNodeNamesAndArrays = {} i = 0 for file in os.listdir(getPysqgIncludeDir()): if file[-5:] == ".json": Sqg(includes=[file[:-5]]) #This ensures the graphs are in memory graphNode = i i = i+1 sqgs.addDict({ "nodeName":graphNode, "name":file[:-5] }) #Now iterate through the new array list types fileHandle = open(os.path.join(getPysqgIncludeDir(), file), 'r') jsonSqg = json.load(fileHandle) fileHandle.close() for arrayListType in jsonSqg.keys(): if arrayListType not in ("name", "include", "parents", "sharedVariables"): #This is very hacky, it would be nice to have a clean way to know which sqg defined which array list. arrayList = InMemoryArrayList(type=arrayListType) arrayNode = i i = i+1 typeStringsToNodeNamesAndArrays[arrayListType] = (arrayNode, arrayList) arrayLists.addDict({ "nodeName":arrayNode, "name":arrayListType, "arrayNames":" ".join(arrayList.getArrayNames()), "arrayTypes":" ".join(arrayList.getArrayTypes()) }) graphEdges.addDict({ "outNode":graphNode, "inNode":arrayNode }) #Add in the inherits edges for typeString in typeStringsToNodeNamesAndArrays.keys():