TrackPanelGene.tsx 4.66 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
/**
 * See the NOTICE file distributed with this work for additional information
 * regarding copyright ownership.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import React from 'react';
import { useSelector } from 'react-redux';

import { useGetTrackPanelGeneQuery } from 'src/content/app/browser/state/genomeBrowserApiSlice';

import { getBrowserTrackStates } from 'src/content/app/browser/browserSelectors';

import { getTranscriptMetadata as getTranscriptSupportLevel } from 'src/content/app/entity-viewer/shared/components/default-transcript-label/TranscriptQualityLabel';

import TrackPanelListItem from './TrackPanelListItem';

import { Status } from 'src/shared/types/status';
import type { TrackActivityStatus } from 'src/content/app/browser/track-panel/trackPanelConfig';
import type { EnsObjectTrack } from 'src/shared/state/ens-object/ensObjectTypes';
import type {
  TrackPanelGene as TrackPanelGeneType,
  TrackPanelTranscript as TrackPanelTranscriptType
} from 'src/content/app/browser/state/types/track-panel-gene';

type TrackPanelGeneProps = {
  genomeId: string;
  geneId: string;
  ensObjectId: string;
};

// TODO: change track ids
const GENE_TRACK_ID = 'track:gene-feat';
const TRANSCRIPT_TRACK_ID = 'track:gene-feat-1';

const TrackPanelGene = (props: TrackPanelGeneProps) => {
  const { genomeId, geneId, ensObjectId } = props;
  const { data } = useGetTrackPanelGeneQuery({
    genomeId,
    geneId
  });
  const trackStates = useSelector(getBrowserTrackStates);
  const trackStatusGetter = prepareTrackStatusGetter({
    trackStates,
    genomeId,
    objectId: ensObjectId,
    categoryName: 'main'
  });

  if (!data) {
    return null;
  }

  const { gene } = data;
  const geneTrackStatus = trackStatusGetter(GENE_TRACK_ID);
  const transcriptTrackStatus = trackStatusGetter(TRANSCRIPT_TRACK_ID);

  return (
    <div>
      <TrackPanelListItem
        categoryName="main"
        trackStatus={geneTrackStatus}
        defaultTrackStatus={Status.SELECTED}
        track={prepareGeneTrackData(gene)}
      >
        {prepareTranscriptsTrackData(gene).map((transcriptTrackData) => (
          <TrackPanelListItem
            key={transcriptTrackData.label}
            categoryName="main"
            trackStatus={transcriptTrackStatus}
            defaultTrackStatus={Status.SELECTED}
            track={transcriptTrackData}
          />
        ))}
      </TrackPanelListItem>
    </div>
  );
};

// TODO: examine EnsObjectTrack
// as we change our thinking about the EnsObject type, the EnsObjectTrack type,
// and the TrackPanelListItem components, the functions below will need changing

const prepareGeneTrackData = (gene: TrackPanelGeneType): EnsObjectTrack => {
  return {
    label: gene.symbol ?? gene.stable_id,
    track_id: GENE_TRACK_ID,
    stable_id: gene.stable_id,
    additional_info: gene.metadata.biotype.label,
    description: null // FIXME: remove this field from EnsObjectTrack
  };
};

const prepareTranscriptsTrackData = (
  gene: TrackPanelGeneType
): EnsObjectTrack[] => {
  const canonicalTranscript = gene.transcripts.find(
    (transcript) => transcript.metadata.canonical
  ) as TrackPanelTranscriptType;
  const canonicalTranscriptTrackData = {
    label: canonicalTranscript.stable_id,
    track_id: TRANSCRIPT_TRACK_ID,
    stable_id: canonicalTranscript.stable_id,
    description: null,
    colour: 'BLUE',
    additional_info: canonicalTranscript.metadata.biotype.label,
    support_level: getTranscriptSupportLevel(canonicalTranscript)?.label
  };
  return [canonicalTranscriptTrackData];
};

type GetTrackStatusParams = {
  trackStates: ReturnType<typeof getBrowserTrackStates>;
  genomeId: string;
  objectId: string;
  categoryName: string;
  trackId: string;
};

const prepareTrackStatusGetter =
  (params: Omit<GetTrackStatusParams, 'trackId'>) => (trackId: string) =>
    getTrackStatus({ ...params, trackId });

const getTrackStatus = (params: GetTrackStatusParams): TrackActivityStatus => {
  const { trackStates, genomeId, objectId, categoryName, trackId } = params;
  const defaultTrackStatus = Status.SELECTED;
  return (
    trackStates?.[genomeId]?.objectTracks?.[objectId]?.[categoryName]?.[
      trackId
    ] ?? defaultTrackStatus
  );
};

export default TrackPanelGene;