コード例 #1
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")
コード例 #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)

		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")
コード例 #3
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)

        # 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")
コード例 #4
0
    def do_plot(self, simOutDir, plotOutDir, plotOutFileName, simDataFile,
                validationDataFile, metadata):
        if not os.path.isdir(simOutDir):
            raise Exception, "simOutDir does not currently exist as a directory"

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

        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")
コード例 #5
0
	def do_plot(self, variantDir, plotOutDir, plotOutFileName, simDataFile, validationDataFile, metadata):
		if not os.path.isdir(variantDir):
			raise Exception, "variantDir does not currently exist as a directory"

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

		# Get all cells in each seed
		ap = AnalysisPaths(variantDir, cohort_plot = True)

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

		fig, axesList = plt.subplots(ap.n_generation, sharey = True, sharex = True,
			subplot_kw={'aspect': 0.4, 'adjustable': 'box'})

		initial_masses = np.zeros((max_cells_in_gen, ap.n_generation))
		final_masses = np.zeros((max_cells_in_gen, ap.n_generation))

		for genIdx in range(ap.n_generation):
			gen_cells = ap.get_cells(generation = [genIdx])
			for simDir in gen_cells:
				simOutDir = os.path.join(simDir, "simOut")
				mass = TableReader(os.path.join(simOutDir, "Mass"))
				cellMass = mass.readColumn("cellMass")

				initial_masses[np.where(simDir == gen_cells)[0], genIdx] = cellMass[0] / 1000.
				final_masses[np.where(simDir == gen_cells)[0], genIdx] = cellMass[-1] / 1000.

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

		for idx, axes in enumerate(axesList):
			axes.plot(initial_masses[:, idx], final_masses[:, idx], 'o')
			z = np.polyfit(initial_masses[:, idx], final_masses[:, idx], 1)
			p = np.poly1d(z)
			axes.plot(initial_masses[:, idx], p(initial_masses[:, idx]), '--')
			text_x = np.mean(axes.get_xlim())
			text_y = np.mean(axes.get_ylim()) + np.mean(axes.get_ylim())*0.1
			axes.text(text_x, text_y, r"$m_f$=%.3f$\times$$m_i$ + %.3f" % (z[0], z[1]))


		axesList[-1].set_xlabel("Initial mass (pg)")
		axesList[ap.n_generation / 2].set_ylabel("Final mass (pg)")

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

		exportFigure(plt, plotOutDir, plotOutFileName, metadata)
		plt.close("all")
コード例 #6
0
def getFinalMass((variant, ap)):
    try:
        simDir = ap.get_cells(variant=[variant])[0]

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

        mass = TableReader(os.path.join(simOutDir, "Mass"))
        cellDry = mass.readColumn("dryMass")
        return cellDry[-1]
    except Exception as e:
        print e
        return np.nan
コード例 #7
0
	def do_plot(self, seedOutDir, plotOutDir, plotOutFileName, simDataFile, validationDataFile, metadata):
		if not os.path.isdir(seedOutDir):
			raise Exception, "seedOutDir does not currently exist as a directory"

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

		ap = AnalysisPaths(seedOutDir, multi_gen_plot = True)

		# Get all cells
		allDir = ap.get_cells()

		massNames = [
					"dryMass",
					"proteinMass",
					#"tRnaMass",
					"rRnaMass",
					'mRnaMass',
					"dnaMass"
					]

		cleanNames = [
					"Dry\nmass",
					"Protein\nmass",
					#"tRNA\nmass",
					"rRNA\nmass",
					"mRNA\nmass",
					"DNA\nmass"
					]

		fig, axesList = plt.subplots(len(massNames), sharex = True)

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

			for idx, massType in enumerate(massNames):
				massToPlot = mass.readColumn(massNames[idx])
				axesList[idx].plot(time / 60. / 60., massToPlot, linewidth = 2)

				axesList[idx].set_ylabel(cleanNames[idx] + " (fg)")

		for axes in axesList:
			axes.get_ylim()
			axes.set_yticks(list(axes.get_ylim()))

		axesList[0].set_title("Cell mass fractions")
		axesList[len(massNames) - 1].set_xlabel("Time (hr)")

		plt.subplots_adjust(hspace = 0.2, wspace = 0.5)
		exportFigure(plt, plotOutDir, plotOutFileName,metadata)
		plt.close("all")
