diff --git a/scripts/custom_modules_example.jl b/scripts/custom_modules_example.jl index 70e5b9b..8a3ba12 100644 --- a/scripts/custom_modules_example.jl +++ b/scripts/custom_modules_example.jl @@ -17,7 +17,7 @@ fitness_dict = Dict([ (['A', 'D', 'A', 'A'], 3.0), (['A', 'E', 'A', 'A'], 0.0), ]) -struct DummyScreening <: DESilico.Screening end +struct DummyScreening <: DESilico.AbstractScreening end (::DummyScreening)(sequence::Vector{Char}) = fitness_dict[sequence] (s::DummyScreening)(sequences::AbstractVector{Vector{Char}}) = map(sequence -> s(sequence), sequences) @@ -25,7 +25,7 @@ struct DummyScreening <: DESilico.Screening end # A custom `SelectionStrategy` structure should implement a method with signature # `(::CustomSelectionStrategy)(sequence_fitness_pairs::AbstractVector{Variant})` # which returns a vector of the selected sequences as a subtype of `AbstractVector{Vector{Char}}`. -struct DummySelectionStrategy <: DESilico.SelectionStrategy end +struct DummySelectionStrategy <: DESilico.AbstractSelectionStrategy end function (::DummySelectionStrategy)(variants::Vector{Variant}) [variants[1].sequence] end @@ -34,7 +34,7 @@ end # A custom `Mutagenesis` structure should implement a method with signature # `(::CustomMutagenesis)(parents::AbstractVector{Vector{Char}})` # which returns a vector of the created sequences as a subtype of `AbstractVector{Vector{Char}}`. -struct DummyMutagenesis <: DESilico.Mutagenesis end +struct DummyMutagenesis <: DESilico.AbstractMutagenesis end function (::DummyMutagenesis)(parents::Vector{Vector{Char}}) new_parent = copy(parents[1]) new_parent[2] = new_parent[2] + 1 diff --git a/scripts/de_sandbox.jl b/scripts/de_sandbox.jl index e97b496..00ad9f7 100644 --- a/scripts/de_sandbox.jl +++ b/scripts/de_sandbox.jl @@ -1,8 +1,8 @@ """ A simple de!() sandbox. -Select different DE modules (Screening, SelectionStrategy, Mutagenesis) and `initial_population` -to try different modules. +Select different DE modules (`AbstractScreening`, `AbstractSelectionStrategy`, `AbstractMutagenesis` implementations) +and `initial_population` to try different modules. """ # GB1 data from diff --git a/scripts/profiling.jl b/scripts/profiling.jl index 5416263..8ba8960 100644 --- a/scripts/profiling.jl +++ b/scripts/profiling.jl @@ -3,8 +3,8 @@ include("de_sandbox.jl") """ A simple function for profiling `de!()`. -Select different DE modules (`Screening`, `SelectionStrategy`, `Mutagenesis`) and `initial_population` -in `de_sandbox.jl` to profile different modules. +Select different DE modules (`AbstractScreening`, `AbstractSelectionStrategy`, `AbstractMutagenesis` implementations) +and `initial_population` in `de_sandbox.jl` to profile different modules. """ function run_de(n::Int) for _ in 1:n @@ -22,8 +22,8 @@ end """ A simple function for profiling `de_evaluation()`. -Select different DE modules (`Screening`, `SelectionStrategy`, `Mutagenesis`) and `initial_population` -in `de_sandbox.jl` to profile different modules. +Select different DE modules (`AbstractScreening`, `AbstractSelectionStrategy`, `AbstractMutagenesis` implementations) +and `initial_population` in `de_sandbox.jl` to profile different modules. """ function run_de_evaluation(n::Int) de_evaluation( diff --git a/src/de.jl b/src/de.jl index a47e5a9..239b134 100644 --- a/src/de.jl +++ b/src/de.jl @@ -7,9 +7,9 @@ Perform directed `n_iterations` of directed evolution and save results into `seq - `sequence_space::SequenceSpace`: Maintains the current population of mutants and library of screened variants. # Keywords -- `screening::Screening`: Assigns fitness value to a sequence. -- `selection_strategy::SelectionStrategy`: Defines the algorithm used to select new parents from a pool of screened variants. -- `mutagenesis:Mutagenesis`: Defines the algorithm used to create new mutants from current population. +- `screening::AbstractScreening`: Assigns fitness value to a sequence. +- `selection_strategy::AbstractSelectionStrategy`: Defines the algorithm used to select new parents from a pool of screened variants. +- `mutagenesis::AbstractMutagenesis`: Defines the algorithm used to create new mutants from current population. - `n_iterations::Integer=1`: Specifies the number of iteration of DE. Has to be greater than 0. # Examples @@ -19,9 +19,9 @@ See 'https://github.com/soldamatlab/DESilico.jl/blob/master/scripts/custom_modul """ function de!( sequence_space::SequenceSpace; - screening::Screening, - selection_strategy::SelectionStrategy, - mutagenesis::Mutagenesis, + screening::AbstractScreening, + selection_strategy::AbstractSelectionStrategy, + mutagenesis::AbstractMutagenesis, n_iterations::Int=1, ) @assert n_iterations > 0 @@ -33,7 +33,7 @@ function de!( end end -function screeen_mutants(sequences::AbstractVector{Vector{Char}}, screening::Screening) +function screeen_mutants(sequences::AbstractVector{Vector{Char}}, screening::AbstractScreening) fitness = screening(sequences) map((s, f) -> Variant(s, f), sequences, fitness) end diff --git a/src/de_evaluation.jl b/src/de_evaluation.jl index b2eaec9..c0dd74a 100644 --- a/src/de_evaluation.jl +++ b/src/de_evaluation.jl @@ -9,18 +9,18 @@ Returns the top fitness obtained in each run as a `Vector{Float64}`. - `runs::Int`: Number of times `de!()` will be called. # Keywords -- `screening::Screening`: Assigns fitness value to a sequence. -- `selection_strategy::SelectionStrategy`: Defines the algorithm used to select new parents from a pool of screened variants. -- `mutagenesis:Mutagenesis`: Defines the algorithm used to create new mutants from current population. +- `screening::AbstractScreening`: Assigns fitness value to a sequence. +- `selection_strategy::AbstractSelectionStrategy`: Defines the algorithm used to select new parents from a pool of screened variants. +- `mutagenesis::AbstractMutagenesis`: Defines the algorithm used to create new mutants from current population. - `n_iterations::Integer`: Specifies the number of iteration of `de!()`. - `parallel::Bool=false`: If true, the calls of `de!()` will be run in parallel. """ function de_evaluation( sequence_space::SequenceSpace, runs::Int; - screening::Screening, - selection_strategy::SelectionStrategy, - mutagenesis::Mutagenesis, + screening::AbstractScreening, + selection_strategy::AbstractSelectionStrategy, + mutagenesis::AbstractMutagenesis, n_iterations::Int, parallel::Bool=false, ) @@ -45,17 +45,17 @@ Returns the top fitness obtained in each run as a `Vector{Float64}`. - `starting_variants::AbstractVector{Variant}`: Each of the `starting_variants` will be used as the sole initial parent in one run of `de!()`. # Keywords -- `screening::Screening`: Assigns fitness value to a sequence. -- `selection_strategy::SelectionStrategy`: Defines the algorithm used to select new parents from a pool of screened variants. -- `mutagenesis:Mutagenesis`: Defines the algorithm used to create new mutants from current population. +- `screening::AbstractScreening`: Assigns fitness value to a sequence. +- `selection_strategy::AbstractSelectionStrategy`: Defines the algorithm used to select new parents from a pool of screened variants. +- `mutagenesis::AbstractMutagenesis`: Defines the algorithm used to create new mutants from current population. - `n_iterations::Integer`: Specifies the number of iteration of `de!()`. - `parallel::Bool=false`: If true, the calls of `de!()` will be run in parallel. """ function de_evaluation( starting_variants::AbstractVector{Variant}; - screening::Screening, - selection_strategy::SelectionStrategy, - mutagenesis::Mutagenesis, + screening::AbstractScreening, + selection_strategy::AbstractSelectionStrategy, + mutagenesis::AbstractMutagenesis, n_iterations::Int, parallel::Bool=false, ) @@ -74,9 +74,9 @@ function _run_de_sequential!( results::Vector{Float64}, get_sequence_space::Function, runs::Int; - screening::Screening, - selection_strategy::SelectionStrategy, - mutagenesis::Mutagenesis, + screening::AbstractScreening, + selection_strategy::AbstractSelectionStrategy, + mutagenesis::AbstractMutagenesis, n_iterations::Int, ) for r = 1:runs @@ -96,9 +96,9 @@ function _run_de_parallel!( results::Vector{Float64}, get_sequence_space::Function, runs::Int; - screening::Screening, - selection_strategy::SelectionStrategy, - mutagenesis::Mutagenesis, + screening::AbstractScreening, + selection_strategy::AbstractSelectionStrategy, + mutagenesis::AbstractMutagenesis, n_iterations::Int, ) Threads.@threads :static for r = 1:runs diff --git a/src/mutagenesis/recombination.jl b/src/mutagenesis/recombination.jl index c0efc02..8f4a0c4 100644 --- a/src/mutagenesis/recombination.jl +++ b/src/mutagenesis/recombination.jl @@ -3,7 +3,7 @@ Creates all recombinations of parents sequences. Recombination() """ -struct Recombination <: Mutagenesis end +struct Recombination <: AbstractMutagenesis end function (m::Recombination)(parents::AbstractVector{Vector{Char}}) @assert DESilico.same_length_sequences(parents) diff --git a/src/mutagenesis/single_mutation.jl b/src/mutagenesis/single_mutation.jl index bde1a14..94d40be 100644 --- a/src/mutagenesis/single_mutation.jl +++ b/src/mutagenesis/single_mutation.jl @@ -6,7 +6,7 @@ Creates all single-symbol mutants for each parent sequence. # Arguments `alphabet::Set{Char}`: Contains the characters which will be used to create mutants. """ -struct SingleMutation <: Mutagenesis +struct SingleMutation <: AbstractMutagenesis alphabet::Set{Char} end diff --git a/src/screening/dict_screening.jl b/src/screening/dict_screening.jl index ed64d44..4553279 100644 --- a/src/screening/dict_screening.jl +++ b/src/screening/dict_screening.jl @@ -30,7 +30,7 @@ Constructs `DictScreening` from a '.xlsx' file. - `fitness_column::String`: Label in the first row of the column containing the fitness values. Default is "Fitness". - `default::Float64`: If present, the constructor returns `DictScreeningWithDefault` instead. """ -struct DictScreening <: Screening +struct DictScreening <: AbstractScreening fitness_dict::Dict{Vector{Char},Float64} end @@ -84,7 +84,7 @@ Constructs `DictScreeningWithDefault` via `DictScreening` constructors by adding `default::Float64`: Default fitness value returned for sequences not present in `fitness_dict`. See `DictScreening`. """ -struct DictScreeningWithDefault <: Screening +struct DictScreeningWithDefault <: AbstractScreening fitness_dict::Dict{Vector{Char},Float64} default::Float64 end diff --git a/src/selection_strategy/sampling_select.jl b/src/selection_strategy/sampling_select.jl index a5c919a..6334ee9 100644 --- a/src/selection_strategy/sampling_select.jl +++ b/src/selection_strategy/sampling_select.jl @@ -11,7 +11,7 @@ Randomly selects `k` sequences. # Keywords - `weighting::Float64`: Adding this argument to the constructer returns a `WeightedSamplingSelect` instead. """ -struct SamplingSelect <: SelectionStrategy +struct SamplingSelect <: AbstractSelectionStrategy k::Int SamplingSelect(k::Int) = k > 0 ? new(k) : throw(ArgumentError("`k` needs to be greater than 0")) @@ -40,7 +40,7 @@ or via `SamplingSelect` constructor by providing the `weighting` keyword: - `weighting::Float64`: Defines the influence of fitness values on the weighted probabilities on scale <0,1). """ -struct WeightedSamplingSelect <: SelectionStrategy +struct WeightedSamplingSelect <: AbstractSelectionStrategy k::Int weighting::Real diff --git a/src/selection_strategy/top_k.jl b/src/selection_strategy/top_k.jl index c2203ad..f67a46d 100644 --- a/src/selection_strategy/top_k.jl +++ b/src/selection_strategy/top_k.jl @@ -6,7 +6,7 @@ Selects `k` sequences with the highest fitness. # Arguments - `k::Int`: Defines the number of sequences which will be selected. """ -struct TopK <: SelectionStrategy +struct TopK <: AbstractSelectionStrategy k::Int TopK(k::Int) = k > 0 ? new(k) : throw(ArgumentError("`k` needs to be greater than 0")) end diff --git a/src/types/types.jl b/src/types/types.jl index 4840ffa..1fbe015 100644 --- a/src/types/types.jl +++ b/src/types/types.jl @@ -7,9 +7,9 @@ Structures derived from this type have to implement the following method: This method should return vector of selected sequences as a subtype of `AbstractVector{Vector{Char}}`. Structures derived from this type can have a parameter `k` which specifies the number of sequences that should be selected. -This can be useful with some Mutagenesis implementations that require a specific amount of selected sequences as input. +This can be useful with some AbstractMutagenesis implementations that require a specific amount of selected sequences as input. """ -abstract type SelectionStrategy end +abstract type AbstractSelectionStrategy end """ Specifies the algorithm used to create new sequences from a library of parent sequences. @@ -25,7 +25,7 @@ This can be useful with some SelectionStrategy implementations that require a sp Structures derived from this type can have a parameter `alphabet` which specifies the allowed characters in the sequences. """ -abstract type Mutagenesis end +abstract type AbstractMutagenesis end """ Specifies the oracle used to evaluate fitness of a sequence. @@ -34,4 +34,4 @@ Structures derived from this type have to implement the following method: `(::CustomScreening)(sequences::AbstractVector{Vector{Char}})` This method should return the sequences' fitness values as a subtype of `AbstarctVector{Float64}`. """ -abstract type Screening end +abstract type AbstractScreening end diff --git a/test/unit/de.jl b/test/unit/de.jl index 9741dea..d6dbe56 100644 --- a/test/unit/de.jl +++ b/test/unit/de.jl @@ -9,18 +9,18 @@ (['A', 'D', 'A', 'A'], 3.0), (['A', 'E', 'A', 'A'], 0.0), ]) - struct DummyScreening <: DESilico.Screening end + struct DummyScreening <: DESilico.AbstractScreening end (::DummyScreening)(sequence::Vector{Char}) = fitness_dict[sequence] (s::DummyScreening)(sequences::AbstractVector{Vector{Char}}) = map(sequence -> s(sequence), sequences) # Define a custom SelectionStrategy - struct DummySelectionStrategy <: DESilico.SelectionStrategy end + struct DummySelectionStrategy <: DESilico.AbstractSelectionStrategy end function (::DummySelectionStrategy)(variants::Vector{Variant}) [variants[1].sequence] end # Define a custom Mutagenesis - struct DummyMutagenesis <: DESilico.Mutagenesis end + struct DummyMutagenesis <: DESilico.AbstractMutagenesis end function (::DummyMutagenesis)(parents::Vector{Vector{Char}}) new_parent = copy(parents[1]) new_parent[2] = new_parent[2] + 1