mzdata-param 0.66.0

A library to read mass spectrometry data formats and a data model for mass spectra
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
use std::{
    collections::{HashMap, HashSet, VecDeque},
    fs, path,
};

use super::*;

use bincode::serde::Compat;
use context_error::{BoxedError, CreateError, FullErrorContent, StaticErrorContent};
use mzcv::{CVData, CVIndex, CVSource, CVStructure, HashBufReader};
use serde::{Deserialize, Serialize};
use sha1::Digest;
use std::sync::{Arc, OnceLock};

impl TryFrom<mzcv::ControlledVocabulary> for ControlledVocabulary {
    type Error = ControlledVocabularyResolutionError;

    fn try_from(value: mzcv::ControlledVocabulary) -> Result<Self, Self::Error> {
        Ok(match value {
            mzcv::ControlledVocabulary::MS => Self::MS,
            mzcv::ControlledVocabulary::UO => Self::UO,
            mzcv::ControlledVocabulary::EFO => Self::EFO,
            mzcv::ControlledVocabulary::OBI => Self::OBI,
            mzcv::ControlledVocabulary::HANCESTRO => Self::HANCESTRO,
            mzcv::ControlledVocabulary::BFO => Self::BFO,
            mzcv::ControlledVocabulary::NCIT => Self::NCIT,
            mzcv::ControlledVocabulary::BTO => Self::BTO,
            mzcv::ControlledVocabulary::PRIDE => Self::PRIDE,
            _ => {
                return Err(
                    ControlledVocabularyResolutionError::UnknownControlledVocabulary(
                        value.to_string(),
                    ),
                );
            }
        })
    }
}

impl From<ControlledVocabulary> for mzcv::ControlledVocabulary {
    fn from(value: ControlledVocabulary) -> Self {
        match value {
            ControlledVocabulary::MS => mzcv::ControlledVocabulary::MS,
            ControlledVocabulary::UO => mzcv::ControlledVocabulary::UO,
            ControlledVocabulary::EFO => mzcv::ControlledVocabulary::EFO,
            ControlledVocabulary::OBI => mzcv::ControlledVocabulary::OBI,
            ControlledVocabulary::HANCESTRO => mzcv::ControlledVocabulary::HANCESTRO,
            ControlledVocabulary::BFO => mzcv::ControlledVocabulary::BFO,
            ControlledVocabulary::NCIT => mzcv::ControlledVocabulary::NCIT,
            ControlledVocabulary::BTO => mzcv::ControlledVocabulary::BTO,
            ControlledVocabulary::PRIDE => mzcv::ControlledVocabulary::PRIDE,
            #[cfg(feature = "imzml")]
            ControlledVocabulary::IMS => mzcv::ControlledVocabulary::Unknown,
            ControlledVocabulary::Unknown => mzcv::ControlledVocabulary::Unknown,
        }
    }
}

impl TryFrom<mzcv::Curie> for CURIE {
    type Error = CURIEParsingError;
    fn try_from(value: mzcv::Curie) -> Result<Self, Self::Error> {
        Ok(CURIE::new(
            value.cv.try_into()?,
            match value.accession {
                mzcv::AccessionCode::Numeric(v) => v,
                mzcv::AccessionCode::Alphanumeric(_, _) => "".parse()?,
            },
        ))
    }
}

impl TryFrom<mzcv::OboIdentifier> for CURIE {
    type Error = CURIEParsingError;

    fn try_from(value: mzcv::OboIdentifier) -> Result<Self, Self::Error> {
        match mzcv::Curie::try_from(value) {
            Ok(v) => v.try_into(),
            Err(e) => match e {
                mzcv::CURIEParsingError::UnknownControlledVocabulary => {
                    Err(CURIEParsingError::UnknownControlledVocabulary(
                        ControlledVocabularyResolutionError::UnknownControlledVocabulary(
                            "".to_string(),
                        ),
                    ))
                }
                mzcv::CURIEParsingError::AccessionParsingError(_) => {
                    let v = "".parse::<u32>()?;
                    Ok(CURIE::new(ControlledVocabulary::Unknown, v))
                }
                mzcv::CURIEParsingError::MissingNamespaceSeparator => {
                    Err(CURIEParsingError::MissingNamespaceSeparator)
                }
            },
        }
    }
}