コード例 #8
0
	def do_plot(self, variantDir, plotOutDir, plotOutFileName, simDataFile, validationDataFile, metadata):
		if not os.path.isdir(variantDir):
			raise Exception, "variantDir does not currently exist as a directory"

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

		# Get all cells in each seed
		ap = AnalysisPaths(variantDir, cohort_plot = True)

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

		fig, axesList = plt.subplots(ap.n_generation, sharey = True, sharex = True,
			subplot_kw={'aspect': 0.4, 'adjustable': 'box'})

		initial_masses = np.zeros((max_cells_in_gen, ap.n_generation))
		final_masses = np.zeros((max_cells_in_gen, ap.n_generation))

		for genIdx in range(ap.n_generation):
			gen_cells = ap.get_cells(generation = [genIdx])
			for simDir in gen_cells:
				simOutDir = os.path.join(simDir, "simOut")
				mass = TableReader(os.path.join(simOutDir, "Mass"))
				cellMass = mass.readColumn("cellMass")

				initial_masses[np.where(simDir == gen_cells)[0], genIdx] = cellMass[0] / 1000.
				final_masses[np.where(simDir == gen_cells)[0], genIdx] = cellMass[-1] / 1000.

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

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

		axesList[-1].set_xlabel("Initial mass (pg)")
		axesList[ap.n_generation / 2].set_ylabel("Frequency")

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

		exportFigure(plt, plotOutDir, plotOutFileName, metadata)
		plt.close("all")
コード例 #9
0
def getMassData(simDir, massNames):
    simOutDir = os.path.join(simDir, "simOut")
    time = TableReader(os.path.join(simOutDir, "Main")).readColumn("time")
    mass = TableReader(os.path.join(simOutDir, "Mass"))

    massFractionData = np.zeros((len(massNames), time.size))

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

    if len(massNames) == 1:
        massFractionData = massFractionData.reshape(-1)

    return time, massFractionData
コード例 #10
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)

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

		fba_results = TableReader(os.path.join(simOutDir, "FBAResults"))
		exFlux = fba_results.readColumn("externalExchangeFluxes")
		exMolec = fba_results.readAttribute("externalMoleculeIDs")
		moleculeIDs = ["GLC[p]", "OXYGEN-MOLECULE[p]"]

		# Plot
		fig = plt.figure(figsize = (8, 11.5))
		rows = len(moleculeIDs)
		cols = 1

		for index, molecule in enumerate(["GLC[p]", "OXYGEN-MOLECULE[p]"]):
			if molecule not in exMolec:
				continue
			moleculeFlux = -1. * exFlux[:, exMolec.index(molecule)]
			ax = plt.subplot(rows, cols, index + 1)
			ax.plot(time / 60. / 60., moleculeFlux)

			averageFlux = np.average(moleculeFlux)
			yRange = np.min([np.abs(np.max(moleculeFlux) - averageFlux), np.abs(np.min(moleculeFlux) - averageFlux)])
			ymin = np.round(averageFlux - yRange)
			ymax = np.round(averageFlux + yRange)
			ax.set_ylim([ymin, ymax])

			abs_max = np.max(moleculeFlux)
			abs_min = np.min(moleculeFlux)

			plt.figtext(0.7, 1. / float(rows) * 0.7 + (rows - 1 - index) / float(rows),
				"Max: %s\nMin: %s" % (abs_max, abs_min), fontsize = 8)

			ax.set_ylabel("External %s\n(mmol/gDCW/hr)" % molecule, fontsize = 8)
			ax.set_xlabel("Time (hr)", fontsize = 8)
			ax.set_title("%s" % molecule, fontsize = 10, y = 1.1)
			ax.tick_params(labelsize = 8, which = "both", direction = "out")


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

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

		# 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")
コード例 #12
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")
コード例 #13
0
    def do_plot(self, seedOutDir, plotOutDir, plotOutFileName, simDataFile,
                validationDataFile, metadata):
        if not os.path.isdir(seedOutDir):
            raise Exception, "seedOutDir does not currently exist as a directory"

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

        ap = AnalysisPaths(seedOutDir, multi_gen_plot=True)

        # TODO: Declutter Y-axis

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

        massNames = [
            "dryMass",
        ]

        cleanNames = [
            "Dry\nmass",
        ]

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

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

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

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

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

                exportFigure(
                    plt, plotOutDir,
                    "r01_{}_gen{}".format(massType, allDir.index(simDir)))
                plt.close("all")
