# madbot_cli.associations

*module*

The data associations of a node: paired reads, an assembly and its reads, others.

A node of the deposit file gives ``associations``, a list of rules. Each rule
makes associations of one type from the data of the node:

- ``pattern``: the data whose name matches, grouped by the ``group`` of the
  pattern, one association per group of two data or more;
- ``list``: associations given one by one, each by its ``name`` and the names
  of its data;
- ``suggested: true``: the associations madbot suggests, as the web
  application shows them.

An association is identified by its data: the one of madbot holding exactly
the data of a group is that group's. The fields of its type (`TYPE_FIELDS`)
take their values from the ``metadata`` of the rule, and per group from
``by_group``. Apply creates the associations and writes those values; it never
changes the type of an association, nor removes one.

[View source](https://gitlab.com/ifb-elixirfr/madbot/madbot/-/blob/c7108faae5e7dd06591be53c8d5bd5e75d880ebe/cli/madbot_cli/associations.py#L1-1)

## madbot_cli.associations.ASSOCIATION_TYPES

*attribute* · *module attribute*

```python
ASSOCIATION_TYPES = ('paired', 'assembly', 'other')
```

The types of an association.

[View source](https://gitlab.com/ifb-elixirfr/madbot/madbot/-/blob/c7108faae5e7dd06591be53c8d5bd5e75d880ebe/cli/madbot_cli/associations.py#L35-35)

## madbot_cli.associations.CREATE

*attribute* · *module attribute*

```python
CREATE = 'create'
```

[View source](https://gitlab.com/ifb-elixirfr/madbot/madbot/-/blob/c7108faae5e7dd06591be53c8d5bd5e75d880ebe/cli/madbot_cli/associations.py#L56-56)

## madbot_cli.associations.TYPE_FIELDS

*attribute* · *module attribute*

```python
TYPE_FIELDS: dict[str, tuple[str, ...]] = {'paired': ('library_layout'), 'assembly': ('assembly'), 'other': ()}
```

The madbot metadata fields of each type of association, by slug.

As the API binds them (``DataAssociation.TYPE_FIELD_SLUGS``): madbot gives
every association of a type these fields, and they are the only ones a file
fills on an association.

[View source](https://gitlab.com/ifb-elixirfr/madbot/madbot/-/blob/c7108faae5e7dd06591be53c8d5bd5e75d880ebe/cli/madbot_cli/associations.py#L38-42)

## madbot_cli.associations.UNCHANGED

*attribute* · *module attribute*

```python
UNCHANGED = 'unchanged'
```

[View source](https://gitlab.com/ifb-elixirfr/madbot/madbot/-/blob/c7108faae5e7dd06591be53c8d5bd5e75d880ebe/cli/madbot_cli/associations.py#L58-58)

## madbot_cli.associations.UPDATE

*attribute* · *module attribute*

```python
UPDATE = 'update'
```

[View source](https://gitlab.com/ifb-elixirfr/madbot/madbot/-/blob/c7108faae5e7dd06591be53c8d5bd5e75d880ebe/cli/madbot_cli/associations.py#L57-57)

## madbot_cli.associations.Candidates

*class*

```python
class Candidates
```

The data of a node an association can take, linked or about to be.

``source`` is the key of the source of a data still to link; the
connection of a source, by key, is found in ``connections`` once it
exists.

[View source](https://gitlab.com/ifb-elixirfr/madbot/madbot/-/blob/c7108faae5e7dd06591be53c8d5bd5e75d880ebe/cli/madbot_cli/associations.py#L411-447)

### madbot_cli.associations.Candidates.connections

*attribute* · *instance attribute*

```python
connections = connections
```

[View source](https://gitlab.com/ifb-elixirfr/madbot/madbot/-/blob/c7108faae5e7dd06591be53c8d5bd5e75d880ebe/cli/madbot_cli/associations.py#L425-425)

### madbot_cli.associations.Candidates.items

*attribute* · *instance attribute*

```python
items = sorted(({: item} for item in items), key=lambda item: item['name'])
```

[View source](https://gitlab.com/ifb-elixirfr/madbot/madbot/-/blob/c7108faae5e7dd06591be53c8d5bd5e75d880ebe/cli/madbot_cli/associations.py#L424-424)

### madbot_cli.associations.Candidates.by_name

*method*

```python
def by_name(name: str, key: str | None) -> list[dict]
```

Return the data of a name, of one source of the file when given.

[View source](https://gitlab.com/ifb-elixirfr/madbot/madbot/-/blob/c7108faae5e7dd06591be53c8d5bd5e75d880ebe/cli/madbot_cli/associations.py#L441-447)

### madbot_cli.associations.Candidates.in_source

*method*

```python
def in_source(item: Mapping, key: str) -> bool
```

Say whether a data comes from a source of the file.

[View source](https://gitlab.com/ifb-elixirfr/madbot/madbot/-/blob/c7108faae5e7dd06591be53c8d5bd5e75d880ebe/cli/madbot_cli/associations.py#L427-432)

### madbot_cli.associations.Candidates.of_sources

*method*

```python
def of_sources(keys: Iterable[str] | None) -> list[dict]
```

Return the data of some sources of the file; all when None.

[View source](https://gitlab.com/ifb-elixirfr/madbot/madbot/-/blob/c7108faae5e7dd06591be53c8d5bd5e75d880ebe/cli/madbot_cli/associations.py#L434-439)

## madbot_cli.associations.apply_associations

*function*

```python
def apply_associations(client: Client, plan: Mapping, fields: Mapping[str, Mapping], log: op.Log = _silent) -> dict
```

Create the associations of a plan and write the values of their fields.

A failure is reported and the next association is handled.

**Parameters**

- `client` (`Client`) — The client, with its workspace set.
- `plan` (`mapping`) — The plan made by `plan_associations`, every data linked.
- `fields` (`mapping`) — The madbot fields, by slug.
- `log` (`callable`) (default: `_silent`) — Called with a message for each change; the failures are returned.

**Returns**

- (`dict`) — ``created`` and ``updated`` counts, and the ``failures``, each ``{association, error}``.

[View source](https://gitlab.com/ifb-elixirfr/madbot/madbot/-/blob/c7108faae5e7dd06591be53c8d5bd5e75d880ebe/cli/madbot_cli/associations.py#L850-914)

## madbot_cli.associations.check_value

*function*

```python
def check_value(schema: Mapping | None, value: Any) -> str | None
```

Check a value against the schema of its field.

**Parameters**

- `schema` (`mapping or None`) — The JSON Schema of the field; no check when None.
- `value` (`Any`) — The value; None when there is none.

**Returns**

- (`str or None`) — What is wrong, None when the value is valid.

[View source](https://gitlab.com/ifb-elixirfr/madbot/madbot/-/blob/c7108faae5e7dd06591be53c8d5bd5e75d880ebe/cli/madbot_cli/associations.py#L382-408)

## madbot_cli.associations.create_association

*function*

```python
def create_association(client: Client, kind: str, data_ids: list[str]) -> dict
```

Create an association of data.

**Parameters**

- `client` (`Client`) — The client, with its workspace set.
- `kind` (`str`) — Its type, one of `ASSOCIATION_TYPES`.
- `data_ids` (`list of str`) — Its data, two or more.

**Returns**

- (`dict`) — The association. madbot answers with the one already holding exactly these data when there is one, its type left as it is.

[View source](https://gitlab.com/ifb-elixirfr/madbot/madbot/-/blob/c7108faae5e7dd06591be53c8d5bd5e75d880ebe/cli/madbot_cli/associations.py#L235-257)

## madbot_cli.associations.field_slugs

*function*

```python
def field_slugs(rules: Iterable[Mapping]) -> set[str]
```

List the fields the associations of some rules may have.

**Parameters**

- `rules` (`iterable of mapping`) — The ``associations`` of a node.

**Returns**

- (`set of str`) — The slugs of the fields of their types.

[View source](https://gitlab.com/ifb-elixirfr/madbot/madbot/-/blob/c7108faae5e7dd06591be53c8d5bd5e75d880ebe/cli/madbot_cli/associations.py#L825-844)

## madbot_cli.associations.group_by_pattern

*function*

```python
def group_by_pattern(items: Iterable[Mapping], pattern: str) -> tuple[OrderedDict[str, list[dict]], list[str]]
```

Group data by the ``group`` of a pattern searched in their name.

**Parameters**

- `items` (`iterable of mapping`) — The data, each with its ``name``.
- `pattern` (`str`) — A regular expression with a ``group`` group.

**Returns**

- (`tuple`) — The data of each group, by group, in the order of the names; and the names that match no group.

[View source](https://gitlab.com/ifb-elixirfr/madbot/madbot/-/blob/c7108faae5e7dd06591be53c8d5bd5e75d880ebe/cli/madbot_cli/associations.py#L450-479)

## madbot_cli.associations.label_of

*function*

```python
def label_of(association: Mapping) -> str
```

Name an association for a person.

**Parameters**

- `association` (`mapping`) — A planned association, or one of madbot.

**Returns**

- (`str`) — Its type, its group when it has one, and its data.

[View source](https://gitlab.com/ifb-elixirfr/madbot/madbot/-/blob/c7108faae5e7dd06591be53c8d5bd5e75d880ebe/cli/madbot_cli/associations.py#L296-311)

## madbot_cli.associations.list_association_metadata

*function*

```python
def list_association_metadata(client: Client, data_id: str, association_id: str) -> dict[str, dict]
```

Read the metadata of an association, by field slug.

**Parameters**

- `client` (`Client`) — The client, with its workspace set.
- `data_id` (`str`) — A data of the association.
- `association_id` (`str`) — The association.

**Returns**

- (`dict`) — For each field slug, ``{id, field, value}``: the metadata, the identifier of the field and the value of the association, None when empty.

[View source](https://gitlab.com/ifb-elixirfr/madbot/madbot/-/blob/c7108faae5e7dd06591be53c8d5bd5e75d880ebe/cli/madbot_cli/associations.py#L167-199)

## madbot_cli.associations.list_data_associations

*function*

```python
def list_data_associations(client: Client, data_id: str, node_id: str) -> list[dict]
```

List the associations of a data that hold data of a node.

**Parameters**

- `client` (`Client`) — The client, with its workspace set.
- `data_id` (`str`) — The data.
- `node_id` (`str`) — The node.

**Returns**

- (`list of dict`) — The associations as the API lists them.

[View source](https://gitlab.com/ifb-elixirfr/madbot/madbot/-/blob/c7108faae5e7dd06591be53c8d5bd5e75d880ebe/cli/madbot_cli/associations.py#L64-82)

## madbot_cli.associations.list_node_associations

*function*

```python
def list_node_associations(client: Client, node_id: str, datalinks: Iterable[Mapping]) -> list[dict]
```

List the associations of the data of a node, each once.

**Parameters**

- `client` (`Client`) — The client, with its workspace set.
- `node_id` (`str`) — The node.
- `datalinks` (`iterable of mapping`) — The datalinks of the node.

**Returns**

- (`list of dict`) — Each association as ``{id, type, data, names, members}``: its identifier, its type, the identifiers of its data as a frozenset, their names, and each data as ``{name, data}``, by name.

[View source](https://gitlab.com/ifb-elixirfr/madbot/madbot/-/blob/c7108faae5e7dd06591be53c8d5bd5e75d880ebe/cli/madbot_cli/associations.py#L85-129)

## madbot_cli.associations.list_suggestions

*function*

```python
def list_suggestions(client: Client, node_id: str) -> list[dict]
```

List the associations madbot suggests for the data of a node.

The data already together in an association are left out by madbot.

**Parameters**

- `client` (`Client`) — The client, with its workspace set.
- `node_id` (`str`) — The node.

**Returns**

- (`list of dict`) — Each suggestion as ``{type, data: [{id, name, datalink}]}``.

[View source](https://gitlab.com/ifb-elixirfr/madbot/madbot/-/blob/c7108faae5e7dd06591be53c8d5bd5e75d880ebe/cli/madbot_cli/associations.py#L132-164)

## madbot_cli.associations.merged_values

*function*

```python
def merged_values(rule: Mapping, group: str | None) -> dict[str, Any]
```

Return the values a rule gives to the fields of one of its associations.

**Parameters**

- `rule` (`mapping`) — The rule, from the file.
- `group` (`str or None`) — The group of the association.

**Returns**

- (`dict`) — By field slug: the value of ``metadata``, completed by the one of ``by_group`` for the group (key by key when both are objects).

[View source](https://gitlab.com/ifb-elixirfr/madbot/madbot/-/blob/c7108faae5e7dd06591be53c8d5bd5e75d880ebe/cli/madbot_cli/associations.py#L314-338)

## madbot_cli.associations.plan_associations

*function*

```python
def plan_associations(label: str, rules: list[Mapping], candidates: Iterable[Mapping], existing: list[dict], suggestions: list[dict], connections: Mapping[str, Mapping], fields: Mapping[str, Mapping], current: Mapping[str, Mapping[str, Mapping]] | None = None, complete: bool = True) -> dict
```

Plan the associations of a node from the rules of the file.

Nothing is read nor written: the inputs come from the API.

**Parameters**

- `label` (`str`) — The path of the node, for the messages.
- `rules` (`list of mapping`) — The ``associations`` of the node in the file.
- `candidates` (`iterable of mapping`) — The data of the node, each ``{name, data, connection, source?}``; ``data`` is None for a data still to link, which gives the key of its ``source``.
- `existing` (`list of dict`) — The associations of madbot, as `list_node_associations` gives them.
- `suggestions` (`list of dict`) — The suggestions of madbot, as `list_suggestions` gives them.
- `connections` (`mapping`) — The plan of the connection of each source of the file, by key.
- `fields` (`mapping`) — The madbot fields of the types, by slug.
- `current` (`mapping`) (default: `None`) — The metadata of each existing association, by its identifier.
- `complete` (`bool`) (default: `True`) — Whether every data of the node is known; when not, a name of a ``list`` not found yet waits for the data instead of being an error.

**Returns**

- (`dict`) — ``later``: some associations wait for data still to be found; ``associations``: each planned one, ``{rule, type, group, names, data, id, exists, action, values, writes, missing}``; ``untracked``: the associations of madbot no rule designates; ``ignored``: the names no pattern matches; ``errors`` and ``warnings``.

[View source](https://gitlab.com/ifb-elixirfr/madbot/madbot/-/blob/c7108faae5e7dd06591be53c8d5bd5e75d880ebe/cli/madbot_cli/associations.py#L752-822)

## madbot_cli.associations.plan_values

*function*

```python
def plan_values(entry: dict, current: Mapping[str, Mapping], fields: Mapping[str, Mapping]) -> None
```

Plan the values of the fields of one association, in place.

**Parameters**

- `entry` (`dict`) — A planned association, with its ``values`` from the file.
- `current` (`mapping`) — Its metadata in madbot, by field slug, as `list_association_metadata` gives them; empty when it does not exist yet.
- `fields` (`mapping`) — The madbot fields, by slug.
- `Only`
- `file`

[View source](https://gitlab.com/ifb-elixirfr/madbot/madbot/-/blob/c7108faae5e7dd06591be53c8d5bd5e75d880ebe/cli/madbot_cli/associations.py#L710-749)

## madbot_cli.associations.read_fields

*function*

```python
def read_fields(client: Client, slugs: Iterable[str]) -> dict[str, dict]
```

Read the madbot fields of some slugs, each with its JSON Schema.

**Parameters**

- `client` (`Client`) — The client.
- `slugs` (`iterable of str`) — The slugs of the fields.

**Returns**

- (`dict`) — Each field as the API gives it, by slug, its ``schema`` read from its address; the fields the instance does not have are left out.

[View source](https://gitlab.com/ifb-elixirfr/madbot/madbot/-/blob/c7108faae5e7dd06591be53c8d5bd5e75d880ebe/cli/madbot_cli/associations.py#L202-229)

## madbot_cli.associations.set_association_metadata

*function*

```python
def set_association_metadata(client: Client, data_id: str, association_id: str, field_id: str, value: Any, metadata_id: str | None = None) -> None
```

Give a value to a field of an association.

**Parameters**

- `client` (`Client`) — The client, with its workspace set.
- `data_id` (`str`) — A data of the association.
- `association_id` (`str`) — The association.
- `field_id` (`str`) — The madbot metadata field.
- `value` (`Any`) — The value.
- `metadata_id` (`str`) (default: `None`) — The metadata of the association for that field, when it has one.

[View source](https://gitlab.com/ifb-elixirfr/madbot/madbot/-/blob/c7108faae5e7dd06591be53c8d5bd5e75d880ebe/cli/madbot_cli/associations.py#L260-290)

## madbot_cli.associations.variant_of

*function*

```python
def variant_of(schema: Mapping, value: Any) -> Mapping
```

Choose the variant of a ``oneOf`` schema a value names by a constant.

The value of a library layout names its variant by ``library_layout:
PAIRED``: its errors are those of that variant, not of the others.

**Parameters**

- `schema` (`mapping`) — A JSON Schema.
- `value` (`Any`) — A value.

**Returns**

- (`mapping`) — The schema with the variant the value names in place of ``oneOf``; the schema itself when no variant or several match.

[View source](https://gitlab.com/ifb-elixirfr/madbot/madbot/-/blob/c7108faae5e7dd06591be53c8d5bd5e75d880ebe/cli/madbot_cli/associations.py#L341-379)
