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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
use super::{annotation::Annotation, clade::Clade, kmers_map::KmersMap};

use mycelium_base::utils::errors::MappedErrors;
use phylotree::tree::Tree as PhyloTree;
use serde::{Deserialize, Serialize};
use std::{ffi::OsStr, fs::read_to_string, mem::size_of_val, path::Path};
use uuid::Uuid;

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Tree {
    /// The unique identifier for the tree.
    ///
    /// The id is a unique identifier for the tree. The id is a Universally
    /// Unique Identifier (UUID) that is generated when the tree is created.
    pub id: Uuid,

    /// The human-readable name for the tree.
    ///
    /// When the tree is created using the from_file function, the name is set
    /// from the file name where the tree is parsed from.
    pub name: String,

    /// The minimum branch support value.
    ///
    /// The minimum branch support value is the minimum support value for a
    /// branch to be included in the tree. Branches with support values below
    /// this value are removed from the tree and children are reconnected to the
    /// parent node.
    pub min_branch_support: f64,

    /// The in-memory size of the tree (in Mb).
    ///
    /// This is usual to predict the memory usage of the tree index.
    in_memory_size: Option<String>,

    /// The root Clade of the tree.
    ///
    /// The root is the root Clade of the tree. The root Clade is the starting
    /// point of the tree and contains the children nodes.
    pub root: Clade,

    /// The annotations associated with the tree.
    ///
    /// The annotations are the taxonomic annotations associated with nodes in
    /// the tree. The annotations are stored as a vector of Annotation objects.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub annotations: Option<Vec<Annotation>>,

    //#[serde(skip_serializing_if = "Option::is_none")]
    pub kmers_map: Option<KmersMap>,
}

impl Tree {
    /// Create a new Tree object.
    ///
    /// The function creates a new Tree object with an id, name, and root Clade.
    /// The id is a unique identifier for the tree. The name is a human-readable
    /// name for the tree. The root is the root Clade of the tree.
    pub fn new(
        id: Uuid,
        name: String,
        min_branch_support: f64,
        root: Clade,
    ) -> Tree {
        Tree {
            id,
            name,
            min_branch_support,
            in_memory_size: None,
            root,
            annotations: None,
            kmers_map: None,
        }
    }

    pub fn get_in_memory_size(&self) -> Option<String> {
        self.in_memory_size.clone()
    }

    pub fn update_in_memory_size(&mut self) {
        let id_size = size_of_val(&self.id);

        let name_size = size_of_val(&self.name);

        let root_size = size_of_val(&self.root);

        let annotations_size = match &self.annotations {
            Some(annotations) => size_of_val(annotations),
            None => 0,
        };

        let kmers_map_size = match &self.kmers_map {
            Some(kmers_map) => size_of_val(kmers_map),
            None => 0,
        };

        self.in_memory_size = Some(format!(
            "{:.6} Mb",
            ((id_size
                + name_size
                + root_size
                + annotations_size
                + kmers_map_size)
                / 1_000_000) as f64
        ));
    }

    /// Pretty print the tree.
    ///
    /// The function prints the tree in a human-readable format. The function
    /// prints the root node and recursively prints each child node.
    pub fn pretty_print(&self) {
        println!("R: {}", self.name);

        for child in self.root.children.to_owned().unwrap() {
            self.pretty_print_clade(&child, 0);
        }
    }

    /// Pretty print a clade.
    ///
    /// The function prints a clade in a human-readable format. The function
    /// prints the clade id, name, and support value. If the clade is an
    /// internal node, the function prints the children nodes recursively.
    fn pretty_print_clade(&self, clade: &Clade, level: usize) {
        let mut indent = String::new();

        for _ in 0..level {
            indent.push_str("  ");
        }

        let id = clade.id;
        let name = clade.name.clone().unwrap_or("Unnamed".to_string());
        let support = clade.support.unwrap_or(-1.0);

        if clade.is_leaf() {
            println!("{}L:{}  {}", indent, id, name);
        } else {
            println!("{}I:{} ({})", indent, id, support);
        }

        if let Some(children) = clade.children.to_owned() {
            for child in children {
                self.pretty_print_clade(&child, level + 1);
            }
        }
    }

    pub fn from_yaml_file(file_path: &Path) -> Result<Tree, MappedErrors> {
        let file_content =
            read_to_string(file_path).expect("Could not read file");

        let tree: Tree =
            serde_yaml::from_str(&file_content).expect("Could not parse tree");

        Ok(tree)
    }

