def checkComplexIsDefault(self, binding, complex):
     try:
         molName, bindingSiteName = binding
     except:
         pdb.set_trace()
         a = 1
     if "." in complex:
         return False
     if not complex.startswith(molName):
         pdb.set_trace()
         raise ValueError, "Bad complex"
     if "!" in complex:
         return False
     theBindingSites = util.getBindingSites(complex)
     if len(theBindingSites) != 1:
         return False
     for modSite in util.getModificationList(complex):
         modSite = modSite.split("~")
         if modSite[1] != "none":
             return False
     else:
         return True
示例#2
0
 def checkComplexIsDefault(self, binding, complex):
     try:
         molName, bindingSiteName = binding
     except:
         pdb.set_trace()
         a = 1
     if "." in complex:
         return False
     if not complex.startswith(molName):
         pdb.set_trace()
         raise ValueError, "Bad complex"
     if "!" in complex:
         return False
     theBindingSites = util.getBindingSites(complex)
     if len(theBindingSites) != 1:
         return False
     for modSite in util.getModificationList(complex):
         modSite = modSite.split("~")
         if modSite[1] != "none":
             return False
     else:
         return True
    def addAllostericOmnis(self, allostericOmnisElement):
        # pdb.set_trace()
        for bindingWithAllostery in self.allosteryMapping.keys():
            owningMolName, allostericBindingSiteName = bindingWithAllostery
            complexAllosteryPairs = [
                (key, self.allosteryMapping[bindingWithAllostery][key])
                for key in self.allosteryMapping[bindingWithAllostery].keys()
            ]
            for complex_allostery_pair in complexAllosteryPairs:
                complex, allostericName = complex_allostery_pair
                if allostericName == "default":
                    continue

                allostericOmniElmt = XmlObject("allosteric-omni")
                allostericOmnisElement.addSubElement(allostericOmniElmt)

                # Now prepare the four sub-elements
                plexElmt = XmlObject("plex")
                instanceStatesElmt = XmlObject("instance-states")
                unboundSitesElmt = XmlObject("unbound-sites")
                boundSitesElmt = XmlObject("bound-sites")
                allostericStatesElmt = XmlObject("allosteric-sites")

                # First we construct the molRefToInstanceDict
                molRefToInstanceDict = {}
                tmpMolInstanceInt = {}

                molArray = complex.split(".")

                for molNdx in xrange(len(molArray)):
                    mol = molArray[molNdx]
                    parenNdx = mol.find("(")
                    molType = mol[:parenNdx]

                    if molType not in tmpMolInstanceInt:
                        tmpMolInstanceInt[molType] = 1
                        molInstance = "the-" + molType
                    else:
                        molInstance = molType + "-" + str(tmpMolInstanceInt[molType])
                        tmpMolInstanceInt[molType] = tmpMolInstanceInt[molType] + 1

                    molRefToInstanceDict[molInstance] = molType
                    molArray[molNdx] = molInstance + mol[parenNdx:]

                # So now we calculate -- the plex.
                bindingDict = {}
                genericBoundSitesArray = []
                unboundSitesArray = []
                modificationSitesArray = []
                for molSpec in molArray:
                    # Process the molNames
                    molInstance = util.getMolNameFromFullMolSpec(molSpec)
                    molInstanceElmt = XmlObject("mol-instance")
                    molInstanceElmt.addAttribute("name", molInstance)
                    molRefElmt = XmlObject("mol-ref")
                    molRefElmt.addAttribute("name", molRefToInstanceDict[molInstance])
                    molRefElmt.attachToParent(molInstanceElmt).attachToParent(plexElmt)

                    # Collect the bindings
                    bindingSites = util.getBindingSites(molSpec)

                    explicitlyBoundBindingSites = [
                        x.split("!") for x in bindingSites if ("!" in x and x[-1] != "!" and x[-1] != "+")
                    ]
                    boundBindingSites = [
                        x.split("!") for x in bindingSites if ("!" in x and x[-1] != "!" and x[-1] == "+")
                    ]
                    unboundBindingSites = [x for x in bindingSites if "!" not in x]

                    modificationSites = [x.split("~") for x in util.getModificationList(molSpec)]

                    for halfBinding in explicitlyBoundBindingSites:
                        bindingSite, bindingNdx = halfBinding[0], int(halfBinding[1])
                        if not bindingNdx in bindingDict:
                            bindingDict[bindingNdx] = []
                        bindingDict[bindingNdx].append((molInstance, bindingSite))

                    # Collect the unbound binding sites.
                    for unboundBindingSite in unboundBindingSites:
                        unboundSitesArray.append((molInstance, unboundBindingSite))

                    for boundSite in boundBindingSites:
                        genericBoundSitesArray.append((molInstance, boundSite[0]))

                    for modificationSite in modificationSites:
                        modificationSitesArray.append((molInstance, modificationSite[0], modificationSite[1]))

                # Do the bindings
                listOfBindings = [bindingDict[key] for key in bindingDict.keys()]

                for binding in listOfBindings:
                    try:
                        assert len(binding) == 2
                    except:
                        pdb.set_trace()
                        a = 1
                for binding in listOfBindings:
                    bindingElmt = XmlObject("binding")
                    bindingElmt.attachToParent(plexElmt)
                    for halfBinding in binding:
                        molInstance, bindingRef = halfBinding
                        molInstanceRefElmt = XmlObject("mol-instance-ref")
                        molInstanceRefElmt.addAttribute("name", molInstance)
                        bindingSiteRefElmt = XmlObject("binding-site-ref")
                        bindingSiteRefElmt.addAttribute("name", bindingRef)
                        bindingSiteRefElmt.attachToParent(molInstanceRefElmt).attachToParent(bindingElmt)

                # Now we add in the modificationSitesArray
                for modMolName in set([x[0] for x in modificationSitesArray]):
                    modMolModifications = [x for x in modificationSitesArray if x[0] == modMolName]
                    modMolInstanceRefElmt = XmlObject("mod-mol-instance-ref")
                    modMolInstanceRefElmt.attachToParent(instanceStatesElmt)
                    modMolInstanceRefElmt.addAttribute("name", modMolName)
                    modMapElmt = XmlObject("mod-map")
                    modMapElmt.attachToParent(modMolInstanceRefElmt)
                    for modification in modMolModifications:
                        modSiteRefElmt = XmlObject("mod-site-ref")
                        modSiteRefElmt.addAttribute("name", modification[1])
                        modRefElmt = XmlObject("mod-ref")
                        modRefElmt.addAttribute("name", modification[2])
                        modRefElmt.attachToParent(modSiteRefElmt).attachToParent(modMapElmt)

                # Now we add in the unboundSitesArray
                for molWithUnboundBindings in set([x[0] for x in unboundSitesArray]):
                    unboundSites = [x for x in unboundSitesArray if x[0] == molWithUnboundBindings]
                    for site in unboundSites:
                        instanceRefElmt = XmlObject("instance-ref")
                        instanceRefElmt.addAttribute("name", molWithUnboundBindings)
                        siteRefElmt = XmlObject("site-ref")
                        siteRefElmt.addAttribute("name", site[1])
                        siteRefElmt.attachToParent(instanceRefElmt).attachToParent(unboundSitesElmt)

                # Now we add in the boundSitesArray
                for molWithGenericBoundBindings in set([x[0] for x in genericBoundSitesArray]):
                    genericBoundSites = [x for x in genericBoundSitesArray if x[0] == molWithGenericBoundBindings]
                    for site in genericBoundSites:
                        instanceRefElmt = XmlObject("instance-ref")
                        instanceRefElmt.addAttribute("name", molWithGenericBoundBindings)
                        siteRefElmt = XmlObject("site-ref")
                        siteRefElmt.addAttribute("name", site[1])
                        siteRefElmt.attachToParent(instanceRefElmt).attachToParent(boundSitesElmt)

                molInstanceRefElmt = XmlObject("mol-instance-ref")
                molInstanceRefElmt.addAttribute("name", "the-" + owningMolName)
                bindingSiteRefElmt = XmlObject("binding-site-ref")
                bindingSiteRefElmt.addAttribute("name", allostericBindingSiteName)
                siteShapeRefElmt = XmlObject("site-shape-ref")
                siteShapeRefElmt.addAttribute("name", allostericName)
                siteShapeRefElmt.attachToParent(bindingSiteRefElmt).attachToParent(molInstanceRefElmt).attachToParent(
                    allostericStatesElmt
                )

                ## End of function
                plexElmt.attachToParent(allostericOmniElmt)
                if instanceStatesElmt.getNumberOfChildren() > 0:
                    instanceStatesElmt.attachToParent(allostericOmniElmt)
                if unboundSitesElmt.getNumberOfChildren() > 0:
                    unboundSitesElmt.attachToParent(allostericOmniElmt)
                if boundSitesElmt.getNumberOfChildren():
                    boundSitesElmt.attachToParent(allostericOmniElmt)
                allostericStatesElmt.attachToParent(allostericOmniElmt)
