import {
  make_radios,
  renderTable,
  sayerror,
  table2col
} from "./chunk-SA7APTJR.js";
import "./chunk-HJ6L54YS.js";
import "./chunk-LSEFWW72.js";
import "./chunk-SKREEF3H.js";
import {
  Menu
} from "./chunk-HYOEWQ5P.js";
import "./chunk-HBW42TDT.js";
import "./chunk-G6O3URDN.js";
import "./chunk-FN5XPUPH.js";
import "./chunk-IIT367QZ.js";
import "./chunk-RZGEKL77.js";
import "./chunk-UI7OCM2A.js";
import "./chunk-YZK37YCC.js";
import {
  dofetch3,
  fileSize
} from "./chunk-7EMWHCVW.js";
import "./chunk-7IYJZZQI.js";
import "./chunk-M3J4MINX.js";
import "./chunk-PF4DSFDR.js";
import "./chunk-QH6W3NVF.js";
import "./chunk-NOEAT6CX.js";
import "./chunk-4UWS5Y3N.js";
import "./chunk-DD4R5P6W.js";
import "./chunk-JNITUVXP.js";
import "./chunk-KSGA62R2.js";
import "./chunk-LOZEKOES.js";
import "./chunk-TOU7EVFQ.js";
import "./chunk-OAWQ6LOO.js";
import "./chunk-TLT4YIG3.js";
import "./chunk-KYBIQBXE.js";
import {
  select_default
} from "./chunk-I6Y4O3RR.js";
import "./chunk-OMR2DT66.js";
import "./chunk-DQC5FFGV.js";
import "./chunk-HFNDKYVF.js";

