Example #1
0
    def __init__(self, **kwargs):

        TemplateView.__init__(self, **kwargs)
        HasWorkingProject.__init__(self)

        self.listOfSimulations = None
        self.fileUploadForm = DocumentForm()
Example #2
0
    def loadModel(self, request):

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

            new_sbml_model = SbmlModel(project=self.project,
                                       sbml_file=request.FILES['docfile'])
            new_sbml_model.save()

            try:
                doc = SbmlDocument()
                doc.readSbmlFromFile(
                    os.path.join(settings.MEDIA_ROOT,
                                 str(new_sbml_model.sbml_file)))

                new_sbml_model.name = doc.model.getName()
                new_sbml_model.save()

            except MissingSubmodelException:
                new_sbml_model.delete()
                self.addError(
                    "This model is importing some models which were not found in the project folder. Please import them first"
                )

            # Is triggered where name is None ??
            except ModelException:
                name = os.path.splitext(str(new_sbml_model.sbml_file))[0]
                new_sbml_model.name = name
                new_sbml_model.save()
    def __init__(self, **kwargs):

        TemplateView.__init__(self, **kwargs)
        HasWorkingProject.__init__(self)
        HasUserLoggedIn.__init__(self)
        HasErrorMessages.__init__(self)

        self.listOfModels = None
        self.fileUploadForm = DocumentForm()
Example #4
0
	def __init__(self, **kwargs):

		TemplateView.__init__(self, **kwargs)
		HasWorkingProject.__init__(self)

		self.listOfExperiments = None
		self.experimentId = None
		self.experimentName = None
		self.experimentNotes = None
		self.fileUploadForm = DocumentForm()
Example #5
0
	def __init__(self, **kwargs):

		TemplateView.__init__(self, **kwargs)
		HasWorkingProject.__init__(self)

		self.listOfFolders = None
		self.createFolderShow = None
		self.createFolderError = None
		self.sendFolderShow = None
		self.sendFolderError = None
		self.fileUploadForm = DocumentForm()
Example #6
0
	def importExperiment(self, request):

		if self.isProjectOwner(request):

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

				new_experiment = Experiment(project=self.project)
				new_experiment.save()

				archive = request.FILES['docfile']
				path = default_storage.save(str(archive), ContentFile(archive.read()))
				importExperimentViaManager(new_experiment, str(join(settings.MEDIA_ROOT, path)))
				remove(join(settings.MEDIA_ROOT, path))
Example #7
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()
	def __init__(self, **kwargs):

		TemplateView.__init__(self, **kwargs)
		HasWorkingProject.__init__(self)

		self.listOfSimulations = None
		self.fileUploadForm = DocumentForm()
Example #9
0
	def __init__(self, **kwargs):

		TemplateView.__init__(self, **kwargs)
		HasWorkingProject.__init__(self)
		HasUserLoggedIn.__init__(self)
		HasErrorMessages.__init__(self)

		self.listOfModels = None
		self.fileUploadForm = DocumentForm()
    def loadModel(self, request):

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

            new_sbml_model = SbmlModel(project=self.project,
                                       sbml_file=request.FILES['docfile'])
            new_sbml_model.save()

            try:
                doc = SbmlDocument()
                doc.readSbmlFromFile(
                    os.path.join(settings.MEDIA_ROOT,
                                 str(new_sbml_model.sbml_file)))

                dependencies = getDetailedModelDependencies(doc)
                if len(dependencies) > 0:

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

                new_sbml_model.name = doc.model.getName()
                new_sbml_model.save()

            except MissingSubmodelException:
                new_sbml_model.delete()
                self.addError(
                    "This model is importing some models which were not found in the project folder. Please import them first"
                )

            # Is triggered where name is None ??
            except ModelException:
                name = os.path.splitext(str(new_sbml_model.sbml_file))[0]
                new_sbml_model.name = name
                new_sbml_model.save()
