Skip to content

Commit 7538152

Browse files
authored
Merge pull request #16 from nossleinad/copy_partition
Avoid deepcopy pt. 2
2 parents 53f75c0 + f8f92aa commit 7538152

File tree

6 files changed

+52
-3
lines changed

6 files changed

+52
-3
lines changed

src/MolecularEvolution.jl

+1
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ export
129129

130130
#things the user might overload
131131
copy_partition_to!,
132+
copy_partition,
132133
equilibrium_message,
133134
sample_partition!,
134135
obs2partition!,

src/core/nodes/FelNode.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ end
6060
function internal_message_init!(tree::FelNode, empty_message::Vector{<:Partition})
6161
for node in getnodelist(tree)
6262
if !isleafnode(node)
63-
node.child_messages = [deepcopy(empty_message) for i in node.children]
63+
node.child_messages = [copy_message(empty_message) for i in node.children]
6464
end
65-
node.message = deepcopy(empty_message)
66-
node.parent_message = deepcopy(empty_message)
65+
node.message = copy_message(empty_message)
66+
node.parent_message = copy_message(empty_message)
6767
end
6868
end
6969

src/models/continuous_models/gaussian_partition.jl

+5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ mutable struct GaussianPartition <: ContinuousPartition
1515
end
1616
end
1717

18+
#Overloading the copy_partition to avoid deepcopy.
19+
function copy_partition(src::GaussianPartition)
20+
return GaussianPartition(src.mean, src.var, src.norm_const)
21+
end
22+
1823
#From the first section of http://www.tina-vision.net/docs/memos/2003-003.pdf
1924
function merge_two_gaussians(g1::GaussianPartition, g2::GaussianPartition)
2025
#Handling some edge cases. These aren't mathematically sensible. A gaussian with "Inf" variance will behave like a 1,1,1,1 vector in discrete felsenstein.

src/models/discrete_models/codon_models.jl

+4
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,10 @@ mutable struct CodonPartition <: DiscretePartition
325325
zeros(sites),
326326
)
327327
end
328+
function CodonPartition(state, states, sites, scaling; code = universal_code)
329+
@assert size(state) == (states, sites) && states == length(code.sense_codons)
330+
new(state, states, sites, scaling)
331+
end
328332
end
329333

330334
#Make this handle IUPAC ambigs sensible. Any codon compatible with the ambig should get a 1.0

src/models/discrete_models/discrete_partitions.jl

+25
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ function copy_partition_to!(dest::T, src::T) where {T<:DiscretePartition}
1010
dest.scaling .= src.scaling
1111
end
1212

13+
#Overloading the copy_partition to avoid deepcopy.
14+
function copy_partition(src::T) where {T<:DiscretePartition}
15+
return T(copy(src.state), src.states, src.sites, copy(src.scaling))
16+
end
17+
1318
#I should add a constructor that constructs a DiscretePartition from an existing array.
1419
mutable struct CustomDiscretePartition <: DiscretePartition
1520
state::Array{Float64,2}
@@ -24,6 +29,10 @@ mutable struct CustomDiscretePartition <: DiscretePartition
2429
state_arr .= freq_vec
2530
new(state_arr, length(freq_vec), sites, zeros(sites))
2631
end
32+
function CustomDiscretePartition(state, states, sites, scaling)
33+
@assert size(state) == (states, sites)
34+
new(state, states, sites, scaling)
35+
end
2736
end
2837

2938
mutable struct NucleotidePartition <: DiscretePartition
@@ -40,6 +49,10 @@ mutable struct NucleotidePartition <: DiscretePartition
4049
state_arr .= freq_vec
4150
new(state_arr, 4, sites, zeros(sites))
4251
end
52+
function NucleotidePartition(state, states, sites, scaling)
53+
@assert size(state) == (states, sites) && states == 4
54+
new(state, states, sites, scaling)
55+
end
4356
end
4457

4558
mutable struct GappyNucleotidePartition <: DiscretePartition
@@ -56,6 +69,10 @@ mutable struct GappyNucleotidePartition <: DiscretePartition
5669
state_arr .= freq_vec
5770
new(state_arr, 5, sites, zeros(sites))
5871
end
72+
function GappyNucleotidePartitionPartition(state, states, sites, scaling)
73+
@assert size(state) == (states, sites) && states == 5
74+
new(state, states, sites, scaling)
75+
end
5976
end
6077

6178
mutable struct AminoAcidPartition <: DiscretePartition
@@ -72,6 +89,10 @@ mutable struct AminoAcidPartition <: DiscretePartition
7289
state_arr .= freq_vec
7390
new(state_arr, 20, sites, zeros(sites))
7491
end
92+
function AminoAcidPartition(state, states, sites, scaling)
93+
@assert size(state) == (states, sites) && states == 20
94+
new(state, states, sites, scaling)
95+
end
7596
end
7697

7798
mutable struct GappyAminoAcidPartition <: DiscretePartition
@@ -88,6 +109,10 @@ mutable struct GappyAminoAcidPartition <: DiscretePartition
88109
state_arr .= freq_vec
89110
new(state_arr, 21, sites, zeros(sites))
90111
end
112+
function GappyAminoAcidPartition(state, states, sites, scaling)
113+
@assert size(state) == (states, sites) && states == 21
114+
new(state, states, sites, scaling)
115+
end
91116
end
92117

93118
"""

src/models/models.jl

+14
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,20 @@ function copy_partition_to!(dest::GaussianPartition,src::GaussianPartition)
2121
end
2222
=#
2323

24+
#Fallback method. This should be overloaded to avoid deepcopy wherever performance requires it
25+
function copy_partition(src::T) where {T <: Partition}
26+
return deepcopy(src)
27+
end
28+
29+
#Example overloading for GaussianPartition:
30+
#=
31+
function copy_partition(src::GaussianPartition)
32+
return GaussianPartition(src.mean, src.var, src.norm_const)
33+
end
34+
=#
35+
36+
copy_message(msg::Vector{<:Partition}) = [copy_partition(x) for x in msg]
37+
2438
#This is a function shared for all models - perhaps move this elsewhere
2539
function combine!(dest::T, source_arr::Vector{<:T}, wipe::Bool) where {T<:Partition}
2640
#Init to be equal to 1, then multiply everything on.

0 commit comments

Comments
 (0)