Esempio n. 1
0
    def testRule(self):

        user = User.objects.filter(username='******')[0]
        self.assertEqual(len(Project.objects.filter(user=user)), 1)
        project = Project.objects.filter(user=user)[0]
        self.assertEqual(len(SbmlModel.objects.filter(project=project)), 0)

        if isdir(join(settings.MEDIA_ROOT, project.folder)):
            rmtree(join(settings.MEDIA_ROOT, project.folder))
            mkdir(join(settings.MEDIA_ROOT, project.folder))

        c = Client()
        self.assertTrue(c.login(username='******', password='******'))

        response_choose_project = c.get('/project/%s/' % project.folder)
        self.assertRedirects(response_choose_project,
                             '/models/',
                             status_code=302,
                             target_status_code=200)

        files_folder = join(dirname(__file__), "files")
        model_filename = join(files_folder, "modelqlzB7i.xml")

        response_load_model = c.post('/models/', {
            'action': 'load_model',
            'docfile': open(model_filename, 'rb')
        })

        self.assertEqual(response_load_model.status_code, 200)
        self.assertEqual(len(SbmlModel.objects.filter(project=project)), 1)

        model = SbmlModel.objects.filter(project=project)[0]
        sbml_doc = SbmlDocument()
        sbml_doc.readSbmlFromFile(
            join(settings.MEDIA_ROOT, str(model.sbml_file)))
        sbml_model = sbml_doc.getModelInstance()
        listOfRules = sbml_model.listOfRules + sbml_model.listOfInitialAssignments
        listOfVariables = []
        for variable in sbml_model.listOfVariables:
            if ((variable.isParameter() and variable.isGlobal())
                    or variable.isSpecies() or variable.isCompartment()
                    or variable.isStoichiometry()):
                listOfVariables.append(variable)

        rule = listOfRules[0]

        response_get_compartment = c.post('/json/get_rule/', {
            'rule_ind': '0',
        })

        self.assertEqual(response_get_compartment.status_code, 200)
        json_response = loads(response_get_compartment.content.decode('utf-8'))

        self.assertEqual(json_response[u'rule_id'], listOfRules.index(rule))
        self.assertEqual(json_response[u'rule_type'], 1)
        self.assertEqual(
            json_response[u'variable'],
            listOfVariables.index(
                sbml_model.listOfVariables.getBySbmlId('total_ras_gtp')))

        response_choose_project = c.post('/models/', {
            'action': 'choose_project',
            'project_id': 0
        })
        self.assertEqual(response_choose_project.status_code, 200)
        self.assertEqual(response_choose_project.context['project_name'],
                         "Project")

        response_choose_model = c.post('/edit/rules/', {
            'action': 'choose_model',
            'model_id': 0
        })
        self.assertEqual(response_choose_model.status_code, 200)
        self.assertEqual(response_choose_model.context['model_name'],
                         "SOS-Ras-MAPK with n17")

        response_save_rule = c.post(
            '/edit/rules/', {
                'action':
                'save',
                'rule_id':
                listOfRules.index(rule),
                'rule_type':
                1,
                'variable_id':
                listOfVariables.index(
                    sbml_model.listOfVariables.getBySbmlId(
                        'total_mapk_activated')),
                'rule_expression':
                "75*ras_gtp",
            })

        self.assertEqual(response_save_rule.status_code, 200)
        self.assertEqual(response_save_rule.context['form'].getErrors(), [])

        sbml_doc = SbmlDocument()
        sbml_doc.readSbmlFromFile(
            join(settings.MEDIA_ROOT, str(model.sbml_file)))
        sbml_model = sbml_doc.getModelInstance()
        listOfRules = sbml_model.listOfRules + sbml_model.listOfInitialAssignments
        listOfVariables = []
        for variable in sbml_model.listOfVariables:
            if ((variable.isParameter() and variable.isGlobal())
                    or variable.isSpecies() or variable.isCompartment()
                    or variable.isStoichiometry()):
                listOfVariables.append(variable)

        rule = listOfRules[0]

        self.assertEqual(rule.getType(), 1)
        self.assertEqual(
            rule.getVariable(),
            sbml_model.listOfVariables.getBySbmlId('total_mapk_activated'))
        formula = MathFormula(sbml_model)
        formula.setPrettyPrintMathFormula("75*ras_gtp", rawFormula=True)
        self.assertEqual(
            simplify(rule.getDefinition().getDeveloppedInternalMathFormula() -
                     formula.getDeveloppedInternalMathFormula()), 0)

        response_delete_rule = c.post('/edit/rules/', {
            'action': 'delete',
            'rule_id': 0
        })
        self.assertEqual(response_delete_rule.status_code, 200)
        self.assertEqual(response_delete_rule.context['form'].getErrors(), [])
Esempio n. 2
0
	def testRule(self):

		user = User.objects.filter(username='******')[0]
		self.assertEqual(len(Project.objects.filter(user=user)), 1)
		project = Project.objects.filter(user=user)[0]
		self.assertEqual(len(SbmlModel.objects.filter(project=project)), 0)

		if isdir(join(settings.MEDIA_ROOT, project.folder)):
			rmtree(join(settings.MEDIA_ROOT, project.folder))
			mkdir(join(settings.MEDIA_ROOT, project.folder))

		c = Client()
		self.assertTrue(c.login(username='******', password='******'))

		response_choose_project = c.get('/project/%s/' % project.folder)
		self.assertRedirects(response_choose_project, '/models/', status_code=302, target_status_code=200)

		files_folder = join(dirname(__file__), "files")
		model_filename = join(files_folder, "modelqlzB7i.xml")

		response_load_model = c.post('/models/', {
			'action': 'load_model',
			'docfile': open(model_filename, 'rb')
		})

		self.assertEqual(response_load_model.status_code, 200)
		self.assertEqual(len(SbmlModel.objects.filter(project=project)), 1)

		model = SbmlModel.objects.filter(project=project)[0]
		sbml_doc = SbmlDocument()
		sbml_doc.readSbmlFromFile(join(settings.MEDIA_ROOT, str(model.sbml_file)))
		sbml_model = sbml_doc.getModelInstance()
		listOfRules = sbml_model.listOfRules + sbml_model.listOfInitialAssignments
		listOfVariables = []
		for variable in sbml_model.listOfVariables:
			if ((variable.isParameter() and variable.isGlobal())
				or variable.isSpecies()
				or variable.isCompartment()
				or variable.isStoichiometry()):
				listOfVariables.append(variable)

		rule = listOfRules[0]

		response_get_compartment = c.post('/json/get_rule/', {
			'rule_ind': '0',
		})

		self.assertEqual(response_get_compartment.status_code, 200)
		json_response = loads(response_get_compartment.content.decode('utf-8'))


		self.assertEqual(json_response[u'rule_id'], listOfRules.index(rule))
		self.assertEqual(json_response[u'rule_type'], 1)
		self.assertEqual(json_response[u'variable'], listOfVariables.index(sbml_model.listOfVariables.getBySbmlId('total_ras_gtp')))

		response_choose_project = c.post('/models/', {
			'action': 'choose_project',
			'project_id': 0
		})
		self.assertEqual(response_choose_project.status_code, 200)
		self.assertEqual(response_choose_project.context['project_name'], "Project")

		response_choose_model = c.post('/edit/rules/', {
			'action': 'choose_model',
			'model_id': 0
		})
		self.assertEqual(response_choose_model.status_code, 200)
		self.assertEqual(response_choose_model.context['model_name'], "SOS-Ras-MAPK with n17")

		response_save_rule = c.post('/edit/rules/', {
			'action': 'save',
			'rule_id': listOfRules.index(rule),
			'rule_type': 1,
			'variable_id': listOfVariables.index(sbml_model.listOfVariables.getBySbmlId('total_mapk_activated')),
			'rule_expression': "75*ras_gtp",
		})

		self.assertEqual(response_save_rule.status_code, 200)
		self.assertEqual(response_save_rule.context['form'].getErrors(), [])

		sbml_doc = SbmlDocument()
		sbml_doc.readSbmlFromFile(join(settings.MEDIA_ROOT, str(model.sbml_file)))
		sbml_model = sbml_doc.getModelInstance()
		listOfRules = sbml_model.listOfRules + sbml_model.listOfInitialAssignments
		listOfVariables = []
		for variable in sbml_model.listOfVariables:
			if ((variable.isParameter() and variable.isGlobal())
				or variable.isSpecies()
				or variable.isCompartment()
				or variable.isStoichiometry()):
				listOfVariables.append(variable)

		rule = listOfRules[0]


		self.assertEqual(rule.getType(), 1)
		self.assertEqual(rule.getVariable(), sbml_model.listOfVariables.getBySbmlId('total_mapk_activated'))
		formula = MathFormula(sbml_model)
		formula.setPrettyPrintMathFormula("75*ras_gtp", rawFormula=True)
		self.assertEqual(simplify(rule.getDefinition().getDeveloppedInternalMathFormula()-formula.getDeveloppedInternalMathFormula()), 0)

		response_delete_rule = c.post('/edit/rules/', {
			'action': 'delete',
			'rule_id': 0
		})
		self.assertEqual(response_delete_rule.status_code, 200)
		self.assertEqual(response_delete_rule.context['form'].getErrors(), [])
