Git Product home page Git Product logo

rphyloxml's Introduction

Travis build status Build status Coverage status

rphyloxml

rphyloxml provides access to the phyloXML file format. For now, the only functions that are implemented in the package are:

  • write_phyloxml: A method to coerce phylo objects from the ape package as phyloXML (XML) documents.
  • read_phyloxml: A method to read phyloXML documents into R. It returns a data frame with the structure of the tree and a nested list with each nodes’ annotations.
  • validate_phyloxml: A wrapper of xml2::xml_validate, which allows validating a phyloXML doc using the phyloxml.xsd schema (see here)

This package has been motivated to be used with the javascript library jsPhyloSVG, for which we are currently developing an R package with the same name that provides an htmlwidget here.

Installation

You can install rphyloxml from github with:

# install.packages("devtools")
devtools::install_github("USCBiostats/rphyloxml")

Writing phyloXML files

In the following example, we create a random tree using the rtree function from the ape package, and later on coerce it into a phyloXML document using write_phyloxml.

library(ape)
library(rphyloxml)
set.seed(12)

x <- rtree(3)
x
#> 
#> Phylogenetic tree with 3 tips and 2 internal nodes.
#> 
#> Tip labels:
#> [1] "t2" "t3" "t1"
#> 
#> Rooted; includes branch lengths.
z <- write_phyloxml(x)
z
#> {xml_document}
#> <phyloxml schemaLocation="http://www.phyloxml.org http://www.phyloxml.org/1.10/phyloxml.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.phyloxml.org">
#> [1] <phylogeny rooted="true" rerootable="false">\n    <name>A phylogenetic tr ...

You can get a “nicer” view of it by doing the following:

cat(as.character(z))
#> <?xml version="1.0" encoding="UTF-8"?>
#> <phyloxml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.phyloxml.org" xsi:schemaLocation="http://www.phyloxml.org http://www.phyloxml.org/1.10/phyloxml.xsd">
#>   <phylogeny rooted="true" rerootable="false">
#>     <name>A phylogenetic tree</name>
#>     <description>Some description</description>
#>     <clade>
#>       <clade>
#>         <branch_length>0.26938187633641064167</branch_length>
#>         <clade>
#>           <name>t2</name>
#>           <branch_length>0.16934812325052917004</branch_length>
#>         </clade>
#>         <clade>
#>           <name>t3</name>
#>           <branch_length>0.03389562247321009636</branch_length>
#>         </clade>
#>       </clade>
#>       <clade>
#>         <name>t1</name>
#>         <branch_length>0.17878500418737530708</branch_length>
#>       </clade>
#>     </clade>
#>   </phylogeny>
#> </phyloxml>

And to store the document, you just need to use xml2 (which is what powers the package) as follows:

xml2::write_xml(z, "mynicetree.xml")

Reading XML files

We will read the file amphibian_tree_of_life_Frost_DR_2006.xml available in both the package and the phyloxml website.

# Reading from the package files
fn <- system.file("phyloxml/amphibian_tree_of_life_Frost_DR_2006.xml", package="rphyloxml")
xmltree <- read_phyloxml(fn)

