Exemple #1
0
    def loadSimulation(self, request):

        self.fileUploadForm = DocumentForm(request.POST, request.FILES)
        if self.fileUploadForm.is_valid():

            sedml_archive = SEDMLSimulation(
                project=self.project, sedml_file=request.FILES['docfile'])
            sedml_archive.name = basename(str(
                request.FILES['docfile'])).split('.')[0]
            sedml_archive.save()

            # Now everything is in the same folder
            sedml_doc = SedmlDocument()
            sedml_doc.readSedmlFromFile(
                join(settings.MEDIA_ROOT, str(sedml_archive.sedml_file)))
            sedml_doc.listOfModels.removePaths()

            sbml_files = sedml_doc.listOfModels.makeLocalSources()

            for sbml_file in sbml_files:

                if len(
                        SbmlModel.objects.filter(
                            project=self.project,
                            sbml_file=join(
                                join(str(self.project.folder), "models"),
                                basename(sbml_file)))) == 0:

                    t_file = File(open(sbml_file, 'r'))
                    sbml_model = SbmlModel(project=self.project,
                                           sbml_file=t_file)
                    sbml_model.save()
                    try:
                        doc = SbmlDocument()

                        doc.readSbmlFromFile(
                            join(settings.MEDIA_ROOT,
                                 str(sbml_model.sbml_file)))

                        sbml_model.name = doc.model.getName()
                        sbml_model.save()
                    except ModelException:
                        name = os.path.splitext(str(sbml_model.sbml_file))[0]
                        sbml_model.name = name
                        sbml_model.save()

            sedml_doc.writeSedmlToFile(
                join(settings.MEDIA_ROOT, str(sedml_archive.sedml_file)))
            self.loadListOfSimulations()
