src/scBONITA/singleCell.py
def assignAttractors(
        self,
        pathwayFiles=[],
        useMinimalRuleSet=True,
        simSteps=10,
        removeZeroAttractors=True,
    ):

        """Takes a list of pathways to search the current working directory for the output of rule inference and to assign attractors for these networks.

        Parameters
        ----------

        pathwayFiles: list of strings
            Each string is a path to the processed graphml files for which rules have been inferred
        useMinimalRuleSet: Boolean.
            Whether to use the minimal rule set (True) or to randomly sample the ERS (False)
        simSteps: number of steps in the simulation to identify attractors
            The column in distanceDF which contains the attractors to which the cells have been assigned. Default is 'decider', as this is the name in the output of assignAttractors
        removeZeroAttractors: bool
            Whether or not to remove attractors that have a zero value for all genes

        Returns
        -------
            Dictionary of dataframes containing distance between attractors and cells for all pathways in pathwayFiles (if rule inference was successful for these pathways).
        """

        # get the networks used for rule inference - you can change this parameter when you call the function
        if len(pathwayFiles) == 0:
            pathwayFiles = glob.glob("*_processed.graphml")
        else:
            pathwayFiles = pathwayFiles
        # Reconstruct the singleCell object
        # objectFile = glob.glob("*.binscTest.pickle")
        # scObject = pickle.load(open(objectFile[0], "rb"))
        featureTable = {}  # record of assignment of cells to attractors
        enumerateDict = {}  # records attractor counts by network
        distanceDFDict = (
            {}
        )  # output - list of dataframes with distance between attractors and cells
        # iterate over all networks used for rule inference
        for pathway in pathwayFiles:
            # if path.exists(str(pathway + "_equivs1.pickle")) and not path.exists(
            #        str(pathway + "_attractorDistance_mgp.csv")
            # ):  #if rules were successfully inferred and assignAttractors has not yet been successfully executed
            if path.exists(str(pathway + "_equivs1.pickle")):
                print("#############")
                # Read in graph
                print(pathway)
                net = nx.read_graphml(pathway)
                # Create ruleMaker object
                self.__inherit(
                    net,
                    removeSelfEdges=False,
                    restrictIncomingEdges=True,
                    maxIncomingEdges=3,
                    groundTruth=False,
                )
                self.__setupEmptyKOKI()
                self._ruleMaker__updateCpointers()
                KOlist = []  # knocked-out genes
                KIlist = []  # knocked-in genes
                attractorList = []
                # individual = processERS(
                #    str(pathway + "_equivs1.pickle"))  #get a rule set #
                if useMinimalRuleSet:
                    individual = self.processERS_minimalRule(
                        str(pathway + "_equivs1.pickle")
                    )  # get a rule set
                else:
                    individual = self.processERS(
                        str(pathway + "_equivs1.pickle")
                    )  # get a rule set
                with open(pathway + "_minimal_rules.txt", "w") as text_file:
                    text_file.write(self.writeModel(individual, self))
                text_file.close()
                knockins = np.zeros(
                    len(self.nodeList), dtype=np.intc, order="C"
                )  # set up empty ko and ki genes
                knockouts = np.zeros(len(self.nodeList), dtype=np.intc, order="C")
                for knocker in KOlist:
                    knockouts[knocker] = 1
                for knocker in KIlist:
                    knockins[knocker] = 1
                # put objects in correct format for passing to C
                nodeIndividual = np.array(individual, dtype=np.intc, order="C")
                indLen = len(nodeIndividual)
                andNodes = self.andNodeList
                nodeNum = len(self.nodeList)
                andNodeInvert = self.andNodeInvertList
                individualParse = np.array(
                    self.individualParse, dtype=np.intc, order="C"
                )
                andLenList = np.array(self.andLenList, dtype=np.intc, order="C")
                nodePositions1 = self.nodePositions
                nodePositionsC = np.array(nodePositions1, dtype=np.intc, order="C")
                binMatC3 = np.array(
                    copy.deepcopy(self.binMat.toarray(order="C")),
                    order="C",
                    dtype=np.intc,
                )
                for sampleIndex in range(0, len(self.sampleList)):
                    vals = np.full(
                        shape=(simSteps, nodeNum),
                        fill_value=0,
                        dtype=np.intc,
                        order="C",
                    )
                    res = np.full(
                        shape=2, fill_value=2, dtype=np.intc, order="C"
                    )  # initiate output array
                    res = self._singleCell__cluster(
                        vals,
                        res,
                        sampleIndex,
                        nodeIndividual,
                        indLen,
                        nodeNum,
                        andLenList,
                        individualParse,
                        andNodes,
                        andNodeInvert,
                        simSteps,
                        knockouts,
                        knockins,
                        binMatC3,
                        self.nodePositions,
                    )
                    attractor = []
                    for temp in range(res[0], res[1] + 1):
                        attractor.append(tuple(vals[temp, :]))
                    attractorList.extend(attractor)
                # Find unique attractor states
                attractorList = [list(x) for x in set(tuple(x) for x in attractorList)]
                print(
                    str(len(attractorList))
                    + " unique attractors were identified for "
                    + pathway
                )
                if removeZeroAttractors:
                    # remove attractors that are all 0
                    testIfZero = [sum(x) for x in set(tuple(x) for x in attractorList)]
                    attractorList = [
                        attractorList[i]
                        for i in range(len(attractorList))
                        if testIfZero[i] > 0
                    ]
                attractorList = tuple(attractorList)
                # Calculate new Distance between chosen attractors and cells
                newdistanceDict = {}
                for i in range(0, len(self.sampleList)):
                    cell = str(self.sampleList[i])
                    # get value of cell
                    cellInitValue = self.binMat[:, i]
                    cellInitValue = cellInitValue.toarray()
                    cellInitValue = cellInitValue.tolist()
                    cellInitValue = [
                        cellInitValue[temp] for temp in list(nodePositions1)
                    ]
                    # set up data structure to store Hamming distance to attractors
                    newdistanceDict[cell] = []
                    for attr in attractorList:
                        newdistanceDict[cell].append(
                            sum(
                                [abs(i1 - i2[0]) for i1, i2 in zip(attr, cellInitValue)]
                            )
                        )
                # Change table to a pandas dataframe for easy readability and plotting
                distanceDF = pd.DataFrame.from_dict(newdistanceDict, orient="index")
                distanceDF.columns = [str(attr) for attr in attractorList]
                enumerateDict[pathway] = str(len(attractorList))
                # distanceDF['decider'] = [
                #    np.inf for i in range(0, len(distanceDF.index))
                # ]
                decider = distanceDF.idxmin(axis=1)
                decider = [distanceDF.columns.get_loc(temp) for temp in decider]
                distanceDF.loc[:, "deciderDistance"] = distanceDF.min(axis=1)
                distanceDF.loc[:, "decider"] = decider
                print(
                    str(len(set(decider)))
                    + " of these unique attractors mapped to cells for "
                    + pathway
                )
                distanceDF.to_csv(str(pathway + "_attractorDistance.csv"), index=True)
                distanceDFDict[pathway] = distanceDF
            else:
                print("No output from rule inference for " + pathway)
            return distanceDFDict
