Skip to content

Commit

Permalink
move readme endpoint types to api folder
Browse files Browse the repository at this point in the history
  • Loading branch information
vincerubinetti committed Sep 25, 2024
1 parent d5dad7f commit b91713e
Show file tree
Hide file tree
Showing 17 changed files with 268 additions and 316 deletions.
16 changes: 7 additions & 9 deletions frontend/src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,19 @@ import {
convertAnalysisResults,
convertConvertIds,
revertAnalysisInputs,
type _AnalysisResults,
type _ConvertIds,
type AnalysisInputs,
type Species,
} from "@/api/convert";
import {
type _AnalysisResults,
type _ConvertIdsInputs,
type _ConvertIdsResults,
} from "@/api/types";

/** check input list of genes. convert to entrez, check if in-network, etc. */
export const checkGenes = async (
genes: string[],
species: Species = "Human",
) => {
export const checkGenes = async (params: _ConvertIdsInputs) => {
const headers = new Headers();
headers.append("Content-Type", "application/json");
const params = { genes, species };
const response = await request<_ConvertIds>(
const response = await request<_ConvertIdsResults>(
`${api}/gpz-convert-ids`,
undefined,
{ method: "POST", headers, body: JSON.stringify(params) },
Expand Down
133 changes: 133 additions & 0 deletions frontend/src/api/convert.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/** handle conversion between frontend and backend data formats */

import type {
_AnalysisInputs,
_AnalysisResults,
_ConvertIdsResults,
} from "@/api/types";

/** backend format to frontend format */
export const convertConvertIds = (backend: _ConvertIdsResults) => {
/** map "couldn't convert" status to easier-to-work-with value */
for (const row of backend.df_convert_out)
if (row["Entrez ID"].match(/Could Not be mapped to Entrez/i))
row["Entrez ID"] = "";

return {
count: backend.input_count,
success: backend.df_convert_out.filter((row) => row["Entrez ID"]).length,
error: backend.df_convert_out.filter((row) => !row["Entrez ID"]).length,
summary: backend.table_summary.map((row) => ({
network: row.Network,
positiveGenes: row.PositiveGenes,
totalGenes: row.NetworkGenes,
})),
table: backend.df_convert_out.map((row) => ({
input: row["Original ID"],
entrez: row["Entrez ID"],
name: row["Gene Name"],
inNetwork:
(row["In BioGRID?"] ?? row["In IMP?"] ?? row["In STRING?"]) === "Y",
})),
};
};

/** backend format to frontend format */
export const convertAnalysisInputs = (backend: _AnalysisInputs) => ({
name: backend.name,
genes: backend.genes,
speciesTrain: backend.sp_trn,
speciesTest: backend.sp_tst,
network: backend.net_type,
genesetContext: backend.gsc,
negatives: backend.negatives,
});

/** ml endpoint params frontend format */
export type AnalysisInputs = ReturnType<typeof convertAnalysisInputs>;

/** frontend format to backend format */
export const revertAnalysisInputs = (
frontend: AnalysisInputs,
): _AnalysisInputs => ({
name: frontend.name,
genes: frontend.genes,
sp_trn: frontend.speciesTrain,
sp_tst: frontend.speciesTest,
net_type: frontend.network,
gsc: frontend.genesetContext,
negatives: frontend.negatives,
});

/** backend format to frontend format */
export const convertAnalysisResults = (backend: _AnalysisResults) => ({
inputGenes: backend.df_convert_out_subset.map((row) => ({
input: row["Original ID"],
entrez: row["Entrez ID"].match(/Could Not be mapped to Entrez/i)
? ""
: row["Entrez ID"],
name: row["Gene Name"],
inNetwork:
(row["In BioGRID?"] ?? row["In IMP?"] ?? row["In STRING?"]) === "Y",
})),
crossValidation: backend.avgps,
positiveGenes: backend.positive_genes,
predictions: backend.df_probs.map((row) => ({
entrez: row.Entrez,
symbol: row.Symbol,
name: row.Name,
knownNovel: row["Known/Novel"],
classLabel: expandClass(row["Class-Label"]),
probability: row.Probability,
zScore: row["Z-score"],
pAdjusted: row["P-adjusted"],
rank: row.Rank,
})),
similarities: backend.df_sim.map((row) => ({
task: row.Task,
id: row.ID,
name: row.Name,
similarity: row.Similarity,
zScore: row["Z-score"],
pAdjusted: row["P-adjusted"],
rank: row.Rank,
})),
network: {
nodes: backend.df_probs.map((row) => ({
entrez: row.Entrez,
symbol: row.Symbol,
name: row.Name,
knownNovel: row["Known/Novel"],
classLabel: expandClass(row["Class-Label"]),
probability: row.Probability,
zScore: row["Z-score"],
pAdjusted: row["P-adjusted"],
rank: row.Rank,
})),
edges: backend.df_edge.map((row) => ({
source: row.Node1,
target: row.Node2,
weight: row.Weight,
})),
},
neutralInfo: (() => {
const { "All Neutrals": all, ...sets } = backend.neutral_gene_info;
return {
all: Array.isArray(all) ? all : [],
sets: Object.entries(sets).flatMap(([Id, value]) =>
Array.isArray(value) ? [] : { Id, ...value },
),
};
})(),
});

/** ml endpoint params frontend format */
export type AnalysisResults = ReturnType<typeof convertAnalysisResults>;

/** convert class label abbreviation to full text */
const expandClass = (
abbrev: _AnalysisResults["df_probs"][number]["Class-Label"],
) => (({ P: "Positive", N: "Negative", U: "Neutral" }) as const)[abbrev];

/** full analysis */
export type Analysis = { inputs: AnalysisInputs; results: AnalysisResults };
Loading

0 comments on commit b91713e

Please sign in to comment.