Exemplo n.º 1
0
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)
Exemplo n.º 2
0
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)
Exemplo n.º 3
0
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) 
Exemplo n.º 4
0
def makeSqgFromJsonSqg(jsonSqg):
    """Make sqg object from json-sqg object
    """
    #Fn to parse out variables from top level json
    def fn(name, defaultObj):
        obj = defaultObj
        if name in jsonSqg:
            obj = jsonSqg[name]
            assert isinstance(obj, defaultObj.__class__)
        return obj
    
    name = None
    if "name" in jsonSqg:
        name = int(jsonSqg["name"])
    
    sqg = Sqg(includes=fn("include", []), 
              name=name, 
              parents=fn("parents", []), 
              sharedVariables=fn("sharedVariables", {}))
    
    arrayListTypes = [ arrayListType for arrayListType in jsonSqg.keys() if arrayListType not in \
                      ("name", "include", "parents", "sharedVariables") ]
    while len(arrayListTypes) > 0:
        arrayListType = arrayListTypes.pop()
        jsonArrayList = jsonSqg[arrayListType]
        assert isinstance(jsonArrayList, [].__class__)
        properties = jsonArrayList[0]
        
        def fn(name):
            if name in properties:
                return properties[name]
            return None
        
        inherits = fn("inherits")
        if inherits != None and inherits in arrayListTypes:
            arrayListTypes = [ arrayListType ] + arrayListTypes
            continue
        
        if len(jsonArrayList) not in (1, 2, 3):
            raise RuntimeError("Got an incorrect number of arguments for a json array list: %s %i" % (jsonArrayList, len(jsonArrayList)))
        
        if len(jsonArrayList) in (1, 2): #In memory
            arrayList = InMemoryArrayList(type=arrayListType, 
                                          inherits=inherits, 
                                          sharedVariables=fn("sharedVariables"), 
                                          variables=fn("variables"))
            if len(jsonArrayList) == 2:
                arrays = jsonArrayList[1]
                arrayWidth = arrayList.getArrayWidth()
                #Now parse in the different arrays
                assert len(arrays) % arrayWidth == 0 #Otherwise the length of the array is not divisible by the length of each entry
                for j in xrange(0,len(arrays),arrayWidth):
                    arrayList.addArray(arrays[j:j+arrayWidth])
        else: #On disk
            arrayList = OnDiskArrayList(file=jsonArrayList[2],
                                        type=arrayListType, 
                                        inherits=inherits, 
                                        sharedVariables=fn("sharedVariables"), 
                                        variables=fn("variables"))
        
        sqg.setArrayList(arrayList)
    return sqg
Exemplo n.º 5
0
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 ])
Exemplo n.º 6
0
"""Creates a sqg representation of the hierarchy of different ArrayLists as a forest with the graphs that define them also represented.
""" 

import os
import sys
import json
import networkx as NX
from pysqg.sqg import Sqg
from pysqg.arrayList import InMemoryArrayList
from pysqg.jsonSqg import makeJsonSqgFromSqg
from pysqg.shared import getPysqgIncludeDir
from pysqg.py.networkX import networkxWrite

inheritanceGraph = Sqg(includes=["mixedGraph"])
arrayLists = InMemoryArrayList(type="arrayLists", inherits="node", variables=[ "name", "string", "arrayNames", "string", "arrayTypes", "string" ]) #Nodes representing array list types
sqgs = InMemoryArrayList(type="sqgs", inherits="node", variables=[ "name", "string" ]) #Nodes representing sqg definitions
inheritEdges = InMemoryArrayList(type="inherit", inherits="directedEdge") #Edges showing inheritance between array lists
graphEdges = InMemoryArrayList(type="graph", inherits="directedEdge") #Edges showing which show which sqg defined which array list

#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