@@ -407,20 +407,29 @@ public boolean hasIndex() {
407
407
*/
408
408
@ Override
409
409
public BAMIndex getIndex () {
410
- if (!hasIndex ())
410
+ if (!hasIndex ()) {
411
411
throw new SAMException ("No index is available for this BAM file." );
412
+ }
412
413
if (mIndex == null ) {
413
- SamIndexes samIndex = getIndexType ();
414
- if (samIndex == null ) {
415
- mIndex = mEnableIndexCaching ? new CachingBAMFileIndex (mIndexStream , getFileHeader ().getSequenceDictionary ())
416
- : new DiskBasedBAMFileIndex (mIndexStream , getFileHeader ().getSequenceDictionary ());
417
- } else if (samIndex .equals (SamIndexes .BAI )) {
418
- mIndex = mEnableIndexCaching ? new CachingBAMFileIndex (mIndexFile , getFileHeader ().getSequenceDictionary (), mEnableIndexMemoryMapping )
419
- : new DiskBasedBAMFileIndex (mIndexFile , getFileHeader ().getSequenceDictionary (), mEnableIndexMemoryMapping );
420
- } else if (samIndex .equals (SamIndexes .CSI )) {
421
- mIndex = new CSIIndex (mIndexFile , mEnableIndexMemoryMapping , getFileHeader ().getSequenceDictionary ());
422
- } else {
423
- throw new SAMFormatException ("Unsupported BAM index file: " + mIndexFile .getName ());
414
+ final SamIndexes samIndexType = getIndexType ();
415
+ final SAMSequenceDictionary sequenceDictionary = getFileHeader ().getSequenceDictionary ();
416
+ if (mIndexFile != null ) {
417
+ if (samIndexType .equals (SamIndexes .BAI )) {
418
+ mIndex = mEnableIndexCaching ? new CachingBAMFileIndex (mIndexFile , sequenceDictionary , mEnableIndexMemoryMapping )
419
+ : new DiskBasedBAMFileIndex (mIndexFile , sequenceDictionary , mEnableIndexMemoryMapping );
420
+ } else if (samIndexType .equals (SamIndexes .CSI )) {
421
+ mIndex = new CSIIndex (mIndexFile , mEnableIndexMemoryMapping , sequenceDictionary );
422
+ } else {
423
+ throw new SAMFormatException ("Unsupported BAM index file format: " + mIndexFile .getName ());
424
+ }
425
+ } else if (mIndexStream != null ) {
426
+ if (samIndexType .equals (SamIndexes .BAI )) {
427
+ mIndex = new CachingBAMFileIndex (mIndexStream , sequenceDictionary );
428
+ } else if (samIndexType .equals (SamIndexes .CSI )) {
429
+ mIndex = new CSIIndex (mIndexStream , sequenceDictionary );
430
+ } else {
431
+ throw new SAMFormatException ("Unsupported BAM index file format: " + mIndexStream .getSource ());
432
+ }
424
433
}
425
434
}
426
435
@@ -438,8 +447,13 @@ public SamIndexes getIndexType() {
438
447
} else if (mIndexFile .getName ().toLowerCase ().endsWith (FileExtensions .CSI )) {
439
448
return SamIndexes .CSI ;
440
449
}
441
-
442
450
throw new SAMFormatException ("Unknown BAM index file type: " + mIndexFile .getName ());
451
+ } else if (mIndexStream != null ) {
452
+ final SamIndexes samIndexesType = SamIndexes .getSAMIndexTypeFromStream (mIndexStream );
453
+ if (samIndexesType == SamIndexes .BAI || samIndexesType == SamIndexes .CSI ) {
454
+ return samIndexesType ;
455
+ }
456
+ throw new SAMFormatException (String .format ("Unknown BAM index file type: %s in %s" , samIndexesType , mIndexStream .getSource ()));
443
457
}
444
458
445
459
return null ;
0 commit comments