Skip to content

Commit

Permalink
bam/examples/query: Support querying unmapped records
Browse files Browse the repository at this point in the history
  • Loading branch information
zaeleus committed Aug 31, 2023
1 parent b95d5ba commit e2523f0
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions noodles-bam/examples/bam_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,31 @@
use std::{env, io, path::PathBuf};

use noodles_bam as bam;
use noodles_sam as sam;
use noodles_sam::{self as sam, alignment::Record};
use sam::AlignmentWriter;

const UNMAPPED: &str = "*";

fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut args = env::args();

let src = args.nth(1).map(PathBuf::from).expect("missing src");
let region = args.next().expect("missing region").parse()?;
let raw_region = args.next().expect("missing region");

let mut reader = bam::indexed_reader::Builder::default().build_from_path(src)?;
let header = reader.read_header()?;

let query = reader.query(&header, &region)?;
let records: Box<dyn Iterator<Item = io::Result<Record>>> = if raw_region == UNMAPPED {
reader.query_unmapped(&header).map(Box::new)?
} else {
let region = raw_region.parse()?;
reader.query(&header, &region).map(Box::new)?
};

let stdout = io::stdout().lock();
let mut writer = sam::Writer::new(stdout);

for result in query {
for result in records {
let record = result?;
writer.write_alignment_record(&header, &record)?;
}
Expand Down

0 comments on commit e2523f0

Please sign in to comment.