#[derive(Clone, Debug, bincode::Encode, bincode::Decode)]
pub struct MSTerm {
    pub name: Box<str>,
    accession: bincode::serde::Compat<CURIE>,
    pub(crate) parents: HashSet<bincode::serde::Compat<CURIE>>,
    pub synonyms: Vec<Box<str>>,
    pub relationships: Vec<(Box<str>, Box<str>)>,
}

impl MSTerm {
    #[inline(always)]
    pub fn accession(&self) -> CURIE {
        self.accession.0
    }

    #[inline(always)]
    pub fn curie(&self) -> CURIE {
        self.accession()
    }

    #[inline(always)]
    pub fn parents(&self) -> impl ExactSizeIterator<Item = &CURIE> + '_ {
        self.parents.iter().map(|v| &v.0)
    }

    #[inline(always)]
    pub fn is_a(&self, accession: &CURIE) -> bool {
        self.parents.iter().any(|v| v.0 == *accession)
    }

    #[inline(always)]
    pub fn has_value_types(&self) -> impl Iterator<Item = &str> + '_ {
        self.relationships.iter().filter_map(|(r, v)| {
            if r.as_ref() == "has_value_type" {
                Some(v.as_ref())
            } else {
                None
            }
        })
    }
}

impl CVData for MSTerm {
    type Index = CURIE;

    fn index(&self) -> Option<Self::Index> {
        Some(self.accession.0)
    }

    fn name(&self) -> Option<std::borrow::Cow<'_, str>> {
        Some(Cow::Borrowed(self.name.as_ref()))
    }

    fn synonyms(&self) -> impl Iterator<Item = &str> {
        self.synonyms.iter().map(|s| s.as_ref())
    }

    fn parents(&self) -> impl Iterator<Item = &Self::Index> {
        self.parents.iter().map(|c| &c.0)
    }

    fn curie(&self) -> Option<mzcv::Curie> {
        Some(mzcv::Curie {
            cv: self.accession.0.controlled_vocabulary().into(),
            accession: mzcv::AccessionCode::Numeric(self.accession.0.accession_int()),
        })
    }
}

/// A global facade for interacting with the PSI-MS controlled vocabulary
#[derive(Debug)]
pub struct MSVocabulary();

static MS: OnceLock<mzcv::CVIndex<MSVocabulary>> = OnceLock::new();

