diff --git a/.Rbuildignore b/.Rbuildignore index 5084075d..b3f64100 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -15,4 +15,6 @@ tmp/ revdep/ ^codemeta\.json$ ^tic.R$ -^.pre-commit-config.yaml$ \ No newline at end of file +^.pre-commit-config.yaml$ +^.*\.Rproj$ +^\.Rproj\.user$ diff --git a/.gitignore b/.gitignore index eae1c4e6..6d1efe09 100644 --- a/.gitignore +++ b/.gitignore @@ -18,3 +18,7 @@ revdep/ README.html README_files/ figure/ +.Rproj.user +*.Rproj + +cit*.t*xt diff --git a/DESCRIPTION b/DESCRIPTION index 9d0d8e74..3ee31176 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -49,7 +49,7 @@ VignetteBuilder: knitr Encoding: UTF-8 LazyData: true -RoxygenNote: 6.1.99.9001 +RoxygenNote: 7.0.0 X-schema.org-isPartOf: https://ropensci.org X-schema.org-keywords: metadata, ropensci, phylogenetics, independant-contrasts, biodiversity diff --git a/NAMESPACE b/NAMESPACE index 8a185da3..e4377723 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,6 +1,6 @@ # Generated by roxygen2: do not edit by hand -S3method(`[`,otl_ott_id) +S3method("[",otl_ott_id) S3method(candidate_for_synth,study_meta) S3method(flags,match_names) S3method(flags,taxon_info) @@ -63,6 +63,7 @@ S3method(unique_name,tol_summary) S3method(update,match_names) export(candidate_for_synth) export(flags) +export(get_citations) export(get_publication) export(get_study) export(get_study_meta) diff --git a/R/base.R b/R/base.R index 7ee1ce9b..28a72351 100644 --- a/R/base.R +++ b/R/base.R @@ -208,3 +208,19 @@ check_label_format <- function(x) { return(FALSE) } } + +# res is an output from .tol_induced_subtree or .tol_subtree +# returns a list with tree and study ids supporting a subtree +studies_from_otl <- function(res){ + if (is.list(res)) { + if (!is.null(res$supporting_studies)) { + studies <- res$supporting_studies + } + else { + stop("Cannot find supporting studies") + } + } + study_ids <- sapply(stringr::str_split(studies, "@"), '[', 1) + trees <- sapply(stringr::str_split(studies, "@"), '[', 2) + return(list(study_ids = study_ids, trees = trees)) +} diff --git a/R/studies-methods.R b/R/studies-methods.R index dad86309..500eb41a 100644 --- a/R/studies-methods.R +++ b/R/studies-methods.R @@ -87,3 +87,11 @@ candidate_for_synth.study_meta <- function(sm) { get_study_year.study_meta <- function(sm) { sm[["nexml"]][["^ot:studyYear"]] } + +##' Get citations for a set of study ids +##' @export +##' @param study_ids is a vector of study ids +get_citations <- function(study_ids){ + citations <- sapply(study_ids, function(x) get_publication.study_meta(get_study_meta(study_id = x))) + return(citations) +} diff --git a/R/tol.R b/R/tol.R index c9369293..5dcf2b61 100644 --- a/R/tol.R +++ b/R/tol.R @@ -427,6 +427,8 @@ tol_subtree <- function(ott_id = NULL, node_id = NULL, label_format = NULL, ##' (the default). ##' @param file If specified, the function will write the subtree to a file in ##' newick format. +##' @param file_citations If specified, the function will write citations from +##' supporting source trees to a txt file. It will automatically add the "txt" extension. ##' @param ... additional arguments to customize the API call (see ##' \code{\link{rotl}} for more information). ##' @@ -455,11 +457,28 @@ tol_subtree <- function(ott_id = NULL, node_id = NULL, label_format = NULL, ##' } ##' @export tol_induced_subtree <- function(ott_ids = NULL, node_ids = NULL, label_format = NULL, - file, ...) { + file, file_citations, ...) { res <- .tol_induced_subtree( ott_ids = ott_ids, node_ids = node_ids, label_format = label_format, ... ) + # print citations + if (!missing(file_citations)) { + filecit <- paste0(file_citations, ".txt") + citations <- get_citations(studies_from_otl(res)$study_ids) + unlink(filecit) + cat(paste(citations, collapse = '\n\n'), file = filecit) + message(paste0("Citations were written to ", filecit, " in dir ", getwd())) + } + # else { + # # enhance: if file is not provided write to tmp dir + # # tmpdir <- "temporary file dir" + # # message(paste("'citations.txt' file has been written to dir", tmpdir)) + # filecit <- "citations.txt" + # enhance: print to screen at leats part of citations, so users know that they're there. + # } + + if (!missing(file)) { unlink(file) cat(res$newick, file = file) diff --git a/man/get_citations.Rd b/man/get_citations.Rd new file mode 100644 index 00000000..661bdf59 --- /dev/null +++ b/man/get_citations.Rd @@ -0,0 +1,14 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/studies-methods.R +\name{get_citations} +\alias{get_citations} +\title{Get citations for a set of study ids} +\usage{ +get_citations(study_ids) +} +\arguments{ +\item{study_ids}{is a vector of study ids} +} +\description{ +Get citations for a set of study ids +} diff --git a/man/get_study_meta.Rd b/man/get_study_meta.Rd index e98b83f0..fccfbd7a 100644 --- a/man/get_study_meta.Rd +++ b/man/get_study_meta.Rd @@ -31,12 +31,12 @@ get_study_year(sm) get_study_meta(study_id, ...) } \arguments{ +\item{sm}{an object created by \code{get_study_meta}} + \item{study_id}{the study identifier (character)} \item{...}{additional arguments to customize the API request (see \code{\link{rotl}} package documentation).} - -\item{sm}{an object created by \code{get_study_meta}} } \value{ named-list containing the metadata associated with the diff --git a/man/list_trees.Rd b/man/list_trees.Rd index f53d3343..d7ffa93e 100644 --- a/man/list_trees.Rd +++ b/man/list_trees.Rd @@ -15,10 +15,10 @@ list_trees(matched_studies, ...) \item{matched_studies}{an object created by \code{studies_find_trees} or \code{studies_find_studies}.} +\item{...}{Currently unused} + \item{study_id}{a \code{study_id} listed in the object returned by \code{studies_find_trees}} - -\item{...}{Currently unused} } \value{ \code{list_trees} returns a list of the tree_ids for each diff --git a/man/strip_ott_ids.Rd b/man/strip_ott_ids.Rd index 056821ce..b1e202ef 100644 --- a/man/strip_ott_ids.Rd +++ b/man/strip_ott_ids.Rd @@ -25,8 +25,8 @@ Strip OTT ids from tip labels \dontrun{ genera <- c("Perdix", "Setophaga", "Cinclus", "Struthio") tr <- tol_induced_subtree(ott_ids=c(102710, 285198, 267845, 292466)) -tr$tip.label \%in\% genera +tr$tip.label %in% genera tr$tip.label <- strip_ott_ids(tr$tip.label) -tr$tip.label \%in\% genera +tr$tip.label %in% genera } } diff --git a/man/tol_induced_subtree.Rd b/man/tol_induced_subtree.Rd index 25443b77..ccedc4bd 100644 --- a/man/tol_induced_subtree.Rd +++ b/man/tol_induced_subtree.Rd @@ -9,6 +9,7 @@ tol_induced_subtree( node_ids = NULL, label_format = NULL, file, + file_citations, ... ) } @@ -26,6 +27,9 @@ tips in the induced tree.} \item{file}{If specified, the function will write the subtree to a file in newick format.} +\item{file_citations}{If specified, the function will write citations from +supporting source trees to a txt file. It will automatically add the "txt" extension.} + \item{...}{additional arguments to customize the API call (see \code{\link{rotl}} for more information).} } diff --git a/man/tol_node_info.Rd b/man/tol_node_info.Rd index 6d2ac5aa..b1e7f05a 100644 --- a/man/tol_node_info.Rd +++ b/man/tol_node_info.Rd @@ -34,17 +34,17 @@ tol_node_info(ott_id = NULL, node_id = NULL, include_lineage = FALSE, ...) \method{tol_lineage}{tol_node}(tax, ...) } \arguments{ +\item{tax}{an object returned by \code{tol_node_info}.} + +\item{...}{additional arguments to customize the API call (see +?rotl for more information)} + \item{ott_id}{Numeric. The OpenTree taxonomic identifier.} \item{node_id}{Character. The OpenTree node identifier.} \item{include_lineage}{Logical (default = FALSE). Whether to return the lineage of the node from the synthetic tree.} - -\item{...}{additional arguments to customize the API call (see -?rotl for more information)} - -\item{tax}{an object returned by \code{tol_node_info}.} } \value{ \code{tol_node_info} returns an invisible list of summary