src/scBONITA/singleCell.py
def __scorePathway(self, RAs, pathImportances, pathname):
        """calculate z score for a given pathway"""
        CVdict = {}
        impactScore = {}
        score = 0
        allNodes = list(RAs.keys())
        for node in pathImportances:
            print("***Node: " + str(node) + "***")
            print("RA: " + str(RAs[node]))
            print("IS: " + str(pathImportances[node]))
            nodeData = (
                self.binMat[self.geneList.index(node), range(0, len(self.sampleList))]
                .todense()
                .tolist()
            )
            # score+=abs(float(RAs[node]))*math.log(float(pathImportances[node]),2) #*CVdict[node]
            # score+=abs(float(RAs[node]))*float(pathImportances[node])
            CVdict[node] = np.std(nodeData)
            print(CVdict[node])
            impactScore[node] = (
                abs(float(RAs[node])) * float(pathImportances[node]) * CVdict[node]
            )
            # print(impactScore[node])
            score += impactScore[node]
            # score+=(abs(float(RAs[node]))*float(pathImportances[node]))/nodeData_std
        # print(score)
        print(allNodes)
        print(
            "Relative abundance mean difference: "
            + str(np.mean([abs(RAs[value]) for value in allNodes]))
        )
        randomScores = []
        for i in range(1000):
            tempscore = 0
            for node in pathImportances:
                t1 = str(allNodes[randint(0, len(allNodes) - 1)])
                # tempscore+=abs(RAs[t1]*math.log(float(pathImportances[node]),2)) #*CVdict[node]
                # tempscore+=abs(RAs[t1]*float(pathImportances[node]))
                tempscore += abs(RAs[t1] * float(pathImportances[node])) * CVdict[node]
                # tempscore+=(abs(RAs[t1]*float(pathImportances[node])))/CVdict[node]
            randomScores.append(tempscore)
        meaner = np.mean(randomScores)
        stdev = np.std(randomScores)
        zscore = (score - meaner) / stdev

        # make histogram of the scores
        # An "interface" to matplotlib.axes.Axes.hist() method
        n, bins, patches = plt.hist(
            x=randomScores, bins="auto", color="#0504aa", alpha=0.7, rwidth=0.85
        )
        plt.grid(axis="y", alpha=0.75)
        plt.xlabel("Impact Score")
        plt.ylabel("Frequency")
        maxfreq = n.max()
        # Set a clean upper y-axis limit.
        plt.ylim(ymax=np.ceil(maxfreq / 10) * 10 if maxfreq % 10 else maxfreq + 10)
        plt.title(self.__getPathwayName(str(pathname)))
        axes = plt.gca()
        y_min, y_max = axes.get_ylim()
        plt.text(
            meaner,
            y_max - 15,
            "Mean="
            + str(round(meaner, 2))
            + "\n Std.dev="
            + str(round(stdev, 2))
            + "\n Pathway score="
            + str(round(score, 2)),
            bbox=dict(facecolor="red", alpha=0.75),
        )
        plt.savefig(
            pathname[:8] + "_impact_scores_hist.png", format="png", bbox_inches="tight"
        )
        plt.close()
        return zscore, impactScore, CVdict
calculate z score for a given pathway
calculate z score for a given pathway
calculate z score for a given pathway
def __scorePathway(self, RAs, pathImportances, pathname):
        CVdict = {}
        impactScore = {}
        score = 0
        allNodes = list(RAs.keys())
        for node in pathImportances:
            print("***Node: " + str(node) + "***")
            print("RA: " + str(RAs[node]))
            print("IS: " + str(pathImportances[node]))
            nodeData = (
                self.binMat[self.geneList.index(node), range(0, len(self.sampleList))]
                .todense()
                .tolist()
            )
            CVdict[node] = np.std(nodeData)
            print(CVdict[node])
            impactScore[node] = (
                abs(float(RAs[node])) * float(pathImportances[node]) * CVdict[node]
            )
            score += impactScore[node]
        print(allNodes)
        print(
            "Relative abundance mean difference: "
            + str(np.mean([abs(RAs[value]) for value in allNodes]))
        )
        randomScores = []
        for i in range(1000):
            tempscore = 0
            for node in pathImportances:
                t1 = str(allNodes[randint(0, len(allNodes) - 1)])
                tempscore += abs(RAs[t1] * float(pathImportances[node])) * CVdict[node]
            randomScores.append(tempscore)
        meaner = np.mean(randomScores)
        stdev = np.std(randomScores)
        zscore = (score - meaner) / stdev
        n, bins, patches = plt.hist(
            x=randomScores, bins="auto", color="#0504aa", alpha=0.7, rwidth=0.85
        )
        plt.grid(axis="y", alpha=0.75)
        plt.xlabel("Impact Score")
        plt.ylabel("Frequency")
        maxfreq = n.max()
        plt.ylim(ymax=np.ceil(maxfreq / 10) * 10 if maxfreq % 10 else maxfreq + 10)
        plt.title(self.__getPathwayName(str(pathname)))
        axes = plt.gca()
        y_min, y_max = axes.get_ylim()
        plt.text(
            meaner,
            y_max - 15,
            "Mean="
            + str(round(meaner, 2))
            + "\n Std.dev="
            + str(round(stdev, 2))
            + "\n Pathway score="
            + str(round(score, 2)),
            bbox=dict(facecolor="red", alpha=0.75),
        )
        plt.savefig(
            pathname[:8] + "_impact_scores_hist.png", format="png", bbox_inches="tight"
        )
        plt.close()
        return zscore, impactScore, CVdict
