fetchSequenceChecksums.ts 5.43 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/**
 * 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 { gql } from '@apollo/client';

import { client } from 'src/gql-client';

21 22
import { Sequence } from 'ensemblRoot/src/shared/types/thoas/sequence';

23
export type TranscriptSequenceMetadata = {
Imran Salam's avatar
Imran Salam committed
24 25
  stable_id: string;
  unversioned_stable_id: string;
26 27 28
  cdna?: {
    checksum: string;
    label: string;
29
  };
30 31 32
  cds?: {
    checksum: string;
    label: string;
33
  };
34 35 36
  protein?: {
    checksum: string;
    label: string;
37 38 39
  };
};

Imran Salam's avatar
Imran Salam committed
40 41 42 43 44 45 46 47 48 49 50
export type GeneSequenceMetadata = {
  stable_id: string;
  unversioned_stable_id: string;
  transcripts: TranscriptSequenceMetadata[];
};

type TranscriptInResponse = {
  stable_id: string; // for apollo client caching
  unversioned_stable_id: string;
  product_generating_contexts: Array<{
    cdna: {
51
      sequence: Sequence;
Imran Salam's avatar
Imran Salam committed
52 53
    };
    cds: {
54
      sequence: Sequence;
55
    } | null;
Imran Salam's avatar
Imran Salam committed
56 57
    product: {
      stable_id: string;
58
      sequence: Sequence;
59
    } | null;
Imran Salam's avatar
Imran Salam committed
60 61 62
  }>;
};

63
type TranscriptQueryResult = {
Imran Salam's avatar
Imran Salam committed
64 65 66 67 68 69 70 71
  transcript: TranscriptInResponse;
};

type GeneQueryResult = {
  gene: {
    stable_id: string;
    unversioned_stable_id: string;
    transcripts: TranscriptInResponse[];
72 73 74
  };
};

Imran Salam's avatar
Imran Salam committed
75 76 77 78 79 80 81 82 83 84
type TranscriptQueryVariables = {
  genomeId: string;
  transcriptId: string;
};

type GeneQueryVariables = {
  genomeId: string;
  geneId: string;
};

85 86 87
const transcriptChecksumsQuery = gql`
  query Transcript($genomeId: String!, $transcriptId: String!) {
    transcript(byId: { genome_id: $genomeId, stable_id: $transcriptId }) {
Imran Salam's avatar
Imran Salam committed
88 89
      stable_id
      unversioned_stable_id
90 91
      product_generating_contexts {
        cds {
92 93 94
          sequence {
            checksum
          }
95 96
        }
        cdna {
97 98 99
          sequence {
            checksum
          }
100 101
        }
        product {
102
          stable_id
103 104 105
          sequence {
            checksum
          }
106 107 108 109 110 111
        }
      }
    }
  }
`;

Imran Salam's avatar
Imran Salam committed
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
const onlyGeneQuery = gql`
  query Gene($genomeId: String!, $geneId: String!) {
    gene(byId: { genome_id: $genomeId, stable_id: $geneId }) {
      stable_id
      unversioned_stable_id
    }
  }
`;

const geneChecksumsQuery = gql`
  query Gene($genomeId: String!, $geneId: String!) {
    gene(byId: { genome_id: $genomeId, stable_id: $geneId }) {
      stable_id
      unversioned_stable_id
      transcripts {
        stable_id
        unversioned_stable_id
        product_generating_contexts {
          cds {
131 132 133
            sequence {
              checksum
            }
Imran Salam's avatar
Imran Salam committed
134 135
          }
          cdna {
136 137 138
            sequence {
              checksum
            }
Imran Salam's avatar
Imran Salam committed
139 140 141
          }
          product {
            stable_id
142 143 144
            sequence {
              checksum
            }
Imran Salam's avatar
Imran Salam committed
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
          }
        }
      }
    }
  }
`;

const processTranscriptData = (transcript: TranscriptInResponse) => {
  // TODO: expect to fetch genomic sequence here as well when checksum becomes available
  const { stable_id, unversioned_stable_id } = transcript; // can't retrieve transcript seq via REST using stable_id
  const productGeneratingContext = transcript.product_generating_contexts[0];

  if (!productGeneratingContext) {
    return {
      stable_id,
      unversioned_stable_id
    };
  }

  return {
    stable_id,
    unversioned_stable_id,
    cdna: {
168
      checksum: productGeneratingContext.cdna.sequence.checksum,
Imran Salam's avatar
Imran Salam committed
169 170
      label: `${stable_id} cdna`
    },
171 172 173 174 175 176 177 178 179 180 181 182
    cds: productGeneratingContext.cds
      ? {
          checksum: productGeneratingContext.cds.sequence.checksum,
          label: `${stable_id} cds`
        }
      : undefined,
    protein: productGeneratingContext.product
      ? {
          checksum: productGeneratingContext.product.sequence.checksum,
          label: `${productGeneratingContext.product.stable_id} pep`
        }
      : undefined
Imran Salam's avatar
Imran Salam committed
183
  };
184 185
};

186
export const fetchTranscriptSequenceMetadata = (
Imran Salam's avatar
Imran Salam committed
187 188 189
  variables: TranscriptQueryVariables
): Promise<TranscriptSequenceMetadata> =>
  client
190
    .query<TranscriptQueryResult>({
191 192 193
      query: transcriptChecksumsQuery,
      variables
    })
Imran Salam's avatar
Imran Salam committed
194
    .then(({ data }) => processTranscriptData(data.transcript));
195

Imran Salam's avatar
Imran Salam committed
196 197 198
type OnlyGeneSequenceMetadata = {
  stable_id: string;
  unversioned_stable_id: string;
199
};
Imran Salam's avatar
Imran Salam committed
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
// temporary function; will change it to fetch gene sequence checksum when available
export const fetchGeneWithoutTranscriptsSequenceMetadata = (
  variables: GeneQueryVariables
): Promise<OnlyGeneSequenceMetadata> =>
  client
    .query<{ gene: OnlyGeneSequenceMetadata }>({
      query: onlyGeneQuery,
      variables
    })
    .then(({ data }) => data.gene);

export const fetchGeneSequenceMetadata = (
  variables: GeneQueryVariables
): Promise<GeneSequenceMetadata> =>
  client
    .query<GeneQueryResult>({
      query: geneChecksumsQuery,
      variables
    })
    .then(({ data }) => ({
      stable_id: data.gene.stable_id,
      unversioned_stable_id: data.gene.unversioned_stable_id,
      transcripts: data.gene.transcripts.map((transcript) =>
        processTranscriptData(transcript)
      )
    }));