InstantDownloadTranscript.tsx 7.08 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/**
 * 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.
 */

17 18 19
import React, { useState, useEffect } from 'react';
import classNames from 'classnames';
import pick from 'lodash/pick';
20
import intersection from 'lodash/intersection';
21 22 23 24 25 26 27 28 29 30

import { fetchForTranscript } from '../instant-download-fetch/fetchForTranscript';

import InstantDownloadTranscriptVisualisation from './InstantDownloadTranscriptVisualisation';
import Checkbox from 'src/shared/components/checkbox/Checkbox';
import InstantDownloadButton from '../instant-download-button/InstantDownloadButton';

import styles from './InstantDownloadTranscript.scss';

type Layout = 'horizontal' | 'vertical';
31
type Theme = 'light' | 'dark';
32 33 34

type TranscriptFields = {
  id: string;
35
  biotype: string;
36 37 38 39 40 41
};

type GeneFields = {
  id: string;
};

42
export type InstantDownloadTranscriptEntityProps = {
43
  genomeId: string;
44 45
  transcript: TranscriptFields;
  gene: GeneFields;
46 47 48
};

type Props = InstantDownloadTranscriptEntityProps & {
49
  layout: Layout;
50
  theme: Theme;
51 52 53 54 55
};

type TranscriptSectionProps = {
  transcript: TranscriptFields;
  options: Partial<TranscriptOptions>;
56
  theme: Theme;
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
  onChange: (key: keyof TranscriptOptions) => void;
};

type GeneSectionProps = {
  gene: GeneFields;
  isGenomicSequenceSelected: boolean;
  onChange: () => void;
};

export type TranscriptOptions = {
  genomicSequence: boolean;
  proteinSequence: boolean;
  cdna: boolean;
  cds: boolean;
};

export type TranscriptOption = keyof Partial<TranscriptOptions>;

export const transcriptOptionsOrder: TranscriptOption[] = [
  'genomicSequence',
  'cdna',
  'proteinSequence',
  'cds'
];

82
export const defaultTranscriptOptions: TranscriptOptions = {
83 84 85 86 87 88 89 90 91 92 93 94 95
  genomicSequence: false,
  cdna: false,
  proteinSequence: false,
  cds: false
};

const transcriptOptionLabels: Record<keyof TranscriptOptions, string> = {
  genomicSequence: 'Genomic sequence',
  proteinSequence: 'Protein sequence',
  cdna: 'cDNA',
  cds: 'CDS'
};

Imran Salam's avatar
Imran Salam committed
96
export const filterTranscriptOptions = (
97
  biotype: string
98
): Partial<TranscriptOptions> => {
99
  return biotype === 'protein_coding'
100
    ? defaultTranscriptOptions
101
    : pick(defaultTranscriptOptions, ['genomicSequence', 'cdna']);
102 103 104 105
};

