graphannis-core 4.1.2

This crate supports graph representation and generic query-functionality.
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
use super::*;
use crate::{
    graph::storage::{WriteableGraphStorage, adjacencylist::AdjacencyListStorage},
    types::{AnnoKey, Annotation},
};
use pretty_assertions::assert_eq;

/// Creates an example graph storage with the folllowing structure:
///
/// ```
/// 0   1   2   3  4    5
///  \ /     \ /    \  /
///   6       7       8
///   |       |       |
///   9      10      11
///    \      |      /
///     \     |     /
///      \    |    /
///       \   |   /
///        \  |  /
///           12
///   
/// ```
fn create_topdown_gs() -> Result<AdjacencyListStorage> {
    let mut orig = AdjacencyListStorage::new();

    // First layer
    orig.add_edge((0, 6).into())?;
    orig.add_edge((1, 6).into())?;
    orig.add_edge((2, 7).into())?;
    orig.add_edge((3, 7).into())?;
    orig.add_edge((4, 8).into())?;
    orig.add_edge((5, 8).into())?;

    // Second layer
    orig.add_edge((6, 9).into())?;
    orig.add_edge((7, 10).into())?;
    orig.add_edge((8, 11).into())?;

    // Third layer
    orig.add_edge((9, 12).into())?;
    orig.add_edge((10, 12).into())?;
    orig.add_edge((11, 12).into())?;

    // Add annotations to last layer
    let key = AnnoKey {
        name: "example".into(),
        ns: "default_ns".into(),
    };
    let anno = Annotation {
        key,
        val: "last".into(),
    };
    orig.add_edge_annotation((9, 12).into(), anno.clone())?;
    orig.add_edge_annotation((10, 12).into(), anno.clone())?;
    orig.add_edge_annotation((11, 12).into(), anno.clone())?;

    Ok(orig)
}

#[test]
fn test_source_nodes() {
    // Create an example graph storage to copy the value from
    let node_annos = AnnoStorageImpl::new(None).unwrap();
    let orig = create_topdown_gs().unwrap();
    let mut target = DiskPathStorage::new().unwrap();
    target.copy(&node_annos, &orig).unwrap();

    let result: Result<Vec<_>> = target.source_nodes().collect();
    let mut result = result.unwrap();
    result.sort();

    assert_eq!(vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], result);
}

#[test]
fn test_outgoing_edges() {
    // Create an example graph storage to copy the value from
    let node_annos = AnnoStorageImpl::new(None).unwrap();
    let orig = create_topdown_gs().unwrap();
    let mut target = DiskPathStorage::new().unwrap();
    target.copy(&node_annos, &orig).unwrap();

    let result: Result<Vec<_>> = target.get_outgoing_edges(0).collect();
    assert_eq!(vec![6], result.unwrap());

    let result: Result<Vec<_>> = target.get_outgoing_edges(3).collect();
    assert_eq!(vec![7], result.unwrap());

    let result: Result<Vec<_>> = target.get_outgoing_edges(7).collect();
    assert_eq!(vec![10], result.unwrap());

    let result: Result<Vec<_>> = target.get_outgoing_edges(11).collect();
    assert_eq!(vec![12], result.unwrap());

    let result: Result<Vec<_>> = target.get_outgoing_edges(12).collect();
    assert_eq!(0, result.unwrap().len());

    let result: Result<Vec<_>> = target.get_outgoing_edges(100).collect();
    assert_eq!(0, result.unwrap().len());
}

#[test]
fn test_ingoing_edges() {
    // Create an example graph storage to copy the value from
    let node_annos = AnnoStorageImpl::new(None).unwrap();
    let orig = create_topdown_gs().unwrap();
    let mut target = DiskPathStorage::new().unwrap();
    target.copy(&node_annos, &orig).unwrap();

    let result: Result<Vec<_>> = target.get_ingoing_edges(12).collect();
    let mut result = result.unwrap();
    result.sort();
    assert_eq!(vec![9, 10, 11], result);

    let result: Result<Vec<_>> = target.get_ingoing_edges(10).collect();
    let mut result = result.unwrap();
    result.sort();
    assert_eq!(vec![7], result);

    let result: Result<Vec<_>> = target.get_ingoing_edges(8).collect();
    let mut result = result.unwrap();
    result.sort();
    assert_eq!(vec![4, 5], result);

    let result: Result<Vec<_>> = target.get_ingoing_edges(0).collect();
    assert_eq!(0, result.unwrap().len());

    let result: Result<Vec<_>> = target.get_ingoing_edges(1).collect();
    assert_eq!(0, result.unwrap().len());

    let result: Result<Vec<_>> = target.get_ingoing_edges(100).collect();
    assert_eq!(0, result.unwrap().len());
}

