R/AssemblyStats.R

Defines functions N90 N50 NXX

Documented in N50 N90

### -----------------------------------------------------------------
### N50 calculation for a assembly
### Exported!
NXX <- function(filepath, XX=50){
  if(file_ext(filepath) == "2bit"){
    lengths <- seqlengths(TwoBitFile(filepath))
  }else if(file_ext(filepath) %in% c("fa", "fasta")){
    lengths <- fasta.seqlengths(filepath)
  }else{
    stop("The suffix can only be .2bit, .fa, .fasta!")
  }
  lengths <- as.numeric(sort(lengths, decreasing=TRUE))
  index <- which(cumsum(lengths) / sum(lengths) >= XX/100)[1]
  return(lengths[index])
}

N50 <- function(fn){
  return(NXX(fn, XX=50))
}

N90 <- function(fn){
  return(NXX(fn, XX=90))
}
ge11232002/CNEr documentation built on Oct. 26, 2022, 7:08 p.m.