File size: 1,125 Bytes
c289d87 | 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 | from __future__ import annotations
import random
from typing import Dict, Iterable, List
def cluster_naive_order(
ligand_ids: Iterable[str],
cluster_map: Dict[str, int],
seed: int,
) -> List[str]:
"""
Static cluster baseline ordering:
1) one representative per cluster (cluster round-robin),
2) then remaining members cluster-by-cluster.
"""
ids = [str(x) for x in ligand_ids]
by_cluster: Dict[int, List[str]] = {}
for lid in ids:
by_cluster.setdefault(int(cluster_map.get(lid, -1)), []).append(lid)
rng = random.Random(int(seed))
cluster_ids = sorted(by_cluster.keys())
rng.shuffle(cluster_ids)
for cid in cluster_ids:
rng.shuffle(by_cluster[cid])
order: List[str] = []
for cid in cluster_ids:
if by_cluster[cid]:
order.append(by_cluster[cid][0])
for cid in cluster_ids:
members = by_cluster[cid][1:]
order.extend(members)
seen = set()
out: List[str] = []
for lid in order:
if lid in seen:
continue
seen.add(lid)
out.append(lid)
return out
|