-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added a handful of metadata access methods
Includes methods: get_citation, get_license, get_metadata (top-level meta elements only) summary (just uses phylo method for now, should display basic metadata too...). methods are defined for nexml class, and extended to nexmlTree class
- Loading branch information
Showing
9 changed files
with
179 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,3 +26,4 @@ Collate: | |
'nexml_write.R' | ||
'meta.R' | ||
'extend_phylo.R' | ||
'metadata_methods.R' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
#' @export | ||
setGeneric("get_license", function(object) standardGeneric("get_license")) | ||
|
||
#' @export | ||
setGeneric("get_citation", function(object) standardGeneric("get_citation")) | ||
|
||
|
||
setMethod("summary", | ||
signature("nexml"), | ||
function(object, ...) | ||
summary(as(object, "phylo")) | ||
) | ||
setMethod("summary", | ||
signature("nexmlTree"), | ||
function(object, ...) | ||
summary(as(object, "nexml")) | ||
) | ||
|
||
|
||
## Ironically, it is easier to extract the license from the XML representation using XPath than to extract it from the R S4 representation. | ||
|
||
|
||
|
||
## Using newXMLDoc(object) leads invariably to segfaults.... | ||
## safer to write out and parse. | ||
setxpath <- function(object){ | ||
suppressWarnings(saveXML(object, "tmp.xml")) | ||
doc <- xmlParse("tmp.xml") | ||
unlink("tmp.xml") | ||
doc | ||
} | ||
|
||
## FIXME handle namespaces correctly! | ||
setMethod("get_citation", | ||
signature("nexml"), | ||
function(object){ | ||
b <- setxpath(as(object, "XMLInternalElementNode")) | ||
unname(xpathSApply(b, "/nex:nexml/nex:meta[@property='dcterms:bibliographicCitation']/@content")) | ||
}) | ||
|
||
setMethod("get_license", | ||
signature("nexml"), | ||
function(object){ | ||
b <- setxpath(as(object, "XMLInternalElementNode")) | ||
dc_rights <- unname(xpathSApply(b, "/nex:nexml/nex:meta[@property='dc:rights']/@content")) | ||
cc_license <- unname(xpathSApply(b, "/nex:nexml/nex:meta[@rel='cc:license']/@href")) | ||
if(length(dc_rights) > 0) | ||
dc_rights | ||
else | ||
cc_license | ||
}) | ||
|
||
#' get all top-level metadata | ||
setMethod("get_metadata", signature("nexml"), function(object){ | ||
b <- setxpath(as(object, "XMLInternalElementNode")) | ||
references <- getNodeSet(b, "/nex:nexml/nex:meta[@property]") | ||
rel = sapply(references, | ||
function(x) | ||
xmlAttrs(x)['rel']) | ||
href = sapply(references, | ||
function(x) | ||
xmlAttrs(x)['href']) | ||
names(href) = rel | ||
literals <- getNodeSet(b, "/nex:nexml/nex:meta[@rel]") | ||
property = sapply(literals, | ||
function(x) | ||
xmlAttrs(x)['property']) | ||
content = sapply(literals, | ||
function(x) | ||
xmlAttrs(x)['content']) | ||
names(content) = property | ||
c(content, href) | ||
}) | ||
|
||
|
||
|
||
|
||
|
||
## Would be convenient to inherit these automatically... | ||
setMethod("get_metadata", signature("nexmlTree"), function(object) | ||
get_metadata(as(object, "nexml"))) | ||
setMethod("get_citation", signature("nexmlTree"), function(object) | ||
get_citation(as(object, "nexml"))) | ||
setMethod("get_license", signature("nexmlTree"), function(object) | ||
get_license(as(object, "nexml"))) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
content("extract_metadata") | ||
|
||
library(ape) | ||
library(RNeXML) | ||
library(XML) | ||
data(bird.orders) | ||
|
||
history <- new("meta", | ||
content = "Mapped from the bird.orders data in the ape package using RNeXML", | ||
datatype = "xsd:string", id = "meta5144", property = "skos:historyNote", | ||
'xsi:type' = "LiteralMeta") | ||
modified <- new("meta", | ||
content = "2013-10-04", datatype = "xsd:string", id = "meta5128", | ||
property = "prism:modificationDate", 'xsi:type' = "LiteralMeta") | ||
website <- new("meta", | ||
href = "http://carlboettiger.info", | ||
rel = "foaf:homepage", 'xsi:type' = "ResourceMeta") | ||
nexml_write(bird.orders, file="example.xml", | ||
title = "My test title", | ||
description = "A description of my test", | ||
creator = "Carl Boettiger <cboettig@gmail.com>", | ||
publisher = "unpublished data", | ||
pubdate = "2012-04-01", | ||
citation = citation("ape"), | ||
additional_metadata = list(history, modified, website, rdfa), | ||
additional_namespaces = c(skos = "http://www.w3.org/2004/02/skos/core#", | ||
prism = "http://prismstandard.org/namespaces/1.2/basic/", | ||
foaf = "http://xmlns.com/foaf/0.1/")) | ||
|
||
|
||
|
||
|
||
test_that("we can extract various metadata", { | ||
|
||
## FIXME add the appropriate expect_that checks here | ||
nex <- read.nexml("example.xml") | ||
get_citation(nex) | ||
get_license(nex) | ||
get_metadata(nex) | ||
summary(nex) | ||
|
||
}) | ||
|
||
|
||
|
||
test_that("example parses", { | ||
require(XML) | ||
## xmlParse and check with xpath | ||
results <- xmlSchemaValidate("http://www.nexml.org/2009/nexml.xsd", "example.xml") | ||
expect_equal(results$status, 0) | ||
expect_equal(length(results$errors), 0) | ||
|
||
unlink("example.xml") # cleanup | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters