code: public Path createIndex(Path outputIndex) throws IOException {
          FileUtils.checkDirectory(outputIndex.toAbsolutePath().getParent(), true);
  
          SamReaderFactory srf = SamReaderFactory.make().enable(SamReaderFactory.Option.INCLUDE_SOURCE_IN_RECORDS);
          srf.validationStringency(ValidationStringency.LENIENT);
          try (SamReader reader = srf.open(SamInputResource.of(bamFile.toFile()))) {
  
              // Files need to be sorted by coordinates to create the index
              SAMFileHeader.SortOrder sortOrder = reader.getFileHeader().getSortOrder();
              if (!sortOrder.equals(SAMFileHeader.SortOrder.coordinate)) {
                  throw new IOException("Sorted file expected. File '" + bamFile.toString()
                          + "' is not sorted by coordinates (" + sortOrder.name() + ")");
              }
  
              if (reader.type().equals(SamReader.Type.BAM_TYPE)) {
                  BAMIndexer.createIndex(reader, outputIndex.toFile(), Log.getInstance(BamManager.class));
              } else {
                  if (reader.type().equals(SamReader.Type.CRAM_TYPE)) {
                      // TODO This really needs to be tested!
                      SeekableStream streamFor = SeekableStreamFactory.getInstance().getStreamFor(bamFile.toString());
                      CRAMBAIIndexer.createIndex(streamFor, outputIndex.toFile(), Log.getInstance(BamManager.class),
                              ValidationStringency.DEFAULT_STRINGENCY);
                  } else {
                      throw new IOException("This is not a BAM or CRAM file. SAM files cannot be indexed");
                  }
              }
          }
          return outputIndex;
      }
docs: Creates a BAM/CRAM index file.
  @param outputIndex The bai index file to be created.
  @return
  @throws IOException