mirror of
https://github.com/chanzuckerberg/cellxgene.git
synced 2026-10-03 08:58:11 +08:00
* Added TS. Updated build and linting config. Added types. * [ts-migrate][.] Rename files from JS/JSX to TS/TSX Co-authored-by: ts-migrate <> * [ts-migrate][.] Run TS Migrate Co-authored-by: ts-migrate <> * Corrected files mangled by ts-migrate. * Updated lint config, minor linting. * Re-enabled Husky. * Updated tests and config. * Reverted webpack devtool config. * Removed obsolete snapshots. * Added annotations snap. * Updated tsconfig includes wrt linting. * Removed ts-migrate. Co-authored-by: Timmy Huang <tihuan@users.noreply.github.com>
This commit is contained in:
co-authored by
ts-migrate
Timmy Huang
parent
7328cbdbd5
commit
934cc5c69b
@@ -0,0 +1,135 @@
|
||||
/*
|
||||
Helpers for schema management
|
||||
|
||||
TODO: all this would be much more natural if done with a framework
|
||||
like immutable.js
|
||||
*/
|
||||
import cloneDeep from "lodash.clonedeep";
|
||||
|
||||
import fromEntries from "../fromEntries";
|
||||
import catLabelSort from "../catLabelSort";
|
||||
|
||||
/*
|
||||
System wide schema assumptions:
|
||||
- schema and data wil be consistent (eg, for user-created annotations)
|
||||
- schema will be internally self-consistent (eg, index matches columns)
|
||||
*/
|
||||
|
||||
export function indexEntireSchema(schema: any) {
|
||||
/* Index schema for ease of use */
|
||||
schema.annotations.obsByName = fromEntries(
|
||||
schema.annotations?.obs?.columns?.map((v: any) => [v.name, v]) ?? []
|
||||
);
|
||||
schema.annotations.varByName = fromEntries(
|
||||
schema.annotations?.var?.columns?.map((v: any) => [v.name, v]) ?? []
|
||||
);
|
||||
schema.layout.obsByName = fromEntries(
|
||||
schema.layout?.obs?.map((v: any) => [v.name, v]) ?? []
|
||||
);
|
||||
schema.layout.varByName = fromEntries(
|
||||
schema.layout?.var?.map((v: any) => [v.name, v]) ?? []
|
||||
);
|
||||
|
||||
return schema;
|
||||
}
|
||||
|
||||
function _copyObsAnno(schema: any) {
|
||||
/* redux copy conventions - WARNING, only for modifying obs annotations */
|
||||
return {
|
||||
...schema,
|
||||
annotations: {
|
||||
...schema.annotations,
|
||||
obs: cloneDeep(schema.annotations.obs),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function _copyObsLayout(schema: any) {
|
||||
return {
|
||||
...schema,
|
||||
layout: {
|
||||
...schema.layout,
|
||||
obs: cloneDeep(schema.layout.obs),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function _reindexObsAnno(schema: any) {
|
||||
/* reindex obs annotations ONLY */
|
||||
schema.annotations.obsByName = fromEntries(
|
||||
schema.annotations.obs.columns.map((v: any) => [v.name, v])
|
||||
);
|
||||
return schema;
|
||||
}
|
||||
|
||||
function _reindexObsLayout(schema: any) {
|
||||
schema.layout.obsByName = fromEntries(
|
||||
schema.layout.obs.map((v: any) => [v.name, v])
|
||||
);
|
||||
return schema;
|
||||
}
|
||||
|
||||
export function removeObsAnnoColumn(schema: any, name: any) {
|
||||
const newSchema = _copyObsAnno(schema);
|
||||
newSchema.annotations.obs.columns = schema.annotations.obs.columns.filter(
|
||||
(v: any) => v.name !== name
|
||||
);
|
||||
return _reindexObsAnno(newSchema);
|
||||
}
|
||||
|
||||
// @ts-expect-error ts-migrate(6133) FIXME: 'name' is declared but its value is never read.
|
||||
export function addObsAnnoColumn(schema: any, name: any, defn: any) {
|
||||
const newSchema = _copyObsAnno(schema);
|
||||
newSchema.annotations.obs.columns.push(defn);
|
||||
return _reindexObsAnno(newSchema);
|
||||
}
|
||||
|
||||
export function removeObsAnnoCategory(schema: any, name: any, category: any) {
|
||||
/* remove a category from a categorical annotation */
|
||||
const categories = schema.annotations.obsByName[name]?.categories;
|
||||
if (!categories)
|
||||
throw new Error("column does not exist or is not categorical");
|
||||
|
||||
const idx = categories.indexOf(category);
|
||||
if (idx === -1) throw new Error("category does not exist");
|
||||
|
||||
const newSchema = _reindexObsAnno(_copyObsAnno(schema));
|
||||
|
||||
/* remove category. Do not need to resort as this can't change presentation order */
|
||||
newSchema.annotations.obsByName[name].categories.splice(idx, 1);
|
||||
return newSchema;
|
||||
}
|
||||
|
||||
export function addObsAnnoCategory(schema: any, name: any, category: any) {
|
||||
/* add a category to a categorical annotation */
|
||||
const categories = schema.annotations.obsByName[name]?.categories;
|
||||
if (!categories)
|
||||
throw new Error("column does not exist or is not categorical");
|
||||
|
||||
const idx = categories.indexOf(category);
|
||||
if (idx !== -1) throw new Error("category already exists");
|
||||
|
||||
const newSchema = _reindexObsAnno(_copyObsAnno(schema));
|
||||
|
||||
/* add category, retaining presentation sort order */
|
||||
const catAnno = newSchema.annotations.obsByName[name];
|
||||
catAnno.categories = catLabelSort(catAnno.writable, [
|
||||
...catAnno.categories,
|
||||
category,
|
||||
]);
|
||||
return newSchema;
|
||||
}
|
||||
|
||||
export function addObsLayout(schema: any, layout: any) {
|
||||
/* add or replace a layout */
|
||||
const newSchema = _copyObsLayout(schema);
|
||||
newSchema.layout.obs.push(layout);
|
||||
return _reindexObsLayout(newSchema);
|
||||
}
|
||||
|
||||
export function removeObsLayout(schema: any, name: any) {
|
||||
/* remove a layout */
|
||||
const newSchema = _copyObsLayout(schema);
|
||||
newSchema.layout.obs = schema.layout.obs.filter((v: any) => v.name !== name);
|
||||
return _reindexObsLayout(newSchema);
|
||||
}
|
||||
Reference in New Issue
Block a user