Beispiel #1
0
    def do_plot(self, inputDir, plotOutDir, plotOutFileName, simDataFile,
                validationDataFile, metadata):
        if metadata is not None and SHUFFLE_VARIANT_TAG not in metadata[
                "variant"]:
            print "This plot only runs for variants where parameters are shuffled."
            return

        if not os.path.isdir(inputDir):
            raise Exception, "variantDir does not currently exist as a directory"

        if not os.path.exists(plotOutDir):
            os.mkdir(plotOutDir)

        validation_data = cPickle.load(open(validationDataFile, "rb"))
        schmidtCounts = validation_data.protein.schmidt2015Data[
            "glucoseCounts"]

        ap = AnalysisPaths(inputDir, variant_plot=True)

        pool = Pool(processes=parallelization.plotter_cpus())
        args = zip(
            range(ap.n_variant), [ap] * ap.n_variant,
            [validation_data.protein.schmidt2015Data["monomerId"].tolist()] *
            ap.n_variant, [schmidtCounts] * ap.n_variant)
        result = pool.map(getPCC, args)
        # cPickle.dump(result, open("pcc_results.cPickle", "w"), cPickle.HIGHEST_PROTOCOL)
        pool.close()
        pool.join()
        # result = cPickle.load(open("pcc_results.cPickle", "r"))
        controlPcc, controlPvalue = result[0]
        pccs, pvals = zip(*result[1:])
        pccs = np.array(pccs)
        pvals = np.array(pvals)

        fig = plt.figure()
        fig.set_figwidth(5)
        fig.set_figheight(5)
        ax = plt.subplot(1, 1, 1)

        pccs = np.array([x for x in pccs if not np.isnan(x)])
        ax.hist(pccs, np.sqrt(pccs.size))
        ax.axvline(controlPcc, color="k", linestyle="dashed", linewidth=2)

        ax.set_xlabel("Proteome correlation (Pearson r)")
        ax.set_title("Mean: %0.3g     Std: %0.3g     Control: %0.3g" %
                     (pccs.mean(), pccs.std(), controlPcc))

        axes_list = [ax]

        for a in axes_list:
            for tick in a.yaxis.get_major_ticks():
                tick.label.set_fontsize(FONT_SIZE)
            for tick in a.xaxis.get_major_ticks():
                tick.label.set_fontsize(FONT_SIZE)

        whitePadSparklineAxis(ax)

        plt.subplots_adjust(bottom=0.2, wspace=0.3)

        exportFigure(plt, plotOutDir, plotOutFileName, metadata)
def plot_threshold(ax, data, threshold, reactions):
    """
	Plots the largest negative change for each reaction the changes the flux
	more than the threshold.  Prints the full reaction names for these reactions.

	Args:
		ax (matplotlib.axes): axes to plot on
		data (ndarray[float]): 2D array (n time steps x m reactions) of fluxes
			with the last column being the reference flux
		threshold (float): threshold to plot and use as cutoff
		reactions (ndarray[str]): names for each reaction in data
	"""

    sorted_idx = np.argsort(data)
    below_idx = np.where(data[sorted_idx] < threshold)[0]

    # Plot data
    ax.set_yscale('symlog', threshold=0.01)
    ax.bar(below_idx, data[sorted_idx[below_idx]], color='b')
    ax.axhline(threshold, color='k', linestyle='--', linewidth=0.5)

    # Format axes
    y_ticks = np.hstack((ax.get_yticks(), threshold))
    sparkline.whitePadSparklineAxis(ax)
    ax.spines["bottom"].set_visible(False)
    ax.tick_params(bottom=False)
    ax.set_xticks(below_idx)
    ax.set_xticklabels([r[:10] for r in reactions[sorted_idx[below_idx]]],
                       rotation=90)
    ax.set_yticks(y_ticks)
    ax.tick_params(labelsize=6)
    ax.yaxis.set_major_formatter(FormatStrFormatter('%.1f'))

    print(reactions[sorted_idx[below_idx]])
def plot_lows(ax, data, threshold, label):
    """
	Plots the largest negative change for each reaction.

	Args:
		ax (matplotlib.axes): axes to plot on
		data (ndarray[float]): 2D array (n time steps x m reactions) of fluxes
			with the last column being the reference flux
		threshold (float): threshold to plot
		label (str): reaction label for y axis
	"""

    # Plot data
    ax.set_yscale('symlog', threshold=0.01)
    ax.fill_between(range(len(data)), sorted(data), color='b')
    ax.axhline(threshold, color='k', linestyle='--', linewidth=0.5)

    # Format axes
    y_ticks = np.hstack((ax.get_yticks(), threshold))
    sparkline.whitePadSparklineAxis(ax, xAxis=False)
    ax.set_xticks([])
    ax.set_yticks(y_ticks)
    ax.tick_params(labelsize=6)
    ax.yaxis.set_major_formatter(FormatStrFormatter('%.1f'))
    ax.set_ylabel('Maximum change in\n{} flux'.format(label), fontsize=6)
Beispiel #4
0
def plot(proteinIds, zeroAtLeastOnce, essential, ax, axEssential, xloc, width,
         functionalUnitIds):
    if not len(functionalUnitIds):
        notAlwaysPresent = zeroAtLeastOnce.sum()
        alwaysPresent = len(proteinIds) - notAlwaysPresent

        # Count number of essential genes
        notAlwaysPresentEssential = np.logical_and(zeroAtLeastOnce,
                                                   essential).sum()
        alwaysPresentEssential = np.logical_and(
            np.logical_not(zeroAtLeastOnce), essential).sum()

    else:
        functionalUnitIndices = [
            proteinIds.index(x) for x in functionalUnitIds
        ]
        notAlwaysPresent = zeroAtLeastOnce[functionalUnitIndices].sum()
        alwaysPresent = len(functionalUnitIds) - notAlwaysPresent

        # Count number of essential genes
        notAlwaysPresentEssential = np.logical_and(
            zeroAtLeastOnce[functionalUnitIndices],
            essential[functionalUnitIndices]).sum()
        alwaysPresentEssential = np.logical_and(
            np.logical_not(zeroAtLeastOnce[functionalUnitIndices]),
            essential[functionalUnitIndices]).sum()

    # Plot
    bar = ax.bar(xloc + width, [alwaysPresent, notAlwaysPresent],
                 width,
                 color="lightgray")
    axEssential.bar(xloc + width, [alwaysPresent, notAlwaysPresent],
                    width,
                    color="lightgray")
    barEssential = axEssential.bar(
        xloc + width, [alwaysPresentEssential, notAlwaysPresentEssential],
        width,
        color="b")

    # Format
    for axis in [ax, axEssential]:
        whitePadSparklineAxis(axis)
        axis.set_xticklabels(["Always\npresent", "0 at\nleast once"])
        axis.set_xticks(xloc + width)
    ax.set_yticks([x.get_height() for x in bar])
    axRight = axEssential.twinx()
    axRight.set_yticks([x[1].get_height() for x in [bar, barEssential]])
    axEssential.set_yticks([x[0].get_height() for x in [bar, barEssential]])
    axRight.set_ylim(ax.get_ylim())
    axRight.spines["left"].set_visible(False)
    axRight.spines["top"].set_visible(False)
    return
    def do_plot(self, seedOutDir, plotOutDir, plotOutFileName, simDataFile,
                validationDataFile, metadata):
        if not os.path.isdir(seedOutDir):
            raise Exception, "seedOutDir does not currently exist as a directory"

        if not os.path.exists(plotOutDir):
            os.mkdir(plotOutDir)

        # Get all cells
        ap = AnalysisPaths(seedOutDir, multi_gen_plot=True)
        allDir = ap.get_cells()
        # allDir = ap.get_cells(generation = [0, 1, 2])

        sim_data = cPickle.load(open(simDataFile, "rb"))
        metaboliteNames = np.array(
            sorted(sim_data.process.metabolism.concDict.keys()))
        nMetabolites = len(metaboliteNames)

        validation_data = cPickle.load(open(validationDataFile, "rb"))
        toyaReactions = validation_data.reactionFlux.toya2010fluxes[
            "reactionID"]
        toyaFluxes = validation_data.reactionFlux.toya2010fluxes[
            "reactionFlux"]
        toyaStdev = validation_data.reactionFlux.toya2010fluxes[
            "reactionFluxStdev"]
        toyaFluxesDict = dict(zip(toyaReactions, toyaFluxes))
        toyaStdevDict = dict(zip(toyaReactions, toyaStdev))

        sim_data = cPickle.load(open(simDataFile))
        cellDensity = sim_data.constants.cellDensity

        modelFluxes = {}
        toyaOrder = []
        for rxn in toyaReactions:
            modelFluxes[rxn] = []
            toyaOrder.append(rxn)

        for simDir in allDir:
            simOutDir = os.path.join(simDir, "simOut")

            mainListener = TableReader(os.path.join(simOutDir, "Main"))
            timeStepSec = mainListener.readColumn("timeStepSec")
            mainListener.close()

            massListener = TableReader(os.path.join(simOutDir, "Mass"))
            cellMass = massListener.readColumn("cellMass")
            dryMass = massListener.readColumn("dryMass")
            massListener.close()

            coefficient = dryMass / cellMass * sim_data.constants.cellDensity.asNumber(
                MASS_UNITS / VOLUME_UNITS)

            fbaResults = TableReader(os.path.join(simOutDir, "FBAResults"))
            reactionIDs = np.array(fbaResults.readAttribute("reactionIDs"))
            reactionFluxes = (COUNTS_UNITS / MASS_UNITS / TIME_UNITS) * (
                fbaResults.readColumn("reactionFluxes").T / coefficient).T
            fbaResults.close()

            for toyaReaction in toyaReactions:
                fluxTimeCourse = []

                for rxn in reactionIDs:
                    if re.findall(toyaReaction, rxn):
                        reverse = 1
                        if re.findall("(reverse)", rxn):
                            reverse = -1

                        if len(fluxTimeCourse):
                            fluxTimeCourse += reverse * reactionFluxes[:,
                                                                       np.
                                                                       where(
                                                                           reactionIDs
                                                                           ==
                                                                           rxn
                                                                       )]
                        else:
                            fluxTimeCourse = reverse * reactionFluxes[:,
                                                                      np.where(
                                                                          reactionIDs
                                                                          ==
                                                                          rxn)]

                if len(fluxTimeCourse):
                    modelFluxes[toyaReaction].append(
                        np.mean(fluxTimeCourse).asNumber(units.mmol / units.g /
                                                         units.h))

        toyaVsReactionAve = []
        for rxn, toyaFlux in toyaFluxesDict.iteritems():
            if rxn in modelFluxes:
                toyaVsReactionAve.append(
                    (np.mean(modelFluxes[rxn]),
                     toyaFlux.asNumber(units.mmol / units.g / units.h),
                     np.std(modelFluxes[rxn]), toyaStdevDict[rxn].asNumber(
                         units.mmol / units.g / units.h)))

        toyaVsReactionAve = np.array(toyaVsReactionAve)
        correlationCoefficient = np.corrcoef(toyaVsReactionAve[:, 0],
                                             toyaVsReactionAve[:, 1])[0, 1]

        plt.figure(figsize=(8, 8))
        plt.title("Central Carbon Metabolism Flux, Pearson R = {:.2}".format(
            correlationCoefficient))
        plt.errorbar(toyaVsReactionAve[:, 1],
                     toyaVsReactionAve[:, 0],
                     xerr=toyaVsReactionAve[:, 3],
                     yerr=toyaVsReactionAve[:, 2],
                     fmt="o",
                     ecolor="k")
        ylim = plt.ylim()
        plt.plot([ylim[0], ylim[1]], [ylim[0], ylim[1]], color="k")
        plt.xlabel("Toya 2010 Reaction Flux [mmol/g/hr]")
        plt.ylabel("Mean WCM Reaction Flux [mmol/g/hr]")
        ax = plt.axes()
        ax.set_ylim(plt.xlim())
        whitePadSparklineAxis(plt.axes())

        exportFigure(plt, plotOutDir, plotOutFileName, metadata)
        plt.close("all")
    def do_plot(self, variantDir, plotOutDir, plotOutFileName, simDataFile,
                validationDataFile, metadata):
        if not os.path.isdir(variantDir):
            raise Exception, 'variantDir does not currently exist as a directory'

        filepath.makedirs(plotOutDir)

        ap = AnalysisPaths(variantDir, cohort_plot=True)

        limited_metabolites = []
        for sim_dir in ap.get_cells():
            sim_out_dir = os.path.join(sim_dir, 'simOut')

            # Listeners used
            kinetics_reader = TableReader(
                os.path.join(sim_out_dir, "EnzymeKinetics"))

            # Load data
            try:
                metabolite_indices = {
                    m: i
                    for i, m in enumerate(
                        kinetics_reader.readAttribute('metaboliteNames'))
                }
                metabolite_counts = kinetics_reader.readColumn(
                    "metaboliteCountsFinal")[1:, :]
                counts_to_molar = kinetics_reader.readColumn(
                    'countsToMolar')[1:].reshape(-1, 1)
            except:
                print('Error reading data from {}'.format(sim_out_dir))
                continue

            # Calculate concentrations
            met_idx = np.array(
                [metabolite_indices[m] for m in LIMITED_METABOLITES])
            metabolite_conc = counts_to_molar * metabolite_counts[:, met_idx]
            limited_metabolites += [metabolite_conc]

        limited_metabolites = np.vstack(limited_metabolites)

        # Values to calculate significance between different cohorts
        print('Metabolites: {}'.format(LIMITED_METABOLITES))
        print('Means: {}'.format(limited_metabolites.mean(axis=0)))
        print('Stds: {}'.format(limited_metabolites.std(axis=0)))
        print('N: {}'.format(limited_metabolites.shape[0]))

        plt.figure(figsize=(4, 4))
        xticks = [0, 1]

        # Plot data
        plt.violinplot(limited_metabolites, xticks, showmeans=True)

        # Format axes
        plt.ylim([0, 50])
        whitePadSparklineAxis(plt.gca())
        plt.xticks(xticks, LIMITED_METABOLITES)
        plt.ylabel('Concentration (uM)')

        plt.tight_layout()
        exportFigure(plt, plotOutDir, plotOutFileName, metadata)
        plt.close('all')
Beispiel #7
0
    def do_plot(self, inputDir, plotOutDir, plotOutFileName, simDataFile,
                validationDataFile, metadata):
        if metadata["variant"] != "condition":
            print('This analysis only runs for the "condition" variant.')
            return

        if not os.path.isdir(inputDir):
            raise Exception, 'inputDir does not currently exist as a directory'

        filepath.makedirs(plotOutDir)

        ap = AnalysisPaths(inputDir, variant_plot=True)
        n_gens = ap.n_generation
        variants = ap.get_variants()

        if n_gens - 1 < FIRST_GENERATION:
            print('Not enough generations to plot.')
            return

        all_growth_rates = []
        all_rna_to_protein_ratios = []

        for variant in variants:
            doubling_times = np.zeros(0)
            variant_rna_to_protein_ratios = np.zeros(0)

            all_cells = ap.get_cells(variant=[variant],
                                     generation=range(FIRST_GENERATION,
                                                      n_gens))

            if len(all_cells) == 0:
                continue

            for simDir in all_cells:
                try:
                    simOutDir = os.path.join(simDir, "simOut")
                    mass = TableReader(os.path.join(simOutDir, "Mass"))
                    rna_mass = mass.readColumn("rnaMass")
                    protein_mass = mass.readColumn("proteinMass")

                    time = TableReader(os.path.join(simOutDir,
                                                    "Main")).readColumn("time")

                    doubling_times = np.hstack(
                        (doubling_times, (time[-1] - time[0]) / 3600.))

                    variant_rna_to_protein_ratios = np.hstack(
                        (variant_rna_to_protein_ratios,
                         rna_mass.mean() / protein_mass.mean()))
                except:
                    continue

            variant_growth_rates = np.log(2) / doubling_times

            all_growth_rates.append(variant_growth_rates)
            all_rna_to_protein_ratios.append(variant_rna_to_protein_ratios)

        # Get errorbar plot
        plt.figure(figsize=FIGSIZE)

        plt.style.use('seaborn-deep')
        color_cycle = plt.rcParams['axes.prop_cycle'].by_key()['color']
        marker_styles = ['o', '^', 'x']
        labels = ['basal', 'anaerobic', '+AA']

        ax = plt.subplot2grid((1, 1), (0, 0))

        for i in range(3):
            ax.errorbar(all_growth_rates[i].mean(),
                        all_rna_to_protein_ratios[i].mean(),
                        yerr=all_rna_to_protein_ratios[i].std(),
                        color=color_cycle[0],
                        mec=color_cycle[0],
                        marker=marker_styles[i],
                        markersize=8,
                        mfc='white',
                        linewidth=1,
                        capsize=2,
                        label=labels[i])

        # Add linear plot proposed in Scott et al. (2010)
        x_linear = np.linspace(0.05, 1.95, 100)
        y_linear = x_linear / 4.5 + 0.087
        ax.plot(x_linear, y_linear, linewidth=2, color=color_cycle[2])

        ax.set_xlim([0, 2])
        ax.set_ylim([0, 0.7])
        ax.get_yaxis().get_major_formatter().set_useOffset(False)
        ax.get_xaxis().get_major_formatter().set_useOffset(False)

        whitePadSparklineAxis(ax)

        ax.tick_params(which='both',
                       bottom=True,
                       left=True,
                       top=False,
                       right=False,
                       labelbottom=True,
                       labelleft=True)

        ax.set_xlabel("Growth rate $\lambda$ (hour$^{-1}$)")
        ax.set_ylabel("RNA/protein mass ratio")
        exportFigure(plt, plotOutDir, plotOutFileName, metadata)

        # Get clean version of errorbar plot
        ax.set_xlabel("")
        ax.set_ylabel("")
        ax.set_yticklabels([])
        ax.set_xticklabels([])
        exportFigure(plt, plotOutDir, plotOutFileName + "_clean", metadata)

        plt.close("all")

        # Get scatter version of plot
        plt.figure(figsize=FIGSIZE)
        ax = plt.subplot2grid((1, 1), (0, 0))

        options = {"edgecolors": color_cycle[0], "alpha": 0.25, "s": 20}

        ax.scatter(all_growth_rates[0],
                   all_rna_to_protein_ratios[0],
                   facecolors="none",
                   marker="o",
                   label=labels[0],
                   **options)
        ax.scatter(all_growth_rates[1],
                   all_rna_to_protein_ratios[1],
                   facecolors="none",
                   marker="^",
                   label=labels[1],
                   **options)
        ax.scatter(all_growth_rates[2],
                   all_rna_to_protein_ratios[2],
                   marker="x",
                   label=labels[2],
                   **options)

        x_linear = np.linspace(0.05, 2.45, 100)
        y_linear = x_linear / 4.5 + 0.087
        ax.plot(x_linear, y_linear, linewidth=2, color=color_cycle[2])

        ax.set_xlim([0, 2.5])
        ax.set_ylim([0, 0.8])
        ax.get_yaxis().get_major_formatter().set_useOffset(False)
        ax.get_xaxis().get_major_formatter().set_useOffset(False)

        whitePadSparklineAxis(ax)

        ax.tick_params(which='both',
                       bottom=True,
                       left=True,
                       top=False,
                       right=False,
                       labelbottom=True,
                       labelleft=True)

        ax.set_xlabel("Growth rate $\lambda$ (hour$^{-1}$)")
        ax.set_ylabel("RNA/protein mass ratio")
        exportFigure(plt, plotOutDir, plotOutFileName + "_scatter", metadata)
    def do_plot(self, inputDir, plotOutDir, plotOutFileName, simDataFile,
                validationDataFile, metadata):
        if not os.path.isdir(inputDir):
            raise Exception, "variantDir does not currently exist as a directory"

        if not os.path.exists(plotOutDir):
            os.mkdir(plotOutDir)

        ap = AnalysisPaths(inputDir, variant_plot=True)

        fig = plt.figure()
        fig.set_figwidth(5)
        fig.set_figheight(5)

        bremer_tau = [40, 100, 24]

        bremer_origins_per_cell_at_initiation = [2, 1, 4]
        bremer_rrn_init_rate = [20 * 23, 4 * 12.4, 58 * 35.9]

        bremer_rna_mass_per_cell = [77, 20, 211]
        bremer_elng_rate = [18, 12, 21]

        sim_doubling_time = np.zeros(ap.n_variant)
        sim_doubling_time_std = np.zeros(ap.n_variant)

        sim_origins_per_cell_at_initiation = np.zeros(ap.n_variant)
        sim_rna_mass_per_cell = np.zeros(ap.n_variant)
        sim_elng_rate = np.zeros(ap.n_variant)
        sim_rrn_init_rate = np.zeros(ap.n_variant)

        sim_origins_per_cell_at_initiation_std = np.zeros(ap.n_variant)
        sim_elng_rate_std = np.zeros(ap.n_variant)
        sim_rna_mass_per_cell_std = np.zeros(ap.n_variant)
        sim_rrn_init_rate_std = np.zeros(ap.n_variant)

        variants = ap.get_variants()

        for varIdx in range(ap.n_variant):
            variant = variants[varIdx]

            print("variant {}".format(variant))

            all_cells = ap.get_cells(variant=[variant])

            print("Total cells: {}".format(len(all_cells)))

            try:
                sim_data = cPickle.load(open(ap.get_variant_kb(variant)))
            except Exception as e:
                print "Couldn't load sim_data object. Exiting.", e
                return

            num_origin_at_init = np.zeros(len(all_cells))
            doubling_time = np.zeros(len(all_cells))
            meanRnaMass = np.zeros(len(all_cells))
            meanElngRate = np.zeros(len(all_cells))
            meanRrnInitRate = np.zeros(len(all_cells))

            for idx, simDir in enumerate(all_cells):
                print "cell {} of {}".format(idx, len(all_cells))

                simOutDir = os.path.join(simDir, "simOut")

                try:
                    time = TableReader(os.path.join(simOutDir,
                                                    "Main")).readColumn("time")
                    doubling_time[idx] = time[-1] - time[0]
                except Exception as e:
                    print 'Error with data for %s: %s' % (simDir, e)
                    continue

                timeStepSec = TableReader(os.path.join(
                    simOutDir, "Main")).readColumn("timeStepSec")

                meanRnaMass[idx] = TableReader(os.path.join(
                    simOutDir, "Mass")).readColumn("rnaMass").mean()
                meanElngRate[idx] = TableReader(
                    os.path.join(simOutDir, "RibosomeData")).readColumn(
                        "effectiveElongationRate").mean()

                numOrigin = TableReader(
                    os.path.join(simOutDir,
                                 "ReplicationData")).readColumn("numberOfOric")

                massPerOric = TableReader(
                    os.path.join(
                        simOutDir,
                        "ReplicationData")).readColumn("criticalMassPerOriC")
                idxInit = np.where(massPerOric >= 1)[0]
                numOriginAtInit = numOrigin[idxInit - 1]
                if numOriginAtInit.size:
                    num_origin_at_init[idx] = numOriginAtInit.mean()
                else:
                    num_origin_at_init[idx] = np.nan

                transcriptDataFile = TableReader(
                    os.path.join(simOutDir, "TranscriptElongationListener"))
                rnaSynth = transcriptDataFile.readColumn("countRnaSynthesized")
                isRRna = sim_data.process.transcription.rnaData["isRRna"]
                meanRrnInitRate[idx] = (rnaSynth[:, isRRna].sum(axis=1) /
                                        timeStepSec).mean() * 60. / 3

            sim_rna_mass_per_cell[varIdx] = meanRnaMass.mean()
            sim_elng_rate[varIdx] = meanElngRate.mean()
            sim_origins_per_cell_at_initiation[varIdx] = np.nanmean(
                num_origin_at_init)
            sim_doubling_time[varIdx] = np.nanmean(doubling_time) / 60.
            sim_rrn_init_rate[varIdx] = np.nanmean(meanRrnInitRate)

            sim_rna_mass_per_cell_std[varIdx] = meanRnaMass.std()
            sim_elng_rate_std[varIdx] = meanElngRate.std()
            sim_origins_per_cell_at_initiation_std[varIdx] = np.nanstd(
                num_origin_at_init)
            sim_doubling_time_std[varIdx] = np.nanstd(doubling_time) / 60.
            sim_rrn_init_rate_std[varIdx] = np.nanstd(meanRrnInitRate)

        bremer_tau = np.array(bremer_tau)

        ax0 = plt.subplot2grid((2, 2), (0, 0))
        ax1 = plt.subplot2grid((2, 2), (1, 0), sharex=ax0)
        ax2 = plt.subplot2grid((2, 2), (0, 1), sharex=ax0)
        ax3 = plt.subplot2grid((2, 2), (1, 1), sharex=ax0)

        lines = {'linestyle': 'dashed'}
        plt.rc('lines', **lines)
        plt.style.use('seaborn-deep')
        color_cycle = plt.rcParams['axes.prop_cycle'].by_key()['color']

        ax0.errorbar(
            sim_doubling_time[np.argsort(sim_doubling_time)[::-1]],
            sim_rna_mass_per_cell[np.argsort(sim_doubling_time)[::-1]],
            yerr=sim_rna_mass_per_cell_std[np.argsort(sim_doubling_time)
                                           [::-1]],
            color=color_cycle[0],
            **SIM_PLOT_STYLE)
        ax0.errorbar(
            bremer_tau[np.argsort(bremer_tau)[::-1]],
            np.array(bremer_rna_mass_per_cell)[np.argsort(bremer_tau)[::-1]],
            color=color_cycle[2],
            **EXP_PLOT_STYLE)
        ax0.set_title("RNA mass per cell (fg)", fontsize=FONT_SIZE)
        ax0.set_xlim([0, 135])
        ax0.set_ylim([0, 250])
        ax0.legend(loc=1, fontsize='xx-small', markerscale=0.5, frameon=False)

        ax1.errorbar(
            sim_doubling_time[np.argsort(sim_doubling_time)[::-1]],
            sim_elng_rate[np.argsort(sim_doubling_time)[::-1]],
            yerr=sim_elng_rate_std[np.argsort(sim_doubling_time)[::-1]],
            color=color_cycle[0],
            **SIM_PLOT_STYLE)
        ax1.errorbar(bremer_tau[np.argsort(bremer_tau)[::-1]],
                     np.array(bremer_elng_rate)[np.argsort(bremer_tau)[::-1]],
                     color=color_cycle[2],
                     **EXP_PLOT_STYLE)
        ax1.set_title("Ribosome elongation\nrate (aa/s/ribosome)",
                      fontsize=FONT_SIZE)
        ax1.set_xlabel("Doubling time (min)", fontsize=FONT_SIZE)
        ax1.set_ylim([0, 24])

        ax2.errorbar(sim_doubling_time[np.argsort(sim_doubling_time)[::-1]],
                     sim_origins_per_cell_at_initiation[np.argsort(
                         sim_doubling_time)[::-1]],
                     yerr=sim_origins_per_cell_at_initiation_std[np.argsort(
                         sim_doubling_time)[::-1]],
                     color=color_cycle[0],
                     **SIM_PLOT_STYLE)
        ax2.errorbar(bremer_tau[np.argsort(bremer_tau)[::-1]],
                     np.array(bremer_origins_per_cell_at_initiation)[
                         np.argsort(bremer_tau)[::-1]],
                     color=color_cycle[2],
                     **EXP_PLOT_STYLE)
        ax2.set_title("Average origins at chrom. init.", fontsize=FONT_SIZE)
        ax2.set_ylim([0.5, 4.5])

        ax3.errorbar(
            sim_doubling_time[np.argsort(sim_doubling_time)[::-1]],
            sim_rrn_init_rate[np.argsort(sim_doubling_time)[::-1]],
            yerr=sim_rrn_init_rate_std[np.argsort(sim_doubling_time)[::-1]],
            color=color_cycle[0],
            **SIM_PLOT_STYLE)
        ax3.errorbar(
            bremer_tau[np.argsort(bremer_tau)[::-1]],
            np.array(bremer_rrn_init_rate)[np.argsort(bremer_tau)[::-1]],
            color=color_cycle[2],
            **EXP_PLOT_STYLE)
        ax3.set_title("Rate of rrn initiation (1/min)", fontsize=FONT_SIZE)
        ax3.set_ylim([0, 2500])

        # ax3.legend(loc=1, frameon=True, fontsize=7)
        ax3.set_xlabel("Doubling time (min)", fontsize=FONT_SIZE)

        axes_list = [ax0, ax1, ax2, ax3]

        for a in axes_list:
            for tick in a.yaxis.get_major_ticks():
                tick.label.set_fontsize(FONT_SIZE)
            for tick in a.xaxis.get_major_ticks():
                tick.label.set_fontsize(FONT_SIZE)

        whitePadSparklineAxis(ax0, False)
        whitePadSparklineAxis(ax1)
        whitePadSparklineAxis(ax2, False)
        whitePadSparklineAxis(ax3)

        plt.subplots_adjust(bottom=0.2, wspace=0.3)

        exportFigure(plt, plotOutDir, plotOutFileName, metadata)
