Java Code Examples for htsjdk.samtools.SAMFileReader#close()
The following examples show how to use
htsjdk.samtools.SAMFileReader#close() .
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: TestQualityScorePreservation.java From cramtools with Apache License 2.0 | 6 votes |
private SAMRecord buildSAMRecord(String seqName, String line) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { baos.write("@HD\tVN:1.0\tGO:none SO:coordinate\n".getBytes()); baos.write(("@SQ\tSN:" + seqName + "\tLN:247249719\n").getBytes()); baos.write(line.replaceAll("\\s+", "\t").getBytes()); baos.close(); } catch (IOException e) { throw new RuntimeException(e); } ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); SAMFileReader r = new SAMFileReader(bais); try { return r.iterator().next(); } finally { r.close(); } }
Example 2
Source File: SamBamUtils.java From chipster with MIT License | 5 votes |
public static void closeIfPossible(SAMFileReader reader) { if (reader != null) { try { reader.close(); } catch (Exception e) { // Ignore } } }
Example 3
Source File: pullLargeLengths.java From HMMRATAC with GNU General Public License v3.0 | 4 votes |
/** * Read the data and create a list of lengths */ private void read(){ int counter = 0; SAMFileReader reader = new SAMFileReader(bam,index); ArrayList<Double> temp = new ArrayList<Double>(); for (int i = 0; i < genome.size();i++){ String chr = genome.get(i).getChrom(); int start = genome.get(i).getStart(); int stop = genome.get(i).getStop(); CloseableIterator<SAMRecord> iter = reader.query(chr,start,stop,false); while (iter.hasNext()){ SAMRecord record = null; try{ record = iter.next(); } catch(SAMFormatException ex){ System.out.println("SAM Record is problematic. Has mapQ != 0 for unmapped read. Will continue anyway"); } if(record != null){ if(!record.getReadUnmappedFlag() && !record.getMateUnmappedFlag() && record.getFirstOfPairFlag()) { if (record.getMappingQuality() >= minQ){ if (Math.abs(record.getInferredInsertSize()) > 100 && Math.abs(record.getInferredInsertSize()) < 1000){ counter+=1; temp.add((double)Math.abs(record.getInferredInsertSize())); } } } } } iter.close(); } reader.close(); lengths = new double[counter]; for (int i = 0;i < temp.size();i++){ if (temp.get(i) > 100){ lengths[i] = temp.get(i); } } }
Example 4
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 5
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(); }