Skip to content

Commit

Permalink
New GRanges::coverage() method, and metadata support for TSV output.
Browse files Browse the repository at this point in the history
  • Loading branch information
vsbuffalo committed Mar 7, 2024
1 parent 071a1bc commit 587b0cb
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ pub fn build_tsv_writer_with_config(
None => Box::new(io::stdout()),
};

// Write metadata, if there.
if let Some(metadata_rows) = &config.metadata {
for metadata_row in metadata_rows {
writeln!(writer_boxed, "#{}", metadata_row)?;
}
}

// Write headers, if there.
if let Some(headers) = &config.headers {
writeln!(writer_boxed, "{}", headers.join("\t"))?;
}
Expand Down Expand Up @@ -955,6 +963,7 @@ impl FeatureDensity {
let config = TsvConfig {
no_value_string: "NA".to_string(),
headers: Some(headers),
metadata: None,
};
window_counts.write_to_tsv(self.output.as_ref(), &config)?;
}
Expand All @@ -966,6 +975,7 @@ impl FeatureDensity {
let config = TsvConfig {
no_value_string: "NA".to_string(),
headers: Some(headers),
metadata: None,
};
window_counts.write_to_tsv(self.output.as_ref(), &config)?;
}
Expand Down
23 changes: 22 additions & 1 deletion src/granges.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,14 @@ where
std::mem::take(&mut self.ranges)
}

/// Take both the ranges and data from a [`GRanges`] object,
/// returning them in a tuple.
pub fn take_both(&mut self) -> Result<(GenomeMap<C>, T), GRangesError> {
let data = std::mem::take(&mut self.data).ok_or(GRangesError::NoDataContainer)?;
let ranges = std::mem::take(&mut self.ranges);
Ok((ranges, data))
}

}

impl<C, T> GRanges<C, T>
Expand Down Expand Up @@ -219,8 +222,9 @@ impl<'a, C, T> AsGRangesRef<'a, C, T> for GRanges<C, T> {
}
}

impl<'a, T> GenomicRangesTsvSerialize<'a, VecRangesIndexed> for GRanges<VecRangesIndexed, T>
impl<'a, C, T> GenomicRangesTsvSerialize<'a, C> for GRanges<C, T>
where
C: IterableRangeContainer,
T: IndexedDataContainer + 'a,
<T as IndexedDataContainer>::Item<'a>: Serialize,
{
Expand Down Expand Up @@ -439,6 +443,18 @@ where
}
Ok(all_midpoints)
}

/// Get the total coverage (the sum of all range widths).
pub fn coverage(&self) -> Position {
let mut coverage = 0;
for (_seqname, ranges) in self.ranges.iter() {
for range in ranges.iter_ranges() {
coverage += range.width()
}
}
coverage
}

}

impl<C, T> GRanges<C, T>
Expand Down Expand Up @@ -702,6 +718,11 @@ where
pub fn midpoints(&self) -> Result<GenomeMap<Vec<Position>>, GRangesError> {
self.0.midpoints()
}

/// Get the total coverage (the sum of all range widths).
pub fn coverage(&self) -> Position {
self.0.coverage()
}
}

impl<U> GRanges<VecRangesIndexed, Vec<U>> {
Expand Down
2 changes: 2 additions & 0 deletions src/io/tsv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ lazy_static! {
pub static ref BED_TSV: TsvConfig = TsvConfig {
no_value_string: ".".to_string(),
headers: None,
metadata: None,
};
}

Expand All @@ -17,4 +18,5 @@ lazy_static! {
pub struct TsvConfig {
pub no_value_string: String,
pub headers: Option<Vec<String>>,
pub metadata: Option<Vec<String>>
}

0 comments on commit 587b0cb

Please sign in to comment.