Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle features of origin of plasmid #11

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ jobs:
- {os: ubuntu-latest, r: 'devel', http-user-agent: 'release'}
- {os: ubuntu-latest, r: 'release'}
- {os: ubuntu-latest, r: 'oldrel-1'}
- {os: ubuntu-latest, r: 'oldrel-2'}

env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/pkgdown.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ jobs:
group: pkgdown-${{ github.event_name != 'pull_request' || github.run_id }}
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
permissions:
contents: write
steps:
- uses: actions/checkout@v3

Expand Down
9 changes: 5 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.Rproj.user
.Rhistory
.RData
.Ruserdata
.Rproj.user
.Rhistory
.RData
.Ruserdata
docs
5 changes: 3 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Suggests:
testthat (>= 3.0.0)
VignetteBuilder: knitr
Depends: R (>= 2.10)
URL: https::/github.com/bradyajohnston/plasmapr
BugReports: https://github.com/bradyajohnston/plasmapr/issues
URL: https://github.com/BradyAJohnston/plasmapr, https://bradyajohnston.github.io/plasmapR/
BugReports: https://github.com/BradyAJohnston/plasmapR/issues
Config/testthat/edition: 3
Config/testthat/parallel: true
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export(StatArrow)
export(StatArrowLabel)
export(plot_plasmid)
export(read_gb)
export(stat_arrow)
importFrom(rlang,.data)
28 changes: 17 additions & 11 deletions R/dataframe.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
.feature_list_to_df <- function(x) {
# dat <- data.frame(
# index = numeric(),
# name = character(),
# type = character(),
# start = numeric(),
# end = numeric(),
# direction = numeric()
# )
.feature_list_to_df <- function(x, bp = NULL, ...) {

feats <- lapply(seq(length(x)), \(i) {
feat <- x[[i]]
Expand All @@ -22,13 +14,27 @@
})

dat <- do.call(rbind, feats)
# }

# turn certain features in to numeric columns
dat$start <- as.numeric(dat$start)
dat$end <- as.numeric(dat$end)
dat$direction <- as.numeric(dat$direction)

over_origin <- dat$start > dat$end & dat$direction == 1

if (any(over_origin)) {
offset <- dat$end[over_origin] + 1

Check warning on line 26 in R/dataframe.R

View check run for this annotation

Codecov / codecov/patch

R/dataframe.R#L26

Added line #L26 was not covered by tests

if (is.null(bp)) {
bp <- max(c(dat$start, dat$end))

Check warning on line 29 in R/dataframe.R

View check run for this annotation

Codecov / codecov/patch

R/dataframe.R#L28-L29

Added lines #L28 - L29 were not covered by tests
}

dat$start <- dat$start - offset
dat$end <- dat$end - offset

Check warning on line 33 in R/dataframe.R

View check run for this annotation

Codecov / codecov/patch

R/dataframe.R#L32-L33

Added lines #L32 - L33 were not covered by tests

dat$end[over_origin] <- bp

Check warning on line 35 in R/dataframe.R

View check run for this annotation

Codecov / codecov/patch

R/dataframe.R#L35

Added line #L35 was not covered by tests

}

# only return features where a start was successfully parsed
# dat[!is.na(dat$start), ]
Expand All @@ -46,5 +52,5 @@
#' @rdname as.data.frame.plasmid
#' @export
as.data.frame.plasmid <- function(x, row.names, optional, ...) {
.feature_list_to_df(x$features)
.feature_list_to_df(x$features, bp = x$length)
}
17 changes: 8 additions & 9 deletions R/get.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@
}

.get_start_end <- function(x) {
vector <- stringr::str_extract(x, "\\d+\\.\\.\\d+") |>
stringr::str_split("\\.\\.") |>
unlist() |>
as.numeric()
if (length(vector) == 1) {
c(vector, vector)
} else {
vector
}
start_stops <- stringr::str_extract_all(x, "\\d+\\.\\.\\d+")[[1]]
numbers <- unlist(stringr::str_extract_all(start_stops, "\\d+"))
numbers <- as.numeric(numbers)

# takes into account where there is a join(500..600, 1..100)
# where the feature goes over the origin
vector <- c(numbers[1], numbers[length(numbers)])
vector
}

.get_feature_type <- function(x) {
Expand Down
12 changes: 9 additions & 3 deletions R/plot.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@

#' @importFrom rlang .data
#' @noRd
.plot_plasmid <- function(dat, bp, name = "Plasmid Name", label_wrap = 20) {
.plot_plasmid <- function(
dat,
bp,
name = "Plasmid Name",
rotation = 0,
label_wrap = 20
) {
dat <- dat[dat$type != "source", ]

name_supplied <- !is.null(name) & name != ""
Expand All @@ -22,7 +28,7 @@
)) +
ggplot2::geom_hline(yintercept = yintercept) +
ggplot2::coord_polar(
start = pi / 4
start = rotation
) +


Expand Down Expand Up @@ -91,7 +97,7 @@
#' @export
plot_plasmid <- function(plasmid, name = "Plasmid Name", label_wrap = 20) {
if (methods::is(plasmid, "plasmid")) {
features <- as.data.frame(plasmid)
features <- as.data.frame(plasmid, bp = plasmid$length)

Check warning on line 100 in R/plot.R

View check run for this annotation

Codecov / codecov/patch

R/plot.R#L100

Added line #L100 was not covered by tests
} else if (methods::is(plasmid, "data.frame")) {
features <- plasmid
} else {
Expand Down
84 changes: 54 additions & 30 deletions R/stat_arrow.R
Original file line number Diff line number Diff line change
Expand Up @@ -154,34 +154,58 @@ StatArrowLabel <- ggplot2::ggproto('StatArrowLabel', StatArrow,
# required_aes = c('start', 'end')
)

stat_arrow <-
function(mapping = NULL,
data = NULL,
geom = "polygon",
position = "identity",
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE,
...,
bp = 6000,
# middle = 4,
arrowhead_size = 8
) {
ggplot2::layer(
stat = "arrow",
data = data,
mapping = mapping,
geom = geom,
position = position,
show.legend = show.legend,
inherit.aes = inherit.aes,
params = list(
na.rm = na.rm,
bp = bp,
# middle = middle,
arrowhead_size = arrowhead_size,
# start = start,
# end = end,
...)
#' Custom Stat: Arrow
#'
#' This stat creates arrow shapes in a ggplot.
#'
#' @param mapping The aesthetic mapping, usually constructed with aes().
#' @param data The dataset to be used in the layer.
#' @param geom The geometric object to use for the layer, default is "polygon".
#' @param position Position adjustment, default is "identity".
#' @param na.rm Should missing values be removed?
#' @param show.legend Logical. Should this layer be included in the legends?
#' @param inherit.aes Should inherit aesthetics from the parent plot?
#' @param ... Other arguments passed to layer().
#' @param bp The base parameter for the arrow shape.
#' @param arrowhead_size The size of the arrowhead.
#' @return A ggplot layer with arrow shapes.
#' @export
#'
#' @seealso \code{\link[ggplot2]{geom_segment}}
#'
#' @details
#' Used for drawing features for a plasmid that work in both cartesian and
#' polad coordinate systems.
#'
#' @note
#' The `bp` parameter determines the base parameter of the arrow shape.
#' The `arrowhead_size` parameter controls the size of the arrowhead.
#'
#' @keywords plotting
stat_arrow <- function(mapping = NULL,
data = NULL,
geom = "polygon",
position = "identity",
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE,
...,
bp = 6000,
arrowhead_size = 8
) {
ggplot2::layer(
stat = "arrow",
data = data,
mapping = mapping,
geom = geom,
position = position,
show.legend = show.legend,
inherit.aes = inherit.aes,
params = list(
na.rm = na.rm,
bp = bp,
arrowhead_size = arrowhead_size,
...
)
}
)
}
24 changes: 12 additions & 12 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
url: https://bradyajohnston.github.io/plasmapr
authors:
Brady Johnston:
href: https://bradyajohnston.github.io
template:
params:
bootswatch: flatly
ganalytics: G-Z2J58R6DJC
docsearch:
index_name: plasmapr
url: https://github.com/bradyajohnston/plasmapR, https://bradyajohnston.github.io/plasmapR/

authors:
Brady Johnston:
href: https://bradyajohnston.github.io

template:
params:
bootswatch: flatly
ganalytics: G-Z2J58R6DJC
docsearch:
index_name plasmapr
Loading
Loading