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 in each seed
        ap = AnalysisPaths(variantDir, cohort_plot=True)

        max_cells_in_gen = 0
        for genIdx in range(ap.n_generation):
            n_cells = len(ap.get_cells(generation=[genIdx]))
            if n_cells > max_cells_in_gen:
                max_cells_in_gen = n_cells

        fig, axesList = plt.subplots(ap.n_generation, sharex=True)

        doubling_time = np.zeros((max_cells_in_gen, ap.n_generation))

        for genIdx in range(ap.n_generation):
            gen_cells = ap.get_cells(generation=[genIdx])
            for simDir in gen_cells:
                simOutDir = os.path.join(simDir, "simOut")
                time = TableReader(os.path.join(simOutDir,
                                                "Main")).readColumn("time")
                initialTime = TableReader(os.path.join(
                    simOutDir, "Main")).readAttribute("initialTime")

                doubling_time[np.where(simDir == gen_cells)[0],
                              genIdx] = (time.max() - initialTime) / 60.

        # Plot initial vs final masses
        if ap.n_generation == 1:
            axesList = [axesList]

        for idx, axes in enumerate(axesList):
            if max_cells_in_gen > 1:
                axes.hist(doubling_time[:, idx].flatten(),
                          int(np.ceil(np.sqrt(doubling_time[:, idx].size))))
            else:
                axes.plot(doubling_time[:, idx], 1, 'x')
                axes.set_ylim([0, 2])
            axes.axvline(doubling_time[:, idx].mean(),
                         color='k',
                         linestyle='dashed',
                         linewidth=2)
            axes.text(
                doubling_time[:, idx].mean(), 1, "Mean: %.3f Var: %.3f" %
                (doubling_time[:, idx].mean(), doubling_time[:, idx].var()))

        axesList[-1].set_xlabel("Doubling time (min))")
        axesList[ap.n_generation / 2].set_ylabel("Frequency")

        plt.subplots_adjust(hspace=0.2, wspace=0.5)

        exportFigure(plt, plotOutDir, plotOutFileName, metadata)
        plt.close("all")
Beispiel #2
0
def getDivisionTime((variant, ap)):
    try:
        simDir = ap.get_cells(variant=[variant])[0]

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

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

        return (time_column.max() - initialTime) / 60.
    except Exception as e:
        print e
        return np.nan
    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, multi_gen_plot=True)

        # TODO: Declutter Y-axis

        # Get all cells
        allDir = ap.get_cells().tolist()

        massNames = [
            "dryMass",
        ]

        cleanNames = [
            "Dry\nmass",
        ]

        for simDir in allDir:
            simOutDir = os.path.join(simDir, "simOut")
            initialTime = TableReader(os.path.join(
                simOutDir, "Main")).readAttribute("initialTime")
            time = TableReader(os.path.join(
                simOutDir, "Main")).readColumn("time") - initialTime
            mass = TableReader(os.path.join(simOutDir, "Mass"))

            for idx, massType in enumerate(massNames):
                massToPlot = mass.readColumn(massNames[idx])

                f = plt.figure(figsize=(1.25, 0.8), frameon=False)

                ax = f.add_axes([0, 0, 1, 1])
                ax.axis("off")

                ax.plot(time, massToPlot, linewidth=2)
                ax.set_ylim([massToPlot.min(), massToPlot.max()])
                ax.set_xlim([time.min(), time.max()])

                exportFigure(
                    plt, plotOutDir,
                    "r01_{}_gen{}".format(massType, allDir.index(simDir)))
                plt.close("all")
