Beispiel #1
0
    def synchronizeFromCatalyst(self,
                                _catalystSubset,
                                _inducedSubgraph=False,
                                _twoStepAnalysis=False,
                                _operator='OR'):

        #self.substrateGraph.delAllSubGraphs()

        if _operator not in ['AND', 'OR']:
            raise EntanglementError(
                "Operator _operator value should be 'AND' or 'OR'", _operator)

        #appel du calcul a partir du sous graph concernant ces catalyseurs
        #parametrage (limiter a l'ensemble?)
        #1. retrouver tous les substrats concernes par le sous ensemble de catalyseurs
        #2. mesurer l'intrication sur ce sous ensemble de substrats limite au sous ensemble de catalyseurs
        #3. mesurer l'intrication du substrat avec tous leurs catalyseurs

        #on suppose que les catalyseurs sont fournis dans une liste en l'etat, on verra plus tard pour la souplesse
        #on va commencer par un 'ou' logique des catalyseurs sur les noeuds
        substrateSubgraph = None
        subsetAnalysis = None
        secondAnalysis = None
        nodeList = []
        edgeList = []

        for n in self.substrateGraph.getNodes():
            cList = self.catalystProperty[n].split(self.catalystSeparator)
            if _operator == 'OR':
                for c in _catalystSubset:
                    if c in cList:
                        nodeList.append(n)
                        break
            elif _operator == 'AND':
                containmentTest = [c in cList for c in _catalystSubset]
                if False not in containmentTest:
                    nodeList.append(n)

        if _inducedSubgraph:
            substrateSubgraph = self.substrateGraph.inducedSubgraph(nodeList)
        else:
            for e in self.substrateGraph.getEdges():
                cList = self.catalystProperty[e].split(self.catalystSeparator)
                if _operator == 'OR':
                    for c in _catalystSubset:
                        if c in cList:
                            edgeList.append(e)
                            break
                elif _operator == 'AND':
                    containmentTest = [c in cList for c in _catalystSubset]
                    if False not in containmentTest:
                        nodeList.append(e)

            subgraphSelector = self.substrateGraph.getLocalBooleanProperty(
                "_entanglementSelector")
            for n in nodeList:
                subgraphSelector[n] = True
            for e in edgeList:
                subgraphSelector[e] = True

            substrateSubgraph = self.substrateGraph.addSubGraph(
                subgraphSelector)
            if self.substrateGraph.existLocalProperty("_entanglementSelector"):
                self.substrateGraph.delLocalProperty("_entanglementSelector")
            if substrateSubgraph.existLocalProperty("_entanglementSelector"):
                substrateSubgraph.delLocalProperty("_entanglementSelector")

        if substrateSubgraph:
            subsetAnalysis = entanglementAnalysisLgt.EntanglementAnalysis(
                substrateSubgraph, self.catalystProperty,
                self.catalystSeparator, self.minCatalystNumber,
                self.weightProperty, _catalystSubset,
                self.constrainSubstrateTopologyFromCatalysts)

            if _twoStepAnalysis:
                secondAnalysis = entanglementAnalysisLgt.EntanglementAnalysis(
                    substrateSubgraph, self.catalystProperty,
                    self.catalystSeparator, self.minCatalystNumber,
                    self.weightProperty, self.catalystSubset,
                    self.constrainSubstrateTopologyFromCatalysts)
        return {
            'substrate': substrateSubgraph,
            'catalystAnalysis': subsetAnalysis,
            'secondAnalysis': secondAnalysis
        }
