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)

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

        rnaIds = [
            "EG10367_RNA[c]",
            "EG11036_RNA[c]",
            "EG50002_RNA[c]",
            "EG10671_RNA[c]",
            "EG50003_RNA[c]",
            "EG10669_RNA[c]",
            "EG10873_RNA[c]",
            "EG12179_RNA[c]",
            "EG10321_RNA[c]",
            "EG10544_RNA[c]",
        ]
        names = [
            "gapA - Glyceraldehyde 3-phosphate dehydrogenase",
            "tufA - Elongation factor Tu",
            "rpmA - 50S Ribosomal subunit protein L27",
            "ompF - Outer membrane protein F",
            "acpP - Apo-[acyl carrier protein]",
            "ompA - Outer membrane protein A",
            "rplL - 50S Ribosomal subunit protein L7/L12 dimer",
            "cspE - Transcription antiterminator and regulator of RNA stability",
            "fliC - Flagellin",
            "lpp - Murein lipoprotein",
        ]

        moleculeIds = bulkMolecules.readAttribute("objectNames")
        rnaIndexes = np.array([moleculeIds.index(x) for x in rnaIds], np.int)
        rnaCounts = bulkMolecules.readColumn("counts")[:, rnaIndexes]

        bulkMolecules.close()

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

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

        for subplotIdx in xrange(1, 10):

            plt.subplot(3, 3, subplotIdx)

            plt.plot(time / 60., rnaCounts[:, subplotIdx])
            plt.xlabel("Time (min)")
            plt.ylabel("mRNA counts")
            plt.title(names[subplotIdx].split(" - ")[0])

        plt.subplots_adjust(hspace=0.5, top=0.95, bottom=0.05)
        exportFigure(plt, plotOutDir, plotOutFileName, metadata)
        plt.close("all")
示例#2
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)

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

        rnaIds = [
            "G7355_RNA[c]",
            "EG11783_RNA[c]",
            "G7742_RNA[c]",
            "G6253_RNA[c]",
            "EG10632_RNA[c]",
            "EG11484_RNA[c]",
            "G7889_RNA[c]",
            "EG10997_RNA[c]",
            "EG10780_RNA[c]",
            "EG11060_RNA[c]",
        ]
        names = [
            "ypjD - Predicted inner membrane protein",
            "intA - CP4-57 prophage; integrase",
            "yrfG - Purine nucleotidase",
            "ylaC - Predicted inner membrane protein",
            "nagA - N-acetylglucosamine-6-phosphate deacetylase",
            "yigZ - Predicted elongation factor",
            "lptG - LptG (part of LPS transport system)",
            "mnmE - GTPase, involved in modification of U34 in tRNA",
            "pspE - Thiosulfate sulfurtransferase",
            "ushA - UDP-sugar hydrolase / 5'-ribonucleotidase / 5'-deoxyribonucleotidase",
        ]

        moleculeIds = bulkMolecules.readAttribute("objectNames")
        rnaIndexes = np.array([moleculeIds.index(x) for x in rnaIds], np.int)
        rnaCounts = bulkMolecules.readColumn("counts")[:, rnaIndexes]

        bulkMolecules.close()

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

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

        for subplotIdx in xrange(1, 10):

            plt.subplot(3, 3, subplotIdx)

            plt.plot(time / 60., rnaCounts[:, subplotIdx])
            plt.xlabel("Time (min)")
            plt.ylabel("mRNA counts")
            plt.title(names[subplotIdx].split(" - ")[0])

        plt.subplots_adjust(hspace=0.5, top=0.95, bottom=0.05)
        exportFigure(plt, plotOutDir, plotOutFileName, metadata)
        plt.close("all")
示例#3
0
def mp_worker(sim_dir):
	sim_out_dir = os.path.join(sim_dir, 'simOut')
	ribosome_count_avg_cell = None

	try:
		(ribosome_30s_count, ribosome_50s_count) = read_bulk_molecule_counts(
			sim_out_dir, (
				[ribosome_30s_id],
				[ribosome_50s_id]))

		unique_molecule_reader = TableReader(os.path.join(sim_out_dir, 'UniqueMoleculeCounts'))
		unique_molecule_ids = unique_molecule_reader.readAttribute('uniqueMoleculeIds')
		unique_molecule_counts = unique_molecule_reader.readColumn('uniqueMoleculeCounts')
		unique_molecule_reader.close()

		index_ribosome = unique_molecule_ids.index('activeRibosome')
		ribosome_active_count = unique_molecule_counts[:, index_ribosome]

		index_average_cell = int(len(ribosome_active_count) * CELL_CYCLE_FRACTION)
		ribosome_count_avg_cell = ribosome_active_count[index_average_cell] + min(
			ribosome_30s_count[index_average_cell],
			ribosome_50s_count[index_average_cell])

	except Exception as e:
		print('Excluded from analysis due to broken files: {}'.format(sim_out_dir))

	return ribosome_count_avg_cell
    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)

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

        moleculeIds = bulkMolecules.readAttribute("objectNames")

        waterIndex = np.array(moleculeIds.index('WATER[c]'), np.int)

        waterCount = bulkMolecules.readColumn("counts")[:, waterIndex]
        initialTime = TableReader(os.path.join(
            simOutDir, "Main")).readAttribute("initialTime")
        time = TableReader(os.path.join(
            simOutDir, "Main")).readColumn("time") - initialTime

        bulkMolecules.close()

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

        plt.plot(time / 60., waterCount, linewidth=2)
        plt.xlabel("Time (min)")
        plt.ylabel("WATER[c] counts")
        plt.title("Counts of water")

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

        ribosomeData = TableReader(os.path.join(simOutDir, "RibosomeData"))

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

        ribosomeData.close()

        plt.figure(figsize=(8.5, 11))
        plt.plot(time / 60, fractionStalled)

        plt.xlabel("Time (min)")
        plt.ylabel("Fraction of ribosomes stalled")

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

        exportFigure(plt, plotOutDir, plotOutFileName, metadata)
        plt.close("all")
