Skip to content

Commit 14d7b89

Browse files
authored
give writemultinewick same options as writenewick (#244)
also: * v1.3.1 * new method for getconnectingedge; existing (and new) method return nothing instead of an error if a connecting edge doesn't exists
1 parent c4973f9 commit 14d7b89

File tree

5 files changed

+52
-16
lines changed

5 files changed

+52
-16
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "PhyloNetworks"
22
uuid = "33ad39ac-ed31-50eb-9b15-43d0656eaa72"
33
license = "MIT"
4-
version = "1.3.0"
4+
version = "1.3.1"
55

66
[deps]
77
BioSequences = "7e6ae17a-c86d-528c-b3b9-7f778a29fe59"

src/auxiliary.jl

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -458,8 +458,8 @@ end
458458
"""
459459
getconnectingedge(node1::Node, node2::Node)
460460
461-
Edge shared by (or connecting) `node1` and `node2`, that is: edge incident
462-
to both nodes. An error is thrown if the 2 nodes are not connected.
461+
Edge shared by (or connecting) `node1` and `node2`, that is, edge incident
462+
to both nodes; or `nothing` if the nodes are not adjacent (without error).
463463
464464
See also [`isconnected`](@ref)
465465
"""
@@ -469,15 +469,36 @@ function getconnectingedge(node1::Node, node2::Node)
469469
e1 === e2 && return e1
470470
end
471471
end
472-
error("nodes not connected")
472+
return nothing
473+
end
474+
"""
475+
getconnectingedge(n1_number::Int, n2_number::Int, net::HybridNetwork)
476+
477+
Edge in `net` that is incident to nodes `n1` and `n2` of specified numbers;
478+
or `nothing` if `n1` and `n2` are not adjacent and no such edge exists
479+
(without any error).
480+
481+
Warning: if `net` has parallel edges, there might 2+ (hybrid and partner)
482+
edges connecting `n1` and `n2`. One of them is returned, arbitrarily.
483+
"""
484+
function getconnectingedge(n1num::Int, n2num::Int, net::HybridNetwork)
485+
n1i = findfirst(n -> n.number == n1num, net.node)
486+
isnothing(n1i) && error("no node numbered $n1num")
487+
n1 = net.node[n1i]
488+
for e in n1.edge
489+
n2 = getOtherNode(e,n1)
490+
if n2.number == n2num
491+
return e
492+
end
493+
end
473494
return nothing
474495
end
475496

476497
"""
477-
isconnected(node1::Node, node2::Node)
498+
isconnected(n1::Node, n2::Node)
478499
479-
Check if two nodes are connected by an edge. Return true if connected, false
480-
if not connected.
500+
`true` if nodes `n1` and `n2` are adjacent, that is, connected by an edge;
501+
`false` otherwise.
481502
482503
See also [`getconnectingedge`](@ref)
483504
"""

src/readwrite.jl

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1202,18 +1202,30 @@ julia> writemultinewick(net, stdout) # to write to the screen (standard
12021202
(G,H,F);
12031203
```
12041204
"""
1205-
function writemultinewick(n::Vector{HybridNetwork},file::AbstractString; append::Bool=false)
1205+
function writemultinewick(
1206+
n::Vector{HybridNetwork},
1207+
file::AbstractString;
1208+
append::Bool=false,
1209+
kwargs...
1210+
)
12061211
mode = (append ? "a" : "w")
12071212
open(file, mode) do s
1208-
writemultinewick(n,s)
1213+
writemultinewick(n,s; kwargs...)
12091214
end # closes file safely
12101215
end
12111216

1212-
function writemultinewick(net::Vector{HybridNetwork},s::IO)
1217+
function writemultinewick(
1218+
net::Vector{HybridNetwork},
1219+
s::IO;
1220+
round::Bool=false,
1221+
digits::Integer=3,
1222+
di::Bool=false,
1223+
internallabel::Bool=true,
1224+
support=nothing,
1225+
)
12131226
for i in 1:length(net)
12141227
try
1215-
# writeTopologyLevel1(net[i],s,false,true,"none",false,false,3)
1216-
writenewick(net[i],s) # no rounding, not for dendroscope
1228+
writenewick(net[i],s,round,digits,di,internallabel,support)
12171229
write(s,"\n")
12181230
catch err
12191231
if isa(err, RootMismatch) # continue writing other networks in list

test/test_auxiliary.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ PhyloNetworks.EdgeT{PhyloNetworks.Node}:
5656
@test PhyloNetworks.getIndexNode(net0.edge[10], net0.node[8]) == 1
5757
@test PhyloNetworks.getIndexHybrid(net0.node[8], net) == 1
5858
@test PhyloNetworks.getconnectingedge(net0.node[8], net0.node[13]) === net0.edge[10]
59+
@test isnothing(PhyloNetworks.getconnectingedge(net0.node[8], net0.node[12]))
60+
@test PhyloNetworks.getconnectingedge(1, -4, net0) === net0.edge[1]
61+
@test isnothing(PhyloNetworks.getconnectingedge(2, -5, net0))
5962
PhyloNetworks.deleteIntNode!(net0, getroot(net0))
6063
@test net0.numedges == 19
6164
@test net0.numnodes == 18

test/test_relaxed_reading.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,11 @@ end
7676
@test_logs (:warn, r"^γ read") readnewick("(a:::.5);")
7777
@test_logs (:warn, r"^partners: 2 with no γ, 3 with γ>1.") readnewick("((b)#H1,#H1:::1.1);")
7878
@test_logs (:warn, r"^partners: 3 with no γ, 2 with γ>1.") readnewick("((b)#H1:::1.1,#H1);")
79-
net = readnewick("(a, b):0.5;")
79+
net = readnewick("(a, b:0.123:0.81);")
8080
tmpfile = "tmp.nwk"
81-
writemultinewick([net], tmpfile)
82-
writenewick(net, tmpfile; append=true, support=true)
83-
@test readlines(tmpfile) == ["(a,b);","(a,b);"]
81+
writemultinewick([net], tmpfile; round=true, digits=1, support=:y)
82+
writenewick(net, tmpfile; append=true)
83+
@test readlines(tmpfile) == ["(a,b:0.1:0.8);","(a,b:0.123);"]
8484
rm(tmpfile)
8585
@test_throws "not find or open tmp.nwk" readnewick(tmpfile)
8686
@test writenewick(readnewick("((a,(b)#H1)i1,(#H1,c))r;")) == "((a,(b)#H1)i1,(#H1,c))r;"

0 commit comments

Comments
 (0)