#[derive(Clone, Copy)]
pub struct KnownNamespace {
name: &'static str,
marker: Option<&'static str>,
claims: fn(&str) -> bool,
}
impl PartialEq for KnownNamespace {
fn eq(&self, other: &Self) -> bool {
self.name == other.name
}
}
impl Eq for KnownNamespace {}
impl std::fmt::Debug for KnownNamespace {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("KnownNamespace").field(&self.name).finish()
}
}
impl KnownNamespace {
pub const ALL: [Self; 7] = [
Self {
name: "Ensembl gene",
marker: Some("the `ENS` prefix"),
claims: ensembl_gene,
},
Self {
name: "UniProt accession",
marker: Some("digits in fixed positions, at six characters or ten"),
claims: uniprot_accession,
},
Self {
name: "RefSeq",
marker: Some("a two-letter prefix and an underscore"),
claims: refseq,
},
Self {
name: "HGNC ID",
marker: Some("the `HGNC:` prefix"),
claims: hgnc_id,
},
Self {
name: "ChEMBL target",
marker: Some("the `CHEMBL` prefix"),
claims: chembl_target,
},
Self {
name: "Entrez gene",
marker: Some("digits throughout"),
claims: entrez_gene,
},
Self {
name: "HGNC symbol",
marker: None,
claims: hgnc_symbol,
},
];
#[must_use]
pub const fn name(&self) -> &'static str {
self.name
}
#[must_use]
pub const fn marker(&self) -> Option<&'static str> {
self.marker
}
#[must_use]
pub const fn discriminates(&self) -> bool {
self.marker.is_some()
}
#[must_use]
pub fn claims(&self, identifier: &str) -> bool {
(self.claims)(identifier.trim())
}
#[must_use]
pub fn resolve(identifier: &str) -> Option<&'static Self> {
if crate::note::looks_like_a_date(identifier.trim()) {
return None;
}
Self::ALL
.iter()
.find(|namespace| namespace.claims(identifier))
}
#[must_use]
pub fn infer<'a, I>(identifiers: I) -> Inference
where
I: IntoIterator<Item = &'a str>,
{
let mut found: Option<&'static Self> = None;
for identifier in identifiers {
let Some(resolved) = Self::resolve(identifier) else {
return Inference::Unsettled;
};
match found {
None => found = Some(resolved),
Some(agreed) if agreed == resolved => {}
Some(_) => return Inference::Unsettled,
}
}
match found {
None => Inference::Unsettled,
Some(shape) if shape.discriminates() => Inference::Settled(shape),
Some(shape) => Inference::Indistinguishable(shape),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Inference {
Settled(&'static KnownNamespace),
Unsettled,
Indistinguishable(&'static KnownNamespace),
}
impl Inference {
#[must_use]
pub const fn settled(self) -> Option<&'static KnownNamespace> {
match self {
Self::Settled(namespace) => Some(namespace),
Self::Unsettled | Self::Indistinguishable(_) => None,
}
}
#[must_use]
pub const fn indistinguishable(self) -> Option<&'static KnownNamespace> {
match self {
Self::Indistinguishable(namespace) => Some(namespace),
Self::Settled(_) | Self::Unsettled => None,
}
}
}
fn ensembl_gene(identifier: &str) -> bool {
let Some(rest) = identifier.strip_prefix("ENS") else {
return false;
};
let rest = match rest.split_once('.') {
Some((stem, version)) if !version.is_empty() && all_digits(version) => stem,
Some(_) => return false,
None => rest,
};
(0..=3).any(|species| {
rest.len() > species
&& rest[..species].chars().all(|c| c.is_ascii_uppercase())
&& rest[species..]
.strip_prefix('G')
.is_some_and(|digits| digits.len() == 11 && all_digits(digits))
})
}
fn uniprot_accession(identifier: &str) -> bool {
let stem = match identifier.split_once('-') {
Some((base, isoform)) if !isoform.is_empty() && all_digits(isoform) => base,
Some(_) => return false,
None => identifier,
};
let characters: Vec<char> = stem.chars().collect();
let Some((&first, rest)) = characters.split_first() else {
return false;
};
if !first.is_ascii_uppercase() {
return false;
}
if matches!(first, 'O' | 'P' | 'Q') {
return rest.len() == 5
&& rest[0].is_ascii_digit()
&& rest[1..4].iter().all(char::is_ascii_alphanumeric)
&& rest[4].is_ascii_digit();
}
let (Some(&digit), blocks) = (rest.first(), &rest[1.min(rest.len())..]) else {
return false;
};
digit.is_ascii_digit()
&& matches!(blocks.len(), 4 | 8)
&& blocks.chunks(4).all(is_accession_block)
}
fn is_accession_block(block: &[char]) -> bool {
let [letter, middle_one, middle_two, last] = *block else {
return false;
};
letter.is_ascii_uppercase()
&& [middle_one, middle_two]
.iter()
.all(char::is_ascii_alphanumeric)
&& last.is_ascii_digit()
}
fn refseq(identifier: &str) -> bool {
const PREFIXES: [&str; 14] = [
"NM", "NR", "NP", "XM", "XR", "XP", "NC", "NG", "NT", "NW", "NZ", "AP", "YP", "WP",
];
let Some((prefix, rest)) = identifier.split_once('_') else {
return false;
};
if !PREFIXES.contains(&prefix) {
return false;
}
let rest = rest.to_owned();
match rest.split_once('.') {
Some((number, version)) => {
all_digits(number) && !number.is_empty() && all_digits(version) && !version.is_empty()
}
None => all_digits(&rest) && !rest.is_empty(),
}
}
fn hgnc_id(identifier: &str) -> bool {
identifier
.strip_prefix("HGNC:")
.is_some_and(|number| !number.is_empty() && all_digits(number))
}
fn chembl_target(identifier: &str) -> bool {
identifier
.strip_prefix("CHEMBL")
.is_some_and(|number| !number.is_empty() && all_digits(number))
}
fn entrez_gene(identifier: &str) -> bool {
!identifier.is_empty() && all_digits(identifier)
}
fn hgnc_symbol(identifier: &str) -> bool {
const LONGEST: usize = 24;
let mut characters = identifier.chars();
characters.next().is_some_and(|c| c.is_ascii_uppercase())
&& identifier.len() <= LONGEST
&& characters.all(|c| c.is_ascii_alphanumeric() || c == '-')
}
fn all_digits(text: &str) -> bool {
text.chars().all(|c| c.is_ascii_digit())
}