def mp_worker(sim_dir):
    sim_out_dir = os.path.join(sim_dir, 'simOut')
    rnap_count_avg_cell = None

    try:
        bulk_molecule_reader = TableReader(
            os.path.join(sim_out_dir, 'BulkMolecules'))
        index_rnap = bulk_molecule_reader.readAttribute('objectNames').index(
            rnap_id)
        rnap_count = bulk_molecule_reader.readColumn('counts',
                                                     np.array([index_rnap]))

        unique_molecule_reader = TableReader(
            os.path.join(sim_out_dir, 'UniqueMoleculeCounts'))
        unique_molecule_ids = unique_molecule_reader.readAttribute(
            'uniqueMoleculeIds')
        unique_molecule_counts = unique_molecule_reader.readColumn(
            'uniqueMoleculeCounts')
        unique_molecule_reader.close()

        index_rnap = unique_molecule_ids.index('activeRnaPoly')
        rnap_active_count = unique_molecule_counts[:, index_rnap]

        index_average_cell = int(len(rnap_active_count) * CELL_CYCLE_FRACTION)
        rnap_count_avg_cell = rnap_count[
            index_average_cell] + rnap_active_count[index_average_cell]

    except Exception as e:
        print('Excluded from analysis due to broken files: {}'.format(
            sim_out_dir))

    return rnap_count_avg_cell
    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)

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

        rnaIds = [
            "EG10789_RNA[c]",
            "EG11556_RNA[c]",
            "EG12095_RNA[c]",
            "G1_RNA[c]",
            "G360_RNA[c]",
            "EG10944_RNA[c]",
            "EG12419_RNA[c]",
            "EG10372_RNA[c]",
            "EG10104_RNA[c]",
            "EG10539_RNA[c]",
        ]
        names = [
            "ptsI - PTS enzyme I",
            "talB - Transaldolase",
            "secG - SecG",
            "thiS - ThiS protein",
            "flgD - Flagellar biosynthesis",
            "serA - (S)-2-hydroxyglutarate reductase",
            "gatY - GatY",
            "gdhA - Glutamate dehydrogenase",
            "atpG - ATP synthase F1 complex - gamma subunit",
            "livJ - Branched chain amino acid ABC transporter - periplasmic binding protein",
        ]

        moleculeIds = bulkMolecules.readAttribute("objectNames")
        rnaIndexes = np.array([moleculeIds.index(x) for x in rnaIds], np.int)
        rnaCounts = bulkMolecules.readColumn("counts")[:, rnaIndexes]

        bulkMolecules.close()

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

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

        for subplotIdx in xrange(1, 10):

            plt.subplot(3, 3, subplotIdx)

            plt.plot(time / 60., rnaCounts[:, subplotIdx])
            plt.xlabel("Time (min)")
            plt.ylabel("mRNA counts")
            plt.title(names[subplotIdx].split(" - ")[0])

        plt.subplots_adjust(hspace=0.5, top=0.95, bottom=0.05)
        exportFigure(plt, plotOutDir, plotOutFileName, 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)

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

        # Get tRNA IDs and counts
        sim_data = cPickle.load(open(simDataFile, "rb"))
        isTRna = sim_data.process.transcription.rnaData["isTRna"]
        rnaIds = sim_data.process.transcription.rnaData["id"][isTRna]
        bulkMolecules = TableReader(os.path.join(simOutDir, "BulkMolecules"))
        moleculeIds = bulkMolecules.readAttribute("objectNames")
        rnaIndexes = np.array(
            [moleculeIds.index(moleculeId) for moleculeId in rnaIds], np.int)
        rnaCountsBulk = bulkMolecules.readColumn("counts")[:, rnaIndexes]
        bulkMolecules.close()

        # Plot
        fig = plt.figure(figsize=(8.5, 11))
        ax = plt.subplot(1, 1, 1)
        ax.plot(time, rnaCountsBulk)
        ax.set_xlim([time[0], time[-1]])
        ax.set_xlabel("Time (s)")
        ax.set_ylabel("Counts of tRNAs")
        ax.spines["right"].set_visible(False)
        ax.spines["top"].set_visible(False)
        ax.tick_params(right="off", top="off", which="both", direction="out")

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

		sim_data = cPickle.load(open(simDataFile, "rb"))
		isMRna = sim_data.process.transcription.rnaData["isMRna"]
		isRRna = sim_data.process.transcription.rnaData["isRRna"]
		isTRna = sim_data.process.transcription.rnaData["isTRna"]

		rnaSynthProbListener = TableReader(os.path.join(simOutDir, "RnaSynthProb"))
		rnaIds = rnaSynthProbListener.readAttribute('rnaIds')
		rnaSynthProb = rnaSynthProbListener.readColumn('rnaSynthProb')
		time = rnaSynthProbListener.readColumn('time')
		rnaSynthProbListener.close()

		mRnaSynthProb = rnaSynthProb[:, isMRna].sum(axis = 1)
		rRnaSynthProb = rnaSynthProb[:, isRRna].sum(axis = 1)
		tRnaSynthProb = rnaSynthProb[:, isTRna].sum(axis = 1)



		# Plot
		rows = 3
		cols = 1
		fig = plt.figure(figsize = (11, 8.5))
		plt.figtext(0.4, 0.96, "RNA synthesis probabilities over time", fontsize = 12)
		nMRnas = np.sum(isMRna)
		nRRnas = np.sum(isRRna)
		nTRnas = np.sum(isTRna)
		subplotOrder = [mRnaSynthProb, rRnaSynthProb, tRnaSynthProb]
		subplotTitles = ["mRNA\n(sum of %s mRNAs)" % nMRnas, "rRNA\n(sum of %s rRNAs)" % nRRnas, "tRNA\n(sum of %s tRNAs)" % nTRnas]

		for index, rnaSynthProb in enumerate(subplotOrder):
			ax = plt.subplot(rows, cols, index + 1)
			ax.plot(time, rnaSynthProb)

			ax.set_title(subplotTitles[index], fontsize = 10)
			ymin = np.min(rnaSynthProb)
			ymax = np.max(rnaSynthProb)
			yaxisBuffer = np.around(1.2*(ymax - ymin), 3)
			ax.set_ylim([ymin, yaxisBuffer])
			ax.set_yticks([ymin, ymax, yaxisBuffer])
			ax.set_yticklabels([ymin, np.around(ymax, 3), yaxisBuffer], fontsize = 10)
			ax.set_xlim([time[0], time[-1]])
			ax.tick_params(axis = "x", labelsize = 10)
			ax.spines["left"].set_visible(False)
			ax.spines["right"].set_visible(False)


		plt.subplots_adjust(hspace = 0.5, )
		exportFigure(plt, plotOutDir, plotOutFileName, 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)

		sim_data = cPickle.load(open(simDataFile))

		# Get exchange flux data
		fbaResults = TableReader(os.path.join(simOutDir, "FBAResults"))
		initialTime = units.s * TableReader(os.path.join(simOutDir, "Main")).readAttribute("initialTime")
		time = units.s * TableReader(os.path.join(simOutDir, "Main")).readColumn("time") - initialTime
		externalExchangeFluxes = fbaResults.readColumn("externalExchangeFluxes")
		externalMoleculeIDs = np.array(fbaResults.readAttribute("externalMoleculeIDs"))
		fbaResults.close()

		massExchange = sim_data.getter.getMass(externalMoleculeIDs).asNumber(units.g / units.mmol) * externalExchangeFluxes # g / gDCW-hr

		# Get growth rate data
		growthRateData = TableReader(os.path.join(simOutDir, "Mass"))
		growthRate = ((1 / units.s) * growthRateData.readColumn("instantaniousGrowthRate")).asUnit(1 / units.h) # g / gDCW-hr
		doublingTime = (1 / growthRate) * np.log(2)

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

		ax1 = plt.subplot(3,1,1)
		ax1.plot(time.asNumber(units.min), doublingTime.asNumber(units.min))
		ax1.plot(time.asNumber(units.min), sim_data.doubling_time.asNumber(units.min) * np.ones(time.asNumber().size), linestyle='--')
		medianDoublingTime = np.median(doublingTime.asNumber(units.min)[1:])
		ax1.set_ylim([medianDoublingTime - 2*medianDoublingTime, medianDoublingTime + 2*medianDoublingTime])
		ax1.set_ylabel("Doubling\ntime (min)")

		ax2 = plt.subplot(3,1,2)
		ax2.plot(time.asNumber(units.min), massExchange)
		maxMassExchange = massExchange[100:].max()
		minMassExchange = massExchange[100:].min()
		ax2.set_ylim([minMassExchange, maxMassExchange])
		ax2.set_ylabel("Mass exchange\n(g / gDCW-hr)")

		ax3 = plt.subplot(3,1,3)
		water = massExchange[:, np.where(externalMoleculeIDs == "WATER[p]")[0][0]].copy()
		waterAll = massExchange[:, np.where(externalMoleculeIDs == "WATER[p]")[0][0]].copy()
		water[doublingTime.asNumber() > 0.] = np.nan
		ax3.plot(time.asNumber(units.min), waterAll, 'k.')
		ax3.plot(time.asNumber(units.min), water, 'b.')
		maxMassExchange = massExchange[100:].max()
		minMassExchange = massExchange[100:].min()
		ax3.set_ylim([minMassExchange, maxMassExchange])
		ax3.set_ylabel("Water exchange\nwhen doubling time < 0")

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

        # Get the names of rnas from the KB

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

        rnaIds = sim_data.process.transcription.rnaData["id"][
            sim_data.relation.rnaIndexToMonomerMapping]

        proteinIds = sim_data.process.translation.monomerData["id"]

        bulkMolecules = TableReader(os.path.join(simOutDir, "BulkMolecules"))
        bulkMoleculeCounts = bulkMolecules.readColumn("counts")

        moleculeIds = bulkMolecules.readAttribute("objectNames")

        rnaIndexes = np.array(
            [moleculeIds.index(moleculeId) for moleculeId in rnaIds], np.int)

        rnaCountsBulk = bulkMoleculeCounts[:, rnaIndexes]

        proteinIndexes = np.array(
            [moleculeIds.index(moleculeId) for moleculeId in proteinIds],
            np.int)

        proteinCountsBulk = bulkMoleculeCounts[:, proteinIndexes]

        bulkMolecules.close()

        relativeMRnaCounts = rnaCountsBulk[
            -1, :]  #/ rnaCountsBulk[-1, :].sum()
        relativeProteinCounts = proteinCountsBulk[
            -1, :]  #/ proteinCountsBulk[-1, :].sum()

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

        plt.plot(relativeMRnaCounts,
                 relativeProteinCounts,
                 'o',
                 markeredgecolor='k',
                 markerfacecolor='none')

        plt.xlabel("RNA count (at final time step)")
        plt.ylabel("Protein count (at final time step)")

        # plt.show()

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

		sim_data = cPickle.load(open(simDataFile, "r"))
		trpIdx = sim_data.moleculeGroups.aaIDs.index("TRP[c]")

		growthLimits = TableReader(os.path.join(simOutDir, "GrowthLimits"))

		trpRequests = growthLimits.readColumn("aaRequestSize")[BURN_IN:, trpIdx]

		growthLimits.close()

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

		moleculeIds = bulkMolecules.readAttribute("objectNames")

		trpSynIdx = moleculeIds.index("TRYPSYN[c]")

		trpSynCounts = bulkMolecules.readColumn("counts")[BURN_IN:, trpSynIdx]

		bulkMolecules.close()

		trpSynKcat = 2**( (37. - 25.) / 10.) * 4.1 # From PMID 6402362 (kcat of 4.1/s measured at 25 C)

		initialTime = TableReader(os.path.join(simOutDir, "Main")).readAttribute("initialTime")
		time = TableReader(os.path.join(simOutDir, "Main")).readColumn("time")[BURN_IN:] - initialTime
		timeStep = TableReader(os.path.join(simOutDir, "Main")).readColumn("timeStepSec")[BURN_IN:]


		trpSynMaxCapacity = trpSynKcat * trpSynCounts * timeStep

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

		plt.subplot(3, 1, 1)

		plt.plot(time / 60., trpSynMaxCapacity, linewidth = 2)
		plt.ylabel("Tryptophan Synthase Max Capacity")

		plt.subplot(3, 1, 2)

		plt.plot(time / 60., trpRequests, linewidth = 2)
		plt.ylabel("TRP requested by translation")

		plt.subplot(3, 1, 3)

		plt.plot(time / 60., trpSynMaxCapacity / trpRequests, linewidth = 2)
		plt.xlabel("Time (min)")
		plt.ylabel("(Max capacity) / (Request)")

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

		bulkMolecules = TableReader(os.path.join(simOutDir, "BulkMolecules"))
		bulkMoleculeCounts = bulkMolecules.readColumn("counts")

		moleculeIds = bulkMolecules.readAttribute("objectNames")
		rnapId = "APORNAP-CPLX[c]"
		rnapIndex = moleculeIds.index(rnapId)
		rnapCountsBulk = bulkMoleculeCounts[:, rnapIndex]

		RNAP_RNA_IDS = ["EG10893_RNA[c]", "EG10894_RNA[c]", "EG10895_RNA[c]", "EG10896_RNA[c]"]
		rnapRnaIndexes = np.array([moleculeIds.index(rnapRnaId) for rnapRnaId in RNAP_RNA_IDS], np.int)
		rnapRnaCounts = bulkMoleculeCounts[:, rnapRnaIndexes]

		bulkMolecules.close()

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

		rnapIndex = uniqueMoleculeCounts.readAttribute("uniqueMoleculeIds").index("activeRnaPoly")
		initialTime = TableReader(os.path.join(simOutDir, "Main")).readAttribute("initialTime")
		time = TableReader(os.path.join(simOutDir, "Main")).readColumn("time") - initialTime
		nActive = uniqueMoleculeCounts.readColumn("uniqueMoleculeCounts")[:, rnapIndex]

		uniqueMoleculeCounts.close()

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

		plt.subplot(5, 1, 1)

		plt.plot(time / 60., nActive + rnapCountsBulk)
		plt.xlabel("Time (min)")
		plt.ylabel("Protein Counts")
		plt.title("RNA Polymerase")

		for subplotIdx in xrange(2, 6):
			rnapRnaCountsIdx = subplotIdx - 2

			plt.subplot(5, 1, subplotIdx)

			plt.plot(time / 60., rnapRnaCounts[:, rnapRnaCountsIdx])
			plt.xlabel("Time (min)")
			plt.ylabel("mRNA counts")
			plt.title(RNAP_RNA_IDS[rnapRnaCountsIdx])

		plt.subplots_adjust(hspace = 0.5, top = 0.95, bottom = 0.05)
		exportFigure(plt, plotOutDir, plotOutFileName, 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)

		# Get the names of rnas from the KB

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

		isTRna = sim_data.process.transcription.rnaData["isTRna"]

		rnaIds = sim_data.process.transcription.rnaData["id"][isTRna]

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

		moleculeIds = bulkMolecules.readAttribute("objectNames")

		rnaIndexes = np.array([moleculeIds.index(moleculeId) for moleculeId in rnaIds], np.int)

		rnaCountsBulk = bulkMolecules.readColumn("counts")[:, rnaIndexes]

		bulkMolecules.close()

		# avgCounts = rnaCountsBulk.mean(0)

		# relativeCounts = avgCounts / avgCounts.sum()

		# relativeCounts = rnaCountsBulk[-1, :] / rnaCountsBulk[-1, :].sum()

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

		counts = rnaCountsBulk[-1, :]

		expectedCountsArbitrary = sim_data.process.transcription.rnaExpression[sim_data.condition][isTRna]

		expectedCounts = expectedCountsArbitrary/expectedCountsArbitrary.sum() * counts.sum()

		maxLine = 1.1 * max(expectedCounts.max(), counts.max())
		plt.plot([0, maxLine], [0, maxLine], '--r')
		plt.plot(expectedCounts, counts, 'o', markeredgecolor = 'k', markerfacecolor = 'none')

		plt.xlabel("Expected tRNA count (scaled to total)")
		plt.ylabel("Actual tRNA count (at final time step)")

		# plt.show()

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

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

		atpAllocatedInitial = bulkMolecules.readColumn("atpAllocatedInitial")
		atpRequested = bulkMolecules.readColumn("atpRequested")

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

		bulkMolecules.close()


		# Plot
		plt.figure(figsize = (8.5, 11))
		rows = 7
		cols = 2

		for processIndex in np.arange(len(processNames)):
			ax = plt.subplot(rows, cols, processIndex + 1)
			ax.plot(time / 60., atpAllocatedInitial[:, processIndex])
			ax.plot(time / 60., atpRequested[:, processIndex])
			ax.set_title(str(processNames[processIndex]), fontsize = 8, y = 0.85)

			ymin = np.amin([atpAllocatedInitial[:, processIndex], atpRequested[:, processIndex]])
			ymax = np.amax([atpAllocatedInitial[:, processIndex], atpRequested[:, processIndex]])
			ax.set_ylim([ymin, ymax])
			ax.set_yticks([ymin, ymax])
			ax.set_yticklabels(["%0.2e" % ymin, "%0.2e" % ymax])
			ax.spines['top'].set_visible(False)
			ax.spines['bottom'].set_visible(False)
			ax.xaxis.set_ticks_position('bottom')
			ax.tick_params(which = 'both', direction = 'out', labelsize = 6)
			# ax.set_xticks([])

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

		plt.subplots_adjust(hspace = 2.0, wspace = 2.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)

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

		fba_results = TableReader(os.path.join(simOutDir, "FBAResults"))
		exFlux = fba_results.readColumn("externalExchangeFluxes")
		exMolec = fba_results.readAttribute("externalMoleculeIDs")
		fba_results.close()

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

		exchangeMasses = {} # some duplicates in exMolec like CO2 and water so create dict to avoid double counting
		raw_data = KnowledgeBaseEcoli()
		for metabolite in raw_data.metabolites:
			for molecID in exMolec:
				if molecID.split("[")[0] == "WATER":
					exchangeMasses[molecID] = 18.015 * exFlux[:,exMolec.index(molecID)] * 10**-3 * cellMass * timeStepSec / 60 / 60
				if molecID.split("[")[0] == metabolite["id"]:
					exchangeMasses[molecID] = metabolite["mw7.2"] * exFlux[:,exMolec.index(molecID)] * 10**-3 * cellMass * timeStepSec / 60 / 60

		massInflux = 0
		for molecID in exchangeMasses.keys():
			massInflux += exchangeMasses[molecID]

		massProduced = processMassDifferences[:,0]	# in metabolism
		massDiff = massInflux + massProduced

		plt.plot(time / 60. / 60., -massDiff)
		plt.tick_params(axis='both', which='major', labelsize=10)
		plt.ylabel("Mass Accumulation per time step (fg)")
		plt.title("Mass imported - mass created in metabolism process")

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

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

		fbaResults = TableReader(os.path.join(simOutDir, "FBAResults"))
		externalExchangeFluxes = fbaResults.readColumn("externalExchangeFluxes")
		initialTime = TableReader(os.path.join(simOutDir, "Main")).readAttribute("initialTime")
		time = TableReader(os.path.join(simOutDir, "Main")).readColumn("time") - initialTime
		timeStepSec = TableReader(os.path.join(simOutDir, "Main")).readColumn("timeStepSec")
		externalMoleculeIDs = np.array(fbaResults.readAttribute("externalMoleculeIDs"))
		fbaResults.close()

		if GLUCOSE_ID not in externalMoleculeIDs:
			print "This plot only runs when glucose is the carbon source."
			return

		glucoseIdx = np.where(externalMoleculeIDs == GLUCOSE_ID)[0][0]
		glucoseFlux = FLUX_UNITS * externalExchangeFluxes[:, glucoseIdx]

		mass = TableReader(os.path.join(simOutDir, "Mass"))
		cellMass = MASS_UNITS * mass.readColumn("cellMass")
		cellDryMass = MASS_UNITS * mass.readColumn("dryMass")
		growth = GROWTH_UNITS * mass.readColumn("growth") / timeStepSec
		mass.close()

		glucoseMW = sim_data.getter.getMass([GLUCOSE_ID])[0]

		glucoseMassFlux = glucoseFlux * glucoseMW * cellDryMass

		glucoseMassYield = growth / -glucoseMassFlux

		fig = plt.figure(figsize = (8.5, 11))
		plt.plot(time, glucoseMassYield)
		plt.xlabel("Time (s)")
		plt.ylabel("g cell / g glucose")

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

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

        moleculeIds = bulkMolecules.readAttribute("objectNames")

        sim_data = cPickle.load(open(simDataFile))

        aaIDs = sim_data.moleculeGroups.aaIDs
        aaIndexes = np.array([moleculeIds.index(aaId) for aaId in aaIDs],
                             np.int)
        aaCounts = bulkMolecules.readColumn("counts")[:, aaIndexes]

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

        bulkMolecules.close()

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

        for idx in xrange(21):

            plt.subplot(6, 4, idx + 1)

            plt.plot(time / 60., aaCounts[:, idx], linewidth=2)
            plt.xlabel("Time (min)")
            plt.ylabel("Counts")
            plt.title(aaIDs[idx])

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

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

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

        moleculeIds = bulkMolecules.readAttribute("objectNames")

        NTP_IDS = ['ATP[c]', 'CTP[c]', 'GTP[c]', 'UTP[c]']
        ntpIndexes = np.array([moleculeIds.index(ntpId) for ntpId in NTP_IDS],
                              np.int)

        ntpCounts = bulkMolecules.readColumn("counts")[:, ntpIndexes]
        initialTime = TableReader(os.path.join(
            simOutDir, "Main")).readAttribute("initialTime")
        time = TableReader(os.path.join(
            simOutDir, "Main")).readColumn("time") - initialTime

        bulkMolecules.close()

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

        for idx in xrange(4):

            plt.subplot(2, 2, idx + 1)

            plt.plot(time / 60., ntpCounts[:, idx], linewidth=2)
            plt.xlabel("Time (min)")
            plt.ylabel("Counts")
            plt.title(NTP_IDS[idx])

        print "NTPs required for cell division (nt/cell-cycle) = %d" % sum(
            ntpCounts[0, :])
        plt.subplots_adjust(hspace=0.5)

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

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

        moleculeIds = bulkMolecules.readAttribute("objectNames")
        rnapId = "APORNAP-CPLX[c]"
        rnapIndex = moleculeIds.index(rnapId)
        rnapCountsBulk = bulkMolecules.readColumn("counts")[:, rnapIndex]

        bulkMolecules.close()

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

        rnapIndex = uniqueMoleculeCounts.readAttribute(
            "uniqueMoleculeIds").index("activeRnaPoly")
        initialTime = TableReader(os.path.join(
            simOutDir, "Main")).readAttribute("initialTime")
        time = TableReader(os.path.join(
            simOutDir, "Main")).readColumn("time") - initialTime
        nActive = uniqueMoleculeCounts.readColumn(
            "uniqueMoleculeCounts")[:, rnapIndex]

        uniqueMoleculeCounts.close()

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

        plt.plot(time / 60., nActive * 100. / (nActive + rnapCountsBulk))
        #plt.axis([0,60,0,25])
        plt.xlabel("Time (min)")
        plt.ylabel("Percent of RNA Polymerase Molecules that are Active")
        plt.title("Active RNA Polymerase Percentage")

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

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

		moleculeIds = bulkMolecules.readAttribute("objectNames")

		RIBOSOME_RNA_IDS = ["RRLA-RRNA[c]", "RRSA-RRNA[c]", "RRFA-RRNA[c]"]
		ribosomeRnaIndexes = np.array([moleculeIds.index(rRnaId) for rRnaId in RIBOSOME_RNA_IDS], np.int)
		ribosomeRnaCountsBulk = bulkMolecules.readColumn("counts")[:, ribosomeRnaIndexes]

		bulkMolecules.close()

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

		ribosomeIndex = uniqueMoleculeCounts.readAttribute("uniqueMoleculeIds").index("activeRibosome")
		initialTime = TableReader(os.path.join(simOutDir, "Main")).readAttribute("initialTime")
		time = TableReader(os.path.join(simOutDir, "Main")).readColumn("time") - initialTime
		nActive = uniqueMoleculeCounts.readColumn("uniqueMoleculeCounts")[:, ribosomeIndex]

		uniqueMoleculeCounts.close()

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

		plt.plot(time / 60, nActive)
		plt.plot([time[0] / 60., time[-1] / 60.], [2 * nActive[0], 2 * nActive[0]], "r--")
		plt.xlabel("Time (min)")
		plt.ylabel("Counts")
		plt.title("Active Ribosomes Final:Initial = %0.2f" % (nActive[-1] / float(nActive[0])))

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

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

        cellDensity = sim_data.constants.cellDensity

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

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

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

        dryMassFracAverage = np.mean(dryMass / cellMass)

        toya_reactions = validation_data.reactionFlux.toya2010fluxes[
            "reactionID"]
        toya_fluxes = FLUX_UNITS * np.array(
            [(dryMassFracAverage * cellDensity * x).asNumber(FLUX_UNITS) for x
             in validation_data.reactionFlux.toya2010fluxes["reactionFlux"]])

        netFluxes = []
        for toyaReactionID in toya_reactions:
            fluxTimeCourse = net_flux(
                toyaReactionID, reactionIDs,
                reactionFluxes).asNumber(FLUX_UNITS).squeeze()
            netFluxes.append(fluxTimeCourse)

        trimmedReactions = FLUX_UNITS * np.array(netFluxes)

        corrCoefTimecourse = []
        for fluxes in trimmedReactions.asNumber(FLUX_UNITS).T:
            correlationCoefficient = np.corrcoef(
                fluxes, toya_fluxes.asNumber(FLUX_UNITS))[0, 1]
            corrCoefTimecourse.append(correlationCoefficient)

        meanCorr = np.mean(
            np.array(corrCoefTimecourse)[~np.isnan(corrCoefTimecourse)])

        plt.figure()
        plt.plot(time / 60., corrCoefTimecourse)
        plt.axhline(y=meanCorr, color='r')
        plt.title("Measured vs. Simulated Central Carbon Fluxes")
        plt.text(.5 * np.max(time / 60.),
                 .95 * meanCorr,
                 "Mean = {:.2}".format(meanCorr),
                 horizontalalignment="center")
        plt.xlabel("Time (min)")
        plt.ylabel("Pearson R")

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

        # Load data from KB
        sim_data = cPickle.load(open(simDataFile, "rb"))

        endoRnaseIds = sim_data.process.rna_decay.endoRnaseIds
        exoRnaseIds = sim_data.moleculeGroups.exoRnaseIds
        RnaseIds = np.concatenate((endoRnaseIds, exoRnaseIds))

        # Load count data for s30 proteins, rRNA, and final 30S complex
        bulkMolecules = TableReader(os.path.join(simOutDir, "BulkMolecules"))
        moleculeIds = bulkMolecules.readAttribute("objectNames")
        bulkMoleculeCounts = bulkMolecules.readColumn("counts")

        # Get indexes
        proteinIndexes = np.array(
            [moleculeIds.index(protein) for protein in RnaseIds], np.int)
        exoproteinIndexes = np.array(
            [moleculeIds.index(protein) for protein in exoRnaseIds], np.int)
        endoproteinIndexes = np.array(
            [moleculeIds.index(protein) for protein in endoRnaseIds], np.int)

        # Load data
        initialTime = TableReader(os.path.join(
            simOutDir, "Main")).readAttribute("initialTime")
        time = TableReader(os.path.join(
            simOutDir, "Main")).readColumn("time") - initialTime
        RnaseCounts = bulkMoleculeCounts[:, proteinIndexes]

        exoRnaseCounts = bulkMoleculeCounts[:, exoproteinIndexes]
        endoRnaseCounts = bulkMoleculeCounts[:, endoproteinIndexes]
        bulkMolecules.close()

        rnaDegradationListenerFile = TableReader(
            os.path.join(simOutDir, "RnaDegradationListener"))
        countRnaDegraded = rnaDegradationListenerFile.readColumn(
            'countRnaDegraded')
        nucleotidesFromDegradation = rnaDegradationListenerFile.readColumn(
            'nucleotidesFromDegradation')
        FractionActiveEndoRNases = rnaDegradationListenerFile.readColumn(
            'FractionActiveEndoRNases')
        DiffRelativeFirstOrderDecay = rnaDegradationListenerFile.readColumn(
            'DiffRelativeFirstOrderDecay')
        FractEndoRRnaCounts = rnaDegradationListenerFile.readColumn(
            'FractEndoRRnaCounts')
        fragmentBasesDigested = rnaDegradationListenerFile.readColumn(
            'fragmentBasesDigested')
        rnaDegradationListenerFile.close()

        TranscriptElongationListenerFile = TableReader(
            os.path.join(simOutDir, "TranscriptElongationListener"))
        countNTPsUSed = TranscriptElongationListenerFile.readColumn(
            'countNTPsUSed')
        countRnaSynthesized = TranscriptElongationListenerFile.readColumn(
            'countRnaSynthesized')
        TranscriptElongationListenerFile.close()

        totalRnaseCounts = RnaseCounts.sum(axis=1)
        requiredRnaseTurnover = nucleotidesFromDegradation / RnaseCounts.sum(
            axis=1)

        totalexoRnaseCounts = exoRnaseCounts.sum(axis=1)
        totalendoRnaseCounts = endoRnaseCounts.sum(axis=1)

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

        # Translation
        gtpUsed = growthLimitsDataFile.readColumn("gtpAllocated")
        growthLimitsDataFile.close()

        # Load metabolism production
        fbaResults = TableReader(os.path.join(simOutDir, "FBAResults"))
        initialTime = TableReader(os.path.join(
            simOutDir, "Main")).readAttribute("initialTime")
        time = TableReader(os.path.join(
            simOutDir, "Main")).readColumn("time") - initialTime
        deltaMetabolites = fbaResults.readColumn("deltaMetabolites")
        outputMoleculeIDs = np.array(
            fbaResults.readAttribute("metaboliteNames"))
        fbaResults.close()

        # Load ntps required for cell doubling
        bulkMolecules = TableReader(os.path.join(simOutDir, "BulkMolecules"))
        moleculeIds = bulkMolecules.readAttribute("objectNames")
        NTP_IDS = ['ATP[c]', 'CTP[c]', 'GTP[c]', 'UTP[c]']
        ntpIndexes = np.array([moleculeIds.index(ntpId) for ntpId in NTP_IDS],
                              np.int)
        ntpCounts = bulkMoleculeCounts[:, ntpIndexes]
        bulkMolecules.close()

        # Plotting
        plt.figure(figsize=(8.5, 11))
        plt.rc('font', **FONT)
        max_yticks = 5

        ax = plt.subplot(7, 2, 1)
        plt.plot(time / 60., countRnaSynthesized.sum(axis=1))
        plt.ylabel("RNAs synthesized", fontsize=9)
        yloc = plt.MaxNLocator(max_yticks)
        ax.yaxis.set_major_locator(yloc)

        ax = plt.subplot(7, 2, 2)
        plt.plot(time / 60., gtpUsed / 1e6)
        plt.ylabel("Translation ($10^{%d}$nt)" % 6, fontsize=9)
        plt.title("GTPs needed (x$10^{%d}$) = %.2f" % (6,
                                                       (gtpUsed.sum() / 1e6)),
                  fontsize=9)
        yloc = plt.MaxNLocator(max_yticks)
        ax.yaxis.set_major_locator(yloc)

        ax = plt.subplot(7, 2, 3)
        plt.plot(time / 60., countRnaDegraded.sum(axis=1))
        plt.ylabel("RNAs degraded", fontsize=9)
        yloc = plt.MaxNLocator(max_yticks)
        ax.yaxis.set_major_locator(yloc)

        ax = plt.subplot(7, 2, 5)
        plt.plot(time / 60., totalendoRnaseCounts)
        plt.ylabel("EndoRNase counts", fontsize=9)
        yloc = plt.MaxNLocator(max_yticks)
        ax.yaxis.set_major_locator(yloc)

        ax = plt.subplot(7, 2, 4)
        plt.plot(time / 60., countNTPsUSed / 1e6)
        plt.ylabel("Transcription ($10^{%d}$nt)" % 6, fontsize=9)
        plt.title("NTPs needed(x$10^{%d}$) = %.2f" %
                  (6, (countNTPsUSed.sum() / 1e6)),
                  fontsize=9)
        yloc = plt.MaxNLocator(max_yticks)
        ax.yaxis.set_major_locator(yloc)

        ax = plt.subplot(7, 2, 7)
        plt.plot(time / 60., totalexoRnaseCounts)
        plt.ylabel("ExoRNase counts", fontsize=9)
        yloc = plt.MaxNLocator(max_yticks)
        ax.yaxis.set_major_locator(yloc)

        IdxAtp = (np.where("ATP[c]" == outputMoleculeIDs))[0][0]
        ATP = np.sum(deltaMetabolites[:, IdxAtp])
        IdxGtp = (np.where("GTP[c]" == outputMoleculeIDs))[0][0]
        GTP = np.sum(deltaMetabolites[:, IdxGtp])
        IdxCtp = (np.where("CTP[c]" == outputMoleculeIDs))[0][0]
        CTP = np.sum(deltaMetabolites[:, IdxCtp])
        IdxUtp = (np.where("UTP[c]" == outputMoleculeIDs))[0][0]
        UTP = np.sum(deltaMetabolites[:, IdxUtp])
        NtpsProduced = ATP + GTP + CTP + UTP
        ax = plt.subplot(7, 2, 6)
        plt.plot(time / 60.,
                 (deltaMetabolites[:, IdxAtp] + deltaMetabolites[:, IdxGtp] +
                  deltaMetabolites[:, IdxCtp] + deltaMetabolites[:, IdxUtp]) /
                 1e6)
        plt.ylabel("Metabolism ($10^{%d}$nt)" % 6, fontsize=9)
        plt.title(
            "NTPs produced (x$10^{%d}$) = %.2f" %
            (6,
             (sum(deltaMetabolites[:, IdxAtp] + deltaMetabolites[:, IdxGtp] +
                  deltaMetabolites[:, IdxCtp] + deltaMetabolites[:, IdxUtp]) /
              1e6)),
            fontsize=9)
        yloc = plt.MaxNLocator(max_yticks)
        ax.yaxis.set_major_locator(yloc)

        ax = plt.subplot(7, 2, 9)
        plt.plot(time / 60., FractionActiveEndoRNases * 100)
        plt.ylabel("EndoRN capacity (%)", fontsize=9)
        yloc = plt.MaxNLocator(max_yticks)
        ax.yaxis.set_major_locator(yloc)

        ax = plt.subplot(7, 2, 8)
        plt.plot(time / 60., fragmentBasesDigested / 1e6)
        plt.ylabel("Exo-digestion ($10^{%d}$nt)" % 6, fontsize=9)
        plt.title("NTPs recycled (x$10^{%d}$) = %.2f" %
                  (6, (fragmentBasesDigested.sum() / 1e6)),
                  fontsize=9)
        yloc = plt.MaxNLocator(max_yticks)
        ax.yaxis.set_major_locator(yloc)

        ax = plt.subplot(7, 2, 11)
        plt.plot(time / 60., DiffRelativeFirstOrderDecay)
        plt.ylabel("sum(Residuals)", fontsize=9)
        yloc = plt.MaxNLocator(max_yticks)
        ax.yaxis.set_major_locator(yloc)

        ax = plt.subplot(7, 2, 10)
        plt.plot(time / 60., (ntpCounts[:, 0] + ntpCounts[:, 1] +
                              ntpCounts[:, 2] + ntpCounts[:, 3]) / 1e6)
        plt.ylabel("Net production ($10^{%d}$nt)" % 6, fontsize=9)
        plt.title("NTPs required for cell division (x$10^{%d}$) = %.2f" %
                  (6, ((ntpCounts[0, 0] + ntpCounts[0, 1] + ntpCounts[0, 2] +
                        ntpCounts[0, 3]) / 1e6)),
                  fontsize=9)
        yloc = plt.MaxNLocator(max_yticks)
        ax.yaxis.set_major_locator(yloc)

        # compute active ExoRNase capacity (%)
        ActiveExoRNcapacity = fragmentBasesDigested.astype(float) / (
            totalexoRnaseCounts *
            sim_data.constants.KcatExoRNase.asNumber()) * 100

        ax = plt.subplot(7, 2, 13)
        plt.plot(time / 60., ActiveExoRNcapacity)
        plt.xlabel("Time (min)")
        plt.ylabel("ExoRN capacity (%)", fontsize=9)
        yloc = plt.MaxNLocator(max_yticks)
        ax.yaxis.set_major_locator(yloc)

        # compute instantaneous balance of nTPs
        InstantaneousNTPs = -gtpUsed - countNTPsUSed + (
            deltaMetabolites[:, IdxAtp] + deltaMetabolites[:, IdxGtp] +
            deltaMetabolites[:, IdxCtp] +
            deltaMetabolites[:, IdxUtp]) + fragmentBasesDigested

        ax = plt.subplot(7, 2, 12)
        plt.plot(time / 60., InstantaneousNTPs / 1e6)
        plt.xlabel("Time (min)")
        plt.ylabel("Balance ($10^{%d}$nt)" % 6, fontsize=9)
        plt.title("Average instantaneous balance (x$10^{%d}$) = %.4f" %
                  (6, (np.mean(InstantaneousNTPs) / 1e6)),
                  fontsize=9)
        yloc = plt.MaxNLocator(max_yticks)
        ax.yaxis.set_major_locator(yloc)

        plt.subplots_adjust(hspace=0.6, wspace=0.35)

        exportFigure(plt, plotOutDir, plotOutFileName, metadata)