Example #11
0
	def loadModel(self, request):

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

			new_sbml_model = SbmlModel(project=self.project, sbml_file=request.FILES['docfile'])
			new_sbml_model.save()

			try:
				doc = SbmlDocument()
				doc.readSbmlFromFile(os.path.join(settings.MEDIA_ROOT, str(new_sbml_model.sbml_file)))

				dependencies = getDetailedModelDependencies(doc)
				if len(dependencies) > 0:

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

				new_sbml_model.name = doc.model.getName()
				new_sbml_model.save()

			except MissingSubmodelException:
				new_sbml_model.delete()
				self.addError(
					"This model is importing some models which were not found in the project folder. Please import them first")

			# Is triggered where name is None ??
			except ModelException:
				name = os.path.splitext(str(new_sbml_model.sbml_file))[0]
				new_sbml_model.name = name
				new_sbml_model.save()
Example #12
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:

					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()
Example #13
0
class DataView(TemplateView, HasWorkingProject):

	template_name = 'data/data.html'

	def __init__(self, **kwargs):

		TemplateView.__init__(self, **kwargs)
		HasWorkingProject.__init__(self)

		self.listOfExperiments = None
		self.experimentId = None
		self.experimentName = None
		self.experimentNotes = None
		self.fileUploadForm = DocumentForm()


	def get_context_data(self, **kwargs):

		kwargs = HasWorkingProject.get_context_data(self, **kwargs)

		kwargs['experimental_data'] = self.listOfExperiments
		kwargs['experiment_id'] = self.experimentId
		kwargs['experiment_name'] = self.experimentName
		kwargs['experiment_notes'] = self.experimentNotes
		kwargs['file_upload_form'] = self.fileUploadForm
		return kwargs


	def get(self, request, *args, **kwargs):

		HasWorkingProject.load(self, request, *args, **kwargs)
		self.loadExperiments(request)
		return TemplateView.get(self, request, *args, **kwargs)


	def post(self, request, *args, **kwargs):

		HasWorkingProject.load(self, request, *args, **kwargs)
		self.loadExperiments(request)

		if "action" in request.POST:

			if HasWorkingProject.isChooseProject(self, request):
				self.loadExperiments(request)

			elif request.POST['action'] == "delete":
				self.deleteExperiment(request)

			elif request.POST['action'] == "save":
				self.saveExperiment(request)

			elif request.POST['action'] == "duplicate":
				self.duplicateExperiment(request)

			elif request.POST['action'] == "import":
				self.importExperiment(request)

		return TemplateView.get(self, request, *args, **kwargs)


	def loadExperiments(self, request):
		self.listOfExperiments = Experiment.objects.filter(project=self.project)


	def deleteExperiment(self, request):

		if self.isProjectOwner(request):

			experiment = Experiment.objects.get(project=self.project,
												id=request.POST['id'])

			deleteExperimentViaManager(experiment)

	def saveExperiment(self, request):

		if self.isProjectOwner(request):
			if str(request.POST['experiment_id']) == "":
				t_experiment = Experiment(project=self.project)
			else:
				t_experiment = Experiment.objects.get(project=self.project, id=str(request.POST['experiment_id']))

			t_experiment.name = str(request.POST['experiment_name'])
			t_experiment.notes = str(request.POST['experiment_notes'])
			t_experiment.save()


	def duplicateExperiment(self, request):

		if self.isProjectOwner(request):

			t_experiment = Experiment.objects.get(project=self.project,
												  id=request.POST['id'])

			new_experiment = Experiment(project=self.project)
			new_experiment.save()
			copyExperimentViaManager(t_experiment, new_experiment)


	def importExperiment(self, request):

		if self.isProjectOwner(request):

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

				new_experiment = Experiment(project=self.project)
				new_experiment.save()

				archive = request.FILES['docfile']
				path = default_storage.save(str(archive), ContentFile(archive.read()))
				importExperimentViaManager(new_experiment, str(join(settings.MEDIA_ROOT, path)))
				remove(join(settings.MEDIA_ROOT, path))
class ListOfModelsView(TemplateView, HasWorkingProject, HasUserLoggedIn,
                       HasErrorMessages):

    template_name = 'models/models.html'

    def __init__(self, **kwargs):

        TemplateView.__init__(self, **kwargs)
        HasWorkingProject.__init__(self)
        HasUserLoggedIn.__init__(self)
        HasErrorMessages.__init__(self)

        self.listOfModels = None
        self.fileUploadForm = DocumentForm()

    def get_context_data(self, **kwargs):

        kwargs = HasWorkingProject.get_context_data(self, **kwargs)
        kwargs = HasErrorMessages.get_context_data(self, **kwargs)

        kwargs['form'] = self.fileUploadForm
        kwargs['sbml_models'] = self.listOfModels

        return kwargs

    def get(self, request, *args, **kwargs):

        if len(args) > 0:
            HasWorkingProject.load(self, request, *args, **kwargs)
            self.setProject(request, args[0])
            return redirect('models')

        self.load(request, *args, **kwargs)
        return TemplateView.get(self, request, *args, **kwargs)

    def post(self, request, *args, **kwargs):

        self.load(request, *args, **kwargs)

        if "action" in request.POST:
            if HasWorkingProject.isChooseProject(self, request):
                self.loadModels(request)

            elif request.POST['action'] == "delete_model":
                self.deleteModel(request)
                self.load(request, *args, **kwargs)

            elif request.POST['action'] == "load_model":
                self.loadModel(request)
                self.load(request, *args, **kwargs)

            elif request.POST['action'] == "duplicate_model":
                self.duplicateModel(request)
                self.load(request, *args, **kwargs)

            elif request.POST['action'] == "new_model":
                self.newModel(request)
                self.load(request, *args, **kwargs)

            elif request.POST['action'] == "load_biomodels":
                self.loadBiomodels(request)
                self.load(request, *args, **kwargs)

        return TemplateView.get(self, request, *args, **kwargs)

    def load(self, request, *args, **kwargs):

        HasWorkingProject.load(self, request, *args, **kwargs)

        self.loadModels(request)

    def newModel(self, request):

        model_name = str(request.POST['model_name'])
        model_filename = os.path.join(settings.MEDIA_ROOT,
                                      new_model_filename())

        open(model_filename, "a")
        new_model = SbmlModel(project=self.project,
                              name=model_name,
                              sbml_file=File(open(model_filename, "rb")))
        os.remove(model_filename)
        new_model.save()

        doc = SbmlDocument()
        doc.model.newModel(model_name)
        doc.writeSbmlToFile(
            os.path.join(settings.MEDIA_ROOT, str(new_model.sbml_file)))

    def duplicateModel(self, request):

        model = SbmlModel.objects.get(id=request.POST['id'])
        t_file = File(
            open(os.path.join(settings.MEDIA_ROOT, str(model.sbml_file)),
                 'rb'))

        new_sbml_model = SbmlModel(project=self.project,
                                   name=("%s (copy)" % str(model.name)),
                                   sbml_file=t_file)

        new_sbml_model.save()
        t_filename = os.path.join(settings.MEDIA_ROOT,
                                  str(new_sbml_model.sbml_file))

        doc = SbmlDocument()
        doc.readSbmlFromFile(t_filename)
        doc.model.setName(str(new_sbml_model.name))
        doc.writeSbmlToFile(t_filename)

    def loadModel(self, request):

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

            new_sbml_model = SbmlModel(project=self.project,
                                       sbml_file=request.FILES['docfile'])
            new_sbml_model.save()

            try:
                doc = SbmlDocument()
                doc.readSbmlFromFile(
                    os.path.join(settings.MEDIA_ROOT,
                                 str(new_sbml_model.sbml_file)))

                dependencies = getDetailedModelDependencies(doc)
                if len(dependencies) > 0:

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

                new_sbml_model.name = doc.model.getName()
                new_sbml_model.save()

            except MissingSubmodelException:
                new_sbml_model.delete()
                self.addError(
                    "This model is importing some models which were not found in the project folder. Please import them first"
                )

            # Is triggered where name is None ??
            except ModelException:
                name = os.path.splitext(str(new_sbml_model.sbml_file))[0]
                new_sbml_model.name = name
                new_sbml_model.save()

    def deleteModel(self, request):

        model = SbmlModel.objects.get(project=self.project,
                                      id=request.POST['id'])

        for model_dependency in ModelsDependency.objects.filter(
                project=self.project, submodel=model):
            if model_dependency.model != model:
                self.addError(
                    "This model is used as a submodel by %s and cannot be removed."
                    % model_dependency.model.name)

        if not self.hasErrors():
            ModelsDependency.objects.filter(project=self.project,
                                            model=model).delete()
            os.remove(os.path.join(settings.MEDIA_ROOT, str(model.sbml_file)))
            model.delete()

    def loadBiomodels(self, request):

        model_id = str(request.POST['model_id'])
        model_filename = os.path.join(settings.MEDIA_ROOT,
                                      new_model_filename())

        open(model_filename, "a")
        new_model = SbmlModel(project=self.project,
                              sbml_file=File(open(model_filename, "rb")))
        os.remove(model_filename)
        new_model.save()

        doc = SbmlDocument()
        try:

            doc.readFromBiomodels(model_id)
            doc.writeSbmlToFile(
                os.path.join(settings.MEDIA_ROOT, str(new_model.sbml_file)))
            new_model.name = doc.model.getName()
            new_model.save()

        except:
            self.addError("Unable to load model from biomodels")
            new_model.delete()

    def loadModels(self, request, *args, **kwargs):

        # self.projectId = str(args[0])
        # self.project = ModelFolder.objects.get(user=request.user, id=self.projectId)
        if self.project is not None:
            self.listOfModels = []
            for model in SbmlModel.objects.filter(project=self.project):
                filename = str(model.sbml_file)
                if filename.startswith(settings.MEDIA_ROOT):
                    filename = filename.replace(settings.MEDIA_ROOT, "")
                self.listOfModels.append((model.id, model.name, filename))
