Data Models
nplinker.scoring
¶
LinkGraph
¶
Class to represent the links between objects in NPLinker.
This class wraps the networkx.Graph
class to provide a more user-friendly interface for
working with the links.
The links between objects are stored as edges in a graph, while the objects themselves are stored as nodes.
The scoring data for each link (or link data) is stored as the key/value attributes of the edge.
Examples:
Create a LinkGraph object:
Display the empty LinkGraph object:
>>> lg
| | Object 1 | Object 2 | Metcalf Score | Rosetta Score |
|----|------------|------------|-----------------|-----------------|
Add a link between a GCF and a Spectrum object:
Display all links in LinkGraph object:
>>> lg
| | Object 1 | Object 2 | Metcalf Score | Rosetta Score |
|----|--------------|------------------------|-----------------|-----------------|
| 1 | GCF(id=gcf1) | Spectrum(id=spectrum1) | 1 | - |
Get all links for a given object:
Get all links in the LinkGraph:
Check if there is a link between two objects:
Get the link data between two objects:
Source code in src/nplinker/scoring/link_graph.py
links
property
¶
links: list[LINK]
Get all links.
Returns:
-
list[LINK]
–A list of tuples containing the links between objects.
Examples:
__getitem__
¶
__getitem__(u: Entity) -> dict[Entity, LINK_DATA]
Get all links for a given object.
Parameters:
-
u
(Entity
) –the given object
Returns:
-
dict[Entity, LINK_DATA]
–A dictionary of links for the given object.
Raises:
-
KeyError
–if the input object is not found in the link graph.
Source code in src/nplinker/scoring/link_graph.py
add_link
¶
add_link(u: Entity, v: Entity, **data: Score) -> None
Add a link between two objects.
The objects u
and v
must be different types, i.e. one must be a GCF and the other must be
a Spectrum or MolecularFamily.
Parameters:
-
u
(Entity
) –the first object, either a GCF, Spectrum, or MolecularFamily
-
v
(Entity
) –the second object, either a GCF, Spectrum, or MolecularFamily
-
data
(Score
, default:{}
) –keyword arguments. At least one scoring method and its data must be provided. The key must be the name of the scoring method defined in
ScoringMethod
, and the value is aScore
object, e.g.metcalf=Score("metcalf", 1.0, {"cutoff": 0.5})
.
Examples:
Source code in src/nplinker/scoring/link_graph.py
has_link
¶
has_link(u: Entity, v: Entity) -> bool
Check if there is a link between two objects.
Parameters:
-
u
(Entity
) –the first object, either a GCF, Spectrum, or MolecularFamily
-
v
(Entity
) –the second object, either a GCF, Spectrum, or MolecularFamily
Returns:
-
bool
–True if there is a link between the two objects, False otherwise
Examples:
Source code in src/nplinker/scoring/link_graph.py
get_link_data
¶
Get the data for a link between two objects.
Parameters:
-
u
(Entity
) –the first object, either a GCF, Spectrum, or MolecularFamily
-
v
(Entity
) –the second object, either a GCF, Spectrum, or MolecularFamily
Returns:
-
LINK_DATA | None
–A dictionary of scoring methods and their data for the link between the two objects, or
-
LINK_DATA | None
–None if there is no link between the two objects.
Examples:
Source code in src/nplinker/scoring/link_graph.py
filter
¶
Return a new LinkGraph object with the filtered links between the given objects.
The new LinkGraph object will only contain the links between u_nodes
and v_nodes
.
If u_nodes
or v_nodes
is empty, the new LinkGraph object will contain the links for
the given objects in v_nodes
or u_nodes
, respectively. If both are empty, return an
empty LinkGraph object.
Note that not all objects in u_nodes
and v_nodes
need to be present in the original
LinkGraph.
Parameters:
-
u_nodes
(Sequence[Entity]
) –a sequence of objects used as the first object in the links
-
v_nodes
(Sequence[Entity]
, default:[]
) –a sequence of objects used as the second object in the links
Returns:
-
LinkGraph
–A new LinkGraph object with the filtered links between the given objects.
Examples:
Filter the links for gcf1
and gcf2
:
>>> new_lg = lg.filter([gcf1, gcf2])
Filter the links for `spectrum1` and `spectrum2`:
>>> new_lg = lg.filter([spectrum1, spectrum2])
Filter the links between two lists of objects:
>>> new_lg = lg.filter([gcf1, gcf2], [spectrum1, spectrum2])
Source code in src/nplinker/scoring/link_graph.py
Score
dataclass
¶
A data class to represent score data.
Attributes:
-
name
(str
) –the name of the scoring method. See
ScoringMethod
for valid values. -
value
(float
) –the score value.
-
parameter
(dict
) –the parameters used for the scoring method.
__post_init__
¶
Check if the value of name
is valid.
Raises:
-
ValueError
–if the value of
name
is not valid.