Skip to content

Commit

Permalink
Implement load() to handle JSON fetching and object creation.
Browse files Browse the repository at this point in the history
  • Loading branch information
LTLA committed Dec 4, 2024
1 parent 66e112e commit 7cb2b23
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 5 deletions.
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,11 @@ const fetch_range = async (path, start, end) => {
};
```

Once that's done, we use the **wobbegong.js** library to set up our interface to the SummarizedExperiment:
Once that's done, we use the **wobbegong.js** library to load the interface to the SummarizedExperiment:

```js
import * as wob from "wobbegong";
const se_summary = await fetch_json("my_dataset/summary.json");
const se = new wob.SummarizedExperiment(se_summary, "my_dataset", fetch_json, fetch_range)

const se = await wob.load("my_dataset", fetch_json, fetch_range);
se.numberOfRows();
se.numberOfColumns();
se.isSingleCellExperiment();
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"name": "wobbegong",
"description": "Retrieving parts of a SummarizedExperiment via HTTP range requests.",
"license": "MIT",
"version": "0.1.1",
"version": "0.1.2",
"keywords": [
"single-cell",
"scRNA-seq",
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from "./DataFrame.js";
export * from "./Matrix.js";
export * from "./ReducedDimensionResult.js";
export * from "./SummarizedExperiment.js";
export * from "./load.js";
27 changes: 27 additions & 0 deletions src/load.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { SummarizedExperiment } from "./SummarizedExperiment.js";
import { DataFrame } from "./DataFrame.js";

/**
* Load an interface to a SummarizedExperiment or a DataFrame.
*
* @param {string} path - Path to a directory containing a SummarizedExperiment or DataFrame.
* This may be a relative or absolute path, depending on how the files are hosted.
* @param {function} fetch_json - A function that accepts `path`, a path to a file inside `path`.
* It should retrieve the contents of `path` and load them as a JSON object.
* It may also return a promise that resolves to such an object.
* @param {function} fetch_range - A function that accepts `file` (a path to a file inside `path`), `start` and `end`.
* It should retrieve bytes from `path` in the interval `[start, end)` and return a Uint8Array containing those bytes.
* It may also return a promise that resolves to such a Uint8Array.
*
* @return {DataFrame|SummarizedExperiment} Interface to a {@link DataFrame} or {@link SummarizedExperiment},
* depending on the object at `path`.
*/
export async function load(path, load_json, load_range) {
const summary = await load_json(path + "/summary.json");
if (summary.object == "data_frame") {
return new DataFrame(summary, path, load_range);
} else if ([ "summarized_experiment", "single_cell_experiment"].indexOf(summary.object) >= 0) {
return new SummarizedExperiment(summary, path, load_json, load_range)
}
throw new Error("unknown object type '" + summary.object + "' at path '" + path + "'");
}
21 changes: 21 additions & 0 deletions tests/load.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import * as wob from "../src/index.js";
import { localFetchRange, localFetchJson } from "./utils.js";
import * as fs from "fs";
import * as p from "path";
import * as u from "url";

test("load works for SummarizedExperiments", async () => {
const path = p.join(p.dirname(u.fileURLToPath(import.meta.url)), "mock-files", "full");
const my_se = await wob.load(path, localFetchJson, localFetchRange);
expect(my_se instanceof wob.SummarizedExperiment).toBe(true);
expect(my_se.numberOfRows()).toEqual(50);
expect(my_se.numberOfColumns()).toEqual(20);
})

test("load works for DataFrames", async () => {
const path = p.join(p.dirname(u.fileURLToPath(import.meta.url)), "mock-files", "full", "column_data");
const my_df = await wob.load(path, localFetchJson, localFetchRange);
expect(my_df instanceof wob.DataFrame).toBe(true);
expect(my_df.numberOfRows()).toEqual(20);
expect(my_df.numberOfColumns()).toEqual(4);
})

0 comments on commit 7cb2b23

Please sign in to comment.