Example #15
0
	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
Example #16
0
class ListOfProjectsView(TemplateView, HasWorkingProject):

	template_name = 'models/projects.html'

	def __init__(self, **kwargs):

		TemplateView.__init__(self, **kwargs)
		HasWorkingProject.__init__(self)

		self.listOfFolders = None
		self.createFolderShow = None
		self.createFolderError = None
		self.sendFolderShow = None
		self.sendFolderError = None
		self.fileUploadForm = DocumentForm()

	def get_context_data(self, **kwargs):

		kwargs = HasWorkingProject.get_context_data(self, **kwargs)

		kwargs['create_folder_error'] = self.createFolderError
		kwargs['create_folder_show'] = self.createFolderShow

		kwargs['send_folder_error'] = self.sendFolderError
		kwargs['send_folder_show'] = self.sendFolderShow
		kwargs['load_project_form'] = self.fileUploadForm
		return kwargs


	def get(self, request, *args, **kwargs):

		self.load(request, *args, **kwargs)
		return TemplateView.get(self, request, *args, **kwargs)


	def post(self, request, *args, **kwargs):

		self.load(request, *args, **kwargs)

		if "action" in request.POST:
			if HasWorkingProject.isChooseProject(self, request):
				self.load(request, *args, **kwargs)

			elif request.POST['action'] == "new_folder":
				self.newFolder(request)

			elif request.POST['action'] == "copy_folder":
				self.copyFolder(request)

			elif request.POST['action'] == "delete_folder":
				self.deleteFolder(request)

			elif request.POST['action'] == "send_folder":
				self.sendFolder(request)

			elif request.POST['action'] == "load_folder":
				self.loadFolder(request)

			elif request.POST['action'] == "save_project":
				self.saveProject(request)

		return TemplateView.get(self, request, *args, **kwargs)


	def load(self, request, *args, **kwargs):

		HasWorkingProject.load(self, request, *args, **kwargs)
		if self.isUserLoggedIn(request):
			self.loadFolders(request)

	def newFolder(self, request):

		name = str(request.POST['project_name'])
		access = False
		if 'project_access' in request.POST and request.POST['project_access'] == "on":
			access = True

		if not Project.objects.filter(user=request.user, name=name).exists():
			new_folder = Project(user=request.user, name=name)
			if access:
				new_folder.access = 'PU'
			else:
				new_folder.access = 'PR'

			new_folder.save()
			mkdir(join(settings.MEDIA_ROOT, str(new_folder.folder)))

			self.loadFolders(request)
		else:
			self.createFolderShow = True
			self.createFolderError = "Project %s already exists !" % name

	def copyFolder(self, request):

		folder_id = str(request.POST['id'])
		if Project.objects.filter(user=request.user, id=folder_id).exists():

			folder = Project.objects.get(user=request.user, id=folder_id)
			new_folder = Project(user=request.user, name=(str(folder.name) + " (Copy)"))
			copyProject(folder, new_folder)


	def deleteFolder(self, request):

		folder_id = str(request.POST['id'])

		if self.project_id == int(folder_id):
			self.unsetProject(request)

		if Project.objects.filter(user=request.user, id=folder_id).exists():

			project = Project.objects.get(user=request.user, id=folder_id)
			deleteProject(project)


	def sendFolder(self, request):

		folder_id = str(request.POST['id'])
		t_username = str(request.POST['username'])
		try :
			t_user = User.objects.get(username=t_username)
			if Project.objects.filter(user=request.user, id=folder_id).exists():

				folder = Project.objects.get(user=request.user, id=folder_id)
				new_folder = Project(user=t_user, name=str(folder.name))
				copyProject(folder, new_folder)


		except User.DoesNotExist:
			self.sendFolderShow = True
			self.sendFolderError = "Username %s does not exist" % t_username

	def saveProject(self, request):

		if 'project_name' in request.POST and 'project_id' in request.POST:
			if request.POST['project_id'] != "":
				id = request.POST['project_id']
				name = request.POST['project_name']
				access = False
				if 'project_access' in request.POST and request.POST['project_access'] == "on":
					access = True

				if Project.objects.filter(id=id).exists():
					project = Project.objects.get(id=id)
					project.name = name
					if access:
						project.access = 'PU'
					else:
						project.access = 'PR'
					project.save()

			else:
				self.newFolder(request)

	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 loadFolders(self, request):
		self.listOfFolders = Project.objects.filter(user=request.user)
