fetchSequenceChecksums.ts 5.07 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
export type TranscriptSequenceMetadata = {
Imran Salam's avatar
Imran Salam committed
22 23
  stable_id: string;
  unversioned_stable_id: string;
24 25 26
  cdna?: {
    checksum: string;
    label: string;
27
  };
28 29 30
  cds?: {
    checksum: string;
    label: string;
31
  };
32 33 34
  protein?: {
    checksum: string;
    label: string;
35 36 37
  };
};

Imran Salam's avatar
Imran Salam committed
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
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: {
      sequence_checksum: string;
    };
    cds: {
      sequence_checksum: string;
    };
    product: {
      stable_id: string;
      sequence_checksum: string;
    };
  }>;
};

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

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

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

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

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

Imran Salam's avatar
Imran Salam committed
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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
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 {
            sequence_checksum
          }
          cdna {
            sequence_checksum
          }
          product {
            stable_id
            sequence_checksum
          }
        }
      }
    }
  }
`;

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: {
      checksum: productGeneratingContext.cdna.sequence_checksum,
      label: `${stable_id} cdna`
    },
    cds: {
      checksum: productGeneratingContext.cds.sequence_checksum,
      label: `${stable_id} cds`
    },
    protein: {
      checksum: productGeneratingContext.product.sequence_checksum,
      label: `${productGeneratingContext.product.stable_id} pep`
    }
  };
166 167
};

168
export const fetchTranscriptSequenceMetadata = (
Imran Salam's avatar
Imran Salam committed
169 170 171
  variables: TranscriptQueryVariables
): Promise<TranscriptSequenceMetadata> =>
  client
172
    .query<TranscriptQueryResult>({
173 174 175
      query: transcriptChecksumsQuery,
      variables
    })
Imran Salam's avatar
Imran Salam committed
176
    .then(({ data }) => processTranscriptData(data.transcript));
177

Imran Salam's avatar
Imran Salam committed
178 179 180
type OnlyGeneSequenceMetadata = {
  stable_id: string;
  unversioned_stable_id: string;
181
};
Imran Salam's avatar
Imran Salam committed
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
// 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)
      )
    }));