Exemple #2
0
    def saveSimulation(self, request):

        self.form.read(request)
        if not self.form.hasErrors():
            self.createDocument()

            steady_states = self.createSteadyStates()

            if self.form.saveModelSnapshot:
                sbml_model = join(dirname(self.model_filename),
                                  copyModelHierarchy(self.model_filename))
            else:
                sbml_model = self.model_filename

            variables = []
            if self.form.selectedSpeciesIds is not None:
                for id_var in self.form.selectedSpeciesIds:
                    variables.append(self.listOfVariables[id_var])

            if self.form.selectedReactionsIds is not None:
                for id_var in self.form.selectedReactionsIds:
                    variables.append(self.listOfVariables[id_var])

            model = self.addModel(
                sbml_model,
                var_input=self.listOfVariables[self.form.speciesId])
            self.addSteadyStatesCurve(
                steady_states, model,
                ("Simulation" if self.form.simulationName is None else
                 self.form.simulationName), variables, self.form.steady_states,
                self.listOfVariables[self.form.speciesId])

            simulation_filename = join(settings.MEDIA_ROOT,
                                       new_sedml_filename())
            open(simulation_filename, "a")
            new_simulation = SEDMLSimulation(
                project=self.project,
                name=("Simulation" if self.form.simulationName is None else
                      self.form.simulationName),
                sedml_file=File(open(simulation_filename, "rb")),
                sbml_file=sbml_model)
            new_simulation.save()
            filename = join(settings.MEDIA_ROOT,
                            str(new_simulation.sedml_file))
            remove(simulation_filename)
            self.saveSedml(filename)
	def saveSimulation(self, request):

		self.form.read(request)
		if not self.form.hasErrors():
			self.createDocument()

			steady_states = self.createSteadyStates()

			if self.form.saveModelSnapshot:
				sbml_model = join(dirname(self.model_filename), copyModelHierarchy(self.model_filename))
			else:
				sbml_model = self.model_filename

			variables = []
			if self.form.selectedSpeciesIds is not None:
				for id_var in self.form.selectedSpeciesIds:
					variables.append(self.listOfVariables[id_var])

			if self.form.selectedReactionsIds is not None:
				for id_var in self.form.selectedReactionsIds:
					variables.append(self.listOfVariables[id_var])

			model = self.addModel(sbml_model, var_input=self.listOfVariables[self.form.speciesId])
			self.addSteadyStatesCurve(
				steady_states, model,
				("Simulation" if self.form.simulationName is None else self.form.simulationName),
				variables,
				self.form.steady_states,
				self.listOfVariables[self.form.speciesId]
			)

			simulation_filename = join(settings.MEDIA_ROOT, new_sedml_filename())
			open(simulation_filename, "a")
			new_simulation = SEDMLSimulation(
				project=self.project,
				name=("Simulation" if self.form.simulationName is None else self.form.simulationName),
				sedml_file=File(open(simulation_filename, "rb")),
				sbml_file=sbml_model
			)
			new_simulation.save()
			filename = join(settings.MEDIA_ROOT, str(new_simulation.sedml_file))
			remove(simulation_filename)
			self.saveSedml(filename)
	def loadSimulation(self, request):

		self.fileUploadForm = DocumentForm(request.POST, request.FILES)
		if self.fileUploadForm.is_valid():

			sedml_archive = SEDMLSimulation(project=self.project, sedml_file=request.FILES['docfile'])
			sedml_archive.name = basename(str(request.FILES['docfile'])).split('.')[0]
			sedml_archive.save()

			# Now everything is in the same folder
			sedml_doc = SedmlDocument()
			sedml_doc.readSedmlFromFile(join(settings.MEDIA_ROOT, str(sedml_archive.sedml_file)))
			sedml_doc.listOfModels.removePaths()

			sbml_files = sedml_doc.listOfModels.makeLocalSources()

			for sbml_file in sbml_files:

				if len(SbmlModel.objects.filter(project=self.project, sbml_file=join(join(str(self.project.folder), "models"), basename(sbml_file)))) == 0:

					sbml_model = SbmlModel(project=self.project, sbml_file=File(open(sbml_file, 'rb')))
					sbml_model.save()
					try:
						doc = SbmlDocument()

						doc.readSbmlFromFile(join(settings.MEDIA_ROOT, str(sbml_model.sbml_file)))

						sbml_model.name = doc.model.getName()
						sbml_model.save()
					except Exception:
						name = splitext(str(sbml_model.sbml_file))[0]
						sbml_model.name = name
						sbml_model.save()

			sedml_doc.writeSedmlToFile(join(settings.MEDIA_ROOT, str(sedml_archive.sedml_file)))
			self.loadListOfSimulations()
    def saveSimulation(self, request):

        self.form.read(request)
        if not self.form.hasErrors():
            self.createDocument()
            timecourse = self.createUniformTimecourse(self.form.timeMin,
                                                      self.form.timeMax,
                                                      self.form.timeEch)

            if self.form.saveModelSnapshot:
                sbml_model = join(dirname(self.model_filename),
                                  copyModelHierarchy(self.model_filename))
            else:
                sbml_model = self.model_filename

            if self.form.experimentId is not None:

                self.loadExperiments()
                t_experiment_id = self.experiments[self.form.experimentId].id
                experiment = Experiment.objects.get(id=t_experiment_id)
                conditions = Condition.objects.filter(experiment=experiment)

                for i, condition in enumerate(conditions):
                    modifications = []
                    input_data = Treatment.objects.filter(condition=condition)
                    for data in input_data:

                        var = None
                        if self.getModelInstance().listOfSpecies.containsName(
                                data.species):
                            var = self.getModelInstance(
                            ).listOfSpecies.getByName(data.species)
                        elif self.getModelInstance(
                        ).listOfParameters.containsName(data.species):
                            var = self.getModelInstance(
                            ).listOfParameters.getByName(data.species)
                        elif self.getModelInstance(
                        ).listOfCompartments.containsName(data.species):
                            var = self.getModelInstance(
                            ).listOfCompartments.getByName(data.species)

                        if var is not None:
                            modifications.append((var, data.value))

                    model = self.addModel(self.model_filename, modifications)

                    variables = []
                    if self.form.selectedSpeciesIds is not None:
                        for id_var in self.form.selectedSpeciesIds:
                            variables.append(self.listOfVariables[id_var])

                    if self.form.selectedReactionsIds is not None:
                        for id_var in self.form.selectedReactionsIds:
                            variables.append(self.listOfVariables[id_var])

                    self.addTimeseriesCurve(timecourse, model, condition.name,
                                            variables)
            else:

                model = self.addModel(self.model_filename)

                variables = []
                if self.form.selectedSpeciesIds is not None:
                    for id_var in self.form.selectedSpeciesIds:
                        variables.append(self.listOfVariables[id_var])

                if self.form.selectedReactionsIds is not None:
                    for id_var in self.form.selectedReactionsIds:
                        variables.append(self.listOfVariables[id_var])

                x_axis_variable = self.listOfVariables[self.form.speciesId]
                self.addPhaseSpaceCurve(
                    timecourse, model,
                    ("Simulation" if self.form.simulationName is None else
                     self.form.simulationName), variables, x_axis_variable)

            simulation_filename = join(settings.MEDIA_ROOT,
                                       new_sedml_filename())
            open(simulation_filename, "a")
            new_simulation = SEDMLSimulation(
                project=self.project,
                name=("Simulation" if self.form.simulationName is None else
                      self.form.simulationName),
                sedml_file=File(open(simulation_filename, "rb")),
                sbml_file=sbml_model)
            new_simulation.save()
            filename = join(settings.MEDIA_ROOT,
                            str(new_simulation.sedml_file))
            remove(simulation_filename)
            self.saveSedml(filename)
	def saveSimulation(self, request):

		self.form.read(request)
		if not self.form.hasErrors():
			self.createDocument()
			timecourse = self.createUniformTimecourse(self.form.timeMin, self.form.timeMax, self.form.timeEch)

			if self.form.saveModelSnapshot:
				sbml_model = join(dirname(self.model_filename), copyModelHierarchy(self.model_filename))
			else:
				sbml_model = self.model_filename

			if self.form.experimentId is not None:

				self.loadExperiments()
				t_experiment_id = self.experiments[self.form.experimentId].id
				experiment = Experiment.objects.get(id=t_experiment_id)
				conditions = Condition.objects.filter(experiment=experiment)

				for i, condition in enumerate(conditions):
					modifications = []
					input_data = Treatment.objects.filter(condition=condition)
					for data in input_data:

						var = None
						if self.getModelInstance().listOfSpecies.containsName(data.species):
							var = self.getModelInstance().listOfSpecies.getByName(data.species)
						elif self.getModelInstance().listOfParameters.containsName(data.species):
							var = self.getModelInstance().listOfParameters.getByName(data.species)
						elif self.getModelInstance().listOfCompartments.containsName(data.species):
							var = self.getModelInstance().listOfCompartments.getByName(data.species)

						if var is not None:
							modifications.append((var, data.value))

					model = self.addModel(sbml_model, modifications)

					variables = []
					if self.form.selectedSpeciesIds is not None:
						for id_var in self.form.selectedSpeciesIds:
							variables.append(self.listOfVariables[id_var])

					if self.form.selectedReactionsIds is not None:
						for id_var in self.form.selectedReactionsIds:
							variables.append(self.listOfVariables[id_var])

					self.addTimeseriesCurve(timecourse, model, condition.name, variables)
			else:

				model = self.addModel(sbml_model)

				variables = []
				if self.form.selectedSpeciesIds is not None:
					for id_var in self.form.selectedSpeciesIds:
						variables.append(self.listOfVariables[id_var])

				if self.form.selectedReactionsIds is not None:
					for id_var in self.form.selectedReactionsIds:
						variables.append(self.listOfVariables[id_var])

				self.addTimeseriesCurve(
					timecourse, model,
					("Simulation" if self.form.simulationName is None else self.form.simulationName),
					variables
				)

			simulation_filename = join(settings.MEDIA_ROOT, new_sedml_filename())
			open(simulation_filename, "a")
			new_simulation = SEDMLSimulation(
				project=self.project,
				name=("Simulation" if self.form.simulationName is None else self.form.simulationName),
				sedml_file=File(open(simulation_filename, "rb")),
				sbml_file=sbml_model)
			new_simulation.save()
			filename = join(settings.MEDIA_ROOT, str(new_simulation.sedml_file))
			remove(simulation_filename)
			self.saveSedml(filename)
