Skip to content

Commit

Permalink
feat: add record_context function to prelude
Browse files Browse the repository at this point in the history
  • Loading branch information
rob-p committed Feb 26, 2024
1 parent 2b2e098 commit aa85018
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
9 changes: 2 additions & 7 deletions examples/read_chunk_bulk.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use anyhow;
use libradicl;
use libradicl::chunk::Chunk;
use libradicl::rad_types;
use libradicl::record::{PiscemBulkReadRecord, PiscemBulkRecordContext, RecordContext};
use libradicl::record::{PiscemBulkReadRecord, PiscemBulkRecordContext};
use std::io::BufReader;

fn main() -> anyhow::Result<()> {
Expand All @@ -18,11 +17,7 @@ fn main() -> anyhow::Result<()> {

// Any extra context we may need to parse the records. In this case, it's the
// size of the barcode and the umi.
let tag_context = PiscemBulkRecordContext::get_context_from_tag_section(
&p.file_tags,
&p.read_tags,
&p.aln_tags,
)?;
let tag_context = p.get_record_context::<PiscemBulkRecordContext>()?;
let first_chunk = Chunk::<PiscemBulkReadRecord>::from_bytes(&mut ifile, &tag_context);
println!(
"Chunk :: nbytes: {}, nrecs: {}",
Expand Down
8 changes: 2 additions & 6 deletions examples/read_chunk_single_cell.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use anyhow;
use libradicl::chunk::Chunk;
use libradicl::header;
use libradicl::record::{AlevinFryReadRecord, AlevinFryRecordContext, RecordContext};
use libradicl::record::{AlevinFryReadRecord, AlevinFryRecordContext};
use std::io::BufReader;

fn main() -> anyhow::Result<()> {
Expand All @@ -17,11 +17,7 @@ fn main() -> anyhow::Result<()> {
println!("tag map {:?}\n", tag_map);
// Any extra context we may need to parse the records. In this case, it's the
// size of the barcode and the umi.
let tag_context = AlevinFryRecordContext::get_context_from_tag_section(
&p.file_tags,
&p.read_tags,
&p.aln_tags,
)?;
let tag_context = p.get_record_context::<AlevinFryRecordContext>()?;
let first_chunk = Chunk::<AlevinFryReadRecord>::from_bytes(&mut ifile, &tag_context);
println!(
"Chunk :: nbytes: {}, nrecs: {}",
Expand Down
11 changes: 11 additions & 0 deletions src/header.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{self as libradicl, constants};
use libradicl::rad_types::{TagSection, TagSectionLabel};
use libradicl::record::RecordContext;
use noodles_sam as sam;
use scroll::Pread;
use std::io::Read;
Expand Down Expand Up @@ -162,4 +163,14 @@ impl RadPrelude {
//writeln!(&mut s, "file-level tag values [{:?}]", self.file_tag_vals)?;
Ok(s)
}

/// Obtain a [RecordContext] for a record of type `R` from this prelude, by
/// using the associated [TagSection]s. **Note**: Since this function
/// constructs the resulting `R` itself, and doesn't take any `R` parameter,
/// then it must always be invoked with the proper
/// [turbofish](https://doc.rust-lang.org/1.30.0/book/2018-edition/appendix-02-operators.html?highlight=turbofish#non-operator-symbols)
/// notation.
pub fn get_record_context<R: RecordContext>(&self) -> anyhow::Result<R> {
R::get_context_from_tag_section(&self.file_tags, &self.read_tags, &self.aln_tags)
}
}

0 comments on commit aa85018

Please sign in to comment.