<commit_before>import os, sys, re, types, itertools, math, sqlite3

from SphinxReport.Tracker import *
from SphinxReport.Utils import PARAMS as P
from collections import OrderedDict as odict

from RnaseqDiffExpressionReport import *

##############################################################
##############################################################
##############################################################
class TrackerDESeqFit( Tracker ):
    method = "deseq"
    tracks = [ x.asFile() for x in DESIGNS ]
    slices = [ x.asFile() for x in GENESETS ]

    def __call__(self, track, slice = None ):
        design = track
        level = "gene"
        geneset = slice
        method = self.method

        filename = "%(method)s.dir/%(design)s_%(geneset)s_%(method)s_%(level)s_fit_%(track)s.png" % locals()

        # fitting information will not exist if there are no replicates
        if not os.path.exists( filename ): return None

        rst_text = '''
%(level)s %(track)s %(geneset)s
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

.. figure:: %(method)s.dir/%(design)s_%(geneset)s_%(method)s_%(level)s_fit_%(track)s.png

.. figure:: %(method)s.dir/%(design)s_%(geneset)s_%(method)s_%(level)s_residuals_%(track)s.png

''' % locals()

        return odict( (("text", rst_text),) )

##############################################################
##############################################################
##############################################################
class TrackerDESummaryDESeq( RnaseqTracker, SingleTableTrackerRows ):
    table = "deseq_stats"
    fields = ("level", "geneset", "treatment_name", "control_name" )

class TrackerDESummaryEdgeR( RnaseqTracker, SingleTableTrackerRows ):
    table = "edger_stats"
    fields = ("level", "geneset", "treatment_name", "control_name" )

class TrackerDESummaryCuffdiff( RnaseqTracker, SingleTableTrackerRows ):
    table = "cuffdiff_stats"
    fields = ("level", "geneset", "treatment_name", "control_name" )

##############################################################
##############################################################
##############################################################
class TrackerDESummaryPlots( Tracker ):

    tracks = [ x.asFile() for x in DESIGNS ]
    slices = [ x.asFile() for x in GENESETS ]

    def __call__(self, track, slice = None ):
        design = track
        geneset = slice
        method = self.method

        rst_text = []

        for level in self.levels:
            for fn in (
                "%(method)s.dir/%(design)s_%(geneset)s_%(method)s_%(level)s_heatmap.png",
                "%(method)s.dir/%(design)s_%(geneset)s_%(method)s_%(level)s_scvplot.png",
                "%(method)s.dir/%(design)s_%(geneset)s_%(method)s_%(level)s_pvalue_vs_length.png" ):
                f = fn % locals()
                if not os.path.exists(f): continue
                rst_text.append( ".. figure:: %(f)s" % locals() )
                
        if rst_text:
            rst_text = '''
%(design)s %(geneset)s
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

''' % locals() + "\n\n".join( rst_text )
        else:
            rst_text = ""

        return odict( (("text", rst_text),) )

class TrackerDESummaryPlotsDESeq(TrackerDESummaryPlots ):
    method = "deseq"
    levels = ("gene",)

class TrackerDESummaryPlotsEdgeR(TrackerDESummaryPlots ):
    method = "edger"
    levels = ("gene",)

class TrackerDESummaryPlotsCuffdiff(TrackerDESummaryPlots ):
    method = "cuffdiff"
    levels = CUFFDIFF_LEVELS

################################################################
################################################################
################################################################
class TrackerDifferentialExpression( Tracker ):

    tracks = [ x.asFile() for x in GENESETS ]
    slices = [ x.asFile() for x in DESIGNS ]

    def __call__(self, track, slice = None ):

        method = self.method
        design = slice
        rst_text = []
        geneset = track

        for level in self.levels:
            for x,y in itertools.combinations( EXPERIMENTS, 2 ):
                filename = "%(method)s.dir/%(design)s_%(geneset)s_%(level)s_%(x)s_vs_%(y)s_significance.png" % locals()
                if not os.path.exists( filename ): continue

                rst_text.append('''
%(design)s %(geneset)s %(level)s %(x)s vs %(y)s 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

.. figure:: %(filename)s

''' % locals())
                            
        return odict( (("text", "\n".join( rst_text)),) )

class TrackerDEPairwiseDESeq( TrackerDifferentialExpression ):
    method = "deseq"
    levels = ("gene",)
    
class TrackerDEPairwiseCuffdiff( TrackerDifferentialExpression ):
    method = "cuffdiff"
    levels = CUFFDIFF_LEVELS


#############################################################
#############################################################
#############################################################
##
#############################################################
class DifferentialExpressionComparison( RnaseqTracker ):

    tracks = list( itertools.combinations( ("deseq", "cuffdiff", "edger"), 3 ))
 
    slices = [ "%s_%s" % y,x.asFile() for x,y in itertools.product(GENESETS,DESIGNS) ]

class DifferentialExpressionOverlap( DifferentialExpressionComparison ):

    def __call__(self, track, slice = None ):
       
        pair1, pair2, pair3 = track
        <commit_msg>Bug fixes to differential expression rport<commit_after>import os, sys, re, types, itertools, math, sqlite3