示例#4
0
    def addModificationRxnToReactionGenElement(self, rxn, reactionGensElement,
                                               modificationInfo):

        # Create all the objects
        omniGenElmt = XmlObject("omni-gen")
        enablingOmniPlexElmt = XmlObject("enabling-omniplex")
        smallMolExchangesElmt = XmlObject("small-mol-exchanges")
        modificationExchangeElmt = XmlObject("modification-exchanges")
        additionalReactantSpeciesElmt = XmlObject(
            "additional-reactant-species")
        additionalProductSpeciesElmt = XmlObject("additional-product-species")
        rateElmt = XmlObject("rate")

        # Now add them all to the xml structure

        omniGenElmt.attachToParent(reactionGensElement)
        enablingOmniPlexElmt.attachToParent(omniGenElmt)
        smallMolExchangesElmt.attachToParent(omniGenElmt)
        modificationExchangeElmt.attachToParent(omniGenElmt)
        #additionalReactantSpeciesElmt.attachToParent(omniGenElmt)
        #additionalProductSpeciesElmt.attachToParent(omniGenElmt)

        # Add a plex element to the enablingOmniPlexElmt
        plexElmt = XmlObject("plex")
        plexElmt.attachToParent(enablingOmniPlexElmt)
        assert (len(rxn.reactants) == 1 and len(rxn.products) == 1)
        reactant = rxn.reactants[0]
        reactantArray = reactant.split(".")

        molNdxToInstanceName = {}
        molNdxToRefName = {}
        tmpNameGenerator = {}
        bindingSites = {}
        unboundSites = []
        modificationSites = []
        for ndx in range(len(reactantArray)):
            molSpec = reactantArray[ndx]
            parenNdx = molSpec.find("(")
            molRefName = molSpec[:parenNdx]
            if not molRefName in tmpNameGenerator.keys():
                molInstanceName = "the-" + molRefName
                tmpNameGenerator[molRefName] = 1
            else:
                molInstanceName = molRefName + "_" + str(
                    tmpNameGenerator[molRefName])
                tmpNameGenerator[molRefName] += 1

            molNdxToRefName[ndx] = molRefName
            molNdxToInstanceName[ndx] = molInstanceName

            # Create the mol-intance element
            molInstanceElmt = XmlObject("mol-instance")
            molInstanceElmt.addAttribute("name", molInstanceName)
            molRefElmt = XmlObject("mol-ref")
            molRefElmt.addAttribute("name", molRefName)
            molRefElmt.attachToParent(molInstanceElmt).attachToParent(plexElmt)

            # Now delve into the bindings of this muthafucka.
            # Note bound components, unbound componends, and modifications...

            bindingInformation = molSpec[parenNdx + 1:-1]
            bindingInfoArray = bindingInformation.split(",")

            bindings = [x for x in bindingInfoArray if "~" not in x]
            modifications = [x for x in bindingInfoArray if "~" in x]
            boundBindings = [x for x in bindings if "!" in x and x[-1] != "+"]
            unboundBindings = [x for x in bindings if "!" not in x]

            # Process modifications
            for modification in modifications:
                site, state = modification.split("~")
                modificationSites.append((ndx, site, state))

            # Process unbound binding sites
            for unboundBinding in unboundBindings:
                unboundSites.append((ndx, unboundBinding))

            for boundHalfBinding in boundBindings:
                site, bindingNdx = boundHalfBinding.split("!")
                if not bindingNdx in bindingSites.keys():
                    bindingSites[bindingNdx] = []
                bindingSites[bindingNdx].append((ndx, site))

        # End initial pass, now we can put in all the bindingSites,
        # unbound bindingSites, and modification states.

        for ndx in bindingSites.keys():
            bindingElmt = XmlObject("binding")
            bindingElmt.attachToParent(plexElmt)
            for halfBinding in bindingSites[ndx]:
                molInstanceRefElmt = XmlObject("mol-instance-ref")
                molInstanceRefElmt.addAttribute(
                    "name", molNdxToInstanceName[halfBinding[0]])

                bindingSiteRefElmt = XmlObject("binding-site-ref")
                bindingSiteRefElmt.addAttribute("name", halfBinding[1])

                bindingSiteRefElmt.attachToParent(
                    molInstanceRefElmt).attachToParent(bindingElmt)

        # I think I should try to filter out the modification(the relevant one)
        if len(modificationSites) > 0:
            instanceStatesElmt = XmlObject("instance-states")
            instanceStatesElmt.attachToParent(plexElmt)

        for modMolNdx in set([x[0] for x in modificationSites]):
            modMap = [(x[1], x[2]) for x in modificationSites
                      if x[0] == modMolNdx]

            modMolInstanceRefElmt = XmlObject("mod-mol-instance-ref")
            modMolInstanceRefElmt.addAttribute("name",
                                               molNdxToInstanceName[modMolNdx])

            modMapElmt = XmlObject("mod-map")
            modMapElmt.attachToParent(modMolInstanceRefElmt).attachToParent(
                instanceStatesElmt)

            for modification in modMap:
                modSiteRefElmt = XmlObject("mod-site-ref")
                modSiteRefElmt.addAttribute("name", modification[0])

                modRefElmt = XmlObject("mod-ref")
                modRefElmt.addAttribute("name", modification[1])
                modRefElmt.attachToParent(modSiteRefElmt).attachToParent(
                    modMapElmt)

        if len(unboundSites) > 0:
            unboundSitesElmt = XmlObject("unbound-sites")
            unboundSitesElmt.attachToParent(plexElmt)
        for unboundSite in unboundSites:
            instanceRefElmt = XmlObject("instance-ref")
            instanceRefElmt.addAttribute("name",
                                         molNdxToInstanceName[unboundSite[0]])

            siteRefElmt = XmlObject("site-ref")
            siteRefElmt.addAttribute("name", unboundSite[1])
            siteRefElmt.attachToParent(instanceRefElmt).attachToParent(
                unboundSitesElmt)

        ## Now create the modification exchange
        molRef, modSite, modInitialState = modificationInfo
        realNdx = -1
        for ndx in range(len(reactantArray)):
            if reactantArray[ndx].startswith(molRef) and (
                    modSite + "~" + modInitialState) in reactantArray[ndx]:
                realNdx = ndx
                break
        else:
            pdb.set_trace()
            a = 1
            raise ValueError

        modMolInstanceRefElmt = XmlObject("mod-mol-instance-ref")
        modMolInstanceRefElmt.addAttribute("name",
                                           molNdxToInstanceName[realNdx])

        modSiteRefElmt = XmlObject("mod-site-ref")
        modSiteRefElmt.addAttribute("name", modSite)

        modSiteRefElmt.attachToParent(modMolInstanceRefElmt).attachToParent(
            modificationExchangeElmt)

        installedModRefElmt = XmlObject("installed-mod-ref")
        # To find the result, we must find out what state the modSite modification is in the realNdx-th plex on the product side
        modifications = util.getModificationList(
            rxn.products[0].split('.')[realNdx])
        endState = ""
        for x in modifications:
            if x.startswith(modSite):
                endState = x.split('~')[1]
                break
        else:
            raise ValueError

        installedModRefElmt.addAttribute("name", endState)
        installedModRefElmt.attachToParent(modificationExchangeElmt)
        rateElmt.addAttribute(
            "value",
            self.parameterDictionary.evaluateExpression(rxn.rateList[0]))
        rateElmt.attachToParent(omniGenElmt)
    def addModificationRxnToReactionGenElement(self, rxn, reactionGensElement, modificationInfo):

        # Create all the objects
        omniGenElmt = XmlObject("omni-gen")
        enablingOmniPlexElmt = XmlObject("enabling-omniplex")
        smallMolExchangesElmt = XmlObject("small-mol-exchanges")
        modificationExchangeElmt = XmlObject("modification-exchanges")
        additionalReactantSpeciesElmt = XmlObject("additional-reactant-species")
        additionalProductSpeciesElmt = XmlObject("additional-product-species")
        rateElmt = XmlObject("rate")

        # Now add them all to the xml structure

        omniGenElmt.attachToParent(reactionGensElement)
        enablingOmniPlexElmt.attachToParent(omniGenElmt)
        smallMolExchangesElmt.attachToParent(omniGenElmt)
        modificationExchangeElmt.attachToParent(omniGenElmt)
        #additionalReactantSpeciesElmt.attachToParent(omniGenElmt)
        #additionalProductSpeciesElmt.attachToParent(omniGenElmt)

        # Add a plex element to the enablingOmniPlexElmt
        plexElmt = XmlObject("plex")
        plexElmt.attachToParent(enablingOmniPlexElmt)
        assert(len(rxn.reactants) == 1 and len(rxn.products) == 1)
        reactant = rxn.reactants[0]
        reactantArray = reactant.split(".")

        molNdxToInstanceName = {}
        molNdxToRefName = {}
        tmpNameGenerator = {}
        bindingSites = {}
        unboundSites = []
        modificationSites = []
        for ndx in range(len(reactantArray)):
            molSpec = reactantArray[ndx]
            parenNdx = molSpec.find("(")
            molRefName = molSpec[:parenNdx]
            if not molRefName in tmpNameGenerator.keys():
                molInstanceName = "the-" + molRefName
                tmpNameGenerator[molRefName] = 1
            else:
                molInstanceName = molRefName + "_" + str(tmpNameGenerator[molRefName])
                tmpNameGenerator[molRefName] += 1

            molNdxToRefName[ndx] = molRefName
            molNdxToInstanceName[ndx] = molInstanceName

            # Create the mol-intance element
            molInstanceElmt = XmlObject("mol-instance")
            molInstanceElmt.addAttribute("name", molInstanceName)
            molRefElmt = XmlObject("mol-ref")
            molRefElmt.addAttribute("name", molRefName)
            molRefElmt.attachToParent(molInstanceElmt).attachToParent(plexElmt)
            
            # Now delve into the bindings of this muthafucka.
            # Note bound components, unbound componends, and modifications...

            bindingInformation = molSpec[parenNdx + 1: -1]
            bindingInfoArray = bindingInformation.split(",")

            bindings = [x for x in bindingInfoArray if "~" not in x]
            modifications = [x for x in bindingInfoArray if "~" in x]
            boundBindings = [x for x in bindings if "!" in x and x[-1] != "+"]
            unboundBindings = [x for x in bindings if "!" not in x]

            # Process modifications
            for modification in modifications:
                site, state = modification.split("~")
                modificationSites.append( (ndx, site, state))

            # Process unbound binding sites
            for unboundBinding in unboundBindings:
                unboundSites.append( (ndx, unboundBinding))

            for boundHalfBinding in boundBindings:
                site, bindingNdx = boundHalfBinding.split("!")
                if not bindingNdx in bindingSites.keys():
                    bindingSites[bindingNdx] = []
                bindingSites[bindingNdx].append( (ndx, site))
                
        # End initial pass, now we can put in all the bindingSites,
        # unbound bindingSites, and modification states.

        for ndx in bindingSites.keys():
            bindingElmt = XmlObject("binding")
            bindingElmt.attachToParent(plexElmt)
            for halfBinding in bindingSites[ndx]:
                molInstanceRefElmt = XmlObject("mol-instance-ref")
                molInstanceRefElmt.addAttribute("name", molNdxToInstanceName[halfBinding[0]])

                bindingSiteRefElmt = XmlObject("binding-site-ref")
                bindingSiteRefElmt.addAttribute("name", halfBinding[1])

                bindingSiteRefElmt.attachToParent(molInstanceRefElmt).attachToParent(bindingElmt)

        # I think I should try to filter out the modification(the relevant one)
        if len(modificationSites) > 0:
            instanceStatesElmt = XmlObject("instance-states")
            instanceStatesElmt.attachToParent(plexElmt)
            
        for modMolNdx in set([x[0] for x in modificationSites]):
            modMap = [ (x[1], x[2]) for x in modificationSites if x[0] == modMolNdx]

            modMolInstanceRefElmt = XmlObject("mod-mol-instance-ref")
            modMolInstanceRefElmt.addAttribute("name", molNdxToInstanceName[modMolNdx])

            modMapElmt = XmlObject("mod-map")
            modMapElmt.attachToParent(modMolInstanceRefElmt).attachToParent(instanceStatesElmt)

            for modification in modMap:
                modSiteRefElmt = XmlObject("mod-site-ref")
                modSiteRefElmt.addAttribute("name", modification[0])

                modRefElmt = XmlObject("mod-ref")
                modRefElmt.addAttribute("name", modification[1])
                modRefElmt.attachToParent(modSiteRefElmt).attachToParent(modMapElmt)

        if len(unboundSites) > 0:
            unboundSitesElmt = XmlObject("unbound-sites")
            unboundSitesElmt.attachToParent(plexElmt)
        for unboundSite in unboundSites:
            instanceRefElmt = XmlObject("instance-ref")
            instanceRefElmt.addAttribute("name", molNdxToInstanceName[unboundSite[0]])

            siteRefElmt = XmlObject("site-ref")
            siteRefElmt.addAttribute("name", unboundSite[1])
            siteRefElmt.attachToParent(instanceRefElmt).attachToParent(unboundSitesElmt)


        ## Now create the modification exchange
        molRef, modSite, modInitialState = modificationInfo
        realNdx = -1
        for ndx in range(len(reactantArray)):
            if reactantArray[ndx].startswith(molRef) and (modSite + "~" + modInitialState) in reactantArray[ndx]:
                realNdx = ndx
                break
        else:
            pdb.set_trace()
            a = 1
            raise ValueError
        
        modMolInstanceRefElmt = XmlObject("mod-mol-instance-ref")
        modMolInstanceRefElmt.addAttribute("name", molNdxToInstanceName[realNdx])

        modSiteRefElmt = XmlObject("mod-site-ref")
        modSiteRefElmt.addAttribute("name", modSite)

        modSiteRefElmt.attachToParent(modMolInstanceRefElmt).attachToParent(modificationExchangeElmt)

        installedModRefElmt = XmlObject("installed-mod-ref")
        # To find the result, we must find out what state the modSite modification is in the realNdx-th plex on the product side
        modifications = util.getModificationList(rxn.products[0].split('.')[realNdx])
        endState = ""
        for x in modifications:
            if x.startswith(modSite):
                endState = x.split('~')[1]
                break
        else:
            raise ValueError

        installedModRefElmt.addAttribute("name", endState)
        installedModRefElmt.attachToParent(modificationExchangeElmt)
        rateElmt.addAttribute("value", self.parameterDictionary.evaluateExpression(rxn.rateList[0]))
        rateElmt.attachToParent(omniGenElmt)
