Check if a BAM file is corrupt in Java
Hi!
I am building a program in java and part of it would be creating and handling BAM files. My problem is the following: before writing I would like to check if a BAM file with the same name already exists and if yes, is it corrupt. I have found solutions for checking BAM files using samtools and picard, but these seem to be command-line based. Is there a way to check in java? My only option so far seems to be calling either samtools or picard using ProcessBuilder, but I would like to avoid this if possible, as I plan to have lot of BAMs and this doesn't seem to be an efficient solution.
Thanks in advance
• 1,290 views
•
link
1 answer
well you can use picard itself as a library.
java -cp /path/to/picard.jar:path/to/your.jar your.package.Main
and in your java code:
// see https://github.com/broadinstitute/picard/blob/master/src/main/java/picard/sam/ValidateSamFile.java
picard.sam.ValidateSamFile prog = new picard.sam.ValidateSamFile();
int returnValue = prog.instanceMain(new String[]{"-I","/path/to/your/bam", etc... etc..};
• 0 views
•
link
Log in to answer this question.
See: https://github.com/samtools/htsjdk/blob/master/src/main/java/htsjdk/samtools/BAMFileReader.java
don't use a BAMFileReader directly, use a SAMReaderFactory : https://www.javadoc.io/doc/com.github.samtools/htsjdk/1.132/htsjdk/samtools/SamReaderFactory.html