fetchForTranscript.ts 3.02 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
import downloadAsFile from 'src/shared/helpers/downloadAsFile';
18

19 20 21 22 23
import {
  TranscriptOptions,
  TranscriptOption,
  transcriptOptionsOrder
} from 'src/shared/components/instant-download/instant-download-transcript/InstantDownloadTranscript';
24 25 26 27
import {
  fetchTranscriptChecksums,
  TranscriptChecksums
} from './fetchSequenceChecksums';
28 29 30 31 32 33 34 35 36

type Options = {
  transcript: Partial<TranscriptOptions>;
  gene: {
    genomicSequence: boolean;
  };
};

type FetchPayload = {
37
  genomeId: string;
38
  geneId: string;
39
  transcriptId: string;
40 41 42 43 44
  options: Options;
};

export const fetchForTranscript = async (payload: FetchPayload) => {
  const {
45
    genomeId,
46 47 48 49
    geneId,
    transcriptId,
    options: { transcript: transcriptOptions, gene: geneOptions }
  } = payload;
50 51 52 53 54 55
  const checksums = await fetchTranscriptChecksums({
    genomeId,
    transcriptId
  });
  const urls = buildUrlsForTranscript({ geneId, checksums }, transcriptOptions);

56
  if (geneOptions.genomicSequence) {
57
    urls.push(buildFetchUrl({ geneId }, 'genomicSequence'));
58
  }
59

60 61 62 63
  const sequencePromises = urls.map((url) =>
    fetch(url).then((response) => response.text())
  );
  const sequences = await Promise.all(sequencePromises);
64
  const combinedFasta = sequences.join('\n\n');
65 66 67 68 69 70 71

  downloadAsFile(combinedFasta, `${transcriptId}.fasta`, {
    type: 'text/x-fasta'
  });
};

const buildUrlsForTranscript = (
72 73 74 75
  data: {
    geneId: string;
    checksums: TranscriptChecksums;
  },
76 77 78 79 80
  options: Partial<TranscriptOptions>
) => {
  return options
    ? transcriptOptionsOrder
        .filter((option) => options[option])
81
        .map((option) => buildFetchUrl(data, option))
82 83 84
    : [];
};

85 86 87 88 89 90 91 92
const buildFetchUrl = (
  data: {
    geneId: string;
    checksums?: TranscriptChecksums;
  },
  sequenceType: TranscriptOption
) => {
  const sequenceTypeToContextType: Record<TranscriptOption, string> = {
93
    genomicSequence: 'genomic',
94
    proteinSequence: 'product',
95 96 97 98
    cdna: 'cdna',
    cds: 'cds'
  };

99 100 101 102 103 104 105 106 107 108 109
  if (sequenceType === 'genomicSequence') {
    return `https://rest.ensembl.org/sequence/id/${data.geneId}?content-type=text/x-fasta&type=${sequenceTypeToContextType.genomicSequence}`;
  } else {
    const contextType = sequenceTypeToContextType[
      sequenceType
    ] as keyof TranscriptChecksums;
    const checksum =
      data.checksums && data.checksums[contextType]?.sequence_checksum;

    return `/refget/sequence/${checksum}?accept=text/x-fasta`;
  }
110
};