示例#24
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 data from KB
        sim_data = cPickle.load(open(simDataFile, "rb"))
        nAvogadro = sim_data.constants.nAvogadro
        cellDensity = sim_data.constants.cellDensity

        # Load time
        initialTime = 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 tyrA
        massReader = TableReader(os.path.join(simOutDir, "Mass"))

        cellMass = units.fg * massReader.readColumn("cellMass")
        proteinMass = units.fg * massReader.readColumn("proteinMass")

        massReader.close()

        # Load data from bulk molecules
        bulkMoleculesReader = TableReader(
            os.path.join(simOutDir, "BulkMolecules"))
        bulkMoleculeIds = bulkMoleculesReader.readAttribute("objectNames")
        # Get the concentration of intracellular phe
        pheId = ["PHE[c]"]
        pheIndex = np.array([bulkMoleculeIds.index(x) for x in pheId])
        pheCounts = bulkMoleculesReader.readColumn(
            "counts")[:, pheIndex].reshape(-1)
        pheMols = 1. / nAvogadro * pheCounts
        volume = cellMass / cellDensity
        pheConcentration = pheMols * 1. / volume

        # Get the amount of active tyrR (that isn't promoter bound)
        tyrRActiveId = ["MONOMER0-162[c]"]
        tyrRActiveIndex = np.array(
            [bulkMoleculeIds.index(x) for x in tyrRActiveId])
        tyrRActiveCounts = bulkMoleculesReader.readColumn(
            "counts")[:, tyrRActiveIndex].reshape(-1)

        # Get the amount of inactive tyrR
        tyrRInactiveId = ["PD00413[c]"]
        tyrRInactiveIndex = np.array(
            [bulkMoleculeIds.index(x) for x in tyrRInactiveId])
        tyrRInactiveCounts = bulkMoleculesReader.readColumn(
            "counts")[:, tyrRInactiveIndex].reshape(-1)

        # Get the promoter-bound status of the tyrA gene
        tyrATfBoundId = ["EG11039_RNA__MONOMER0-162"]
        tyrATfBoundIndex = np.array(
            [bulkMoleculeIds.index(x) for x in tyrATfBoundId])
        tyrATfBoundCounts = bulkMoleculesReader.readColumn(
            "counts")[:, tyrATfBoundIndex].reshape(-1)

        # Get the amount of monomeric tyrA
        tyrAProteinId = ["CHORISMUTPREPHENDEHYDROG-MONOMER[c]"]
        tyrAProteinIndex = np.array(
            [bulkMoleculeIds.index(x) for x in tyrAProteinId])
        tyrAProteinCounts = bulkMoleculesReader.readColumn(
            "counts")[:, tyrAProteinIndex].reshape(-1)

        tyrAComplexId = ["CHORISMUTPREPHENDEHYDROG-CPLX[c]"]
        tyrAComplexIndex = np.array(
            [bulkMoleculeIds.index(x) for x in tyrAComplexId])
        tyrAComplexCounts = bulkMoleculesReader.readColumn(
            "counts")[:, tyrAComplexIndex].reshape(-1)

        bulkMoleculesReader.close()

        tyrAProteinTotalCounts = tyrAProteinCounts + 2 * tyrAComplexCounts

        # Compute the tyrA mass in the cell
        tyrAMw = sim_data.getter.getMass(tyrAProteinId)
        tyrAMass = 1. / nAvogadro * tyrAProteinTotalCounts * tyrAMw

        # Compute the proteome mass fraction
        proteomeMassFraction = tyrAMass.asNumber(
            units.fg) / proteinMass.asNumber(units.fg)

        # Get the tyrA synthesis probability
        rnaSynthProbReader = TableReader(
            os.path.join(simOutDir, "RnaSynthProb"))

        rnaIds = rnaSynthProbReader.readAttribute("rnaIds")
        tyrASynthProbId = ["EG11039_RNA[c]"]
        tyrASynthProbIndex = np.array(
            [rnaIds.index(x) for x in tyrASynthProbId])
        tyrASynthProb = rnaSynthProbReader.readColumn(
            "rnaSynthProb")[:, tyrASynthProbIndex].reshape(-1)

        recruitmentColNames = sim_data.process.transcription_regulation.recruitmentColNames
        tfs = sorted(
            set([
                x.split("__")[-1] for x in recruitmentColNames
                if x.split("__")[-1] != "alpha"
            ]))
        tyrRIndex = [i for i, tf in enumerate(tfs) if tf == "MONOMER0-162"][0]
        tyrRBound = rnaSynthProbReader.readColumn("nActualBound")[:, tyrRIndex]

        rnaSynthProbReader.close()

        # Calculate total tyrR - active, inactive and bound
        tyrRTotalCounts = tyrRActiveCounts + tyrRInactiveCounts + tyrRBound

        # Compute moving averages
        width = 100

        tyrATfBoundCountsMA = np.convolve(tyrATfBoundCounts,
                                          np.ones(width) / width,
                                          mode="same")
        tyrASynthProbMA = np.convolve(tyrASynthProb,
                                      np.ones(width) / width,
                                      mode="same")

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

        ##############################################################
        ax = plt.subplot(6, 1, 1)
        ax.plot(time, pheConcentration.asNumber(units.umol / units.L))
        plt.ylabel("Internal phe Conc. [uM]", fontsize=6)

        ymin = np.amin(pheConcentration.asNumber(units.umol / units.L) * 0.9)
        ymax = np.amax(pheConcentration.asNumber(units.umol / units.L) * 1.1)
        if ymin != ymax:
            ax.set_ylim([ymin, ymax])
            ax.set_yticks([ymin, ymax])
            ax.set_yticklabels(["%0.0f" % ymin, "%0.0f" % ymax])
        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=6)
        ax.set_xticks([])
        ##############################################################

        ##############################################################
        ax = plt.subplot(6, 1, 2)
        ax.plot(time, tyrRActiveCounts)
        ax.plot(time, tyrRInactiveCounts)
        ax.plot(time, tyrRTotalCounts)
        plt.ylabel("TyrR Counts", fontsize=6)
        plt.legend(["Active", "Inactive", "Total"], fontsize=6)

        ymin = min(np.amin(tyrRActiveCounts * 0.9),
                   np.amin(tyrRInactiveCounts * 0.9))
        ymax = np.amax(tyrRTotalCounts * 1.1)
        if ymin != ymax:
            ax.set_ylim([ymin, ymax])
            ax.set_yticks([ymin, ymax])
            ax.set_yticklabels(["%0.0f" % ymin, "%0.0f" % ymax])
        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=6)
        ax.set_xticks([])
        ##############################################################

        ##############################################################
        ax = plt.subplot(6, 1, 3)
        ax.plot(time, tyrATfBoundCounts)
        ax.plot(time, tyrATfBoundCountsMA, color="g")
        plt.ylabel("TyrR Bound To tyrA Promoter", fontsize=6)

        ymin = np.amin(tyrATfBoundCounts * 1.)
        ymax = np.amax(tyrATfBoundCounts * 1.)
        if ymin != ymax:
            ax.set_ylim([ymin, ymax])
            ax.set_yticks([ymin, ymax])
            ax.set_yticklabels(["%0.0f" % ymin, "%0.0f" % ymax])
        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=6)
        ax.set_xticks([])
        ##############################################################

        ##############################################################
        ax = plt.subplot(6, 1, 4)
        ax.plot(time, tyrASynthProb)
        ax.plot(time, tyrASynthProbMA, color="g")
        plt.ylabel("tyrA Synthesis Prob.", fontsize=6)

        ymin = np.amin(tyrASynthProb[1:] * 0.9)
        ymax = np.amax(tyrASynthProb[1:] * 1.1)
        if ymin != ymax:
            ax.set_ylim([ymin, ymax])
            ax.set_yticks([ymin, ymax])
            ax.set_yticklabels(["%0.2e" % ymin, "%0.2e" % ymax])
        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=6)
        ax.set_xticks([])
        ##############################################################

        ##############################################################
        ax = plt.subplot(6, 1, 5)
        ax.plot(time, tyrAProteinTotalCounts)
        plt.ylabel("TyrA Counts", fontsize=6)

        ymin = np.amin(tyrAProteinTotalCounts * 0.9)
        ymax = np.amax(tyrAProteinTotalCounts * 1.1)
        if ymin != ymax:
            ax.set_ylim([ymin, ymax])
            ax.set_yticks([ymin, ymax])
            ax.set_yticklabels(["%0.0f" % ymin, "%0.0f" % ymax])
        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=6)
        ax.set_xticks([])
        ##############################################################

        ##############################################################
        ax = plt.subplot(6, 1, 6)
        ax.plot(time, proteomeMassFraction)
        plt.ylabel("TyrA Mass Fraction of Proteome", fontsize=6)

        ymin = np.amin(proteomeMassFraction * 0.9)
        ymax = np.amax(proteomeMassFraction * 1.1)
        if ymin != ymax:
            ax.set_ylim([ymin, ymax])
            ax.set_yticks([ymin, ymax])
            ax.set_yticklabels(["%0.2e" % ymin, "%0.2e" % ymax])
        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=6)
        ax.set_xticks([])
        ##############################################################

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

		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()
