#!/usr/bin/env python3 """How often a sequence's nearest neighbours share its label, per rank, for one or more feature tables. Each table goes through fastsne's own preprocessing (optional sqrt, centre, PCA to --pca components), then exact Euclidean k-NN. Prints, per table, the mean fraction of the k neighbours that share the query's domain / phylum / class / order / family / genus (queries with an empty label at that rank are skipped). usage: knn_eval.py OUT.tax.tsv a.fsb[:sqrt|:z] b.fsb ... [--k 15 --pca 128] """ import argparse, struct import numpy as np ap = argparse.ArgumentParser() ap.add_argument("tax"); ap.add_argument("tables", nargs="+") ap.add_argument("--k", type=int, default=15); ap.add_argument("--pca", type=int, default=128) a = ap.parse_args() rows = [l.rstrip("\n").split("\t") for l in open(a.tax)][1:] taxid = {r[0]: r for r in rows} ranks = {"domain": 1, "phylum": 3, "class": 4, "order": 5, "family": 6, "genus": 7} def read_fsb(path): with open(path, "rb") as f: magic, d, n = f.read(4), *struct.unpack("