Esempio n. 3
0
	def testCompartment(self):

		user = User.objects.filter(username='******')[0]
		self.assertEqual(len(Project.objects.filter(user=user)), 1)
		project = Project.objects.filter(user=user)[0]
		self.assertEqual(len(SbmlModel.objects.filter(project=project)), 0)

		if isdir(join(settings.MEDIA_ROOT, project.folder)):
			rmtree(join(settings.MEDIA_ROOT, project.folder))
			mkdir(join(settings.MEDIA_ROOT, project.folder))

		c = Client()
		self.assertTrue(c.login(username='******', password='******'))

		response_choose_project = c.get('/project/%s/' % project.folder)
		self.assertRedirects(response_choose_project, '/models/', status_code=302, target_status_code=200)

		files_folder = join(dirname(__file__), "files")
		model_filename = join(files_folder, "modelqlzB7i.xml")

		response_load_model = c.post('/models/', {
			'action': 'load_model',
			'docfile': open(model_filename, 'rb')
		})

		self.assertEqual(response_load_model.status_code, 200)
		self.assertEqual(len(SbmlModel.objects.filter(project=project)), 1)

		model = SbmlModel.objects.filter(project=project)[0]
		sbml_doc = SbmlDocument()
		sbml_doc.readSbmlFromFile(join(settings.MEDIA_ROOT, str(model.sbml_file)))
		sbml_model = sbml_doc.getModelInstance()
		compartment = sbml_model.listOfCompartments.getBySbmlId('cell')

		response_get_compartment = c.post('/json/get_compartment/', {
			'sbml_id': 'cell',
		})

		self.assertEqual(response_get_compartment.status_code, 200)
		json_response = loads(response_get_compartment.content.decode('utf-8'))

		self.assertEqual(json_response[u'id'], sbml_model.listOfCompartments.index(compartment))
		self.assertEqual(json_response[u'sbml_id'], compartment.getSbmlId())
		self.assertEqual(json_response[u'name'], compartment.getName())
		self.assertEqual(json_response[u'value'], compartment.getValue())
		self.assertEqual(json_response[u'constant'], 1 if compartment.constant else 0)

		response_choose_project = c.post('/models/', {
			'action': 'choose_project',
			'project_id': 0
		})
		self.assertEqual(response_choose_project.status_code, 200)
		self.assertEqual(response_choose_project.context['project_name'], "Project")

		response_choose_model = c.post('/edit/compartments/', {
			'action': 'choose_model',
			'model_id': 0
		})
		self.assertEqual(response_choose_model.status_code, 200)
		self.assertEqual(response_choose_model.context['model_name'], "SOS-Ras-MAPK with n17")

		response_save_compartment = c.post('/edit/compartments/', {
			'action': 'save',
			'compartment_id': sbml_model.listOfCompartments.index(compartment),
			'compartment_name': "New name",
			'compartment_sbml_id': "new_name",
			'compartment_size': 75,
			'compartment_unit': 2,
			'compartment_constant': "on",
			'compartment_sboterm': "",
		})

		self.assertEqual(response_save_compartment.status_code, 200)
		self.assertEqual(response_save_compartment.context['getErrors'], [])

		sbml_doc = SbmlDocument()
		sbml_doc.readSbmlFromFile(join(settings.MEDIA_ROOT, str(model.sbml_file)))
		sbml_model = sbml_doc.getModelInstance()
		compartment = sbml_model.listOfCompartments.getBySbmlId('new_name')

		self.assertTrue(compartment is not None)
		self.assertEqual(compartment.getName(), "New name")
		self.assertEqual(compartment.getValue(), 75)
		self.assertEqual(compartment.constant, True)
		self.assertEqual(compartment.getUnits(), sbml_model.listOfUnitDefinitions[2])

		response_delete_compartment = c.post('/edit/compartments/', {
			'action': 'delete',
			'compartment_id': sbml_model.listOfCompartments.index(compartment)
		})
		self.assertEqual(response_delete_compartment.status_code, 200)
		self.assertEqual(response_delete_compartment.context['getErrors'], ['Compartment contains 25 species'])


		response_save_new_compartment = c.post('/edit/compartments/', {
			'action': 'save',
			'compartment_id': "",
			'compartment_name': "New compartment",
			'compartment_sbml_id': "new_compartment",
			'compartment_size': 75,
			'compartment_unit': "",
			'compartment_constant': "on",
			'compartment_sboterm': "",
		})

		self.assertEqual(response_save_new_compartment.status_code, 200)

		sbml_doc = SbmlDocument()
		sbml_doc.readSbmlFromFile(join(settings.MEDIA_ROOT, str(model.sbml_file)))
		sbml_model = sbml_doc.getModelInstance()
		compartment = sbml_model.listOfCompartments.getBySbmlId('new_compartment')

		self.assertTrue(compartment != None)
		self.assertEqual(compartment.getName(), "New compartment")
		self.assertEqual(compartment.getValue(), 75)
		self.assertEqual(compartment.getUnits(), None)
		self.assertEqual(compartment.constant, True)


		response_delete_compartment = c.post('/edit/compartments/', {
			'action': 'delete',
			'compartment_id': sbml_model.listOfCompartments.index(compartment)
		})
		self.assertEqual(response_delete_compartment.status_code, 200)
		self.assertEqual(response_delete_compartment.context['getErrors'], [])
Esempio n. 4
0
	def testEvents(self):

		user = User.objects.filter(username='******')[0]
		self.assertEqual(len(Project.objects.filter(user=user)), 1)
		project = Project.objects.filter(user=user)[0]
		self.assertEqual(len(SbmlModel.objects.filter(project=project)), 0)

		if isdir(join(settings.MEDIA_ROOT, project.folder)):
			rmtree(join(settings.MEDIA_ROOT, project.folder))
			mkdir(join(settings.MEDIA_ROOT, project.folder))

		c = Client()
		self.assertTrue(c.login(username='******', password='******'))

		response_choose_project = c.get('/project/%s/' % project.folder)
		self.assertRedirects(response_choose_project, '/models/', status_code=302, target_status_code=200)

		files_folder = join(dirname(__file__), "files")
		model_filename = join(files_folder, "modelqlzB7i.xml")

		response_load_model = c.post('/models/', {
			'action': 'load_model',
			'docfile': open(model_filename, 'rb')
		})

		self.assertEqual(response_load_model.status_code, 200)
		self.assertEqual(len(SbmlModel.objects.filter(project=project)), 1)

		model = SbmlModel.objects.filter(project=project)[0]
		sbml_doc = SbmlDocument()
		sbml_doc.readSbmlFromFile(join(settings.MEDIA_ROOT, str(model.sbml_file)))
		sbml_model = sbml_doc.getModelInstance()
		listOfVariables = []
		for variable in sbml_model.listOfVariables:
			if ((variable.isParameter() and variable.isGlobal())
				or variable.isSpecies()
				or variable.isCompartment()
				or variable.isStoichiometry()):
				listOfVariables.append(variable)

		response_choose_project = c.post('/models/', {
			'action': 'choose_project',
			'project_id': 0
		})
		self.assertEqual(response_choose_project.status_code, 200)
		self.assertEqual(response_choose_project.context['project_name'], "Project")

		response_choose_model = c.post('/edit/events/', {
			'action': 'choose_model',
			'model_id': 0
		})
		self.assertEqual(response_choose_model.status_code, 200)
		self.assertEqual(response_choose_model.context['model_name'], "SOS-Ras-MAPK with n17")

		response_save_event = c.post('/edit/events/', {
			'action': 'save',
			'event_id': '',
			'event_sbmlid': 'test_event',
			'event_name': "Test event",
			'event_trigger': "time==0",
			'event_priority': "",
			'event_delay': "",
			'event_assignment_0_id': listOfVariables.index(sbml_model.listOfVariables.getBySbmlId('ras_gtp')),
			'event_assignment_0_expression': 'ras_gtp*2'
		})

		self.assertEqual(response_save_event.status_code, 200)
		self.assertEqual(response_save_event.context['form'].getErrors(), [])

		model = SbmlModel.objects.filter(project=project)[0]
		sbml_doc = SbmlDocument()
		sbml_doc.readSbmlFromFile(join(settings.MEDIA_ROOT, str(model.sbml_file)))
		sbml_model = sbml_doc.getModelInstance()
		listOfVariables = []
		for variable in sbml_model.listOfVariables:
			if ((variable.isParameter() and variable.isGlobal())
				or variable.isSpecies()
				or variable.isCompartment()
				or variable.isStoichiometry()):
				listOfVariables.append(variable)

		response_get_event = c.post('/json/get_event/', {
			'event_ind': '0',
		})

		self.assertEqual(response_get_event.status_code, 200)
		json_response = loads(response_get_event.content.decode('utf-8'))

		self.assertEqual(json_response[u'event_ind'], 0)
		self.assertEqual(json_response[u'event_name'], "Test event")

		formula = MathFormula(sbml_model)
		formula.setPrettyPrintMathFormula("time==0", rawFormula=True)

		formula_response = MathFormula(sbml_model)
		formula_response.setPrettyPrintMathFormula(json_response[u'event_trigger'], rawFormula=True)

		self.assertEqual(simplify(formula.getDeveloppedInternalMathFormula()-formula_response.getDeveloppedInternalMathFormula()), 0)
		self.assertEqual(json_response[u'event_delay'], "")
		self.assertEqual(json_response[u'list_of_assignments'][0][0], listOfVariables.index(sbml_model.listOfVariables.getBySbmlId('ras_gtp')))

		formula = MathFormula(sbml_model)
		formula.setPrettyPrintMathFormula("ras_gtp*2", rawFormula=True)

		formula_response = MathFormula(sbml_model)
		formula_response.setPrettyPrintMathFormula(json_response[u'list_of_assignments'][0][2], rawFormula=True)
		self.assertEqual(simplify(formula.getDeveloppedInternalMathFormula()-formula_response.getDeveloppedInternalMathFormula()), 0)
		self.assertTrue(u'event_assignment_variable_1' not in json_response)

		# Modifying an event
		response_save_event = c.post('/edit/events/', {
			'action': 'save',
			'event_id': '0',
			'event_sbmlid': 'test_event',
			'event_name': "Test event",
			'event_trigger': "time==100",
			'event_priority': "",
			'event_delay': "",
			'event_assignment_0_id': listOfVariables.index(sbml_model.listOfVariables.getBySbmlId('ras_gtp')),
			'event_assignment_0_expression': 'ras_gtp*2',
			'event_assignment_1_id': listOfVariables.index(sbml_model.listOfVariables.getBySbmlId('ras_gdp')),
			'event_assignment_1_expression': 'ras_gdp/2',

		})
		self.assertEqual(response_save_event.status_code, 200)
		self.assertEqual(response_save_event.context['form'].getErrors(), [])

		model = SbmlModel.objects.filter(project=project)[0]
		sbml_doc = SbmlDocument()
		sbml_doc.readSbmlFromFile(join(settings.MEDIA_ROOT, str(model.sbml_file)))
		sbml_model = sbml_doc.getModelInstance()
		event = sbml_model.listOfEvents[0]
		listOfVariables = []
		for variable in sbml_model.listOfVariables:
			if ((variable.isParameter() and variable.isGlobal())
				or variable.isSpecies()
				or variable.isCompartment()
				or variable.isStoichiometry()):
				listOfVariables.append(variable)

		self.assertEqual(event.getNameOrSbmlId(), "Test event")

		formula = MathFormula(sbml_model)
		formula.setPrettyPrintMathFormula("time==100", rawFormula=True)

		self.assertEqual(
			simplify(formula.getDeveloppedInternalMathFormula() - event.trigger.getDeveloppedInternalMathFormula()),
			0)

		self.assertEqual(event.priority, None)
		self.assertEqual(event.delay, None)
		self.assertEqual(event.listOfEventAssignments[0].getVariable(), sbml_model.listOfVariables.getBySbmlId('ras_gtp'))

		formula = MathFormula(sbml_model)
		formula.setPrettyPrintMathFormula("ras_gtp*2")

		self.assertEqual(
			simplify(formula.getDeveloppedInternalMathFormula() - event.listOfEventAssignments[0].getDefinition().getDeveloppedInternalMathFormula()),
			0)

		self.assertEqual(event.listOfEventAssignments[1].getVariable(), sbml_model.listOfVariables.getBySbmlId('ras_gdp'))

		formula = MathFormula(sbml_model)
		formula.setPrettyPrintMathFormula("ras_gdp/2")

		self.assertEqual(
			simplify(formula.getDeveloppedInternalMathFormula() - event.listOfEventAssignments[1].getDefinition().getDeveloppedInternalMathFormula()),
			0)

		response_delete_event = c.post('/edit/events/', {
			'action': 'delete',
			'event_id': 0
		})
		self.assertEqual(response_delete_event.status_code, 200)
		self.assertEqual(response_delete_event.context['form'].getErrors(), [])

		model = SbmlModel.objects.filter(project=project)[0]
		sbml_doc = SbmlDocument()
		sbml_doc.readSbmlFromFile(join(settings.MEDIA_ROOT, str(model.sbml_file)))
		sbml_model = sbml_doc.getModelInstance()
		self.assertEqual(len(sbml_model.listOfEvents), 0)
