@@ -0,0 +1,3504 @@
+package com.github.sparkcaller;%0A%0Aimport htsjdk.samtools.*;%0Aimport picard.sam.MergeSamFiles;%0Aimport scala.Tuple2;%0A%0Aimport java.io.File;%0Aimport java.io.IOException;%0Aimport java.util.ArrayList;%0Aimport java.util.List;%0A%0Apublic class SAMFileUtils %7B%0A%0A    public static File mergeBAMFiles(List%3CFile%3E samFiles, String outputFileName) %7B%0A        MergeSamFiles samMerger = new MergeSamFiles();%0A        ArrayList%3CString%3E args = new ArrayList%3C%3E();%0A%0A        for (File samFile : samFiles) %7B%0A            args.add(%22I=%22);%0A            args.add(samFile.getPath());%0A        %7D%0A%0A        args.add(%22O=%22);%0A        args.add(outputFileName);%0A%0A        samMerger.instanceMain(args.toArray(new String%5B0%5D));%0A%0A        return new File(outputFileName);%0A    %7D%0A%0A    public static int getSAMNumberOfLines(SamReader samReader) %7B%0A        SAMRecordIterator samIterator = samReader.iterator();%0A%0A        int samRecordSize = 0;%0A        // Get the size of the SAM record.%0A        while(samIterator.hasNext()) %7B%0A            SAMRecord record = samIterator.next();%0A            samRecordSize++;%0A        %7D%0A        samIterator.close();%0A%0A        return samRecordSize;%0A    %7D%0A%0A    public static List%3CFile%3E splitBAMFile(File bamFile, int numSplits) throws IOException %7B%0A        List%3CFile%3E outputFiles = new ArrayList%3C%3E();%0A%0A        SamReader samFile = SamReaderFactory.makeDefault().open(bamFile);%0A%0A        int SAMNumRecords = getSAMNumberOfLines(samFile);%0A%0A        SAMFileWriterFactory samWriterFactory =  new SAMFileWriterFactory();%0A        SAMRecordIterator samIterator = samFile.iterator();%0A%0A        // We rather want to have fewer records per file and more files, as this makes it easier to utilize all nodes.%0A        int recordsPerFile = SAMNumRecords / numSplits;%0A%0A        int currNumRecords = 0;%0A        int currFileNum = 0;%0A%0A        SAMFileHeader samHeader = samFile.getFileHeader();%0A%0A        String splitBaseFilename = Utils.removeExtenstion(bamFile.getPath(), %22bam%22);%0A        Tuple2%3CSAMFileWriter, File%3E currSplitTuple = getSplitSAMWriter(splitBaseFilename, currFileNum, true,%0A                                                                       samHeader, samWriterFactory);%0A        SAMFileWriter currSplitSAMWriter = currSplitTuple._1;%0A        outputFiles.add(currSplitTuple._2);%0A%0A        while(samIterator.hasNext()) %7B%0A            if (currNumRecords == recordsPerFile) %7B%0A                currSplitSAMWriter.close();%0A%0A                currNumRecords = 0;%0A                currFileNum++;%0A                currSplitTuple = getSplitSAMWriter(splitBaseFilename, currFileNum, true, samHeader, samWriterFactory);%0A%0A                currSplitSAMWriter = currSplitTuple._1;%0A                outputFiles.add(currSplitTuple._2);%0A            %7D%0A%0A            currSplitSAMWriter.addAlignment(samIterator.next());%0A            currNumRecords++;%0A        %7D%0A%0A        currSplitSAMWriter.close();%0A        samIterator.close();%0A        samFile.close();%0A%0A        return outputFiles;%0A    %7D%0A%0A    public static Tuple2%3CSAMFileWriter, File%3E getSplitSAMWriter(String baseFilename, int id, boolean isSorted,%0A                                                                SAMFileHeader SAMheader,%0A                                                                SAMFileWriterFactory samFileWriterFactory) %7B%0A%0A        File newSplitFile = new File(baseFilename + %22-split-%22 + Integer.toString(id) + %22.bam%22);%0A        SAMFileWriter currSAMWriter = samFileWriterFactory.makeSAMOrBAMWriter(SAMheader, isSorted, newSplitFile);%0A        return new Tuple2%3C%3E(currSAMWriter, newSplitFile);%0A%0A    %7D%0A%7D%0A

Add a utility class which can be used to perform SAM/BAM specific tasks.