from SphinxReport.Tracker import *
from SphinxReport.Utils import PARAMS as P
from collections import OrderedDict as odict

from RnaseqDiffExpressionReport import *

##############################################################
##############################################################
##############################################################
class TrackerDESeqFit( Tracker ):
    method = "deseq"
    tracks = [ x.asFile() for x in DESIGNS ]
    slices = [ x.asFile() for x in GENESETS ]

    def __call__(self, track, slice = None ):
        design = track
        level = "gene"
        geneset = slice
        method = self.method

        filename = "%(method)s.dir/%(design)s_%(geneset)s_%(method)s_%(level)s_fit_%(track)s.png" % locals()

        # fitting information will not exist if there are no replicates
        if not os.path.exists( filename ): return None

        rst_text = '''
%(level)s %(track)s %(geneset)s
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

.. figure:: %(method)s.dir/%(design)s_%(geneset)s_%(method)s_%(level)s_fit_%(track)s.png

.. figure:: %(method)s.dir/%(design)s_%(geneset)s_%(method)s_%(level)s_residuals_%(track)s.png

''' % locals()

        return odict( (("text", rst_text),) )

##############################################################
##############################################################
##############################################################
class TrackerDESummaryDESeq( RnaseqTracker, SingleTableTrackerRows ):
    table = "deseq_stats"
    fields = ("level", "geneset", "treatment_name", "control_name" )

class TrackerDESummaryEdgeR( RnaseqTracker, SingleTableTrackerRows ):
    table = "edger_stats"
    fields = ("level", "geneset", "treatment_name", "control_name" )

class TrackerDESummaryCuffdiff( RnaseqTracker, SingleTableTrackerRows ):
    table = "cuffdiff_stats"
    fields = ("level", "geneset", "treatment_name", "control_name" )

##############################################################
##############################################################
##############################################################
class TrackerDESummaryPlots( Tracker ):

    tracks = [ x.asFile() for x in DESIGNS ]
    slices = [ x.asFile() for x in GENESETS ]

    def __call__(self, track, slice = None ):
        design = track
        geneset = slice
        method = self.method

        rst_text = []

        for level in self.levels:
            for fn in (
                "%(method)s.dir/%(design)s_%(geneset)s_%(method)s_%(level)s_heatmap.png",
                "%(method)s.dir/%(design)s_%(geneset)s_%(method)s_%(level)s_scvplot.png",
                "%(method)s.dir/%(design)s_%(geneset)s_%(method)s_%(level)s_pvalue_vs_length.png" ):
                f = fn % locals()
                if not os.path.exists(f): continue
                rst_text.append( ".. figure:: %(f)s" % locals() )
                
        if rst_text:
            rst_text = '''
%(design)s %(geneset)s
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

''' % locals() + "\n\n".join( rst_text )
        else:
            rst_text = ""

        return odict( (("text", rst_text),) )

class TrackerDESummaryPlotsDESeq(TrackerDESummaryPlots ):
    method = "deseq"
    levels = ("gene",)

class TrackerDESummaryPlotsEdgeR(TrackerDESummaryPlots ):
    method = "edger"
    levels = ("gene",)

class TrackerDESummaryPlotsCuffdiff(TrackerDESummaryPlots ):
    method = "cuffdiff"
    levels = CUFFDIFF_LEVELS

################################################################
################################################################
################################################################
class TrackerDifferentialExpression( Tracker ):

    tracks = [ x.asFile() for x in GENESETS ]
    slices = [ x.asFile() for x in DESIGNS ]

    def __call__(self, track, slice = None ):

        method = self.method
        design = slice
        rst_text = []
        geneset = track

        for level in self.levels:
            for x,y in itertools.combinations( EXPERIMENTS, 2 ):
                filename = "%(method)s.dir/%(design)s_%(geneset)s_%(level)s_%(x)s_vs_%(y)s_significance.png" % locals()
                if not os.path.exists( filename ): continue

                rst_text.append('''
%(design)s %(geneset)s %(level)s %(x)s vs %(y)s 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

.. figure:: %(filename)s

''' % locals())
                            
        return odict( (("text", "\n".join( rst_text)),) )

class TrackerDEPairwiseDESeq( TrackerDifferentialExpression ):
    method = "deseq"
    levels = ("gene",)
    
class TrackerDEPairwiseCuffdiff( TrackerDifferentialExpression ):
    method = "cuffdiff"
    levels = CUFFDIFF_LEVELS


#############################################################
#############################################################
#############################################################
##
#############################################################
class DifferentialExpressionComparison( RnaseqTracker ):

    tracks = list( itertools.combinations( ("deseq", "cuffdiff", "edger"), 3 ))
 
    slices = [ "%s_%s" % (y,x.asFile()) for x,y in itertools.product(GENESETS,DESIGNS) ]

class DifferentialExpressionOverlap( DifferentialExpressionComparison ):

    def __call__(self, track, slice = None ):
       
        pair1, pair2, pair3 = track
        
{'fraction_non_alphanumeric': 0.32111503303466377, 'fraction_numerical': 0.0010860711376595167, 'mean_word_length': 3.972997299729973, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6277149', 'n_tokens_mistral': 3394, 'n_tokens_neox': 3089, 'n_words': 613}