exon 0.32.4

A platform for scientific data processing and analysis.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
// Copyright 2024 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::{any::Any, sync::Arc};

use arrow::datatypes::{Field, SchemaRef};
use async_trait::async_trait;
use datafusion::{
    catalog::Session,
    datasource::{
        file_format::file_compression_type::FileCompressionType, listing::ListingTableUrl,
        physical_plan::FileScanConfig, TableProvider,
    },
    error::{DataFusionError, Result},
    logical_expr::{TableProviderFilterPushDown, TableType},
    physical_plan::ExecutionPlan,
    prelude::Expr,
};
use exon_common::TableSchema;
use futures::{StreamExt, TryStreamExt};
use noodles::{bgzf, core::Region, vcf};
use object_store::{ObjectMeta, ObjectStore};
use tokio_util::io::StreamReader;

use crate::{
    datasources::{
        exon_listing_table_options::{
            ExonIndexedListingOptions, ExonListingConfig, ExonListingOptions,
        },
        hive_partition::filter_matches_partition_cols,
        indexed_file::indexed_bgzf_file::{
            augment_partitioned_file_with_byte_range, IndexedBGZFFile,
        },
        ExonFileType,
    },
    error::Result as ExonResult,
    physical_plan::{
        file_scan_config_builder::FileScanConfigBuilder, infer_region,
        object_store::pruned_partition_list,
    },
};

use super::{indexed_scanner::IndexedVCFScanner, VCFScan, VCFSchemaBuilder};

#[derive(Debug, Clone)]
/// Options specific to the VCF file format
pub struct ListingVCFTableOptions {
    /// The extension of the files to read
    file_extension: String,

    /// True if the file must be indexed
    indexed: bool,

    /// A region to filter the records
    regions: Vec<Region>,

    /// The file compression type
    file_compression_type: FileCompressionType,

    /// A list of table partition columns
    table_partition_cols: Vec<Field>,

    /// Whether to parse the INFO field
    parse_info: bool,

    /// Whether to parse the FORMAT field
    parse_formats: bool,
}

impl Default for ListingVCFTableOptions {
    fn default() -> Self {
        Self {
            file_extension: ExonFileType::VCF.get_file_extension(FileCompressionType::UNCOMPRESSED),
            indexed: false,
            regions: Vec::new(),
            file_compression_type: FileCompressionType::UNCOMPRESSED,
            table_partition_cols: Vec::new(),
            parse_info: false,
            parse_formats: false,
        }
    }
}

#[async_trait]
impl ExonListingOptions for ListingVCFTableOptions {
    fn table_partition_cols(&self) -> &[Field] {
        &self.table_partition_cols
    }

    fn file_extension(&self) -> &str {
        &self.file_extension
    }

    fn file_compression_type(&self) -> FileCompressionType {
        self.file_compression_type
    }

    async fn create_physical_plan(
        &self,
        conf: FileScanConfig,
    ) -> datafusion::error::Result<Arc<dyn ExecutionPlan>> {
        let scan = VCFScan::new(conf, self.file_compression_type)?;

        Ok(Arc::new(scan))
    }
}

#[async_trait]
impl ExonIndexedListingOptions for ListingVCFTableOptions {
    fn indexed(&self) -> bool {
        self.indexed
    }

    fn regions(&self) -> &[Region] {
        &self.regions
    }

    async fn create_physical_plan_with_regions(
        &self,
        conf: FileScanConfig,
        region: Vec<Region>,
    ) -> datafusion::error::Result<Arc<dyn ExecutionPlan>> {
        let scan = IndexedVCFScanner::new(conf, Arc::new(region[0].clone()))?;

        Ok(Arc::new(scan))
    }
}

impl ListingVCFTableOptions {
    /// Create a new set of options
    pub fn new(file_compression_type: FileCompressionType, indexed: bool) -> Self {
        let file_extension = ExonFileType::VCF.get_file_extension(file_compression_type);

        Self {
            file_extension,
            file_compression_type,
            indexed,
            table_partition_cols: Vec::new(),
            regions: Vec::new(),
            parse_info: false,
            parse_formats: false,
        }
    }

