class ElementalCompositionVisit: def __init__(self,comp): self.table = comp self.eltcomp = Composition() def visit(self,m): self.eltcomp.add(m.composition(self.table))
def fragments(self, r=None, force=False): atroot = False if r == None: r = self.root() atroot = True if force or (not hasattr(r, '_symbol_composition') or not hasattr(r, '_elemental_composition')): self.subtree_composition(r, sym_table=iupacSym, comp_table=ctable) links = r.links() nlink = len(links) if nlink == 0: # self fr = (copy.copy(r._symbol_composition),\ copy.copy(r._elemental_composition),True,0) yield fr return fragstore0 = [] fragstore1 = [] for l in links: fragstore0.append([]) fragstore1.append([]) for fr in self.fragments(l.child()): if fr[2]: fragstore0[-1].append(fr) else: fragstore1[-1].append(fr) fragstore0[-1].append((Composition(), Composition(), True, 1)) for i, prd in enumerate(product(*fragstore0)): symcomp = copy.copy(r._symbol_composition) eltcomp = copy.copy(r._elemental_composition) cl = 0 for l, fr in zip(links, prd): # determine the amount to substract symcomp1 = copy.copy(l.child()._symbol_composition) symcomp1.sub(fr[0]) symcomp.sub(symcomp1) eltcomp1 = copy.copy(l.child()._elemental_composition) eltcomp1.sub(fr[1]) eltcomp.sub(eltcomp1) cl += fr[3] fr = (symcomp, eltcomp, True, cl) yield fr for i in range(nlink): for fr in fragstore0[i][:-1]: fr1 = (fr[0], fr[1], False, fr[3] + 1) yield fr1 for fr in fragstore1[i]: fr1 = (fr[0], fr[1], False, fr[3]) yield fr1
def next(self): for gr in self.glydb: comp = Composition() for m in gr.glycan.all_nodes(): try: sym = self.syms.toStr(m) except KeyError: if m.noring(): if m.count_mod() == 1 and \ m.count_mod(Mod.aldi) == 1: aglycon = 'aldi?' m1 = m.clone() m1.clear_mods() try: sym = self.syms.toStr(m1) except KeyError: sym = 'Xxx' else: sym = 'Xxx' else: sym = 'Xxx' comp[sym] += 1 gr['composition'] = comp gr['compstr'] = comp2str1(comp) yield gr
def iupac_composition(self, floating_substituents=True, aggregate_basecomposition=True): c = Composition() for sym in (self.iupac_composition_syms + self.subst_composition_syms + ['Xxx', 'X']): c[sym] = 0 for m in self.all_nodes(undet_subst=True): try: sym = iupacSym.toStr(m) except KeyError: if isinstance(m, Monosaccharide): c['Xxx'] += 1 else: c['X'] += 1 continue if floating_substituents: syms = map(str.strip, sym.split('+')) else: syms = [sym] if syms[0] not in (self.iupac_composition_syms + self.subst_composition_syms): if isinstance(m, Monosaccharide): syms[0] = 'Xxx' else: syms[0] = 'X' for i in range(1, len(syms)): if syms[i] not in self.subst_composition_syms: syms[i] = 'X' if syms[0] == 'Xxx' or 'X' in syms: c['Xxx'] += 1 continue if syms[0] == 'X': c['X'] += 1 continue for sym in syms: c[sym] += 1 c['Count'] = sum( map(c.__getitem__, self.iupac_composition_syms + ['Xxx'])) if aggregate_basecomposition: c['Hex'] = sum(map(c.__getitem__, ('Man', 'Gal', 'Glc', 'Hex'))) c['HexNAc'] = sum( map(c.__getitem__, ('GalNAc', 'GlcNAc', 'ManNAc', 'HexNAc'))) c['dHex'] = sum(map(c.__getitem__, ('Fuc', 'dHex'))) c['Pent'] = sum(map(c.__getitem__, ('Xyl', 'Pent'))) c['Sia'] = sum(map(c.__getitem__, ('NeuAc', 'NeuGc', 'Sia'))) c['HexA'] = sum( map(c.__getitem__, ('GlcA', 'GalA', 'IdoA', 'ManA', 'HexA'))) c['HexN'] = sum( map(c.__getitem__, ('GlcN', 'GalN', 'ManN', 'HexN'))) return c
def visit(self,m): if self.comp: eltcomp = m.composition(self.comp) for c in m.children(): eltcomp.add(c._elemental_composition) m._elemental_composition = eltcomp if self.sym: symcomp = Composition() symcomp[self.sym.toStr(m)] = 1 for c in m.children(): symcomp.add(c._symbol_composition) m._symbol_composition = symcomp
from IsoShape import IsoShape class MonoisotopicElementMass(dict): def __init__(self): elts = ElementTable() for e in elts.values(): self[e.symbol] = e.mono class AverageElementMass(dict): def __init__(self): elts = ElementTable() for e in elts.values(): self[e.symbol] = e.ave class ElementIsotopes(dict): def __init__(self): elts = ElementTable() for e in elts.values(): self[e.symbol] = e.isotopes def cluster(self,comp,maxpos=5): sh = IsoShape(self,comp,maxpos) return sh.clusterIntensities() if __name__ == '__main__': import sys from CompositionTable import Composition mt = MonoisotopicElementMass() comp = Composition() comp.parse(" ".join(sys.argv[1:])) print comp.mass(mt)
def iupac_composition(self, floating_substituents=True, aggregate_basecomposition=True, redend_only=False): validsyms = self.iupac_composition_syms + self.subst_composition_syms if not floating_substituents: validsyms += self.iupac_aldi_composition_syms c = Composition() for sym in (validsyms + ['Xxx','X']): c[sym] = 0 if not redend_only: nodeiterable = self.all_nodes(undet_subst=True) else: if self.has_root(): nodeiterable = [ self.root() ] else: nodeiterable = [] for m in nodeiterable: try: sym = iupacSym.toStr(m) except KeyError: if isinstance(m,Monosaccharide): c['Xxx'] += 1 else: c['X'] += 1 continue if floating_substituents: syms = [ s.strip() for s in sym.split('+') ] else: syms = [sym] if syms[0] not in validsyms: if isinstance(m,Monosaccharide): syms[0] = 'Xxx' else: syms[0] = 'X' for i in range(1,len(syms)): if syms[i] not in self.subst_composition_syms: syms[i] = 'X' if syms[0] == 'Xxx' or 'X' in syms: c['Xxx'] += 1 continue if syms[0] == 'X': c['X'] += 1 continue for sym in syms: c[sym] += 1 c['Count'] = sum(map(c.__getitem__,self.iupac_composition_syms + ['Xxx'])) if aggregate_basecomposition: c['Hex'] = sum(map(c.__getitem__,('Man','Gal','Glc','Hex'))) c['Hex+aldi'] = sum(map(c.__getitem__,('Man+aldi','Gal+aldi','Glc+aldi','Hex+aldi'))) c['HexNAc'] = sum(map(c.__getitem__,('GalNAc','GlcNAc','ManNAc','HexNAc'))) c['HexNAc+aldi'] = sum(map(c.__getitem__,('GalNAc+aldi','GlcNAc+aldi','ManNAc+aldi','HexNAc+aldi'))) c['dHex'] = sum(map(c.__getitem__,('Fuc','dHex'))) c['dHex+aldi'] = sum(map(c.__getitem__,('Fuc+aldi','dHex+aldi'))) c['Pent'] = sum(map(c.__getitem__,('Xyl','Pent'))) c['Sia'] = sum(map(c.__getitem__,('NeuAc','NeuGc','Sia'))) c['HexA'] = sum(map(c.__getitem__,('GlcA','GalA','IdoA','ManA','HexA'))) c['HexN'] = sum(map(c.__getitem__,('GlcN','GalN','ManN','HexN'))) return c
def permethylated_molecular_weight(self,adduct='C2H6O'): return self.permethylated_elemental_composition().mass(elmt) + \ Composition.fromstr(adduct).mass(elmt)
def underivitized_molecular_weight(self,adduct='H2O'): return self.native_elemental_composition().mass(elmt) + \ Composition.fromstr(adduct).mass(elmt)
def elemental_composition(self,comp_table): eltcomp = Composition() for m in self.all_nodes(undet_subst=True): ec = m.composition(comp_table) eltcomp.add(ec) return eltcomp
def __init__(self,comp): self.table = comp self.eltcomp = Composition()