package com.github.sparkcaller.preprocessing;

import htsjdk.samtools.SamReader;
import htsjdk.samtools.SamReaderFactory;
import org.apache.spark.api.java.function.Function;

import java.io.File;

public class BAMIndexer implements Function<File, File> {
    // Create an index (.bai) file for the given BAM file.
    public static void indexBAM(File bamFile) throws Exception {
        final SamReader bamReader = SamReaderFactory.makeDefault()
                .enable(SamReaderFactory.Option.INCLUDE_SOURCE_IN_RECORDS)
                .open(bamFile);

        File bamIndexFile = new File(bamFile.getPath() + ".bai");
        if (bamIndexFile.exists()) {
            bamIndexFile.delete();
        }

        htsjdk.samtools.BAMIndexer.createIndex(bamReader, bamIndexFile);
    }

    @Override
    public File call(File inputBAM) throws Exception {
        BAMIndexer.indexBAM(inputBAM);
        return new File(inputBAM.getPath() + ".bai");
    }
}

@@ -40,16 +40,59 @@
 ssing;%0A%0A
+import com.github.sparkcaller.utils.Utils;%0A
 import h
@@ -627,16 +627,39 @@
 ew File(
+Utils.removeExtenstion(
 bamFile.
@@ -659,32 +659,40 @@
 amFile.getPath()
+, %22bam%22)
  + %22.bai%22);%0A    
@@ -977,45 +977,16 @@
 urn 
-new File(inputBAM.getPath() + %22.bai%22)
+inputBAM
 ;%0A  

Make the BAMIndexer return the input file, as we don't need the name of the index file anyways.