def test_get_mw(self): '''Tests get_mw method.''' curr_dir = os.path.dirname(os.path.realpath(__file__)) model = read_sbml_model( os.path.join(curr_dir, '../../../data/models/yeastGEM.xml')) tests = 0 while tests < 10: met = random.choice(model.metabolites) # met = model.metabolites.get_by_id('s_3071__91__lp__93__') print(met.id, met.name) # Get existing formula and mw: formula = met.formula if formula: existing_mw = get_molecular_mass(formula, r_mass=2**16) # Unset formula: met.formula = None calc_mw = get_mw(model, met.id) if calc_mw: self.assertAlmostEqual(existing_mw, calc_mw, 0) tests += 1 # Reset formula: met.formula = formula
def get_mw(model, met_id, parent_reacts=None): '''Get molecular weight.''' if parent_reacts is None: parent_reacts = [] met = model.metabolites.get_by_id(met_id) target_coeff = float('NaN') if met.formula: return get_molecular_mass(met.formula, r_mass=2**16) for react in met.reactions: if len(react.metabolites) > 1 and react not in parent_reacts: mw = 0 for react_met, coeff in react.metabolites.items(): if react_met.id != met.id: parent_reacts.append(react) mw += get_mw(model, react_met.id, parent_reacts) * coeff if np.isnan(mw): break else: target_coeff = -coeff if not np.isnan(mw): print('Found:', met.name, mw, react.name) return mw * target_coeff print('Unfound:', met.name) return float('NaN')
def _add_metabolite(model_id, model_ids, current_bigg_ids, bigg_data): '''Adds a given metabolite to the collection of metabolites.''' met_url = 'http://bigg.ucsd.edu/api/v2/models/' + model_id + \ '/metabolites/' + current_bigg_ids[1] met_data = json.loads(urllib2.urlopen(met_url).read()) bg_dat = {} bigg_data[met_data['bigg_id']] = bg_dat bg_dat['name'] = met_data['name'] bg_dat['formula'] = met_data['formula'] bg_dat['database_links'] = dict((key, value[0]['id']) for key, value in met_data['database_links'] .iteritems()) bg_dat['models'] = [model['bigg_id'] for model in met_data['other_models_with_metabolite'] if model['bigg_id'] in model_ids] bg_dat['models'].append(model_id) bg_dat['models'] = sorted(set(bg_dat['models'])) try: bg_dat['mass'] = \ chem_utils.get_molecular_mass(met_data['formula']) except KeyError, error: print met_data['bigg_id'] + ': ' + str(error)
def _normalise_mass(properties): '''Removes ambiguity in mass values by recalculating according to chemical formula.''' properties.pop('mass', None) if 'formula' in properties and properties['formula'] is not None: mono_mass = chem_utils.get_molecular_mass(properties['formula']) if not math.isnan(mono_mass): properties['monoisotopic_mass:float'] = mono_mass
def _normalise_mass(properties): '''Removes ambiguity in mass values by recalculating according to chemical formula.''' properties.pop('mass:float', None) if 'formula' in properties and properties['formula'] is not None: mono_mass = chem_utils.get_molecular_mass(properties['formula']) if not math.isnan(mono_mass): properties['monoisotopic_mass:float'] = mono_mass
def test_get_molecular_mass(self): '''Tests get_molecular_mass method.''' self.assertAlmostEqual(chm_util.get_molecular_mass('H2O'), 18.010564684)