Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions bindings/java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
VERSIONING: this binding shares the Rust ENGINE's version — the Cargo [workspace.package]
version at this repo's root (package gbif-name-parser) — released in lockstep with the
CLI/Python/R bindings, so one version across all bindings means one engine. It is NOT the
version of the api it implements: `org.gbif:name-parser-api` (6.0.1, a dependency
version of the api it implements: `org.gbif:name-parser-api` (6.0.2-SNAPSHOT, a dependency
below) is independently versioned and pinned. The bindings sit at 0.x while new / gathering
real-use feedback, and graduate to the product's 5.x line once stable. Keep <version> below in sync
with the Cargo workspace version — scripts/bump-version.sh does this; see RELEASE.md.
Expand Down Expand Up @@ -92,7 +92,7 @@
<dependency>
<groupId>org.gbif</groupId>
<artifactId>name-parser-api</artifactId>
<version>6.0.1</version>
<version>6.0.2-SNAPSHOT</version>
</dependency>
<!-- @Nullable only, not needed at runtime: the api declares it optional since 6.0 -->
<dependency>
Expand Down
43 changes: 16 additions & 27 deletions crates/nameparser/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ static AL: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"^al\.?$").unwrap());
/// 01_E13`), which rendered "Wolbachia sp. endosymbiont of …". And with the new-species markers
/// `n. sp.` and `nsp` (#139), the unnamed genus of `Carabidae gen. sp.` (#174), and the aggregate
/// marker of `Acalitus agg. brevitarsus/phyllereus`, which rendered "Acalitus agg. agg. …", and a
/// marker glued to its number, `Dynestinae sp2` (#242).
/// marker glued to its number, `Dynestinae sp2` (#242), and the dotless `n sp` (#246).
static PHRASE_SPECIES_MARKER: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(
r"(?i)^(?:(?:spp?|spec)\d|(?:species|spec|spp|sp|nsp|agg|n\.(?-u:\s*)sp|cf|aff|nr|near|[a-z]*(?:bacteri(?:um|a)|archae(?:on|a|ote)|symbionts?))(?-u:\b)|(?:n_)?sp(?:ec)?_|n_gen(?-u:\b)|gen(?:\.|(?-u:\s))).*$",
r"(?i)^(?:(?:spp?|spec)\d|(?:species|spec|spp|sp|nsp|agg|n\.?(?-u:\s*)sp\d*|cf|aff|nr|near|[a-z]*(?:bacteri(?:um|a)|archae(?:on|a|ote)|symbionts?))(?-u:\b)|(?:n_)?sp(?:ec)?_|n_gen(?-u:\b)|gen(?:\.|(?-u:\s))).*$",
)
.unwrap()
});
Expand Down Expand Up @@ -400,10 +400,7 @@ fn append_genus(
show_qualifier: bool,
html: bool,
) {
if show_qualifier && n.has_epithet_qualifier(NamePart::Generic) {
sb.push_str(qualifier(n, NamePart::Generic));
sb.push(' ');
}
append_qualifier(sb, n, NamePart::Generic, show_qualifier);
if hybrid_marker && n.graft_chimaera {
sb.push(GRAFT_MARKER);
sb.push(' ');
Expand All @@ -427,10 +424,7 @@ fn append_infraspecific(
html: bool,
) {
sb.push(' ');
if show_qualifier && n.has_epithet_qualifier(NamePart::Infraspecific) {
sb.push_str(qualifier(n, NamePart::Infraspecific));
sb.push(' ');
}
append_qualifier(sb, n, NamePart::Infraspecific, show_qualifier);
if hybrid_marker && has_notho(n, NamePart::Infraspecific) {
if rank_marker && is_infraspecific_marker(n.rank) {
sb.push_str("notho");
Expand Down Expand Up @@ -462,22 +456,20 @@ fn append_infraspecific(
}
}

/// A qualifier of `part` and a space, if shown and there is one: the `?` of a doubted subgenus,
/// `Glenea (? Macroglenea) anulicornis`.
/// A qualifier of `part` and a space, if shown and there is one: `Abies cf. alba`. The `?` of a
/// doubted part is left out, the doubtful flag and the qualifier itself carry it (#243):
/// `Metacnephia ? sommermanae` is `Metacnephia sommermanae`, `Glenea (? Macroglenea) anulicornis`
/// is `Glenea (Macroglenea) anulicornis`.
fn append_qualifier(sb: &mut String, n: &ParsedName, part: NamePart, show_qualifier: bool) {
if show_qualifier && n.has_epithet_qualifier(part) {
sb.push_str(qualifier(n, part));
sb.push(' ');
}
}

/// Fetch an epithet qualifier known to exist (caller guards with `has_epithet_qualifier`).
fn qualifier(n: &ParsedName, part: NamePart) -> &str {
n.epithet_qualifier
let shown = n
.epithet_qualifier
.as_ref()
.and_then(|m| m.get(&part))
.map(String::as_str)
.unwrap_or("")
.filter(|q| show_qualifier && q.as_str() != "?");
if let Some(q) = shown {
sb.push_str(q);
sb.push(' ');
}
}

/// Java `NameFormatter.joinAuthors(List<String>, Integer maxAuthors)`.
Expand Down Expand Up @@ -873,10 +865,7 @@ fn build_name(n: &ParsedName, f: &Flags) -> Option<String> {
} else {
// species part
sb.push(' ');
if f.show_qualifier && n.has_epithet_qualifier(NamePart::Specific) {
sb.push_str(qualifier(n, NamePart::Specific));
sb.push(' ');
}
append_qualifier(&mut sb, n, NamePart::Specific, f.show_qualifier);
if f.hybrid_marker && has_notho(n, NamePart::Specific) {
sb.push(HYBRID_MARKER);
sb.push(' ');
Expand Down
13 changes: 13 additions & 0 deletions crates/nameparser/src/model/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ pub enum NameType {
/// the catch-all `Other`. Placed before `Other` so the catch-all stays last; this makes it
/// FFI-wire ordinal 4 and shifts `Other` to 5 (a lockstep change gated by the ABI-version bump).
Identifier,
/// Any other unparsable name: no name could be read from it. With the code of the
/// `ParseResult::Unparsable` it is all the parser says about why (#248): a virus carries
/// [`NomCode::Virus`] (`HIV-1 06TG.HT064`); a code or label rather than a name (`Lepidostoma1`,
/// `ANME-1 cluster archaeon`), alternatives in different genera or without one (`Anser/Branta`,
/// `obsoletus/scoticus`), a vector or plasmid, numbers, abbreviations or free text carry none.
Other,
}

Expand Down Expand Up @@ -1162,6 +1167,14 @@ pub mod warnings {
/// Rust-only so far: a species epithet written with a capital, as Linnaeus did (`Ovis Aries`),
/// was put in lower case (ICZN Art. 28).
pub const CAPITALISED_EPITHET: &str = "capitalised epithet put in lower case";
/// No genus or epithet has a single letter (#246).
pub const SINGLE_LETTER: &str = "single letter genus or epithet";
/// A genus or uninomial holding a digit or a character no scientific name uses (#244):
/// `Imsh8ia aleurites`.
pub const UNUSUAL_GENUS: &str = "genus or uninomial with digit or unusual character";
/// An infrageneric, specific or infraspecific epithet holding a digit or a character no
/// scientific name uses (#244, #236): `Imshia aleu3ites`, `Ephedra przewalskiiß`.
pub const UNUSUAL_EPITHET: &str = "epithet with digit or unusual character";
}

#[cfg(test)]
Expand Down
32 changes: 32 additions & 0 deletions crates/nameparser/src/model/name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,18 @@ pub struct ParsedName {
static PUBLISHED_IN_YEAR: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"(?-u:\b(1[5-9]\d{2}|20\d{2}|2100)\b)").unwrap());

/// A word with the suffix GTDB gives a taxon it splits, an underscore and one to three capitals
/// (`Pseudomonas_E`, `Bacillus_BF`, `mitis_I`), as the taxon and the suffix. Not a lower-case label
/// or one with digits (`madecassus_01m`, `Stygiellidae_STYG2`).
pub(crate) fn split_gtdb_suffix(word: &str) -> Option<(&str, &str)> {
let (taxon, suffix) = word.rsplit_once('_')?;
(taxon.chars().next().is_some_and(char::is_alphabetic)
&& !taxon.contains('_')
&& (1..=3).contains(&suffix.len())
&& suffix.chars().all(|c| c.is_ascii_uppercase()))
.then_some((taxon, suffix))
}

impl ParsedName {
/// Add a warning if not already present — mirrors Java's `warnings` HashSet (deduping).
/// (The golden harness sorts warnings before diffing, so order is irrelevant; dedup is what matters.)
Expand Down Expand Up @@ -404,6 +416,8 @@ impl ParsedName {
/// The anchor keeps the name parts down to the deepest one that is present and not qualified
/// (cf., aff., ?, ...). A qualified genus is the exception: `?Sydonia alba`, `Ferganoconcha?
/// oblonga` and `cf. Craticula` are anchored on that genus, as no part of the name is surer.
/// The suffix GTDB gives a split taxon is dropped: `Pseudomonas_E putida` is anchored on
/// *Pseudomonas putida*, `Bacillus_BF` on *Bacillus* (see [`split_gtdb_suffix`]).
/// The anchor takes the rank of its deepest part and the authorship that belongs to it: the
/// specific authorship for a species anchor of an infraspecific name, the generic one for a
/// genus, the name's own when nothing below the anchor is given. The phrase, qualifiers, notes,
Expand Down Expand Up @@ -465,6 +479,7 @@ impl ParsedName {
if deepest == NamePart::Generic {
own_authorship(&mut a);
}
a.drop_gtdb_suffixes();
return Some(a);
}
// an infrageneric name without its genus, "subgen. Trematostoma"
Expand Down Expand Up @@ -573,9 +588,26 @@ impl ParsedName {
} else if kept == deepest {
own_authorship(&mut a);
}
a.drop_gtdb_suffixes();
Some(a)
}

/// The anchor of a GTDB name is the taxon GTDB split it from: `Pseudomonas_E putida` hangs off
/// `Pseudomonas putida`, `Streptococcus mitis_I` off `Streptococcus mitis`, `Bacillus_BF` off
/// `Bacillus`.
fn drop_gtdb_suffixes(&mut self) {
for part in [
&mut self.uninomial,
&mut self.genus,
&mut self.specific_epithet,
&mut self.infraspecific_epithet,
] {
if let Some((taxon, _)) = part.as_deref().and_then(split_gtdb_suffix) {
*part = Some(taxon.to_string());
}
}
}

fn keep_notho(&self, anchor: &mut ParsedName, part: NamePart) {
if self.notho.as_ref().is_some_and(|n| n.contains(&part)) {
anchor.add_notho(part);
Expand Down
59 changes: 38 additions & 21 deletions crates/nameparser/src/pipeline/assemble.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ use std::sync::LazyLock;

use regex::Regex;

use crate::model::name::split_gtdb_suffix;
use crate::model::{rank_utils, warnings, Authorship, NameType, NomCode, ParsedName, Rank, State};
use crate::pipeline::authorship_parser::AuthState;
use crate::pipeline::blacklisted_epithets;
Expand Down Expand Up @@ -163,13 +164,11 @@ pub(crate) fn finish(ctx: &mut ParseContext, auth_state: Option<&AuthState>) {
}

// Step 7: a monomial with an underscore is either "Genus_species" (underscore as space:
// genus + specific epithet, when the after-part starts lowercase) or a GTDB-style phrase
// name (e.g. "Desulfobacterota_B": uninomial + phrase, when the after-part starts
// uppercase). Under the bacterial code a GTDB suffix of capitals stays on the uninomial,
// though: `Bacillus_BF` is one of several genera GTDB splits Bacillus into, not Bacillus with
// a designation, and the suffixed genus of a binomial (`Acholeplasma_D palmae`) is kept whole
// too (#102). Without it the suffix may as well be a BOLD placeholder code
// (`Blattellinae_SB`).
// genus + specific epithet, when the after-part starts lowercase), a GTDB taxon or a
// designation of the taxon before it (uninomial + phrase, `Entodinium_1`). A GTDB suffix of
// capitals stays on the uninomial, as it does on the genus or epithet of a binomial
// (`Acholeplasma_D palmae`, #102): `Bacillus_BF` is one of several genera GTDB splits
// Bacillus into, not Bacillus with a designation. Such a name is informal, see step 19.
if ctx.name.type_ == NameType::Scientific {
if let Some(idx) = ctx.name.uninomial.as_ref().and_then(|u| u.find('_')) {
let uni = ctx.name.uninomial.clone().expect("just matched Some above");
Expand All @@ -181,13 +180,10 @@ pub(crate) fn finish(ctx: &mut ParseContext, auth_state: Option<&AuthState>) {
ctx.name.genus = Some(before);
ctx.name.specific_epithet = Some(after);
ctx.name.rank = Rank::Species;
} else if ctx.requested_code == Some(NomCode::Bacterial)
&& after.chars().count() <= 4
&& after.chars().all(|c| c.is_ascii_uppercase())
{
} else if split_gtdb_suffix(&uni).is_some() {
// "Bacillus_BF" → one GTDB genus
} else {
// "Desulfobacterota_B" → GTDB-style phrase name
// "Entodinium_1" → a designation of the taxon before it
ctx.name.uninomial = Some(before);
ctx.name.phrase = Some(after);
ctx.name.type_ = NameType::Informal;
Expand Down Expand Up @@ -418,8 +414,7 @@ pub(crate) fn finish(ctx: &mut ParseContext, auth_state: Option<&AuthState>) {
// Step 18: a phrase epithet without a cultivar always denotes an INFORMAL name → promote
// a monomial uninomial to a genus so callers see "Baeckea ssp. <phrase>" as a phrase name
// on a genus, not a uninomial scientific name. Skip promotion for suprageneric phrase
// forms (e.g. GTDB "Desulfobacterota_B") where the uninomial really is at family/order
// level.
// forms (e.g. "Eusiridae NZD") where the uninomial really is at family/order level.
if ctx.name.phrase.as_ref().is_some_and(|p| !p.is_empty())
&& ctx.name.cultivar_epithet.is_none()
{
Expand All @@ -430,6 +425,21 @@ pub(crate) fn finish(ctx: &mut ParseContext, auth_state: Option<&AuthState>) {
}
ctx.name.type_ = NameType::Informal;
}

// Step 19: a taxon GTDB split off is no regular scientific name, but an informal one anchored
// on the taxon it was split from: `Pseudomonas_E putida`, `Streptococcus mitis_I`,
// `Bacillus_BF`. Whatever the code: the same suffixes are used elsewhere in the tree.
let gtdb = [
&ctx.name.uninomial,
&ctx.name.genus,
&ctx.name.specific_epithet,
&ctx.name.infraspecific_epithet,
]
.into_iter()
.any(|part| part.as_deref().and_then(split_gtdb_suffix).is_some());
if gtdb {
ctx.name.type_ = NameType::Informal;
}
}

/// The INDETERMINED warning flags exactly the names [`ParsedName::is_indetermined`] holds for: the
Expand Down Expand Up @@ -713,18 +723,25 @@ mod tests {
}

#[test]
fn underscore_split_uppercase_after_becomes_gtdb_phrase_name() {
fn underscore_split_keeps_a_gtdb_suffix_on_an_informal_uninomial() {
let mut ctx = base_ctx();
ctx.name.uninomial = Some("Desulfobacterota_B".to_string());
finish(&mut ctx, None);
assert_eq!(ctx.name.uninomial, Some("Desulfobacterota".to_string()));
assert_eq!(ctx.name.phrase, Some("B".to_string()));
assert_eq!(ctx.name.uninomial, Some("Desulfobacterota_B".to_string()));
assert_eq!(ctx.name.phrase, None);
assert_eq!(ctx.name.type_, NameType::Informal);
assert_eq!(ctx.name.rank, Rank::Unranked);
assert_eq!(
ctx.name.genus, None,
"step 18 must not promote a suprageneric GTDB phrase name to genus"
);
assert_eq!(ctx.name.genus, None);
}

#[test]
fn underscore_split_makes_a_designation_the_phrase() {
let mut ctx = base_ctx();
ctx.name.uninomial = Some("Entodinium_1".to_string());
finish(&mut ctx, None);
assert_eq!(ctx.name.uninomial, Some("Entodinium".to_string()));
assert_eq!(ctx.name.phrase, Some("1".to_string()));
assert_eq!(ctx.name.type_, NameType::Informal);
}

#[test]
Expand Down
10 changes: 6 additions & 4 deletions crates/nameparser/src/pipeline/authorship_split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,14 +269,16 @@ pub fn find_boundary(tokens: &[Token], ctx: &ParseContext) -> usize {
}
// "n. sp." before any epithet is the species marker "sp." of a new species
// ("Jardinella n. sp. keel", #139): walk on to the "sp"
let dot = usize::from(tokens.get(i + 1).is_some_and(|d| d.kind == TokenKind::Dot));
if t.text == "n"
&& !have_epithet
&& tokens.get(i + 1).is_some_and(|d| d.kind == TokenKind::Dot)
&& tokens.get(i + 2).is_some_and(|m| {
m.kind == TokenKind::Word && m.text.eq_ignore_ascii_case("sp")
&& tokens.get(i + 1 + dot).is_some_and(|m| {
m.kind == TokenKind::Word
&& (m.text.eq_ignore_ascii_case("sp")
|| crate::pipeline::name_tokens::is_glued_indet_marker(&m.text))
})
{
i += 2;
i += 1 + dot;
continue;
}
// cf./aff. qualifiers and indet markers — keep walking
Expand Down
Loading
Loading