diff --git a/src/services/v1_service/service.ts b/src/services/v1_service/service.ts index 9a15295..8737362 100644 --- a/src/services/v1_service/service.ts +++ b/src/services/v1_service/service.ts @@ -1,28 +1,10 @@ -import {readFileSync} from "fs"; -import {resolve} from "path"; import {Errors, GET, Path, QueryParam, Return, ContextResponse} from "typescript-rest"; +import {AnnotationStatus, Aspect, makeAnnotationIndex} from "../../utils/ingest"; import {queryAnnotated, QueryOption, Segment, Strategy} from "../../queries/queries"; -import { - AnnotationStatus, - Aspect, - ingestData, - makeAnnotationIndex, - StructuredData, - UnstructuredText -} from "../../utils/ingest"; import {annotationsToGAF, genesToCSV} from '../../utils/exporters'; +import { getDataset } from '../../utils/data_fetcher'; import express from "express"; -// TODO use data fetcher rather than files. -console.log("Begin reading data"); -const genesText = readFileSync(process.env["GENES_FILE"] || resolve("src/assets/gene-types.txt")).toString(); -const annotationsText = readFileSync(process.env["ANNOTATIONS_FILE"] || resolve("src/assets/tair.gaf")).toString(); -const unstructuredText: UnstructuredText = {genesText, annotationsText}; -const maybeDataset = ingestData(unstructuredText); -if (!maybeDataset) throw new Error("failed to parse data"); -const dataset: StructuredData = maybeDataset; -console.log("Finished parsing data"); - type Format = "gaf" | "gene-csv" | "json"; type QueryStatus = "EXP" | "OTHER" | "UNKNOWN" | "UNANNOTATED"; @@ -69,6 +51,8 @@ export class V1Service { query = {tag: "QueryWith", strategy, segments}; } + const dataset = getDataset(); + // TODO include unannotated genes const [queriedGenes, queriedAnnotations] = queryAnnotated(dataset, query); @@ -97,6 +81,7 @@ export class V1Service { @Path("/wgs_segments") @GET get_wgs() { + const dataset = getDataset(); const totalGeneCount = Object.keys(dataset.genes.index).length; const result = Object.entries(dataset.annotations.index) diff --git a/src/utils/data_fetcher.ts b/src/utils/data_fetcher.ts index 278d876..4e35292 100644 --- a/src/utils/data_fetcher.ts +++ b/src/utils/data_fetcher.ts @@ -1,14 +1,10 @@ -import {GeneMap, IAnnotation, GroupedAnnotations, IGene, readData} from "./ingest"; +import {readFileSync} from "fs"; +import {resolve} from "path"; +import {ingestData, StructuredData, UnstructuredText} from "../utils/ingest"; -let mostRecentGeneMap: GeneMap; -let mostRecentAnnotations: IAnnotation[]; -let mostRecentGroupedAnnotations: GroupedAnnotations>; +let mostRecentDataset: StructuredData; -export const getGeneMap = (): GeneMap => { return mostRecentGeneMap; }; - -export const getAnnotations = (): IAnnotation[] => { return mostRecentAnnotations; }; - -export const getGroupedAnnotations = (): GroupedAnnotations> => { return mostRecentGroupedAnnotations; }; +export const getDataset = (): StructuredData => { return mostRecentDataset; }; export const downloadData = (dataUrl: string = "http://current.geneontology.org/annotations/tair.gaf.gz") => { const { execSync } = require("child_process"); @@ -17,10 +13,15 @@ export const downloadData = (dataUrl: string = "http://current.geneontology.org/ export const updateData = () => { downloadData(); - const { geneMap, annotations, groupedAnnotations } = readData(); - mostRecentGeneMap = geneMap; - mostRecentAnnotations = annotations; - mostRecentGroupedAnnotations = groupedAnnotations; + console.log("Begin reading data"); + const genesText = readFileSync(resolve("src/assets/gene-types.txt")).toString(); + const annotationsText = readFileSync(resolve("src/assets/tair.gaf")).toString(); + const unstructuredText: UnstructuredText = {genesText, annotationsText}; + const maybeDataset = ingestData(unstructuredText); + if (!maybeDataset) throw new Error("failed to parse data"); + const dataset: StructuredData = maybeDataset; + mostRecentDataset = dataset; + console.log("Finished parsing data"); }; export const startPeriodicallyCalling = (fn: (...args: any[]) => void, interval: number = (1000 * 60 * 60 * 24), startDate: Date = getTomorrowMorning()) => {