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
//! Loading a knowledge graph, and reading off which sources built it.
//!
//! **The silent failure this catches.** A knowledge graph is the first kind
//! in Ligase that is *assembled* rather than measured. What it holds is a
//! function of which evidence sources were folded into it: add DisGeNET to a
//! graph that had Open Targets and a literature-mined set, and gene–disease
//! edges appear, neighbourhoods widen, and every score derived by
//! propagation or centrality moves. Nothing in the resulting file says which
//! sources went in. So two graphs that differ *only* in their source
//! composition diff, score and join against each other without complaint,
//! and the difference between them reads as a biological or methodological
//! result. A benchmark scored against a graph built from a different source
//! list measures the assembly rather than the method, and comes out
//! plausible. An edge present in one and absent in the other reads as
//! evidence of absence when it is the absence of a source.
//!
//! So the sources are a dimension — `source_set` — and this is the loader
//! that fills it in. That is what admits the kind under
//! [D35](https://ligase.neoforgelabs.tech/project/decisions/): it brings an
//! axis no other kind brings, whose mismatch is silent, plausible, and
//! attributable to biology by a reader who cannot see it.
//!
//! # The sources are read, not asked for
//!
//! They are on the edges, in the per-edge provenance a graph carries, so
//! refusing to read them would be the engine asking for something it is
//! already holding (D44). A declaration still wins where there is one, for
//! the reason a named list's namespace declaration wins: a declaration is a
//! user telling the engine something it cannot read, and a graph's own
//! `sources` may name a mirror, a shard or an internal pipeline stage rather
//! than the source a reader would recognise.
//!
//! An inference that comes back empty is a **gap**, never an empty set. Two
//! graphs that each recorded no sources would otherwise *agree* on the axis,
//! which is exactly the silence the dimension exists to break — the rule
//! [`NameSet`](crate::dim::NameSet) states and [`super::organism`] already
//! holds for an empty organism declaration.
//!
//! # What this loader does not do
//!
//! Read anybody else's graph format. Open Targets' Parquet, Monarch's dumps,
//! and Neo4j and RDF exports are all outside it: what is read is the
//! node/edge JSON described on
//! [the guide](https://ligase.neoforgelabs.tech/guide/knowledge-graphs/).
//! Defining a shape is not what earns a kind its place, and this one is the
//! cheapest envelope that can carry per-edge provenance.
use Path;
use GraphReader;
use ;
use crate;
use crate;
/// Why the organism is unknown: a graph does not record it.
const NO_ORGANISM: &str = "a knowledge graph does not record which organisms its entities \
belong to";
/// Why the subject is unknown.
const NO_SUBJECT: &str = "a knowledge graph does not record what it is a graph of";
/// Why the release is unknown.
const NO_RELEASE: &str = "a knowledge graph does not record which release its sources were \
drawn from";
/// Why the source set is unknown when the graph named none.
const NO_SOURCES: &str = "neither the graph nor any of its nodes and edges names a source -- \
declare source_set";
/// Why the namespace is unknown when the node ids did not settle it.
///
/// Deliberately different advice from a gene list's. A graph of genes and
/// diseases names its entities in two spaces by design, so "the ids do not
/// all match one pattern" is the *normal* case here rather than a sign of
/// something wrong, and the sentence says so instead of implying a fault.
const NOT_INFERRED: &str = "not declared, and the node ids do not all match one namespace's \
pattern -- which is ordinary for a graph holding more than one \
kind of entity; declare identifier_namespace";
/// Load a knowledge graph from JSON.
///
/// Nodes and edges are read, their `sources` lists unioned into the
/// `source_set` dimension, and the identifier namespace read off the node
/// ids where they all agree. Everything else a node or an edge says is
/// carried without being read.
///
/// # Errors
///
/// [`LoadError::Io`] if the file cannot be read, and [`LoadError::Format`]
/// if it is not JSON, has no `nodes` or `edges` array, or holds a node with
/// no `id` or an edge with no `source`, `target` or `type`.
/// The namespace dimension: declared if stated, else read off the node ids.
///
/// The same inference a named list gets, over the ids an edge can point at.
/// A node's other `ids` are deliberately not consulted: they are the names
/// the entity *also* has, in whatever spaces somebody recorded, and feeding
/// them to inference would guarantee a mixed set and so guarantee a decline.