Beispiel #9
0
    def do_plot(self, inputDir, plotOutDir, plotOutFileName, simDataFile,
                validationDataFile, metadata):
        if not os.path.isdir(inputDir):
            raise Exception, "inputDir does not currently exist as a directory"

        if not os.path.exists(plotOutDir):
            os.mkdir(plotOutDir)

        if not os.path.exists(
                os.path.join(
                    plotOutDir,
                    "distribution_division_fluxome_proteome_data_matrix.xls")):
            print "%s must exist as an .xls file for this plot." % "distribution_division_fluxome_proteome_data_matrix.xls"
            return

        # Load data
        wb = open_workbook(
            os.path.join(
                plotOutDir,
                "distribution_division_fluxome_proteome_data_matrix.xls"))
        data_raw = wb.sheet_by_index(0)
        heading = data_raw.row_values(0)
        col_divisionTime = heading.index("division time")
        col_proteome = heading.index("proteome pearson r")
        col_fluxome = heading.index("fluxome pearson r")
        col_finalMass = heading.index("final dry mass (fg)")
        dataIndices = [col_divisionTime, col_proteome, col_fluxome]
        wt = np.array(data_raw.row_values(1), dtype=float)[dataIndices]

        # Load failures
        failures = cPickle.load(
            open(os.path.join(plotOutDir, "failed_variants.cPickle"), "rb"))
        flag1 = 0  # failure
        flag2 = 0  # 3-hour upper limit
        flag3 = 0  # final dry mass < 750
        data = []
        for varId, row in enumerate(xrange(2, data_raw.nrows)):
            row_values = data_raw.row_values(row)
            if (varId + 1) in failures:
                flag1 += 1
            elif row_values[col_divisionTime] == 180:
                flag2 += 1
            elif row_values[col_finalMass] < 750.:
                flag3 += 1
            else:
                data.append(np.array(row_values, dtype=float)[dataIndices])
        data = np.array(data)

        # Plot
        nrows = 6
        ncols = 9
        factor = 1.5
        fig = plt.figure(figsize=(ncols * factor, nrows * factor))
        diviAx = plt.subplot2grid((nrows, ncols), (0, 0 + 1),
                                  rowspan=2,
                                  colspan=2)
        protAx = plt.subplot2grid((nrows, ncols), (0, 3 + 1),
                                  rowspan=2,
                                  colspan=2)
        fluxAx = plt.subplot2grid((nrows, ncols), (0, 6 + 1),
                                  rowspan=2,
                                  colspan=2)
        dpAx = plt.subplot2grid((nrows, ncols), (4, 0 + 1),
                                rowspan=2,
                                colspan=2)
        pfAx = plt.subplot2grid((nrows, ncols), (4, 3 + 1),
                                rowspan=2,
                                colspan=2)
        fdAx = plt.subplot2grid((nrows, ncols), (4, 6 + 1),
                                rowspan=2,
                                colspan=2)
        dp_dAx = plt.subplot2grid((nrows, ncols), (3, 1), rowspan=1, colspan=2)
        pf_pAx = plt.subplot2grid((nrows, ncols), (3, 4), rowspan=1, colspan=2)
        dp_pAx = plt.subplot2grid((nrows, ncols), (4, 0), rowspan=2, colspan=1)
        fd_fAx = plt.subplot2grid((nrows, ncols), (3, 7), rowspan=1, colspan=2)
        pf_fAx = plt.subplot2grid((nrows, ncols), (4, 3), rowspan=2, colspan=1)
        fd_dAx = plt.subplot2grid((nrows, ncols), (4, 6), rowspan=2, colspan=1)
        histAxes = [diviAx, protAx, fluxAx]
        axesList = [
            diviAx, protAx, fluxAx, dpAx, pfAx, fdAx, dp_dAx, dp_pAx, pf_pAx,
            fd_fAx, pf_fAx, fd_dAx
        ]

        # Plot histograms
        nBins = 50
        histTitles = [
            "Division Time", "Proteome Comparison", "Fluxome Comparison"
        ]
        histXlabels = ["Time (min)", "PCC", "PCC"]
        for i, ax in enumerate(histAxes):
            if i == 2:
                fluxAx.set_ylim([fluxAx.get_ylim()[0], 55])
            ax.hist(data[:, i],
                    bins=nBins,
                    edgecolor="none",
                    facecolor="k",
                    alpha=0.5)
            ax.plot([wt[i], wt[i]], [0, ax.get_ylim()[1]],
                    "r",
                    linewidth=LINEWIDTH)
            ax.set_title("%s\nwt = %0.2f" % (histTitles[i], wt[i]),
                         fontsize=TITLE_FONTSIZE)
            ax.set_ylabel("Frequency", fontsize=TITLE_FONTSIZE)
            ax.set_xlabel(histXlabels[i], fontsize=TITLE_FONTSIZE)

        diviAx.set_xlim([30, diviAx.get_xlim()[1]])
        diviAx.text(wt[0] - 5,
                    50,
                    "%0.0f %%" %
                    ((data[:, 0] < wt[0]).sum() / data.shape[0] * 100.),
                    size=LABEL_FONTSIZE,
                    horizontalalignment="right")
        diviAx.text(wt[0] + 5,
                    50,
                    "%0.0f %%" %
                    ((data[:, 0] > wt[0]).sum() / data.shape[0] * 100.),
                    size=LABEL_FONTSIZE,
                    horizontalalignment="left")

        protAx.text(wt[1] - 0.005,
                    40,
                    "%0.0f %%" %
                    ((data[:, 1] < wt[1]).sum() / data.shape[0] * 100.),
                    size=LABEL_FONTSIZE,
                    horizontalalignment="right")
        protAx.text(wt[1] + 0.005,
                    40,
                    "%0.0f %%" %
                    ((data[:, 1] > wt[1]).sum() / data.shape[0] * 100.),
                    size=LABEL_FONTSIZE,
                    horizontalalignment="left")

        fluxAx.text(wt[2] - 0.1,
                    50,
                    "%0.1f %%" % (float(
                        (data[:, 2] < wt[2]).sum()) / data.shape[0] * 100.),
                    size=LABEL_FONTSIZE,
                    horizontalalignment="right")
        fluxAx.text(wt[2] + 0.1,
                    50,
                    "%0.1f %%" % (float(
                        (data[:, 2] > wt[2]).sum()) / data.shape[0] * 100.),
                    size=LABEL_FONTSIZE,
                    horizontalalignment="left")

        for ax in histAxes:
            whitePadSparklineAxis(ax)

        # Plot scatter plots
        axesList = [dpAx, pfAx, fdAx]
        xIndices = [0, 1, 2]
        yIndices = [1, 2, 0]
        for i, ax in enumerate(axesList):
            ax.scatter(data[:, xIndices[i]],
                       data[:, yIndices[i]],
                       s=2,
                       edgecolor="none",
                       facecolor="k")
            ax.plot(wt[xIndices[i]],
                    wt[yIndices[i]],
                    "r.",
                    markersize=MARKERSIZE)
            ax.set_xlim(histAxes[xIndices[i]].get_xlim())
            ax.set_ylim(histAxes[yIndices[i]].get_xlim())
            whitePadSparklineAxis(ax)
            ax.axvline(x=wt[xIndices[i]], linestyle="--", color="r")
            ax.axhline(y=wt[yIndices[i]], linestyle="--", color="r")

        # Plot small histograms
        fd_fAx.set_ylim([fluxAx.get_ylim()[0], 55])
        titles = [
            "Division Time (min)", "Proteome Comparison (PCC)",
            "Fluxome Comparison (PCC)"
        ]
        for i, ax in enumerate([dp_dAx, pf_pAx, fd_fAx]):
            ax.hist(data[:, i],
                    bins=nBins,
                    edgecolor="none",
                    facecolor="k",
                    alpha=0.5)
            ax.axvline(wt[i], color="r", linewidth=LINEWIDTH)
            removeXAxis(ax)
            whitePadSparklineAxis(ax)
            ax.set_title(titles[i], fontsize=TITLE_FONTSIZE)

        pf_fAx.set_xlim([fluxAx.get_ylim()[0], 55])
        for i, ax in enumerate([fd_dAx, dp_pAx, pf_fAx]):
            ax.hist(data[:, i],
                    bins=nBins,
                    edgecolor="none",
                    facecolor="k",
                    alpha=0.5,
                    orientation="horizontal")
            ax.axhline(wt[i], color="r", linewidth=LINEWIDTH)
            removeYAxis(ax)
            whitePadSparklineAxis(ax)
            ax.set_ylabel(titles[i], fontsize=TITLE_FONTSIZE)
            ax.invert_xaxis()

        for ax in axesList:
            ax.tick_params(labelsize=LABEL_FONTSIZE)

        plt.subplots_adjust(wspace=2, hspace=2, left=0.075, right=0.95)
        exportFigure(plt, plotOutDir, plotOutFileName, metadata)

        if GENERATE_CLEAN_SUBPLOTS:

            # Histograms
            fig, ax = plt.subplots(1, 1, figsize=(5, 5))
            filenames = ["Division", "Proteome", "Fluxome"]
            axesList = [diviAx, protAx, fluxAx]
            for i, filename in enumerate(filenames):
                ax.set_xlim(axesList[i].get_xlim())
                ax.set_ylim(axesList[i].get_ylim())
                ax.plot([wt[i], wt[i]], [0, ax.get_ylim()[1]],
                        "r",
                        linewidth=LINEWIDTH)
                ax.hist(data[:, i],
                        bins=nBins,
                        edgecolor="none",
                        facecolor="k",
                        alpha=0.5)
                whitePadSparklineAxis(ax)
                removeXAxis(ax)
                cleanAxis(ax)
                exportFigure(plt, plotOutDir,
                             plotOutFileName + "_%s" % filename, metadata)
                plt.cla()

            # Scatter plots
            fig, ax = plt.subplots(1, 1, figsize=(5, 5))
            filenames = ["DP", "PF", "FD"]
            axesList = [dpAx, pfAx, fdAx]
            xIndices = [0, 1, 2]
            yIndices = [1, 2, 0]
            for i, filename in enumerate(filenames):
                ax.scatter(data[:, xIndices[i]],
                           data[:, yIndices[i]],
                           s=SCATTER_MSIZE,
                           edgecolor="none",
                           facecolor="k")
                ax.plot(wt[xIndices[i]],
                        wt[yIndices[i]],
                        "r.",
                        markersize=MARKERSIZE)
                ax.set_xlim(axesList[i].get_xlim())
                ax.set_ylim(axesList[i].get_ylim())
                removeYAxis(ax)
                whitePadSparklineAxis(ax)
                ax.axvline(x=wt[xIndices[i]], linestyle="--", color="r")
                ax.axhline(y=wt[yIndices[i]], linestyle="--", color="r")
                cleanAxis(ax)
                exportFigure(plt, plotOutDir,
                             plotOutFileName + "_%s" % filename, metadata)
                plt.cla()
            plt.close("all")

            # Small histograms for splom (only generates side histograms)
            filenames = ["Division", "Proteome", "Fluxome"]
            axesList = [diviAx, protAx, fluxAx]
            for i, filename in enumerate(filenames):
                fig, ax = plt.subplots(1, 1, figsize=(2, 5))
                ax.set_ylim(axesList[i].get_xlim())
                ax.set_xlim(axesList[i].get_ylim())
                ax.hist(data[:, i],
                        bins=nBins,
                        edgecolor="none",
                        facecolor="k",
                        alpha=0.5,
                        orientation="horizontal")
                ax.axhline(wt[i], color="r", linewidth=LINEWIDTH)
                ax.invert_xaxis()
                whitePadSparklineAxis(ax)
                cleanAxis(ax)
                exportFigure(plt, plotOutDir,
                             plotOutFileName + "_%s_%s" % (filename, "small"),
                             metadata)
                plt.close("all")

        plt.close("all")
    def do_plot(self, seedOutDir, plotOutDir, plotOutFileName, simDataFile,
                validationDataFile, metadata):
        if not os.path.isdir(seedOutDir):
            raise Exception, "seedOutDir does not currently exist as a directory"

        if not os.path.exists(plotOutDir):
            os.mkdir(plotOutDir)

        # Check if basal sim
        sim_data = cPickle.load(open(simDataFile, "rb"))
        if sim_data.condition != "basal":
            print "Skipping - plot only runs for basal sim."
            return

        # Get all cells
        ap = AnalysisPaths(seedOutDir, cohort_plot=True)
        allDir = ap.get_cells(seed=[0], generation=GENS)
        n_gens = GENS.size
        if len(allDir) < n_gens:
            print "Skipping - particular seed and/or gens were not simulated."
            return

        # Get all ids reqiured
        ids_complexation = sim_data.process.complexation.moleculeNames  # Complexes of proteins, and protein monomers
        ids_complexation_complexes = sim_data.process.complexation.ids_complexes  # Only complexes
        ids_equilibrium = sim_data.process.equilibrium.moleculeNames  # Complexes of proteins + small molecules, small molecules, protein monomers
        ids_equilibrium_complexes = sim_data.process.equilibrium.ids_complexes  # Only complexes
        ids_translation = sim_data.process.translation.monomerData[
            "id"].tolist()  # Only protein monomers
        ids_transcription = sim_data.process.transcription.rnaData[
            "id"].tolist()

        data_50s = sim_data.process.complexation.getMonomers(
            sim_data.moleculeIds.s50_fullComplex)
        data_30s = sim_data.process.complexation.getMonomers(
            sim_data.moleculeIds.s30_fullComplex)
        ribosome_subunit_ids = data_50s["subunitIds"].tolist(
        ) + data_30s["subunitIds"].tolist()
        ribosome_subunit_stoich = np.hstack(
            (data_50s["subunitStoich"], data_30s["subunitStoich"]))
        data_rnap = sim_data.process.complexation.getMonomers(
            sim_data.moleculeIds.rnapFull)
        rnap_subunit_ids = data_rnap["subunitIds"].tolist()
        rnap_subunit_stoich = data_rnap["subunitStoich"]

        # Stoich matrices
        complexStoich = sim_data.process.complexation.stoichMatrixMonomers()
        equilibriumStoich = sim_data.process.equilibrium.stoichMatrixMonomers()

        first_build = True

        # Pre-allocate variables. Rows = Generations, Cols = Monomers
        n_monomers = sim_data.process.translation.monomerData['id'].size

        ratioFinalToInitialCountMultigen = np.zeros((n_gens, n_monomers),
                                                    dtype=np.float)

        if not FROM_CACHE:
            print "Re-running - not using cache"
            for gen_idx, simDir in enumerate(allDir):
                simOutDir = os.path.join(simDir, "simOut")

                time = TableReader(os.path.join(simOutDir,
                                                "Main")).readColumn("time")

                ## READ DATA ##
                # Read in bulk ids and counts
                bulkMolecules = TableReader(
                    os.path.join(simOutDir, "BulkMolecules"))

                if first_build:
                    print "Running first build code"
                    moleculeIds = bulkMolecules.readAttribute("objectNames")

                    complexationIdx = np.array([
                        moleculeIds.index(x) for x in ids_complexation
                    ])  # Complexes of proteins, and protein monomers
                    complexation_complexesIdx = np.array([
                        moleculeIds.index(x)
                        for x in ids_complexation_complexes
                    ])  # Only complexes
                    equilibriumIdx = np.array(
                        [moleculeIds.index(x) for x in ids_equilibrium]
                    )  # Complexes of proteins + small molecules, small molecules, protein monomers
                    equilibrium_complexesIdx = np.array([
                        moleculeIds.index(x) for x in ids_equilibrium_complexes
                    ])  # Only complexes
                    translationIdx = np.array([
                        moleculeIds.index(x) for x in ids_translation
                    ])  # Only protein monomers
                    transcriptionIdx = np.array([
                        moleculeIds.index(x) for x in ids_transcription
                    ])  # Only protein rnas
                    ribosomeIdx = np.array(
                        [moleculeIds.index(x) for x in ribosome_subunit_ids])
                    rnapIdx = np.array(
                        [moleculeIds.index(x) for x in rnap_subunit_ids])

                    cPickle.dump(
                        complexationIdx,
                        open(
                            os.path.join(plotOutDir, "complexationIdx.pickle"),
                            "wb"))
                    cPickle.dump(
                        complexation_complexesIdx,
                        open(
                            os.path.join(plotOutDir,
                                         "complexation_complexesIdx.pickle"),
                            "wb"))
                    cPickle.dump(
                        equilibriumIdx,
                        open(os.path.join(plotOutDir, "equilibriumIdx.pickle"),
                             "wb"))
                    cPickle.dump(
                        equilibrium_complexesIdx,
                        open(
                            os.path.join(plotOutDir,
                                         "equilibrium_complexesIdx.pickle"),
                            "wb"))
                    cPickle.dump(
                        translationIdx,
                        open(os.path.join(plotOutDir, "translationIdx.pickle"),
                             "wb"))
                    cPickle.dump(
                        transcriptionIdx,
                        open(
                            os.path.join(plotOutDir,
                                         "transcriptionIdx.pickle"), "wb"))
                    cPickle.dump(
                        ribosomeIdx,
                        open(os.path.join(plotOutDir, "ribosomeIdx.pickle"),
                             "wb"))
                    cPickle.dump(
                        rnapIdx,
                        open(os.path.join(plotOutDir, "rnapIdx.pickle"), "wb"))

                    first_build = False

                bulkCounts = bulkMolecules.readColumn("counts")
                bulkMolecules.close()

                # Dissociate protein-protein complexes
                bulkCounts[:, complexationIdx] += np.dot(
                    complexStoich,
                    bulkCounts[:, complexation_complexesIdx].transpose() *
                    -1).transpose().astype(np.int)

                # Dissociate protein-small molecule complexes
                bulkCounts[:, equilibriumIdx] += np.dot(
                    equilibriumStoich,
                    bulkCounts[:, equilibrium_complexesIdx].transpose() *
                    -1).transpose().astype(np.int)

                # Load unique molecule data for RNAP and ribosomes
                uniqueMoleculeCounts = TableReader(
                    os.path.join(simOutDir, "UniqueMoleculeCounts"))
                ribosomeIndex = uniqueMoleculeCounts.readAttribute(
                    "uniqueMoleculeIds").index("activeRibosome")
                rnaPolyIndex = uniqueMoleculeCounts.readAttribute(
                    "uniqueMoleculeIds").index("activeRnaPoly")
                nActiveRibosome = uniqueMoleculeCounts.readColumn(
                    "uniqueMoleculeCounts")[:, ribosomeIndex]
                nActiveRnaPoly = uniqueMoleculeCounts.readColumn(
                    "uniqueMoleculeCounts")[:, rnaPolyIndex]
                uniqueMoleculeCounts.close()

                # Add subunits from RNAP and ribosomes
                ribosomeSubunitCounts = (
                    nActiveRibosome.reshape((nActiveRibosome.size, 1)) *
                    ribosome_subunit_stoich.reshape(
                        (1, ribosome_subunit_stoich.size)))
                rnapSubunitCounts = (nActiveRnaPoly.reshape(
                    (nActiveRnaPoly.size, 1)) * rnap_subunit_stoich.reshape(
                        (1, rnap_subunit_stoich.size)))

                bulkCounts[:, ribosomeIdx] += ribosomeSubunitCounts.astype(
                    np.int64)
                bulkCounts[:, rnapIdx] += rnapSubunitCounts.astype(np.int64)

                # Get protein monomer counts for calculations now that all complexes are dissociated
                proteinMonomerCounts = bulkCounts[:, translationIdx]
                ratioFinalToInitialCount = (
                    proteinMonomerCounts[-1, :] +
                    1) / (proteinMonomerCounts[0, :].astype(np.float) + 1)
                ratioFinalToInitialCountMultigen[
                    gen_idx, :] = ratioFinalToInitialCount

            cPickle.dump(
                ratioFinalToInitialCountMultigen,
                open(
                    os.path.join(plotOutDir,
                                 "ratioFinalToInitialCountMultigen.pickle"),
                    "wb"))

        ratioFinalToInitialCountMultigen = cPickle.load(
            open(
                os.path.join(plotOutDir,
                             "ratioFinalToInitialCountMultigen.pickle"), "rb"))
        complexationIdx = cPickle.load(
            open(os.path.join(plotOutDir, "complexationIdx.pickle"), "rb"))
        complexation_complexesIdx = cPickle.load(
            open(os.path.join(plotOutDir, "complexation_complexesIdx.pickle"),
                 "rb"))
        equilibriumIdx = cPickle.load(
            open(os.path.join(plotOutDir, "equilibriumIdx.pickle"), "rb"))
        equilibrium_complexesIdx = cPickle.load(
            open(os.path.join(plotOutDir, "equilibrium_complexesIdx.pickle"),
                 "rb"))
        translationIdx = cPickle.load(
            open(os.path.join(plotOutDir, "translationIdx.pickle"), "rb"))
        transcriptionIdx = cPickle.load(
            open(os.path.join(plotOutDir, "transcriptionIdx.pickle"), "rb"))
        ribosomeIdx = cPickle.load(
            open(os.path.join(plotOutDir, "ribosomeIdx.pickle"), "rb"))
        rnapIdx = cPickle.load(
            open(os.path.join(plotOutDir, "rnapIdx.pickle"), "rb"))

        protein_index_of_interest = np.where(
            np.logical_and(
                ratioFinalToInitialCountMultigen > 1.6,
                ratioFinalToInitialCountMultigen < 2.4).all(axis=0))[0]
        first_gen_flat = ratioFinalToInitialCountMultigen[0, :] < 1.1
        second_gen_burst = ratioFinalToInitialCountMultigen[1, :] > 10
        rest_of_gens_decline = (ratioFinalToInitialCountMultigen[2:, :] <
                                1.1).all(axis=0)
        logic_filter = np.logical_and.reduce(
            (first_gen_flat, second_gen_burst, rest_of_gens_decline))
        protein_index_of_interest_burst = np.where(logic_filter)[0]
        try:  # try block expects particular proteins to plot
            protein_idx = protein_index_of_interest[0]
            protein_idx_burst = protein_index_of_interest_burst[7]
        except Exception as exc:
            print "Error: %s" % exc
            return

        fig, axesList = plt.subplots(ncols=2, nrows=2, sharex=True)
        expProtein_axis = axesList[0, 0]
        expRna_axis = axesList[1, 0]
        burstProtein_axis = axesList[0, 1]
        burstRna_axis = axesList[1, 1]

        mult = 3
        fig_width = mm2inch(80) * mult
        fig_height = mm2inch(50) * mult

        fig.set_figwidth(fig_width)
        fig.set_figheight(fig_height)
        firstLine = True

        plt.style.use('seaborn-deep')
        color_cycle = plt.rcParams['axes.prop_cycle'].by_key()['color']
        exponential_color = color_cycle[2]
        subgen_color = color_cycle[0]

        time_eachGen = []
        y_min = np.full(4, np.inf)
        y_max = np.zeros(4)

        for gen_idx, simDir in enumerate(allDir):
            simOutDir = os.path.join(simDir, "simOut")
            time = TableReader(os.path.join(simOutDir,
                                            "Main")).readColumn("time")
            time_eachGen.append(time[0])
            if gen_idx == 0:
                startTime = time[0]

            ## READ DATA ##
            # Read in bulk ids and counts
            bulkMolecules = TableReader(
                os.path.join(simOutDir, "BulkMolecules"))
            bulkCounts = bulkMolecules.readColumn("counts")
            bulkMolecules.close(
            )  # NOTE (John): .close() doesn't currently do anything

            # Dissociate protein-protein complexes
            bulkCounts[:, complexationIdx] += np.dot(
                complexStoich,
                bulkCounts[:, complexation_complexesIdx].transpose() *
                -1).transpose().astype(np.int)

            # Dissociate protein-small molecule complexes
            bulkCounts[:, equilibriumIdx] += np.dot(
                equilibriumStoich,
                bulkCounts[:, equilibrium_complexesIdx].transpose() *
                -1).transpose().astype(np.int)

            # Load unique molecule data for RNAP and ribosomes
            uniqueMoleculeCounts = TableReader(
                os.path.join(simOutDir, "UniqueMoleculeCounts"))
            ribosomeIndex = uniqueMoleculeCounts.readAttribute(
                "uniqueMoleculeIds").index("activeRibosome")
            rnaPolyIndex = uniqueMoleculeCounts.readAttribute(
                "uniqueMoleculeIds").index("activeRnaPoly")
            nActiveRibosome = uniqueMoleculeCounts.readColumn(
                "uniqueMoleculeCounts")[:, ribosomeIndex]
            nActiveRnaPoly = uniqueMoleculeCounts.readColumn(
                "uniqueMoleculeCounts")[:, rnaPolyIndex]
            uniqueMoleculeCounts.close()

            # Add subunits from RNAP and ribosomes
            ribosomeSubunitCounts = (nActiveRibosome.reshape(
                (nActiveRibosome.size, 1)) * ribosome_subunit_stoich.reshape(
                    (1, ribosome_subunit_stoich.size)))
            rnapSubunitCounts = (nActiveRnaPoly.reshape(
                (nActiveRnaPoly.size, 1)) * rnap_subunit_stoich.reshape(
                    (1, rnap_subunit_stoich.size)))

            bulkCounts[:,
                       ribosomeIdx] += ribosomeSubunitCounts.astype(np.int64)
            bulkCounts[:, rnapIdx] += rnapSubunitCounts.astype(np.int64)

            # Get protein monomer counts for calculations now that all complexes are dissociated
            proteinMonomerCounts = bulkCounts[:, translationIdx]
            rnaMonomerCounts = bulkCounts[:, transcriptionIdx]

            if firstLine:
                firstLine = False

            LINEWIDTH = 1

            time_minutes = time / 60.

            axes = (expProtein_axis, burstProtein_axis, expRna_axis,
                    burstRna_axis)
            counts = (
                proteinMonomerCounts[:, protein_idx],
                proteinMonomerCounts[:, protein_idx_burst],
                rnaMonomerCounts[:, sim_data.relation.rnaIndexToMonomerMapping]
                [:, protein_idx],
                rnaMonomerCounts[:, sim_data.relation.rnaIndexToMonomerMapping]
                [:, protein_idx_burst])
            line_color = (
                exponential_color,
                subgen_color,
                exponential_color,
                subgen_color,
            )
            count_min = (  # better to acquire programatically, but would require loading data twice
                600, 0, 0, 0)
            count_scale = (  # same as above
                2200 - 600, 30 - 0, 7 - 0, 1 - 0)

            # These are *approximate* estimates of the axes sizes, using the
            # size of the figure plus the fact that the subplots are 2x2.
            # This is good enough since we primarily need the aspect ratio;
            # however there is probably a programmatic way to get this info
            # from the axes objects themselves.
            axes_width = fig_width / 2
            axes_height = fig_height / 2

            # No easy way to know how long the total set of simulations
            # will be without rewriting a lot of code, so assume that
            # the total time is roughly the time of the current generation
            # multiplied by the total number of generations.
            rescaled_time = (time_minutes - time_minutes.min()) / (
                (time_minutes.max() - time_minutes.min()) * n_gens)

            for i, (ax, c, lc, cm, cs) in enumerate(
                    izip(axes, counts, line_color, count_min, count_scale)):
                rescaled_counts = (c.astype(np.float64) - cm) / cs

                # Roughly rescale the data into the plotted dimensions for
                # better point downsampling
                points = np.column_stack([
                    rescaled_time * axes_width, rescaled_counts * axes_height
                ])

                RDP_THRESHOLD = 1e-5

                keep = rdp(points, RDP_THRESHOLD)

                x = time_minutes[keep]
                y = c[keep]

                if y.min() < y_min[i]:
                    y_min[i] = y.min()
                if y.max() > y_max[i]:
                    y_max[i] = y.max()

                ax.plot(x, y, color=lc, linewidth=LINEWIDTH)

        expProtein_axis.set_ylim([y_min[0], y_max[0]])
        burstProtein_axis.set_ylim([y_min[1], y_max[1]])
        expRna_axis.set_ylim([y_min[2], y_max[2]])
        burstRna_axis.set_ylim([y_min[3], y_max[3]])

        expProtein_axis.set_title("Exponential dynamics: {}".format(
            sim_data.process.translation.monomerData['id'][protein_idx][:-3]),
                                  fontsize=9)
        burstProtein_axis.set_title("Sub-generational dynamics: {}".format(
            sim_data.process.translation.monomerData['id'][protein_idx_burst]
            [:-3]),
                                    fontsize=9)
        expProtein_axis.set_ylabel("Protein\ncount", rotation=0, fontsize=9)
        expRna_axis.set_ylabel("mRNA\ncount", rotation=0, fontsize=9)

        time_eachGen.append(time[-1])
        time_eachGen = np.array(time_eachGen)

        expProtein_axis.set_xlim([startTime / 60., time[-1] / 60.])
        burstProtein_axis.set_xlim([startTime / 60., time[-1] / 60.])
        expRna_axis.set_xlim([startTime / 60., time[-1] / 60.])
        burstRna_axis.set_xlim([startTime / 60., time[-1] / 60.])

        whitePadSparklineAxis(expProtein_axis, False)
        whitePadSparklineAxis(burstProtein_axis, False)
        whitePadSparklineAxis(expRna_axis)
        whitePadSparklineAxis(burstRna_axis)

        expRna_axis.set_xticks(time_eachGen / 60.)
        burstRna_axis.set_xticks(time_eachGen / 60.)
        xlabel = GENS.tolist()
        xlabel.append(GENS[-1] + 1)
        expRna_axis.set_xticklabels(xlabel)
        burstRna_axis.set_xticklabels(xlabel)

        burstRna_axis.set_xlabel("Time (gens)", fontsize=9)
        expRna_axis.set_xlabel("Time (gens)", fontsize=9)

        axesList = axesList.flatten().tolist()
        for axes in axesList:
            for tick in axes.xaxis.get_major_ticks():
                tick.label.set_fontsize(9)
            for tick in axes.yaxis.get_major_ticks():
                tick.label.set_fontsize(9)

        plt.subplots_adjust(wspace=0.2, hspace=0.15)
        exportFigure(plt, plotOutDir, plotOutFileName, metadata)

        for axes in axesList:
            axes.set_xlabel("")
            axes.set_ylabel("")
            axes.set_title("")
            axes.set_xticklabels([])
            axes.set_yticklabels([])

        exportFigure(plt, plotOutDir, plotOutFileName + "_stripped", metadata)
        plt.close("all")