Takes a list of pathways to search the current working directory for the output of rule inference and to assign attractors for these networks.

        Parameters
        ----------

        pathwayFiles: list of strings
            Each string is a path to the processed graphml files for which rules have been inferred
        useMinimalRuleSet: Boolean.
            Whether to use the minimal rule set (True) or to randomly sample the ERS (False)
        simSteps: number of steps in the simulation to identify attractors
            The column in distanceDF which contains the attractors to which the cells have been assigned. Default is 'decider', as this is the name in the output of assignAttractors
        removeZeroAttractors: bool
            Whether or not to remove attractors that have a zero value for all genes

        Returns
        -------
            Dictionary of dataframes containing distance between attractors and cells for all pathways in pathwayFiles (if rule inference was successful for these pathways).
        
Takes a list of pathways to search the current working directory for the output of rule inference and to assign attractors for these networks.
Parameters

list of strings
Each string is a path to the processed graphml files for which rules have been inferred
useMinimalRuleSet: Boolean.
Whether to use the minimal rule set (True) or to randomly sample the ERS (False)
simSteps: number of steps in the simulation to identify attractors
The column in distanceDF which contains the attractors to which the cells have been assigned. Default is 'decider', as this is the name in the output of assignAttractors
removeZeroAttractors: bool
Whether or not to remove attractors that have a zero value for all genes

Returns

