def run(self,
            design,
            base_dir,
            model=None,
            contrast=None,
            outfile_prefix=None,
            counts=None,
            tpm=None,
            fdr=0.1,
            lrt=False,
            reduced_model=None):

        if lrt:
            E.info("Note: LRT will not generate fold changes")
            assert reduced_model is not None, ("need to provide a reduced "
                                               "model to use LRT")

        # Design table needs a "sample" column
        design.table['sample'] = design.table.index
        r_design_df = pandas2ri.py2ri(design.table)

        E.info('running sleuth: groups=%s, pairs=%s, replicates=%s, pairs=%s, '
               'additional_factors:' %
               (design.groups, design.pairs, design.has_replicates,
                design.has_pairs))

        # load sleuth
        R('''suppressMessages(library('sleuth'))''')

        # make variates string to ensure all model terms are in the
        # design dataframe for sleuth
        model_terms = [x for x in re.split("[\+~ ]+", model)[1:]
                       if x != "0"]
        variates = ",".join(model_terms) % "c(%s)"

        createSleuthObject = R('''
        function(design_df){
        sample_id = design_df$sample
        kal_dirs <- sapply(sample_id,
                           function(id) file.path('%(base_dir)s', id))

        design_df <- dplyr::select(design_df, sample = sample,
                                   %(variates)s)
        design_df <- dplyr::mutate(design_df, path = kal_dirs)

        so <- suppressMessages(sleuth_prep(design_df, %(model)s))
        so <- suppressMessages(sleuth_fit(so))
        return(so)
        }''' % locals())

        so = createSleuthObject(r_design_df)

        # write out counts and tpm tables if required
        if counts:

            makeCountsTable = R('''
            function(so){

            library('reshape')

            df = cast(so$obs_raw, target_id~sample, value = "est_counts")
            colnames(df)[1] <- "transcript_id"
            write.table(df, "%(counts)s", sep="\t", row.names=F, quote=F)

            }''' % locals())

            makeCountsTable(so)

        if tpm:

            makeTPMTable = R('''
            function(so){

            library('reshape')

            df = cast(so$obs_raw, target_id~sample, value = "tpm")
            colnames(df)[1] <- "transcript_id"
            write.table(df, "%(tpm)s", sep="\t", row.names=F, quote=F)

            }''' % locals())

            makeTPMTable(so)

        if lrt:
            differentialTesting = R('''
            function(so){
            so <- suppressMessages(sleuth_fit(so, formula = %(reduced_model)s,
                                   fit_name = "reduced"))
            so <- suppressMessages(sleuth_lrt(so, "reduced", "full"))

            return(so)
            } ''' % locals())

        else:
            differentialTesting = R('''
            function(so){
            so <- sleuth_wt(so, which_beta = '%(contrast)s')

            p_ma = plot_ma(so, '%(contrast)s')
            ggsave("%(outfile_prefix)s_%(contrast)s_sleuth_ma.png",
                width=15, height=15, units="cm")

            p_vars = plot_vars(so, '%(contrast)s')
            ggsave("%(outfile_prefix)s_%(contrast)s_sleuth_vars.png",
                width=15, height=15, units="cm")

            p_mean_var = plot_mean_var(so)
            ggsave("%(outfile_prefix)s_%(contrast)s_sleuth_mean_var.png",
            width=15, height=15, units="cm")

            return(so)
            } ''' % locals())

        results = differentialTesting(so)

        final_result = DEResult_Sleuth(so=results, contrast=contrast, lrt=lrt)

        return final_result
dataset/ETHPy150Open CGATOxford/cgat/CGAT/Expression.py/DEExperiment_Sleuth.run/swapped operands of `%`