Java Code Examples for htsjdk.samtools.SAMFileReader#setDefaultValidationStringency()
The following examples show how to use
htsjdk.samtools.SAMFileReader#setDefaultValidationStringency() .
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 static void sortSamBam(File samBamFile, File sortedBamFile) { SAMFileReader.setDefaultValidationStringency(ValidationStringency.SILENT); SAMFileReader reader = new SAMFileReader(IOUtil.openFileForReading(samBamFile)); SAMFileWriter writer = null; try { reader.getFileHeader().setSortOrder(SAMFileHeader.SortOrder.coordinate); writer = new SAMFileWriterFactory().makeBAMWriter(reader.getFileHeader(), false, sortedBamFile); Iterator<SAMRecord> iterator = reader.iterator(); while (iterator.hasNext()) { writer.addAlignment(iterator.next()); } } finally { closeIfPossible(reader); closeIfPossible(writer); } }
Example 2
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 3
Source File: SamBamUtils.java From chipster with MIT License | 6 votes |
public String printSamBam(InputStream samBamStream, int maxRecords) throws IOException { SAMFileReader.setDefaultValidationStringency(ValidationStringency.SILENT); SAMFileReader in = new SAMFileReader(samBamStream); SAMFileHeader header = in.getFileHeader(); ByteArrayOutputStream buffer = new ByteArrayOutputStream(); SAMFileWriter out = new SAMFileWriterFactory().makeSAMWriter(header, true, buffer); int i = 0; try { for (final SAMRecord rec : in) { if (i > maxRecords) { break; } out.addAlignment(rec); i++; } } finally { closeIfPossible(out); } if (i > maxRecords) { buffer.write("SAM/BAM too long for viewing, truncated here!\n".getBytes()); } return buffer.toString(); }
Example 4
Source File: BamDataSource.java From chipster with MIT License | 5 votes |
/** * Generally we would like to have both data and index files, * because otherwise we cannot access random locations. * * @param data * @param index * @throws URISyntaxException * @throws IOException */ public BamDataSource(DataUrl data, DataUrl index) throws URISyntaxException, IOException { super(data); // BAMFileReader emits useless warning to System.err that can't be turned off, // so we direct it to other stream and discard. PrintStream originalErr = System.err; System.setErr(new PrintStream(new ByteArrayOutputStream())); SAMFileReader.setDefaultValidationStringency(ValidationStringency.SILENT); this.reader = SamBamUtils.getSAMReader(data.getUrl(), index.getUrl()); LinkedList<String> chrList = new LinkedList<>(); // Iterate chromosomes to check naming convention for (SAMSequenceRecord sequenceRecord : this.reader.getFileHeader().getSequenceDictionary().getSequences()) { String name = sequenceRecord.getSequenceName(); chrList.add(name); } // Create unnormaliser for this naming convention // Look only at the first sequence (assume all have the same convention) this.chromosomeNameUnnormaliser = new ChromosomeNameUnnormaliser(chrList); // Restore System.err System.setErr(originalErr); }
Example 5
Source File: SamBamUtils.java From chipster with MIT License | 5 votes |
public void normaliseBam(File bamFile, File normalisedBamFile) { // Read in a BAM file and its header SAMFileReader.setDefaultValidationStringency(ValidationStringency.SILENT); SAMFileReader reader = new SAMFileReader(IOUtil.openFileForReading(bamFile)); SAMFileWriter writer = null; try { SAMFileHeader normalisedHeader = reader.getFileHeader(); // Alter the chromosome names in header's SAMSequenceDictionary SAMSequenceDictionary normalisedDictionary = new SAMSequenceDictionary(); for (SAMSequenceRecord sequenceRecord : normalisedHeader.getSequenceDictionary().getSequences()) { // Normalise chromosome String sequenceName = chromosomeNormaliser.normaliseChromosome(sequenceRecord.getSequenceName()); normalisedDictionary.addSequence(new SAMSequenceRecord(sequenceName, sequenceRecord.getSequenceLength())); } normalisedHeader.setSequenceDictionary(normalisedDictionary); // Write new BAM file with normalised chromosome names writer = new SAMFileWriterFactory().makeBAMWriter(normalisedHeader, true, normalisedBamFile); for (final SAMRecord rec : reader) { rec.setHeader(normalisedHeader); writer.addAlignment(rec); } } finally { closeIfPossible(reader); closeIfPossible(writer); } }
Example 6
Source File: Cram2Fastq.java From cramtools with Apache License 2.0 | 4 votes |
@Override public void doRun() throws IOException { super.doRun(); fo.close(); if (fo.empty) return; log.info("Sorting overflow BAM: ", fo.file.length()); SAMFileReader.setDefaultValidationStringency(ValidationStringency.SILENT); SAMFileReader r = new SAMFileReader(fo.file); SAMRecordIterator iterator = r.iterator(); if (!iterator.hasNext()) { r.close(); fo.file.delete(); return; } SAMRecord r1 = iterator.next(); SAMRecord r2 = null; counter = multiFastqOutputter.getCounter(); log.info("Counter=" + counter); while (!brokenPipe.get() && iterator.hasNext()) { r2 = iterator.next(); if (r1.getReadName().equals(r2.getReadName())) { print(r1, r2); counter++; r1 = null; if (!iterator.hasNext()) break; r1 = iterator.next(); r2 = null; } else { print(r1, 0); r1 = r2; r2 = null; counter++; } } if (r1 != null) print(r1, 0); r.close(); fo.file.delete(); }
Example 7
Source File: TestBAMRecordView.java From cramtools with Apache License 2.0 | 4 votes |
@Test public void test() throws IOException { byte[] buf = new byte[1024]; BAMRecordView view = new BAMRecordView(buf); view.setRefID(0); view.setAlignmentStart(77); view.setMappingScore(44); view.setIndexBin(99); view.setFlags(555); view.setMateRefID(0); view.setMateAlStart(78); view.setInsertSize(133); view.setReadName("name1"); view.setCigar(TextCigarCodec.decode("10M")); view.setBases("AAAAAAAAAA".getBytes()); view.setQualityScores("BBBBBBBBBB".getBytes()); int id = 'A' << 16 | 'M' << 8 | 'A'; view.addTag(id, "Q".getBytes(), 0, 1); int len = view.finish(); System.out.println(Arrays.toString(Arrays.copyOf(buf, len))); ByteArrayOutputStream baos = new ByteArrayOutputStream(); SAMFileHeader header = new SAMFileHeader(); header.addSequence(new SAMSequenceRecord("14", 14)); ByteArrayOutputStream baos2 = new ByteArrayOutputStream(); SAMFileWriter writer = new SAMFileWriterFactory().makeBAMWriter(header, true, baos2); SAMRecord record = new SAMRecord(header); record.setReferenceIndex(0); record.setAlignmentStart(1); record.setCigarString("10M"); record.setFlags(555); record.setMappingQuality(44); record.setMateReferenceIndex(0); record.setMateAlignmentStart(0); record.setInferredInsertSize(133); record.setReadName("name1"); record.setReadBases("AAAAAAAAAA".getBytes()); record.setBaseQualities("BBBBBBBBBB".getBytes()); record.setAttribute("AM", 'Q'); System.out.println("BAMFileWriter.addAlignment():"); writer.addAlignment(record); System.out.println("."); writer.close(); System.out.println("------------------------------------------"); System.out.println(); System.out.println(new String(baos2.toByteArray())); System.out.println(); SAMFileReader.setDefaultValidationStringency(ValidationStringency.SILENT); SAMFileReader reader2 = new SAMFileReader(new ByteArrayInputStream(baos2.toByteArray())); SAMRecordIterator iterator = reader2.iterator(); while (iterator.hasNext()) { record = iterator.next(); System.out.println(record.getSAMString()); } System.out.println("------------------------------------------"); BlockCompressedOutputStream bcos = new BlockCompressedOutputStream(baos, null); bcos.write("BAM\1".getBytes()); bcos.write(toByteArray(header)); CramInt.writeInt32(header.getSequenceDictionary().size(), bcos); for (final SAMSequenceRecord sequenceRecord : header.getSequenceDictionary().getSequences()) { byte[] bytes = sequenceRecord.getSequenceName().getBytes(); CramInt.writeInt32(bytes.length + 1, bcos); bcos.write(sequenceRecord.getSequenceName().getBytes()); bcos.write(0); CramInt.writeInt32(sequenceRecord.getSequenceLength(), bcos); } bcos.write(buf, 0, len); bcos.close(); System.out.println(new String(baos.toByteArray())); SAMFileReader reader = new SAMFileReader(new ByteArrayInputStream(baos.toByteArray())); iterator = reader.iterator(); while (iterator.hasNext()) { record = iterator.next(); System.out.println(record.getSAMString()); } reader.close(); }