Beispiel #4
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)

        # Load KB
        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)

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

        # Load replication data
        dnaPolyFile = TableReader(os.path.join(simOutDir, "ReplicationData"))
        sequenceIdx = dnaPolyFile.readColumn("sequenceIdx")
        sequenceLength = dnaPolyFile.readColumn("sequenceLength")
        numberOfOric = dnaPolyFile.readColumn("numberOfOric")
        criticalMassPerOriC = dnaPolyFile.readColumn("criticalMassPerOriC")
        criticalInitiationMass = dnaPolyFile.readColumn(
            "criticalInitiationMass")
        dnaPolyFile.close()

        # Load dna mass data
        massFile = TableReader(os.path.join(simOutDir, "Mass"))
        totalMass = massFile.readColumn("cellMass")
        dnaMass = massFile.readColumn("dnaMass")
        massFile.close()

        # Setup elongation length data
        reverseIdx = 1
        reverseCompIdx = 3
        reverseSequences = np.logical_or(sequenceIdx == reverseIdx,
                                         sequenceIdx == reverseCompIdx)
        sequenceLength[
            reverseSequences] = -1 * sequenceLength[reverseSequences]
        sequenceLength[sequenceLength == PLACE_HOLDER] = np.nan

        # Count pairs of forks, initation, and termination events
        pairsOfForks = (sequenceIdx != PLACE_HOLDER).sum(axis=1) / 4

        # Count chromosome equivalents
        chromMass = (sim_data.getter.getMass(['CHROM_FULL[c]'])[0] /
                     sim_data.constants.nAvogadro).asNumber(units.fg)
        chromEquivalents = dnaMass / chromMass

        # Count full chromosomes
        bulkMoleculesFile = TableReader(
            os.path.join(simOutDir, "BulkMolecules"))
        bulkIds = bulkMoleculesFile.readAttribute("objectNames")
        chromIdx = bulkIds.index("CHROM_FULL[c]")
        fullChromosomeCounts = bulkMoleculesFile.readColumn("counts")[:,
                                                                      chromIdx]

        # Count critical initiation mass equivalents
        # criticalInitiationMass[0] = criticalInitiationMass[1]
        criticalMassEquivalents = totalMass / criticalInitiationMass

        # Plot stuff
        plt.figure(figsize=(8.5, 11))

        ax = plt.subplot(7, 1, 1)
        ax.plot(time / 60.,
                sequenceLength,
                marker='.',
                markersize=1,
                linewidth=0)
        ax.set_xticks([0, time.max() / 60])
        ax.set_yticks([-1 * genomeLength / 2, 0, genomeLength / 2])
        ax.set_yticklabels(['-terC', 'oriC', '+terC'])
        ax.set_ylabel("DNA polymerase\nposition (nt)")

        ax = plt.subplot(7, 1, 2, sharex=ax)
        ax.plot(time / 60., chromEquivalents, linewidth=2)
        ax.set_xticks([0, time.max() / 60])
        ax.set_yticks(
            np.arange(chromEquivalents.min(),
                      chromEquivalents.max() + 0.5, 0.5))
        ax.set_ylabel("Chromosome\nequivalents")

        ax = plt.subplot(7, 1, 3, sharex=ax)
        ax.plot(time / 60., pairsOfForks, linewidth=2)
        ax.set_xticks([0, time.max() / 60])
        ax.set_yticks(np.arange(0, 7))
        ax.set_ylim([0, 6])
        ax.set_ylabel("Pairs of forks")

        ax = plt.subplot(7, 1, 4, sharex=ax)
        ax.plot(time / 60., criticalMassEquivalents, linewidth=2)
        ax.set_xticks([0, time.max() / 60])
        ax.set_yticks(np.arange(1., 8., 0.5))
        ax.set_ylim([
            np.around(criticalMassEquivalents[1:].min(), decimals=1) - 0.1,
            np.around(criticalMassEquivalents[1:].max(), decimals=1) + 0.1
        ])
        ax.set_ylabel("Factors of critical\ninitiation mass")

        ax = plt.subplot(7, 1, 5, sharex=ax)
        ax.plot(time / 60., criticalMassPerOriC, linewidth=2)
        ax.set_xticks([0, time.max() / 60])
        ax.set_yticks([0.5, 1.0])
        # ax.set_ylim([0.4, 1.1])
        ax.set_ylabel("Critical mass\nper oriC")

        ax = plt.subplot(7, 1, 6, sharex=ax)
        ax.plot(time / 60., numberOfOric, linewidth=2)
        ax.set_xticks([0, time.max() / 60])
        ax.set_ylabel("Number of\noriC")
        ax.set_ylim([0, numberOfOric.max() + 1])

        ax = plt.subplot(7, 1, 7, sharex=ax)
        ax.plot(time / 60., fullChromosomeCounts, linewidth=2)
        ax.set_xticks([0, time.max() / 60])
        ax.set_ylabel("Full\nchromosomes")
        ax.set_ylim([0, fullChromosomeCounts.max() + 1])

        ax.set_xlim([0, time.max() / 60])
        ax.set_xlabel("Time (min)")

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

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

        for idx in [0, 1, 2, 3]:
            ax = plt.subplot(4, 1, idx + 1)
            data = (sequenceIdx == idx).sum(axis=1)
            ax.plot(time / 60., data)
            ax.set_ylim([0, data.max() + 1])

        exportFigure(plt, plotOutDir, 'replicationSequenceIdx')
        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)

        # Load data from KB
        sim_data = cPickle.load(open(simDataFile, "rb"))
        nAvogadro = sim_data.constants.nAvogadro
        moleculeIds = sim_data.moleculeGroups.aaIDs
        moleculeIds.append('GTP[c] (translation)')
        moleculeIds.extend(sim_data.moleculeGroups.ntpIds)

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

        # Load data
        growthLimitsDataFile = TableReader(
            os.path.join(simOutDir, "GrowthLimits"))

        # Translation
        gtpPoolSize = growthLimitsDataFile.readColumn("gtpPoolSize")
        gtpRequestSize = growthLimitsDataFile.readColumn("gtpRequestSize")
        gtpAllocated = growthLimitsDataFile.readColumn("gtpAllocated")
        gtpUsed = growthLimitsDataFile.readColumn("gtpUsed")

        aaPoolSize = growthLimitsDataFile.readColumn("aaPoolSize")
        aaRequestSize = growthLimitsDataFile.readColumn("aaRequestSize")
        aaAllocated = growthLimitsDataFile.readColumn("aaAllocated")
        aasUsed = growthLimitsDataFile.readColumn("aasUsed")

        # Transcription
        ntpPoolSize = growthLimitsDataFile.readColumn("ntpPoolSize")
        ntpRequestSize = growthLimitsDataFile.readColumn("ntpRequestSize")
        ntpAllocated = growthLimitsDataFile.readColumn("ntpAllocated")
        ntpUsed = growthLimitsDataFile.readColumn("ntpUsed")

        # Create aggregate
        poolSize = np.hstack(
            (aaPoolSize, gtpPoolSize.reshape(gtpPoolSize.size,
                                             1), ntpPoolSize))
        requestSize = np.hstack(
            (aaRequestSize, gtpRequestSize.reshape(gtpPoolSize.size,
                                                   1), ntpRequestSize))
        allocated = np.hstack(
            (aaAllocated, gtpAllocated.reshape(gtpPoolSize.size,
                                               1), ntpAllocated))
        used = np.hstack((aasUsed, gtpUsed.reshape(gtpPoolSize.size,
                                                   1), ntpUsed))

        growthLimitsDataFile.close()

        # Load ATP usage
        # ATPusageListenerFile = TableReader(os.path.join(simOutDir, "ATPhydrolyzedUsageListener"))
        # atpsHydrolyzed = ATPusageListenerFile.readColumn('atpsHydrolyzed')
        # ATPusageListenerFile.close()

        # bulkMolecules = TableReader(os.path.join(simOutDir, "BulkMolecules"))
        # bulkMoleculeIds = bulkMolecules.readAttribute("objectNames")
        # bulkMolecules.close()

        # Plot
        fig = plt.figure(figsize=(11, 11))

        # Plot GTP consumption
        # plt.rc('xtick', labelsize=5)
        # plt.rc('ytick', labelsize=5)
        # plt.plot(time / 60., gtpUsed)
        # plt.plot(time / 60., atpsHydrolyzed)
        # plt.xlabel("Time (min)", fontsize = 5)
        # plt.ylabel("GTP counts", fontsize = 5)
        print "GTP consumption by polypeptide elongation = %d" % np.sum(
            gtpUsed)
        # print "ATP hydrolyzed for non-growth associated maintenance = %d" % np.sum(atpsHydrolyzed)

        for idx in range(len(moleculeIds)):
            ax = plt.subplot(7, 4, idx + 1)
            ax.plot(time / 60.,
                    poolSize[:, idx],
                    linewidth=2,
                    label="pool size",
                    color='k')
            ax.plot(time / 60.,
                    requestSize[:, idx],
                    linewidth=2,
                    label="request size",
                    color='b')
            ax.plot(time / 60.,
                    allocated[:, idx],
                    linewidth=2,
                    label="allocated",
                    color='r',
                    linestyle='--')
            ax.plot(time / 60.,
                    used[:, idx],
                    linewidth=2,
                    label="used",
                    color="c",
                    linestyle=":")

            # Highlight title if request is greater than pool
            bbox = None
            if (poolSize < requestSize)[:, idx].any() or (
                    allocated < requestSize)[:, idx].any():
                bbox = {'facecolor': 'red', 'alpha': 0.5, 'pad': 10}
            elif (used[:, idx] < allocated[:, idx]).any():
                bbox = {'facecolor': 'orange', 'alpha': 0.5, 'pad': 10}
            ax.set_title(moleculeIds[idx], bbox=bbox)

            # Set ticks so this is all easy to read
            ax.spines['top'].set_visible(False)
            ax.spines['bottom'].set_visible(False)
            ax.xaxis.set_ticks_position('none')
            ax.tick_params(which='both', direction='out', labelsize=10)
            ax.set_xticks([time.min() / 60., time.max() / 60.])
            ax.set_yticks([
                0.,
                np.max([
                    poolSize[:, idx].max(), requestSize[:, idx].max(),
                    allocated[:, idx].max()
                ])
            ])

        # Create legend
        ax = plt.subplot(7, 4, len(moleculeIds) + 1)
        ax.plot(0, 0, linewidth=2, label="pool size", color='k')
        ax.plot(0, 0, linewidth=2, label="request size", color='b')
        ax.plot(0,
                0,
                linewidth=2,
                label="allocated",
                color='r',
                linestyle='--')
        ax.plot(0, 0, linewidth=2, label="used", color="c", linestyle=':')
        ax.legend(loc=10, prop={'size': 10})
        ax.spines['top'].set_visible(False)
        ax.spines['bottom'].set_visible(False)
        ax.spines['left'].set_visible(False)
        ax.spines['right'].set_visible(False)
        ax.xaxis.set_ticks_position('none')
        ax.yaxis.set_ticks_position('none')
        ax.set_xticks([])
        ax.set_yticks([])
        ax.set_title("Highlights process under-usage",
                     fontsize=12,
                     bbox={
                         'facecolor': 'orange',
                         'alpha': 0.5,
                         'pad': 10
                     })
        ax.set_xlabel("Highlights pool/allocation limit",
                      fontsize=12,
                      bbox={
                          'facecolor': 'red',
                          'alpha': 0.5,
                          'pad': 10
                      })

        # Save
        plt.subplots_adjust(hspace=0.5, wspace=0.5)

        exportFigure(plt, plotOutDir, plotOutFileName, 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, 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):
        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)

        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)

        ap = AnalysisPaths(seedOutDir, multi_gen_plot=True)

        # Get all cells
        allDir = ap.get_cells()

        fig, axesList = plt.subplots(6, sharex=True)
        fig.set_size_inches(11, 11)
        for simDir in allDir:
            simOutDir = os.path.join(simDir, "simOut")
            time = TableReader(os.path.join(
                simOutDir, "Main")).readColumn("time") / 60. / 60.
            mass = TableReader(os.path.join(simOutDir, "Mass"))

            division_time = time.max()

            # Plot dna polymerase 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

            axesList[0].plot(time,
                             sequenceLength,
                             marker=',',
                             markersize=1,
                             linewidth=0)
            axesList[0].set_xlim([0, time.max()])
            axesList[0].set_yticks(
                [-1 * genomeLength / 2, 0, genomeLength / 2])
            axesList[0].set_yticklabels(['-terC', 'oriC', '+terC'])
            axesList[0].set_ylabel("DNA polymerase\nposition (nt)")

            # Plot dna polymerase counts
            pairsOfForks = (sequenceIdx != PLACE_HOLDER).sum(axis=1) / 4

            axesList[1].plot(time, pairsOfForks, linewidth=2)
            axesList[1].set_yticks(np.arange(0, 7))
            axesList[1].set_ylim([0, 6])
            axesList[1].set_xlim([0, time.max()])
            axesList[1].plot([time.max(), time.max()], axesList[1].get_ylim(),
                             'k')
            axesList[1].set_ylabel("Pairs of\nforks")

            # Factors of critical initiation mass
            totalMass = mass.readColumn("cellMass")
            criticalInitiationMass = TableReader(
                os.path.join(
                    simOutDir,
                    "ReplicationData")).readColumn("criticalInitiationMass")
            criticalMassEquivalents = totalMass / criticalInitiationMass

            axesList[2].plot(time, criticalMassEquivalents, linewidth=2)
            for N in CRITICAL_N:
                axesList[2].plot([0, time.max()], [N] * 2, 'k', linestyle='--')
            axesList[2].set_ylabel("Factors of critical\ninitiation mass")
            axesList[2].set_xlim([0, time.max()])

            # Dry mass
            dryMass = mass.readColumn("dryMass")
            axesList[3].plot(time, dryMass, linewidth=2)
            axesList[3].set_ylabel("Dry mass (fg)")
            axesList[3].set_xlim([0, time.max()])
            mass.close()

            # Number of oriC
            numberOfOric = TableReader(
                os.path.join(simOutDir,
                             "ReplicationData")).readColumn("numberOfOric")
            axesList[4].plot(time, numberOfOric, linewidth=2)
            axesList[4].set_ylabel("Number of\noriC")
            axesList[4].set_xlim([0, time.max()])

            # Mass per oriC
            criticalMassPerOriC = TableReader(
                os.path.join(
                    simOutDir,
                    "ReplicationData")).readColumn("criticalMassPerOriC")
            axesList[5].plot(time, criticalMassPerOriC, linewidth=2)
            axesList[5].set_ylabel("Critical mass\nper oriC")
            axesList[5].set_xlim([0, time.max()])

        axesList[0].set_title("Replication plots")
        axesList[-1].set_xlabel("Time (hr)")

        plt.subplots_adjust(hspace=0.2, wspace=0.5)

        exportFigure(plt, plotOutDir, plotOutFileName, metadata)
        plt.close("all")
