def createStochKitModelWrapperFromStochKitModel(handler, model, public = False): species = [] parameters = [] reactions = [] meshWrapperDb = db.GqlQuery("SELECT * FROM MeshWrapper WHERE user_id = :1", handler.user.user_id()).get() def fixName(name): if re.match('^[^a-zA-Z_]', name): name = 'a' + name return re.sub('[^a-zA-Z0-9_]', '', name) spatial = { 'initial_conditions' : [], 'mesh_wrapper_id' : meshWrapperDb.key().id(), 'reactions_subdomain_assignments': {}, 'species_diffusion_coefficients': {}, 'species_subdomain_assignments': {} } for specieName, specie in model.listOfSpecies.items(): name = fixName(specie.name) species.append({ 'name' : name, 'initialCondition' : specie.initial_value }) spatial['species_diffusion_coefficients'][name] = 0.0 spatial['species_subdomain_assignments'][name] = meshWrapperDb.uniqueSubdomains for parameterName, parameter in model.listOfParameters.items(): parameter.evaluate() name = fixName(parameter.name) parameters.append({ 'name' : name, 'value' : parameter.value }) modelType = 'massaction' for reactionName, reaction in model.listOfReactions.items(): outReaction = {} reactants = [] products = [] totalReactants = 0 reactantCount = len(reaction.reactants.items()) productCount = len(reaction.products.items()) for reactantName, stoichiometry in reaction.reactants.items(): reactantName = fixName(reactantName) reactants.append({ 'specie' : reactantName, 'stoichiometry' : stoichiometry }) totalReactants += stoichiometry for productName, stoichiometry in reaction.products.items(): productName = fixName(productName) products.append({ 'specie' : productName, 'stoichiometry' : stoichiometry }) if reaction.massaction == True: outReaction['type'] = 'massaction' outReaction['rate'] = fixName(reaction.marate.name) if reactantCount == 0 and productCount == 1: outReaction['type'] = 'creation' elif reactantCount == 1 and productCount == 0: outReaction['type'] = 'destruction' elif reactantCount == 2 and productCount == 1: outReaction['type'] = 'merge' elif reactantCount == 1 and productCount == 1 and totalReactants == 1: outReaction['type'] = 'change' elif reactantCount == 1 and productCount == 1 and totalReactants == 2: outReaction['type'] = 'dimerization' elif reactantCount == 1 and productCount == 2: outReaction['type'] = 'split' elif reactantCount == 2 and productCount == 2: outReaction['type'] = 'four' if totalReactants > 2: raise Exception("Error in Reaction {0}: StochKit mass action reactions cannot have more than 2 total reacting particles. Total stoichiometry for this reaction is {1}".format(reactionName, totalRreactants)) else: modelType = 'custom' outReaction['type'] = 'custom' outReaction['equation'] = reaction.propensity_function outReaction['reactants'] = reactants outReaction['products'] = products outReaction['name'] = fixName(reaction.name) spatial['reactions_subdomain_assignments'][fixName(reaction.name)] = meshWrapperDb.uniqueSubdomains reactions.append(outReaction) names = [modelt['name'] for modelt in ModelManager.getModels(handler)] modelDb = StochKitModelWrapper() tmpName = fixName(model.name) while tmpName in names: tmpName = fixName(model.name) + '_' + ''.join(random.choice('abcdefghijklmnopqrztuvwxyz') for x in range(3)) name = tmpName modelDb.user_id = handler.user.user_id() modelDb.name = name modelDb.type = modelType modelDb.species = species modelDb.parameters = parameters modelDb.reactions = reactions modelDb.isSpatial = False modelDb.units = model.units modelDb.spatial = spatial modelDb.zipFileName = None modelDb.is_public = public modelDb.put() return modelDb
def updateModel(handler, jsonModel): createModel = False if "id" in jsonModel: modelWrap = StochKitModelWrapper.get_by_id(jsonModel["id"]) userID = handler.user.user_id() if userID != modelWrap.user_id: raise "Error accessing model {0} with user id {1} (model owned by {2})".format(jsonModel["id"], userID, modelWrap.user_id) else: createModel = True modelWrap = StochKitModelWrapper() if 'isSpatial' not in jsonModel or 'spatial' not in jsonModel: jsonModel['isSpatial'] = False jsonModel['spatial'] = { 'subdomains' : [], 'mesh_wrapper_id' : None, 'species_diffusion_coefficients' : {} , 'species_subdomain_assignments' : {} , 'reactions_subdomain_assignments' : {}, 'initial_conditions' : [] } # This seems insane if 'user_id' in jsonModel: userID = jsonModel['user_id'] else: userID = handler.user.user_id() #if "name" not in jsonModel: # raise Exception("Why is this code here?") if 'isSpatial' in jsonModel: modelWrap.isSpatial = jsonModel['isSpatial'] if 'spatial' in jsonModel: modelWrap.spatial = jsonModel['spatial'] # Make sure we have access to a copy of the mesh (if it exists) if "mesh_wrapper_id" in modelWrap.spatial and modelWrap.spatial["mesh_wrapper_id"]: meshDbCurrent = mesheditor.MeshWrapper.get_by_id(modelWrap.spatial["mesh_wrapper_id"]) if createModel: if meshDbCurrent.user_id != userID: meshDb = mesheditor.MeshWrapper() meshDb.user_id = userID names = [x.name for x in db.Query(mesheditor.MeshWrapper).filter('user_id =', handler.user.user_id()).run()] tmpName = meshDbCurrent.name i = 0 while tmpName in names: tmpName = meshDbCurrent.name + '_' + str(i) i += 1 meshDb.name = tmpName meshDb.description = meshDbCurrent.description meshDb.meshFileId = meshDbCurrent.meshFileId meshDb.subdomains = meshDbCurrent.subdomains meshDb.uniqueSubdomains = meshDbCurrent.uniqueSubdomains meshDb.undeletable = False#meshDbCurrent.undeletable meshDb.ghost = False meshDb.put() modelWrap.spatial["mesh_wrapper_id"] = meshDb.key().id() else: meshDbCurrent.ghost = False meshDbCurrent.put() # This is maintained here! modelWrap.subdomains = meshDbCurrent.uniqueSubdomains if 'is_public' not in jsonModel: jsonModel['is_public'] = False modelWrap.user_id = userID modelWrap.name = jsonModel["name"] modelWrap.species = jsonModel["species"] modelWrap.type = jsonModel["type"] modelWrap.parameters = jsonModel["parameters"] modelWrap.reactions = jsonModel["reactions"] #modelWrap.spatial = jsonModel["spatial"] #modelWrap.isSpatial = jsonModel["isSpatial"] modelWrap.is_public = jsonModel["is_public"] modelWrap.units = jsonModel["units"] return modelWrap.put().id()