Skip to content

Commit

Permalink
finish igv-create.js; better inference on track APIs
Browse files Browse the repository at this point in the history
Signed-off-by: eternal-flame-AD <[email protected]>
  • Loading branch information
eternal-flame-AD committed Sep 2, 2024
1 parent 1ff7e16 commit 6fe24d5
Showing 1 changed file with 65 additions and 15 deletions.
80 changes: 65 additions & 15 deletions js/igv.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,20 @@ declare class Opaque<N extends string> {
private readonly __opaque_brand: N;
}

export type TrackLoad =
export type TrackType = "annotation" | "alignment" | "variant" | "seg" | "cnvpytor" | "wig" | "merged";

export type TrackLoad<T extends TrackType> =
Tracks.TrackCommonOptions &
((Tracks.AnnotationTrackOptions & Tracks.TypeFormatPair<'annotation', Tracks.AnnotationFormat>) |
(Tracks.WigTrackOptions & Tracks.TypeFormatPair<'wig', Tracks.WigFormat>) |
(Tracks.WigMergedTrackOptions & { type: 'merged' }) |
(Tracks.AlignmentTrackOptions & Tracks.TypeFormatPair<'alignment', Tracks.AlignmentFormat>) |
(Tracks.VariantTrackOptions & Tracks.TypeFormatPair<'variant', Tracks.VariantFormat>) |
(Tracks.SegTrackOptions & Tracks.TypeFormatPair<'seg', Tracks.SegTrackFormat>) |
(Tracks.CnvPyTorTrackOptions & Tracks.TypeFormatPair<'cnvpytor', Tracks.CnvPyTorFormat>));

export type TrackOf<T extends string> =
(T extends "annotation" ? (Tracks.AnnotationTrackOptions & Tracks.TypeFormatPair<'annotation', Tracks.AnnotationFormat>) :
T extends "wig" ? (Tracks.WigTrackOptions & Tracks.TypeFormatPair<'wig', Tracks.WigFormat>) :
T extends "merged" ? (Tracks.WigMergedTrackOptions & { type: 'merged' }) :
T extends "alignment" ? (Tracks.AlignmentTrackOptions & Tracks.TypeFormatPair<'alignment', Tracks.AlignmentFormat>) :
T extends "variabt" ? (Tracks.VariantTrackOptions & Tracks.TypeFormatPair<'variant', Tracks.VariantFormat>) :
T extends "seg" ? (Tracks.SegTrackOptions & Tracks.TypeFormatPair<'seg', Tracks.SegTrackFormat>) :
T extends "cnvpytor" ? (Tracks.CnvPyTorTrackOptions & Tracks.TypeFormatPair<'cnvpytor', Tracks.CnvPyTorFormat>) :
never);

export type TrackOf<T extends TrackType> =
T extends "annotation" ? Tracks.Track :
T extends "alignment" ? Tracks.AlignmentTrack :
T extends "variant" ? Tracks.Track :
Expand All @@ -26,10 +29,13 @@ export type TrackOf<T extends string> =
T extends "merged" ? Tracks.Track :
never;

export type TrackOfLoad<K extends TrackLoad> = K extends { type: infer T } ? T extends string ? TrackOf<T> : never : never;

export namespace Tracks {
export class Track { }
export class Track {
public readonly id: string;
public readonly type: string;
public readonly name?: string;
}

export interface TypeFormatPair<K extends string, F extends string> {
type?: K;
Expand Down Expand Up @@ -511,7 +517,7 @@ interface CreateOptExtras {
* Array of configuration objects defining tracks initially displayed when app launches.
*
*/
tracks: TrackLoad[];
tracks: TrackLoad<TrackType>[];
}

export type CreateOpt = GenomeOpt & CreateOptExtras;
Expand Down Expand Up @@ -539,7 +545,7 @@ declare class _Browser {
loadSessionObject(session: Opaque<'igv.js session JSON'>): void;
loadSession(session: string): void;
// TODO: check the return type
loadTrack(track: TrackLoad): Promise<Tracks.Track>;
loadTrack<T extends TrackType>(track: TrackLoad<T>): Promise<TrackOf<T>>;
loadSampleInfo({ url: string }): void;
findTracks(func: (track: Tracks.Track) => boolean): Tracks.Track[];
findTracks(property: string, value: any): Tracks.Track[];
Expand Down Expand Up @@ -567,8 +573,52 @@ export type Browser = _Browser;

type CreateBrowser = (div: HTMLElement, options: CreateOpt) => Promise<Browser>;

export type GlobalConfig = {
minimumBases: number;
showIdeogram: boolean;
showCircularView: boolean;
showCircularViewButton: boolean;
showTrackLabelButton: boolean;
showTrackLabels: boolean;
doShowROITableButton: boolean;
showROITable: boolean;
showCursorTrackingGuideButton: boolean;
showCursorTrackingGuide: boolean;
showCenterGuideButton: boolean;
showCenterGuide: boolean;
showSampleNames: boolean;
showSVGButton: boolean;
showControls: boolean;
showNavigation: boolean;
showRuler: boolean;
flanking: number;
pairsSupported: boolean;
tracks: TrackLoad<TrackType>[];
}


export type IGV = {
createBrowser: CreateBrowser;
readonly setDefaults: (config: Partial<GlobalConfig>) => void;
/**
* Create an igv.browser instance. This object defines the public API for interacting with the genome browser.
*
* @param parentDiv - DOM tree root
* @param config - configuration options.
*
*/
readonly createBrowser: CreateBrowser;
readonly removeBrowser: (browser: Browser) => void;
readonly removeAllBrowsers: () => void;
readonly setApiKey: (apiKey: string) => void;
readonly setGoogleOauthToken: (token: string) => void;
readonly setOauthToken: (token: string, host: string) => void;
readonly getAllBrowsers: () => Browser[];
/**
* This function provided so clients can inform igv of a visibility change, typically when an igv instance is
* made visible from a tab, accordion, or similar widget.
*/
readonly visibilityChange: () => Promise<void>;
readonly version: () => string;
}

declare const igv: IGV;
Expand Down

0 comments on commit 6fe24d5

Please sign in to comment.