示例#6
0
    def addAllostericOmnis(self, allostericOmnisElement):
        # pdb.set_trace()
        for bindingWithAllostery in self.allosteryMapping.keys():
            owningMolName, allostericBindingSiteName = bindingWithAllostery
            complexAllosteryPairs = [(key, self.allosteryMapping[bindingWithAllostery][key]) for key in self.allosteryMapping[bindingWithAllostery].keys()]
            for complex_allostery_pair in complexAllosteryPairs:
                complex, allostericName = complex_allostery_pair
                if allostericName == "default":
                    continue

                allostericOmniElmt = XmlObject("allosteric-omni")
                allostericOmnisElement.addSubElement(allostericOmniElmt)

                # Now prepare the four sub-elements
                plexElmt = XmlObject("plex")
                instanceStatesElmt = XmlObject("instance-states")
                unboundSitesElmt = XmlObject("unbound-sites")
                boundSitesElmt = XmlObject("bound-sites")
                allostericStatesElmt = XmlObject("allosteric-sites")

                # First we construct the molRefToInstanceDict
                molRefToInstanceDict = {}
                tmpMolInstanceInt = {}

                molArray = complex.split('.')

                for molNdx in xrange(len(molArray)):
                    mol = molArray[molNdx]
                    parenNdx = mol.find("(")
                    molType = mol[:parenNdx]

                    if molType not in tmpMolInstanceInt:
                        tmpMolInstanceInt[molType] = 1
                        molInstance = "the-" + molType
                    else:
                        molInstance = molType + "-" + str(tmpMolInstanceInt[molType])
                        tmpMolInstanceInt[molType] = tmpMolInstanceInt[molType] + 1

                    molRefToInstanceDict[molInstance] = molType
                    molArray[molNdx] = molInstance + mol[parenNdx:]

                # So now we calculate -- the plex.
                bindingDict = {}
                genericBoundSitesArray = []
                unboundSitesArray = []
                modificationSitesArray = []
                for molSpec in molArray:
                    # Process the molNames
                    molInstance = util.getMolNameFromFullMolSpec(molSpec)
                    molInstanceElmt = XmlObject("mol-instance")
                    molInstanceElmt.addAttribute("name", molInstance)
                    molRefElmt = XmlObject("mol-ref")
                    molRefElmt.addAttribute("name", molRefToInstanceDict[molInstance])
                    molRefElmt.attachToParent(molInstanceElmt).attachToParent(plexElmt)

                    # Collect the bindings
                    bindingSites = util.getBindingSites(molSpec)

                    explicitlyBoundBindingSites = [x.split("!") for x in bindingSites if ("!" in x and x[-1] != "!" and x[-1] != "+")]
                    boundBindingSites = [x.split("!") for x in bindingSites if ("!" in x and x[-1] != "!" and x[-1] == "+")]
                    unboundBindingSites = [x for x in bindingSites if "!" not in x]

                    modificationSites = [x.split("~") for x in util.getModificationList(molSpec)]

                    for halfBinding in explicitlyBoundBindingSites:
                        bindingSite, bindingNdx = halfBinding[0], int(halfBinding[1])
                        if not bindingNdx in bindingDict:
                            bindingDict[bindingNdx] = []
                        bindingDict[bindingNdx].append( (molInstance, bindingSite))

                    # Collect the unbound binding sites.
                    for unboundBindingSite in unboundBindingSites:
                        unboundSitesArray.append( (molInstance, unboundBindingSite) )

                    for boundSite in boundBindingSites:
                        genericBoundSitesArray.append( (molInstance, boundSite[0]))

                    for modificationSite in modificationSites:
                        modificationSitesArray.append( (molInstance, modificationSite[0], modificationSite[1]))

                # Do the bindings
                listOfBindings = [ bindingDict[key] for key in bindingDict.keys()]

                for binding in listOfBindings:
                    try:
                        assert(len(binding)==2)
                    except:
                        pdb.set_trace()
                        a = 1
                for binding in listOfBindings:
                    bindingElmt = XmlObject("binding")
                    bindingElmt.attachToParent(plexElmt)
                    for halfBinding in binding:
                        molInstance, bindingRef = halfBinding
                        molInstanceRefElmt = XmlObject("mol-instance-ref")
                        molInstanceRefElmt.addAttribute("name", molInstance)
                        bindingSiteRefElmt = XmlObject("binding-site-ref")
                        bindingSiteRefElmt.addAttribute("name", bindingRef)
                        bindingSiteRefElmt.attachToParent(molInstanceRefElmt).attachToParent(bindingElmt)

                # Now we add in the modificationSitesArray
                for modMolName in set( [ x[0] for x in modificationSitesArray]):
                    modMolModifications = [x for x in modificationSitesArray if x[0] == modMolName]
                    modMolInstanceRefElmt = XmlObject("mod-mol-instance-ref")
                    modMolInstanceRefElmt.attachToParent(instanceStatesElmt)
                    modMolInstanceRefElmt.addAttribute("name", modMolName)
                    modMapElmt = XmlObject("mod-map")
                    modMapElmt.attachToParent(modMolInstanceRefElmt)
                    for modification in modMolModifications:
                        modSiteRefElmt = XmlObject("mod-site-ref")
                        modSiteRefElmt.addAttribute("name", modification[1])
                        modRefElmt = XmlObject("mod-ref")
                        modRefElmt.addAttribute("name", modification[2])
                        modRefElmt.attachToParent(modSiteRefElmt).attachToParent(modMapElmt)

                # Now we add in the unboundSitesArray
                for molWithUnboundBindings in set( [ x[0] for x in unboundSitesArray]):
                    unboundSites = [x for x in unboundSitesArray if x[0] == molWithUnboundBindings]
                    for site in unboundSites:
                        instanceRefElmt = XmlObject("instance-ref")
                        instanceRefElmt.addAttribute("name", molWithUnboundBindings)
                        siteRefElmt = XmlObject("site-ref")
                        siteRefElmt.addAttribute("name", site[1])
                        siteRefElmt.attachToParent(instanceRefElmt).attachToParent(unboundSitesElmt)

                # Now we add in the boundSitesArray
                for molWithGenericBoundBindings in set([ x[0] for x in genericBoundSitesArray]):
                    genericBoundSites = [x for x in genericBoundSitesArray if x[0] == molWithGenericBoundBindings]
                    for site in genericBoundSites:
                        instanceRefElmt = XmlObject("instance-ref")
                        instanceRefElmt.addAttribute("name", molWithGenericBoundBindings)
                        siteRefElmt = XmlObject("site-ref")
                        siteRefElmt.addAttribute("name", site[1])
                        siteRefElmt.attachToParent(instanceRefElmt).attachToParent(boundSitesElmt)
                        


                molInstanceRefElmt = XmlObject("mol-instance-ref")
                molInstanceRefElmt.addAttribute("name", "the-" + owningMolName)
                bindingSiteRefElmt = XmlObject("binding-site-ref")
                bindingSiteRefElmt.addAttribute("name", allostericBindingSiteName)
                siteShapeRefElmt = XmlObject("site-shape-ref")
                siteShapeRefElmt.addAttribute("name", allostericName)
                siteShapeRefElmt.attachToParent(bindingSiteRefElmt).attachToParent(molInstanceRefElmt).attachToParent(allostericStatesElmt)

                ## End of function
                plexElmt.attachToParent(allostericOmniElmt)
                if instanceStatesElmt.getNumberOfChildren() > 0:
                    instanceStatesElmt.attachToParent(allostericOmniElmt)
                if unboundSitesElmt.getNumberOfChildren() > 0:
                    unboundSitesElmt.attachToParent(allostericOmniElmt)
                if boundSitesElmt.getNumberOfChildren():
                    boundSitesElmt.attachToParent(allostericOmniElmt)
                allostericStatesElmt.attachToParent(allostericOmniElmt)