genomicframe-core 0.2.0

High-performance genomics I/O and interoperability layer
Documentation
use crate::error::{Error, Result};
use crate::expression::{extract_f64, extract_usize, ExprToFilter};
use crate::expression::{CompiledAndFilter, CompiledNotFilter, CompiledOrFilter, Expr};
use crate::filters::RecordFilter;
use crate::formats::fastq::filters::*;
use crate::formats::fastq::FastqRecord;

impl ExprToFilter<FastqRecord> for Expr {
    fn compile(&self) -> Result<Box<dyn RecordFilter<FastqRecord>>> {
        match self {
            // Quality filter
            Expr::Gt(left, right) | Expr::Gte(left, right) if matches!(**left, Expr::Column(ref name) if name == "quality" || name == "qual") =>
            {
                let min_mean_qual = extract_f64(right)?;
                Ok(Box::new(QualityFilter { min_mean_qual }))
            }

            // Length filter
            Expr::Gt(left, right) | Expr::Gte(left, right) if matches!(**left, Expr::Column(ref name) if name == "length") =>
            {
                let min_length = extract_usize(right)?;
                Ok(Box::new(MinLengthFilter { min_length }))
            }

            Expr::Lt(left, right) | Expr::Lte(left, right) if matches!(**left, Expr::Column(ref name) if name == "length") =>
            {
                let max_length = extract_usize(right)?;
                Ok(Box::new(LengthFilter {
                    min_length: 0,
                    max_length,
                }))
            }

            // GC content filter
            Expr::Gt(left, right) | Expr::Gte(left, right) if matches!(**left, Expr::Column(ref name) if name == "gc_content") =>
            {
                let min_gc = extract_f64(right)?;
                Ok(Box::new(GCContentFilter {
                    min_gc,
                    max_gc: 1.0,
                }))
            }

            // Boolean logic
            Expr::And(exprs) => {
                if exprs.is_empty() {
                    return Err(Error::invalid_input("Empty AND expression"));
                }

                let mut result = exprs[0].compile()?;
                for expr in &exprs[1..] {
                    let next = expr.compile()?;
                    result = Box::new(CompiledAndFilter {
                        left: result,
                        right: next,
                    });
                }
                Ok(result)
            }

            Expr::Or(exprs) => {
                if exprs.is_empty() {
                    return Err(Error::invalid_input("Empty OR expression"));
                }

                let mut result = exprs[0].compile()?;
                for expr in &exprs[1..] {
                    let next = expr.compile()?;
                    result = Box::new(CompiledOrFilter {
                        left: result,
                        right: next,
                    });
                }
                Ok(result)
            }

            Expr::Not(expr) => {
                let inner = expr.compile()?;
                Ok(Box::new(CompiledNotFilter { inner }))
            }

            _ => Err(Error::invalid_input(format!(
                "Expression not supported for FASTQ: {}",
                self
            ))),
        }
    }
}