Skip to content

flip_strand() function? #100

@eggrandio

Description

@eggrandio

Hello,

I was wondering if it would be useful to add a flip_strand() function to switch strand from "+" to "-" and viceversa, leaving "*" if unstranded. I am not aware of a function that does this in GenomicRanges.

I adapted the flip_strand_info() function from TranscriptomeReconstructoR. It does not cover the "*" case.

Probably there is a more elegant/efficient way of doing this:

#' Flip strand orientation of a GenomicRanges object
#'
#' @param gr \code{GRanges} or \code{GRangesList} object
#' @return Object of the same class as input with positive and negative strands flipped
#' @export
flip_strand <- function(gr) {
  stopifnot(class(gr) == "GRanges" || BiocGenerics::grepl("GRangesList", class(gr)))
  if (class(gr) == "GRanges") {
    BiocGenerics::strand(gr) <- ifelse(BiocGenerics::strand(gr) == "*", "*",
                                       ifelse(BiocGenerics::strand(gr) == "+", "-", "+"))
  } else {
    unl <- BiocGenerics::unlist(gr, use.names = FALSE)
    BiocGenerics::strand(unl) <- ifelse(BiocGenerics::strand(unl) == "*", "*",
                                        ifelse(BiocGenerics::strand(unl) == "+", "-", "+"))
    gr <- BiocGenerics::relist(unl, gr)
  }
  return(gr)
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions