Process gene panel buckets and map to case panels

by alfredopacino

JavaScript

function mapCasePanelBuckets(
  caseGenePanels,
  dslResponse
) {
  const genePanelBuckets = dslResponse.aggregations?.genes?.genePanels?.matching_panels?.panels?.buckets;
  const superpanelBuckets = dslResponse.aggregations?.genes?.genePanels?.superpanels?.matching_panels?.panels?.buckets;

  if (!Array.isArray(genePanelBuckets) || !Array.isArray(superpanelBuckets)) {
    throw new Error('Invalid response from variants DSL API: missing panel buckets');
  }

  const toEntry = (bucket) => {
    const [panelIdentifier, panelName, panelVersion] = bucket.key;
    return { panelIdentifier, panelName, panelVersion };
  };

  const allEntries = [...genePanelBuckets, ...superpanelBuckets].map(toEntry);

  const uniqueEntries = uniqBy(
    allEntries,
    ({ panelIdentifier, panelName, panelVersion }) => `${panelIdentifier}__${panelName}__${panelVersion}`
  );

  if (uniqueEntries.length === 0) {
    throw new Error('No Case Panels matches found in non-GMS Panels pool');
  }

  const entriesByIdentifier = groupBy(uniqueEntries, 'panelIdentifier');

  return caseGenePanels.flatMap((panel) => {
    const candidates = entriesByIdentifier[String(panel.panel_id)] ?? [];

    // TODO 'N/A' is a temp fix so no matches are clear.
    return candidates.map((candidate) => ({
      panelIdentifier: Number(candidate.panelIdentifier),
      panelName: candidate.panelName || 'N/A',
      panelVersion: candidate.panelVersion || 'N/A',
      casePanelVersion: panel.version,
    }));
  });

var casePanels =  [
  {                                                                                                                                                                                                                                                                                              
    "panel_id": "541",                                                                                                                                    ...