コード例 #14
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")
コード例 #15
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")
コード例 #16
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'

        filepath.makedirs(plotOutDir)

        with open(simDataFile, 'rb') as f:
            sim_data = cPickle.load(f)
        with open(validationDataFile, 'rb') as f:
            validation_data = cPickle.load(f)

        # Listeners used
        main_reader = TableReader(os.path.join(simOutDir, 'Main'))

        # Load data
        time = main_reader.readColumn('time')

        plt.figure()

        ### Create Plot ###

        exportFigure(plt, plotOutDir, plotOutFileName, metadata)
        plt.close('all')
コード例 #17
0
    def do_plot(self, inputDir, plotOutDir, plotOutFileName, simDataFile,
                validationDataFile, metadata):
        if metadata["variant"] != "condition":
            print("This plot only runs for the 'condition' variant.")
            return

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

        filepath.makedirs(plotOutDir)

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

        gens = [2, 3]

        initial_volumes = []
        added_volumes = []

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

            cell_density = sim_data.constants.cellDensity

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

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

            if len(all_cells) == 0:
                continue

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

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

            added_masses = final_masses - initial_masses

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

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

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

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

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

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

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

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

        whitePadSparklineAxis(ax)

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

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

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

        plt.close("all")
コード例 #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)

        # 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)
    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")
コード例 #20
0
    def do_plot(self, inputDir, plotOutDir, plotOutFileName, simDataFile,
                validationDataFile, metadata):
        if metadata.get('variant', '') != 'flux_sensitivity':
            print 'This plot only runs for the flux_sensitivity variant.'
            return

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

        filepath.makedirs(plotOutDir)

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

        succ_fluxes = []
        iso_fluxes = []
        for variant in variants:
            for sim_dir in ap.get_cells(variant=[variant]):
                simOutDir = os.path.join(sim_dir, "simOut")

                # Listeners used
                fba_reader = TableReader(os.path.join(simOutDir, 'FBAResults'))

                # Load data
                reactions = np.array(
                    fba_reader.readAttribute('sensitivity_reactions'))
                succ_fluxes += [
                    fba_reader.readColumn('succinate_flux_sensitivity')[1:, :]
                ]
                iso_fluxes += [
                    fba_reader.readColumn('isocitrate_flux_sensitivity')[1:, :]
                ]

        succ_fluxes = np.vstack(succ_fluxes)
        iso_fluxes = np.vstack(iso_fluxes)

        succ_z = calc_z(succ_fluxes)
        iso_z = calc_z(iso_fluxes)

        threshold = -0.1

        # Plot data
        plt.figure()
        gs = gridspec.GridSpec(2, 2)

        ## Succinate dehydrogenase all fluxes
        ax = plt.subplot(gs[0, 0])
        plot_lows(ax, succ_z, threshold, 'succinate dehydrogenase')

        ## Succinate dehydrogenase fluxes over threshold
        ax = plt.subplot(gs[0, 1])
        plot_threshold(ax, succ_z, threshold, reactions)

        ## Isocitrate dehydrogenase all fluxes
        ax = plt.subplot(gs[1, 0])
        plot_lows(ax, iso_z, threshold, 'isocitrate dehydrogenase')

        ## Isocitrate dehydrogenase fluxes over threshold
        ax = plt.subplot(gs[1, 1])
        plot_threshold(ax, iso_z, threshold, reactions)

        plt.tight_layout()
        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)

		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()
