Skip to content
Merged
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
/.gradle/
/build/
.metadata
.idea
Original file line number Diff line number Diff line change
@@ -1,47 +1,42 @@
package scriptmanager.scripts.BAM_Manipulation;

import htsjdk.samtools.BAMIndexer;
import htsjdk.samtools.SAMRecord;
import htsjdk.samtools.SamReader;
import htsjdk.samtools.SamReaderFactory;
import htsjdk.samtools.ValidationStringency;
import picard.sam.BuildBamIndex;

import java.io.File;
import java.io.IOException;
import java.text.NumberFormat;

import javax.swing.JOptionPane;
import java.util.ArrayList;

/**
* Picard wrapper for BuildBamIndex
*
* @author Erik Pavloski
* @see scriptmanager.window_interface.BAM_Manipulation.BAIIndexerWIndow
*/
public class BAIIndexer {
/**
* Index a BAM file and output said index to a file of the same name with a .bai
* extension
*
* @param input the BAM file to index
* @return the BAM index file (.bai)
* @throws IOException
*/
public static File generateIndex(File input) throws IOException {
SamReaderFactory factory = SamReaderFactory.makeDefault().enable(SamReaderFactory.Option.INCLUDE_SOURCE_IN_RECORDS, SamReaderFactory.Option.CACHE_FILE_BASED_INDEXES, SamReaderFactory.Option.VALIDATE_CRC_CHECKSUMS).validationStringency(ValidationStringency.LENIENT);
File retVal = null;
System.out.println("Generating New Index File...");
try{
String output = input.getCanonicalPath() + ".bai";
retVal = new File(output);
//Generate index
SamReader inputSam = factory.open(input);
BAMIndexer bamindex = new BAMIndexer(retVal, inputSam.getFileHeader());
int counter = 0;
for(SAMRecord record : inputSam) {
if(counter % 1000000 == 0) {
System.out.print("Tags processed: " + NumberFormat.getIntegerInstance().format(counter) + "\r");
System.out.flush();
}
counter++;
bamindex.processAlignment(record);
}
bamindex.finish();
inputSam.close();
System.out.println("\nIndex File Generated");
return retVal;
}
catch(htsjdk.samtools.SAMException exception){
System.out.println(exception.getMessage());
JOptionPane.showMessageDialog(null, exception.getMessage());
retVal = null;
}
// Tells user that their file is being generated
System.out.println("Generating Index File...");
// Build output filepath
String output = input.getCanonicalPath() + ".bai";
File retVal = new File(output);
// Instatiate Picard object
final BuildBamIndex buildBamIndex = new BuildBamIndex();
// Build input argument string
final ArrayList<String> args = new ArrayList<>();
args.add("INPUT=" + input.getAbsolutePath());
args.add("OUTPUT=" + retVal.getAbsolutePath());
// Call Picard with args
buildBamIndex.instanceMain(args.toArray(new String[args.size()]));

System.out.println("Index File Generated");
return retVal;
}
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,39 @@
package scriptmanager.scripts.BAM_Manipulation;

import htsjdk.samtools.SAMException;
import htsjdk.samtools.SAMFileHeader;
import htsjdk.samtools.SAMFileWriter;
import htsjdk.samtools.SAMFileWriterFactory;
import htsjdk.samtools.SAMRecord;
import htsjdk.samtools.SamReader;
import htsjdk.samtools.SamReaderFactory;
import htsjdk.samtools.util.IOUtil;
import picard.sam.SortSam;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

/**
* Picard wrapper for MergeSamFiles SortSam
*
* @author Erik Pavloski
* @see scriptmanager.window_interface.BAM_Manipulation.SortBAMWindow
*/
public class BAMFileSort {
public static void sort(File INPUT, File OUTPUT) {
IOUtil.assertFileIsReadable(INPUT);
IOUtil.assertFileIsWritable(OUTPUT);
final SamReader reader = SamReaderFactory.makeDefault().open(INPUT);
reader.getFileHeader().setSortOrder(SAMFileHeader.SortOrder.coordinate);
final SAMFileWriter writer = new SAMFileWriterFactory().makeSAMOrBAMWriter(reader.getFileHeader(), false, OUTPUT);
for (final SAMRecord rec: reader) {
writer.addAlignment(rec);
}
writer.close();
}
}
/**
* The following code uses Picard's SortSam to sort a BAM file by coordinate
*
* @param input the BAM file to be sorted (corresponds to INPUT)
* @param output the file to write the sorted BAM to (corresponds to OUTPUT)
* @throws SAMException
* @throws IOException
*/
public static void sort(File input, File output) throws SAMException, IOException {
// Tells user their File is being sorted
System.out.println("Sorting Bam File...");
// Instatiate Picard object
final SortSam sorter = new SortSam();
// Build input argument string
final ArrayList<String> args = new ArrayList<>();
args.add("INPUT=" + input.getAbsolutePath());
args.add("OUTPUT=" + output.getAbsolutePath());
args.add("SORT_ORDER=" + SAMFileHeader.SortOrder.coordinate);
// Call Picard with args
sorter.instanceMain(args.toArray(new String[args.size()]));
}
}
49 changes: 49 additions & 0 deletions src/main/java/scriptmanager/scripts/BAM_Manipulation/MergeBAM.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package scriptmanager.scripts.BAM_Manipulation;

import htsjdk.samtools.SAMException;
import picard.sam.MergeSamFiles;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

/**
* Picard wrapper for MergeSamFiles
*
* @author Erik Pavloski
* @see scriptmanager.window_interface.BAM_Manipulation
*/
public class MergeBAM {
/**
* Excecute the Picard MergeSamFiles command line tool after checking ever input file has been indexed.
*
* @param inputs the list of input files to merge (corresponds to several INPUT values)
* @param output the output file for the merged BAM file (corresponds to OUTPUT)
* @param useMultipleCpus whether or not to parallelize (corresponds to USE_THREADING)
* @throws SAMException
* @throws IOException
*/
public static void run(ArrayList<File> inputs, File output, boolean useMultipleCpus) throws SAMException, IOException {
// Check all BAM files have an index
for (int x = 0; x<inputs.size(); x++) {
File f = new File(inputs.get(x) + ".bai");
if(!f.exists() || f.isDirectory()) {
throw new SAMException("BAI Index File does not exist for: " + inputs.get(x).getName() + "\n");
}
}
// Tells the user their files are being merged
System.out.println("Merging BAM Files...");
// Merges the files
final MergeSamFiles merger = new MergeSamFiles();
final ArrayList<String> args = new ArrayList<>();
for (File input : inputs) {
args.add("INPUT=" + input.getAbsolutePath());
}
args.add("OUTPUT=" + output.getAbsolutePath());
if (useMultipleCpus) {
args.add("USE_THREADING=true");
}
merger.instanceMain(args.toArray(new String[args.size()]));
System.out.println("BAM Files Merged.");
}
}

This file was deleted.

Loading