R/fev2df.R

Defines functions fev2df

Documented in fev2df

# fev2df
#' Convert a palmscan Field-Equals-Value (FEV) column into a dataframe
#' @param fev.col character vector of FEV
#' @return A 1-column data.frame
#' @keywords palmid fev
#' @examples
#' # fev.df <- as.data.frame( apply(fev.tsv, 2, fev2df) )
#' @import dplyr ggplot2
#' @export
fev2df <- function(fev.col) {
  # input FEV
  fev.name  <- gsub("=.*", "", fev.col[1])
  fev.value <- gsub(".*=", "", fev.col)

  # check column names match
  fev.cols <- c("score", "query", "gene",	"order", "confidence",
                "qlen",	"pp_start",	"pp_end",	"pp_length",
                "v1_length", "v2_length",
                "pssm_total_score", "pssm_min_score",
                "motifs",	"super", "group", "comments")



  if ( !(fev.name %in% fev.cols) ) {
    print("fev.input:")
    print(fev.name)
    print("fev.expect:")
    print(fev.cols)
    error_msg <- c(".fev input has an unrecognized .fev value")
    stop(error_msg)
  }

  # Convert certain columns to different type
  # numerics
  fev.numerics <- c("score", "qlen", "pp_start", "pp_end", "pp_length",
                    "v1_length", "v2_length", "pssm_total_score")
  fev.factors <- c("gene", "order", "confidence")
  # else character

  if (fev.name %in% fev.numerics) {
    fev.value <- as.numeric(fev.value)
    # factors
  } else if ( fev.name %in% fev.factors) {
    fev.value <- as.factor(fev.value)
  }

  # return data.frame in canonical order
  ret.df <-  data.frame( col1 = fev.value )
    colnames(ret.df) <- fev.name

  return(ret.df)
}

Try the palmid package in your browser

Any scripts or data that you put into this service are public.

palmid documentation built on Oct. 15, 2021, 9:06 a.m.