コード例 #22
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, inputDir, plotOutDir, plotOutFileName, simDataFile, validationDataFile, metadata):
		if not os.path.isdir(inputDir):
			raise Exception, 'inputDir does not currently exist as a directory'

		filepath.makedirs(plotOutDir)

		with open(os.path.join(inputDir, 'kb', constants.SERIALIZED_FIT1_FILENAME), 'rb') as f:
			sim_data = cPickle.load(f)
		with open(validationDataFile, 'rb') as f:
			validation_data = cPickle.load(f)

		ap = AnalysisPaths(inputDir, variant_plot=True)
		variants = ap.get_variants()
		expected_n_variants = 2
		n_variants = len(variants)

		if n_variants < expected_n_variants:
			print('This plot only runs for {} variants.'.format(expected_n_variants))
			return

		# IDs for appropriate proteins
		ids_complexation = sim_data.process.complexation.moleculeNames
		ids_complexation_complexes = sim_data.process.complexation.ids_complexes
		ids_equilibrium = sim_data.process.equilibrium.moleculeNames
		ids_equilibrium_complexes = sim_data.process.equilibrium.ids_complexes
		ids_translation = sim_data.process.translation.monomerData['id'].tolist()
		ids_protein = sorted(set(ids_complexation + ids_equilibrium + ids_translation))

		# Stoichiometry matrices
		equil_stoich = sim_data.process.equilibrium.stoichMatrixMonomers()
		complex_stoich = sim_data.process.complexation.stoichMatrixMonomers()

		# Protein container views
		protein_container = BulkObjectsContainer(ids_protein, dtype=np.float64)
		view_complexation = protein_container.countsView(ids_complexation)
		view_complexation_complexes = protein_container.countsView(ids_complexation_complexes)
		view_equilibrium = protein_container.countsView(ids_equilibrium)
		view_equilibrium_complexes = protein_container.countsView(ids_equilibrium_complexes)

		# Load model data
		model_counts = np.zeros((len(PROTEINS_WITH_HALF_LIFE), expected_n_variants))
		model_std = np.zeros((len(PROTEINS_WITH_HALF_LIFE), expected_n_variants))
		for i, variant in enumerate(variants):
			if i >= expected_n_variants:
				print('Skipping variant {} - only runs for {} variants.'.format(variant, expected_n_variants))
				continue

			variant_counts = []
			for sim_dir in ap.get_cells(variant=[variant]):
				simOutDir = os.path.join(sim_dir, 'simOut')

				# Listeners used
				unique_counts_reader = TableReader(os.path.join(simOutDir, 'UniqueMoleculeCounts'))

				# Account for bulk molecules
				(bulk_counts,) = read_bulk_molecule_counts(simOutDir, ids_protein)
				protein_container.countsIs(bulk_counts.mean(axis=0))

				# Account for unique molecules
				ribosome_index = unique_counts_reader.readAttribute('uniqueMoleculeIds').index('activeRibosome')
				rnap_index = unique_counts_reader.readAttribute('uniqueMoleculeIds').index('activeRnaPoly')
				n_ribosomes = unique_counts_reader.readColumn('uniqueMoleculeCounts')[:, ribosome_index]
				n_rnap = unique_counts_reader.readColumn('uniqueMoleculeCounts')[:, rnap_index]
				protein_container.countsInc(n_ribosomes.mean(), [sim_data.moleculeIds.s30_fullComplex, sim_data.moleculeIds.s50_fullComplex])
				protein_container.countsInc(n_rnap.mean(), [sim_data.moleculeIds.rnapFull])

				# Account for small-molecule bound complexes
				view_equilibrium.countsDec(equil_stoich.dot(view_equilibrium_complexes.counts()))

				# Account for monomers in complexed form
				view_complexation.countsDec(complex_stoich.dot(view_complexation_complexes.counts()))

				variant_counts.append(protein_container.countsView(PROTEINS_WITH_HALF_LIFE).counts())
			model_counts[:, i] = np.mean(variant_counts, axis=0)
			model_std[:, i] = np.std(variant_counts, axis=0)

		# Validation data
		schmidt_ids = {m: i for i, m in enumerate(validation_data.protein.schmidt2015Data['monomerId'])}
		schmidt_counts = validation_data.protein.schmidt2015Data['glucoseCounts']
		validation_counts = np.array([schmidt_counts[schmidt_ids[p]] for p in PROTEINS_WITH_HALF_LIFE])

		# Process data
		model_log_counts = np.log10(model_counts)
		model_log_lower_std = model_log_counts - np.log10(model_counts - model_std)
		model_log_upper_std = np.log10(model_counts + model_std) - model_log_counts
		validation_log_counts = np.log10(validation_counts)
		r_before = stats.pearsonr(validation_log_counts, model_log_counts[:, 0])
		r_after = stats.pearsonr(validation_log_counts, model_log_counts[:, 1])

		# Scatter plot of model vs validation counts
		max_counts = np.ceil(max(validation_log_counts.max(), model_log_upper_std.max()))
		limits = [0, max_counts]
		plt.figure()
		colors = plt.rcParams['axes.prop_cycle'].by_key()['color']

		## Plot data
		for i in range(expected_n_variants):
			plt.errorbar(validation_log_counts, model_log_counts[:, i],
				yerr=np.vstack((model_log_lower_std[:, i], model_log_upper_std[:, i])),
				fmt='o', color=colors[i], ecolor='k', capsize=3, alpha=0.5)
		plt.plot(limits, limits, 'k--', linewidth=0.5, label='_nolegend_')

		## Format axes
		plt.xlabel('Validation Counts\n(log10(counts))')
		plt.ylabel('Average Simulation Counts\n(log10(counts))')
		ax = plt.gca()
		ax.spines['right'].set_visible(False)
		ax.spines['top'].set_visible(False)
		ax.spines['left'].set_position(('outward', 10))
		ax.spines['bottom'].set_position(('outward', 10))
		ax.xaxis.set_major_locator(MaxNLocator(integer=True))
		ax.yaxis.set_major_locator(MaxNLocator(integer=True))

		## Add legend
		legend_text = [
			'Before: r={:.2f}, p={:.3f}'.format(r_before[0], r_before[1]),
			'After: r={:.2f}, p={:.3f}'.format(r_after[0], r_after[1]),
			]
		plt.legend(legend_text, frameon=False)

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

		plt.close('all')