impl MSVocabulary {
    /// Get the version metadata of the vocabulary
    pub fn version() -> &'static mzcv::CVVersion {
        Self::init().version()
    }

    /// Update the PSI-MS vocabulary to the version at the provided path. This will update
    /// the parsed cache on disk and attempt to set the global in-memory representation to
    /// use this version.
    ///
    /// # Warning
    /// If the controlled vocabulary is already loaded, this will fail to update the current
    /// version in memory, but should still update the version on disk so that it will be read
    /// from when the a new process starts.
    ///
    /// This *does not* update the compile time static version embedded in the binary used by [`Self::init_static`].
    /// That can only be upgraded at build time.
    pub fn update_from_obo<P: AsRef<path::Path>>(
        obo_path: P,
    ) -> Result<(), Box<dyn std::error::Error>> {
        let infile = obo_path.as_ref();
        let cv_reader: HashBufReader<Box<dyn std::io::Read>, sha1::Sha1> =
            if infile.extension().is_some_and(|s| s == "gz") {
                HashBufReader::boxed(flate2::read::GzDecoder::new(fs::File::open(&infile)?))
            } else {
                HashBufReader::boxed(fs::File::open(&infile)?)
            };

        let (ver, data, errs) = match MSVocabulary::parse([cv_reader].into_iter()) {
            Ok(r) => r,
            Err(e) => {
                let root = BoxedError::default().add_underlying_errors(e);
                return Err(Box::new(root));
            }
        };
        for e in errs {
            log::warn!("While updating PSI-MS CV: {e}");
        }
        let mut this = CVIndex::<Self>::empty();
        this.update_from_structure(ver, data)?;
        if let Err(_) = MS.set(this) {
            log::error!("Failed to update global in-memory singleton, value was already initialized. On-disk cache updated.")
        }
        Ok(())
    }

    /// Get or create the global instance of the [`mzcv::CVIndex`] for [`MSVocabulary`]
    pub fn init() -> &'static mzcv::CVIndex<MSVocabulary> {
        MS.get_or_init(|| {
            let (cv, errs) = mzcv::CVIndex::<Self>::init();
            for e in errs {
                log::error!("Error while initializing MS vocabulary database: {e}");
            }
            cv
        })
    }

    /// Get or create the global instance of the [`mzcv::CVIndex`] for [`MSVocabulary`]
    /// using only the statically embedded data.
    pub fn init_static() -> &'static mzcv::CVIndex<MSVocabulary> {
        MS.get_or_init(|| {
            let cv = mzcv::CVIndex::<Self>::init_static();
            cv
        })
    }

    /// Get a term by its [`CURIE`]
    pub fn get(curie: CURIE) -> Option<Arc<MSTerm>> {
        Self::init().get_by_index(&curie)
    }

    /// Get a term by its human-readable name
    pub fn get_by_name(name: &str) -> Option<Arc<MSTerm>> {
        Self::init().get_by_name(name)
    }

    /// Get the immediate parents of this term
    ///
    /// This follows the `is_a` OBO relationship.
    pub fn parents_of(curie: CURIE) -> Option<HashSet<Compat<CURIE>>> {
        Self::get(curie).map(|v| v.parents.clone())
    }

    /// Get all parents of this term, recursively querying the graph
    ///
    /// This follows the `is_a` OBO relationship.
    pub fn parents_of_recursive(curie: CURIE) -> HashSet<Compat<CURIE>> {
        let cv = Self::init();
        let mut acc = HashSet::new();
        let mut queue = VecDeque::from([Compat(curie)]);
        while !queue.is_empty() {
            let idx = queue.pop_front().unwrap();
            if let Some(term) = cv.get_by_index(&idx.0) {
                for p in term.parents.iter() {
                    if acc.contains(p) {
                        continue;
                    }
                    queue.push_back(p.clone());
                }
            }
            acc.insert(idx);
        }
        acc.remove(&Compat(curie));
        acc
    }

    /// Get the immediate children of this term
    ///
    /// This follows the `is_a` OBO relationship.
    pub fn children_of(curie: CURIE) -> Option<&'static [Compat<CURIE>]> {
        let cv = Self::init();
        cv.data()
            .child_map()
            .get(&Compat(curie))
            .map(|v| v.as_slice())
    }

    /// Test if `child` is derived from `parent` along a transitive `is_a` relationship
    pub fn is_child_of(child: CURIE, parent: CURIE) -> bool {
        let cv = Self::init();

        let child_term = match cv.get_by_index(&child) {
            Some(t) => t,
            None => {
                return false;
            }
        };

        let parent = Compat(parent);
        if child_term.parents.contains(&parent) {
            return true;
        }

        let mut seen = HashSet::new();
        let mut queue = VecDeque::from_iter(child_term.parents().copied());

        while !queue.is_empty() {
            let index = queue.pop_front().unwrap();
            seen.insert(index);
            if let Some(term) = cv.get_by_index(&index) {
                for of in term.parents() {
                    if *of == parent.0 {
                        return true;
                    }
                    if seen.contains(of) {
                        continue;
                    }
                    queue.push_back(*of);
                }
            }
        }
        return false;
    }

    /// Get all children of this term, recursively querying the graph
    ///
    /// This follows the `is_a` OBO relationship.
    pub fn children_of_recursive(curie: CURIE) -> HashSet<Compat<CURIE>> {
        let cv = Self::init();
        let mut acc = HashSet::new();
        let mut queue = VecDeque::from([Compat(curie)]);
        while !queue.is_empty() {
            let idx = queue.pop_front().unwrap();
            if let Some(children) = cv.data().child_map().get(&idx).map(|v| v.as_slice()) {
                for p in children.iter() {
                    if acc.contains(p) {
                        continue;
                    }
                    queue.push_back(p.clone());
                }
            }
            acc.insert(idx);
        }
        acc.remove(&Compat(curie));
        acc
    }
}