Beispiel #2
0
    def analyseGraph(self, jsonGraph = 0, weightProperty = ""):

        graph = self.substrate
        entanglementIntensity = 0
        entanglementHomogeneity = 0
        onlyOneNode = False
        onlySingleEdges = False
        print "in graphManager, the weight property: ",weightProperty

        if jsonGraph:
                nodeList = []
                graphNList = []
                #print 'the selection: ',jsonGraph
                for n in jsonGraph[u'nodes']:
                        nodeList.append(n[u'baseID'])

                baseIDP = self.substrate.getDoubleProperty("baseID")
                for n in self.substrate.getNodes():
                        if baseIDP[n] in nodeList:
                                graphNList.append(n)
                if len(graphNList) > 1:
                        graph = self.substrate.inducedSubGraph(graphNList)

                elif len(graphNList) == 1:
                        onlyOneNode = True
                        print "there is only one node in the selection"

        # this has to be set because of the clusterAnalysis script

        if not graph.existProperty("descripteurs"):                
                descP = graph.getStringProperty("descripteurs")
                o_descP = graph.getStringProperty("descriptors")
                for n in graph.getNodes():
                        descP[n] = o_descP[n]
                        #print 'node ', descP[n]
                for e in graph.getEdges():
                        descP[e] = o_descP[e]
                        #print 'edge ', descP[e]

        labelList = []

        if not onlyOneNode:                                        
            c = entanglementAnalysisLgt.EntanglementAnalysis(graph, "descripteurs", _weightProperty = weightProperty)

            if c.catalystGraph.numberOfNodes() > 0:
                resLen = [len(k) for k in c.catalystToEntIndex]
                mainComponent = resLen.index(max(resLen))
                

                entanglementIntensity = float(c.entanglementIntensity[mainComponent])
                entanglementHomogeneity = float(c.entanglementHomogeneity[mainComponent])

                ##### update for tulip-4.10
                #vL = c.catalystGraph.getLayoutProperty("viewLayout")
                #c.catalystGraph.computeLayoutProperty("GEM (Frick)", vL)
                layoutDataSet = tlp.getDefaultPluginParameters("GEM (Frick)", c.catalystGraph)
                c.catalystGraph.applyLayoutAlgorithm("GEM (Frick)", layoutDataSet)

                tName = c.catalystGraph.getStringProperty("catalystName")
                label = c.catalystGraph.getStringProperty("label")
                baseID = c.catalystGraph.getDoubleProperty("baseID")
                #occ = c.catalystGraph.getStringProperty("occurence")


                labelToCatalystGraphNode = {}
                labelToCatalystGraphEdge = {}
                # sets the baseID for persistence
                # this should be changed at some point because it is computationnally heavy
                for n in c.catalystGraph.getNodes():
                        label[n] = tName[n]
                        baseID[n] = n.id
                        labelToCatalystGraphNode[tName[n]] = n

                for e in c.catalystGraph.getEdges():
                        baseID[e] = e.id
                        sL = label[c.catalystGraph.source(e)]
                        tL = label[c.catalystGraph.target(e)]
                        edgeLabel = sL+';'+tL
                        if sL > tL:
                            edgeLabel = tL+';'+sL
                        label[e] = edgeLabel
                        labelToCatalystGraphEdge[edgeLabel] = e

                #for n in c.catalystGraph.getNodes():
                #for n in c.catalystGraph.getEdges():
                #    print "baseID:", baseID[n], " label:",label[n]
            
                labelList = [label[n] for n in c.catalystGraph.getNodes()]
                labelEList = [label[e] for e in c.catalystGraph.getEdges()]


                # associates the right baseIDs
                if self.catalyst:
                    labelCatalystP = self.catalyst.getStringProperty("label")
                    baseIDCatalystP = self.catalyst.getDoubleProperty("baseID")
                    
                    nbAssign = 0
                    for n in self.catalyst.getNodes():
                        if labelCatalystP[n] in labelList:
                            baseID[labelToCatalystGraphNode[labelCatalystP[n]]] = baseIDCatalystP[n]
                            nbAssign += 1
                        if nbAssign == len(labelList):
                            break

                    nbAssign = 0
                    for e in self.catalyst.getEdges():
                        edgeLabel = labelCatalystP[e]
                        if edgeLabel in labelEList:
                            baseID[labelToCatalystGraphEdge[edgeLabel]] = baseIDCatalystP[e] 
                            nbAssign += 1
                        if nbAssign == len(labelEList):
                            break       

                    #baseIDcopy = lambda n: baseID[labelToCatalystGraphNode[labelCatalyst[n]]] = baseIDCatalystP[n] 
                    #[baseIDcopy(n) for n in self.catalyst.getNodes() if labelCatalyst[n] in labelList]        

                #returnGraph = c.typeGraph


                if jsonGraph:
                        return [c.catalystGraph, entanglementIntensity, entanglementHomogeneity]

                if not self.catalyst:
                        self.catalyst = tlp.newGraph()
                else:
                        self.catalyst.clear()

                tlp.copyToGraph(self.catalyst, c.catalystGraph)
               

                return [self.catalyst, entanglementIntensity, entanglementHomogeneity]



            else:
                onlySingleEdges = True



        if onlyOneNode or onlySingleEdges:
            descP = graph.getStringProperty("descripteurs")
            returnGraph = tlp.newGraph()
            labelList = []
            if onlyOneNode:
                labelList = descP[graphNList[0]].split(';')
            if onlySingleEdges:
                descP = graph.getStringProperty("descripteurs")
                for e in graph.getEdges():
                    labelList.extend(descP[e].split(';'))
                labelList = list(set(labelList))

            if self.catalyst:
                labelCatalystP = self.catalyst.getStringProperty("label")
                baseIDCatalystP = self.catalyst.getDoubleProperty("baseID")
                label = returnGraph.getStringProperty("label")
                baseID = returnGraph.getDoubleProperty("baseID")

                nbAssign = 0
                for n in self.catalyst.getNodes():
                    if labelCatalystP[n] in labelList:
                        nn = returnGraph.addNode()
                        label[nn] = labelCatalystP[n]
                        baseID[nn] = baseIDCatalystP[n]
                        nbAssign += 1
                    if nbAssign == len(labelList):
                        break

            if jsonGraph:
                    return [returnGraph, entanglementIntensity, entanglementHomogeneity]

            if not self.catalyst:
                    self.catalyst = tlp.newGraph()
            else:
                    self.catalyst.clear()

            return [self.catalyst, entanglementIntensity, entanglementHomogeneity]
