I'm writing a function in R for an R package which takes as input a BAM.
my_func = function(my_bam){
if (!file.exists(my_bam)){
stop("Input a valid BAM file.")
}
## do awesome stuff with BAM
}
Trying to write robust software, I would like to do more of a check whether this BAM is actually a BAM. Above, I'm just using file.exists()
.
I could also do something simple like "check whether the file has the extension *.bam
" with
grepl('*.bam', my_bam) ## TRUE for file.bam, FALSE for file.txt
However, is there a quick way to check that the input is actually a BAM? I would prefer to throw an error here, if the user inputs a *txt or *bedpe.
I don't think it's necessary to validate the BAM per se, whereby I believe most users rely upon Picard:
https://broadinstitute.github.io/picard/command-line-overview.html#ValidateSamFile
My first thought was Rsamtools for this operation:
https://bioconductor.org/packages/release/bioc/html/Rsamtools.html
However, it doesn't appear that there is a function here which checks the user inputs a BAM, e.g. BamFile()
doesn't appear to throw an error
> library(Rsamtools)
> fake_bam = 'nonsense_string.bam' ## a string I made up, not a real BAM
> BamFile(fake_bam)
class: BamFile
path: nonsense_string.bam
index: NA
isOpen: FALSE
yieldSize: NA
obeyQname: FALSE
asMates: FALSE
qnamePrefixEnd: NA
qnameSuffixStart: NA
How could I "check" that the user is inputting a BAM file? Perhaps pipe to samtools (which doesn't feel very stable in terms of software)?