#[test]
fn test_path_for_node() {
    // Create an example graph storage to copy the value from
    let node_annos = AnnoStorageImpl::new(None).unwrap();
    let orig = create_topdown_gs().unwrap();
    let mut target = DiskPathStorage::new().unwrap();
    target.copy(&node_annos, &orig).unwrap();

    assert_eq!(vec![6, 9, 12], target.path_for_node(0).unwrap());
    assert_eq!(vec![9, 12], target.path_for_node(6).unwrap());
    assert_eq!(vec![12], target.path_for_node(10).unwrap());

    assert_eq!(vec![7, 10, 12], target.path_for_node(2).unwrap());
    assert_eq!(vec![10, 12], target.path_for_node(7).unwrap());
    assert_eq!(vec![12], target.path_for_node(10).unwrap());

    assert_eq!(0, target.path_for_node(100).unwrap().len());
}

#[test]
fn test_find_connected() {
    // Create an example graph storage to copy the value from
    let node_annos = AnnoStorageImpl::new(None).unwrap();
    let orig = create_topdown_gs().unwrap();
    let mut target = DiskPathStorage::new().unwrap();
    target.copy(&node_annos, &orig).unwrap();

    let result: Result<Vec<_>> = target
        .find_connected(0, 0, std::ops::Bound::Unbounded)
        .collect();
    assert_eq!(vec![0, 6, 9, 12], result.unwrap());

    let result: Result<Vec<_>> = target
        .find_connected(0, 1, std::ops::Bound::Unbounded)
        .collect();
    assert_eq!(vec![6, 9, 12], result.unwrap());

    let result: Result<Vec<_>> = target
        .find_connected(1, 0, std::ops::Bound::Unbounded)
        .collect();
    assert_eq!(vec![1, 6, 9, 12], result.unwrap());

    let result: Result<Vec<_>> = target
        .find_connected(7, 1, std::ops::Bound::Included(2))
        .collect();
    assert_eq!(vec![10, 12], result.unwrap());

    let result: Result<Vec<_>> = target
        .find_connected(7, 1, std::ops::Bound::Included(1))
        .collect();
    assert_eq!(vec![10], result.unwrap());

    let result: Result<Vec<_>> = target
        .find_connected(7, 1, std::ops::Bound::Excluded(1))
        .collect();
    // Excluding distance 1 means there can't be any valid resut
    assert_eq!(0, result.unwrap().len());

    let result: Result<Vec<_>> = target
        .find_connected(10, 1, std::ops::Bound::Unbounded)
        .collect();
    assert_eq!(vec![12], result.unwrap());
}

#[test]
fn test_find_connected_inverse() {
    let node_annos = AnnoStorageImpl::new(None).unwrap();
    let orig = create_topdown_gs().unwrap();
    let mut target = DiskPathStorage::new().unwrap();
    target.copy(&node_annos, &orig).unwrap();

    let result: Result<Vec<_>> = target
        .find_connected_inverse(12, 0, Bound::Unbounded)
        .collect();
    let mut result = result.unwrap();
    result.sort();
    assert_eq!(vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], result);

    let result: Result<Vec<_>> = target
        .find_connected_inverse(12, 1, Bound::Excluded(2))
        .collect();
    let mut result = result.unwrap();
    result.sort();
    assert_eq!(vec![9, 10, 11], result);

    let result: Result<Vec<_>> = target
        .find_connected_inverse(10, 1, Bound::Included(2))
        .collect();
    let mut result = result.unwrap();
    result.sort();
    assert_eq!(vec![2, 3, 7], result);

    let result: Result<Vec<_>> = target
        .find_connected_inverse(12, 3, Bound::Included(3))
        .collect();
    let mut result = result.unwrap();
    result.sort();
    assert_eq!(vec![0, 1, 2, 3, 4, 5], result);
}

#[test]
fn test_distance() {
    let node_annos = AnnoStorageImpl::new(None).unwrap();
    let orig = create_topdown_gs().unwrap();
    let mut target = DiskPathStorage::new().unwrap();
    target.copy(&node_annos, &orig).unwrap();

    assert_eq!(None, target.distance(7, 7).unwrap());
    assert_eq!(None, target.distance(12, 1).unwrap());
    assert_eq!(Some(1), target.distance(0, 6).unwrap());
    assert_eq!(Some(1), target.distance(3, 7).unwrap());
    assert_eq!(Some(1), target.distance(4, 8).unwrap());
    assert_eq!(Some(2), target.distance(4, 11).unwrap());
    assert_eq!(Some(2), target.distance(6, 12).unwrap());
    assert_eq!(Some(3), target.distance(2, 12).unwrap());
    assert_eq!(Some(3), target.distance(3, 12).unwrap());
}

