ropensci / taxa

taxonomic classes for R
https://docs.ropensci.org/taxa
Other
48 stars 12 forks source link

New function(s) to add taxa to a taxonomy #172

Open zachary-foster opened 6 years ago

zachary-foster commented 6 years ago

add_taxon(obj, new_taxon, supertaxon = NULL, subtaxa = NULL)

Adds new_taxon to obj taxonomy. supertaxon and subtaxa would accept existing taxon IDs.

Alternatively, there could be three separate functions corresponding to the top three points above:

Thoughs @sckott? It seems like there could be a few ways to do implement a function to add taxa and I am not sure what would be best.

sckott commented 6 years ago

good idea.

is there a way to do this now? and are you talking about the object taxonomy or just to any objects in the pkg?

zachary-foster commented 6 years ago

is there a way to do this now?

For taxmap and taxonomy objects, not easily (we dont have a function to do it). You would need to modify the edge list and add a taxon to the taxa list.

and are you talking about the object taxonomy or just to any objects in the pkg?

I was just thinking about taxonomy objects, but it would make sense to have to same functionality for hierarchy, and hierarchies objects. For taxa objects, you can just append/split them like any other list. I am not sure what would be the best way to do this with hierarchy and hierarchies objects. Those would be much easier to modify "by hand" than taxonomy objects.

sckott commented 6 years ago

So right now, someone would need to e.g., recreate the entire taxonomy object with new taxa if they had new taxa to add?

zachary-foster commented 6 years ago

That would perhaps be the least error-prone way to do it for taxonomy objects: output the classifications(), add taxon in same format, and reread with parse_tax_data, but some info would be lost (e.g. ranks). For taxmap, it would be harder to preserve the datasets.

One could modify an object in place as well, but it could be tricky:

> library(taxa)
> ex_taxonomy
<Taxonomy>
  9 taxa: b. Mammalia, c. Notoryctidae, d. Felidae ... h. Notoryctes typhlops, i. Puma concolor, j. Panthera tigris
  9 edges: NA->b, b->c, b->d, c->e, d->f, d->g, e->h, f->i, g->j
> print_tree(ex_taxonomy)
Mammalia
├─Notoryctidae
│ └─Notoryctes
│   └─Notoryctes typhlops
└─Felidae
  ├─Puma
  │ └─Puma concolor
  └─Panthera
    └─Panthera tigris
> ex_taxonomy$taxa <- c(ex_taxonomy$taxa, new_id = taxon("new_taxon"))
> ex_taxonomy$edge_list <- rbind(ex_taxonomy$edge_list, c("b", "new_id"))
> ex_taxonomy
<Taxonomy>
  10 taxa: b. Mammalia, c. Notoryctidae, d. Felidae ... i. Puma concolor, j. Panthera tigris, new_id. new_taxon
  10 edges: NA->b, b->c, b->d, c->e, d->f, d->g, e->h, f->i, g->j, b->new_id
> print_tree(ex_taxonomy)
Mammalia
├─Notoryctidae
│ └─Notoryctes
│   └─Notoryctes typhlops
├─Felidae
│ ├─Puma
│ │ └─Puma concolor
│ └─Panthera
│   └─Panthera tigris
└─new_taxon
sckott commented 6 years ago

Definitely seems like a pain point that we should sort out

zachary-foster commented 5 years ago

Also, could add a function called add_subtree that adds one taxonomy/taxmap as subtaxa of a taxon in another taxonomy/taxmap.