fn convert_stanza_to_msterm(obj: mzcv::OboStanza) -> Option<Arc<MSTerm>> {
    let mut data = MSTerm {
        accession: bincode::serde::Compat(obj.id.try_into().ok()?),
        name: obj.lines["name"][0].0.clone(),
        synonyms: obj.synonyms.iter().map(|s| s.synonym.clone()).collect(),
        parents: HashSet::new(),
        relationships: Vec::new(),
    };

    for (rel, tp, _ns, _comment) in obj.relationship {
        match rel {
            mzcv::RelationType::IsA => {
                match mzcv::Curie::try_from(tp) {
                    Ok(val) => {
                        let parent = CURIE::try_from(val).unwrap();
                        data.parents.insert(bincode::serde::Compat(parent))
                    }
                    Err(e) => {
                        log::error!("Failed to parse CV identifier: {e}");
                        continue;
                    }
                };
            }
            mzcv::RelationType::Other(rel) => {
                data.relationships
                    .push((rel, tp.to_string().into_boxed_str()));
            }
        }
    }
    Some(Arc::new(data))
}

/// A data structure for walking up and down trees.
#[derive(Debug, bincode::Encode, bincode::Decode)]
pub struct VocabularyData<T: CVData>
where
    T::Index: Serialize + for<'a> Deserialize<'a>,
{
    terms: Vec<Arc<T>>,
    child_map: HashMap<Compat<T::Index>, Vec<Compat<T::Index>>>,
}

impl<T: CVData> IntoIterator for VocabularyData<T>
where
    T::Index: Serialize + for<'a> Deserialize<'a>,
{
    type Item = Arc<T>;

    type IntoIter = std::vec::IntoIter<Self::Item>;

    fn into_iter(self) -> Self::IntoIter {
        self.terms.into_iter()
    }
}

impl<T: CVData> Default for VocabularyData<T>
where
    T::Index: Serialize + for<'a> Deserialize<'a>,
{
    fn default() -> Self {
        Self {
            terms: Vec::default(),
            child_map: HashMap::default(),
        }
    }
}

impl<T: CVData> VocabularyData<T>
where
    T::Index: Serialize + for<'a> Deserialize<'a>,
{
    pub fn new(
        terms: Vec<Arc<T>>,
        child_map: HashMap<Compat<T::Index>, Vec<Compat<T::Index>>>,
    ) -> Self {
        Self { terms, child_map }
    }

    pub fn terms(&self) -> &[Arc<T>] {
        &self.terms
    }

    pub fn child_map(&self) -> &HashMap<Compat<T::Index>, Vec<Compat<T::Index>>> {
        &self.child_map
    }
}

impl<T: CVData> CVStructure<T> for VocabularyData<T>
where
    T::Index: Serialize + for<'a> Deserialize<'a>,
{
    fn is_empty(&self) -> bool {
        self.terms.is_empty()
    }

    fn len(&self) -> usize {
        self.terms.len()
    }

    fn clear(&mut self) {
        self.terms.clear();
        self.child_map.clear();
    }

    type Index = usize;

    fn iter_indexed(&self) -> Self::IterIndexed<'_> {
        self.terms.iter_indexed()
    }

    fn iter_data(&self) -> Self::IterData<'_> {
        self.terms.iter().cloned()
    }

    fn add(&mut self, data: std::sync::Arc<T>) {
        self.terms.push(data);
    }

    fn index(&self, index: Self::Index) -> Option<std::sync::Arc<T>> {
        self.terms.get(index).cloned()
    }

    fn remove(&mut self, index: Self::Index) {
        self.terms.remove(index);
    }

    type IterIndexed<'a>
        = <Vec<Arc<T>> as CVStructure<T>>::IterIndexed<'a>
    where
        Self: 'a;

    type IterData<'a>
        = <Vec<Arc<T>> as CVStructure<T>>::IterData<'a>
    where
        Self: 'a;
}