Beispiel #11
0
    def do_plot(self, seedOutDir, plotOutDir, plotOutFileName, simDataFile,
                validationDataFile, metadata):
        if not os.path.isdir(seedOutDir):
            raise Exception, "seedOutDir does not currently exist as a directory"

        if not os.path.exists(plotOutDir):
            os.mkdir(plotOutDir)

        ## Identify sub-genenerationally transcribed genes
        # Get all cells
        ap = AnalysisPaths(seedOutDir, multi_gen_plot=True)
        allDir = ap.get_cells()

        # Load sim data
        sim_data = cPickle.load(open(simDataFile, "rb"))
        rnaIds = sim_data.process.transcription.rnaData["id"]
        geneIds = sim_data.process.transcription.rnaData["geneId"]

        # For each generation
        nonzeroSumRnaCounts_allGens = []
        for i, simDir in enumerate(allDir):
            simOutDir = os.path.join(simDir, "simOut")

            # Read counts of transcripts
            bulkMolecules = TableReader(
                os.path.join(simOutDir, "BulkMolecules"))
            if i == 0:
                moleculeIds = bulkMolecules.readAttribute("objectNames")
                rnaIndices = np.array([moleculeIds.index(x) for x in rnaIds])
            rnaCounts = bulkMolecules.readColumn("counts")[:, rnaIndices]
            bulkMolecules.close()

            # Sum counts over timesteps
            sumRnaCounts = rnaCounts.sum(axis=0)

            # Flag where the sum is nonzero (True if nonzero, False if zero)
            nonzeroSumRnaCounts = sumRnaCounts != 0
            nonzeroSumRnaCounts_allGens.append(nonzeroSumRnaCounts)

        # Average (mean) over generations
        nonzeroSumRnaCounts_allGens = np.array(nonzeroSumRnaCounts_allGens)
        avgRnaCounts = nonzeroSumRnaCounts_allGens.mean(axis=0)

        # Identify subgenerationally transcribed genes
        subgenRnaIndices = np.where(
            np.logical_and(avgRnaCounts != 0., avgRnaCounts != 1.))[0]
        subgenRnaIds = rnaIds[subgenRnaIndices]
        subgenMonomerIndices = [
            np.where(
                sim_data.process.translation.monomerData["rnaId"] == x)[0][0]
            for x in subgenRnaIds
        ]
        subgenMonomerIds = sim_data.process.translation.monomerData["id"][
            subgenMonomerIndices]

        # Identify subgenerationally transcribed genes that function as monomers
        complexationMoleculeNames = sim_data.process.complexation.moleculeNames
        subgenMonomerOnlyIds = [
            x for x in subgenMonomerIds if x not in complexationMoleculeNames
        ]
        subgenMonomersInComplexesIds = [
            x for x in subgenMonomerIds if x in complexationMoleculeNames
        ]

        ## Identify complexes that subgenerationally transcribed genes participate in
        subgenComplexIds = []
        for complexId in sim_data.process.complexation.complexNames:
            subunitIds = sim_data.process.complexation.getMonomers(
                complexId)["subunitIds"]
            if np.any([x in subgenMonomerIds for x in subunitIds]):
                subgenComplexIds.append(complexId)
        subgenComplexIds = list(set(subgenComplexIds))

        ## Identify functional units that have a 0 count for at least one timestep
        proteinIds = list(set(subgenMonomerOnlyIds + subgenComplexIds))
        if not len(proteinIds):
            print "Returned -- No subgenerational functional units were found."
            return
        zeroAtLeastOnce = np.zeros(len(proteinIds), dtype=bool)
        for i, simDir in enumerate(allDir):
            simOutDir = os.path.join(simDir, "simOut")

            # Read counts of proteins
            bulkMolecules = TableReader(
                os.path.join(simOutDir, "BulkMolecules"))
            if i == 0:
                moleculeIds = bulkMolecules.readAttribute("objectNames")
                proteinIndices = np.array(
                    [moleculeIds.index(x) for x in proteinIds])
                proteinCounts = bulkMolecules.readColumn("counts")[
                    SKIP_TIMESTEPS:, proteinIndices]
            else:
                proteinCounts = bulkMolecules.readColumn(
                    "counts")[:, proteinIndices]
            bulkMolecules.close()

            # Flag proteins with a minimum count of 0
            minProteinCounts = np.min(proteinCounts, axis=0)
            zeroAtLeastOnce[minProteinCounts == 0] = True

        ## Identify essential functional units
        # Load validation data
        validation_data = cPickle.load(open(validationDataFile, "rb"))
        essentialGenes_genes = validation_data.essentialGenes.essentialGenes
        essentialGenes_monomers = validation_data.essentialGenes.essentialProteins

        # Flag essential monomers
        essential = np.zeros(len(proteinIds), dtype=bool)
        essential[[x in essentialGenes_monomers for x in proteinIds]] = True

        # Flag essential complexes
        subgenMonomerIdToComplexIds = {}
        for complexId in subgenComplexIds:
            subunitIds = sim_data.process.complexation.getMonomers(
                complexId)["subunitIds"]
            isEssential = np.any(
                [x in essentialGenes_monomers for x in subunitIds])

            if isEssential:
                complexIndex = proteinIds.index(complexId)
                essential[complexIndex] = True

            # Add unadded complexes to subgenMonomerIdToComplexIds
            for monomerId in subunitIds:
                if monomerId not in subgenMonomerIdToComplexIds.keys():
                    subgenMonomerIdToComplexIds[monomerId] = []
                subgenMonomerIdToComplexIds[monomerId].append(complexId)

        ## Plot
        nrows = 2
        ncols = 3
        fig, axesList = plt.subplots(nrows, ncols, figsize=(11, 8.5))
        [[axMonomers, axComplexes, axTotal],
         [axMonomersEssential, axComplexesEssential,
          axTotalEssential]] = axesList
        xloc = np.arange(2)
        width = 0.75

        # Plot subgenerational genes that don't form complexes
        args = {
            "proteinIds": proteinIds,
            "zeroAtLeastOnce": zeroAtLeastOnce,
            "essential": essential,
            "ax": axMonomers,
            "axEssential": axMonomersEssential,
            "xloc": xloc,
            "width": width,
            "functionalUnitIds": subgenMonomerOnlyIds,
        }
        plot(**args)
        axMonomers.set_title("{0} monomeric\nfunctional units".format(
            len(subgenMonomerOnlyIds)))
        axMonomers.set_ylabel("# functional units")
        axMonomersEssential.set_ylabel(
            "# functional units\n(essential genes in blue)")

        # Plot subgenerational genes that form complexes
        args["ax"] = axComplexes
        args["axEssential"] = axComplexesEssential
        args["functionalUnitIds"] = subgenComplexIds
        plot(**args)
        axComplexes.set_title("{0} complexed\nfunctional units".format(
            len(subgenComplexIds)))

        # Plot subgenenerational functional units
        args["ax"] = axTotal
        args["axEssential"] = axTotalEssential
        args["functionalUnitIds"] = []
        plot(**args)
        axTotal.set_title("{0} + {1} = {2}\nfunctional units".format(
            len(subgenMonomerOnlyIds), len(subgenComplexIds), len(proteinIds)))

        plt.subplots_adjust(wspace=0.3)
        exportFigure(plt, plotOutDir, plotOutFileName, metadata)
        plt.close("all")

        # Plot unlabeled version of final panel for Fig. 4E
        fig, ax = plt.subplots(1, 1, figsize=(8.5, 11))
        notAlwaysPresent = zeroAtLeastOnce.sum()
        alwaysPresent = len(proteinIds) - notAlwaysPresent
        notAlwaysPresentEssential = np.logical_and(zeroAtLeastOnce,
                                                   essential).sum()
        alwaysPresentEssential = np.logical_and(
            np.logical_not(zeroAtLeastOnce), essential).sum()
        barAll = ax.bar(xloc + width, [alwaysPresent, notAlwaysPresent],
                        width,
                        color="lightgray")
        barEssential = ax.bar(
            xloc + width, [alwaysPresentEssential, notAlwaysPresentEssential],
            width,
            color="blue")

        # Format
        whitePadSparklineAxis(ax)
        ax.set_xticks(xloc + width)
        ax.set_xticklabels([])
        ax.set_yticks([0] +
                      [x[0].get_height() for x in [barAll, barEssential]])

        axRight = ax.twinx()
        axRight.set_ylim(ax.get_ylim())
        axRight.set_yticks([x[1].get_height() for x in [barAll, barEssential]])

        axRight.spines["left"].set_visible(False)
        axRight.spines["top"].set_visible(False)
        axRight.spines["right"].set_position(("outward", 10))
        axRight.spines["bottom"].set_position(("outward", 10))
        for axis in [ax, axRight]:
            axis.set_yticklabels([])
            axis.tick_params(length=10.0)

        plt.subplots_adjust(bottom=0.3, top=0.7, left=0.2, right=0.8)
        exportFigure(plt, plotOutDir, "{0}_clean".format(plotOutFileName),
                     metadata)
        plt.close("all")

        if WRITE_TO_FILE:
            with open(os.path.join(plotOutDir, "%s.tsv" % plotOutFileName),
                      "wb") as f:
                f.write(
                    "gene\trna\tmonomer\tcomplex(es)\tzero_at_least_once\tessential\n"
                )
                for rnaId, monomerId in zip(subgenRnaIds, subgenMonomerIds):
                    # Get gene Id
                    rnaIndex = np.where(rnaIds == rnaId)[0][0]
                    geneId = geneIds[rnaIndex]

                    # Get IDs of complex(es)
                    if monomerId in subgenMonomerIdToComplexIds:
                        complexIds = subgenMonomerIdToComplexIds[monomerId]
                        functionalUnitIds = complexIds
                    else:
                        complexIds = None
                        functionalUnitIds = [monomerId]

                    # Were funcational units 0 at least once
                    isZero = []
                    for functionalUnitId in functionalUnitIds:
                        functionalUnitIndex = proteinIds.index(
                            functionalUnitId)
                        isZero.append(zeroAtLeastOnce[functionalUnitIndex])

                    # Is this gene essential
                    isEssential = geneId in essentialGenes_genes

                    # Write
                    f.write("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\n".format(
                        geneId, rnaId, monomerId, complexIds, isZero,
                        isEssential))
    def do_plot(self, seedOutDir, plotOutDir, plotOutFileName, simDataFile,
                validationDataFile, metadata):
        if not os.path.isdir(seedOutDir):
            raise Exception, "seedOutDir does not currently exist as a directory"

        if not os.path.exists(plotOutDir):
            os.mkdir(plotOutDir)

        # Get all cells
        ap = AnalysisPaths(seedOutDir, multi_gen_plot=True)
        allDir = ap.get_cells()

        if len(allDir) <= 1:
            print "Skipping - this plot only runs for multigen sims"
            return

        sim_data = cPickle.load(open(simDataFile, "rb"))
        validation_data = cPickle.load(open(validationDataFile, "rb"))

        # Get mRNA data
        rnaIds = sim_data.process.transcription.rnaData["id"]
        isMRna = sim_data.process.transcription.rnaData["isMRna"]
        mRnaIndexes = np.where(isMRna)[0]
        mRnaIds = np.array([rnaIds[x] for x in mRnaIndexes])

        time = []
        time_eachGen = []
        transcribedBool = []
        simulatedSynthProbs = []
        transcriptionEvents = []

        for gen, simDir in enumerate(allDir):
            simOutDir = os.path.join(simDir, "simOut")

            if gen < FIRST_N_GENS:
                time += TableReader(os.path.join(
                    simOutDir, "Main")).readColumn("time").tolist()
                time_eachGen.append(
                    TableReader(os.path.join(
                        simOutDir, "Main")).readColumn("time").tolist()[0])

            rnaSynthProb = TableReader(os.path.join(simOutDir, "RnaSynthProb"))
            simulatedSynthProb = np.mean(
                rnaSynthProb.readColumn("rnaSynthProb")[:, mRnaIndexes],
                axis=0)
            rnaSynthProb.close()
            simulatedSynthProbs.append(simulatedSynthProb)

            bulkMolecules = TableReader(
                os.path.join(simOutDir, "BulkMolecules"))
            moleculeIds = bulkMolecules.readAttribute("objectNames")
            mRnaIndexes_bulk = np.array(
                [moleculeIds.index(x) for x in mRnaIds])
            moleculeCounts = bulkMolecules.readColumn(
                "counts")[:, mRnaIndexes_bulk]
            bulkMolecules.close()
            moleculeCountsSumOverTime = moleculeCounts.sum(axis=0)
            mRnasTranscribed = np.array(
                [x != 0 for x in moleculeCountsSumOverTime])
            transcribedBool.append(mRnasTranscribed)

            rnapDataReader = TableReader(os.path.join(simOutDir, "RnapData"))
            rnaInitEvent = rnapDataReader.readColumn(
                "rnaInitEvent")[:, mRnaIndexes]
            rnapDataReader.close()

            if gen == 0:
                transcriptionEvents = (rnaInitEvent != 0)
            elif gen < FIRST_N_GENS:
                transcriptionEvents = np.vstack(
                    (transcriptionEvents, (rnaInitEvent != 0)))
            else:
                pass

        time = np.array(time)
        time_eachGen.append(time[-1])
        time_eachGen = np.array(time_eachGen)
        transcribedBool = np.array(transcribedBool)
        simulatedSynthProbs = np.array(simulatedSynthProbs)

        indexingOrder = np.argsort(np.mean(simulatedSynthProbs, axis=0))
        transcribedBoolOrdered = np.mean(transcribedBool,
                                         axis=0)[indexingOrder]
        transcriptionEventsOrdered = transcriptionEvents[:, indexingOrder]
        mRnaIdsOrdered = mRnaIds[indexingOrder]

        alwaysPresentIndexes = np.where(transcribedBoolOrdered == 1.)[0]
        neverPresentIndexes = np.where(transcribedBoolOrdered == 0.)[0]
        sometimesPresentIndexes = np.array([
            x for x in np.arange(len(transcribedBoolOrdered))
            if x not in alwaysPresentIndexes and x not in neverPresentIndexes
        ])

        plt.style.use('seaborn-deep')
        color_cycle = plt.rcParams['axes.prop_cycle'].by_key()['color']
        color_always = color_cycle[2]
        color_never = color_cycle[4]
        color_subgen = color_cycle[0]
        colors = np.repeat(color_subgen, len(transcribedBoolOrdered))
        colors[alwaysPresentIndexes] = color_always
        colors[neverPresentIndexes] = color_never
        always = transcribedBoolOrdered[alwaysPresentIndexes]
        never = transcribedBoolOrdered[neverPresentIndexes]
        sometimes = transcribedBoolOrdered[sometimesPresentIndexes]

        alwaysTranscriptionEvents = []
        for i in alwaysPresentIndexes:
            v = (time[transcriptionEventsOrdered[:, i]] / 3600.).tolist()
            if transcriptionEventsOrdered[:, i].sum() == 0:
                v = [-1]
            alwaysTranscriptionEvents.append(v)

        neverTranscriptionEvents = []
        for i in neverPresentIndexes:
            v = (time[transcriptionEventsOrdered[:, i]] / 3600.).tolist()
            if transcriptionEventsOrdered[:, i].sum() == 0:
                v = [-1]
            neverTranscriptionEvents.append(v)

        sometimesTranscriptionEvents = []
        for i in sometimesPresentIndexes:
            v = (time[transcriptionEventsOrdered[:, i]] / 3600.).tolist()
            if transcriptionEventsOrdered[:, i].sum() == 0:
                v = [-1]
            sometimesTranscriptionEvents.append(v)

        # Plot
        plt.figure(figsize=(11, 8))
        scatterAxis = plt.subplot2grid((2, 4), (0, 0), colspan=3, rowspan=2)
        histAxis = plt.subplot2grid((2, 4), (0, 3),
                                    colspan=1,
                                    rowspan=2,
                                    sharey=scatterAxis)

        scatterAxis.scatter(np.arange(len(transcribedBoolOrdered)),
                            transcribedBoolOrdered,
                            marker='o',
                            facecolors=colors,
                            edgecolors="none",
                            s=20)
        scatterAxis.set_xlim([0, len(transcribedBoolOrdered)])
        scatterAxis.set_ylim([0, 1])
        whitePadSparklineAxis(scatterAxis)

        N, bins, patches = histAxis.hist(transcribedBoolOrdered,
                                         bins=len(allDir) + 1,
                                         orientation='horizontal')
        for i in xrange(1, len(patches) - 1):
            plt.setp(patches[i], facecolor="none", edgecolor=color_subgen)
        plt.setp(patches[0], facecolor="none", edgecolor=color_never)
        plt.setp(patches[-1], facecolor="none", edgecolor=color_always)
        whitePadSparklineAxis(histAxis)
        histAxis.xaxis.tick_bottom()
        histXmin, histXmax = histAxis.get_xlim()

        scatterAxis.set_ylim([-.01, 1.01])
        scatterAxis.set_yticklabels([])
        scatterAxis.set_xticklabels([])
        histAxis.set_xscale("log")
        histAxis.set_xlim([10, 2000])
        histAxis.set_yticklabels([])
        histAxis.set_xticklabels([])

        plt.subplots_adjust(wspace=0.6,
                            hspace=0.4,
                            right=0.9,
                            bottom=0.1,
                            left=0.1,
                            top=0.9)
        exportFigure(plt, plotOutDir,
                     plotOutFileName + "_frequency_histogram__clean", metadata)

        if PLOT_GENES_OF_INTEREST:
            ## Identifying particular genes
            dcurId = "G7826_RNA[c]"
            clppId = "EG10158_RNA[c]"
            dcucId = "G6347_RNA[c]"
            dcurIndex = np.where(mRnaIdsOrdered == dcurId)[0]
            clppIndex = np.where(mRnaIdsOrdered == clppId)[0]
            dcucIndex = np.where(mRnaIdsOrdered == dcucId)[0]

            scatterAxis.scatter(dcurIndex,
                                transcribedBoolOrdered[dcurIndex],
                                facecolor="orange",
                                edgecolors="none")
            scatterAxis.scatter(clppIndex,
                                transcribedBoolOrdered[clppIndex],
                                facecolor="orange",
                                edgecolors="none")
            scatterAxis.scatter(dcucIndex,
                                transcribedBoolOrdered[dcucIndex],
                                facecolor="orange",
                                edgecolors="none")
            scatterAxis.text(dcurIndex + 500,
                             transcribedBoolOrdered[dcurIndex],
                             "dcuR",
                             fontsize=18)
            scatterAxis.text(clppIndex + 500,
                             transcribedBoolOrdered[clppIndex],
                             "clpP",
                             fontsize=18)
            scatterAxis.text(dcucIndex + 500,
                             transcribedBoolOrdered[dcucIndex],
                             "dcuC",
                             fontsize=18)
            exportFigure(
                plt, plotOutDir,
                plotOutFileName + "_frequency_histogram__clean__genes",
                metadata)

        plt.suptitle(
            "Frequency of observing at least 1 transcript per generation",
            fontsize=14)
        scatterAxis.set_xlabel(
            "Genes ordered by simulated synthesis probability", fontsize=12)
        histAxis.text(histXmax * 1.6,
                      0,
                      "%s genes\n(%0.1f%%)" %
                      (len(never), 100. *
                       (len(never) / float(len(transcribedBoolOrdered)))),
                      fontsize=14,
                      verticalalignment="center",
                      color=color_never)
        histAxis.text(histXmax * 1.6,
                      1,
                      "%s genes\n(%0.1f%%)" %
                      (len(always), 100. *
                       (len(always) / float(len(transcribedBoolOrdered)))),
                      fontsize=14,
                      verticalalignment="center",
                      color=color_always)
        histAxis.text(histXmax * 1.6,
                      0.5,
                      "%s genes\n(%0.1f%%)" %
                      (len(sometimes), 100. *
                       (len(sometimes) / float(len(transcribedBoolOrdered)))),
                      fontsize=14,
                      verticalalignment="center",
                      color=color_subgen)
        scatterAxis.set_yticklabels([0, 1])
        scatterAxis.set_xticklabels([0, len(transcribedBoolOrdered)])
        histAxis.set_xticks([10, 2000])
        histAxis.set_xticklabels([10, 2000])
        histAxis.set_yticklabels([0, 1])
        exportFigure(plt, plotOutDir, plotOutFileName + "_frequency_histogram",
                     metadata)
        plt.close("all")

        plt.figure(figsize=(16, 8))
        alwaysAxis = plt.subplot(2, 1, 1)
        sometimesAxis = plt.subplot(2, 1, 2, sharex=alwaysAxis)

        alwaysAxis.eventplot(alwaysTranscriptionEvents,
                             orientation="horizontal",
                             linewidths=2.,
                             linelengths=4.,
                             color=color_always)
        alwaysAxis.set_xlim([0, time[-1] / 3600.])
        alwaysAxis.set_ylim([-1, len(always)])
        alwaysAxis.set_xticks([])
        alwaysAxis.set_yticks([])
        alwaysAxis.tick_params(top="off")
        alwaysAxis.tick_params(bottom="off")
        alwaysAxis.tick_params(axis="x", labelbottom='off')

        sometimesAxis.eventplot(sometimesTranscriptionEvents,
                                orientation="horizontal",
                                linewidths=2.,
                                linelengths=4.,
                                color=color_subgen)
        sometimesAxis.set_xlim([0, time[-1] / 3600.])
        sometimesAxis.set_ylim([-1, len(sometimes)])
        sometimesAxis.set_xticks([0, time[-1] / 3600.])
        sometimesAxis.set_yticks([])
        sometimesAxis.tick_params(top="off")
        sometimesAxis.tick_params(which='both', direction='out', labelsize=12)
        time_eachGen = np.array(time_eachGen)
        sometimesAxis.set_xticks(time_eachGen / 3600.)
        sometimesAxis.set_xticklabels([])

        plt.subplots_adjust(wspace=0,
                            hspace=0,
                            right=0.9,
                            bottom=0.1,
                            left=0.1,
                            top=0.9)
        exportFigure(plt, plotOutDir, plotOutFileName + "_eventplot__clean",
                     metadata)

        plt.suptitle("Transcription initiation events", fontsize=14)
        alwaysAxis.set_ylabel("Freq. = 1", fontsize=14)
        sometimesAxis.set_ylabel("0 < Freq. < 1", fontsize=14)
        sometimesAxis.set_xlabel("Time (gens)", fontsize=14)
        sometimesAxis.set_xticklabels(np.arange(FIRST_N_GENS + 1))
        exportFigure(plt, plotOutDir, plotOutFileName + "_eventplot", metadata)
        plt.close("all")

        # Essential genes figure
        nRed = len(never)
        nGreen = len(sometimes)
        nBlue = len(always)

        plotRed = 0
        plotGreen = 0
        plotBlue = 0

        essentialGenes_rna = validation_data.essentialGenes.essentialRnas
        for g in essentialGenes_rna:
            i = np.where(mRnaIdsOrdered == str(g))[0][0]
            f = transcribedBoolOrdered[i]

            if f == 0.0:
                plotRed += 1
            elif f == 1.0:
                plotBlue += 1
            else:
                plotGreen += 1
        xloc = np.arange(3)
        width = 0.8

        plt.figure()
        ax = plt.subplot(1, 1, 1)
        ax.bar(xloc + width, [
            plotRed / float(len(essentialGenes_rna)),
            plotGreen / float(len(essentialGenes_rna)),
            plotBlue / float(len(essentialGenes_rna))
        ],
               width,
               color=[color_never, color_subgen, color_always],
               edgecolor="none")
        whitePadSparklineAxis(ax)
        ax.spines["left"].set_position(("outward", 0))
        ax.set_yticks([0.0, 0.2, 0.4, 0.6, 0.8])
        ax.set_yticklabels(["0%", "20%", "40%", "60%", "80%"])
        ax.set_ylabel("Percentage of essential genes")
        ax.set_xticks(xloc + 1.5 * width)
        ax.set_xticklabels([
            "%s / %s" % (plotRed, len(essentialGenes_rna)),
            "%s / %s" % (plotGreen, len(essentialGenes_rna)),
            "%s / %s" % (plotBlue, len(essentialGenes_rna))
        ])
        ax.set_xlabel("Total number of essential genes: %s" %
                      len(essentialGenes_rna))
        plt.subplots_adjust(right=0.9, bottom=0.15, left=0.2, top=0.9)
        exportFigure(plt, plotOutDir, plotOutFileName + "_essential_genes",
                     metadata)

        ax.set_yticklabels([])
        ax.set_ylabel("")
        remove_xaxis(ax)
        exportFigure(plt, plotOutDir,
                     plotOutFileName + "_essential_genes__clean", metadata)

        if PLOT_DENOMINATOR_N_EACH_FREQ_GROUP:
            plt.figure()
            ax = plt.subplot(1, 1, 1)
            ax.bar(xloc + width, [
                plotRed / float(nRed), plotGreen / float(nGreen),
                plotBlue / float(nBlue)
            ],
                   width,
                   color=[color_never, color_subgen, color_always],
                   edgecolor="none")
            whitePadSparklineAxis(ax)
            ax.spines["left"].set_position(("outward", 0))
            ax.set_yticks([0.0, 0.1, 0.2])
            ax.set_yticklabels(["0%", "10%", "20%"])
            ax.set_ylabel("Percentage of genes that are essential genes")
            ax.set_xticks(xloc + 1.5 * width)
            ax.set_xticklabels([
                "%s / %s" % (plotRed, nRed),
                "%s / %s" % (plotGreen, nGreen),
                "%s / %s" % (plotBlue, nBlue)
            ])
            plt.subplots_adjust(right=0.9, bottom=0.1, left=0.2, top=0.9)
            exportFigure(plt, plotOutDir,
                         plotOutFileName + "_essential_genes_v2", metadata)
        plt.close()

        # Gene annotation/antibiotics figure
        geneFunctions = validation_data.geneFunctions.geneFunctions
        unknown = {"r": 0, "g": 0, "b": 0}
        resistance = {"r": 0, "g": 0, "b": 0}
        for frameID, function_ in geneFunctions.iteritems():
            if function_ in [
                    "Unknown function", "Unclear/under-characterized"
            ]:
                i = np.where([frameID in x for x in mRnaIdsOrdered])[0][0]
                f = transcribedBoolOrdered[i]
                if f == 0.0:
                    unknown["r"] += 1
                elif f == 1.0:
                    unknown["b"] += 1
                else:
                    unknown["g"] += 1

            elif function_ in ["Antibiotic resistance", "Toxin/antitoxin"]:
                i = np.where([frameID in x for x in mRnaIdsOrdered])[0][0]
                f = transcribedBoolOrdered[i]
                if f == 0.0:
                    resistance["r"] += 1
                elif f == 1.0:
                    resistance["b"] += 1
                else:
                    resistance["g"] += 1

        nUnknown = np.sum([unknown[x] for x in ["r", "g", "b"]])
        plt.figure()
        ax = plt.subplot(1, 1, 1)
        ax.bar(xloc + width, [
            unknown["r"] / float(nUnknown), unknown["g"] / float(nUnknown),
            unknown["b"] / float(nUnknown)
        ],
               width,
               color=[color_never, color_subgen, color_always],
               edgecolor="none")
        whitePadSparklineAxis(ax)
        ax.spines["left"].set_position(("outward", 0))
        ax.set_yticks([0.0, 0.2, 0.4, 0.6, 0.8])
        ax.set_yticklabels(["0%", "20%", "40%", "60%", "80%"])
        ax.set_ylabel("Percentage of poorly understood / annotated genes")
        ax.set_xticks(xloc + 1.5 * width)
        ax.set_xticklabels([
            "%s / %s" % (unknown["r"], nUnknown),
            "%s / %s" % (unknown["g"], nUnknown),
            "%s / %s" % (unknown["b"], nUnknown)
        ])
        ax.set_xlabel(
            "Total number of poorly understood / annotated genes: %s" %
            nUnknown)
        plt.subplots_adjust(right=0.9, bottom=0.15, left=0.2, top=0.9)
        exportFigure(plt, plotOutDir, plotOutFileName + "_unannotated",
                     metadata)

        ax.set_yticklabels([])
        ax.set_ylabel("")
        remove_xaxis(ax)
        exportFigure(plt, plotOutDir, plotOutFileName + "_unannotated__clean",
                     metadata)

        if PLOT_DENOMINATOR_N_EACH_FREQ_GROUP:
            plt.figure()
            ax = plt.subplot(1, 1, 1)
            ax.bar(xloc + width, [
                unknown["r"] / float(nRed), unknown["g"] / float(nGreen),
                unknown["b"] / float(nBlue)
            ],
                   width,
                   color=[color_never, color_subgen, color_always],
                   edgecolor="none")
            whitePadSparklineAxis(ax)
            ax.spines["left"].set_position(("outward", 0))
            ax.set_yticks([0.0, 0.2, 0.4, 0.6])
            ax.set_yticklabels(["0%", "20%", "40%", "60%"])
            ax.set_ylabel(
                "Percentage of genes that are poorly understood / annotated")
            ax.set_xticks(xloc + 1.5 * width)
            ax.set_xticklabels([
                "%s / %s" % (unknown["r"], nRed),
                "%s / %s" % (unknown["g"], nGreen),
                "%s / %s" % (unknown["b"], nBlue)
            ])
            plt.subplots_adjust(right=0.9, bottom=0.1, left=0.2, top=0.9)
            exportFigure(plt, plotOutDir, plotOutFileName + "_v2", metadata)
        plt.close()

        nResistance = float(np.sum([resistance[x] for x in ["r", "g", "b"]]))
        plt.figure()
        ax = plt.subplot(1, 1, 1)
        ax.bar(xloc + width, [
            resistance["r"] / nResistance, resistance["g"] / nResistance,
            resistance["b"] / nResistance
        ],
               width,
               color=[color_never, color_subgen, color_always],
               edgecolor="none")
        whitePadSparklineAxis(ax)
        ax.spines["left"].set_position(("outward", 0))
        ax.set_yticks([0.0, 0.2, 0.4, 0.6, 0.8])
        ax.set_yticklabels(["0%", "20%", "40%", "60%", "80%"])
        ax.set_ylabel("Percentage of antibiotic related genes")
        ax.set_xticks(xloc + 1.5 * width)
        ax.set_xticklabels([
            "%s / %s" % (resistance["r"], int(nResistance)),
            "%s / %s" % (resistance["g"], int(nResistance)),
            "%s / %s" % (resistance["b"], int(nResistance))
        ])
        ax.set_xlabel("Total number of antibiotic related genes: %s" %
                      int(nResistance))
        plt.subplots_adjust(right=0.9, bottom=0.15, left=0.2, top=0.9)
        exportFigure(plt, plotOutDir, plotOutFileName + "_antibiotic",
                     metadata)

        ax.set_yticklabels([])
        ax.set_ylabel("")
        remove_xaxis(ax)
        exportFigure(plt, plotOutDir, plotOutFileName + "_antibiotic__clean",
                     metadata)

        if PLOT_DENOMINATOR_N_EACH_FREQ_GROUP:
            plt.figure()
            ax = plt.subplot(1, 1, 1)
            ax.bar(xloc + width, [
                resistance["r"] / float(nRed), resistance["g"] / float(nGreen),
                resistance["b"] / float(nBlue)
            ],
                   width,
                   color=[color_never, color_subgen, color_always],
                   edgecolor="none")
            whitePadSparklineAxis(ax)
            ax.spines["left"].set_position(("outward", 0))
            ax.set_yticks([0.0, 0.025])
            ax.set_yticklabels(["0%", "2.5%"])
            ax.set_ylabel("Percentage of genes that are antibiotic related")
            ax.set_xticks(xloc + 1.5 * width)
            ax.set_xticklabels([
                "%s / %s" % (resistance["r"], nRed),
                "%s / %s" % (resistance["g"], nGreen),
                "%s / %s" % (resistance["b"], nBlue)
            ])
            plt.subplots_adjust(right=0.9, bottom=0.1, left=0.2, top=0.9)
            exportFigure(plt, plotOutDir, plotOutFileName + "_antibiotic_v2",
                         metadata)
        plt.close()
    def do_plot(self, inputDir, plotOutDir, plotOutFileName, simDataFile,
                validationDataFile, metadata):
        if not os.path.isdir(inputDir):
            raise Exception, "inputDir does not currently exist as a directory"
        if not os.path.exists(plotOutDir):
            os.mkdir(plotOutDir)

        # Get cells
        ap = AnalysisPaths(inputDir, variant_plot=True)
        if ap.n_variant != 9:
            print "This plot expects all variants of meneParams"
            return

        # Get constants from wildtype variant
        sim_data = cPickle.load(open(ap.get_variant_kb(4),
                                     "rb"))  # 4 is the wildtype variant
        cellDensity = sim_data.constants.cellDensity
        nAvogadro = sim_data.constants.nAvogadro

        # Initialize variables
        enzymeId = "MENE-CPLX[c]"
        endProductIds = ["REDUCED-MENAQUINONE[c]", "CPD-12115[c]"]
        TARGET_CONC = len(endProductIds) * TARGET_CONC_SINGLE

        # Check for cache
        cacheFileName = "%s.pickle" % plotOutFileName
        CACHE_EXISTS = False
        if os.path.exists(os.path.join(plotOutDir, cacheFileName)):
            CACHE_EXISTS = True

        if not CACHE_EXISTS:
            # Investigate each variant
            meneDepletion = np.zeros([ap.n_seed, ap.n_variant])
            endProductDepletion = np.zeros([ap.n_seed, ap.n_variant])

            for variant in xrange(ap.n_variant):
                for seed in xrange(ap.n_seed):
                    cells = ap.get_cells(variant=[variant], seed=[seed])
                    timeMeneDepleted = []  # seconds
                    timeEndProdDepleted = []  # seconds

                    for i, simDir in enumerate(cells):
                        simOutDir = os.path.join(simDir, "simOut")

                        # Get molecule counts
                        bulkMolecules = TableReader(
                            os.path.join(simOutDir, "BulkMolecules"))
                        moleculeIds = bulkMolecules.readAttribute(
                            "objectNames")
                        meneIndex = moleculeIds.index(enzymeId)
                        meneCounts = bulkMolecules.readColumn(
                            "counts")[:, meneIndex]
                        endProductIndices = [
                            moleculeIds.index(x) for x in endProductIds
                        ]
                        endProductCounts = bulkMolecules.readColumn(
                            "counts")[:, endProductIndices]
                        bulkMolecules.close()

                        # Compute time with zero counts of tetramer (MENE-CPLX)
                        timeStepSec = TableReader(
                            os.path.join(simOutDir,
                                         "Main")).readColumn("timeStepSec")
                        meneDepletionIndices = np.where(meneCounts == 0)[0]
                        timeMeneDepleted.append(
                            timeStepSec[meneDepletionIndices].sum())

                        # Compute time with end products under the target concentration
                        mass = TableReader(os.path.join(
                            simOutDir,
                            "Mass")).readColumn("cellMass") * units.fg
                        volume = mass / cellDensity
                        endProductConcentrations = np.sum([
                            endProductCounts[:, col] / nAvogadro / volume
                            for col in xrange(endProductCounts.shape[1])
                        ],
                                                          axis=0)
                        endProductDepletionIndices = np.where(
                            endProductConcentrations < (
                                (1 - THRESHOLD) * TARGET_CONC))[0]
                        timeEndProdDepleted.append(
                            timeStepSec[endProductDepletionIndices].sum())

                    # Record MENE-CPLX depletion
                    totalTime = TableReader(os.path.join(
                        simOutDir,
                        "Main")).readColumn("time")[-1] + timeStepSec[-1]
                    fractionMeneDepleted = np.sum(timeMeneDepleted) / totalTime
                    meneDepletion[seed, variant] = fractionMeneDepleted

                    # Record end product depletion
                    fractionEndProdDepleted = np.sum(
                        timeEndProdDepleted) / totalTime
                    endProductDepletion[seed,
                                        variant] = fractionEndProdDepleted

            # Cache
            D = {"mene": meneDepletion, "endProduct": endProductDepletion}
            cPickle.dump(D, open(os.path.join(plotOutDir, cacheFileName),
                                 "wb"))

        else:
            D = cPickle.load(
                open(os.path.join(plotOutDir, cacheFileName), "rb"))
            meneDepletion = D["mene"]
            endProductDepletion = D["endProduct"]

        # Compute average and standard deviations
        meneDepletion_avg = np.average(meneDepletion, axis=0)
        meneDepletion_std = np.std(meneDepletion, axis=0)
        endProductDepletion_avg = np.average(endProductDepletion, axis=0)
        endProductDepletion_std = np.std(endProductDepletion, axis=0)

        # Plot
        fig, axesList = plt.subplots(2, 1, figsize=(8, 8))
        ax1, ax2 = axesList
        xvals = np.arange(ap.n_variant)
        fig.suptitle("Sensitivity Analysis: menE depletion")

        for ax, avg, std in zip(axesList,
                                [meneDepletion_avg, endProductDepletion_avg],
                                [meneDepletion_std, endProductDepletion_std]):
            ax.scatter(xvals,
                       avg,
                       edgecolor="none",
                       clip_on=False,
                       s=MARKERSIZE)
            ax.errorbar(xvals,
                        avg,
                        yerr=std,
                        color="b",
                        linewidth=1,
                        clip_on=False,
                        fmt="o",
                        capsize=4,
                        capthick=1,
                        markeredgecolor="none")

        ax1.set_title("MenE tetramer depletion", fontsize=FONTSIZE)
        ax2.set_title("Menaquinone products depletion", fontsize=FONTSIZE)
        xlabels = [
            "1/10 x", "1/8 x", "1/4 x", "1/2 x", "1 x", "2 x", "4 x", "8 x",
            "10 x"
        ]
        title_tags = [
            "counts = 0",
            "<%s percent of wildtype" % (THRESHOLD * 100)
        ]
        for i, ax in enumerate([ax1, ax2]):
            ax.set_ylabel("Fraction of Time\n%s" % title_tags[i],
                          fontsize=FONTSIZE)
            ax.set_xlabel("Factor of increase of menE synthesis probability",
                          fontsize=FONTSIZE)
            ax.set_xlim([-0.25, 8.25])
            whitePadSparklineAxis(ax)
            ax.set_xticks(xvals)
            ax.set_xticklabels(xlabels)
            ax.set_yticks([0, 1])

        plt.subplots_adjust(hspace=1, wspace=1, top=0.9, bottom=0.1)
        exportFigure(plt, plotOutDir, plotOutFileName, metadata)
        plt.close("all")

        # Plot clean versions for figure
        FIRST = True
        for avg, std, filename in zip(
            [meneDepletion_avg, endProductDepletion_avg],
            [meneDepletion_std, endProductDepletion_std],
            ["mene", "menaquinone"]):
            fig, ax = plt.subplots(1, 1, figsize=(10, 3))
            ax.scatter(xvals,
                       avg,
                       edgecolor="none",
                       clip_on=False,
                       s=MARKERSIZE)
            ax.errorbar(xvals,
                        avg,
                        yerr=std,
                        color="b",
                        linewidth=1,
                        clip_on=False,
                        fmt="o",
                        capsize=4,
                        capthick=1,
                        markeredgecolor="none")
            ax.set_xlim([-0.25, 8.25])
            if FIRST:
                FIRST = False
                whitePadSparklineAxis(ax, False)
            else:
                whitePadSparklineAxis(ax)
            ax.set_xticks(xvals)
            ax.set_xticklabels([])
            ax.set_yticks([0, 1])
            ax.set_yticklabels([])
            exportFigure(plt, plotOutDir, plotOutFileName + "_%s" % filename,
                         metadata)
            plt.close("all")