Exemple #7
0
def importProject(new_folder, filename):

	new_combine_archive = CombineArchive()
	new_folder.name = basename(filename).split('.')[0]
	new_folder.save()

	try:
		new_combine_archive.readArchive(filename)

		deps = []
		for sbml_file in new_combine_archive.getAllSbmls():
			t_file = File(open(sbml_file, 'rb'))

			sbml_model = SbmlModel(project=new_folder, sbml_file=t_file)
			sbml_model.save()

			try:
				doc = SbmlDocument()

				doc.readSbmlFromFile(join(settings.MEDIA_ROOT, str(sbml_model.sbml_file)))

				dependencies = getDetailedModelDependencies(doc)
				if len(dependencies) > 0:
					deps.append((sbml_model, dependencies))

				sbml_model.name = doc.model.getName()
				sbml_model.save()
			except ModelException:
				name = splitext(str(sbml_model.sbml_file))[0]
				sbml_model.name = name
				sbml_model.save()

		for model, submodels in deps:

			for submodel_filename, submodel_ref in submodels:
				submodel_filename = join(new_folder.folder, "models", submodel_filename)
				submodel = SbmlModel.objects.get(sbml_file=submodel_filename)
				new_dependency = ModelsDependency(project=new_folder, model=model, submodel=submodel, submodel_ref=submodel_ref)
				new_dependency.save()

		for sedml_filename in new_combine_archive.getAllSedmls():

			sedml_archive = SEDMLSimulation(project=new_folder, sedml_file=File(open(sedml_filename, 'rb')))
			sedml_archive.name = basename(sedml_filename).split('.')[0]
			sedml_archive.save()

			# Now everything is in the same folder
			sedml_doc = SedmlDocument()
			sedml_doc.readSedmlFromFile(join(settings.MEDIA_ROOT, str(sedml_archive.sedml_file)))
			sedml_doc.listOfModels.removePaths()
			sbml_files = sedml_doc.listOfModels.makeLocalSources()

			for sbml_file in sbml_files:

				if len(SbmlModel.objects.filter(project=new_folder, sbml_file=join(join(str(new_folder.folder), "models"), basename(sbml_file)))) == 0:

					t_file = File(open(sbml_file, 'rb'))
					sbml_model = SbmlModel(project=new_folder, sbml_file=t_file)
					sbml_model.save()
					try:
						doc = SbmlDocument()

						doc.readSbmlFromFile(join(settings.MEDIA_ROOT, str(sbml_model.sbml_file)))

						sbml_model.name = doc.model.getName()
						sbml_model.save()
					except ModelException:
						name = splitext(str(sbml_model.sbml_file))[0]
						sbml_model.name = name
						sbml_model.save()

			sedml_doc.writeSedmlToFile(join(settings.MEDIA_ROOT, str(sedml_archive.sedml_file)))


		for numl_file in new_combine_archive.getAllNumls():
			new_experiment = Experiment(project=new_folder)
			new_experiment.save()
			importExperiment(new_experiment, numl_file)

	except CombineException as e:
		print(e.message)