    /// Create a new Tree from a .newick file.
    ///
    /// The phylotre::tree::Tree is parsed from the file and converted to a Tree
    /// object with a root Clade.
    pub fn init_from_file(
        tree_path: &Path,
        min_branch_support: f64,
    ) -> Result<Tree, MappedErrors> {
        assert!(
            vec!["nwk", "newick", "tree"].contains(
                &tree_path
                    .extension()
                    .and_then(OsStr::to_str)
                    .expect("Could not get extension")
            ),
            "Tree file format is not supported"
        );

        let newick_content =
            read_to_string(tree_path).expect("Could not read file");

        let phylo_tree = PhyloTree::from_newick(&newick_content.as_str())
            .expect("Could not parse tree");

        let root_name = (if let Some(name) = tree_path.file_name() {
            Some(
                name.to_str()
                    .expect("Could not convert path to string")
                    .to_string(),
            )
        } else {
            None
        })
        .unwrap_or("UnnamedTree".to_string());

        if !phylo_tree.is_rooted().unwrap_or(false) {
            panic!("Tree is not rooted");
        }

        let root_tree = match phylo_tree.get_root() {
            Err(err) => panic!("Could not get root: {err}"),
            Ok(root) => phylo_tree.get(&root).expect("Could not get root"),
        };

        if !root_tree.is_root() {
            panic!("Root node is not a root");
        }

        let children = Self::get_children_nodes(&phylo_tree, &root_tree.id);

        let sanitized_root =
            Tree::sanitize(Clade::new_root(0.0, children), min_branch_support)?;

        let mut new_tree = Tree::new(
            Uuid::new_v3(&Uuid::NAMESPACE_DNS, &*root_name.as_bytes()),
            root_name,
            min_branch_support,
            sanitized_root,
        );

        new_tree.root = Self::fix_parent_ids(&mut new_tree.root, None);

        Ok(new_tree)
    }

    fn fix_parent_ids(clade: &mut Clade, parent: Option<u64>) -> Clade {
        clade.children = if let Some(children) = clade.children.to_owned() {
            let children = children
                .into_iter()
                .map(|mut child| {
                    child.parent = Some(clade.id);
                    Self::fix_parent_ids(&mut child, Some(clade.id))
                })
                .collect::<Vec<Clade>>();

            Some(children)
        } else {
            None
        };

        clade.parent = parent;
        clade.clone()
    }

    /// Remove low supported branches
    ///
    /// The function removes low supported branches from the tree, reconnecting
    /// children to the parent node.
    fn sanitize(
        clade: Clade,
        min_branch_support: f64,
    ) -> Result<Clade, MappedErrors> {
        let mut children = Vec::<Clade>::new();

        if let Some(clade_children) = clade.to_owned().children {
            for child in clade_children {
                let sanitized_child =
                    Self::sanitize(child, min_branch_support)?;

                if let Some(support) = sanitized_child.support {
                    if support >= min_branch_support
                        || sanitized_child.is_leaf()
                    {
                        children.push(sanitized_child);
                    } else {
                        if let Some(grand_children) = sanitized_child.children {
                            for grand_child in grand_children {
                                children.push(grand_child);
                            }
                        }
                    }
                } else {
                    children.push(sanitized_child);
                }
            }
        }

        let mut sanitized_clade = clade.clone();

        sanitized_clade.children = match children.len() {
            0 => None,
            _ => Some(children),
        };

        Ok(sanitized_clade)
    }

    /// Recursively extract children nodes from a PhyloTree.
    ///
    /// The function recursively extracts children nodes from a PhyloTree and
    /// creates a Clade object for each node. The function returns a vector of
    /// Clade objects.
    fn get_children_nodes(
        tree: &PhyloTree,
        node_id: &usize,
    ) -> Option<Vec<Clade>> {
        if let Ok(node) = tree.get(node_id) {
            let mut children = Vec::<Clade>::new();

            //
            // Each child node is a single level below the parent node.
            //
            for child_id in node.children.to_owned() {
                //
                // Try to extract children node by node id.
                //
                let child_node =
                    tree.get(&child_id).expect("Child node not found");

                //
                // If the child node is a tip, create a new leaf Clade and
                // insert into the children vector.
                //
                if child_node.is_tip() {
                    let leaf_node = Clade::new_leaf(
                        child_node.id.try_into().expect("Could not convert id"),
                        node_id.to_owned() as u64,
                        child_node
                            .name
                            .clone()
                            .unwrap_or("Unnamed".to_string()),
                        child_node.parent_edge,
                    );
                    children.push(leaf_node);

                //
                // Otherwise, try to extract children nodes from the child node,
                // create a new internal Clade and insert into the children
                // vector.
                //
                } else {
                    let nested_children =
                        Self::get_children_nodes(tree, &child_id);

                    if let Some(nested_children) = nested_children {
                        let internal_node = Clade::new_internal(
                            child_node
                                .id
                                .try_into()
                                .expect("Could not convert id"),
                            node_id.to_owned() as u64,
                            None,
                            match child_node.name.clone() {
                                Some(name) => match name.parse::<f64>() {
                                    Err(_) => None,
                                    Ok(value) => Some(value),
                                },
                                None => None,
                            },
                            child_node.parent_edge,
                            Some(nested_children),
                        );

                        children.push(internal_node);
                    } else {
                        return None;
                    }
                }
            }

            return Some(children);
        }

        return None;
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::path::PathBuf;

    #[test]
    fn test_tree_from_file() {
        let path = PathBuf::from("src/tests/data/colletotrichum-acutatom-complex/inputs/Colletotrichum_acutatum_gapdh-PhyML.nwk");

        let tree = Tree::init_from_file(&path, 80.0);

        assert!(tree.is_ok());

        tree.unwrap().pretty_print();
    }
}