Dictionary of dataframes containing distance between attractors and cells for all pathways in pathwayFiles (if rule inference was successful for these pathways).
Takes a list of pathways to search the current working directory for the output of rule inference and to assign attractors for these networks . Parameters list of strings Each string is a path to the processed graphml files for which rules have been inferred useMinimalRuleSet : Boolean . Whether to use the minimal rule set ( True ) or to randomly sample the ERS ( False ) simSteps : number of steps in the simulation to identify attractors The column in distanceDF which contains the attractors to which the cells have been assigned . Default is ' decider ' as this is the name in the output of assignAttractors removeZeroAttractors : bool Whether or not to remove attractors that have a zero value for all genes Returns Dictionary of dataframes containing distance between attractors and cells for all pathways in pathwayFiles ( if rule inference was successful for these pathways ) .
def assignAttractors(
        self,
        pathwayFiles=[],
        useMinimalRuleSet=True,
        simSteps=10,
        removeZeroAttractors=True,
    ):
        if len(pathwayFiles) == 0:
            pathwayFiles = glob.glob("*_processed.graphml")
        else:
            pathwayFiles = pathwayFiles
        featureTable = {}  
        enumerateDict = {}  
        distanceDFDict = (
            {}
        )  
        for pathway in pathwayFiles:
            if path.exists(str(pathway + "_equivs1.pickle")):
                print("#############")
                print(pathway)
                net = nx.read_graphml(pathway)
                self.__inherit(
                    net,
                    removeSelfEdges=False,
                    restrictIncomingEdges=True,
                    maxIncomingEdges=3,
                    groundTruth=False,
                )
                self.__setupEmptyKOKI()
                self._ruleMaker__updateCpointers()
                KOlist = []  
                KIlist = []  
                attractorList = []
                if useMinimalRuleSet:
                    individual = self.processERS_minimalRule(
                        str(pathway + "_equivs1.pickle")
                    )  
                else:
                    individual = self.processERS(
                        str(pathway + "_equivs1.pickle")
                    )  
                with open(pathway + "_minimal_rules.txt", "w") as text_file:
                    text_file.write(self.writeModel(individual, self))
                text_file.close()
                knockins = np.zeros(
                    len(self.nodeList), dtype=np.intc, order="C"
                )  
                knockouts = np.zeros(len(self.nodeList), dtype=np.intc, order="C")
                for knocker in KOlist:
                    knockouts[knocker] = 1
                for knocker in KIlist:
                    knockins[knocker] = 1
                nodeIndividual = np.array(individual, dtype=np.intc, order="C")
                indLen = len(nodeIndividual)
                andNodes = self.andNodeList
                nodeNum = len(self.nodeList)
                andNodeInvert = self.andNodeInvertList
                individualParse = np.array(
                    self.individualParse, dtype=np.intc, order="C"
                )
                andLenList = np.array(self.andLenList, dtype=np.intc, order="C")
                nodePositions1 = self.nodePositions
                nodePositionsC = np.array(nodePositions1, dtype=np.intc, order="C")
                binMatC3 = np.array(
                    copy.deepcopy(self.binMat.toarray(order="C")),
                    order="C",
                    dtype=np.intc,
                )
                for sampleIndex in range(0, len(self.sampleList)):
                    vals = np.full(
                        shape=(simSteps, nodeNum),
                        fill_value=0,
                        dtype=np.intc,
                        order="C",
                    )
                    res = np.full(
                        shape=2, fill_value=2, dtype=np.intc, order="C"
                    )  
                    res = self._singleCell__cluster(
                        vals,
                        res,
                        sampleIndex,
                        nodeIndividual,
                        indLen,
                        nodeNum,
                        andLenList,
                        individualParse,
                        andNodes,
                        andNodeInvert,
                        simSteps,
                        knockouts,
                        knockins,
                        binMatC3,
                        self.nodePositions,
                    )
                    attractor = []
                    for temp in range(res[0], res[1] + 1):
                        attractor.append(tuple(vals[temp, :]))
                    attractorList.extend(attractor)
                attractorList = [list(x) for x in set(tuple(x) for x in attractorList)]
                print(
                    str(len(attractorList))
                    + " unique attractors were identified for "
                    + pathway
                )
                if removeZeroAttractors:
                    testIfZero = [sum(x) for x in set(tuple(x) for x in attractorList)]
                    attractorList = [
                        attractorList[i]
                        for i in range(len(attractorList))
                        if testIfZero[i] > 0
                    ]
                attractorList = tuple(attractorList)
                newdistanceDict = {}
                for i in range(0, len(self.sampleList)):
                    cell = str(self.sampleList[i])
                    cellInitValue = self.binMat[:, i]
                    cellInitValue = cellInitValue.toarray()
                    cellInitValue = cellInitValue.tolist()
                    cellInitValue = [
                        cellInitValue[temp] for temp in list(nodePositions1)
                    ]
                    newdistanceDict[cell] = []
                    for attr in attractorList:
                        newdistanceDict[cell].append(
                            sum(
                                [abs(i1 - i2[0]) for i1, i2 in zip(attr, cellInitValue)]
                            )
                        )
                distanceDF = pd.DataFrame.from_dict(newdistanceDict, orient="index")
                distanceDF.columns = [str(attr) for attr in attractorList]
                enumerateDict[pathway] = str(len(attractorList))
                decider = distanceDF.idxmin(axis=1)
                decider = [distanceDF.columns.get_loc(temp) for temp in decider]
                distanceDF.loc[:, "deciderDistance"] = distanceDF.min(axis=1)
                distanceDF.loc[:, "decider"] = decider
                print(
                    str(len(set(decider)))
                    + " of these unique attractors mapped to cells for "
                    + pathway
                )
                distanceDF.to_csv(str(pathway + "_attractorDistance.csv"), index=True)
                distanceDFDict[pathway] = distanceDF
            else:
                print("No output from rule inference for " + pathway)
            return distanceDFDict
