# How to loop through all the species and write additional reactions based on that

**URL:** <https://discourse.julialang.org/t/how-to-loop-through-all-the-species-and-write-additional-reactions-based-on-that/115438>\
**Category:** Modelling & Simulations\
**Created:** [June 10, 2024, 3:58pm UTC](https://discourse.julialang.org/t/how-to-loop-through-all-the-species-and-write-additional-reactions-based-on-that/115438 "2024-06-10T15:58:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wenzhe\_Ma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wenzhe_ma/32/13020_2.png) [@Wenzhe\_Ma](https://discourse.julialang.org/u/Wenzhe_Ma)\
**Post date:** [June 10, 2024, 3:58pm UTC](https://discourse.julialang.org/t/how-to-loop-through-all-the-species-and-write-additional-reactions-based-on-that/115438/1 "2024-06-10T15:58:35Z")

</div>

What I want to do is to build a few models based on ReactionSystem in Catalyst.jl, named as m1, m2… Each of them has some common species s1, s2… I want to have a function with m1, m2 as arguments, which will automatically loop through the common species and build new reactions like m1.s1\<-\>m2.s1…  
I haven’t figured out a way to do it. Thanks in advance for any comments and suggestions

---

<div class="post-metadata">

**Author:** ![isaacsas](https://avatars.discourse-cdn.com/v4/letter/i/f6c823/32.png) [@isaacsas](https://discourse.julialang.org/u/isaacsas)\
**Post date:** [July 1, 2024, 7:43am UTC](https://discourse.julialang.org/t/how-to-loop-through-all-the-species-and-write-additional-reactions-based-on-that/115438/2 "2024-07-01T07:43:13Z")

</div>

Sorry for missing this comment – I’ve been sick and then traveling and haven’t kept up on issues the past several weeks. If you haven’t figured anything out, perhaps this is what you were looking for?

```julia
using Catalyst
using ModelingToolkit: getname

rn1 = @reaction_network rn1 begin
       k1, X1 --> X2
       end

rn2 = @reaction_network rn2 begin
       k2, X2 --> X1
       end

rxs = []
i = 1
for s in species(rn1)
    if any(isequal(s), species(rn2))
        ratesym = Symbol("exchangerate$(i)")
        rate = (@parameters $(ratesym))[1]
        sname1 = getproperty(rn1, getname(s))
        sname2 = getproperty(rn2, getname(s))
        push!(rxs, Reaction(rate, [sname1], [sname2]))
        i += 1
     end
end
@variables t
@named rn = ReactionSystem(rxs, t; systems = [rn1, rn2])

```

giving

```julia
julia> reactions(rn)
4-element Vector{Reaction}:
 exchangerate1, rn1₊X1 --> rn2₊X1
 exchangerate2, rn1₊X2 --> rn2₊X2
 rn1₊k1, rn1₊X1 --> rn1₊X2
 rn2₊k2, rn2₊X2 --> rn2₊X1

```

---

<div class="post-metadata">

**Author:** ![Wenzhe\_Ma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wenzhe_ma/32/13020_2.png) [@Wenzhe\_Ma](https://discourse.julialang.org/u/Wenzhe_Ma)\
**Post date:** [September 24, 2024, 7:52pm UTC](https://discourse.julialang.org/t/how-to-loop-through-all-the-species-and-write-additional-reactions-based-on-that/115438/3 "2024-09-24T19:52:15Z")

</div>

I just saw the replay. No I haven’t figured it out and used some walkaround. This is still very helpful! Thank you and hope you well.
