Source code

Download source archive ↓

src/biom.py

1"""Checked in-memory BIOM 1.0 table transformations for the Galaxy BIOM wrappers."""
2
3import math
4
5from contract import lines, text
6from tables import json_lines, parse_json
7
8
9def checked_ids(records):
10    if not isinstance(records, list) or len(records) > 256 or any(
11            not isinstance(r, dict) or not isinstance(r.get("id"), str)
12            or not r["id"] or len(r["id"]) > 256 or any(c in r["id"] for c in "\t\r\n")
13            for r in records):
14        raise ValueError("invalid BIOM axis records")
15    if len({r["id"] for r in records}) != len(records):
16        raise ValueError("BIOM axis identifiers must be unique")
17    return records
18
19
20def values(data):
21    table = parse_json(text(data))
22    if not isinstance(table, dict) or table.get("format") not in ("Biological Observation Matrix 1.0",
23                                                              "Biological Observation Matrix 1.0.0"):
24        raise ValueError("expected a BIOM 1.0 JSON object")
25    rows, columns = checked_ids(table.get("rows")), checked_ids(table.get("columns"))
26    h, w = len(rows), len(columns)
27    if table.get("shape") != [h, w]:
28        raise ValueError("BIOM table shape and axis lengths differ")
29    entries = table.get("data")
30
31    def count(n):
32        if type(n) not in (int, float) or not 0 <= n <= 1e100 or not math.isfinite(n):
33            raise ValueError("invalid BIOM abundance")
34        return n
35
36    if table.get("matrix_type") == "dense":
37        if not isinstance(entries, list) or len(entries) != h or any(
38                not isinstance(row, list) or len(row) != w for row in entries):
39            raise ValueError("invalid dense BIOM table")
40        return table, [[count(n) for n in row] for row in entries]
41    if table.get("matrix_type") != "sparse" or not isinstance(entries, list):
42        raise ValueError("matrix_type must be dense or sparse")
43    matrix = [[0] * w for _ in range(h)]
44    used = set()
45    for cell in entries:
46        if not isinstance(cell, list) or len(cell) != 3:
47            raise ValueError("invalid sparse BIOM entry")
48        i, j, n = cell
49        if type(i) is not int or type(j) is not int or not 0 <= i < h or not 0 <= j < w or (i, j) in used:
50            raise ValueError("invalid or repeated sparse BIOM coordinate")
51        used.add((i, j))
52        matrix[i][j] = count(n)
53    return table, matrix
54
55
56def execute(operation, inputs, params):
57    table, matrix = values(inputs["source"])
58    axis = params.get("axis")
59    h, w = table["shape"]
60    if operation == "summarize":
61        if axis == "observation":
62            entries, counts = table["rows"], matrix
63        else:
64            entries, counts = table["columns"], [[row[j] for row in matrix] for j in range(w)]
65        totals = [sum(x > 0 for x in row) if params["qualitative"] else math.fsum(row) for row in counts]
66        rows = [entry["id"] + "\t" + format(v, ".12g") + "\n" for entry, v in zip(entries, totals)]
67        return ("id\tcount\n" + "".join(rows)).encode()
68    if operation == "relative":
69        if axis == "observation":
70            totals = list(map(math.fsum, matrix))
71            matrix = [[v / totals[i] if totals[i] else 0.0 for v in row] for i, row in enumerate(matrix)]
72        else:
73            totals = [math.fsum(row[j] for row in matrix) for j in range(w)]
74            matrix = [[v / totals[j] if totals[j] else 0.0 for j, v in enumerate(row)] for row in matrix]
75        table["matrix_element_type"] = "float"
76    if operation == "presence":
77        matrix = [[int(v > 0) for v in row] for row in matrix]
78        table["matrix_element_type"] = "int"
79    if operation == "subset":
80        wanted = set(lines(inputs["reference"]))
81        if axis == "observation":
82            keep = [i for i, entry in enumerate(table["rows"]) if entry["id"] in wanted]
83            table["rows"] = [table["rows"][i] for i in keep]
84            matrix = [matrix[i] for i in keep]
85            h = len(keep)
86        else:
87            keep = [j for j, entry in enumerate(table["columns"]) if entry["id"] in wanted]
88            table["columns"] = [table["columns"][j] for j in keep]
89            matrix = [[row[j] for j in keep] for row in matrix]
90            w = len(keep)
91        table["shape"] = [h, w]
92    table["data"] = (matrix if table["matrix_type"] == "dense" else
93                     [[i, j, v] for i, row in enumerate(matrix) for j, v in enumerate(row) if v != 0])
94    return json_lines([table])