Beispiel #14
0
    def do_plot(self, seedOutDir, plotOutDir, plotOutFileName, simDataFile,
                validationDataFile, metadata):
        if not os.path.isdir(seedOutDir):
            raise Exception, "seedOutDir does not currently exist as a directory"

        if not os.path.exists(plotOutDir):
            os.mkdir(plotOutDir)

        # Get all cells
        ap = AnalysisPaths(seedOutDir, multi_gen_plot=True)
        allDir = ap.get_cells()

        sim_data = cPickle.load(open(simDataFile, "rb"))
        cellDensity = sim_data.constants.cellDensity
        rnaIds = sim_data.process.transcription.rnaData["id"]

        enzyme_rna_transcription_index = np.where(
            rnaIds == ENZYME_RNA_ID)[0][0]

        simOutDir = os.path.join(allDir[0], "simOut")
        bulk_molecules_reader = TableReader(
            os.path.join(simOutDir, "BulkMolecules"))
        fba_results_reader = TableReader(os.path.join(simOutDir, "FBAResults"))

        moleculeIDs = bulk_molecules_reader.readAttribute("objectNames")
        reactionIDs = np.array(fba_results_reader.readAttribute("reactionIDs"))

        enzyme_complex_index = moleculeIDs.index(ENZYME_COMPLEX_ID)
        enzyme_monomer_index = moleculeIDs.index(ENZYME_MONOMER_ID)
        enzyme_rna_counts_index = moleculeIDs.index(ENZYME_RNA_ID)
        metabolite_indexes = [moleculeIDs.index(x) for x in METABOLITE_IDS]
        reaction_index = np.where(reactionIDs == ENZYME_REACTION_ID)[0][0]

        # Initialize arrays
        time = []
        enzyme_fluxes = []
        enzyme_complex_counts = []
        enzyme_monomer_counts = []
        enzyme_rna_counts = []
        enzyme_rna_init_events = []
        metabolite_counts = np.empty((0, len(METABOLITE_IDS)))

        cellMass = []
        dryMass = []
        timeStepSec = []
        generationTicks = [0.]

        n_transcription_init_events_per_gen = []
        enzyme_complex_avg_counts = []

        for simDir in allDir:
            simOutDir = os.path.join(simDir, "simOut")

            main_reader = TableReader(os.path.join(simOutDir, "Main"))
            mass_reader = TableReader(os.path.join(simOutDir, "Mass"))
            bulk_molecules_reader = TableReader(
                os.path.join(simOutDir, "BulkMolecules"))
            fba_results_reader = TableReader(
                os.path.join(simOutDir, "FBAResults"))
            rnap_data_reader = TableReader(os.path.join(simOutDir, "RnapData"))

            time.extend(main_reader.readColumn("time").tolist())
            generationTicks.append(time[-1])

            timeStepSec.extend(main_reader.readColumn("timeStepSec").tolist())
            cellMass.extend(mass_reader.readColumn("cellMass").tolist())
            dryMass.extend(mass_reader.readColumn("dryMass").tolist())

            molecule_counts = bulk_molecules_reader.readColumn("counts")
            enzyme_monomer_counts.extend(
                molecule_counts[:, enzyme_monomer_index].tolist())
            enzyme_rna_counts.extend(
                molecule_counts[:, enzyme_rna_counts_index].tolist())

            enzyme_complex_counts_this_gen = molecule_counts[:,
                                                             enzyme_complex_index]
            enzyme_complex_counts.extend(
                enzyme_complex_counts_this_gen.tolist())
            enzyme_complex_avg_counts.append(
                np.mean(enzyme_complex_counts_this_gen))

            metabolite_counts = np.vstack(
                (metabolite_counts, molecule_counts[:, metabolite_indexes]))

            reactionFluxes = np.array(
                fba_results_reader.readColumn("reactionFluxes"))
            enzyme_fluxes.extend(reactionFluxes[:, reaction_index].tolist())

            rna_init_events_this_gen = rnap_data_reader.readColumn(
                "rnaInitEvent")[:, enzyme_rna_transcription_index]

            enzyme_rna_init_events.extend(rna_init_events_this_gen.tolist())
            n_transcription_init_events_per_gen.append(
                np.sum(rna_init_events_this_gen))

        coefficient = (units.fg * np.array(dryMass)) / (
            units.fg * np.array(cellMass)) * (timeStepSec *
                                              units.s) * cellDensity
        enzyme_fluxes = (((COUNTS_UNITS / VOLUME_UNITS) * enzyme_fluxes) /
                         coefficient).asNumber(units.mmol / units.g / units.h)

        # Convert time to hours
        time = np.array(time)
        time_hours = time / 3600.

        # Plot
        plt.figure(figsize=(11, 8.5))
        plt.suptitle("O-succinylbenzoate-CoA ligase downstream behaviors",
                     fontsize=FONTSIZE)

        # Define axes
        rnaInitAxis = plt.subplot(6, 1, 1)
        rnaAxis = plt.subplot(6, 1, 2, sharex=rnaInitAxis)
        monomerAxis = plt.subplot(6, 1, 3, sharex=rnaInitAxis)
        complexAxis = plt.subplot(6, 1, 4, sharex=rnaInitAxis)
        fluxAxis = plt.subplot(6, 1, 5, sharex=rnaInitAxis)
        metAxis = plt.subplot(6, 1, 6)

        # Plot transcription initiation events
        rnaInitAxis.plot(time_hours, enzyme_rna_init_events, c="b")
        rnaInitAxis.set_ylabel(r"$menE$" + "\n transcription\nevents",
                               fontsize=FONTSIZE,
                               rotation=0)
        rnaInitAxis.yaxis.set_label_coords(-.1, 0.25)
        rnaInitAxis.set_xlim([time_hours[0], time_hours[-1]])
        whitePadSparklineAxis(rnaInitAxis, xAxis=False)
        rnaInitAxis.set_yticks([0, 1])

        rnaAxis.plot(time_hours, enzyme_rna_counts, c="b")
        rnaAxis.set_ylabel("menE mRNA\ncounts", fontsize=FONTSIZE, rotation=0)
        rnaAxis.yaxis.set_label_coords(-.1, 0.25)
        whitePadSparklineAxis(rnaAxis, xAxis=False)
        rnaAxis.set_yticks([0, max(enzyme_rna_counts)])

        monomerAxis.plot(time_hours, enzyme_monomer_counts, c="b")
        monomerAxis.set_ylabel("MenE monomer\ncounts",
                               fontsize=FONTSIZE,
                               rotation=0)
        monomerAxis.yaxis.set_label_coords(-.1, 0.25)
        whitePadSparklineAxis(monomerAxis, xAxis=False)
        monomerAxis.set_yticks([0, 4, max(enzyme_monomer_counts)])

        complexAxis.plot(time_hours, enzyme_complex_counts, c="b")
        complexAxis.set_ylabel("MenE tetramer\ncounts",
                               fontsize=FONTSIZE,
                               rotation=0)
        complexAxis.yaxis.set_label_coords(-.1, 0.25)
        whitePadSparklineAxis(complexAxis, xAxis=False)
        complexAxis.set_yticks([0, max(enzyme_complex_counts)])

        fluxAxis.plot(time_hours, enzyme_fluxes, c="b")
        fluxAxis.set_ylabel("SUCBZL flux\n(mmol/gDCW/hour)",
                            fontsize=FONTSIZE,
                            rotation=0)
        fluxAxis.yaxis.set_label_coords(-.1, 0.25)
        whitePadSparklineAxis(fluxAxis, xAxis=False)
        fluxAxis.set_yticks([min(enzyme_fluxes), max(enzyme_fluxes)])

        metAxis.plot(time_hours, np.sum(metabolite_counts, axis=1), c="b")
        metAxis.set_ylabel("End product\ncounts",
                           fontsize=FONTSIZE,
                           rotation=0)
        metAxis.yaxis.set_label_coords(-.1, 0.25)
        metAxis.set_xlabel("Time (hour)\ntickmarks at each new generation",
                           fontsize=FONTSIZE)
        metAxis.set_ylim([metAxis.get_ylim()[0] * 0.2, metAxis.get_ylim()[1]])
        metAxis.set_xlim([time_hours[0], time_hours[-1]])
        whitePadSparklineAxis(metAxis)
        metAxis.set_yticklabels(
            ["%0.1e" % metAxis.get_ylim()[0],
             "%0.1e" % metAxis.get_ylim()[1]])
        metAxis.set_xticks(np.array(generationTicks) / 3600.)
        xticklabels = np.repeat("     ", len(generationTicks))
        xticklabels[0] = "0"
        xticklabels[-1] = "%0.2f" % (time_hours[-1])
        metAxis.set_xticklabels(xticklabels)

        noComplexIndexes = np.where(np.array(enzyme_complex_counts) == 0)[0]
        patchStart = []
        patchEnd = []
        if len(noComplexIndexes):
            prev = noComplexIndexes[0]
            patchStart.append(prev)
            for i in noComplexIndexes:
                if np.abs(i - prev) > 1:
                    patchStart.append(i)
                    patchEnd.append(prev)
                prev = i
            patchEnd.append(prev)

        axesList = [
            rnaInitAxis, rnaAxis, monomerAxis, complexAxis, fluxAxis, metAxis
        ]
        for axis in axesList:
            axis.tick_params(labelsize=LABELSIZE)
            for i in xrange(len(patchStart)):
                width = time_hours[patchEnd[i]] - time_hours[patchStart[i]]
                if width <= 0.1:
                    continue

                height = axis.get_ylim()[1] - axis.get_ylim()[0]
                axis.add_patch(
                    patches.Rectangle(
                        (time_hours[patchStart[i]], axis.get_ylim()[0]),
                        width,
                        height,
                        alpha=0.25,
                        color="gray",
                        linewidth=0.))

        plt.subplots_adjust(hspace=0.5,
                            right=0.9,
                            bottom=0.1,
                            left=0.15,
                            top=0.9)
        exportFigure(plt, plotOutDir, plotOutFileName, metadata)

        # Get clean version of plot
        for a in axesList:
            clearLabels(a)

        plt.suptitle("")
        metAxis.set_xticklabels([])
        metAxis.set_xlabel("")
        exportFigure(plt, plotOutDir, plotOutFileName + "__clean", "")
        plt.close("all")
    def do_plot(self, variantDir, plotOutDir, plotOutFileName, simDataFile,
                validationDataFile, metadata):
        if not os.path.isdir(variantDir):
            raise Exception, "variantDir does not currently exist as a directory"

        if not os.path.exists(plotOutDir):
            os.mkdir(plotOutDir)

        # Get all cells
        ap = AnalysisPaths(variantDir, cohort_plot=True)
        allDir = ap.get_cells()

        validation_data = cPickle.load(open(validationDataFile, "rb"))
        toyaReactions = validation_data.reactionFlux.toya2010fluxes[
            "reactionID"]
        toyaFluxes = validation_data.reactionFlux.toya2010fluxes[
            "reactionFlux"]
        toyaStdev = validation_data.reactionFlux.toya2010fluxes[
            "reactionFluxStdev"]
        toyaFluxesDict = dict(zip(toyaReactions, toyaFluxes))
        toyaStdevDict = dict(zip(toyaReactions, toyaStdev))

        sim_data = cPickle.load(open(simDataFile))
        cellDensity = sim_data.constants.cellDensity

        modelFluxes = {}
        toyaOrder = []
        for rxn in toyaReactions:
            modelFluxes[rxn] = []
            toyaOrder.append(rxn)

        for simDir in allDir:
            simOutDir = os.path.join(simDir, "simOut")

            try:
                massListener = TableReader(os.path.join(simOutDir, "Mass"))
                cellMass = massListener.readColumn("cellMass")
                dryMass = massListener.readColumn("dryMass")
                massListener.close()
            except Exception as e:
                print(e)
                continue

            # skip if no data
            if cellMass.shape is ():
                continue

            coefficient = dryMass / cellMass * cellDensity.asNumber(
                MASS_UNITS / VOLUME_UNITS)

            fbaResults = TableReader(os.path.join(simOutDir, "FBAResults"))
            reactionIDs = np.array(fbaResults.readAttribute("reactionIDs"))
            reactionFluxes = (COUNTS_UNITS / MASS_UNITS / TIME_UNITS) * (
                fbaResults.readColumn("reactionFluxes").T / coefficient).T
            fbaResults.close()

            for toyaReaction in toyaReactions:
                fluxTimeCourse = []

                for rxn in reactionIDs:
                    if re.findall(toyaReaction, rxn):
                        reverse = 1
                        if re.findall("(reverse)", rxn):
                            reverse = -1

                        if len(fluxTimeCourse):
                            fluxTimeCourse += reverse * reactionFluxes[:,
                                                                       np.
                                                                       where(
                                                                           reactionIDs
                                                                           ==
                                                                           rxn
                                                                       )]
                        else:
                            fluxTimeCourse = reverse * reactionFluxes[:,
                                                                      np.where(
                                                                          reactionIDs
                                                                          ==
                                                                          rxn)]

                if len(fluxTimeCourse):
                    modelFluxes[toyaReaction].append(
                        np.mean(fluxTimeCourse).asNumber(units.mmol / units.g /
                                                         units.h))

        toyaVsReactionAve = []
        rxn_order = []
        for rxn, toyaFlux in toyaFluxesDict.iteritems():
            rxn_order.append(rxn)
            if rxn in modelFluxes:
                toyaVsReactionAve.append(
                    (np.mean(modelFluxes[rxn]),
                     toyaFlux.asNumber(units.mmol / units.g / units.h),
                     np.std(modelFluxes[rxn]), toyaStdevDict[rxn].asNumber(
                         units.mmol / units.g / units.h)))

        outlier_indicies = np.zeros(len(toyaReactions), bool)
        outlier_indicies[rxn_order.index(
            'SUCCINATE-DEHYDROGENASE-UBIQUINONE-RXN-SUC/UBIQUINONE-8//FUM/CPD-9956.31.'
        )] = True
        outlier_indicies[rxn_order.index('ISOCITDEH-RXN')] = True

        toyaVsReactionAve = np.array(toyaVsReactionAve)
        rWithAll = pearsonr(toyaVsReactionAve[:, 0], toyaVsReactionAve[:, 1])
        rWithoutOutliers = pearsonr(toyaVsReactionAve[~outlier_indicies, 0],
                                    toyaVsReactionAve[~outlier_indicies, 1])

        plt.figure(figsize=(3.5, 3.5))
        ax = plt.axes()
        plt.title(
            "Central Carbon Metabolism Flux, Pearson R = %.4f, p = %s\n(%.4f, %s without outliers)"
            % (rWithAll[0], rWithAll[1], rWithoutOutliers[0],
               rWithoutOutliers[1]),
            fontsize=6)
        plt.errorbar(toyaVsReactionAve[:, 1],
                     toyaVsReactionAve[:, 0],
                     xerr=toyaVsReactionAve[:, 3],
                     yerr=toyaVsReactionAve[:, 2],
                     fmt="none",
                     ecolor="k",
                     alpha=0.5,
                     linewidth=0.5)
        ylim = plt.ylim()
        plt.plot([ylim[0], ylim[1]], [ylim[0], ylim[1]], color="k")
        plt.plot(toyaVsReactionAve[~outlier_indicies, 1],
                 toyaVsReactionAve[~outlier_indicies, 0],
                 "ob",
                 markeredgewidth=0.1,
                 alpha=0.9)
        plt.plot(toyaVsReactionAve[outlier_indicies, 1],
                 toyaVsReactionAve[outlier_indicies, 0],
                 "o",
                 color='#ed713a',
                 markeredgewidth=0.1,
                 alpha=0.9)
        plt.xlabel("Toya 2010 Reaction Flux [mmol/g/hr]")
        plt.ylabel("Mean WCM Reaction Flux [mmol/g/hr]")
        ax = plt.axes()
        whitePadSparklineAxis(ax)

        xlim = [-20, 30]
        ylim = [-20, 70]
        ax.set_xlim(xlim)
        ax.set_ylim(ylim)
        ax.set_xticks(range(int(xlim[0]), int(xlim[1]) + 1, 10))
        ax.set_yticks(range(int(ylim[0]), int(ylim[1]) + 1, 10))

        exportFigure(plt, plotOutDir, plotOutFileName, metadata)

        ax.set_xlabel("")
        ax.set_ylabel("")
        ax.set_title("")
        ax.set_xticklabels([])
        ax.set_yticklabels([])

        exportFigure(plt, plotOutDir, plotOutFileName + "_stripped", metadata)
        plt.close("all")
	def do_plot(self, inputDir, plotOutDir, plotOutFileName, simDataFile, validationDataFile, metadata):
		if not os.path.isdir(inputDir):
			raise Exception, "inputDir does not currently exist as a directory"
		if not os.path.exists(plotOutDir):
			os.mkdir(plotOutDir)

		# Get cells
		ap = AnalysisPaths(inputDir, variant_plot = True)
		if ap.n_variant != len(FACTORS):
			print("This plot expects all variants of subgen_expression")
			return

		# Get constants from wildtype variant
		sim_data = cPickle.load(open(ap.get_variant_kb(4), "rb")) # 4 is the wildtype variant
		cellDensity = sim_data.constants.cellDensity
		nAvogadro = sim_data.constants.nAvogadro
		metabolite_target = sim_data.process.metabolism.concDict[METABOLITE_ID]
		metabolite_threshold = (THRESHOLD * metabolite_target).asNumber(CONC_UNITS)

		# Investigate each variant
		enzyme_depletion = np.zeros([ap.n_seed, ap.n_variant])
		metabolite_depletion = np.zeros([ap.n_seed, ap.n_variant])

		for variant in xrange(ap.n_variant):
			for seed in xrange(ap.n_seed):
				cells = ap.get_cells(variant=[variant], seed=[seed])
				time_enzyme_depleted = []  # seconds
				time_metabolite_depleted = []  # seconds

				for i, simDir in enumerate(cells):
					simOutDir = os.path.join(simDir, "simOut")

					main_reader = TableReader(os.path.join(simOutDir, "Main"))
					mass_reader = TableReader(os.path.join(simOutDir, "Mass"))

					# Get molecule counts
					(enzyme_counts, metabolite_counts) = read_bulk_molecule_counts(simOutDir, (ENZYME_IDS, [METABOLITE_ID]))

					# Compute time with zero counts of enzyme
					time_step_sec = main_reader.readColumn("timeStepSec")
					time_enzyme_depleted.append(time_step_sec[np.sum(enzyme_counts, axis=1) == 0].sum())

					# Compute time with end products under the target concentration
					mass = units.fg * mass_reader.readColumn("cellMass")
					volume = mass / cellDensity
					metabolite_conc = (1 / nAvogadro / volume * metabolite_counts).asNumber(CONC_UNITS)
					time_metabolite_depleted.append(time_step_sec[metabolite_conc < metabolite_threshold].sum())

				# Record MENE-CPLX depletion
				total_time = main_reader.readColumn("time")[-1] + time_step_sec[-1]
				fraction_enzyme_depleted = np.sum(time_enzyme_depleted) / total_time
				enzyme_depletion[seed, variant] = fraction_enzyme_depleted

				# Record end product depletion
				fraction_metabolite_depleted = np.sum(time_metabolite_depleted) / total_time
				metabolite_depletion[seed, variant] = fraction_metabolite_depleted

		# Compute average and standard deviations
		metabolite_depletion_avg = np.average(metabolite_depletion, axis = 0)
		metabolite_depletion_std = np.std(metabolite_depletion, axis = 0)
		enzyme_depletion_avg = np.average(enzyme_depletion, axis = 0)
		enzyme_depletion_std = np.std(enzyme_depletion, axis = 0)

		# Plot
		fig, axesList = plt.subplots(2, 1, figsize = (8, 8))
		ax1, ax2 = axesList
		xvals = np.arange(ap.n_variant)
		fig.suptitle("Sensitivity Analysis: pabB depletion")

		for ax, avg, std in zip(axesList, [metabolite_depletion_avg, enzyme_depletion_avg], [metabolite_depletion_std, enzyme_depletion_std]):
			ax.scatter(xvals, avg, edgecolor = "none", clip_on = False, s = MARKERSIZE)
			ax.errorbar(xvals, avg, yerr = std, color = "b", linewidth = 1, clip_on = False, fmt = "o", capsize = 4, capthick = 1, markeredgecolor = "none")

		ax1.set_title("Enzyme depletion", fontsize = FONTSIZE)
		ax2.set_title("Metabolite depletion", fontsize = FONTSIZE)
		xlabels = ["1/10 x", "1/8 x", "1/4 x", "1/2 x", "1 x", "2 x", "4 x", "8 x", "10 x"]
		title_tags = ["counts = 0", "<%s%% of wildtype" % (THRESHOLD * 100)]
		for i, ax in enumerate([ax1, ax2]):
			ax.set_ylabel("Fraction of Time\n%s" % title_tags[i], fontsize = FONTSIZE)
			ax.set_xlabel("Factor of change of pabB synthesis probability", fontsize = FONTSIZE)
			ax.set_xlim([-0.25, 8.25])
			whitePadSparklineAxis(ax)
			ax.set_xticks(xvals)
			ax.set_xticklabels(xlabels)
			ax.set_yticks([0, 1])

		plt.subplots_adjust(hspace = 1, wspace = 1, top = 0.9, bottom = 0.1)
		exportFigure(plt, plotOutDir, plotOutFileName, metadata)
		plt.close("all")

		# Plot clean versions for figure
		FIRST = True
		for avg, std, filename in zip([metabolite_depletion_avg, enzyme_depletion_avg], [metabolite_depletion_std, enzyme_depletion_std], ["pabB", "methylene-thf"]):
			fig, ax = plt.subplots(1, 1, figsize = (10, 3))
			ax.scatter(xvals, avg, edgecolor = "none", clip_on = False, s = MARKERSIZE)
			ax.errorbar(xvals, avg, yerr = std, color = "b", linewidth = 1, clip_on = False, fmt = "o", capsize = 4, capthick = 1, markeredgecolor = "none")
			ax.set_xlim([-0.25, 8.25])
			if FIRST:
				FIRST = False
				whitePadSparklineAxis(ax, False)
			else:
				whitePadSparklineAxis(ax)
			ax.set_xticks(xvals)
			ax.set_xticklabels([])
			ax.set_yticks([0, 1])
			ax.set_yticklabels([])
			exportFigure(plt, plotOutDir, plotOutFileName + "_%s" % filename, metadata)
			plt.close("all")
    def do_plot(self, inputDir, plotOutDir, plotOutFileName, simDataFile,
                validationDataFile, metadata):
        if metadata is not None and SHUFFLE_VARIANT_TAG not in metadata[
                "variant"]:
            print "This plot only runs for variants where parameters are shuffled."
            return

        if not os.path.isdir(inputDir):
            raise Exception, "variantDir does not currently exist as a directory"

        if not os.path.exists(plotOutDir):
            os.mkdir(plotOutDir)

        ap = AnalysisPaths(inputDir, variant_plot=True)
        control_sim = ap.get_cells(variant=[0])
        variant_cells = ap.get_cells(variant=range(1, ap.n_variant))

        doublingTimes = []
        for simDir in variant_cells:
            try:
                simOutDir = os.path.join(simDir, "simOut")
                time = TableReader(os.path.join(simOutDir,
                                                "Main")).readColumn("time")
                initialTime = TableReader(os.path.join(
                    simOutDir, "Main")).readAttribute("initialTime")

                doublingTimes.append((time.max() - initialTime) / 60.)
            except Exception as e:
                print e
                continue

        doublingTimes = np.array(doublingTimes)

        controlDoublingTime = None
        for simDir in control_sim:

            simOutDir = os.path.join(simDir, "simOut")
            time = TableReader(os.path.join(simOutDir,
                                            "Main")).readColumn("time")
            initialTime = TableReader(os.path.join(
                simOutDir, "Main")).readAttribute("initialTime")

            controlDoublingTime = (time.max() - initialTime) / 60.

        fig = plt.figure()
        fig.set_figwidth(5)
        fig.set_figheight(5)
        ax = plt.subplot(1, 1, 1)
        ax.hist(doublingTimes, np.sqrt(doublingTimes.size))
        ax.axvline(controlDoublingTime,
                   color="k",
                   linestyle="dashed",
                   linewidth=2)

        ax.set_xlabel("Cell Division Time (min)")
        ax.set_title(
            "Mean: %0.3g     Std: %0.3g     Control: %0.3g" %
            (doublingTimes.mean(), doublingTimes.std(), controlDoublingTime))

        axes_list = [ax]

        for a in axes_list:
            for tick in a.yaxis.get_major_ticks():
                tick.label.set_fontsize(FONT_SIZE)
            for tick in a.xaxis.get_major_ticks():
                tick.label.set_fontsize(FONT_SIZE)

        whitePadSparklineAxis(ax)

        plt.subplots_adjust(bottom=0.2, wspace=0.3)

        exportFigure(plt, plotOutDir, plotOutFileName, metadata)
	def do_plot(self, seedOutDir, plotOutDir, plotOutFileName, simDataFile, validationDataFile, metadata):
		if not os.path.isdir(seedOutDir):
			raise Exception, "seedOutDir does not currently exist as a directory"

		if not os.path.exists(plotOutDir):
			os.mkdir(plotOutDir)

		# Get all cells
		ap = AnalysisPaths(seedOutDir, multi_gen_plot = True)
		allDir = ap.get_cells()

		sim_data = cPickle.load(open(simDataFile, "rb"))
		rxnStoich = sim_data.process.metabolism.reactionStoich

		reactants = [
			"GLC[p]",
			"GLC-6-P[c]",
			"FRUCTOSE-6P[c]",
			"FRUCTOSE-16-DIPHOSPHATE[c]",
			"DIHYDROXY-ACETONE-PHOSPHATE[c]",
			"GAP[c]",
			"DPG[c]",
			"G3P[c]",
			"2-PG[c]",
			"PHOSPHO-ENOL-PYRUVATE[c]",
			"PYRUVATE[c]",
			"ACETYL-COA[c]",
			"CIT[c]",
			"CIS-ACONITATE[c]",
			"THREO-DS-ISO-CITRATE[c]",
			"2-KETOGLUTARATE[c]",
			"SUC-COA[c]",
			"SUC[c]",
			"FUM[c]",
			"MAL[c]",
			"GLC-6-P[c]",
			"D-6-P-GLUCONO-DELTA-LACTONE[c]",
			"CPD-2961[c]",
			"RIBULOSE-5P[c]",
			"RIBULOSE-5P[c]",
			"XYLULOSE-5-PHOSPHATE[c]",
			"D-SEDOHEPTULOSE-7-P[c]",
			"FRUCTOSE-6P[c]",
			"WATER[c]",
			"SER[c]",
			"ACETYLSERINE[c]",
			"H**O-CYS[c]",
			"GLT[c]",
			"GLT[c]",
			"INDOLE[c]",
			"HISTIDINAL[c]",
			"PYRUVATE[c]",
			"L-ALPHA-ALANINE[c]",
			"GLT[c]",
			"GLT[c]",
			"GLT[c]",
			"L-ASPARTATE[c]",
			"O-PHOSPHO-L-HOMOSERINE[c]",
			"MESO-DIAMINOPIMELATE[c]",
			"L-ARGININO-SUCCINATE[c]",
			"2-KETOGLUTARATE[c]",
			"GLT[c]",
			"L-DELTA1-PYRROLINE_5-CARBOXYLATE[c]",
			"DGMP[c]",
			"DGDP[c]",
			"DGMP[c]",
			"DAMP[c]",
			"DADP[c]",
			"DAMP[c]",
			"TMP[c]",
			"TDP[c]",
			"TMP[c]",
			"DUMP[c]",
			"DUDP[c]",
			"DUMP[c]",
			"DCMP[c]",
			"DCDP[c]",
			"DCMP[c]",
			"GMP[c]",
			"GDP[c]",
			"GMP[c]",
			"AMP[c]",
			"ADP[c]",
			"AMP[c]",
			"UMP[c]",
			"UDP[c]",
			"UMP[c]",
			"CMP[c]",
			"CDP[c]",
			"CMP[c]",
			"GDP[c]",
			"GTP[c]",
			"ADP[c]",
			"ATP[c]",
			"TMP[c]",
			"UDP[c]",
			"UTP[c]",
			"UTP[c]",
			"CDP[c]",
			"CTP[c]",
			"GUANOSINE[c]",
			"CYTIDINE[c]",
			"OROTIDINE-5-PHOSPHATE[c]",
			]

		products = [
			"GLC-6-P[c]",
			"FRUCTOSE-6P[c]",
			"FRUCTOSE-16-DIPHOSPHATE[c]",
			"DIHYDROXY-ACETONE-PHOSPHATE[c]",
			"GAP[c]",
			"DPG[c]",
			"G3P[c]",
			"2-PG[c]",
			"PHOSPHO-ENOL-PYRUVATE[c]",
			"PYRUVATE[c]",
			"ACETYL-COA[c]",
			"CIT[c]",
			"CIS-ACONITATE[c]",
			"THREO-DS-ISO-CITRATE[c]",
			"2-KETOGLUTARATE[c]",
			"SUC-COA[c]",
			"SUC[c]",
			"FUM[c]",
			"MAL[c]",
			"OXALACETIC_ACID[c]",
			"D-6-P-GLUCONO-DELTA-LACTONE[c]",
			"CPD-2961[c]",
			"RIBULOSE-5P[c]",
			"XYLULOSE-5-PHOSPHATE[c]",
			"RIBOSE-5P[c]",
			"D-SEDOHEPTULOSE-7-P[c]",
			"ERYTHROSE-4P[c]",
			"ERYTHROSE-4P[c]",
			"SER[c]",
			"GLY[c]",
			"CYS[c]",
			"MET[c]",
			"TYR[c]",
			"PHE[c]",
			"TRP[c]",
			"HIS[c]",
			"L-ALPHA-ALANINE[c]",
			"VAL[c]",
			"LEU[c]",
			"ILE[c]",
			"L-ASPARTATE[c]",
			"ASN[c]",
			"THR[c]",
			"LYS[c]",
			"ARG[c]",
			"GLT[c]",
			"GLN[c]",
			"PRO[c]",
			"DGDP[c]",
			"DGTP[c]",
			"DGTP[c]",
			"DADP[c]",
			"DATP[c]",
			"DATP[c]",
			"TDP[c]",
			"TTP[c]",
			"TTP[c]",
			"DUDP[c]",
			"DUTP[c]",
			"DUTP[c]",
			"DCDP[c]",
			"DCTP[c]",
			"DCTP[c]",
			"GDP[c]",
			"GTP[c]",
			"GTP[c]",
			"ADP[c]",
			"ATP[c]",
			"ATP[c]",
			"UDP[c]",
			"UTP[c]",
			"UTP[c]",
			"CDP[c]",
			"CTP[c]",
			"CTP[c]",
			"DGDP[c]",
			"DGTP[c]",
			"DADP[c]",
			"DATP[c]",
			"DUMP[c]",
			"DUDP[c]",
			"DUTP[c]",
			"CTP[c]",
			"DCDP[c]",
			"DCTP[c]",
			"GMP[c]",
			"CMP[c]",
			"UMP[c]",
			]

		transports = [
			"GLC[p]",
			"OXYGEN-MOLECULE[p]",
			]

		fullReactants = [
			"2-KETOGLUTARATE[c]",
			"L-DELTA1-PYRROLINE_5-CARBOXYLATE[c]",
			"DADP[c]",
		]

		fullProducts = [
			"SUC-COA[c]",
			"PRO[c]",
			"DATP[c]",
		]

		figAll = plt.figure(figsize = (17, 22))
		figFull = plt.figure()

		subplotRows = 10
		subplotCols = 9

		firstGen = True
		for simDir in allDir:
			simOutDir = os.path.join(simDir, "simOut")

			mainListener = TableReader(os.path.join(simOutDir, "Main"))
			initialTime = mainListener.readAttribute("initialTime")
			time = mainListener.readColumn("time")
			mainListener.close()

			# ignore initial and final points to avoid moving average edge effects
			timeIdx = np.logical_and(np.logical_and(np.logical_or(np.logical_and(time >= START, time < SHIFT - MA_WIDTH), np.logical_and(time > SHIFT + BURNIN, time <= END)), time > initialTime + BURNIN), time < time[-MA_WIDTH])

			massListener = TableReader(os.path.join(simOutDir, "Mass"))
			cellMass = massListener.readColumn("cellMass")
			dryMass = massListener.readColumn("dryMass")
			massListener.close()

			coefficient = dryMass / cellMass * sim_data.constants.cellDensity.asNumber(MASS_UNITS / VOLUME_UNITS) # units - g/L

			fbaResults = TableReader(os.path.join(simOutDir, "FBAResults"))
			reactionIDs = fbaResults.readAttribute("reactionIDs")
			flux = (COUNTS_UNITS / MASS_UNITS / TIME_UNITS) * (fbaResults.readColumn("reactionFluxes").T / coefficient).T
			transportFluxes = fbaResults.readColumn("externalExchangeFluxes")
			transportMolecules = fbaResults.readAttribute("externalMoleculeIDs")
			fbaResults.close()

			flux = flux.asNumber(units.mmol / units.g / units.h)

			plt.figure(figAll.number)
			for idx, transport in enumerate(transports):
				ax = plt.subplot(subplotRows, subplotCols, idx + 1)

				if firstGen:
					ax.axhline(0, color = "#aaaaaa", linewidth = 0.25)
					ax.axvline(SHIFT, color = "#aaaaaa", linewidth = 0.25)
					ax.set_title("Transport for %s" % (transport,), fontsize = 4)
					ax.tick_params(axis = "both", labelsize = 4)

				if transport in transportMolecules:
					maFlux = np.array([np.convolve(-transportFluxes[:, transportMolecules.index(transport)], np.ones(MA_WIDTH) / MA_WIDTH, mode = "same")]).T
					ax.plot(time[timeIdx], maFlux[timeIdx], color = "b", linewidth = 0.5)

			for idx, (reactant, product) in enumerate(zip(reactants, products)):
				ax = plt.subplot(subplotRows, subplotCols, idx + 1 + len(transports))
				totalFlux = np.zeros_like(flux[:, 0])

				for rxn in rxnStoich:
					if reactant in rxnStoich[rxn] and product in rxnStoich[rxn]:
						if rxnStoich[rxn][reactant] < 0 and rxnStoich[rxn][product] > 0:
							direction = 1
						elif rxnStoich[rxn][reactant] > 0 and rxnStoich[rxn][product] < 0:
							direction = -1
						else:
							continue

						totalFlux += flux[:, reactionIDs.index(rxn)] * direction

				if firstGen:
					ax.axhline(0, color = "#aaaaaa", linewidth = 0.25)
					ax.axvline(SHIFT, color = "#aaaaaa", linewidth = 0.25)
					ax.set_title("%s to %s" % (reactant, product), fontsize = 4)
					ax.tick_params(axis = "both", labelsize = 4)

				totalFlux = np.array([np.convolve(totalFlux, np.ones(MA_WIDTH) / MA_WIDTH, mode = "same")]).T
				ax.plot(time[timeIdx], totalFlux[timeIdx], color = "b", linewidth = 0.5)

			plt.figure(figFull.number)
			for idx, (reactant, product) in enumerate(zip(fullReactants, fullProducts)):
				ax = plt.subplot(3, 1, idx+1)
				totalFlux = np.zeros_like(flux[:, 0])

				for rxn in rxnStoich:
					if reactant in rxnStoich[rxn] and product in rxnStoich[rxn]:
						if rxnStoich[rxn][reactant] < 0 and rxnStoich[rxn][product] > 0:
							direction = 1
						elif rxnStoich[rxn][reactant] > 0 and rxnStoich[rxn][product] < 0:
							direction = -1
						else:
							continue

						totalFlux += flux[:, reactionIDs.index(rxn)] * direction

				if firstGen:
					ax.axhline(0, color = "#aaaaaa", linewidth = 0.25)
					ax.axvline(SHIFT / 60, color = "#aaaaaa", linewidth = 0.25)
					ax.set_title("%s to %s" % (reactant, product), fontsize = 4)
					ax.tick_params(axis = "both", labelsize = 4)

				totalFlux = np.array([np.convolve(totalFlux, np.ones(MA_WIDTH) / MA_WIDTH, mode = "same")]).T
				ax.plot(time / 60, totalFlux, color = "b", linewidth = 0.5)

			firstGen = False

		plt.figure(figAll.number)
		for i in range(subplotRows * subplotCols):
			ax = plt.subplot(subplotRows, subplotCols, i + 1)
			plt.minorticks_off()

			whitePadSparklineAxis(ax)
			xlim = ax.get_xlim()
			ylim = ax.get_ylim()
			ax.set_yticks([ylim[0], ylim[1]])
			ax.set_xticks([xlim[0], xlim[1]])

		exportFigure(plt, plotOutDir, plotOutFileName, metadata)

		for i in range(subplotRows * subplotCols):
			ax = plt.subplot(subplotRows, subplotCols, i + 1)
			ax.set_axis_off()

		exportFigure(plt, plotOutDir, plotOutFileName + "_stripped", metadata)

		plt.figure(figFull.number)
		for i in range(3):
			ax = plt.subplot(3, 1, i+1)
			plt.minorticks_off()

			whitePadSparklineAxis(ax)
			bounds = ax.dataLim.bounds
			xlim = [bounds[0], bounds[2]]
			ylim = [bounds[1], bounds[3]]
			ax.set_xticks(xlim)
			ax.set_yticks(ylim)
			ax.set_xlim(xlim)
			ax.set_ylim(ylim)

		exportFigure(plt, plotOutDir, plotOutFileName + "_full", metadata)
		plt.close("all")