str(xmltree, 4)
#> List of 1
#>  $ Amphibian Phylogeny, Frost DR, 2006:List of 4
#>   ..$ .Data     :'data.frame':   715 obs. of  4 variables:
#>   .. ..$ id           : int [1:715] 359 360 361 362 363 364 365 366 367 368 ...
#>   .. ..$ isleaf       : logi [1:715] FALSE FALSE FALSE FALSE FALSE FALSE ...
#>   .. ..$ parent       : int [1:715] NA 359 360 361 362 363 364 365 366 367 ...
#>   .. ..$ branch_length: logi [1:715] NA NA NA NA NA NA ...
#>   ..$ node.meta :List of 715
#>   .. ..$ :List of 1
#>   .. .. ..$ taxonomy:List of 2
#>   .. ..$ : Named list()
#>   .. ..$ :List of 1
#>   .. .. ..$ taxonomy:List of 3
#>   .. ..$ :List of 1
#>   .. .. ..$ taxonomy:List of 3
#>   .. ..$ :List of 1
#>   .. .. ..$ taxonomy:List of 3
#>   .. ..$ : Named list()
#>   .. ..$ : Named list()
#>   .. ..$ : Named list()
#>   .. ..$ :List of 1
#>   .. .. ..$ taxonomy:List of 3
#>   .. ..$ : Named list()
#>   .. ..$ : Named list()
#>   .. ..$ : Named list()
#>   .. ..$ :List of 1
#>   .. .. ..$ taxonomy:List of 3
#>   .. ..$ : Named list()
#>   .. ..$ : Named list()
#>   .. ..$ : Named list()
#>   .. ..$ : Named list()
#>   .. ..$ : Named list()
#>   .. ..$ : Named list()
#>   .. ..$ : Named list()
#>   .. ..$ : Named list()
#>   .. ..$ :List of 2
#>   .. .. ..$ color   :List of 3
#>   .. .. ..$ taxonomy:List of 4
#>   .. ..$ :List of 1
#>   .. .. ..$ color:List of 3
#>   .. ..$ :List of 1
#>   .. .. ..$ color:List of 3
#>   .. ..$ :List of 1
#>   .. .. ..$ color:List of 3
#>   .. ..$ :List of 1
#>   .. .. ..$ color:List of 3
#>   .. ..$ :List of 1
#>   .. .. ..$ color:List of 3
#>   .. ..$ :List of 1
#>   .. .. ..$ color:List of 3
#>   .. ..$ :List of 1
#>   .. .. ..$ color:List of 3
#>   .. ..$ :List of 2
#>   .. .. ..$ color   :List of 3
#>   .. .. ..$ taxonomy:List of 3
#>   .. ..$ :List of 2
#>   .. .. ..$ color   :List of 3
#>   .. .. ..$ taxonomy:List of 3
#>   .. ..$ :List of 2
#>   .. .. ..$ color   :List of 3
#>   .. .. ..$ taxonomy:List of 3
#>   .. ..$ :List of 2
#>   .. .. ..$ color   :List of 3
#>   .. .. ..$ taxonomy:List of 4
#>   .. ..$ :List of 2
#>   .. .. ..$ color   :List of 3
#>   .. .. ..$ taxonomy:List of 6
#>   .. ..$ :List of 1
#>   .. .. ..$ color:List of 3
#>   .. ..$ :List of 2
#>   .. .. ..$ color   :List of 3
#>   .. .. ..$ taxonomy:List of 5
#>   .. ..$ :List of 2
#>   .. .. ..$ color   :List of 3
#>   .. .. ..$ taxonomy:List of 3
#>   .. ..$ :List of 1
#>   .. .. ..$ color:List of 3
#>   .. ..$ :List of 2
#>   .. .. ..$ color   :List of 3
#>   .. .. ..$ taxonomy:List of 5
#>   .. ..$ :List of 2
#>   .. .. ..$ color   :List of 3
#>   .. .. ..$ taxonomy:List of 3
#>   .. ..$ :List of 1
#>   .. .. ..$ color:List of 3
#>   .. ..$ :List of 1
#>   .. .. ..$ color:List of 3
#>   .. ..$ :List of 1
#>   .. .. ..$ color:List of 3
#>   .. ..$ :List of 2
#>   .. .. ..$ color   :List of 3
#>   .. .. ..$ taxonomy:List of 3
#>   .. ..$ :List of 2
#>   .. .. ..$ color   :List of 3
#>   .. .. ..$ taxonomy:List of 3
#>   .. ..$ :List of 2
#>   .. .. ..$ color   :List of 3
#>   .. .. ..$ taxonomy:List of 4
#>   .. ..$ :List of 1
#>   .. .. ..$ color:List of 3
#>   .. ..$ :List of 2
#>   .. .. ..$ color   :List of 3
#>   .. .. ..$ taxonomy:List of 4
#>   .. ..$ :List of 2
#>   .. .. ..$ color   :List of 3
#>   .. .. ..$ taxonomy:List of 3
#>   .. ..$ :List of 1
#>   .. .. ..$ color:List of 3
#>   .. ..$ :List of 2
#>   .. .. ..$ color   :List of 3
#>   .. .. ..$ taxonomy:List of 3
#>   .. ..$ :List of 2
#>   .. .. ..$ color   :List of 3
#>   .. .. ..$ taxonomy:List of 4
#>   .. ..$ :List of 1
#>   .. .. ..$ color:List of 3
#>   .. ..$ :List of 2
#>   .. .. ..$ color   :List of 3
#>   .. .. ..$ taxonomy:List of 3
#>   .. ..$ :List of 1
#>   .. .. ..$ color:List of 3
#>   .. ..$ :List of 2
#>   .. .. ..$ color   :List of 3
#>   .. .. ..$ taxonomy:List of 4
#>   .. ..$ :List of 2
#>   .. .. ..$ color   :List of 3
#>   .. .. ..$ taxonomy:List of 4
#>   .. ..$ :List of 2
#>   .. .. ..$ color   :List of 3
#>   .. .. ..$ taxonomy:List of 4
#>   .. ..$ :List of 2
#>   .. .. ..$ color   :List of 3
#>   .. .. ..$ taxonomy:List of 5
#>   .. ..$ :List of 2
#>   .. .. ..$ color   :List of 3
#>   .. .. ..$ taxonomy:List of 3
#>   .. ..$ : Named list()
#>   .. ..$ :List of 2
#>   .. .. ..$ color   :List of 3
#>   .. .. ..$ taxonomy:List of 4
#>   .. ..$ :List of 2
#>   .. .. ..$ color   :List of 3
#>   .. .. ..$ taxonomy:List of 3
#>   .. ..$ :List of 2
#>   .. .. ..$ color   :List of 3
#>   .. .. ..$ taxonomy:List of 5
#>   .. ..$ :List of 2
#>   .. .. ..$ color   :List of 3
#>   .. .. ..$ taxonomy:List of 3
#>   .. ..$ :List of 2
#>   .. .. ..$ color   :List of 3
#>   .. .. ..$ taxonomy:List of 3
#>   .. ..$ :List of 2
#>   .. .. ..$ color   :List of 3
#>   .. .. ..$ taxonomy:List of 6
#>   .. ..$ :List of 2
#>   .. .. ..$ color   :List of 3
#>   .. .. ..$ taxonomy:List of 6
#>   .. ..$ :List of 2
#>   .. .. ..$ color   :List of 3
#>   .. .. ..$ taxonomy:List of 4
#>   .. ..$ : Named list()
#>   .. ..$ : Named list()
#>   .. ..$ : Named list()
#>   .. ..$ : Named list()
#>   .. ..$ :List of 2
#>   .. .. ..$ color   :List of 3
#>   .. .. ..$ taxonomy:List of 3
#>   .. ..$ :List of 2
#>   .. .. ..$ color   :List of 3
#>   .. .. ..$ taxonomy:List of 3
#>   .. ..$ :List of 2
#>   .. .. ..$ color   :List of 3
#>   .. .. ..$ taxonomy:List of 3
#>   .. ..$ :List of 2
#>   .. .. ..$ color   :List of 3
#>   .. .. ..$ taxonomy:List of 3
#>   .. ..$ :List of 2
#>   .. .. ..$ color   :List of 3
#>   .. .. ..$ taxonomy:List of 4
#>   .. ..$ :List of 2
#>   .. .. ..$ color   :List of 3
#>   .. .. ..$ taxonomy:List of 4
#>   .. ..$ :List of 2
#>   .. .. ..$ color   :List of 3
#>   .. .. ..$ taxonomy:List of 3
#>   .. ..$ :List of 1
#>   .. .. ..$ color:List of 3
#>   .. ..$ :List of 2
#>   .. .. ..$ color   :List of 3
#>   .. .. ..$ taxonomy:List of 4
#>   .. ..$ :List of 2
#>   .. .. ..$ color   :List of 3
#>   .. .. ..$ taxonomy:List of 4
#>   .. ..$ :List of 2
#>   .. .. ..$ color   :List of 3
#>   .. .. ..$ taxonomy:List of 4
#>   .. ..$ : Named list()
#>   .. ..$ :List of 2
#>   .. .. ..$ color   :List of 3
#>   .. .. ..$ taxonomy:List of 4
#>   .. ..$ :List of 2
#>   .. .. ..$ color   :List of 3
#>   .. .. ..$ taxonomy:List of 3
#>   .. ..$ :List of 2
#>   .. .. ..$ color   :List of 3
#>   .. .. ..$ taxonomy:List of 3
#>   .. ..$ :List of 1
#>   .. .. ..$ color:List of 3
#>   .. ..$ :List of 2
#>   .. .. ..$ color   :List of 3
#>   .. .. ..$ taxonomy:List of 4
#>   .. ..$ :List of 2
#>   .. .. ..$ color   :List of 3
#>   .. .. ..$ taxonomy:List of 4
#>   .. ..$ :List of 2
#>   .. .. ..$ color   :List of 3
#>   .. .. ..$ taxonomy:List of 4
#>   .. ..$ : Named list()
#>   .. ..$ : Named list()
#>   .. ..$ :List of 2
#>   .. .. ..$ color   :List of 3
#>   .. .. ..$ taxonomy:List of 3
#>   .. ..$ :List of 1
#>   .. .. ..$ color:List of 3
#>   .. ..$ :List of 2
#>   .. .. ..$ color   :List of 3
#>   .. .. ..$ taxonomy:List of 4
#>   .. ..$ :List of 2
#>   .. .. ..$ color   :List of 3
#>   .. .. ..$ taxonomy:List of 3
#>   .. ..$ :List of 2
#>   .. .. ..$ color   :List of 3
#>   .. .. ..$ taxonomy:List of 5
#>   .. .. [list output truncated]
#>   ..$ rooted    : chr "true"
#>   ..$ rerootable: chr "false"
#>  - attr(*, "class")= chr "phyloxml"
# We can coerce this into a mulitphylo list
(apetree <- phyloxml2phylo(xmltree))[[1]]
#> 
#> Phylogenetic tree with 358 tips and 357 internal nodes.
#> 
#> Tip labels:
#>  =119767|Capensibufo rosei, =125255|Capensibufo tradouwi, =119769|Stephopaedes anotis, =8386|Bufo marinus, =8384|Bufo bufo, =103606|Torrentophryne aspinia, ...
#> 
#> Rooted; includes branch lengths.
plot(apetree, cex=.25)
#> Warning in plot.phylo(x[[i]], ...): 715 branch length(s) NA(s): branch lengths
#> ignored in the plot