Esempio n. 5
0
	def testReactions(self):

		user = User.objects.filter(username='******')[0]
		self.assertEqual(len(Project.objects.filter(user=user)), 1)
		project = Project.objects.filter(user=user)[0]
		self.assertEqual(len(SbmlModel.objects.filter(project=project)), 0)

		if isdir(join(settings.MEDIA_ROOT, project.folder)):
			rmtree(join(settings.MEDIA_ROOT, project.folder))
			mkdir(join(settings.MEDIA_ROOT, project.folder))

		c = Client()
		self.assertTrue(c.login(username='******', password='******'))

		response_choose_project = c.get('/project/%s/' % project.folder)
		self.assertRedirects(response_choose_project, '/models/', status_code=302, target_status_code=200)

		files_folder = join(dirname(__file__), "files")
		model_filename = join(files_folder, "modelqlzB7i.xml")

		response_load_model = c.post('/models/', {
			'action': 'load_model',
			'docfile': open(model_filename, 'rb')
		})

		self.assertEqual(response_load_model.status_code, 200)
		self.assertEqual(len(SbmlModel.objects.filter(project=project)), 1)

		model = SbmlModel.objects.filter(project=project)[0]
		sbml_doc = SbmlDocument()
		sbml_doc.readSbmlFromFile(join(settings.MEDIA_ROOT, str(model.sbml_file)))
		sbml_model = sbml_doc.getModelInstance()
		reaction = sbml_model.listOfReactions.getBySbmlId('reaction_4')

		response_choose_project = c.post('/models/', {
			'action': 'choose_project',
			'project_id': 0
		})
		self.assertEqual(response_choose_project.status_code, 200)
		self.assertEqual(response_choose_project.context['project_name'], "Project")

		response_choose_model = c.post('/edit/reactions/', {
			'action': 'choose_model',
			'model_id': 0
		})
		self.assertEqual(response_choose_model.status_code, 200)
		self.assertEqual(response_choose_model.context['model_name'], "SOS-Ras-MAPK with n17")
		self.assertEqual(
			[t_reaction.getReactionDescription() for t_reaction in response_choose_model.context['list_of_reactions']],
			[t_reaction.getReactionDescription() for t_reaction in sbml_model.listOfReactions]
		)


		response_get_reaction = c.post('/json/get_reaction/', {
			'sbml_id': 'reaction_4'
		})

		self.assertEqual(response_get_reaction.status_code, 200)
		json_response = loads(response_get_reaction.content.decode('utf-8'))

		self.assertEqual(json_response[u'id'], sbml_model.listOfReactions.index(reaction))
		self.assertEqual(json_response[u'sbml_id'], reaction.getSbmlId())
		self.assertEqual(json_response[u'name'], reaction.getName())
		self.assertEqual(json_response[u'kinetic_law'], reaction.kineticLaw.getPrettyPrintMathFormula())
		self.assertEqual(json_response[u'reaction_type'], reaction.getReactionType())
		self.assertEqual(json_response[u'reaction_type_name'], KineticLaw.reactionTypes[reaction.getReactionType()])
		self.assertEqual(json_response[u'reversible'], reaction.reversible)

		self.assertEqual(json_response[u'list_of_reactants'], [
			[
				sbml_model.listOfSpecies.index(reactant.getSpecies()),
				reactant.stoichiometry.getPrettyPrintMathFormula()
			]
			for reactant in reaction.listOfReactants
		])
		self.assertEqual(json_response[u'list_of_modifiers'], [
			[
				sbml_model.listOfSpecies.index(modifier.getSpecies()),
				modifier.stoichiometry.getPrettyPrintMathFormula()
			]
			for modifier in reaction.listOfModifiers
		])
		self.assertEqual(json_response[u'list_of_products'], [
			[
				sbml_model.listOfSpecies.index(product.getSpecies()),
				product.stoichiometry.getPrettyPrintMathFormula()
			]
			for product in reaction.listOfProducts
		])

		response_delete_reaction = c.post('/edit/reactions/', {
			'action': 'delete',
			'reaction_id': 2
		})

		self.assertEqual(response_delete_reaction.status_code, 200)

		model = SbmlModel.objects.filter(project=project)[0]
		sbml_doc = SbmlDocument()
		sbml_doc.readSbmlFromFile(join(settings.MEDIA_ROOT, str(model.sbml_file)))
		sbml_model = sbml_doc.getModelInstance()
		self.assertEqual(len(sbml_model.listOfReactions), 21)
		self.assertEqual(sbml_model.listOfReactions.getBySbmlId('reaction_4'), None)
		response_create_creation = c.post('/edit/reactions/', {
			'action': 'save',
			'reaction_id': "",
			'reaction_sbml_id': "reaction_4",
			'reaction_name': "Ras activation by SOS-Ras-GDP",
			'reaction_reactant_0': 1,
			'reaction_reactant_0_stoichiometry': "1",
			'reaction_modifier_0': 3,
			'reaction_modifier_0_stoichiometry': "1",
			'reaction_product_0': 2,
			'reaction_product_0_stoichiometry': "1",
			'reaction_type': 1,
			'reaction_parameter_0': 2,
			'reaction_parameter_1': 3,
			'reaction_sboterm': ""
		})

		self.assertEqual(response_create_creation.status_code, 200)
		model = SbmlModel.objects.filter(project=project)[0]
		sbml_doc = SbmlDocument()
		sbml_doc.readSbmlFromFile(join(settings.MEDIA_ROOT, str(model.sbml_file)))
		sbml_model = sbml_doc.getModelInstance()
		reaction = sbml_model.listOfReactions.getBySbmlId('reaction_4')
		self.assertEqual(len(sbml_model.listOfReactions), 22)
		self.assertEqual(reaction.getName(), "Ras activation by SOS-Ras-GDP")

		response_modify_creation = c.post('/edit/reactions/', {
			'action': 'save',
			'reaction_id': sbml_model.listOfReactions.index(reaction),
			'reaction_sbml_id': "reaction_4",
			'reaction_name': "Ras activation by SOS-Ras-GDP, modified",
			'reaction_reactant_0': 1,
			'reaction_reactant_0_stoichiometry': "1",
			'reaction_modifier_0': 3,
			'reaction_modifier_0_stoichiometry': "1",
			'reaction_product_0': 2,
			'reaction_product_0_stoichiometry': "1",
			'reaction_type': 1,
			'reaction_parameter_0': 2,
			'reaction_parameter_1': 3,
			'reaction_sboterm': ""
		})

		self.assertEqual(response_modify_creation.status_code, 200)
		model = SbmlModel.objects.filter(project=project)[0]
		sbml_doc = SbmlDocument()
		sbml_doc.readSbmlFromFile(join(settings.MEDIA_ROOT, str(model.sbml_file)))
		sbml_model = sbml_doc.getModelInstance()
		reaction = sbml_model.listOfReactions.getBySbmlId('reaction_4')
		self.assertEqual(len(sbml_model.listOfReactions), 22)
		self.assertEqual(reaction.getName(), "Ras activation by SOS-Ras-GDP, modified")
Esempio n. 6
0
	def testSpecies(self):

		user = User.objects.filter(username='******')[0]
		self.assertEqual(len(Project.objects.filter(user=user)), 1)
		project = Project.objects.filter(user=user)[0]
		self.assertEqual(len(SbmlModel.objects.filter(project=project)), 0)

		if isdir(join(settings.MEDIA_ROOT, project.folder)):
			rmtree(join(settings.MEDIA_ROOT, project.folder))
			mkdir(join(settings.MEDIA_ROOT, project.folder))

		c = Client()
		self.assertTrue(c.login(username='******', password='******'))

		response_choose_project = c.get('/project/%s/' % project.folder)
		self.assertRedirects(response_choose_project, '/models/', status_code=302, target_status_code=200)

		files_folder = join(dirname(__file__), "files")
		model_filename = join(files_folder, "modelqlzB7i.xml")

		response_load_model = c.post('/models/', {
			'action': 'load_model',
			'docfile': open(model_filename, 'rb')
		})

		self.assertEqual(response_load_model.status_code, 200)
		self.assertEqual(len(SbmlModel.objects.filter(project=project)), 1)

		model = SbmlModel.objects.filter(project=project)[0]
		sbml_doc = SbmlDocument()
		sbml_doc.readSbmlFromFile(join(settings.MEDIA_ROOT, str(model.sbml_file)))
		sbml_model = sbml_doc.getModelInstance()
		species = sbml_model.listOfSpecies.getBySbmlId('ras_gtp')

		response_get_species = c.post('/json/get_species/', {
			'sbml_id': 'ras_gtp'
		})

		self.assertEqual(response_get_species.status_code, 200)
		json_response = loads(response_get_species.content.decode('utf-8'))
		self.assertEqual(json_response[u'id'], sbml_model.listOfSpecies.index(species))
		self.assertEqual(json_response[u'sbml_id'], species.getSbmlId())
		self.assertEqual(json_response[u'name'], species.getName())
		self.assertEqual(json_response[u'compartment_name'], species.getCompartment().getName())
		self.assertEqual(json_response[u'compartment_id'], sbml_model.listOfCompartments.index(species.getCompartment()))
		self.assertEqual(json_response[u'value'], species.getValue())
		self.assertEqual(json_response[u'isConcentration'], 1 if not species.hasOnlySubstanceUnits else 0)
		self.assertEqual(json_response[u'constant'], 1 if species.constant else 0)
		self.assertEqual(json_response[u'boundaryCondition'], 1 if species.boundaryCondition else 0)


		response_choose_project = c.post('/models/', {
			'action': 'choose_project',
			'project_id': 0
		})
		self.assertEqual(response_choose_project.status_code, 200)
		self.assertEqual(response_choose_project.context['project_name'], "Project")

		response_choose_model = c.post('/edit/species/', {
			'action': 'choose_model',
			'model_id': 0
		})
		self.assertEqual(response_choose_model.status_code, 200)
		self.assertEqual(response_choose_model.context['model_name'], "SOS-Ras-MAPK with n17")

		response_save_species = c.post('/edit/species/', {
			'action': 'save',
			'species_id': 2,
			'species_name': "New name",
			'species_sbml_id': "new_name",
			'species_value': 75,
			'species_value_type': 0,
			'species_compartment': 0,
			'species_unit': 2,
			'species_constant': "on",
			'species_boundary': "on",
			'species_sboterm': "",
		})

		self.assertEqual(response_save_species.status_code, 200)

		sbml_doc = SbmlDocument()
		sbml_doc.readSbmlFromFile(join(settings.MEDIA_ROOT, str(model.sbml_file)))
		sbml_model = sbml_doc.getModelInstance()
		species = sbml_model.listOfSpecies.getBySbmlId('new_name')

		self.assertTrue(species is not None)
		self.assertEqual(species.getName(), "New name")
		self.assertEqual(species.getValue(), 75)
		self.assertEqual(species.hasOnlySubstanceUnits, True)
		self.assertEqual(species.constant, True)
		self.assertEqual(species.boundaryCondition, True)

		response_delete_species = c.post('/edit/species/', {
			'action': 'delete',
			'species_id': sbml_model.listOfSpecies.index(species)
		})
		self.assertEqual(response_delete_species.status_code, 200)
		self.assertEqual(response_delete_species.context['getErrors'], ['Species is used in reactions'])

		response_save_new_species = c.post('/edit/species/', {
			'action': 'save',
			'species_id': "",
			'species_name': "New species",
			'species_sbml_id': "new_species",
			'species_value': 2500,
			'species_value_type': 0,
			'species_compartment': 0,
			'species_unit': 2,
			'species_constant': "off",
			'species_boundary': "off",
			'species_sboterm': "",
		})

		self.assertEqual(response_save_new_species.status_code, 200)

		sbml_doc = SbmlDocument()
		sbml_doc.readSbmlFromFile(join(settings.MEDIA_ROOT, str(model.sbml_file)))
		sbml_model = sbml_doc.getModelInstance()
		species = sbml_model.listOfSpecies.getBySbmlId('new_species')

		self.assertTrue(species != None)
		self.assertEqual(species.getName(), "New species")
		self.assertEqual(species.getValue(), 2500)
		self.assertEqual(species.isConcentration(), False)
		self.assertEqual(species.getCompartment().getName(), "cell")
		self.assertEqual(species.constant, False)
		self.assertEqual(species.boundaryCondition, False)

		response_delete_species = c.post('/edit/species/', {
			'action': 'delete',
			'species_id': sbml_model.listOfSpecies.index(species)
		})
		self.assertEqual(response_delete_species.status_code, 200)

		sbml_doc = SbmlDocument()
		sbml_doc.readSbmlFromFile(join(settings.MEDIA_ROOT, str(model.sbml_file)))
		sbml_model = sbml_doc.getModelInstance()

		species = sbml_model.listOfSpecies.getBySbmlId('new_species')

		self.assertEqual(species, None)
