Java Code Examples for htsjdk.samtools.SamReader#query()

The following examples show how to use htsjdk.samtools.SamReader#query() . 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: Reader.java    From dataflow-java with Apache License 2.0 6 votes vote down vote up
void openFile() throws IOException {
  LOG.info("Processing shard " + shard);
  final SamReader reader = BAMIO.openBAM(storageClient, shard.file,
      options.getStringency());
  iterator = null;
  if (reader.hasIndex() && reader.indexing() != null) {
    if (filter == Filter.UNMAPPED_ONLY) {
      LOG.info("Processing unmapped");
      iterator = reader.queryUnmapped();
    } else if (shard.span != null) {
      LOG.info("Processing span for " + shard.contig);
      iterator = reader.indexing().iterator(shard.span);
    } else if (shard.contig.referenceName != null && !shard.contig.referenceName.isEmpty()) {
      LOG.info("Processing all bases for " + shard.contig);
      iterator = reader.query(shard.contig.referenceName, (int) shard.contig.start,
          (int) shard.contig.end, false);
    }
  }
  if (iterator == null) {
    LOG.info("Processing all reads");
    iterator = reader.iterator();
  }
}
 
Example 2
Source File: HeaderInfo.java    From dataflow-java with Apache License 2.0 5 votes vote down vote up
public static HeaderInfo getHeaderFromBAMFile(Storage.Objects storage, String BAMPath, Iterable<Contig> explicitlyRequestedContigs) throws IOException {
  HeaderInfo result = null;

  // Open and read start of BAM
  LOG.info("Reading header from " + BAMPath);
  final SamReader samReader = BAMIO
      .openBAM(storage, BAMPath, ValidationStringency.DEFAULT_STRINGENCY);
  final SAMFileHeader header = samReader.getFileHeader();
  Contig firstContig = getFirstExplicitContigOrNull(header, explicitlyRequestedContigs);
  if (firstContig == null) {
    final SAMSequenceRecord seqRecord = header.getSequence(0);
    firstContig = new Contig(seqRecord.getSequenceName(), -1, -1);
  }

  LOG.info("Reading first chunk of reads from " + BAMPath);
  final SAMRecordIterator recordIterator = samReader.query(
      firstContig.referenceName, (int)firstContig.start + 1, (int)firstContig.end + 1, false);

  Contig firstShard = null;
  while (recordIterator.hasNext() && result == null) {
    SAMRecord record = recordIterator.next();
    final int alignmentStart = record.getAlignmentStart();
    if (firstShard == null && alignmentStart > firstContig.start &&
        (alignmentStart < firstContig.end || firstContig.end == -1)) {
      firstShard = new Contig(firstContig.referenceName, alignmentStart, alignmentStart);
      LOG.info("Determined first shard to be " + firstShard);
      result = new HeaderInfo(header, firstShard);
    }
  }
  recordIterator.close();
  samReader.close();

  if (result == null) {
    throw new IOException("Did not find reads for the first contig " + firstContig.toString());
  }
  LOG.info("Finished header reading from " + BAMPath);
  return result;
}
 
Example 3
Source File: BAMIOITCase.java    From dataflow-java with Apache License 2.0 5 votes vote down vote up
@Test
public void openBAMTest() throws IOException {
 GCSOptions popts = PipelineOptionsFactory.create().as(GCSOptions.class);
 final Storage.Objects storageClient = Transport.newStorageClient(popts).build().objects();

 SamReader samReader = BAMIO.openBAM(storageClient, TEST_BAM_FNAME, ValidationStringency.DEFAULT_STRINGENCY);
 SAMRecordIterator iterator =  samReader.query("1", 550000, 560000, false);
 int readCount = 0;
 while (iterator.hasNext()) {
     iterator.next();
     readCount++;
 }
 Assert.assertEquals("Unexpected count of unmapped reads",
	  EXPECTED_UNMAPPED_READS_COUNT, readCount);
}