def updateSpecies(species, referenceMolecule): flag = False #print '--',str(species),str(referenceMolecule) for moleculeStructure in species.molecules: if moleculeStructure.name == referenceMolecule.name: for component in referenceMolecule.components: count = [x.name for x in referenceMolecule.components ].count(component.name) count -= [x.name for x in moleculeStructure.components ].count(component.name) newComponent = st.Component(component.name) if len(component.states) > 0: newComponent.addState('0') if count > 0: for _ in range(0, count): moleculeStructure.addComponent(deepcopy(newComponent)) elif count < 0: for _ in range(0, -count): referenceMolecule.addComponent(deepcopy(newComponent)) flag = True elif count == 0: localComponents = [ x for x in moleculeStructure.components if x.name == component.name ] referenceComponents = [ x for x in referenceMolecule.components if x.name == component.name ] if [x.states for x in localComponents ] != [x.states for x in referenceComponents]: for lc in localComponents: for rc in referenceComponents: for ls in lc.states: if ls not in rc.states: rc.addState(ls, update=False) for rs in rc.states: if rs not in lc.states: lc.addState(rs, update=False) for component in moleculeStructure.components: count = [x.name for x in referenceMolecule.components ].count(component.name) count -= [x.name for x in moleculeStructure.components ].count(component.name) newComponent = st.Component(component.name) if len(component.states) > 0: newComponent.addState(component.states[0]) newComponent.addState('0') if count > 0: for idx in range(0, count): moleculeStructure.addComponent(deepcopy(newComponent)) elif count < 0: for idx in range(0, -count): referenceMolecule.addComponent(deepcopy(newComponent)) flag = True return flag
def makeUnboundPattern(mol,comp): '''Make unbound pattern from Molecule Component ''' c = st.Component(comp.name,comp.idx,[],[]) m = st.Molecule(mol.name,mol.idx) m.addComponent(c) sp = st.Species() sp.addMolecule(m) return sp
def makeBondPattern(mc1,mc2): '''Make bond pattern from [Molecule Component], [Molecule Component] ''' [m1,c1] = mc1 [m2,c2] = mc2 c_1 = st.Component(c1.name,c1.idx,[],[]) c_1.addBond('1') m_1 = st.Molecule(m1.name, m1.idx) m_1.addComponent(c_1) c_2 = st.Component(c2.name,c2.idx,[],[]) c_2.addBond('1') m_2 = st.Molecule(m2.name, m2.idx) m_2.addComponent(c_2) sp = st.Species() sp.addMolecule(m_1) sp.addMolecule(m_2) return sp
def makeStatePattern(mol,comp,state): '''Make state pattern from Molecule Component State ''' c = st.Component(comp.name,comp.idx,[],[]) c.addState(state) c.setActiveState(state) m = st.Molecule(mol.name,mol.idx) m.addComponent(c) sp = st.Species() sp.addMolecule(m) return sp
def parseMolecules(molecules): ''' Parses an XML molecule section Returns: a molecule structure ''' mol = st.Molecule(molecules.get('name'), molecules.get('id')) components = \ molecules.find('.//{http://www.sbml.org/sbml/level3}ListOfComponentTypes') if components != None: for component in components.getchildren(): comp = st.Component(component.get('name'), component.get('id')) mol.addComponent(comp) return mol
def merge(reactant1, reactant2, r1, r2, translator, outputFlag=False): ''' Receives two species reactant1 and reactant1, and their intersection points r1 and r2 and creates a new complex that is the union ''' species = st.Species() if reactant1 in translator: species.append(translator[reactant1]) if reactant2 in translator: species.append(translator[reactant2]) if outputFlag: print '-----------', species, reactant1, reactant2, reactant2 in translator rnd = max(species.getBondNumbers()) + 1 molecule1 = st.Molecule(binding1) molecule2 = st.Molecule(binding2) component1 = st.Component(r1) component2 = st.Component(r2) component1.addBond(str(rnd)) component2.addBond(str(rnd)) molecule1.addComponent(component1) molecule2.addComponent(component2) species.addMolecule(molecule1, True, 1) counter = 2 if binding1 == binding2 else 1 species.addMolecule(molecule2, True, counter) ####TODO: update the rawDAtabase with the m1m2 information if molecule1.name in translator: sp = st.Species() sp.addMolecule(deepcopy(molecule1)) translator[molecule1.name].extend(sp) translator[molecule1.name].reset() if molecule2.name in translator: sp = st.Species() sp.addMolecule(deepcopy(molecule2)) translator[molecule2.name].extend(sp) translator[molecule2.name].reset()
def createMolecule(molecule, bonds): nameDict = {} mol = st.Molecule(molecule.get('name'), molecule.get('id')) nameDict[molecule.get('id')] = molecule.get('name') listOfComponents = molecule.find( './/{http://www.sbml.org/sbml/level3}ListOfComponents') if listOfComponents != None: for element in listOfComponents: component = st.Component(element.get('name'), element.get('id')) nameDict[element.get('id')] = element.get('name') if element.get('numberOfBonds') in ['+', '?']: component.addBond(element.get('numberOfBonds')) elif element.get('numberOfBonds') != '0': component.addBond(findBond(bonds, element.get('id'))) state = element.get( 'state') if element.get('state') != None else '' component.states.append(state) component.activeState = state mol.addComponent(component) return mol, nameDict
def addBondToComponent(species, moleculeName, componentName, bond, priority=1): order = 1 for molecule in species.molecules: if moleculeName == molecule.name: if priority == order or len([ x for x in species.molecules if x.name == moleculeName ]) == order: for component in molecule.components: if componentName == component.name: # if we are adding a second bond to the same component #it actually means that we have two components with the #same name if len(component.bonds) == 0: component.addBond(bond) return else: newComponent = st.Component(componentName) newComponent.addBond(bond) molecule.addComponent(newComponent) return else: order += 1
def catalysis(original, dictionary, rawDatabase, catalysisDatabase, translator, namingConvention, classification, reactionProperties): """ This method is for reactions of the form A+ B -> A' + B """ #if 'EGF_EGFRim2_GAP_Grb2_Sos_Ras_GDP' in original[0] or 'EGF_EGFRim2_GAP_Grb2_Sos_Ras_GDP' in original[1]: # print original,'EGF_EGFRim2_GAP_Grb2_Sos_Ras_GDP' in translator result = catalyze(namingConvention[0], namingConvention[1], classification, rawDatabase, translator, reactionProperties) k = [min(namingConvention, key=len) in x for x in original[0]] k2 = [max(namingConvention, key=len) in x for x in original[1]] k = k and k2 sortedResult = [result[0], result[1]] if any(k) else [result[1], result[0]] sortedConvention = [ namingConvention[0], namingConvention[1] ] if any(k) else [namingConvention[1], namingConvention[0]] flag = False if 'EGF_EGFRm2' in original[1]: print 'hello' for reactantGroup, res, conv in zip(original, sortedResult, sortedConvention): for reactant in reactantGroup: flag = False species = st.Species() #if original[0][0] in translator: # species = deepcopy(translator[original[0][0]]) #make a copy of the original element we are going to modify if reactant in translator: species = deepcopy(translator[reactant]) elif sortedConvention[0] in translator: species = deepcopy(translator[sortedConvention[0]]) tmp = dictionary[reactant] for element in tmp: molecule = st.Molecule(element) #here it would be much more precise to have the molecule #that is going to be modified instead of just modifying the #first thing you find if element in conv: #chunk = result[1] if reactant == max(namingConvention,key=len) else result[0] component = st.Component(res[0]) component.addState(res[1]) molecule.addComponent(component, 1) flag = True finalMolecule = molecule ''' else: if conv in reactant: component = st.Component(res[0]) component.addState(res[1]) molecule.addComponent(component,1) print conv,molecule,element flag = True #continue ''' #FIXME: the comparison should be done a lil more carefully #to avoid overlap species.addMolecule(molecule, True) if str(species) == '': species.addMolecule(molecule) break if flag: if reactant not in translator: translator[reactant] = species else: translator[reactant].extend(species, False) if finalMolecule.name in translator: if len(translator[finalMolecule.name].molecules) == 1: sp = st.Species() sp.addMolecule(deepcopy(finalMolecule)) translator[finalMolecule.name].extend(sp, False) translator[finalMolecule.name].reset() else: sp = st.Species() sp.addMolecule(molecule) translator[molecule.name] = deepcopy(sp) if len(original[0]) < len(original[1]): rebalance(original, sortedConvention, translator)
def getIntersection(reactants, product, dictionary, rawDatabase, translator, synthesisDatabase, originalProductName, outputFlag=False): ''' this method goes through two complexes and tries to check how they get together to create a product (e.g. how their components link) either by using previous knowledge or by creating a new complex ''' #global log extended1 = (copy(dictionary[reactants[0]])) extended2 = (copy(dictionary[reactants[1]])) if isinstance(extended1, str): extended1 = [extended1] if isinstance(extended2, str): extended2 = [extended2] #if we can find an element in the database that is a subset of #union(extended1,extended2) we take it intersection = findIntersection(extended1, extended2, synthesisDatabase) #otherwise we create it from scratch if not intersection: r1 = getFreeRadical(extended1, extended2[0], rawDatabase, translator, product, dictionary) r2 = getFreeRadical(extended2, extended1[0], rawDatabase, translator, product, dictionary) binding1, binding2 = getBindingPoints(extended1, extended2, reactants, originalProductName[0]) if not r1 or not r2: #prin t 'Cannot infer how',extended1,'binds to',extended2 #log['reactions'].append((reactants,product)) #return None,None,None #TODO this section should be activated by a flag instead #of being accessed by default #print extended1,extended2 createIntersection((binding1, binding2), rawDatabase, translator, dictionary) r1 = getFreeRadical((binding1, ), binding2, rawDatabase, translator, product, dictionary) r2 = getFreeRadical((binding2, ), binding1, rawDatabase, translator, product, dictionary) #print 'rrrrrrrrrrr',r1,r2 if not r1 or not r2: return (None, None, None, None) ##todo: modify code to allow for carry over free radicals #FIXME: we can remove synthesisDatabase easily species = st.Species() if reactants[0] in translator: species.append(translator[reactants[0]]) if reactants[1] in translator: species.append(translator[reactants[1]]) if outputFlag: print '-----------', species, reactants[0], reactants[ 1], reactants[1] in translator print '+++', binding1, binding2, r1, r2 bondName = max(species.getBondNumbers()) + 1 molecule1 = st.Molecule(binding1) molecule2 = st.Molecule(binding2) component1 = st.Component(r1) component2 = st.Component(r2) component1.addBond(str(bondName)) component2.addBond(str(bondName)) molecule1.addComponent(component1) molecule2.addComponent(component2) if outputFlag: print '////////', molecule1, molecule2 species.addMolecule(molecule1, True, 1) counter = 2 if binding1 == binding2 else 1 species.addMolecule(molecule2, True, counter) if outputFlag: print '\\\\\\', species ####TODO: update the rawDAtabase with the m1m2 information sp = st.Species() sp.addMolecule(deepcopy(molecule1)) if molecule1.name in translator: translator[molecule1.name].extend(sp) else: translator[molecule1.name] = sp translator[molecule1.name].reset() sp = st.Species() sp.addMolecule(deepcopy(molecule2)) if molecule2.name in translator: translator[molecule2.name].extend(sp) else: translator[molecule2.name] = sp translator[molecule2.name].reset() if outputFlag: print '||||||||||||||||||||||', translator[molecule2.name] #print reactants,product,str(species) #print name1,name2,extended1,extended2 #print {x:str(translator[x]) for x in translator}, translator if len(species.molecules) == 0: return (None, None, None, None) return species, [], [], [] return extended1, extended2, intersection, []
def createBindingRBM(element, translator, dependencyGraph, bioGridFlag): species = st.Species() #go over the sct and reuse existing stuff for molecule in dependencyGraph[element[0]][0]: if molecule in translator: tmpSpecies = translator[molecule] if molecule != getTrueTag(dependencyGraph, molecule): original = translator[getTrueTag(dependencyGraph, molecule)] updateSpecies(tmpSpecies, original.molecules[0]) species.addMolecule(deepcopy(tmpSpecies.molecules[0])) else: mol = st.Molecule(molecule) dependencyGraph[molecule] = deepcopy(mol) species.addMolecule(mol) #how do things bind together? moleculePairsList = getComplexationComponents2(species, bioGridFlag) #TODO: update basic molecules with new components #translator[molecule[0].name].molecules[0].components.append(deepcopy(newComponent1)) #translator[molecule[1].name].molecules[0].components.append(deepcopy(newComponent2)) for idx, molecule in enumerate(moleculePairsList): flag = False #add bonds where binding components already exist for component in molecule[0].components: if component.name == molecule[1].name.lower() and \ len(component.bonds) == 0: component.bonds.append(idx) flag = True break if not flag: #create components if they dont exist already. #Add a bond afterwards newComponent1 = st.Component(molecule[1].name.lower()) molecule[0].components.append(newComponent1) if newComponent1.name not in [x.name for x in translator[molecule[0].name].molecules[0]. \ components]: translator[molecule[0].name].molecules[0]. \ components.append(deepcopy(newComponent1)) molecule[0].components[-1].bonds.append(idx) flag = False #same thing for the other member of the bond for component in molecule[1].components: if component.name == molecule[0].name.lower() and len( component.bonds) == 0: component.bonds.append(idx) flag = True break if not flag: newComponent2 = st.Component(molecule[0].name.lower()) molecule[1].components.append(newComponent2) if molecule[0].name != molecule[1].name: if newComponent2.name not in [x.name for x in translator[molecule[0].name].molecules[0]. \ components]: translator[ molecule[1].name].molecules[0].components.append( deepcopy(newComponent2)) molecule[1].components[-1].bonds.append(idx) #update the translator translator[element[0]] = species
def addComponentToMolecule(species, moleculeName, componentName): for molecule in species.molecules: if moleculeName == molecule.name: if componentName not in [x.name for x in molecule.components]: component = st.Component(componentName) molecule.addComponent(component)