示例#26
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()

        enzymeMonomerId = "GLUTCYSLIG-MONOMER[c]"
        enzymeRnaId = "EG10418_RNA[c]"
        reactionId = "GLUTCYSLIG-RXN"
        transcriptionFreq = 1.0
        metaboliteId = "GLUTATHIONE[c]"

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

        sim_data = cPickle.load(open(simDataFile, "rb"))
        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")
        enzymeMonomerIndex = moleculeIds.index(enzymeMonomerId)
        enzymeRnaIndex = moleculeIds.index(enzymeRnaId)
        metaboliteIndex = moleculeIds.index(metaboliteId)
        bulkMolecules.close()

        time = []
        enzymeFluxes = []
        enzymeMonomerCounts = []
        enzymeRnaCounts = []
        enzymeRnaInitEvent = []
        metaboliteCounts = []

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

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

            bulkMolecules = TableReader(
                os.path.join(simOutDir, "BulkMolecules"))
            moleculeCounts = bulkMolecules.readColumn("counts")
            enzymeMonomerCounts += moleculeCounts[:,
                                                  enzymeMonomerIndex].tolist()
            enzymeRnaCounts += moleculeCounts[:, enzymeRnaIndex].tolist()
            metaboliteCounts += moleculeCounts[:, metaboliteIndex].tolist()
            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"))
            enzymeRnaInitEvent += rnapDataReader.readColumn(
                "rnaInitEvent")[:, np.where(
                    mRnaIds == enzymeRnaId)[0][0]].tolist()
            rnapDataReader.close()

        time = np.array(time)

        # Plot
        fig = plt.figure(figsize=(10, 10))
        rnaInitAxis = plt.subplot(5, 1, 1)
        rnaAxis = plt.subplot(5, 1, 2, sharex=rnaInitAxis)
        monomerAxis = plt.subplot(5, 1, 3, sharex=rnaInitAxis)
        fluxAxis = plt.subplot(5, 1, 4, sharex=rnaInitAxis)
        metAxis = plt.subplot(5, 1, 5, sharex=rnaInitAxis)

        rnaInitAxis.plot(time / 3600., enzymeRnaInitEvent)
        rnaInitAxis.set_title("%s transcription initiation events" %
                              enzymeRnaId,
                              fontsize=10)
        rnaInitAxis.set_ylim([0, rnaInitAxis.get_ylim()[1] * 1.1])
        rnaInitAxis.set_xlim([0, time[-1] / 3600.])

        rnaAxis.plot(time / 3600., enzymeRnaCounts)
        rnaAxis.set_title("%s counts" % enzymeRnaId, fontsize=10)

        monomerAxis.plot(time / 3600., enzymeMonomerCounts)
        monomerAxis.set_title("%s counts" % enzymeMonomerId, fontsize=10)

        fluxAxis.plot(time / 3600., enzymeFluxes)
        fluxAxis.set_title(
            "%s flux (%s / %s / %s)" %
            (reactionId, COUNTS_UNITS, VOLUME_UNITS, TIME_UNITS),
            fontsize=10)

        metAxis.plot(time / 3600., metaboliteCounts)
        metAxis.set_title("%s counts" % metaboliteId, fontsize=10)
        metAxis.set_xlabel(
            "Time (hour)\n(%s frequency of at least 1 transcription per generation)"
            % transcriptionFreq,
            fontsize=10)

        plt.subplots_adjust(
            wspace=0.4, hspace=0.4
        )  #, right = 0.83, bottom = 0.05, left = 0.07, top = 0.95)
        exportFigure(plt, plotOutDir, plotOutFileName, metadata)
        plt.close("all")