rphyloxml's People

Contributors

gvegayon avatar

Watchers

 avatar  avatar  avatar

rphyloxml's Issues

function "read_phyloxml" returned a zero-length list

I am trying to read phylogenetic trees from XML files by function "read_phyloxml", but it returns a list with zero length. I tried with my own XML tree downloaded from Tree of Life, and I also ran the example code of the package instructions, but the same problem happened: it returned List of 0.
Here is the code. The file "test.xml" I use is accessible in http://tolweb.org/onlinecontributors/appservice=external&page=xml/TreeStructureService&node_id=8205).

`####### my insect phylogenetic trees
library(rphyloxml)
phyloinsect <- read_phyloxml ("C://Users/Hanlun Liu/Desktop/plphylogeny/test.xml")
str(phyloinsect)

####### examples from github
fn <- system.file("phyloxml/amphibian_tree_of_life_Frost_DR_2006.xml", package="rphyloxml")
xmltree <- read_phyloxml(fn)
str(xmltree)
read_phyloxml("amphibian_tree_of_life_Frost_DR_2006.xml")

######## examples from R help
library(ape)
set.seed(1)
x <- rtree(10)
x2 <- write_phyloxml(x)
a <- read_phyloxml(x2)
str(a)`

all str() return Named list() - attr(*, "class")= chr "phyloxml"

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.