inline bool operator<(ReactionRule const& lhs, ReactionRule const& rhs)
{
    int tmp = memberwise_compare(lhs.get_reactants(), rhs.get_reactants());
    if (tmp > 0)
    {
        return false;
    }
    else if (tmp < 0)
    {
        return true;
    }
    return memberwise_compare(lhs.get_products(), rhs.get_products()) < 0;
}
Example #2
0
//============================================================
//	Model
//============================================================
ReactionRule *init_reaction_from_json(json js_reaction) {
	ReactionRule *r = new ReactionRule;
	string reactant( json_cast<string>(js_reaction["reactant"]) );
	int reaStoich( json_cast<int>(js_reaction["reaStoich"]) );
	r->add_reactant(reactant, reaStoich);

	string product( json_cast<string>(js_reaction["product"]) );
	int proStoich( json_cast<int>(js_reaction["proStoich"]) );
	r->add_product(product, proStoich);
	r->set_kinetic_parameter( json_cast<double>(js_reaction["kineticParameter"]) );

	return r;
}
Example #3
0
std::vector<ReactionRule> NetworkModel::apply(
    const ReactionRule& rr, const ReactionRule::reactant_container_type& reactants) const
{
    if (rr.reactants().size() != reactants.size())
    {
        return std::vector<ReactionRule>();
    }

    ReactionRule::reactant_container_type::const_iterator
        i(rr.reactants().begin()), j(reactants.begin());
    for (; i != rr.reactants().end(); ++i, ++j)
    {
        if (*i != *j)
        {
            return std::vector<ReactionRule>();
        }
    }
    return std::vector<ReactionRule>(1, rr);
}
Example #4
0
Model *init_model_from_csv(string &csv_model) {
	bool header = true, first = true;
	Model *m = new Model;
	pfi::text::csv_parser psr(csv_model);
	for(pfi::text::csv_iterator p(psr), q; p != q; ++p) {
		if (header == true && first == true) {
			first = false;
			continue;
		}
		ReactionRule *r = new ReactionRule;
		string reactant( (*p)[1] );
		string product( (*p)[3] );
		r->add_reactant( reactant, atoi((*p)[2]) );
		r->add_product(product, atoi((*p)[4]) );
		r->set_kinetic_parameter( double(atof( (*p)[5] )) );
		m->reactions.push_back( *r );
		delete r;
	}
	return m;
}
Example #5
0
std::vector<ReactionRule> ReactionRule::generate(const reactant_container_type& reactants) const
{
    const std::vector<std::vector<Species> > possibles(rrgenerate(*this, reactants));
    std::vector<ReactionRule> retval;
    retval.reserve(possibles.size());
    for (std::vector<std::vector<Species> >::const_iterator i(possibles.begin());
        i != possibles.end(); ++i)
    {
        const ReactionRule rr(reactants, *i, this->k());
        std::vector<ReactionRule>::iterator
            j(std::find(retval.begin(), retval.end(), rr));
        if (j != retval.end())
        {
            (*j).set_k((*j).k() + rr.k());
        }
        else
        {
            retval.push_back(rr);
        }
    }
    return retval;
}
Example #6
0
void NetworkModel::add_reaction_rule(const ReactionRule& rr)
{
    reaction_rule_container_type::const_iterator
        i(std::find(reaction_rules_.begin(), reaction_rules_.end(), rr));
    if (i != reaction_rules_.end())
    {
        throw AlreadyExists("reaction rule already exists");
    }

    reaction_rules_map_[rr.reactants()].insert(reaction_rules_.size());
    reaction_rules_.push_back(rr);
    dirty_ = true;
}
Example #7
0
inline bool operator<(const ReactionRule& lhs, const ReactionRule& rhs)
{
    if (lhs.reactants() < rhs.reactants())
    {
        return true;
    }
    else if (lhs.reactants() > rhs.reactants())
    {
        return false;
    }
    return (lhs.products() < rhs.products());
}
Example #8
0
void NetworkModel::remove_reaction_rule(const ReactionRule& rr)
{
    reaction_rule_container_type::iterator
        i(std::find(reaction_rules_.begin(), reaction_rules_.end(), rr));
    if (i == reaction_rules_.end())
    {
        throw NotFound("reaction rule not found");
    }

    reaction_rule_container_type::size_type const
        idx(i - reaction_rules_.begin()), last_idx(reaction_rules_.size() - 1);
    reaction_rules_map_type::iterator
        j(reaction_rules_map_.find(rr.reactants()));
    if (j == reaction_rules_map_.end())
    {
        throw IllegalState("no corresponding map key found");
    }
    else if ((*j).second.erase(idx) == 0)
    {
        throw IllegalState("no corresponding map value found");
    }

    if (idx < last_idx)
    {
        reaction_rule_container_type::value_type const
            last_value(reaction_rules_[last_idx]);
        (*i) = last_value;
        j = reaction_rules_map_.find(last_value.reactants());
        if (j == reaction_rules_map_.end())
        {
            throw IllegalState("no corresponding map key for the last found");
        }
        else if ((*j).second.erase(last_idx) == 0)
        {
            throw IllegalState("no corresponding map value for the last found");
        }
        (*j).second.insert(idx);
    }

    reaction_rules_.pop_back();
    dirty_ = true;
}
Example #9
0
void NetworkModel::add_reaction_rule(const ReactionRule& rr)
{
    reaction_rule_container_type::iterator
        i(std::find(reaction_rules_.begin(), reaction_rules_.end(), rr));
    if (i != reaction_rules_.end())
    {
        // throw AlreadyExists("reaction rule already exists");
        std::cerr << "WARN: reaction rule ["
            << rr.as_string() << "] already exists." << std::endl;
        (*i) = ReactionRule((*i).reactants(), (*i).products(), (*i).k() + rr.k());
        return;
    }

    const reaction_rule_container_type::size_type idx(reaction_rules_.size());
    reaction_rules_.push_back(rr);

    if (rr.reactants().size() == 1)
    {
        first_order_reaction_rules_map_[rr.reactants()[0].serial()].push_back(idx);
    }
    else if (rr.reactants().size() == 2)
    {
        const Species::serial_type
            serial1(rr.reactants()[0].serial()),
            serial2(rr.reactants()[1].serial());
        const std::pair<Species::serial_type, Species::serial_type>
            key(serial1 < serial2?
                std::make_pair(serial1, serial2):
                std::make_pair(serial2, serial1));
        second_order_reaction_rules_map_[key].push_back(idx);
    }
    else
    {
        ;
    }
}
Example #10
0
void NetworkModel::remove_reaction_rule(const ReactionRule& rr)
{
    reaction_rule_container_type::iterator
        i(std::find(reaction_rules_.begin(), reaction_rules_.end(), rr));
    if (i == reaction_rules_.end())
    {
        throw NotFound("reaction rule not found");
    }

    reaction_rule_container_type::size_type const
        idx(i - reaction_rules_.begin()), last_idx(reaction_rules_.size() - 1);
    if (rr.reactants().size() == 1)
    {
        first_order_reaction_rules_map_type::iterator
            j(first_order_reaction_rules_map_.find(rr.reactants()[0].serial()));
        if (j == first_order_reaction_rules_map_.end())
        {
            throw IllegalState("no corresponding map key found");
        }

        first_order_reaction_rules_map_type::mapped_type::iterator
            k(std::remove((*j).second.begin(), (*j).second.end(), idx));
        if (k == (*j).second.end())
        {
            throw IllegalState("no corresponding map value found");
        }
        else
        {
            (*j).second.erase(k, (*j).second.end());
        }
    }
    else if (rr.reactants().size() == 2)
    {
        second_order_reaction_rules_map_type::iterator
            j(second_order_reaction_rules_map_.find(std::make_pair(
                rr.reactants()[0].serial(), rr.reactants()[1].serial())));
        if (j == second_order_reaction_rules_map_.end())
        {
            throw IllegalState("no corresponding map key found");
        }

        second_order_reaction_rules_map_type::mapped_type::iterator
            k(std::remove((*j).second.begin(), (*j).second.end(), idx));
        if (k == (*j).second.end())
        {
            throw IllegalState("no corresponding map value found");
        }
        else
        {
            (*j).second.erase(k, (*j).second.end());
        }
    }

    if (idx < last_idx)
    {
        reaction_rule_container_type::value_type const
            last_value(reaction_rules_[last_idx]);
        (*i) = last_value;

        if (last_value.reactants().size() == 1)
        {
            first_order_reaction_rules_map_type::iterator
                j(first_order_reaction_rules_map_.find(
                    last_value.reactants()[0].serial()));
            if (j == first_order_reaction_rules_map_.end())
            {
                throw IllegalState("no corresponding map key for the last found");
            }

            first_order_reaction_rules_map_type::mapped_type::iterator
                k(std::remove((*j).second.begin(), (*j).second.end(), last_idx));
            if (k == (*j).second.end())
            {
                throw IllegalState("no corresponding map value found");
            }
            else
            {
                (*j).second.erase(k, (*j).second.end());
            }
            (*j).second.push_back(idx);
        }
        else if (last_value.reactants().size() == 2)
        {
            second_order_reaction_rules_map_type::iterator
                j(second_order_reaction_rules_map_.find(std::make_pair(
                    last_value.reactants()[0].serial(),
                    last_value.reactants()[1].serial())));
            if (j == second_order_reaction_rules_map_.end())
            {
                throw IllegalState("no corresponding map key for the last found");
            }
            second_order_reaction_rules_map_type::mapped_type::iterator
                k(std::remove((*j).second.begin(), (*j).second.end(), last_idx));
            if (k == (*j).second.end())
            {
                throw IllegalState("no corresponding map value found");
            }
            else
            {
                (*j).second.erase(k, (*j).second.end());
            }
            (*j).second.push_back(idx);
        }
    }

    reaction_rules_.pop_back();
}
inline bool operator==(ReactionRule const& lhs, ReactionRule const& rhs)
{
    return lhs.get_reactants() == rhs.get_reactants() &&
            memberwise_compare(lhs.get_products(), rhs.get_products()) == 0;
}
inline bool valid(ReactionRule const& r)
{
    return r.get_reactants().size() != 0;
}
Example #13
0
inline bool operator==(const ReactionRule& lhs, const ReactionRule& rhs)
{
    return ((lhs.reactants() == rhs.reactants())
            && (lhs.products() == rhs.products()));
}