    /// Set the region
    pub fn with_regions(self, regions: Vec<Region>) -> Self {
        Self {
            regions,
            indexed: true,
            ..self
        }
    }

    /// Set the file extension
    pub fn with_file_extension(self, file_extension: String) -> Self {
        Self {
            file_extension,
            ..self
        }
    }

    /// Set the table partition columns
    pub fn with_table_partition_cols(self, table_partition_cols: Vec<Field>) -> Self {
        Self {
            table_partition_cols,
            ..self
        }
    }

    /// Set the parse info field
    pub fn with_parse_info(self, parse_info: bool) -> Self {
        Self { parse_info, ..self }
    }

    /// Set the parse formats field
    pub fn with_parse_formats(self, parse_formats: bool) -> Self {
        Self {
            parse_formats,
            ..self
        }
    }

    async fn infer_schema_from_object_meta(
        &self,
        store: &Arc<dyn ObjectStore>,
        objects: &[ObjectMeta],
    ) -> datafusion::error::Result<TableSchema> {
        if objects.is_empty() {
            return Err(DataFusionError::Execution(
                "No objects found in the table path".to_string(),
            ));
        }

        let get_result = store.get(&objects[0].location).await?;

        let stream_reader = Box::pin(get_result.into_stream().map_err(DataFusionError::from));
        let stream_reader = StreamReader::new(stream_reader);

        let mut builder = VCFSchemaBuilder::default()
            .with_parse_info(self.parse_info)
            .with_parse_formats(self.parse_formats)
            .with_partition_fields(self.table_partition_cols.clone());

        let header = match self.file_compression_type {
            FileCompressionType::GZIP => {
                let bgzf_reader = bgzf::AsyncReader::new(stream_reader);
                let mut vcf_reader = vcf::AsyncReader::new(bgzf_reader);

                vcf_reader.read_header().await?
            }
            FileCompressionType::UNCOMPRESSED => {
                let mut vcf_reader = vcf::AsyncReader::new(stream_reader);
                vcf_reader.read_header().await?
            }
            _ => {
                return Err(DataFusionError::Execution(
                    "Unsupported file compression type".to_string(),
                ))
            }
        };

        builder = builder.with_header(header);

        let table_schema = builder.build()?;

        Ok(table_schema)
    }

    /// Infer the schema of the files in the table
    pub async fn infer_schema<'a>(
        &'a self,
        state: &dyn Session,
        table_path: &'a ListingTableUrl,
    ) -> Result<TableSchema> {
        let store = state.runtime_env().object_store(table_path)?;

        let files = exon_common::object_store_files_from_table_path(
            &store,
            table_path.as_ref(),
            table_path.prefix(),
            self.file_extension.as_str(),
            None,
        )
        .await;

        // collect the files as a slice
        let files = files
            .try_collect::<Vec<_>>()
            .await
            .map_err(|e| DataFusionError::Execution(format!("Unable to get path info: {}", e)))?;

        self.infer_schema_from_object_meta(&store, &files).await
    }
}

#[derive(Debug, Clone)]
/// A VCF listing table
pub struct ListingVCFTable<T> {
    table_schema: TableSchema,

    config: ExonListingConfig<T>,
}

impl<T> ListingVCFTable<T> {
    /// Create a new VCF listing table
    pub fn new(config: ExonListingConfig<T>, table_schema: TableSchema) -> Self {
        Self {
            table_schema,
            config,
        }
    }
}