Example #17
0
class ListOfModelsView(TemplateView, HasWorkingProject, HasUserLoggedIn, HasErrorMessages):

	template_name = 'models/models.html'

	def __init__(self, **kwargs):

		TemplateView.__init__(self, **kwargs)
		HasWorkingProject.__init__(self)
		HasUserLoggedIn.__init__(self)
		HasErrorMessages.__init__(self)

		self.listOfModels = None
		self.fileUploadForm = DocumentForm()


	def get_context_data(self, **kwargs):

		kwargs = HasWorkingProject.get_context_data(self, **kwargs)
		kwargs = HasErrorMessages.get_context_data(self, **kwargs)

		kwargs['form'] = self.fileUploadForm
		kwargs['sbml_models'] = self.listOfModels

		return kwargs


	def get(self, request, *args, **kwargs):

		if len(args) > 0:
			HasWorkingProject.load(self, request, *args, **kwargs)
			self.setProject(request, args[0])
			return redirect('models')

		self.load(request, *args, **kwargs)
		return TemplateView.get(self, request, *args, **kwargs)


	def post(self, request, *args, **kwargs):

		self.load(request, *args, **kwargs)

		if "action" in request.POST:
			if HasWorkingProject.isChooseProject(self, request):
				self.loadModels(request)

			elif request.POST['action'] == "delete_model":
				self.deleteModel(request)
				self.load(request, *args, **kwargs)


			elif request.POST['action'] == "load_model":
				self.loadModel(request)
				self.load(request, *args, **kwargs)

			elif request.POST['action'] == "duplicate_model":
				self.duplicateModel(request)
				self.load(request, *args, **kwargs)

			elif request.POST['action'] == "new_model":
				self.newModel(request)
				self.load(request, *args, **kwargs)

			elif request.POST['action'] == "load_biomodels":
				self.loadBiomodels(request)
				self.load(request, *args, **kwargs)


		return TemplateView.get(self, request, *args, **kwargs)


	def load(self, request, *args, **kwargs):

		HasWorkingProject.load(self, request, *args, **kwargs)

		self.loadModels(request)

	def newModel(self, request):

		model_name = str(request.POST['model_name'])
		model_filename = os.path.join(settings.MEDIA_ROOT, new_model_filename())

		open(model_filename, "a")
		new_model = SbmlModel(project=self.project, name=model_name, sbml_file=File(open(model_filename, "rb")))
		os.remove(model_filename)
		new_model.save()

		doc = SbmlDocument()
		doc.model.newModel(model_name)
		doc.writeSbmlToFile(os.path.join(settings.MEDIA_ROOT, str(new_model.sbml_file)))


	def duplicateModel(self, request):

		model = SbmlModel.objects.get(id=request.POST['id'])
		t_file = File(open(os.path.join(settings.MEDIA_ROOT, str(model.sbml_file)), 'rb'))

		new_sbml_model = SbmlModel(project=self.project,
										name=("%s (copy)" % str(model.name)),
										sbml_file=t_file)

		new_sbml_model.save()
		t_filename = os.path.join(settings.MEDIA_ROOT, str(new_sbml_model.sbml_file))

		doc = SbmlDocument()
		doc.readSbmlFromFile(t_filename)
		doc.model.setName(str(new_sbml_model.name))
		doc.writeSbmlToFile(t_filename)


	def loadModel(self, request):

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

			new_sbml_model = SbmlModel(project=self.project, sbml_file=request.FILES['docfile'])
			new_sbml_model.save()

			try:
				doc = SbmlDocument()
				doc.readSbmlFromFile(os.path.join(settings.MEDIA_ROOT, str(new_sbml_model.sbml_file)))

				dependencies = getDetailedModelDependencies(doc)
				if len(dependencies) > 0:

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

				new_sbml_model.name = doc.model.getName()
				new_sbml_model.save()

			except MissingSubmodelException:
				new_sbml_model.delete()
				self.addError(
					"This model is importing some models which were not found in the project folder. Please import them first")

			# Is triggered where name is None ??
			except ModelException:
				name = os.path.splitext(str(new_sbml_model.sbml_file))[0]
				new_sbml_model.name = name
				new_sbml_model.save()


	def deleteModel(self, request):

		model = SbmlModel.objects.get(project=self.project, id=request.POST['id'])

		for model_dependency in ModelsDependency.objects.filter(project=self.project, submodel=model):
			if model_dependency.model != model:
				self.addError("This model is used as a submodel by %s and cannot be removed." % model_dependency.model.name)

		if not self.hasErrors():
			ModelsDependency.objects.filter(project=self.project, model=model).delete()
			os.remove(os.path.join(settings.MEDIA_ROOT, str(model.sbml_file)))
			model.delete()

	def loadBiomodels(self, request):

		model_id = str(request.POST['model_id'])
		model_filename = os.path.join(settings.MEDIA_ROOT, new_model_filename())

		open(model_filename, "a")
		new_model = SbmlModel(project=self.project, sbml_file=File(open(model_filename, "rb")))
		os.remove(model_filename)
		new_model.save()


		doc = SbmlDocument()
		try:

			doc.readFromBiomodels(model_id)
			doc.writeSbmlToFile(os.path.join(settings.MEDIA_ROOT, str(new_model.sbml_file)))
			new_model.name = doc.model.getName()
			new_model.save()

		except:
			self.addError("Unable to load model from biomodels")
			new_model.delete()

	def loadModels(self, request, *args, **kwargs):

		# self.projectId = str(args[0])
		# self.project = ModelFolder.objects.get(user=request.user, id=self.projectId)
		if self.project is not None:
			self.listOfModels = []
			for model in SbmlModel.objects.filter(project=self.project):
				filename = str(model.sbml_file)
				if filename.startswith(settings.MEDIA_ROOT):
					filename = filename.replace(settings.MEDIA_ROOT, "")
				self.listOfModels.append((model.id, model.name, filename))
