From 814937f3b66b84169a915f9cd5912a4cc55d5285 Mon Sep 17 00:00:00 2001 From: tcarver <tjc> Date: Thu, 1 Dec 2011 18:52:42 +0000 Subject: [PATCH] throw SAMException if BAM index not present --- .../artemis/components/alignment/BamView.java | 28 +++++++++++++------ 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/uk/ac/sanger/artemis/components/alignment/BamView.java b/uk/ac/sanger/artemis/components/alignment/BamView.java index c5b4e5483..64f11d106 100644 --- a/uk/ac/sanger/artemis/components/alignment/BamView.java +++ b/uk/ac/sanger/artemis/components/alignment/BamView.java @@ -94,6 +94,7 @@ import org.apache.log4j.Level; import net.sf.picard.sam.BuildBamIndex; import net.sf.samtools.AlignmentBlock; +import net.sf.samtools.SAMException; import net.sf.samtools.SAMFileHeader; import net.sf.samtools.SAMFileReader; import net.sf.samtools.SAMRecord; @@ -413,14 +414,25 @@ public class BamView extends JPanel File bamIndexFile = getBamIndexFile(bam); if(!bamIndexFile.exists()) { - System.out.println("Index file not found so using picard to index the BAM."); - System.out.println("This requires BAM to be sorted by coordinate and for the\n"+ - "sort order to be in the header line, HD."); - System.out.println("If this does not work run:\nsamtools index "+bam); - // Use Picard to index the file - // requires reads to be sorted by coordinate - new BuildBamIndex().instanceMain( - new String[]{ "I="+bam, "O="+bamIndexFile.getAbsolutePath(), "MAX_RECORDS_IN_RAM=50000" }); + try + { + logger4j.warn("Index file not found so using picard to index the BAM."); + // Use Picard to index the file + // requires reads to be sorted by coordinate + new BuildBamIndex().instanceMain( + new String[]{ "I="+bam, "O="+bamIndexFile.getAbsolutePath(), "MAX_RECORDS_IN_RAM=50000" }); + } + catch(SAMException e) + { + String ls = System.getProperty("line.separator"); + String msg = + "BAM index file is missing. The BAM file needs to be sorted and indexed"+ls+ + "This can be done using samtools (http://samtools.sf.net/):"+ls+ls+ + "samtools sort <in.bam> <out.prefix>"+ls+ + "samtools index <sorted.bam>"; + + throw new SAMException(msg); + } } final SAMFileReader samFileReader; -- GitLab