import{a as i,u as p,H as S,I as A,R as e,c as m,D as M,T as x,B as o,X as D,Z as F,d as T,e as $,m as j}from"./index-Bm2SqMk3.js";const B=j()(r=>({textAreaFont:{fontFamily:"Courier New"},mgap:{display:"flex",flexDirection:"column",gap:r.spacing(4)}}));function I({model:r,handleClose:l}){const{classes:g}=B(),[s,v]=i.useState(),[y,c]=i.useState(),[E,w]=i.useState("");return i.useEffect(()=>{(async()=>{try{c(void 0);const n=p.getContainingView(r);if(!n.initialized)return;const{rpcManager:t}=p.getSession(r),{sources:a,mafFilter:C,adapterConfig:b}=r,d=S.getRpcSessionId(r),f=await t.call(d,"MultiVariantGetGenotypeMatrix",{regions:n.dynamicBlocks.contentBlocks,sources:a,mafFilter:C,sessionId:d,adapterConfig:b}),h=Object.values(f),R=Object.keys(f),k=`try(library(fastcluster), silent=TRUE)
inputMatrix<-matrix(c(${h.map(u=>u.genotypes.join(",")).join(`,
`)}
),nrow=${h.length},byrow=TRUE)
rownames(inputMatrix)<-c(${R.map(u=>`'${u}'`).join(",")})
resultClusters<-hclust(dist(inputMatrix), method='single')
cat(resultClusters$order,sep='\\n')`;v(k)}catch(n){!p.isAbortException(n)&&A(r)&&(console.error(n),c(n))}})()},[r]),e.createElement(m.Dialog,{open:!0,title:"Cluster by genotype",onClose:l},e.createElement(M,null,e.createElement("div",{className:g.mgap},e.createElement(x,null,"This page will produce an R script that will perform hierarchical clustering on the visible genotype data using `hclust`."),e.createElement(x,null,"You can then paste the results in this form to specify the row ordering."),s?e.createElement("div",null,e.createElement("div",null,"Step 1:"," ",e.createElement(o,{variant:"contained",onClick:()=>{D.saveAs(new Blob([s||""],{type:"text/plain;charset=utf-8"}),"cluster.R")}},"Download Rscript")," ","or"," ",e.createElement(o,{variant:"contained",onClick:()=>{F(s||"")}},"Copy Rscript to clipboard"),e.createElement("div",null,e.createElement(T,{multiline:!0,fullWidth:!0,variant:"outlined",placeholder:"Step 2. Paste results from Rscript here (sequence of numbers, one per line, specifying the new ordering)",rows:10,value:E,onChange:n=>{w(n.target.value)},slotProps:{input:{classes:{input:g.textAreaFont}}}})))):e.createElement(m.LoadingEllipses,{variant:"h6",title:"Generating genotype matrix"}),y?e.createElement(m.ErrorMessage,{error:y}):null)),e.createElement($,null,e.createElement(o,{disabled:!s,variant:"contained",onClick:()=>{const{sources:n}=r;if(n)try{r.setLayout(E.split(`
`).map(t=>t.trim()).filter(t=>!!t).map(t=>+t).map(t=>{const a=n[t-1];if(!a)throw new Error(`out of bounds at ${t}`);return a}))}catch(t){console.error(t),c(t)}l()}},"Apply clustering"),e.createElement(o,{variant:"contained",color:"secondary",onClick:()=>{l()}},"Cancel")))}export{I as default};
