src/sys_def.jl

MEDYAN.add_diffusion_coeff!

add_diffusion_coeff!(s::SysDef, name::Symbol, coeff::Float64)

Update the diffusion coefficient for an existing diffusing species.

The species must already exist in s.diffusing (typically added via def_diffusing_species!.

Arguments

  • s::SysDef: The system definition to modify.
  • name::Symbol: The name of the existing diffusing species.
  • coeff::Float64: The diffusion coefficient in units of nm²/s.

Returns

The modified SysDef.

See also: def_diffusing_species! to add a new species with its diffusion coefficient in one step.

MEDYAN.def_diffusing_species!

def_diffusing_species!(s::SysDef, name::Symbol; coeff::Float64)

Dynamically add a new diffusing species to the system definition.

Arguments

  • s::SysDef: The system definition to modify.
  • name::Symbol: The name of the new diffusing species.
  • coeff::Float64: The diffusion coefficient in units of (nm²/s).

Returns

The modified SysDef.

Throws

  • Error if a diffusing species with the given name already exists.

MEDYAN.def_fixed_species!

def_fixed_species!(s::SysDef, name::Symbol)

Dynamically add a new fixed (non-diffusing) species to the system definition.

Must be called before adding any sites (filament sites, filament end sites, membrane sites, link types, etc.) or reactions.

Arguments

  • s::SysDef: The system definition to modify.
  • name::Symbol: The name of the new fixed species.

Returns

The modified SysDef.

Throws

  • Error if the fixed species already exists.
  • Error if sites or reactions have already been added.

MEDYAN.def_fila_type!

def_fila_type!(s::SysDef; name::Symbol, mono_states::Vector{Symbol}, param::FilamentMechParams)

Add a new filament type to the system definition.

Arguments

  • s::SysDef: The system definition to modify.
  • name::Symbol: The name of the new filament type.
  • mono_states::Vector{Symbol}: Vector of monomer state names for this filament type. Must not be empty.
  • param::FilamentMechParams: The filament mechanical parameters.

Returns

The modified SysDef.

Throws

  • Error if a filament type with the given name already exists.
  • Error if mono_states is empty.

MEDYAN.add_filament_params!

add_filament_params!(s::SysDef, filament_name::Symbol, filament_params::FilamentMechParams)

Update the filament mechanical parameters for an existing filament type.

The filament type must already exist in s.filament (typically added via def_fila_type!.

Arguments

  • s::SysDef: The system definition to modify.
  • filament_name::Symbol: The name of the existing filament type.
  • filament_params::FilamentMechParams: The mechanical parameters to set.

Returns

The modified SysDef.

See also: def_fila_type! to add a new filament type with its parameters in one step.

MEDYAN.addfilamentsite!

addfilamentsite!(s::SysDef,filamenttypename::Symbol,filamentsitename::Symbol,site)

MEDYAN.addfilamentendsite!

addfilamentendsite!(s::SysDef,filamenttypename::Symbol,filamentendsitename::Symbol,site)::SysDef

MEDYAN.add_decimated_2mon_site!

add_decimated_2mon_site!(s::SysDef,decimated_2mon_sitename::Symbol,site)::SysDef

MEDYAN.addpossiblecadherinsite!

addpossiblecadherinsite!(s::SysDef,possiblecadherinsitename::Symbol,site)::SysDef

MEDYAN.addmembranesite!

addmembranesite!(
    s::MEDYAN.SysDef,
    membranesitename::Symbol,
    site
) -> MEDYAN.SysDef

Add a new site with a specified name. During this process, a new fixed species is created suffixed with “membranesite.”.

MEDYAN.addreaction!

addreaction!(s::SysDef,reactionexpr::AbstractString,rate::Float64,invvolumepower::Int)::SysDef

Add a reaction to the system. Return s

reactionexpr is a string describing the reaction stoichiometry

reactionexpr is comprised of reactant and product parts seperated by a "-->"

All whitespace characters are ignored.

Each side is then split by "+" to get the species names.

Repeated or extra "+" are ignored.

A species name can be prepended by a positive integer to represent multiple copies.

  1. rate::Float64: Base rate for the reaction. ((nm³)^(invvolumepower)/s) rate constants correspond to stochastic rate constants in the sense used by Gillespie (J. Comp. Phys., 1976, 22 (4)).
  2. invvolumepower::Int: volumefactor= (1/volume)^invvolumepower where volume is the volume of the compartment in nm³. Generally this is 0 for reactions without another diffusing reactant, and 1 if there is another diffusing reactant.

Example good reactionexpr

"diffusing.a + diffusing.b --> diffusing.c"
"diffusing.c --> diffusing.a + diffusing.b"
"+ + diffusing.c + --> + diffusing.a + + diffusing.b + +"
" --> diffusing.a + diffusing.b"
"diffusing.a + diffusing.b --> "
"diffusing.a + diffusing.a --> "
"2diffusing.a --> "
"2diffusing.a --> 20diffusing.a"
"diffusing.c + diffusing.b --> diffusing.c + diffusing.b"
"fixedspecies.rate1b --> fixedspecies.g"
"fixedspecies.rate1b + fixedspecies.g --> fixedspecies.g"
"fixedspecies.rate1b + 23fixedspecies.g --> fixedspecies.g"
"fixedspecies.g --> fixedspecies.rate1b + 23fixedspecies.g"
"fixedspecies.g + fixedspecies.rate1b--> 2fixedspecies.rate1b + 23fixedspecies.g"
"filamentsite.MT.d --> filamentsite.MT.d"
"filamentsite.MT.d + diffusing.a --> filamentsite.MT.d"
"fixedspecies.g --> diffusing.a"
"diffusing.a --> fixedspecies.g"
"filamentsite.actin.pm + diffusing.a --> filamentsite.actin.pm"

MEDYAN.addreactioncallback!

addreactioncallback!(s::SysDef, reactantexpr::AbstractString, rate::Float64, invvolumepower::Int, callback)::SysDef

Like addreaction! but also adds callback. callback is called when the reaction happens with input of MEDYAN.Context and Int the chem_voxel id where the reaction happened.

The callback should handle updating species counts.

MEDYAN.errorcheck_addcallback(callback,s::SysDef) can optionally be overloaded to add errorchecking when the callback is added.

MEDYAN.def_reaction!

def_reaction!(s::SysDef, reaction_expr::AbstractString; base_rate, invvolumepower=0, affect!=nothing)

Add a chem_voxel reaction to the system definition. Returns s.

reaction_expr is a string describing the reaction. The format depends on whether affect! is provided:

  • Without affect!: reaction_expr must contain "-->" to separate reactants from products. Stoichiometry is computed automatically.
  • With affect!: reaction_expr lists only the reactants (no "-->"). Net stoichiometry is zero; the callback handles all state changes.

reaction_expr is comprised of reactant and product parts separated by "-->". All whitespace is ignored. Each side is split by "+" to get species names. Repeated or extra "+" are ignored. A species name can be prepended by a positive integer to represent multiple copies (e.g., "2diffusing.A").

Species name prefixes:

  • diffusing.X — diffusing species
  • fixedspecies.X — fixed (non-diffusing) species
  • filamentsite.T.N — filament monomer site
  • filamentendsite.T.N — filament end site
  • decimated_2mon_site.N — decimated two-monomer site

Keyword Arguments

Rate

  • base_rate::Float64: Rate constant. Units: (nm³)^invvolumepower / s. Rate constants are stochastic rate constants in the sense of Gillespie (1976).
  • invvolumepower::Int = 0: volumefactor = (1/volume)^invvolumepower where volume is the chem_voxel volume in nm³. Typically 0 for unimolecular, 1 for bimolecular, 2 for trimolecular.

Callback (optional)

  • affect!: Callback fired when the reaction occurs. Signature: (c::Context; chem_voxel::Int, kwargs...) -> Any. When provided, reaction_expr must not contain "-->".

Examples

using MEDYAN
s = MEDYAN.SysDef()
def_diffusing_species!(s, :A; coeff=2.5e7)
def_diffusing_species!(s, :B; coeff=2.5e7)
def_diffusing_species!(s, :C; coeff=2.5e7)

# Bimolecular: A + B → C
def_reaction!(s, "diffusing.A + diffusing.B --> diffusing.C";
    base_rate = 1.5e6,
    invvolumepower = 1,
)

# Unimolecular: C → A + B
def_reaction!(s, "diffusing.C --> diffusing.A + diffusing.B";
    base_rate = 1.75,
)

# Zero-order creation
def_reaction!(s, " --> diffusing.A + diffusing.B";
    base_rate = 1.75,
)

# With a callback (reactants only, no "-->")
def_reaction!(s, "diffusing.A + diffusing.B";
    base_rate = 1e8,
    invvolumepower = 1,
    affect! = (c; chem_voxel, kwargs...) -> begin
        # custom logic here
        nothing
    end,
)

MEDYAN.def_fila_reaction!

def_fila_reaction!(s::SysDef; fila_type, name, match, ...)

Add a filament monomer reaction. The reaction fires at monomers whose local neighborhood matches a pattern of states. When it fires, the matched monomers can be changed to new, or a custom affect! callback can handle arbitrary side effects.

All pattern positions must lie on the filament. Monomers near the ends where the pattern would extend past the boundary are automatically skipped. Use def_fila_tip_reaction! for reactions at filament ends.

Keyword Arguments

Required

  • fila_type::Symbol: Filament type (e.g., :actin).

  • name::Symbol: Unique name for this reaction site.

  • match::Vector: Pattern of monomer states to match, ordered minus → plus end. Each element can be:

    • Symbol — exact state (e.g., :a)
    • Vector{Symbol} — any of these states (e.g., [:a, :b])
    • anystate — any state

State change (provide exactly one)

  • new::Vector{Symbol}: States to assign to the matched monomers after firing. Must be the same length as match. Cannot be used with affect!.
  • affect!: Custom callback (c::Context; chem_voxel::Int, center::FilaMonoIdx, kwargs...) -> Int. Called instead of automatic state changes. The returned Int is a status code. Cannot be used with new.

Stoichiometry

  • net_stoich::Vector{Pair{Symbol,Int}} = Pair{Symbol,Int}[]: Net change to diffusing species when the reaction fires. Each entry is species_name => amount, e.g. [:ligand => -1] to consume one ligand per firing. The callback handles the count update automatically. Can only be used with new, not with affect!.

Rate

  • base_rate::Float64: Rate constant. Units: (nm³)^invvolumepower / s.
  • invvolumepower::Int = 0: Set to 0 for unimolecular, 1 for bimolecular.
  • reactants_extra::String = "": Additional reactant species that contribute to propensity (e.g., "diffusing.ligand"). The species is not consumed automatically.

Geometry

  • center::Int = cld(length(match), 2): Which position in match is the center of the reaction. Determines which chem_voxel the reaction is assigned to.

Examples

using MEDYAN
s = MEDYAN.SysDef()
def_fila_type!(s; name=:actin, mono_states=[:a, :b, :c], param=MEDYAN.ACTIN_TWIST_PARAMS)

# Aging: every :a monomer transitions to :b
def_fila_reaction!(s;
    fila_type = :actin, name = :aging,
    match = [:a],
    new = [:b],
    base_rate = 2.4,
)

# Cooperative transition: :a next to :b on the minus side becomes :b
def_fila_reaction!(s;
    fila_type = :actin, name = :cooperate,
    match = [:b, :a],
    new = [:b, :b],
    center = 2,
    base_rate = 0.4,
)

# Bimolecular binding with a diffusing ligand using affect!
def_diffusing_species!(s, :ligand; coeff=2.5e7)
def_fila_reaction!(s;
    fila_type = :actin, name = :bind_affect,
    match = [:a],
    affect! = (c; center, chem_voxel, kwargs...) -> let
        update_fila_mono_state!(c, center, :b)
        add_diffusing_count!(c; species=:ligand, chem_voxel, amount=-1)
        1
    end,
    base_rate = 1e6,
    invvolumepower = 1,
    reactants_extra = "diffusing.ligand",
)

# Bimolecular binding with a diffusing ligand using net_stoich
def_fila_reaction!(s;
    fila_type = :actin, name = :bind_stoich,
    match = [:a],
    new = [:b],
    net_stoich = [:ligand => -1],
    base_rate = 1e6,
    invvolumepower = 1,
    reactants_extra = "diffusing.ligand",
)

# Match a set of states: :a or :b → :c
def_fila_reaction!(s;
    fila_type = :actin, name = :a_or_b,
    match = [[:a, :b]],
    new = [:c],
    base_rate = 1.0,
)

# anystate with neighbor constraint: any monomer whose plus neighbor is :a
def_fila_reaction!(s;
    fila_type = :actin, name = :any_then_a,
    match = [anystate, :a],
    new = [:a, :b],
    center = 2,
    base_rate = 1.0,
)

MEDYAN.def_fila_tip_reaction!

def_fila_tip_reaction!(s::SysDef; fila_type, name, is_minus_end, match, spacing, ...)

Add a filament tip reaction that fires only at either the plus or minus end of a filament.

This mirrors def_fila_reaction! in API style and rate wiring, but the pattern is anchored at a filament end. new may be shorter or longer than match to represent depolymerization or polymerization.

match and new are always ordered from minus end toward plus end. If is_minus_end = true, the pattern is matched against the first length(match) monomers; if is_minus_end = false, it is matched against the last length(match) monomers.

Keyword Arguments

Required

  • fila_type::Symbol: Filament type (e.g., :actin).
  • name::Symbol: Unique name for this tip site.
  • is_minus_end::Bool: Which filament end to target. true targets the minus end; false targets the plus end.
  • match::Vector: Pattern of monomer states to match (minus → plus order). Elements can be Symbol, Vector{Symbol} (state set), or anystate.
  • spacing::Float64: Space needed at the filament end for this reaction (nm). This affects a load-force rate factor via exp(-β * spacing * loadforce) where β is 1/kT and loadforce is the external force pushing axially on the end of the filament.

State change (provide exactly one)

  • new::Vector{Symbol}: New monomer states (minus → plus order) for the tip region. If length(new) > length(match), monomers are added. If length(new) < length(match), monomers are removed. Cannot be used with affect!.
  • affect!: Custom callback (c::Context; chem_voxel::Int, tip::FilaTipIdx, kwargs...) -> Int. Called instead of automatic changes. Cannot be used with new.

Stoichiometry

  • net_stoich::Vector{Pair{Symbol,Int}} = Pair{Symbol,Int}[]: Net change to diffusing species when the reaction fires. Each entry is species_name => amount, e.g. [:actin_mon => -1] to consume one monomer per polymerization. The callback handles the count update automatically. Can only be used with new, not with affect!.

Rate

  • base_rate::Float64: Rate constant. Units: (nm³)^invvolumepower / s.
  • invvolumepower::Int = 0: Set to 0 for unimolecular, 1 for bimolecular.
  • reactants_extra::String = "": Additional reactant species that contribute to propensity (e.g., "diffusing.ligand"). The species is not consumed automatically.
  • added_monomers::Union{Int, Nothing}: The number of monomers added by the reaction. This is automatically determined if new is passed. Each filament has a limited number of monomers that can be added per end

between mechanic solves given by FilamentMechParams.max_num_unmin_end. The filament tip reaction is disabled on tips where the added monomers would cause that limit to be exceeded.

Examples

using MEDYAN
s = MEDYAN.SysDef()
def_fila_type!(s; name=:actin, mono_states=[:a, :plusend, :minusend], param=MEDYAN.ACTIN_TWIST_PARAMS)
def_diffusing_species!(s, :actin_mon; coeff=2.5e7)

# Plus-end polymerization: add one monomer (bimolecular with diffusing actin)
def_fila_tip_reaction!(s;
    fila_type = :actin,
    name = :pp,
    is_minus_end = false,
    match = [:plusend],
    spacing = 2.7,
    new = [:a, :plusend],
    net_stoich = [:actin_mon => -1],
    base_rate = 1.93e7,
    invvolumepower = 1,
    reactants_extra = "diffusing.actin_mon",
)

# Plus-end polymerization: add one monomer (bimolecular with diffusing actin)
# Using affect!
def_fila_tip_reaction!(s;
    fila_type = :actin,
    name = :pp_affect,
    is_minus_end = false,
    match = [:a, :plusend],
    spacing = 2.7,
    affect! = (c; tip, chem_voxel, kwargs...) -> begin
        update_fila_mono_state!(c, FilaMonoIdx(c, tip), :a)
        polymerize_fila!(c, tip, :plusend)
        add_diffusing_count!(c; species=:actin_mon, chem_voxel, amount=-1)
        1
    end,
    added_monomers = 1,
    base_rate = 1.93e7,
    invvolumepower = 1,
    reactants_extra = "diffusing.actin_mon",
)

# Minus-end depolymerization: remove one monomer, release actin back to solution
def_fila_tip_reaction!(s;
    fila_type = :actin, name = :dm,
    is_minus_end = true,
    match = [:minusend, :a],
    spacing = 0.0,
    new = [:minusend],
    net_stoich = [:actin_mon => +1],
    base_rate = 1.4,
)

MEDYAN.add_membranesitereaction!

Add a membrane site with the corresponding reaction with callback.

Keyword arguments:

  • s: SysDef.
  • name_newmembranesite: Symbol.
  • membranediffusingreactants: Vector of symbols as membrane reactants. 0 or 1 reactant is currently supported.
  • membranediffusingproducts: Vector of symbols as membrane products.
  • reactionexpr_extra: Reaction expression for other species involved.
  • rate: Float.
  • changerage_bypotentialenergy: Whether the rate is affected by potential energy.
  • invvolumepower: rate scaling with compartment volume.

Notes:

  • If error occurs, this function does not ensure that s is unchanged.