#[test]
fn test_is_connected() {
    let node_annos = AnnoStorageImpl::new(None).unwrap();
    let orig = create_topdown_gs().unwrap();
    let mut target = DiskPathStorage::new().unwrap();
    target.copy(&node_annos, &orig).unwrap();

    assert_eq!(
        false,
        target.is_connected(7, 7, 0, Bound::Unbounded).unwrap()
    );
    assert_eq!(
        false,
        target.is_connected(12, 1, 0, Bound::Unbounded).unwrap()
    );
    assert_eq!(
        true,
        target.is_connected(0, 6, 1, Bound::Included(1)).unwrap()
    );
    assert_eq!(
        true,
        target.is_connected(3, 7, 1, Bound::Excluded(2)).unwrap()
    );
    assert_eq!(
        true,
        target.is_connected(4, 8, 1, Bound::Unbounded).unwrap()
    );
    assert_eq!(
        true,
        target.is_connected(4, 11, 2, Bound::Excluded(4)).unwrap()
    );
    assert_eq!(
        true,
        target.is_connected(6, 12, 1, Bound::Included(2)).unwrap()
    );
    assert_eq!(
        true,
        target.is_connected(2, 12, 3, Bound::Unbounded).unwrap()
    );
    assert_eq!(
        true,
        target.is_connected(3, 12, 3, Bound::Included(3)).unwrap()
    );
}

#[test]
fn test_save_load() {
    // Create an example graph storage to copy the value from
    let node_annos = AnnoStorageImpl::new(None).unwrap();
    let orig = create_topdown_gs().unwrap();
    let mut save_gs = DiskPathStorage::new().unwrap();
    save_gs.copy(&node_annos, &orig).unwrap();

    let tmp_location = tempfile::TempDir::new().unwrap();
    save_gs.save_to(tmp_location.path()).unwrap();

    let new_gs = DiskPathStorage::load_from(tmp_location.path()).unwrap();

    let result: Result<Vec<_>> = new_gs.source_nodes().collect();
    let mut result = result.unwrap();
    result.sort();

    assert_eq!(vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], result);

    assert_eq!(
        0,
        new_gs
            .get_anno_storage()
            .get_annotations_for_item(&Edge {
                source: 0,
                target: 12
            })
            .unwrap()
            .len()
    );
    assert_eq!(
        0,
        new_gs
            .get_anno_storage()
            .get_annotations_for_item(&Edge {
                source: 3,
                target: 12
            })
            .unwrap()
            .len()
    );
    assert_eq!(
        0,
        new_gs
            .get_anno_storage()
            .get_annotations_for_item(&Edge {
                source: 7,
                target: 10
            })
            .unwrap()
            .len()
    );

    for source in 9..=11 {
        let edge_anno = new_gs
            .get_anno_storage()
            .get_annotations_for_item(&(source, 12).into())
            .unwrap();
        assert_eq!(1, edge_anno.len());
        assert_eq!("default_ns", edge_anno[0].key.ns);
        assert_eq!("example", edge_anno[0].key.name);
        assert_eq!("last", edge_anno[0].val);
    }
}

#[test]
fn test_save_load_same_location() {
    let node_annos = AnnoStorageImpl::new(None).unwrap();
    let orig = create_topdown_gs().unwrap();
    let mut save_gs = DiskPathStorage::new().unwrap();
    save_gs.copy(&node_annos, &orig).unwrap();

    let tmp_location = tempfile::TempDir::new().unwrap();
    save_gs.save_to(tmp_location.path()).unwrap();

    let tmp_gs = DiskPathStorage::load_from(tmp_location.path()).unwrap();
    tmp_gs.save_to(tmp_location.path()).unwrap();

    let new_gs = DiskPathStorage::load_from(tmp_location.path()).unwrap();

    let result: Result<Vec<_>> = new_gs.source_nodes().collect();
    let mut result = result.unwrap();
    result.sort();

    assert_eq!(vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], result);

    assert_eq!(
        0,
        new_gs
            .get_anno_storage()
            .get_annotations_for_item(&Edge {
                source: 0,
                target: 12
            })
            .unwrap()
            .len()
    );
    assert_eq!(
        0,
        new_gs
            .get_anno_storage()
            .get_annotations_for_item(&Edge {
                source: 3,
                target: 12
            })
            .unwrap()
            .len()
    );
    assert_eq!(
        0,
        new_gs
            .get_anno_storage()
            .get_annotations_for_item(&Edge {
                source: 7,
                target: 10
            })
            .unwrap()
            .len()
    );

    for source in 9..=11 {
        let edge_anno = new_gs
            .get_anno_storage()
            .get_annotations_for_item(&(source, 12).into())
            .unwrap();
        assert_eq!(1, edge_anno.len());
        assert_eq!("default_ns", edge_anno[0].key.ns);
        assert_eq!("example", edge_anno[0].key.name);
        assert_eq!("last", edge_anno[0].val);
    }
}

#[test]
fn test_has_ingoing_edges() {
    let node_annos = AnnoStorageImpl::new(None).unwrap();
    let orig = create_topdown_gs().unwrap();
    let mut target = DiskPathStorage::new().unwrap();
    target.copy(&node_annos, &orig).unwrap();

    // Test first layer
    for n in 0..=5 {
        assert_eq!(false, target.has_ingoing_edges(n).unwrap());
    }
    // Test all other nodes
    for n in 6..=12 {
        assert_eq!(true, target.has_ingoing_edges(n).unwrap());
    }
    // Test some non-existing nodes
    assert_eq!(false, target.has_ingoing_edges(123).unwrap());
    assert_eq!(false, target.has_ingoing_edges(2048).unwrap());
}