use super::framing::write_vg_group;
use super::orientations_to_vg_edge;
use crate::core::core_types::{Edge, Node, NodeId, Path};
use crate::core::graph_dto::CoreGraphDTO;
use crate::error::PanResult;
use crate::formats::common::id_map::{IdMap, parse_canonical_int};
use crate::proto_gen::vg as vg_proto;
use log::debug;
use prost::Message;
use rayon::prelude::*;
use std::io::Write;
pub(super) const VG_CHUNK_SIZE: usize = 1000;
pub(super) const VG_GROUP_SIZE: usize = 1000;
pub(super) enum VgChunk<'a> {
Nodes {
first: NodeId,
nodes: &'a [Node],
},
Edges(&'a [Edge]),
Path {
path: &'a Path,
start: usize,
end: usize,
},
}
pub(super) fn vg_chunks(graph: &CoreGraphDTO) -> Vec<VgChunk<'_>> {
let mut chunks: Vec<VgChunk<'_>> = graph
.nodes()
.as_slice()
.chunks(VG_CHUNK_SIZE)
.enumerate()
.map(|(i, nodes)| VgChunk::Nodes {
first: i * VG_CHUNK_SIZE,
nodes,
})
.collect();
chunks.extend(graph.edges().chunks(VG_CHUNK_SIZE).map(VgChunk::Edges));
for path in graph.paths() {
let len = path.steps.len();
let mut start = 0;
loop {
let end = (start + VG_CHUNK_SIZE).min(len);
chunks.push(VgChunk::Path { path, start, end });
start = end;
if start >= len {
break;
}
}
}
chunks
}
pub(super) struct VgWriteContext<'a> {
pub(super) graph: &'a CoreGraphDTO,
pub(super) vg_ids: Vec<i64>,
}
#[allow(
clippy::cast_possible_truncation,
clippy::cast_possible_wrap,
clippy::cast_sign_loss
)]
impl<'a> VgWriteContext<'a> {
pub(super) fn new(graph: &'a CoreGraphDTO) -> Self {
let n = graph.nodes().len();
let named = graph.node_name_map().as_ref().and_then(|map| {
let ids: Option<Vec<i64>> = (0..n)
.into_par_iter()
.map(|id| parse_canonical_int(map.get(&id)?).filter(|&vg_id| vg_id > 0))
.collect();
ids.filter(|ids| IdMap::from_ids(ids).is_ok())
});
let vg_ids = named.unwrap_or_else(|| {
debug!("Node names are not distinct positive integers; VG ids are 1..={n}");
(1..=n as i64).collect()
});
Self { graph, vg_ids }
}
fn node(&self, id: NodeId, node: &Node) -> vg_proto::Node {
let vg_id = self.vg_ids[id];
let name = self
.graph
.node_name_map()
.as_ref()
.and_then(|m| m.get(&id))
.filter(|name| name.as_slice() != itoa::Buffer::new().format(vg_id).as_bytes())
.map_or_else(String::new, |name| {
String::from_utf8_lossy(name).into_owned()
});
vg_proto::Node {
sequence: String::from_utf8_lossy(&node.sequence).into_owned(),
name,
id: vg_id,
}
}
fn edge(&self, edge: &Edge) -> vg_proto::Edge {
let (from_start, to_end) = orientations_to_vg_edge(edge.from_orient, edge.to_orient);
vg_proto::Edge {
from: self.vg_ids[edge.from_node],
to: self.vg_ids[edge.to_node],
from_start,
to_end,
overlap: edge.overlap as i32,
}
}
fn path(&self, path: &Path, start: usize, end: usize) -> vg_proto::Path {
let mapping = path.steps[start..end]
.iter()
.zip(start..)
.map(|(step, index)| {
let node_seq_len = self
.graph
.nodes()
.get(step.node_id())
.map_or(0, |n| n.sequence.len()) as i32;
vg_proto::Mapping {
position: Some(vg_proto::Position {
node_id: self.vg_ids[step.node_id()],
offset: 0,
is_reverse: step.is_reverse(),
name: String::new(),
}),
edit: vec![vg_proto::Edit {
from_length: node_seq_len,
to_length: node_seq_len,
sequence: String::new(),
}],
rank: (index + 1) as i64,
}
})
.collect();
vg_proto::Path {
name: String::from_utf8_lossy(&path.name).into_owned(),
mapping,
is_circular: false,
length: 0,
}
}
fn encode(&self, chunk: &VgChunk<'_>) -> Vec<u8> {
let graph = match *chunk {
VgChunk::Nodes { first, nodes } => vg_proto::Graph {
node: nodes
.iter()
.zip(first..)
.map(|(n, id)| self.node(id, n))
.collect(),
..Default::default()
},
VgChunk::Edges(edges) => vg_proto::Graph {
edge: edges.iter().map(|e| self.edge(e)).collect(),
..Default::default()
},
VgChunk::Path { path, start, end } => vg_proto::Graph {
path: vec![self.path(path, start, end)],
..Default::default()
},
};
graph.encode_to_vec()
}
}
pub(super) fn write_vg(graph: &CoreGraphDTO, writer: &mut dyn Write) -> PanResult<()> {
let context = VgWriteContext::new(graph);
let chunks = vg_chunks(graph);
if chunks.is_empty() {
return write_vg_group(writer, &[vg_proto::Graph::default().encode_to_vec()]);
}
for group in chunks.chunks(VG_GROUP_SIZE) {
let encoded: Vec<Vec<u8>> = group.par_iter().map(|c| context.encode(c)).collect();
write_vg_group(writer, &encoded)?;
}
Ok(())
}