示例#1
0
 def decode_complex_pattern(self, cp):
     cp_obj = ComplexPattern(
         [self.decode_monomer_pattern(mp) for mp in cp['monomer_patterns']],
         self._modelget(cp['compartment']),
         cp['match_once']
     )
     cp_obj._tag = self._modelget(cp['tag'])
     return cp_obj
示例#2
0
 def _parse_initials(self):
     for i in self._x.iterfind(_ns('{0}ListOfSpecies/{0}Species')):
         value_param = i.get('concentration')
         try:
             value = float(value_param)
             # Need to create a new parameter for the initial conc. literal
             name = re.sub('[^\w]+', '_',
                           i.get('name').replace(')', '').replace(
                               '(', '')) + '_0'
             try:
                 value_param = self.parameter(name, value)
             except ValueError as ve:
                 raise BnglImportError(str(ve))
         except ValueError:
             # Retrieve existing parameter or (constant) expression
             try:
                 value_param = self.model.parameters[i.get('concentration')]
             except KeyError:
                 value_param = self.model.expressions[i.get(
                     'concentration')]
         mon_pats = self._parse_species(i)
         species_cpt = self.model.compartments.get(i.get('compartment'))
         cp = ComplexPattern(mon_pats, species_cpt)
         fixed = i.get('Fixed') == "1"
         self.initial(cp, value_param, fixed)
示例#3
0
文件: bngl.py 项目: weilandtd/pysb
    def _parse_initials(self):
        for i in self._x.iterfind(_ns('{0}ListOfSpecies/{0}Species')):
            if i.get('Fixed') is not None and i.get('Fixed') == "1":
                self._warn_or_except('Species %s is fixed, but will be '
                                     'treated as an ordinary species in '
                                     'PySB.' % i.get('name'))

            value_param = i.get('concentration')
            try:
                value = float(value_param)
                # Need to create a new parameter for the initial conc. literal
                name = re.sub('[^\w]+', '_',
                              i.get('name').replace(')', '').replace(
                                  '(', '')) + '_0'
                try:
                    value_param = self.parameter(name, value)
                except ValueError as ve:
                    raise BnglImportError(str(ve))
            except ValueError:
                # Retrieve existing parameter or (constant) expression
                try:
                    value_param = self.model.parameters[i.get('concentration')]
                except KeyError:
                    value_param = self.model.expressions[i.get(
                        'concentration')]
            mon_pats = self._parse_species(i)
            species_cpt = self.model.compartments.get(i.get('compartment'))
            self.initial(ComplexPattern(mon_pats, species_cpt), value_param)
示例#4
0
文件: bngl.py 项目: weilandtd/pysb
 def _parse_observables(self):
     for o in self._x.iterfind(_ns('{0}ListOfObservables/{0}Observable')):
         o_name = o.get('name')
         cplx_pats = []
         for mp in o.iterfind(_ns('{0}ListOfPatterns/{0}Pattern')):
             match_once = mp.get('matchOnce')
             match_once = True if match_once == "1" else False
             cplx_pats.append(
                 ComplexPattern(self._parse_species(mp),
                                compartment=None,
                                match_once=match_once))
         self.observable(o_name,
                         ReactionPattern(cplx_pats),
                         match=o.get('type').lower())
示例#5
0
 def _parse_observables(self):
     for o in self._x.iterfind(_ns('{0}ListOfObservables/{0}Observable')):
         o_name = o.get('name')
         match_mode = o.get('type').lower()
         # Some BNG observables have same name as a monomer, but in PySB
         # these must be unique
         if o_name in self.model.monomers.keys():
             o_name = 'Obs_{}'.format(o_name)
         cplx_pats = []
         for mp in o.iterfind(_ns('{0}ListOfPatterns/{0}Pattern')):
             cpt = self.model.compartments.get(mp.get('compartment'))
             match_once = mp.get('matchOnce')
             match_once = True if match_once == "1" and \
                 match_mode != 'species' else False
             cplx_pats.append(
                 ComplexPattern(self._parse_species(mp),
                                compartment=cpt,
                                match_once=match_once))
         self.observable(o_name,
                         ReactionPattern(cplx_pats),
                         match=match_mode)
