forked from sriram98v/phylo-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.rs
192 lines (167 loc) · 4.26 KB
/
node.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#![allow(clippy::needless_lifetimes)]
/// Module with traits of rooted tree nodes
pub mod simple_rnode;
use crate::node::simple_rnode::{
RootedMetaNode, RootedTreeNode, RootedWeightedNode, RootedZetaNode, EdgeWeight, NodeWeight, NodeTaxa
};
use std::fmt::{Debug, Display};
/// Default NodeID type
pub type NodeID = usize;
/// Default NodeID type
pub type PhyloNode = Node<String,f32,f32>;
/// A node structure in an arena-memory managed tree, linking to connected neighbours via NodeID
#[derive(Clone)]
pub struct Node<T,W,Z>
where
T: NodeTaxa,
W: EdgeWeight,
Z: NodeWeight,
{
/// A unique identifier for a node
id: NodeID,
/// A link to the node parent (set to None for root)
parent: Option<NodeID>,
/// Children of node
children: Vec<NodeID>,
/// Taxa annotation of node
taxa: Option<T>,
/// Weight of edge ending in node
weight: Option<W>,
/// Real number annotation of node (used by some algorithms)
zeta: Option<Z>,
}
impl<T,W,Z> RootedTreeNode for Node<T,W,Z>
where
T: NodeTaxa,
W: EdgeWeight,
Z: NodeWeight,
{
type NodeID = NodeID;
fn new(id: Self::NodeID) -> Self {
Node {
id,
parent: None,
children: vec![],
taxa: None,
weight: None,
zeta: None,
}
}
fn get_id(&self) -> Self::NodeID {
self.id
}
fn set_id(&mut self, id: Self::NodeID) {
self.id = id
}
fn set_parent(&mut self, parent: Option<Self::NodeID>) {
self.parent = parent;
}
fn get_parent(&self) -> Option<Self::NodeID> {
self.parent
}
fn get_children(&self) -> impl ExactSizeIterator<Item = Self::NodeID> + DoubleEndedIterator {
self.children.clone().into_iter()
}
fn add_child(&mut self, child: Self::NodeID) {
self.children.push(child);
}
fn remove_child(&mut self, child: &Self::NodeID) {
self.children.retain(|x| x != child);
}
}
impl<T,W,Z> RootedMetaNode for Node<T,W,Z>
where
T: NodeTaxa,
W: EdgeWeight,
Z: NodeWeight,
{
type Meta = T;
fn get_taxa<'a>(&'a self) -> Option<&'a Self::Meta> {
self.taxa.as_ref()
}
fn set_taxa(&mut self, taxa: Option<Self::Meta>) {
self.taxa = taxa;
}
}
impl<T,W,Z> RootedWeightedNode for Node<T,W,Z>
where
T: NodeTaxa,
W: EdgeWeight,
Z: NodeWeight,
{
type Weight = W;
fn get_weight(&self) -> Option<Self::Weight> {
self.weight
}
fn set_weight(&mut self, w: Option<Self::Weight>) {
self.weight = w;
}
}
impl<T,W,Z> RootedZetaNode for Node<T,W,Z>
where
T: NodeTaxa,
W: EdgeWeight,
Z: NodeWeight,
{
type Zeta = Z;
fn get_zeta(&self) -> Option<Self::Zeta> {
self.zeta
}
fn set_zeta(&mut self, w: Option<Self::Zeta>) {
self.zeta = w;
}
}
impl<T,W,Z> Debug for Node<T,W,Z>
where
T: NodeTaxa,
W: EdgeWeight,
Z: NodeWeight,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}:{}:{}:{}:{}",
self.get_id(),
self.node_type(),
match self.get_taxa() {
None => "No Taxa".to_string(),
Some(t) => t.to_string(),
},
match self.get_weight() {
None => "Unweighted".to_string(),
Some(t) => t.to_string(),
},
match self.get_zeta() {
None => "No Zeta".to_string(),
Some(z) => z.to_string(),
}
)
}
}
impl<T,W,Z> Display for Node<T,W,Z>
where
T: NodeTaxa,
W: EdgeWeight,
Z: NodeWeight,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}:{}:{}:{}:{}",
self.get_id(),
self.node_type(),
match self.get_taxa() {
None => "None".to_string(),
Some(t) => t.to_string(),
},
match self.get_weight() {
None => "".to_string(),
Some(t) => t.to_string(),
},
match self.get_zeta() {
None => "No Zeta".to_string(),
Some(z) => z.to_string(),
}
)
}
}