#![allow(clippy::unwrap_used, clippy::expect_used)]
use ligase::dim::{Contig, ContigNaming, ReferenceId, Relation, Verdict, relate};
fn contig(name: &str, length: u64, md5: Option<&str>) -> Contig {
Contig {
name: name.to_owned(),
length: Some(length),
md5: md5.map(str::to_owned),
}
}
fn lengthless(name: &str, md5: Option<&str>) -> Contig {
Contig {
name: name.to_owned(),
length: None,
md5: md5.map(str::to_owned),
}
}
const M1: &str = "a965a71aa3690f605935c54d320905ab";
const M2: &str = "89c2d6e069f386bafb1a0c811ba821f4";
const M3: &str = "61bb649e97c09f49b40b225b9f863777";
const M1_OTHER: &str = "44e7acaffac988f931debb037a014055";
fn bare() -> ReferenceId {
ReferenceId::new(
None,
vec![contig("1", 20, Some(M1)), contig("2", 20, Some(M2))],
)
}
fn prefixed() -> ReferenceId {
ReferenceId::new(
None,
vec![contig("chr1", 20, Some(M1)), contig("chr2", 20, Some(M2))],
)
}
#[test]
fn identity_ignores_the_label() {
let labelled = ReferenceId::new(Some("hg38".into()), bare().contigs().to_vec());
let differently_labelled = ReferenceId::new(Some("GRCh38".into()), bare().contigs().to_vec());
assert_eq!(
relate(&labelled, &differently_labelled, Relation::Identical),
Verdict::Agree
);
}
#[test]
fn same_names_and_lengths_but_different_md5_is_a_reference_mismatch() {
let left = ReferenceId::new(
Some("hg38".into()),
vec![contig("1", 20, Some(M1)), contig("2", 20, Some(M2))],
);
let right = ReferenceId::new(
Some("hg38".into()),
vec![contig("1", 20, Some(M1_OTHER)), contig("2", 20, Some(M2))],
);
match relate(&left, &right, Relation::Identical) {
Verdict::ContigConflict { name, .. } => assert_eq!(name, "1"),
other => panic!("expected a contig conflict, got {other:?}"),
}
}
#[test]
fn same_names_but_different_lengths_is_a_reference_mismatch() {
let left = ReferenceId::new(None, vec![contig("1", 20, None)]);
let right = ReferenceId::new(None, vec![contig("1", 21, None)]);
match relate(&left, &right, Relation::Identical) {
Verdict::ContigConflict { name, .. } => assert_eq!(name, "1"),
other => panic!("expected a contig conflict, got {other:?}"),
}
}
#[test]
fn a_missing_length_on_one_side_is_not_invented() {
let sized = ReferenceId::new(None, vec![contig("1", 20, Some(M1))]);
let unsized_ = ReferenceId::new(None, vec![lengthless("1", Some(M1))]);
assert_eq!(
relate(&sized, &unsized_, Relation::Identical),
Verdict::Agree
);
let wrong = ReferenceId::new(None, vec![lengthless("1", Some(M1_OTHER))]);
assert!(matches!(
relate(&sized, &wrong, Relation::Identical),
Verdict::ContigConflict { .. }
));
}
#[test]
fn a_missing_md5_on_one_side_falls_back_to_name_and_length() {
let with_md5 = ReferenceId::new(None, vec![contig("1", 20, Some(M1))]);
let without = ReferenceId::new(None, vec![contig("1", 20, None)]);
assert_eq!(
relate(&with_md5, &without, Relation::Identical),
Verdict::Agree
);
}
#[test]
fn chr_prefixes_are_a_naming_difference_not_a_reference_difference() {
match relate(&prefixed(), &bare(), Relation::Identical) {
Verdict::NamingDiffers { left, right, .. } => {
assert_eq!(left, ContigNaming::Prefixed);
assert_eq!(right, ContigNaming::Bare);
}
other => panic!("expected a naming difference, got {other:?}"),
}
}
#[test]
fn naming_style_is_detected_from_the_contig_names() {
assert_eq!(bare().naming(), ContigNaming::Bare);
assert_eq!(prefixed().naming(), ContigNaming::Prefixed);
let mixed = ReferenceId::new(
None,
vec![contig("chr1", 20, Some(M1)), contig("2", 20, Some(M2))],
);
assert_eq!(mixed.naming(), ContigNaming::Mixed);
}
#[test]
fn chr_m_and_mt_name_the_same_contig_only_when_the_sequence_agrees() {
let ucsc = ReferenceId::new(None, vec![contig("chrM", 16569, Some(M3))]);
let ensembl = ReferenceId::new(None, vec![contig("MT", 16569, Some(M3))]);
assert!(matches!(
relate(&ucsc, &ensembl, Relation::Identical),
Verdict::NamingDiffers { .. }
));
let hg19 = ReferenceId::new(None, vec![contig("chrM", 16571, Some(M1))]);
match relate(&hg19, &ensembl, Relation::Identical) {
Verdict::ContigConflict { .. } => {}
other => panic!("differing mitochondrial sequence must not pass as naming: {other:?}"),
}
}
#[test]
fn data_mentioning_a_subset_of_contigs_is_contained_in_the_reference() {
let data = ReferenceId::new(None, vec![contig("1", 20, Some(M1))]);
let whole = ReferenceId::new(
None,
vec![
contig("1", 20, Some(M1)),
contig("2", 20, Some(M2)),
contig("3", 20, Some(M3)),
],
);
assert_eq!(relate(&data, &whole, Relation::DataWithin), Verdict::Agree);
}
#[test]
fn a_subset_is_not_identical_when_two_references_are_compared() {
let subset = ReferenceId::new(None, vec![contig("1", 20, Some(M1))]);
match relate(&subset, &bare(), Relation::Identical) {
Verdict::MissingContig { name, .. } => assert_eq!(name, "2"),
other => panic!("expected a missing contig, got {other:?}"),
}
}
#[test]
fn a_contig_absent_from_the_reference_is_a_mismatch() {
let data = ReferenceId::new(
None,
vec![contig("1", 20, Some(M1)), contig("9", 20, Some(M3))],
);
match relate(&data, &bare(), Relation::DataWithin) {
Verdict::MissingContig { name, .. } => assert_eq!(name, "9"),
other => panic!("expected a missing contig, got {other:?}"),
}
}
#[test]
fn containment_still_checks_the_checksum_of_shared_contigs() {
let data = ReferenceId::new(None, vec![contig("1", 20, Some(M1_OTHER))]);
match relate(&data, &bare(), Relation::DataWithin) {
Verdict::ContigConflict { name, .. } => assert_eq!(name, "1"),
other => panic!("expected a contig conflict, got {other:?}"),
}
}
#[test]
fn reordered_contigs_are_their_own_verdict() {
let reordered = ReferenceId::new(
None,
vec![contig("2", 20, Some(M2)), contig("1", 20, Some(M1))],
);
match relate(&reordered, &bare(), Relation::Identical) {
Verdict::OrderDiffers { .. } => {}
other => panic!("expected an order difference, got {other:?}"),
}
}
#[test]
fn containment_of_a_subset_in_a_different_order_still_flags_the_order() {
let data = ReferenceId::new(
None,
vec![contig("3", 20, Some(M3)), contig("1", 20, Some(M1))],
);
let whole = ReferenceId::new(
None,
vec![
contig("1", 20, Some(M1)),
contig("2", 20, Some(M2)),
contig("3", 20, Some(M3)),
],
);
match relate(&data, &whole, Relation::DataWithin) {
Verdict::OrderDiffers { .. } => {}
other => panic!("expected an order difference, got {other:?}"),
}
}
#[test]
fn a_label_describes_a_reference_when_one_was_declared() {
let labelled = ReferenceId::new(
Some("HXB2".into()),
vec![contig("K03455.1", 9719, Some(M1))],
);
assert_eq!(labelled.describe(), "HXB2");
}
#[test]
fn an_unlabelled_reference_describes_itself_by_its_contigs() {
let single = ReferenceId::new(None, vec![contig("K03455.1", 9719, Some(M1))]);
assert_eq!(single.describe(), "K03455.1 (9719 bp)");
assert_eq!(bare().describe(), "2 contigs, 40 bp");
}
#[test]
fn fingerprints_distinguish_references_that_describe_themselves_the_same_way() {
let left = ReferenceId::new(Some("hg38".into()), vec![contig("1", 20, Some(M1))]);
let right = ReferenceId::new(Some("hg38".into()), vec![contig("1", 20, Some(M1_OTHER))]);
assert_eq!(left.describe(), right.describe());
assert_ne!(left.fingerprint(), right.fingerprint());
}
#[test]
fn a_fingerprint_is_stable_across_naming_style() {
assert_eq!(bare().fingerprint(), prefixed().fingerprint());
}