示例#6
0
    def _parse_rules(self):
        # Store reversible rates for post-processing (we don't know if we'll
        # encounter fwd or rev rule first)
        rev_rates = {}
        for r in self._x.iterfind(
                _ns('{0}ListOfReactionRules/'
                    '{0}ReactionRule')):
            r_name = r.get('name')

            r_rate_xml = r.find(_ns('{}RateLaw'))
            if r_rate_xml is None:
                raise BnglImportError('Rate law missing for rule %s' % r_name)
            r_rate = self._parse_rate_law(r_rate_xml)

            # Store reverse rates for post-processing
            if r_name.startswith('_reverse_'):
                r_name = r_name[9:]
                rev_rates[r_name] = r_rate
                continue

            # Compile reactant and product patterns
            reactant_pats = []
            for rp in r.iterfind(
                    _ns('{0}ListOfReactantPatterns/'
                        '{0}ReactantPattern')):
                cpt = self.model.compartments.get(rp.get('compartment'))
                reactant_pats.append(
                    ComplexPattern(self._parse_species(rp), cpt))
                if 'label' in rp.attrib:
                    reactant_pats[-1]._tag = self.model.tags[rp.get('label')]
            product_pats = []
            for pp in r.iterfind(
                    _ns('{0}ListOfProductPatterns/'
                        '{0}ProductPattern')):
                cpt = self.model.compartments.get(pp.get('compartment'))
                product_pats.append(
                    ComplexPattern(self._parse_species(pp), cpt))

                if 'label' in pp.attrib:
                    product_pats[-1]._tag = self.model.tags[pp.get('label')]
            rule_exp = RuleExpression(ReactionPattern(reactant_pats),
                                      ReactionPattern(product_pats),
                                      is_reversible=False)

            # Process any DeleteMolecules declaration
            delete_molecules = False
            for del_operations in r.iterfind(
                    _ns('{0}ListOfOperations/'
                        '{0}Delete')):
                if del_operations.get('DeleteMolecules') == "1":
                    delete_molecules = True
                    break

            # Process any MoveConnected declaration
            move_connected = False
            for change_cpt_ops in r.iterfind(
                    _ns('{0}ListOfOperations/'
                        '{0}ChangeCompartment')):
                if change_cpt_ops.get('moveConnected') == "1":
                    move_connected = True
                    break

            # Give warning/error if ListOfExcludeReactants or
            # ListOfExcludeProducts is present
            if r.find(_ns('{}ListOfExcludeReactants')) is not None or \
               r.find(_ns('{}ListOfExcludeProducts')) is not None:
                self._warn_or_except(
                    'ListOfExcludeReactants and/or '
                    'ListOfExcludeProducts declarations '
                    'are deprecated in BNG, and not supported '
                    'in PySB.')

            # Give warning/error if ListOfIncludeReactants or
            # ListOfIncludeProducts is present
            if r.find(_ns('{}ListOfIncludeReactants')) is not None or \
               r.find(_ns('{}ListOfIncludeProducts')) is not None:
                self._warn_or_except(
                    'ListOfIncludeReactants and/or '
                    'ListOfIncludeProducts declarations '
                    'are deprecated in BNG, and not supported '
                    'in PySB.')

            self.rule(r_name,
                      rule_exp,
                      r_rate,
                      delete_molecules=delete_molecules,
                      move_connected=move_connected)

        # Set the reverse rates
        for r_name, rev_rate in rev_rates.items():
            rule = self.model.rules[r_name]
            rule.rule_expression.is_reversible = True
            rule.is_reversible = True
            rule.rate_reverse = rev_rate
示例#7
0
文件: bngl.py 项目: weilandtd/pysb
    def _parse_rules(self):
        # Store reversible rates for post-processing (we don't know if we'll
        # encounter fwd or rev rule first)
        rev_rates = {}
        for r in self._x.iterfind(
                _ns('{0}ListOfReactionRules/'
                    '{0}ReactionRule')):
            r_name = r.get('name')

            r_rate_xml = r.find(_ns('{}RateLaw'))
            if r_rate_xml is None:
                raise BnglImportError('Rate law missing for rule %s' % r_name)
            r_rate = self._parse_rate_law(r_rate_xml)

            # Store reverse rates for post-processing
            if r_name.startswith('_reverse_'):
                r_name = r_name[9:]
                rev_rates[r_name] = r_rate
                continue

            # Compile reactant and product patterns
            reactant_pats = []
            for rp in r.iterfind(
                    _ns('{0}ListOfReactantPatterns/'
                        '{0}ReactantPattern')):
                cpt = self.model.compartments.get(rp.get('compartment'))
                reactant_pats.append(
                    ComplexPattern(self._parse_species(rp), cpt))
            product_pats = []
            for pp in r.iterfind(
                    _ns('{0}ListOfProductPatterns/'
                        '{0}ProductPattern')):
                cpt = self.model.compartments.get(pp.get('compartment'))
                product_pats.append(
                    ComplexPattern(self._parse_species(pp), cpt))
            rule_exp = RuleExpression(ReactionPattern(reactant_pats),
                                      ReactionPattern(product_pats),
                                      is_reversible=False)

            # Process any DeleteMolecules declaration
            delete_molecules = False
            for del_operations in r.iterfind(
                    _ns('{0}ListOfOperations/'
                        '{0}Delete')):
                if del_operations.get('DeleteMolecules') == "1":
                    delete_molecules = True
                    break

            # Give warning/error if ListOfExcludeReactants or
            # ListOfExcludeProducts is present
            if r.find(_ns('{}ListOfExcludeReactants')) is not None or \
               r.find(_ns('{}ListOfExcludeProducts')) is not None:
                self._warn_or_except('ListOfExcludeReactants and/or '
                                     'ListOfExcludeProducts declarations will '
                                     'be ignored. This may lead to long '
                                     'network generation times.')

            # Give warning/error if ListOfIncludeReactants or
            # ListOfIncludeProducts is present
            if r.find(_ns('{}ListOfIncludeReactants')) is not None or \
               r.find(_ns('{}ListOfIncludeProducts')) is not None:
                self._warn_or_except('ListOfIncludeReactants and/or '
                                     'ListOfIncludeProducts declarations will '
                                     'be ignored. This may lead to long '
                                     'network generation times.')

            self.rule(r_name,
                      rule_exp,
                      r_rate,
                      delete_molecules=delete_molecules)

        # Set the reverse rates
        for r_name, rev_rate in rev_rates.items():
            rule = self.model.rules[r_name]
            rule.rule_expression.is_reversible = True
            rule.is_reversible = True
            rule.rate_reverse = rev_rate