Exemple #8
0
def copySimulation(simulation, new_project):

	t_file = File(open(join(settings.MEDIA_ROOT, str(simulation.sedml_file)), 'rb'))
	new_simulation = SEDMLSimulation(project=new_project, name=simulation.name, sedml_file=t_file)
	new_simulation.save()
	def loadFolder(self, request):

		self.fileUploadForm = DocumentForm(request.POST, request.FILES)
		if self.fileUploadForm.is_valid():

			new_folder = Project(user=request.user)
			new_folder.save()
			mkdir(join(settings.MEDIA_ROOT, str(new_folder.folder)))

			new_archive = CombineArchiveModel(project=new_folder, archive_file=request.FILES['docfile'])
			new_archive.save()

			new_combine_archive = CombineArchive()
			filename = join(settings.MEDIA_ROOT, str(new_archive.archive_file))
			t_name = basename(str(new_archive.archive_file))
			new_folder.name = t_name.split('.')[0]
			new_folder.save()

			try:
				new_combine_archive.readArchive(filename)

				for sbml_file in new_combine_archive.getAllSbmls():
					t_file = File(open(sbml_file, 'r'))

					sbml_model = SbmlModel(project=new_folder, sbml_file=t_file)
					sbml_model.save()

					try:
						doc = SbmlDocument()

						doc.readSbmlFromFile(join(settings.MEDIA_ROOT, str(sbml_model.sbml_file)))

						sbml_model.name = doc.model.getName()
						sbml_model.save()
					except ModelException:
						name = splitext(str(sbml_model.sbml_file))[0]
						sbml_model.name = name
						sbml_model.save()


				for sedml_filename in new_combine_archive.getAllSedmls():
					sedml_archive = SEDMLSimulation(project=new_folder, sedml_file=File(open(sedml_filename, 'r')))
					sedml_archive.name = basename(sedml_filename).split('.')[0]
					sedml_archive.save()

					# Now everything is in the same folder
					sedml_doc = SedmlDocument()
					sedml_doc.readSedmlFromFile(join(settings.MEDIA_ROOT, str(sedml_archive.sedml_file)))
					sedml_doc.listOfModels.removePaths()

					sbml_files = sedml_doc.listOfModels.makeLocalSources()

					for sbml_file in sbml_files:

						if len(SbmlModel.objects.filter(project=new_folder, sbml_file=join(join(str(new_folder.folder), "models"), basename(sbml_file)))) == 0:

							t_file = File(open(sbml_file, 'r'))
							sbml_model = SbmlModel(project=new_folder, sbml_file=t_file)
							sbml_model.save()
							try:
								doc = SbmlDocument()

								doc.readSbmlFromFile(join(settings.MEDIA_ROOT, str(sbml_model.sbml_file)))

								sbml_model.name = doc.model.getName()
								sbml_model.save()
							except ModelException:
								name = splitext(str(sbml_model.sbml_file))[0]
								sbml_model.name = name
								sbml_model.save()

					sedml_doc.writeSedmlToFile(join(settings.MEDIA_ROOT, str(sedml_archive.sedml_file)))



			except CombineException as e:
				print e.message
    def saveSimulation(self, request):

        self.form.read(request)
        if not self.form.hasErrors():

            sedml_doc = SedmlDocument()

            simulation = sedml_doc.listOfSimulations.createUniformTimeCourse()
            simulation.setInitialTime(self.form.timeMin)
            simulation.setOutputStartTime(self.form.timeMin)
            simulation.setOutputEndTime(self.form.timeMax)
            simulation.setNumberOfPoints(
                int((self.form.timeMax - self.form.timeMin) /
                    self.form.timeEch))
            simulation.getAlgorithm().setCVODE()

            if self.form.experimentId is not None:

                self.loadExperiments(request)
                t_experiment_id = self.experiments[self.form.experimentId].id
                experiment = Experiment.objects.get(id=t_experiment_id)
                conditions = Condition.objects.filter(experiment=experiment)

                self.nbConditions = len(conditions)
                self.experimentName = experiment.name
                self.experiment = SigNetSimExperiment()
                self.conditionNames = []
                self.observations = []

                for i, condition in enumerate(conditions):
                    input_data = Treatment.objects.filter(condition=condition)

                    model = sedml_doc.listOfModels.createModel()
                    model.setLanguageSbml()
                    model.setSource(self.model_filename)

                    for data in input_data:

                        var = None
                        if self.getModelInstance().listOfSpecies.containsName(
                                data.species):
                            var = self.getModelInstance(
                            ).listOfSpecies.getByName(data.species)
                        elif self.getModelInstance(
                        ).listOfParameters.containsName(data.species):
                            var = self.getModelInstance(
                            ).listOfParameters.getByName(data.species)
                        elif self.getModelInstance(
                        ).listOfCompartments.containsName(data.species):
                            var = self.getModelInstance(
                            ).listOfCompartments.getByName(data.species)

                        if var is not None:
                            change = model.listOfChanges.createChangeAttribute(
                            )
                            change.setTarget(var)
                            change.setNewValue(data.value)

                    task = sedml_doc.listOfTasks.createTask()
                    task.setModel(model)
                    task.setSimulation(simulation)

                    data_time = sedml_doc.listOfDataGenerators.createDataGenerator(
                    )
                    data_time.setName("Time")
                    var_time = data_time.listOfVariables.createVariable(
                        "time_%d" % i)
                    var_time.setTask(task)
                    var_time.setModel(model)
                    var_time.setSymbolTime()
                    data_time.getMath().setInternalMathFormula(
                        var_time.getSympySymbol())

                    plot2D = sedml_doc.listOfOutputs.createPlot2D()

                    plot2D.setName(str(condition.name))
                    i_var = 0
                    if self.form.selectedSpeciesIds is not None:
                        for id_var in self.form.selectedSpeciesIds:
                            i_var += 1
                            data = sedml_doc.listOfDataGenerators.createDataGenerator(
                                "data_%d_%d" % (i, i_var))
                            data.setName(
                                self.listOfVariables[id_var].getNameOrSbmlId())
                            var = data.listOfVariables.createVariable(
                                "var_%d_%d" % (i, i_var))
                            var.setTask(task)
                            var.setModel(model)
                            var.setTarget(self.listOfVariables[id_var])
                            data.getMath().setInternalMathFormula(
                                var.getSympySymbol())

                            curve = plot2D.listOfCurves.createCurve(
                                "curve_%d_%d" % (i, i_var))
                            curve.setXData(data_time)
                            curve.setYData(data)

                    if self.form.selectedReactionsIds is not None:
                        for id_var in self.form.selectedReactionsIds:
                            i_var += 1
                            data = sedml_doc.listOfDataGenerators.createDataGenerator(
                                "data_%d_%d" % (i, i_var))
                            data.setName(
                                self.listOfVariables[id_var].getNameOrSbmlId())
                            var = data.listOfVariables.createVariable(
                                "var_%d_%d" % (i, i_var))
                            var.setTask(task)
                            var.setModel(model)
                            var.setTarget(self.listOfVariables[id_var])
                            data.getMath().setInternalMathFormula(
                                var.getSympySymbol())

                            curve = plot2D.listOfCurves.createCurve(
                                "curve_%d_%d" % (i, i_var))
                            curve.setXData(data_time)
                            curve.setYData(data)

            else:

                model = sedml_doc.listOfModels.createModel()
                model.setLanguageSbml()
                model.setSource(self.model_filename)

                task = sedml_doc.listOfTasks.createTask()
                task.setModel(model)
                task.setSimulation(simulation)

                data_time = sedml_doc.listOfDataGenerators.createDataGenerator(
                )
                data_time.setName("Time")
                var_time = data_time.listOfVariables.createVariable("time")
                var_time.setTask(task)
                var_time.setModel(model)
                var_time.setSymbolTime()
                data_time.getMath().setInternalMathFormula(
                    var_time.getSympySymbol())

                plot2D = sedml_doc.listOfOutputs.createPlot2D()

                plot2D.setName("Simulation")
                i_var = 0
                if self.form.selectedSpeciesIds is not None:
                    for id_var in self.form.selectedSpeciesIds:
                        i_var += 1
                        data = sedml_doc.listOfDataGenerators.createDataGenerator(
                            "data_%d" % i_var)
                        data.setName(
                            self.listOfVariables[id_var].getNameOrSbmlId())
                        var = data.listOfVariables.createVariable("var_%d" %
                                                                  i_var)
                        var.setTask(task)
                        var.setModel(model)
                        var.setTarget(self.listOfVariables[id_var])
                        data.getMath().setInternalMathFormula(
                            var.getSympySymbol())

                        curve = plot2D.listOfCurves.createCurve("curve_%d" %
                                                                i_var)
                        curve.setXData(data_time)
                        curve.setYData(data)

                if self.form.selectedReactionsIds is not None:
                    for id_var in self.form.selectedReactionsIds:
                        i_var += 1
                        data = sedml_doc.listOfDataGenerators.createDataGenerator(
                            "data_%d" % i_var)
                        data.setName(
                            self.listOfVariables[id_var].getNameOrSbmlId())
                        var = data.listOfVariables.createVariable("var_%d" %
                                                                  i_var)
                        var.setTask(task)
                        var.setModel(model)
                        var.setTarget(self.listOfVariables[id_var])
                        data.getMath().setInternalMathFormula(
                            var.getSympySymbol())

                        curve = plot2D.listOfCurves.createCurve("curve_%d" %
                                                                i_var)
                        curve.setXData(data_time)
                        curve.setYData(data)

            open("simulation.xml", "a")

            new_simulation = SEDMLSimulation(project=self.project,
                                             name="Simulation",
                                             sedml_file=File(
                                                 open("simulation.xml", "r")))
            new_simulation.save()

            filename = join(settings.MEDIA_ROOT,
                            str(new_simulation.sedml_file))
            sedml_doc.writeSedmlToFile(filename)