-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement load() to handle JSON fetching and object creation.
- Loading branch information
Showing
5 changed files
with
52 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 + "'"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}) |