Beispiel #9
0
    def do_plot(self, seedOutDir, plotOutDir, plotOutFileName, simDataFile,
                validationDataFile, metadata):
        print "Disabled because it's slow"
        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)

        ap = AnalysisPaths(seedOutDir, multi_gen_plot=True)

        # Get first cell from each generation
        firstCellLineage = []
        for gen_idx in range(ap.n_generation):
            firstCellLineage.append(ap.get_cells(generation=[gen_idx])[0])

        sim_data = cPickle.load(open(simDataFile, "rb"))
        max_elongationRate = 21.  # TODO: Fix this
        elongationRate = float(
            sim_data.growthRateParameters.ribosomeElongationRate.asNumber(
                units.aa / units.s))

        fig = plt.figure()
        fig.set_size_inches(10, 12)

        ## Create composition plot
        massNames = [
            "proteinMass",
            "rnaMass",
        ]

        cleanNames = [
            "Protein",
            "RNA",
        ]

        colors = [
            "red",
            "blue",
        ]

        # Composition plotting
        # Create plotting indexes with some offsets to make it look nice
        initFinalIdx = range(4 * len(firstCellLineage))
        for idx, value in enumerate(initFinalIdx):
            if idx % 2:
                initFinalIdx[idx] = initFinalIdx[idx] - 0.1
            else:
                initFinalIdx[idx] = initFinalIdx[idx] + 0.1
        initFinalIdx = np.array(initFinalIdx)
        distanceInitFinalIdx = np.arange(start=0.5,
                                         stop=4 * len(firstCellLineage) + 0.5,
                                         step=2)
        tickLabels = tuple(
            ["$t=0$\nActual", "Expected", "$t=t_d$\nActual", "Expected"] *
            len(firstCellLineage))
        barWidth = 0.75
        gs = gridspec.GridSpec(6, 4)
        ax1 = plt.subplot(gs[:1, :-2])
        ax8 = plt.subplot(gs[1, :-2])
        ax2 = plt.subplot(gs[:2, 2:])

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

            # Get simulation composition
            time, massData = getMassData(simDir, massNames)
            timeStep = TableReader(os.path.join(
                simOutDir, "Main")).readColumn("timeStepSec")
            _, dryMass = getMassData(simDir, ["dryMass"])

            # massDataNorm = massData / massData.sum(axis = 0)
            massDataNorm = massData / dryMass

            initialComp = massDataNorm[:, 1:6 * 60 + 1].mean(axis=1)
            finalComp = massDataNorm[:, -5 * 60:].mean(axis=1)
            initialDryMass = dryMass[0]
            finalDryMass = dryMass[-1]

            # Get expected composition based on growth rates
            _, growthRate = getMassData(simDir, ["instantaniousGrowthRate"])

            initialGrowthRate = np.mean(growthRate[1:6 * 60 + 1]) * 60
            initialDoublingTime = np.log(
                2
            ) / initialGrowthRate * units.min  # Five mintues skipping the first minute
            finalGrowthRate = (np.mean(growthRate[-5 * 60:]) * 60)
            finalDoublingTime = np.log(
                2) / finalGrowthRate * units.min  # Last five minutes

            expectedInitialComp, expectedInitialMass = getExpectedComposition(
                initialDoublingTime)
            #expectedInitialComp = expectedInitialComp / expectedInitialComp.sum()
            expectedInitialComp = expectedInitialComp / expectedInitialMass.asNumber(
                units.fg)
            expectedFinalComp, finalCompInitMass = getExpectedComposition(
                finalDoublingTime)
            #expectedFinalComp = expectedFinalComp / expectedFinalComp.sum()
            expectedFinalMass = finalCompInitMass * 2
            expectedFinalComp = expectedFinalComp / expectedFinalMass.asNumber(
                units.fg) * 2
            initialDistance = np.linalg.norm(expectedInitialComp - initialComp,
                                             2) / np.linalg.norm(
                                                 expectedInitialComp, 2)
            finalDistance = np.linalg.norm(expectedFinalComp - finalComp,
                                           2) / np.linalg.norm(
                                               expectedFinalComp, 2)

            # Calculate ribosomal rna doubling times
            rrn16S_produced = TableReader(
                os.path.join(simOutDir,
                             "RibosomeData")).readColumn("rrn16S_produced")
            rrn23S_produced = TableReader(
                os.path.join(simOutDir,
                             "RibosomeData")).readColumn("rrn23S_produced")
            rrn5S_produced = TableReader(
                os.path.join(simOutDir,
                             "RibosomeData")).readColumn("rrn5S_produced")

            ids_16s = []
            ids_16s.extend(sim_data.moleculeGroups.s30_16sRRNA)
            ids_16s.append(sim_data.moleculeIds.s30_fullComplex)

            ids_23s = []
            ids_23s.extend(sim_data.moleculeGroups.s50_23sRRNA)
            ids_23s.append(sim_data.moleculeIds.s50_fullComplex)

            ids_5s = []
            ids_5s.extend(sim_data.moleculeGroups.s50_5sRRNA)
            ids_5s.append(sim_data.moleculeIds.s50_fullComplex)

            bulkMolecules = TableReader(
                os.path.join(simOutDir, "BulkMolecules"))
            moleculeIds = bulkMolecules.readAttribute("objectNames")

            idx_16s = np.array([moleculeIds.index(comp) for comp in ids_16s],
                               np.int)
            idx_23s = np.array([moleculeIds.index(comp) for comp in ids_23s],
                               np.int)
            idx_5s = np.array([moleculeIds.index(comp) for comp in ids_5s],
                              np.int)

            rrn16s_count_bulk = bulkMolecules.readColumn(
                "counts")[:, idx_16s].sum(axis=1)
            rrn23s_count_bulk = bulkMolecules.readColumn(
                "counts")[:, idx_23s].sum(axis=1)
            rrn5s_count_bulk = bulkMolecules.readColumn("counts")[:,
                                                                  idx_5s].sum(
                                                                      axis=1)

            uniqueMoleculeCounts = TableReader(
                os.path.join(simOutDir, "UniqueMoleculeCounts"))

            ribosomeIndex = uniqueMoleculeCounts.readAttribute(
                "uniqueMoleculeIds").index("activeRibosome")
            ribosome_count_unique = uniqueMoleculeCounts.readColumn(
                "uniqueMoleculeCounts")[:, ribosomeIndex]

            rrn16s_count = rrn16s_count_bulk + ribosome_count_unique
            rrn23s_count = rrn23s_count_bulk + ribosome_count_unique
            rrn5s_count = rrn5s_count_bulk + ribosome_count_unique

            rrn16S_doubling_time = units.s * np.log(2) / (
                rrn16S_produced / timeStep / rrn16s_count)
            rrn23S_doubling_time = units.s * np.log(2) / (
                rrn23S_produced / timeStep / rrn23s_count)
            rrn5S_doubling_time = units.s * np.log(2) / (
                rrn5S_produced / timeStep / rrn5s_count)

            rrn16S_doubling_time[rrn16S_doubling_time.asNumber() ==
                                 np.inf] = np.nan * units.s
            rrn23S_doubling_time[rrn23S_doubling_time.asNumber() ==
                                 np.inf] = np.nan * units.s
            rrn5S_doubling_time[rrn5S_doubling_time.asNumber() ==
                                np.inf] = np.nan * units.s

            # Plotting

            oldLayerData = np.zeros(4)
            indexesForGen = initFinalIdx[gen * 4:(gen + 1) * 4]
            ax1.axvline(x=indexesForGen[-1] + barWidth + 0.225,
                        linewidth=2,
                        color='k',
                        linestyle='--')

            for idx, massType in enumerate(massNames):
                layerData = np.array([
                    initialComp[idx], expectedInitialComp[idx], finalComp[idx],
                    expectedFinalComp[idx]
                ])
                if gen == 0:
                    ax1.bar(left=indexesForGen,
                            height=layerData,
                            width=barWidth,
                            bottom=oldLayerData,
                            label=cleanNames[idx],
                            color=colors[idx])
                else:
                    ax1.bar(left=indexesForGen,
                            height=layerData,
                            width=barWidth,
                            bottom=oldLayerData,
                            color=colors[idx])
                oldLayerData += layerData
            ax1.set_ylim([0, 1])
            ax1.legend(prop={'size': 4})
            ax1.set_title('Mass composition')

            # Plot Euclidian distance
            distanceIdxForGen = distanceInitFinalIdx[gen * 2:(gen + 1) * 2]
            ax8.bar(distanceIdxForGen, [initialDistance, finalDistance])
            ax8.set_ylabel("Relative error")

            # Plot dry mass
            ax2.bar(indexesForGen, [
                initialDryMass,
                expectedInitialMass.asNumber(units.fg), finalDryMass,
                expectedFinalMass.asNumber(units.fg)
            ], barWidth)
            ax2.set_title('Dry mass (fg)')
            ax2.axvline(x=indexesForGen[-1] + barWidth + 0.225,
                        linewidth=2,
                        color='k',
                        linestyle='--')

            # Plot growth rate
            ax3 = plt.subplot(gs[2, :-1])
            doublingTime = np.log(2) / growthRate / 60.  # min
            avgDoublingTime = doublingTime[1:].mean()
            stdDoublingTime = doublingTime[1:].std()
            ax3.plot(time / 60., doublingTime)
            ax3.set_ylim([0, avgDoublingTime + 2 * stdDoublingTime])
            ax3.set_ylabel("Doubling time (min)")
            ax3.axvline(x=time.max() / 60.,
                        linewidth=2,
                        color='k',
                        linestyle='--')

            # Plot ribosome data
            simOutDir = os.path.join(simDir, "simOut")
            ribosomeDataFile = TableReader(
                os.path.join(simOutDir, "RibosomeData"))
            effectiveElongationRate = ribosomeDataFile.readColumn(
                "effectiveElongationRate")
            initialTime = TableReader(os.path.join(
                simOutDir, "Main")).readAttribute("initialTime")
            time = TableReader(os.path.join(simOutDir,
                                            "Main")).readColumn("time")
            ribosomeDataFile.close()

            ax4 = plt.subplot(gs[3, :-1])
            ax4.plot(time / 60.,
                     effectiveElongationRate,
                     label="Effective elongation rate",
                     linewidth=2)
            ax4.plot(time / 60., max_elongationRate * np.ones(time.size),
                     'r--')
            ax4.set_ylabel("Effective elongation\nrate (aa/s/ribosome)")
            ax4.axvline(x=time.max() / 60.,
                        linewidth=2,
                        color='k',
                        linestyle='--')

            # Plot rrn counts
            # bulkMoleculesFile = TableReader(os.path.join(simOutDir, "BulkMolecules"))
            # bulkMoleculeIds = bulkMoleculesFile.readAttribute("objectNames")
            # rrn_idx = bulkMoleculesFile.readAttribute("objectNames").index('rrn_operon')
            # rrn_counts = bulkMoleculesFile.readColumn("counts")[:, rrn_idx]
            # bulkMoleculesFile.close()

            # ax5 = plt.subplot(gs[4, :-1])
            # ax5.plot(time / 60., rrn_counts, label="Rrn operon counts", linewidth=2)
            # ax5.set_ylim([rrn_counts.min() - 1, rrn_counts.max() + 1])
            # ax5.set_ylabel("Rrn operons")
            # ax5.axvline(x = time.max() / 60., linewidth=2, color='k', linestyle='--')

            ax6 = plt.subplot(gs[5, :-1])
            ax6.plot(time / 60.,
                     rrn16S_doubling_time.asNumber(units.min),
                     linewidth=2)
            ax6.plot(time / 60.,
                     rrn23S_doubling_time.asNumber(units.min),
                     linewidth=2)
            ax6.plot(time / 60.,
                     rrn5S_doubling_time.asNumber(units.min),
                     linewidth=2)
            ax6.axvline(x=time.max() / 60.,
                        linewidth=2,
                        color='k',
                        linestyle='--')
            ax6.set_ylabel("rrn doubling time")
            ax6.set_xlabel("Time (min)")

        # Set axes labels for x-axis in plots 1 and 2
        ax8.set_xticks(initFinalIdx + barWidth / 2.)
        ax8.set_xticklabels(tickLabels)
        plt.setp(ax8.xaxis.get_majorticklabels(), rotation=70, fontsize=8)
        ax2.set_xticks(initFinalIdx + barWidth / 2.)
        ax2.set_xticklabels(tickLabels)
        plt.setp(ax2.xaxis.get_majorticklabels(), rotation=70, fontsize=8)

        ## Create log growth plot
        massNames = [
            "dryMass",
            "proteinMass",
            "rnaMass",
            "dnaMass",
        ]

        cleanNames = [
            "Dry",
            "Protein",
            "RNA",
            "DNA",
        ]

        colors = [
            "black",
            "red",
            "blue",
            "green",
        ]

        ax7 = plt.subplot(gs[2:, -1])
        simDir = firstCellLineage[
            0]  # Only considering the first generation for now
        time, massData = getMassData(simDir, massNames)
        for idx, massType in enumerate(massNames):
            ax7.plot(time / 60.,
                     np.log(massData[idx, :]),
                     label=cleanNames[idx],
                     color=colors[idx])
        ax7.legend(prop={'size': 6})
        ax7.set_xlabel('Time (min)')
        ax7.set_ylabel('log(mass in fg)')

        fig.subplots_adjust(hspace=.5, wspace=0.3)

        exportFigure(plt, plotOutDir, plotOutFileName, metadata)
        plt.close("all")
