Skip to content

Commit

Permalink
more specific name, docstring, cosmetic changes
Browse files Browse the repository at this point in the history
  • Loading branch information
cecileane committed Jul 7, 2022
1 parent faf1aa5 commit 79ecc71
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
27 changes: 17 additions & 10 deletions src/traits.jl
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,9 @@ Returns an object of type [`MatrixTopologicalOrder`](@ref).
"""
function sharedPathMatrix(net::HybridNetwork;
checkPreorder=true::Bool)
checkBranchLengths(net::HybridNetwork)
check_nonmissing_nonnegative_edgelengths(net,
"""The variance-covariance matrix of the network is not defined.
A phylogenetic regression cannot be done.""")
recursionPreOrder(net,
checkPreorder,
initsharedPathMatrix,
Expand Down Expand Up @@ -383,16 +385,21 @@ function initsharedPathMatrix(nodes::Vector{Node}, params)
return(zeros(Float64,n,n))
end

function checkBranchLengths(net::HybridNetwork)
branches = [e.number for e in net.edge]
undefined = branches[[e.length == -1.0 for e in net.edge]]
negatives = branches[[e.length < 0 for e in net.edge]]
str = "The variance-covariance matrix of the network is not defined, and the phylogenetic regression cannot be done."
if (length(undefined) > 0)
error(string("Branches ", undefined, " have no length in the network. ", str))
"""
check_nonmissing_nonnegative_edgelengths(net, str="")
Throw an Exception if `net` has undefined edge lengths (coded as -1.0) or
negative edge lengths. The error message indicates the number of the offending
edge(s), followed by `str`.
"""
function check_nonmissing_nonnegative_edgelengths(net::HybridNetwork, str="")
if any(e.length == -1.0 for e in net.edge)
undefined = [e.number for e in net.edge if e.length == -1.0]
error(string("Branch(es) number ", join(undefined,","), " have no length.\n", str))
end
if (length(negatives) > 0)
error(string("Branches ", negatives, " have a negative or zero length in the network. ", str))
if any(e.length < 0 for e in net.edge)
negatives = [e.number for e in net.edge if e.length < 0.0]
error(string("Branch(es) number ", join(negatives,","), " have negative length.\n", str))
end
end

Expand Down
17 changes: 9 additions & 8 deletions test/test_lm.jl
Original file line number Diff line number Diff line change
Expand Up @@ -631,19 +631,20 @@ end
## No branch length
net = readTopology("(A:2.5,((B,#H1:1::0.1):1,(C:1,(D:1)#H1:1::0.9):1):0.5);");
dfr = DataFrame(trait = [11.6,8.1,10.3,9.1], tipNames = ["A","B","C","D"]);
@test_throws ErrorException("Branches [2] have no length in the network. The variance-covariance matrix of the network is not defined, and the phylogenetic regression cannot be done.") phylolm(@formula(trait ~ 1), dfr, net);
@test_throws ErrorException("""Branch(es) number 2 have no length.
The variance-covariance matrix of the network is not defined.
A phylogenetic regression cannot be done.""") phylolm(@formula(trait ~ 1), dfr, net);
## Negative branch length
net.edge[2].length = -0.5;
@test_throws ErrorException("Branches [2] have a negative or zero length in the network. The variance-covariance matrix of the network is not defined, and the phylogenetic regression cannot be done.") phylolm(@formula(trait ~ 1), dfr, net);
## Zero branch length
@test_throws ErrorException("""Branch(es) number 2 have negative length.
The variance-covariance matrix of the network is not defined.
A phylogenetic regression cannot be done.""") phylolm(@formula(trait ~ 1), dfr, net);
## Zero branch length: allowed
net.edge[2].length = 0.0;
fit = phylolm(@formula(trait ~ 1), dfr, net);
@test loglikelihood(fit) -6.245746681512051
## Non zero branch length
net.edge[2].length = 0.1;
fit = phylolm(@formula(trait ~ 1), dfr, net);
@test loglikelihood(fit) -6.2197697662066815
## Illicit zero branch length
net.edge[2].length = 0.1; # back to non-zero
## Illicit zero length for 1st edge: from root to single "outgroup" taxon
net.edge[1].length = 0.0;
@test_throws PosDefException phylolm(@formula(trait ~ 1), dfr, net);
end
Expand Down

0 comments on commit 79ecc71

Please sign in to comment.