const InstantDownloadTranscript = (props: Props) => {
  const {
106 107
    genomeId,
    gene: { id: geneId },
108
    transcript: { id: transcriptId, biotype }
109 110
  } = props;
  const [transcriptOptions, setTranscriptOptions] = useState(
111
    filterTranscriptOptions(biotype)
112 113 114 115
  );
  const [isGeneSequenceSelected, setIsGeneSequenceSelected] = useState(false);

  useEffect(() => {
116 117
    setTranscriptOptions(filterTranscriptOptions(biotype));
  }, [biotype]);
118

119 120
  const resetCheckboxes = () => {
    setIsGeneSequenceSelected(false);
121
    setTranscriptOptions(filterTranscriptOptions(biotype));
122 123 124
  };

  const onSubmit = async () => {
125 126 127 128 129 130 131 132 133 134
    const payload = {
      genomeId,
      geneId,
      transcriptId,
      options: {
        transcript: transcriptOptions,
        gene: { genomicSequence: isGeneSequenceSelected }
      }
    };

135 136 137 138 139
    try {
      await fetchForTranscript(payload);
    } finally {
      resetCheckboxes();
    }
140 141
  };

142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
  const onTranscriptOptionChange = (key: keyof TranscriptOptions) => {
    const updatedOptions = {
      ...transcriptOptions,
      [key]: !transcriptOptions[key]
    };
    setTranscriptOptions(updatedOptions);
  };
  const onGeneOptionChange = () => {
    setIsGeneSequenceSelected(!isGeneSequenceSelected);
  };

  const layoutClass =
    props.layout === 'horizontal'
      ? classNames(styles.layout, styles.layoutHorizontal)
      : classNames(styles.layout, styles.layoutVertical);

158 159 160 161 162
  const themeClass =
    props.theme === 'dark' ? styles.themeDark : styles.themeLight;

  const wrapperClassNames = classNames(themeClass, layoutClass);

163 164 165 166 167 168
  const isButtonDisabled = !hasSelectedOptions({
    ...transcriptOptions,
    geneSequence: isGeneSequenceSelected
  });

  return (
169
    <div className={wrapperClassNames}>
170 171 172
      <TranscriptSection
        transcript={props.transcript}
        options={transcriptOptions}
173
        theme={props.theme}
174 175 176 177 178 179 180
        onChange={onTranscriptOptionChange}
      />
      <GeneSection
        gene={props.gene}
        isGenomicSequenceSelected={isGeneSequenceSelected}
        onChange={onGeneOptionChange}
      />
181 182 183 184 185 186
      <div className={styles.downloadButton}>
        <InstantDownloadButton
          isDisabled={isButtonDisabled}
          onClick={onSubmit}
        />
      </div>
187 188 189 190 191
    </div>
  );
};

InstantDownloadTranscript.defaultProps = {
192 193
  layout: 'horizontal',
  theme: 'dark'
194 195 196 197
} as Props;

const TranscriptSection = (props: TranscriptSectionProps) => {
  const { transcript, options } = props;
198 199 200 201 202
  const orderedOptionKeys = intersection(
    transcriptOptionsOrder,
    Object.keys(options)
  );
  const checkboxes = orderedOptionKeys.map((key) => (
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
    <Checkbox
      key={key}
      classNames={{ unchecked: styles.checkboxUnchecked }}
      labelClassName={styles.checkboxLabel}
      label={transcriptOptionLabels[key as TranscriptOption]}
      checked={options[key as TranscriptOption] as boolean}
      onChange={() => props.onChange(key as TranscriptOption)}
    />
  ));

  const transcriptVisualisation = (
    <InstantDownloadTranscriptVisualisation
      isGenomicSequenceEnabled={options.genomicSequence}
      isProteinSequenceEnabled={options.proteinSequence}
      isCDNAEnabled={options.cdna}
      isCDSEnabled={options.cds}
219
      theme={props.theme}
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
    />
  );

  return (
    <div className={styles.transcriptSection}>
      <div className={styles.label}>
        Transcript
        <span className={styles.featureId}>{transcript.id}</span>
      </div>
      <div className={styles.transcriptVis}>{transcriptVisualisation}</div>
      <div className={styles.checkboxGrid}>{checkboxes}</div>
    </div>
  );
};

const GeneSection = (props: GeneSectionProps) => {
  return (
    <div className={styles.geneSection}>
      <div className={styles.label}>
        Gene
        <span className={styles.featureId}>{props.gene.id}</span>
      </div>
      <div>
        <Checkbox
          classNames={{
            checkboxHolder: styles.checkboxWrapper,
            unchecked: styles.checkboxUnchecked
          }}
          labelClassName={styles.checkboxLabel}
          label="Genomic sequence"
          checked={props.isGenomicSequenceSelected}
          onChange={props.onChange}
        />
      </div>
    </div>
  );
};

const hasSelectedOptions = (
  options: Partial<TranscriptOptions> & { geneSequence: boolean }
) => {
  return Object.keys(options).some(
    (key) => options[key as keyof TranscriptOptions]
  );
};

export default InstantDownloadTranscript;