def assignAttractors ( self , pathwayFiles = [ ] , useMinimalRuleSet = True , simSteps = 10 , removeZeroAttractors = True , ) : if len ( pathwayFiles ) == 0 : pathwayFiles = glob . glob ( "*_processed.graphml" ) else : pathwayFiles = pathwayFiles featureTable = { } enumerateDict = { } distanceDFDict = ( { } ) for pathway in pathwayFiles : if path . exists ( str ( pathway + "_equivs1.pickle" ) ) : print ( "#############" ) print ( pathway ) net = nx . read_graphml ( pathway ) self . __inherit ( net , removeSelfEdges = False , restrictIncomingEdges = True , maxIncomingEdges = 3 , groundTruth = False , ) self . __setupEmptyKOKI ( ) self . _ruleMaker__updateCpointers ( ) KOlist = [ ] KIlist = [ ] attractorList = [ ] if useMinimalRuleSet : individual = self . processERS_minimalRule ( str ( pathway + "_equivs1.pickle" ) ) else : individual = self . processERS ( str ( pathway + "_equivs1.pickle" ) ) with open ( pathway + "_minimal_rules.txt" , "w" ) as text_file : text_file . write ( self . writeModel ( individual , self ) ) text_file . close ( ) knockins = np . zeros ( len ( self . nodeList ) , dtype = np . intc , order = "C" ) knockouts = np . zeros ( len ( self . nodeList ) , dtype = np . intc , order = "C" ) for knocker in KOlist : knockouts [ knocker ] = 1 for knocker in KIlist : knockins [ knocker ] = 1 nodeIndividual = np . array ( individual , dtype = np . intc , order = "C" ) indLen = len ( nodeIndividual ) andNodes = self . andNodeList nodeNum = len ( self . nodeList ) andNodeInvert = self . andNodeInvertList individualParse = np . array ( self . individualParse , dtype = np . intc , order = "C" ) andLenList = np . array ( self . andLenList , dtype = np . intc , order = "C" ) nodePositions1 = self . nodePositions nodePositionsC = np . array ( nodePositions1 , dtype = np . intc , order = "C" ) binMatC3 = np . array ( copy . deepcopy ( self . binMat . toarray ( order = "C" ) ) , order = "C" , dtype = np . intc , ) for sampleIndex in range ( 0 , len ( self . sampleList ) ) : vals = np . full ( shape = ( simSteps , nodeNum ) , fill_value = 0 , dtype = np . intc , order = "C" , ) res = np . full ( shape = 2 , fill_value = 2 , dtype = np . intc , order = "C" ) res = self . _singleCell__cluster ( vals , res , sampleIndex , nodeIndividual , indLen , nodeNum , andLenList , individualParse , andNodes , andNodeInvert , simSteps , knockouts , knockins , binMatC3 , self . nodePositions , ) attractor = [ ] for temp in range ( res [ 0 ] , res [ 1 ] + 1 ) : attractor . append ( tuple ( vals [ temp , : ] ) ) attractorList . extend ( attractor ) attractorList = [ list ( x ) for x in set ( tuple ( x ) for x in attractorList ) ] print ( str ( len ( attractorList ) ) + " unique attractors were identified for " + pathway ) if removeZeroAttractors : testIfZero = [ sum ( x ) for x in set ( tuple ( x ) for x in attractorList ) ] attractorList = [ attractorList [ i ] for i in range ( len ( attractorList ) ) if testIfZero [ i ] > 0 ] attractorList = tuple ( attractorList ) newdistanceDict = { } for i in range ( 0 , len ( self . sampleList ) ) : cell = str ( self . sampleList [ i ] ) cellInitValue = self . binMat [ : , i ] cellInitValue = cellInitValue . toarray ( ) cellInitValue = cellInitValue . tolist ( ) cellInitValue = [ cellInitValue [ temp ] for temp in list ( nodePositions1 ) ] newdistanceDict [ cell ] = [ ] for attr in attractorList : newdistanceDict [ cell ] . append ( sum ( [ abs ( i1 - i2 [ 0 ] ) for i1 , i2 in zip ( attr , cellInitValue ) ] ) ) distanceDF = pd . DataFrame . from_dict ( newdistanceDict , orient = "index" ) distanceDF . columns = [ str ( attr ) for attr in attractorList ] enumerateDict [ pathway ] = str ( len ( attractorList ) ) decider = distanceDF . idxmin ( axis = 1 ) decider = [ distanceDF . columns . get_loc ( temp ) for temp in decider ] distanceDF . loc [ : , "deciderDistance" ] = distanceDF . min ( axis = 1 ) distanceDF . loc [ : , "decider" ] = decider print ( str ( len ( set ( decider ) ) ) + " of these unique attractors mapped to cells for " + pathway ) distanceDF . to_csv ( str ( pathway + "_attractorDistance.csv" ) , index = True ) distanceDFDict [ pathway ] = distanceDF else : print ( "No output from rule inference for " + pathway ) return distanceDFDict
Takes a list of pathways to search the current working directory for the output of rule inference and to assign attractors for these networks.
Takes a list of pathways to search the current working directory for the output of rule inference and to assign attractors for these networks .
"""Takes a list of pathways to search the current working directory for the output of rule inference and to assign attractors for these networks.

        Parameters
        ----------

        pathwayFiles: list of strings
            Each string is a path to the processed graphml files for which rules have been inferred
        useMinimalRuleSet: Boolean.
            Whether to use the minimal rule set (True) or to randomly sample the ERS (False)
        simSteps: number of steps in the simulation to identify attractors
            The column in distanceDF which contains the attractors to which the cells have been assigned. Default is 'decider', as this is the name in the output of assignAttractors
        removeZeroAttractors: bool
            Whether or not to remove attractors that have a zero value for all genes

        Returns
        -------
            Dictionary of dataframes containing distance between attractors and cells for all pathways in pathwayFiles (if rule inference was successful for these pathways).
        """ # get the networks used for rule inference - you can change this parameter when you call the function # Reconstruct the singleCell object # objectFile = glob.glob("*.binscTest.pickle") # scObject = pickle.load(open(objectFile[0], "rb")) # record of assignment of cells to attractors # records attractor counts by network # output - list of dataframes with distance between attractors and cells # iterate over all networks used for rule inference # if path.exists(str(pathway + "_equivs1.pickle")) and not path.exists( #        str(pathway + "_attractorDistance_mgp.csv") # ):  #if rules were successfully inferred and assignAttractors has not yet been successfully executed # Read in graph # Create ruleMaker object # knocked-out genes # knocked-in genes # individual = processERS( #    str(pathway + "_equivs1.pickle"))  #get a rule set # # get a rule set # get a rule set # set up empty ko and ki genes # put objects in correct format for passing to C # initiate output array # Find unique attractor states # remove attractors that are all 0 # Calculate new Distance between chosen attractors and cells # get value of cell # set up data structure to store Hamming distance to attractors # Change table to a pandas dataframe for easy readability and plotting # distanceDF['decider'] = [ #    np.inf for i in range(0, len(distanceDF.index)) # ]
self
pathwayFiles
useMinimalRuleSet
simSteps
removeZeroAttractors