impl mzcv::CVSource for MSVocabulary {
    type Data = MSTerm;

    type Structure = VocabularyData<MSTerm>;

    fn cv_name() -> &'static str {
        "MS"
    }

    fn files() -> &'static [mzcv::CVFile] {
        &[mzcv::CVFile {
            name: "MS",
            extension: "obo",
            url: Some("http://purl.obolibrary.org/obo/ms.obo"),
            compression: mzcv::CVCompression::None,
        }]
    }

    fn static_data() -> Option<(mzcv::CVVersion, Self::Structure)> {
        #[cfg(feature = "static_data")]
        {
            use bincode::config::Configuration;
            use std::io;
            let buf = io::Cursor::new(include_bytes!("ms.dat"));
            let mut reader = flate2::bufread::GzDecoder::new(buf);
            let cache = bincode::decode_from_std_read::<
                (mzcv::CVVersion, Self::Structure),
                Configuration,
                _,
            >(&mut reader, Configuration::default())
            .unwrap();
            Some(cache)
        }
        #[cfg(not(feature = "static_data"))]
        {
            None
        }
    }

    fn parse(
        mut reader: impl Iterator<Item = mzcv::HashBufReader<Box<dyn std::io::Read>, impl Digest>>,
    ) -> Result<
        (
            mzcv::CVVersion,
            Self::Structure,
            Vec<BoxedError<'static, mzcv::CVError>>,
        ),
        Vec<BoxedError<'static, mzcv::CVError>>,
    > {
        use context_error::CreateError;
        let reader = reader.next().unwrap();
        let obo = mzcv::OboOntology::from_raw(reader).map_err(|e| {
            vec![
                context_error::BoxedError::small(
                    mzcv::CVError::FileCouldNotBeParsed,
                    e.get_short_description(),
                    e.get_long_description(),
                )
                .add_contexts(e.get_contexts().iter().cloned()),
            ]
        })?;
        let version = obo.version();
        let terms: Vec<Arc<MSTerm>> = obo
            .objects
            .into_iter()
            .filter(|o| o.stanza_type == mzcv::OboStanzaType::Term)
            .filter_map(convert_stanza_to_msterm)
            .collect();

        let mut child_map: HashMap<Compat<CURIE>, Vec<Compat<CURIE>>> =
            HashMap::with_capacity(terms.len());
        for term in terms.iter() {
            for parent in term.parents.iter() {
                child_map
                    .entry(parent.clone())
                    .or_default()
                    .push(term.accession.clone());
            }
        }

        let state = VocabularyData::new(terms, child_map);
        Ok((version, state, Vec::new()))
    }
}

pub trait CVTraversal<T: CVSource> {
    /// Helper method to mirror [`mzcv::CVIndex::get_by_index`]
    fn get_by_index(&self, key: <T::Data as CVData>::Index) -> Option<Arc<T::Data>>;

    fn child_map(
        &self,
    ) -> &HashMap<Compat<<T::Data as CVData>::Index>, Vec<Compat<<T::Data as CVData>::Index>>>;

    /// Get the immediate children of this term
    ///
    /// This follows the `is_a` OBO relationship.
    fn children_of(
        &self,
        curie: <T::Data as CVData>::Index,
    ) -> Option<&'_ [Compat<<T::Data as CVData>::Index>]>;

