Skip to content

Commit

Permalink
Rename abstract types
Browse files Browse the repository at this point in the history
  • Loading branch information
soldatmat committed Feb 21, 2024
1 parent 9229b4c commit 4011a82
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 48 deletions.
6 changes: 3 additions & 3 deletions scripts/custom_modules_example.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ 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)

# We define a custom SelectionStrategy.
# 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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions scripts/de_sandbox.jl
Original file line number Diff line number Diff line change
@@ -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
Expand Down
8 changes: 4 additions & 4 deletions scripts/profiling.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand Down
14 changes: 7 additions & 7 deletions src/de.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
36 changes: 18 additions & 18 deletions src/de_evaluation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand All @@ -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,
)
Expand All @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/mutagenesis/recombination.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/mutagenesis/single_mutation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions src/screening/dict_screening.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/selection_strategy/sampling_select.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/selection_strategy/top_k.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions src/types/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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
6 changes: 3 additions & 3 deletions test/unit/de.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4011a82

Please sign in to comment.