Beispiel #19
0
    def do_plot(self, simOutDir, plotOutDir, plotOutFileName, simDataFile,
                validationDataFile, metadata):
        if not os.path.isdir(simOutDir):
            raise Exception, "simOutDir does not currently exist as a directory"

        if not os.path.exists(plotOutDir):
            os.mkdir(plotOutDir)

        mass = TableReader(os.path.join(simOutDir, "Mass"))

        protein = mass.readColumn("proteinMass")
        tRna = mass.readColumn("tRnaMass")
        rRna = mass.readColumn("rRnaMass")
        mRna = mass.readColumn("mRnaMass")
        dna = mass.readColumn("dnaMass")
        smallMolecules = mass.readColumn("smallMoleculeMass")

        initialTime = TableReader(os.path.join(
            simOutDir, "Main")).readAttribute("initialTime")
        t = TableReader(os.path.join(simOutDir,
                                     "Main")).readColumn("time") - initialTime

        masses = np.vstack([
            protein / protein[0],
            rRna / rRna[0],
            tRna / tRna[0],
            mRna / mRna[0],
            dna / dna[0],
            smallMolecules / smallMolecules[0],
        ]).T

        massLabels = ["Protein", "rRNA", "tRNA", "mRNA", "DNA", "Small Mol."]

        plt.figure(figsize=(2, 2.5))

        ax = plt.gca()
        ax.set_prop_cycle(
            plt.style.library['fivethirtyeight']['axes.prop_cycle'])

        plt.plot(t / 60., masses, linewidth=1)
        plt.xlabel("Time (min)")
        plt.ylabel("Mass (normalized by t = 0 min)")
        plt.title("Biomass components")
        plt.legend(massLabels, loc="best")
        plt.axhline(2, linestyle='--', color='k')

        whitePadSparklineAxis(ax)

        xticks = [0, t[-1] / 60]
        yticks = [1, 2, 2.2]
        ax.set_xlim(xticks)
        ax.set_ylim((yticks[0], yticks[-1]))
        ax.set_xticks(xticks)
        ax.set_yticks(yticks)

        exportFigure(plt, plotOutDir, plotOutFileName, metadata)

        ax.set_xlabel("")
        ax.set_ylabel("")
        ax.set_title("")
        ax.set_xticklabels([])
        ax.set_yticklabels([])
        ax.legend_.remove()

        exportFigure(plt, plotOutDir, plotOutFileName + "_stripped", metadata)
        plt.close("all")
    def do_plot(self, simOutDir, plotOutDir, plotOutFileName, simDataFile,
                validationDataFile, metadata):
        if not os.path.isdir(simOutDir):
            raise Exception, 'simOutDir does not currently exist as a directory'

        if not os.path.exists(plotOutDir):
            os.mkdir(plotOutDir)

        # Read data from listeners
        enzymeKinetics = TableReader(os.path.join(simOutDir, "EnzymeKinetics"))
        actualConc = enzymeKinetics.readColumn("metaboliteConcentrations")[
            1:, :]
        enzymeKinetics.close()

        fbaResults = TableReader(os.path.join(simOutDir, "FBAResults"))
        targetConc = fbaResults.readColumn("targetConcentrations")[1:, :]
        moleculeNames = np.array(
            fbaResults.readAttribute("homeostaticTargetMolecules"))
        fbaResults.close()

        mainReader = TableReader(os.path.join(simOutDir, "Main"))
        time = mainReader.readColumn("time")[1:] - mainReader.readAttribute(
            "initialTime")
        mainReader.close()

        # Average concentrations over time for subplot 1
        actualConcAve = np.nanmean(actualConc, axis=0)
        targetConcAve = np.nanmean(targetConc, axis=0)

        plt.figure(figsize=(8.5, 11))

        # Plot average comparison with lines denoting order of magnitude
        ax = plt.subplot(3, 1, 1)
        ax.plot([-6, 6], [-6, 6], 'k')
        ax.plot([-5, 6], [-6, 5], 'k')
        ax.plot([-6, 5], [-5, 6], 'k')
        ax.plot(np.log10(targetConcAve),
                np.log10(actualConcAve),
                "ob",
                markeredgewidth=0,
                alpha=0.25)
        ax.set_xlabel("Log10(Target Concentration [mmol/L])", fontsize=8)
        ax.set_ylabel("Log10(Actual Concentration [mmol/L])", fontsize=8)

        whitePadSparklineAxis(ax)
        xlim = ax.get_xlim()
        ylim = ax.get_ylim()
        ax.set_ylim(ylim[0] - 0.1, ylim[1])
        ax.set_xlim(xlim[0] - 0.1, xlim[1])
        ax.set_yticks(range(-6, int(ylim[1]) + 1, 2))
        ax.set_xticks(range(-6, int(xlim[1]) + 1, 2))
        ax.tick_params(axis='both', which='major', labelsize=6)

        # Plot ratio of actual concentration to target concentration
        ratio = np.log10(actualConc / targetConc)
        ax = plt.subplot(3, 1, 2)
        ax.plot(time / 60, ratio)
        ax.set_xlabel("Time (min)", fontsize=8)
        ax.set_ylabel("Log10(Concentration to Target)", fontsize=8)
        ax.tick_params(axis='both', which='major', labelsize=6)

        # Plot outliers of ratio
        means = np.mean(ratio, axis=0)
        mean = np.mean(means)
        std = np.std(means)
        outliers = np.unique(
            np.where((ratio[1:, :] > mean + std / 2)
                     | (ratio[1:, :] < mean - std / 2))[1])
        idx = outliers[np.argsort(means[outliers])][::-1]

        ax = plt.subplot(3, 1, 3)
        if len(idx):
            ax.plot(time / 60, ratio[:, idx])
            ax.legend(moleculeNames[idx], fontsize=6)
        ax.set_xlabel("Time (min)", fontsize=8)
        ax.set_ylabel("Log10(Concentration to Target)", fontsize=8)
        ax.tick_params(axis='both', which='major', labelsize=6)

        plt.tight_layout()

        exportFigure(plt, plotOutDir, plotOutFileName)
        plt.close("all")
	def do_plot(self, seedOutDir, plotOutDir, plotOutFileName, simDataFile, validationDataFile, metadata):
		if not os.path.isdir(seedOutDir):
			raise Exception, "seedOutDir does not currently exist as a directory"

		if not os.path.exists(plotOutDir):
			os.mkdir(plotOutDir)

		# Get all cells
		ap = AnalysisPaths(seedOutDir, multi_gen_plot = True)
		allDir = ap.get_cells()

		sim_data = cPickle.load(open(simDataFile, "rb"))
		cellDensity = sim_data.constants.cellDensity
		rna_ids = sim_data.process.transcription.rnaData["id"]

		enzyme_rna_transcription_indexes = np.array([
			np.where(rna_ids == enzyme_rna_id)[0][0]
			for enzyme_rna_id in ENZYME_RNA_IDS
			])

		simOutDir = os.path.join(allDir[0], "simOut")
		bulk_molecules_reader = TableReader(os.path.join(simOutDir, "BulkMolecules"))
		fba_results_reader = TableReader(os.path.join(simOutDir, "FBAResults"))

		moleculeIDs = bulk_molecules_reader.readAttribute("objectNames")
		reactionIDs = np.array(fba_results_reader.readAttribute("reactionIDs"))

		enzyme_rna_count_indexes = np.array([
			moleculeIDs.index(enzyme_rna_id)
			for enzyme_rna_id in ENZYME_RNA_IDS
			])
		enzyme_monomer_indexes = np.array([
			moleculeIDs.index(enzyme_monomer_id)
			for enzyme_monomer_id in ENZYME_MONOMER_IDS
			])
		enzyme_complex_indexes = np.array([
			moleculeIDs.index(enzyme_complex_id)
			for enzyme_complex_id in ENZYME_COMPLEX_IDS
			])
		reaction_indexes = np.array([
			np.where(reactionIDs == reaction_id)[0][0]
			for reaction_id in ENZYME_REACTION_IDS
			])
		metabolite_index = moleculeIDs.index(METABOLITE_ID)

		# Initialize arrays
		time = []
		enzyme_rna_init_events = np.empty((0, len(ENZYME_RNA_IDS)))
		enzyme_rna_counts = np.empty((0, len(ENZYME_RNA_IDS)))
		enzyme_total_monomer_counts = np.empty((0, len(ENZYME_RNA_IDS)))
		enzyme_complex_counts = np.empty((0, len(ENZYME_COMPLEX_IDS)))
		enzyme_fluxes = np.empty((0, len(ENZYME_REACTION_IDS)))
		metabolite_counts = []

		cellMass = []
		dryMass = []
		timeStepSec = []
		generationTicks = []

		proteins_produced_per_gen = np.empty((0, len(ENZYME_RNA_IDS)))
		average_complex_counts_per_gen = []

		first_gen = True

		for simDir in allDir:
			simOutDir = os.path.join(simDir, "simOut")

			main_reader = TableReader(os.path.join(simOutDir, "Main"))
			mass_reader = TableReader(os.path.join(simOutDir, "Mass"))
			bulk_molecules_reader = TableReader(
				os.path.join(simOutDir, "BulkMolecules"))
			fba_results_reader = TableReader(os.path.join(simOutDir, "FBAResults"))
			rnap_data_reader = TableReader(os.path.join(simOutDir, "RnapData"))

			time.extend(main_reader.readColumn("time").tolist())

			if first_gen:
				generationTicks.extend([time[0], time[-1]])
				first_gen = False
			else:
				generationTicks.append(time[-1])

			timeStepSec.extend(main_reader.readColumn("timeStepSec").tolist())
			cellMass.extend(mass_reader.readColumn("cellMass").tolist())
			dryMass.extend(mass_reader.readColumn("dryMass").tolist())

			rna_init_events_this_gen = rnap_data_reader.readColumn(
				"rnaInitEvent")[:, enzyme_rna_transcription_indexes]
			enzyme_rna_init_events = np.vstack((
				enzyme_rna_init_events, rna_init_events_this_gen))

			molecule_counts = bulk_molecules_reader.readColumn("counts")

			enzyme_rna_counts = np.vstack((
				enzyme_rna_counts,
				molecule_counts[:, enzyme_rna_count_indexes]))

			enzyme_monomer_counts_this_gen = molecule_counts[:, enzyme_monomer_indexes]
			enzyme_complex_counts_this_gen = molecule_counts[:, enzyme_complex_indexes]

			enzyme_total_monomer_counts_this_gen = (
					enzyme_monomer_counts_this_gen +
					enzyme_complex_counts_this_gen.sum(axis=1)[:, None])

			enzyme_total_monomer_counts = np.vstack((
				enzyme_total_monomer_counts,
				enzyme_total_monomer_counts_this_gen))
			enzyme_complex_counts = np.vstack((
				enzyme_complex_counts,
				enzyme_complex_counts_this_gen))
			proteins_produced_per_gen = np.vstack((
				proteins_produced_per_gen,
				(enzyme_total_monomer_counts_this_gen[-1, :] -
					enzyme_total_monomer_counts_this_gen[0, :])
				))
			average_complex_counts_per_gen.append(
				enzyme_complex_counts_this_gen.sum(axis=1).mean())

			metabolite_counts.extend(molecule_counts[:, metabolite_index])

			reactionFluxes = np.array(fba_results_reader.readColumn("reactionFluxes"))
			enzyme_fluxes = np.vstack((
				enzyme_fluxes, reactionFluxes[:, reaction_indexes]))

		# Sum reaction fluxes and convert units
		flux_conversion_coeff = (units.fg * np.array(dryMass)) / (units.fg * np.array(cellMass)) * (timeStepSec * units.s) * cellDensity
		enzyme_fluxes = (((COUNTS_UNITS / VOLUME_UNITS) * enzyme_fluxes.sum(axis=1)) / flux_conversion_coeff).asNumber(units.mmol / units.g / units.h)

		# Convert time to hours
		time = np.array(time)
		time_hours = time / 3600.

		# Add counts of complexed monomers to monomer counts
		enzyme_total_monomer_counts += enzyme_complex_counts.sum(axis=1)[:, None]

		# Plot
		plt.figure(figsize = (14, 8.5))
		plt.style.use('seaborn-deep')
		color_cycle = plt.rcParams['axes.prop_cycle'].by_key()['color']
		plt.suptitle(
			"4-amino-4-deoxychorismate synthase downstream effects",
			fontsize = FONTSIZE)
		pre_merge_colors = [color_cycle[0], color_cycle[2]]
		post_merge_color = color_cycle[3]

		# Define axes
		rna_init_axis = plt.subplot(6, 1, 1)
		rna_axis = plt.subplot(6, 1, 2, sharex=rna_init_axis)
		monomer_axis = plt.subplot(6, 1, 3, sharex=rna_init_axis)
		complex_axis = plt.subplot(6, 1, 4, sharex=rna_init_axis)
		flux_axis = plt.subplot(6, 1, 5, sharex=rna_init_axis)
		met_axis = plt.subplot(6, 1, 6, sharex=rna_init_axis)

		# Plot transcription initiation events
		rna_init_axis.set_prop_cycle(color=pre_merge_colors)
		rna_init_axis.plot(time_hours, enzyme_rna_init_events)
		rna_init_axis.set_ylabel("Transcription\nevents", fontsize = FONTSIZE, rotation = 0)
		rna_init_axis.yaxis.set_label_coords(-.12, 0.25)
		rna_init_axis.set_xlim([time_hours[0], time_hours[-1]])
		rna_init_axis.set_ylim([0, 1])
		whitePadSparklineAxis(rna_init_axis, xAxis = False)

		# Print average transcription frequency of each gene
		for rna_id, prob in zip(ENZYME_RNA_IDS,
				enzyme_rna_init_events.sum(axis=0)/len(proteins_produced_per_gen)):
			print("%s transcription frequency: %.3f"%(rna_id, prob))

		rna_axis.set_prop_cycle(color=pre_merge_colors)
		rna_axis.plot(time_hours, enzyme_rna_counts)
		rna_axis.set_ylabel("mRNA\ncounts", fontsize = FONTSIZE, rotation = 0)
		rna_axis.yaxis.set_label_coords(-.12, 0.25)
		rna_axis.set_ylim([0, np.max(enzyme_rna_counts)])
		whitePadSparklineAxis(rna_axis, xAxis = False)

		monomer_axis.set_prop_cycle(color=pre_merge_colors)
		monomer_axis.plot(time_hours, enzyme_total_monomer_counts)
		monomer_axis.set_ylabel("Protein monomer\ncounts", fontsize = FONTSIZE, rotation = 0)
		monomer_axis.yaxis.set_label_coords(-.12, 0.25)
		monomer_axis.set_ylim([0, np.max(enzyme_total_monomer_counts)])
		whitePadSparklineAxis(monomer_axis, xAxis = False)

		# Print average number of protein produced per generation
		for rna_id, count in zip(ENZYME_RNA_IDS,
				proteins_produced_per_gen.mean(axis=0)):
			print("%s average proteins produced per gen: %.2f" % (rna_id, count))

		complex_axis.plot(time_hours, enzyme_complex_counts.sum(axis=1), color=post_merge_color)
		complex_axis.set_ylabel("Protein complex\ncounts", fontsize = FONTSIZE, rotation = 0)
		complex_axis.yaxis.set_label_coords(-.12, 0.25)
		complex_axis.set_ylim([0, np.max(enzyme_complex_counts.sum(axis=1))])
		whitePadSparklineAxis(complex_axis, xAxis = False)

		# Print mean and std of average complex counts in each gen
		print("Complex counts average: %.2f" % (np.array(average_complex_counts_per_gen).mean(),))
		print("Complex counts std: %.2f" % (np.array(average_complex_counts_per_gen).std(),))

		flux_axis.plot(time_hours, enzyme_fluxes, color=post_merge_color)
		flux_axis.set_yscale("symlog", linthreshy=FLUX_LINEAR_THRESHOLD)
		flux_axis.set_ylabel("PABASYN-RXN\n(reverse)\ntotal flux\n(mmol/gDCW/hour)", fontsize = FONTSIZE, rotation = 0)
		flux_axis.yaxis.set_label_coords(-.12, 0.25)
		flux_axis.set_ylim([0, np.max(enzyme_fluxes)])
		whitePadSparklineAxis(flux_axis, xAxis=False)
		flux_axis.get_yaxis().set_tick_params(which='minor', size=0)
		flux_axis.get_xaxis().set_tick_params(which='minor', width=0)
		flux_max = flux_axis.get_ylim()[1]
		flux_axis.set_yticks([0, FLUX_LINEAR_THRESHOLD, flux_max])
		flux_axis.set_yticklabels(["0", "%0.0e"%(FLUX_LINEAR_THRESHOLD, ), "%.2f"%(flux_max, )])

		met_axis.plot(time_hours, metabolite_counts, color=post_merge_color)
		met_axis.set_ylabel("End product\ncounts", fontsize = FONTSIZE, rotation = 0)
		met_axis.yaxis.set_label_coords(-.12, 0.25)
		met_axis.set_xlabel("Time (hour)\ntickmarks at each new generation", fontsize = FONTSIZE)
		met_axis.set_ylim([0, np.max(metabolite_counts)])
		met_axis.set_xlim([time_hours[0], time_hours[-1]])
		whitePadSparklineAxis(met_axis)
		met_axis.set_yticklabels([0, "%0.1e" % met_axis.get_ylim()[1]])
		met_axis.set_xticks(np.array(generationTicks) / 3600.)
		xticklabels = np.repeat("     ", len(generationTicks))
		xticklabels[0] = "%0.2f" % (time_hours[0])
		xticklabels[-1] = "%0.2f" % (time_hours[-1])
		met_axis.set_xticklabels(xticklabels)

		# Add patches to indicate absence of complexes
		noComplexIndexes = np.where(np.array(enzyme_complex_counts.sum(axis=1)) == 0)[0]
		patchStart = []
		patchEnd = []
		if len(noComplexIndexes):
			prev = noComplexIndexes[0]
			patchStart.append(prev)
			for i in noComplexIndexes:
				if np.abs(i - prev) > 1:
					patchStart.append(i)
					patchEnd.append(prev)
				prev = i
			patchEnd.append(prev)

		axesList = [rna_init_axis, rna_axis, monomer_axis, complex_axis, flux_axis, met_axis]
		for axis in axesList:
			axis.tick_params(labelsize = LABELSIZE)
			for i in xrange(len(patchStart)):
				width = time_hours[patchEnd[i]] - time_hours[patchStart[i]]
				if width <= 0.1:
					continue

				height = axis.get_ylim()[1] - axis.get_ylim()[0]
				axis.add_patch(patches.Rectangle((time_hours[patchStart[i]], axis.get_ylim()[0]), width, height, alpha = 0.25, color = "gray", linewidth = 0.))

		plt.subplots_adjust(hspace = 0.5, right = 0.9, bottom = 0.1, left = 0.15, top = 0.9)
		exportFigure(plt, plotOutDir, plotOutFileName, metadata)

		# Get clean version of plot
		for a in axesList:
			clearLabels(a)

		plt.suptitle("")
		met_axis.set_xticklabels([])
		met_axis.set_xlabel("")
		exportFigure(plt, plotOutDir, plotOutFileName + "__clean", "")
		plt.close("all")