Esempio n. 7
0
    def testSpecies(self):

        user = User.objects.filter(username='******')[0]
        self.assertEqual(len(Project.objects.filter(user=user)), 1)
        project = Project.objects.filter(user=user)[0]
        self.assertEqual(len(SbmlModel.objects.filter(project=project)), 0)

        if isdir(join(settings.MEDIA_ROOT, project.folder)):
            rmtree(join(settings.MEDIA_ROOT, project.folder))
            mkdir(join(settings.MEDIA_ROOT, project.folder))

        c = Client()
        self.assertTrue(c.login(username='******', password='******'))

        response_choose_project = c.get('/project/%s/' % project.folder)
        self.assertRedirects(response_choose_project,
                             '/models/',
                             status_code=302,
                             target_status_code=200)

        files_folder = join(dirname(__file__), "files")
        model_filename = join(files_folder, "modelqlzB7i.xml")

        response_load_model = c.post('/models/', {
            'action': 'load_model',
            'docfile': open(model_filename, 'rb')
        })

        self.assertEqual(response_load_model.status_code, 200)
        self.assertEqual(len(SbmlModel.objects.filter(project=project)), 1)

        model = SbmlModel.objects.filter(project=project)[0]
        sbml_doc = SbmlDocument()
        sbml_doc.readSbmlFromFile(
            join(settings.MEDIA_ROOT, str(model.sbml_file)))
        sbml_model = sbml_doc.getModelInstance()
        species = sbml_model.listOfSpecies.getBySbmlId('ras_gtp')

        response_get_species = c.post('/json/get_species/',
                                      {'sbml_id': 'ras_gtp'})

        self.assertEqual(response_get_species.status_code, 200)
        json_response = loads(response_get_species.content.decode('utf-8'))
        self.assertEqual(json_response[u'id'],
                         sbml_model.listOfSpecies.index(species))
        self.assertEqual(json_response[u'sbml_id'], species.getSbmlId())
        self.assertEqual(json_response[u'name'], species.getName())
        self.assertEqual(json_response[u'compartment_name'],
                         species.getCompartment().getName())
        self.assertEqual(
            json_response[u'compartment_id'],
            sbml_model.listOfCompartments.index(species.getCompartment()))
        self.assertEqual(json_response[u'value'], species.getValue())
        self.assertEqual(json_response[u'isConcentration'],
                         1 if not species.hasOnlySubstanceUnits else 0)
        self.assertEqual(json_response[u'constant'],
                         1 if species.constant else 0)
        self.assertEqual(json_response[u'boundaryCondition'],
                         1 if species.boundaryCondition else 0)

        response_choose_project = c.post('/models/', {
            'action': 'choose_project',
            'project_id': 0
        })
        self.assertEqual(response_choose_project.status_code, 200)
        self.assertEqual(response_choose_project.context['project_name'],
                         "Project")

        response_choose_model = c.post('/edit/species/', {
            'action': 'choose_model',
            'model_id': 0
        })
        self.assertEqual(response_choose_model.status_code, 200)
        self.assertEqual(response_choose_model.context['model_name'],
                         "SOS-Ras-MAPK with n17")

        response_save_species = c.post(
            '/edit/species/', {
                'action': 'save',
                'species_id': 2,
                'species_name': "New name",
                'species_sbml_id': "new_name",
                'species_value': 75,
                'species_value_type': 0,
                'species_compartment': 0,
                'species_unit': 2,
                'species_constant': "on",
                'species_boundary': "on",
                'species_sboterm': "",
            })

        self.assertEqual(response_save_species.status_code, 200)

        sbml_doc = SbmlDocument()
        sbml_doc.readSbmlFromFile(
            join(settings.MEDIA_ROOT, str(model.sbml_file)))
        sbml_model = sbml_doc.getModelInstance()
        species = sbml_model.listOfSpecies.getBySbmlId('new_name')

        self.assertTrue(species is not None)
        self.assertEqual(species.getName(), "New name")
        self.assertEqual(species.getValue(), 75)
        self.assertEqual(species.hasOnlySubstanceUnits, True)
        self.assertEqual(species.constant, True)
        self.assertEqual(species.boundaryCondition, True)

        response_delete_species = c.post(
            '/edit/species/', {
                'action': 'delete',
                'species_id': sbml_model.listOfSpecies.index(species)
            })
        self.assertEqual(response_delete_species.status_code, 200)
        self.assertEqual(response_delete_species.context['getErrors'],
                         ['Species is used in reactions'])

        response_save_new_species = c.post(
            '/edit/species/', {
                'action': 'save',
                'species_id': "",
                'species_name': "New species",
                'species_sbml_id': "new_species",
                'species_value': 2500,
                'species_value_type': 0,
                'species_compartment': 0,
                'species_unit': 2,
                'species_constant': "off",
                'species_boundary': "off",
                'species_sboterm': "",
            })

        self.assertEqual(response_save_new_species.status_code, 200)

        sbml_doc = SbmlDocument()
        sbml_doc.readSbmlFromFile(
            join(settings.MEDIA_ROOT, str(model.sbml_file)))
        sbml_model = sbml_doc.getModelInstance()
        species = sbml_model.listOfSpecies.getBySbmlId('new_species')

        self.assertTrue(species != None)
        self.assertEqual(species.getName(), "New species")
        self.assertEqual(species.getValue(), 2500)
        self.assertEqual(species.isConcentration(), False)
        self.assertEqual(species.getCompartment().getName(), "cell")
        self.assertEqual(species.constant, False)
        self.assertEqual(species.boundaryCondition, False)

        response_delete_species = c.post(
            '/edit/species/', {
                'action': 'delete',
                'species_id': sbml_model.listOfSpecies.index(species)
            })
        self.assertEqual(response_delete_species.status_code, 200)

        sbml_doc = SbmlDocument()
        sbml_doc.readSbmlFromFile(
            join(settings.MEDIA_ROOT, str(model.sbml_file)))
        sbml_model = sbml_doc.getModelInstance()

        species = sbml_model.listOfSpecies.getBySbmlId('new_species')

        self.assertEqual(species, None)
