Skip to content

Commit

Permalink
feat: add bed
Browse files Browse the repository at this point in the history
  • Loading branch information
tshauck committed Dec 14, 2023
1 parent 1957ef0 commit 0665d55
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
3 changes: 3 additions & 0 deletions exon/exon-core/src/datasources/bed/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,6 @@ pub mod table_provider;

pub use self::file_opener::BEDOpener;
pub use self::scanner::BEDScan;

mod udtf;
pub use self::udtf::BEDScanFunction;
48 changes: 48 additions & 0 deletions exon/exon-core/src/datasources/bed/udtf.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2023 WHERE TRUE Technologies.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::sync::Arc;

use crate::datasources::ScanFunction;
use datafusion::{
datasource::{function::TableFunctionImpl, TableProvider},
error::Result,
logical_expr::Expr,
};
use exon_bed::BEDSchemaBuilder;

use super::table_provider::{ListingBEDTable, ListingBEDTableConfig, ListingBEDTableOptions};

/// A table function that returns a table provider for a BED file.
#[derive(Debug, Default)]
pub struct BEDScanFunction {}

impl TableFunctionImpl for BEDScanFunction {
fn call(&self, exprs: &[Expr]) -> Result<Arc<dyn TableProvider>> {
let listing_scan_function = ScanFunction::try_from(exprs)?;

let schema = BEDSchemaBuilder::default().build();

let listing_table_options =
ListingBEDTableOptions::new(listing_scan_function.file_compression_type);

let listing_fasta_table_config =
ListingBEDTableConfig::new(listing_scan_function.listing_table_url)
.with_options(listing_table_options);

let listing_fasta_table = ListingBEDTable::try_new(listing_fasta_table_config, schema)?;

Ok(Arc::new(listing_fasta_table))
}
}
2 changes: 2 additions & 0 deletions exon/exon-core/src/session_context/exon_context_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use noodles::core::Region;
use crate::{
datasources::{
bcf::table_provider::{ListingBCFTable, ListingBCFTableConfig, ListingBCFTableOptions},
bed::BEDScanFunction,
fasta::FastaScanFunction,
fastq::FastqScanFunction,
gff::GFFScanFunction,
Expand Down Expand Up @@ -160,6 +161,7 @@ pub trait ExonSessionExt {
ctx.register_udtf("fastq_scan", Arc::new(FastqScanFunction::default()));
ctx.register_udtf("gff_scan", Arc::new(GFFScanFunction::default()));
ctx.register_udtf("gtf_scan", Arc::new(GTFScanFunction::default()));
ctx.register_udtf("bed_scan", Arc::new(BEDScanFunction::default()));

ctx
}
Expand Down
15 changes: 15 additions & 0 deletions exon/exon-core/tests/sqllogictests/slt/bed-select-tests.slt
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,18 @@ chr1 11874 12227 NR_046018_exon_0_0_chr1_11874_f NULL + NULL NULL NULL NULL NULL

statement ok
DROP TABLE bed;

query T
SELECT COUNT(*) FROM bed_scan('$CARGO_MANIFEST_DIR/test-data/datasources/bed/test.bed');
----
10

query T
SELECT COUNT(*) FROM bed_scan('$CARGO_MANIFEST_DIR/test-data/datasources/bed-partition/');
----
20

query T
SELECT COUNT(*) FROM bed_scan('$CARGO_MANIFEST_DIR/test-data/datasources/bed/test.bed.gz', 'gzip');
----
1

0 comments on commit 0665d55

Please sign in to comment.