Ejemplo n.º 1
0
def initializeSqg():
	sqg = Sqg(includes=['ungappedOverlapGraph'])
	nodes = InMemoryArrayList(type="node")
	segmentEdges = InMemoryArrayList(type="multiLabelledSegment")
	adjacencyEdges = InMemoryArrayList(type="overlapAdjacency")
	walks = InMemoryArrayList(type="walk", inherits="mixedSubgraph", variables=[ "start", "int", "stop", "int" ], sharedVariables={ "edges":[ "segmentEdge", "adjacencyEdge" ] })
	sqg.setArrayList(nodes)
	sqg.setArrayList(segmentEdges)
	sqg.setArrayList(adjacencyEdges)
	sqg.setArrayList(walks)
	return sqg
Ejemplo n.º 2
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 ])
Ejemplo n.º 3
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
Ejemplo n.º 4
0
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
        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')