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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
#![recursion_limit = "1024"]
#[macro_use]
extern crate diesel;
#[macro_use]
extern crate diesel_migrations;
extern crate dotenv;
extern crate thiserror;
extern crate indextree;
extern crate core;
extern crate seq_io;
extern crate clap;

/// ncbitaxonomy: a module for working with a local copy of the NCBI taxonomy database

use thiserror::Error;
use std::io;

#[derive(Error, Debug)]
pub enum NcbiTaxonomyError {
    #[error(transparent)]
    Io(#[from] io::Error),
    #[error("format error in nodes.dmp in line {0}")]
    NodeFileFormatError(String),
    #[error(transparent)]
    ParseIntError(#[from] ::std::num::ParseIntError)
}

#[derive(Error, Debug)]
pub enum ToSqliteError {
    #[error(transparent)]
    Diesel(#[from] diesel::result::Error),
    #[error(transparent)]
    MigrationError(#[from] diesel_migrations::RunMigrationsError),
    #[error("Error looking up id {0}")]
    IdLookupError(String)
}

use std::collections::{HashMap, HashSet};
use std::fs::File;
use std::io::{BufReader,BufRead};
use indextree::{Arena, NodeId, Traverse};
pub use indextree::NodeEdge;
use std::iter::FromIterator;

pub mod models;
pub mod schema;

use diesel::prelude::*;
use diesel::sqlite::SqliteConnection;
use dotenv::dotenv;
use std::env;

use self::models::*;
use diesel::expression::dsl::count;

pub const VERSION: &str = env!("CARGO_PKG_VERSION");

fn establish_connection(db_url: Option<&str>) -> SqliteConnection {
    dotenv().ok();

    let database_url = match db_url {
        Some(val) => val.to_owned(),
        None => env::var("DATABASE_URL").expect("DATABASE_URL must be set")
    };

    SqliteConnection::establish(&database_url)
        .unwrap_or_else(|_| panic!("Error connecting to {}", database_url))
}

fn get_canonical_ranks() -> HashSet<String> {
    // canonical ranks (+ superkingdom) as they appear in the NCBI taxonomy database
    HashSet::from_iter(vec!["superkingdom", "kingdom", "phylum", "class", "order", "family", "genus", "species"].iter().map(|x| (*x).to_string()))
}

pub trait NcbiTaxonomy {
    fn contains_id(&self, taxid: i32) -> bool;
    fn contains_name(&self, name: &str) -> bool;
    fn is_descendant(&self, name: &str, ancestor_name: &str) -> bool;
    fn is_descendant_taxid(&self, taxid: i32, ancestor_taxid: i32) -> bool;
    fn get_name_by_id(&self, taxid: i32) -> Option<String>;
    fn get_id_by_name(&self, name: &str) -> Option<i32>;
    fn get_lineage(&self, name: &str) -> Option<Vec<i32>>;
    fn get_distance_to_common_ancestor_taxid(&self, taxid1: i32, taxid2: i32, only_canonical: bool) -> Option<(i32, i32)>;
    fn get_distance_to_common_ancestor(&self, name1: &str, name2: &str, only_canonical: bool) -> Option<(i32, String)>;
}

#[derive(Debug)]
pub struct NcbiFileTaxonomy {
    arena: Arena<i32>,
    name_to_node: HashMap<String, NodeId>,
    id_to_node: HashMap<i32, NodeId>,
    id_to_name: HashMap<i32, String>,
    id_to_rank: HashMap<i32, String>
}

impl NcbiFileTaxonomy {

    /// from_ncbi_files
    ///
    /// Reads the `nodes.dmp` file and `names.dmp` file from the NCBI Taxonomy database to
    /// generate a NcbiTaxonomy structure
    ///
    /// # Examples
    ///
    /// ```
    /// use ncbitaxonomy::*;
    ///
    /// let taxonomy = NcbiFileTaxonomy::from_ncbi_files("data/nodes.dmp", "data/names.dmp");
    /// ```
    pub fn from_ncbi_files(nodes_filename: &str, names_filename: &str) -> Result<NcbiFileTaxonomy, NcbiTaxonomyError> {
        let mut child_ids_by_parent_id: HashMap<i32, Vec<i32>> = HashMap::new();
        let mut id_to_rank = HashMap::new();
        let nodes_file = File::open(nodes_filename)?;
        for line_maybe in BufReader::new(nodes_file).lines() {
            let line = line_maybe?;
            let mut fields = line.split("\t|\t");
            let id_str = fields.next().ok_or_else(|| NcbiTaxonomyError::NodeFileFormatError(line.clone()))?;
            let parent_id_str = fields.next().ok_or_else(|| NcbiTaxonomyError::NodeFileFormatError(line.clone()))?;
            let rank = fields.next().ok_or_else(|| NcbiTaxonomyError::NodeFileFormatError(line.clone()))?.to_string();
            let id = id_str.parse::<i32>()?;
            let parent_id = parent_id_str.parse::<i32>()?;
            id_to_rank.insert(id, rank);
            if parent_id != id {  // this happens for the root node
                // thanks to https://stackoverflow.com/questions/33243784/append-to-vector-as-value-of-hashmap/33243862
                // for this way to get the existing entry or insert an empty list.
                child_ids_by_parent_id.entry(parent_id).or_insert_with(Vec::new).push(id);
            }
        }

        let mut keys = child_ids_by_parent_id.keys().collect::<Vec<&i32>>();
        keys.sort_unstable_by(|a, b| a.cmp(b));

        let mut arena: Arena<i32> = Arena::new();

        let mut id_to_node: HashMap<i32, NodeId> = HashMap::new();
        for id in keys {
            let node_id = match id_to_node.get(id) {
                Some(node_id) => *node_id,
                None => arena.new_node(*id),
            };
            id_to_node.insert(*id, node_id);
            for child in child_ids_by_parent_id.get(&id).expect("ID not found in child_ids_by_parent_id") {
                let child_node_id = match id_to_node.get(child) {
                    Some(child_node_id) => *child_node_id,
                    None => arena.new_node(*child),
                };
                id_to_node.insert(*child, child_node_id);
                assert_ne!(node_id, child_node_id, "child node id same as node: {} (for {} {})", node_id, *id, *child);
                node_id.append(child_node_id, &mut arena).unwrap();  // might return Failure, in which case we panic!
            }
        }

        // now its time to read the names_filename that maps names to IDs
        let mut name_to_node = HashMap::new();
        let mut id_to_name = HashMap::new();
        let name_file = File::open(names_filename)?;
        for line_maybe in BufReader::new(name_file).lines() {
            let line = line_maybe?;
            let fields = line.split("\t|\t").collect::<Vec<&str>>();
            if fields[3].starts_with("scientific name") {
                let id_str = fields[0];
                let id = id_str.parse::<i32>().or_else(|e| Err(NcbiTaxonomyError::ParseIntError(e)))?;
                let name = if fields[2] != "" { fields[2].to_string() } else { fields[1].to_string() };
                let node_id = id_to_node.get(&id).expect("ID not found in id_to_node");
                id_to_name.insert(id, name.clone());
                name_to_node.insert(name, *node_id);
            }
        }

        let tree = NcbiFileTaxonomy { arena, name_to_node, id_to_node, id_to_name, id_to_rank };
        Ok(tree)
    }

    pub fn save_to_sqlite(&self, db_url: Option<&str>) -> Result<SqliteConnection, ToSqliteError> {
        // design of storing a tree in a relational DB inspired by:
        // https://makandracards.com/makandra/45275-storing-trees-in-databases
        use schema::taxonomy;
        let connection = establish_connection(db_url);

        embed_migrations!();
        embedded_migrations::run(&connection)?;

        connection.transaction::<_, ToSqliteError, _>(|| {
            for (id, nodeid) in self.id_to_node.iter() {
                let mut ancestors_vec = nodeid.ancestors(&self.arena).map(|nodeid| self.get_id_by_node(nodeid).unwrap().to_string()).collect::<Vec<String>>();
                ancestors_vec.reverse();
                let ancestors_string = ancestors_vec.join("/");
                let name = match self.id_to_name.get(id) {
                    Some(val) => val,
                    None => { return Err(ToSqliteError::IdLookupError(id.to_string())) }
                };

                let taxon_record = NewTaxon {
                    id,
                    ancestry: match ancestors_string  {
                        v if v == "1" => None,
                        _ => Some(&ancestors_string[..])
                    },
                    name,
                    rank: match self.id_to_rank.get(id) {
                        Some(v) => Some(&v[..]),
                        None => None
                    }

                };
                diesel::insert_into(taxonomy::table)
                    .values(&taxon_record   )
                    .execute(&connection)?;
            }
            Ok(())
        })?;
        Ok(connection)
    }

    /// get_node_by_id
    ///
    /// get a NodeId from a numeric NCBI Taxonomy ID
    pub fn get_node_by_id(&self, id: i32) -> Option<&NodeId> {
        self.id_to_node.get(&id)
    }

    /// traversal
    ///
    /// traverse the tree nodes (in depth first order) from the node with a given NCBI Taxonomy ID
    pub fn traversal(&self, from: i32) -> Option<Traverse<i32>> {
        match self.get_node_by_id(from) {
            Some(node_id) => Some(node_id.traverse(&self.arena)),
            None => None
        }
    }

    /// get_id_by_node
    ///
    /// get the NCBI Taxonomy ID held by the node with a given NodeId
    pub fn get_id_by_node(&self, node_id: NodeId) -> Option<i32> {
        match self.arena.get(node_id)  {
            Some(node) => Some(node.data),
            None => None
        }
    }
}

impl NcbiTaxonomy for NcbiFileTaxonomy {
    /// contains_id
    ///
    /// check whether the taxonomy contains a (number) ID
    fn contains_id(&self, id: i32) -> bool {
        self.id_to_node.contains_key(&id)
    }

    /// contains_name
    ///
    /// check whether the taxonomy contains a node with the specified name
    ///
    /// **note:** the name used is what is reported as a the 'scientific name' in the NCBI Taxonomy database.
    /// synonyms are currently not supported
    fn contains_name(&self, name: &str) -> bool {
        self.name_to_node.contains_key(name)
    }

    /// is_descendant
    ///
    /// check if a certain named node is a descendant of another named named
    fn is_descendant(&self, name: &str, ancestor_name: &str) -> bool {
        let id = match self.get_id_by_name(name) {
            Some(id) => id,
            None => return false
        };
        let ancestor_id = match self.get_id_by_name(ancestor_name) {
            Some(id) => id,
            None => return false
        };
        self.is_descendant_taxid(id, ancestor_id)
    }

    /// is_descendant_taxid
    ///
    /// check if a certain node with taxid is a descendant of another taxid
    fn is_descendant_taxid(&self, taxid: i32, ancestor_taxid: i32) -> bool {
        let id = match self.id_to_node.get(&taxid) {
            Some(id) => id,
            None => return false
        };
        let ancestor_id = match self.id_to_node.get(&ancestor_taxid) {
            Some(id) => id,
            None => return false
        };
        for node in id.ancestors(&self.arena) {
            if node == *ancestor_id {
                return true
            }
        }
        false
    }

    /// get_name_by_id
    ///
    /// get the scientific name associated with a given NCBI Taxonomy ID
    fn get_name_by_id(&self, id: i32) -> Option<String> {
        self.id_to_name.get(&id).cloned()
    }

    fn get_id_by_name(&self, name: &str) -> Option<i32> {
        match self.name_to_node.get(name) {
            Some(nodeid) => self.get_id_by_node(*nodeid),
            None => None
        }
    }

    /// get_lineage
    ///
    /// get the list of IDs of a taxon and its parents (up to the root)
    fn get_lineage(&self, name: &str) -> Option<Vec<i32>> {
        let node_id = match self.name_to_node.get(name)  {
            Some(val) => val,
            None => return None
        };

        Some(node_id.ancestors(&self.arena).map(|node_id| *(&self.get_id_by_node(node_id).unwrap())).collect())
    }

    /// get_distance_to_common_ancestor_id
    ///
    /// get the distance (in steps in the tree) between taxid1 and the common ancestor with taxid2
    fn get_distance_to_common_ancestor_taxid(&self, taxid1: i32, taxid2: i32, only_canonical: bool) -> Option<(i32, i32)> {
        let canonical_ranks = get_canonical_ranks();
        if taxid1 == taxid2 {
            return Some((0, taxid1))
        }

        let taxon1 = self.id_to_node.get(&taxid1)?;
        let taxon2 = self.id_to_node.get(&taxid2)?;

        let mut ancestors_distance1 = HashMap::new();
        let mut current_distance = 0;
        let taxid1_rank = self.id_to_rank.get(&taxid1)?;
        if !only_canonical || canonical_ranks.contains(taxid1_rank) {
            // we know that taxid1 != taxid2, so either taxid2 is an ancestor of
            // taxid1 or there is a common ancestor further back. in the first case,
            // taxid2 will be found in the ancestors of taxid1, so thifs distance will
            // not be used. in the second case, taxid1 might be found in the ancestors
            // of taxid2, so this distance will still not be used
            ancestors_distance1.insert(taxid1, 0);
        }
        for node in taxon1.ancestors(&self.arena) {
            let nodeid = self.get_id_by_node(node)?;
            let rank = self.id_to_rank.get(&nodeid)?;

            if !only_canonical || canonical_ranks.contains(rank) {
                current_distance += 1;
                if nodeid == taxid2 {
                    // taxid2 is an ancestor of taxid1
                    return Some((current_distance, taxid2))
                }
                ancestors_distance1.insert(nodeid, current_distance);
            }
        }

        // if we got here then we did not see taxid2 yet, i.e. taxid2 is not a
        // direct ancestor of taxid1
        current_distance = 0;
        for node in taxon2.ancestors(&self.arena) {
            let nodeid = self.get_id_by_node(node).unwrap();
            let rank = self.id_to_rank.get(&nodeid)?;
            if !only_canonical || canonical_ranks.contains(rank) {
                current_distance += 1;
                if ancestors_distance1.contains_key(&nodeid) {
                    // the distance to te common ancestor is the distance from taxon2
                    // to an ancestor that is also an ancestor to taxon1
                    return Some((current_distance, nodeid))
                }
            }
        }
        None
    }

    /// get_distance_to_common_ancestor
    ///
    /// find the distance in the tree between name1 and name2
    fn get_distance_to_common_ancestor(&self, name1: &str, name2: &str, only_canonical: bool) -> Option<(i32, String)> {
        let taxon1 = self.name_to_node.get(name1)?;

        let taxon2 = self.name_to_node.get(name2)?;

        match self.get_distance_to_common_ancestor_taxid(self.get_id_by_node(*taxon1).unwrap(),
                                                   self.get_id_by_node(*taxon2).unwrap(), only_canonical) {
            Some((distance, taxid)) => Some((distance, self.get_name_by_id(taxid).unwrap())),
            None => None
        }
    }
}

pub struct NcbiSqliteTaxonomy {
    connection: SqliteConnection
}

impl NcbiSqliteTaxonomy {
    pub fn new(db_url: Option<&str>) -> Self {
        NcbiSqliteTaxonomy {
            connection: establish_connection(db_url)
        }
    }

    pub fn from_connection(connection: SqliteConnection) -> Self {
        NcbiSqliteTaxonomy {
            connection
        }
    }

    fn get_ancestry_for_taxid(&self, taxid: i32) -> Option<String> {
        use schema::taxonomy::dsl::*;

        let results: Vec<Option<String>> = taxonomy.filter(id.eq(taxid))
            .select(ancestry)
            .load(&self.connection)
            .expect("Error loading taxonomy");

        match results.len() {
            1 => results[0].clone(),
            _ => panic!("taxid {} not found in taxonomy", taxid)
        }
    }

    fn get_ancestors(&self, taxid: i32) -> Vec<i32> {
        let ancestry_string = self.get_ancestry_for_taxid(taxid);
        match ancestry_string {
            None => vec![], // the root taxon has no ancestry
            Some(val) => {
                let mut ancestors: Vec<i32> = val.split('/').map(|id_str| id_str.parse::<i32>().unwrap()).collect();
                ancestors.reverse();
                ancestors
            }
        }
    }

    fn get_rank(&self, taxid: i32) -> Option<String> {
        use schema::taxonomy::dsl::*;

        let results: Vec<Option<String>> = taxonomy.filter(id.eq(taxid))
            .select(rank)
            .load(&self.connection)
            .expect("Error loading taxonomy");

        match results.len() {
            1 => results[0].clone(),
            _ => panic!("taxid {} not found in taxonomy", taxid)
        }
    }
}

impl NcbiTaxonomy for NcbiSqliteTaxonomy {

    fn contains_id(&self, taxid: i32) -> bool {
        use schema::taxonomy::dsl::*;

        let results: Vec<i64> = taxonomy.filter(id.eq(taxid))
            .select(count(id))
            .load(&self.connection)
            .expect("Error loading taxonomy");

        results[0] == 1
    }

    fn contains_name(&self, name_str: &str) -> bool {
        use schema::taxonomy::dsl::*;

        let results: Vec<i64> = taxonomy.filter(name.eq(name_str))
            .select(count(id))
            .load(&self.connection)
            .expect("Error loading taxonomy");

        results[0] == 1
    }

    fn is_descendant(&self, name_str: &str, ancestor: &str) -> bool {
        let taxid = match self.get_id_by_name(name_str) {
            Some(val) => val,
            None => return false
        };

        let ancestor_taxid = match self.get_id_by_name(ancestor) {
            Some(val) => val,
            None => return false
        };

        self.is_descendant_taxid(taxid, ancestor_taxid)
    }

    fn is_descendant_taxid(&self, taxid: i32, ancestor_taxid: i32) -> bool {
        use schema::taxonomy::dsl::*;

        // ancestor pattern is id/id/id so if ancestor_taxid is an ancestor
        // of taxid, LIKE 'ancestor_taxid/%' OR LIKE '%/ancestor_taxid/%' OR LIKE '%/ancestor_taxid'
        // will be true
        let pattern1 = format!("{}/%", ancestor_taxid);
        let pattern2 = format!("%/{}/%", ancestor_taxid);
        let pattern3 = format!("%/{}", ancestor_taxid);

        let results: Vec<i64> = taxonomy.filter(
                id.eq(taxid).and(
                    ancestry.like(pattern1)
                        .or(ancestry.like(pattern2))
                        .or(ancestry.like(pattern3))
                ))
            .select(count(id))
            .load(&self.connection)
            .expect("Error loading taxonomy");

        match results.len() {
            1 => true,
            _ => false
        }
    }

    fn get_name_by_id(&self, taxid: i32) -> Option<String> {
        use schema::taxonomy::dsl::*;

        let results: Vec<String> = taxonomy.filter(id.eq(taxid))
            .select(name)
            .load(&self.connection)
            .expect("Error loading taxonomy");

        match results.len() {
            1 => Some(results[0].clone()),
            _ => None
        }
    }

    fn get_id_by_name(&self, name_str: &str) -> Option<i32> {
        use schema::taxonomy::dsl::*;

        let results: Vec<i32> = taxonomy.filter(name.eq(name_str))
            .select(id)
            .load(&self.connection)
            .expect("Error loading taxonomy");

        match results.len() {
            1 => Some(results[0]),
            _ => None
        }
    }

    fn get_lineage(&self, name: &str) -> Option<Vec<i32>> {
        let tax_id = match self.get_id_by_name(name)  {
            Some(val) => val,
            None => return None
        };
        Some(self.get_ancestors(tax_id))
    }

    fn get_distance_to_common_ancestor_taxid(&self, taxid1: i32, taxid2: i32, only_canonical: bool) -> Option<(i32, i32)> {
        // canonical ranks (+ superkingdom) as they appear in the NCBI taxonomy database
        let canonical_ranks = get_canonical_ranks();

        if taxid1 == taxid2 {
            return Some((0, taxid1))
        }

        let mut ancestors_distance1 = HashMap::new();
        let mut current_distance = 0;
        // TODO: make rank a NON NULL column
        let taxid1_rank = self.get_rank(taxid1)?;
        if !only_canonical || canonical_ranks.contains(&taxid1_rank) {
            // see comment above for why distance is 0
            ancestors_distance1.insert(taxid1, 0);
        }
        for taxid in self.get_ancestors(taxid1) {
            let current_rank = self.get_rank(taxid)?;
            if taxid == taxid2 {
                return Some((current_distance, taxid2))
            }
            if !only_canonical || canonical_ranks.contains(&current_rank) {
                current_distance += 1;
                ancestors_distance1.insert(taxid, current_distance);
            }
        }

        current_distance = 0;
        for taxid in self.get_ancestors(taxid2) {
            let current_rank = self.get_rank(taxid)?;
            if !only_canonical || canonical_ranks.contains(&current_rank) {
                current_distance += 1;
                if ancestors_distance1.contains_key(&taxid) {
                    return Some((current_distance, taxid))
                }
            }
        }
        None
    }

    fn get_distance_to_common_ancestor(&self, name1: &str, name2: &str, only_canonical: bool) -> Option<(i32, String)> {
        let taxid1 = match self.get_id_by_name(name1) {
            Some(val) => val,
            None => return None
        };

        let taxid2 = match self.get_id_by_name(name2) {
            Some(val) => val,
            None => return None
        };

        match self.get_distance_to_common_ancestor_taxid(taxid1, taxid2, only_canonical) {
            Some((distance, taxid)) => Some((distance, self.get_name_by_id(taxid).unwrap())),
            None => None
        }
    }
}

#[cfg(test)]
mod tests {
    use super::{NcbiFileTaxonomy, NcbiSqliteTaxonomy, NcbiTaxonomy, NodeEdge};

    pub struct NcbiFileTaxonomyFixture {
        pub taxonomy: NcbiFileTaxonomy,
    }

    impl Default for NcbiFileTaxonomyFixture {
        fn default() -> Self {
            let tree = NcbiFileTaxonomy::from_ncbi_files("data/sample_tree_nodes.dmp", "data/sample_tree_names.dmp").unwrap();
            Self { taxonomy: tree }
        }
    }

    pub struct NcbiSqliteTaxonomyFixture {
        pub taxonomy: NcbiSqliteTaxonomy,
    }

    impl Default for NcbiSqliteTaxonomyFixture {
        fn default() -> Self {
            let tree = NcbiSqliteTaxonomy::new(Some("data/ncbi_taxonomy.sqlite"));
            Self { taxonomy: tree }
        }
    }

    #[test]
    fn contains_id() {
        let fixture = NcbiFileTaxonomyFixture::default();
        assert!(fixture.taxonomy.contains_id(504556));
    }

    #[test]
    fn sqlite_contains_id() {
        let fixture = NcbiSqliteTaxonomyFixture::default();
        assert!(fixture.taxonomy.contains_id(504556));
    }

    #[test]
    fn contains_name() {
        let fixture = NcbiFileTaxonomyFixture::default();
        assert!(fixture.taxonomy.contains_name("Propionibacterium phage PAS7"));
        assert!(fixture.taxonomy.contains_name("Viruses"));
        assert!(fixture.taxonomy.contains_name("environmental samples <bacteriophages>"));
    }

    #[test]
    fn sqlite_contains_name() {
        let fixture = NcbiSqliteTaxonomyFixture::default();
        assert!(fixture.taxonomy.contains_name("Propionibacterium phage PAS7"));
        assert!(fixture.taxonomy.contains_name("Viruses"));
        assert!(fixture.taxonomy.contains_name("environmental samples <bacteriophages>"));
    }

    #[test]
    fn get_node_by_id() {
        let fixture = NcbiFileTaxonomyFixture::default();
        assert_eq!(fixture.taxonomy.get_node_by_id(999999999), None);
        assert!(match fixture.taxonomy.get_node_by_id(504556) { Some(_) => true, None => false })
    }

    #[test]
    fn get_id_by_node() {
        let fixture = NcbiFileTaxonomyFixture::default();
        let node_id = fixture.taxonomy.get_node_by_id(504556).unwrap();
        assert_eq!(fixture.taxonomy.get_id_by_node(*node_id).unwrap(), 504556);
    }

    #[test]
    fn get_name_by_id() {
        let fixture = NcbiFileTaxonomyFixture::default();
        assert_eq!(fixture.taxonomy.get_name_by_id(370556).unwrap(), "Streptococcus phage 9429.1");
    }

    #[test]
    fn sqlite_get_name_by_id() {
        let fixture = NcbiSqliteTaxonomyFixture::default();
        assert_eq!(fixture.taxonomy.get_name_by_id(370556).unwrap(), "Streptococcus phage 9429.1");
    }

    #[test]
    fn get_lineage() {
        let fixture = NcbiFileTaxonomyFixture::default();
        assert_eq!(fixture.taxonomy.get_lineage("Streptococcus phage 9429.1"), Some(vec![370556,387088,12333,10239,1]));
    }

    #[test]
    fn get_lineage_sqlite() {
        let fixture = NcbiSqliteTaxonomyFixture::default();
        assert_eq!(fixture.taxonomy.get_lineage("Streptococcus phage 9429.1"), Some(vec![370556,387088,12333,10239,1]));
    }

    #[test]
    fn traversal() {
        let fixture = NcbiFileTaxonomyFixture::default();
        let traversal = fixture.taxonomy.traversal(12333);
        match traversal {
            Some(traversal) => {
                let mut counter = 0;
                for node_edge in traversal {
                    match node_edge {
                        NodeEdge::Start(_) => counter += 1,
                        _ => ()
                    }
                }
                assert_eq!(counter, 500)
            }
            None => assert_eq!(true, false, "Failed to load traversal from 12333")
        }
    }

    #[test]
    fn descendants() {
        let fixture = NcbiFileTaxonomyFixture::default();
        assert!(fixture.taxonomy.is_descendant("Propionibacterium phage PAS7", "unclassified bacterial viruses"));
    }

    #[test]
    fn sqlite_descendants() {
        let fixture = NcbiSqliteTaxonomyFixture::default();
        assert!(fixture.taxonomy.is_descendant("Propionibacterium phage PAS7", "unclassified bacterial viruses"));
    }

    #[test]
    fn taxid_descendants() {
        let fixture = NcbiFileTaxonomyFixture::default();
        assert!(fixture.taxonomy.is_descendant_taxid(504556, 12333));
    }

    #[test]
    fn sqlite_taxid_descendants() {
        let fixture = NcbiSqliteTaxonomyFixture::default();
        assert!(fixture.taxonomy.is_descendant_taxid(504556, 12333));
    }

    #[test]
    fn distance_to_common_ancestor_taxid() {
        let fixture = NcbiFileTaxonomyFixture::default();
        assert_eq!(fixture.taxonomy.get_distance_to_common_ancestor_taxid(156615, 12340, false), Some((2, 12333)));
    }

    #[test]
    fn distance_to_common_ancestor_taxid_canonical() {
        let fixture = NcbiFileTaxonomyFixture::default();
        assert_eq!(fixture.taxonomy.get_distance_to_common_ancestor_taxid(156615, 12340, true), Some((2, 10239)));
    }

    #[test]
    fn distance_to_common_ancestor() {
        let fixture = NcbiFileTaxonomyFixture::default();
        assert_eq!(fixture.taxonomy.get_distance_to_common_ancestor("Cyanophage clone GS2601", "Enterobacteria phage 933J", false),
                   Some((2, "unclassified bacterial viruses".to_string())));
    }

    #[test]
    fn distance_to_common_ancestor_canonical() {
        let fixture = NcbiFileTaxonomyFixture::default();
        assert_eq!(fixture.taxonomy.get_distance_to_common_ancestor("Cyanophage clone GS2601", "Enterobacteria phage 933J", true),
                   Some((2, "Viruses".to_string())));
    }

    #[test]
    fn sqlite_distance_to_common_ancestor_taxid() {
        let fixture = NcbiSqliteTaxonomyFixture::default();
        assert_eq!(fixture.taxonomy.get_distance_to_common_ancestor_taxid(156615, 12340, false), Some((2, 12333)));
    }

    #[test]
    fn sqlite_distance_to_common_ancestor_taxid_canonical() {
        let fixture = NcbiSqliteTaxonomyFixture::default();
        assert_eq!(fixture.taxonomy.get_distance_to_common_ancestor_taxid(156615, 12340, true), Some((2, 10239)));
    }

    #[test]
    fn sqlite_distance_to_common_ancestor() {
        let fixture = NcbiSqliteTaxonomyFixture::default();
        assert_eq!(fixture.taxonomy.get_distance_to_common_ancestor("Cyanophage clone GS2601", "Enterobacteria phage 933J", false),
                   Some((2, "unclassified bacterial viruses".to_string())));
    }

    #[test]
    fn sqlite_distance_to_common_ancestor_canonical() {
        let fixture = NcbiSqliteTaxonomyFixture::default();
        assert_eq!(fixture.taxonomy.get_distance_to_common_ancestor("Cyanophage clone GS2601", "Enterobacteria phage 933J", true),
                   Some((2, "Viruses".to_string())));
    }
}