class FBAModel(StoredObject): """ A class representing an FBA Model in the stored environment Parent Classes: StoredObject -> FBAModel """ storedType = service.types()['FBAModel'] DEFAULT_BIOCHEM = Biochemistry(6, 489) def get_reactions(self): """ Returns a list of ModelReaction objects representing this model's reactions """ model_obj = self.get_object() return [ModelReaction(r) for r in model_obj['modelreactions'] ] # Potentially Wasteful but maintains D.R.Y. def get_features(self): """ returns a set<str> of all gene features in the model :return: set<str> features """ rxns = self.get_reactions() features = set() for r in rxns: features |= r.gpr.ftrs return features
class ReactionProbabilities(StoredObject): """ a class representing a ReactionProbabilities in the stored environment """ storedType = service.types()['ReactionProbabilities'] def __init__(self, object_id, workspace_id): super(ReactionProbabilities, self).__init__(object_id, workspace_id) self._prob_hash = None def probability_hash(self): """ :return: a dictionary of the reactions in this object to their probabilities """ if self._prob_hash is not None: return self._prob_hash prob_hash = dict() for rxn in self.data['reaction_probabilities']: prob_hash[rxn[0]] = rxn[1] self._prob_hash = prob_hash return prob_hash def get_likelihood(self, reaction_id): """ returns the likelihood a reaction is represented in the genome, raises ValueError if not found :param reaction_id: :return: """ try: return self.probability_hash()[reaction_id] except KeyError: return -1
class Media(StoredObject): """ a class representing a media in the stored environment """ storedType = service.types()['Media'] def fba_formulation(self, arguments=None): """ Generates an FBA formulation used to create an FBA object. :param arguments: (optional) additional arguments to FBA formulation :return: dictionary of arguments to FBA (fba_formulation) """ return service.fba_formulation(self)
class ProteomeComparison(StoredObject): """ a class representing a Porteome Comparison in the stored environment """ storedType = service.types()['ProteomeComparison'] def get_genomes(self): """ returns the genomes of the compared genomes :return: tuple (Genome, Genome) """ g1ws, g1obj, _version = self.data['genome1ref'].split('/') g12ws, g2obj, _version = self.data['genome2ref'].split('/') return Genome(g1obj, g1ws), Genome(g2obj, g12ws) def find_matches(self, gene, genome=None): """ Finds the matches for a particular gene in the proteome comparison. The genome can be inferred or set manually :param gene: gene to look for matches :param genome: (optional) the genome the gene is located in :return: list of tuples(string, int) (gene, %hit) that were found as matches, often a singleton list """ # Set genome to index of the genome in the comparison (have to because of ugly KBase ProtComp Data Structure0 if genome is not None: genomes = [g.get_genome_id() for g in self.get_genomes()] if genome.get_genome_id() not in genomes: raise ValueError('gene: ' + str(gene) + ' not in genomes: ' + str(genomes)) genome = genomes.index(genome.get_genome_id()) else: genomes = [g.get_genome_id() for g in self.get_genomes()] for i in range(len(genomes)): if gene.startswith(genomes[i]): genome = i if genome is None: raise ValueError('gene: ' + str(gene) + ' not in genomes: ' + str(genomes)) # Find map index in proteome comp data structure for gene map_key = 'proteome' + str(genome + 1) + 'map' match_name_key = 'proteome1names' if map_key == 'proteome2map' else 'proteome2names' data_key = 'data' + str(genome + 1) names_map = self.data[map_key] matches = self.data[data_key][names_map[gene]] result = [] for m in matches: result.append((self.data[match_name_key][m[0]], m[2])) return result def get_genome_names(self): return [g.get_genome_name() for g in self.get_genomes()]
class Biochemistry(StoredObject): """ a class representing a Biochemistry Object """ storedType = service.types()['Biochemistry'] def get_compound(self, compound_id): """ returns dictionary of information on a compound :param compound_id: str e.g. 'cpd00011' :return: """ for c in self.data['compounds']: if compound_id == c['id']: return c raise ValueError(str(compound_id) + ' is not in the biochemistry')
class Genome(StoredObject): """ a class representing a genome in the stored environment """ storedType = service.types()['Genome'] def get_genome_id(self): return self.data['id'] def get_genome_name(self): return self.data['scientific_name'] def get_features(self): return self.data['features'] def alias_map(self, key_str, val_str=None): """ returns a dictionary of aliases id types for this genome (a key and value reference same gene) :param key_str: A key indicating what kind of alias to use as keys (the standard prefix ('MMP', 'kb|g.575') :param val_str: (optional) str indicating what kind of alias to map to (default is ID (e.g. kb|g.575) :return: dictionary of alias mappings """ result = dict() for f in self.get_features(): if 'aliases' in f: for alias in f['aliases']: if alias.startswith(key_str): if val_str is not None: values = [ a for a in f['aliases'] if a.startswith(val_str) ] assert len(values) <= 1 if len(values) > 0: result[alias] = values[0] else: result[alias] = f['id'] for key in result: val = result[key] for f in self.get_features(): if 'aliases' in f and val in f['aliases']: assert f['id'] == key, 'Not a 1:1 mapping ' + str( key) + ', ' + str(val) return result return result
class FBA(StoredObject): """ a class representing an FBA result in the stored environment """ storedType = service.types()['FBA'] def __init__(self, object_id, workspace_id): super(FBA, self).__init__(object_id, workspace_id) self.objective = self.get_objective() def get_objective(self): """ returns the objective value from the FBA Run :return: """ return self.data['objectiveValue'] def get_model(self): """ returns the FBAModel associated with this FBA :return: FBAModel """ info = self.data['fbamodel_ref'].split('/') return FBAModel(info[1], info[0]) def get_media(self): """ returns the Media associated with this FBA :return: Media """ info = self.data['media_ref'].split('/') return Media(info[1], info[0]) def blocked_reactions(self): """ returns a list of rxn_ids of reactions that were incapable of carrying flux in FBA WARNING: MAKE SURE THIS LINKS TO AN FBA OBJECT WHERE FVA WAS RUN OR ERRONEROUS RESULTS WILL BE FOUND :return: """ result = list() for r in self.data['FBAReactionVariables']: if r['max'] == 0 and r['min'] == 0: assert r['class'] == 'Blocked' result.append(r['modelreaction_ref'].split('/')[-1]) return result def primary_exchanges(self): """ Returns the 10 :return: """ biochem = FBAModel.DEFAULT_BIOCHEM flux = [[f['value'], f['modelcompound_ref'].split('/')[-1]] for f in self.data['FBACompoundVariables']] for f in flux: try: c = biochem.get_compound(f[1].split('_')[0]) f[1] = c['name'] + '(' + c['formula'] + ')' except ValueError: pass flux.sort() return flux[0:10], flux[-10:]