Example #18
0
class ListOfSimulationsView(TemplateView, HasWorkingProject):

    template_name = 'simulate/list_of_simulations.html'

    def __init__(self, **kwargs):

        TemplateView.__init__(self, **kwargs)
        HasWorkingProject.__init__(self)

        self.listOfSimulations = None
        self.fileUploadForm = DocumentForm()

    def get_context_data(self, **kwargs):

        kwargs = HasWorkingProject.get_context_data(self, **kwargs)
        kwargs['list_of_simulations'] = self.listOfSimulations
        kwargs['load_simulation_form'] = self.fileUploadForm

        return kwargs

    def get(self, request, *args, **kwargs):

        self.load(request, *args, **kwargs)
        return TemplateView.get(self, request, *args, **kwargs)

    def post(self, request, *args, **kwargs):

        self.load(request, *args, **kwargs)

        if "action" in request.POST:
            if HasWorkingProject.isChooseProject(self, request):
                self.load(request, *args, **kwargs)

            elif request.POST['action'] == "delete_simulation":
                self.deleteSimulation(request)

            elif request.POST['action'] == "load_simulation":
                self.loadSimulation(request)

        return TemplateView.get(self, request, *args, **kwargs)

    def load(self, request, *args, **kwargs):

        HasWorkingProject.load(self, request, *args, **kwargs)
        self.loadListOfSimulations()

    def loadListOfSimulations(self):
        self.listOfSimulations = []

        for simulation in SEDMLSimulation.objects.filter(project=self.project):
            self.listOfSimulations.append(
                (simulation.name, str(simulation.sedml_file)))

    def deleteSimulation(self, request):

        if 'id' in request.POST and request.POST['id'] != "":
            t_simulation = SEDMLSimulation.objects.filter(
                project=self.project)[int(request.POST['id'])]
            remove(join(settings.MEDIA_ROOT, str(t_simulation.sedml_file)))
            t_simulation.delete()
            self.loadListOfSimulations()

    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()