    /// Get the immediate parents of this term
    ///
    /// This follows the `is_a` OBO relationship.
    fn parents_of(
        &self,
        curie: <T::Data as CVData>::Index,
    ) -> Option<HashSet<Compat<<T::Data as CVData>::Index>>>;

    /// Get all parents of this term, recursively querying the graph
    ///
    /// This follows the `is_a` OBO relationship.
    fn parents_of_recursive(
        &self,
        curie: <T::Data as CVData>::Index,
    ) -> HashSet<Compat<<T::Data as CVData>::Index>> {
        let mut acc: HashSet<Compat<<<T as CVSource>::Data as CVData>::Index>> = HashSet::new();
        let mut queue = VecDeque::from([Compat(curie.clone())]);
        while !queue.is_empty() {
            let idx = queue.pop_front().unwrap();
            if let Some(term) = self.get_by_index(idx.0.clone()) {
                for p in term.parents() {
                    let p = Compat(p.clone());
                    if acc.contains(&p) {
                        continue;
                    }
                    queue.push_back(p);
                }
            }
            acc.insert(idx);
        }
        acc.remove(&Compat(curie));
        acc
    }

    /// Get all children of this term, recursively querying the graph
    ///
    /// This follows the `is_a` OBO relationship.
    fn children_of_recursive(
        &self,
        curie: <T::Data as CVData>::Index,
    ) -> HashSet<Compat<<T::Data as CVData>::Index>> {
        let cv = self;
        let mut acc = HashSet::new();
        let mut queue = VecDeque::from([Compat(curie.clone())]);
        while !queue.is_empty() {
            let idx = queue.pop_front().unwrap();
            if let Some(children) = cv.child_map().get(&idx).map(|v| v.as_slice()) {
                for p in children.iter() {
                    if acc.contains(p) {
                        continue;
                    }
                    queue.push_back(p.clone());
                }
            }
            acc.insert(idx);
        }
        acc.remove(&Compat(curie));
        acc
    }

    /// Test if `child` is derived from `parent` along a transitive `is_a` relationship
    fn is_child_of(
        &self,
        child: <T::Data as CVData>::Index,
        parent: <T::Data as CVData>::Index,
    ) -> bool {
        let cv = self;

        let child_term = match cv.get_by_index(child) {
            Some(t) => t,
            None => {
                return false;
            }
        };

        let parent = Compat(parent);
        if child_term.parents().any(|v| *v == parent.0) {
            return true;
        }

        let mut seen = HashSet::new();
        let mut queue = VecDeque::from_iter(child_term.parents().cloned());

        while !queue.is_empty() {
            let index = queue.pop_front().unwrap();
            seen.insert(index.clone());
            if let Some(term) = cv.get_by_index(index) {
                for of in term.parents() {
                    if *of == parent.0 {
                        return true;
                    }
                    if seen.contains(of) {
                        continue;
                    }
                    queue.push_back(of.clone());
                }
            }
        }
        return false;
    }
}

impl CVTraversal<MSVocabulary> for CVIndex<MSVocabulary> {
    fn child_map(
        &self,
    ) -> &HashMap<Compat<<MSTerm as CVData>::Index>, Vec<Compat<<MSTerm as CVData>::Index>>> {
        self.data().child_map()
    }

    fn children_of(
        &self,
        curie: CURIE,
    ) -> Option<&'_ [Compat<<<MSVocabulary as CVSource>::Data as CVData>::Index>]> {
        let state = self.data();
        state.child_map().get(&Compat(curie)).map(|v| &**v)
    }

    fn parents_of(
        &self,
        curie: CURIE,
    ) -> Option<HashSet<Compat<<<MSVocabulary as CVSource>::Data as CVData>::Index>>> {
        self.get_by_index(&curie)
            .as_ref()
            .map(|v| v.parents.clone())
    }

    fn get_by_index(
        &self,
        key: <<MSVocabulary as CVSource>::Data as CVData>::Index,
    ) -> Option<Arc<<MSVocabulary as CVSource>::Data>> {
        self.get_by_index(&key)
    }
}