// gdc/maf.js
var tip = new Menu();
var tableColumns = [
  { label: "Case", sortable: true },
  { label: "Project", sortable: true },
  { label: "Samples" },
  { label: "File Size", barplot: { tickFormat: "~s" }, sortable: true }
  // barchart column not sortable yet
];
var mafColumns = [
  { column: "Hugo_Symbol", selected: true },
  { column: "Entrez_Gene_Id", selected: true },
  { column: "Center", selected: true },
  { column: "NCBI_Build", selected: true },
  { column: "Chromosome", selected: true },
  { column: "Start_Position", selected: true },
  { column: "End_Position", selected: true },
  { column: "Strand", selected: true },
  { column: "Variant_Classification", selected: true },
  { column: "Variant_Type", selected: true },
  { column: "Reference_Allele", selected: true },
  { column: "Tumor_Seq_Allele1", selected: true },
  { column: "Tumor_Seq_Allele2", selected: true },
  { column: "dbSNP_RS", selected: true },
  { column: "dbSNP_Val_Status", selected: true },
  { column: "Tumor_Sample_Barcode", selected: true },
  { column: "Matched_Norm_Sample_Barcode", selected: true },
  { column: "Match_Norm_Seq_Allele1", selected: true },
  { column: "Match_Norm_Seq_Allele2", selected: true },
  { column: "Tumor_Validation_Allele1", selected: true },
  { column: "Tumor_Validation_Allele2", selected: true },
  { column: "Match_Norm_Validation_Allele1", selected: true },
  { column: "Match_Norm_Validation_Allele2", selected: true },
  { column: "Verification_Status", selected: true },
  { column: "Validation_Status", selected: true },
  { column: "Mutation_Status", selected: true },
  { column: "Sequencing_Phase", selected: true },
  { column: "Sequence_Source", selected: true },
  { column: "Validation_Method", selected: true },
  { column: "Score", selected: true },
  { column: "BAM_File", selected: true },
  { column: "Sequencer", selected: true },
  { column: "Tumor_Sample_UUID", selected: true },
  { column: "Matched_Norm_Sample_UUID", selected: true },
  { column: "HGVSc", selected: true },
  { column: "HGVSp", selected: true },
  { column: "HGVSp_Short", selected: true },
  { column: "Transcript_ID", selected: true },
  { column: "Exon_Number", selected: true },
  { column: "t_depth", selected: true },
  { column: "t_ref_count", selected: true },
  { column: "t_alt_count", selected: true },
  { column: "n_depth", selected: true },
  { column: "n_ref_count", selected: true },
  { column: "n_alt_count", selected: true },
  { column: "all_effects", selected: true },
  { column: "Allele", selected: true },
  { column: "Gene", selected: true },
  { column: "Feature", selected: true },
  { column: "Feature_type", selected: true },
  { column: "One_Consequence", selected: true },
  { column: "Consequence", selected: true },
  { column: "cDNA_position", selected: true },
  { column: "CDS_position", selected: true },
  { column: "Protein_position", selected: true },
  { column: "Amino_acids", selected: true },
  { column: "Codons", selected: true },
  { column: "Existing_variation", selected: true },
  { column: "DISTANCE", selected: true },
  { column: "TRANSCRIPT_STRAND", selected: true },
  { column: "SYMBOL", selected: true },
  { column: "SYMBOL_SOURCE", selected: true },
  { column: "HGNC_ID", selected: true },
  { column: "BIOTYPE", selected: true },
  { column: "CANONICAL", selected: true },
  { column: "CCDS", selected: true },
  { column: "ENSP", selected: true },
  { column: "SWISSPROT", selected: true },
  { column: "TREMBL", selected: true },
  { column: "UNIPARC", selected: true },
  { column: "UNIPROT_ISOFORM", selected: true },
  { column: "RefSeq", selected: true },
  { column: "MANE", selected: true },
  { column: "APPRIS", selected: true },
  { column: "FLAGS", selected: true },
  { column: "SIFT", selected: true },
  { column: "PolyPhen", selected: true },
  { column: "EXON", selected: true },
  { column: "INTRON", selected: true },
  { column: "DOMAINS", selected: true },
  { column: "1000G_AF", selected: true },
  { column: "1000G_AFR_AF", selected: true },
  { column: "1000G_AMR_AF", selected: true },
  { column: "1000G_EAS_AF", selected: true },
  { column: "1000G_EUR_AF", selected: true },
  { column: "1000G_SAS_AF", selected: true },
  { column: "ESP_AA_AF", selected: true },
  { column: "ESP_EA_AF", selected: true },
  { column: "gnomAD_AF", selected: true },
  { column: "gnomAD_AFR_AF", selected: true },
  { column: "gnomAD_AMR_AF", selected: true },
  { column: "gnomAD_ASJ_AF", selected: true },
  { column: "gnomAD_EAS_AF", selected: true },
  { column: "gnomAD_FIN_AF", selected: true },
  { column: "gnomAD_NFE_AF", selected: true },
  { column: "gnomAD_OTH_AF", selected: true },
  { column: "gnomAD_SAS_AF", selected: true },
  { column: "MAX_AF", selected: true },
  { column: "MAX_AF_POPS", selected: true },
  { column: "gnomAD_non_cancer_AF", selected: true },
  { column: "gnomAD_non_cancer_AFR_AF", selected: true },
  { column: "gnomAD_non_cancer_AMI_AF", selected: true },
  { column: "gnomAD_non_cancer_AMR_AF", selected: true },
  { column: "gnomAD_non_cancer_ASJ_AF", selected: true },
  { column: "gnomAD_non_cancer_EAS_AF", selected: true },
  { column: "gnomAD_non_cancer_FIN_AF", selected: true },
  { column: "gnomAD_non_cancer_MID_AF", selected: true },
  { column: "gnomAD_non_cancer_NFE_AF", selected: true },
  { column: "gnomAD_non_cancer_OTH_AF", selected: true },
  { column: "gnomAD_non_cancer_SAS_AF", selected: true },
  { column: "gnomAD_non_cancer_MAX_AF_adj", selected: true },
  { column: "gnomAD_non_cancer_MAX_AF_POPS_adj", selected: true },
  { column: "CLIN_SIG", selected: true },
  { column: "SOMATIC", selected: true },
  { column: "PUBMED", selected: true },
  { column: "TRANSCRIPTION_FACTORS", selected: true },
  { column: "MOTIF_NAME", selected: true },
  { column: "MOTIF_POS", selected: true },
  { column: "HIGH_INF_POS", selected: true },
  { column: "MOTIF_SCORE_CHANGE", selected: true },
  { column: "miRNA", selected: true },
  { column: "IMPACT", selected: true },
  { column: "PICK", selected: true },
  { column: "VARIANT_CLASS", selected: true },
  { column: "TSL", selected: true },
  { column: "HGVS_OFFSET", selected: true },
  { column: "PHENO", selected: true },
  { column: "GENE_PHENO", selected: true },
  { column: "CONTEXT", selected: true },
  { column: "case_id", selected: true },
  { column: "GDC_FILTER", selected: true },
  { column: "COSMIC", selected: true },
  { column: "hotspot", selected: true },
  { column: "tumor_bam_uuid", selected: true },
  { column: "normal_bam_uuid", selected: true },
  { column: "RNA_Support", selected: true },
  { column: "RNA_depth", selected: true },
  { column: "RNA_ref_count", selected: true },
  { column: "RNA_alt_count", selected: true },
  { column: "callers", selected: true }
];
async function gdcMAFui({ filter0, callbacks, debugmode = false }, holder) {
  try {
    if (callbacks) {
      delete callbacks.sjcharts;
      for (const n in callbacks) {
        if (typeof callbacks[n] != "function") throw `callbacks.${n} not function`;
      }
    }
    {
      const cn = /* @__PURE__ */ new Set();
      for (const c of mafColumns) {
        if (!c.column) throw ".column missing from an element";
        if (cn.has(c.column)) throw "duplicate column: " + c.column;
        cn.add(c.column);
      }
    }
    update({ filter0 });
  } catch (e) {
    console.log(e);
    sayerror(holder, e.message || e);
  }
  async function update({ filter0: filter02 }) {
    holder.selectAll("*").remove();
    const obj = {
      holder,
      errDiv: holder.append("div"),
      controlDiv: holder.append("div"),
      tableDiv: holder.append("div"),
      opts: {
        filter0: filter02,
        experimentalStrategy: "WXS"
      },
      mafTableArg: null
    };
    makeControls(obj);
    await getFilesAndShowTable(obj);
    callbacks?.postRender?.(publicApi);
  }
  const publicApi = { update };
  return publicApi;
}
function makeControls(obj) {
  const table = table2col({ holder: obj.controlDiv });
  table.addRow("Access", "Open");
  table.addRow("Workflow Type", "Aliquot Ensemble Somatic Variant Merging and Masking");
  {
    const [td1, td2] = table.addRow("Experimental Strategy");
    make_radios({
      holder: td2,
      options: [
        {
          label: "WXS",
          value: "WXS",
          checked: obj.opts.experimentalStrategy == "WXS",
          testid: "sjpp-gdcmaf-radio-wxs"
        },
        {
          label: "Targeted Sequencing",
          value: "Targeted Sequencing",
          checked: obj.opts.experimentalStrategy == "Targeted Sequencing",
          testid: "sjpp-gdcmaf-radio-targeted"
        }
      ],
      styles: { display: "inline" },
      callback: async (value) => {
        obj.opts.experimentalStrategy = value;
        await getFilesAndShowTable(obj);
      }
    });
  }
  {
    let updateText2 = function() {
      clickText.text(
        `${mafColumns.reduce((c, i) => c + (i.selected ? 1 : 0), 0)} of ${mafColumns.length} columns selected. Click to change`
      );
    };
    var updateText = updateText2;
    const [td1, td2] = table.addRow("Output Columns");
    const clickText = td2.append("span").attr("data-testid", "sjpp-gdcmaf-columnhandle").attr("class", "sja_clbtext").on("click", (event) => {
      const rows = [], selectedRows = [];
      for (const [i, c] of mafColumns.entries()) {
        rows.push([{ value: c.column }]);
        if (c.selected) selectedRows.push(i);
      }
      renderTable({
        div: tip.clear().showunder(event.target).d,
        rows,
        columns: [{ label: "Column Name" }],
        selectedRows,
        dataTestId: "sjpp-gdcmaf-columnTableUi",
        noButtonCallback: (i, n) => {
          mafColumns[i].selected = n.checked;
          updateText2();
        }
      });
    });
    updateText2();
  }
}
async function getFilesAndShowTable(obj) {
  obj.tableDiv.selectAll("*").remove();
  const wait = obj.tableDiv.append("div").style("margin", "30px 10px 10px 10px").text("Loading...");
  let result;
  try {
    const body = {
      experimentalStrategy: obj.opts.experimentalStrategy
    };
    if (obj.opts.filter0) body.filter0 = obj.opts.filter0;
    result = await dofetch3("gdc/maf", { body });
    if (result.error) throw result.error;
    if (!Array.isArray(result.files)) throw "result.files[] not array";
    if (result.files.length == 0) throw "No MAF files available.";
    if (result.filesTotal > result.files.length) {
      wait.text(`Showing first ${result.files.length} MAF files out of ${result.filesTotal} total.`);
    } else {
      wait.text(`Showing ${result.files.length} MAF files.`);
    }
    const rows = [];
    for (const f of result.files) {
      const row = [
        {
          html: `<a href=https://portal.gdc.cancer.gov/files/${f.id} target=_blank>${f.case_submitter_id}</a>`,
          value: f.case_submitter_id
        },
        { value: f.project_id },
        {
          html: f.sample_types.map((i) => {
            return '<span class="sja_mcdot" style="padding:1px 8px;background:#ddd;color:black;white-space:nowrap">' + i + "</span>";
          }).join(" ")
        },
        { value: f.file_size }
        // do not send in text-formated file size, table sorting won't work
      ];
      rows.push(row);
    }
    obj.mafTableArg = {
      rows,
      columns: tableColumns,
      resize: true,
      div: obj.tableDiv.append("div"),
      selectAll: true,
      // comment out for quicker testing
      dataTestId: "sjpp-gdcmaf-maffiletable-" + obj.opts.experimentalStrategy,
      header: { allowSort: true },
      selectedRows: [],
      //[198], // uncomment out for quicker testing
      buttonsToLeft: true,
      buttons: [
        {
          text: " ",
          // table.ts requires this
          onChange: updateButtonBySelectionChange,
          callback: submitSelectedFiles,
          dataTestId: "sjpp-gdcmaf-submitBtn"
        }
      ]
    };
    renderTable(obj.mafTableArg);
  } catch (e) {
    wait.text(e.message || e);
    if (e.stack) console.log(e.stack);
  }
  function updateButtonBySelectionChange(lst, button) {
    let sum = 0;
    for (const i of lst) sum += result.files[i].file_size;
    if (sum == 0) {
      button.innerHTML = "No file selected";
      button.disabled = true;
      return;
    }
    button.disabled = false;
    button.innerHTML = sum < result.maxTotalSizeCompressed ? `Download ${fileSize(sum)} compressed MAF data` : `Download ${fileSize(result.maxTotalSizeCompressed)} compressed MAF data (${fileSize(sum)} selected)`;
  }
  let serverMessage;
  async function submitSelectedFiles(lst, button) {
    const outColumns = mafColumns.filter((i) => i.selected).map((i) => i.column);
    if (outColumns.length == 0) {
      window.alert("No output columns selected.");
      return;
    }
    mayCreateServerMessageSpan(button);
    const fileIdLst = [];
    for (const i of lst) {
      fileIdLst.push(result.files[i].id);
    }
    if (fileIdLst.length == 0) return;
    obj.holder.style("pointer-events", "none").style("opacity", 0.5);
    const oldText = button.innerHTML;
    button.innerHTML = "Loading... Please wait";
    serverMessage.style("display", "none");
    let data;
    try {
      data = await dofetch3("gdc/mafBuild", { body: { fileIdLst, columns: outColumns } });
      if (!Object.keys(data).length) throw "server returned blank multipart";
      if (data.errors?.body) {
        const errors = data.errors.body || [];
        if (Array.isArray(errors)) {
          const fileErrors = errors.filter((d) => d.url);
          if (fileErrors.length) displayRunStatusErrors(fileErrors);
          const nonFileErrors = errors.filter((d) => !d.url);
          for (const e of nonFileErrors) sayerror(obj.errDiv, e.error || e.message);
        }
      }
      if (!data.gzfile) throw "missing gzfile from response";
      const href = URL.createObjectURL(data.gzfile.body);
      const a = document.createElement("a");
      a.href = href;
      a.download = `cohortMAF.${(/* @__PURE__ */ new Date()).toISOString().split("T")[0]}.maf.gz`;
      a.style.display = "none";
      document.body.appendChild(a);
      a.click();
      document.body.removeChild(a);
    } catch (e) {
      sayerror(obj.errDiv, e);
    } finally {
      button.innerHTML = oldText;
      obj.holder.style("pointer-events", "auto").style("opacity", 1);
    }
  }
  function mayCreateServerMessageSpan(button) {
    if (serverMessage) return;
    const holder = select_default(button.parentElement);
    serverMessage = holder.append("span").attr("class", "sja_clbtext").style("display", "none");
  }
  function displayRunStatusErrors(errors) {
    const rows = [];
    for (const e of errors) {
      if (typeof e.error != "string") throw ".error=string missing from an entry";
      if (typeof e.url != "string") throw ".url=string missing from an entry";
      const l = e.url.split("/");
      const uuid = l[l.length - 1];
      const fo = result.files.find((i) => i.id == uuid);
      if (fo) {
        rows.push([
          { html: `<a href=${e.url} target=_blank>${fo.case_submitter_id}</a>` },
          { value: fo.project_id },
          { value: fileSize(fo.file_size) },
          { value: e.error }
        ]);
      } else {
        rows.push([{ value: uuid }, { value: "?" }, { value: "?" }, { value: e.error }]);
      }
    }
    serverMessage.text(`${errors.length} empty/failed file${errors.length > 1 ? "s" : ""}`).style("display", "").on("click", (event) => {
      renderTable({
        rows,
        columns: [{ column: "" }, { column: "" }, { column: "" }, { column: "" }],
        showHeader: false,
        div: tip.clear().showunder(event.target).d
      });
    });
  }
}
export {
  gdcMAFui
};
//# sourceMappingURL=maf-U237OWZ3.js.map