Example #19
0
class ListOfSimulationsView(TemplateView, HasWorkingProject):

	template_name = 'simulate/list_of_simulations.html'

	def __init__(self, **kwargs):

		TemplateView.__init__(self, **kwargs)
		HasWorkingProject.__init__(self)

		self.listOfSimulations = None
		self.fileUploadForm = DocumentForm()


	def get_context_data(self, **kwargs):

		kwargs = HasWorkingProject.get_context_data(self, **kwargs)
		kwargs['list_of_simulations'] = self.listOfSimulations
		kwargs['load_simulation_form'] = self.fileUploadForm

		return kwargs


	def get(self, request, *args, **kwargs):

		self.load(request, *args, **kwargs)
		return TemplateView.get(self, request, *args, **kwargs)


	def post(self, request, *args, **kwargs):

		self.load(request, *args, **kwargs)

		if "action" in request.POST:
			if HasWorkingProject.isChooseProject(self, request):
				self.load(request, *args, **kwargs)

			elif request.POST['action'] == "delete_simulation":
				self.deleteSimulation(request)

			elif request.POST['action'] == "load_simulation":
				self.loadSimulation(request)

		return TemplateView.get(self, request, *args, **kwargs)

	def load(self, request, *args, **kwargs):

		HasWorkingProject.load(self, request, *args, **kwargs)
		self.loadListOfSimulations()

	def loadListOfSimulations(self):
		self.listOfSimulations = []

		for simulation in SEDMLSimulation.objects.filter(project=self.project):
			self.listOfSimulations.append((simulation.id, simulation.name, str(simulation.sedml_file)))

	def deleteSimulation(self, request):

		if 'id' in request.POST and request.POST['id'] != "":
			t_simulation = SEDMLSimulation.objects.get(project=self.project, id=int(request.POST['id']))
			deleteSimulation(t_simulation)
			self.loadListOfSimulations()


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