Esempio n. 8
0
	def testLocalParameter(self):

		user = User.objects.filter(username='******')[0]
		self.assertEqual(len(Project.objects.filter(user=user)), 1)
		project = Project.objects.filter(user=user)[0]
		self.assertEqual(len(SbmlModel.objects.filter(project=project)), 0)

		c = Client()
		self.assertTrue(c.login(username='******', password='******'))

		response_choose_project = c.get('/project/%s/' % project.folder)
		self.assertRedirects(response_choose_project, '/models/', status_code=302, target_status_code=200)

		files_folder = join(dirname(__file__), "files")
		model_filename = join(files_folder, "modelqlzB7i.xml")

		response_load_model = c.post('/models/', {
			'action': 'load_model',
			'docfile': open(model_filename, 'rb')
		})

		self.assertEqual(response_load_model.status_code, 200)
		self.assertEqual(len(SbmlModel.objects.filter(project=project)), 1)

		model = SbmlModel.objects.filter(project=project)[0]
		sbml_doc = SbmlDocument()
		sbml_doc.readSbmlFromFile(join(settings.MEDIA_ROOT, str(model.sbml_file)))
		sbml_model = sbml_doc.getModelInstance()
		parameter = sbml_model.listOfParameters.getBySbmlId('sos_ras_gdp_comp')

		response_get_reaction = c.post('/json/get_reaction/', {
			'sbml_id': 'reaction_2',
		})

		self.assertEqual(response_get_reaction.status_code, 200)
		json_response = loads(response_get_reaction.content.decode('utf-8'))
		self.assertEqual(json_response[u'kinetic_law'], '-1 * sos_ras_gdp * sos_ras_gdp_decomp + ras_gdp * sos * sos_ras_gdp_comp')

		response_get_parameter = c.post('/json/get_parameter/', {
			'sbml_id': 'sos_ras_gdp_comp',
			'reaction': ''
		})

		self.assertEqual(response_get_parameter.status_code, 200)
		json_response = loads(response_get_parameter.content.decode('utf-8'))
		self.assertEqual(json_response[u'id'], sbml_model.listOfParameters.index(parameter))
		self.assertEqual(json_response[u'reaction_id'], "")

		response_to_local_parameter = c.post('/edit/parameters/', {
			'action': 'save',
			'parameter_id': json_response[u'id'],
			'parameter_name': json_response[u'name'],
			'parameter_sbml_id': json_response[u'sbml_id'],
			'parameter_value': json_response[u'value'],
			'parameter_unit': json_response[u'unit_id'],
			'parameter_constant': "on" if json_response[u'constant'] == 1 else "",
			'parameter_scope': 1,
			'parameter_sboterm': "",
		})

		self.assertEqual(response_to_local_parameter.status_code, 200)
		self.assertEqual(response_to_local_parameter.context['getErrors'], [])
		self.assertEqual(response_to_local_parameter.context['form'].getErrors(), [])

		response_get_reaction = c.post('/json/get_reaction/', {
			'sbml_id': 'reaction_2',
		})

		self.assertEqual(response_get_reaction.status_code, 200)
		json_response = loads(response_get_reaction.content.decode('utf-8'))
		self.assertEqual(
			json_response[u'kinetic_law'],
			'-1 * sos_ras_gdp * sos_ras_gdp_decomp + sos_ras_gdp_comp * ras_gdp * sos'
		)

		sbml_doc = SbmlDocument()
		sbml_doc.readSbmlFromFile(join(settings.MEDIA_ROOT, str(model.sbml_file)))
		sbml_model = sbml_doc.getModelInstance()
		reaction = sbml_model.listOfReactions.getBySbmlId('reaction_2')
		self.assertEqual(
			str(reaction.kineticLaw.getDefinition().getInternalMathFormula()),
			"_local_0_sos_ras_gdp_comp*ras_gdp*sos - sos_ras_gdp*sos_ras_gdp_decomp"
		)

		self.assertEqual(sbml_model.listOfParameters.getBySbmlId("sos_ras_gdp_comp"), None)

		response_get_parameter = c.post('/json/get_parameter/', {
			'sbml_id': 'sos_ras_gdp_comp',
			'reaction': '1'
		})

		self.assertEqual(response_get_parameter.status_code, 200)
		json_response = loads(response_get_parameter.content.decode('utf-8'))

		response_list_parameters = c.get('/edit/parameters/')
		self.assertEqual(response_list_parameters.status_code, 200)
		list_of_parameters = [param.getSbmlId() for param in response_list_parameters.context['list_of_parameters']]



		response_to_global_parameter = c.post('/edit/parameters/', {
			'action': 'save',
			'parameter_id': list_of_parameters.index("sos_ras_gdp_comp"),
			'parameter_name': json_response[u'name'],
			'parameter_sbml_id': json_response[u'sbml_id'],
			'parameter_value': json_response[u'value'],
			'parameter_unit': json_response[u'unit_id'],
			'parameter_constant': "on" if json_response[u'constant'] == 1 else "",
			'parameter_scope': 0,
			'parameter_sboterm': "",
		})

		self.assertEqual(response_to_global_parameter.status_code, 200)
		self.assertEqual(response_to_global_parameter.context['getErrors'], [])
		self.assertEqual(response_to_global_parameter.context['form'].getErrors(), [])

		response_get_reaction = c.post('/json/get_reaction/', {
			'sbml_id': 'reaction_2',
		})

		self.assertEqual(response_get_reaction.status_code, 200)
		json_response = loads(response_get_reaction.content.decode('utf-8'))
		# print json_response
		self.assertEqual(
			json_response[u'kinetic_law'],
			'-1 * sos_ras_gdp * sos_ras_gdp_decomp + ras_gdp * sos * sos_ras_gdp_comp'
		)

		sbml_doc = SbmlDocument()
		sbml_doc.readSbmlFromFile(join(settings.MEDIA_ROOT, str(model.sbml_file)))
		sbml_model = sbml_doc.getModelInstance()
		reaction = sbml_model.listOfReactions.getBySbmlId('reaction_2')
		self.assertEqual(
			str(reaction.kineticLaw.getDefinition().getInternalMathFormula()),
			"ras_gdp*sos*sos_ras_gdp_comp - sos_ras_gdp*sos_ras_gdp_decomp"
		)

		self.assertEqual(sbml_model.listOfParameters.getBySbmlId("sos_ras_gdp_comp").getSbmlId(), "sos_ras_gdp_comp")
Esempio n. 9
0
    def testEvents(self):

        user = User.objects.filter(username='******')[0]
        self.assertEqual(len(Project.objects.filter(user=user)), 1)
        project = Project.objects.filter(user=user)[0]
        self.assertEqual(len(SbmlModel.objects.filter(project=project)), 0)

        if isdir(join(settings.MEDIA_ROOT, project.folder)):
            rmtree(join(settings.MEDIA_ROOT, project.folder))
            mkdir(join(settings.MEDIA_ROOT, project.folder))

        c = Client()
        self.assertTrue(c.login(username='******', password='******'))

        response_choose_project = c.get('/project/%s/' % project.folder)
        self.assertRedirects(response_choose_project,
                             '/models/',
                             status_code=302,
                             target_status_code=200)

        files_folder = join(dirname(__file__), "files")
        model_filename = join(files_folder, "modelqlzB7i.xml")

        response_load_model = c.post('/models/', {
            'action': 'load_model',
            'docfile': open(model_filename, 'rb')
        })

        self.assertEqual(response_load_model.status_code, 200)
        self.assertEqual(len(SbmlModel.objects.filter(project=project)), 1)

        model = SbmlModel.objects.filter(project=project)[0]
        sbml_doc = SbmlDocument()
        sbml_doc.readSbmlFromFile(
            join(settings.MEDIA_ROOT, str(model.sbml_file)))
        sbml_model = sbml_doc.getModelInstance()
        listOfVariables = []
        for variable in sbml_model.listOfVariables:
            if ((variable.isParameter() and variable.isGlobal())
                    or variable.isSpecies() or variable.isCompartment()
                    or variable.isStoichiometry()):
                listOfVariables.append(variable)

        response_choose_project = c.post('/models/', {
            'action': 'choose_project',
            'project_id': 0
        })
        self.assertEqual(response_choose_project.status_code, 200)
        self.assertEqual(response_choose_project.context['project_name'],
                         "Project")

        response_choose_model = c.post('/edit/events/', {
            'action': 'choose_model',
            'model_id': 0
        })
        self.assertEqual(response_choose_model.status_code, 200)
        self.assertEqual(response_choose_model.context['model_name'],
                         "SOS-Ras-MAPK with n17")

        response_save_event = c.post(
            '/edit/events/', {
                'action':
                'save',
                'event_id':
                '',
                'event_sbmlid':
                'test_event',
                'event_name':
                "Test event",
                'event_trigger':
                "time==0",
                'event_priority':
                "",
                'event_delay':
                "",
                'event_assignment_0_id':
                listOfVariables.index(
                    sbml_model.listOfVariables.getBySbmlId('ras_gtp')),
                'event_assignment_0_expression':
                'ras_gtp*2'
            })

        self.assertEqual(response_save_event.status_code, 200)
        self.assertEqual(response_save_event.context['form'].getErrors(), [])

        model = SbmlModel.objects.filter(project=project)[0]
        sbml_doc = SbmlDocument()
        sbml_doc.readSbmlFromFile(
            join(settings.MEDIA_ROOT, str(model.sbml_file)))
        sbml_model = sbml_doc.getModelInstance()
        listOfVariables = []
        for variable in sbml_model.listOfVariables:
            if ((variable.isParameter() and variable.isGlobal())
                    or variable.isSpecies() or variable.isCompartment()
                    or variable.isStoichiometry()):
                listOfVariables.append(variable)

        response_get_event = c.post('/json/get_event/', {
            'event_ind': '0',
        })

        self.assertEqual(response_get_event.status_code, 200)
        json_response = loads(response_get_event.content.decode('utf-8'))

        self.assertEqual(json_response[u'event_ind'], 0)
        self.assertEqual(json_response[u'event_name'], "Test event")

        formula = MathFormula(sbml_model)
        formula.setPrettyPrintMathFormula("time==0", rawFormula=True)

        formula_response = MathFormula(sbml_model)
        formula_response.setPrettyPrintMathFormula(
            json_response[u'event_trigger'], rawFormula=True)

        self.assertEqual(
            simplify(formula.getDeveloppedInternalMathFormula() -
                     formula_response.getDeveloppedInternalMathFormula()), 0)
        self.assertEqual(json_response[u'event_delay'], "")
        self.assertEqual(
            json_response[u'list_of_assignments'][0][0],
            listOfVariables.index(
                sbml_model.listOfVariables.getBySbmlId('ras_gtp')))

        formula = MathFormula(sbml_model)
        formula.setPrettyPrintMathFormula("ras_gtp*2", rawFormula=True)

        formula_response = MathFormula(sbml_model)
        formula_response.setPrettyPrintMathFormula(
            json_response[u'list_of_assignments'][0][2], rawFormula=True)
        self.assertEqual(
            simplify(formula.getDeveloppedInternalMathFormula() -
                     formula_response.getDeveloppedInternalMathFormula()), 0)
        self.assertTrue(u'event_assignment_variable_1' not in json_response)

        # Modifying an event
        response_save_event = c.post(
            '/edit/events/', {
                'action':
                'save',
                'event_id':
                '0',
                'event_sbmlid':
                'test_event',
                'event_name':
                "Test event",
                'event_trigger':
                "time==100",
                'event_priority':
                "",
                'event_delay':
                "",
                'event_assignment_0_id':
                listOfVariables.index(
                    sbml_model.listOfVariables.getBySbmlId('ras_gtp')),
                'event_assignment_0_expression':
                'ras_gtp*2',
                'event_assignment_1_id':
                listOfVariables.index(
                    sbml_model.listOfVariables.getBySbmlId('ras_gdp')),
                'event_assignment_1_expression':
                'ras_gdp/2',
            })
        self.assertEqual(response_save_event.status_code, 200)
        self.assertEqual(response_save_event.context['form'].getErrors(), [])

        model = SbmlModel.objects.filter(project=project)[0]
        sbml_doc = SbmlDocument()
        sbml_doc.readSbmlFromFile(
            join(settings.MEDIA_ROOT, str(model.sbml_file)))
        sbml_model = sbml_doc.getModelInstance()
        event = sbml_model.listOfEvents[0]
        listOfVariables = []
        for variable in sbml_model.listOfVariables:
            if ((variable.isParameter() and variable.isGlobal())
                    or variable.isSpecies() or variable.isCompartment()
                    or variable.isStoichiometry()):
                listOfVariables.append(variable)

        self.assertEqual(event.getNameOrSbmlId(), "Test event")

        formula = MathFormula(sbml_model)
        formula.setPrettyPrintMathFormula("time==100", rawFormula=True)

        self.assertEqual(
            simplify(formula.getDeveloppedInternalMathFormula() -
                     event.trigger.getDeveloppedInternalMathFormula()), 0)

        self.assertEqual(event.priority, None)
        self.assertEqual(event.delay, None)
        self.assertEqual(event.listOfEventAssignments[0].getVariable(),
                         sbml_model.listOfVariables.getBySbmlId('ras_gtp'))

        formula = MathFormula(sbml_model)
        formula.setPrettyPrintMathFormula("ras_gtp*2")

        self.assertEqual(
            simplify(formula.getDeveloppedInternalMathFormula() -
                     event.listOfEventAssignments[0].getDefinition(
                     ).getDeveloppedInternalMathFormula()), 0)

        self.assertEqual(event.listOfEventAssignments[1].getVariable(),
                         sbml_model.listOfVariables.getBySbmlId('ras_gdp'))

        formula = MathFormula(sbml_model)
        formula.setPrettyPrintMathFormula("ras_gdp/2")

        self.assertEqual(
            simplify(formula.getDeveloppedInternalMathFormula() -
                     event.listOfEventAssignments[1].getDefinition(
                     ).getDeveloppedInternalMathFormula()), 0)

        response_delete_event = c.post('/edit/events/', {
            'action': 'delete',
            'event_id': 0
        })
        self.assertEqual(response_delete_event.status_code, 200)
        self.assertEqual(response_delete_event.context['form'].getErrors(), [])

        model = SbmlModel.objects.filter(project=project)[0]
        sbml_doc = SbmlDocument()
        sbml_doc.readSbmlFromFile(
            join(settings.MEDIA_ROOT, str(model.sbml_file)))
        sbml_model = sbml_doc.getModelInstance()
        self.assertEqual(len(sbml_model.listOfEvents), 0)
