Skip to content

Commit 4db102e

Browse files
committed
Basic Handle
1 parent 9adcdd8 commit 4db102e

File tree

3 files changed

+99
-11
lines changed

3 files changed

+99
-11
lines changed

flatgfa-py/example.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import flatgfa
22
from collections import Counter
33

4-
graph = flatgfa.parse("../tests/k.gfa")
5-
depths = Counter()
6-
for path in graph.paths:
7-
for step in path:
8-
depths[step.segment.id] += 1
4+
graph = flatgfa.load("chr6.flatgfa")
5+
print(graph.size)
6+
# graph.print_gaf_lookup("ch6.gaf")
97

10-
print("#node.id\tdepth")
11-
for seg in graph.segments:
12-
print("{}\t{}".format(seg.name, depths[seg.id]))
8+
for read in graph.test_gaf("ch6.gaf"):
9+
for event in read:
10+
print(event.handle)
11+
print(event.range)
12+
print(event.get_seq(graph))

flatgfa-py/src/lib.rs

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use flatgfa::ops::gaf::ChunkEvent;
12
use flatgfa::pool::Id;
23
use flatgfa::{self, file, memfile, print, FlatGFA, HeapGFAStore};
34
use pyo3::exceptions::PyIndexError;
@@ -126,6 +127,93 @@ impl PyFlatGFA {
126127
mmap.flush()?;
127128
Ok(())
128129
}
130+
131+
#[getter]
132+
fn size(&self) -> usize {
133+
let gfa = self.0.view();
134+
file::size(&gfa)
135+
}
136+
137+
fn print_gaf_lookup(&self, gaf: &str) {
138+
let gfa = self.0.view();
139+
140+
let name_map = flatgfa::namemap::NameMap::build(&gfa);
141+
142+
let gaf_buf = flatgfa::memfile::map_file(&gaf);
143+
let parser = flatgfa::ops::gaf::GAFParser::new(&gaf_buf);
144+
145+
// Print the actual sequences for each chunk in the GAF.
146+
for read in parser {
147+
print!("{}\t", read.name);
148+
for event in flatgfa::ops::gaf::PathChunker::new(&gfa, &name_map, read) {
149+
event.print_seq(&gfa);
150+
}
151+
println!();
152+
}
153+
}
154+
155+
fn test_gaf(&self, gaf: &str) -> Vec<Vec<PyChunkEvent>> {
156+
let gfa = self.0.view();
157+
158+
let name_map = flatgfa::namemap::NameMap::build(&gfa);
159+
160+
let gaf_buf = flatgfa::memfile::map_file(&gaf);
161+
let parser = flatgfa::ops::gaf::GAFParser::new(&gaf_buf);
162+
163+
let r: Vec<Vec<PyChunkEvent>> = parser.into_iter().map(
164+
|x| flatgfa::ops::gaf::PathChunker::new(&gfa, &name_map, x)
165+
.into_iter()
166+
.map(|c| PyChunkEvent { chunk_event: c.into() })
167+
.collect()
168+
).collect();
169+
170+
r
171+
}
172+
}
173+
174+
#[pyclass(frozen)]
175+
#[pyo3(name = "ChunkEvent", module = "flatgfa")]
176+
struct PyChunkEvent {
177+
chunk_event: Arc<ChunkEvent>,
178+
}
179+
180+
#[pymethods]
181+
impl PyChunkEvent {
182+
fn __str__(&self) -> String {
183+
"".to_string()
184+
}
185+
186+
#[getter]
187+
fn handle(&self) -> String {
188+
let seg_num: u32 = self.chunk_event.handle.segment().into();
189+
format!("{}, {}", &self.chunk_event.handle.orient(), seg_num)
190+
}
191+
192+
#[getter]
193+
fn range(&self) -> String {
194+
match self.chunk_event.range {
195+
flatgfa::ops::gaf::ChunkRange::None => { "".to_string() },
196+
flatgfa::ops::gaf::ChunkRange::All => { "all".to_string() },
197+
flatgfa::ops::gaf::ChunkRange::Partial(start, end) => {
198+
format!("[{}, {})", start, end)
199+
}
200+
}
201+
}
202+
203+
fn get_seq(&self, gfa: &PyFlatGFA) -> String {
204+
let inner_gfa = gfa.0.view();
205+
let seq = inner_gfa.get_seq_oriented(self.chunk_event.handle);
206+
207+
match self.chunk_event.range {
208+
flatgfa::ops::gaf::ChunkRange::Partial(start, end) => {
209+
seq.slice(start..end).to_string()
210+
}
211+
flatgfa::ops::gaf::ChunkRange::All => {
212+
seq.to_string()
213+
}
214+
flatgfa::ops::gaf::ChunkRange::None => {"".to_string()}
215+
}
216+
}
129217
}
130218

131219
/// A reference to a list of *any* type within a FlatGFA.

flatgfa/src/ops/gaf.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,12 @@ impl<'a, 'b> PathChunker<'a, 'b> {
137137
#[derive(Debug)]
138138
pub struct ChunkEvent {
139139
index: usize,
140-
handle: flatgfa::Handle,
141-
range: ChunkRange,
140+
pub handle: flatgfa::Handle,
141+
pub range: ChunkRange,
142142
}
143143

144144
#[derive(Debug)]
145-
enum ChunkRange {
145+
pub enum ChunkRange {
146146
None,
147147
All,
148148
Partial(usize, usize),

0 commit comments

Comments
 (0)