htsjdk.samtools.BAMIndexer Java Examples
The following examples show how to use
htsjdk.samtools.BAMIndexer.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SamBamUtils.java From chipster with MIT License | 6 votes |
public void indexBam(File bamFile, File baiFile) { SAMFileReader.setDefaultValidationStringency(ValidationStringency.SILENT); final SamReader bam; // input from a normal file IOUtil.assertFileIsReadable(bamFile); bam = SamReaderFactory.makeDefault().referenceSequence(null) .enable(SamReaderFactory.Option.INCLUDE_SOURCE_IN_RECORDS) .open(bamFile); if (bam.type() != SamReader.Type.BAM_TYPE) { throw new SAMException("Input file must be bam file, not sam file."); } if (!bam.getFileHeader().getSortOrder().equals(SAMFileHeader.SortOrder.coordinate)) { throw new SAMException("Input bam file must be sorted by coordinate"); } BAMIndexer.createIndex(bam, baiFile); CloserUtil.close(bam); }
Example #2
Source File: BAMTestUtil.java From Hadoop-BAM with MIT License | 4 votes |
public static File writeBamFile(int numPairs, SAMFileHeader.SortOrder sortOrder) throws IOException { // file will be both queryname and coordinate sorted, so use one or the other SAMRecordSetBuilder samRecordSetBuilder = new SAMRecordSetBuilder(true, sortOrder); for (int i = 0; i < numPairs; i++) { int chr = 20; int start1 = (i + 1) * 1000; int start2 = start1 + 100; if (i == 5) { // add two unmapped fragments instead of a mapped pair samRecordSetBuilder.addFrag(String.format("test-read-%03d-1", i), chr, start1, false, true, null, null, -1, false); samRecordSetBuilder.addFrag(String.format("test-read-%03d-2", i), chr, start2, false, true, null, null, -1, false); } else { samRecordSetBuilder.addPair(String.format("test-read-%03d", i), chr, start1, start2); } } if (numPairs > 0) { // add two unplaced unmapped fragments if non-empty samRecordSetBuilder.addUnmappedFragment(String.format ("test-read-%03d-unplaced-unmapped", numPairs++)); samRecordSetBuilder.addUnmappedFragment(String.format ("test-read-%03d-unplaced-unmapped", numPairs++)); } final File bamFile = File.createTempFile("test", ".bam"); bamFile.deleteOnExit(); SAMFileHeader samHeader = samRecordSetBuilder.getHeader(); final SAMFileWriter bamWriter = new SAMFileWriterFactory() .makeSAMOrBAMWriter(samHeader, true, bamFile); for (final SAMRecord rec : samRecordSetBuilder.getRecords()) { bamWriter.addAlignment(rec); } bamWriter.close(); // create BAM index if (sortOrder.equals(SAMFileHeader.SortOrder.coordinate)) { SamReader samReader = SamReaderFactory.makeDefault() .enable(SamReaderFactory.Option.INCLUDE_SOURCE_IN_RECORDS) .open(bamFile); BAMIndexer.createIndex(samReader, new File(bamFile.getAbsolutePath() .replaceFirst("\\.bam$", BAMIndex.BAMIndexSuffix))); } return bamFile; }