2023-09-11 12:51:41 +02:00
|
|
|
#' @title Import data file
|
2024-09-13 13:52:39 +02:00
|
|
|
#' @description Imports data from several formats (FASTA, VCF, SAM, BAM).
|
2023-09-11 12:51:41 +02:00
|
|
|
#' @param data raw dataset
|
|
|
|
|
#' @param format data format (guesses from extension if not provided)
|
|
|
|
|
#' @param verbose if \code{TRUE}, prints extra output information
|
|
|
|
|
#' @return The data in a format that can be used by the other functions
|
|
|
|
|
#' @export
|
|
|
|
|
#' @examples
|
|
|
|
|
#' path_inst <- system.file("extdata", "", package = "rBAPS")
|
|
|
|
|
#' importFile(file.path(path_inst, "FASTA_clustering_haploid.fasta"))
|
2023-08-09 11:11:12 +02:00
|
|
|
importFile <- function(data, format, verbose) {
|
|
|
|
|
# Parsing data format ------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
if (missing(format)) {
|
|
|
|
|
format <- gsub(".*\\.(.+)$", "\\1", data)
|
|
|
|
|
message("Format not provided. Guessing from file extension: ", format)
|
|
|
|
|
}
|
|
|
|
|
format <- tolower(format)
|
|
|
|
|
|
|
|
|
|
# Dispatching to proper loading function -----------------------------------
|
|
|
|
|
|
|
|
|
|
if (format == "fasta") {
|
|
|
|
|
out <- load_fasta(data)
|
|
|
|
|
} else if (format == "vcf") {
|
|
|
|
|
out <- vcfR::read.vcfR(data, verbose = verbose)
|
|
|
|
|
} else if (format == "sam") {
|
|
|
|
|
stop(
|
|
|
|
|
"SAM files not directly supported. ",
|
|
|
|
|
"Install the samtools software and execute\n\n",
|
|
|
|
|
"samtools view -b ", data, " > out_file.bam\n\nto convert to BAM ",
|
|
|
|
|
"and try running this function again with 'format=BAM'"
|
|
|
|
|
)
|
|
|
|
|
} else if (format == "bam") {
|
|
|
|
|
out <- Rsamtools::scanBam(data)
|
|
|
|
|
} else {
|
|
|
|
|
stop("Format not supported.")
|
|
|
|
|
}
|
|
|
|
|
return(out)
|
|
|
|
|
}
|