Esempio n. 10
0
    def testOptimization(self):

        user = User.objects.filter(username='******')[0]
        self.assertEqual(len(Project.objects.filter(user=user)), 1)
        project = Project.objects.filter(user=user)[0]

        self.assertEqual(len(SbmlModel.objects.filter(project=project)), 0)

        if isdir(join(settings.MEDIA_ROOT, project.folder)):
            rmtree(join(settings.MEDIA_ROOT, project.folder))
            mkdir(join(settings.MEDIA_ROOT, project.folder))

        c = Client()
        self.assertTrue(c.login(username='******', password='******'))

        response_choose_project = c.get('/project/%s/' % project.folder)
        self.assertRedirects(response_choose_project,
                             '/models/',
                             status_code=302,
                             target_status_code=200)

        response_choose_project = c.post('/models/', {
            'action': 'choose_project',
            'project_id': 0
        })
        self.assertEqual(response_choose_project.status_code, 200)

        files_folder = join(dirname(__file__), "files")
        comp_files_folder = join(files_folder, "comp_model")

        model_filename = join(comp_files_folder, "modelcEvRcX.xml")
        response_load_submodel_1 = c.post('/models/', {
            'action': 'load_model',
            'docfile': open(model_filename, 'rb')
        })

        self.assertEqual(response_load_submodel_1.status_code, 200)
        self.assertEqual(len(SbmlModel.objects.filter(project=project)), 1)

        model_filename = join(comp_files_folder, "modelEHfev9.xml")
        response_load_submodel_2 = c.post('/models/', {
            'action': 'load_model',
            'docfile': open(model_filename, 'rb')
        })

        self.assertEqual(response_load_submodel_2.status_code, 200)
        self.assertEqual(len(SbmlModel.objects.filter(project=project)), 2)

        model_filename = join(comp_files_folder, "modelI1vrys.xml")
        response_load_submodel_3 = c.post('/models/', {
            'action': 'load_model',
            'docfile': open(model_filename, 'rb')
        })

        self.assertEqual(response_load_submodel_3.status_code, 200)
        self.assertEqual(len(SbmlModel.objects.filter(project=project)), 3)

        model_filename = join(comp_files_folder, "modelz9xdww.xml")

        response_load_model = c.post('/models/', {
            'action': 'load_model',
            'docfile': open(model_filename, 'rb')
        })

        self.assertEqual(response_load_model.status_code, 200)
        self.assertEqual(len(SbmlModel.objects.filter(project=project)), 4)

        response_choose_model = c.post('/fit/data/', {
            'action': 'choose_model',
            'model_id': 3
        })
        self.assertEqual(response_choose_model.status_code, 200)
        experiment_filename = join(files_folder, "ras_data.xml")

        response_import_data = c.post(
            '/data/', {
                'action': 'import',
                'docfile': open(experiment_filename, 'rb')
            })

        self.assertEqual(response_import_data.status_code, 200)
        self.assertEqual([
            experiment.name
            for experiment in response_import_data.context['experimental_data']
        ], [u'Ras-GTP quantifications'])
        experiment_id = response_import_data.context['experimental_data'][0].id

        response_get_fit_data = c.get('/fit/data/')
        self.assertEqual(response_get_fit_data.status_code, 200)
        self.assertEqual([
            dataset for dataset in
            response_get_fit_data.context['experimental_data_sets']
        ], [u'Ras-GTP quantifications'])

        response_list_optimizations = c.get('/fit/list/')
        self.assertEqual(response_list_optimizations.status_code, 200)
        self.assertEqual(
            len(response_list_optimizations.context['optimizations']), 0)

        response_add_dataset = c.post('/json/add_dataset/', {'dataset_ind': 0})

        self.assertEqual(response_add_dataset.status_code, 200)
        mapping = loads(
            response_add_dataset.content.decode('utf-8'))['model_xpaths']

        sbml_filename = str(
            SbmlModel.objects.filter(project=project)[3].sbml_file)

        doc = SbmlDocument()
        doc.readSbmlFromFile(join(settings.MEDIA_ROOT, sbml_filename))

        self.assertEqual(sorted(list(mapping.keys())),
                         ['FGF2', 'Total Ras-GTP'])

        self.assertEqual(
            doc.getModelInstance().listOfSpecies.index(
                doc.getByXPath(mapping['FGF2'], instance=True)), 4)
        self.assertEqual(
            doc.getModelInstance().listOfSpecies.index(
                doc.getByXPath(mapping['Total Ras-GTP'], instance=True)), 13)

        response_create_optimization = c.post(
            '/fit/data/',
            {
                'action': 'create',
                'dataset_0':
                experiment_id,  #response_get_fit_data.context['experimental_data_sets'][0].id,
                'list_dataset_0_data_species_0_value': "FGF2",
                'list_dataset_0_species_0_value': 4,
                'list_dataset_0_data_species_1_value': "Total Ras-GTP",
                'list_dataset_0_species_1_value': 13,
                'parameter_0_id': 1,
                'parameter_0_name': "SOS activation by FGF2",
                'parameter_0_value': 1.0,
                'parameter_0_min': 1e-4,
                'parameter_0_max': 1e+4,
                'parameter_0_precision': 7,
                'parameter_1_active': "on",
                'parameter_1_id': 1,
                'parameter_1_name':
                "SOS inactivation by Mapk catalytic constant",
                'parameter_1_value': 1.0,
                'parameter_1_min': 1e-6,
                'parameter_1_max': 1e+6,
                'parameter_1_precision': 7,
                'nb_cores': 2,
                'lambda': 0.02,
                'score_precision': 0.001,
                'param_precision': 7,
                'initial_temperature': 1,
                'initial_moves': 200,
                'freeze_count': 1,
                'negative_penalty': 0
            })

        self.assertEqual(response_create_optimization.status_code, 200)
        self.assertEqual(
            response_create_optimization.context['form'].getErrors(), [])
        sleep(10)

        response_list_optimizations = c.get('/fit/list/')
        self.assertEqual(response_list_optimizations.status_code, 200)
        self.assertEqual(
            len(response_list_optimizations.context['optimizations']), 1)
        self.assertEqual(
            response_list_optimizations.context['optimizations'][0][0].status,
            "Running")

        sleep(10)

        optimization_id = response_list_optimizations.context['optimizations'][
            0][0].optimization_id
        response_get_optimization = c.get('/fit/%s/' % optimization_id)
        self.assertEqual(response_get_optimization.status_code, 200)

        max_time = 360
        sleep(360)
        response_list_optimizations = c.get('/fit/list/')
        self.assertEqual(response_list_optimizations.status_code, 200)
        self.assertEqual(
            response_list_optimizations.context['optimizations'][0][0].status,
            "Finished")

        response_get_optimization = c.get('/fit/%s/' % optimization_id)
        self.assertEqual(response_get_optimization.status_code, 200)

        scores = response_get_optimization.context['score_values']
        self.assertTrue(scores[len(scores) - 1] < 1.01)
