Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tutorial error: KeyError when calling: assign_migration! #11

Open
fieldofnodes opened this issue Oct 7, 2024 · 1 comment
Open

Tutorial error: KeyError when calling: assign_migration! #11

fieldofnodes opened this issue Oct 7, 2024 · 1 comment

Comments

@fieldofnodes
Copy link

using Pkg
Pkg.activate(".")
using GeneDrive

# Tutorial from https://vnvasquez.github.io/GeneDrive.jl/dev/datasetup_tutorials/


# Select species type
species = AedesAegypti

# Define how genetic information is passed on
genetics = genetics_mendelian();

# Choose functional form of environmental response for species life stages
enviro_response = stages_rossi();

# Update population size as desired
update_population_size(enviro_response, 500);

# Assemble organism
organisms = make_organisms(species, genetics, enviro_response);

# Define temperature functional form and data
temperature = example_temperature_timeseries;

# Specify the geographic coordinates
coordinates = (16.1820, 145.7210);

# Define the spatial structure, name the location, and "populate" it
node1 = Node(:YorkeysKnob, organisms, temperature, coordinates);

# Define a second node
coordinates2 = (17.0966, 145.7747);
node2 = Node(:Gordonsvale, organisms, temperature, coordinates2);

# Create a network comprised of the two nodes
network = Network(:Queensland, node1, node2);

# Specify that adult males and females of all genotypes move
migration_data = Dict( # node1 <-> node2
    ("Male", "AA") => Dict((:YorkeysKnob, :Gordonsvale) => 0.02,
                           (:Gordonsvale, :YorkeysKnob) => 0.02),
    ("Male", "Aa") => Dict((:YorkeysKnob, :Gordonsvale) => 0.02,
                            (:Gordonsvale, :YorkeysKnob) => 0.02),
    ("Male", "aa") => Dict((:YorkeysKnob, :Gordonsvale) => 0.02,
                            (:Gordonsvale, :YorkeysKnob) => 0.02),
    ("Female", "AA") => Dict((:YorkeysKnob, :Gordonsvale) => 0.02,
                            (:Gordonsvale, :YorkeysKnob) => 0.02),
    ("Female", "Aa") => Dict((:YorkeysKnob, :Gordonsvale) => 0.02,
                            (:Gordonsvale, :YorkeysKnob) => 0.02),
    ("Female", "aa") => Dict((:YorkeysKnob, :Gordonsvale) => 0.02,
                            (:Gordonsvale, :YorkeysKnob) => 0.02)
);

These commands run fine.

Then:

# Add migration to the network object
assign_migration!(network, migration_data, species);

Gives this error

ERROR: KeyError: key "AA" not found
Stacktrace:
 [1] getindex
   @ .\dict.jl:498 [inlined]
 [2] _fill_migration_array!(initial_migration::Matrix{…}, migration_data::Dict{…}, gene_to_index_migration_matrix::Dict{…}, stage_to_index_migration_matrix::Dict{…}, locations_key_map::Dict{…})
   @ GeneDrive C:\Users\jonathan.miller\.julia\packages\GeneDrive\vNsku\src\spatial_structure.jl:101
 [3] assign_migration!(network::Network, migration_data::Dict{Tuple{…}, Dict{…}}, species::Type{AedesAegypti})
   @ GeneDrive C:\Users\jonathan.miller\.julia\packages\GeneDrive\vNsku\src\spatial_structure.jl:155
 [4] top-level scope
   @ c:\Users\jonathan.miller\Projects\genetic_modelling\src\julia-version\testing_genedrive.jl:1
Some type information was truncated. Use `show(err)` to see complete types.

I am running on

Device name	OXI-UK-LT00007
Processor	Intel(R) Core(TM) 7 150U   1.80 GHz
Installed RAM	32.0 GB (31.7 GB usable)
Device ID	2745EA06-3695-40A7-9454-D2E8B641C90E
Product ID	00330-80849-51267-AA000
System type	64-bit operating system, x64-based processor
Pen and touch	No pen or touch input is available for this display

And I have GeneDrive.jl version: GeneDrive v0.1.0

@fieldofnodes
Copy link
Author

From assign_migration!,

_fill_migration_array!(
        initial_migration,
        migration_data,
        gene_to_index_migration_matrix,
        stage_to_index_migration_matrix,
        network.locations_key_map,
    )

is the last executed line in the function.

I go to this function which relies on

const life_stage_key_map =
    Dict("Egg" => Egg, "Larva" => Larva, "Pupa" => Pupa, "Male" => Male, "Female" => Female)

const genetics_key_map = Dict(
    "WW" => WW,
    "ww" => ww,
    # end Wolbachia
    "HH" => HH,
    "Hh" => Hh,
    "HR" => HR,
    "hh" => hh,
    "hR" => hR,
    "RR" => RR,
    # end single-locus homing gene drive (HGD/MCR)
    "WR" => WR,
    # end RIDL (total = WW, WR, RR)
    "AA" => AA,
    "Aa" => Aa,
    "aa" => aa,
    # end Mendelian
)

these are in src/definitions.jl which are included in the module scope, but are possible not known at function call.

Specifically,

function _fill_migration_array!(
    initial_migration,
    migration_data,
    gene_to_index_migration_matrix,
    stage_to_index_migration_matrix,
    locations_key_map,
)

    for (life_gene_key, migration_matrix) in migration_data
        stage, gene = life_gene_key
        stage_index = stage_to_index_migration_matrix[life_stage_key_map[stage]]
        gene_index = gene_to_index_migration_matrix[genetics_key_map[gene]]
        for ix in stage_index
            mat = initial_migration[ix, gene_index]

            for (from_to_nodes, move_rate) in migration_matrix
                from_node, to_node = from_to_nodes
                from_index = locations_key_map[from_node]
                to_index = locations_key_map[to_node]
                mat[from_index, to_index] = move_rate
            end

            for i in 1:size(mat)[1]
                mat[i, i] = -sum(mat[i, :])
            end
        end
    end
end

These two lines are indexed by stage and gene. The error occurs from genetics_key_map[gene but not from life_stage_key_map[stage].

        stage_index = stage_to_index_migration_matrix[life_stage_key_map[stage]]
        gene_index = gene_to_index_migration_matrix[genetics_key_map[gene]]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant