This is a test version of Biostars. For the public version, visit https://www.biostars.org.
BAM cannot be indexed without setting a fileSource for record Error while trying to generate an index file with htsjdk

I'm working on a program and I want it to have an option to generate an index file from inputted sorted BAM file. My code looks like this:

File chosenFile = fileChooser.getSelectedFile();
File output;
String path = chosenFile.getPath();
int lastSlash = path.lastIndexOf("/");
String baseFileName = path.substring(lastSlash + 1, path.length());

if (baseFileName.endsWith(BamFileIoUtils.BAM_FILE_EXTENSION)) {
    final int index = baseFileName.lastIndexOf(".");
    output = new File(baseFileName.substring(0, index) + BAMIndex.BAMIndexSuffix);
  } else {
    output = new File(baseFileName + BAMIndex.BAMIndexSuffix);
    }

    IOUtil.assertFileIsWritable(output);
    final SamReader bam;
    IOUtil.assertFileIsReadable(chosenFile);

bam = SamReaderFactory.makeDefault().validationStringency(ValidationStringency.SILENT).open(chosenFile);
SAMFileSource source = bam.getfi
if (!bam.getFileHeader().getSortOrder().equals(SAMFileHeader.SortOrder.coordinate)) {
    throw new SAMException("Input bam file must be sorted by coordinates.");

    }
BAMIndexer.createIndex(bam, output);
CloserUtil.close(bam);

The BAM files that I'm using are 100% sorted so that's not the issue. Anyone got any idea why am I getting this error and maybe how to fix it?

samtools htsjdk java bam

what is the outout of

$ file in.bam
$ samtools view -H in.bam | head
@HD VN:1.4  SQ:unsorted SO:coordinate
@SQ SN:gi|449020133|emb|BX284601.5|_Caenorhabditis_elegans_Bristol_N2_genomic_chromosome,_I LN:15072434
@SQ SN:gi|449020134|emb|BX284602.5|_Caenorhabditis_elegans_Bristol_N2_genomic_chromosome,_II    LN:15279421
@SQ SN:gi|449020129|emb|BX284603.4|_Caenorhabditis_elegans_Bristol_N2_genomic_chromosome,_III   LN:13783801
@SQ SN:gi|449020130|emb|BX284604.4|_Caenorhabditis_elegans_Bristol_N2_genomic_chromosome,_IV    LN:17493829
@SQ SN:gi|449020131|emb|BX284605.5|_Caenorhabditis_elegans_Bristol_N2_genomic_chromosome,_V LN:20924180
@SQ SN:gi|449020132|emb|BX284606.5|_Caenorhabditis_elegans_Bristol_N2_genomic_chromosome,_X LN:17718942
@PG ID:01   PN:ART_Illumina CL:/home/isovic/graphmap/aligneval/src/../tools/art_bin_VanillaIceCream/art_illumina -i /home/isovic/graphmap/aligneval/src/../reference-genomes/caenorhabditis_elegans.fa -o /home/isovic/graphmap/aligneval/src/../reads-simulated/Illumina-1k-single_end/caenorhabditis_elegans/reads -l 150 -f 4 -sam -rs 1427801776
@PG ID:samtools PN:samtools PP:01   VN:1.13 CL:samtools view -bS reads.sam
@PG ID:samtools.1   PN:samtools PP:samtools VN:1.13 CL:samtools sort -o reads_sorted.bam reads.bam

1 answer

add

 .enable(SamReaderFactory.Option.INCLUDE_SOURCE_IN_RECORDS)

to the SamReaderFactory

like this:

bam = SamReaderFactory.makeDefault().validationStringency(ValidationStringency.SILENT).enable(SamReaderFactory.Option.INCLUDE_SOURCE_IN_RECORDS).open(chosenFile);

Log in to answer this question.