Esempio n. 11
0
    def testCompartment(self):

        user = User.objects.filter(username='******')[0]
        self.assertEqual(len(Project.objects.filter(user=user)), 1)
        project = Project.objects.filter(user=user)[0]
        self.assertEqual(len(SbmlModel.objects.filter(project=project)), 0)

        if isdir(join(settings.MEDIA_ROOT, project.folder)):
            rmtree(join(settings.MEDIA_ROOT, project.folder))
            mkdir(join(settings.MEDIA_ROOT, project.folder))

        c = Client()
        self.assertTrue(c.login(username='******', password='******'))

        response_choose_project = c.get('/project/%s/' % project.folder)
        self.assertRedirects(response_choose_project,
                             '/models/',
                             status_code=302,
                             target_status_code=200)

        files_folder = join(dirname(__file__), "files")
        model_filename = join(files_folder, "modelqlzB7i.xml")

        response_load_model = c.post('/models/', {
            'action': 'load_model',
            'docfile': open(model_filename, 'rb')
        })

        self.assertEqual(response_load_model.status_code, 200)
        self.assertEqual(len(SbmlModel.objects.filter(project=project)), 1)

        model = SbmlModel.objects.filter(project=project)[0]
        sbml_doc = SbmlDocument()
        sbml_doc.readSbmlFromFile(
            join(settings.MEDIA_ROOT, str(model.sbml_file)))
        sbml_model = sbml_doc.getModelInstance()
        compartment = sbml_model.listOfCompartments.getBySbmlId('cell')

        response_get_compartment = c.post('/json/get_compartment/', {
            'sbml_id': 'cell',
        })

        self.assertEqual(response_get_compartment.status_code, 200)
        json_response = loads(response_get_compartment.content.decode('utf-8'))

        self.assertEqual(json_response[u'id'],
                         sbml_model.listOfCompartments.index(compartment))
        self.assertEqual(json_response[u'sbml_id'], compartment.getSbmlId())
        self.assertEqual(json_response[u'name'], compartment.getName())
        self.assertEqual(json_response[u'value'], compartment.getValue())
        self.assertEqual(json_response[u'constant'],
                         1 if compartment.constant else 0)

        response_choose_project = c.post('/models/', {
            'action': 'choose_project',
            'project_id': 0
        })
        self.assertEqual(response_choose_project.status_code, 200)
        self.assertEqual(response_choose_project.context['project_name'],
                         "Project")

        response_choose_model = c.post('/edit/compartments/', {
            'action': 'choose_model',
            'model_id': 0
        })
        self.assertEqual(response_choose_model.status_code, 200)
        self.assertEqual(response_choose_model.context['model_name'],
                         "SOS-Ras-MAPK with n17")

        response_save_compartment = c.post(
            '/edit/compartments/', {
                'action': 'save',
                'compartment_id':
                sbml_model.listOfCompartments.index(compartment),
                'compartment_name': "New name",
                'compartment_sbml_id': "new_name",
                'compartment_size': 75,
                'compartment_unit': 2,
                'compartment_constant': "on",
                'compartment_sboterm': "",
            })

        self.assertEqual(response_save_compartment.status_code, 200)
        self.assertEqual(response_save_compartment.context['getErrors'], [])

        sbml_doc = SbmlDocument()
        sbml_doc.readSbmlFromFile(
            join(settings.MEDIA_ROOT, str(model.sbml_file)))
        sbml_model = sbml_doc.getModelInstance()
        compartment = sbml_model.listOfCompartments.getBySbmlId('new_name')

        self.assertTrue(compartment is not None)
        self.assertEqual(compartment.getName(), "New name")
        self.assertEqual(compartment.getValue(), 75)
        self.assertEqual(compartment.constant, True)
        self.assertEqual(compartment.getUnits(),
                         sbml_model.listOfUnitDefinitions[2])

        response_delete_compartment = c.post(
            '/edit/compartments/', {
                'action': 'delete',
                'compartment_id':
                sbml_model.listOfCompartments.index(compartment)
            })
        self.assertEqual(response_delete_compartment.status_code, 200)
        self.assertEqual(response_delete_compartment.context['getErrors'],
                         ['Compartment contains 25 species'])

        response_save_new_compartment = c.post(
            '/edit/compartments/', {
                'action': 'save',
                'compartment_id': "",
                'compartment_name': "New compartment",
                'compartment_sbml_id': "new_compartment",
                'compartment_size': 75,
                'compartment_unit': "",
                'compartment_constant': "on",
                'compartment_sboterm': "",
            })

        self.assertEqual(response_save_new_compartment.status_code, 200)

        sbml_doc = SbmlDocument()
        sbml_doc.readSbmlFromFile(
            join(settings.MEDIA_ROOT, str(model.sbml_file)))
        sbml_model = sbml_doc.getModelInstance()
        compartment = sbml_model.listOfCompartments.getBySbmlId(
            'new_compartment')

        self.assertTrue(compartment != None)
        self.assertEqual(compartment.getName(), "New compartment")
        self.assertEqual(compartment.getValue(), 75)
        self.assertEqual(compartment.getUnits(), None)
        self.assertEqual(compartment.constant, True)

        response_delete_compartment = c.post(
            '/edit/compartments/', {
                'action': 'delete',
                'compartment_id':
                sbml_model.listOfCompartments.index(compartment)
            })
        self.assertEqual(response_delete_compartment.status_code, 200)
        self.assertEqual(response_delete_compartment.context['getErrors'], [])
Esempio n. 12
0
    def testReactions(self):

        user = User.objects.filter(username='******')[0]
        self.assertEqual(len(Project.objects.filter(user=user)), 1)
        project = Project.objects.filter(user=user)[0]
        self.assertEqual(len(SbmlModel.objects.filter(project=project)), 0)

        if isdir(join(settings.MEDIA_ROOT, project.folder)):
            rmtree(join(settings.MEDIA_ROOT, project.folder))
            mkdir(join(settings.MEDIA_ROOT, project.folder))

        c = Client()
        self.assertTrue(c.login(username='******', password='******'))

        response_choose_project = c.get('/project/%s/' % project.folder)
        self.assertRedirects(response_choose_project,
                             '/models/',
                             status_code=302,
                             target_status_code=200)

        files_folder = join(dirname(__file__), "files")
        model_filename = join(files_folder, "modelqlzB7i.xml")

        response_load_model = c.post('/models/', {
            'action': 'load_model',
            'docfile': open(model_filename, 'rb')
        })

        self.assertEqual(response_load_model.status_code, 200)
        self.assertEqual(len(SbmlModel.objects.filter(project=project)), 1)

        model = SbmlModel.objects.filter(project=project)[0]
        sbml_doc = SbmlDocument()
        sbml_doc.readSbmlFromFile(
            join(settings.MEDIA_ROOT, str(model.sbml_file)))
        sbml_model = sbml_doc.getModelInstance()
        reaction = sbml_model.listOfReactions.getBySbmlId('reaction_4')

        response_choose_project = c.post('/models/', {
            'action': 'choose_project',
            'project_id': 0
        })
        self.assertEqual(response_choose_project.status_code, 200)
        self.assertEqual(response_choose_project.context['project_name'],
                         "Project")

        response_choose_model = c.post('/edit/reactions/', {
            'action': 'choose_model',
            'model_id': 0
        })
        self.assertEqual(response_choose_model.status_code, 200)
        self.assertEqual(response_choose_model.context['model_name'],
                         "SOS-Ras-MAPK with n17")
        self.assertEqual([
            t_reaction.getReactionDescription() for t_reaction in
            response_choose_model.context['list_of_reactions']
        ], [
            t_reaction.getReactionDescription()
            for t_reaction in sbml_model.listOfReactions
        ])

        response_get_reaction = c.post('/json/get_reaction/',
                                       {'sbml_id': 'reaction_4'})

        self.assertEqual(response_get_reaction.status_code, 200)
        json_response = loads(response_get_reaction.content.decode('utf-8'))

        self.assertEqual(json_response[u'id'],
                         sbml_model.listOfReactions.index(reaction))
        self.assertEqual(json_response[u'sbml_id'], reaction.getSbmlId())
        self.assertEqual(json_response[u'name'], reaction.getName())
        self.assertEqual(json_response[u'kinetic_law'],
                         reaction.kineticLaw.getPrettyPrintMathFormula())
        self.assertEqual(json_response[u'reaction_type'],
                         reaction.getReactionType())
        self.assertEqual(json_response[u'reaction_type_name'],
                         KineticLaw.reactionTypes[reaction.getReactionType()])
        self.assertEqual(json_response[u'reversible'], reaction.reversible)

        self.assertEqual(json_response[u'list_of_reactants'], [[
            sbml_model.listOfSpecies.index(reactant.getSpecies()),
            reactant.stoichiometry.getPrettyPrintMathFormula()
        ] for reactant in reaction.listOfReactants])
        self.assertEqual(json_response[u'list_of_modifiers'], [[
            sbml_model.listOfSpecies.index(modifier.getSpecies()),
            modifier.stoichiometry.getPrettyPrintMathFormula()
        ] for modifier in reaction.listOfModifiers])
        self.assertEqual(json_response[u'list_of_products'], [[
            sbml_model.listOfSpecies.index(product.getSpecies()),
            product.stoichiometry.getPrettyPrintMathFormula()
        ] for product in reaction.listOfProducts])

        response_delete_reaction = c.post('/edit/reactions/', {
            'action': 'delete',
            'reaction_id': 2
        })

        self.assertEqual(response_delete_reaction.status_code, 200)

        model = SbmlModel.objects.filter(project=project)[0]
        sbml_doc = SbmlDocument()
        sbml_doc.readSbmlFromFile(
            join(settings.MEDIA_ROOT, str(model.sbml_file)))
        sbml_model = sbml_doc.getModelInstance()
        self.assertEqual(len(sbml_model.listOfReactions), 21)
        self.assertEqual(sbml_model.listOfReactions.getBySbmlId('reaction_4'),
                         None)
        response_create_creation = c.post(
            '/edit/reactions/', {
                'action': 'save',
                'reaction_id': "",
                'reaction_sbml_id': "reaction_4",
                'reaction_name': "Ras activation by SOS-Ras-GDP",
                'reaction_reactant_0': 1,
                'reaction_reactant_0_stoichiometry': "1",
                'reaction_modifier_0': 3,
                'reaction_modifier_0_stoichiometry': "1",
                'reaction_product_0': 2,
                'reaction_product_0_stoichiometry': "1",
                'reaction_type': 1,
                'reaction_parameter_0': 2,
                'reaction_parameter_1': 3,
                'reaction_sboterm': ""
            })

        self.assertEqual(response_create_creation.status_code, 200)
        model = SbmlModel.objects.filter(project=project)[0]
        sbml_doc = SbmlDocument()
        sbml_doc.readSbmlFromFile(
            join(settings.MEDIA_ROOT, str(model.sbml_file)))
        sbml_model = sbml_doc.getModelInstance()
        reaction = sbml_model.listOfReactions.getBySbmlId('reaction_4')
        self.assertEqual(len(sbml_model.listOfReactions), 22)
        self.assertEqual(reaction.getName(), "Ras activation by SOS-Ras-GDP")

        response_modify_creation = c.post(
            '/edit/reactions/', {
                'action': 'save',
                'reaction_id': sbml_model.listOfReactions.index(reaction),
                'reaction_sbml_id': "reaction_4",
                'reaction_name': "Ras activation by SOS-Ras-GDP, modified",
                'reaction_reactant_0': 1,
                'reaction_reactant_0_stoichiometry': "1",
                'reaction_modifier_0': 3,
                'reaction_modifier_0_stoichiometry': "1",
                'reaction_product_0': 2,
                'reaction_product_0_stoichiometry': "1",
                'reaction_type': 1,
                'reaction_parameter_0': 2,
                'reaction_parameter_1': 3,
                'reaction_sboterm': ""
            })

        self.assertEqual(response_modify_creation.status_code, 200)
        model = SbmlModel.objects.filter(project=project)[0]
        sbml_doc = SbmlDocument()
        sbml_doc.readSbmlFromFile(
            join(settings.MEDIA_ROOT, str(model.sbml_file)))
        sbml_model = sbml_doc.getModelInstance()
        reaction = sbml_model.listOfReactions.getBySbmlId('reaction_4')
        self.assertEqual(len(sbml_model.listOfReactions), 22)
        self.assertEqual(reaction.getName(),
                         "Ras activation by SOS-Ras-GDP, modified")
