Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api): subscription for genomic axis changes #935

Merged
merged 4 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions editor/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -332,12 +332,17 @@ function Editor(props: RouteComponentProps) {
// gosRef.current.api.subscribe('trackClick', (type, eventData) => {
// console.warn(type, eventData.id, eventData.spec, eventData.shape);
// });
// Location API
// gosRef.current.api.subscribe('location', (type, eventData) => {
// console.warn(type, eventData.id, eventData.genomicRange);
// });
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is where I've been testing the new location API. I noticed that there was commented out code for the other subscriptions so I did the same.

}
return () => {
// gosRef.current?.api.unsubscribe('mouseOver');
// gosRef.current?.api.unsubscribe('click');
// gosRef.current?.api.unsubscribe('rangeSelect');
// gosRef.current?.api.unsubscribe('trackClick');
// gosRef.current?.api.unsubscribe('location');
};
}, [gosRef.current]);

Expand Down
8 changes: 8 additions & 0 deletions src/core/gosling.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,13 @@ interface RangeMouseEventData extends CommonEventData {
genomicRange: [GenomicPosition, GenomicPosition] | null;
}

/**
* Data about the genomic range of a track
*/
interface LocationEventData extends Omit<CommonEventData, 'data'> {
genomicRange: [GenomicPosition, GenomicPosition];
}

/**
* The visual parameters that determine the shape of a linear track.
* Origin is the left top corner.
Expand Down Expand Up @@ -295,6 +302,7 @@ export type _EventMap = {
rawData: CommonEventData;
trackMouseOver: TrackMouseEventData;
trackClick: TrackMouseEventData; // TODO (Jul-25-2022): with https:/higlass/higlass/pull/1098, we can support circular layouts
location: LocationEventData;
};

/** Options for determining mouse events in detail, e.g., turning on specific events only */
Expand Down
49 changes: 39 additions & 10 deletions src/core/utils/assembly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,48 @@ export interface ChromSize {

/**
* Get relative chromosome position (e.g., `100` => `{ chromosome: 'chr1', position: 100 }`)
* @param absPos number which is the absolute chromosome position
* @param assembly the assembly used to calculate which chromosome position
* @param returnWithinAssembly If true, then if the absolute position is before the first chromosome, it returns the
* first position of the first chromosome. If the absolute position is after the last chromosome, it returns the last
* position of the last chromosome
Comment on lines +23 to +25
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the extra option I added to this function so that it doesn't return "unknown" chromosome if the axis is out of the assembly bounds.

* @returns the genomic position of the absPos
*/
export function getRelativeGenomicPosition(absPos: number, assembly?: Assembly): GenomicPosition {
const [chromosome, absInterval] = Object.entries(computeChromSizes(assembly).interval).find(d => {
const [start, end] = d[1];
return start <= absPos && absPos < end;
}) ?? [null, null];

if (!chromosome || !absInterval) {
// The number is out of range
export function getRelativeGenomicPosition(
absPos: number,
assembly?: Assembly,
returnWithinAssembly = false
etowahadams marked this conversation as resolved.
Show resolved Hide resolved
): GenomicPosition {
const chrSizes = Object.entries(computeChromSizes(assembly).interval);
const minPosChr = { chromosome: 'unknown', position: Infinity } as GenomicPosition;
const maxPosChr = { chromosome: 'unknown', position: 0 } as GenomicPosition;
for (const chrSize of chrSizes) {
const [chromosome, absInterval] = chrSize;
const [start, end] = absInterval;
// absPos was found within this chromosome
if (start <= absPos && absPos < end) {
return { chromosome, position: absPos - start } as GenomicPosition;
}
// Update the min and max chromosomes found
if (start < minPosChr.position) {
minPosChr.chromosome = chromosome;
minPosChr.position = start;
}
if (end > maxPosChr.position) {
maxPosChr.chromosome = chromosome;
maxPosChr.position = end;
}
}
if (returnWithinAssembly) {
// Return either the min or max chromosome position
if (absPos < minPosChr.position) {
return minPosChr;
} else {
return maxPosChr;
}
} else {
return { chromosome: 'unknown', position: absPos };
}

return { chromosome, position: absPos - absInterval[0] };
}

/**
Expand Down
12 changes: 12 additions & 0 deletions src/gosling-track/gosling-track.ts
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,18 @@ const factory: PluginTrackFactory<Tile, GoslingTrackOptions> = (HGC, context, op
this.refreshTiles();
this.draw();
this.forceDraw();

// Publish the new genomic axis domain
const genomicRange = newXScale
.domain()
.map(absPos => getRelativeGenomicPosition(absPos, this.#assembly, true)) as [
GenomicPosition,
GenomicPosition
];
publish('location', {
id: context.viewUid,
genomicRange: genomicRange
});
}

/* *
Expand Down