Skip to content

Quickstart

This quickstart shows how to create a graph, work with slices and annotations, add hyperedges, and run an algorithm through a backend interoperability accessor.

Prerequisite: install annnet via the Installation guide (add extras like networkx if you want backend algorithm interoperability).

For exact APIs used below, see AnnNet, Slices, NetworkX adapter, and Native .annnet format.

Build a small graph

import annnet as an

G = an.AnnNet(directed=True)
G.slices.add_slice("toy")
G.slices.active = "toy"

for v in ["A", "B", "C", "D"]:
    G.add_vertices(v, label=v, kind="gene")

# Binary edges (directed + undirected)
G.add_edges("A", "B", weight=2.0, directed=True, relation="activates")
G.add_edges("B", "C", weight=1.0, directed=False, relation="binds")

# Hyperedge (directed head→tail)
G.add_edges(["A", "B"], ["C", "D"], weight=1.0, directed=True)

Run an algorithm (NetworkX)

# Requires networkx installed
deg = G.nx.degree_centrality(G)

G.nx is a lazy graph-owned accessor. It converts G to a NetworkX graph on demand, replaces the G argument with that backend graph for the algorithm call, and returns the NetworkX result. The same pattern exists for G.ig and G.gt when those optional backends are installed.

You can fetch a concrete NetworkX graph with options:

nxG = G.nx.backend(
    directed=True,
    hyperedge_mode="skip",  # or "expand"
    slice="toy",
    simple=True,            # collapse multiedges
)

See also Interoperability and the NetworkX adapter reference.

Convert and save

import annnet as an

# File formats
an.io.to_graphml(G, "graph.gml", directed=True, hyperedge_mode="reify")

# Lossless storage
an.io.write(G, "my_graph.annnet", overwrite=True)
R = an.io.read("my_graph.annnet")

See the GraphML and GEXF reference and the Native .annnet format reference.

Next step