Beispiel #3
0
    def synchronizeFromSubstrate(self,
                                 _substrateSubset,
                                 _idProperty=None,
                                 _inducedSubgraph=False,
                                 _uncompleteGraphHandler='REMOVE'):
        #plus simple, appel du calcul sur le sous graphe
        #tester _substrateSubset: liste de noeuds, sous-graphe, liste d'ids
        substrateSubgraph = None
        substrateNodeList = []

        if _uncompleteGraphHandler not in ['REMOVE', 'ADD', 'ERROR']:
            raise EntanglementError(
                "_uncompleteGraphHandlerValue should be 'REMOVE' 'ADD' or 'ERROR'",
                _operator)

        # a tlp.Graph defines the subset
        if isinstance(_substrateSubset, tlp.Graph):
            #verifier que _substrateSubset est bien un sous graphe de self.substrateGraph
            nodeList = []
            for n in _substrateSubset.getNodes():
                if not self.substrateGraph.isElement(n):
                    raise EntanglementError(
                        "Some nodes do not belong to the original substrate graph",
                        _substrateSubset)
                nodeList.append(n)

            for e in _substrateSubset.getEdges():
                if not self.substrateGraph.isElement(e):
                    raise EntanglementError(
                        "Some edges do not belong to the original substrate graph",
                        _substrateSubset)
                if self.substrateGraph.source(
                        e) not in nodeList or self.substrateGraph.target(
                            e) not in nodeList:
                    if _uncompleteGraphHandler == 'ERROR':
                        raise EntanglementError(
                            "Some edges are not connected to a pair of nodes",
                            _substrateSubset)
                    elif _uncompleteGraphHandler == 'ADD':
                        if self.substrateGraph.source(e) not in nodeList:
                            nodeList.append(self.substrateGraph.source(e))
                            _substrateSubset.addNode(
                                self.substrateGraph.source(e))
                        if self.substrateGraph.target(e) not in nodeList:
                            nodeList.append(self.substrateGraph.target(e))
                            _substrateSubset.addNode(
                                self.substrateGraph.target(e))
                    elif _uncompleteGraphHandler == 'REMOVE':
                        _substrateSubset.delEdge(e)

                    #we can add an option to discard those edges or add up some nodes?

            substrateSubgraph = _substrateSubset
            substrateNodeList = [n for n in substrateSubgraph.getNodes()]

        # a list of nodes (or ids) defines the subset (should we be able to give a list of edges as well?)
        elif isinstance(_substrateSubset, list) and len(_substrateSubset) > 0:

            if isinstance(_idProperty, str) or isinstance(
                    _idProperty, unicode):
                if not self.substrateGraph.existProperty(_idProperty):
                    raise EntanglementError(
                        "The given property for Ids does not belong to the original substrate graph",
                        _idProperty)

                idType = graph.getProperty(_idProperty).getTypename()

                if idType == "string":
                    _idProperty = self.substrateGraph.getStringProperty(
                        _idProperty)
                elif idType == "double":
                    _idProperty = self.substrateGraph.getDoubleProperty(
                        _idProperty)
                else:
                    raise EntanglementError(
                        "The given property for Ids is of no matching type",
                        [_idProperty, idType])

            if isinstance(_substrateSubset[0], tlp.node):
                for n in _substrateSubset:
                    if not self.substrateGraph.isElement(n):
                        raise EntanglementError(
                            "Some nodes do not belong to the original substrate graph",
                            _substrateSubset)
                substrateNodeList = _substrateSubset

            elif isinstance(_substrateSubset[0], numbers.Number):
                if not _idProperty and not isinstance(_substrateSubset[0],
                                                      float):
                    idToNode = {
                        n.id: n
                        for n in self.substrateGraph.getNodes()
                    }
                    for i in _substrateSubset:
                        if not i in idToNode:
                            raise EntanglementError(
                                "The node do not belong to the original substrate graph",
                                i)
                        substrateNodeList.append(idToNode[i])

                elif isinstance(_idProperty, tlp.DoubleProperty):
                    if not self.substrateGraph.existProperty(
                            _idProperty.getName()):
                        raise EntanglementError(
                            "The property does not belong to the original substrate graph",
                            _idProperty)
                    idToNode = {
                        _idProperty[n]: n
                        for n in self.substrateGraph.getNodes()
                    }
                    if len(idToNode) != self.substrateGraph.numberOfNodes():
                        raise EntanglementError(
                            "Multiple instances of the same id(s), right property?",
                            _idProperty)

                    for i in _substrateSubset:
                        if not i in idToNode:
                            raise EntanglementError(
                                "The node do not belong to the original substrate graph",
                                i)
                        substrateNodeList.append(idToNode[i])
                else:
                    raise EntanglementError(
                        "'Float' ids with wrong type of property",
                        [_substrateSubset, _idProperty])

            elif isinstance(_substrateSubset[0], str) or isinstance(
                    _substrateSubset[0], unicode):
                if not isinstance(_idProperty, tlp.StringProperty):
                    raise EntanglementError(
                        "'String' ids with wrong type of property",
                        [_substrateSubset, _idProperty])

                if not self.substrateGraph.existProperty(
                        _idProperty.getName()):
                    raise EntanglementError(
                        "The property does not belong to the original substrate graph",
                        _idProperty)

                idToNode = {
                    _idProperty[n]: n
                    for n in self.substrateGraph.getNodes()
                }
                if len(idToNode) != self.substrateGraph.numberOfNodes():
                    raise EntanglementError(
                        "Multiple instances of the same id(s), right property?",
                        _idProperty)

                for i in _substrateSubset:
                    if not i in idToNode:
                        raise EntanglementError(
                            "The node do not belong to the original substrate graph",
                            i)
                    substrateNodeList.append(idToNode[i])

            else:
                raise EntanglementError("Cannot handle ids type",
                                        _substrateSubset)

            _inducedSubgraph = True

        else:
            raise EntanglementError(
                "Cannot handle subset type, should be graph or list",
                _substrateSubset)

        if _inducedSubgraph:
            substrateSubgraph = self.substrateGraph.inducedSubgraph(
                substrateNodeList)

        subsetAnalysis = entanglementAnalysisLgt.EntanglementAnalysis(
            substrateSubgraph, self.catalystProperty, self.catalystSeparator,
            self.minCatalystNumber, self.weightProperty, self.catalystSubset,
            self.constrainSubstrateTopologyFromCatalysts)
        return {'catalystAnalysis': subsetAnalysis}