コード例 #24
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")
コード例 #25
0
	def do_plot(self, inputDir, plotOutDir, plotOutFileName, simDataFile, validationDataFile, metadata):
		massNames = [
					"dryMass",
					"proteinMass",
					#"tRnaMass",
					"rRnaMass",
					'mRnaMass',
					"dnaMass"
					]

		cleanNames = [
					"Dry\nmass",
					"Protein\nmass",
					#"tRNA\nmass",
					"rRNA\nmass",
					"mRNA\nmass",
					"DNA\nmass"
					]

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

		ap = AnalysisPaths(inputDir, variant_plot = True)
		all_cells = ap.get_cells()

		# Build a mapping from variant id to color
		idToColor = {}
		for idx, (cell_id, color) in enumerate(itertools.izip(all_cells, itertools.cycle(COLORS_LARGE))):
			idToColor[idx] = color

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

		fig, axesList = plt.subplots(len(massNames), sharex = True)

		currentMaxTime = 0
		for cellIdx, simDir in enumerate(all_cells):
			with open(os.path.join(simDir[:-32],'metadata','short_name')) as f:
				variant_name = [line for line in f][0]

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

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

			for massIdx, massType in enumerate(massNames):
				massToPlot = mass.readColumn(massType)
				axesList[massIdx].plot(((time / 60.) / 60.), massToPlot, linewidth = 2, color=idToColor[cellIdx], label=variant_name)

				# set axes to size that shows all generations
				cellCycleTime = ((time[-1] - time[0]) / 60. / 60. )
				if cellCycleTime > currentMaxTime:
					currentMaxTime = cellCycleTime

				axesList[massIdx].set_xlim(0, currentMaxTime*ap.n_generation*1.1)
				axesList[massIdx].set_ylabel(cleanNames[massIdx] + " (fg)")

		for idx, axes in enumerate(axesList):
			axes.get_ylim()
			axes.set_yticks(list(axes.get_ylim()))

		axesList[0].set_title("Cell mass fractions")
		plt.legend(bbox_to_anchor=(.92, 5), loc=2, borderaxespad=0., prop={'size':6})
		axesList[len(massNames) - 1].set_xlabel("Time (hr)")
		plt.subplots_adjust(hspace = 0.2, wspace = 0.5)

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

        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")
コード例 #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()

        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")
コード例 #28
0
    def do_plot(self, variantDir, plotOutDir, plotOutFileName, simDataFile,
                validationDataFile, metadata):
        if not os.path.isdir(variantDir):
            raise Exception, 'variantDir does not currently exist as a directory'

        filepath.makedirs(plotOutDir)

        ap = AnalysisPaths(variantDir, cohort_plot=True)

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

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

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

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

        limited_metabolites = np.vstack(limited_metabolites)

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

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

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

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

        plt.tight_layout()
        exportFigure(plt, plotOutDir, plotOutFileName, metadata)
        plt.close('all')
    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")
コード例 #30
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 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")