#[async_trait]
impl<T: ExonIndexedListingOptions + 'static> TableProvider for ListingVCFTable<T> {
    fn as_any(&self) -> &dyn Any {
        self
    }

    fn schema(&self) -> SchemaRef {
        Arc::clone(&self.table_schema.table_schema())
    }

    fn table_type(&self) -> TableType {
        TableType::Base
    }

    fn supports_filters_pushdown(
        &self,
        filters: &[&Expr],
    ) -> Result<Vec<TableProviderFilterPushDown>> {
        tracing::trace!(
            "vcf table provider supports_filters_pushdown: {:?}",
            filters
        );

        Ok(filters
            .iter()
            .map(|f| {
                if let Expr::ScalarFunction(s) = f {
                    if s.name() == "vcf_region_filter" && (s.args.len() == 2 || s.args.len() == 3) {
                        return TableProviderFilterPushDown::Exact;
                    }
                }

                filter_matches_partition_cols(f, self.config.options.table_partition_cols())
            })
            .collect())
    }

    async fn scan(
        &self,
        state: &dyn Session,
        projection: Option<&Vec<usize>>,
        filters: &[Expr],
        limit: Option<usize>,
    ) -> Result<Arc<dyn ExecutionPlan>> {
        let url = self
            .config
            .inner
            .table_paths
            .first()
            .ok_or(DataFusionError::Execution(
                "No table paths found in the configuration".to_string(),
            ))?;

        let object_store = state.runtime_env().object_store(url.object_store())?;

        let mut regions = filters
            .iter()
            .map(|f| {
                if let Expr::ScalarFunction(s) = f {
                    let r = infer_region::infer_region_from_udf(s, "vcf_region_filter")?;
                    Ok(r)
                } else {
                    Ok(None)
                }
            })
            .collect::<ExonResult<Vec<_>>>()?
            .into_iter()
            .flatten()
            .collect::<Vec<_>>();

        // add the regions from the configuration
        let config_regions = self.config.options.regions().to_vec();
        regions.extend(config_regions);

        if regions.len() > 1 {
            return Err(DataFusionError::NotImplemented(
                "Multiple regions are not supported yet".to_string(),
            ));
        }

        if regions.is_empty() && self.config.options.indexed() {
            return Err(DataFusionError::Plan(
                "INDEXED_VCF table requires a region filter. See the UDF 'vcf_region_filter'."
                    .to_string(),
            ));
        }

        if regions.is_empty() {
            let file_list = pruned_partition_list(
                &object_store,
                url,
                filters,
                self.config.options.file_extension(),
                self.config.options.table_partition_cols(),
            )
            .await?
            .try_collect::<Vec<_>>()
            .await?;

            let file_schema = self.table_schema.file_schema()?;
            let file_scan_config =
                FileScanConfigBuilder::new(url.object_store(), file_schema, vec![file_list])
                    .projection_option(projection.cloned())
                    .limit_option(limit)
                    .table_partition_cols(self.config.options.table_partition_cols().to_vec())
                    .build();

            let table = self
                .config
                .options
                .create_physical_plan(file_scan_config)
                .await?;

            return Ok(table);
        }

        let mut file_list = pruned_partition_list(
            &object_store,
            self.config.inner.table_paths.first().unwrap(),
            filters,
            self.config.options.file_extension(),
            self.config.options.table_partition_cols(),
        )
        .await?;

        let mut file_partitions = Vec::new();

        while let Some(f) = file_list.next().await {
            let f = f?;

            for region in &regions {
                let file_byte_range = augment_partitioned_file_with_byte_range(
                    Arc::clone(&object_store),
                    &f,
                    region,
                    &IndexedBGZFFile::Vcf,
                )
                .await?;

                file_partitions.extend(file_byte_range);
            }
        }

        let file_schema = self.table_schema.file_schema()?;
        let file_scan_config =
            FileScanConfigBuilder::new(url.object_store(), file_schema, vec![file_partitions])
                .projection_option(projection.cloned())
                .limit_option(limit)
                .table_partition_cols(self.config.options.table_partition_cols().to_vec())
                .build();

        let table = self
            .config
            .options
            .create_physical_plan_with_regions(file_scan_config, regions.to_vec())
            .await?;

        return Ok(table);
    }
}

#[cfg(test)]
mod tests {
    use std::sync::Arc;

    use crate::{datasources::vcf::IndexedVCFScanner, ExonSession};

