Skip to content

Commit

Permalink
feat: update pyft to have a writer function
Browse files Browse the repository at this point in the history
  • Loading branch information
mrvollger committed Jan 11, 2024
1 parent e98ceed commit 3bb2f45
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 15 deletions.
3 changes: 3 additions & 0 deletions py-ft/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

bam_f = "../tests/data/center.bam"
fiberbam = pyft.Fiberbam(bam_f)
out_fiberbam = pyft.Fiberwriter("test.bam", bam_f)
rgn = ["chr1", 15_323_697, 120_000_000]
for fiber in tqdm.tqdm(fiberbam.fetch(*rgn)):
# the number of ccs passes
Expand All @@ -16,6 +17,8 @@
# lift reference positions to query (fiber) positions
fiber.lift_reference_positions([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

out_fiberbam.write(fiber)


for fiber in fiberbam.center(rgn[0], start=rgn[1], end=rgn[2], strand="-"):
# returns the same fiber object as above; however, all the positions have been modified to be relative to the region fetched
Expand Down
69 changes: 54 additions & 15 deletions py-ft/src/fiberdata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ pub struct Fiberdata {
/// offset for centering the fiber
#[pyo3(get, set)]
offset: Option<i64>,
/// record
fiber: FiberseqData,
}
#[pymethods]
impl Fiberdata {
Expand Down Expand Up @@ -121,32 +123,40 @@ impl Fiberdata {
strand,
)
};
let rg = if let Ok(Aux::String(f)) = fiber.record.aux(b"RG") {
let rg = if let std::result::Result::Ok(Aux::String(f)) = fiber.record.aux(b"RG") {
log::trace!("{f}");
f
} else {
"."
};

// fiberseq features
let m6a = Basemods::new(fiber.m6a.starts, fiber.m6a.reference_starts, fiber.m6a.ml);
let cpg = Basemods::new(fiber.cpg.starts, fiber.cpg.reference_starts, fiber.cpg.ml);
let m6a = Basemods::new(
fiber.m6a.starts.clone(),
fiber.m6a.reference_starts.clone(),
fiber.m6a.ml.clone(),
);
let cpg = Basemods::new(
fiber.cpg.starts.clone(),
fiber.cpg.reference_starts.clone(),
fiber.cpg.ml.clone(),
);

let nuc = Ranges {
starts: fiber.nuc.starts,
ends: fiber.nuc.ends,
lengths: fiber.nuc.lengths,
reference_starts: fiber.nuc.reference_starts,
reference_ends: fiber.nuc.reference_ends,
reference_lengths: fiber.nuc.reference_lengths,
starts: fiber.nuc.starts.clone(),
ends: fiber.nuc.ends.clone(),
lengths: fiber.nuc.lengths.clone(),
reference_starts: fiber.nuc.reference_starts.clone(),
reference_ends: fiber.nuc.reference_ends.clone(),
reference_lengths: fiber.nuc.reference_lengths.clone(),
};
let msp = Ranges {
starts: fiber.msp.starts,
ends: fiber.msp.ends,
lengths: fiber.msp.lengths,
reference_starts: fiber.msp.reference_starts,
reference_ends: fiber.msp.reference_ends,
reference_lengths: fiber.msp.reference_lengths,
starts: fiber.msp.starts.clone(),
ends: fiber.msp.ends.clone(),
lengths: fiber.msp.lengths.clone(),
reference_starts: fiber.msp.reference_starts.clone(),
reference_ends: fiber.msp.reference_ends.clone(),
reference_lengths: fiber.msp.reference_lengths.clone(),
};

let aligned_block_pairs = fiber.record.aligned_block_pairs().collect();
Expand All @@ -168,10 +178,39 @@ impl Fiberdata {
nuc,
aligned_block_pairs,
offset,
fiber,
}
}
}

#[pyclass]
pub struct Fiberwriter {
writer: bam::Writer,
}

#[pymethods]
impl Fiberwriter {
#[new]
pub fn new(output_bam_path: &str, template_bam_path: &str) -> Self {
let header = bam::Header::from_template(
bam::Reader::from_path(template_bam_path)
.expect("unable to open template bam file")
.header(),
);
let writer = bam::Writer::from_path(output_bam_path, &header, bam::Format::Bam)
.expect("unable to open output bam file");
Self { writer }
}
/// Write a Fiberdata object to a bam file.
#[pyo3(signature = (fiberdata))]
pub fn write(&mut self, fiberdata: &Fiberdata) -> PyResult<()> {
self.writer
.write(&fiberdata.fiber.record)
.expect("unable to write record");
pyo3::prelude::PyResult::Ok(())
}
}

#[pyclass]
/// Open a fiberseq bam file. Must have an index.
pub struct Fiberbam {
Expand Down
1 change: 1 addition & 0 deletions py-ft/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ fn pyft(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_class::<fiberdata::Fiberdata>()?;
m.add_class::<fiberdata::Basemods>()?;
m.add_class::<fiberdata::Ranges>()?;
m.add_class::<fiberdata::Fiberwriter>()?;
Ok(())
}

0 comments on commit 3bb2f45

Please sign in to comment.