def test_bind(self): """ """ ethanol = formula.Formula('C2H5OH') aceticacid = formula.Formula('CH3COOH') diethylester = ethanol.bind(aceticacid) assert diethylester.formula == 'C4H8O2'
def test_add(self): """ """ aceticacid = formula.Formula('CH3COOH') acetate = aceticacid - formula.Formula('H', charge = 1) assert acetate.charge == -1 acetate += formula.Formula('H', charge = 1) assert abs(acetate.mass - aceticacid.mass) < 0.0000001
def test_bind_split(self): """ """ ethanol = formula.Formula('C2H5OH') aceticacid = formula.Formula('CH3COOH') diethylester = ethanol.bind(aceticacid) ethanol2, aceticacid2 = diethylester.split(ethanol) assert ethanol.formula == ethanol2.formula assert aceticacid.formula == aceticacid2.formula
def mz_lowest_error_from_name( self, measured_mz, adduct, name=None, **kwargs, ): """ Regarding a measured m/z and an assumed adduct type and name returns the m/z of the record with matching name and lowest error. """ exmasses = self.masses_from_name(name=name, **kwargs) if exmasses is not None: adduct_method = (settings.get('ex2ad_all')[adduct]) addmasses = np.array([ getattr(formula.Formula(exmass), adduct_method)() for exmass in exmasses ]) ppms = np.array( [common.ppm(addmass, measured_mz) for addmass in addmasses]) return addmasses[np.argmin(np.abs(ppms))]
def process_line(l): """ Parameters ---------- l : Returns ------- """ l = l.split('\t') mass = (formula.Formula(l[1]).mass if l[1] else float(l[0]) if l[0] else None) self.constraints[l[2]] = ( tuple( fragment.FragConstraint( hg=constr.split(',')[0], sub=(tuple(constr.split(',')[1:]) if ',' in constr else ()), sph=constr.split('|')[1] if '|' in constr else None, # no way at the moment to define chain # type in file but later can be added easily chaintype=None) for constr in l[4].strip().split(';')) if l[4].strip() else ( # an empty constraint to make sure anything matches fragment.FragConstraint(), )) return [mass, l[2], l[3], np.nan, np.nan, np.nan, get_charge(l[3])]
def __iter__(self, cores_only = False): for i in range(len(self.cores)): self.update_core(i) for c in self.chlens: self.c = c for u in self.unsats: self.u = u if not self.c_u_diff(self.c, self.u): continue # implicit hydrogens h = c * 2 + 2 - self.valence - 2 * u p = self.get_prefix() new_counts = self.counts.copy() new_counts['C'] += c new_counts['H'] += h new_attrs = copy.deepcopy(self.attrs) # `attrs` might contain methods which are called # with the present instance passed and their returned # value will be the attribute value of the # iteration products for k, v in iteritems(new_attrs.__dict__): if hasattr(v, '__call__'): setattr(new_attrs, k, v(self)) if self.chain_type and self.chain_attr and c > 0: new_attrs.chain = self.get_chain() new = self + formula.Formula(**new_counts) new.attrs = new_attrs new.c = c new.u = u new.get_prefix = lambda: p new.variable_aliphatic_chain = ( self.variable_aliphatic_chain ) new.name = self.getname(self) # new.attrs.chain = self.get_chain() yield new if cores_only: break if cores_only: break
def get_substituent(sub): """Creates a `Formula` object if the substituent is not already an instance of `Formula` or `Substituent`. Parameters ---------- sub : Returns ------- """ return ( formula.Formula(sub) if hasattr(sub, 'lower') or type(sub) is float else formula.Formula(**sub) if type(sub) is dict else sub )
def add_annotations(mol, record, exact_mass_formula_fallback=True): """ Parameters ---------- mol : record : exact_mass_formula_fallback : (Default value = True) Returns ------- """ mol.db_id = record[0] mol.name = record[3] if hasattr(mol, 'OBMol'): mol.OBMol.SetTitle(record[2]) else: mol.title = tuple(n.strip() for n in record[2:5] if n.strip()) mol.chebi = record[24] if len(record) > 24 else '' mol.lipidmaps = record[25] if len(record) > 25 else '' mol.hmdb = record[26] if len(record) > 26 else '' mol.smiles = record[8] mol.swl_exact_mass = float(record[14]) if record[14] else None if not mol.swl_exact_mass and exact_mass_formula_fallback: try: # note: this is dangerous because the formula is sometimes # dehydrogenated and charged state while exact mass # should be uncharged with all hydrogenes mol.swl_exact_mass = formula.Formula(record[11]).mass except KeyError: pass mol.swl_formula = record[11] mol.inchi = record[9] mol.inchikey = record[10] mol.level = record[1] return mol
def mz_from_name( self, adduct, name=None, database_preference=None, **kwargs, ): exmass = self.mass_from_name( name=name, database_preference=database_preference, **kwargs, ) if exmass is not None: adduct_method = (settings.get('ex2ad_all')[adduct]) return getattr(formula.Formula(exmass), adduct_method)()
def __iter__(self): for rec in sdf.SdfReader.__iter__(self): if ('EXACT_MASS' not in rec['annot'] or float(rec['annot']['EXACT_MASS']) == 0): try: exmass = formula.Formula(rec['annot']['FORMULA']).mass except KeyError: # if no exact mass it means # this is a higher level category continue else: exmass = float(rec['annot']['EXACT_MASS']) names = [ rec['name'][nametype].strip() for nametype in ('COMMON_NAME', 'SYSTEMATIC_NAME') if nametype in rec['name'] ] if 'SYNONYMS' in rec['name']: names.extend(n.strip() for n in rec['name']['SYNONYMS'].split(';')) names = [n.strip() for n in names if n.strip()] hg, chainsum, chains = self.nameproc.process(names) liprec = lipproc.LipidRecord( lab=lipproc.LipidLabel( db_id=rec['id'], db='LipidMaps', names=tuple(names), formula=rec['annot']['FORMULA'], ), hg=hg, chainsum=chainsum, chains=chains, ) yield exmass, liprec
def test_calc_mass(self): """ """ ethanol = formula.Formula('C2H5OH') assert abs(ethanol.mass - 46.04186481376) < 0.0000001
# In[11]: mass.expr('H + n') # Hydrogensulphate ion: # In[12]: mass.expr('HSO4 + e') # Additional attributes can be provided in keyword arguments to carry metadata. # In[40]: lactic_acid = formula.Formula('CH3CHOHCOOH', name='lactic acid') lactic_acid.attrs.name # A galactose: # In[15]: ((2 * formula.Formula('C6H12O6')) - 'H2O').formula # ## 2: Calculations with adducts # In[4]: from lipyd import mz # This is an oleic acid. What is the mass of the [M-H]- adduct?