Beispiel #22
0
    def do_plot(self, inputDir, plotOutDir, plotOutFileName, simDataFile,
                validationDataFile, metadata):
        if not os.path.isdir(inputDir):
            raise Exception, "variantDir does not currently exist as a directory"

        if not os.path.exists(plotOutDir):
            os.mkdir(plotOutDir)

        ap = AnalysisPaths(inputDir, variant_plot=True)

        if ap.n_generation == 1:
            print "Need more data to create addedMass"
            return

        allScatter = plt.figure()
        allScatter.set_figwidth(11)
        allScatter.set_figheight(6)

        xHist = plt.figure()
        xHist.set_figwidth(11)
        xHist.set_figheight(6)

        yHist = plt.figure()
        yHist.set_figwidth(11)
        yHist.set_figheight(6)

        plt.style.use('seaborn-deep')
        color_cycle = plt.rcParams['axes.prop_cycle'].by_key()['color']

        title_list = [
            "Glucose minimal\n" + r"$\tau = $" + "44 min",
            "Glucose minimal anaerobic\n" + r"$\tau = $" + "100 min",
            "Glucose minimal + 20 amino acids\n" + r"$\tau = $" + "22 min"
        ]

        plot = False

        for varIdx in ap.get_variants():

            if varIdx == 0:
                plotIdx = 1
                gen = [2, 3]
            elif varIdx == 1:
                plotIdx = 0
                gen = [2, 3]
            elif varIdx == 2:
                plotIdx = 2
                gen = [2, 3]
            else:
                continue

            initial_masses = np.zeros(0)
            final_masses = np.zeros(0)

            all_cells = ap.get_cells(generation=gen, variant=[varIdx])
            if len(all_cells) == 0:
                continue
            plot = True

            fail = 0
            for simDir in all_cells:
                try:
                    simOutDir = os.path.join(simDir, "simOut")
                    mass = TableReader(os.path.join(simOutDir, "Mass"))
                    cellMass = mass.readColumn("dryMass")

                    initial_masses = np.hstack((initial_masses, cellMass[0]))
                    final_masses = np.hstack((final_masses, cellMass[-1]))
                except Exception as e:
                    print e
                    fail += 1

            added_masses = final_masses - initial_masses

            all_scaled_initial_masses = initial_masses / initial_masses.mean()
            all_scaled_added_masses = added_masses / added_masses.mean()

            idxs_to_keep = np.where((0.6 < all_scaled_initial_masses)
                                    & (all_scaled_initial_masses < 1.25)
                                    & (0.45 < all_scaled_added_masses)
                                    & (all_scaled_added_masses < 1.5))

            scaled_initial_masses = all_scaled_initial_masses[idxs_to_keep]
            scaled_added_masses = all_scaled_added_masses[idxs_to_keep]

            nbins = 5

            n, xbin = np.histogram(scaled_initial_masses, bins=nbins)
            sy, xbin = np.histogram(scaled_initial_masses,
                                    bins=nbins,
                                    weights=scaled_added_masses)
            sy2, xbin = np.histogram(scaled_initial_masses,
                                     bins=nbins,
                                     weights=scaled_added_masses *
                                     scaled_added_masses)
            mean = sy / n
            std = np.sqrt(sy2 / (n - 1) - n * mean * mean / (n - 1))

            slope, intercept, r_value, p_value, std_err = linregress(
                scaled_initial_masses, scaled_added_masses)

            # plot all scatter plots
            plt.figure(allScatter.number)
            ax = plt.subplot2grid((1, 3), (0, plotIdx))
            ax.plot(scaled_initial_masses,
                    scaled_added_masses,
                    '.',
                    color="black",
                    alpha=0.2,
                    zorder=1,
                    markeredgewidth=0.0)
            ax.errorbar(((xbin[1:] + xbin[:-1]) / 2),
                        mean,
                        yerr=std,
                        color="black",
                        linewidth=1,
                        zorder=2)
            ax.plot(scaled_initial_masses,
                    slope * scaled_initial_masses + intercept,
                    color="blue")

            ax.set_title(
                title_list[varIdx] + ", n=%d, n*=%d" %
                ((len(all_cells) - fail), len(scaled_initial_masses)) + "\n" +
                r"$m_{add}$=%.3f$\times$$m_{init}$ + %.3f" %
                (slope, intercept) + "\n" + "p-value=%0.2g" % p_value,
                fontsize=FONT_SIZE)

            ax.set_xlim([0.6, 1.25])
            ax.set_ylim([0.45, 1.5])
            ax.get_yaxis().get_major_formatter().set_useOffset(False)
            ax.get_xaxis().get_major_formatter().set_useOffset(False)

            if varIdx == 1:
                ax.set_ylabel("Normed added mass", fontsize=FONT_SIZE)
            ax.set_xlabel("Normed initial mass", fontsize=FONT_SIZE)

            plt.subplots_adjust(bottom=0.2)

            whitePadSparklineAxis(ax)

            for tick in ax.yaxis.get_major_ticks():
                tick.label.set_fontsize(FONT_SIZE)
            for tick in ax.xaxis.get_major_ticks():
                tick.label.set_fontsize(FONT_SIZE)

            # plot stripped figure
            fig = plt.figure()
            fig.set_figwidth(1.73)
            fig.set_figheight(1.18)
            ax = plt.subplot2grid((1, 1), (0, 0))
            ax.plot(scaled_initial_masses,
                    scaled_added_masses,
                    '.',
                    color=color_cycle[0],
                    alpha=0.2,
                    zorder=1,
                    markeredgewidth=0.0)
            ax.set_title(title_list[varIdx] + ", n=%d, n*=%d" %
                         (len(all_cells) - fail, len(scaled_initial_masses)),
                         fontsize=FONT_SIZE)
            ax.plot(scaled_initial_masses,
                    slope * scaled_initial_masses + intercept,
                    color='k')

            ax.set_ylim([0.45, 1.5])

            ax.get_yaxis().get_major_formatter().set_useOffset(False)
            ax.get_xaxis().get_major_formatter().set_useOffset(False)

            plt.subplots_adjust(bottom=0.2)

            whitePadSparklineAxis(ax)

            ax.tick_params(axis='x',
                           which='both',
                           bottom='off',
                           top='off',
                           labelbottom='off')
            ax.tick_params(axis='y',
                           which='both',
                           left='off',
                           right='off',
                           labelleft='off')

            ax.set_xlabel("")
            ax.set_ylabel("")

            plt.subplots_adjust(top=0.95,
                                bottom=3 * trim,
                                left=2 * trim,
                                right=0.95,
                                hspace=0,
                                wspace=0)

            exportFigure(plt,
                         plotOutDir,
                         plotOutFileName + str(varIdx) + "_stripped",
                         metadata,
                         transparent=True)

            # plot histogram for x-axis
            plt.figure(xHist.number)
            bins = 25
            ax = plt.subplot2grid((1, 3), (0, plotIdx))
            ax.hist(all_scaled_initial_masses, bins, color=color_cycle[0])

            ax.axvline(x=0.6, color="k", linestyle="--")
            ax.axvline(x=1.25, color="k", linestyle="--")
            ax.set_title(title_list[varIdx] + "\n" + "[0.6, 1.25]",
                         fontsize=FONT_SIZE)
            ax.yaxis.set_major_locator(MaxNLocator(integer=True))

            ax.set_xlabel("Normed initial mass", fontsize=FONT_SIZE)

            plt.subplots_adjust(bottom=0.2)

            whitePadSparklineAxis(ax)

            for tick in ax.yaxis.get_major_ticks():
                tick.label.set_fontsize(FONT_SIZE)
            for tick in ax.xaxis.get_major_ticks():
                tick.label.set_fontsize(FONT_SIZE)

            # plot histogram for y-axis
            plt.figure(yHist.number)
            ax = plt.subplot2grid((1, 3), (0, plotIdx))
            ax.hist(all_scaled_added_masses, bins, color=color_cycle[0])

            ax.axvline(x=0.45, color="k", linestyle="--")
            ax.axvline(x=1.5, color="k", linestyle="--")
            ax.set_title(title_list[varIdx] + "\n" + "[0.45, 1.5]",
                         fontsize=FONT_SIZE)
            ax.yaxis.set_major_locator(MaxNLocator(integer=True))

            ax.set_xlabel("Normed added mass", fontsize=FONT_SIZE)

            plt.subplots_adjust(bottom=0.2)

            whitePadSparklineAxis(ax)

            for tick in ax.yaxis.get_major_ticks():
                tick.label.set_fontsize(FONT_SIZE)
            for tick in ax.xaxis.get_major_ticks():
                tick.label.set_fontsize(FONT_SIZE)

        if plot:
            plt.figure(allScatter.number)
            exportFigure(plt, plotOutDir, plotOutFileName, metadata)
            plt.figure(xHist.number)
            exportFigure(plt,
                         plotOutDir,
                         plotOutFileName + "_histogram_scaled_initial_mass",
                         metadata,
                         transparent=True)
            plt.figure(yHist.number)
            exportFigure(plt,
                         plotOutDir,
                         plotOutFileName + "_histogram_scaled_added_mass",
                         metadata,
                         transparent=True)
        plt.close("all")
    def do_plot(self, seedOutDir, plotOutDir, plotOutFileName, simDataFile,
                validationDataFile, metadata):
        if not os.path.isdir(seedOutDir):
            raise Exception, "seedOutDir does not currently exist as a directory"

        if not os.path.exists(plotOutDir):
            os.mkdir(plotOutDir)

        BUILD_CACHE = True
        if os.path.exists(os.path.join(plotOutDir, "figure5D.pickle")):
            BUILD_CACHE = False

        enzymeComplexId = "MENE-CPLX[c]"
        enzymeMonomerId = "O-SUCCINYLBENZOATE-COA-LIG-MONOMER[c]"
        enzymeRnaId = "EG12437_RNA[c]"
        reactionId = "O-SUCCINYLBENZOATE-COA-LIG-RXN"
        metaboliteIds = ["REDUCED-MENAQUINONE[c]", "CPD-12115[c]"]

        # Get all cells
        ap = AnalysisPaths(seedOutDir, multi_gen_plot=True)
        if 0 not in ap._path_data["seed"]:
            print "Skipping -- figure5D only runs for seed 0"
            return

        allDir = ap.get_cells(seed=[0])

        sim_data = cPickle.load(open(simDataFile, "rb"))
        cellDensity = sim_data.constants.cellDensity
        nAvogadro = sim_data.constants.nAvogadro

        rnaIds = sim_data.process.transcription.rnaData["id"]
        isMRna = sim_data.process.transcription.rnaData["isMRna"]
        mRnaIndexes = np.where(isMRna)[0]
        mRnaIds = np.array([rnaIds[x] for x in mRnaIndexes])

        simOutDir = os.path.join(allDir[0], "simOut")
        bulkMolecules = TableReader(os.path.join(simOutDir, "BulkMolecules"))
        moleculeIds = bulkMolecules.readAttribute("objectNames")
        enzymeComplexIndex = moleculeIds.index(enzymeComplexId)
        enzymeMonomerIndex = moleculeIds.index(enzymeMonomerId)
        enzymeRnaIndex = moleculeIds.index(enzymeRnaId)
        metaboliteIndexes = [moleculeIds.index(x) for x in metaboliteIds]
        bulkMolecules.close()

        if BUILD_CACHE:
            time = []
            enzymeFluxes = []
            enzymeComplexCounts = []
            enzymeMonomerCounts = []
            enzymeRnaCounts = []
            enzymeRnaInitEvent = []
            metaboliteCounts = np.array([])

            cellMass = []
            dryMass = []
            timeStepSec = []
            generationTicks = [0.]

            nTranscriptionInitEventsPerGen = []
            nAvgTetramersPerGen = []

            for gen, simDir in enumerate(allDir):
                simOutDir = os.path.join(simDir, "simOut")

                time += TableReader(os.path.join(
                    simOutDir, "Main")).readColumn("time").tolist()
                generationTicks.append(time[-1])
                timeStepSec += TableReader(os.path.join(
                    simOutDir, "Main")).readColumn("timeStepSec").tolist()
                cellMass += TableReader(os.path.join(
                    simOutDir, "Mass")).readColumn("cellMass").tolist()
                dryMass += TableReader(os.path.join(
                    simOutDir, "Mass")).readColumn("dryMass").tolist()

                bulkMolecules = TableReader(
                    os.path.join(simOutDir, "BulkMolecules"))
                moleculeCounts = bulkMolecules.readColumn("counts")
                enzymeComplexCountsInThisGen = moleculeCounts[:,
                                                              enzymeComplexIndex].tolist(
                                                              )
                enzymeMonomerCounts += moleculeCounts[:,
                                                      enzymeMonomerIndex].tolist(
                                                      )
                enzymeRnaCounts += moleculeCounts[:, enzymeRnaIndex].tolist()

                enzymeComplexCounts += enzymeComplexCountsInThisGen
                nAvgTetramersPerGen.append(
                    np.mean(enzymeComplexCountsInThisGen))

                if gen == 0:
                    metaboliteCounts = moleculeCounts[:, metaboliteIndexes]
                else:
                    metaboliteCounts = np.vstack(
                        (metaboliteCounts, moleculeCounts[:,
                                                          metaboliteIndexes]))
                bulkMolecules.close()

                fbaResults = TableReader(os.path.join(simOutDir, "FBAResults"))
                reactionIDs = np.array(fbaResults.readAttribute("reactionIDs"))
                reactionFluxes = np.array(
                    fbaResults.readColumn("reactionFluxes"))
                enzymeFluxes += reactionFluxes[:,
                                               np.where(
                                                   reactionIDs ==
                                                   reactionId)[0][0]].tolist()
                fbaResults.close()

                rnapDataReader = TableReader(
                    os.path.join(simOutDir, "RnapData"))
                rnaInitEventsInThisGen = rnapDataReader.readColumn(
                    "rnaInitEvent")[:,
                                    np.where(
                                        rnaIds == enzymeRnaId)[0][0]].tolist()
                rnapDataReader.close()

                enzymeRnaInitEvent += rnaInitEventsInThisGen
                nTranscriptionInitEventsPerGen.append(
                    np.sum(rnaInitEventsInThisGen))

            time = np.array(time)
            cPickle.dump(
                {
                    "time": time,
                    "enzymeRnaInitEvent": enzymeRnaInitEvent,
                    "enzymeRnaCounts": enzymeRnaCounts,
                    "enzymeMonomerCounts": enzymeMonomerCounts,
                    "enzymeComplexCounts": enzymeComplexCounts,
                    "enzymeFluxes": enzymeFluxes,
                    "metaboliteCounts": metaboliteCounts,
                    "dryMass": dryMass,
                    "cellMass": cellMass,
                    "timeStepSec": timeStepSec,
                    "generationTicks": generationTicks,
                    "nTranscriptionInitEventsPerGen":
                    nTranscriptionInitEventsPerGen,  # storing value to report in paper
                    "nAvgTetramersPerGen":
                    nAvgTetramersPerGen,  # storing value to report in paper
                },
                open(os.path.join(plotOutDir, "figure5D.pickle"), "wb"))
        else:
            D = cPickle.load(
                open(os.path.join(plotOutDir, "figure5D.pickle"), "r"))
            time = D["time"]
            enzymeRnaInitEvent = D["enzymeRnaInitEvent"]
            enzymeRnaCounts = D["enzymeRnaCounts"]
            enzymeMonomerCounts = D["enzymeMonomerCounts"]
            enzymeComplexCounts = D["enzymeComplexCounts"]
            enzymeFluxes = D["enzymeFluxes"]
            metaboliteCounts = D["metaboliteCounts"]
            dryMass = D["dryMass"]
            cellMass = D["cellMass"]
            timeStepSec = D["timeStepSec"]
            generationTicks = D["generationTicks"]

        cellVolume = units.g * np.array(cellMass) / cellDensity
        coefficient = (units.fg * np.array(dryMass)) / (
            units.fg * np.array(cellMass)) * cellDensity * (timeStepSec *
                                                            units.s)
        enzymeFluxes = (((COUNTS_UNITS / VOLUME_UNITS) * enzymeFluxes) /
                        coefficient).asNumber(units.mmol / units.g / units.h)

        averages = []
        indices = [np.where(time == x)[0][0] for x in generationTicks]
        for x in np.arange(len(indices) - 1):
            avg = np.average(enzymeComplexCounts[indices[x]:indices[x + 1]])
            averages.append(avg)

        # Plot
        fig = plt.figure(figsize=(11, 8.5))
        plt.suptitle("O-succinylbenzoate-CoA ligase downstream behaviors",
                     fontsize=FONTSIZE)
        rnaInitAxis = plt.subplot(6, 1, 1)
        rnaAxis = plt.subplot(6, 1, 2, sharex=rnaInitAxis)
        monomerAxis = plt.subplot(6, 1, 3, sharex=rnaInitAxis)
        complexAxis = plt.subplot(6, 1, 4, sharex=rnaInitAxis)
        fluxAxis = plt.subplot(6, 1, 5, sharex=rnaInitAxis)
        metAxis = plt.subplot(6, 1, 6)

        rnaInitLine = rnaInitAxis.plot(time / 3600., enzymeRnaInitEvent, c="b")
        rnaInitAxis.set_ylabel(r"$menE$" + "\n transcription\nevents",
                               fontsize=FONTSIZE,
                               rotation=0)
        rnaInitAxis.yaxis.set_label_coords(-.1, 0.25)
        rnaInitAxis.set_xlim([time[0] / 3600., time[-1] / 3600.])
        whitePadSparklineAxis(rnaInitAxis, xAxis=False)
        rnaInitAxis.set_yticks([0, 1])

        rnaLine = rnaAxis.plot(time / 3600., enzymeRnaCounts, c="b")
        rnaAxis.set_ylabel("menE mRNA\ncounts", fontsize=FONTSIZE, rotation=0)
        rnaAxis.yaxis.set_label_coords(-.1, 0.25)
        whitePadSparklineAxis(rnaAxis, xAxis=False)
        rnaAxis.set_yticks([0, max(enzymeRnaCounts)])

        monomerLine = monomerAxis.plot(time / 3600.,
                                       enzymeMonomerCounts,
                                       c="b")
        monomerAxis.set_ylabel("MenE monomer\ncounts",
                               fontsize=FONTSIZE,
                               rotation=0)
        monomerAxis.yaxis.set_label_coords(-.1, 0.25)
        whitePadSparklineAxis(monomerAxis, xAxis=False)
        monomerAxis.set_yticks([0, 4, max(enzymeMonomerCounts)])

        complexLine = complexAxis.plot(time / 3600.,
                                       enzymeComplexCounts,
                                       c="b")
        complexAxis.set_ylabel("MenE tetramer\ncounts",
                               fontsize=FONTSIZE,
                               rotation=0)
        complexAxis.yaxis.set_label_coords(-.1, 0.25)
        whitePadSparklineAxis(complexAxis, xAxis=False)
        complexAxis.set_yticks([0, max(enzymeComplexCounts)])

        fluxLine = fluxAxis.plot(time / 3600., enzymeFluxes, c="b")
        fluxAxis.set_ylabel("SUCBZL flux\n(mmol/gDCW/hour)",
                            fontsize=FONTSIZE,
                            rotation=0)
        fluxAxis.yaxis.set_label_coords(-.1, 0.25)
        whitePadSparklineAxis(fluxAxis, xAxis=False)
        fluxAxis.set_yticks([min(enzymeFluxes), max(enzymeFluxes)])

        metLine = metAxis.plot(time / 3600.,
                               np.sum(metaboliteCounts, axis=1),
                               c="b")
        metAxis.set_ylabel("End product\ncounts",
                           fontsize=FONTSIZE,
                           rotation=0)
        metAxis.yaxis.set_label_coords(-.1, 0.25)
        metAxis.set_xlabel("Time (hour)\ntickmarks at each new generation",
                           fontsize=FONTSIZE)
        metAxis.set_ylim([metAxis.get_ylim()[0] * 0.2, metAxis.get_ylim()[1]])
        metAxis.set_xlim([time[0] / 3600., time[-1] / 3600.])
        whitePadSparklineAxis(metAxis)
        metAxis.set_yticklabels(
            ["%0.1e" % metAxis.get_ylim()[0],
             "%0.1e" % metAxis.get_ylim()[1]])
        metAxis.set_xticks(np.array(generationTicks) / 3600.)
        xticklabels = np.repeat("     ", len(generationTicks))
        xticklabels[0] = "0"
        xticklabels[-1] = "%0.2f" % (time[-1] / 3600.)
        metAxis.set_xticklabels(xticklabels)

        noComplexIndexes = np.where(np.array(enzymeComplexCounts) == 0)[0]
        patchStart = []
        patchEnd = []
        if len(noComplexIndexes):
            prev = noComplexIndexes[0]
            patchStart.append(prev)
            for i in noComplexIndexes:
                if np.abs(i - prev) > 1:
                    patchStart.append(i)
                    patchEnd.append(prev)
                prev = i
            patchEnd.append(prev)

        axesList = [
            rnaInitAxis, rnaAxis, monomerAxis, complexAxis, fluxAxis, metAxis
        ]
        for axis in axesList:
            axis.tick_params(labelsize=LABELSIZE)
            for i in xrange(len(patchStart)):
                width = time[patchEnd[i]] / 3600. - time[patchStart[i]] / 3600.
                if width <= 0.1:
                    continue

                height = axis.get_ylim()[1] - axis.get_ylim()[0]
                axis.add_patch(
                    patches.Rectangle(
                        (time[patchStart[i]] / 3600., axis.get_ylim()[0]),
                        width,
                        height,
                        alpha=0.25,
                        color="gray",
                        linewidth=0.))

        plt.subplots_adjust(hspace=0.5,
                            right=0.9,
                            bottom=0.1,
                            left=0.15,
                            top=0.9)
        exportFigure(plt, plotOutDir, plotOutFileName, metadata)

        axesList = [
            rnaInitAxis, rnaAxis, monomerAxis, complexAxis, fluxAxis, metAxis
        ]

        for a in axesList:
            clearLabels(a)
        plt.suptitle("")
        metAxis.set_xticklabels([])
        metAxis.set_xlabel("")
        exportFigure(plt, plotOutDir, plotOutFileName + "__clean", "")
        plt.close("all")

        if PLOT_DOWNSTREAM:
            fig, axesList = plt.subplots(12, figsize=(11, 8.5))
            plt.subplots_adjust(hspace=0.5,
                                right=0.95,
                                bottom=0.05,
                                left=0.15,
                                top=0.95)
            enzymeIds = [
                "MENE-CPLX[c]", "CPLX0-7882[c]", "CPLX0-8128[c]",
                "DMK-MONOMER[i]", "2-OCTAPRENYL-METHOXY-BENZOQ-METH-MONOMER[c]"
            ]
            reactionIds = [
                "O-SUCCINYLBENZOATE-COA-LIG-RXN", "NAPHTHOATE-SYN-RXN",
                "RXN-9311", "DMK-RXN", "ADOMET-DMK-METHYLTRANSFER-RXN"
            ]
            reactantIds = ["CPD-12115[c]"]
            enzymeIndexes = [moleculeIds.index(x) for x in enzymeIds]
            reactantIndexes = [moleculeIds.index(x) for x in reactantIds]

            for gen, simDir in enumerate(allDir):
                simOutDir = os.path.join(simDir, "simOut")

                time_ = TableReader(os.path.join(simOutDir,
                                                 "Main")).readColumn("time")
                timeStepSec = TableReader(os.path.join(
                    simOutDir, "Main")).readColumn("timeStepSec")
                cellMass = TableReader(os.path.join(
                    simOutDir, "Mass")).readColumn("cellMass")
                dryMass = TableReader(os.path.join(
                    simOutDir, "Mass")).readColumn("dryMass")

                bulkMolecules = TableReader(
                    os.path.join(simOutDir, "BulkMolecules"))
                moleculeCounts = bulkMolecules.readColumn("counts")
                enzymeCounts = moleculeCounts[:, enzymeIndexes]
                metCounts = moleculeCounts[:, metaboliteIndexes[0]]
                reactantCounts = moleculeCounts[:, reactantIndexes]
                bulkMolecules.close()

                fbaResults = TableReader(os.path.join(simOutDir, "FBAResults"))
                reactionIDs = np.array(
                    fbaResults.readAttribute("reactionIDs")).tolist()
                reactionIndexes = [reactionIDs.index(x) for x in reactionIds]
                reactionFluxes = np.array(
                    fbaResults.readColumn("reactionFluxes"))
                enzymeFluxes = reactionFluxes[:, reactionIndexes]
                fbaResults.close()

                cellVolume = units.g * np.array(cellMass) / cellDensity
                coefficient = (units.fg * np.array(dryMass)) / (
                    units.fg *
                    np.array(cellMass)) * cellDensity * (units.s * timeStepSec)

                for i, row in enumerate(xrange(0, 2 * len(enzymeIds), 2)):
                    countAxis = axesList[row]
                    fluxAxis = axesList[row + 1]
                    plotFlux = (
                        ((COUNTS_UNITS / VOLUME_UNITS) * enzymeFluxes[:, i]) /
                        coefficient).asNumber(units.mmol / units.g / units.h)
                    countAxis.plot(time_ / 3600.,
                                   enzymeCounts[:, i],
                                   color="b")
                    fluxAxis.plot(time_ / 3600., plotFlux, color="b")
                axesList[-2].plot(time_ / 3600., reactantCounts, color="b")
                axesList[-1].plot(time_ / 3600., metCounts, color="b")

            ylabels = [
                "menE", "menB", "menI", "menA", "ubiE", "CPD-12115",
                "Menaquinone"
            ]
            for i, axis in enumerate(axesList[::2]):
                axis.set_xlim([0, time_[-1] / 3600.])
                axis.set_ylabel("%s" % ylabels[i], rotation=0)
                whitePadSparklineAxis(axis, False)
            for axis in axesList[1::2]:
                axis.set_xlim([0, time_[-1] / 3600.])
                whitePadSparklineAxis(axis)
            axesList[-1].set_ylabel(ylabels[-1], rotation=0)

            for axis in axesList:
                for i in xrange(len(patchStart)):
                    width = time[patchEnd[i]] / 3600. - time[
                        patchStart[i]] / 3600.
                    if width <= 0.1:
                        continue

                    height = axis.get_ylim()[1] - axis.get_ylim()[0]
                    axis.add_patch(
                        patches.Rectangle(
                            (time[patchStart[i]] / 3600., axis.get_ylim()[0]),
                            width,
                            height,
                            alpha=0.25,
                            color="gray",
                            linewidth=0.))

            plt.subplots_adjust(hspace=0.5,
                                right=0.95,
                                bottom=0.05,
                                left=0.11,
                                top=0.95)
            exportFigure(plt, plotOutDir,
                         plotOutFileName + "__downstreamFluxes", metadata)
    def do_plot(self, variantDir, plotOutDir, plotOutFileName, simDataFile,
                validationDataFile, metadata):
        if not os.path.isdir(variantDir):
            raise Exception, "variantDir does not currently exist as a directory"

        if not os.path.exists(plotOutDir):
            os.mkdir(plotOutDir)

        # Get all cells
        ap = AnalysisPaths(variantDir, cohort_plot=True)
        allDir = ap.get_cells()

        sim_data = cPickle.load(open(simDataFile, "rb"))

        constraintIsKcatOnly = sim_data.process.metabolism.constraintIsKcatOnly

        targetFluxList = []
        actualFluxList = []
        reactionConstraintlist = []

        for simDir in allDir:
            simOutDir = os.path.join(simDir, "simOut")

            try:
                mainListener = TableReader(os.path.join(simOutDir, "Main"))
            except Exception as e:
                print(e)
                continue
            time = mainListener.readColumn("time")
            mainListener.close()

            # skip if no data
            if time.shape is ():
                continue
            burnIn = time > BURN_IN_TIME
            burnIn[0] = False

            massListener = TableReader(os.path.join(simOutDir, "Mass"))
            cellMass = massListener.readColumn("cellMass")
            dryMass = massListener.readColumn("dryMass")
            massListener.close()

            coefficient = dryMass / cellMass * sim_data.constants.cellDensity.asNumber(
                MASS_UNITS / VOLUME_UNITS)

            # read constraint data
            enzymeKineticsReader = TableReader(
                os.path.join(simOutDir, "EnzymeKinetics"))
            targetFluxes = (COUNTS_UNITS / MASS_UNITS / TIME_UNITS) * (
                enzymeKineticsReader.readColumn("targetFluxes").T /
                coefficient).T
            actualFluxes = (COUNTS_UNITS / MASS_UNITS / TIME_UNITS) * (
                enzymeKineticsReader.readColumn("actualFluxes").T /
                coefficient).T
            reactionConstraint = enzymeKineticsReader.readColumn(
                "reactionConstraint")
            constrainedReactions = np.array(
                enzymeKineticsReader.readAttribute("constrainedReactions"))
            enzymeKineticsReader.close()

            targetFluxes = targetFluxes.asNumber(units.mmol / units.g /
                                                 units.h)
            actualFluxes = actualFluxes.asNumber(units.mmol / units.g /
                                                 units.h)

            targetAve = np.nanmean(targetFluxes[burnIn, :], axis=0)
            actualAve = np.nanmean(actualFluxes[burnIn, :], axis=0)

            if len(targetFluxList) == 0:
                targetFluxList = np.array([targetAve])
                actualFluxList = np.array([actualAve])
                reactionConstraintList = np.array(
                    reactionConstraint[burnIn, :])
            else:
                targetFluxList = np.concatenate(
                    (targetFluxList, np.array([targetAve])), axis=0)
                actualFluxList = np.concatenate(
                    (actualFluxList, np.array([actualAve])), axis=0)
                reactionConstraintList = np.concatenate(
                    (reactionConstraintList,
                     np.array(reactionConstraint[burnIn, :])),
                    axis=0)

        # determine average across all cells
        targetAve = np.nanmean(targetFluxList, axis=0)
        actualAve = np.nanmean(actualFluxList, axis=0)

        # categorize reactions that use constraints with only kcat, Km and kcat, or switch between both types of constraints
        kcatOnlyReactions = np.all(
            constraintIsKcatOnly[reactionConstraintList], axis=0)
        kmAndKcatReactions = ~np.any(
            constraintIsKcatOnly[reactionConstraintList], axis=0)
        mixedReactions = ~(kcatOnlyReactions ^ kmAndKcatReactions)

        # categorize how well the actual flux matches the target flux
        thresholds = [2, 10]
        categorization = np.zeros(reactionConstraint.shape[1])
        for i, threshold in enumerate(thresholds):
            categorization[actualAve / targetAve < 1. / threshold] = i + 1
            categorization[actualAve / targetAve > threshold] = i + 1
        categorization[actualAve == 0] = -2
        categorization[actualAve == targetAve] = -1

        # write data for each reaction to a file
        csvFile = open(os.path.join(plotOutDir, plotOutFileName + ".tsv"),
                       "wb")
        output = csv.writer(csvFile, delimiter="\t")
        output.writerow(["Km and kcat", "Target", "Actual", "Category"])
        for reaction, target, flux, category in zip(
                constrainedReactions[kmAndKcatReactions],
                targetAve[kmAndKcatReactions], actualAve[kmAndKcatReactions],
                categorization[kmAndKcatReactions]):
            output.writerow([reaction, target, flux, category])

        output.writerow(["kcat only"])
        for reaction, target, flux, category in zip(
                constrainedReactions[kcatOnlyReactions],
                targetAve[kcatOnlyReactions], actualAve[kcatOnlyReactions],
                categorization[kcatOnlyReactions]):
            output.writerow([reaction, target, flux, category])

        if np.sum(mixedReactions):
            output.writerow(["mixed constraints"])
            for reaction, target, flux, category in zip(
                    constrainedReactions[mixedReactions],
                    targetAve[mixedReactions], actualAve[mixedReactions],
                    categorization[mixedReactions]):
                output.writerow([reaction, target, flux, category])

        csvFile.close()

        # add small number to allow plotting of 0 flux on log scale
        targetAve += 1e-6
        actualAve += 1e-6

        pearsonAll = pearsonr(np.log10(targetAve), np.log10(actualAve))
        pearsonNoZeros = pearsonr(np.log10(targetAve[(categorization != -2)]),
                                  np.log10(actualAve[(categorization != -2)]))

        # plot data
        plt.figure(figsize=(4, 4))
        ax = plt.axes()
        plt.plot([-6, 4], [-6, 4], 'k', linewidth=0.75)
        plt.plot([-5, 4], [-6, 3], 'k', linewidth=0.5)
        plt.plot([-6, 3], [-5, 4], 'k', linewidth=0.5)
        plt.plot(np.log10(targetAve),
                 np.log10(actualAve),
                 'o',
                 color="black",
                 markersize=8,
                 alpha=0.15,
                 zorder=1,
                 markeredgewidth=0.0)
        plt.xlabel("Log10(Target Flux [mmol/g/hr])")
        plt.ylabel("Log10(Actual Flux [mmol/g/hr])")
        plt.title("PCC = %.3f, p = %s\n(%.3f, p = %s without points at zero)" %
                  (pearsonAll[0], pearsonAll[1], pearsonNoZeros[0],
                   pearsonNoZeros[1]))
        plt.minorticks_off()
        whitePadSparklineAxis(ax)
        xlim = ax.get_xlim()
        ylim = ax.get_ylim()
        ax.set_ylim(ylim[0] - 0.5, ylim[1])
        ax.set_xlim(xlim[0] - 0.5, xlim[1])
        ax.set_yticks(range(-6, int(ylim[1]) + 1, 2))
        ax.set_xticks(range(-6, int(xlim[1]) + 1, 2))

        exportFigure(plt, plotOutDir, plotOutFileName)

        ax.set_xlabel("")
        ax.set_ylabel("")
        ax.set_title("")
        ax.set_xticklabels([])
        ax.set_yticklabels([])

        exportFigure(plt, plotOutDir, plotOutFileName + "_stripped", metadata)
        plt.close("all")
    def do_plot(self, inputDir, plotOutDir, plotOutFileName, simDataFile,
                validationDataFile, metadata):
        if metadata["variant"] != "condition":
            print("This plot only runs for the 'condition' variant.")
            return

        if not os.path.isdir(inputDir):
            raise Exception, 'inputDir does not currently exist as a directory'

        filepath.makedirs(plotOutDir)

        ap = AnalysisPaths(inputDir, variant_plot=True)
        variants = ap.get_variants()

        gens = [2, 3]

        initial_volumes = []
        added_volumes = []

        for variant in variants:
            with open(ap.get_variant_kb(variant), 'rb') as f:
                sim_data = cPickle.load(f)

            cell_density = sim_data.constants.cellDensity

            initial_masses = np.zeros(0)
            final_masses = np.zeros(0)

            all_cells = ap.get_cells(variant=[variant], generation=gens)

            if len(all_cells) == 0:
                continue

            for simDir in all_cells:
                try:
                    simOutDir = os.path.join(simDir, "simOut")
                    mass = TableReader(os.path.join(simOutDir, "Mass"))
                    cellMass = mass.readColumn("cellMass")

                    initial_masses = np.hstack((initial_masses, cellMass[0]))
                    final_masses = np.hstack((final_masses, cellMass[-1]))
                except:
                    continue

            added_masses = final_masses - initial_masses

            initial_volume = initial_masses / cell_density.asNumber(
                units.fg / units.um**3)
            added_volume = added_masses / cell_density.asNumber(
                units.fg / units.um**3)

            initial_volumes.append(initial_volume)
            added_volumes.append(added_volume)

        plt.style.use('seaborn-deep')
        color_cycle = plt.rcParams['axes.prop_cycle'].by_key()['color']

        plt.figure(figsize=(4, 4))
        ax = plt.subplot2grid((1, 1), (0, 0))

        options = {
            "edgecolors": color_cycle[0],
            "alpha": 0.2,
            "s": 50,
            "clip_on": False
        }
        labels = ["minimal", "anaerobic", "minimal + AA"]

        ax.scatter(initial_volumes[2],
                   added_volumes[2],
                   marker="x",
                   label=labels[2],
                   **options)
        ax.scatter(initial_volumes[0],
                   added_volumes[0],
                   facecolors="none",
                   marker="o",
                   label=labels[0],
                   **options)
        ax.scatter(initial_volumes[1],
                   added_volumes[1],
                   facecolors="none",
                   marker="^",
                   label=labels[1],
                   **options)

        ax.set_xlim([0, 4])
        ax.set_ylim([0, 4])
        ax.set_xlabel("Birth Volume ($\mu m^3$)")
        ax.set_ylabel("Added Volume ($\mu m^3$)")
        ax.legend()

        ax.get_yaxis().get_major_formatter().set_useOffset(False)
        ax.get_xaxis().get_major_formatter().set_useOffset(False)

        whitePadSparklineAxis(ax)

        ax.tick_params(which='both',
                       bottom=True,
                       left=True,
                       top=False,
                       right=False,
                       labelbottom=True,
                       labelleft=True)

        plt.tight_layout()
        exportFigure(plt, plotOutDir, plotOutFileName, metadata)

        # Get clean version of plot
        ax.set_xlabel("")
        ax.set_ylabel("")
        ax.set_yticklabels([])
        ax.set_xticklabels([])
        exportFigure(plt, plotOutDir, plotOutFileName + "_clean", metadata)

        plt.close("all")
	def do_plot(self, inputDir, plotOutDir, plotOutFileName, simDataFile, validationDataFile, metadata):
		if not os.path.isdir(inputDir):
			raise Exception, "variantDir does not currently exist as a directory"

		if not os.path.exists(plotOutDir):
			os.mkdir(plotOutDir)

		ap = AnalysisPaths(inputDir, variant_plot = True)

		if ap.n_generation == 1:
			print "Need more data to create plot"
			return

		fig = plt.figure()
		fig.set_figwidth(15)
		fig.set_figheight(5)

		doublingTimeVariants = [44, 100, 22]

		for varIdx in range(ap.n_variant):

			if varIdx == 0:
				plotIdx = 1
				gen = [2,3]
			elif varIdx == 1:
				plotIdx = 0
				gen = [2,3]
			elif varIdx == 2:
				plotIdx = 2
				gen = [6,7]
			else:
				continue

			initial_masses = np.zeros(0)
			final_masses = np.zeros(0)

			all_cells = ap.get_cells(generation=[2,3], variant=[varIdx])
			if len(all_cells) == 0:
				continue

			doublingTimes = np.zeros(len(all_cells))
			for idx, simDir in enumerate(all_cells):
				try:
					simOutDir = os.path.join(simDir, "simOut")
					time = TableReader(os.path.join(simOutDir, "Main")).readColumn("time")
				except Exception as e:
					print 'Error reading data for %s; %s' % (simDir, e)

				doublingTimes[idx] = (time[-1] - time[0]) / 60.

			bins = 16
			ax = plt.subplot2grid((1, 3), (0, plotIdx))
			ax.hist(doublingTimes, bins)
			ax.axvline(x = doublingTimeVariants[varIdx], color = "r", linestyle = "--")

			ax.set_title("%i min" % (doublingTimeVariants[varIdx]), fontsize = FONT_SIZE)

			ax.set_xlabel("Doubling Time (min)", fontsize = FONT_SIZE)

			plt.subplots_adjust(bottom = 0.2)

			whitePadSparklineAxis(ax)

			for tick in ax.yaxis.get_major_ticks():
				tick.label.set_fontsize(FONT_SIZE)
			for tick in ax.xaxis.get_major_ticks():
				tick.label.set_fontsize(FONT_SIZE)

		exportFigure(plt, plotOutDir, plotOutFileName, metadata)
	def do_plot(self, simOutDir, plotOutDir, plotOutFileName, simDataFile, validationDataFile, metadata):
		if not os.path.isdir(simOutDir):
			raise Exception, "simOutDir does not currently exist as a directory"

		if not os.path.exists(plotOutDir):
			os.mkdir(plotOutDir)

		sim_data = cPickle.load(open(simDataFile))

		constraintIsKcatOnly = sim_data.process.metabolism.constraintIsKcatOnly

		mainListener = TableReader(os.path.join(simOutDir, "Main"))
		initialTime = mainListener.readAttribute("initialTime")
		time = mainListener.readColumn("time") - initialTime
		mainListener.close()

		massListener = TableReader(os.path.join(simOutDir, "Mass"))
		cellMass = massListener.readColumn("cellMass")
		dryMass = massListener.readColumn("dryMass")
		massListener.close()

		coefficient = dryMass / cellMass * sim_data.constants.cellDensity.asNumber(MASS_UNITS / VOLUME_UNITS)

		# read constraint data
		enzymeKineticsReader = TableReader(os.path.join(simOutDir, "EnzymeKinetics"))
		targetFluxes = (COUNTS_UNITS / MASS_UNITS / TIME_UNITS) * (enzymeKineticsReader.readColumn("targetFluxes").T / coefficient).T
		actualFluxes = (COUNTS_UNITS / MASS_UNITS / TIME_UNITS) * (enzymeKineticsReader.readColumn("actualFluxes").T / coefficient).T
		reactionConstraint = enzymeKineticsReader.readColumn("reactionConstraint")
		constrainedReactions = np.array(enzymeKineticsReader.readAttribute("constrainedReactions"))
		enzymeKineticsReader.close()

		targetFluxes = targetFluxes.asNumber(units.mmol / units.g / units.h)
		actualFluxes = actualFluxes.asNumber(units.mmol / units.g / units.h)

		targetAve = np.mean(targetFluxes[BURN_IN_STEPS:, :], axis = 0)
		actualAve = np.mean(actualFluxes[BURN_IN_STEPS:, :], axis = 0)

		kcatOnlyReactions = np.all(constraintIsKcatOnly[reactionConstraint[BURN_IN_STEPS:,:]], axis = 0)
		kmAndKcatReactions = ~np.any(constraintIsKcatOnly[reactionConstraint[BURN_IN_STEPS:,:]], axis = 0)
		mixedReactions = ~(kcatOnlyReactions ^ kmAndKcatReactions)

		thresholds = [2, 10]
		categorization = np.zeros(reactionConstraint.shape[1])
		categorization[actualAve == 0] = -2
		categorization[actualAve == targetAve] = -1
		for i, threshold in enumerate(thresholds):
			# categorization[targetAve / actualAve > threshold] = i + 1
			categorization[actualAve / targetAve > threshold] = i + 1

		# url for ecocyc to highlight fluxes that are 0 on metabolic network diagram
		siteStr = "https://ecocyc.org/overviewsWeb/celOv.shtml?zoomlevel=1&orgid=ECOLI"
		excluded = ['RXN0-2201', 'RXN-16000', 'RXN-12583', 'RXN-11496', 'DIMESULFREDUCT-RXN', '3.6.1.41-R[4/63051]5-NUCLEOTID-RXN'] # reactions not recognized by ecocyc
		rxns = []
		for i, reaction in enumerate(constrainedReactions):
			if actualAve[i] == 0:
				rxn = re.findall(".+RXN", reaction)
				if len(rxn) == 0:
					rxn = re.findall("RXN[^-]*-[0-9]+", reaction)
				if rxn[0] not in excluded:
					siteStr += "&rnids=%s" % rxn[0]
				rxns.append(rxn[0])
		# print siteStr

		csvFile = open(os.path.join(plotOutDir, plotOutFileName + ".tsv"), "wb")
		output = csv.writer(csvFile, delimiter = "\t")
		output.writerow(["ecocyc link:", siteStr])
		output.writerow(["Km and kcat", "Target", "Actual", "Category"])
		for reaction, target, flux, category in zip(constrainedReactions[kmAndKcatReactions], targetAve[kmAndKcatReactions], actualAve[kmAndKcatReactions], categorization[kmAndKcatReactions]):
			output.writerow([reaction, target, flux, category])

		output.writerow(["kcat only"])
		for reaction, target, flux, category in zip(constrainedReactions[kcatOnlyReactions], targetAve[kcatOnlyReactions], actualAve[kcatOnlyReactions], categorization[kcatOnlyReactions]):
			output.writerow([reaction, target, flux, category])

		if np.sum(mixedReactions):
			output.writerow(["mixed constraints"])
			for reaction, target, flux, category in zip(constrainedReactions[mixedReactions], targetAve[mixedReactions], actualAve[mixedReactions], categorization[mixedReactions]):
				output.writerow([reaction, target, flux, category])

		csvFile.close()

		targetAve += 1e-6
		actualAve += 1e-6

		axes_limits = [1e-7, 1e4]
		plt.figure(figsize = (8, 8))
		ax = plt.axes()
		plt.loglog(axes_limits, axes_limits, 'k')
		plt.loglog(targetAve, actualAve, "ob", markeredgewidth = 0.25, alpha = 0.25)
		plt.xlabel("Target Flux (mmol/g/hr)")
		plt.ylabel("Actual Flux (mmol/g/hr)")
		plt.minorticks_off()
		whitePadSparklineAxis(ax)
		ax.set_ylim(axes_limits)
		ax.set_xlim(axes_limits)
		ax.set_yticks(axes_limits)
		ax.set_xticks(axes_limits)

		exportFigure(plt, plotOutDir, plotOutFileName)
		plt.close("all")

		source = ColumnDataSource(
			data = dict(
				x = targetAve,
				y = actualAve,
				reactionName = constrainedReactions)
			)

		hover = HoverTool(
			tooltips = [
				("Reaction", "@reactionName"),
				]
			)

		TOOLS = [hover,
			BoxZoomTool(),
			LassoSelectTool(),
			PanTool(),
			WheelZoomTool(),
			ResizeTool(),
			UndoTool(),
			RedoTool(),
			"reset",
			]

		p1 = figure(x_axis_label = "Target",
			x_axis_type = "log",
			x_range = [min(targetAve[targetAve > 0]), max(targetAve)],
			y_axis_label = "Actual",
			y_axis_type = "log",
			y_range = [min(actualAve[actualAve > 0]), max(actualAve)],
			width = 800,
			height = 800,
			tools = TOOLS,
			)

		p1.scatter(targetAve, actualAve, source = source, size = 8)
		p1.line([1e-15, 10], [1e-15, 10], line_color = "red", line_dash = "dashed")


		## bar plot of error
		# sortedReactions = [constrainedReactions[x] for x in np.argsort(aveError)[::-1]]
		# aveError[np.log10(aveError) == -np.inf] = 0

		# source = ColumnDataSource(
		# 	data = dict(
		# 			x = sorted(relError, reverse = True),
		# 			reactionName = sortedReactions
		# 		)
		# 	)

		# p2 = Bar(data, values = "x")

		# hover2 = p2.select(dict(type=HoverTool))
		# hover2.tooltips = [("Reaction", "@reactionName")]

		## flux for each reaction
		hover2 = HoverTool(
			tooltips = [
				("Reaction", "@reactionName"),
				]
			)

		TOOLS2 = [hover2,
			BoxZoomTool(),
			LassoSelectTool(),
			PanTool(),
			WheelZoomTool(),
			ResizeTool(),
			UndoTool(),
			RedoTool(),
			"reset",
			]

		p2 = figure(x_axis_label = "Time(s)",
			y_axis_label = "Flux",
			y_axis_type = "log",
			y_range = [1e-8, 1],
			width = 800,
			height = 800,
			tools = TOOLS2,
			)

		colors = COLORS_LARGE
		nTimesteps = len(time[BURN_IN_STEPS:])
		x = time[BURN_IN_STEPS:]
		y = actualFluxes[BURN_IN_STEPS:, 0]
		reactionName = np.repeat(constrainedReactions[0], nTimesteps)

		source = ColumnDataSource(
			data = dict(
				x = x,
				y = y,
				reactionName = reactionName)
			)

		p2.line(x, y, line_color = colors[0], source = source)

		# Plot remaining metabolites onto initialized figure
		for m in np.arange(1, actualFluxes.shape[1]):
			y = actualFluxes[BURN_IN_STEPS:, m]
			reactionName = np.repeat(constrainedReactions[m], nTimesteps)

			source = ColumnDataSource(
				data = dict(
					x = x,
					y = y,
					reactionName = reactionName)
			)

			p2.line(x, y, line_color = colors[m % len(colors)], source = source)

		if not os.path.exists(os.path.join(plotOutDir, "html_plots")):
			os.makedirs(os.path.join(plotOutDir, "html_plots"))

		p = bokeh.io.vplot(p1, p2)
		bokeh.io.output_file(os.path.join(plotOutDir, "html_plots", plotOutFileName + ".html"), title=plotOutFileName, autosave=False)
		bokeh.io.save(p)
		bokeh.io.curstate().reset()
    def do_plot(self, variantDir, plotOutDir, plotOutFileName, simDataFile,
                validationDataFile, metadata):
        if not os.path.isdir(variantDir):
            raise Exception, "variantDir does not currently exist as a directory"

        if not os.path.exists(plotOutDir):
            os.mkdir(plotOutDir)

        sim_data = cPickle.load(open(simDataFile, "rb"))
        oriC = sim_data.constants.oriCCenter.asNumber()
        terC = sim_data.constants.terCCenter.asNumber()
        genomeLength = len(sim_data.process.replication.genome_sequence)

        mult = 2.3
        fig = plt.figure(figsize=(6, 6))
        #fig.set_figwidth(mm2inch(97)*mult)
        #fig.set_figheight(mm2inch(58)*mult)

        ax0 = plt.subplot2grid((3, 4), (0, 0), colspan=4)
        ax1 = plt.subplot2grid((3, 4), (1, 0), colspan=4, sharex=ax0)
        ax2 = plt.subplot2grid((3, 4), (2, 0), colspan=4, sharex=ax0)
        ax0_xlim = ax0.get_xlim()
        #ax3 = plt.subplot2grid((5,7), (3,0), colspan = 4, sharex=ax0)
        #ax4 = plt.subplot2grid((5,7), (4,0), colspan = 4, sharex=ax0)

        # Get all cells in each seed
        ap = AnalysisPaths(variantDir, cohort_plot=True)
        #all_cells = ap.get_cells(seed=[0,1,2,3])
        all_cells = ap.get_cells(seed=[4])

        if not len(all_cells):
            return

        for idx, simDir in enumerate(all_cells):
            #color = "black"
            color = "#0d71b9"
            alpha = 1
            if idx % 2:
                color = "#0d71b9"
                blue = 0.8

            simOutDir = os.path.join(simDir, "simOut")

            time = TableReader(os.path.join(simOutDir,
                                            "Main")).readColumn("time")

            ## Cell mass
            mass = TableReader(os.path.join(simOutDir, "Mass"))
            cellMass = mass.readColumn("cellMass")
            massPerOric = TableReader(
                os.path.join(
                    simOutDir,
                    "ReplicationData")).readColumn("criticalMassPerOriC")
            idxInit = np.where(massPerOric >= 1)[0]
            ax0.plot(time / 60.,
                     cellMass,
                     color=color,
                     alpha=alpha,
                     linewidth=2)
            ax0.plot(time[idxInit] / 60.,
                     cellMass[idxInit],
                     markersize=6,
                     linewidth=0,
                     marker="o",
                     color="#ed2224",
                     markeredgewidth=0)

            ## Inst. growth rate
            growthRate = mass.readColumn("instantaniousGrowthRate")
            growthRate = (1 / units.s) * growthRate
            growthRate = growthRate.asNumber(1 / units.min)
            growthRate[abs(growthRate - np.median(growthRate)) > 1.25 *
                       np.nanstd(growthRate)] = np.nan
            ax1.plot(time / 60., growthRate, color=color, alpha=alpha)

            ## Rna over protein
            # Get active ribosome counts
            uniqueMoleculeCounts = TableReader(
                os.path.join(simOutDir, "UniqueMoleculeCounts"))
            ribosomeIndex = uniqueMoleculeCounts.readAttribute(
                "uniqueMoleculeIds").index("activeRibosome")
            ribosomeCounts = uniqueMoleculeCounts.readColumn(
                "uniqueMoleculeCounts")[:, ribosomeIndex]
            uniqueMoleculeCounts.close()
            ribosomeConcentration = (
                (1 / sim_data.constants.nAvogadro) * ribosomeCounts) / (
                    (1.0 / sim_data.constants.cellDensity) *
                    (units.fg * cellMass))
            ribosomeConcentration = ribosomeConcentration.asNumber(units.umol /
                                                                   units.L)
            ax2.plot(time / 60.,
                     ribosomeConcentration,
                     color=color,
                     alpha=alpha,
                     linewidth=2)
            ax2.set_ylim([18., 28.])
            # rnaMass = mass.readColumn("rnaMass")
            # proteinMass = mass.readColumn("proteinMass")
            # rnaOverProtein = rnaMass / proteinMass
            # ax2.plot(time / 60., rnaOverProtein, color = color, alpha = alpha, linewidth=2)

            ## Fork position
            sequenceIdx = TableReader(
                os.path.join(simOutDir,
                             "ReplicationData")).readColumn("sequenceIdx")
            sequenceLength = TableReader(
                os.path.join(simOutDir,
                             "ReplicationData")).readColumn("sequenceLength")
            reverseIdx = 1
            reverseCompIdx = 3
            reverseSequences = np.logical_or(sequenceIdx == reverseIdx,
                                             sequenceIdx == reverseCompIdx)
            sequenceLength[
                reverseSequences] = -1 * sequenceLength[reverseSequences]
            sequenceLength[sequenceLength == PLACE_HOLDER] = np.nan

            # Down sample dna polymerase position, every position is only plotted once here
            # using numpy ninja-ness
            unique, index, value = np.unique(sequenceLength,
                                             return_index=True,
                                             return_inverse=True)
            m = np.zeros_like(value, dtype=bool)
            m[index] = True
            m = m.reshape(sequenceLength.shape)
            sequenceLength[~m] = np.nan

            # ax3.plot(time / 60., sequenceLength, marker=',', markersize=2, linewidth=0, color = color, alpha = alpha)
            # ax3.set_yticks([-1 * genomeLength / 2, 0, genomeLength / 2])
            # ax3.set_yticklabels(['-terC', 'oriC', '+terC'])

            ## Pairs of forks
            # pairsOfForks = (sequenceIdx != PLACE_HOLDER).sum(axis = 1) / 4
            # ax4.plot(time / 60., pairsOfForks, linewidth=2, color = color, alpha = alpha)
            # ax4.set_yticks(np.arange(0,7))
            # ax4.set_ylim([0, 6])

        y_ticks = ax0.get_yticks()
        new_y_ticks = y_ticks[0:-1:2]
        ax0.set_yticks(new_y_ticks)

        ax0.set_xlim([0., time.max() / 60.])
        ax0.set_ylabel("Cell mass\n(fg)", fontsize=FONT_SIZE)
        ax0.xaxis.set_visible(False)
        #ax0.axvline(x=44*2+22., linewidth=3, color='gray', alpha = 0.5)

        nutrients_time_series_label = sim_data.external_state.environment.nutrients_time_series_label
        try:
            T_ADD_AA = sim_data.external_state.environment.nutrients_time_series[
                nutrients_time_series_label][1][0] / 60.
        except Exception as e:
            print "nutrients_time_series does not have correct dimensions for this analysis. Exiting.", e
            return
        axes_list = [ax0, ax1, ax2]  #, ax3, ax4]
        for a in axes_list:
            shift_time = T_ADD_AA
            width = a.get_xlim()[1] - shift_time
            height = a.get_ylim()[1] - a.get_ylim()[0]
            a.add_patch(
                patches.Rectangle(
                    (shift_time, a.get_ylim()[0]),  # (x,y)
                    width,  # width
                    height,  # height
                    alpha=0.25,
                    color="gray",
                    linewidth=0.))

        for a in axes_list:
            for tick in a.yaxis.get_major_ticks():
                tick.label.set_fontsize(FONT_SIZE)

        ax1.set_ylabel(r"$\mu$ $(\frac{gDCW}{gDCW \cdot \, min})$",
                       fontsize=FONT_SIZE)
        ax1.xaxis.set_visible(False)
        # ax1.axvline(x=44*2+22., linewidth=3, color='gray', alpha = 0.5)
        ax1.set_ylim([0, 0.032])

        #ax2.set_ylabel("RNA/Protein\n(fg/fg)", fontsize=FONT_SIZE)
        ax2.set_ylabel("Active\nribosome\n(umol/L)", fontsize=FONT_SIZE)
        ax2.xaxis.set_visible(False)
        # ax2.axvline(x=44*2+22., linewidth=3, color='gray', alpha = 0.5)

        y_ticks = ax2.get_yticks()
        new_y_ticks = y_ticks[0:-1:2]
        ax2.set_yticks(new_y_ticks)

        # ax3.set_ylabel("DNA polymerase\nposition (nt)", fontsize=FONT_SIZE)
        # ax3.xaxis.set_visible(False)
        # # ax3.axvline(x=44*2+22., linewidth=3, color='gray', alpha = 0.5)

        # ax4.set_ylabel("Relative rate\nof dNTP\npolymerization", fontsize=FONT_SIZE)
        # ax4.set_xlabel("Time (min)", fontsize=FONT_SIZE)
        # # ax4.axvline(x=44*2+22., linewidth=3, color='gray', alpha = 0.5)

        whitePadSparklineAxis(ax0, False)
        whitePadSparklineAxis(ax1, False)
        whitePadSparklineAxis(ax2, False)
        # whitePadSparklineAxis(ax3, False)
        # whitePadSparklineAxis(ax4)

        exportFigure(plt, plotOutDir, plotOutFileName + "_b", metadata)

        for axes in axes_list:
            axes.tick_params(
                axis='x',  # changes apply to the x-axis
                which='both',  # both major and minor ticks are affected
                bottom='off',  # ticks along the bottom edge are off
                top='off',  # ticks along the top edge are off
                labelbottom='off')  # labels along the bottom edge are off
            axes.tick_params(
                axis='y',  # changes apply to the x-axis
                which='both',  # both major and minor ticks are affected
                left='off',  # ticks along the bottom edge are off
                right='off',  # ticks along the top edge are off
                labelleft='off')  # labels along the bottom edge are off

            axes.set_xlabel("")
            axes.set_ylabel("")

        plt.subplots_adjust(top=1, bottom=trim, left=trim, right=1)

        exportFigure(plt,
                     plotOutDir,
                     plotOutFileName + "_b_stripped",
                     metadata,
                     transparent=True)
        plt.close("all")

        ################ PART II #######################

        mult = 2.3
        fig = plt.figure()
        fig.set_figwidth(mm2inch(97) * mult)
        fig.set_figheight(mm2inch(38) * mult)

        # ax0 = plt.subplot2grid((3,4), (0,0), colspan = 4)
        # ax1 = plt.subplot2grid((3,4), (1,0), colspan = 4, sharex=ax0)
        # ax2 = plt.subplot2grid((3,4), (2,0), colspan = 4, sharex=ax0)
        ax3 = plt.subplot2grid((2, 4), (0, 0), colspan=4, sharex=ax0)
        ax4 = plt.subplot2grid((2, 4), (1, 0), colspan=4, sharex=ax0)

        # Get all cells in each seed
        ap = AnalysisPaths(variantDir, cohort_plot=True)
        all_cells = ap.get_cells(seed=[4])

        if not len(all_cells):
            return

        for idx, simDir in enumerate(all_cells):
            #color = "black"
            color = "#0d71b9"
            alpha = 1
            if idx % 2:
                color = "#0d71b9"
                blue = 0.8

            simOutDir = os.path.join(simDir, "simOut")

            time = TableReader(os.path.join(simOutDir,
                                            "Main")).readColumn("time")

            ## Cell mass
            # mass = TableReader(os.path.join(simOutDir, "Mass"))
            # cellMass = mass.readColumn("cellMass")
            # ax0.plot(time / 60., cellMass, color = color, alpha = alpha, linewidth=2)

            ## Inst. growth rate
            # growthRate = mass.readColumn("instantaniousGrowthRate")
            # growthRate = (1 / units.s) * growthRate
            # growthRate = growthRate.asNumber(1 / units.min)
            # growthRate[abs(growthRate - np.median(growthRate)) > 1.25 * np.nanstd(growthRate)] = np.nan
            # ax1.plot(time / 60., growthRate, color = color, alpha = alpha)

            ## Rna over protein
            # Get active ribosome counts
            # uniqueMoleculeCounts = TableReader(os.path.join(simOutDir, "UniqueMoleculeCounts"))
            # ribosomeIndex = uniqueMoleculeCounts.readAttribute("uniqueMoleculeIds").index("activeRibosome")
            # ribosomeCounts = uniqueMoleculeCounts.readColumn("uniqueMoleculeCounts")[:, ribosomeIndex]
            # uniqueMoleculeCounts.close()
            # ribosomeConcentration = ((1 / sim_data.constants.nAvogadro) * ribosomeCounts) / ((1.0 / sim_data.constants.cellDensity) * (units.fg * cellMass))
            # ribosomeConcentration = ribosomeConcentration.asNumber(units.umol / units.L)
            # ax2.plot(time / 60., ribosomeConcentration, color = color, alpha = alpha, linewidth=2)
            # ax2.set_ylim([20., 35.])
            # rnaMass = mass.readColumn("rnaMass")
            # proteinMass = mass.readColumn("proteinMass")
            # rnaOverProtein = rnaMass / proteinMass
            # ax2.plot(time / 60., rnaOverProtein, color = color, alpha = alpha, linewidth=2)

            ## Fork position
            sequenceIdx = TableReader(
                os.path.join(simOutDir,
                             "ReplicationData")).readColumn("sequenceIdx")
            sequenceLength = TableReader(
                os.path.join(simOutDir,
                             "ReplicationData")).readColumn("sequenceLength")
            reverseIdx = 1
            reverseCompIdx = 3
            reverseSequences = np.logical_or(sequenceIdx == reverseIdx,
                                             sequenceIdx == reverseCompIdx)
            sequenceLength[
                reverseSequences] = -1 * sequenceLength[reverseSequences]
            sequenceLength[sequenceLength == PLACE_HOLDER] = np.nan

            # Down sample dna polymerase position, every position is only plotted once here
            # using numpy ninja-ness
            unique, index, value = np.unique(sequenceLength,
                                             return_index=True,
                                             return_inverse=True)
            m = np.zeros_like(value, dtype=bool)
            m[index] = True
            m = m.reshape(sequenceLength.shape)
            sequenceLength[~m] = np.nan

            ax3.plot(time / 60.,
                     sequenceLength,
                     marker='o',
                     markersize=2,
                     linewidth=0,
                     color=color,
                     alpha=alpha)
            #ax3.plot(time / 60., sequenceLength, linewidth=1, color = color, alpha = alpha)
            ax3.set_yticks([-1 * genomeLength / 2, 0, genomeLength / 2])
            ax3.set_yticklabels(['-terC', 'oriC', '+terC'])

            # Pairs of forks
            pairsOfForks = (sequenceIdx != PLACE_HOLDER).sum(axis=1) / 4
            ax4.plot(time / 60.,
                     pairsOfForks,
                     linewidth=2,
                     color=color,
                     alpha=alpha)
            ax4.set_yticks(np.arange(0, 7))
            ax4.set_ylim([0, 6.1])
            ax4.set_yticks([0, 6.])
            ax4.set_yticklabels(["0", "6"])

        # y_ticks = ax0.get_yticks()
        # new_y_ticks = y_ticks[0:-1:2]
        # ax0.set_yticks(new_y_ticks)

        ax3.set_xlim([0., time.max() / 60.])
        # ax0.set_ylabel("Cell mass\n(fg)", fontsize=FONT_SIZE)
        # ax0.xaxis.set_visible(False)
        #ax0.axvline(x=44*2+22., linewidth=3, color='gray', alpha = 0.5)

        nutrients_time_series_label = sim_data.external_state.environment.nutrients_time_series_label
        T_ADD_AA = sim_data.external_state.environment.nutrients_time_series[
            nutrients_time_series_label][1][0] / 60.
        axes_list = [ax3, ax4]
        for a in axes_list:
            shift_time = T_ADD_AA
            width = a.get_xlim()[1] - shift_time
            height = a.get_ylim()[1] - a.get_ylim()[0]
            a.add_patch(
                patches.Rectangle(
                    (shift_time, a.get_ylim()[0]),  # (x,y)
                    width,  # width
                    height,  # height
                    alpha=0.25,
                    color="gray",
                    linewidth=0.))

        for a in axes_list:
            for tick in a.yaxis.get_major_ticks():
                tick.label.set_fontsize(FONT_SIZE)

        # ax1.set_ylabel(r"$\mu$ $(\frac{gDCW}{gDCW-min})$", fontsize=FONT_SIZE)
        # ax1.xaxis.set_visible(False)
        # # ax1.axvline(x=44*2+22., linewidth=3, color='gray', alpha = 0.5)
        # ax1.set_ylim([0.012, 0.032])

        # #ax2.set_ylabel("RNA/Protein\n(fg/fg)", fontsize=FONT_SIZE)
        # ax2.set_ylabel("Active\nribosome\n(umol/L)", fontsize=FONT_SIZE)
        # ax2.xaxis.set_visible(False)
        # # ax2.axvline(x=44*2+22., linewidth=3, color='gray', alpha = 0.5)

        # y_ticks = ax2.get_yticks()
        # new_y_ticks = y_ticks[0:-1:2]
        # ax2.set_yticks(new_y_ticks)

        ax3.set_ylabel("DNA polymerase\nposition (nt)", fontsize=FONT_SIZE)
        ax3.xaxis.set_visible(False)
        # ax3.axvline(x=44*2+22., linewidth=3, color='gray', alpha = 0.5)

        ax4.set_ylabel("Relative rate\nof dNTP\npolymerization",
                       fontsize=FONT_SIZE)
        ax4.set_xlabel("Time (min)", fontsize=FONT_SIZE)
        # ax4.axvline(x=44*2+22., linewidth=3, color='gray', alpha = 0.5)

        # whitePadSparklineAxis(ax0, False)
        # whitePadSparklineAxis(ax1, False)
        # whitePadSparklineAxis(ax2, False)
        whitePadSparklineAxis(ax3, False)
        whitePadSparklineAxis(ax4)

        plt.subplots_adjust(bottom=0.15)

        exportFigure(plt, plotOutDir, plotOutFileName + "_e", metadata)

        for axes in axes_list:
            axes.tick_params(
                axis='x',  # changes apply to the x-axis
                which='both',  # both major and minor ticks are affected
                bottom='off',  # ticks along the bottom edge are off
                top='off',  # ticks along the top edge are off
                labelbottom='off')  # labels along the bottom edge are off
            axes.tick_params(
                axis='y',  # changes apply to the x-axis
                which='both',  # both major and minor ticks are affected
                left='off',  # ticks along the bottom edge are off
                right='off',  # ticks along the top edge are off
                labelleft='off')  # labels along the bottom edge are off

            axes.set_xlabel("")
            axes.set_ylabel("")

        plt.subplots_adjust(top=1, bottom=trim, left=trim, right=1)

        exportFigure(plt,
                     plotOutDir,
                     plotOutFileName + "_e_stripped",
                     metadata,
                     transparent=True)
        plt.close("all")
    def do_plot(self, seedOutDir, plotOutDir, plotOutFileName, simDataFile,
                validationDataFile, metadata):
        return

        if not os.path.isdir(seedOutDir):
            raise Exception, "seedOutDir does not currently exist as a directory"

        if not os.path.exists(plotOutDir):
            os.mkdir(plotOutDir)

        # Get all ids reqiured
        sim_data = cPickle.load(open(simDataFile, "rb"))
        ids_complexation = sim_data.process.complexation.moleculeNames  # Complexe of proteins, and protein monomers
        ids_complexation_complexes = sim_data.process.complexation.ids_complexes  # Only complexes
        ids_equilibrium = sim_data.process.equilibrium.moleculeNames  # Complexes of proteins + small molecules, small molecules, protein monomers
        ids_equilibrium_complexes = sim_data.process.equilibrium.ids_complexes  # Only complexes
        ids_translation = sim_data.process.translation.monomerData[
            "id"].tolist()  # Only protein monomers

        # ids_ribosome =
        data_50s = sim_data.process.complexation.getMonomers(
            sim_data.moleculeIds.s50_fullComplex)
        data_30s = sim_data.process.complexation.getMonomers(
            sim_data.moleculeIds.s30_fullComplex)
        ribosome_subunit_ids = data_50s["subunitIds"].tolist(
        ) + data_30s["subunitIds"].tolist()
        ribosome_subunit_stoich = np.hstack(
            (data_50s["subunitStoich"], data_30s["subunitStoich"]))

        data_rnap = sim_data.process.complexation.getMonomers(
            sim_data.moleculeIds.rnapFull)
        rnap_subunit_ids = data_rnap["subunitIds"].tolist()
        rnap_subunit_stoich = data_rnap["subunitStoich"]

        # Get all cells
        ap = AnalysisPaths(seedOutDir, cohort_plot=True)
        allDir = ap.get_cells(seed=[0])

        first_build = True

        # Pre-allocate variables. Rows = Generations, Cols = Monomers
        n_monomers = sim_data.process.translation.monomerData['id'].size
        n_sims = ap.n_generation

        monomerExistMultigen = np.zeros((n_sims, n_monomers), dtype=np.bool)
        ratioFinalToInitialCountMultigen = np.zeros((n_sims, n_monomers),
                                                    dtype=np.float)
        initiationEventsPerMonomerMultigen = np.zeros((n_sims, n_monomers),
                                                      dtype=np.int)
        monomerCountInitialMultigen = np.zeros((n_sims, n_monomers),
                                               dtype=np.int)
        cellMassInitialMultigen = np.zeros(n_sims, dtype=np.float)

        if not FROM_CACHE:

            for gen_idx, simDir in enumerate(allDir):
                simOutDir = os.path.join(simDir, "simOut")

                time = TableReader(os.path.join(simOutDir,
                                                "Main")).readColumn("time")

                ## READ DATA ##
                # Read in bulk ids and counts
                bulkMolecules = TableReader(
                    os.path.join(simOutDir, "BulkMolecules"))

                if first_build:
                    moleculeIds = bulkMolecules.readAttribute("objectNames")

                    complexationIdx = np.array([
                        moleculeIds.index(x) for x in ids_complexation
                    ])  # Complexe of proteins, and protein monomers
                    complexation_complexesIdx = np.array([
                        moleculeIds.index(x)
                        for x in ids_complexation_complexes
                    ])  # Only complexes
                    equilibriumIdx = np.array(
                        [moleculeIds.index(x) for x in ids_equilibrium]
                    )  # Complexes of proteins + small molecules, small molecules, protein monomers
                    equilibrium_complexesIdx = np.array([
                        moleculeIds.index(x) for x in ids_equilibrium_complexes
                    ])  # Only complexes
                    translationIdx = np.array([
                        moleculeIds.index(x) for x in ids_translation
                    ])  # Only protein monomers

                    ribosomeIdx = np.array(
                        [moleculeIds.index(x) for x in ribosome_subunit_ids])
                    rnapIdx = np.array(
                        [moleculeIds.index(x) for x in rnap_subunit_ids])

                    first_build = False

                bulkCounts = bulkMolecules.readColumn("counts")
                bulkMolecules.close()

                # Dissociate protein-protein complexes
                bulkCounts[:, complexationIdx] += np.dot(
                    sim_data.process.complexation.stoichMatrixMonomers(),
                    bulkCounts[:, complexation_complexesIdx].transpose() *
                    -1).transpose()

                # Dissociate protein-small molecule complexes
                bulkCounts[:, equilibriumIdx] += np.dot(
                    sim_data.process.equilibrium.stoichMatrixMonomers(),
                    bulkCounts[:, equilibrium_complexesIdx].transpose() *
                    -1).transpose()

                # Load unique molecule data for RNAP and ribosomes
                uniqueMoleculeCounts = TableReader(
                    os.path.join(simOutDir, "UniqueMoleculeCounts"))
                ribosomeIndex = uniqueMoleculeCounts.readAttribute(
                    "uniqueMoleculeIds").index("activeRibosome")
                rnaPolyIndex = uniqueMoleculeCounts.readAttribute(
                    "uniqueMoleculeIds").index("activeRnaPoly")
                nActiveRibosome = uniqueMoleculeCounts.readColumn(
                    "uniqueMoleculeCounts")[:, ribosomeIndex]
                nActiveRnaPoly = uniqueMoleculeCounts.readColumn(
                    "uniqueMoleculeCounts")[:, rnaPolyIndex]
                uniqueMoleculeCounts.close()

                # Add subunits from RNAP and ribosomes
                ribosomeSubunitCounts = (
                    nActiveRibosome.reshape((nActiveRibosome.size, 1)) *
                    ribosome_subunit_stoich.reshape(
                        (1, ribosome_subunit_stoich.size)))
                rnapSubunitCounts = (nActiveRnaPoly.reshape(
                    (nActiveRnaPoly.size, 1)) * rnap_subunit_stoich.reshape(
                        (1, rnap_subunit_stoich.size)))

                bulkCounts[:, ribosomeIdx] += ribosomeSubunitCounts
                bulkCounts[:, rnapIdx] += rnapSubunitCounts

                # Get protein monomer counts for calculations now that all complexes are dissociated
                proteinMonomerCounts = bulkCounts[:, translationIdx]

                ## CALCULATIONS ##
                # Calculate if monomer exists over course of cell cycle
                monomerExist = proteinMonomerCounts.sum(axis=0) > 1

                # Calculate if monomer comes close to doubling
                ratioFinalToInitialCount = (
                    proteinMonomerCounts[-1, :] +
                    1) / (proteinMonomerCounts[0, :].astype(np.float) + 1)
                # monomerDouble = ratioFinalToInitialCount > (1 - CLOSE_TO_DOUBLE)

                # Load transcription initiation event data
                rnapData = TableReader(os.path.join(simOutDir, "RnapData"))
                initiationEventsPerRna = rnapData.readColumn(
                    "rnaInitEvent").sum(axis=0)

                # Map transcription initiation events to monomers
                initiationEventsPerMonomer = initiationEventsPerRna[
                    sim_data.relation.rnaIndexToMonomerMapping]

                # Load cell mass
                cellMassInitial = TableReader(os.path.join(
                    simOutDir, "Mass")).readColumn("cellMass")[0]

                # Log data
                monomerExistMultigen[gen_idx, :] = monomerExist
                ratioFinalToInitialCountMultigen[
                    gen_idx, :] = ratioFinalToInitialCount
                initiationEventsPerMonomerMultigen[
                    gen_idx, :] = initiationEventsPerMonomer
                monomerCountInitialMultigen[gen_idx, :] = proteinMonomerCounts[
                    0, :]
                cellMassInitialMultigen[gen_idx] = cellMassInitial

            cPickle.dump(
                monomerExistMultigen,
                open(os.path.join(plotOutDir, "monomerExistMultigen.pickle"),
                     "wb"))
            cPickle.dump(
                ratioFinalToInitialCountMultigen,
                open(
                    os.path.join(plotOutDir,
                                 "ratioFinalToInitialCountMultigen.pickle"),
                    "wb"))
            cPickle.dump(
                initiationEventsPerMonomerMultigen,
                open(
                    os.path.join(plotOutDir,
                                 "initiationEventsPerMonomerMultigen.pickle"),
                    "wb"))
            cPickle.dump(
                monomerCountInitialMultigen,
                open(
                    os.path.join(plotOutDir,
                                 "monomerCountInitialMultigen.pickle"), "wb"))
            cPickle.dump(
                cellMassInitialMultigen,
                open(
                    os.path.join(plotOutDir, "cellMassInitialMultigen.pickle"),
                    "wb"))

        monomerExistMultigen = cPickle.load(
            open(os.path.join(plotOutDir, "monomerExistMultigen.pickle"),
                 "rb"))
        ratioFinalToInitialCountMultigen = cPickle.load(
            open(
                os.path.join(plotOutDir,
                             "ratioFinalToInitialCountMultigen.pickle"), "rb"))
        initiationEventsPerMonomerMultigen = cPickle.load(
            open(
                os.path.join(plotOutDir,
                             "initiationEventsPerMonomerMultigen.pickle"),
                "rb"))
        monomerCountInitialMultigen = cPickle.load(
            open(
                os.path.join(plotOutDir, "monomerCountInitialMultigen.pickle"),
                "rb"))
        cellMassInitialMultigen = cPickle.load(
            open(os.path.join(plotOutDir, "cellMassInitialMultigen.pickle"),
                 "rb"))
        cellMassInitialMultigen = units.fg * cellMassInitialMultigen

        existFractionPerMonomer = monomerExistMultigen.mean(axis=0)
        averageFoldChangePerMonomer = ratioFinalToInitialCountMultigen  #.mean(axis=0)
        averageInitiationEventsPerMonomer = initiationEventsPerMonomerMultigen.mean(
            axis=0)

        averageInitiationEventsPerMonomer = np.tile(
            averageInitiationEventsPerMonomer, (6, 1))

        mws = sim_data.getter.getMass(
            sim_data.process.translation.monomerData['id'])
        monomerInitialMasses = (mws * monomerCountInitialMultigen /
                                sim_data.constants.nAvogadro)

        # np.tile(cellMassInitialMultigen.asNumber().reshape((1,10)), (n_monomers,1))

        # initialMassFractions = monomerInitialMasses.asNumber(units.fg).transpose() / np.tile(cellMassInitialMultigen.asNumber().reshape((1,10)), (n_monomers,1))
        # averageInitialMassFractions = initialMassFractions.mean(axis = 1)
        # avgMonomerInitialMass = monomerInitialMasses.asNumber(units.fg).mean(axis=0)
        # avgMonomerInitialMassFraction = avgMonomerInitialMass / avgMonomerInitialMass.sum()

        # uniqueBurstSizes = np.unique(initiationEventsPerMonomerMultigen)
        # probExistByBurstSize = np.zeros(uniqueBurstSizes.size)
        # probDoubleByBurstSize = np.zeros(uniqueBurstSizes.size)

        # for idx, burstSize in enumerate(uniqueBurstSizes):
        # 	mask = initiationEventsPerMonomerMultigen == burstSize
        # 	mask_sum = mask.sum()
        # 	probExistByBurstSize[idx] = monomerExistMultigen[mask].sum() / float(mask.sum())
        # 	probDoubleByBurstSize[idx] = monomerDoubleMultigen[mask].sum() / float(mask.sum())

        # fig, axesList = plt.subplots(4,1)

        mult = 3
        fig = plt.figure()
        fig.set_figwidth(mm2inch(80) * mult)
        fig.set_figheight(mm2inch(50) * mult)

        scatterAxis = plt.subplot2grid(
            (3, 4), (0, 0), colspan=3,
            rowspan=3)  #, sharex = xhistAxis, sharey = yhistAxis)
        # scatterAxis.axhline(1.0, linewidth=0.5, color='black', linestyle="--", xmin = 0.5, xmax = 1.)
        # scatterAxis.axhline(2.0, linewidth=0.5, color='black', linestyle="--", xmin = 0.5, xmax = 1.)
        # xhistAxis = plt.subplot2grid((4,5), (0,0), colspan=3, sharex = scatterAxis)
        yhistAxis = plt.subplot2grid((3, 4), (0, 3),
                                     rowspan=3)  #, sharey = scatterAxis)
        # yhistAxis.axhline(1.0, linewidth=1.0, color='black', linestyle = 'dotted')
        # yhistAxis.axhline(2.0, linewidth=1.0, color='black', linestyle = 'dotted')
        #yhistAxis_2 = plt.subplot2grid((4,5), (1,4), rowspan=3, sharey = scatterAxis)
        #yhistAxis_2.axhline(1.0, linewidth=0.5, color='black', linestyle="--")
        #yhistAxis_2.axhline(2.0, linewidth=0.5, color='black', linestyle="--")

        # xhistAxis.xaxis.set_visible(False)
        #yhistAxis.yaxis.set_visible(False)

        smallBurst = averageInitiationEventsPerMonomer <= 1.
        # scatterAxis.set_xlim([1e-1, 1e3])
        # ----> scatterAxis.set_ylim([0.7, 100])

        # scatterAxis.semilogx(averageInitiationEventsPerMonomer[smallBurst], averageFoldChangePerMonomer[smallBurst], marker = '.', color = "red", alpha = 0.9, lw = 0.)#, s = 5)
        # scatterAxis.semilogx(averageInitiationEventsPerMonomer[~smallBurst], averageFoldChangePerMonomer[~smallBurst], marker = '.', color = "blue", alpha = 0.9, lw = 0.)#, s = 5)
        ## scatterAxis.semilogx(averageInitiationEventsPerMonomer[smallBurst], averageFoldChangePerMonomer[smallBurst], marker = '.', color = "green", alpha = 0.9, lw = 0.)#, s = 5)
        ## scatterAxis.semilogx(averageInitiationEventsPerMonomer[~smallBurst], averageFoldChangePerMonomer[~smallBurst], marker = '.', color = "blue", alpha = 0.9, lw = 0.)#, s = 5)

        scatterAxis.loglog(averageInitiationEventsPerMonomer[smallBurst],
                           averageFoldChangePerMonomer[smallBurst],
                           marker='.',
                           color="blue",
                           alpha=0.5,
                           lw=0.)  #, s = 5)
        scatterAxis.loglog(averageInitiationEventsPerMonomer[~smallBurst],
                           averageFoldChangePerMonomer[~smallBurst],
                           marker='.',
                           color="red",
                           alpha=0.5,
                           lw=0.)  #, s = 5)

        scatterAxis.set_ylabel(
            "Fold change per protein\nin each generation ({} generations)".
            format(ap.n_generation),
            fontsize=FONT_SIZE)
        scatterAxis.set_xlabel(
            "Average number of transcription events\nper protein per generation ({} generations)"
            .format(ap.n_generation),
            fontsize=FONT_SIZE)

        # lims = yhistAxis.get_ylim()
        # step = (lims[1] - lims[0]) / 125
        # bins = np.arange(lims[0], lims[1] + step, step)

        # mass_in_binrange = np.zeros(bins.size-1, dtype=np.float)
        # for i in range(len(bins) - 1):
        # 	in_bin_range = np.logical_and(averageFoldChangePerMonomer > bins[i], averageFoldChangePerMonomer < bins[i+1])
        # 	mass_in_binrange[i] = avgMonomerInitialMassFraction[in_bin_range].sum()

        #yhistAxis_2.barh(bottom = bins[:-1], width = mass_in_binrange, height=(lims[1] - lims[0]) / 125, color = "white")
        #yhistAxis_2.set_xlim([0., 1.])
        #yhistAxis_2.yaxis.set_label_position("right")
        #yhistAxis_2.set_xlabel("Fraction of\nproteome mass", fontsize = FONT_SIZE)
        #scatterAxis.set_xlim([-10., 1000.])

        # yhistAxis.hist(averageFoldChangePerMonomer[~smallBurst], histtype = 'step', bins = 25, orientation='horizontal', log = True)
        # yhistAxis.hist(averageFoldChangePerMonomer[smallBurst], histtype = 'step', bins = 100, orientation='horizontal', log = True, color="green")

        yhistAxis.hist(averageFoldChangePerMonomer[~smallBurst],
                       histtype='step',
                       bins=np.logspace(np.log10(0.01), np.log10(1000.), 25),
                       range=[0.7, 100],
                       log=True,
                       orientation='horizontal',
                       color="red",
                       linewidth=1)
        yhistAxis.hist(averageFoldChangePerMonomer[smallBurst],
                       histtype='step',
                       bins=np.logspace(np.log10(0.01), np.log10(1000.), 125),
                       range=[0.7, 100],
                       log=True,
                       orientation='horizontal',
                       color="blue",
                       linewidth=1)
        # yhistAxis.set_ylim([0.7, 100])
        yhistAxis.set_yscale("log")

        # xhistAxis.hist(averageInitiationEventsPerMonomer[smallBurst], histtype = 'step', color = "green", bins = np.logspace(np.log10(0.01), np.log10(1000.), 125), log = True, range = [-10., 1000.])
        # xhistAxis.hist(averageInitiationEventsPerMonomer[~smallBurst], histtype = 'step', bins = np.logspace(np.log10(0.01), np.log10(1000.), 125), log = True, range = [-10., 1000.])
        # xhistAxis.set_xscale("log")

        for label in yhistAxis.xaxis.get_ticklabels()[::2]:
            label.set_visible(False)

        whitePadSparklineAxis(scatterAxis)
        whitePadSparklineAxis(yhistAxis)
        # whitePadSparklineAxis(xhistAxis)

        yhistAxis.set_yticks([1., 2.])
        yhistAxis.set_yticklabels([])

        # Label 1 and 2 with arrows
        yhistAxis.annotate("1",
                           xy=(1e4, 1),
                           xytext=(1e5, 1),
                           fontsize=FONT_SIZE,
                           arrowprops=dict(facecolor="black",
                                           edgecolor="none",
                                           width=0.5,
                                           headwidth=4),
                           verticalalignment="center")
        yhistAxis.annotate("2",
                           xy=(1e4, 2),
                           xytext=(1e5, 2),
                           fontsize=FONT_SIZE,
                           arrowprops=dict(facecolor="black",
                                           edgecolor="none",
                                           width=0.5,
                                           headwidth=4),
                           verticalalignment="center")

        for tick in scatterAxis.xaxis.get_major_ticks():
            tick.label.set_fontsize(FONT_SIZE)
        for tick in scatterAxis.yaxis.get_major_ticks():
            tick.label.set_fontsize(FONT_SIZE)

        for tick in yhistAxis.xaxis.get_major_ticks():
            tick.label.set_fontsize(FONT_SIZE)
        for tick in yhistAxis.yaxis.get_major_ticks():
            tick.label.set_fontsize(FONT_SIZE)

        scatterAxis.tick_params(
            axis='both',  # which axis
            which='both',  # both major and minor ticks are affected
            right='off',  # ticks along the bottom edge are off
            left='on',  # ticks along the top edge are off
            top='off',
            bottom='on',
        )

        yhistAxis.tick_params(
            axis='both',  # which axis
            which='both',  # both major and minor ticks are affected
            right='off',  # ticks along the bottom edge are off
            left='on',  # ticks along the top edge are off
            top='off',
            bottom='on',
        )

        # xhistAxis.tick_params(
        # 	axis='both',          # which axis
        # 	which='both',      # both major and minor ticks are affected
        # 	right='off',      # ticks along the bottom edge are off
        # 	left='on',         # ticks along the top edge are off
        # 	top = 'off',
        # 	bottom = 'on',
        # 	)

        plt.subplots_adjust(wspace=0.3, hspace=0.3, bottom=0.2)

        # for label in yhistAxis_2.xaxis.get_ticklabels()[::2]:
        # 	label.set_visible(False)

        # axesList[0].semilogy(uniqueBurstSizes, probExistByBurstSize)
        # axesList[1].semilogy(uniqueBurstSizes, probDoubleByBurstSize)

        # # axesList[0].set_ylabel("Probability exists")
        # # axesList[1].set_ylabel("Probability doubles")
        # # axesList[1].set_xlabel("Number of transcription events per generation")

        # axesList[2].semilogy(uniqueBurstSizes, probExistByBurstSize)
        # axesList[2].set_xlim([0., 10.])
        # #axesList[2].set_ylim([0.96, 1.0])
        # axesList[3].semilogy(uniqueBurstSizes, probDoubleByBurstSize)
        # axesList[3].set_xlim([0., 10.])
        # #axesList[3].set_ylim([0.96, 1.0])

        # axesList[0].set_ylabel("Probability\nexists")
        # axesList[1].set_ylabel("Probability\ndoubles")
        # axesList[2].set_ylabel("Probability\nexists")
        # axesList[3].set_ylabel("Probability\ndoubles")
        # axesList[3].set_xlabel("Number of transcription events per generation")

        exportFigure(plt, plotOutDir, plotOutFileName, metadata)
        #plt.close("all")

        scatterAxis.set_xlabel("")
        scatterAxis.set_ylabel("")
        scatterAxis.set_xticklabels([])
        scatterAxis.set_yticklabels([])

        yhistAxis.set_xlabel("")
        yhistAxis.set_ylabel("")
        yhistAxis.set_xticklabels([])

        exportFigure(plt, plotOutDir, plotOutFileName + "_stripped", metadata)
        plt.close("all")
    def do_plot(self, seedOutDir, plotOutDir, plotOutFileName, simDataFile,
                validationDataFile, metadata):
        if not os.path.isdir(seedOutDir):
            raise Exception, "seedOutDir does not currently exist as a directory"
        if not os.path.exists(plotOutDir):
            os.mkdir(plotOutDir)

        ap = AnalysisPaths(seedOutDir, cohort_plot=True)

        # Get all cells
        allDir = ap.get_cells(seed=[0])

        # fig = plt.figure(figsize = (14, 10))
        # ax1 = plt.subplot2grid((4,2), (0,0), rowspan = 4)
        # ax2 = plt.subplot2grid((4,2), (0,1))
        # ax3 = plt.subplot2grid((4,2), (1,1))
        # ax4 = plt.subplot2grid((4,2), (2,1))
        # ax5 = plt.subplot2grid((4,2), (3,1))
        # axesList = [ax2, ax3, ax4, ax5]
        # colors = ["blue", "green", "red", "cyan"]
        colors = ["#43aa98", "#0071bb", "#bf673c"]

        mult = 2.3

        fig, axis = plt.subplots(1, 1)
        fig.set_figwidth(mm2inch(55.2) * mult)
        fig.set_figheight(mm2inch(53.36) * mult)

        width = 200

        timeMultigen = np.zeros(0)
        cellMassGrowthRateMultigen = np.zeros(0)
        proteinGrowthRateMultigen = np.zeros(0)
        rnaGrowthRateMultigen = np.zeros(0)

        for gen, simDir in enumerate(allDir):
            simOutDir = os.path.join(simDir, "simOut")
            time = TableReader(os.path.join(simOutDir,
                                            "Main")).readColumn("time")
            timeStep = TableReader(os.path.join(
                simOutDir, "Main")).readColumn("timeStepSec")
            mass = TableReader(os.path.join(simOutDir, "Mass"))

            cellMass = mass.readColumn("cellMass")
            proteinMass = mass.readColumn("proteinMass")
            rnaMass = mass.readColumn("rnaMass")
            dnaMass = mass.readColumn("dnaMass")

            cellmassGrowthRate = np.diff(
                cellMass) / cellMass[1:] / timeStep[:-1]
            proteinGrowthRate = np.diff(
                proteinMass) / proteinMass[1:] / timeStep[:-1]
            rnaGrowthRate = np.diff(rnaMass) / rnaMass[1:] / timeStep[:-1]
            dnaGrowthRate = np.diff(dnaMass) / dnaMass[1:] / timeStep[:-1]

            timeMultigen = np.hstack((timeMultigen, time[:-1]))
            cellMassGrowthRateMultigen = np.hstack(
                (cellMassGrowthRateMultigen, cellmassGrowthRate))
            proteinGrowthRateMultigen = np.hstack(
                (proteinGrowthRateMultigen, proteinGrowthRate))
            rnaGrowthRateMultigen = np.hstack(
                (rnaGrowthRateMultigen, rnaGrowthRate))

        # seriesScrubber(cellMassGrowthRateMultigen, 1.5)
        # seriesScrubber(proteinGrowthRateMultigen, 1.5)
        # seriesScrubber(rnaGrowthRateMultigen, 1.5)

        cellMassGrowthRateMultigen = np.convolve(cellMassGrowthRateMultigen,
                                                 np.ones(width) / width,
                                                 mode="same")
        proteinGrowthRateMultigen = np.convolve(proteinGrowthRateMultigen,
                                                np.ones(width) / width,
                                                mode="same")
        rnaGrowthRateMultigen = np.convolve(rnaGrowthRateMultigen,
                                            np.ones(width) / width,
                                            mode="same")
        # seriesScrubber(proteinGrowthRateMultigen, 2)

        # rnaGrowthRate = np.convolve(rnaGrowthRate, np.ones(width) / width, mode = "same")
        # dnaGrowthRate = np.convolve(dnaGrowthRate, np.ones(width) / width, mode = "same")

        # 	seriesScrubber(rnaGrowthRate,1.25)
        # seriesScrubber(dnaGrowthRate,3.25)

        linewidth = 2
        axis.plot(timeMultigen[:-width] / 60.,
                  cellMassGrowthRateMultigen[:-width] * 60.,
                  color=colors[0],
                  alpha=0.9,
                  label="Cell mass",
                  linewidth=linewidth)
        axis.plot(timeMultigen[:-width] / 60.,
                  proteinGrowthRateMultigen[:-width] * 60.,
                  color=colors[1],
                  alpha=0.9,
                  label="Protein fraction",
                  linewidth=linewidth)
        axis.plot(timeMultigen[:-width] / 60.,
                  rnaGrowthRateMultigen[:-width] * 60.,
                  color=colors[2],
                  alpha=0.9,
                  label="RNA fraction",
                  linewidth=linewidth)
        axis.legend(loc=4, frameon=False)
        axis.set_ylim([0.014, 0.029])

        whitePadSparklineAxis(axis)
        axis.set_xlabel("Time (min)")
        axis.set_ylabel("Averaged instantaneous growth rate " +
                        r"$(\frac{\frac{dX}{dt}}{X} [=] \frac{1}{min})$")
        plt.subplots_adjust(bottom=0.2, left=0.2)
        exportFigure(plt, plotOutDir, plotOutFileName, metadata)

        for axes in [axis]:
            axes.tick_params(
                axis='x',  # changes apply to the x-axis
                which='both',  # both major and minor ticks are affected
                bottom='off',  # ticks along the bottom edge are off
                top='off',  # ticks along the top edge are off
                labelbottom='off')  # labels along the bottom edge are off
            axes.tick_params(
                axis='y',  # changes apply to the x-axis
                which='both',  # both major and minor ticks are affected
                left='off',  # ticks along the bottom edge are off
                right='off',  # ticks along the top edge are off
                labelleft='off')  # labels along the bottom edge are off

            axes.set_xlabel("")
            axes.set_ylabel("")

        plt.subplots_adjust(top=1, bottom=trim, left=trim, right=1)

        exportFigure(plt,
                     plotOutDir,
                     plotOutFileName + "_stripped",
                     metadata,
                     transparent=True)
        plt.close("all")

        ################ ALTERNATE SIZE ######################
        mult = 3

        fig, axis = plt.subplots(1, 1)
        fig.set_figwidth(mm2inch(29.25) * mult)
        fig.set_figheight(mm2inch(24.401) * mult)

        width = 200

        timeMultigen = np.zeros(0)
        cellMassGrowthRateMultigen = np.zeros(0)
        proteinGrowthRateMultigen = np.zeros(0)
        rnaGrowthRateMultigen = np.zeros(0)

        for gen, simDir in enumerate(allDir):
            simOutDir = os.path.join(simDir, "simOut")
            time = TableReader(os.path.join(simOutDir,
                                            "Main")).readColumn("time")
            timeStep = TableReader(os.path.join(
                simOutDir, "Main")).readColumn("timeStepSec")
            mass = TableReader(os.path.join(simOutDir, "Mass"))

            cellMass = mass.readColumn("cellMass")
            proteinMass = mass.readColumn("proteinMass")
            rnaMass = mass.readColumn("rnaMass")
            dnaMass = mass.readColumn("dnaMass")

            cellmassGrowthRate = np.diff(
                cellMass) / cellMass[1:] / timeStep[:-1]
            proteinGrowthRate = np.diff(
                proteinMass) / proteinMass[1:] / timeStep[:-1]
            rnaGrowthRate = np.diff(rnaMass) / rnaMass[1:] / timeStep[:-1]
            dnaGrowthRate = np.diff(dnaMass) / dnaMass[1:] / timeStep[:-1]

            timeMultigen = np.hstack((timeMultigen, time[:-1]))
            cellMassGrowthRateMultigen = np.hstack(
                (cellMassGrowthRateMultigen, cellmassGrowthRate))
            proteinGrowthRateMultigen = np.hstack(
                (proteinGrowthRateMultigen, proteinGrowthRate))
            rnaGrowthRateMultigen = np.hstack(
                (rnaGrowthRateMultigen, rnaGrowthRate))

        # seriesScrubber(cellMassGrowthRateMultigen, 1.5)
        # seriesScrubber(proteinGrowthRateMultigen, 1.5)
        # seriesScrubber(rnaGrowthRateMultigen, 1.5)

        cellMassGrowthRateMultigen = np.convolve(cellMassGrowthRateMultigen,
                                                 np.ones(width) / width,
                                                 mode="same")
        proteinGrowthRateMultigen = np.convolve(proteinGrowthRateMultigen,
                                                np.ones(width) / width,
                                                mode="same")
        rnaGrowthRateMultigen = np.convolve(rnaGrowthRateMultigen,
                                            np.ones(width) / width,
                                            mode="same")
        # seriesScrubber(proteinGrowthRateMultigen, 2)

        # rnaGrowthRate = np.convolve(rnaGrowthRate, np.ones(width) / width, mode = "same")
        # dnaGrowthRate = np.convolve(dnaGrowthRate, np.ones(width) / width, mode = "same")

        # 	seriesScrubber(rnaGrowthRate,1.25)
        # seriesScrubber(dnaGrowthRate,3.25)

        linewidth = 2
        axis.plot(timeMultigen[:-width] / 60.,
                  cellMassGrowthRateMultigen[:-width] * 60.,
                  color=colors[0],
                  alpha=0.9,
                  label="Cell mass",
                  linewidth=linewidth)
        axis.plot(timeMultigen[:-width] / 60.,
                  proteinGrowthRateMultigen[:-width] * 60.,
                  color=colors[1],
                  alpha=0.9,
                  label="Protein fraction",
                  linewidth=linewidth)
        axis.plot(timeMultigen[:-width] / 60.,
                  rnaGrowthRateMultigen[:-width] * 60.,
                  color=colors[2],
                  alpha=0.9,
                  label="RNA fraction",
                  linewidth=linewidth)
        # axis.legend(loc=4,frameon=False)
        axis.set_ylim([0.014, 0.029])

        whitePadSparklineAxis(axis)
        axis.set_xlabel("Time (min)")
        axis.set_ylabel("Averaged instantaneous growth rate " +
                        r"$(\frac{\frac{dX}{dt}}{X} [=] \frac{1}{min})$")
        plt.subplots_adjust(bottom=0.2, left=0.2)
        exportFigure(plt, plotOutDir, plotOutFileName + "_altsize", metadata)

        for axes in [axis]:
            axes.tick_params(
                axis='x',  # changes apply to the x-axis
                which='both',  # both major and minor ticks are affected
                bottom='off',  # ticks along the bottom edge are off
                top='off',  # ticks along the top edge are off
                labelbottom='off')  # labels along the bottom edge are off
            axes.tick_params(
                axis='y',  # changes apply to the x-axis
                which='both',  # both major and minor ticks are affected
                left='off',  # ticks along the bottom edge are off
                right='off',  # ticks along the top edge are off
                labelleft='off')  # labels along the bottom edge are off

            axes.set_xlabel("")
            axes.set_ylabel("")

        plt.subplots_adjust(top=1, bottom=trim_1, left=trim_1, right=1)

        exportFigure(plt,
                     plotOutDir,
                     plotOutFileName + "_altsize_stripped",
                     metadata,
                     transparent=True)
        plt.close("all")