Esempio n. 13
0
	def testOptimization(self):

		user = User.objects.filter(username='******')[0]
		self.assertEqual(len(Project.objects.filter(user=user)), 1)
		project = Project.objects.filter(user=user)[0]

		self.assertEqual(len(SbmlModel.objects.filter(project=project)), 0)

		if isdir(join(settings.MEDIA_ROOT, project.folder)):
			rmtree(join(settings.MEDIA_ROOT, project.folder))
			mkdir(join(settings.MEDIA_ROOT, project.folder))

		c = Client()
		self.assertTrue(c.login(username='******', password='******'))

		response_choose_project = c.get('/project/%s/' % project.folder)
		self.assertRedirects(response_choose_project, '/models/', status_code=302, target_status_code=200)

		response_choose_project = c.post('/models/', {
			'action': 'choose_project',
			'project_id': 0
		})
		self.assertEqual(response_choose_project.status_code, 200)

		files_folder = join(dirname(__file__), "files")
		comp_files_folder = join(files_folder, "comp_model")

		model_filename = join(comp_files_folder, "modelcEvRcX.xml")
		response_load_submodel_1 = c.post('/models/', {
			'action': 'load_model',
			'docfile': open(model_filename, 'rb')
		})

		self.assertEqual(response_load_submodel_1.status_code, 200)
		self.assertEqual(len(SbmlModel.objects.filter(project=project)), 1)

		model_filename = join(comp_files_folder, "modelEHfev9.xml")
		response_load_submodel_2 = c.post('/models/', {
			'action': 'load_model',
			'docfile': open(model_filename, 'rb')
		})

		self.assertEqual(response_load_submodel_2.status_code, 200)
		self.assertEqual(len(SbmlModel.objects.filter(project=project)), 2)

		model_filename = join(comp_files_folder, "modelI1vrys.xml")
		response_load_submodel_3 = c.post('/models/', {
			'action': 'load_model',
			'docfile': open(model_filename, 'rb')
		})

		self.assertEqual(response_load_submodel_3.status_code, 200)
		self.assertEqual(len(SbmlModel.objects.filter(project=project)), 3)

		model_filename = join(comp_files_folder, "modelz9xdww.xml")

		response_load_model = c.post('/models/', {
			'action': 'load_model',
			'docfile': open(model_filename, 'rb')
		})

		self.assertEqual(response_load_model.status_code, 200)
		self.assertEqual(len(SbmlModel.objects.filter(project=project)), 4)

		response_choose_model = c.post('/fit/data/', {
			'action': 'choose_model',
			'model_id': 3
		})
		self.assertEqual(response_choose_model.status_code, 200)
		experiment_filename = join(files_folder, "ras_data.xml")

		response_import_data = c.post('/data/', {
			'action': 'import',
			'docfile': open(experiment_filename, 'rb')
		})

		self.assertEqual(response_import_data.status_code, 200)
		self.assertEqual(
			[experiment.name for experiment in response_import_data.context['experimental_data']],
			[u'Ras-GTP quantifications']
		)
		experiment_id = response_import_data.context['experimental_data'][0].id

		response_get_fit_data = c.get('/fit/data/')
		self.assertEqual(response_get_fit_data.status_code, 200)
		self.assertEqual(
			[dataset for dataset in response_get_fit_data.context['experimental_data_sets']],
			[u'Ras-GTP quantifications']
		)

		response_list_optimizations = c.get('/fit/list/')
		self.assertEqual(response_list_optimizations.status_code, 200)
		self.assertEqual(len(response_list_optimizations.context['optimizations']), 0)

		response_add_dataset = c.post('/json/add_dataset/', {
			'dataset_ind': 0
		})

		self.assertEqual(response_add_dataset.status_code, 200)
		mapping = loads(response_add_dataset.content.decode('utf-8'))['model_xpaths']

		sbml_filename = str(SbmlModel.objects.filter(project=project)[3].sbml_file)

		doc = SbmlDocument()
		doc.readSbmlFromFile(join(settings.MEDIA_ROOT, sbml_filename))

		self.assertEqual(sorted(list(mapping.keys())), ['FGF2', 'Total Ras-GTP'])

		self.assertEqual(doc.getModelInstance().listOfSpecies.index(doc.getByXPath(mapping['FGF2'], instance=True)), 4)
		self.assertEqual(doc.getModelInstance().listOfSpecies.index(doc.getByXPath(mapping['Total Ras-GTP'], instance=True)), 13)

		response_create_optimization = c.post('/fit/data/', {
			'action': 'create',
			'dataset_0': experiment_id, #response_get_fit_data.context['experimental_data_sets'][0].id,

			'list_dataset_0_data_species_0_value': "FGF2",
			'list_dataset_0_species_0_value': 4,
			'list_dataset_0_data_species_1_value': "Total Ras-GTP",
			'list_dataset_0_species_1_value': 13,

			'parameter_0_id': 1,
			'parameter_0_name': "SOS activation by FGF2",
			'parameter_0_value': 1.0,
			'parameter_0_min': 1e-4,
			'parameter_0_max': 1e+4,
			'parameter_0_precision': 7,

			'parameter_1_active': "on",
			'parameter_1_id': 1,
			'parameter_1_name': "SOS inactivation by Mapk catalytic constant",
			'parameter_1_value': 1.0,
			'parameter_1_min': 1e-6,
			'parameter_1_max': 1e+6,
			'parameter_1_precision': 7,

			'nb_cores': 2,
			'lambda': 0.02,
			'score_precision': 0.001,
			'param_precision': 7,
			'initial_temperature': 1,
			'initial_moves': 200,
			'freeze_count': 1,
			'negative_penalty': 0
		})

		self.assertEqual(response_create_optimization.status_code, 200)
		self.assertEqual(response_create_optimization.context['form'].getErrors(), [])
		sleep(10)

		response_list_optimizations = c.get('/fit/list/')
		self.assertEqual(response_list_optimizations.status_code, 200)
		self.assertEqual(len(response_list_optimizations.context['optimizations']), 1)
		self.assertEqual(response_list_optimizations.context['optimizations'][0][0].status, "Running")

		sleep(10)

		optimization_id = response_list_optimizations.context['optimizations'][0][0].optimization_id
		response_get_optimization = c.get('/fit/%s/' % optimization_id)
		self.assertEqual(response_get_optimization.status_code, 200)

		max_time = 360
		sleep(360)
		response_list_optimizations = c.get('/fit/list/')
		self.assertEqual(response_list_optimizations.status_code, 200)
		self.assertEqual(response_list_optimizations.context['optimizations'][0][0].status, "Finished")

		response_get_optimization = c.get('/fit/%s/' % optimization_id)
		self.assertEqual(response_get_optimization.status_code, 200)

		scores = response_get_optimization.context['score_values']
		self.assertTrue(scores[len(scores)-1] < 1.01)
Esempio n. 14
0
    def testUnits(self):

        user = User.objects.filter(username='******')[0]
        self.assertEqual(len(Project.objects.filter(user=user)), 1)
        project = Project.objects.filter(user=user)[0]
        self.assertEqual(len(SbmlModel.objects.filter(project=project)), 0)

        if isdir(join(settings.MEDIA_ROOT, project.folder)):
            rmtree(join(settings.MEDIA_ROOT, project.folder))
            mkdir(join(settings.MEDIA_ROOT, project.folder))

        c = Client()
        self.assertTrue(c.login(username='******', password='******'))

        response_choose_project = c.get('/project/%s/' % project.folder)
        self.assertRedirects(response_choose_project,
                             '/models/',
                             status_code=302,
                             target_status_code=200)

        files_folder = join(dirname(__file__), "files")
        model_filename = join(files_folder, "modelqlzB7i.xml")

        response_load_model = c.post('/models/', {
            'action': 'load_model',
            'docfile': open(model_filename, 'rb')
        })

        self.assertEqual(response_load_model.status_code, 200)
        self.assertEqual(len(SbmlModel.objects.filter(project=project)), 1)

        model = SbmlModel.objects.filter(project=project)[0]
        sbml_doc = SbmlDocument()
        sbml_doc.readSbmlFromFile(
            join(settings.MEDIA_ROOT, str(model.sbml_file)))
        sbml_model = sbml_doc.getModelInstance()

        response_choose_project = c.post('/models/', {
            'action': 'choose_project',
            'project_id': 0
        })
        self.assertEqual(response_choose_project.status_code, 200)
        self.assertEqual(response_choose_project.context['project_name'],
                         "Project")

        response_choose_model = c.post('/edit/units/', {
            'action': 'choose_model',
            'model_id': 0
        })
        self.assertEqual(response_choose_model.status_code, 200)
        self.assertEqual(response_choose_model.context['model_name'],
                         "SOS-Ras-MAPK with n17")

        self.assertEqual(response_choose_model.context['unit_definitions'], [
            'Litre', 'Nanomoles', 'Nanomoles per litre', 'Seconds',
            'per Seconds', 'per Nanomoles ', 'Dimensionless',
            'per Nanomoles per Seconds', 'Nanomoles per seconds',
            'Nanomoles per litre per seconds',
            'per Nanomoles per liter per Seconds'
        ])

        response_save_unit = c.post(
            '/edit/units/', {
                'action': 'save',
                'unit_definition_id': '',
                'unit_definition_name': "Nanomolars",
                'unit_definition_identifier': "nanomolars",
                'subunit_id_0': list(Unit.unit_id.values()).index("mole"),
                'subunit_exponent_0': 1,
                'subunit_scale_0': -9,
                'subunit_multiplier_0': 1,
                'subunit_id_1': list(Unit.unit_id.values()).index("litre"),
                'subunit_exponent_1': -1,
                'subunit_scale_1': 1,
                'subunit_multiplier_1': 1,
            })

        self.assertEqual(response_save_unit.status_code, 200)
        self.assertEqual(response_save_unit.context['form'].getErrors(), [])
        self.assertEqual(response_save_unit.context['unit_definitions'], [
            'Litre', 'Nanomoles', 'Nanomoles per litre', 'Seconds',
            'per Seconds', 'per Nanomoles ', 'Dimensionless',
            'per Nanomoles per Seconds', 'Nanomoles per seconds',
            'Nanomoles per litre per seconds',
            'per Nanomoles per liter per Seconds', 'Nanomolars'
        ])

        response_get_unit_definition = c.post(
            '/json/get_unit_definition/', {
                'id':
                response_save_unit.context['unit_definitions'].index(
                    'Nanomolars'),
            })
        self.assertEqual(response_get_unit_definition.status_code, 200)
        json_response = loads(
            response_get_unit_definition.content.decode('utf-8'))

        self.assertEqual(json_response[u'unit_id'], "nanomolars")
        self.assertEqual(json_response[u'name'], "Nanomolars")
        self.assertEqual(json_response[u'desc'], "nmole.litre^-1")
        self.assertEqual(json_response[u'list_of_units'][0],
                         [u'nmole', 20, u'mole', 1, -9, 1.0])
        self.assertEqual(json_response[u'list_of_units'][1],
                         [u'litre^-1', 16, u'litre', -1, 1, 1.0])

        response_delete_unit_definition = c.post(
            '/edit/units/', {
                'action':
                'delete',
                'id':
                response_save_unit.context['unit_definitions'].index(
                    'Nanomolars')
            })

        self.assertEqual(response_delete_unit_definition.status_code, 200)
        self.assertEqual(
            response_delete_unit_definition.context['unit_definitions'], [
                'Litre', 'Nanomoles', 'Nanomoles per litre', 'Seconds',
                'per Seconds', 'per Nanomoles ', 'Dimensionless',
                'per Nanomoles per Seconds', 'Nanomoles per seconds',
                'Nanomoles per litre per seconds',
                'per Nanomoles per liter per Seconds'
            ])