Moved file import away from greedyMix() (#25)

This commit is contained in:
Waldir Leoncio 2023-08-09 11:11:12 +02:00
parent 7888012bf6
commit bf7d05eb8d
2 changed files with 39 additions and 36 deletions

View file

@ -13,40 +13,5 @@
#' data <- system.file("extdata", "FASTA_clustering_haploid.fasta", package = "rBAPS") #' data <- system.file("extdata", "FASTA_clustering_haploid.fasta", package = "rBAPS")
#' greedyMix(data) #' greedyMix(data)
greedyMix <- function(data, format, verbose = TRUE) { greedyMix <- function(data, format, verbose = TRUE) {
# Parsing data format ------------------------------------------------------ data <- importFile(data, format, verbose)
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 if (format == "genepop") {
if (toupper(adegenet::.readExt(data)) == "TXT") {
message("Creating a copy of the file with the .gen extension")
dataGen <- gsub("txt", "gen", data)
file.copy(data, dataGen)
out <- adegenet::read.genepop(dataGen)
} else {
out <- adegenet::read.genepop(data)
}
} else {
stop("Format not supported.")
}
return(out)
} }

38
R/importFile.R Normal file
View file

@ -0,0 +1,38 @@
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 if (format == "genepop") {
if (toupper(adegenet::.readExt(data)) == "TXT") {
message("Creating a copy of the file with the .gen extension")
dataGen <- gsub("txt", "gen", data)
file.copy(data, dataGen)
out <- adegenet::read.genepop(dataGen)
} else {
out <- adegenet::read.genepop(data)
}
} else {
stop("Format not supported.")
}
return(out)
}