| 241 | |
| 242 | impl FromBam for Region { |
| 243 | fn from_bam(bam_path: &Path) -> Result<Self> { |
| 244 | let mut bam = bam::IndexedReader::from_path(bam_path)?; |
| 245 | let header = bam.header(); |
| 246 | let target = header.target_names()[0]; |
| 247 | let target = std::str::from_utf8(target)?.to_string(); |
| 248 | bam.fetch(FetchDefinition::All)?; |
| 249 | let start = bam |
| 250 | .records() |
| 251 | .next() |
| 252 | .context( |
| 253 | "Could not find first alignment in bam file. Please specify a region with -g.", |
| 254 | )?? |
| 255 | .pos(); |
| 256 | let end = bam |
| 257 | .records() |
| 258 | .last() |
| 259 | .context( |
| 260 | "Could not find last alignment in bam file. Please specify a region with -g.", |
| 261 | )?? |
| 262 | .cigar() |
| 263 | .end_pos(); |
| 264 | Ok(Region { target, start, end }) |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | #[derive(Debug, Clone, Eq, PartialEq)] |