    use arrow::datatypes::{DataType, Field, Fields};
    use datafusion::physical_plan::{
        coalesce_partitions::CoalescePartitionsExec, filter::FilterExec,
    };
    use exon_test::test_path;

    #[cfg(feature = "fixtures")]
    #[tokio::test]
    async fn test_chr17_queries() -> Result<(), Box<dyn std::error::Error>> {
        use crate::{tests::test_fixture_table_url, ExonSession};

        let path = test_fixture_table_url("chr17/")?;

        let ctx = ExonSession::new_exon()?;
        ctx.session
            .sql(
                format!(
                    "CREATE EXTERNAL TABLE vcf_file STORED AS VCF LOCATION '{}';",
                    path.to_string().as_str()
                )
                .as_str(),
            )
            .await?;

        let sql = "SELECT chrom, pos FROM vcf_file LIMIT 5;";
        let df = ctx.session.sql(sql).await?;

        // Get the first batch
        let mut batches = df.collect().await?;
        let batch = batches.remove(0);

        assert_eq!(batch.num_rows(), 5);
        assert_eq!(batch.num_columns(), 2);

        Ok(())
    }

    #[tokio::test]
    #[cfg(feature = "fixtures")]
    async fn test_chr17_positions() -> Result<(), Box<dyn std::error::Error>> {
        use crate::{tests::test_fixture_table_url, ExonSession};

        let path = test_fixture_table_url(
            "chr17/ALL.chr17.integrated_phase1_v3.20101123.snps_indels_svs.genotypes.vcf.gz",
        )?;

        let ctx = ExonSession::new_exon()?;

        ctx.session.sql(
            format!(
                "CREATE EXTERNAL TABLE vcf_file STORED AS INDEXED_VCF LOCATION '{}' OPTIONS (compression gzip);",
                path.to_string().as_str()
            )
            .as_str(),
        )
        .await?;

        let sql_commands = vec![
            "SELECT chrom, pos FROM vcf_file WHERE vcf_region_filter('17:1-1000', chrom, pos);",
            "SELECT chrom, pos FROM vcf_file WHERE vcf_region_filter('17:1000-1000000', chrom, pos);",
            "SELECT chrom, pos FROM vcf_file WHERE vcf_region_filter('17:1234-1424000', chrom, pos);",
            "SELECT chrom, pos FROM vcf_file WHERE vcf_region_filter('17:1000000-1424000', chrom, pos);",
        ];

        for sql in sql_commands {
            let df = ctx.session.sql(sql).await?;

            // Get the first batch
            let mut batches = df.collect().await?;
            let batch = batches.remove(0);

            assert!(batch.num_rows() > 0);
            assert_eq!(batch.num_columns(), 2);
        }

        Ok(())
    }

    #[cfg(feature = "fixtures")]
    #[tokio::test]
    async fn test_region_query_with_additional_predicates() -> Result<(), Box<dyn std::error::Error>>
    {
        use crate::ExonSession;

        let path = crate::tests::test_fixture_table_url(
            "chr17/ALL.chr17.integrated_phase1_v3.20101123.snps_indels_svs.genotypes.vcf.gz",
        )?;

        let ctx = ExonSession::new_exon()?;
        ctx.session.sql(
            format!(
                "CREATE EXTERNAL TABLE vcf_file STORED AS INDEXED_VCF LOCATION '{}' OPTIONS (compression gzip);",
                path.to_string().as_str()
            )
            .as_str(),
        )
        .await?;

        let sql =
            "SELECT chrom FROM vcf_file WHERE vcf_region_filter('17:1000-1000000', chrom, pos) AND qual != 100;";
        let df = ctx.session.sql(sql).await?;

        let cnt_where_qual_neq_100 = df.count().await?;
        assert!(cnt_where_qual_neq_100 > 0);

        let cnt_total = ctx
            .session
            .sql(
                "SELECT chrom FROM vcf_file WHERE vcf_region_filter('17:1000-1000000', chrom, pos)",
            )
            .await?
            .count()
            .await?;

        assert!(cnt_where_qual_neq_100 < cnt_total);

        Ok(())
    }

