use std::sync::Arc;
use arrow::datatypes::{DataType, Field, SchemaRef};
use exon_common::TableSchema;
use noodles::core::Region;
use object_store::ObjectStore;
#[derive(Debug)]
pub struct FASTAConfig {
pub batch_size: usize,
pub file_schema: SchemaRef,
pub object_store: Arc<dyn ObjectStore>,
pub projection: Option<Vec<usize>>,
pub fasta_sequence_buffer_capacity: usize,
pub use_large_utf8: bool,
pub region: Option<Region>,
pub region_file: Option<String>,
}
impl FASTAConfig {
pub fn new(object_store: Arc<dyn ObjectStore>, file_schema: SchemaRef) -> Self {
Self {
object_store,
file_schema,
batch_size: exon_common::DEFAULT_BATCH_SIZE,
projection: None,
fasta_sequence_buffer_capacity: 384,
use_large_utf8: false,
region: None,
region_file: None,
}
}
pub fn with_region(mut self, region: Region) -> Self {
self.region = Some(region);
self
}
pub fn with_region_file(mut self, region_file: String) -> Self {
self.region_file = Some(region_file);
self
}
pub fn with_batch_size(mut self, batch_size: usize) -> Self {
self.batch_size = batch_size;
self
}
pub fn with_projection(mut self, projection: Vec<usize>) -> Self {
let file_projection = projection
.iter()
.filter(|f| **f < self.file_schema.fields().len())
.cloned()
.collect::<Vec<_>>();
self.projection = Some(file_projection);
self
}
pub fn with_fasta_sequence_buffer_capacity(
mut self,
fasta_sequence_buffer_capacity: usize,
) -> Self {
self.fasta_sequence_buffer_capacity = fasta_sequence_buffer_capacity;
self
}
pub fn with_use_large_utf8(mut self, use_large_utf8: bool) -> Self {
self.use_large_utf8 = use_large_utf8;
self
}
}
pub struct FASTASchemaBuilder {
fields: Vec<Field>,
partition_fields: Vec<Field>,
large_utf8: bool,
}
impl Default for FASTASchemaBuilder {
fn default() -> Self {
Self {
fields: vec![
Field::new("id", DataType::Utf8, false),
Field::new("description", DataType::Utf8, true),
Field::new("sequence", DataType::Utf8, false),
],
partition_fields: vec![],
large_utf8: false,
}
}
}
impl FASTASchemaBuilder {
pub fn with_large_utf8(mut self, large_utf8: bool) -> Self {
self.large_utf8 = large_utf8;
self
}
pub fn with_partition_fields(mut self, partition_fields: Vec<Field>) -> Self {
self.partition_fields.extend(partition_fields);
self
}
pub fn build(&mut self) -> TableSchema {
if self.large_utf8 {
let field = Field::new("sequence", DataType::LargeUtf8, false);
self.fields[2] = field;
}
let file_field_projection = self
.fields
.iter()
.enumerate()
.map(|(i, _)| i)
.collect::<Vec<_>>();
self.fields.extend(self.partition_fields.clone());
let arrow_schema = Arc::new(arrow::datatypes::Schema::new(self.fields.clone()));
TableSchema::new(arrow_schema, file_field_projection)
}
}