From 8143bb47985f009c4b2c8fd6876aa5c7e39a232d Mon Sep 17 00:00:00 2001 From: Benjamin Demaille Date: Thu, 13 Aug 2026 09:24:02 +0200 Subject: [PATCH] fix: fail with a GTF-specific error when no genes can be extracted A malformed or truncated GTF that yields zero genes used to log a WARN and carry on, so the run died later in count_reads with a "Chromosome name mismatch" error, an empty "GTF chromosomes (first 5):" list and a chromosome_mapping suggestion with nothing in it. The real cause (bad annotation file) was buried in an easy-to-miss warning above. parse_gtf now tracks why nothing was extracted and returns a fatal, actionable error naming the most likely cause: no annotation lines, lines with fewer than the 9 mandatory columns, no exon features (listing the feature types actually present, which flags GFF3 input), or exon features without gene_id attributes. count_reads keeps a guard for library callers: an empty annotation index now reports an empty-annotation error instead of the chromosome-mismatch diagnosis, which cannot apply when there is nothing to match against. Closes #126 Co-Authored-By: Claude Opus 5 (1M context) --- src/gtf.rs | 174 +++++++++++++++++++++++++++++++++-- src/rna/dupradar/counting.rs | 12 +++ 2 files changed, 178 insertions(+), 8 deletions(-) diff --git a/src/gtf.rs b/src/gtf.rs index fa209a35..67daffa3 100644 --- a/src/gtf.rs +++ b/src/gtf.rs @@ -8,7 +8,7 @@ use anyhow::{Context, Result}; use indexmap::IndexMap; -use std::collections::HashMap; +use std::collections::{BTreeSet, HashMap}; use std::io::BufRead; /// Represents a single exon interval. @@ -157,6 +157,80 @@ struct TranscriptBuilder { stop_codons: Vec<(u64, u64)>, } +/// Counters collected while scanning a GTF, used to explain an empty parse. +#[derive(Debug, Default)] +struct GtfParseDiagnostics { + /// Non-comment, non-empty lines seen. + data_lines: u64, + /// Lines with fewer than the 9 mandatory tab-separated columns. + short_lines: u64, + /// Well-formed `exon` lines seen (before the gene_id check). + exon_lines: u64, + /// Feature lines of a type we consume that carried no `gene_id` attribute. + missing_gene_id: u64, + /// Distinct values seen in column 3 (the feature-type column). + feature_types: BTreeSet, +} + +/// Build the fatal error raised when a GTF yields no genes. +/// +/// The message names the most likely root cause based on what the scan saw, +/// so the failure is actionable instead of surfacing later as an unrelated +/// "chromosome name mismatch". +fn empty_gtf_error(path: &str, diag: &GtfParseDiagnostics) -> anyhow::Error { + let cause = if diag.data_lines == 0 { + "the file contains no annotation lines (it is empty, or every line is a comment)" + .to_string() + } else if diag.short_lines == diag.data_lines { + format!( + "all {} annotation lines have fewer than the 9 mandatory tab-separated GTF columns \ + (truncated file, or space- instead of tab-separated)", + diag.data_lines + ) + } else if diag.exon_lines == 0 { + let types: Vec<&str> = diag + .feature_types + .iter() + .take(8) + .map(|s| s.as_str()) + .collect(); + format!( + "no 'exon' features were found; feature types present: {}{}. \ + GFF3 files (which use 'ID='/'Parent=' attributes) are not supported — convert to GTF", + if types.is_empty() { + "none".to_string() + } else { + types.join(", ") + }, + if diag.feature_types.len() > 8 { + ", ..." + } else { + "" + } + ) + } else if diag.missing_gene_id > 0 { + format!( + "{} feature lines were found but none carried a 'gene_id' attribute \ + (GFF3 files use 'ID='/'Parent=' instead — convert to GTF)", + diag.missing_gene_id + ) + } else { + "the file has exon features with gene_id attributes, but none could be assembled into a \ + gene" + .to_string() + }; + + anyhow::anyhow!( + "No genes could be extracted from GTF file '{}': {}.\n\ + \n\ + Every RustQC analysis needs a usable gene annotation, so processing cannot continue.\n\ + Check that the file is uncorrupted, tab-separated GTF (not GFF3) with 'exon' features \ + carrying 'gene_id' attributes.", + path, + cause + ) +} + /// Parse a GTF file and return a map of gene_id -> Gene. /// /// Extracts all exon and CDS features, groups them by gene_id, and builds @@ -180,6 +254,10 @@ pub fn parse_gtf(path: &str, extra_attributes: &[String]) -> Result = IndexMap::new(); + // Diagnostics, used to explain *why* nothing was extracted if the file + // turns out to yield no genes (see `empty_gtf_error`). + let mut diag = GtfParseDiagnostics::default(); + for line in reader.lines() { let line = line.context("Failed to read line from GTF file")?; @@ -187,13 +265,19 @@ pub fn parse_gtf(path: &str, extra_attributes: &[String]) -> Result = line.split('\t').collect(); if fields.len() < 9 { + diag.short_lines += 1; continue; } let feature_type = fields[2]; + if feature_type == "exon" { + diag.exon_lines += 1; + } + diag.feature_types.insert(feature_type.to_string()); // We care about exon, CDS, start_codon, and stop_codon features if feature_type != "exon" @@ -224,7 +308,11 @@ pub fn parse_gtf(path: &str, extra_attributes: &[String]) -> Result id, - None => continue, // Skip features without gene_id + None => { + // Skip features without gene_id (GFF3 uses ID=/Parent= instead) + diag.missing_gene_id += 1; + continue; + } }; // For exon features, update the gene-level data (unchanged from original) @@ -290,13 +378,11 @@ pub fn parse_gtf(path: &str, extra_attributes: &[String]) -> Result 0 && genes_with_reads == 0 { let bam_chroms: Vec<&str> = tid_to_name.iter().take(5).map(|s| s.as_str()).collect(); let gtf_chroms: Vec<&str> = index.keys().take(5).map(|s| s.as_str()).collect(); + // An empty annotation index is not a naming mismatch — there is nothing + // on the GTF side to match against. Say so instead of suggesting a + // chromosome_mapping that cannot possibly help. + anyhow::ensure!( + !gtf_chroms.is_empty(), + "No reads could be assigned to any gene because the annotation is empty: \ + the parsed GTF contains no genes on any chromosome.\n\ + \n\ + This is an annotation problem, not a chromosome naming mismatch. Check that the \ + GTF file is uncorrupted, tab-separated GTF (not GFF3) with 'exon' features carrying \ + 'gene_id' attributes." + ); anyhow::bail!( "Chromosome name mismatch: no reads could be assigned to any gene.\n\ \n\