Beispiel #10
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)

		ap = AnalysisPaths(seedOutDir, multi_gen_plot = True)

		allDirs = ap.get_cells()

		###################
		#Initialize Figure:
		num_subplots = 9
		nRows = 3
		nCols = 3

		fig =plt.figure(figsize=(7,4))
		subplots_to_make = []
		for i in range(1, num_subplots+1):
			subplots_to_make.append((nRows, nCols, i))

		for nrows, ncols, plot_number in subplots_to_make:
			sub = fig.add_subplot(nrows, ncols, plot_number)

			sub.tick_params(which = 'both', direction = 'out', labelsize = 6)

			sub.spines['top'].set_visible(False)
			sub.spines['right'].set_visible(False)
			sub.spines['left'].set_position(('outward', 5))
			if plot_number < (num_subplots + 1 - nCols):
				sub.xaxis.set_ticks_position('none')
				sub.spines['bottom'].set_visible(False)
				sub.set_xticks([])
			else:
				sub.spines['bottom'].set_position(('outward', 5))

		###################

		# Load data from KB
		sim_data = cPickle.load(open(simDataFile, "rb"))
		nAvogadro = sim_data.constants.nAvogadro
		cellDensity = sim_data.constants.cellDensity
		oriC = sim_data.constants.oriCCenter.asNumber()
		terC = sim_data.constants.terCCenter.asNumber()
		genomeLength = len(sim_data.process.replication.genome_sequence)

		recruitmentColNames = sim_data.process.transcription_regulation.recruitmentColNames
		tfs = sorted(set([x.split("__")[-1] for x in recruitmentColNames
			if x.split("__")[-1] != "alpha"]))
		trpRIndex = [i for i, tf in enumerate(tfs) if tf == "CPLX-125"][0]

		tfBoundIds = [target + "__CPLX-125" for target in sim_data.tfToFC["CPLX-125"].keys()]
		synthProbIds = [target + "[c]" for target in sim_data.tfToFC["CPLX-125"].keys()]

		for simDir in allDirs:
			simOutDir = os.path.join(simDir, "simOut")
			# Load time
			initialTime = 0#TableReader(os.path.join(simOutDir, "Main")).readAttribute("initialTime")
			time = TableReader(os.path.join(simOutDir, "Main")).readColumn("time") - initialTime

			# Load mass data
			# Total cell mass is needed to compute concentrations (since we have cell density)
			# Protein mass is needed to compute the mass fraction of the proteome that is trpA
			massReader = TableReader(os.path.join(simOutDir, "Mass"))
			cellMass = units.fg * massReader.readColumn("cellMass")
			cellMass_no_conv = massReader.readColumn("cellMass")
			proteinMass = units.fg * massReader.readColumn("proteinMass")

			# Get instantanous growth rate
			growth_rate = massReader.readColumn("instantaniousGrowthRate")

			massReader.close()

			# Load data from bulk molecules
			bulkMoleculesReader = TableReader(os.path.join(simOutDir, "BulkMolecules"))
			bulkMoleculeIds = bulkMoleculesReader.readAttribute("objectNames")

			# Get the concentration of intracellular trp
			trpId = ["TRP[c]"]
			trpIndex = np.array([bulkMoleculeIds.index(x) for x in trpId])
			trpCounts = bulkMoleculesReader.readColumn("counts")[:, trpIndex].reshape(-1)
			trpMols = 1. / nAvogadro * trpCounts
			volume = cellMass / cellDensity
			trpConcentration = trpMols * 1. / volume

			# Get the promoter-bound status for all regulated genes
			tfBoundIndex = np.array([bulkMoleculeIds.index(x) for x in tfBoundIds])
			tfBoundCounts = bulkMoleculesReader.readColumn("counts")[:, tfBoundIndex]

			# Get the amount of monomeric trpA
			trpAProteinId = ["TRYPSYN-APROTEIN[c]"]
			trpAProteinIndex = np.array([bulkMoleculeIds.index(x) for x in trpAProteinId])
			trpAProteinCounts = bulkMoleculesReader.readColumn("counts")[:, trpAProteinIndex].reshape(-1)

			# Get the amount of complexed trpA
			trpABComplexId = ["TRYPSYN[c]"]
			trpABComplexIndex = np.array([bulkMoleculeIds.index(x) for x in trpABComplexId])
			trpABComplexCounts = bulkMoleculesReader.readColumn("counts")[:, trpABComplexIndex].reshape(-1)

			# Get the amount of trpA mRNA
			trpARnaId = ["EG11024_RNA[c]"]
			trpARnaIndex = np.array([bulkMoleculeIds.index(x) for x in trpARnaId])
			trpARnaCounts = bulkMoleculesReader.readColumn("counts")[:, trpARnaIndex].reshape(-1)

			#Get mass per Origin

			mass_per_oriC = TableReader(os.path.join(simOutDir, "ReplicationData")).readColumn("criticalMassPerOriC")

			#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()

			#Find the sequence index and length (to find the fork position later):
			sequenceIdx = TableReader(os.path.join(simOutDir, "ReplicationData")).readColumn("sequenceIdx")
			sequenceLength = TableReader(os.path.join(simOutDir, "ReplicationData")).readColumn("sequenceLength")
			sequenceLength[sequenceLength == -1] = np.nan

			bulkMoleculesReader.close()

			# Computations:

			# Compute total counts and concentration of trpA in monomeric and complexed form
			# (we know the stoichiometry)
			trpAProteinTotalCounts = trpAProteinCounts + 2 * trpABComplexCounts

			# Compute moving averages
			width = 100

			tfBoundCountsMA = np.array([np.convolve(tfBoundCounts[:,i],
				np.ones(width) / width, mode = "same")
					for i in range(tfBoundCounts.shape[1])]).T

			# Find the index of initialization:
			idxInit = np.where(mass_per_oriC >= 1)[0]

			# Calculate the growth rate:
			growth_rate = (1 / units.s) * growth_rate
			growth_rate = growth_rate.asNumber(1 / units.min)

			# Calculate Ribosome Concentration:
			ribosomeConcentration = ((1 / sim_data.constants.nAvogadro) * ribosomeCounts) / ((1.0 / sim_data.constants.cellDensity) * (cellMass))
			ribosomeConcentration = ribosomeConcentration.asNumber(units.umol / units.L)

			# Fork Position:
			reverseIdx = 1
			reverseCompIdx = 3
			reverseSequences = np.logical_or(sequenceIdx == reverseIdx,
				sequenceIdx == reverseCompIdx)
			sequenceLength[reverseSequences] = -1 * sequenceLength[reverseSequences]

			# Down sample dna polymerase position, every position is only plotted once here
			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

			dnap_times = []
			dnap_positions = []
			for t, pos in zip(time, sequenceLength)[::10]:
				for p in pos[np.isfinite(pos)]:
					dnap_times += [t]
					dnap_positions += [p]
			dnap_times = np.array(dnap_times)
			dnap_positions = np.array(dnap_positions)

			# Relative Rate of dNTP polymerization
			relative_rate_dNTP_poly = (sequenceIdx != -1).sum(axis = 1) / 4

			'''
			Plots:
			Note: Some of the Y axis limits are hard coded. 
			This was to make the plot have more rounded numbers for the paper. 
			These limits may change simulation to simulation though, 
			so make sure to change to be more dynamic 
			if using on a different data set.
			'''

			# Plot parameters:
			plot_line_color = '#0d71b9'
			plot_marker_color = '#ed2224'
			plot_font_size = 6

			##############################################################
			ax1 = plt.subplot(nRows, nCols, 1)
			ax1.plot(time, cellMass_no_conv, color = plot_line_color)
			ax1.plot(time[idxInit], cellMass_no_conv[idxInit],  markersize=4,
				linewidth=0, marker="o", color = plot_marker_color,
				markeredgewidth=0)
			plt.ylabel("Cell Mass\n(fg)", fontsize = plot_font_size)
			y_min_1, y_max_1 = 1000, 4600
			ax1.set_ylim([y_min_1, y_max_1])
			ax1.set_yticks([y_min_1, y_max_1])
			ax1.set_xlim([0, time.max()])
			##############################################################
			ax2 = plt.subplot(nRows, nCols, 2)
			ax2.plot(dnap_times, dnap_positions, marker='o', markersize=.5,
				linewidth=0, color = plot_line_color)
			plt.ylabel("DNA polymerase\nposition", fontsize = plot_font_size)
			ax2.set_yticks([-1 * genomeLength / 2, 0, genomeLength / 2])
			ax2.set_yticklabels(['-terC', 'oriC', '+terC'])
			ax2.set_xlim([0, time.max()])
			##############################################################
			ax3 = plt.subplot(nRows, nCols, 3)
			ax3.plot(time, trpARnaCounts, color = plot_line_color)
			plt.ylabel("TrpA mRNA\nCounts", fontsize = plot_font_size)
			y_min_3, y_max_3 = ax3.get_ylim()
			ax3.set_ylim([0, y_max_3])
			ax3.set_yticks([0, y_max_3])
			ax3.set_yticklabels([0, "%0.0f" % y_max_3])
			ax3.set_xlim([0, time.max()])
			##############################################################
			ax4 = plt.subplot(nRows, nCols, 4)
			ax4.plot(time, growth_rate, color = plot_line_color)
			plt.ylabel("Instantaneouse growth Rate", fontsize = plot_font_size)
			ax4.set_ylabel(r"$\mu$ $(\frac{gDCW}{gDCW \cdot \, min})$")
			y_min_4, y_max_4 = 0, 0.032
			ax4.set_ylim([y_min_4, y_max_4])
			ax4.set_yticks([y_min_4, y_max_4])
			ax4.set_xlim([0, time.max()])
			##############################################################
			ax5 = plt.subplot(nRows, nCols, 5)
			ax5.plot(time, relative_rate_dNTP_poly, color = plot_line_color)
			plt.ylabel("Relative rate of\ndNTP polymerization",
				fontsize = plot_font_size)
			y_min_5, y_max_5 = 0, 6.
			ax5.set_ylim([y_min_5, y_max_5])
			ax5.set_yticks([y_min_5, y_max_5])
			ax5.set_xlim([0, time.max()])
			##############################################################
			ax6 = plt.subplot(nRows, nCols, 6)
			ax6.plot(time, trpAProteinTotalCounts, color = plot_line_color)
			plt.ylabel("TrpA Counts", fontsize = plot_font_size)
			y_min_6, y_max_6 = 800, 6000.
			ax6.set_ylim([y_min_6, y_max_6])
			ax6.set_yticks([y_min_6,y_max_6])
			ax6.set_xlim([0, time.max()])
			##############################################################
			ax7 = plt.subplot(nRows, nCols, 7)
			ax7.plot(time, ribosomeConcentration, color = plot_line_color)
			ax7.set_ylim([15., 25.])
			plt.ylabel("Active Ribosome\n(umol/L)", fontsize = plot_font_size)
			y_min_7, y_max_7 = ax7.get_ylim()
			ax7.set_yticks([y_min_7, y_max_7])
			ax7.set_yticklabels(["%0.0f" % y_min_7, "%0.0f" % y_max_7])
			ax7.set_xlim([0, time.max()])
			ax7.set_xticks([0, time.max()])
			ax7.set_xticklabels([0., np.round(time.max() / 60., decimals = 0)])
			##############################################################
			ax8 = plt.subplot(nRows, nCols, 8)
			ax8.plot(time, tfBoundCountsMA, color = plot_line_color)
			#comment out color in order to see colors per generation
			plt.ylabel("TrpR Bound To Promoters\n(Moving Average)",
				fontsize = plot_font_size)
			y_min_8, y_max_8 = 0, 1
			ax8.set_ylim([y_min_8, y_max_8])
			ax8.set_yticks([y_min_8, y_max_8])
			ax8.set_xlim([0, time.max()])
			ax8.set_xticks([0, time.max()])
			ax8.set_xticklabels([0., np.round(time.max() / 60., decimals = 0)])
			##############################################################
			ax9 = plt.subplot(nRows, nCols, 9)
			ax9.plot(time, trpConcentration.asNumber(units.umol / units.L),
				color = plot_line_color)
			plt.ylabel("Internal TRP Conc.\n(uM)", fontsize = plot_font_size)
			y_min_9, y_max_9 = 0, 400
			ax9.set_ylim([y_min_9, y_max_9])
			ax9.set_yticks([y_min_9, y_max_9])
			ax9.set_yticklabels([y_min_9, y_max_9])
			ax9.set_xlim([0, time.max()])
			ax9.set_xticks([0, time.max()])
			ax9.set_xticklabels([0., np.round(time.max() / 60., decimals = 0)])
			##############################################################
		plt.tight_layout()
		exportFigure(plt, plotOutDir, plotOutFileName, metadata)
		plt.close("all")