示例#27
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()

        validation_data = cPickle.load(open(validationDataFile, "rb"))
        essentialRnas = validation_data.essentialGenes.essentialRnas

        # Get mRNA data
        sim_data = cPickle.load(open(simDataFile, "rb"))
        rnaIds = sim_data.process.transcription.rnaData["id"]
        isMRna = sim_data.process.transcription.rnaData["isMRna"]
        synthProb = sim_data.process.transcription.rnaSynthProb["basal"]
        mRnaIndexes = np.where(isMRna)[0]

        mRnaSynthProb = np.array([synthProb[x] for x in mRnaIndexes])
        mRnaIds = np.array([rnaIds[x] for x in mRnaIndexes])

        if not USE_CACHE:
            # Get whether or not mRNAs were transcribed
            time = []
            transcribedBool = []
            simulatedSynthProbs = []
            transcriptionEvents = []
            for gen, simDir in enumerate(allDir):
                simOutDir = os.path.join(simDir, "simOut")

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

                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)
                else:
                    transcriptionEvents = np.vstack(
                        (transcriptionEvents, (rnaInitEvent != 0)))

            time = np.array(time)
            transcribedBool = np.array(transcribedBool)
            simulatedSynthProbs = np.array(simulatedSynthProbs)

            indexingOrder = np.argsort(np.mean(simulatedSynthProbs, axis=0))
            transcribedBoolOrdered = np.mean(transcribedBool,
                                             axis=0)[indexingOrder]
            simulatedSynthProbsOrdered = np.mean(simulatedSynthProbs,
                                                 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
            ])
            colors = np.repeat("g", len(transcribedBoolOrdered))
            colors[alwaysPresentIndexes] = "b"
            colors[neverPresentIndexes] = "r"

            # Assemble data
            alwaysTranscriptionEvents_E = []
            alwaysTranscriptionEvents_N = []
            alwaysId_E = []
            alwaysId_N = []
            for i in alwaysPresentIndexes:
                v = (time[transcriptionEventsOrdered[:, i]] / 3600.).tolist()
                if transcriptionEventsOrdered[:, i].sum() == 0:
                    v = [-1]

                if mRnaIdsOrdered[i] in essentialRnas:
                    alwaysTranscriptionEvents_E.append(v)
                else:
                    alwaysTranscriptionEvents_N.append(v)

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

                if mRnaIdsOrdered[i] in essentialRnas:
                    neverTranscriptionEvents_E.append(v)
                else:
                    neverTranscriptionEvents_N.append(v)

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

                if mRnaIdsOrdered[i] in essentialRnas:
                    sometimesTranscriptionEvents_E.append(v)
                else:
                    sometimesTranscriptionEvents_N.append(v)

            cPickle.dump(
                {
                    "time": time,
                    "always_E": alwaysTranscriptionEvents_E,
                    "always_N": alwaysTranscriptionEvents_N,
                    "never_E": neverTranscriptionEvents_E,
                    "never_N": neverTranscriptionEvents_N,
                    "sometimes_E": sometimesTranscriptionEvents_E,
                    "sometimes_N": sometimesTranscriptionEvents_N,
                    "transcriptionFrequency": transcribedBoolOrdered,
                    "colors": colors,
                    "id": mRnaIdsOrdered,
                },
                open(os.path.join(plotOutDir, "transcriptionEvents.pickle"),
                     "wb"))

        if USE_CACHE:
            D = cPickle.load(
                open(os.path.join(plotOutDir, "transcriptionEvents.pickle"),
                     "r"))
            time = D["time"]
            alwaysTranscriptionEvents_E = D["always_E"]
            alwaysTranscriptionEvents_N = D["always_N"]
            neverTranscriptionEvents_E = D["never_E"]
            neverTranscriptionEvents_N = D["never_N"]
            sometimesTranscriptionEvents_E = D["sometimes_E"]
            sometimesTranscriptionEvents_N = D["sometimes_N"]
            transcribedBoolOrdered = D["transcriptionFrequency"]
            colors = D["colors"]
            mRnaIdsOrdered = D["id"]

        # Plot
        blue = [0, 0, 1]
        green = [0, 0.5, 0]
        red = [1, 0, 0]
        gray = [0, 0, 0]

        fig = plt.figure(figsize=(8, 10))
        scatterAxis = plt.subplot2grid((5, 4), (0, 0), colspan=3, rowspan=2)
        histAxis = plt.subplot2grid((5, 4), (0, 3),
                                    colspan=1,
                                    rowspan=2,
                                    sharey=scatterAxis)
        alwaysAxis = plt.subplot2grid((5, 4), (2, 0), colspan=4, rowspan=1)
        sometimesAxis = plt.subplot2grid((5, 4), (3, 0),
                                         colspan=4,
                                         rowspan=1,
                                         sharex=alwaysAxis)
        neverAxis = plt.subplot2grid((5, 4), (4, 0),
                                     colspan=4,
                                     rowspan=1,
                                     sharex=alwaysAxis)

        scatterAxis.scatter(np.arange(len(transcribedBoolOrdered)),
                            transcribedBoolOrdered,
                            marker='o',
                            facecolors=colors,
                            edgecolors="none",
                            s=5)
        scatterAxis.set_title(
            "Frequency of observing at least 1 transcript per generation\n(Genes ordered by simulated synthesis probability)",
            fontsize=10)
        scatterAxis.set_xlim([-1, len(transcribedBoolOrdered)])
        scatterAxis.set_ylim([-0.1, 1.1])
        scatterAxis.tick_params(top="off")
        scatterAxis.tick_params(right="off")
        scatterAxis.tick_params(which='both', direction='out', labelsize=8)

        histAxis.hist(transcribedBoolOrdered,
                      bins=len(allDir) + 1,
                      orientation='horizontal',
                      color="k",
                      alpha=0.5)
        histAxis.set_xscale("log")
        histAxis.spines["right"].set_visible(False)
        histAxis.tick_params(right="off")
        histAxis.tick_params(which='both', direction='out', labelsize=8)
        histAxis.text(
            histAxis.get_xlim()[1] * 1.5,
            0,
            "%s genes\n(%0.1f%%)" %
            (len(neverTranscriptionEvents_N) + len(neverTranscriptionEvents_E),
             100. * (len(neverTranscriptionEvents_N) +
                     len(neverTranscriptionEvents_E)) /
             float(len(transcribedBoolOrdered))),
            fontsize=10,
            verticalalignment="top")
        histAxis.text(histAxis.get_xlim()[1] * 1.5,
                      1,
                      "%s genes\n(%0.1f%%)" %
                      (len(alwaysTranscriptionEvents_N) +
                       len(alwaysTranscriptionEvents_E), 100. *
                       (len(alwaysTranscriptionEvents_N) +
                        len(alwaysTranscriptionEvents_E)) /
                       float(len(transcribedBoolOrdered))),
                      fontsize=10,
                      verticalalignment="bottom")
        histAxis.text(histAxis.get_xlim()[1] * 1.5,
                      0.5,
                      "%s genes\n(%0.1f%%)" %
                      (len(sometimesTranscriptionEvents_N) +
                       len(sometimesTranscriptionEvents_E), 100. *
                       (len(sometimesTranscriptionEvents_N) +
                        len(sometimesTranscriptionEvents_E)) /
                       float(len(transcribedBoolOrdered))),
                      fontsize=10,
                      verticalalignment="center")
        histAxis.add_patch(
            patches.Rectangle(
                (histAxis.get_xlim()[1] * 0.7, 1. / (len(allDir) + 1)),
                1e4,
                1. - 2. / (len(allDir) + 1),
                facecolor=green,
                edgecolor="none"))

        alwaysAxis.eventplot(alwaysTranscriptionEvents_N +
                             alwaysTranscriptionEvents_E,
                             orientation="horizontal",
                             linewidths=2.,
                             linelengths=1.,
                             colors=[blue] * len(alwaysTranscriptionEvents_N) +
                             [gray] * len(alwaysTranscriptionEvents_E))
        alwaysAxis.set_ylabel("Always present", fontsize=10)
        alwaysAxis.set_title("Transcription initiation events", fontsize=10)
        alwaysAxis.set_yticks([])
        alwaysAxis.tick_params(top="off")
        alwaysAxis.tick_params(which='both', direction='out', labelsize=8)
        alwaysAxis.set_xlim([0, time[-1] / 3600.])
        alwaysAxis.set_ylim([
            -1,
            np.max([
                N_GENES_TO_PLOT,
                len(alwaysTranscriptionEvents_E) +
                len(alwaysTranscriptionEvents_N)
            ])
        ])
        alwaysAxis.text(alwaysAxis.get_xlim()[1] * 1.02,
                        len(alwaysTranscriptionEvents_N) * 0.5,
                        "%s\nnon-essential\ngenes" %
                        len(alwaysTranscriptionEvents_N),
                        fontsize=10,
                        verticalalignment="center")
        alwaysAxis.text(alwaysAxis.get_xlim()[1] * 1.02,
                        len(alwaysTranscriptionEvents_N) +
                        len(alwaysTranscriptionEvents_E) * 0.5,
                        "%s essential\ngenes" %
                        len(alwaysTranscriptionEvents_E),
                        fontsize=10,
                        verticalalignment="center")

        sometimesAxis.eventplot(
            sometimesTranscriptionEvents_N + sometimesTranscriptionEvents_E,
            orientation="horizontal",
            linewidths=2.,
            linelengths=1.,
            colors=[green] * len(sometimesTranscriptionEvents_N) +
            [gray] * len(sometimesTranscriptionEvents_E))
        sometimesAxis.set_ylabel("Sub-generational", fontsize=10)
        sometimesAxis.set_yticks([])
        sometimesAxis.tick_params(top="off")
        sometimesAxis.set_ylim([
            -1,
            np.max([
                N_GENES_TO_PLOT,
                len(sometimesTranscriptionEvents_E) +
                len(sometimesTranscriptionEvents_N)
            ])
        ])
        sometimesAxis.tick_params(which='both', direction='out', labelsize=8)
        sometimesAxis.text(sometimesAxis.get_xlim()[1] * 1.02,
                           len(sometimesTranscriptionEvents_N) * 0.5,
                           "%s\nnon-essential\ngenes" %
                           len(alwaysTranscriptionEvents_N),
                           fontsize=10,
                           verticalalignment="center")
        sometimesAxis.text(sometimesAxis.get_xlim()[1] * 1.02,
                           len(sometimesTranscriptionEvents_N) +
                           len(sometimesTranscriptionEvents_E) * 0.5,
                           "%s essential\ngenes" %
                           len(sometimesTranscriptionEvents_E),
                           fontsize=10,
                           verticalalignment="center")

        neverAxis.eventplot(neverTranscriptionEvents_N +
                            neverTranscriptionEvents_E,
                            orientation="horizontal",
                            linewidths=2.,
                            linelengths=1.,
                            colors=[red] * len(neverTranscriptionEvents_N) +
                            [gray] * len(neverTranscriptionEvents_E))
        neverAxis.set_ylabel("Never present", fontsize=10)
        neverAxis.set_xlabel("Time (hour)", fontsize=10)
        neverAxis.set_yticks([])
        neverAxis.tick_params(top="off")
        neverAxis.set_ylim([
            -1,
            np.max([
                N_GENES_TO_PLOT,
                len(neverTranscriptionEvents_E) +
                len(neverTranscriptionEvents_N)
            ])
        ])
        neverAxis.tick_params(which='both', direction='out', labelsize=8)
        neverAxis.text(neverAxis.get_xlim()[1] * 1.02,
                       len(neverTranscriptionEvents_N) * 0.5,
                       "%s\nnon-essential\ngenes" %
                       len(neverTranscriptionEvents_N),
                       fontsize=10,
                       verticalalignment="center")
        neverAxis.text(neverAxis.get_xlim()[1] * 1.02,
                       len(neverTranscriptionEvents_N) +
                       len(neverTranscriptionEvents_E) * 0.5,
                       "%s essential\ngenes" % len(neverTranscriptionEvents_E),
                       fontsize=10,
                       verticalalignment="center")

        plt.subplots_adjust(wspace=0.4,
                            hspace=0.4,
                            right=0.83,
                            bottom=0.05,
                            left=0.07,
                            top=0.95)
        exportFigure(plt, plotOutDir, plotOutFileName, 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)

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

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

        fig, axesList = plt.subplots(3)
        fig.set_size_inches(11, 11)

        histo = np.zeros(4)
        limitedCounts = np.zeros(len(metaboliteNames))

        ax2 = axesList[2]
        for simDir in allDir:
            simOutDir = os.path.join(simDir, "simOut")

            enzymeKineticsData = TableReader(
                os.path.join(simOutDir, "EnzymeKinetics"))
            metaboliteCounts = enzymeKineticsData.readColumn(
                "metaboliteCountsFinal")
            normalizedCounts = metaboliteCounts / metaboliteCounts[1, :]
            enzymeKineticsData.close()

            # Read time info from the listener
            initialTime = TableReader(os.path.join(
                simOutDir, "Main")).readAttribute("initialTime")
            time = TableReader(os.path.join(simOutDir,
                                            "Main")).readColumn("time")

            metaboliteLimited = np.zeros((len(time), nMetabolites))

            diff = np.diff(normalizedCounts, axis=0)
            limited = []
            for i in xrange(diff.shape[0] - WINDOW):
                currentStepLimited = np.where(
                    np.any(diff[i:i +
                                WINDOW] > 0, axis=0) == False)[0].astype(int)
                metaboliteLimited[i, currentStepLimited] = 1
                limited = np.append(limited, currentStepLimited).astype(int)

            nLimited = len(np.unique(limited))
            if nLimited >= len(histo):
                histo = np.append(histo, np.zeros(nLimited - len(histo) + 1))
            histo[nLimited] += 1
            limitedCounts[limited] += 1

            ax2.plot(time / 60,
                     metaboliteLimited * range(metaboliteLimited.shape[1]))
            ax2.axvline(initialTime / 60, color="r", linestyle="--")

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

        ax0 = axesList[0]
        labels = np.arange(len(histo))
        ax0.bar(labels - 0.5, histo, 1)
        ax0.set_xticks(labels)
        ax0.set_xlabel("Number of limited metabolites")
        ax0.set_ylabel("Number of generations")

        ax1 = axesList[1]
        ax1.bar(
            np.arange(len(np.where(limitedCounts > 0)[0])) - 0.4,
            limitedCounts[limitedCounts > 0])
        ax1.set_xticks(np.arange(len(np.where(limitedCounts > 0)[0])))
        ax1.set_xticklabels(metaboliteNames[limitedCounts > 0], fontsize=6)
        ax1.set_xlabel("Metabolite Limited")
        ax1.set_ylabel("Number of genreations")

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

        # 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, 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
        cellDensity = sim_data.constants.cellDensity

        # Load time
        initialTime = 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")
        proteinMass = units.fg * massReader.readColumn("proteinMass")

        massReader.close()

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

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

        # Get the amount of active trpR (that isn't promoter bound)
        trpRActiveId = ["CPLX-125[c]"]
        trpRActiveIndex = np.array(
            [bulkMoleculeIds.index(x) for x in trpRActiveId])
        trpRActiveCounts = bulkMoleculeCounts[:, trpRActiveIndex].reshape(-1)

        # Get the amount of inactive trpR
        trpRInactiveId = ["PC00007[c]"]
        trpRInactiveIndex = np.array(
            [bulkMoleculeIds.index(x) for x in trpRInactiveId])
        trpRInactiveCounts = bulkMoleculeCounts[:,
                                                trpRInactiveIndex].reshape(-1)

        # Get the amount of monomeric trpR
        trpRMonomerId = ["PD00423[c]"]
        trpRMonomerIndex = np.array(
            [bulkMoleculeIds.index(x) for x in trpRMonomerId])
        trpRMonomerCounts = bulkMoleculeCounts[:, trpRMonomerIndex].reshape(-1)

        # Get the promoter-bound status for all regulated genes
        tfBoundIds = [
            target + "__CPLX-125"
            for target in sim_data.tfToFC["CPLX-125"].keys()
        ]
        tfBoundIndex = np.array([bulkMoleculeIds.index(x) for x in tfBoundIds])
        tfBoundCounts = bulkMoleculeCounts[:, tfBoundIndex]

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

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

        bulkMoleculesReader.close()

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

        # Compute the trpA mass in the cell
        trpAMw = sim_data.getter.getMass(trpAProteinId)
        trpAMass = 1. / nAvogadro * trpAProteinTotalCounts * trpAMw

        # Compute the proteome mass fraction
        proteomeMassFraction = trpAMass.asNumber(
            units.fg) / proteinMass.asNumber(units.fg)

        # Get the synthesis probability for all regulated genes
        rnaSynthProbReader = TableReader(
            os.path.join(simOutDir, "RnaSynthProb"))

        rnaIds = rnaSynthProbReader.readAttribute("rnaIds")
        synthProbIds = [
            target + "[c]" for target in sim_data.tfToFC["CPLX-125"].keys()
        ]
        synthProbIndex = np.array([rnaIds.index(x) for x in synthProbIds])
        synthProbs = rnaSynthProbReader.readColumn(
            "rnaSynthProb")[:, synthProbIndex]

        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]
        trpRBound = rnaSynthProbReader.readColumn("nActualBound")[:, trpRIndex]

        rnaSynthProbReader.close()

        # Calculate total trpR - active, inactive, bound and monomeric
        trpRTotalCounts = 2 * (trpRActiveCounts + trpRInactiveCounts +
                               trpRBound) + trpRMonomerCounts

        # 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
        synthProbsMA = np.array([
            np.convolve(synthProbs[:, i], np.ones(width) / width, mode="same")
            for i in range(synthProbs.shape[1])
        ]).T

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

        ##############################################################
        ax = plt.subplot(6, 1, 1)
        ax.plot(time, trpConcentration.asNumber(units.umol / units.L))
        plt.ylabel("Internal TRP Conc. [uM]", fontsize=6)

        ymin = np.amin(trpConcentration.asNumber(units.umol / units.L) * 0.9)
        ymax = np.amax(trpConcentration.asNumber(units.umol / units.L) * 1.1)
        if ymin != ymax:
            ax.set_ylim([ymin, ymax])
            ax.set_yticks([ymin, ymax])
            ax.set_yticklabels(["%0.0f" % ymin, "%0.0f" % ymax])
        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=6)
        ax.set_xticks([])
        ##############################################################

        ##############################################################
        ax = plt.subplot(6, 1, 2)
        ax.plot(time, trpRActiveCounts)
        ax.plot(time, trpRInactiveCounts)
        ax.plot(time, trpRTotalCounts)
        plt.ylabel("TrpR Counts", fontsize=6)
        plt.legend(["Active (dimer)", "Inactive (dimer)", "Total (monomeric)"],
                   fontsize=6)

        ymin = min(np.amin(trpRActiveCounts * 0.9),
                   np.amin(trpRInactiveCounts * 0.9))
        ymax = np.amax(trpRTotalCounts * 1.1)
        if ymin != ymax:
            ax.set_ylim([ymin, ymax])
            ax.set_yticks([ymin, ymax])
            ax.set_yticklabels(["%0.0f" % ymin, "%0.0f" % ymax])
        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=6)
        ax.set_xticks([])
        ##############################################################

        ##############################################################
        ax = plt.subplot(6, 1, 3)
        ax.plot(time, tfBoundCountsMA)
        plt.ylabel("TrpR Bound To Promoters\n(Moving Average)", fontsize=6)

        ymin = np.amin(tfBoundCountsMA * 1.)
        ymax = np.amax(tfBoundCountsMA * 1.)
        if ymin != ymax:
            ax.set_ylim([ymin, ymax])
            ax.set_yticks([ymin, ymax])
            ax.set_yticklabels(["%0.0f" % ymin, "%0.0f" % ymax])
        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=6)
        ax.set_xticks([])
        ##############################################################

        ##############################################################
        ax = plt.subplot(6, 1, 4)
        ax.plot(time, synthProbsMA)
        plt.ylabel("Regulated Gene Synthesis Prob.\n(Moving Average)",
                   fontsize=6)

        ymin = np.amin(synthProbsMA[1:] * 0.9)
        ymax = np.amax(synthProbsMA[1:] * 1.1)
        if ymin != ymax:
            ax.set_ylim([ymin, ymax])
            ax.set_yticks([ymin, ymax])
            ax.set_yticklabels(["%0.2e" % ymin, "%0.2e" % ymax])
        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=6)
        ax.set_xticks([])
        ##############################################################

        ##############################################################
        ax = plt.subplot(6, 1, 5)
        ax.plot(time, trpAProteinTotalCounts)
        plt.ylabel("TrpA Counts", fontsize=6)

        ymin = np.amin(trpAProteinTotalCounts * 0.9)
        ymax = np.amax(trpAProteinTotalCounts * 1.1)
        if ymin != ymax:
            ax.set_ylim([ymin, ymax])
            ax.set_yticks([ymin, ymax])
            ax.set_yticklabels(["%0.0f" % ymin, "%0.0f" % ymax])
        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=6)
        ax.set_xticks([])
        ##############################################################

        ##############################################################
        ax = plt.subplot(6, 1, 6)
        ax.plot(time, proteomeMassFraction)
        plt.ylabel("TrpA Mass Fraction of Proteome", fontsize=6)

        ymin = np.amin(proteomeMassFraction * 0.9)
        ymax = np.amax(proteomeMassFraction * 1.1)
        if ymin != ymax:
            ax.set_ylim([ymin, ymax])
            ax.set_yticks([ymin, ymax])
            ax.set_yticklabels(["%0.2e" % ymin, "%0.2e" % ymax])
        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=6)
        ax.set_xticks([])
        ##############################################################

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