    #[tokio::test]
    async fn test_region_pushdown() -> Result<(), Box<dyn std::error::Error>> {
        let ctx = ExonSession::new_exon()?;
        let table_path = test_path("vcf", "index.vcf.gz");
        let table_path = table_path.to_str().ok_or("Invalid path")?;

        let sql = format!(
            "CREATE EXTERNAL TABLE vcf_file STORED AS VCF LOCATION '{}' OPTIONS (compression gzip);",
            table_path
        );
        ctx.session.sql(&sql).await?;

        let sql_statements = vec![
            "SELECT * FROM vcf_file WHERE vcf_region_filter('1:9999921', chrom, pos);",
            "SELECT * FROM vcf_file WHERE vcf_region_filter('1:9999921-9999922', chrom, pos);",
            "SELECT * FROM vcf_file WHERE vcf_region_filter('1', chrom);",
        ];

        for sql_statement in sql_statements {
            let df = ctx.session.sql(sql_statement).await?;

            let physical_plan = ctx
                .session
                .state()
                .create_physical_plan(df.logical_plan())
                .await?;

            if let Some(scan) = physical_plan.as_any().downcast_ref::<FilterExec>() {
                let scan = scan
                    .input()
                    .as_any()
                    .downcast_ref::<CoalescePartitionsExec>()
                    .ok_or("Invalid partition")?;

                let scan = scan.input().as_any().downcast_ref::<IndexedVCFScanner>();
                assert!(scan.is_some());
            }
        }

        Ok(())
    }

    #[tokio::test]
    async fn test_vcf_parsing_string() -> Result<(), Box<dyn std::error::Error>> {
        let ctx = ExonSession::new_exon()?;

        let table_path = test_path("vcf", "index.vcf");

        let sql = "SET exon.vcf_parse_info = true;";
        ctx.session.sql(sql).await?;

        let sql = "SET exon.vcf_parse_formats = true;";
        ctx.session.sql(sql).await?;

        let sql = format!(
            "CREATE EXTERNAL TABLE vcf_file STORED AS VCF LOCATION '{}';",
            table_path.to_str().ok_or("Invalid path")?
        );
        ctx.session.sql(&sql).await?;

        let sql = "SELECT * FROM vcf_file WHERE chrom = '1' AND pos = 100000;";
        let df = ctx.session.sql(sql).await?;

        // Check that the last two columns are strings.
        let schema = df.schema();

        let infos_fields = Fields::from(vec![
            Field::new("INDEL", DataType::Boolean, true),
            Field::new("IDV", DataType::Int32, true),
            Field::new("IMF", DataType::Float32, true),
            Field::new("DP", DataType::Int32, true),
            Field::new("VDB", DataType::Float32, true),
            Field::new("RPB", DataType::Float32, true),
            Field::new("MQB", DataType::Float32, true),
            Field::new("BQB", DataType::Float32, true),
            Field::new("MQSB", DataType::Float32, true),
            Field::new("SGB", DataType::Float32, true),
            Field::new("MQ0F", DataType::Float32, true),
            Field::new(
                "I16",
                DataType::List(Arc::new(Field::new("item", DataType::Float32, true))),
                true,
            ),
            Field::new(
                "QS",
                DataType::List(Arc::new(Field::new("item", DataType::Float32, true))),
                true,
            ),
        ]);
        assert_eq!(schema.field(7).data_type(), &DataType::Struct(infos_fields));

        let inner_item_fields = vec![
            Field::new("GT", DataType::Utf8, true),
            Field::new(
                "PL",
                DataType::List(Arc::new(Field::new("item", DataType::Int32, true))),
                true,
            ),
            Field::new("PG", DataType::Int32, true),
        ];

        let inner_struct = DataType::Struct(Fields::from(inner_item_fields));
        let inner_list = DataType::List(Arc::new(Field::new("item", inner_struct, true)));
        assert_eq!(schema.field(8).data_type(), &inner_list);

        Ok(())
    }
}