def __scorePathway ( self , RAs , pathImportances , pathname ) : CVdict = { } impactScore = { } score = 0 allNodes = list ( RAs . keys ( ) ) for node in pathImportances : print ( "***Node: " + str ( node ) + "***" ) print ( "RA: " + str ( RAs [ node ] ) ) print ( "IS: " + str ( pathImportances [ node ] ) ) nodeData = ( self . binMat [ self . geneList . index ( node ) , range ( 0 , len ( self . sampleList ) ) ] . todense ( ) . tolist ( ) ) CVdict [ node ] = np . std ( nodeData ) print ( CVdict [ node ] ) impactScore [ node ] = ( abs ( float ( RAs [ node ] ) ) * float ( pathImportances [ node ] ) * CVdict [ node ] ) score += impactScore [ node ] print ( allNodes ) print ( "Relative abundance mean difference: " + str ( np . mean ( [ abs ( RAs [ value ] ) for value in allNodes ] ) ) ) randomScores = [ ] for i in range ( 1000 ) : tempscore = 0 for node in pathImportances : t1 = str ( allNodes [ randint ( 0 , len ( allNodes ) - 1 ) ] ) tempscore += abs ( RAs [ t1 ] * float ( pathImportances [ node ] ) ) * CVdict [ node ] randomScores . append ( tempscore ) meaner = np . mean ( randomScores ) stdev = np . std ( randomScores ) zscore = ( score - meaner ) / stdev n , bins , patches = plt . hist ( x = randomScores , bins = "auto" , color = "#0504aa" , alpha = 0.7 , rwidth = 0.85 ) plt . grid ( axis = "y" , alpha = 0.75 ) plt . xlabel ( "Impact Score" ) plt . ylabel ( "Frequency" ) maxfreq = n . max ( ) plt . ylim ( ymax = np . ceil ( maxfreq / 10 ) * 10 if maxfreq % 10 else maxfreq + 10 ) plt . title ( self . __getPathwayName ( str ( pathname ) ) ) axes = plt . gca ( ) y_min , y_max = axes . get_ylim ( ) plt . text ( meaner , y_max - 15 , "Mean=" + str ( round ( meaner , 2 ) ) + "\n Std.dev=" + str ( round ( stdev , 2 ) ) + "\n Pathway score=" + str ( round ( score , 2 ) ) , bbox = dict ( facecolor = "red" , alpha = 0.75 ) , ) plt . savefig ( pathname [ : 8 ] + "_impact_scores_hist.png" , format = "png" , bbox_inches = "tight" ) plt . close ( ) return zscore , impactScore , CVdict
calculate z score for a given pathway
calculate z score for a given pathway
"""calculate z score for a given pathway""" # score+=abs(float(RAs[node]))*math.log(float(pathImportances[node]),2) #*CVdict[node] # score+=abs(float(RAs[node]))*float(pathImportances[node]) # print(impactScore[node]) # score+=(abs(float(RAs[node]))*float(pathImportances[node]))/nodeData_std # print(score) # tempscore+=abs(RAs[t1]*math.log(float(pathImportances[node]),2)) #*CVdict[node] # tempscore+=abs(RAs[t1]*float(pathImportances[node])) # tempscore+=(abs(RAs[t1]*float(pathImportances[node])))/CVdict[node] # make histogram of the scores # An "interface" to matplotlib.axes.Axes.hist() method # Set a clean upper y-axis limit.
self
RAs
pathImportances
pathname