chromosomix 0.19.0 → 0.20.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,8 @@
1
+ function vt(){return typeof MessageChannel>"u"?new Promise(e=>{setTimeout(e,0)}):new Promise(e=>{const t=new MessageChannel;t.port1.onmessage=()=>{t.port1.close(),t.port2.close(),e()},t.port2.postMessage(void 0)})}const nu=200,su=3e3,ou=3e4,ru=1e3;function iu(e=nu,t=su){const n=[];let s=performance.now();const o=setInterval(()=>{const r=performance.now(),i=r-s;for(s=r,i<ou?n.push({at:r,span:i,late:Math.max(0,i-e)}):n.length=0;n.length>0&&r-n[0].at>t;)n.shift()},e);return{busy(){if(n.length<3)return 0;let r=0,i=0;for(const a of n)r+=a.span,i+=a.late;return r<=0?0:Math.min(1,i/r)},reset(){n.length=0,s=performance.now()},stop(){clearInterval(o)}}}function st(e){if(e==null)return"None";if(typeof e=="boolean")return e?"True":"False";if(typeof e=="number")return Number.isInteger(e),String(e);if(Array.isArray(e))return`[${e.map(st).join(", ")}]`;if(typeof e!="string")return String(e);const t=e.includes("'")&&!e.includes('"')?'"':"'",n=e.replace(/\\/g,"\\\\").replace(/\n/g,"\\n").replace(/\r/g,"\\r").replace(/\t/g,"\\t").replace(new RegExp(t,"g"),`\\${t}`);return`${t}${n}${t}`}function Gt(e){return`[${e.map(st).join(", ")}]`}function Kc(e){const t=e.trim();if(!/^[+-]?[0-9](?:_?[0-9])*$/.test(t))return null;const n=Number(t.replace(/_/g,""));return Number.isFinite(n)?n:null}class T extends Error{code;detail;constructor(t,n,s={}){super(n),this.name="ViewerError",this.code=t,this.detail=s}}class ct extends T{constructor(t,n={},s="format"){super(s,t,n),this.name="MetaCoolerFormatError"}}function We(e,t=20){return e.length<=t?Gt(e):`${Gt(e.slice(0,t))} ... (${e.length-t} more)`}const Is="HDF5::MetaCooler",wo=1,Vc="HDF5::MMetaCooler",Fr="HDF5::Cooler",xn="HDF5::SCOOL",bo=1,fs="cells",$r=["bins","pixels","indexes"],gi=5e4,au=3/4,cu=8,Kn=2**31,lu=1e9,uu=2,pi=["symmetric-upper","square"],Vn="genome-then-length",vn="__unbinned__",hu=["","null","none","nan"],Ao=new Intl.NumberFormat(void 0,{maximumFractionDigits:0}),fu=new Intl.NumberFormat(void 0,{maximumFractionDigits:3}),du=new Intl.NumberFormat(void 0,{maximumSignificantDigits:3}),wi=new Map,mu=new Intl.NumberFormat("en-US",{maximumFractionDigits:0}),It={int(e){return Number.isFinite(e)?Ao.format(e):"—"},num(e){if(!Number.isFinite(e))return"—";if(Number.isInteger(e))return Ao.format(e);const t=Math.abs(e);return t>=.001?fu.format(e):t>=1e-7?du.format(e):e.toExponential(2)},fixed(e,t){if(!Number.isFinite(e))return"—";let n=wi.get(t);return n===void 0&&(n=new Intl.NumberFormat(void 0,{minimumFractionDigits:t,maximumFractionDigits:t}),wi.set(t,n)),n.format(e)},pct(e,t=1){return Number.isFinite(e)?`${(e*100).toFixed(t)}%`:"—"},pos(e){return Number.isFinite(e)?mu.format(Math.round(e)):"—"},bp(e){return Number.isFinite(e)?Math.abs(e)>=1e6?`${(e/1e6).toFixed(2)} Mb`:Math.abs(e)>=1e3?`${(e/1e3).toFixed(1)} kb`:`${Ao.format(e)} bp`:"—"},bytes(e){if(!Number.isFinite(e))return"—";const t=["B","kB","MB","GB","TB"];let n=e,s=0;for(;n>=1e3&&s<t.length-1;)n/=1e3,s++;return`${s===0?n:n.toFixed(1)} ${t[s]}`}};function gu(e){return!Number.isFinite(e)||e<=0?"—":e>=1e6?`${yo(e/1e6)} Mb`:e>=1e3?`${yo(e/1e3)} kb`:`${yo(e)} bp`}function yo(e){return String(Number(e.toFixed(2)))}const pu=["exact","case","decorated","prefix","padded","roman","organelle","accession","version"],bi=new Set(["gi","gb","ref","emb","dbj","tpg","tpe","tpd","lcl","pir","prf","sp","tr","pdb","ena","bbs"]);function Er(e){let t=e.trim().split(/\s+/)[0]??"";if(t=t.replace(/[,;]+$/,""),t.includes("|")){const n=t.split("|").filter(s=>s!=="");if(n.some(s=>bi.has(s.toLowerCase()))){const s=n.filter((o,r)=>bi.has(o.toLowerCase())?!1:!(/^[0-9]+$/.test(o)&&r>0&&n[r-1].toLowerCase()==="gi"));s.length>0&&(t=s[s.length-1])}}return t}const Yc=/^(?:chromosome|chrom|chr)[ _.-]?/i;function Wc(e){const t=e.replace(Yc,"");if(t===""||t===e)return null;const n=t.toLowerCase();return/^[0-9]/.test(t)||vs.has(n)||xs.has(n)||Ks(t)>0||/^un[_.-]/i.test(t)||/^[A-Za-z]{1,2}[0-9]/.test(t)||Jc.test(t)||Zc.test(t.replace(/\.[0-9]+$/,""))?t:null}const wu=/^(?:lg|linkage_?group|group)[ _.-]?(?=[0-9])/i,Xc=/_(random|alt|fix|hap\d+|maternal|paternal)$/i,bu=/(?:^|[_.-])([A-Z]{2}[0-9]{5,6})v([0-9]+)(?:$|[_.-])/,Jc=/^(N[CGTWZ]|AC)_([0-9]{5,})$/i,Zc=/^[A-Z]{1,6}[0-9]{5,8}$/,Au=[["mito",["m","mt","mtdna","mito","mitochondrion","mitochondria","mitochondrial","mitochondrion_genome","dmel_mitochondrion_genome","mitochondrial_genome","mitochondrion_dna","chrmt","nc_012920","j01415","nc_001224","nc_005089"]],["plastid",["c","pt","cp","chloroplast","plastid","chloroplast_genome","chrc","nc_000932"]],["2micron",["2-micron","2micron","2u","2-micron_plasmid","nc_001398"]]],xs=new Map;for(const[e,t]of Au)for(const n of t)xs.set(n,e);const vs=new Set(["x","y","z","w"]),yu=/^(XL|L|X{0,3})(IX|IV|V?I{0,3})$/i,Nu={"":0,x:10,xx:20,xxx:30,xl:40,l:50},Mu={"":0,i:1,ii:2,iii:3,iv:4,v:5,vi:6,vii:7,viii:8,ix:9};function Ks(e){const t=yu.exec(e);if(t===null)return 0;const n=Nu[t[1].toLowerCase()],s=Mu[t[2].toLowerCase()];return n===void 0||s===void 0?0:n+s}const Iu=[{from:1,to:22,first:1,tail:["x","y"]},{from:67,to:85,first:1,tail:["x","y"]}];function xu(e){for(const t of Iu){if(e>=t.from&&e<=t.to)return String(t.first+(e-t.from));const n=e-t.to-1;if(n>=0&&n<t.tail.length)return t.tail[n]}return null}const or=200;function vu(e){return e.length<2||vs.has(e.toLowerCase())?!1:Ks(e)>0}function Oe(e){const t=e.map(c=>jc(c)),s=t.filter(c=>vu(c)).length>=2,o=t.filter(c=>/^[0-9]+$/.test(c)&&Number(c)>=1&&Number(c)<=or),r=o.length<or,i=c=>t.some(l=>l.toLowerCase()===c),a=r&&o.length>=2;return{roman:s,ordinals:r,xIsTen:s&&!a&&(i("ix")||i("xi"))}}function jc(e){const t=Er(e);return(Wc(t)??t).replace(Xc,"")}function ds(e,t){const n=[],s=Er(e);s!==e.trim()&&n.push("decorated");const o=Wc(s),r=o??s;o!==null&&n.push("prefix");const i=Xc.exec(r),a=i===null?r:r.slice(0,i.index),c=i===null?"":`+${i[1].toLowerCase()}`,l=a.toLowerCase(),u=bu.exec(r);if(u!==null)return n.push("accession"),{key:`acc:${u[1].toLowerCase()}`,via:n,version:u[2],opaque:!1};const h=/^(.*?)\.([0-9]+)$/.exec(a),f=h===null?a:h[1],d=h===null?null:h[2],m=xs.get(l)??xs.get(f.toLowerCase());if(m!==void 0)return m!==l&&n.push("organelle"),{key:`org:${m}`,via:n,version:d,opaque:!1};const g=Jc.exec(f);if(g!==null){n.push("accession");const b=g[1].toLowerCase()==="nc"?xu(Number(g[2])):null;return b!==null?{key:vs.has(b)?`sex:${b}${c}`:`num:${b}${c}`,via:n,version:d,opaque:!1}:{key:`acc:${f.toLowerCase()}${c}`,via:n,version:d,opaque:!1}}if(Zc.test(f))return n.push("accession"),{key:`acc:${f.toLowerCase()}${c}`,via:n,version:d,opaque:!1};if(/^[0-9]+$/.test(a)&&t.ordinals&&Number(a)<=or)return/^0[0-9]/.test(a)&&n.push("padded"),{key:`num:${Number(a)}${c}`,via:n,version:null,opaque:!1};const p=/^([0-9]{1,2})([a-z]{1,2})$/i.exec(a);if(p!==null&&t.ordinals)return p[1].startsWith("0")&&n.push("padded"),{key:`num:${Number(p[1])}${p[2].toLowerCase()}${c}`,via:n,version:null,opaque:!1};const w=wu.exec(a);if(w!==null){const b=a.slice(w[0].length);if(/^[0-9]+$/.test(b))return/^0[0-9]/.test(b)&&n.push("padded"),{key:`lg:${Number(b)}${c}`,via:n,version:null,opaque:!1}}if(vs.has(l)&&!(l==="x"&&t.xIsTen))return{key:`sex:${l}${c}`,via:n,version:null,opaque:!1};if(t.roman){const b=Ks(a);if(b>0)return n.push("roman"),{key:`num:${b}${c}`,via:n,version:null,opaque:!1}}return{key:`raw:${l}${c}`,via:n,version:null,opaque:!0}}function ks(e){return pu.indexOf(e)}function ku(e,t){let n="case";for(const s of[...e.via,...t.via])ks(s)>ks(n)&&(n=s);return n}function No(e,t){return e===void 0||t===void 0||e<=0||t<=0?"unknown":e===t?"agree":"differ"}const Ai=new WeakMap;class we{names;context;styleCache=null;get style(){return this.styleCache??=Cu(this.names,this.context),this.styleCache}exact=new Map;folded=new Map;keyed=new Map;constructor(t){this.names=t.map(n=>n.name),this.context=Oe(this.names);for(const n of t){this.exact.has(n.name)||this.exact.set(n.name,n),yi(this.folded,n.name.toLowerCase(),n);const s=ds(n.name,this.context);yi(this.keyed,s.key,{sequence:n,canon:s})}}static of(t){return new we(t)}static ofNames(t){return new we(t.map(n=>({name:n})))}static for(t){const n=Ai.get(t);if(n!==void 0)return n;const s=typeof t[0]=="string"?we.ofNames(t):we.of(t);return Ai.set(t,s),s}has(t){return this.exact.has(t)}find(t,n={}){const{length:s}=n,o=this.exact.get(t);if(o!==void 0)return{name:o.name,asked:t,tier:"exact",lengths:No(s,o.length)};const r=this.folded.get(t.toLowerCase());if(r!==void 0&&r.length===1)return{name:r[0].name,asked:t,tier:"case",lengths:No(s,r[0].length)};const i=n.from??this.context,a=this.byKey(t,ds(t,i),s);if(a!==null)return a;if(!i.roman){const c=jc(t).toLowerCase();if(c!=="x"&&c!=="l"&&Ks(c)>0)return this.byKey(t,ds(t,{...i,roman:!0,xIsTen:!1}),s)}return null}byKey(t,n,s){const o=this.keyed.get(n.key);if(o===void 0||o.length===0)return null;const r=Su(o,s);if(r===null)return null;const i=ku(n,r.canon),a=n.version!==null&&r.canon.version!==null&&n.version!==r.canon.version;return{name:r.sequence.name,asked:t,tier:a&&ks(i)<ks("version")?"version":i,lengths:No(s,r.sequence.length)}}}function Su(e,t){if(e.length===1)return e[0];if(t===void 0||t<=0)return null;const n=e.filter(s=>s.sequence.length===t);return n.length===1?n[0]:null}function yi(e,t,n){const s=e.get(t);s===void 0?e.set(t,[n]):s.push(n)}function Cu(e,t=Oe(e)){if(e.length===0)return"empty";let n=0,s=0,o=0,r=0;for(const a of e){const c=ds(a,t);if(c.opaque){r++;continue}c.via.includes("accession")?o++:Yc.test(Er(a))?n++:s++}const i=n+s+o;return i===0||r>i*4?"assembly":t.roman&&n<=s?"roman":o>n&&o>s?"accession":n>0&&s>0?"mixed":n>0?t.roman?"roman":"ucsc":t.roman?"roman":"ensembl"}function Ss(e,t){const n=Fu(e),s=n.get(t);if(s!==void 0)return s;const o=we.for(e).find(t);return o===null?-1:n.get(o.name)??-1}const Ni=new WeakMap;function Fu(e){const t=Ni.get(e);if(t!==void 0)return t;const n=new Map;return e.forEach((s,o)=>{const r=typeof s=="string"?s:s.name;n.has(r)||n.set(r,o)}),Ni.set(e,n),n}let fn=null;function Fn(){const e=globalThis;return typeof e.window<"u"?!1:typeof e.process<"u"&&e.process?.versions?.node!=null}function Xe(){return fn||(fn=$u().catch(e=>{throw fn=null,new T("not-hdf5",`Could not initialise the HDF5 (h5wasm) runtime: ${Eu(e)}`,{runtime:Fn()?"node":"browser"})}),fn)}async function $u(){let e;return Fn()?e=await import("h5wasm/node"):e=await import("./hdf5_hl-8FoHa7pu.js"),await e.ready,{File:e.File,Group:e.Group,Dataset:e.Dataset,FS:e.FS,Module:e.Module}}function Eu(e){return e instanceof Error?`${e.name}: ${e.message}`:String(e)}const Bu="bhilnpq",Tu="BHILNPQu",Ou="defg";function tl(e){const t=e.trim(),n=()=>{throw new ct(`cannot interpret the HDF5 dtype ${JSON.stringify(e)} as a numpy kind`,{dtype:e})};t===""&&n();const s="<>|=".includes(t[0])?t[0]:"",o=s===""?t:t.slice(1);o===""&&n();const r=o[0],i=o.slice(1);switch(i!==""&&!/^\d+$/.test(i)&&n(),r){case"A":case"S":return i===""?"O":"S";case"U":return"U";case"O":return"O";case"?":return"b";case"b":return s==="|"?"b":"i";default:return Bu.includes(r)?"i":Tu.includes(r)?"u":Ou.includes(r)?"f":n()}}const rr=new Uint8Array(new Uint32Array([1]).buffer)[0]===1?0:1;function ir(e){const t=new Float64Array(e.length);if(e instanceof BigInt64Array||e instanceof BigUint64Array){const n=e.length,s=new Uint32Array(e.buffer,e.byteOffset,n*2),o=e instanceof BigInt64Array?new Int32Array(e.buffer,e.byteOffset,n*2):s,r=rr,i=1-rr;for(let a=0;a<n;a++)t[a]=o[2*a+i]*4294967296+s[2*a+r];return t}return t.set(e),t}function el(e){const t=e.length,n=new Int32Array(e.buffer,e.byteOffset,t*2),s=new Int32Array(t),o=rr;for(let r=0;r<t;r++)s[r]=n[2*r+o];return s}const Lu=new TextDecoder("utf-8");function ar(e,t){const n=e.Module.H5T_class_t;return t.type===n.H5T_STRING.value?"string":t.type===n.H5T_INTEGER.value||t.type===n.H5T_FLOAT.value||t.type===n.H5T_ENUM.value?"number":"other"}function _u(e,t,n,s,o){const r=o-s;if(r<=0)return new Uint8Array(0);const a=n.metadata.size*r,c=Br(e,a);try{return e.Module.get_dataset_data(t.file_id,n.path,[BigInt(r)],[BigInt(s)],[1n],BigInt(c)),e.Module.HEAPU8.slice(c,c+a)}finally{e.Module._free(c)}}function Du(e,t,n){const s=new Array(n);for(let o=0;o<n;o++){let r=e.subarray(o*t,(o+1)*t);const i=r.indexOf(0);i>-1&&(r=r.subarray(0,i)),s[o]=Lu.decode(r)}return s}function nl(e,t,n,s,o){const r=o-s;if(r<=0)return[];const i=n.metadata;if(!i.vlen)return Du(_u(e,t,n,s,o),i.size,r);const a=i.size*r,c=Br(e,a);try{e.Module.get_dataset_data(t.file_id,n.path,[BigInt(r)],[BigInt(s)],[1n],BigInt(c));const l=new Uint32Array(e.Module.HEAPU8.slice(c,c+a).buffer),u=new Array(r);for(let h=0;h<r;h++)u[h]=e.Module.UTF8ToString(l[h]);return u}finally{e.Module.reclaim_vlen_memory_count(t.file_id,n.path,"",BigInt(c),[BigInt(r)]),e.Module._free(c)}}function Ru(e,t){if(t.littleEndian===!1||t.vlen)return null;const n=e.Module.H5T_class_t;if(t.type===n.H5T_INTEGER.value)return t.size===8?t.signed?"i64":"u64":t.size===4?t.signed?"i32":"u32":null;if(t.type===n.H5T_FLOAT.value){if(t.size===8)return"f64";if(t.size===4)return"f32"}return null}function sl(e,t,n,s,o){const r=t.metadata,i=Ru(e.h5,r);if(i===null)return null;const a=s-n,{Module:c}=e.h5,l=Br(e.h5,r.size*a);try{return c.get_dataset_data(e.file.file_id,t.path,[BigInt(a)],[BigInt(n)],[1n],BigInt(l)),o(c.HEAPU8.buffer,l,a,i)}finally{c._free(l)}}function zu(e,t,n,s){switch(s){case"f64":return new Float64Array(e,t,n).slice();case"i64":case"u64":return ir(s==="i64"?new BigInt64Array(e,t,n):new BigUint64Array(e,t,n));default:{const o=new Float64Array(n);return o.set(s==="i32"?new Int32Array(e,t,n):s==="u32"?new Uint32Array(e,t,n):new Float32Array(e,t,n)),o}}}function Pu(e,t,n,s){switch(s){case"i32":return new Int32Array(e,t,n).slice();case"i64":case"u64":return el(s==="i64"?new BigInt64Array(e,t,n):new BigUint64Array(e,t,n));default:{const o=new Int32Array(n);return o.set(s==="u32"?new Uint32Array(e,t,n):s==="f64"?new Float64Array(e,t,n):new Float32Array(e,t,n)),o}}}function ol(e,t,n,s){if(n-t<=0)return new Float64Array(0);if(s!==void 0){const i=sl(s,e,t,n,zu);if(i!==null)return i}const r=e.slice([[t,n]]);if(r instanceof Float64Array)return r;if(r instanceof BigInt64Array||r instanceof BigUint64Array||ArrayBuffer.isView(r)&&!(r instanceof DataView))return ir(r);throw new ct(`/${e.path.replace(/^\//,"")} did not read back as a numeric array`,{path:e.path})}function qu(e,t,n,s){const o=n-t;if(o<=0)return new Int32Array(0);if(s!==void 0){const i=sl(s,e,t,n,Pu);if(i!==null)return i}const r=e.slice([[t,n]]);if(r instanceof Int32Array)return r;if(r instanceof BigInt64Array||r instanceof BigUint64Array)return el(r);if(ArrayBuffer.isView(r)&&!(r instanceof DataView)){const i=new Int32Array(o);return i.set(r),i}throw new ct(`/${e.path.replace(/^\//,"")} did not read back as a numeric array`,{path:e.path})}function on(e,t,n){return{length:e,columns:t,data:n,get(s,o){const r=n[s];if(r===void 0||o<0||o>=e)return;const i=r[o];if(typeof i=="bigint")return Number(i);if(typeof i=="string"||typeof i=="number")return i},strings(s){const o=n[s];if(o===void 0)throw new ct(`no such column: ${s}`,{column:s});if(Array.isArray(o)&&(o.length===0||typeof o[0]=="string"))return o;throw new ct(`column ${s} is not a string column`,{column:s})},numbers(s){const o=n[s];if(o===void 0)throw new ct(`no such column: ${s}`,{column:s});if(o instanceof BigInt64Array)return Array.from(o,Number);if(Array.isArray(o)&&o.length>0&&typeof o[0]=="string")throw new ct(`column ${s} is not a numeric column`,{column:s});return Array.from(o)}}}function Uu(e,t,n,s,o,r,i){const a=[];for(const g of n.keys()){const p=n.get(g);if(!(p instanceof e.Dataset))continue;const w=p.metadata,b=w.shape;!b||b.length!==1||a.push({name:g,ds:p,md:w,length:b[0]})}if(new Set(a.map(g=>g.length)).size>1){const g=a.map(p=>`${p.name}=${p.length}`).sort().join(", ");throw new ct(`/${s} columns have unequal lengths (${g}); every column of a table must have one row per record`,{table:s})}const l=a.length===0?0:a[0].length,u=Mi(o??0,0,l),h=Mi(r??l,u,l),f=i===void 0?null:new Set(i),d=[],m={};for(const{name:g,ds:p,md:w}of a)if(!(f!==null&&!f.has(g)))switch(ar(e,w)){case"string":d.push(g),m[g]=nl(e,t,p,u,h);break;case"number":d.push(g),m[g]=ol(p,u,h,{h5:e,file:t});break}return on(h-u,d,m)}function Mi(e,t,n){return Math.min(Math.max(Math.trunc(e),t),n)}function Br(e,t){if(t>e.Module.MAXIMUM_MEMORY)throw new ct(`a ${t} byte read exceeds the wasm heap limit of ${e.Module.MAXIMUM_MEMORY} bytes`,{nbytes:t,limit:e.Module.MAXIMUM_MEMORY});const n=e.Module._malloc(t);if(n===0)throw new ct(`allocation of ${t} bytes in the wasm heap failed`,{nbytes:t});return n}function Pt(e){const t=e.indexOf("::");let n,s;if(t!==-1){n=e.slice(0,t).trim();const o=e.slice(t+2).trim();!o||o==="/"?s="/":s=o.startsWith("/")?o:`/${o}`}else n=e.trim(),s="/";if(!n)throw new T("bad-uri",`URI has an empty file path: ${st(e)}`,{uri:e});return{path:n,group:s}}function Gu(e,t){return t==="/"?e:`${e}::${t}`}const Hu=["genomes","contigs","bins","pixels","indexes"],Qu=["bin-size","format-version","nbins","nnz","sum","ngenomes","ncontigs"];function Ot(e,t){const n=e[t];return typeof n=="string"?n:null}function ms(e,t){const n=e[t];if(n==null)return null;const s=()=>{throw new ct(`attrs[${st(t)}] must be an integer, got ${st(n)}`,{key:t,value:n})};if(typeof n=="string"){if(hu.includes(n.trim().toLowerCase()))return null;const o=Kc(n);return o===null?s():o}return typeof n=="boolean"?s():typeof n=="number"&&Number.isInteger(n)?n:s()}function rl(e){const t=e["storage-mode"];if(t==null)return"symmetric-upper";if(typeof t=="string"&&pi.includes(t))return t;throw new ct(`attrs['storage-mode'] must be one of ${Gt([...pi].sort())}, got ${st(t)}`,{"storage-mode":t})}function il(e){return e.has("entries")?e.keys("entries"):[]}function Ku(e,t){return $r.some(n=>!e.has(`${t}/${n}`))?!1:Ot(e.attrsAt(t),"format")===Fr}function Vu(e,t){if(Ot(t,"format")===xn)return e.has(fs);if(Ot(t,"metacooler-format")===Is)return!1;const n=Ot(t,"format");return n!==null&&n!==Fr||$r.some(s=>e.has(s))||e.has("chroms")?!1:!e.has("resolutions")&&!e.has("entries")}function Yu(e,t){if(Ot(t,"format")===xn){const s=e.keys(fs);if(s.length>0)return{names:s,group:fs};throw new T("not-a-cooler",`${e.uri} declares format ${st(xn)} but has no cells under /${fs}. Top-level keys: ${Gt(e.keys("").slice(0,20))}.`,{format:xn,group:e.group})}if($r.some(s=>e.has(s)))return null;const n=e.keys("");return n.length===0?null:Ku(e,n[0])?{names:n,group:""}:null}function Wu(e,t,n={}){const s=t??e.attrs(),o=Ot(s,"format");if(n.metacool===!1&&Xu(e,s,o)&&Ii(e),o===Vc&&e.has("entries"))return{kind:"mmetacool",group:e.group,entries:il(e)};if(Ot(s,"metacooler-format")===Is){const a=Hu.filter(c=>!e.has(c));if(a.length===0)return{kind:"metacool",group:e.group};throw new T("not-a-cooler",`${e.uri} declares metacooler-format ${st(Is)} but is missing ${Gt(a.map(c=>`/${c}`))}. It cannot be read as a metacool, and reading it as the plain cooler it also is would drop the genome layer it says it has. If this came out of \`cooler zoomify\`, re-derive the resolutions with \`metacooler zoomify\` instead.`,{missing:[...a]})}const r=e.keys("resolutions");if(r.length>0)return{kind:"mcool",group:e.group,entries:al(r)};const i=Yu(e,s);if(i!==null)return{kind:"scool",group:e.group,entries:i.names,cellGroup:i.group};if(o===Fr)return{kind:"cool",group:e.group};throw n.metacool===!1&&Ii(e),new T("not-a-cooler",`${e.uri} is HDF5 but not a cooler or metacool file: its 'format' attribute is ${st(o)} and 'metacooler-format' is ${st(Ot(s,"metacooler-format"))}. Top-level keys: ${Gt(e.keys("").slice(0,20))}. Convert a plain cooler with \`metacooler create cool\`.`,{format:o,group:e.group})}function Xu(e,t,n){return n===Vc&&e.has("entries")||Ot(t,"metacooler-format")===Is}function Ii(e){throw new T("not-a-cooler",`${e.uri} is an HDF5 file, but not a contact map this viewer reads: it is not a cooler, an .mcool or an .scool. Top-level keys: ${Gt(e.keys("").slice(0,20))}.`,{group:e.group})}function Ju(e){const{path:t}=Pt(e.uri),n=il(e),s=n.map(r=>`/entries/${r}`),o=s.length>0?s[0]:"/entries/<name>";throw new T("mmetacool-root",`No metacool entry at root of ${t}. Metacool entries found: ${Gt(s)}. Use '::' to specify a group path, e.g. ${t}::${o}.`,{path:t,entries:n})}function al(e){return e.length>0&&e.every(n=>/^\d+$/.test(n)&&Number(n)>0)?[...e].sort((n,s)=>Number(n)-Number(s)):e}function Zu(e){const t=[...e].sort((n,s)=>n-s);return t[Math.floor(t.length/3)]}function ju(e,t){const{path:n}=Pt(e.uri),s=t.map(r=>`/resolutions/${r}`),o=s.length>0?s[0]:"/resolutions/<binsize>";throw new T("mcool-root",`No cooler at root of ${n}: an .mcool is a collection of resolutions, each a complete cooler. Resolutions found: ${Gt(s)}. Use '::' to pick one, e.g. ${n}::${o}.`,{path:n,entries:t,group:"resolutions"})}function Cs(e,t){const n=e.cellGroup??"";return n===""?t:`${n}/${t}`}function cl(e,t,n){if(ms(n,"bin-size")!==null)return{attrs:n,from:null};const s=t.entries?.[0];if(s===void 0)return{attrs:n,from:null};const o=Cs(t,s);return{attrs:e.attrsAt(o),from:o}}function t2(e,t){if(!((t.entries??[]).length>0))throw new T("not-a-cooler",`${Pt(e.uri).path} is a single-cell cooler container with no cells in it.`,{path:Pt(e.uri).path})}function e2(e,t,n,s={}){t.kind==="mmetacool"&&Ju(e),t.kind==="mcool"&&ju(e,t.entries??[]);const o=n??e.attrs();if(t.kind==="metacool"){const c=ms(o,"metacooler-format-version");if(c!==null&&c!==wo)throw new T("bad-format-version",`This file declares metacooler-format-version ${c}; the viewer only understands version ${wo}. Re-create it with a matching metacooler release, or open it with the version of the viewer that reads ${c}.`,{found:c,expected:wo});const l=Ot(o,"contig-ordering");if(l!==Vn)throw new T("bad-ordering",`This file's contig-ordering is ${st(l)}; the viewer requires ${st(Vn)}. Only a genome-grouped layout puts every genome in one contiguous block of bins, which is what makes a genome or a genome pair a single bounded read. Re-create the file with \`metacooler create pairs --contig-order ${Vn}\`.`,{found:l,expected:Vn})}const r=t.kind==="scool"?cl(e,t,o):{attrs:o};if(t.kind==="scool"){t2(e,t);const c=ms(o,"format-version");if(Ot(o,"format")===xn&&c!==null&&c!==bo)throw new T("bad-format-version",`This file declares scool format-version ${c}; the viewer understands version ${bo}. Open it with a viewer that reads ${c}, or re-create it with \`cooler.create_scool\`.`,{found:c,expected:bo})}const i=Ot(r.attrs,"bin-type"),a=ms(r.attrs,"bin-size");if(i==="variable"||a===null||a<=0)throw new T("variable-bins",s.metacool===!1?`The viewer supports fixed-bin files only; this file's bin-type is ${st(i)} and its bin-size is ${st(a)}. Bin its contacts at a fixed size — \`cooler cload pairs\` does, from the pairs it was made from — and open that.`:`The viewer supports fixed-bin files only; this file's bin-type is ${st(i)} and its bin-size is ${st(a)}. Fragment-level and contig-level metacools are variable-bin by construction. Produce a fixed-bin file with \`metacooler binnify --binsize <bp>\` and open that.`,{"bin-type":i,"bin-size":a});rl(r.attrs)}function ll(e){if(typeof e!="string")return e;try{return JSON.parse(e)}catch{return e}}function n2(e){if(typeof e=="number")return Number.isFinite(e)?Math.trunc(e):e;if(typeof e=="string"){const t=Kc(e);return t===null?e:t}return e}function Ne(e){return typeof e=="number"&&Number.isFinite(e)?e:null}function dn(e){return typeof e=="string"?e:null}function s2(e){const t={};for(const[w,b]of Object.entries(e))t[w]=ll(b);const n=w=>{const b=t[w];return delete t[w],b},s=n("metacooler-format"),o=n("metacooler-format-url"),r=n("metacooler-format-version"),i=s!=null,a=i?s:t.format,c=i?o:t["format-url"],l=i?r:t["format-version"],u=n("contig-ordering"),h=n("nchroms"),f=n("assembly-name"),d=n("assembly"),m=n("genome-assembly");let g=m!==void 0?m:d;(g==null||g===""||g==="unknown")&&f!==void 0&&f!==null&&(g=f),n("metadata");const p={"bin-size":t["bin-size"],"format-version":l,nbins:t.nbins,nnz:t.nnz,sum:t.sum,ngenomes:t.ngenomes,ncontigs:h};for(const w of Qu)p[w]!==void 0&&(p[w]=n2(p[w]));return{...t,format:dn(a),"format-url":dn(c),"format-version":Ne(p["format-version"]),"bin-size":Ne(p["bin-size"]),"bin-type":dn(t["bin-type"]),ordering:dn(u),assembly:dn(g),"storage-mode":rl(e),nbins:Ne(p.nbins),nnz:Ne(p.nnz),sum:Ne(p.sum),ngenomes:Ne(p.ngenomes),ncontigs:Ne(p.ncontigs)}}function o2(e){const t=ll(e.metadata);return t!==null&&typeof t=="object"&&!Array.isArray(t)?t:{}}const r2=new TextDecoder("utf-8");async function i2(e,t,n){const s=await Xe(),o=await t.mount();let r;try{r=new s.File(o,"r")}catch(l){throw t.close(),new T("not-hdf5",`${e} could not be opened as an HDF5 file: ${a2(l)}`,{uri:e,source:t.id})}const i=ul(n);try{if(i!=="/"){const l=r.get(i);if(l===null)throw new T("entry-not-found",`Group '${i}' not found in ${t.id}. Top-level keys: ${We(r.keys())}`,{uri:e,group:i,available:r.keys()});if(!(l instanceof s.Group))throw new ct(`'${i}' in ${t.id} is not an HDF5 group`,{uri:e,group:i},"entry-not-found")}}catch(l){throw r.close(),t.close(),l}let a=[],c="";try{for(const l of["entries","resolutions"]){if(r.get_type(`/${l}`)<0)continue;const u=r.get(l);if(u instanceof s.Group){a=al(u.keys()),c=l;break}}}catch{}return new Tr(e,i,s,r,t,a,c)}class Tr{uri;group;rootEntries;rootEntryGroup;h5;file;heap;source;closed=!1;borrowed;constructor(t,n,s,o,r,i=[],a="",c=!1){this.uri=t,this.group=n,this.rootEntries=i,this.rootEntryGroup=a,this.borrowed=c,this.h5=s,this.file=o,this.heap={h5:s,file:o},this.source=r}attrs(){return this.attrsAt("")}attrsAt(t){const n=this.entity(t),s=n instanceof this.h5.Group||n instanceof this.h5.Dataset;if(n===null||!s)throw new ct(`${this.describe(t)} does not exist, or carries no attributes`,{uri:this.uri,path:this.resolve(t)});const o={};for(const[r,i]of Object.entries(n.attrs))o[r]=this.decodeAttr(n.path,r,i.metadata,i.json_value);return o}has(t){return this.assertOpen(),this.file.get_type(this.resolve(t))>=0}keys(t){const n=this.entity(t);return n===null||!(n instanceof this.h5.Group)?[]:n.keys()}isSoftLink(t){if(!this.has(t))return!1;const n=this.file.get_link(this.resolve(t));return typeof n=="string"&&n.length>0}datasetLength(t){const n=this.dataset(t).metadata.shape;return n&&n.length>0?n[0]:0}dtype(t){const n=this.dataset(t).dtype;if(typeof n!="string")throw new ct(`${this.describe(t)} has a compound datatype, which this reader does not read`,{uri:this.uri,path:this.resolve(t)});return n}readNumeric(t,n,s){const[o,r,i]=this.numericRange(t,n,s);return ol(o,r,i,this.heap)}readInt32(t,n,s){const[o,r,i]=this.numericRange(t,n,s);return qu(o,r,i,this.heap)}numericRange(t,n,s){const o=this.dataset(t);if(ar(this.h5,o.metadata)!=="number")throw new ct(`${this.describe(t)} is ${String(o.dtype)}, not a numeric dataset`,{uri:this.uri,path:this.resolve(t),dtype:String(o.dtype)});const[r,i]=this.range(o,n,s);return[o,r,i]}readStrings(t,n,s){const o=this.dataset(t),r=o.metadata;if(ar(this.h5,r)!=="string")throw new ct(`${this.describe(t)} is ${String(o.dtype)}, not a string dataset`,{uri:this.uri,path:this.resolve(t),dtype:String(o.dtype)});const[i,a]=this.range(o,n,s);return nl(this.h5,this.file,o,i,a)}readTable(t,n,s,o){const r=this.entity(t);if(r===null||!(r instanceof this.h5.Group))throw new ct(`${this.describe(t)} is not a table group`,{uri:this.uri,path:this.resolve(t)});const i=this.resolve(t).slice(this.group==="/"?1:this.group.length+1);return Uu(this.h5,this.file,r,i,n,s,o)}at(t){const n=this.resolve(t);return new Tr(this.uri,n,this.h5,this.file,this.source,this.rootEntries,this.rootEntryGroup,!0)}close(){if(!(this.closed||this.borrowed)){this.closed=!0;try{this.file.close()}finally{this.source.close()}}}resolve(t){const n=t.replace(/^\/+/,"");return n===""?this.group:ul(this.group==="/"?`/${n}`:`${this.group}/${n}`)}describe(t){return`${this.uri} :: ${this.resolve(t)}`}entity(t){this.assertOpen();const n=this.resolve(t);return n==="/"?this.file:this.file.get(n)}dataset(t){const n=this.entity(t);if(n===null)throw new ct(`${this.describe(t)} does not exist`,{uri:this.uri,path:this.resolve(t)});if(!(n instanceof this.h5.Dataset))throw new ct(`${this.describe(t)} is not a dataset`,{uri:this.uri,path:this.resolve(t)});return n}range(t,n,s){const o=t.metadata.shape;if(!o||o.length!==1)throw new ct(`/${t.path.replace(/^\//,"")} is ${o?`${o.length}-D`:"scalar"}; this reader only slices 1-D columns`,{path:t.path,shape:o});const r=o[0],i=Math.min(Math.max(Math.trunc(n??0),0),r),a=Math.min(Math.max(Math.trunc(s??r),i),r);return[i,a]}decodeAttr(t,n,s,o){if(s.type===this.h5.Module.H5T_class_t.H5T_STRING.value&&!s.vlen){const r=this.readFixedStringAttr(t,n,s);if(r!==null)return r}return c2(o)}readFixedStringAttr(t,n,s){const o=s.total_size,r=s.size*o;if(r<=0)return null;const i=this.h5.Module,a=i._malloc(r);if(a===0)return null;let c;try{i.get_attribute_data(this.file.file_id,t,n,BigInt(a)),c=i.HEAPU8.slice(a,a+r)}finally{i._free(a)}const l=[];for(let h=0;h<o;h++){let f=c.subarray(h*s.size,(h+1)*s.size);const d=f.indexOf(0);d>-1&&(f=f.subarray(0,d)),l.push(r2.decode(f))}const u=s.shape;return u&&u.length>0?l:l[0]??""}assertOpen(){if(this.closed)throw new ct(`${this.uri} is closed`,{uri:this.uri})}}function a2(e){return e instanceof Error?e.message:String(e)}function ul(e){const t=`/${e}`.replace(/\/+/g,"/").replace(/(.)\/$/,"$1");return t===""?"/":t}function c2(e){if(e==null)return null;if(typeof e=="bigint")return Number(e);if(typeof e=="string"||typeof e=="number"||typeof e=="boolean")return e;if(Array.isArray(e)){if(e.every(n=>typeof n=="string"))return e;const t=n=>typeof n=="number"||typeof n=="boolean"||typeof n=="bigint";return e.every(t)?e.map(n=>Number(n)):e.map(n=>String(n))}return null}const He=4096,Fs={binsPerCell:1,contigsPerCell:1};function hl(e,t,n,s=Fs,o){const r=Math.max(0,Math.trunc(e));if(r===0)return{slot:new Int32Array(0),cellStart:new Int32Array(1),cellSegments:new Int32Array(0),cells:0,plan:Fs};const i=t.length>0?t.length:1,a=Math.max(1,Math.trunc(n)),c=h=>t.length>0?t[h]:0,l=h=>(h+1<i?c(h+1):r)-c(h),u=Math.max(s.binsPerCell,fl(i,l,a,o));return u2(r,i,c,l,u,o)}function l2(e,t,n,s){let o=0,r=0,i=-1;for(let a=0;a<e;a++){const c=t(a);if(c>=n){o+=Math.ceil(c/n),r=0,i=-1;continue}const l=s===void 0?0:s[a];r===0||r+c>n||l!==i?(o++,r=c,i=l):r+=c}return o}function fl(e,t,n,s){let o=s;const r=c=>l2(e,t,c,o)<=n;let i=1;for(;!r(i);)if(i>=Number.MAX_SAFE_INTEGER/2){if(o===void 0)return i;o=void 0,i=1}else i*=2;let a=1;for(;a<i;){const c=a+i>>1;r(c)?i=c:a=c+1}return a}function u2(e,t,n,s,o,r){const i=new Int32Array(e),a=[],c=[];let l=0,u=0,h=-1;for(let d=0;d<t;d++){const m=n(d),g=s(d),p=m+g;if(g>=o){u=0,h=-1;for(let b=m;b<p;b++)(b-m)%o===0&&(a.push(b),c.push(1),l++),i[b]=l-1;continue}const w=r===void 0?0:r[d];u===0||u+g>o||w!==h?(a.push(m),c.push(1),l++,u=g,h=w):(u+=g,c[l-1]++);for(let b=m;b<p;b++)i[b]=l-1}a.push(e);let f=1;for(const d of c)f=Math.max(f,d);return{slot:i,cellStart:Int32Array.from(a),cellSegments:Int32Array.from(c),cells:l,plan:{binsPerCell:o,contigsPerCell:f}}}function h2(e){const t=[];for(let n=0;n<e.length;n++)(n===0||e[n]!==e[n-1])&&t.push(n);return Int32Array.from(t)}function xi(e,t,n,s=Fs,o=He,r){const i=Math.max(0,n-t);if(i===0)return{slot:new Int32Array(0),ids:new Int32Array(0),stride:1,contigsPerCell:1,cellStart:new Int32Array(1),members:new Int32Array(0),memberStart:new Int32Array(1)};const a=e.contigsOfBins(t,n),c=h2(a),l=r===void 0||r.length===0?void 0:Int32Array.from(c,m=>r[a[m]]??-1),u=hl(i,c,o,s,l),h=new Int32Array(u.cells),f=[],d=new Int32Array(u.cells+1);for(let m=0;m<u.cells;m++){d[m]=f.length;const g=u.cellStart[m],p=u.cellStart[m+1];h[m]=a[g];for(let w=g;w<p;w++)(w===g||a[w]!==a[w-1])&&f.push(a[w])}return d[u.cells]=f.length,{slot:u.slot,ids:h,stride:u.plan.binsPerCell,contigsPerCell:u.plan.contigsPerCell,cellStart:u.cellStart,members:Int32Array.from(f),memberStart:d}}function f2(e,t,n,s=He,o){const r=t.map(({extent:y})=>Math.max(0,y[1]-y[0])),i=r.reduce((y,A)=>y+A,0),a=o===void 0?void 0:Int32Array.from(t,({id:y})=>o[y]??-1),c=Math.max(n.binsPerCell,fl(r.length,y=>r[y],Math.max(1,Math.trunc(s)),a)),l=new Int32Array(e).fill(-1),u=[],h=[],f=[],d=[];let m=0,g=0,p=-1,w=1,b=0;for(let y=0;y<t.length;y++){const{id:A,extent:N}=t[y],[M,I]=N,x=r[y];if(x===0)continue;const v=a===void 0?0:a[y],S=x>=c;for(let k=M;k<I;k++){const C=k-M;(S?C%c===0:C===0&&(g===0||g+x>c||v!==p))?(u.push(A),h.push(m),d.push(f.length),f.push(A),g=0,p=v,b=0):C===0&&f.push(A),l[k]=u.length-1,m++}S?(g=0,p=-1,b=0):(g+=x,b++,w=Math.max(w,b))}return h.push(m),d.push(f.length),{slot:l,ids:Int32Array.from(u),stride:c,contigsPerCell:w,cellStart:Int32Array.from(h),members:Int32Array.from(f),memberStart:Int32Array.from(d),bins:i}}function dl(e){const t=new Int32Array(e.length),n=new Map,s=[];for(let o=0;o<e.length;o++){const r=e[o];let i=n.get(r);i===void 0&&(i=s.length,n.set(r,i),s.push(r)),t[o]=i}return{slot:t,ids:Int32Array.from(s)}}function d2(e,t,n,s,o=!1){const r=t.ids.length,i=n.ids.length,a=new Float64Array(r*i),c=o&&e.nrows===e.ncols;for(let l=0;l<e.nrows;l++){const u=t.slot[l],h=l*e.ncols;for(let f=c?l:0;f<e.ncols;f++){const d=n.slot[f],m=c&&u>d?d*i+u:u*i+d,g=e.values[h+f];Number.isNaN(g)||(a[m]+=g)}}if(c)for(let l=0;l<r;l++)for(let u=l+1;u<i;u++)a[u*i+l]=a[l*i+u];return{nrows:r,ncols:i,values:a,rowIds:t.ids,colIds:n.ids,level:s}}function ml(e,t,n){return dl(e.contigsOfBins(t,n))}function gl(e,t,n,s){const o=e.contigsOfBins(n,s),r=new Int32Array(o.length);for(let i=0;i<o.length;i++)r[i]=t[o[i]];return dl(r)}function vi(e,t,n){const s=e.ids.length;if(s<=t)return{grouping:e,perCell:1};const o=Int32Array.from({length:s},(l,u)=>u),r=n===void 0?void 0:Int32Array.from(e.ids,n),i=hl(s,o,t,Fs,r),a=new Int32Array(e.slot.length);for(let l=0;l<a.length;l++){const u=e.slot[l];a[l]=u<0?-1:i.slot[u]}const c=new Int32Array(i.cells);for(let l=0;l<i.cells;l++)c[l]=e.ids[i.cellStart[l]];return{grouping:{slot:a,ids:c},perCell:i.plan.contigsPerCell}}function m2(e,t,n,s,o,r,i=!0,a=Number.POSITIVE_INFINITY){const c=m=>o==="contig"?ml(n,m[0],m[1]):gl(n,s,m[0],m[1]),l=e[0]===t[0]&&e[1]===t[1],u=o==="contig"&&s.length>0?m=>s[m]??-1:void 0,h=vi(c(e),a,u),f=l?h:vi(c(t),a,u),d=cr(e,t,h.grouping,f.grouping,r,i);return{place:d.place,finish:()=>({...d.finish(),level:o}),perCell:Math.max(h.perCell,f.perCell)}}function cr(e,t,n,s,o,r=!0,i=!1){const c=e[0]===t[0]&&e[1]===t[1]&&r,l=n.ids.length,u=s.ids.length,h=i&&c&&l===u,f=new Float64Array(h?l*(l+1)/2:l*u),[d,m]=e,[g,p]=t,w=o!=="transpose",b=o!=="direct"&&!c,y=o==="symmetric",A=(N,M,I)=>{const x=n.slot[N],v=s.slot[M];if(!(x<0||v<0)){if(h){const S=x<v?x:v,k=x<v?v:x;f[S*l-S*(S-1)/2+(k-S)]+=I;return}f[c&&x>v?v*u+x:x*u+v]+=I}};return{place(N,M,I){Number.isNaN(I)||(w&&N>=d&&N<m&&M>=g&&M<p&&A(N-d,M-g,I),b&&!(y&&N===M)&&M>=d&&M<m&&N>=g&&N<p&&A(M-d,N-g,I))},finish(){if(h){const N=new Float32Array(l*l);let M=0;for(let I=0;I<l;I++)for(let x=I;x<l;x++,M++){const v=f[M];N[I*l+x]=v,N[x*l+I]=v}return{nrows:l,ncols:u,values:N,rowIds:n.ids,colIds:s.ids}}if(c)for(let N=0;N<l;N++)for(let M=N+1;M<u;M++)f[M*u+N]=f[N*u+M];return{nrows:l,ncols:u,values:f,rowIds:n.ids,colIds:s.ids}}}}function g2(e,t,n,s,o=!0){const r=l=>s==="contig"?ml(t,l[0],l[1]):gl(t,n,l[0],l[1]),i=e.rowExtent[0]===e.colExtent[0]&&e.rowExtent[1]===e.colExtent[1],a=r(e.rowExtent),c=i?a:r(e.colExtent);return d2(e,a,c,s,i&&o)}function p2(e,t){let n=0,s=e.length-1;for(;n<s;){const o=n+s+1>>1;e[o]<=t?n=o:s=o-1}return n}class w2{binsize;chromOffset;contigLengths;constructor(t,n,s){this.binsize=t,this.chromOffset=n,this.contigLengths=s}get nContigs(){return this.contigLengths.length}get nBins(){return this.chromOffset.length>0?this.chromOffset[this.chromOffset.length-1]:0}binsInContig(t){return Math.ceil(this.contigLengths[t]/this.binsize)}contigStartBin(t){return this.chromOffset[t]}contigBinExtent(t){return[this.chromOffset[t],this.chromOffset[t+1]]}binIndex(t,n){return this.chromOffset[t]+Math.floor(n/this.binsize)}contigOfBin(t){return p2(this.chromOffset,t)}binStart(t){const n=this.contigOfBin(t);return(t-this.chromOffset[n])*this.binsize}binEnd(t){const n=this.contigOfBin(t),s=(t-this.chromOffset[n])*this.binsize;return Math.min(s+this.binsize,this.contigLengths[n])}contigsOfBins(t,n){const s=new Int32Array(Math.max(0,n-t));if(s.length===0)return s;let o=this.contigOfBin(t),r=this.chromOffset[o+1];for(let i=t;i<n;i++){for(;i>=r&&o+1<this.nContigs;)o+=1,r=this.chromOffset[o+1];s[i-t]=o}return s}}const mn=256,b2=["#FFFEF9","#FCF9CE","#FFF2A9","#FDE188","#FFCA67","#FAAA4B","#F78E40","#F15C34","#ED3024","#D42027","#B01F29","#7A1128","#1A0A10"],A2=["#FFFFFF","#FFF5F0","#FEE0D2","#FCBBA1","#FC9272","#FB6A4A","#EF3B2C","#CB181D","#A50F15","#67000D"],y2=["#053061","#2166AC","#4393C3","#92C5DE","#D1E5F0","#F7F7F7","#FDDBC7","#F4A582","#D6604D","#B2182B","#67001F"],N2=["#1659b1","#487BA8","#819EC8","#D1E6F1","#FFFFFF","#FAC1C1","#F1606C","#EE222A"],M2=[0,.125,.25,.375,.5,2/3,5/6,1],I2=["#00004D","#0000FF","#FFFFFF","#FF0000","#800000"];function gn(e,t,n){const s=e.map(a=>[Number.parseInt(a.slice(1,3),16),Number.parseInt(a.slice(3,5),16),Number.parseInt(a.slice(5,7),16)]),o=s.length-1,r=n??s.map((a,c)=>c/o),i=new Array(t*3);for(let a=0;a<t;a++){const c=a/(t-1);let l=0;for(;l<o-1&&c>=r[l+1];)l++;const u=r[l+1]-r[l],h=u>0?(c-r[l])/u:0;for(let f=0;f<3;f++)i[a*3+f]=Math.round(s[l][f]+(s[l+1][f]-s[l][f])*h)}return i}gn(b2,mn),gn(A2,mn),gn(y2,mn),gn(N2,mn,M2),gn(I2,mn);const pl="RdBu_r",x2=16384;function ki(e,t=x2){return e<=t?1:Math.ceil(e/t)}const ae="weight",tt="count",v2=new Set(["bin1_id","bin2_id"]);function k2(e){const t=e.keys("pixels").filter(n=>!v2.has(n));return[...t.filter(n=>n===tt),...t.filter(n=>n!==tt).sort()]}const Mo={range:"Use a narrower --range/--range2.",binSelection:"Use --range to select a smaller region, or --level contig."};function S2(e,t,n){if(e*t>gi**2){const s=e*t*8/1e9;throw new T("dense-too-large",`Refusing to allocate a ${e}×${t} dense matrix (${s.toFixed(1)} GB). ${n}`,{nRows:e,nCols:t,limit:gi})}}function C2(e,t){const n=e*t,s=Math.ceil(e/ki(e))*Math.ceil(t/ki(t));return n*4+s*12+E2}function wl(){const e=globalThis.navigator?.deviceMemory;return F2(typeof e=="number"&&e>0?e:cu)}function F2(e){const t=e*1073741824*au;return e>=uu?Math.max(lu,t):t}function $2(e,t,n,s={}){const o=e*t;if(o*4>=Kn){const a=Math.floor(Math.sqrt(Kn/4));throw new T("dense-too-large",`Refusing to load a ${e}×${t} block: its ${lr(o)} values are ${It.bytes(o*4)} and a browser serves at most ${It.bytes(Kn)} in a single array — a limit of the tab, not of this machine, so more memory and more patience will not lift it. The largest square block is about ${a}×${a} bins.`+(n===""?"":` ${n}`),{nRows:e,nCols:t,cells:o,bytes:o*4,limit:Kn,where:"array-limit"})}const r=s.budgetBytes??wl(),i=C2(e,t);if(!(i<=r))throw new T("dense-too-large",`Refusing to load a ${e}×${t} block in a browser tab: ${lr(o)} cells needs about ${It.bytes(i)}, and this tab is budgeting ${It.bytes(r)}. ${n}`,{nRows:e,nCols:t,cells:o,bytes:i,budget:r,where:"browser"})}function lr(e){return e>=1e9?`${(e/1e9).toFixed(1)} billion`:e>=1e6?`${(e/1e6).toFixed(0)} million`:String(e)}function Si(e){const t=tl(e);if(t==="i"||t==="u"||t==="f")return t;throw new T("format",`/pixels/count is stored as ${e} (kind '${t}'); expected one of the kinds ['i', 'u', 'f']. A non-numeric count column cannot be rendered.`,{dtype:e,kind:t})}const Vs=1e6,E2=Vs*36;class Or{constructor(t,n){this.offsets=t,this.firstBin=n}row=0;bin1(t,n){const s=new Int32Array(Math.max(0,n-t)),o=this.offsets,r=o.length-2;if(r<0)return s;t<o[this.row]&&this.seek(t);let i=this.row;for(let a=t;a<n;){for(;i<r&&o[i+1]<=a;)i++;const c=i<r?Math.min(n,o[i+1]):n,l=c>a?c:n;s.fill(this.firstBin+i,a-t,l-t),a=l}return this.row=i,s}seek(t){const n=this.offsets;let s=0,o=Math.max(0,n.length-2);for(;s<o;){const r=s+o+1>>1;n[r]<=t?s=r:o=r-1}this.row=s}}function B2(e,t,n){if(n<=t)return null;const s=e.readNumeric("indexes/bin1_offset",t,n+1),o=s[0],r=s[s.length-1];return r<=o?null:{p0:o,p1:r,rows:new Or(s,t)}}function Lr(e,t,n,s=tt,o=null){const r=e.readInt32("pixels/bin2_id",t,n),i=e.readNumeric(`pixels/${s}`,t,n);return{bin1:o===null?e.readInt32("pixels/bin1_id",t,n):o.bin1(t,t+i.length),bin2:r,count:i,length:i.length}}async function T2(e,t,n,s,o={}){const r=B2(e,t,n);if(r===null)return!0;const{p0:i,p1:a,rows:c}=r,l=Math.max(1,Math.trunc(o.chunkSize??Vs)),u=a-i,h=o.value??tt;for(let f=i;f<a;f+=l){if(o.cancelled?.()===!0)return!1;const d=Math.min(f+l,a);s(Lr(e,f,d,h,c)),o.onProgress?.(d-i,u),await vt()}return!0}const bl=65536;async function O2(e,t,n,s={},o=null){const r=Math.max(1,Math.trunc(s.chunkSize??Vs)),i=t.reduce((u,[h,f])=>u+Math.max(0,f-h),0),a=s.value??tt,c=o===null?null:new Or(o,0);let l=0;for(const[u,h]of t){c?.seek(u);for(let f=u;f<h;f+=r){if(s.cancelled?.()===!0)return!1;const d=Math.min(f+r,h);n(Lr(e,f,d,a,c)),l+=d-f,s.onProgress?.(l,i),await vt()}}return!0}function Je(e,t,n){return n!=="symmetric-upper"?{span:e,mode:"direct"}:t[1]<=e[0]?{span:t,mode:"transpose"}:e[1]<=t[0]?{span:e,mode:"direct"}:{span:[Math.min(e[0],t[0]),Math.min(e[1],t[1])],mode:"symmetric"}}function L2(e,t,n,s,o){const[r,i]=t,[a,c]=n,l=c-a,{bin1:u,bin2:h,count:f,length:d}=e,m=s.mode!=="transpose",g=s.mode!=="direct",p=s.mode==="symmetric";for(let w=0;w<d;w++){const b=u[w],y=h[w],A=f[w];m&&b>=r&&b<i&&y>=a&&y<c&&(o[(b-r)*l+(y-a)]+=A),g&&!(p&&b===y)&&y>=r&&y<i&&b>=a&&b<c&&(o[(y-r)*l+(b-a)]+=A)}}async function _2(e,t,n){const s=Ci(t.rows,e.nBins,"rows"),o=Ci(t.cols??t.rows,e.nBins,"cols"),r=s[1]-s[0],i=o[1]-o[0];S2(r,i,n),$2(r,i,n,{...t.budgetBytes===void 0?{}:{budgetBytes:t.budgetBytes}});const a=D2(r,i,n);if(r>0&&i>0){const c=Je(s,o,e.storageMode);await e.store.forEachWindow(s,o,c,l=>{L2(l,s,o,c,a)},{chunkSize:Vs,value:t.value??tt}),t.balance===!0&&R2(e.handle,s,o,a)}return{nrows:r,ncols:i,values:a,rowExtent:s,colExtent:o}}function D2(e,t,n){try{return new Float32Array(e*t)}catch(s){const o=e*t;throw new T("dense-too-large",`This machine could not allocate the ${e}×${t} block: ${lr(o)} cells is ${It.bytes(o*4)} in one array, and the request was refused. ${n}`,{nRows:e,nCols:t,cells:o,bytes:o*4,where:"allocation",cause:String(s)})}}function R2(e,t,n,s){const o=ae,r=`bins/${o}`;if(!e.has(r))throw new T("unsupported",`This file has no /bins/${o} column, so there are no weights to balance with. Run \`cooler balance\` on it, or view the raw counts.`,{column:o});const i=e.readNumeric(r,t[0],t[1]),a=n[0]===t[0]&&n[1]===t[1]?i:e.readNumeric(r,n[0],n[1]),c=n[1]-n[0];for(let l=0;l<t[1]-t[0];l++){const u=i[l],h=l*c;for(let f=0;f<c;f++)s[h+f]=s[h+f]*u*a[f]}}function Ci(e,t,n){const[s,o]=e;if(!Number.isInteger(s)||!Number.isInteger(o)||s<0||o<s||o>t)throw new T("bad-range",`Bin range for ${n} is [${s}, ${o}); it must be a half-open integer interval inside [0, ${t}).`,{extent:e,nBins:t,which:n});return[s,o]}class Al{constructor(t,n,s){this.handle=t,this.nBins=n,this.countKind=s}offsets=null;get nnz(){return this.handle.datasetLength("pixels/bin1_id")}scoreColumns(){return k2(this.handle)}binOffsets(){return this.offsets??=this.handle.readNumeric("indexes/bin1_offset"),this.offsets}async forEachWindow(t,n,s,o,r={}){return T2(this.handle,s.span[0],s.span[1],o,r)}async forEachBands(t,n,s={}){return O2(this.handle,this.pixelBandsOf(t),n,s,this.binOffsets())}async scanAll(t){const n=t.chunkSize??1e6,s=this.nnz,o=t.storedRows===!0?null:new Or(this.binOffsets(),0);for(let r=0;r<s;r+=n){const i=Math.min(r+n,s),a=t.onChunk(Lr(this.handle,r,i,tt,o));if(t.onProgress?.(i,s),a===!1)return;await(t.pause?.()??vt())}}pixelBandsOf(t){const n=this.binOffsets(),s=[];for(const[r,i]of t){if(i<=r)continue;const a=n[Math.max(0,r)],c=n[Math.min(this.nBins,i)];c>a&&s.push([a,c])}s.sort((r,i)=>r[0]-i[0]);const o=[];for(const r of s){const i=o[o.length-1];i!==void 0&&r[0]<=i[1]+bl?i[1]=Math.max(i[1],r[1]):o.push([r[0],r[1]])}return o}}const z2=64*1024*1024,P2=256;function Se(e,t,n){return e*n-e*(e-1)/2+(t-e)}function $s(e){return e*(e+1)/2}function yl(e,t){if(!(e>0))return 0;const n=(Math.log10(e)-t.low)*(254/(t.high-t.low)),s=1+Math.round(n);return s<1?1:s>255?255:s}function q2(e,t){return e===0?Number.NEGATIVE_INFINITY:t.low+(e-1)*(t.high-t.low)/254}function U2(e){const t=new Float64Array(256);for(let n=1;n<256;n++)t[n]=10**q2(n,e);return t}function G2(e){const s=new Uint32Array(4e3);let o=0,r=Number.NEGATIVE_INFINITY;for(let h=0;h<e.length;h++){const f=e[h];if(!(f>0))continue;const d=Math.log10(f);d>r&&(r=d);const m=Math.floor((d- -20)*100);s[m<0?0:m>=4e3?3999:m]++,o++}if(o<1e3)return null;const i=o*.001;let a=0,c=-20;for(let h=0;h<4e3;h++)if(a+=s[h],a>=i){c=-20+h/100;break}const l=c-1,u=Math.max(r+.5,l+1);return{low:l,high:u}}const Fi={low:-12,high:8},H2=4e6;function Q2(e,t){const n=i=>{let a=0;for(let c=0;c+1<e.length;c++)a+=Math.ceil((e[c+1]-e[c])/i);return a};let s=1;for(let i=0;i+1<e.length;i++)s=Math.max(s,e[i+1]-e[i]);if(n(1)<=t)return 1;if(n(s)>t)return s;let o=1,r=s;for(;o+1<r;){const i=o+r>>1;n(i)<=t?r=i:o=i}return r}async function K2(e){const{store:t,layout:n,weights:s}=e,o=n.n,r=n.slot,i=r.length,a=new Uint8Array($s(o)),c=new Float64Array(256),l=e.gather===void 0?null:V2(n,s,e.gather),u=new Int32Array(o).fill(-1),h=new Int32Array(o);for(let N=0;N<i;N++){const M=r[N];M<0||(u[M]<0&&(u[M]=N),h[M]=N+1)}const f=(N,M)=>{const I=[];for(let x=N;x<M;x++){if(u[x]<0)continue;const v=I[I.length-1];v!==void 0&&v[1]===u[x]?v[1]=h[x]:I.push([u[x],h[x]])}return I},d=Math.max(1,Math.floor((e.bandBytes??z2)/(4*Math.max(1,o)))),m=new Float32Array(Math.min(o,d)*o);let g=null;try{g=t.binOffsets()}catch{g=null}const p=N=>g===null?0:N.reduce((M,[I,x])=>M+(g[x]-g[I]),0),w=g!==null?p(f(0,o)):o;let b=0,y=null,A=0;for(let N=0;N<o;N+=d){if(e.cancelled?.()===!0)return null;const M=Math.min(o,N+d),I=f(N,M),x=(M-N)*o;m.fill(0,0,x);const v=b;if(!await t.forEachBands(I,k=>{for(let C=0;C<k.length;C++){const B=k.bin1[C],F=k.bin2[C];if(F<B)continue;const $=r[B],_=r[F];if($<0||_<0)continue;let E=k.count[C];s!==null&&(E*=s[B]*s[F]),E>0&&(m[($-N)*o+_]+=$===_&&B!==F?2*E:E)}A+=k.length},{...e.cancelled===void 0?{}:{cancelled:e.cancelled},...e.value===void 0?{}:{value:e.value},onProgress:k=>{g!==null&&e.onProgress?.(v+k,w)}}))return null;y??=G2(m.subarray(0,x))??Fi;for(let k=N;k<M;k++){const C=(k-N)*o;let B=Se(k,k,o);for(let F=k;F<o;F++,B++){const $=yl(m[C+F],y);a[B]=$,c[$]++}l?.row(k,m,C)}b=g!==null?v+p(I):M,e.onProgress?.(b,w),await vt()}return{cells:a,quantizer:y??Fi,histogram:c,pixels:A,...l===null?{}:{contacts:l.finish()}}}function V2(e,t,n){const s=e.n,o=Math.max(1,Math.trunc(n.width)),r=new Uint16Array(s);for(let I=0;I<e.slot.length;I++)e.slot[I]>=0&&(t===null||t[I]>0)&&r[e.slot[I]]++;const i=new Int32Array(s);for(let I=0;I+1<e.offsets.length;I++)i.fill(I,e.offsets[I],e.offsets[I+1]);const a=Q2(e.offsets,Math.max(1,n.coarseCells)),c=new Int32Array(s);let l=0;for(let I=0;I+1<e.offsets.length;I++){const x=e.offsets[I];for(let v=x;v<e.offsets[I+1];v++)c[v]=l+Math.floor((v-x)/a);l+=Math.ceil((e.offsets[I+1]-x)/a)}const u=new Float32Array(s*o).fill(Number.NaN),h=new Float64Array($s(l)),f=new Uint32Array($s(l)),d=new Float64Array(s),m=Math.max(0,Math.trunc(n.endDepth??0)),g=new Int32Array(s).fill(-1);if(m>0)for(let I=0;I+1<e.offsets.length;I++){const x=e.offsets[I],v=e.offsets[I+1],S=v-x;g.fill(2*I,x,x+Math.min(m,Math.ceil(S/2))),g.fill(2*I+1,v-Math.min(m,Math.floor(S/2)),v)}const p=Math.max(0,Math.trunc(n.endBudget??H2)),w=new Int32Array(m>0?s+1:0);let b=new Int32Array(m>0?1024:0),y=new Float32Array(b.length),A=0,N=!0;const M=(I,x)=>{if(A>=p){N=!1;return}if(A===b.length){const v=Math.min(p,b.length*2),S=new Int32Array(v),k=new Float32Array(v);S.set(b),k.set(y),b=S,y=k}b[A]=I,y[A]=x,A++};return{row(I,x,v){m>0&&(w[I]=A);const S=r[I];if(S===0)return;const k=i[I],C=c[I],B=Se(C,C,l)-C,F=g[I]>=0;for(let $=I;$<s;$++){const _=r[$];if(_===0)continue;const E=x[v+$]/(S*_);$-I<o&&i[$]===k?u[I*o+($-I)]=E:F&&E>0&&g[$]>=0&&M($,E);const L=B+c[$];h[L]+=E,f[L]++,d[I]+=E,$!==I&&(d[$]+=E)}},finish(){const I=new Float32Array(h.length);for(let x=0;x<h.length;x++)I[x]=f[x]===0?Number.NaN:h[x]/f[x];return{n:s,sequenceOf:i,width:o,band:u,coarse:{n:l,factor:a,of:c,values:I},marginal:d,kept:r,...m>0?{ends:{depth:m,zone:g,rowStart:Y2(w,A),cols:b.slice(0,A),values:y.slice(0,A),complete:N}}:{}}}}}function Y2(e,t){return e[e.length-1]=t,e}function W2(e,t,n=P2){const s=U2(t),o=[e];let r=e;for(;r.n>n;){const i=r.n,a=r.cells,c=Math.ceil(i/2),l=new Uint8Array($s(c));let u=0;for(let h=0;h<c;h++){const f=2*h,d=f+1;for(let m=h;m<c;m++,u++){const g=2*m,p=g+1;let w=s[a[Se(f,g,i)]],b=1;p<i&&(w+=s[a[Se(f,p,i)]],b++),d<i&&(w+=s[a[d<=g?Se(d,g,i):Se(g,d,i)]],b++,p<i&&(w+=s[a[Se(d,p,i)]],b++)),l[u]=yl(w/b,t)}}r={n:c,cells:l},o.push(r)}return o}function dt(e){return e.gap!==void 0}const X2=["debris","contaminant","haplotig","unplaced"];function Ys(e){const t=new Map(e.names.map((n,s)=>[n,e.lengths[s]]));for(const n of e.elsewhere??[])t.has(n.name)||t.set(n.name,n.length);return t}function ie(e,t){for(let n=1;;n++){const s=`${e}_e${n}`;if(!t.has(s))return s}}function Ws(e){return dt(e)?e.gap:e.end-e.start}function Ue(e){let t=0;for(const n of e)t+=Ws(n);return t}function ce(e,t,n){return e<t?t:e>n?n:e}function J2(e,t,n){return dt(e)?[{gap:t},{gap:e.gap-t}]:e.strand===1?[{...e,id:n.next(e.id),end:e.start+t},{...e,id:n.next(e.id),start:e.start+t}]:[{...e,id:n.next(e.id),start:e.end-t},{...e,id:n.next(e.id),end:e.end-t}]}function Qe(e,t,n){const s=[],o=[];let r=0;for(const i of e){const a=Ws(i);if(r+a<=t)s.push(i);else if(r>=t)o.push(i);else{const[c,l]=J2(i,t-r,n);s.push(c),o.push(l)}r+=a}return[s,o]}function Yn(e,t,n,s){const[o,r]=Qe(e,t,s),[i,a]=Qe(r,n-t,s);return[o,i,a]}function pn(e){const t=[];for(let n=e.length-1;n>=0;n--){const s=e[n];t.push(dt(s)?{gap:s.gap}:{...s,strand:s.strand===1?-1:1})}return t}function Dt(e){const t=[];for(const n of e){if(Ws(n)===0)continue;const s=t[t.length-1];if(s!==void 0&&dt(s)&&dt(n)){t[t.length-1]={gap:s.gap+n.gap};continue}if(s!==void 0&&!dt(s)&&!dt(n)&&s.sequence===n.sequence&&s.strand===n.strand){if(n.strand===1&&s.end===n.start){t[t.length-1]={...s,end:n.end};continue}if(n.strand===-1&&s.start===n.end){t[t.length-1]={...s,start:n.start};continue}}t.push(n)}return t}class Z2{counter=0;next(t){return this.counter+=1,`${t.split("~")[0]}~${this.counter}`}}function j2(e){return{sequences:e.names.map((t,n)=>({name:t,parts:[{id:t,sequence:t,start:0,end:e.lengths[n],strand:1}]})),aside:[]}}function _t(e,t){return e.sequences.find(n=>n.name===t)??null}function Wn(e,t,n){const s=Ue(e.parts),o=ce(Math.round(t),0,s),r=ce(Math.round(n),0,s);return o<=r?[o,r]:[r,o]}function th(e,t,n,s){const o=e.sequences.map(c=>({...c,parts:[...c.parts]})),r={sequences:o,aside:[...e.aside]},i=new Set(o.map(c=>c.name)),a=new Set(Ys(n).keys());switch(t.op){case"cut":{const c=_t(r,t.sequence);if(c===null)return e;const l=ce(Math.round(t.at),0,Ue(c.parts)),[u,h]=Qe(c.parts,l,s);return c.parts=[...u,...h],r}case"invert":{const c=_t(r,t.sequence);if(c===null)return e;const[l,u]=Wn(c,t.from,t.to);if(u===l)return e;const[h,f,d]=Yn(c.parts,l,u,s);return c.parts=Dt([...h,...pn(f),...d]),r}case"move":case"duplicate":{const c=_t(r,t.sequence),l=_t(r,t.into);if(c===null||l===null)return e;const[u,h]=Wn(c,t.from,t.to);if(h===u)return e;const[f,d,m]=Yn(c.parts,u,h,s),g=t.strand===1?d:pn(d);t.op==="move"?c.parts=Dt([...f,...m]):g.forEach((y,A)=>{dt(y)||(g[A]={...y,id:s.next(y.id)})});const p=ce(Math.round(t.at),0,Ue(l.parts)),[w,b]=Qe(l.parts,p,s);return l.parts=Dt([...w,...g,...b]),c!==l&&c.parts.length===0&&(r.sequences=r.sequences.filter(y=>y!==c)),r}case"join":{const c=_t(r,t.a),l=_t(r,t.b);if(c===null||l===null||c===l)return e;const u=t.aEnd==="end"?c.parts:pn(c.parts),h=t.bEnd==="start"?l.parts:pn(l.parts),f=t.gap>0?[{gap:Math.round(t.gap)}]:[];return c.parts=Dt([...u,...f,...h]),a.has(c.name)&&!Xs(c,n)&&(i.delete(c.name),c.name=ie(c.name,i)),t.place==="b"?(r.sequences=r.sequences.filter(d=>d!==c),r.sequences.splice(r.sequences.indexOf(l),1,c)):r.sequences=r.sequences.filter(d=>d!==l),r}case"split":{const c=_t(r,t.sequence);if(c===null)return e;const l=ce(Math.round(t.at),0,Ue(c.parts));if(l===0||l===Ue(c.parts))return e;const[u,h]=Qe(c.parts,l,s);c.parts=Dt(u);const f=Nn(c.name);a.has(c.name)&&(i.delete(c.name),c.name=ie(f,i),i.add(c.name));const d=t.name!==void 0&&!i.has(t.name)?t.name:ie(f,i);i.add(d);const m=r.sequences.indexOf(c);return r.sequences.splice(m+1,0,{name:d,parts:Dt(h)}),r}case"aside":{const c=_t(r,t.sequence);if(c===null)return e;const[l,u]=Wn(c,t.from,t.to);if(u===l)return e;const[h,f,d]=Yn(c.parts,l,u,s),m=typeof t.genome=="string"&&t.genome!==""?t.genome:void 0;for(const g of f)dt(g)||r.aside.push({piece:g,why:t.why,...m===void 0?{}:{genome:m}});return c.parts=Dt([...h,...d]),c.parts.length===0&&(r.sequences=r.sequences.filter(g=>g!==c)),r}case"restore":{const c=r.aside.findIndex(({piece:h})=>h.sequence===t.sequence&&h.start===t.start&&h.end===t.end);if(c<0)return e;const[l]=r.aside.splice(c,1),u=ie(Nn(t.sequence),i);return r.sequences.push({name:u,parts:[{...l.piece,strand:1}]}),r}case"bring":{const c=n.elsewhere?.find(h=>h.name===t.sequence);if(c===void 0||!(c.length>0))return e;const l=h=>h.sequence===c.name;if(r.sequences.some(h=>h.parts.some(f=>!dt(f)&&l(f)))||r.aside.some(h=>l(h.piece)))return e;const u=i.has(c.name)?ie(c.name,i):c.name;return r.sequences.push({name:u,parts:[{id:s.next(c.name),sequence:c.name,start:0,end:c.length,strand:1}]}),r}case"rotate":{const c=_t(r,t.sequence);if(c===null)return e;const l=ce(Math.round(t.at),0,Ue(c.parts)),[u,h]=Qe(c.parts,l,s);return c.parts=Dt([...h,...u]),r}case"rename":{const c=_t(r,t.sequence);return c===null||t.name===""||i.has(t.name)&&t.name!==c.name||a.has(t.name)&&Nl(c,n)!==t.name?e:(c.name=t.name,c.named=!0,r)}case"reorder":{const c=new Map(t.names.map((l,u)=>[l,u]));return r.sequences.sort((l,u)=>(c.get(l.name)??1/0)-(c.get(u.name)??1/0)),r}case"place":{const c=_t(r,t.sequence);if(c===null)return e;const[l,u]=Wn(c,t.from,t.to);if(u===l)return e;const h=r.sequences,f=h.indexOf(c),d=t.before===null?h.length:h.findIndex(A=>A.name===t.before),[m,g,p]=Yn(c.parts,l,u,s),w=Dt(t.strand===1?g:pn(g)),b=Dt([...m,...p]);if(b.every(A=>dt(A))){c.parts=w,h.splice(f,1);const A=d<0?h.length:d>f?d-1:d;return h.splice(A,0,c),r}c.parts=b,a.has(c.name)&&(i.delete(c.name),c.name=ie(Nn(c.name),i),i.add(c.name));const y=ie(Nn(c.name),i);return i.add(y),h.splice(d<0?h.length:d,0,{name:y,parts:w}),r}case"layout":return eh(t,n,s)}}function eh(e,t,n){const s=Ys(t),o=new Map,r=(u,h,f)=>{const d=o.get(u)??[];d.push([h,f]),o.set(u,d)},i=(u,h,f)=>{const d=s.get(u);if(d===void 0)return null;const m=ce(Math.round(h),0,d),g=ce(Math.round(f),0,d);return g>m?[m,g]:null},a=new Set,c=[];for(const u of Array.isArray(e.sequences)?e.sequences:[]){if(u===null||typeof u!="object"||!Array.isArray(u.parts))continue;const h=[];for(const m of u.parts){if(m===null||typeof m!="object")continue;if("gap"in m){typeof m.gap=="number"&&m.gap>0&&h.push({gap:Math.round(m.gap)});continue}if(typeof m.sequence!="string"||typeof m.start!="number"||typeof m.end!="number"||m.strand!==1&&m.strand!==-1)continue;const g=i(m.sequence,m.start,m.end);g!==null&&(r(m.sequence,g[0],g[1]),h.push({id:n.next(m.sequence),sequence:m.sequence,start:g[0],end:g[1],strand:m.strand}))}for(;h.length>0&&dt(h[0]);)h.shift();for(;h.length>0&&dt(h[h.length-1]);)h.pop();if(h.length===0)continue;const f=typeof u.name=="string"?u.name:"",d=f!==""&&!a.has(f)?f:ie(f===""?"seq":f,a);a.add(d),c.push({name:d,parts:Dt(h),...u.circular===!0?{circular:!0}:{}})}const l=[];for(const u of Array.isArray(e.aside)?e.aside:[]){if(u===null||typeof u!="object"||typeof u.sequence!="string"||!X2.includes(u.why))continue;const h=i(u.sequence,u.start,u.end);if(h===null)continue;r(u.sequence,h[0],h[1]);const f=typeof u.genome=="string"&&u.genome!==""?u.genome:void 0;l.push({piece:{id:n.next(u.sequence),sequence:u.sequence,start:h[0],end:h[1],strand:1},why:u.why,...f===void 0?{}:{genome:f}})}for(let u=0;u<t.names.length;u++){const h=t.names[u],f=(o.get(h)??[]).sort((m,g)=>m[0]-g[0]);let d=0;for(const[m,g]of f)m>d&&l.push({piece:{id:n.next(h),sequence:h,start:d,end:m,strand:1},why:"unplaced"}),d=Math.max(d,g);d<t.lengths[u]&&l.push({piece:{id:n.next(h),sequence:h,start:d,end:t.lengths[u],strand:1},why:"unplaced"})}return{sequences:c,aside:l}}function Nn(e){return e.replace(/_e\d+$/,"")}function Xs(e,t){let n=0,s=null;for(const i of e.parts){if(dt(i)||i.strand!==1||i.start!==n||s!==null&&i.sequence!==s)return!1;s=i.sequence,n=i.end}if(s===null)return!1;const o=t.names.indexOf(s);if(o>=0)return n===t.lengths[o];const r=t.elsewhere?.find(i=>i.name===s);return r!==void 0&&n===r.length}function nh(e,t){const n=new Set(Ys(t).keys()),s=new Set(e.sequences.map(o=>o.name));for(const o of e.sequences)n.has(o.name)&&!Xs(o,t)&&(s.delete(o.name),o.name=ie(Nn(o.name),s),s.add(o.name));for(const o of e.sequences){if(o.named===!0)continue;const r=Nl(o,t);r!==null&&o.name!==r&&!s.has(r)&&(s.delete(o.name),o.name=r,s.add(r))}return e}function Nl(e,t){if(!Xs(e,t))return null;const n=e.parts[0];return dt(n)?null:n.sequence}class _r{original;sequences;aside;offsets=new Map;byOriginal=new Map;constructor(t,n){this.original=t,this.sequences=n.sequences,this.aside=n.aside;for(const s of n.sequences){const o=[];let r=0;for(const i of s.parts){if(o.push(r),!dt(i)){const a=this.byOriginal.get(i.sequence)??[];a.push({sequence:s.name,offset:r,piece:i}),this.byOriginal.set(i.sequence,a)}r+=Ws(i)}o.push(r),this.offsets.set(s.name,o)}for(const s of this.byOriginal.values())s.sort((o,r)=>o.piece.start-r.piece.start)}static of(t){const n=new Z2;let s=j2(t.original);const o=ce(t.cursor,0,t.history.length);for(let r=0;r<o;r++)s=nh(th(s,t.history[r].step,t.original,n),t.original);return new _r(t.original,s)}get isIdentity(){return this.aside.length===0&&this.sequences.length===this.original.names.length&&this.sequences.every((t,n)=>t.name===this.original.names[n]&&Xs(t,this.original))}sequence(t){return this.sequences.find(n=>n.name===t)??null}lengthOf(t){const n=this.offsets.get(t);return n===void 0?null:n[n.length-1]}contigs(){return this.sequences.map(t=>({name:t.name,length:this.lengthOf(t.name)??0}))}toOriginal(t,n){const s=this.sequence(t),o=this.offsets.get(t);if(s===null||o===void 0)return null;const r=sh(o,n);if(r<0)return null;const i=s.parts[r];if(dt(i))return null;const a=n-o[r];return i.strand===1?{sequence:i.sequence,bp:i.start+a,strand:1}:{sequence:i.sequence,bp:i.end-1-a,strand:-1}}toEdited(t,n){const s=[];for(const o of this.landings(t)){const r=o.piece;n<r.start||n>=r.end||s.push(r.strand===1?{sequence:o.sequence,bp:o.offset+(n-r.start),strand:1}:{sequence:o.sequence,bp:o.offset+(r.end-1-n),strand:-1})}return s}landings(t){return this.byOriginal.get(t)??[]}piecesIn(t,n,s){const o=this.sequence(t),r=this.offsets.get(t);if(o===null||r===void 0)return[];const i=[];for(let a=0;a<o.parts.length;a++){const c=o.parts[a],l=r[a],u=r[a+1];if(dt(c)||u<=n||l>=s)continue;const h=Math.max(n,l)-l,f=Math.min(s,u)-l,d=c.strand===1?{...c,start:c.start+h,end:c.start+f}:{...c,start:c.end-f,end:c.end-h};i.push({offset:l+h,piece:d})}return i}junctions(){const t=[];for(const n of this.sequences){const s=this.offsets.get(n.name)??[];for(let o=1;o<n.parts.length;o++)t.push({sequence:n.name,at:s[o]})}return t}}function sh(e,t){if(t<0||t>=e[e.length-1])return-1;let n=0,s=e.length-2;for(;n<=s;){const o=n+s>>1;if(t<e[o])s=o-1;else if(t>=e[o+1])n=o+1;else return o}return-1}function oh(e,t,n){const s=e.original;if(s.genome!==void 0){const o=new Map(t.map((r,i)=>[r,n[i]]));for(const[r,i]of Ys(s)){const a=o.get(r);if(a===void 0)return`the edit names ${r}, which this file does not have`;if(a!==i)return`${r} is ${i} bp in the edit and ${a} bp here`}return null}if(s.names.length!==t.length)return`the edit was made on ${s.names.length} sequences and this file has ${t.length}`;for(let o=0;o<t.length;o++){if(s.names[o]!==t[o])return`sequence ${o+1} is ${s.names[o]} in the edit and ${t[o]} here`;if(s.lengths[o]!==n[o])return`${t[o]} is ${s.lengths[o]} bp in the edit and ${n[o]} bp here`}return null}const rh=64;class Dr{names;lengths;chromOffset;toOriginal;landingStart;landings;originalBins;constructor(t){this.names=t.names,this.lengths=t.lengths,this.chromOffset=t.chromOffset,this.toOriginal=t.toOriginal,this.originalBins=t.originalBins;const n=new Int32Array(t.originalBins+1);for(const r of t.toOriginal)r>=0&&n[r+1]++;for(let r=0;r<t.originalBins;r++)n[r+1]+=n[r];const s=n.slice(0,t.originalBins),o=new Int32Array(n[t.originalBins]);t.toOriginal.forEach((r,i)=>{r>=0&&(o[s[r]++]=i)}),this.landingStart=n,this.landings=o}get nBins(){return this.toOriginal.length}static of(t,n,s){const o=t.binsize,r=new Map(n.map((u,h)=>[u,h])),i=[],a=[],c=[],l=[0];for(const u of s.sequences){const h=s.lengthOf(u.name)??0,f=Math.ceil(h/o);let d=0;u.parts.forEach((m,g)=>{const p=dt(m)?m.gap:m.end-m.start,w=Math.min(f,Math.round(d/o));d+=p;const b=g===u.parts.length-1?f:Math.min(f,Math.round(d/o)),y=Math.max(0,b-w),A=dt(m)?void 0:r.get(m.sequence);if(dt(m)||A===void 0){for(let B=0;B<y;B++)i.push(-1);return}const[N,M]=t.contigBinExtent(A),I=M-N,x=t.contigLengths[A],v=B=>B>=x?I:Math.max(0,Math.min(I,Math.round(B/o)));let S=v(m.start),k=v(m.end);k<=S&&(S=Math.max(0,Math.min(I-1,Math.floor((m.start+m.end)/2/o))),k=S+1);const C=k-S;for(let B=0;B<y;B++){const F=Math.min(C-1,Math.floor((B+.5)*C/y));i.push(N+(m.strand===1?S+F:k-1-F))}}),a.push(u.name),c.push(h),l.push(i.length)}return new Dr({names:a,lengths:Float64Array.from(c),chromOffset:Float64Array.from(l),toOriginal:Int32Array.from(i),originalBins:t.nBins})}originalBands(t){const n=new Uint8Array(this.originalBins);for(const[s,o]of t)for(let r=Math.max(0,s);r<Math.min(this.nBins,o);r++){const i=this.toOriginal[r];i>=0&&(n[i]=1)}return this.bandsOf(n)}windowBands(t,n){const s=([r,i])=>{let a=-1;for(let c=Math.max(0,r);c<Math.min(this.nBins,i);c++)a=Math.max(a,this.toOriginal[c]);return a},o=new Uint8Array(this.originalBins);for(const[[r,i],a]of[[t,s(n)],[n,s(t)]])for(let c=Math.max(0,r);c<Math.min(this.nBins,i);c++){const l=this.toOriginal[c];l>=0&&l<=a&&(o[l]=1)}return this.bandsOf(o)}bandsOf(t){const n=[];let s=-1;for(let o=0;o<=this.originalBins;o++){const r=o<this.originalBins&&t[o]===1;r&&s<0&&(s=o),!r&&s>=0&&(n.push([s,o]),s=-1)}return n}originalRectangles(t,n,s){const o=new Uint8Array(this.originalBins);for(const[[l,u],h]of[[t,1],[n,2]])for(let f=Math.max(0,l);f<Math.min(this.nBins,u);f++){const d=this.toOriginal[f];d>=0&&(o[d]|=h)}const r=[];let i=-1,a=0;for(let l=0;l<=this.originalBins;l++){const u=l<this.originalBins?o[l]:0;u!==0?(i<0&&(i=l,a=0),a|=u):i>=0&&(r.push({extent:[i,l],bits:a}),i=-1)}const c=[];for(let l=0;l<r.length;l++)for(let u=l;u<r.length;u++){const h=r[l].bits,f=r[u].bits;if(h&1&&f&2||h&2&&f&1){if(c.length===s)return null;c.push([r[l].extent,r[u].extent])}}return c}translate(t,n,s,o){s.reset();const{landingStart:r,landings:i}=this,[[a,c],[l,u]]=o??[[0,this.originalBins],[0,this.originalBins]];for(let h=0;h<t.length;h++){const f=t.bin1[h],d=t.bin2[h];if(o!==void 0&&!(f>=a&&f<c&&d>=l&&d<u)&&!(f>=l&&f<u&&d>=a&&d<c))continue;const m=r[f],g=r[f+1];if(m===g)continue;const p=r[d],w=r[d+1];if(p===w)continue;const b=t.count[h];if(f===d&&n){for(let y=m;y<g;y++)for(let A=y;A<g;A++)s.push(i[y],i[A],b,n);continue}for(let y=m;y<g;y++)for(let A=p;A<w;A++)s.push(i[y],i[A],b,n)}return s.chunk()}gather(t,n=0,s=this.nBins){const o=new Float64Array(Math.max(0,s-n));for(let r=n;r<s;r++){const i=this.toOriginal[r];o[r-n]=i>=0&&i<t.length?t[i]:Number.NaN}return o}gatherText(t,n=0,s=this.nBins){const o=[];for(let r=n;r<s;r++){const i=this.toOriginal[r];o.push(i>=0&&i<t.length?t[i]:"")}return o}}class Ml{bin1=new Int32Array(1024);bin2=new Int32Array(1024);count=new Float64Array(1024);length=0;reset(){this.length=0}push(t,n,s,o){this.length===this.bin1.length&&this.grow();const r=o&&t>n;this.bin1[this.length]=r?n:t,this.bin2[this.length]=r?t:n,this.count[this.length]=s,this.length++}chunk(){return{bin1:this.bin1,bin2:this.bin2,count:this.count,length:this.length}}grow(){const t=this.bin1.length*2,n=new Int32Array(t);n.set(this.bin1);const s=new Int32Array(t);s.set(this.bin2);const o=new Float64Array(t);o.set(this.count),this.bin1=n,this.bin2=s,this.count=o}}class ih{constructor(t,n,s){this.inner=t,this.lens=n,this.upper=s}buffer=new Ml;get nnz(){return this.inner.nnz}get countKind(){return this.inner.countKind}scoreColumns(){return this.inner.scoreColumns()}binOffsets(){const t=this.inner.binOffsets(),n=new Float64Array(this.lens.nBins+1);for(let s=0;s<this.lens.nBins;s++){const o=this.lens.toOriginal[s];n[s+1]=n[s]+(o>=0?t[o+1]-t[o]:0)}return n}async forEachWindow(t,n,s,o,r){const i=this.inner.rectangular===!0?this.lens.originalRectangles(t,n,rh):null;if(i===null){const c=this.upper?this.lens.windowBands(t,n):this.lens.originalBands([t]);return c.length===0?!0:this.inner.forEachBands(c,l=>o(this.lens.translate(l,this.upper,this.buffer)),r)}const a=this.upper?"symmetric-upper":"square";for(const c of i){const[l,u]=c;if(!await this.inner.forEachWindow(l,u,Je(l,u,a),f=>o(this.lens.translate(f,this.upper,this.buffer,c)),r))return!1}return!0}forEachBands(t,n,s){const o=this.lens.originalBands(t);return o.length===0?Promise.resolve(!0):this.inner.forEachBands(o,r=>n(this.lens.translate(r,this.upper,this.buffer)),s)}scanAll(t){return this.inner.scanAll({...t,onChunk:n=>t.onChunk(this.lens.translate(n,this.upper,this.buffer))})}}function ah(e,t,n,s){const o=new Map,r=new Map,i=c=>c.startsWith("bins/"),a=c=>c==="chroms"||c==="contigs";return new Proxy(e,{get(c,l){switch(l){case"attrs":return()=>s;case"datasetLength":return u=>i(u)?t.nBins:u.startsWith("chroms/")||u.startsWith("contigs/")?n.length:c.datasetLength(u);case"readNumeric":return(u,h,f)=>{if(!i(u))return c.readNumeric(u,h,f);let d=o.get(u);return d===void 0&&(d=c.readNumeric(u),o.set(u,d)),t.gather(d,h??0,f??t.nBins)};case"readStrings":return(u,h,f)=>{if(!i(u))return c.readStrings(u,h,f);let d=r.get(u);return d===void 0&&(d=c.readStrings(u),r.set(u,d)),t.gatherText(d,h??0,f??t.nBins)};case"readTable":return(u,h,f,d)=>a(u)?n:c.readTable(u,h,f,d);default:{const u=Reflect.get(c,l,c);return typeof u=="function"?u.bind(c):u}}}})}function ch(e){return on(e.names.length,["name","length"],{name:e.names,length:e.lengths})}function lh(e,t,n,s={}){const o=oh(e,t,Array.from(n));if(o!==null)return{refusal:`The edit was made on another assembly: ${o}.`};const r=_r.of(e);return r.isIdentity&&s.always!==!0?null:{assembly:r}}class Rr{constructor(t,n,s){this.factor=t,this.fineOffset=n,this.chromOffset=s}static of(t,n){const s=Float64Array.from(t),o=new Float64Array(s.length);for(let r=0;r+1<s.length;r++)o[r+1]=o[r]+Math.ceil((s[r+1]-s[r])/n);return new Rr(n,s,o)}get nBins(){return this.chromOffset[this.chromOffset.length-1]??0}get fineBins(){return this.fineOffset[this.fineOffset.length-1]??0}coarseBin(t){if(t>=this.fineBins)return this.nBins;const n=$i(this.fineOffset,t);return this.chromOffset[n]+Math.floor((t-this.fineOffset[n])/this.factor)}fineBin(t){if(t>=this.nBins)return this.fineBins;const n=$i(this.chromOffset,t);return this.fineOffset[n]+(t-this.chromOffset[n])*this.factor}toFine(t){return[this.fineBin(t[0]),this.fineBin(t[1])]}toCoarse(t){const n=this.coarseBin(t[0]);return t[1]<=t[0]?[n,n]:[n,this.coarseBin(t[1]-1)+1]}coarseIndex(){const t=new Int32Array(this.fineBins);for(let n=0;n+1<this.fineOffset.length;n++){const s=this.chromOffset[n],o=this.fineOffset[n];for(let r=o;r<this.fineOffset[n+1];r++)t[r]=s+Math.floor((r-o)/this.factor)}return t}}function $i(e,t){let n=0,s=e.length-1;for(;n<s;){const o=n+s>>>1;e[o]<=t?n=o+1:s=o}return Math.max(0,n-1)}const uh="weight",hh=65536;class fh{constructor(t,n,s){this.inner=t,this.grid=n,this.storageMode=s}index=null;get nnz(){return null}get countKind(){return this.inner.countKind}get rectangular(){return this.inner.rectangular}scoreColumns(){return this.inner.scoreColumns()}binOffsets(){const t=this.inner.binOffsets(),n=new Float64Array(this.grid.nBins+1);for(let s=0;s<=this.grid.nBins;s++)n[s]=t[this.grid.fineBin(s)]??0;return n}async forEachWindow(t,n,s,o,r){const i=this.grid.toFine(t),a=this.grid.toFine(n),c=Je(i,a,this.storageMode),l=new Io(this.coarseIndex(),{rows:t,cols:n,mode:c.mode},o),u=await this.inner.forEachWindow(i,a,c,h=>l.add(h),r);return u&&l.finish(),u}forEachBands(t,n,s){const o=t.map(i=>this.grid.toFine(i)).filter(([i,a])=>a>i);if(o.length===0)return Promise.resolve(!0);const r=new Io(this.coarseIndex(),this.grid.nBins,n);return this.inner.forEachBands(o,i=>r.add(i),s).then(i=>(i&&r.finish(),i))}async scanAll(t){let n=!1;const s=new Io(this.coarseIndex(),this.grid.nBins,o=>{n||t.onChunk(o)===!1&&(n=!0)});await this.inner.scanAll({...t,onChunk:o=>(s.add(o),!n)}),n||s.finish()}coarseIndex(){return this.index??=this.grid.coarseIndex(),this.index}}class Io{constructor(t,n,s){this.coarseOf=t,this.reach=n,this.emit=s,typeof n=="number"?(this.colLo=0,this.colSpan=n,this.rowLo=0,this.sums=new Float64Array(n)):(this.colLo=n.cols[0],this.colSpan=n.cols[1]-n.cols[0],this.rowLo=n.rows[0],this.sums=new Float64Array(this.colSpan+(n.rows[1]-n.rows[0]))),this.stamp=new Int32Array(this.sums.length)}sums;stamp;touched=new Int32Array(256);nTouched=0;row=-1;generation=0;out=new Ml;held=0;colLo;colSpan;rowLo;add(t){const{bin1:n,bin2:s,count:o,length:r}=t,i=this.coarseOf,a=typeof this.reach=="number"?null:this.reach;for(let c=0;c<r;c++){const l=i[n[c]],u=i[s[c]];if(l===void 0||u===void 0||a!==null&&!dh(a,l,u))continue;l!==this.row&&this.startRow(l);let h=u-this.colLo;if((h<0||h>=this.colSpan)&&(h=this.colSpan+(u-this.rowLo)),!(h<0||h>=this.sums.length))if(this.stamp[h]!==this.generation){if(this.stamp[h]=this.generation,this.sums[h]=o[c],this.nTouched===this.touched.length){const f=new Int32Array(this.touched.length*2);f.set(this.touched),this.touched=f}this.touched[this.nTouched++]=u}else this.sums[h]+=o[c]}this.held>0&&this.handOn()}finish(){this.closeRow(),this.row=-1,this.held>0&&this.handOn()}startRow(t){this.closeRow(),this.row=t,this.generation++}closeRow(){if(this.nTouched===0)return;const t=this.touched.subarray(0,this.nTouched).sort();for(const n of t){let s=n-this.colLo;(s<0||s>=this.colSpan)&&(s=this.colSpan+(n-this.rowLo)),this.out.push(this.row,n,this.sums[s],!1),this.held++}this.nTouched=0,this.held>=hh&&this.handOn()}handOn(){this.emit(this.out.chunk()),this.out.reset(),this.held=0}}function dh(e,t,n){const{rows:s,cols:o,mode:r}=e;return r!=="transpose"&&t>=s[0]&&t<s[1]&&n>=o[0]&&n<o[1]?!0:r!=="direct"&&n>=s[0]&&n<s[1]&&t>=o[0]&&t<o[1]}function mh(e,t,n){const s=new Map,o=new Map,r=c=>c.startsWith("bins/"),i=c=>{let l=s.get(c);return l===void 0&&(l=gh(c.slice(5),e.readNumeric(c),t),s.set(c,l)),l},a=c=>{let l=o.get(c);if(l===void 0){const u=e.readStrings(c);l=[];for(let h=0;h<t.nBins;h++)l.push(u[t.fineBin(h)]??"");o.set(c,l)}return l};return new Proxy(e,{get(c,l){switch(l){case"attrs":return()=>n;case"datasetLength":return u=>r(u)?t.nBins:c.datasetLength(u);case"readNumeric":return(u,h,f)=>r(u)?i(u).slice(h??0,f??t.nBins):c.readNumeric(u,h,f);case"readStrings":return(u,h,f)=>r(u)?a(u).slice(h??0,f??t.nBins):c.readStrings(u,h,f);default:{const u=Reflect.get(c,l,c);return typeof u=="function"?u.bind(c):u}}}})}function gh(e,t,n){const s=new Float64Array(n.nBins);for(let o=0;o<n.nBins;o++){const r=n.fineBin(o),i=n.fineBin(o+1);if(e==="chrom"||e==="start")s[o]=t[r];else if(e==="end")s[o]=t[i-1];else if(e===uh){let a=0,c=0;for(let l=r;l<i;l++){const u=t[l];Number.isFinite(u)&&u>0&&(a++,c+=1/u)}s[o]=a===0?Number.NaN:a/c}else{let a=0,c=0;for(let l=r;l<i;l++){const u=t[l];Number.isFinite(u)&&(a++,c+=u)}s[o]=a===0?Number.NaN:c/a}}return s}const ph="AGFzbQEAAAABCAFgBH9/f38AAg8BA2VudgZtZW1vcnkCAAIDAgEABAUBcAEBAQYIAX8BQYCIBAsHCQEFZmxveWQAAAq5CQG2CQQJfwJ8FH8EewJAIAIgA04NACABQQFIDQACQCABQQdLDQAgAkEDdCEEIAFBA3QhBSABQQJGIQYgAUEERiEHIAFBBUYhCCABQQZGIQkDQCAAIAIgAWxBA3RqIQogACELIAEhDANAAkAgCyAEaisDACINRAAAAAAAAPB/YQ0AAkAgDSAKKwMAoCIOIAsrAwBjRQ0AIAsgDjkDAAsgAUEBRg0AAkAgDSAKKwMIoCIOIAtBCGoiDysDAGNFDQAgDyAOOQMACyAGDQACQCANIAorAxCgIg4gC0EQaiIPKwMAY0UNACAPIA45AwALIAFBA0YNAAJAIA0gCisDGKAiDiALQRhqIg8rAwBjRQ0AIA8gDjkDAAsgBw0AAkAgDSAKKwMgoCIOIAtBIGoiDysDAGNFDQAgDyAOOQMACyAIDQACQCANIAorAyigIg4gC0EoaiIPKwMAY0UNACAPIA45AwALIAkNACANIAorAzCgIg0gC0EwaiIPKwMAY0UNACAPIA05AwALIAsgBWohCyAMQX9qIgwNAAsgBEEIaiEEIAJBAWoiAiADRw0ADAILCyABIAFBDyABQQ9KGyIEQfj///8HcSILQQFyIgwgASAMShsiDCALayIFIAxBAXEiEGshESALIBByIAxrIRIgACABIARBeGoiC0F4cUEJaiIMIAEgDEobQQN0IgwgC0EDdEFAcSILa2ohEyAAIAIgAWxBA3QiBCAMaiALa2ohFCAAQcAAaiEVIAFBA3QhFkEAIRcgBUEGSSEYIAAgBGoiGUHAAGoiGiEbA0AgFCAWIBdsIgtqIRwgGiALaiEdIAAgAiABbEEDdGohHiAAIR8gFSEgQQAhIQNAAkAgACAhIAFsQQN0aiIIIAJBA3RqKwMAIg5EAAAAAAAA8H9hDQAgFiAhbCEiIA79FCEjQQghBEEAIQUgGyEKICAhD0EAIQwDQCAeIAxBA3QiC2oiDP0AADAhJCAM/QAAICElIAz9AAAQISYgCCALaiILIAv9AAAAICMgDP0AAAD98AH99gH9CwAAIAsgC/0AABAgIyAm/fAB/fYB/QsAECALIAv9AAAgICMgJf3wAf32Af0LACAgCyAL/QAAMCAjICT98AH99gH9CwAwIAoiB0HAAGohCiAPIgZBwABqIQ8gBSIJQQFqIQUgBCILIQwgC0EIaiIEIAFMDQALIAsgAU4NAAJAIBgNAAJAIAAgCUEGdCIMaiAiakHAAGogHCAMak8NACAdIAxqIBMgDGogImpJDQELIAsgEWohCyASIQwDQAJAICMgB/0AAwD98AEiJCAG/QADAP1JIiX9GwBBAXFFDQAgBiAk/SEAOQMACwJAICX9GwJBAXFFDQAgBkEIaiAk/SEBOQMACyAHQRBqIQcgBkEQaiEGIAxBAmoiDA0ACyAQRQ0BCyAZIAtBA3QiDGohBCAfIAxqIQwDQAJAIA4gBCsDAKAiDSAMKwMAY0UNACAMIA05AwALIARBCGohBCAMQQhqIQwgC0EBaiILIAFIDQALCyAfIBZqIR8gICAWaiEgICFBAWoiISABRw0ACyAZIBZqIRkgGyAWaiEbIBdBAWohFyACQQFqIgIgA0cNAAsLCwAwBG5hbWUACwpmbG95ZC53YXNtAQgBAAVmbG95ZAcSAQAPX19zdGFja19wb2ludGVyADgJcHJvZHVjZXJzAQxwcm9jZXNzZWQtYnkBDFVidW50dSBjbGFuZxExOC4xLjMgKDF1YnVudHUxKQA1D3RhcmdldF9mZWF0dXJlcwMrD211dGFibGUtZ2xvYmFscysIc2lnbi1leHQrB3NpbWQxMjg=",zr=.2,xo=3e3,wh=.05,bh=.995,Ah=2e3,yh=10;function Nh(e,t,n=zr,s){if(t<2)return null;const o=new Float64Array(t);for(let m=0;m<t;m++){const g=m*t;for(let p=0;p<t;p++)o[p]+=e[g+p]}const r=$h(o);let i=0;for(let m=0;m<t;m++)r[m]===0&&i++;if(i<2)return null;const a=new Int32Array(i);for(let m=0,g=0;m<t;m++)r[m]===0&&(a[g++]=m);const c=new Float64Array(i*i);for(let m=0;m<i;m++){const g=a[m]*t,p=m*i;for(let w=0;w<i;w++)c[p+w]=e[g+a[w]]}const{rows:l,cols:u}=Ih(c,i),h=new Float64Array(i*i);for(let m=0;m<h.length;m++){const g=c[m];h[m]=g>0?1/Math.pow(g,n):Number.POSITIVE_INFINITY}xh(h,i);const f=-1/n;for(let m=0;m<h.length;m++){const g=h[m];h[m]=Number.isFinite(g)?Math.pow(g,f):0}const d=Fh(c,h,i);return{kept:a,scn:c,boosted:d,rows:l,cols:u}}function Mh(e,t,n=zr,s){const o=Nh(e,t,n);if(o===null)return null;const{kept:r,boosted:i,rows:a,cols:c}=o,l=r.length;for(let u=0;u<l;u++){const h=u*l;for(let f=0;f<l;f++){const d=(a[u]*c[f]+a[f]*c[u])/2;i[h+f]=d>0?i[h+f]/d:0}}return{kept:r,counts:i}}function Ih(e,t,n=yh){const s=new Float64Array(t).fill(1),o=new Float64Array(t).fill(1),r=new Float64Array(t);for(let i=0;i<n;i++){r.fill(0);for(let a=0;a<t;a++){const c=a*t;for(let l=0;l<t;l++)r[l]+=e[c+l]}for(let a=0;a<t;a++){const c=Math.max(1,r[a]);r[a]=c,o[a]/=c}for(let a=0;a<t;a++){const c=a*t;for(let l=0;l<t;l++)e[c+l]/=r[l]}for(let a=0;a<t;a++){const c=a*t;let l=0;for(let h=0;h<t;h++)l+=e[c+h];const u=Math.max(1,l);s[a]/=u;for(let h=0;h<t;h++)e[c+h]/=u}}for(let i=0;i<t;i++)for(let a=i+1;a<t;a++){const c=(e[i*t+a]+e[a*t+i])/2;e[i*t+a]=c,e[a*t+i]=c}return{rows:s,cols:o}}function xh(e,t,n,s="auto"){if(s!=="js"&&Sh(e,t))return"wasm";for(let o=0;o<t;o++){const r=o*t;for(let i=0;i<t;i++){const a=i*t,c=e[a+o];if(c!==Number.POSITIVE_INFINITY)for(let l=0;l<t;l++){const u=c+e[r+l];u<e[a+l]&&(e[a+l]=u)}}}return"js"}let Re;const vo=131072,vh=65536,Ei=64;function kh(){if(Re!==void 0)return Re;Re=null;try{if(typeof WebAssembly>"u")return null;const e=atob(ph),t=new Uint8Array(e.length);for(let n=0;n<e.length;n++)t[n]=e.charCodeAt(n);WebAssembly.validate(t)&&(Re=new WebAssembly.Module(t))}catch{Re=null}return Re}function Sh(e,t,n){const s=kh();if(s===null)return!1;let o,r;try{const i=new WebAssembly.Memory({initial:Math.ceil((vo+e.length*8)/vh)});o=new WebAssembly.Instance(s,{env:{memory:i}}).exports.floyd,r=new Float64Array(i.buffer,vo,e.length)}catch{return!1}r.set(e);for(let i=0;i<t;i+=Ei){const a=Math.min(t,i+Ei);o(vo,t,i,a)}return e.set(r),!0}function Ch(e){const t=new Float64Array(e);t[0]=1;for(let a=1;a<e;a++)t[a]=t[a-1]*1.01;const n=[];let s=0;for(let a=0;a<e&&s<e;a++){const c=Math.floor(t[a]);n.push(c),s+=c}const o=n.length-1;n[o]-=s-e,n[o-1]+=n[o],n.length=o;const r=new Int32Array(e);let i=0;for(let a=0;a<n.length;a++)for(let c=0;c<n[a];c++)r[i++]=a;return{window:r,sizes:n}}function Bi(e,t,n){const s=new Float64Array(n.length);let o=0;for(let r=0;r<n.length;r++){let i=0,a=0;for(let c=0;c<n[r];c++,o++){for(let l=0;l+o<t;l++)i+=e[l*t+l+o];a+=t-o}s[r]=a>0?i/a:0}return s}function Fh(e,t,n){const{window:s,sizes:o}=Ch(n),r=Bi(e,n,o),i=Bi(t,n,o),a=new Float64Array(t);for(let c=0;c<n;c++){const l=s[c],u=i[l]>0?r[l]/i[l]:0;for(let h=c;h<n;h++){const f=(h-c)*n+h,d=h*n+(h-c);a[f]=t[f]*u,a[d]=t[d]*u}}return a}function $h(e,t=wh,n=bh,s=Ah){const o=e.length,r=new Uint8Array(o),i=[];let a=0;for(let w=0;w<o;w++){const b=e[w];b>0&&i.push(b),b>a&&(a=b)}if(i.length===0)return r.fill(1),r;const c=Math.floor(a)+1,l=c>1?a/(c-1):0,u=w=>{const b=w*l+l/2;let y=0;for(const A of i)y+=Eh((b-A)/s);return y},h=u(-1),f=u(c-1)-h,d=w=>f>0?(u(w)-h)/f:1,m=w=>{let b=0,y=c-1;for(;b<y;){const A=b+y>>>1;d(A)>=w?y=A:b=A+1}return b>0&&Math.abs(d(b-1)-w)<=Math.abs(d(b)-w)?b-1:b},g=m(t),p=m(n);for(let w=0;w<o;w++)(e[w]<g||e[w]>p)&&(r[w]=1);return r}function Eh(e){return .5*Bh(-e/Math.SQRT2)}function Bh(e){const t=Math.abs(e);let n;if(t<2){let s=t,o=t;for(let r=1;r<200&&(s*=2*t*t/(2*r+1),o+=s,!(s<Math.abs(o)*1e-17));r++);n=1-2/Math.sqrt(Math.PI)*Math.exp(-t*t)*o}else if(t>27)n=0;else{let o=1/t,r=1e300,i=o;for(let a=1;a<300;a++){const c=a/2;o=t+c*o,o===0&&(o=1e-300),r=t+c/r,r===0&&(r=1e-300),o=1/o;const l=r*o;if(i*=l,Math.abs(l-1)<1e-16)break}n=Math.exp(-t*t)/Math.sqrt(Math.PI)*i}return e>=0?n:2-n}const Th=9007199254740992;function Ti(e,t){return e<<t|e>>>32-t}function Oi(e){return e=Math.imul(e^e>>>16,2246822507),e=Math.imul(e^e>>>13,3266489909),(e^e>>>16)>>>0}function Il(...e){let t=2166136261;for(const n of e){const s=Number.isFinite(n)?Math.trunc(n):0,o=s>>>0,r=Math.floor(s/4294967296)>>>0;t=Oi((t^o)+2654435769),t=Oi((t^r)+2135587861)}return t}class xl{s0;s1;s2;s3;constructor(t){let n=t|0;const s=()=>{n=n+2654435769|0;let o=n^n>>>16;return o=Math.imul(o,569420461),o^=o>>>15,o=Math.imul(o,1935289751),(o^o>>>15)>>>0};this.s0=s(),this.s1=s(),this.s2=s(),this.s3=s(),(this.s0|this.s1|this.s2|this.s3)===0&&(this.s0=1)}nextUint32(){let t=this.s0,n=this.s1,s=this.s2,o=this.s3;const r=Math.imul(Ti(Math.imul(n,5),7),9)>>>0,i=n<<9;return s^=t,o^=n,n^=s,t^=o,s^=i,o=Ti(o,11),this.s0=t,this.s1=n,this.s2=s,this.s3=o,r}nextDouble(){const t=this.nextUint32()>>>5,n=this.nextUint32()>>>6;return(t*67108864+n)/Th}interval(t){if(t<=0)return 0;if(t<=4294967295){let n=t;for(n|=n>>>1,n|=n>>>2,n|=n>>>4,n|=n>>>8,n|=n>>>16;;){const s=(this.nextUint32()&n)>>>0;if(s<=t)return s}}return Math.floor(this.nextDouble()*(t+1))}}function vl(e,t,n){if(!(t>0)||!(n>0))return 0;if(n>=1)return t;if(n<=.5)return n*t<=30?Li(e,t,n):_i(e,t,n);const s=1-n;return t-(s*t<=30?Li(e,t,s):_i(e,t,s))}function Li(e,t,n){const s=1-n,o=Math.exp(t*Math.log1p(-n)),r=t*n,i=Math.min(t,Math.floor(r+10*Math.sqrt(r*s+1)));let a=0,c=o,l=e.nextDouble();for(;l>c;)a++,a>i?(a=0,c=o,l=e.nextDouble()):(l-=c,c=(t-a+1)*n*c/(a*s));return a}function Xn(e){const t=e*e;return(13680-(462-(132-(99-140/t)/t)/t)/t)/e/166320}function _i(e,t,n){const s=Math.min(n,1-n),o=1-s,r=t*s+s,i=Math.floor(r),a=Math.floor(2.195*Math.sqrt(t*s*o)-4.6*o)+.5,c=i+.5,l=c-a,u=c+a,h=.134+20.5/(15.3+i);let f=(r-l)/(r-l*s);const d=f*(1+f/2);f=(u-r)/(u*o);const m=f*(1+f/2),g=a*(1+2*h),p=g+h/d,w=p+h/m,b=t*s*o,y=A=>n>.5?t-A:A;for(;;){const A=e.nextDouble()*w;let N=e.nextDouble(),M;if(A<=a)return y(Math.floor(c-a*N+A));if(A<=g){const _=l+(A-a)/h;if(N=N*h+1-Math.abs(i-_+.5)/a,N>1)continue;M=Math.floor(_)}else if(A<=p){if(M=Math.floor(l+Math.log(N)/d),M<0||N===0)continue;N=N*(A-g)*d}else{if(M=Math.floor(u-Math.log(N)/m),M>t||N===0)continue;N=N*(A-p)*m}const I=Math.abs(M-i);if(!(I>20&&I<b/2-1)){const _=s/o,E=_*(t+1);let O=1;if(i<M)for(let L=i+1;L<=M;L++)O*=E/L-_;else if(i>M)for(let L=M+1;L<=i;L++)O/=E/L-_;if(N>O)continue;return y(M)}const x=I/b*((I*(I/3+.625)+.16666666666666666)/b+.5),v=-I*I/(2*b),S=Math.log(N);if(S<v-x)return y(M);if(S>v+x)continue;const k=M+1,C=i+1,B=t+1-i,F=t-M+1,$=c*Math.log(C/k)+(t-i+.5)*Math.log(B/F)+(M-i)*Math.log(F*s/(k*o))+Xn(C)+Xn(B)+Xn(k)+Xn(F);if(!(S>$))return y(M)}}const Oh=(()=>{const e=new Float64Array(126);for(let t=2;t<126;t++)e[t]=e[t-1]+Math.log(t);return e})(),Lh=.9189385332046728;function me(e){return e<126?Oh[e]:(e+.5)*Math.log(e)-e+(Lh+1/e*(1/12-1/(360*e*e)))}function kl(e,t,n,s){if(!(s>0)||!(t>0))return 0;if(!(n>0))return Math.min(s,t);const o=t+n;return s>=o?t:s>=10&&s<=o-10?zh(e,t,n,s):_h(e,t,n,s)}function _h(e,t,n,s){const o=t+n,r=Math.floor(o/2);let i=s>r?o-s:s,a=o,c=t;for(;i>0&&c>0&&a>c;)a--,e.interval(a)<c&&c--,i--;return a===c&&(c-=i),s>r?c:t-c}const Dh=1.7155277699214135,Rh=.8989161620588988;function zh(e,t,n,s){const o=t+n,r=Math.min(s,o-s),i=Math.min(t,n),a=Math.max(t,n),c=i/o,l=a/o,h=r*c+.5,f=(o-r)*r*c*l/(o-1),d=Math.sqrt(f+.5),m=Dh*d+Rh,g=Math.floor((r+1)*(i+1)/(o+2)),p=me(g)+me(i-g)+me(r-g)+me(a-r+g),w=Math.min(Math.min(r,i)+1,Math.floor(h+16*d));let b;for(;;){const y=e.nextDouble(),A=e.nextDouble();if(y===0)continue;const N=h+m*(A-.5)/y;if(N<0||N>=w)continue;b=Math.floor(N);const M=me(b)+me(i-b)+me(r-b)+me(a-r+b),I=p-M;if(y*(4-y)-3<=I)break;if(!(y*(y-I)>=1)&&2*Math.log(y)<=I)break}return t>n&&(b=r-b),r<s&&(b=t-b),b}const Ph=1;function qh(e,t){return t===e?"none":t<e?"sub":"over"}const Uh=81,Sl=91,Cl=14,Gh=192*1024*1024,Hh=4096,Qh=32,Kh=1e6;function le(e,t={}){return new T("unsupported",e,{...t,resample:!0})}function Vh(e){return e>=0&&Number.isInteger(e)}class gs{constructor(t){this.starts=t}last=0;get count(){return this.starts.length-1}of(t){const n=this.starts,s=this.last;if(t>=n[s]&&t<n[s+1])return s;let o=0,r=n.length-2;for(;o<r;){const i=o+r+1>>>1;n[i]<=t?o=i:r=i-1}return this.last=o,o}touching(t,n){const s=[];if(n<=t)return s;const o=this.of(Math.max(0,t));for(let r=o;r<this.count&&this.starts[r]<n;r++)this.starts[r+1]>this.starts[r]&&s.push(r);return s}}async function Di(e,t,n={}){const s=new gs(t),o=s.count,r=t[o],i=o<=512?new Float64Array(o*o):null,a=i===null?new Map:null;let c=0,l=null,u=!1;if(await e.scanAll({chunkSize:Kh,...n.pause===void 0?{}:{pause:n.pause},onChunk:p=>{if(n.cancelled?.()===!0)return u=!0,!1;const{bin1:w,bin2:b,count:y,length:A}=p;for(let N=0;N<A;N++){const M=y[N];if(!Vh(M))return l=le(`This map stores ${M} where a number of contacts would be, so there are no contacts to draw from it. Resampling reads raw counts — whole numbers, one per contact.`,{value:M}),!1;if(M===0)continue;const I=w[N],x=b[N];if(x<I)return l=le("This map stores pixels below its diagonal, so each contact would be drawn twice. Resampling reads upper-triangular files — cooler’s default."),!1;const v=s.of(I),S=x<t[v+1]?v:s.of(x);c++,i!==null?i[v*o+S]+=M:a.set(v*o+S,(a.get(v*o+S)??0)+M)}return!0},onProgress:n.onProgress}),l!==null)throw l;if(u)throw new DOMException("The census was cancelled.","AbortError");const h=[];if(i!==null)for(let p=0;p<i.length;p++)i[p]>0&&h.push(p);else{for(const p of a.keys())h.push(p);h.sort((p,w)=>p-w)}const f=new Int32Array(h.length),d=new Int32Array(h.length),m=new Float64Array(h.length);let g=0;for(let p=0;p<h.length;p++){const w=h[p];f[p]=Math.floor(w/o),d[p]=w-f[p]*o,m[p]=i!==null?i[w]:a.get(w),g+=m[p]}return{total:g,nnz:c,sequences:o,nBins:r,a:f,b:d,sum:m}}function Yh(e){if(!Number.isFinite(e)||!Number.isInteger(e)||e<=0)throw le(`${e} is not a number of contacts: it must be a positive whole number.`,{contacts:e});if(e>Number.MAX_SAFE_INTEGER)throw le(`${e} contacts is past what can be counted exactly here (2⁵³).`,{contacts:e});return e}function Wh(e,t,n,s){const o=Yh(t.contacts),r=e.total;if(!(r>0))throw le("This map holds no contacts, so there is nothing to draw from it.",{total:r});const i=t.seed??Ph,a=t.alpha??zr,c=qh(r,o),l=new Map,u={mode:c,contacts:o,total:r,seed:i,alpha:a,shares:l,weightScale:Math.sqrt(r/o)};if(c==="none")return u;const h=new xl(Il(i,Uh,c==="sub"?Sl:Cl)),f=e.sum.length;if(c==="sub"){let g=o,p=r;for(let w=0;w<f&&g>0;w++){const b=e.sum[w],y=kl(h,b,p-b,g);y>0&&l.set(e.a[w]*e.sequences+e.b[w],y),g-=y,p-=b}return u}if(!(a>0))throw le(`Boost-HiC’s alpha must be above zero; ${a} was given.`,{alpha:a});for(let g=0;g<f;g++){const p=e.a[g];if(e.b[g]!==p)continue;const w=n[p+1]-n[p];if(w>xo){const b=s?.[p]??`sequence ${p+1}`;throw le(`${b} is ${w.toLocaleString("en-US")} bins at this resolution, and Boost-HiC — which oversamples it — walks every path between them: at most ${xo.toLocaleString("en-US")}. Open a coarser resolution to oversample this map.`,{sequence:b,bins:w,limit:xo})}}let d=o-r,m=r;for(let g=0;g<f&&d>0;g++){const p=e.sum[g],w=g===f-1?d:vl(h,d,Math.min(1,p/m));w>0&&l.set(e.a[g]*e.sequences+e.b[g],w),d-=w,m-=p}return u}class Ri{bin1;bin2;count;length=0;constructor(t=1024){this.bin1=new Int32Array(t),this.bin2=new Int32Array(t),this.count=new Float64Array(t)}push(t,n,s){this.length===this.bin1.length&&this.grow(this.length*2),this.bin1[this.length]=t,this.bin2[this.length]=n,this.count[this.length]=s,this.length++}grow(t){const n=new Int32Array(t),s=new Int32Array(t),o=new Float64Array(t);n.set(this.bin1.subarray(0,this.length)),s.set(this.bin2.subarray(0,this.length)),o.set(this.count.subarray(0,this.length)),this.bin1=n,this.bin2=s,this.count=o}}function Xh(e,t,n,s,o){let r=t,i=n;const{bin1:a,bin2:c,count:l}=e;for(let u=0;u<l.length&&r>0;u++){const h=l[u];if(h===0)continue;let f;if(r===i)f=h,i-=h;else if(h<=Qh){f=0;for(let d=0;d<h;d++)s.nextDouble()*i<r-f&&f++,i--}else f=kl(s,h,i-h,r),i-=h;r-=f,f>0&&o.push(a[u],c[u],f)}}function Fl(e,t,n){const s=e.length,o=new Float64Array(s);if(t<=0||s===0)return o;const r=new Float64Array(s+1);let i=-1;for(let c=s-1;c>=0;c--){const l=e[c]>0?e[c]:0;r[c]=r[c+1]+l,l>0&&i<0&&(i=c)}if(i<0)return o;let a=t;for(let c=0;c<s&&a>0;c++){const l=e[c];if(!(l>0))continue;const u=c===i?a:vl(n,a,Math.min(1,l/r[c]));o[c]=u,a-=u}return o}function Jh(e,t,n,s){const o=Fl(e.count,t,n),{bin1:r,bin2:i,count:a}=e;for(let c=0;c<a.length;c++){const l=a[c]+o[c];l>0&&s.push(r[c],i[c],l)}}function Zh(e,t,n){const s=new Float64Array(n*n),{bin1:o,bin2:r,count:i}=e;for(let a=0;a<i.length;a++){const c=o[a]-t,l=r[a]-t;s[c*n+l]+=i[a],c!==l&&(s[l*n+c]+=i[a])}return s}function jh(e,t,n,s,o,r,i){const a=Zh(e,t,n),c=Mh(new Float64Array(a),n,o),l=n*(n+1)/2,u=new Float64Array(l);let h=0;for(let d=0;d<n;d++)for(let m=d;m<n;m++)u[h++]=a[d*n+m];if(c!==null){const{kept:d,counts:m}=c,g=d.length;let p=0,w=0;for(let y=0;y<g;y++)for(let A=y;A<g;A++)p+=a[d[y]*n+d[A]],w+=m[y*g+A];const b=w>0?p/w:0;for(let y=0;y<g;y++){const A=d[y],N=A*n-A*(A-1)/2-A;for(let M=y;M<g;M++)u[N+d[M]]=m[y*g+M]*b}}const f=Fl(u,s,r);h=0;for(let d=0;d<n;d++)for(let m=d;m<n;m++,h++){const g=a[d*n+m]+f[h];g>0&&i.push(t+d,t+m,g)}}class tf{constructor(t,n,s,o){this.inner=t,this.geometry=n,this.spec=s,this.index=new gs(n.starts),this.census=o}countKind="i";index;census;planned=null;plan=null;bands=new Map;bytes=0;get nnz(){return this.inner.nnz}get weightScale(){return this.plan?.weightScale??1}ready(){return this.planned??=this.census.then(t=>{if(t.nBins!==this.geometry.starts[this.index.count])throw le("This census was taken over another map.",{bins:t.nBins,nBins:this.geometry.starts[this.index.count]});const n=Wh(t,this.spec,this.geometry.starts,this.geometry.names);return this.plan=n,n}),this.planned}scoreColumns(){return["count"]}binOffsets(){return this.inner.binOffsets()}async forEachWindow(t,n,s,o,r={}){return(await this.ready()).mode==="none"?this.inner.forEachWindow(t,n,s,o,r):this.walk([[s.span[0],s.span[1]]],o,r)}async forEachBands(t,n,s={}){return(await this.ready()).mode==="none"?this.inner.forEachBands(t,n,s):this.walk(t,n,s)}async scanAll(t){if((await this.ready()).mode==="none")return this.inner.scanAll(t);const n=this.geometry.starts[this.index.count];let s=!1;await this.walk([[0,n]],o=>{s||t.onChunk(o)===!1&&(s=!0)},{cancelled:()=>s,onProgress:(o,r)=>t.onProgress?.(o,r)})}async walk(t,n,s){if(s.value!==void 0&&s.value!=="count")throw le(`A resampled map is drawn from its contacts, and “${s.value}” is not a count of them. Read the counts, or balance them, while the map is resampled.`,{value:s.value});const o=await this.ready(),r=ef(t),i=[];for(const[l,u]of r)for(const h of this.index.touching(l,u))i[i.length-1]!==h&&i.push(h);const a=[...new Set(i)].sort((l,u)=>l-u);let c=0;for(let l=0;l<a.length;){if(s.cancelled?.()===!0)return!1;const u=[];let h=0;for(;l<a.length;){const d=a[l],m=this.geometry.starts[d+1]-this.geometry.starts[d];if(u.length>0&&h+m>Hh)break;u.push(d),h+=m,l++}const f=u.filter(d=>!this.bands.has(d));if(f.length>0&&await this.draw(f,o,s)===null)return!1;for(const d of u){const m=this.bands.get(d);this.touch(d);for(const[g,p]of r){const w=Math.max(g,m.from)-m.from,b=Math.min(p,m.from+m.rows.length-1)-m.from;if(b<=w)continue;const y=m.rows[w],A=m.rows[b];A>y&&n({bin1:m.bin1.subarray(y,A),bin2:m.bin2.subarray(y,A),count:m.count.subarray(y,A),length:A-y})}c++,s.onProgress?.(c,a.length)}this.evict(new Set(u)),await vt()}return!0}async draw(t,n,s){const o=this.geometry.starts,r=new Map;for(const u of t)r.set(u,new Ri);const i=new gs(o);let a=-1,c;if(!await this.inner.forEachBands(t.map(u=>[o[u],o[u+1]]),u=>{const{bin1:h,bin2:f,count:d,length:m}=u;for(let g=0;g<m;g++){if(d[g]===0)continue;const p=i.of(h[g]);p!==a&&(a=p,c=r.get(p)),c?.push(h[g],f[g],d[g])}},...s.cancelled===void 0?[]:[{cancelled:s.cancelled}]))return null;for(const u of t){if(this.store(this.drawBand(u,r.get(u),n)),r.delete(u),s.cancelled?.()===!0)return null;await vt()}return!0}drawBand(t,n,s){const o=this.geometry.starts,r=o[t],i=o[t+1],a=this.index.count,c=n.length,l=nf(n),u=new gs(o),h=new Int32Array(c),f=new Map,d=[];let m=-1,g=-1;for(let I=0;I<c;I++){const x=u.of(n.bin2[l[I]]);if(x!==m){let v=f.get(x);v===void 0&&(v=d.length,f.set(x,v),d.push(x)),m=x,g=v}h[I]=g}const p=new Int32Array(d.length+1);for(let I=0;I<c;I++)p[h[I]+1]++;for(let I=0;I<d.length;I++)p[I+1]+=p[I];const w=p.slice(0,d.length),b=new Int32Array(c),y=new Int32Array(c),A=new Float64Array(c);for(let I=0;I<c;I++){const x=l[I],v=w[h[I]]++;b[v]=n.bin1[x],y[v]=n.bin2[x],A[v]=n.count[x]}const N=new Ri(Math.max(1024,s.mode==="sub"?Math.ceil(c/2):c)),M=d.map((I,x)=>({b:I,local:x})).sort((I,x)=>I.b-x.b);for(const{b:I,local:x}of M){const v=p[x],S=p[x+1],k={bin1:b.subarray(v,S),bin2:y.subarray(v,S),count:A.subarray(v,S)};if(s.mode==="none"){for(let F=0;F<k.count.length;F++)N.push(k.bin1[F],k.bin2[F],k.count[F]);continue}const C=s.shares.get(t*a+I)??0,B=new xl(Il(s.seed,s.mode==="sub"?Sl:Cl,t,I));if(s.mode==="sub"){let F=0;for(let $=0;$<k.count.length;$++)F+=k.count[$];Xh(k,C,F,B,N)}else I===t?jh(k,r,i-r,C,s.alpha,B,N):Jh(k,C,B,N)}return sf(t,r,i,N)}store(t){this.bands.set(t.sequence,t),this.bytes+=t.bytes}touch(t){const n=this.bands.get(t);n!==void 0&&(this.bands.delete(t),this.bands.set(t,n))}evict(t){for(const[n,s]of this.bands){if(this.bytes<=Gh)return;t.has(n)||(this.bands.delete(n),this.bytes-=s.bytes)}}}function ef(e){const t=e.filter(([s,o])=>o>s).map(([s,o])=>[s,o]);t.sort((s,o)=>s[0]-o[0]);const n=[];for(const s of t){const o=n[n.length-1];o!==void 0&&s[0]<=o[1]?o[1]=Math.max(o[1],s[1]):n.push(s)}return n}function nf(e){const t=e.length,n=new Int32Array(t);let s=!0;for(let i=0;i<t;i++)n[i]=i,i>0&&(e.bin1[i]<e.bin1[i-1]||e.bin1[i]===e.bin1[i-1]&&e.bin2[i]<e.bin2[i-1])&&(s=!1);if(s)return n;const{bin1:o,bin2:r}=e;return n.sort((i,a)=>o[i]-o[a]||r[i]-r[a])}function sf(e,t,n,s){const o=n-t,r=new Int32Array(o+1);for(let u=0;u<s.length;u++)r[s.bin1[u]-t+1]++;for(let u=0;u<o;u++)r[u+1]+=r[u];const i=r.slice(0,o),a=new Int32Array(s.length),c=new Int32Array(s.length),l=new Float64Array(s.length);for(let u=0;u<s.length;u++){const h=i[s.bin1[u]-t]++;a[h]=s.bin1[u],c[h]=s.bin2[u],l[h]=s.count[u]}return{sequence:e,from:t,bin1:a,bin2:c,count:l,rows:r,bytes:s.length*16+r.length*4}}class Jn{constructor(t,n,s,o,r){this.nBins=o,this.countKind=r,this.cells=s.map(i=>({name:i,store:new Al(t.at(Cs(n,i)),o,r)}))}countKind;cells;offsets=null;onCells=null;get names(){return this.cells.map(t=>t.name)}get nnz(){let t=0;for(const n of this.cells)t+=n.store.nnz;return t}scoreColumns(){const[t,...n]=this.cells;if(t===void 0)return[tt];let s=t.store.scoreColumns();for(const o of n){const r=new Set(o.store.scoreColumns());s=s.filter(i=>r.has(i))}return s.length>0?s:[tt]}binOffsets(){if(this.offsets!==null)return this.offsets;const t=new Float64Array(this.nBins+1);for(const n of this.cells){const s=n.store.binOffsets(),o=Math.min(t.length,s.length);for(let r=0;r<o;r++)t[r]+=s[r]}return this.offsets=t,t}async forEachWindow(t,n,s,o,r={}){let i=0;for(const a of this.cells){if(!await a.store.forEachWindow(t,n,s,o,r))return!1;await vt(),this.onCells?.(++i,this.cells.length)}return!0}async forEachBands(t,n,s={}){let o=0;for(const r of this.cells){if(!await r.store.forEachBands(t,n,s))return!1;await vt(),this.onCells?.(++o,this.cells.length)}return!0}async scanAll(t){const n=this.nnz;let s=0,o=!1;for(const r of this.cells){if(o)return;const i=s;await r.store.scanAll({...t.chunkSize===void 0?{}:{chunkSize:t.chunkSize},...t.storedRows===void 0?{}:{storedRows:t.storedRows},onChunk:a=>{const c=t.onChunk(a);return c===!1&&(o=!0),c},onProgress:a=>{s=i+a,t.onProgress?.(s,n)}}),s=i+r.store.nnz,t.onProgress?.(Math.min(s,n),n)}}async scanPerCell(t){const n=this.nnz;let s=0;for(let o=0;o<this.cells.length;o++){const r=this.cells[o];t.onCell?.(o,r.name);const i=s;let a=!1;if(await r.store.scanAll({...t.chunkSize===void 0?{}:{chunkSize:t.chunkSize},onChunk:c=>{const l=t.onChunk(o,c);return l===!1&&(a=!0),l},onProgress:c=>{s=i+c,t.onProgress?.(s,n)}}),a)return;s=i+r.store.nnz,t.onProgress?.(Math.min(s,n),n),await vt()}}}function zi(e,t){if(t===null||t.length===0)return[...e];const n=new Set(e),s=t.filter(r=>!n.has(r));if(s.length>0)throw new T("cell-not-found",`This file has no ${s.length===1?"cell":"cells"} ${Gt(s.slice(0,8))}. It carries ${e.length} ${e.length===1?"cell":"cells"}, starting ${Gt(e.slice(0,3))}.`,{missing:s,available:e.length});const o=new Set(t);return e.filter(r=>o.has(r))}function xt(e,t){const n=Bl(e),s=Tl(n,t);if(s===null)return n;throw new T("format",`The ${t.field} of this ${t.format} reads ${hr(e)}, ${s.reason}. `+Ze,{format:t.format,field:t.field,value:typeof e=="bigint"?e.toString():e,ceiling:t.ceiling,...t.available===void 0?{}:{available:t.available},...s.most===void 0?{}:{most:s.most}})}function of(e,t){return Tl(Bl(e),t)===null}function Js(e,t,n){const s={format:n.format,field:n.field,offset:e,length:t,size:n.size};if(!ur(e)||!ur(t))throw new T("format",`The ${n.field} of this ${n.format} points at ${hr(t)} bytes from byte ${hr(e)}, which is not a place in a file. ${Ze}`,s);if(n.ceiling!==void 0&&t>n.ceiling)throw new T("format",`The ${n.field} of this ${n.format} is ${It.int(t)} bytes long, more than the ${It.int(n.ceiling)} any real one holds. ${Ze}`,{...s,ceiling:n.ceiling});if(e+t>n.size)throw new T("format",`The ${n.field} of this ${n.format} points at bytes ${It.int(e)} to ${It.int(e+t)}, past the end of a file of ${It.int(n.size)}. ${Ze}`,s)}const Pi=65536,Pr=2**29;async function qr(e,t){if(typeof DecompressionStream>"u")throw new T("unsupported",`Reading a ${t.format} needs DecompressionStream, which this runtime does not have. Every browser since 2023 has it; a very old one does not.`,{format:t.format});const n=new DecompressionStream(t.encoding),s=n.writable.getWriter(),o=n.readable.getReader(),r=[];let i=0,a=!1;const c=(async()=>{for(;;){const{done:g,value:p}=await o.read();if(g)return;if(i+=p.byteLength,i>t.maxBytes){a=!0,await o.cancel();return}r.push(p)}})(),l=(async()=>{for(let g=0;g<e.byteLength;g+=Pi){if(a)return;const p=e.subarray(g,Math.min(g+Pi,e.byteLength));await s.write(p)}a||await s.close()})(),[u,h]=await Promise.allSettled([c,l]);if(a)throw El(t,t.maxBytes,e.byteLength);const f=u.status==="rejected"?u.reason:h.status==="rejected"?h.reason:null;if(f!==null){const g=f instanceof Error?f.message:String(f);throw new T("format",`The ${t.field} of this ${t.format} does not inflate (${g.replace(/\.$/,"")}). ${Ze}`,{format:t.format,field:t.field})}if(r.length===1)return r[0];const d=new Uint8Array(i);let m=0;for(const g of r)d.set(g,m),m+=g.length;return d}function $l(e,t){return Math.min(t.ceiling,Math.max(t.floor,Math.ceil(e*t.ratio)))}function El(e,t,n){return new T("format",`The ${e.field} of this ${e.format} inflates to more than ${It.bytes(t)}, past what any real one holds. ${Ze}`,{format:e.format,field:e.field,maxBytes:t,compressed:n})}const Ze="The file is truncated, corrupt, or not the format its first bytes claim.";function Bl(e){return typeof e=="number"?e:e>=BigInt(Number.MIN_SAFE_INTEGER)&&e<=BigInt(Number.MAX_SAFE_INTEGER)?Number(e):Number.NaN}function ur(e){return Number.isSafeInteger(e)&&e>=0}function Tl(e,t){if(!ur(e))return{reason:"which is not a count"};if(e>t.ceiling)return{reason:`more than the ${It.int(t.ceiling)} any real one declares`};if(t.available!==void 0){const n=Math.floor(Math.max(0,t.available)/Math.max(1,t.bytesPerItem??1));if(e>n){const s=Math.max(0,t.available);return{reason:`and the ${It.int(s)} byte${s===1?"":"s"} after it could hold at most ${It.int(n)}`,most:n}}}return null}function hr(e){return typeof e=="bigint"?e.toString():Number.isSafeInteger(e)?It.int(e):String(e)}const qi=[7,8,9],mt=".hic",Ol=2**31-1,rf=2**40;class ne extends RangeError{needed;constructor(t){super(`a read needs ${t} bytes of a shorter buffer`),this.name="Truncated",this.needed=t}}class rn{constructor(t){this.bytes=t,this.view=new DataView(t.buffer,t.byteOffset,t.byteLength)}view;at=0;get position(){return this.at}set position(t){this.at=t}get remaining(){return this.bytes.byteLength-this.at}need(t){if(this.at+t>this.bytes.byteLength)throw new ne(this.at+t)}int8(){this.need(1);const t=this.view.getInt8(this.at);return this.at+=1,t}int16(){this.need(2);const t=this.view.getInt16(this.at,!0);return this.at+=2,t}int32(){this.need(4);const t=this.view.getInt32(this.at,!0);return this.at+=4,t}int64(){this.need(8);const t=this.view.getBigInt64(this.at,!0);return this.at+=8,t>BigInt(Number.MAX_SAFE_INTEGER)||t<BigInt(Number.MIN_SAFE_INTEGER)?Number.NaN:Number(t)}float32(){this.need(4);const t=this.view.getFloat32(this.at,!0);return this.at+=4,t}float64(){this.need(8);const t=this.view.getFloat64(this.at,!0);return this.at+=8,t}string(){const t=this.at,n=this.bytes.indexOf(0,t);if(n<0)throw new ne(Math.max(t,this.bytes.byteLength)+1);const s=new TextDecoder().decode(this.bytes.subarray(t,n));return this.at=n+1,s}skip(t){if(!(t>=0))throw new RangeError(`a cursor cannot skip ${t} bytes`);this.at+=t}}const af=512*1024;function Ll(e){return e.length>=4&&e[0]===72&&e[1]===73&&e[2]===67&&e[3]===0}function cf(e){let t=Math.min(af,e.size);for(;;){const n=e.readBytes(0,t);try{return lf(n,e.size)}catch(s){if(!(s instanceof ne))throw s;if(t>=e.size)throw new T("format",`This .hic ends inside its header, ${e.size.toLocaleString("en-US")} bytes in. The file is truncated, or is not the format its first four bytes claim.`,{size:e.size});t=Math.min(e.size,Math.max(t*4,s.needed))}}}function lf(e,t=e.byteLength){if(!Ll(e))throw new T("format","This file does not begin with the `.hic` magic bytes.",{});const n=new rn(e);n.position=4;const s=n.int32();if(!qi.includes(s))throw new T("unsupported",`This is a version ${s} .hic file, and this reader accepts versions ${qi.join(", ")}. Versions below 7 predate the block index this reader navigates by, and a version above the list is a format this build has not seen. Convert it with \`hic2cool\` or re-run Juicer Tools \`pre\`.`,{version:s});const o=n.int64(),r=n.string();let i=null,a=null;s>=9&&(i=n.int64(),a=n.int64());const c=()=>t-n.position,l={},u=xt(n.int32(),{format:mt,field:"header attribute count",ceiling:1e6,available:c(),bytesPerItem:2});for(let g=0;g<u;g++){const p=n.string();l[p]=n.string()}const h=xt(n.int32(),{format:mt,field:"chromosome count",ceiling:1e7,available:c(),bytesPerItem:1+(s>=9?8:4)});ko(h>0,"chromosome count");const f=[];for(let g=0;g<h;g++){const p=n.string(),w=xt(s>=9?n.int64():n.int32(),{format:mt,field:`length of chromosome ${p}`,ceiling:rf});f.push({name:p,length:w})}const d=xt(n.int32(),{format:mt,field:"resolution count",ceiling:1e4,available:c(),bytesPerItem:4});ko(d>0,"resolution count");const m=[];for(let g=0;g<d;g++){const p=n.int32();ko(p>0,"bin size"),m.push(p)}return{version:s,footerPosition:o,genomeId:r,attributes:l,chroms:f,bpResolutions:m,normVectorIndexPosition:i,normVectorIndexLength:a,headerBytes:n.position}}function ko(e,t){if(!e)throw new T("format",`The ${t} in this .hic header is not a plausible number. The file is either truncated or is not the format its first four bytes claim.`,{what:t})}function fr(e,t,n,s){return`${e}/${t}/${n}/${s}`}function uf(e,t){const{version:n,footerPosition:s}=t,o=n>=9?8:4;Js(s,o+4,{format:mt,field:"footer position",size:e.size});const r=e.size-s,i=new rn(e.readBytes(s,o)),a=xt(n>=9?i.int64():i.int32(),{format:mt,field:"footer length",ceiling:Number.MAX_SAFE_INTEGER,available:r-o}),c={fileSize:e.size,footerPosition:s};let l=Math.min(r,Math.max(a+o+(1<<20),65536));for(;;){const u=e.readBytes(s,l);try{return ff(u,n,o,c)}catch(h){if(!(h instanceof ne))throw h;if(l>=r)throw new T("format",`This .hic ends inside its footer, which begins ${s.toLocaleString("en-US")} bytes in. The file is truncated, or its header does not point at its footer.`,{footerPosition:s,size:e.size});l=Math.min(r,Math.max(l*4,h.needed))}}}const hf=13;function ff(e,t,n,s){const o=new rn(e);o.position=n;const r=s?.fileSize??e.byteLength,i=s?.footerPosition??0,a=()=>r-(i+o.position),c=new Map,l=xt(o.int32(),{format:mt,field:"master index size",ceiling:1e8,available:a(),bytesPerItem:hf});let u=0;for(let d=0;d<l;d++){const m=o.string(),g=o.int64(),p=o.int32();if(s!==void 0&&(Js(g,p,{format:mt,field:`matrix record for ${m}`,size:s.fileSize}),u+=p,u>s.fileSize))throw new T("format",`The master index of this .hic gives its matrix records ${u.toLocaleString("en-US")} bytes between them by entry ${d+1}, more than the whole file's ${s.fileSize.toLocaleString("en-US")}. The file is corrupt, or not the format its first bytes claim.`,{entries:d+1,bytes:u,size:s.fileSize});c.set(m,{key:m,position:g,size:p})}Ui(o,t,!1,a),Ui(o,t,!0,a);const h=new Map,f=[];if(a()>=4){const d=o.int32(),m=18+(t>=9?8:4),g={ceiling:1e8,available:a(),bytesPerItem:m};if(of(d,g)){let p=0;for(let w=0;w<d;w++){const b=o.string(),y=o.int32(),A=o.string(),N=o.int32(),M=o.int64(),I=t>=9?o.int64():o.int32();h.set(fr(b,y,A,N),{type:b,chromIdx:y,unit:A,binSize:N,position:M,nBytes:I}),f.includes(b)||f.push(b),p+=I}s!==void 0&&!(p<=s.fileSize)&&(h.clear(),f.length=0)}}return{masterIndex:c,normVectors:h,normTypes:f}}function Ui(e,t,n,s){if(s()<4)return;const o=t>=9?4:8,r=xt(e.int32(),{format:mt,field:`${n?"normalised ":""}expected-value vector count`,ceiling:1e6,available:s(),bytesPerItem:(n?1:0)+1+4+(t>=9?8:4)+4});for(let i=0;i<r;i++){n&&e.string(),e.string(),e.int32();const a=xt(t>=9?e.int64():e.int32(),{format:mt,field:"expected-value vector length",ceiling:Ol,available:s(),bytesPerItem:o});e.skip(a*o);const c=xt(e.int32(),{format:mt,field:"expected-value scale factor count",ceiling:1e7,available:s(),bytesPerItem:4+o});e.skip(c*(4+o))}if(e.remaining<0)throw new ne(e.position)}function df(e,t,n){const s=`${n.type} normalisation vector of chromosome ${n.chromIdx}`,o=t>=9?8:4,r=t>=9?4:8;Js(n.position,n.nBytes,{format:mt,field:s,size:e.size});const i=e.readBytes(n.position,o);if(i.byteLength<o)throw new T("format",`The ${s} begins in the last ${i.byteLength} bytes of this .hic, too few for its length. The file is truncated, or its normalisation index does not point at its vectors.`,{type:n.type,chromIdx:n.chromIdx,position:n.position});const a=new rn(i),c=xt(t>=9?a.int64():a.int32(),{format:mt,field:`length of the ${s}`,ceiling:Ol,available:n.nBytes-o,bytesPerItem:r}),l=e.readBytes(n.position+o,c*r),u=new DataView(l.buffer,l.byteOffset,l.byteLength),h=new Float64Array(c);for(let f=0;f<c;f++){const d=r===4?u.getFloat32(f*4,!0):u.getFloat64(f*8,!0);h[f]=d>0&&Number.isFinite(d)?1/d:Number.NaN}return h}const Gi={ratio:64,floor:16<<20,ceiling:1<<30},dr=2**26,mf=37,Hi=16,ps=2**31-1,So=4096;function Zs(e,t,n,s){const o=new rn(e),r=()=>s===void 0?void 0:s-o.position;o.int32(),o.int32();const i=xt(o.int32(),{format:mt,field:"resolution count of a matrix record",ceiling:1e4,available:r(),bytesPerItem:mf});for(let a=0;a<i;a++){const c=o.string();o.int32();const l=o.float32();o.float32(),o.float32(),o.float32();const u=o.int32(),h=o.int32(),f=o.int32(),d=xt(o.int32(),{format:mt,field:"block count of a matrix record",ceiling:ps,available:r(),bytesPerItem:Hi});if(c!==t||u!==n){o.skip(d*Hi);continue}if(!(h>=1)||!(f>=1))throw new T("format",`This .hic's ${u} bp matrix declares blocks of ${h} bins in ${f} columns, which no block grid has. The file is truncated, corrupt, or not the format its first bytes claim.`,{binSize:u,blockBinCount:h,blockColumnCount:f});const m=new Map;for(let g=0;g<d;g++){const p=o.int32(),w=o.int64(),b=o.int32();m.set(p,{number:p,position:w,size:b})}return{unit:c,binSize:u,blockBinCount:h,blockColumnCount:f,blocks:m,sumCounts:l}}return null}function gf(e,t,n,s,o){if(o>0&&o<t.size){try{return{zoom:Zs(e(t.position,o),n,s,t.size),beyondHead:!1}}catch(r){if(!(r instanceof ne))throw r}return{zoom:tn(e(t.position,t.size),t,n,s),beyondHead:!0}}return{zoom:tn(e(t.position,t.size),t,n,s),beyondHead:!1}}async function pf(e,t,n,s,o){if(o>0&&o<t.size){try{return{zoom:Zs(await e(t.position,o),n,s,t.size),beyondHead:!1}}catch(r){if(!(r instanceof ne))throw r}return{zoom:tn(await e(t.position,t.size),t,n,s),beyondHead:!0}}return{zoom:tn(await e(t.position,t.size),t,n,s),beyondHead:!1}}function tn(e,t,n,s){try{return Zs(e,n,s,t.size)}catch(o){throw o instanceof ne?new T("format",`The matrix record for ${t.key} in this .hic ends inside its block index, ${e.byteLength.toLocaleString("en-US")} bytes in. The file is truncated, or its master index does not point at its records.`,{key:t.key,position:t.position,size:t.size}):o}}function Zn(e,t,n){const s=[...e].sort((r,i)=>r.position-i.position),o=[];for(const r of s){const i=o[o.length-1],a=r.position+r.size;if(i!==void 0&&r.position-(i.position+i.length)<=t&&a-i.position<=n){i.length=Math.max(i.length,a-i.position),i.refs.push(r);continue}o.push({position:r.position,length:r.size,refs:[r]})}return o}function Qi(e,t,n,s,o){const r=n&&t>=9?Af(e,s,o):bf(e,n,s,o);let i=0;for(const c of r)i+=Math.max(0,c.r2-c.r1+1)*Math.max(0,c.c2-c.c1+1);if(i>Math.max(wf,e.blocks.size))return yf(e,r);const a=new Set;for(const{r1:c,r2:l,c1:u,c2:h}of r)for(let f=c;f<=l;f++)for(let d=u;d<=h;d++)a.add(f*e.blockColumnCount+d);return[...a]}const wf=1<<20;function bf(e,t,n,s){const{blockBinCount:o}=e,r=Math.floor(n[0]/o),i=Math.floor(Math.max(n[0],n[1]-1)/o),a=Math.floor(s[0]/o),c=Math.floor(Math.max(s[0],s[1]-1)/o),l=[{r1:a,r2:c,c1:r,c2:i}];return t&&l.push({r1:r,r2:i,c1:a,c2:c}),l}function Af(e,t,n){const{blockBinCount:s}=e,[o,r]=[t[0],Math.max(t[0],t[1]-1)],[i,a]=[n[0],Math.max(n[0],n[1]-1)],c=Math.floor((o+i)/2/s),l=Math.floor((r+a)/2/s)+1,u=Math.floor(Math.log2(1+Math.abs(o-a)/Math.SQRT2/s)),h=Math.floor(Math.log2(1+Math.abs(r-i)/Math.SQRT2/s)),d=o>a&&r<i||r>i&&o<a?0:Math.min(u,h),m=Math.max(u,h)+1;return[{r1:Math.max(0,d),r2:m,c1:Math.max(0,c),c2:l}]}function yf(e,t){const n=e.blockColumnCount,s=[];for(const o of e.blocks.keys()){const r=Math.floor(o/n),i=o-r*n;t.some(a=>r>=a.r1&&r<=a.r2&&i>=a.c1&&i<=a.c2)&&s.push(o)}return s.sort((o,r)=>o-r)}function Nf(e,t){try{return Mf(e,t)}catch(n){throw n instanceof ne?new T("format",`A block of this .hic ends before its records do, ${e.byteLength.toLocaleString("en-US")} bytes in. The file is truncated, corrupt, or not the format its first bytes claim.`,{bytes:e.byteLength}):n}}function Mf(e,t){const n=new rn(e),s=n.int32();if(s===0)return{x:new Int32Array(0),y:new Int32Array(0),value:new Float32Array(0),length:0};const o=n.int32(),r=n.int32(),i=n.int8()===0,a=t>8?n.int8()===0:!0,c=t>8?n.int8()===0:!0,l=n.int8();if(l!==1&&l!==2)throw new T("format",`A block of this .hic is in encoding ${l}, and the format has two: 1, a list of rows, and 2, a dense rectangle. The file is corrupt, or was written by a tool this reader has not seen.`,{type:l});const u=i?2:4,h=l===1?(a?2:4)+u:u,f=xt(s,{format:mt,field:"record count of a block",ceiling:dr,available:n.remaining,bytesPerItem:h});let d=new Int32Array(f),m=new Int32Array(f),g=new Float32Array(f),p=0;const w=()=>i?n.int16():n.float32();if(l===1){const A=xt(c?n.int16():n.int32(),{format:mt,field:"row count of a block",ceiling:ps,available:n.remaining,bytesPerItem:(c?2:4)+(a?2:4)});for(let N=0;N<A;N++){const M=r+(c?n.int16():n.int32()),I=xt(a?n.int16():n.int32(),{format:mt,field:"column count of a block row",ceiling:ps,available:n.remaining,bytesPerItem:h});for(let x=0;x<I;x++){const v=o+(a?n.int16():n.int32()),S=w();(i?S===-32768:!Number.isFinite(S))||(p===d.length&&([d,m,g]=Ki(d,m,g,n.remaining,h)),d[p]=v,m[p]=M,g[p]=S,p+=1)}}return{x:d,y:m,value:g,length:p}}const b=xt(n.int32(),{format:mt,field:"cell count of a dense block",ceiling:ps,available:n.remaining-2,bytesPerItem:u}),y=n.int16();if(b>0&&!(y>=1))throw new T("format",`A dense block of this .hic is ${y} bins wide, which no rectangle is. The file is truncated, corrupt, or not the format its first bytes claim.`,{width:y});for(let A=0;A<b;A++){const N=w();if(i?N===-32768:!Number.isFinite(N))continue;const M=Math.floor(A/y);p===d.length&&([d,m,g]=Ki(d,m,g,n.remaining,h)),d[p]=o+(A-M*y),m[p]=r+M,g[p]=N,p+=1}return{x:d,y:m,value:g,length:p}}function Ki(e,t,n,s,o){const r=e.length,i=Math.floor(Math.max(0,s)/o)+1,a=r+Math.min(Math.max(r,1024),i);if(a>dr)throw new T("format",`A block of this .hic holds more than ${dr.toLocaleString("en-US")} records, more than any real one. The file is corrupt, or not the format its first bytes claim.`,{records:a});const c=new Int32Array(a),l=new Int32Array(a),u=new Float32Array(a);return c.set(e),l.set(t),u.set(n),[c,l,u]}const mr=48<<20,Vi=4,If=6,xf=64<<20,Yi=28,jn=65536,ts=16<<20,vf=4096,kf=mr/2,Sf=1024,Wi=["SCALE","KR","INTER_KR","GW_KR","VC_SQRT","VC"],Me="BP";class Xi{free;waiting=[];constructor(t){this.free=Math.max(1,t)}async run(t){this.free>0?this.free-=1:await new Promise(n=>this.waiting.push(n));try{return await t()}finally{const n=this.waiting.shift();n===void 0?this.free+=1:n()}}}class Ur{header;footer;chroms;binSize;resolutions;chromOffset;nBins;normType;source;fileIndex;zooms=new Map;zoomsInFlight=new Map;beyondHead=!1;blockCache=new Map;blockCacheBytes=0;normCache=new Map;normFallbacks=new Map;exactSlots;backgroundSlots;constructor(t,n,s,o){this.source=t,this.header=n,this.footer=s;const r=[],i=[];n.chroms.forEach((c,l)=>{c.name.toLowerCase()!=="all"&&(r.push(c),i.push(l))}),this.chroms=r,this.fileIndex=Int32Array.from(i),this.resolutions=[...n.bpResolutions].sort((c,l)=>c-l),this.binSize=o,this.chromOffset=new Float64Array(r.length+1);for(let c=0;c<r.length;c++)this.chromOffset[c+1]=this.chromOffset[c]+Math.ceil(r[c].length/o);this.nBins=this.chromOffset[r.length];const a=this.walkAhead();this.exactSlots=new Xi(a===Vi?If:a),this.backgroundSlots=new Xi(a),this.normType=Wi.find(c=>r.some((l,u)=>s.normVectors.has(fr(c,this.fileIndex[u],Me,o))))??null}static open(t,n={}){const s=cf(t),o=uf(t,s),r=[...s.bpResolutions].sort((a,c)=>a-c);if(r.length===0)throw new T("unsupported","This .hic carries no base-pair resolutions. Fragment-resolution matrices are variable-bin and are excluded from this viewer.",{});const i=n.binSize??Zu(r);if(!r.includes(i))throw new T("unsupported",`This .hic has no ${i} bp resolution. It offers ${r.join(", ")}.`,{binSize:i,available:r});return new Ur(t,s,o,i)}stats(){return this.source.stats()}fileIndexOf(t){return this.fileIndex[t]}chromExtent(t){return[this.chromOffset[t],this.chromOffset[t+1]]}chromAt(t){let n=0,s=this.chroms.length-1;for(;n<s;){const o=n+s+1>>1;this.chromOffset[o]<=t?n=o:s=o-1}return n}chromsSpanning(t){const n=[];if(t[1]<=t[0])return n;const s=this.chromAt(t[0]),o=this.chromAt(Math.min(t[1]-1,this.nBins-1));for(let r=s;r<=o;r++)n.push(r);return n}totalCount(){let t=0;for(const n of this.footer.masterIndex.keys()){const s=this.zoomAt(n);s!==null&&(t+=s.sumCounts)}return t}weights(t,n){const s=new Float64Array(Math.max(0,n-t)).fill(Number.NaN);if(this.normType===null||n<=t)return s;for(const o of this.chromsSpanning([t,n])){const r=this.normVector(o);if(r===null)continue;const i=this.chromOffset[o],a=Math.max(t,i),c=Math.min(n,this.chromOffset[o+1]);for(let l=a;l<c;l++){const u=l-i;s[l-t]=u<r.length?r[u]:Number.NaN}}return s}normVector(t){if(this.normCache.has(t))return this.normCache.get(t)??null;let n=null;if(this.normType!==null){const s=this.normType,o=[s,...Wi.filter(r=>r!==s)];for(const r of o){const i=this.footer.normVectors.get(fr(r,this.fileIndex[t],Me,this.binSize));if(i===void 0)continue;const a=df(this.source,this.header.version,i);if(a.some(c=>Number.isFinite(c))){n=a,r!==s&&this.normFallbacks.set(t,r);break}}}return this.normCache.set(t,n),n}normTypeFor(t){return this.normType===null||(this.normCache.has(t)||this.normVector(t),(this.normCache.get(t)??null)===null)?null:this.normFallbacks.get(t)??this.normType}zoomAt(t){if(this.zooms.has(t))return this.zooms.get(t)??null;const n=this.footer.masterIndex.get(t);let s=null;if(n!==void 0){const o=gf((r,i)=>this.source.readBytes(r,i),n,Me,this.binSize,this.beyondHead?0:So);s=this.fitted(t,o.zoom),o.beyondHead&&(this.beyondHead=!0)}return this.zooms.set(t,s),s}fitted(t,n){if(n===null)return null;let s=0;for(const o of n.blocks.values())s+=Math.max(0,o.size);if(!(s<=this.source.size))throw new T("format",`The ${this.binSize} bp blocks of this .hic's ${t} matrix claim ${s.toLocaleString("en-US")} bytes between them, more than the whole file's ${this.source.size.toLocaleString("en-US")}. The file is corrupt, or not the format its first bytes claim.`,{key:t,binSize:this.binSize,bytes:s,size:this.source.size});return n}zoomAtSparse(t){if(this.zooms.has(t))return Promise.resolve(this.zooms.get(t)??null);const n=this.zoomsInFlight.get(t);if(n!==void 0)return n;const s=this.footer.masterIndex.get(t);if(s===void 0||this.source.readExact===void 0)return Promise.resolve(this.zoomAt(t));const o=pf((r,i)=>this.exactRead(r,i),s,Me,this.binSize,this.beyondHead?0:So).then(r=>{r.beyondHead&&(this.beyondHead=!0);const i=this.fitted(t,r.zoom);return this.zooms.set(t,i),i}).finally(()=>this.zoomsInFlight.delete(t));return this.zoomsInFlight.set(t,o),o}zoomFor(t,n){return this.zoomAt(this.pairKey(t,n))}pairKey(t,n){return`${this.fileIndex[t]}_${this.fileIndex[n]}`}walkAhead(){let t=0;for(let s=0;s<this.chroms.length;s++)t=Math.max(t,this.chromOffset[s+1]-this.chromOffset[s]);const n=Math.max(1,t*t*Yi);return Math.max(1,Math.min(Vi,Math.floor(xf/n)))}walkPlans(t){const n=[];let s=[],o=0;for(const r of t){const i=this.binsOf(r[0])*this.binsOf(r[1])*Yi;s.length>0&&(o+i>kf||s.length>=Sf)&&(n.push(s),s=[],o=0),s.push(r),o+=i}return s.length>0&&n.push(s),n}async prefetchWalk(t,n={}){if(this.source.readExact===void 0)return!0;const s=[],o=new Set;for(const h of t){const f=this.pairKey(Math.min(h.a,h.b),Math.max(h.a,h.b));if(o.has(f)||this.zooms.has(f))continue;o.add(f);const d=this.footer.masterIndex.get(f);d===void 0?this.zooms.set(f,null):s.push(d)}const r=[];for(const h of s){const f=this.beyondHead?h.size:Math.min(h.size,So);r.push({position:h.position,size:f,entry:h,head:!0});const d=h.position+h.size,m=Math.min(vf,this.source.size-d);m>0&&r.push({position:d,size:m,entry:h,head:!1})}const i=new Map,a=[];if(!await this.fetchRuns(Zn(r,jn,ts),n,(h,f)=>{for(const d of h.refs){const m=d.position-h.position,g=f.subarray(m,m+d.size);if(!d.head){i.set(d.entry.key,{position:d.position,bytes:g.slice()});continue}const{entry:p}=d;try{const w=d.size===p.size?tn(g,p,Me,this.binSize):Zs(g,Me,this.binSize,p.size);this.zooms.set(p.key,this.fitted(p.key,w))}catch(w){if(!(w instanceof ne))throw w;this.beyondHead=!0,a.push({position:p.position,size:p.size,entry:p})}}})||a.length>0&&!await this.fetchRuns(Zn(a,jn,ts),n,(f,d)=>{for(const m of f.refs){const g=m.position-f.position,p=tn(d.subarray(g,g+m.size),m.entry,Me,this.binSize);this.zooms.set(m.entry.key,this.fitted(m.entry.key,p))}}))return!1;const l=[],u=new Set;for(const h of t){const f=this.windowOf(h.a,h.b,h.rows,h.cols);if(f===null)continue;const d=this.pairKey(f.low,f.high),m=this.zooms.get(d);if(m==null)continue;const g=i.get(d);for(const p of Qi(m,this.header.version,f.intra,f.lowSpan,f.highSpan)){const w=m.blocks.get(p);if(w===void 0||w.size<=0)continue;const b=this.blockKey(w);if(u.has(b)||this.blockCache.has(b))continue;u.add(b),this.checkBlock(w);const y=g===void 0?-1:w.position-g.position;g!==void 0&&y>=0&&y+w.size<=g.bytes.byteLength?await this.decode(w,g.bytes.subarray(y,y+w.size)):l.push(w)}}return this.fetchRuns(Zn(l,jn,ts),n,async(h,f)=>{for(const d of h.refs){const m=d.position-h.position;await this.decode(d,f.subarray(m,m+d.size))}})}async fetchRuns(t,n,s){let o=!1;return await Promise.all(t.map(async r=>{await n.pause?.();const i=()=>this.exactSlots.run(async()=>o||n.cancelled?.()===!0?(o=!0,null):this.readExactNow(r.position,r.length)),a=n.pause===void 0?await i():await this.backgroundSlots.run(i);a!==null&&await s(r,a)})),!o}exactRead(t,n){return this.exactSlots.run(()=>this.readExactNow(t,n))}readExactNow(t,n){const s=this.source,o=s.readExact;return o===void 0?Promise.resolve(s.readBytes(t,n)):o.call(s,t,n)}binsOf(t){return this.chromOffset[t+1]-this.chromOffset[t]}hasAnyMatrix(){for(let t=0;t<this.chroms.length;t++)if(this.zoomFor(t,t)!==null)return!0;return!1}indexedPairs(){const t=new Map;this.fileIndex.forEach((o,r)=>t.set(o,r));const n=new Set,s=[];for(const o of this.footer.masterIndex.keys()){const[r,i]=o.split("_").map(h=>Number.parseInt(h,10)),a=t.get(r),c=t.get(i);if(a===void 0||c===void 0)continue;const l=a<=c?[a,c]:[c,a],u=`${l[0]}_${l[1]}`;n.has(u)||(n.add(u),s.push(l))}return s.sort((o,r)=>o[0]-r[0]||o[1]-r[1]),s}async pairRecords(t,n,s,o,r=!1){const[i,a]=t<=n?[t,n]:[n,t],c=r?await this.zoomAtSparse(this.pairKey(i,a)):this.zoomFor(i,a);if(c===null)return null;const l=this.windowOf(t,n,s,o);if(l===null)return null;const{intra:u,lowSpan:h,highSpan:f}=l,d=Qi(c,this.header.version,u,h,f),m=this.chromOffset[i],g=this.chromOffset[a];let p=0;const w=await this.blocks(c,d,r);for(const M of w)p+=M.length;if(p===0)return null;const b=new Int32Array(p),y=new Int32Array(p),A=new Float64Array(p);let N=0;for(const M of w)for(let I=0;I<M.length;I++){const x=m+M.x[I],v=g+M.y[I];x<=v?(b[N]=x,y[N]=v):(b[N]=v,y[N]=x),A[N]=M.value[I],N+=1}return{bin1:b,bin2:y,count:A,length:N}}windowOf(t,n,s,o){const[r,i]=t<=n?[t,n]:[n,t],a=r===i,[c,l]=a?[this.clipSpan(r,s),this.clipSpan(r,o)]:[this.localSpan(r,s,o),this.localSpan(i,s,o)];return c===null||l===null?null:{low:r,high:i,intra:a,lowSpan:c,highSpan:l}}clipSpan(t,n){const s=this.chromOffset[t],o=Math.max(n[0],s),r=Math.min(n[1],this.chromOffset[t+1]);return r>o?[o-s,r-s]:null}localSpan(t,n,s){const o=this.chromOffset[t],r=this.chromOffset[t+1];let i=Number.POSITIVE_INFINITY,a=Number.NEGATIVE_INFINITY;for(const c of[n,s]){const l=Math.max(c[0],o),u=Math.min(c[1],r);u<=l||(i=Math.min(i,l-o),a=Math.max(a,u-o))}return Number.isFinite(i)?[i,a]:null}async blocks(t,n,s){const o=new Map,r=[];for(const a of n){const c=t.blocks.get(a);if(c===void 0||c.size<=0)continue;const l=this.held(c);if(l!==void 0){o.set(a,l);continue}this.checkBlock(c),r.push(c)}if(s&&this.source.readExact!==void 0)for(const a of Zn(r,jn,ts)){const c=await this.exactRead(a.position,a.length);for(const l of a.refs){const u=l.position-a.position;o.set(l.number,await this.decode(l,c.subarray(u,u+l.size)))}}else for(const a of r)o.set(a.number,await this.decode(a,this.source.readBytes(a.position,a.size)));const i=[];for(const a of n){const c=o.get(a);c!==void 0&&i.push(c)}return i}checkBlock(t){Js(t.position,t.size,{format:mt,field:`block ${t.number} of the ${this.binSize} bp matrix`,size:this.source.size,ceiling:Gi.ceiling})}held(t){const n=this.blockKey(t),s=this.blockCache.get(n);if(s!==void 0)return this.blockCache.delete(n),this.blockCache.set(n,s),s}async decode(t,n){const s=await qr(n,{format:mt,field:`block ${t.number} of the ${this.binSize} bp matrix`,encoding:"deflate",maxBytes:$l(n.byteLength,Gi)}),o=Nf(s,this.header.version);return this.remember(this.blockKey(t),o),o}blockKey(t){return`${this.binSize}/${t.position}`}remember(t,n){const s=n.x.byteLength+n.y.byteLength+n.value.byteLength;if(!(s>mr)){this.blockCache.set(t,n),this.blockCacheBytes+=s;for(const o of this.blockCache.keys()){if(this.blockCacheBytes<=mr)break;const r=this.blockCache.get(o);r!==void 0&&(this.blockCacheBytes-=r.x.byteLength+r.y.byteLength+r.value.byteLength),this.blockCache.delete(o)}}}close(){this.zooms.clear(),this.blockCache.clear(),this.normCache.clear(),this.blockCacheBytes=0}}function Ji(e){throw new T("unsupported",`A .hic has no ${e}: it is indexed per chromosome pair and per block, not as one sorted pixel table. Read pixels through PixelStore (metacooler/store.ts), which is the seam that exists for exactly this difference.`,{path:e})}function Cf(e,t){const n=()=>on(t.chroms.length,["name","length"],{name:t.chroms.map(o=>o.name),length:Float64Array.from(t.chroms,o=>o.length)}),s={format:"HIC","format-url":"https://github.com/aidenlab/juicer/wiki/Data-structure","format-version":t.header.version,"bin-size":t.binSize,"bin-type":"fixed",ordering:"genome-then-length","storage-mode":"symmetric-upper",assembly:t.header.genomeId,nbins:t.nBins,ncontigs:t.chroms.length,nnz:null,sum:null,ngenomes:null,...t.header.attributes,...t.normType===null?{}:{"hic-normalisation":t.normType}};return{uri:e,group:"/",rootEntries:t.resolutions.map(o=>String(o)),rootEntryGroup:"resolutions",attrs:()=>({...s}),attrsAt:o=>o===""||o==="/"?{...s}:{},has(o){return o==="bins/weight"?t.normType!==null:["chroms","chroms/name","chroms/length","indexes","pixels","bins"].includes(o)},keys(o){return o===""||o==="/"?["chroms","bins","pixels","indexes"]:o==="chroms"?["name","length"]:o==="pixels"?["bin1_id","bin2_id","count"]:o==="bins"?t.normType===null?[]:["weight"]:[]},isSoftLink:()=>!1,datasetLength(o){return o==="chroms/name"||o==="chroms/length"?t.chroms.length:o==="bins/weight"?t.nBins:(o.startsWith("pixels/"),0)},dtype(o){return o==="pixels/count"?"<f":o==="chroms/length"?"<i":o==="chroms/name"?"S":"<d"},readNumeric(o,r=0,i){if(o==="indexes/chrom_offset")return t.chromOffset.slice();if(o==="chroms/length"){const a=i??t.chroms.length;return Float64Array.from(t.chroms.slice(r,a),c=>c.length)}if(o==="bins/weight"){if(t.normType===null)throw new T("unsupported","This .hic carries no normalisation vectors, so there are no weights to balance with. Re-run Juicer Tools `addNorm`, or view the raw counts.",{});return t.weights(r,i??t.nBins)}throw(o==="indexes/bin1_offset"||o.startsWith("pixels/"))&&Ji(o),new T("unsupported",`A .hic has no ${o}.`,{path:o})},readInt32(o){throw o.startsWith("pixels/")&&Ji(o),new T("unsupported",`A .hic has no ${o}.`,{path:o})},readStrings(o,r=0,i){if(o==="chroms/name")return t.chroms.slice(r,i??t.chroms.length).map(a=>a.name);throw new T("unsupported",`A .hic has no ${o}.`,{path:o})},readTable(o){if(o==="chroms"||o==="contigs")return n();throw new T("unsupported",`A .hic has no /${o} table.`,{path:o})},at(o){throw new T("unsupported",`A .hic has no /${o} group to open.`,{path:o})},close:()=>t.close()}}function Ff(e,t){return e*1e6+t}class $f{constructor(t){this.file=t}nnz=null;countKind="f";rectangular=!0;scoreColumns(){return["count"]}binOffsets(){throw new T("unsupported","A .hic has no per-bin pixel index, so the stored-pixel density column cannot be computed for one. It is a metagenome diagnostic and a .hic has no genomes, so nothing that draws asks for it.",{})}async forEachWindow(t,n,s,o,r={}){this.refuseOtherColumns(r.value);const i=this.pairsFor(t,n),a=i.length>1;return this.walk(i,(c,l)=>this.file.pairRecords(c,l,t,n,a),o,r,()=>[t,n])}async forEachBands(t,n,s={}){this.refuseOtherColumns(s.value);const o=new Set;for(const l of t)for(const u of this.file.chromsSpanning(l))o.add(u);const r=[0,this.file.nBins],i=this.file.indexedPairs().filter(([l])=>o.has(l)),a=i.length>1,c=l=>{const u=this.clipTo(l,t);return u===null?null:[u,r]};return this.walk(i,(l,u)=>{const h=c(l);return h===null?Promise.resolve(null):this.file.pairRecords(l,u,h[0],h[1],a)},n,s,c)}async scanAll(t){const n=[0,this.file.nBins],s=this.file.indexedPairs(),o=s.length>1;await this.inOrder(s,(r,i)=>this.file.pairRecords(r,i,n,n,o),(r,i)=>{const a=r!==null&&t.onChunk(r)===!1;return t.onProgress?.(i,s.length),!a},void 0,t.pause,()=>[n,n])}pairsFor(t,n){const s=this.file.chromsSpanning(t),o=this.file.chromsSpanning(n),r=new Set,i=[];for(const a of s)for(const c of o){const l=a<=c?[a,c]:[c,a],u=Ff(l[0],l[1]);r.has(u)||(r.add(u),i.push(l))}return i}clipTo(t,n){const[s,o]=this.file.chromExtent(t);let r=Number.POSITIVE_INFINITY,i=Number.NEGATIVE_INFINITY;for(const a of n){const c=Math.max(a[0],s),l=Math.min(a[1],o);l<=c||(r=Math.min(r,c),i=Math.max(i,l))}return Number.isFinite(r)?[r,i]:null}walk(t,n,s,o,r){return this.inOrder(t,n,(i,a)=>(i!==null&&i.length>0&&s(i),o.onProgress?.(a,t.length),!0),()=>o.cancelled?.()===!0,void 0,r)}async inOrder(t,n,s,o,r,i){const a=t.length>1?this.file.walkAhead():1,c=this.planned(t,o,r,i),l=[];let u=0,h=0;for(;h<t.length;){if(await(r?.()??vt()),o?.()===!0)return!1;for(;l.length<a&&u<t.length;){const[d,m]=t[u],g=c(u).then(p=>p?n(d,m):null);u+=1,g.catch(()=>{}),l.push(g)}const f=await l.shift();if(h+=1,!s(f,h))return!1}return!0}planned(t,n,s,o){if(t.length<2||o===void 0)return()=>Promise.resolve(!0);const r=this.file.walkPlans(t),i=[];r.forEach((c,l)=>c.forEach(()=>i.push(l)));const a=new Map;return c=>{const l=i[c];let u=a.get(l);if(u===void 0){const h=r[l].flatMap(([f,d])=>{const m=o(f,d);return m===null?[]:[{a:f,b:d,rows:m[0],cols:m[1]}]});u=this.file.prefetchWalk(h,{pause:s,cancelled:n}),a.set(l,u)}return u}}refuseOtherColumns(t){if(!(t===void 0||t==="count"))throw new T("unsupported",`A .hic stores one value per record, so there is no '${t}' column to draw. Extra per-pixel columns are a cooler feature.`,{value:t})}}const Gr=4<<20,Hr=12,Ef=65536;function _l(e){return e.length>=2&&e[0]===31&&e[1]===139}function Dl(e,t=0){return e.length<t+Hr||e[t]!==31||e[t+1]!==139||e[t+2]!==8||(e[t+3]&4)===0?!1:Qr(e,t)!==null}function Qr(e,t=0){if(e.length<t+Hr)return null;const n=new DataView(e.buffer,e.byteOffset,e.byteLength),s=n.getUint16(t+10,!0);let o=t+12;const r=Math.min(o+s,e.length);for(;o+4<=r;){const i=e[o],a=e[o+1],c=n.getUint16(o+2,!0);if(i===66&&a===67&&c===2)return n.getUint16(o+4,!0)+1;o+=4+c}return null}async function Rl(e){return e.length===0?e:qr(e,{format:"BGZF file",field:"member",encoding:"deflate-raw",maxBytes:Ef})}function Bf(e,t,n){return new DataView(e.buffer,e.byteOffset,e.byteLength).getUint32(t+n-4,!0)}function Tf(e,t,n){const o=new DataView(e.buffer,e.byteOffset,e.byteLength).getUint16(t+10,!0);return e.subarray(t+12+o,t+n-8)}function Kr(e,t={}){const n=e.readBytes(0,Math.min(e.size,64));return Dl(n)?Lf(e,t):_l(n)?Zi(e,t,Of).pipeThrough(new DecompressionStream("gzip")):Zi(e,t,Gr)}const Of=65536;function Zi(e,t,n){const s=t.from??0,o=Math.min(t.to??e.size,e.size),r=Math.max(0,o-s);let i=s,a=0;return new ReadableStream({pull(c){if(t.cancelled?.()===!0||i>=o){c.close();return}const l=e.readBytes(i,Math.min(Gr,o-i));if(l.length===0){c.close();return}i+=l.length,a+=l.length;for(let u=0;u<l.length;u+=n)c.enqueue(l.subarray(u,Math.min(u+n,l.length)));t.onProgress?.(a,r)}})}function Lf(e,t){const n=t.from??0,s=Math.min(t.to??e.size,e.size),o=Math.max(0,s-n);let r=n,i=0,a=new Uint8Array(0),c=!1;const l=u=>{t.onFraming?.({partialMember:a.length>0,endedOnEofMarker:c}),u.close()};return new ReadableStream({async pull(u){for(;;){if(t.cancelled?.()===!0){u.close();return}const h=a.length>=Hr?Qr(a):null;if(h!==null&&a.length>=h){const d=Tf(a,0,h),m=Bf(a,0,h);if(a=a.subarray(h),c=m===0,m>0){u.enqueue(await Rl(d));return}continue}if(r>=s){l(u);return}const f=e.readBytes(r,Math.min(Gr,s-r));if(f.length===0){l(u);return}if(r+=f.length,i+=f.length,t.onProgress?.(i,o),a.length===0)a=f.slice();else{const d=new Uint8Array(a.length+f.length);d.set(a),d.set(f,a.length),a=d}}}})}async function Vr(e,t,n){const s=e.getReader();let o=[],r=0;const i=a=>{if(r===0)return a;const c=new Uint8Array(r+a.length);let l=0;for(const u of o)c.set(u,l),l+=u.length;return c.set(a,l),o=[],r=0,c};try{for(;;){if(n?.()===!0)return!1;const{done:a,value:c}=await s.read();if(a)break;let l=0;for(let u=c.indexOf(10);u>=0;u=c.indexOf(10,l)){const h=i(c.subarray(l,u)),f=h.length>0&&h[h.length-1]===13?h.length-1:h.length;t(h.subarray(0,f)),l=u+1}if(l<c.length&&(o.push(c.slice(l)),r+=c.length-l,r>Co))throw new T("format",`A line of this .pairs file runs past ${Co>>20} MiB without ending, and no pairs line is that long. The file is corrupt, or is not a .pairs file.`,{maxLineBytes:Co})}return r>0&&t(i(new Uint8Array(0))),!0}finally{s.releaseLock()}}const Co=64<<20,_f=["readID","chr1","pos1","chr2","pos2","strand1","strand2"],es={chrom1:["chr1","chrom1","chromosome1"],pos1:["pos1","position1"],chrom2:["chr2","chrom2","chromosome2"],pos2:["pos2","position2"]},ji={strand1:["strand1","strand_1"],strand2:["strand2","strand_2"]},Df=["pair_type","pairtype"],Rf=64<<20,zf=64,Pf=65536;function zl(e){return/^##\s*pairs\s+format/i.test(e)}async function qf(e){let t=null;try{await Vr(Kr(e,{to:Uf(e)}),n=>{t??=new TextDecoder().decode(n)},()=>t!==null)}catch{}return t!==null&&zl(t)}function Uf(e){const t=e.readBytes(0,Math.min(e.size,zf));if(Dl(t)){const n=Qr(t);if(n!==null)return Math.min(e.size,n)}return Math.min(e.size,Pf)}async function js(e){const t=[],n=new Set,s=[];let o=null,r=null,i=null,a=null,c=null,l=0,u=!1;const h=new TextDecoder,f=Kr(e,{to:Math.min(e.size,Rf)});if(await Vr(f,d=>{if(u)return;if(l+=d.length+1,d.length===0||d[0]!==35){u=!0;return}const m=h.decode(d);if(s.push(m),o===null&&zl(m)){o=m.replace(/^##\s*/,"").trim();return}const g=m.indexOf(":");if(g<0)return;const p=m.slice(1,g).trim().toLowerCase(),w=m.slice(g+1).trim();if(p==="chromsize"||p==="chromosome"){const b=w.split(/\s+/);b.length>=2&&ta(t,n,b[0],Number(b[1]))}else if(p==="samheader"){const b=/^@SQ\s.*\bSN:(\S+).*\bLN:(\d+)/.exec(w);b!==null&&ta(t,n,b[1],Number(b[2]))}else p==="genome_assembly"?r=w:p==="sorted"?i=w:p==="shape"?a=w:p==="columns"&&(c=w.split(/\s+/).filter(b=>b!==""))},()=>u).catch(()=>!1),o===null)throw new T("format","This file does not begin with a `## pairs format` line, so it is not a 4DN pairs file. If it is a headerless contact list, add a header with `pairtools header generate`, or bin it with `cooler cload pairs` and open the cooler.",{bytesRead:l});if(t.length===0)throw new T("format","This .pairs file declares no `#chromsize:` lines, so there is no bin table to build. Chromosome sizes cannot be inferred from the records — a grid guessed from the largest coordinate seen would differ between two subsamples of the same library. Add them with `pairtools header generate --chroms-path <chrom.sizes>`.",{contigs:0});return{version:o,assembly:r,sorted:i,shape:a,contigs:t,columns:Gf(c??_f,c!==null),text:s.join(`
2
+ `)}}function ta(e,t,n,s){t.has(n)||!Number.isFinite(s)||s<=0||(t.add(n),e.push({name:n,length:s}))}function Gf(e,t){const n=e.map(i=>i.toLowerCase()),s=i=>{for(const a of i){const c=n.indexOf(a);if(c>=0)return c}return-1},o={chrom1:s(es.chrom1),pos1:s(es.pos1),chrom2:s(es.chrom2),pos2:s(es.pos2),strand1:s(ji.strand1),strand2:s(ji.strand2),pairType:s(Df)},r=["chrom1","pos1","chrom2","pos2"].filter(i=>o[i]<0);if(r.length>0)throw new T("unsupported",`This .pairs file's ${t?"`#columns:` line":"assumed column layout"} has no ${r.join(", ")} column. It declares ${e.join(", ")}. The importer needs two chromosome columns and two position columns.`,{missing:r,columns:[...e]});return o}function Pl(e,t){const n=new Float64Array(e.length+1);let s=0;for(let o=0;o<e.length;o++)n[o]=s,s+=Math.ceil(e[o].length/t);return n[e.length]=s,n}function ql(e,t){let n=0;for(const s of e)n+=Math.ceil(s.length/t);return n}function ea(e){throw new T("unsupported",`A map binned from a .pairs file serves ${e} through PixelStore (metacooler/store.ts), not through the handle. Reaching for the column by name bypasses the chunking and the cancellation every other read has.`,{path:e})}function Hf(e){const{header:t,pixels:n}=e,s=()=>on(n.contigs.length,["name","length"],{name:n.contigs.map(r=>r.name),length:Float64Array.from(n.contigs,r=>r.length)}),o={format:"PAIRS","format-url":"https://github.com/4dn-dcic/pairix/blob/master/pairs_format_specification.md","format-version":Qf(t.version),"bin-size":n.binSize,"bin-type":"fixed","contig-ordering":"genome-then-length","storage-mode":"symmetric-upper","genome-assembly":t.assembly,nbins:n.nBins,nchroms:n.contigs.length,nnz:n.nnz,sum:n.sum,ngenomes:null,"pairs-source":e.uri,"pairs-header":t.text,"pairs-sorted":t.sorted,"pairs-shape":t.shape,"pairs-indexed":e.indexed,"pairs-records":n.sum,...e.declaredRecords==null?{}:{"pairs-declared-records":e.declaredRecords},...n.skippedUnknownContig===0?{}:{"pairs-skipped-unknown-contig":n.skippedUnknownContig},...n.skippedBadPosition===0?{}:{"pairs-skipped-bad-position":n.skippedBadPosition},...n.clampedPosition===0?{}:{"pairs-clamped-positions":n.clampedPosition},...n.wasUnsorted?{"pairs-required-merge":!0}:{},...n.truncated?{"pairs-truncated":!0}:{},...n.contigsWithoutCisContacts.length===0?{}:{"pairs-sequences-without-cis-contacts":n.contigsWithoutCisContacts.join(", ")}};return{uri:e.uri,group:"/",rootEntries:e.rungs.map(r=>String(r)),rootEntryGroup:"resolutions",attrs:()=>({...o}),attrsAt:r=>r===""||r==="/"?{...o}:{},has(r){return["chroms","chroms/name","chroms/length","contigs","indexes","indexes/chrom_offset","pixels","pixels/count","bins"].includes(r)},keys(r){return r===""||r==="/"?["chroms","bins","pixels","indexes"]:r==="chroms"||r==="contigs"?["name","length"]:r==="pixels"?["bin1_id","bin2_id","count"]:r==="indexes"?["chrom_offset"]:r==="bins"?[]:[]},isSoftLink:()=>!1,datasetLength(r){return r==="chroms/name"||r==="chroms/length"?n.contigs.length:r==="indexes/chrom_offset"?n.contigs.length+1:r.startsWith("pixels/")?n.nnz:0},dtype(r){return r==="pixels/count"||r==="pixels/bin1_id"||r==="pixels/bin2_id"||r==="chroms/length"?"<i":r==="chroms/name"?"S":"<d"},readNumeric(r,i=0,a){if(r==="indexes/chrom_offset")return n.chromOffset.slice();if(r==="chroms/length"){const c=a??n.contigs.length;return Float64Array.from(n.contigs.slice(i,c),l=>l.length)}throw r==="bins/weight"?new T("unsupported","A map binned from a .pairs file carries raw counts and no balancing weights: nothing has been balanced. Export the .mcool and run `cooler balance` on it, or open a cooler that was written with weights.",{}):((r==="indexes/bin1_offset"||r.startsWith("pixels/"))&&ea(r),new T("unsupported",`An imported .pairs map has no ${r}.`,{path:r}))},readInt32(r){throw r.startsWith("pixels/")&&ea(r),new T("unsupported",`An imported .pairs map has no ${r}.`,{path:r})},readStrings(r,i=0,a){if(r==="chroms/name")return n.contigs.slice(i,a??n.contigs.length).map(c=>c.name);throw new T("unsupported",`An imported .pairs map has no ${r}.`,{path:r})},readTable(r){if(r==="chroms"||r==="contigs")return s();throw new T("unsupported",`An imported .pairs map has no /${r} table.`,{path:r})},at(r){throw new T("unsupported",`An imported .pairs has no /${r} group to open.`,{path:r})},close:()=>{}}}function Qf(e){const t=/v?(\d+)(?:\.(\d+))?/.exec(e);return t===null?null:Number(t[1])}const na=1<<20;class Kf{constructor(t){this.pixels=t}offsets=null;get nnz(){return this.pixels.nnz}countKind="i";scoreColumns(){return["count"]}binOffsets(){if(this.offsets!==null)return this.offsets;const{bin1:t,nnz:n,nBins:s}=this.pixels,o=new Float64Array(s+1);let r=0;for(let i=0;i<s;i++){for(;r<n&&t[r]===i;)r++;o[i+1]=r}return this.offsets=o,o}async forEachWindow(t,n,s,o,r={}){this.refuseOtherColumns(r.value);const i=this.pixelRange(s.span[0],s.span[1]);return this.emit(i[0],i[1],o,r)}async forEachBands(t,n,s={}){this.refuseOtherColumns(s.value);for(const[o,r]of this.mergedBands(t))if(!await this.emit(o,r,n,s))return!1;return!0}async scanAll(t){const n=Math.max(1,Math.trunc(t.chunkSize??na)),s=this.pixels.nnz;for(let o=0;o<s;o+=n){const r=Math.min(o+n,s);if(t.onChunk(this.chunk(o,r))===!1)return;t.onProgress?.(r,s),await vt()}}pixelRange(t,n){const s=this.binOffsets(),o=this.pixels.nBins,r=s[Math.max(0,Math.min(o,t))],i=s[Math.max(0,Math.min(o,n))];return[r,Math.max(r,i)]}mergedBands(t){const n=[];for(const[o,r]of t){if(r<=o)continue;const[i,a]=this.pixelRange(o,r);a>i&&n.push([i,a])}n.sort((o,r)=>o[0]-r[0]);const s=[];for(const o of n){const r=s[s.length-1];r!==void 0&&o[0]<=r[1]+bl?r[1]=Math.max(r[1],o[1]):s.push([o[0],o[1]])}return s}async emit(t,n,s,o){const r=Math.max(1,Math.trunc(o.chunkSize??na)),i=n-t;for(let a=t;a<n;a+=r){if(o.cancelled?.()===!0)return!1;const c=Math.min(a+r,n);s(this.chunk(a,c)),o.onProgress?.(c-t,i),await vt()}return!0}chunk(t,n){const{bin1:s,bin2:o,count:r}=this.pixels;return{bin1:s.slice(t,n),bin2:o.slice(t,n),count:Float64Array.from(r.subarray(t,n)),length:n-t}}refuseOtherColumns(t){if(!(t===void 0||t==="count"))throw new T("unsupported",`A map binned from a .pairs file stores one value per pixel — the number of contacts — so there is no '${t}' column to draw. Extra per-pixel columns come from a cooler that was written with them.`,{value:t})}}function wn(e,t,n){const s=Math.max(0,Math.min(Math.trunc(t),e)),o=Math.max(s,Math.min(Math.trunc(n??e),e));return[s,o]}function Vf(e,t){const{bins:n,pixels:s}=t,o=()=>on(n.names.length,["name","length"],{name:[...n.names],length:n.lengths.slice()}),r={format:"HiC-Pro","format-url":"https://github.com/nservant/HiC-Pro","bin-size":n.binsize,"bin-type":"fixed",ordering:"genome-then-length","storage-mode":"symmetric-upper",nbins:n.nBins,ncontigs:n.names.length,nnz:s.nnz,sum:s.sum,ngenomes:null,"hicpro-matrix":t.matrixName,"hicpro-bins":t.binsName,"hicpro-stored":s.observed};return{uri:e,group:"/",rootEntries:[],rootEntryGroup:"",attrs:()=>({...r}),attrsAt:i=>i===""||i==="/"?{...r}:{},has(i){return["chroms","chroms/name","chroms/length","bins","pixels","pixels/bin1_id","pixels/bin2_id","pixels/count","indexes","indexes/bin1_offset","indexes/chrom_offset"].includes(i)},keys(i){return i===""||i==="/"?["chroms","bins","pixels","indexes"]:i==="chroms"?["name","length"]:i==="pixels"?["bin1_id","bin2_id","count"]:i==="indexes"?["bin1_offset","chrom_offset"]:i==="bins"?[]:[]},isSoftLink:()=>!1,datasetLength(i){return i==="chroms/name"||i==="chroms/length"?n.names.length:i.startsWith("pixels/")?s.nnz:i==="indexes/bin1_offset"?n.nBins+1:i==="indexes/chrom_offset"?n.names.length+1:0},dtype(i){return i==="pixels/count"?"<f":i==="pixels/bin1_id"||i==="pixels/bin2_id"||i==="chroms/length"?"<i":i==="chroms/name"?"S":"<d"},readNumeric(i,a=0,c){if(i==="indexes/chrom_offset")return n.chromOffset.slice();if(i==="indexes/bin1_offset"){const[l,u]=wn(n.nBins+1,a,c);return s.binOffset.slice(l,u)}if(i==="chroms/length"){const[l,u]=wn(n.names.length,a,c);return n.lengths.slice(l,u)}if(i==="pixels/count"){const[l,u]=wn(s.nnz,a,c);return Float64Array.from(s.count.subarray(l,u))}throw i==="bins/weight"?new T("unsupported","A HiC-Pro matrix carries no balancing weights. Its `iced/` matrix is already normalised — open that one instead of balancing this one — and its `.biases` file belongs to the raw matrix, so applying it here would normalise twice.",{path:i}):new T("unsupported",`A HiC-Pro matrix has no ${i}.`,{path:i})},readInt32(i,a=0,c){const[l,u]=wn(s.nnz,a,c);if(i==="pixels/bin1_id")return s.bin1.subarray(l,u);if(i==="pixels/bin2_id")return s.bin2.subarray(l,u);throw new T("unsupported",`A HiC-Pro matrix has no ${i}.`,{path:i})},readStrings(i,a=0,c){if(i==="chroms/name"){const[l,u]=wn(n.names.length,a,c);return n.names.slice(l,u)}throw new T("unsupported",`A HiC-Pro matrix has no ${i}.`,{path:i})},readTable(i){if(i==="chroms"||i==="contigs")return o();throw new T("unsupported",`A HiC-Pro matrix has no /${i} table.`,{path:i})},at(i){throw new T("unsupported",`A HiC-Pro matrix has no /${i} group.`,{path:i})},close:()=>{}}}const Yf={K:1e3,KB:1e3,M:1e6,MB:1e6,G:1e9,GB:1e9};function ee(e,t={}){throw new T("bad-range",e,t)}function sa(e){const t=e.replace(/,/g,"").split(/([0-9.]+)/);(t.length!==3||t[0]!=="")&&ee(`Unexpected token "${e}"`,{token:e});const[,n,s]=t;if(s.length===0)return/^[0-9]+$/.test(n)||ee(`Unexpected token "${e}"`,{token:e}),Number(n);const o=Number.parseFloat(n);Number.isFinite(o)||ee(`Unexpected token "${e}"`,{token:e});const r=s.toUpperCase().trim(),i=Yf[r];return i===void 0&&ee(`Unknown unit '${r}'`,{unit:r}),Math.trunc(o*i)}function Wf(e){const t=/\s*(-)|\s*([0-9,]+(?:\.[0-9]*)?(?:[a-z]+)?)|\s*(.+)/gi,n=[];for(const s of e.matchAll(t)){const o=s[1],r=s[2],i=s[3];o!==void 0?n.push({kind:"HYPHEN",text:o}):r!==void 0?n.push({kind:"COORD",text:r}):n.push({kind:"OTHER",text:i??""})}return n}function Yr(e){const t=[];let n="";for(let s=0;s<e.length;s++){const o=e[s];if(o===","&&!Xf(e,s)){t.push(n),n="";continue}n+=o}return t.push(n),t.map(s=>s.trim()).filter(s=>s!=="")}function Xf(e,t){return/[0-9]$/.test(e.slice(0,t))&&/^[0-9]{3}(?![0-9])/.test(e.slice(t+1))}function to(e,t={}){const n=e.split(":"),s=n[0].trim();if(s.length===0&&ee("Chromosome name cannot be empty",{region:e}),n.length<2)return[s,null,null];const o=Wf(n[1]);let r=0;const i=()=>o[r++],a=(h,f)=>(h===void 0&&ee(`Expected ${f.join(" or ")} token missing`,{region:e}),f.includes(h.kind)||ee(`Unexpected token "${h.text}"`,{region:e}),h),c=sa(a(i(),["COORD"]).text);a(i(),["HYPHEN"]);const l=i();if(l===void 0)return[s,c,null];const u=sa(a(l,["COORD"]).text);return u<c&&t.reversed!==!0&&ee("End coordinate less than start — unless the sequence joins its own ends, which is a tick in the circular column of the Chromosomes table",{region:e,start:c,end:u}),[s,c,u]}function Jf(e,t){const[n,s,o]=to(e),r=t.get(n);if(r===void 0)throw new T("unknown-label",`Unknown sequence label: ${n}`,{label:n});const i=s===null?0:s,a=o===null?r:o;return a<i&&ee("End cannot be less than start",{region:e,start:i,end:a}),(i<0||a>r)&&ee(`Genomic region out of bounds: [${i}, ${a})`,{region:e,start:i,end:a,length:r}),[n,i,a]}function Zf(e,t,n,s){const o=t.split(":")[0],r=e.genomes===null?[]:e.genomes.strings("name"),i=e.contigs.strings("name");if(r.includes(o)){if(o===vn)throw new T("unbinned-not-displayable",`Range ${st(t)}: ${st(vn)} holds every contig that binning failed to assign. It is a bookkeeping row, not a genome — there is no coherent contact map to draw for it — so it cannot be displayed. Its share of the file is reported as a QC statistic in the file info panel.`,{label:o});if(t.includes(":"))throw new T("genome-coordinate-suffix",`Range ${st(t)}: ${st(o)} is a genome name — coordinate suffixes are not allowed for genome ranges. Use just ${st(o)}.`,{label:o,region:t});return{kind:"genome",label:o,extent:e.genomeExtent(o)}}if(i.includes(o))return{kind:"contig",label:o,extent:oa(e,o,t)};const a=(e.names??jf(e)).find(o,{from:n});if(a!==null){const c=t.slice(o.length);return{kind:"contig",label:a.name,extent:oa(e,a.name,`${a.name}${c}`),matched:a}}throw e.genomes===null?new T("unknown-label",`Range ${st(t)}: ${st(o)} is not a sequence of this file.
3
+ Sequences: ${We(i)}`,{label:o,region:t}):new T("unknown-label",`Range ${st(t)}: ${st(o)} is not a known genome or contig.
4
+ Genomes: ${We(e.displayGenomes().map(c=>r[c]))}
5
+ Contigs: ${We(i)}`,{label:o,region:t})}function oa(e,t,n,s){return e.extent(n)}function jf(e){const t=e.contigs.strings("name"),n=e.contigs.numbers("length");return we.of(t.map((s,o)=>({name:s,length:n[o]})))}function t5(e,t,n,s,o){const r=e[t],i=e[t+1]??r,a=Math.min(r+Math.floor(n/o),i),c=Math.min(r+Math.ceil(s/o),i);return[a,Math.max(a,c)]}const e5=["name","length","n_contigs","n_bins"],n5=["name","length"],Fo=["name","length","genome_id"],s5=["name","length","n_contigs","n_bins"],o5=["bin-size","bin-type","storage-mode"],$o=["nnz","sum"];class bt{uri;layout;entries;entryGroup;tables;info;metadata;genomes;contigs;binsize;unlensed;storageMode;countKind;coords;chromnames;get chromsizes(){return this.chromsizeCache??=new Map(this.chromnames.map((t,n)=>[t,this.contigLengths[n]])),this.chromsizeCache}get contigIds(){return this.contigIdCache??=new Map(this.chromnames.map((t,n)=>[t,n])),this.contigIdCache}get names(){return this.nameIndexCache??=we.of(this.chromnames.map((t,n)=>({name:t,length:this.contigLengths[n]}))),this.nameIndexCache}genomeNames;contigGenomeIds;rawHandle;get handle(){const t=this.resampled?.weightScale??1;return t===1?this.rawHandle:(this.weighted?.scale!==t&&(this.weighted={scale:t,handle:r5(this.rawHandle,t)}),this.weighted.handle)}weighted=null;pixelStore;get store(){return this.resampled??this.pixelStore}get storedPixels(){return this.pixelStore}resampled=null;census=null;censusWanted=!1;chromOffset;genomeOffset;genomeIds;closed=!1;fullContigTable=null;fullGenomeTable=null;contigIdCache=null;chromsizeCache=null;nameIndexCache=null;contigLengths;static async open(t,n,s={}){if(await n.mount(),Ll(n.readBytes(0,4)))return bt.fromHic(t,n);if(await qf(n)){const i=await js(n);throw new T("pairs-binning-needed","This is a .pairs file: a list of contacts with no resolution of its own. Choose a bin size to build a map at.",{contigs:i.contigs.length,assembly:i.assembly,sorted:i.sorted})}const o=s.metacool===!1?".cool, .mcool or .scool":".cool, .mcool, .scool or .metacool";if(_l(n.readBytes(0,2)))throw new T("not-hdf5",`This file is gzip-compressed. HDF5 is read in place and cannot be decompressed on the fly, so a ${o} must be decompressed first. A .pairs.gz is read compressed — but this one carries no \`## pairs format\` line, so it is not one.`,{});if(!u5(n))throw new T("not-hdf5",`${Pt(t).path} does not start with the HDF5 signature, so it is not a ${o} whatever its name says. A download that stopped short, an HTML page saved under the file’s name, or a text matrix look like this.`,{});const r=await i2(t,n,Pt(t).group);try{return s.listingAgreed!==!0&&c5(r,n),bt.fromHandle(r,s)}catch(i){if(r.close(),!(i instanceof T)&&i instanceof Error){const a=Pt(t).group;throw new T("format",`${Pt(t).path}${a===""?"":`::${a}`} could not be read as a cooler container. HDF5 said: ${i.message}. The file opens as HDF5, so this is a group, dataset or attribute the reader expected and did not find.`,{hdf5:i.message})}throw i}}static fromHic(t,n){const{group:s}=Pt(t),o=/^\/resolutions\/(\d+)$/.exec(s),r=Ur.open(n,o===null?{}:{binSize:Number(o[1])});if(!r.hasAnyMatrix())throw new T("unsupported",`This .hic carries no base-pair matrix at ${r.binSize} bp. Its resolutions are ${r.resolutions.join(", ")}.`,{binSize:r.binSize,resolutions:r.resolutions});const i=Cf(t,r),a=i.attrs();return new bt({uri:t,handle:i,layout:{kind:"hic",group:s,entries:r.resolutions.map(c=>String(c))},attrs:a,contigs:i.readTable("chroms"),chromOffset:i.readNumeric("indexes/chrom_offset"),store:new $f(r)})}static fromPairs(t){const n=Hf({uri:t.uri,header:t.header,pixels:t.pixels,rungs:t.rungs,indexed:t.indexed,declaredRecords:t.declaredRecords??null});return new bt({uri:t.uri,handle:n,layout:{kind:"pairs",group:`/resolutions/${t.pixels.binSize}`,entries:t.rungs.map(s=>String(s))},attrs:n.attrs(),contigs:n.readTable("chroms"),chromOffset:n.readNumeric("indexes/chrom_offset"),store:new Kf(t.pixels)})}static fromScool(t,n,s,o){const r=zi(n.entries??[],o),i=t.at(Cs(n,(n.entries??[])[0])),a=cl(t,n,s).attrs,c={...s};for(const f of o5)c[f]===void 0&&a[f]!==void 0&&(c[f]=a[f]);const l=i.readNumeric("indexes/chrom_offset"),u=Si(i.dtype("pixels/count")),h=Number(l[l.length-1]??0);return new bt({uri:t.uri,handle:t,tables:i,layout:n,attrs:c,contigs:i.readTable("chroms",void 0,void 0,Fo),chromOffset:l,store:new Jn(t,n,r,h,u)})}static fromHicPro(t,n){if(!n.bins.fixed||n.bins.binsize===null)throw new T("variable-bins",`The viewer supports fixed-bin files only, and ${n.binsName} is not one: its intervals are of differing widths, which is what a fragment-level map (\`GENOME_FRAGMENT\` in HiC-Pro's config) looks like. Re-run HiC-Pro with a \`BIN_SIZE\`, or open the fixed-bin matrix beside this one.`,{"bin-type":"variable","bin-size":null,bins:n.binsName});const s=Vf(t,n);return new bt({uri:t,handle:s,layout:{kind:"hicpro",group:"/"},attrs:s.attrs(),contigs:s.readTable("chroms"),chromOffset:n.bins.chromOffset})}static fromHandle(t,n={}){const s=t.attrs(),o=n.metacool===void 0?{}:{metacool:n.metacool},r=Wu(t,s,o);if(e2(t,r,s,o),r.kind==="scool")return bt.fromScool(t,r,s,null);if(r.kind!=="metacool")return new bt({uri:t.uri,handle:t,layout:r,attrs:s,contigs:t.readTable("chroms",void 0,void 0,Fo),chromOffset:t.readNumeric("indexes/chrom_offset")});if(!t.has("indexes/genome_offset"))throw new ct("indexes/genome_offset is missing");return new bt({uri:t.uri,handle:t,layout:r,attrs:s,genomes:t.readTable("genomes",void 0,void 0,s5),contigs:t.readTable("contigs",void 0,void 0,Fo),chromOffset:t.readNumeric("indexes/chrom_offset"),genomeOffset:t.readNumeric("indexes/genome_offset")})}fullContigs(){return this.fullContigTable??=this.tables.readTable(this.layout.kind==="metacool"?"contigs":"chroms"),this.fullContigTable}fullGenomes(){return this.genomes===null?null:(this.fullGenomeTable??=this.handle.readTable("genomes"),this.fullGenomeTable)}constructor(t){this.uri=t.uri,this.unlensed=t.unlensed??null,this.rawHandle=t.handle,this.tables=t.tables??t.handle,this.layout=t.layout,this.entries=t.handle.rootEntries,this.entryGroup=t.handle.rootEntryGroup,this.genomes=t.genomes??null,this.contigs=t.contigs,this.chromOffset=t.chromOffset,this.genomeOffset=t.genomeOffset??new Float64Array(0),this.info=s2(t.attrs),this.metadata=o2(t.attrs),this.storageMode=this.info["storage-mode"];const n=this.info["bin-size"];if(n===null||n<=0)throw new ct(`attrs['bin-size'] must be positive, got ${st(n)}`,{"bin-size":n});this.binsize=n,this.chromnames=this.contigs.strings("name"),this.genomeNames=this.genomes===null?[]:this.genomes.strings("name");const s=i5(this.contigs,"length");this.contigGenomeIds=this.genomes===null?new Int32Array(0):a5(this.contigs,"genome_id"),this.coords=new w2(this.binsize,this.chromOffset,s),this.contigLengths=s,this.genomeIds=new Map;for(let o=0;o<this.genomeNames.length;o++)this.genomeIds.set(this.genomeNames[o],o);this.countKind=Si(this.tables.dtype("pixels/count")),this.pixelStore=t.store??new Al(this.rawHandle,this.nBins,this.countKind),this.validateLight()}get hasGenomes(){return this.genomes!==null}get hasWeights(){return this.handle.has(`bins/${ae}`)}get scoreColumns(){return this.store.scoreColumns()}get nGenomes(){return this.genomes?.length??0}get nContigs(){return this.contigs.length}get nBins(){return this.coords.nBins}get shape(){return[this.nBins,this.nBins]}hasGenome(t){return this.genomeIds.has(t)}hasContig(t){return this.contigIds.has(t)}genomeId(t){const n=this.genomeIds.get(t);if(n===void 0)throw new T("unknown-label",`Genome ${st(t)} not found. Available: ${We(this.displayGenomes().map(s=>this.genomeNames[s]))}`,{label:t});return n}contigId(t){const n=this.contigIds.get(t);if(n===void 0)throw new T("unknown-label",`Contig ${st(t)} not found. Available: ${We(this.chromnames)}`,{label:t});return n}extent(t){const[n,s,o]=Jf(t,this.chromsizes);return t5(this.chromOffset,this.contigId(n),s,o,this.binsize)}offset(t){return this.extent(t)[0]}genomeContigExtent(t){const n=typeof t=="number"?t:this.genomeId(t);return[this.genomeOffset[n],this.genomeOffset[n+1]]}genomeExtent(t){const n=typeof t=="number"?t:this.genomeId(t),[s,o]=this.genomeContigExtent(n);if(o<=s)return[0,0];this.assertContiguous(n,s,o);const r=this.chromOffset[s],i=this.chromOffset[o];return i<=r?[0,0]:[r,i]}assertContiguous(t,n,s){const o=this.contigGenomeIds;if(o[n]!==t||o[s-1]!==t||n>0&&o[n-1]===t||s<o.length&&o[s]===t)throw new T("bad-ordering",`Genome ${st(this.genomeNames[t])} is not contiguous in bin space under contig-ordering ${st(this.info.ordering)}; a single bin range cannot represent it, so the file's ordering attribute does not describe its actual contig layout. Re-create the file with a genome-grouped ordering.`,{genome:this.genomeNames[t],genomeId:t,contigs:[n,s]})}contigExtent(t){const n=typeof t=="number"?t:this.contigId(t),[s,o]=this.coords.contigBinExtent(n);return o<=s?[0,0]:[s,o]}displayGenomes(){const t=[];for(let n=0;n<this.genomeNames.length;n++)this.genomeNames[n]!==vn&&t.push(n);return t}displayBinExtent(){const t=[0,this.nBins],n=this.genomeIds.get(vn);if(n===void 0)return t;const[s,o]=this.genomeExtent(n);return o<=s?t:s===0?[o,this.nBins]:o===this.nBins?[0,s]:t}unassignedStats(){const t=this.nContigs,n=this.nBins,s=this.genomeIds.get(vn);if(s===void 0||this.genomes===null)return null;const o=Number(this.genomes.get("n_contigs",s)??0),r=Number(this.genomes.get("n_bins",s)??0);return{genomeIndex:s,contigs:o,bins:r,totalContigs:t,totalBins:n,contigFraction:t>0?o/t:0,binFraction:n>0?r/n:0}}genomeDensity(t){const[n,s]=this.genomeExtent(t);if(s<=n)return 0;const o=this.binOffsets();return o[s]-o[n]}binOffsets(){return this.store.binOffsets()}genomeDensities(){const t=new Float64Array(this.nGenomes);if(this.genomes===null)return t;const n=this.binOffsets();for(const s of this.displayGenomes()){const[o,r]=this.genomeExtent(s);r>o&&(t[s]=n[r]-n[o])}return t}async matrix(t,n=Mo.range){return _2(this,t,n)}async genomeMatrix(t){const n=this.genomeExtent(t);return this.matrix({rows:n},Mo.binSelection)}async genomePairMatrix(t,n){return this.matrix({rows:this.genomeExtent(t),cols:this.genomeExtent(n)},Mo.binSelection)}async aggregateSparse(t,n,s={}){const o=ns(t.rows,this.nBins),r=ns(t.cols??t.rows,this.nBins),i=Je(o,r,this.storageMode),a=m2(o,r,this.coords,this.contigGenomeIds,n,i.mode,this.storageMode==="symmetric-upper",He),c=t.balance===!0?this.weightSlices(o,r):null;return await this.store.forEachWindow(o,r,i,l=>{for(let u=0;u<l.length;u++){const h=l.bin1[u],f=l.bin2[u],d=c===null?l.count[u]:l.count[u]*ss(c,o,r,h)*ss(c,o,r,f);a.place(h,f,d)}},ia(s,t.value)),{...a.finish(),contigsPerCell:a.perCell}}async pooledMatrix(t,n,s={}){const o=ns(t.rows,this.nBins),r=ns(t.cols??t.rows,this.nBins),i=Je(o,r,this.storageMode),a=o[0]===r[0]&&o[1]===r[1],c=this.contigGenomeIds,l=xi(this.coords,o[0],o[1],n,He,c),u=a?l:xi(this.coords,r[0],r[1],n,He,c),h=cr(o,r,l,u,i.mode,this.storageMode==="symmetric-upper",!0),f=t.balance===!0?this.weightSlices(o,r):null;return await this.store.forEachWindow(o,r,i,d=>{for(let m=0;m<d.length;m++){const g=d.bin1[m],p=d.bin2[m],w=f===null?d.count[m]:d.count[m]*ss(f,o,r,g)*ss(f,o,r,p);h.place(g,p,w)}},ia(s,t.value)),{...h.finish(),binsPerCell:l.stride,contigsPerCell:Math.max(l.contigsPerCell,u.contigsPerCell),rowMembers:l,colMembers:u}}async selectionMatrix(t,n,s={}){const o=s.byGenome===!0?this.contigGenomeIds:void 0,r=f2(this.nBins,t,n,s.cellCap===void 0?He:s.cellCap,o),i=[0,this.nBins],a=cr(i,i,r,r,Je(i,i,this.storageMode).mode,this.storageMode==="symmetric-upper"),c=s.balance===!0?this.weightSlices(i,i):null;return await this.store.forEachBands(t.map(({extent:u})=>[u[0],u[1]]),u=>{for(let h=0;h<u.length;h++){const f=u.bin1[h],d=u.bin2[h],m=c===null?u.count[h]:u.count[h]*c.rows[f]*c.cols[d];a.place(f,d,m)}},s),{...a.finish(),binsPerCell:r.stride,contigsPerCell:r.contigsPerCell,ids:r.ids,pooling:r}}static withLens(t,n){const s=n.original.genome!==void 0;if(t.hasGenomes&&!s)return{map:t,refusal:"An edit is not yet drawn on a map of several genomes."};if(t.layout.kind==="scool")return{map:t,refusal:"An edit is not yet drawn on a single-cell file."};const o=lh(n,t.chromnames,t.contigLengths,{always:s});if(o===null)return{map:t,refusal:null};if("refusal"in o)return{map:t,refusal:o.refusal};const r=Dr.of(t.coords,t.chromnames,o.assembly),i=ch(r),a={...t.rawHandle.attrs(),nchroms:r.names.length,nbins:r.nBins};return{map:new bt({uri:t.uri,handle:ah(t.rawHandle,r,i,a),layout:t.layout,attrs:a,contigs:i,chromOffset:r.chromOffset,store:new ih(t.store,r,t.storageMode==="symmetric-upper"),unlensed:t}),refusal:null}}static coarsened(t,n){if(!Number.isInteger(n)||n<1)return{map:t,refusal:`A resolution is a whole number of this map's bins, not ${n} of them.`};if(n===1)return{map:t,refusal:null};if(t.layout.kind==="scool")return{map:t,refusal:"A single-cell file is not yet measured at a resolution you added; measure it at one it stores."};const s=Rr.of(t.chromOffset,n),{nnz:o,...r}=t.handle.attrs(),i={...r,"bin-size":t.binsize*n,nbins:s.nBins};return{map:new bt({uri:t.uri,handle:mh(t.handle,s,i),layout:t.layout,attrs:i,contigs:t.contigs,chromOffset:s.chromOffset,...t.genomes===null?{}:{genomes:t.genomes,genomeOffset:t.genomeOffset},store:new fh(t.store,s,t.storageMode),...t.unlensed===null?{}:{unlensed:t.unlensed}}),refusal:null}}async picture(t,n={}){const s=[0,this.nBins],o=n.balance===!0?this.weightSlices(s,s).rows:null,r=await K2({store:this.store,coords:this.coords,layout:t,weights:o,...n.value===void 0?{}:{value:n.value},...n.gather===void 0?{}:{gather:n.gather},...n.cancelled===void 0?{}:{cancelled:n.cancelled},...n.onProgress===void 0?{}:{onProgress:n.onProgress}});return r===null?null:{levels:W2({n:t.n,cells:r.cells},r.quantizer),quantizer:r.quantizer,histogram:r.histogram,pixels:r.pixels,...r.contacts===void 0?{}:{contacts:r.contacts}}}binColumns(){if(!this.tables.has("bins"))return[];const t=[];for(const n of this.tables.keys("bins")){const s=ra(this.tables.dtype(`bins/${n}`));s!==null&&t.push({name:n,text:s})}return t}binColumn(t,n,s){const o=`bins/${t}`;if(!this.tables.has(o))throw new T("unsupported",`This file has no /${o} column.`,{column:t});const r=Math.max(0,Math.min(n,this.nBins)),i=Math.max(r,Math.min(s,this.nBins));return t===ae&&this.tables===this.rawHandle?this.handle.readNumeric(o,r,i):ra(this.tables.dtype(o))===!0?this.tables.readStrings(o,r,i):this.tables.readNumeric(o,r,i)}weightSlices(t,n){const s=`bins/${ae}`;if(!this.handle.has(s))throw new T("unsupported",`This file has no /${s} column, so there are no weights to balance with. Run \`cooler balance\` on it, or view the raw counts.`,{column:ae});const o=this.handle.readNumeric(s,t[0],t[1]),r=n[0]===t[0]&&n[1]===t[1]?o:this.handle.readNumeric(s,n[0],n[1]);return{rows:o,cols:r}}aggregate(t,n){return g2(t,this.coords,this.contigGenomeIds,n,this.storageMode==="symmetric-upper")}get cells(){return this.layout.kind==="scool"?this.layout.entries??[]:[]}get activeCells(){return this.pixelStore instanceof Jn?this.pixelStore.names:[]}selectCells(t){if(this.layout.kind!=="scool")throw new T("unsupported",`${this.uri} is not a single-cell container, so it has no cells to choose.`,{layout:this.layout.kind});const n=zi(this.cells,t);this.pixelStore=new Jn(this.handle,this.layout,n,this.nBins,this.countKind)}async scanCells(t){if(!(this.pixelStore instanceof Jn))throw new T("unsupported",`${this.uri} is not a single-cell container, so it has no cells to walk.`,{layout:this.layout.kind});await this.pixelStore.scanPerCell(t)}cellTable(){const t=this.cells,n=t.map(a=>this.handle.attrsAt(Cs(this.layout,a))),s=["name"],o={name:t},r=new Set;for(const a of n)for(const c of Object.keys(a))r.add(c);const i=[...r].filter(a=>!$o.includes(a)&&a!=="metadata").sort();for(const a of[...$o,...i]){if(!r.has(a))continue;const c=n.map(u=>u[a]);if(!$o.includes(a)&&c.every(u=>u===c[0]))continue;c.every(u=>typeof u=="number"||typeof u=="boolean")?o[a]=Float64Array.from(c,u=>Number(u)):o[a]=c.map(u=>u==null?"":String(u)),s.push(a)}return on(t.length,s,o)}get nnz(){return this.store.nnz??0}async scanPixels(t){await this.store.scanAll(t)}async validateDeep(t){const n=[],s=new Set,o=(b,y)=>{s.has(b)||(s.add(b),n.push(y))},r=this.nBins,i=this.storageMode==="square",a=this.binOffsets(),c=this.nnz;a.length!==r+1?n.push(`bin1_offset length (${a.length}) != ${r+1}`):(a[0]!==0||a[r]!==c)&&n.push(`bin1_offset endpoints (${a[0]}, ${a[r]}) do not bracket the table (0, ${c})`);for(let b=1;b<a.length;b++)if(a[b]<a[b-1]){o("monotone",`bin1_offset is not non-decreasing: bin1_offset[${b-1}]=${a[b-1]} > bin1_offset[${b}]=${a[b]}`);break}let l=0;const u=this.countKind!=="f";let h=-1,f=-1;const d=a.length===r+1;let m=0,g=0;const p=t?.cancelled;if(await this.pixelStore.scanAll({onProgress:t?.onProgress,storedRows:!0,onChunk:b=>{if(p?.()===!0)return!1;for(let y=0;y<b.length;y++,g++){const A=b.bin1[y],N=b.bin2[y],M=b.count[y];if(u&&(l+=M),(A<0||N<0||A>=r||N>=r)&&o("range",`pixel bin ids out of range [0, ${r}) at row ${g}`),!i&&A>N&&o("triangle",`pixels are not upper-triangular (bin1_id <= bin2_id): row ${g} is (${A}, ${N})`),h>=0&&!(h<A||h===A&&f<=N)&&o("order",`pixels are not sorted by (bin1_id, bin2_id) at row ${g}`),d){for(;m<r&&a[m+1]<=g;)m++;m!==A&&o("index",`bin1_offset disagrees with pixels/bin1_id at row ${g}: the index places it in bin ${m}, the pixel says ${A}`)}h=A,f=N}return!0}}),p?.()===!0){const b=new Error("cancelled");throw b.name="AbortError",b}const w=this.info.sum;return u&&w!==null&&w!==l&&n.push(`attrs['sum'] (${w}) != the sum of pixels/count (${l})`),{ok:n.length===0,problems:n,note:`Scanned ${c} pixels for range, upper-triangle, sort order and bin1_offset agreement${u?", and reconciled attrs['sum']":" (float counts: sum not reconciled)"}. The /bins geometry checks of the Python validate() are not run — this reader never reads /bins.`}}validateLight(){const t=(s,o={})=>{throw new ct(s,o)};this.nContigs===0&&t("/contigs table is empty");for(const s of n5)this.contigs.columns.includes(s)||t(`Missing column in /contigs: ${s}`);if(this.chromOffset.length!==this.nContigs+1&&t(`chrom_offset length (${this.chromOffset.length}) != ${this.nContigs+1}`),this.chromOffset[0]!==0&&t(`chrom_offset endpoints (${this.chromOffset[0]}, ${this.nBins}) do not bracket the table`),this.genomes!==null){this.nGenomes===0&&t("/genomes table is empty");for(const s of e5)this.genomes.columns.includes(s)||t(`Missing column in /genomes: ${s}`);this.contigs.columns.includes("genome_id")||t("Missing column in /contigs: genome_id"),this.handle.isSoftLink("chroms")||t("/chroms must be a soft link to /contigs for cooler compatibility"),this.genomeOffset.length!==this.nGenomes+1&&t(`genome_offset length (${this.genomeOffset.length}) != ${this.nGenomes+1}`),(this.genomeOffset[0]!==0||this.genomeOffset[this.nGenomes]!==this.nContigs)&&t(`genome_offset endpoints (${this.genomeOffset[0]}, ${this.genomeOffset[this.nGenomes]}) do not bracket the table (0, ${this.nContigs})`)}const n=[["nchroms",this.info.ncontigs,this.nContigs,"len(/contigs)"],...this.genomes===null?[]:[["ngenomes",this.info.ngenomes,this.nGenomes,"len(/genomes)"]],["nbins",this.info.nbins,this.nBins,"len(/bins)"],["nnz",this.info.nnz,this.tables.datasetLength("pixels/bin1_id"),"len(/pixels)"]];for(const[s,o,r,i]of n)o!==null&&o!==r&&t(`attrs[${st(s)}] (${o}) != ${i} (${r})`)}async resample(t,n={}){if(t===null)return this.resampled=null,null;const s=this.resampleRefusal();this.censusWanted=!0,n.census!==void 0&&(this.census=Promise.resolve(n.census));let o;s!==null?o=Promise.reject(s):(this.census??=Di(this.pixelStore,this.chromOffset,{...n.onProgress===void 0?{}:{onProgress:n.onProgress},cancelled:()=>this.closed||n.cancelled?.()===!0}),o=this.census,o.catch(i=>{i instanceof DOMException&&i.name==="AbortError"&&this.census===o&&(this.census=null)}));const r=new tf(this.pixelStore,{starts:this.chromOffset,names:this.chromnames},t,o);return this.resampled=r,r.ready()}get declaredContacts(){const t=this.info.sum;return typeof t=="number"&&Number.isFinite(t)&&t>=0?t:null}async countContacts(t={}){const n=this.declaredContacts;if(n!==null)return n;if(this.resampleRefusal()!==null)return null;const s=t.pause;this.census??=Di(this.pixelStore,this.chromOffset,{...t.onProgress===void 0?{}:{onProgress:t.onProgress},...s===void 0?{}:{pause:()=>s(()=>this.censusWanted)},cancelled:()=>this.closed});try{return(await this.census).total}catch{return null}}async takenCensus(){if(this.census===null)return null;try{return await this.census}catch{return null}}resampleRefusal(){return this.layout.kind==="scool"?new T("unsupported","A single-cell container’s map is a sum of its cells rather than one library, so it is not resampled as one. Open one cell, or a pseudo-bulk written as a cooler, to resample it.",{resample:!0,layout:"scool"}):this.storageMode!=="symmetric-upper"?new T("unsupported","This map stores both triangles, so each contact would be drawn twice. Resampling reads upper-triangular files — cooler’s default.",{resample:!0,storageMode:this.storageMode}):null}close(){this.closed||(this.closed=!0,this.rawHandle.close())}}function r5(e,t){const n=`bins/${ae}`;return new Proxy(e,{get(s,o){if(o==="readNumeric")return(i,a,c)=>{const l=s.readNumeric(i,a,c);if(i!==n)return l;const u=new Float64Array(l.length);for(let h=0;h<l.length;h++)u[h]=l[h]*t;return u};const r=Reflect.get(s,o,s);return typeof r=="function"?r.bind(s):r}})}function i5(e,t){const n=e.data[t];return n instanceof Float64Array?n:Float64Array.from(e.numbers(t))}function ra(e){try{return"SOU".includes(tl(e))}catch{return null}}function a5(e,t){const n=e.data[t];if(ArrayBuffer.isView(n)&&!(n instanceof DataView)&&!(n instanceof BigInt64Array)){const s=new Int32Array(n.length);return s.set(n),s}return Int32Array.from(e.numbers(t))}function ia(e,t){return t===void 0?e:{...e,value:t}}function ns(e,t){const n=Math.min(Math.max(Math.trunc(e[0]),0),t),s=Math.min(Math.max(Math.trunc(e[1]),n),t);return[n,s]}function ss(e,t,n,s){return s>=t[0]&&s<t[1]?e.rows[s-t[0]]:s>=n[0]&&s<n[1]?e.cols[s-n[0]]:1}function c5(e,t){if(!t.isRemote||!Vu(e,e.attrs()))return;const{path:n}=Pt(e.uri);throw new T("scool-listing-cost",`${n} is a single-cell container, and listing its cells is not a bounded read: the names live in HDF5 link records spread across the whole object. Reading them over the network costs either one download of ${It.bytes(t.size)} or a few percent of that in about a thousand separate requests. Choose which to pay, or open a local copy, where neither cost applies.`,{path:n,size:t.size})}const l5=[137,72,68,70,13,10,26,10];function u5(e){for(const t of[0,512,1024,2048,4096,8192]){if(t+8>e.size)return!1;const n=e.readBytes(t,8);if(n.length===8&&l5.every((s,o)=>n[o]===s))return!0}return!1}const h5="0.20.0",f5="40ec1ec";function ws(e){if(e==null)return null;switch(typeof e){case"string":case"number":case"boolean":return e;case"bigint":return Number(e);case"object":{if(Array.isArray(e))return e.map(ws);if(ArrayBuffer.isView(e))return Array.from(e);if(e instanceof Map){const n={};for(const[s,o]of e)n[String(s)]=ws(o);return n}const t={};for(const[n,s]of Object.entries(e))t[n]=ws(s);return t}default:return String(e)}}function d5(e){return e instanceof T?{name:e.name,code:e.code,message:e.message,detail:ws(e.detail),stack:e.stack}:e instanceof Error?{name:e.name,code:null,message:e.message,detail:{},stack:e.stack}:{name:"Error",code:null,message:String(e),detail:{}}}const ze=1<<20,m5=64<<20;let aa=0;class ue{id;size;isRemote=!1;bytes;blob;existingPath;tempDir;mountPath=null;ownsMount=!1;requests=0;bytesRead=0;cache=new Map;cachedBytes=0;syncReader=null;module=null;readExact;constructor(t,n={}){if(this.tempDir=n.tempDir,this.bytes=null,this.blob=null,this.existingPath=null,g5(t)){const o=typeof File<"u"&&t instanceof File?t.name:null;this.blob=t,this.size=t.size,this.id=n.name??o??`blob-${Eo()}`,this.readExact=(r,i)=>this.readBlobExact(t,r,i);return}if(p5(t)){this.existingPath=t.path,this.size=t.size,this.id=n.name??t.path;return}const s=w5(t);this.bytes=s,this.size=s.byteLength,this.id=n.name??`bytes-${Eo()}`}static async fromPath(t,n={}){if(!Fn())throw new T("bad-uri",`cannot open the filesystem path ${t} in a browser; pass the File object instead`,{path:t});const s=await Xe();let o;try{o=s.FS.stat(t).size}catch(r){throw new T("not-hdf5",`cannot stat ${t}: ${r instanceof Error?r.message:String(r)}`,{path:t})}return new ue({path:t,size:o},{name:n.name??t,...n})}static async open(t,n={}){return typeof t=="string"?ue.fromPath(t,n):new ue(t,n)}async mount(){if(this.mountPath!==null)return this.mountPath;const t=await Xe();if(this.module=t,this.existingPath!==null)return this.mountPath=this.existingPath,this.ownsMount=!1,this.mountPath;if(this.blob!==null&&typeof FileReaderSync<"u")return this.mountPath=this.mountLazy(t,this.blob),this.ownsMount=!0,this.mountPath;const n=this.bytes??new Uint8Array(await this.blob.arrayBuffer()),s=this.mountTarget();return t.FS.writeFile(s,n),this.mountPath=s,this.ownsMount=!0,this.requests+=1,this.bytesRead+=n.byteLength,s}mountLazy(t,n){const s=this.mountTarget().replace(/^\//,""),o=t.FS.createFile("/",s,{},!0,!1),r={length:this.size};return o.contents=r,Object.defineProperty(o,"usedBytes",{get:()=>r.length}),o.stream_ops={...o.stream_ops,read:(i,a,c,l,u)=>{if(u>=this.size)return 0;const h=Math.min(this.size-u,l);return this.copyInto(n,a,c,u,h),h},mmap:(i,a,c)=>{const l=t.Module._malloc(a);if(l===0)throw new Error("mmap allocation failed");const u=Math.max(0,Math.min(this.size-c,a));return u>0&&this.copyInto(n,t.Module.HEAPU8,l,c,u),{ptr:l,allocated:!0}}},`/${s}`}readBytes(t,n){const s=Math.max(0,Math.min(this.size-t,n));if(s<=0)return new Uint8Array(0);if(this.bytes!==null)return this.bytes.subarray(t,t+s).slice();if(this.blob!==null){const a=new Uint8Array(s);return this.copyInto(this.blob,a,0,t,s),a}const o=this.module;if(this.existingPath===null||o===null)throw new T("unsupported",`${this.id} cannot be read directly before it has been mounted.`,{id:this.id});const r=new Uint8Array(s),i=o.FS.open(this.existingPath,"r");try{let a=0;for(;a<s;){const c=o.FS.read(i,r,a,s-a,t+a);if(c<=0)break;a+=c}return this.requests+=1,this.bytesRead+=a,a===s?r:r.subarray(0,a)}finally{o.FS.close(i)}}async readBlobExact(t,n,s){const o=Math.max(0,Math.min(this.size-n,s));if(o===0)return new Uint8Array(0);const r=Math.floor(n/ze),i=Math.floor((n+o-1)/ze);let a=!0;for(let l=r;l<=i&&a;l++)a=this.cache.has(l);if(a)return this.readBytes(n,o);const c=new Uint8Array(await t.slice(n,n+o).arrayBuffer());return this.requests+=1,this.bytesRead+=c.byteLength,c}copyInto(t,n,s,o,r){let i=0;for(;i<r;){const a=o+i,c=Math.floor(a/ze);let l=this.cache.get(c);if(l===void 0){const f=c*ze,d=Math.min(f+ze,this.size);l=new Uint8Array(this.reader().readAsArrayBuffer(t.slice(f,d))),this.store(c,l),this.requests+=1,this.bytesRead+=l.byteLength}else this.cache.delete(c),this.cache.set(c,l);const u=a-c*ze,h=Math.min(l.length-u,r-i);if(h<=0)throw new Error(`short read of ${this.id} at ${a}`);n.set(l.subarray(u,u+h),s+i),i+=h}}store(t,n){this.cache.set(t,n),this.cachedBytes+=n.byteLength;for(const s of this.cache.keys()){if(this.cachedBytes<=m5)break;if(s===t)continue;const o=this.cache.get(s);o&&(this.cachedBytes-=o.byteLength),this.cache.delete(s)}}reader(){return this.syncReader??=new FileReaderSync,this.syncReader}close(){if(this.cache.clear(),this.cachedBytes=0,this.mountPath===null)return;const t=this.mountPath;this.mountPath=null,this.ownsMount&&(this.ownsMount=!1,Xe().then(n=>{try{n.FS.unlink(t)}catch{}},()=>{}))}stats(){return{requests:this.requests,bytes:this.bytesRead}}cachedChunkCount(){return this.cache.size}resetStats(){this.requests=0,this.bytesRead=0}mountTarget(){const t=this.id.replace(/[^A-Za-z0-9._-]+/g,"_").slice(-100),n=`chromosomix-${Eo()}-${t}`;if(!Fn())return`/${n}`;const s=globalThis.process?.env;return`${(this.tempDir??s?.TMPDIR??s?.TMP??s?.TEMP??"/tmp").replace(/\/+$/,"")}/${n}`}}function Eo(){return aa+=1,`${Date.now().toString(36)}${aa.toString(36)}`}function g5(e){return typeof Blob<"u"&&e instanceof Blob}function p5(e){return typeof e=="object"&&e!==null&&typeof e.path=="string"&&typeof e.size=="number"}function w5(e){return e instanceof Uint8Array?e:e instanceof ArrayBuffer?new Uint8Array(e):new Uint8Array(e.buffer,e.byteOffset,e.byteLength)}const b5="/chromosomix-lazy",kn=new Map,ca=new WeakSet;let Bo=0;function A5(e,t,n){return{read:(s,o,r,i,a)=>{if(a>=t)return 0;const c=Math.min(t-a,i);return n(o,r,a,c),c},llseek:(s,o,r)=>{const i=r===1?s.position+o:r===2?t+o:o;if(i<0)throw new Error("a lazy file cannot be sought before its start");return i},mmap:(s,o,r)=>{const i=e.Module._malloc(o);if(i===0)throw new Error("mmap allocation failed");const a=Math.max(0,Math.min(t-r,o));return a>0&&n(e.Module.HEAPU8,i,r,a),{ptr:i,allocated:!0}}}}function y5(e,t,n,s){const o=A5(e,n,s);if(Fn()){M5(e),Bo+=1;const a=`${b5}/${t}-${Bo}`;return kn.set(a,{size:n,ino:79691776+Bo,ops:o}),a}const r=e.FS.createFile("/",t,{},!0,!1),i={length:n};return r.contents=i,Object.defineProperty(r,"usedBytes",{get:()=>i.length}),r.stream_ops={...r.stream_ops,read:o.read,mmap:o.mmap},`/${t}`}function N5(e,t){if(!kn.delete(t))try{e.FS.unlink(t)}catch{}}function la(e){const t=new Date(0);return{dev:76,ino:e.ino,mode:33060,nlink:1,uid:0,gid:0,rdev:0,size:e.size,blksize:4096,blocks:Math.ceil(e.size/512),atime:t,mtime:t,ctime:t}}function M5(e){const t=e.FS;if(ca.has(t))return;ca.add(t);const n=t.open.bind(t),s=t.fstat.bind(t),o=t.stat.bind(t);t.open=(r,i,a)=>{const c=typeof r=="string"?kn.get(r):void 0;if(c===void 0)return n(r,i,a);const l={id:c.ino,mode:33060,node_ops:{},path:r,usedBytes:c.size};return t.createStream({path:r,flags:0,node:l,stream_ops:c.ops,seekable:!0,position:0})},t.fstat=r=>{const i=t.getStream(r),a=i?.path===void 0?void 0:kn.get(i.path);return a!==void 0&&i?.stream_ops===a.ops?la(a):s(r)},t.stat=(r,i)=>{const a=typeof r=="string"?kn.get(r):void 0;return a===void 0?o(r,i):la(a)}}const I5=new Uint32Array([1116352408,1899447441,3049323471,3921009573,961987163,1508970993,2453635748,2870763221,3624381080,310598401,607225278,1426881987,1925078388,2162078206,2614888103,3248222580,3835390401,4022224774,264347078,604807628,770255983,1249150122,1555081692,1996064986,2554220882,2821834349,2952996808,3210313671,3336571891,3584528711,113926993,338241895,666307205,773529912,1294757372,1396182291,1695183700,1986661051,2177026350,2456956037,2730485921,2820302411,3259730800,3345764771,3516065817,3600352804,4094571909,275423344,430227734,506948616,659060556,883997877,958139571,1322822218,1537002063,1747873779,1955562222,2024104815,2227730452,2361852424,2428436474,2756734187,3204031479,3329325298]),x5=new Uint32Array([1779033703,3144134277,1013904242,2773480762,1359893119,2600822924,528734635,1541459225]),Xt=64,Ul=32;function Yt(e,t){return e>>>t|e<<32-t}function bs(e){const t=new Uint32Array(x5),n=new Uint32Array(64),s=e.length*8,o=new Uint8Array((Math.floor((e.length+8)/Xt)+1)*Xt);o.set(e),o[e.length]=128;const r=new DataView(o.buffer);r.setUint32(o.length-8,Math.floor(s/4294967296),!1),r.setUint32(o.length-4,s>>>0,!1);for(let c=0;c<o.length;c+=Xt){for(let w=0;w<16;w++)n[w]=r.getUint32(c+w*4,!1);for(let w=16;w<64;w++){const b=n[w-15],y=n[w-2],A=Yt(b,7)^Yt(b,18)^b>>>3,N=Yt(y,17)^Yt(y,19)^y>>>10;n[w]=n[w-16]+A+n[w-7]+N>>>0}let[l,u,h,f,d,m,g,p]=t;for(let w=0;w<64;w++){const b=Yt(d,6)^Yt(d,11)^Yt(d,25),y=d&m^~d&g,A=p+b+y+I5[w]+n[w]>>>0,N=Yt(l,2)^Yt(l,13)^Yt(l,22),M=l&u^l&h^u&h,I=N+M>>>0;p=g,g=m,m=d,d=f+A>>>0,f=h,h=u,u=l,l=A+I>>>0}t[0]=t[0]+l>>>0,t[1]=t[1]+u>>>0,t[2]=t[2]+h>>>0,t[3]=t[3]+f>>>0,t[4]=t[4]+d>>>0,t[5]=t[5]+m>>>0,t[6]=t[6]+g>>>0,t[7]=t[7]+p>>>0}const i=new Uint8Array(Ul),a=new DataView(i.buffer);for(let c=0;c<8;c++)a.setUint32(c*4,t[c],!1);return i}function Mn(e,t){const n=new Uint8Array(Xt);n.set(e.length>Xt?bs(e):e);const s=new Uint8Array(Xt+t.length),o=new Uint8Array(Xt+Ul);for(let r=0;r<Xt;r++)s[r]=n[r]^54,o[r]=n[r]^92;return s.set(t,Xt),o.set(bs(s),Xt),bs(o)}function ua(e){let t="";for(const n of e)t+=n.toString(16).padStart(2,"0");return t}function Ce(e){return new TextEncoder().encode(e)}const v5="e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",ha="AWS4-HMAC-SHA256",Gl="s3",Hl="aws4_request";function k5(e){const t=(n,s=2)=>String(n).padStart(s,"0");return`${t(e.getUTCFullYear(),4)}${t(e.getUTCMonth()+1)}${t(e.getUTCDate())}T${t(e.getUTCHours())}${t(e.getUTCMinutes())}${t(e.getUTCSeconds())}Z`}function gr(e){return encodeURIComponent(e).replace(/[!'()*]/g,t=>`%${t.charCodeAt(0).toString(16).toUpperCase()}`)}function S5(e){return e===""?"/":e.split("/").map(t=>gr(decodeURIComponent(t))).join("/")}function C5(e){const t=[];return e.forEach((n,s)=>t.push([gr(s),gr(n)])),t.sort((n,s)=>n[0]===s[0]?n[1]<s[1]?-1:1:n[0]<s[0]?-1:1),t.map(([n,s])=>`${n}=${s}`).join("&")}let os=null;function F5(e,t,n){const s=`${e}|${t}|${n}`;if(os!==null&&os.id===s)return os.key;const o=Mn(Ce(`AWS4${e}`),Ce(t)),r=Mn(o,Ce(n)),i=Mn(r,Ce(Gl)),a=Mn(i,Ce(Hl));return os={id:s,key:a},a}function $5(e){const t=new URL(e.url),n=e.now??new Date,s=k5(n),o=s.slice(0,8),r=e.payloadHash??v5,{credential:i}=e,a=[["host",t.host],["x-amz-content-sha256",r],["x-amz-date",s]];i.sessionToken!==void 0&&i.sessionToken!==""&&a.push(["x-amz-security-token",i.sessionToken]);const c=a.map(([g,p])=>`${g}:${p.trim()}
6
+ `).join(""),l=a.map(([g])=>g).join(";"),u=[e.method.toUpperCase(),S5(t.pathname),C5(t.searchParams),c,l,r].join(`
7
+ `),h=`${o}/${i.region}/${Gl}/${Hl}`,f=[ha,s,h,ua(bs(Ce(u)))].join(`
8
+ `),d=ua(Mn(F5(i.secretAccessKey,o,i.region),Ce(f))),m={Authorization:`${ha} Credential=${i.accessKeyId}/${h}, SignedHeaders=${l}, Signature=${d}`,"x-amz-content-sha256":r,"x-amz-date":s};return i.sessionToken!==void 0&&i.sessionToken!==""&&(m["x-amz-security-token"]=i.sessionToken),m}function Te(e){const t={...e.headers};return e.credential===void 0?t:{...t,...$5({method:(e.method??"GET").toUpperCase(),url:e.url,credential:e.credential})}}function Es(e){const t=E5(e);return t.has("x-amz-signature")||t.has("x-goog-signature")?!0:t.has("sig")&&t.has("sv")}function E5(e){const t=new Set;if(typeof e!="string")return t;const n=e.indexOf("#"),s=n<0?e:e.slice(0,n),o=s.indexOf("?");if(o<0)return t;const r=s.slice(o+1);for(const i of r.split("&")){const a=i.indexOf("=");a<=0||t.add(i.slice(0,a).toLowerCase())}return t}const B5=1<<20,T5=64<<20,O5=4<<20,L5=16<<20,_5=8;function D5(e,t){const n=new XMLHttpRequest;n.open("GET",e,!1);for(const[s,o]of Object.entries(t))n.setRequestHeader(s,o);return n.responseType="arraybuffer",n.send(null),{status:n.status,body:new Uint8Array(n.response??new ArrayBuffer(0)),contentRange:n.getResponseHeader("content-range")}}class Ee{id;size;isRemote=!0;url;chunkSize;onRead;maxCacheBytes;cache=new Map;cachedBytes=0;readAhead;recent=[];requests;bytesRead;mountPath=null;credential;headers;fetchImpl;syncFetch;constructor(t,n,s,o){this.url=t,this.id=t,this.size=n,this.credential=s.credential,this.headers=s.headers,this.fetchImpl=s.fetchImpl??((i,a)=>globalThis.fetch(i,a)),this.syncFetch=s.syncFetch??null,this.chunkSize=Math.max(1,Math.trunc(s.chunkSize??B5)),this.readAhead=s.readAhead??!0,this.onRead=s.onRead;const r=Math.trunc(s.maxCacheBytes??T5);this.maxCacheBytes=Math.max(this.chunkSize,r),this.requests=o.requests,this.bytesRead=o.bytes}static async open(t,n={}){const s=n.fetchImpl??globalThis.fetch,o=m=>Te({url:t,method:m,...n.headers===void 0?{}:{headers:n.headers},...n.credential===void 0?{}:{credential:n.credential}});if(typeof s!="function")throw new T("remote-cors","this runtime has no fetch(); remote files need one",{url:t});const r=bn(t),i={requests:0,bytes:0};let a=null;if(!Es(t))try{i.requests+=1,a=await s(t,{method:"HEAD",headers:o("HEAD")})}catch(m){await da(t,r,m,s,i)}if(a&&!a.ok)throw new T("bad-uri",n.credential===void 0?`${r} answered ${a.status} ${a.statusText} to a HEAD of ${t}; the viewer needs a publicly readable object, or a bucket connected with a key for it (connect a bucket…, on the landing page or in the Files column).`:`${r} answered ${a.status} ${a.statusText} to a signed HEAD of ${t}. The key reached the bucket and was rejected: check that the region on the bucket entry matches the bucket, that the key is allowed \`s3:GetObject\` on this prefix, and — for a temporary credential — that the session token has not expired.`,{url:t,host:r,status:a.status,signed:n.credential!==void 0});const c=ma(a);if(c!==null){const m=await R5(t,s,o,i),g=ga(m?.headers.get("content-range")??null)??rs(m,"content-length");if(m!==null&&m.status===206&&ma(m)===null&&g!==null)return new Ee(t,g,n,i);throw new T("remote-compressed",`${r} is serving ${t} with “Content-Encoding: ${c}”, so byte ranges address the compressed stream rather than the file.`,{url:t,host:r,encoding:c,encodedLength:rs(a,"content-length")})}const l=rs(a,"content-length"),u=l===0?null:l;if((a?.headers.get("accept-ranges")??"").toLowerCase().includes("bytes")&&u!==null)return new Ee(t,u,n,i);let f;try{i.requests+=1,f=await s(t,{method:"GET",headers:{...o("GET"),Range:"bytes=0-0"}})}catch(m){throw await da(t,r,m,s,i),new Error("unreachable")}if(i.bytes+=rs(f,"content-length")??1,f.status!==206)throw new T("remote-no-ranges",f.status>=400?`${r} answered ${f.status} ${f.statusText} to a request for the first byte of ${t}`+(Es(t)?": this is a presigned link, and a host refuses one once it has expired.":"."):`${r} does not serve HTTP range requests: a “Range: bytes=0-0” request for ${t} returned ${f.status}, not 206 Partial Content.`,{url:t,host:r,status:f.status});const d=ga(f.headers.get("content-range"))??u;if(d===null)throw new T("remote-cors",`${r} serves ranges for ${t} but does not expose its size to this page: neither “Content-Length” nor “Content-Range” was readable. Add both to the bucket’s CORS “Access-Control-Expose-Headers”.`,{url:t,host:r});return new Ee(t,d,n,i)}async mount(){if(this.mountPath!==null)return this.mountPath;if(this.syncFetch===null&&typeof XMLHttpRequest>"u")throw new T("remote-cors","remote files are read with synchronous XMLHttpRequest, which exists only in a browser worker; this runtime has none.",{url:this.url});const t=await Xe(),n=`remote-${Math.abs(P5(this.url)).toString(36)}`;return this.mountPath=y5(t,n,this.size,(s,o,r,i)=>this.copyInto(s,o,r,i)),this.mountPath}close(){if(this.cache.clear(),this.cachedBytes=0,this.mountPath===null)return;const t=this.mountPath;this.mountPath=null,Xe().then(n=>N5(n,t),()=>{})}stats(){return{requests:this.requests,bytes:this.bytesRead}}resetStats(){this.requests=0,this.bytesRead=0}cachedChunkCount(){return this.cache.size}readBytes(t,n){const s=Math.max(0,Math.min(this.size-t,n)),o=new Uint8Array(s);return s>0&&this.copyInto(o,0,t,s),o}async readExact(t,n){const s=Math.max(0,Math.min(this.size-t,n));if(s===0)return new Uint8Array(0);const o=Math.floor(t/this.chunkSize),r=Math.floor((t+s-1)/this.chunkSize);let i=!0;for(let d=o;d<=r&&i;d++)i=this.cache.has(d);if(i)return this.readBytes(t,s);const a=t,c=t+s-1,l=this.fetchImpl,u=await l(this.url,{method:"GET",headers:{...this.authorisation(),Range:`bytes=${a}-${c}`}});this.requests+=1,this.refuseStatus(u.status,a,c);const h=new Uint8Array(await u.arrayBuffer());this.bytesRead+=h.byteLength,this.onRead?.(this.requests,this.bytesRead);const f=this.windowOf(h,a,c,u.headers.get("content-range"));if(f.byteLength<s)throw new T("remote-no-ranges","a range request returned a short response",{wanted:s,available:f.byteLength});return f.byteLength===s&&f.buffer.byteLength===s?f:f.slice(0,s)}copyInto(t,n,s,o){let r=0;for(;r<o;){const i=s+r,a=Math.floor(i/this.chunkSize),c=this.cache.get(a);if(c){this.touch(a),r+=fa(t,n+r,c,i-a*this.chunkSize,o-r);continue}const l=Math.floor((s+o-1)/this.chunkSize);let u=a;for(;u+1<=l&&!this.cache.has(u+1);)u+=1;const{ahead:h,streak:f}=this.aheadFor(a,u),d=Math.min(u+h,Math.ceil(this.size/this.chunkSize)-1);for(;u<d&&!this.cache.has(u+1);)u+=1;this.recent.push({from:a,to:u,streak:f}),this.recent.length>_5&&this.recent.shift();const m=a*this.chunkSize,g=Math.min((u+1)*this.chunkSize,this.size)-1,p=this.fetchRange(m,g);r+=fa(t,n+r,p,i-m,o-r);for(let w=a;w<=u;w++){const b=w*this.chunkSize-m;if(b>=p.length)break;this.store(w,p.subarray(b,Math.min(b+this.chunkSize,p.length)))}}}aheadFor(t,n){if(!this.readAhead)return{ahead:0,streak:0};const s=Math.max(1,Math.floor(O5/this.chunkSize));let o=0;for(const i of this.recent)t>=i.from-s&&t<=i.to+s&&(o=Math.max(o,i.streak+1));if(o<2)return{ahead:0,streak:o};const r=Math.max(0,Math.floor(L5/this.chunkSize)-(n-t+1));return{ahead:Math.min(r,2**(o-2)),streak:o}}fetchRange(t,n){const s={...this.authorisation(),Range:`bytes=${t}-${n}`},o=(this.syncFetch??D5)(this.url,s);return this.requests+=1,this.refuseStatus(o.status,t,n),this.bytesRead+=o.body.byteLength,this.onRead?.(this.requests,this.bytesRead),this.windowOf(o.body,t,n,o.contentRange)}authorisation(){return this.credential===void 0&&this.headers===void 0?{}:Te({url:this.url,method:"GET",...this.headers===void 0?{}:{headers:this.headers},...this.credential===void 0?{}:{credential:this.credential}})}refuseStatus(t,n,s){if(t===403&&this.credential!==void 0)throw new T("bad-uri",`${bn(this.url)} refused a signed request for bytes ${n}-${s} of ${this.url}. The key reached the bucket and was rejected: check the region on the bucket entry matches the bucket, that the key is allowed \`s3:GetObject\` on this prefix, and — for a temporary credential — that the session token has not expired.`,{url:this.url,status:403,from:n,to:s,signed:!0});if(t!==206&&t!==200)throw new T("remote-no-ranges",`${bn(this.url)} returned ${t} for bytes ${n}-${s} of ${this.url}`,{url:this.url,status:t,from:n,to:s})}windowOf(t,n,s,o){const r=s-n+1,i=Ql(o);if(i!==null&&i.end-i.start+1!==t.byteLength)throw new T("remote-compressed",`${bn(this.url)} answered a request for bytes ${n}-${s} of ${this.url} with “Content-Range: ${o}” — ${i.end-i.start+1} octets — but the body is ${t.byteLength}. The response was re-encoded in transit (transfer compression), so ranges do not address the file. Serve it without “Content-Encoding”, or download it and drop it here.`,{url:this.url,from:n,to:s,claimed:i.end-i.start+1,received:t.byteLength});const a=i?.start??z5(o),c=a===null?t.byteLength>r?n:0:n-a;if(c===0&&t.byteLength<=r)return t;if(c<0||c>=t.byteLength)throw new T("remote-no-ranges",`${bn(this.url)} answered a request for bytes ${n}-${s} of ${this.url} with ${t.byteLength} bytes starting at ${a??"an unstated offset"}, which does not contain what was asked for.`,{url:this.url,from:n,to:s,start:a,length:t.byteLength});return t.subarray(c,Math.min(c+r,t.byteLength))}store(t,n){const s=new Uint8Array(n);this.cache.set(t,s),this.cachedBytes+=s.byteLength;for(const o of this.cache.keys()){if(this.cachedBytes<=this.maxCacheBytes)break;if(o===t)continue;const r=this.cache.get(o);r&&(this.cachedBytes-=r.byteLength),this.cache.delete(o)}}touch(t){const n=this.cache.get(t);n&&(this.cache.delete(t),this.cache.set(t,n))}}function fa(e,t,n,s,o){const r=Math.min(n.length-s,o);if(r<=0)throw new T("remote-no-ranges","a range request returned a short response",{wanted:o,available:n.length-s});return e.set(n.subarray(s,s+r),t),r}async function da(e,t,n,s,o){let r=!1;try{o.requests+=1,await s(e,{method:"GET",mode:"no-cors",headers:{Range:"bytes=0-0"}}),r=!0}catch{r=!1}const i=n instanceof Error?`${n.name}: ${n.message}`:String(n);throw r?new T("remote-cors",`${t} is reachable but refused a cross-origin read of ${e} (${i}). That is what a missing “Access-Control-Allow-Origin” looks like from a browser: the failure has no status and no headers. Configure the bucket to allow GET and HEAD and the “Range” request header, and to expose “Content-Length” and “Content-Range”.`,{url:e,host:t,cause:i,reachable:!0}):new T("remote-cors",`${t} could not be reached for ${e} (${i}). The request failed before any response arrived, so this is a DNS, TLS or connectivity problem rather than a CORS one.`,{url:e,host:t,cause:i,reachable:!1})}function bn(e){try{return new URL(e).host}catch{return e}}async function R5(e,t,n,s){try{return s.requests+=1,await t(e,{method:"HEAD",headers:{...n("HEAD"),Range:"bytes=0-"}})}catch{return null}}function ma(e){const t=(e?.headers.get("content-encoding")??"").trim().toLowerCase();return t===""||t==="identity"?null:t}function rs(e,t){const n=e?.headers.get(t);if(n==null||n.trim()==="")return null;const s=Number(n);return Number.isFinite(s)?s:null}function z5(e){return Ql(e)?.start??null}function Ql(e){if(!e)return null;const t=/bytes\s+(\d+)-(\d+)\/\s*(\d+|\*)\s*$/.exec(e);if(!t)return null;const n=Number(t[1]),s=Number(t[2]),o=t[3]==="*"?null:Number(t[3]);return!Number.isFinite(n)||!Number.isFinite(s)?null:{start:n,end:s,total:o!==null&&Number.isFinite(o)?o:null}}function ga(e){if(!e)return null;const t=/\/\s*(\d+)\s*$/.exec(e);if(!t)return null;const n=Number(t[1]);return Number.isFinite(n)?n:null}function P5(e){let t=0;for(let n=0;n<e.length;n++)t=Math.imul(31,t)+e.charCodeAt(n)|0;return t}class se{rows=[];contigIds=new Map;contigs=[];contigId(t){const n=this.contigIds.get(t);if(n!==void 0)return n;const s=this.contigs.length;return this.contigIds.set(t,s),this.contigs.push(t),s}add(t,n,s,o={}){const r=Math.min(n,s),i=Math.max(r+1,Math.max(n,s));this.rows.push({contig:this.contigId(t),start:r,end:i,strand:o.strand??0,score:o.score??Number.NaN,name:o.name??"",id:o.id??"",detail:o.detail??"",exons:o.exons??null,cds:o.cds??null,bars:o.bars??null,xyz:o.xyz??null,hap:o.hap??0})}get length(){return this.rows.length}finish(t=null,n=null){const s=this.rows;s.sort((d,m)=>d.contig-m.contig||d.start-m.start||d.end-m.end||d.hap-m.hap);const o=s.length,r={contigs:this.contigs,length:o,contig:new Int32Array(o),start:new Int32Array(o),end:new Int32Array(o),strand:new Int8Array(o),score:new Float32Array(o),name:new Array(o),id:new Array(o),detail:new Array(o),exonAt:new Int32Array(o+1),exonStart:new Int32Array(0),exonEnd:new Int32Array(0),cdsStart:new Int32Array(o),cdsEnd:new Int32Array(o),contigAt:new Int32Array(this.contigs.length+1),maxSpan:new Int32Array(this.contigs.length)};let i=0;for(const d of s)i+=d.exons===null?0:d.exons.length>>1;r.exonStart=new Int32Array(i),r.exonEnd=new Int32Array(i);const a=t===null?0:t.labels.length,c=new Float32Array(a*o).fill(Number.NaN);let l=0;const u=n===null?null:{x:new Float32Array(o),y:new Float32Array(o),z:new Float32Array(o),hap:new Int8Array(o)};let h=0;for(let d=0;d<o;d++){const m=s[d];if(r.contig[d]=m.contig,r.start[d]=m.start,r.end[d]=m.end,r.strand[d]=m.strand,r.score[d]=m.score,r.name[d]=m.name,r.id[d]=m.id,r.detail[d]=m.detail,r.cdsStart[d]=m.cds===null?-1:m.cds[0],r.cdsEnd[d]=m.cds===null?-1:m.cds[1],r.exonAt[d]=h,m.exons!==null){const p=[];for(let w=0;w+1<m.exons.length;w+=2)p.push([m.exons[w],m.exons[w+1]]);p.sort((w,b)=>w[0]-b[0]);for(const[w,b]of p)r.exonStart[h]=w,r.exonEnd[h]=b,h++}if(a>0&&m.bars!==null&&m.bars.length===a)for(let p=0;p<a;p++){const w=m.bars[p];c[d*a+p]=w,w>l&&(l=w)}u!==null&&(u.x[d]=m.xyz===null?Number.NaN:m.xyz[0],u.y[d]=m.xyz===null?Number.NaN:m.xyz[1],u.z[d]=m.xyz===null?Number.NaN:m.xyz[2],u.hap[d]=m.hap);const g=m.end-m.start;g>r.maxSpan[m.contig]&&(r.maxSpan[m.contig]=g)}r.exonAt[o]=h;let f=0;for(let d=0;d<this.contigs.length;d++)for(r.contigAt[d]=f;f<o&&r.contig[f]===d;)f++;if(r.contigAt[this.contigs.length]=f,t!==null){const{limit:d,...m}=t;r.bars={...m,values:c,max:Number.isFinite(d)&&d>0?d:l}}return u!==null&&n!==null&&(r.model={...u,haplotypes:[...n.haplotypes],width:n.width}),r}}class Kl{rows=[];contigIds=new Map;contigs=[];contigId(t){const n=this.contigIds.get(t);if(n!==void 0)return n;const s=this.contigs.length;return this.contigIds.set(t,s),this.contigs.push(t),s}add(t,n,s,o,r,i,a={}){this.rows.push({c1:this.contigId(t),s1:n,e1:Math.max(n+1,s),c2:this.contigId(o),s2:r,e2:Math.max(r+1,i),score:a.score??Number.NaN,name:a.name??"",detail:a.detail??""})}get length(){return this.rows.length}finish(){const t=this.rows.length,n={contigs:this.contigs,length:t,contig1:new Int32Array(t),start1:new Int32Array(t),end1:new Int32Array(t),contig2:new Int32Array(t),start2:new Int32Array(t),end2:new Int32Array(t),score:new Float32Array(t),name:new Array(t),detail:new Array(t)};for(let s=0;s<t;s++){const o=this.rows[s];n.contig1[s]=o.c1,n.start1[s]=o.s1,n.end1[s]=o.e1,n.contig2[s]=o.c2,n.start2[s]=o.s2,n.end2[s]=o.e2,n.score[s]=o.score,n.name[s]=o.name,n.detail[s]=o.detail}return n}}(()=>{const e=new Int8Array(256).fill(-1),t=(n,s)=>{for(const o of n)e[o.charCodeAt(0)]=s,e[o.toLowerCase().charCodeAt(0)]=s};return t("TU",0),t("C",1),t("A",2),t("G",3),e})();const q5=["gtf","track","segments","variants","barchart","ideogram","junctions","pairs","sequence","metadata","classes","alignments","reads","structure"];function U5(e){return e==="gtf"||e==="segments"||e==="variants"||e==="barchart"||e==="ideogram"||e==="junctions"||e==="structure"}const G5=" · ",Wr=12,pa=65536;function H5(e){return e.length<Wr||e[0]!==31||e[1]!==139||e[2]!==8||(e[3]&4)===0?!1:Xr(e)!==null}function Xr(e){if(e.length<Wr)return null;const t=new DataView(e.buffer,e.byteOffset,e.byteLength),n=t.getUint16(10,!0);let s=12;const o=Math.min(12+n,e.length);for(;s+4<=o;){const r=e[s],i=e[s+1],a=t.getUint16(s+2,!0);if(r===66&&i===67&&a===2)return t.getUint16(s+4,!0)+1;s+=4+a}return null}const Q5=Rl;async function Vl(e,t=Number.POSITIVE_INFINITY,n=Pr){const s=[];let o=0,r=0;for(;r+Wr<=e.length;){const c=r,l=Xr(e.subarray(r,Math.min(r+64,e.length)));if(l===null||r+l>e.length)break;const h=new DataView(e.buffer,e.byteOffset+r,l).getUint16(10,!0),f=r+12+h,d=r+l-8;if(d>f){const m=await Q5(e.subarray(f,d));if(m.length>0){if(o+m.length>n)throw El({format:"BGZF file",field:"span read"},n,e.length);s.push(m),o+=m.length}}if(r+=l,c>=t)break}const i=new Uint8Array(o);let a=0;for(const c of s)i.set(c,a),a+=c.length;return i}const K5={ratio:64,floor:Pr,ceiling:2**34};async function V5(e,t=Pr){if(!(e.length>2&&e[0]===31&&e[1]===139))return e;if(typeof DecompressionStream>"u")throw new Error("This file is gzipped and this browser has no DecompressionStream to un-gzip it with. Decompress it and drop the plain text file.");return H5(e)?Vl(e,Number.POSITIVE_INFINITY,t):qr(e,{format:"file",field:"gzip stream",encoding:"gzip",maxBytes:t})}async function Y5(e,t){if(e.kind==="local"){const r=Z5(e.file);return r!==null?r:(En(),J5(e.file,new ue(e.file)))}if(e.reading==="whole"){const r=n3(e.url);return r!==null?r:e3(e.url,await Xl(e.url,$n(e),t))}const n=e.reading??"",s=j5(e.url,n);if(s!==null)return s;const o=$n(e);try{return wa(e.url,n,await Ee.open(e.url,{...o,...e.reading==="fine"?{chunkSize:Wl,readAhead:!1,...t?.onRead===void 0?{}:{onRead:t.onRead.bind(t)}}:{}}))}catch(r){if(r instanceof T&&r.code==="remote-compressed")return wa(e.url,n,await t3(e.url,r,o));if(r instanceof T)throw r;const i=Bs(e.url);throw new T("remote-cors",`Could not read ${e.url}. The host ${i} either did not allow this page to read it (CORS must permit GET and HEAD and expose Content-Length and Content-Range) or refused the request outright. Underlying error: ${r instanceof Error?r.message:String(r)}`,{url:e.url,host:i})}}async function pr(e){if(e.kind==="local")return new ue(e.file);const t=$n(e);return e.reading==="whole"?await Xl(e.url,t):await Ee.open(e.url,{...t,...e.reading==="fine"?{chunkSize:Wl,readAhead:!1}:{}})}async function W5(e){if(e.kind==="local")return e.index===void 0?null:new ue(e.index);const t=`${X5(e.url)}.px2`;try{return await Ee.open(t,$n(e))}catch{return null}}function X5(e){const t=e.search(/[?#]/);return t<0?e:e.slice(0,t)}function $n(e){return{...e.credential===void 0?{}:{credential:e.credential},...e.headers===void 0?{}:{headers:e.headers}}}function Yl(e){return e===void 0?{}:"accessKeyId"in e?{credential:e}:e}let Fe=null,je=null;function J5(e,t){return je={file:e,source:t},an(t)}function Z5(e){return je===null||je.file!==e?null:an(je.source)}function wa(e,t,n){return Fe={url:e,reading:t,source:n},an(n)}function an(e){const t=e.readExact?.bind(e);return{id:e.id,size:e.size,isRemote:e.isRemote,mount:()=>e.mount(),readBytes:(n,s)=>e.readBytes(n,s),...t===void 0?{}:{readExact:t},stats:()=>e.stats(),close:()=>{}}}function j5(e,t){return Fe===null||Fe.url!==e||Fe.reading!==t?(En(),null):an(Fe.source)}function En(){Fe?.source.close(),Fe=null,je?.source.close(),je=null,pe?.source.close(),pe=null}const ba=128<<20;async function t3(e,t,n={}){const s=t.detail.encodedLength,o=typeof s=="number"?s:null;if(o!==null&&o>ba)throw new T("remote-too-large",`${t.message} Downloading it whole is the only way left to read it, and at ${Math.round(o/(1<<20))} MB encoded that is too much to ask a browser tab for. Download it and drop it here instead.`,{...t.detail,limit:ba});let r;try{const i=await fetch(e,{headers:Te({url:e,method:"GET",...Yl(n)})});if(!i.ok)throw new Error(`${i.status} ${i.statusText}`);r=new Uint8Array(await i.arrayBuffer())}catch(i){throw new T("remote-compressed",`${t.message} Falling back to downloading it whole also failed: ${i instanceof Error?i.message:String(i)}`,t.detail)}return new ue(r,{name:e})}const Wl=16384,Aa=2*(1<<30);async function Xl(e,t={},n){let s;try{const o=await fetch(e,{headers:Te({url:e,method:"GET",...Yl(t)})});if(!o.ok)throw new Error(`${o.status} ${o.statusText}`);const r=Number(o.headers.get("Content-Length")??Number.NaN);if(Number.isFinite(r)&&r>Aa)throw new T("remote-too-large",`${e} is ${(r/(1<<30)).toFixed(1)} GB, which is more than a browser tab can hold in memory. Download it and drop it here instead, where it is read from disk rather than held.`,{url:e,size:r,limit:Aa});s=await Jl(o,n)}catch(o){throw o instanceof T||o3(o)?o:new T("remote-cors",`Could not download ${e} whole: ${o instanceof Error?o.message:String(o)}`,{url:e,host:Bs(e)})}return new ue(s,{name:e})}let pe=null;function e3(e,t){return En(),pe={url:e,source:t},an(t)}function n3(e){return pe===null?null:pe.url!==e?(pe.source.close(),pe=null,null):an(pe.source)}async function ya(e,t){let n;if(e.kind==="local")n=new Uint8Array(await e.file.arrayBuffer());else{let s;try{s=await fetch(e.url,{headers:Te({url:e.url,method:"GET",...$n(e)})})}catch(o){throw new T("remote-cors",`Could not read ${e.url}: ${o instanceof Error?o.message:String(o)}. The host ${Bs(e.url)} must allow this page to GET it.`,{url:e.url,host:Bs(e.url)})}if(!s.ok)throw new T("remote-cors",`${e.url} answered ${s.status} ${s.statusText}.`,{url:e.url,status:s.status});n=await Jl(s,t)}return V5(n,$l(n.length,K5))}function s3(){const e=new Error("cancelled");return e.name="AbortError",e}function o3(e){return e instanceof Error&&e.name==="AbortError"}async function Jl(e,t){const n=Number(e.headers.get("Content-Length")??Number.NaN),s=Number.isFinite(n)&&n>0?n:0,o=r3(e)?0:s,r=e.body,i=t?.onProgress;if(r===null||t===void 0)return new Uint8Array(await e.arrayBuffer());const a=r.getReader(),c=()=>{throw a.cancel(),s3()};let l=0;if(o>0){const u=new Uint8Array(o);for(;;){const{done:h,value:f}=await a.read();if(h)break;if(t.cancelled?.()===!0&&c(),f!==void 0){if(l+f.length>o){const d=[u.subarray(0,l),f];return await Na(a,t,c,d,l+f.length)}u.set(f,l),l+=f.length,i?.(l,o)}}if(i?.(l,o),l<o){const h=e.url??"";throw new T("remote-cors",`${h===""?"The response":h} ended after ${l} bytes, short of the ${o} its \`Content-Length\` promised. The transfer was cut off; a partial file is not opened as though it were whole. Retry it, or download the file and drop it here.`,{url:e.url,received:l,declared:o})}return u}return await Na(a,t,c,[],0)}async function Na(e,t,n,s,o){const r=t.onProgress;for(o>0&&r?.(o,0);;){const{done:c,value:l}=await e.read();if(c)break;t.cancelled?.()===!0&&n(),l!==void 0&&(s.push(l),o+=l.length,r?.(o,0))}const i=new Uint8Array(o);let a=0;for(const c of s)i.set(c,a),a+=c.length;return i}function r3(e){const t=(e.headers.get("Content-Encoding")??"").trim().toLowerCase();return t!==""&&t!=="identity"}function Bs(e){try{return new URL(e).host}catch{return e}}function i3(e){return/\.matrix(\.gz)?$/i.test(e)}function Ts(e){const t=e.replace(/\.matrix(\.gz)?$/i,"").replace(/_iced$/i,"");return[`${t}_abs.bed`,`${t}_abs.bed.gz`]}const Sn=9,qt=10,Zl=13,jl=35,a3=43,wr=45,c3=46,Bt=48,Ge=57,l3=69,u3=101;function h3(e,t){const n=[],s=[],o=[0];let r=0,i=0,a=null,c=0,l=!1;const u=new TextDecoder,h=e.length;let f=0;for(;f<h;){if(e[f]===qt||e[f]===Zl){f++;continue}if(e[f]===jl||d3(e,f)){for(;f<h&&e[f]!==qt;)f++;continue}const g=f;for(;f<h&&e[f]!==Sn&&e[f]!==qt;)f++;const p=u.decode(e.subarray(g,f));if(f<h&&e[f]===qt)throw Pe(t,i+1,"a row with one column");f++;const w=To(e,f);if(f=w.at,e[f]!==Sn)throw Pe(t,i+1,"a row with two columns");f++;const b=To(e,f);f=b.at;let y=null;if(e[f]===Sn){f++;const N=To(e,f);if(f=N.at,!N.saw)throw new T("format",`${t} is a BED whose fourth column holds names rather than bin ids, so it describes features rather than a bin grid — drop it as a track instead. A HiC-Pro bin file numbers its bins 1…n, and is written as \`<prefix>_<res>_abs.bed\`.`,{label:t,row:i+1});y=N.value}for(;f<h&&e[f]!==qt;)f++;f++;const A=b.value-w.value;if(A<=0)throw Pe(t,i+1,`an interval of width ${A}`);if(A>r&&(r=A),i++,y!==null&&(l||(a=y),l=!0,c=y),n.length===0||n[n.length-1]!==p){if(n.includes(p))throw Pe(t,i,`${JSON.stringify(p)} in two separate runs of rows`);n.push(p),s.push(b.value),o.push(i-1)}else s[s.length-1]=b.value}if(i===0)throw Pe(t,0,"no rows at all");const d=Float64Array.from([...o.slice(1),i]);if(d.length!==n.length+1)throw Pe(t,i,`${n.length} sequences but ${d.length} offsets`);const m=f3(n.length,d,s,r);if(a!==null&&(a!==1||c!==i))throw new T("format",`${t} is not a HiC-Pro bin file: its bin ids run ${a}…${c} over ${i} rows, and HiC-Pro numbers them 1…n in row order. If this is an ordinary BED, it describes features rather than a bin grid — drop it as a track instead.`,{label:t,firstId:a,lastId:c,rows:i});return{names:n,lengths:Float64Array.from(s),chromOffset:d,nBins:i,binsize:m?r:null,fixed:m}}function f3(e,t,n,s){if(s<=0)return!1;for(let o=0;o<e;o++){const r=t[o+1]-t[o],i=(r-1)*s;if(n[o]<=i||n[o]>r*s)return!1}return!0}function d3(e,t){const n="track";if(t+n.length>e.length)return!1;for(let s=0;s<n.length;s++)if(e[t+s]!==n.charCodeAt(s))return!1;return!0}function Pe(e,t,n){return new T("format",`${e} is not a HiC-Pro bin file: ${n}${t>0?` at row ${t}`:""}. The file beside a \`.matrix\` is \`chrom start end binID\`, one row per bin of the whole genome, and HiC-Pro writes it as \`<prefix>_<res>_abs.bed\`.`,{label:e,row:t})}const m3=()=>{},g3=()=>!1;async function p3(e,t,n,s={}){const o=e.length;let r=0;for(let k=0;k<o;k++)e[k]===qt&&r++;if(o>0&&e[o-1]!==qt&&r++,r===0)throw new T("format",`${n} holds no pixels. A HiC-Pro matrix is \`binA binB count\`, one row per non-zero pixel.`,{label:n});const i=new Int32Array(r),a=new Int32Array(r),c=new Float32Array(r),l=new Uint8Array(r),u={out:0,line:0,sawUpper:!1,sawLower:!1};let h=0,f=0;const d=s.onProgress??m3,m=s.cancelled??g3;for(;f<o;){const k=b3(e,Math.min(o,f+w3));if(f=A3(e,f,k,t,n,i,a,c,l,u),await vt(),m())throw I3();d(u.out,r)}const g=u.out,{sawUpper:p,sawLower:w}=u,b=p&&w?"mixed":w?"lower":"upper",y=N3(a,g,t),{order:A,offsets:N}=M3(i,y,g,t),M=new Int32Array(g),I=new Int32Array(g),x=new Float32Array(g),v=new Uint8Array(g);for(let k=0;k<g;k++){const C=A[k];M[k]=i[C],I[k]=a[C],x[k]=c[C],v[k]=l[C]}const S=y3(M,I,x,v,N,t);for(let k=0;k<S.nnz;k++)h+=x[k];return{bin1:M.subarray(0,S.nnz),bin2:I.subarray(0,S.nnz),count:x.subarray(0,S.nnz),binOffset:S.binOffset,nnz:S.nnz,sum:h,observed:b==="mixed"&&S.mirrored>0?"complete":b}}const w3=1<<23;function b3(e,t){let n=t;for(;n<e.length&&e[n]!==qt;)n++;return Math.min(e.length,n+1)}function A3(e,t,n,s,o,r,i,a,c,l){let u=t,h=l.out,f=l.line,d=l.sawUpper,m=l.sawLower;for(;u<n;){if(e[u]===qt||e[u]===Zl){u++;continue}if(e[u]===jl){for(;u<n&&e[u]!==qt;)u++;continue}f++;let g=0,p=!1;for(;u<n;){const x=e[u];if(x<Bt||x>Ge)break;g=g*10+(x-Bt),p=!0,u++}if(!p||e[u]!==Sn)throw Oo(o,f,s);u++;let w=0,b=!1;for(;u<n;){const x=e[u];if(x<Bt||x>Ge)break;w=w*10+(x-Bt),b=!0,u++}if(!b||e[u]!==Sn)throw Oo(o,f,s);u++;let y=0,A=!1,N=!1;for(e[u]===wr&&(N=!0,u++);u<n;){const x=e[u];if(x<Bt||x>Ge)break;y=y*10+(x-Bt),A=!0,u++}if(e[u]===c3){u++;let x=.1;for(;u<n;){const v=e[u];if(v<Bt||v>Ge)break;y+=(v-Bt)*x,x/=10,A=!0,u++}}if(e[u]===u3||e[u]===l3){const x=u;u++;let v=1;e[u]===wr?(v=-1,u++):e[u]===a3&&u++;let S=0,k=!1;for(;u<n;){const C=e[u];if(C<Bt||C>Ge)break;S=S*10+(C-Bt),k=!0,u++}k?y*=10**(S*v):u=x}if(!A)throw Oo(o,f,s);for(N&&(y=-y);u<n&&e[u]!==qt;)u++;u++;const M=g-1,I=w-1;if(M<0||I<0||M>=s||I>=s)throw new T("format",`${o} names bin ${g} × ${w} on line ${f}, and the bin file has ${s} bins numbered 1…${s}. The two halves of the pair are for different resolutions, or for different genomes.`,{label:o,line:f,bin1:g,bin2:w,nBins:s});M<I&&(d=!0),M>I&&(m=!0),r[h]=M<=I?M:I,i[h]=M<=I?I:M,a[h]=y,c[h]=M<=I?0:1,h++}return l.out=h,l.line=f,l.sawUpper=d,l.sawLower=m,u}function y3(e,t,n,s,o,r){let i=0,a=0;const c=new Float64Array(r+1);for(let l=0;l<r;l++){const u=o[l],h=o[l+1];c[l]=i;let f=-1;for(let d=u;d<h;d++){if(d>u&&t[d]===t[d-1]){if(s[d]!==f){a++,f=-2;continue}n[i-1]+=n[d];continue}f=s[d],e[i]=l,t[i]=t[d],n[i]=n[d],i++}}return c[r]=i,{nnz:i,binOffset:c,mirrored:a}}function N3(e,t,n){const s=new Int32Array(n+1);for(let r=0;r<t;r++)s[e[r]+1]++;for(let r=0;r<n;r++)s[r+1]+=s[r];const o=new Int32Array(t);for(let r=0;r<t;r++)o[s[e[r]]++]=r;return o}function M3(e,t,n,s){const o=new Float64Array(s+1);for(let a=0;a<n;a++)o[e[a]+1]++;for(let a=0;a<s;a++)o[a+1]+=o[a];const r=Float64Array.from(o),i=new Int32Array(n);for(let a=0;a<n;a++){const c=t[a];i[r[e[c]]++]=c}return{order:i,offsets:o}}function To(e,t){let n=0,s=!1,o=1;for(e[t]===wr&&(o=-1,t++);t<e.length;){const r=e[t];if(r<Bt||r>Ge)break;n=n*10+(r-Bt),s=!0,t++}return{value:n*o,at:t,saw:s}}function Oo(e,t,n){return new T("format",`${e} is not a HiC-Pro matrix: line ${t} is not \`binA binB count\`. The bin file beside it has ${n} bins, and the matrix names pairs of them, tab-separated, one pixel per line.`,{label:e,line:t,nBins:n})}function I3(){const e=new Error("cancelled");return e.name="AbortError",e}const br=["+-","-+","--","++"],$e=[1,2,4,10,20,40],t0=.05,x3=8;function e0(e=x3){const t=1/e,n=Math.ceil(9.001/t),s=new Set([0]);for(let o=0;o<n;o++)s.add(Math.round(10**(o*t)));return[...s].sort((o,r)=>o-r)}const Ma=33,Lo=43,_o=45;function v3(e,t){return e===Lo?t===_o?0:t===Lo?3:-1:e===_o?t===Lo?1:t===_o?2:-1:-1}const k3=1024;class S3{constructor(t,n){this.contigs=t,this.pairTypeColumn=n,this.edges=e0(),this.distFreq=br.map(()=>new Float64Array(this.edges.length)),this.nContigs=t.length,this.densePairs=t.length<=k3?new Float64Array(t.length*t.length):null}total=0;totalUnmapped=0;totalMapped=0;totalDups=0;cis=0;trans=0;cisAtLeast=new Float64Array($e.length);edges;distFreq;shortTypes=new Float64Array(65536);typeOrder=[];longTypes=new Map;nContigs;densePairs;sparsePairs=new Map;namedPairs=new Map;pairSeen=new Map;decoder=new TextDecoder;pairTypeSeen=!1;add(t,n,s,o,r,i,a,c,l,u,h,f,d){this.total++;let m=!1;if(f>=0&&d>f)if(this.pairTypeSeen=!0,d-f===2){const A=t[f]<<8|t[f+1];this.shortTypes[A]===0&&this.typeOrder.push(String.fromCharCode(t[f],t[f+1])),this.shortTypes[A]++,m=A===17476}else{const A=this.decoder.decode(t.subarray(f,d));this.longTypes.has(A)||this.typeOrder.push(A),this.longTypes.set(A,(this.longTypes.get(A)??0)+1)}const g=o-s===1&&t[s]===Ma,p=a-i===1&&t[i]===Ma;if(g&&p){this.totalUnmapped++;return}if(g||p)return;if(this.totalMapped++,m){this.totalDups++;return}let w;if(n>=0&&r>=0){w=n===r;const A=n*this.nContigs+r;if(this.densePairs!==null)this.densePairs[A]===0&&this.notePair(this.contigs[n].name,this.contigs[r].name),this.densePairs[A]++;else{const N=this.sparsePairs.get(A);N===void 0&&this.notePair(this.contigs[n].name,this.contigs[r].name),this.sparsePairs.set(A,(N??0)+1)}}else{const A=n>=0?this.contigs[n].name:this.decoder.decode(t.subarray(s,o)),N=r>=0?this.contigs[r].name:this.decoder.decode(t.subarray(i,a));w=A===N;const M=`${A} ${N}`;this.namedPairs.has(M)||this.notePair(A,N),this.namedPairs.set(M,(this.namedPairs.get(M)??0)+1)}if(!w){this.trans++;return}if(this.cis++,c<0||l<0)return;const b=Math.abs(l-c),y=v3(u,h);if(y>=0){const A=this.edges;let N=0,M=A.length;for(;M-N>1;){const I=N+M>>1;A[I]<=b?N=I:M=I}this.distFreq[y][N]++}for(let A=0;A<$e.length;A++)b>=$e[A]*1e3&&this.cisAtLeast[A]++}notePair(t,n){const s=`${t} ${n}`;this.pairSeen.has(s)||this.pairSeen.set(s,this.pairSeen.size)}result(t){const n=c=>c.length===2&&this.longTypes.get(c)===void 0?this.shortTypes[c.charCodeAt(0)<<8|c.charCodeAt(1)]:this.longTypes.get(c)??0,s=this.typeOrder.map((c,l)=>({name:c,count:n(c),order:l})).sort((c,l)=>l.count-c.count||c.order-l.order).map(({name:c,count:l})=>[c,l]),o=[],r=this.nContigs;if(this.densePairs!==null)for(let c=0;c<this.densePairs.length;c++){const l=this.densePairs[c];l>0&&o.push({name1:this.contigs[Math.floor(c/r)].name,name2:this.contigs[c%r].name,count:l})}else for(const[c,l]of this.sparsePairs)o.push({name1:this.contigs[Math.floor(c/r)].name,name2:this.contigs[c%r].name,count:l});for(const[c,l]of this.namedPairs){const[u,h]=c.split(" ");o.push({name1:u,name2:h,count:l})}const i=c=>this.pairSeen.get(`${c.name1} ${c.name2}`)??0;o.sort((c,l)=>l.count-c.count||i(c)-i(l));const a={};return br.forEach((c,l)=>{a[c]=Array.from(this.distFreq[l])}),{total:this.total,totalUnmapped:this.totalUnmapped,totalSingleSidedMapped:this.total-this.totalMapped-this.totalUnmapped,totalMapped:this.totalMapped,totalDups:this.totalDups,totalNodups:this.totalMapped-this.totalDups,cis:this.cis,trans:this.trans,pairTypes:s,cisAtLeast:$e.map((c,l)=>[c,this.cisAtLeast[l]]),chromFreq:o.map(({name1:c,name2:l,count:u})=>[c,l,u]),distEdges:[...this.edges],distFreq:a,chromsizes:this.contigs.map(c=>[c.name,c.length]),pairTypeColumn:this.pairTypeColumn&&this.pairTypeSeen,scoped:t}}}const Ke=["++","--","-+","+-"];function n0(e,t=t0){const n=u=>e.distFreq[u],s=e.distEdges.length,o=new Float64Array(s);for(let u=0;u<s;u++){let h=0;for(const f of Ke)h+=n(f)[u];o[u]=h/Ke.length}let r=0,i=null;for(const u of Ke){let h=0;for(let f=0;f<s;f++)(o[f]===0?0:Math.abs(n(u)[f]-o[f])/o[f])>t&&(h=f);h>r&&(r=h,i=u)}const a={};let c=0,l=0;for(const u of Ke){let h=0,f=0;n(u).forEach((d,m)=>{m<=r?h+=d:f+=d}),a[u]=h,c+=h,l+=f}return{distance:i===null?null:e.distEdges[r+1]??Number.MAX_SAFE_INTEGER,strands:i,below:a,belowAll:c,aboveAll:l,binIndex:r}}function C3(e,t){if(e===0)return 0;const n=(e-t)/e;return n===0?0:e/(F3(-Math.exp(-1/n)/n)+1/n)}function F3(e){if(e===0)return 0;const t=-Math.exp(-1);if(e<=t)return Number.NaN;const n=Math.sqrt(2*(Math.E*e+1));let s=e<-.25?-1+n-n*n/3+11/72*n*n*n:e;for(let o=0;o<64;o++){const r=Math.exp(s),i=s*r-e,a=s+1;if(a===0)break;const c=i/(r*a-(s+2)*i/(2*a)),l=s-c;if(!Number.isFinite(l))break;if(Math.abs(l-s)<=1e-16*Math.max(1,Math.abs(l)))return l;s=l}return s}function $3(e){const t=u=>({int:u}),n=(u,h)=>h>0?{float:u/h}:t(0),s=(u,h)=>({float:u/h}),o=[["total",t(e.total)],["total_unmapped",t(e.totalUnmapped)],["total_single_sided_mapped",t(e.totalSingleSidedMapped)],["total_mapped",t(e.totalMapped)],["total_dups",t(e.totalDups)],["total_nodups",t(e.totalNodups)],["cis",t(e.cis)],["trans",t(e.trans)]];for(const[u,h]of e.pairTypes)o.push([`pair_types/${u}`,t(h)]);for(const[u,h]of e.cisAtLeast)o.push([`cis_${u}kb+`,t(h)]);o.push(["summary/frac_cis",n(e.cis,e.totalNodups)]);for(const[u,h]of e.cisAtLeast)o.push([`summary/frac_cis_${u}kb+`,n(h,e.totalNodups)]);o.push(["summary/frac_dups",n(e.totalDups,e.totalMapped)]);const r=C3(e.totalMapped,e.totalDups);o.push(["summary/complexity_naive",e.totalMapped===0||r===0?t(0):{float:r}]);const i=n0(e),a="summary/dist_freq_convergence";o.push([`${a}/convergence_dist`,i.distance===null?{text:"0"}:t(i.distance)]),o.push([`${a}/strands_w_max_convergence_dist`,{text:i.strands??"??"}]),o.push([`${a}/convergence_rel_diff_threshold`,{float:t0}]);for(const u of Ke)o.push([`${a}/n_cis_pairs_below_convergence_dist/${u}`,t(i.below[u])]);o.push([`${a}/n_cis_pairs_below_convergence_dist_all_strands`,t(i.belowAll)]),o.push([`${a}/n_cis_pairs_above_convergence_dist_all_strands`,t(i.aboveAll)]);const c=[["cis",e.cis],["total_mapped",e.totalMapped],["total_nodups",e.totalNodups]];for(const[u,h]of c){let f=0;for(const d of Ke){const m=i.below[d]/h;f+=m,o.push([`${a}/frac_${u}_in_cis_below_convergence_dist/${d}`,s(i.below[d],h)])}o.push([`${a}/frac_${u}_in_cis_below_convergence_dist_all_strands`,{float:f}]),o.push([`${a}/frac_${u}_in_cis_above_convergence_dist_all_strands`,s(i.aboveAll,h)])}for(const[u,h,f]of e.chromFreq)o.push([`chrom_freq/${u}/${h}`,t(f)]);const l=e.distEdges;for(let u=0;u<l.length;u++){const h=u<l.length-1?`${l[u]}-${l[u+1]}`:`${l[u]}+`;for(const f of br)o.push([`dist_freq/${h}/${f}`,t(e.distFreq[f][u])])}for(const[u,h]of e.chromsizes)o.push([`chromsizes/${u}`,t(h)]);return o}function E3(e){return"text"in e?e.text:"int"in e?String(e.int):B3(e.float)}function B3(e){if(Number.isNaN(e))return"nan";if(e===1/0)return"inf";if(e===-1/0)return"-inf";if(e===0)return Object.is(e,-0)?"-0.0":"0.0";const[t,n]=e.toExponential().split("e"),s=Number(n),o=t.startsWith("-"),r=t.replace("-","").replace(".",""),i=o?"-":"";if(s<-4||s>=16){const l=r.length>1?`${r[0]}.${r.slice(1)}`:r,u=Math.abs(s);return`${i}${l}e${s<0?"-":"+"}${u<10?`0${u}`:u}`}if(s<0)return`${i}0.${"0".repeat(-s-1)}${r}`;const a=r.slice(0,s+1).padEnd(s+1,"0"),c=r.slice(s+1);return`${i}${a}.${c===""?"0":c}`}const T3=9,O3=35,Do=1<<20;class L3{unique=new Map;ambiguous=new Map;byName=new Map;decoder=new TextDecoder;constructor(t){for(let n=0;n<t.length;n++){const s=t[n].name;this.byName.set(s,n);const o=_3(s),r=this.unique.get(o);if(r===void 0&&!this.ambiguous.has(o)){this.unique.set(o,n);continue}const i=this.ambiguous.get(o)??[];r!==void 0&&(i.push([t[r].name,r]),this.unique.delete(o)),i.push([s,n]),this.ambiguous.set(o,i)}}lookup(t,n,s){let o=2166136261;for(let c=n;c<s;c++)o^=t[c],o=Math.imul(o,16777619);o|=0;const r=this.unique.get(o);if(r!==void 0)return r;const i=this.ambiguous.get(o);if(i===void 0)return-1;const a=this.decoder.decode(t.subarray(n,s));for(const[c,l]of i)if(c===a)return l;return-1}of(t){return this.byName.get(t)??-1}}function _3(e){let t=2166136261;const n=new TextEncoder().encode(e);for(let s=0;s<n.length;s++)t^=n[s],t=Math.imul(t,16777619);return t|0}const D3=1.05;function R3(e){const t=[0,1];let n=1;for(;n<e;)n=Math.max(n+1,Math.round(n*D3)),t.push(n);return Float64Array.from(t)}function z3(e,t){return e===Ro?t===Ro?0:t===zo?1:-1:e===zo?t===Ro?2:t===zo?3:-1:-1}const Ro=43,zo=45;async function Jr(e,t){const{binSize:n,contigs:s,columns:o}=t,r=t.oneBased??!0,i=Pl(s,n),a=ql(s,n),c=new L3(s),l=s.length,u=o.strand1>=0&&o.strand2>=0,h=o.pairType??-1,f=Math.max(o.chrom1,o.chrom2,o.pos1,o.pos2,...u?[o.strand1,o.strand2]:[],h),d=new S3(s,h>=0),m=t.onRecord;let g=0;for(const Q of s)g=Math.max(g,Q.length);const p=u?R3(g):null,w=p===null?null:[new Float64Array(p.length-1),new Float64Array(p.length-1),new Float64Array(p.length-1),new Float64Array(p.length-1)];let b=0;const y=new Int32Array(s.length);for(let Q=0;Q<s.length;Q++)y[Q]=Math.max(0,Math.ceil(s[Q].length/n)-1);const A=new Int32Array(a);let N=new Int32Array(Math.min(a,65536)),M=0,I=new Int32Array(Do),x=new Int32Array(Do),v=new Int32Array(Do),S=0,k=-1,C=0,B=0,F=0,$=0,_=0,E=!1,O=!1,L=-1;const D=new Set;let q=!1,X=!1;const R=new Uint8Array(s.length);let P=null;const z=t.maxPixelBytes===void 0?1/0:Math.floor(t.maxPixelBytes/12),U=()=>{if(M===0)return;const Q=N.subarray(0,M);if(Q.sort(),S+M>z)throw P=new T("dense-too-large",`Binning this file at ${n} bp produces more stored pixels than the memory budget allows. Choose a coarser base resolution, or import fewer sequences.`,{binSize:n,nnz:S,limitBytes:t.maxPixelBytes}),P;for(;S+M>I.length;)I=is(I),x=is(x),v=is(v);for(let W=0;W<M;W++){const et=Q[W];I[S]=k,x[S]=et,v[S]=A[et],S++,A[et]=0}M=0},K=t.expectedRecords??0,G=Q=>{if(Q.length===0||Q[0]===O3)return;let W=0,et=0,j=-1,ot=-1,at=-1,ht=-1,ft=-1,kt=-1,pt=0,Ft=0,gt=0,yt=0,Nt=-1,oe=-1;const Kt=Q.length;for(let it=0;it<=Kt;it++)if(!(it!==Kt&&Q[it]!==T3)){if(W===o.chrom1?(j=c.lookup(Q,et,it),pt=et,Ft=it):W===o.chrom2?(ot=c.lookup(Q,et,it),gt=et,yt=it):W===h?(Nt=et,oe=it):W===o.pos1?at=Ia(Q,et,it):W===o.pos2?ht=Ia(Q,et,it):u&&W===o.strand1?ft=it>et?Q[et]:-1:u&&W===o.strand2&&(kt=it>et?Q[et]:-1),W++,W>f)break;et=it+1}if(d.add(Q,j,pt,Ft,ot,gt,yt,at,ht,ft,kt,Nt,oe),m!==void 0&&m(Q,j,pt,Ft,ot,gt,yt,at,ht,ft,kt,Nt,oe),j<0||ot<0){F++;return}if(at<0||ht<0){$++;return}if(w!==null&&p!==null&&j===ot){const it=z3(ft,kt);if(it>=0){const Lt=Math.abs(ht-at);let fe=0,de=p.length-1;for(;fe+1<de;){const re=fe+de>>1;p[re]<=Lt?fe=re:de=re}w[it][fe]++,b++}}let wt=Y(at),Et=Y(ht);if(wt>y[j]&&(wt=y[j],_++),Et>y[ot]&&(Et=y[ot],_++),wt+=i[j],Et+=i[ot],wt>Et){const it=wt;wt=Et,Et=it}j===ot&&(R[j]=1);let cn=!1;if(!O){const it=j<=ot?j*l+ot:ot*l+j;it!==L&&(cn=!0,L>=0&&D.add(L),D.has(it)&&(O=!0,D.clear()),L=it)}wt!==k&&(wt<k&&(E=!0,cn||(O=!0)),U(),k=wt),A[Et]===0&&(M===N.length&&(N=is(N)),N[M++]=Et),A[Et]++,C++,B++};function Y(Q){const W=r?Q-1:Q;return W<0?0:Math.floor(W/n)}const V=t.spans??[{from:0,to:e.size}],J=V.reduce((Q,W)=>Q+Math.max(0,W.to-W.from),0);let Z=0;try{for(const Q of V){const W={from:Q.from,to:Q.to,onProgress:j=>{K>0?t.onProgress?.(C,K,"reading records"):t.onProgress?.(Z+j,J,"reading records")}};t.cancelled!==void 0&&(W.cancelled=t.cancelled),t.spans===void 0&&(W.onFraming=j=>{X=!0,(j.partialMember||!j.endedOnEofMarker)&&(q=!0)});const et=await Vr(Kr(e,W),G,t.cancelled);if(Z+=Math.max(0,Q.to-Q.from),!et||t.cancelled?.()===!0)return null}U()}catch(Q){throw P!==null?P:Q}const ut={binSize:n,contigs:[...s],chromOffset:i,nBins:a,bin1:I.subarray(0,S),bin2:x.subarray(0,S),count:v.subarray(0,S),nnz:S,sum:B,skippedUnknownContig:F,skippedBadPosition:$,clampedPosition:_,wasUnsorted:O,truncated:X&&q,contigsWithoutCisContacts:s.filter((Q,W)=>R[W]===0).map(Q=>Q.name),orientation:p===null||w===null?null:{edges:p,counts:w,total:b},stats:d.result(t.spans!==void 0)};return E?P3(ut):ut}function is(e){const t=new Int32Array(e.length*2);return t.set(e),t}function Ia(e,t,n){if(n<=t)return-1;let s=0;for(let o=t;o<n;o++){const r=e[o]-48;if(r<0||r>9)return-1;s=s*10+r}return s}function P3(e){const{nnz:t,nBins:n}=e,s=new Int32Array(n+1);for(let h=0;h<t;h++)s[e.bin1[h]+1]++;for(let h=0;h<n;h++)s[h+1]+=s[h];const o=s.slice(0,n),r=new Int32Array(t),i=new Int32Array(t);for(let h=0;h<t;h++){const f=o[e.bin1[h]]++;r[f]=e.bin2[h],i[f]=e.count[h]}const a=new Int32Array(t),c=new Int32Array(t),l=new Int32Array(t);let u=0;for(let h=0;h<n;h++){const f=s[h],d=s[h+1];if(d<=f)continue;let m=!0;for(let w=f+1;w<d;w++)if(r[w]<r[w-1]){m=!1;break}const g=m?null:Array.from({length:d-f},(w,b)=>f+b);g?.sort((w,b)=>r[w]-r[b]);let p=-1;for(let w=0;w<d-f;w++){const b=g===null?f+w:g[w],y=r[b];if(y===p){l[u-1]+=i[b];continue}a[u]=h,c[u]=y,l[u]=i[b],u++,p=y}}return{...e,bin1:a.subarray(0,u),bin2:c.subarray(0,u),count:l.subarray(0,u),nnz:u}}const s0=[1,2,5,10,20,50,100,200,500,1e3,2e3,5e3,1e4],q3=s0.length;function U3(e,t){const n=Math.max(1,Math.min(t,q3));return s0.slice(0,n).map(s=>e*s)}function G3(e,t){const n=new Int32Array(t);for(let s=0;s+1<e.length;s++){const o=e[s],r=e[s+1];for(let i=o;i<r&&i<t;i++)n[i]=s}return n}async function H3(e,t,n={}){if(t===e.binSize)return e;if(t%e.binSize!==0)throw new Error(`A ladder rung must be a whole multiple of the base: ${t} is not a multiple of ${e.binSize}.`);const s=t/e.binSize,o=e.contigs,r=Pl(o,t),i=ql(o,t),a=G3(e.chromOffset,e.nBins),c=A=>{const N=a[A];return r[N]+Math.floor((A-e.chromOffset[N])/s)},l=new Int32Array(i);let u=new Int32Array(Math.min(i,65536)),h=0,f=new Int32Array(Math.min(e.nnz,1<<20)||1),d=new Int32Array(f.length),m=new Int32Array(f.length),g=0,p=-1,w=0;const b=()=>{if(h===0)return;const A=u.subarray(0,h);for(A.sort();g+h>f.length;)f=as(f),d=as(d),m=as(m);for(let N=0;N<h;N++){const M=A[N];f[g]=p,d[g]=M,m[g]=l[M],g++,l[M]=0}h=0},y=1<<20;for(let A=0;A<e.nnz;A+=y){if(n.cancelled?.()===!0)return null;const N=Math.min(A+y,e.nnz);for(let M=A;M<N;M++){const I=c(e.bin1[M]),x=c(e.bin2[M]);I!==p&&(b(),p=I),l[x]===0&&(h===u.length&&(u=as(u)),u[h++]=x),l[x]+=e.count[M],w+=e.count[M]}n.onProgress?.(N,e.nnz,`aggregating to ${t} bp`),await vt()}return b(),{binSize:t,contigs:[...o],chromOffset:r,nBins:i,bin1:f.subarray(0,g),bin2:d.subarray(0,g),count:m.subarray(0,g),nnz:g,sum:w,skippedUnknownContig:e.skippedUnknownContig,skippedBadPosition:e.skippedBadPosition,clampedPosition:e.clampedPosition,wasUnsorted:e.wasUnsorted,truncated:e.truncated,contigsWithoutCisContacts:[...e.contigsWithoutCisContacts]}}function as(e){const t=new Int32Array(Math.max(1,e.length*2));return t.set(e),t}const Q3=[{magic:"PX2.004",wideLinecount:!0},{magic:"PX2.003",wideLinecount:!1},{magic:"PX2.002",wideLinecount:!1}],xa=[36,40,44];function K3(e){if(e.length<20)throw new T("format","This .px2 index is too short to hold a header.",{bytes:e.length});const t=new DataView(e.buffer,e.byteOffset,e.byteLength),n=String.fromCharCode(...e.subarray(0,8)),s=Q3.find(w=>w.magic===n);if(s===void 0)throw new T("format","This is not a pairix index: the magic bytes are not PX2. A `.tbi` cannot be used for a pairs file — build one with `pairix <file>.pairs.gz`.",{magic:n.replace(/[^\x20-\x7e]/g,"?")});const o=t.getInt32(8,!0);let r=12,i;s.wideLinecount?(i=Number(t.getBigUint64(r,!0)),r+=8):(i=t.getUint32(r,!0),r+=4);const a=V3(t,e,r,o),{splitChar:c,metaChar:l,lineSkip:u,namesAt:h,nameBytes:f}=a,d=[],m=new TextDecoder().decode(e.subarray(h,h+f));for(const w of m.split("\0"))w!==""&&d.push(w);let g=h+f;const p=new Map;for(let w=0;w<o&&!(g+4>e.length);w++){const b=t.getInt32(g,!0);g+=4;let y=Number.POSITIVE_INFINITY,A=Number.NEGATIVE_INFINITY;for(let M=0;M<b&&!(g+8>e.length);M++){g+=4;const I=t.getInt32(g,!0);g+=4;for(let x=0;x<I&&!(g+16>e.length);x++){const v=Number(t.getBigUint64(g,!0)>>16n),S=Number(t.getBigUint64(g+8,!0)>>16n);g+=16,v<y&&(y=v),S>A&&(A=S)}}if(g+4>e.length)break;const N=t.getInt32(g,!0);g+=4;for(let M=0;M<N&&!(g+8>e.length);M++){const I=Number(t.getBigUint64(g,!0)>>16n);g+=8,I>0&&I<y&&(y=I)}Number.isFinite(y)&&Number.isFinite(A)&&w<d.length&&p.set(d[w],{from:y,to:A})}return{keys:d,spans:p,linecount:i,splitChar:c,metaChar:l,lineSkip:u}}function V3(e,t,n,s){const o=[];for(const r of xa){const i=n+r;if(i+4>t.length)continue;const a=e.getInt32(i,!0),c=i+4;if(o.push(a),a<=0||c+a>t.length||new TextDecoder().decode(t.subarray(c,c+a)).split("\0").filter(h=>h!=="").length!==s)continue;const u=n+28;return{splitChar:String.fromCharCode(t[u+1]||124),metaChar:String.fromCharCode(t[u+2]||35),lineSkip:e.getInt32(n+32,!0),namesAt:c,nameBytes:a}}throw new T("format",`This .px2 index has a header this reader cannot lay out: no candidate config size produced a coherent name block for ${s} references. Rebuild it with \`pairix <file>.pairs.gz\`, or open the file without an index.`,{nRef:s,candidates:xa,sawNameBytes:o})}async function Y3(e){const t=await e();if(t===null)return null;try{const n=t.readBytes(0,t.size);return K3(await Vl(n))}finally{t.close()}}function W3(e,t,n){const s=[];for(const r of t){const i=e.spans.get(r);i!==void 0&&s.push({from:i.from,to:X3(n,i.to)})}s.sort((r,i)=>r.from-i.from);const o=[];for(const r of s){const i=o[o.length-1];i!==void 0&&r.from<=i.to+(1<<20)?i.to=Math.max(i.to,r.to):o.push({...r})}return o}function X3(e,t){if(t>=e.size)return e.size;const n=e.readBytes(t,Math.min(pa,e.size-t)),s=Xr(n);return s===null?Math.min(e.size,t+pa):Math.min(e.size,t+s)}async function J3(e,t){const n=await js(e);let s=null;try{s=await Y3(t)}catch(r){if(!(r instanceof T))throw r;s=null}let o=0;for(const r of n.contigs)o+=r.length;return{header:n,index:s,records:s?.linecount??null,genomeLength:o,sourceBytes:e.size}}async function Z3(e,t,n){const s=U3(n.base,n.rungs),o=n.maxPixelBytes??wl(),r=.85,i=(h,f)=>{n.onProgress?.(Math.round(h*1e3),1e3,f)},a=n.keys===void 0?void 0:j3(t,n.keys,e),c={binSize:n.base,contigs:t.header.contigs,columns:t.header.columns,oneBased:n.oneBased??!0,maxPixelBytes:o,onProgress:(h,f,d)=>{i(f>0?h/f*r:0,d??"reading records")}};a!==void 0&&(c.spans=a),t.records!==null&&(c.expectedRecords=t.records),n.cancelled!==void 0&&(c.cancelled=n.cancelled);const l=await Jr(e,c);if(l===null)return null;const u=new Map([[n.base,l]]);for(let h=1;h<s.length;h++){const f=s[h],d=await H3(l,f,{onProgress:(m,g)=>{const p=g>0?m/g:1,w=(h-1+p)/Math.max(1,s.length-1);i(r+w*(1-r),`building the ${td(f)} rung`)},...n.cancelled===void 0?{}:{cancelled:n.cancelled}});if(d===null)return null;u.set(f,d)}return i(1,"done"),{header:t.header,rungs:s,tables:u,indexed:t.index!==null,declaredRecords:t.records}}function j3(e,t,n){if(e.index===null)return;const s=W3(e.index,t,n);return s.length===0?void 0:s}function td(e){return e>=1e6&&e%1e6===0?`${e/1e6} Mb`:e>=1e3&&e%1e3===0?`${e/1e3} kb`:`${e} bp`}async function o0(e,t,n={}){const s=await id(t);return s===null?await ed(e,t,n):await od(e,t,s,n)}async function ed(e,t,n){const s=n.detached===!0?await pr(t):await Y5(t,{...n.report===void 0?{}:{onProgress:(r,i)=>n.report?.(r,i,"downloading")},...n.cancelled===void 0?{}:{cancelled:n.cancelled},...n.report===void 0||!va(e)?{}:{onRead:(r,i)=>n.report?.(i,0,`listing cells · ${r.toLocaleString()} reads`)}}),o=t.kind==="remote"&&t.reading!==void 0;va(e)&&n.report?.(0,0,"listing cells");try{return{mc:await bt.open(e,s,{...o?{listingAgreed:!0}:{},...n.metacool===void 0?{}:{metacool:n.metacool}}),source:s}}catch(r){const i=n.binning;if(i===void 0||!(r instanceof T)||r.code!=="pairs-binning-needed")throw r;return{mc:await nd(e,t,s,i,n),source:s}}}async function nd(e,t,n,s,o){const r=await J3(n,()=>W5(t)),i=await Z3(n,r,{base:s.base,rungs:s.rungs,oneBased:s.oneBased??!0,...o.report===void 0?{}:{onProgress:(c,l,u)=>o.report?.(c,l,u??"binning records")},...o.cancelled===void 0?{}:{cancelled:o.cancelled}});if(i===null)throw sd();const a=i.tables.get(i.rungs[0]);if(a===void 0)throw new T("format",`${e} imported no pixel table.`);return bt.fromPairs({uri:e,header:i.header,pixels:a,rungs:i.rungs,indexed:i.indexed,declaredRecords:i.declaredRecords})}function sd(){const e=new Error("open cancelled");return e.name="AbortError",e}async function od(e,t,n,s){const o=Ar(n),r=Ar(t),i=u=>({...s.report===void 0?{}:{onProgress:(h,f)=>s.report?.(h,f,u)},...s.cancelled===void 0?{}:{cancelled:s.cancelled}}),a=await ya(n,i("reading the bins")),c=h3(a,o);await ld(t,r,c.nBins);const l=await ya(t,i("downloading the matrix"));return rd(e,{bytes:l,name:r},c,o,a.length,s)}async function rd(e,t,n,s,o,r={}){const i=await p3(t.bytes,n.nBins,t.name,{...r.cancelled===void 0?{}:{cancelled:r.cancelled},...r.report===void 0?{}:{onProgress:(c,l)=>r.report?.(c,l,"reading pixels")}});return{mc:bt.fromHicPro(e,{bins:n,pixels:i,matrixName:t.name,binsName:s}),source:hd(t.name,o+t.bytes.length)}}async function id(e){const t=Ar(e);if(!i3(t))return null;if(e.bins!==void 0)return e.bins;if(e.kind==="remote"){const n=await ad(e.url,e.credential,e.headers);if(n!==null)return n}throw new T("hicpro-no-bins",`${t} is a HiC-Pro matrix, which is half of a contact map: the bin file beside it — ${Ts(t).join(" or ")} — is what says which genomic interval each bin id means, and there are no coordinates without it. Drop the two together, pick the matrix out of a connected folder so its bin file comes with it, or name the bin file on the URL as \`<matrix>#bins=<url>\`.`,{matrix:t,wanted:Ts(t)})}async function ad(e,t,n){if(Es(e))return null;const s=e.lastIndexOf("/"),o=s<0?"":e.slice(0,s+1),r=e.slice(s+1),i=Ts(r).map(c=>`${o}${c}`);if(o.includes("/iced/"))for(const c of Ts(r))i.push(`${o.replace("/iced/","/raw/")}${c}`);const a={...t===void 0?{}:{credential:t},...n===void 0?{}:{headers:n}};for(const c of i)try{if((await fetch(c,{method:"HEAD",headers:Te({url:c,method:"HEAD",...a})})).ok)return{kind:"remote",url:c,...a}}catch{}return null}function va(e){return/\.scool(\.gz)?(\?|#|$)/i.test(e)}const cd=200<<20;async function ld(e,t,n){if(e.reading!==void 0)return;const s=await ud(e);if(s===null||s<=cd)return;const o=Math.round(s/15);throw new T("hicpro-load-cost",`${t} is ${Math.round(s/(1<<20))} MB of text, and a HiC-Pro matrix has no index — so the whole of it is read and turned into about ${o.toLocaleString()} pixels over ${n.toLocaleString()} bins before anything can be drawn. That is the only cost: afterwards every region is instant and panning is free.`,{matrix:t,bytes:s,pixels:o,nBins:n})}async function ud(e){if(e.kind==="local")return e.file.size;if(Es(e.url))return null;try{const t=await fetch(e.url,{method:"HEAD",headers:Te({url:e.url,method:"HEAD",...e.credential===void 0?{}:{credential:e.credential},...e.headers===void 0?{}:{headers:e.headers}})}),n=Number(t.headers.get("Content-Length")??Number.NaN);return Number.isFinite(n)?n:null}catch{return null}}function Ar(e){const t=e.kind==="local"?e.file.name:e.url;return t.split(/[\\/]/).pop()?.split(/[?#]/)[0]??t}function hd(e,t){return{id:e,size:t,isRemote:!1,mount:()=>Promise.resolve(e),readBytes(){throw new T("unsupported",`${e} was read whole and parsed at open; there are no bytes left to read from it. A read reaching here is a code path that bypassed the parsed arrays.`,{id:e})},stats:()=>({requests:1,bytes:t}),close:()=>{}}}const Zr=3;function r0(e,t){const n=Math.min(e.length,t.length);let s=0,o=0,r=0;for(let h=0;h<n;h++){const f=e[h],d=t[h];Number.isNaN(f)||Number.isNaN(d)||(o+=f,r+=d,s++)}if(s<Zr)return Number.NaN;const i=o/s,a=r/s;let c=0,l=0,u=0;for(let h=0;h<n;h++){const f=e[h],d=t[h];if(Number.isNaN(f)||Number.isNaN(d))continue;const m=f-i,g=d-a;c+=m*g,l+=m*m,u+=g*g}return l<=0||u<=0?Number.NaN:c/Math.sqrt(l*u)}function i0(e,t){const n=Math.min(e.length,t.length);let s=0;for(let a=0;a<n;a++)!Number.isNaN(e[a])&&!Number.isNaN(t[a])&&s++;if(s<Zr)return Number.NaN;const o=new Float64Array(s),r=new Float64Array(s);let i=0;for(let a=0;a<n;a++){const c=e[a],l=t[a];Number.isNaN(c)||Number.isNaN(l)||(o[i]=c,r[i]=l,i++)}return r0(ka(o),ka(r))}function ka(e){const t=e.length,n=new Float64Array(t),s=[];for(let r=0;r<t;r++)Number.isNaN(e[r])?n[r]=Number.NaN:s.push(r);s.sort((r,i)=>e[r]-e[i]);let o=0;for(;o<s.length;){let r=o+1;for(;r<s.length&&e[s[r]]===e[s[o]];)r++;const i=(o+r+1)/2;for(let a=o;a<r;a++)n[s[a]]=i;o=r}return n}function fd(e,t){const n=Math.max(1,Math.trunc(t)),s=gd(e);if(s.length===0)return new Float64Array(0);const o=new Float64Array(n+1);for(let r=0;r<=n;r++)o[r]=pd(s,r/n);return o}const dd=16384;function md(e,t,n){if(!Number.isFinite(e)||!Number.isFinite(t)||e<=0||t<=e)return new Float64Array(0);const s=Number.isFinite(n)&&n>1?n:1+1e-6,o=[e];let r=e;for(;r<t&&o.length<dd;)r*=s,o.push(r);return Float64Array.from(o)}function gd(e){let t=0;for(let o=0;o<e.length;o++)Number.isNaN(e[o])||t++;const n=new Float64Array(t);let s=0;for(let o=0;o<e.length;o++){const r=e[o];Number.isNaN(r)||(n[s]=r,s++)}return n.sort(),n}function pd(e,t){const n=e.length;if(n===0)return Number.NaN;if(n===1)return e[0];const s=Math.min(1,Math.max(0,t))*(n-1),o=Math.floor(s),r=Math.min(n-1,o+1),i=s-o;return e[o]+i*(e[r]-e[o])}class Bn{maxOffset;sum;count;lap;constructor(t,n=0){this.maxOffset=Math.max(0,Math.trunc(t)),this.sum=new Float64Array(this.maxOffset+1),this.count=new Float64Array(this.maxOffset+1),this.lap=Math.max(0,Math.trunc(n))}separation(t,n){const s=Math.abs(t-n);return this.lap>0?Math.min(s,this.lap-s):s}add(t,n){if(t<0||t>this.maxOffset||Number.isNaN(n))return;const s=t|0;this.sum[s]+=n,this.count[s]+=1}addBlock(t){const{nrows:n,ncols:s,values:o}=t,r=t.rowExtent[0],i=t.colExtent[0];for(let a=0;a<n;a++){const c=r+a,l=a*s;for(let u=0;u<s;u++){const h=o[l+u];if(Number.isNaN(h))continue;const f=this.separation(i+u,c);f>this.maxOffset||(this.sum[f]+=h,this.count[f]+=1)}}}merge(t){const n=Math.min(this.sum.length,t.sum.length);for(let s=0;s<n;s++)this.sum[s]+=t.sum[s],this.count[s]+=t.count[s]}finish(){const t=new Float64Array(this.sum.length);for(let n=0;n<t.length;n++)t[n]=this.count[n]>0?this.sum[n]/this.count[n]:Number.NaN;return{maxOffset:this.maxOffset,sum:this.sum.slice(),count:this.count.slice(),mean:t}}}function jr(e,t,n,s){const o=t>0?t:1,r=Number.isFinite(s)?s:0,i=Math.max(1,Math.ceil(r)),a=md(i,e.maxOffset+1,n),c=[],l=[],u=[];for(let h=0;h+1<a.length;h++){const f=Math.max(i,Math.ceil(a[h])),d=Math.min(e.maxOffset+1,Math.ceil(a[h+1]));let m=0,g=0,p=0;for(let w=f;w<d;w++){const b=e.count[w];b<=0||(m+=e.sum[w],g+=b,p+=b*w)}g<=0||(c.push(p/g*o),l.push(m/g),u.push(g))}return{x:Float64Array.from(c),y:Float64Array.from(l),n:Float64Array.from(u)}}function a0(e,t){const n=Math.min(e.length,t.length),s=Math.max(0,n-2),o=new Float64Array(s),r=new Float64Array(s);for(let i=1;i+1<n;i++){o[i-1]=e[i];const a=e[i-1],c=e[i+1],l=t[i-1],u=t[i+1],h=Math.log(c)-Math.log(a),f=a>0&&c>0&&l>0&&u>0&&h!==0;r[i-1]=f?(Math.log(u)-Math.log(l))/h:Number.NaN}return{x:o,y:r}}function ti(e,t){if(!Number.isFinite(t)||t<0||t>e.maxOffset)return Number.NaN;const{mean:n}=e,s=Math.floor(t);if(s===t&&!Number.isNaN(n[s]))return n[s];let o=s;for(;o>=0&&Number.isNaN(n[o]);)o--;let r=Math.ceil(t);for(;r<=e.maxOffset&&Number.isNaN(n[r]);)r++;if(o<0||r>e.maxOffset)return Number.NaN;const i=(t-o)/(r-o);return n[o]+i*(n[r]-n[o])}function wd(e,t){const n=Number.isFinite(e)&&e>1?e:1.1,s=Number.isFinite(t)&&t>0?t:0,o=[];let r=-1;for(let a=0;o.length<4096;a++){const c=Math.round(Math.pow(n,a));if(c!==r&&(o.push(c),r=c,c>s&&o.length>=2))break}const i=new Float64Array(o.length-1);for(let a=0;a+1<o.length;a++)i[a]=o[a+1]-o[a];return{pos:Float64Array.from(o),width:i}}function bd(e,t){const n=e.length-2;if(n<0||t<e[0])return 0;let s=0,o=n;for(;s<o;){const r=s+o+1>>1;e[r]<=t?s=r:o=r-1}return s}function Ad(e,t,n,s,o){const r=t>0?t:1,i=Math.max(0,Math.ceil(Number.isFinite(s)?s:0)),a=o?2:1,c=wd(n,Math.max(0,e.maxOffset-1)*r),l=c.width.length,u=new Float64Array(l),h=new Float64Array(l),f=new Int32Array(l);let d=0;for(let w=i;w<=e.maxOffset;w++){const b=bd(c.pos,(w-1)*r);f[b]+=1;const y=e.count[w];if(!(y>0))continue;const A=w===0?1:a,N=e.sum[w]/A;Number.isNaN(N)||(u[b]+=N,h[b]+=y/A,d+=N)}if(!(d>0))return{x:new Float64Array(0),y:new Float64Array(0),n:new Float64Array(0)};const m=[],g=[],p=[];for(let w=0;w<l;w++)h[w]<=0||(m.push(c.pos[w]),g.push(u[w]/d/(f[w]*r)),p.push(h[w]));return{x:Float64Array.from(m),y:Float64Array.from(g),n:Float64Array.from(p)}}const yd="AGFzbQEAAAABEAFgDH9/f39/f39/f39/fwACDwEDZW52Bm1lbW9yeQIAAgMDAgAABAUBcAEBAQYIAX8BQYCIBAsHFgIEZ3JhbQAAC2dyYW1fbWFza2VkAAEKthICxQoIE38DewF/BXsGfwV7AX8NfAJAIAYgB04NACACQQR0IQwgAkECdCENIAJBA3QhDiAKQQJ0IQ8gACAGIAJsQQJ0aiEQIApBA2ogC04hEQNAAkAgCCAGIAggBkobIhIgCU4NACAAIAZBAWoiEyAGIBMgB0gbIhQgAmxBAnRqIRUgAyAUIAVrIARsQQN0aiEWIAMgBiAFayAEbEEDdGohFyABIA0gEmxqIRgDQCABIBJBA2oiGSASIBkgCUgbIAJsQQJ0aiEaIAEgEkECaiIbIBIgGyAJSBsgAmxBAnRqIRwgASASQQFqIh0gEiAdIAlIGyACbEECdGohHgJAAkAgEUUNAP0MAAAAAAAAAAAAAAAAAAAAACIfISAgHyEhIAohIiAfISMgHyEkIB8hJSAfISYgHyEnDAELIBohFCAcISggHiEpIBghKiAVISsgECEs/QwAAAAAAAAAAAAAAAAAAAAAIichJiAnISUgJyEkICchIyAKIS0gJyEhICchICAnIR8DQCAhICsgD2r9AAAAIi4gFCAPav0AAAAiL/3mAf3kASEhICAgLiAoIA9q/QAAACIw/eYB/eQBISAgHyAuICkgD2r9AAAAIjH95gH95AEhHyAjIC4gKiAPav0AAAAiMv3mAf3kASEjICQgLCAPav0AAAAiLiAv/eYB/eQBISQgJSAuIDD95gH95AEhJSAmIC4gMf3mAf3kASEmICcgLiAy/eYB/eQBIScgFEEQaiEUIChBEGohKCApQRBqISkgKkEQaiEqICtBEGohKyAsQRBqISwgLUEHaiEzIC1BBGoiIiEtIDMgC0gNAAsLICH9HwC7ICH9HwG7oCAh/R8Cu6AgIf0fA7ugITQgIP0fALsgIP0fAbugICD9HwK7oCAg/R8Du6AhNSAf/R8AuyAf/R8Bu6AgH/0fArugIB/9HwO7oCE2ICP9HwC7ICP9HwG7oCAj/R8Cu6AgI/0fA7ugITcgJP0fALsgJP0fAbugICT9HwK7oCAk/R8Du6AhOCAl/R8AuyAl/R8Bu6AgJf0fArugICX9HwO7oCE5ICb9HwC7ICb9HwG7oCAm/R8Cu6AgJv0fA7ugITogJ/0fALsgJ/0fAbugICf9HwK7oCAn/R8Du6AhOwJAIAsgIkwNACAiQQJ0IRQgCyAiayErIBAhKCAVISkgGCEqA0AgKSAUaioCALsiPCAaIBRqKgIAuyI9oiA0oCE0IDwgHCAUaioCALsiPqIgNaAhNSA8IB4gFGoqAgC7Ij+iIDagITYgPCAqIBRqKgIAuyJAoiA3oCE3ICggFGoqAgC7IjwgPaIgOKAhOCA8ID6iIDmgITkgPCA/oiA6oCE6IDwgQKIgO6AhOyAoQQRqISggKUEEaiEpICpBBGohKiAeQQRqIR4gHEEEaiEcIBpBBGohGiArQX9qIisNAAsLIBcgEkEDdCIaaiIUIDsgFCsDAKA5AwACQCAdIAlOIhQNACAXIB1BA3RqIhwgOiAcKwMAoDkDAAsCQCAbIAlOIhwNACAXIBtBA3RqIh4gOSAeKwMAoDkDAAsCQCAZIAlOIh4NACAXIBlBA3RqIiggOCAoKwMAoDkDAAsCQCATIAdODQAgFiAaaiIaIDcgGisDAKA5AwACQCAUDQAgFiAdQQN0aiIUIDYgFCsDAKA5AwALAkAgHA0AIBYgG0EDdGoiFCA1IBQrAwCgOQMACyAeDQAgFiAZQQN0aiIUIDQgFCsDAKA5AwALIBggDGohGCASQQRqIhIgCUgNAAsLIBAgDmohECAGQQJqIgYgB0gNAAsLC+wHBhR/C3sBfwZ8An0CfAJAIAYgB04NACAAIAYgAWxBAnQiDCAKQQJ0Ig1qaiEOIAFBAnQhDyAAIA1qIRAgACAMaiERIARBBWxBA3QhEiAEQQJ0QQN0IRMgBEEDbEEDdCEUIARBAXRBA3QhFSAKQQNqIAtOIRYDQAJAIAggBiAIIAZKGyIXIAlODQAgECAPIBdsIgFqIRggACABaiEZIAIgBiAFayADbEEDdGoiGiASaiEbIBogE2ohHCAaIBRqIR0gGiAVaiEeA0ACQAJAIBZFDQAgCiEf/QwAAAAAAAAAAAAAAAAAAAAAIiAhISAgISIgICEjICAhJCAgISUMAQsgGCEBIA4hDP0MAAAAAAAAAAAAAAAAAAAAACIlISQgJSEjICUhIiAlISEgJSEgIAohDQNAICT9DAAAgD8AAIA/AACAPwAAgD/9DAAAAAAAAAAAAAAAAAAAAAAgDP0AAAAiJiAm/UEiJ/1SIAH9AAAAIiggKP1BIin9Tv3kASEkICUgJiAn/U4iKiAoICn9TiIm/eYB/eQBISUgAUEQaiEBIAxBEGohDCAiICYgJ/1OIib95AEhIiAjICogKf1OIif95AEhIyAgICYgJv3mAf3kASEgICEgJyAn/eYB/eQBISEgDUEHaiErIA1BBGoiHyENICsgC0gNAAsLICD9HwC7ICD9HwG7oCAg/R8Cu6AgIP0fA7ugISwgIf0fALsgIf0fAbugICH9HwK7oCAh/R8Du6AhLSAi/R8AuyAi/R8Bu6AgIv0fArugICL9HwO7oCEuICP9HwC7ICP9HwG7oCAj/R8Cu6AgI/0fA7ugIS8gJP0fALsgJP0fAbugICT9HwK7oCAk/R8Du6AhMCAl/R8AuyAl/R8Bu6AgJf0fArugICX9HwO7oCExAkAgCyAfTA0AIAsgH2shDSARIB9BAnQiDGohASAZIAxqIQwDQAJAIAEqAgAiMiAyXA0AIAwqAgAiMyAzXA0AIDBEAAAAAAAA8D+gITAgLiAzuyI0oCEuIC8gMrsiNaAhLyA0IDSiICygISwgNSA1oiAtoCEtIDUgNKIgMaAhMQsgAUEEaiEBIAxBBGohDCANQX9qIg0NAAsLIBogF0EDdCIBaiIMIDEgDCsDAKA5AwAgDCAEQQN0aiIMIDAgDCsDAKA5AwAgHiABaiIMIC8gDCsDAKA5AwAgHSABaiIMIC4gDCsDAKA5AwAgHCABaiIMIC0gDCsDAKA5AwAgGyABaiIBICwgASsDAKA5AwAgGSAPaiEZIBggD2ohGCAXQQFqIhcgCUgNAAsLIBEgD2ohESAOIA9qIQ4gBkEBaiIGIAdHDQALCwsAOwRuYW1lAAoJZ3JhbS53YXNtARQCAARncmFtAQtncmFtX21hc2tlZAcSAQAPX19zdGFja19wb2ludGVyADgJcHJvZHVjZXJzAQxwcm9jZXNzZWQtYnkBDFVidW50dSBjbGFuZxExOC4xLjMgKDF1YnVudHUxKQA1D3RhcmdldF9mZWF0dXJlcwMrD211dGFibGUtZ2xvYmFscysIc2lnbi1leHQrB3NpbWQxMjg=",Nd="AGFzbQEAAAABEAFgDH9/f39/f39/f39/fwACDwEDZW52Bm1lbW9yeQIAAgMDAgAABAUBcAEBAQYIAX8BQYCIBAsHFgIEZ3JhbQAAC2dyYW1fbWFza2VkAAEKlRICrQoIE38DewF/BXsGfwV7AX8NfAJAIAYgB04NACACQQR0IQwgAkECdCENIAJBA3QhDiAKQQJ0IQ8gACAGIAJsQQJ0aiEQIApBA2ogC04hEQNAAkAgCCAGIAggBkobIhIgCU4NACAAIAZBAWoiEyAGIBMgB0gbIhQgAmxBAnRqIRUgAyAUIAVrIARsQQN0aiEWIAMgBiAFayAEbEEDdGohFyABIA0gEmxqIRgDQCABIBJBA2oiGSASIBkgCUgbIAJsQQJ0aiEaIAEgEkECaiIbIBIgGyAJSBsgAmxBAnRqIRwgASASQQFqIh0gEiAdIAlIGyACbEECdGohHgJAAkAgEUUNAP0MAAAAAAAAAAAAAAAAAAAAACIfISAgHyEhIAohIiAfISMgHyEkIB8hJSAfISYgHyEnDAELIBohFCAcISggHiEpIBghKiAVISsgECEs/QwAAAAAAAAAAAAAAAAAAAAAIichJiAnISUgJyEkICchIyAKIS0gJyEhICchICAnIR8DQCArIA9q/QAAACIuIBQgD2r9AAAAIi8gIf2FAiEhIC4gKCAPav0AAAAiMCAg/YUCISAgLiApIA9q/QAAACIxIB/9hQIhHyAuICogD2r9AAAAIjIgI/2FAiEjICwgD2r9AAAAIi4gLyAk/YUCISQgLiAwICX9hQIhJSAuIDEgJv2FAiEmIC4gMiAn/YUCIScgFEEQaiEUIChBEGohKCApQRBqISkgKkEQaiEqICtBEGohKyAsQRBqISwgLUEHaiEzIC1BBGoiIiEtIDMgC0gNAAsLICH9HwC7ICH9HwG7oCAh/R8Cu6AgIf0fA7ugITQgIP0fALsgIP0fAbugICD9HwK7oCAg/R8Du6AhNSAf/R8AuyAf/R8Bu6AgH/0fArugIB/9HwO7oCE2ICP9HwC7ICP9HwG7oCAj/R8Cu6AgI/0fA7ugITcgJP0fALsgJP0fAbugICT9HwK7oCAk/R8Du6AhOCAl/R8AuyAl/R8Bu6AgJf0fArugICX9HwO7oCE5ICb9HwC7ICb9HwG7oCAm/R8Cu6AgJv0fA7ugITogJ/0fALsgJ/0fAbugICf9HwK7oCAn/R8Du6AhOwJAIAsgIkwNACAiQQJ0IRQgCyAiayErIBAhKCAVISkgGCEqA0AgKSAUaioCALsiPCAaIBRqKgIAuyI9oiA0oCE0IDwgHCAUaioCALsiPqIgNaAhNSA8IB4gFGoqAgC7Ij+iIDagITYgPCAqIBRqKgIAuyJAoiA3oCE3ICggFGoqAgC7IjwgPaIgOKAhOCA8ID6iIDmgITkgPCA/oiA6oCE6IDwgQKIgO6AhOyAoQQRqISggKUEEaiEpICpBBGohKiAeQQRqIR4gHEEEaiEcIBpBBGohGiArQX9qIisNAAsLIBcgEkEDdCIaaiIUIDsgFCsDAKA5AwACQCAdIAlOIhQNACAXIB1BA3RqIhwgOiAcKwMAoDkDAAsCQCAbIAlOIhwNACAXIBtBA3RqIh4gOSAeKwMAoDkDAAsCQCAZIAlOIh4NACAXIBlBA3RqIiggOCAoKwMAoDkDAAsCQCATIAdODQAgFiAaaiIaIDcgGisDAKA5AwACQCAUDQAgFiAdQQN0aiIUIDYgFCsDAKA5AwALAkAgHA0AIBYgG0EDdGoiFCA1IBQrAwCgOQMACyAeDQAgFiAZQQN0aiIUIDQgFCsDAKA5AwALIBggDGohGCASQQRqIhIgCUgNAAsLIBAgDmohECAGQQJqIgYgB0gNAAsLC+MHBhR/CnsBfwZ8An0CfAJAIAYgB04NACAAIAYgAWxBAnQiDCAKQQJ0Ig1qaiEOIAFBAnQhDyAAIA1qIRAgACAMaiERIARBBWxBA3QhEiAEQQJ0QQN0IRMgBEEDbEEDdCEUIARBAXRBA3QhFSAKQQNqIAtOIRYDQAJAIAggBiAIIAZKGyIXIAlODQAgECAPIBdsIgFqIRggACABaiEZIAIgBiAFayADbEEDdGoiGiASaiEbIBogE2ohHCAaIBRqIR0gGiAVaiEeA0ACQAJAIBZFDQAgCiEf/QwAAAAAAAAAAAAAAAAAAAAAIiAhISAgISIgICEjICAhJCAgISUMAQsgGCEBIA4hDP0MAAAAAAAAAAAAAAAAAAAAACIlISQgJSEjICUhIiAlISEgJSEgIAohDQNAIAz9AAAAIiYgJiAm/UEiJv1OIicgAf0AAAAiKCAoICj9QSIo/U4iKSAl/YUCISUgJP0MAACAPwAAgD8AAIA/AACAP/0MAAAAAAAAAAAAAAAAAAAAACAm/VIgKP1O/eQBISQgAUEQaiEBIAxBEGohDCApICb9TiImICYgIP2FAiEgICcgKP1OIiggKCAh/YUCISEgIiAm/eQBISIgIyAo/eQBISMgDUEHaiEqIA1BBGoiHyENICogC0gNAAsLICD9HwC7ICD9HwG7oCAg/R8Cu6AgIP0fA7ugISsgIf0fALsgIf0fAbugICH9HwK7oCAh/R8Du6AhLCAi/R8AuyAi/R8Bu6AgIv0fArugICL9HwO7oCEtICP9HwC7ICP9HwG7oCAj/R8Cu6AgI/0fA7ugIS4gJP0fALsgJP0fAbugICT9HwK7oCAk/R8Du6AhLyAl/R8AuyAl/R8Bu6AgJf0fArugICX9HwO7oCEwAkAgCyAfTA0AIAsgH2shDSARIB9BAnQiDGohASAZIAxqIQwDQAJAIAEqAgAiMSAxXA0AIAwqAgAiMiAyXA0AIC9EAAAAAAAA8D+gIS8gLSAyuyIzoCEtIC4gMbsiNKAhLiAzIDOiICugISsgNCA0oiAsoCEsIDQgM6IgMKAhMAsgAUEEaiEBIAxBBGohDCANQX9qIg0NAAsLIBogF0EDdCIBaiIMIDAgDCsDAKA5AwAgDCAEQQN0aiIMIC8gDCsDAKA5AwAgHiABaiIMIC4gDCsDAKA5AwAgHSABaiIMIC0gDCsDAKA5AwAgHCABaiIMICwgDCsDAKA5AwAgGyABaiIBICsgASsDAKA5AwAgGSAPaiEZIBggD2ohGCAXQQFqIhcgCUgNAAsLIBEgD2ohESAOIA9qIQ4gBkEBaiIGIAdHDQALCwsAQwRuYW1lABIRZ3JhbS1yZWxheGVkLndhc20BFAIABGdyYW0BC2dyYW1fbWFza2VkBxIBAA9fX3N0YWNrX3BvaW50ZXIAOAlwcm9kdWNlcnMBDHByb2Nlc3NlZC1ieQEMVWJ1bnR1IGNsYW5nETE4LjEuMyAoMXVidW50dTEpAEMPdGFyZ2V0X2ZlYXR1cmVzBCsPbXV0YWJsZS1nbG9iYWxzKwxyZWxheGVkLXNpbWQrCHNpZ24tZXh0KwdzaW1kMTI4",Os=512,Ls=256,At=64,Sa=1e-9;async function Md(e,t,n,s={}){const o=e.m;if(o<=0)return;const{engine:r,rows:i}=l0(e,s.engine??"wasm"),a=new Float64Array(o);for(let c=0;c<o;c+=At){const l=Math.min(o,c+At);if(n!==void 0)for(let u=c;u<l;u++){const h=n(u);h!==void 0&&await h}u0(r,i,c,l,a,t)}}function c0(e){return e+3&-4}function l0(e,t="wasm"){const n=e.m,s=c0(n),o=kd(t,s,n),r=o.x,i=new Float64Array(n),a=new Float64Array(n),c=new Float64Array(n),l=Math.floor(n*n/8),u=new Int32Array(n+1);let h=new Int32Array(Math.min(l,Math.max(64,n))),f=0,d=!1;const m=new Float64Array(n);for(let g=0;g<n;g++){e.row(g,m);let p=0,w=0;for(let M=0;M<n;M++){const I=m[M];Number.isFinite(I)&&(p+=I,w++)}const b=w>0?p/w:0;u[g]=f;const y=g*s;let A=0,N=0;for(let M=0;M<n;M++){const I=m[M];if(Number.isFinite(I)){const x=Math.fround(I-b);r[y+M]=x,A+=x,N+=x*x;continue}if(r[y+M]=Number.NaN,!d){if(f===l){d=!0;continue}if(f===h.length){const x=new Int32Array(Math.min(l,h.length*2));x.set(h),h=x}h[f++]=M}}for(let M=n;M<s;M++)r[y+M]=Number.NaN;i[g]=w,a[g]=A,c[g]=N}if(u[n]=f,!d)for(let g=0;g<n;g++){for(let p=u[g];p<u[g+1];p++)r[g*s+h[p]]=0;for(let p=n;p<s;p++)r[g*s+p]=0}return{engine:o,rows:{m:n,ld:s,first:0,masked:d,count:i,sum:a,squares:c,starts:d?new Int32Array(n+1):u,cols:d?new Int32Array(0):h.subarray(0,f)}}}function u0(e,t,n,s,o,r){const{m:i,first:a}=t,c=i-a;e.run(n-a,s-a,t.masked);const l=e.out;for(let u=n;u<s;u++){const h=(u-n)*c-a;if(t.masked){const f=At*c;for(let d=u+1;d<i;d++){const m=h+d;o[d]=h0(l[m],l[f+m],l[2*f+m],l[3*f+m],l[4*f+m],l[5*f+m])}}else Id(u,e.x,t,l,h,o);r(u,o)}}function Id(e,t,n,s,o,r){const{m:i,ld:a,first:c,count:l,sum:u,squares:h,starts:f,cols:d}=n,m=e-c,g=f[m],p=f[m+1],w=m*a;for(let b=e+1;b<i;b++){const y=b-c;let A=l[m],N=u[m],M=h[m],I=u[y],x=h[y];const v=f[y],S=f[y+1];if(S>v){for(let k=v;k<S;k++){const C=t[w+d[k]];N-=C,M-=C*C}A-=S-v}if(p>g){const k=y*a;for(let C=g;C<p;C++){const B=t[k+d[C]];I-=B,x-=B*B}S>v&&(A+=xd(d,g,p,v,S))}r[b]=h0(s[o+b],A,N,I,M,x)}}function xd(e,t,n,s,o){let r=0,i=t,a=s;for(;i<n&&a<o;){const c=e[i],l=e[a];c===l?(r++,i++,a++):c<l?i++:a++}return r}function h0(e,t,n,s,o,r){if(!(t>=Zr))return Number.NaN;const i=o-n*n/t,a=r-s*s/t;if(!(i>Sa*o)||!(a>Sa*r))return Number.NaN;const c=(e-n*s/t)/Math.sqrt(i*a);return c>1?1:c<-1?-1:c}let cs;function Ca(e){try{if(typeof WebAssembly>"u")return null;const t=atob(e),n=new Uint8Array(t.length);for(let s=0;s<t.length;s++)n[s]=t.charCodeAt(s);return WebAssembly.validate(n)?new WebAssembly.Module(n):null}catch{return null}}function vd(e){return cs??={relaxed:Ca(Nd),simd:Ca(yd)},e==="wasm"?cs.relaxed??cs.simd:cs.simd}function kd(e,t,n){const s=e==="js"?null:vd(e);if(s!==null)try{return Cd(s,t,n)}catch{}return Fd(t,n)}const Sd=65536;function Cd(e,t,n){const s=n*t*4,o=Math.ceil(s/8)*8,r=6*At*n,i=new WebAssembly.Memory({initial:Math.ceil((o+r*8)/Sd)+1}),a=new WebAssembly.Instance(e,{env:{memory:i}}),c=a.exports.gram,l=a.exports.gram_masked,u=new Float32Array(i.buffer,0,n*t),h=new Float64Array(i.buffer,o,r);return{x:u,out:h,run(f,d,m){h.fill(0,0,m?h.length:At*n);for(let g=0;g<t;g+=Os){const p=Math.min(t,g+Os);for(let w=f;w<n;w+=Ls){const b=Math.min(n,w+Ls);m?l(0,t,o,n,At*n,f,f,d,w,b,g,p):c(0,0,t,o,n,f,f,d,w,b,g,p)}}}}}function Fd(e,t){const n=new Float32Array(t*e),s=new Float64Array(6*At*t);return{x:n,out:s,run(o,r,i){s.fill(0,0,i?s.length:At*t);for(let a=0;a<e;a+=Os){const c=Math.min(e,a+Os);for(let l=o;l<t;l+=Ls){const u=Math.min(t,l+Ls);i?Ed(n,e,s,t,At*t,o,o,r,l,u,a,c):$d(n,e,s,t,o,o,r,l,u,a,c)}}}}}function $d(e,t,n,s,o,r,i,a,c,l,u){for(let h=r;h<i;h+=2){const f=h+1<i?h+1:h,d=h*t,m=f*t,g=(h-o)*s,p=(f-o)*s;for(let w=Math.max(h,a);w<c;w+=2){const b=w+1<c?w+1:w,y=w*t,A=b*t;let N=0,M=0,I=0,x=0;for(let v=l;v<u;v++){const S=e[d+v],k=e[m+v],C=e[y+v],B=e[A+v];N+=S*C,M+=S*B,I+=k*C,x+=k*B}n[g+w]+=N,b!==w&&(n[g+b]+=M),f!==h&&(n[p+w]+=I,b!==w&&(n[p+b]+=x))}}}function Ed(e,t,n,s,o,r,i,a,c,l,u,h){for(let f=i;f<a;f++){const d=f*t,m=(f-r)*s;for(let g=Math.max(f,c);g<l;g++){const p=g*t;let w=0,b=0,y=0,A=0,N=0,M=0;for(let I=u;I<h;I++){const x=e[d+I],v=e[p+I];x!==x||v!==v||(w+=x*v,b++,y+=x,A+=v,N+=x*x,M+=v*v)}n[m+g]+=w,n[o+m+g]+=b,n[2*o+m+g]+=y,n[3*o+m+g]+=A,n[4*o+m+g]+=N,n[5*o+m+g]+=M}}}const Bd=384*1024*1024,Td=1024,Fa=.9,Od=6e4;let As=null,Ld=1;function _d(e,t){As!==null&&f0(As),As=t>0?{spawn:e,helpers:t,idle:[],release:null}:null}function Dd(e){const t=e.idle.pop();if(t!==void 0)return t;try{return e.spawn()}catch{return null}}function Rd(e,t){t.onmessage=null,t.onerror=null,e.idle.push(t)}function f0(e){e.release!==null&&clearTimeout(e.release),e.release=null;for(const t of e.idle.splice(0))t.terminate()}function zd(e){e.release!==null&&clearTimeout(e.release),e.release=null,e.idle.length!==0&&(e.release=setTimeout(()=>{e.release=null,f0(e)},Od),e.release.unref?.())}async function d0(e,t,n){const s=e.m,o=As;if(o===null||s<Td)return Md(e,t,n);const r=Pd(s,c0(s),o.helpers);o.release!==null&&clearTimeout(o.release),o.release=null;const i=[];for(let u=1;u<r.length-1;u++)i.push({worker:Dd(o),from:r[u],to:r[u+1],delivered:!1,failed:!1});const a=new Float64Array(s),c=new Map,l=u=>{let h;const f=new Promise(d=>{h=d});c.set(u,{settle:h,lines:f})};try{const{engine:u,rows:h}=l0(e);for(const f of i){const{worker:d,from:m,to:g}=f;for(let A=m;A<g;A+=At)l(A);const p=()=>{f.failed=!0;for(let A=m;A<g;A+=At)c.get(A)?.settle(null)};if(d===null){p();continue}const w=Ld++,b=m+Math.floor((g-1-m)/At)*At;d.onmessage=A=>{const N=A.data;N.id===w&&(N.kind==="band"?(c.get(N.r0)?.settle(N.lines),N.r0===b&&(f.delivered=!0)):N.kind==="error"&&p())},d.onerror=A=>{A.preventDefault(),p()};const y=qd(w,u.x,h,m,g);d.postMessage(y,[y.x.buffer,y.count.buffer,y.sum.buffer,y.squares.buffer,y.starts.buffer,y.cols.buffer])}for(let f=0;f<s;f+=At){const d=Math.min(s,f+At),m=c.get(f),g=m===void 0?null:await m.lines;if(g===null){if(n!==void 0)for(let p=f;p<d;p++){const w=n(p);w!==void 0&&await w}u0(u,h,f,d,a,t);continue}for(let p=f;p<d;p++){if(n!==void 0){const b=n(p);b!==void 0&&await b}const w=(p-f)*s;for(let b=p+1;b<s;b++)a[b]=g[w+b];t(p,a)}}}finally{for(const{worker:u,delivered:h,failed:f}of i)u!==null&&(h&&!f?Rd(o,u):u.terminate());zd(o)}}function Pd(e,t,n){for(let s=n+1;s>1;s--){const o=[0],r=Fa+s-1;for(let a=1;a<s;a++){const c=(Fa+a-1)/r,l=Math.round(e*(1-Math.sqrt(1-c))/At)*At;l>o[o.length-1]&&l<e&&o.push(l)}o.push(e);let i=0;for(let a=1;a<o.length-1;a++)i+=(e-o[a])*t*4;if(o.length>2&&i<=Bd)return o}return[0,e]}function qd(e,t,n,s,o){const{m:r,ld:i}=n,a=r-s,c=new Int32Array(a+1),l=n.starts[s];for(let u=0;u<=a;u++)c[u]=n.starts[s+u]-l;return{id:e,m:r,ld:i,first:s,to:o,masked:n.masked,x:t.slice(s*i,r*i),count:n.count.slice(s,r),sum:n.sum.slice(s,r),squares:n.squares.slice(s,r),starts:c,cols:n.cols.slice(l,n.starts[r])}}function m0(e,t,n=.1){const s=[],o=Math.max(1,Math.ceil(t*n));for(let r=0;r<t;r++){let i=0;for(let a=0;a<t&&i<o;a++)Number.isFinite(e[r*t+a])&&i++;i>=o&&s.push(r)}return Int32Array.from(s)}function zn(e,t){return t>0?Math.min(e,t-e):e}function Po(e,t,n,s,o=new Float64Array(n.length*n.length)){const r=n.length,i=s.cis.length,{group:a,trans:c,ignoreDiags:l}=s,u=s.contacts==="trans",h=s.contacts==="cis",f=s.nearDiagonal??Number.NaN,d=s.cis.map(m=>Float64Array.from({length:m.maxOffset+1},(g,p)=>ti(m,p)));for(let m=0;m<r;m++){const g=n[m],p=a[g],w=d[p],b=s.laps?.[p]??0,y=m*r,A=g*t;for(let N=0;N<r;N++){const M=n[N],I=a[M],x=p===I;if(x?u:h){o[y+N]=Number.NaN;continue}let v;if(x){const k=zn(Math.abs(M-g),b);if(k<l){o[y+N]=f;continue}v=k<w.length?w[k]:Number.NaN}else v=c[p*i+I];const S=e[A+M];o[y+N]=Number.isFinite(S)&&Number.isFinite(v)&&v!==0?S/v:Number.NaN}}return o}async function Ud(e,t,n){const s=new Float64Array(t*t);return await d0({m:t,row:(o,r)=>r.set(e.subarray(o*t,(o+1)*t))},(o,r)=>{s[o*t+o]=1;for(let i=o+1;i<t;i++)s[o*t+i]=Number.isFinite(r[i])?r[i]:0},n),g0(s,t),s}function g0(e,t){for(let s=0;s<t;s+=64){const o=Math.min(t,s+64);for(let r=s;r<t;r+=64){const i=Math.min(t,r+64);for(let a=s;a<o;a++){const c=a*t;for(let l=Math.max(r,a+1);l<i;l++)e[l*t+a]=e[c+l]}}}}const eo=99.9;function $a(e,t,n=eo,s=new Float64Array(t*t)){const o=b0(e,n);for(let r=0;r<t*t;r++)s[r]=w0(e[r],o);return s}const p0=1.03;function Gd(e,t=p0){const n=[0];if(e<1)return Int32Array.from(n);const s=Math.trunc(Math.log(e)/Math.log(t)),o=Math.log10(e),r=s>1?o/(s-1):0;for(let i=0;i<s;i++){const a=Hd(10**(s>1&&i===s-1?o:i*r));a>n[n.length-1]&&n.push(a)}return n[1]!==1&&n.splice(1,0,1),n[n.length-1]!==e&&n.push(e),Int32Array.from(n)}function Hd(e){const t=Math.round(e);return t-e===.5&&t%2!==0?t-1:t}function Qd(e,t,n=p0){const s=e.maxOffset+1,o=Gd(s,n),r=new Float64Array(s).fill(Number.NaN);for(let i=0;i+1<o.length;i++){let a=0,c=0;for(let u=o[i];u<o[i+1];u++)a+=u<t?e.count[u]:e.sum[u],c+=e.count[u];if(c===0)continue;const l=a===0?1:a/c;for(let u=o[i];u<o[i+1];u++)r[u]=l}return{maxOffset:e.maxOffset,sum:e.sum,count:e.count,mean:r}}function Ea(e){return{...e,cis:e.cis.map(t=>Qd(t,e.ignoreDiags)),nearDiagonal:1}}function w0(e,t){return Number.isFinite(e)?Math.min(Math.max(e,0),t)-1:0}function Kd(e,t,n,s=eo){const o=b0(e,s),r=new Float64Array(n.length),i=new Float64Array(t);let a=0;for(let c=0;c<t;c++){for(let l=0;l<t;l++){const u=w0(e[c*t+l],o);i[l]=u,a+=u*u}for(let l=0;l<n.length;l++){const u=n[l];let h=0;for(let f=0;f<t;f++)h+=i[f]*u[f];r[l]+=u[c]*h}}return Array.from(r,c=>a>0?c*c/a:Number.NaN)}function b0(e,t){let n=0;for(let h=0;h<e.length;h++)Number.isFinite(e[h])&&n++;if(n===0)return Number.NaN;const s=Math.min(100,Math.max(0,t))/100*(n-1),o=Math.floor(s),r=n-o,i=new Float64Array(r);let a=0;const c=(h,f)=>{let d=h;for(;;){const m=2*d+1,g=m+1;let p=d;if(m<f&&i[m]<i[p]&&(p=m),g<f&&i[g]<i[p]&&(p=g),p===d)return;const w=i[d];i[d]=i[p],i[p]=w,d=p}};for(let h=0;h<e.length;h++){const f=e[h];if(Number.isFinite(f))if(a<r){let d=a++;for(i[d]=f;d>0;){const m=d-1>>1;if(i[m]<=i[d])break;const g=i[m];i[m]=i[d],i[d]=g,d=m}}else f>i[0]&&(i[0]=f,c(0,a))}const l=i[0],u=s-o;return u===0||a<2?l:(i[0]=i[a-1],c(0,a-1),l+(i[0]-l)*u)}async function Vd(e,t,n){const s=m0(e,t),o=s.length,r=new Float32Array(t*t).fill(Number.NaN);return o<2?{values:r,kept:o,dropped:t-o}:(await d0({m:o,row:(i,a)=>{const c=s[i]*t;for(let l=0;l<o;l++)a[l]=e[c+s[l]]}},(i,a)=>{const c=s[i],l=c*t;r[l+c]=1;for(let u=i+1;u<o;u++)r[l+s[u]]=Number.isFinite(a[u])?a[u]:0},n),g0(r,t),{values:r,kept:o,dropped:t-o})}function Yd(e,t,n){const s=r0(e,t.values);if(!Number.isFinite(s)||Math.abs(s)<n)return{flipped:!1,r:Number.isFinite(s)?s:Number.NaN,confident:!1,signal:t.name};const o=s<0;if(o)for(let r=0;r<e.length;r++)e[r]=-e[r];return{flipped:o,r:s,confident:!0,signal:t.name}}function Wd(e,t,n,s){const o=fd(n,s),r=new Int32Array(t).fill(-1);for(let h=0;h<t;h++){const f=n[h];if(!Number.isFinite(f))continue;let d=0;for(;d<s-1&&f>=o[d+1];)d++;r[h]=d}const i=new Float64Array(s*s),a=new Float64Array(s*s);for(let h=0;h<t;h++){const f=r[h];if(!(f<0))for(let d=0;d<t;d++){const m=r[d];if(m<0)continue;const g=e[h*t+d];Number.isFinite(g)&&(i[f*s+m]+=g,a[f*s+m]+=1)}}const c=new Float32Array(s*s);for(let h=0;h<c.length;h++)c[h]=a[h]>0?i[h]/a[h]:Number.NaN;const l=Math.max(1,Math.round(s/5)),u=(h,f,d,m)=>{let g=0,p=0;for(let w=h;w<f;w++)for(let b=d;b<m;b++){const y=c[w*s+b];Number.isFinite(y)&&(g+=y,p++)}return p===0?Number.NaN:g/p};return{values:c,edges:o,strength:{bb:u(0,l,0,l),aa:u(s-l,s,s-l,s),ab:u(0,l,s-l,s)}}}function Xd(e,t){const n=[];let s=-1,o=-1,r=0,i=0,a=0;const c=()=>{s>=0&&r!==0&&n.push({side:r>0?"A":"B",from:s,to:o,mean:a===0?Number.NaN:i/a,bins:a}),s=-1,i=0,a=0};for(let l=0;l<e.length&&l<t.length;l++){const u=e[l],h=t[l],f=Number.isFinite(h)&&h>0?1:h<0?-1:0;(f!==r||u!==o+1)&&c(),r=f,o=u,f!==0&&(s<0&&(s=u),i+=h,a++)}return c(),n}function Jd(){return new Worker(new URL(""+new URL("gram.worker-Cf7kBq9K.js",import.meta.url).href,import.meta.url),{type:"module"})}const A0=.001,y0=20,Zd=1e-12;function*N0(e,t={}){const n=Math.max(0,Math.round(t.iterations??200)),s=Math.max(1,Math.round(t.history??10)),o=t.tolerance??1e-9,r=t.gradientTolerance??1e-10,i=e.length,a=Float64Array.from(e),c=new Float64Array(i),l=new Float64Array(i),u=new Float64Array(i),h=new Float64Array(i),f=[],d=[],m=[],g=new Float64Array(s),p={evaluations:0};let w=yield*Tn(a,c,p,0,n,Number.POSITIVE_INFINITY);if(!Number.isFinite(w))return{point:a,value:w,iterations:0,evaluations:p.evaluations,stop:"search"};let b="steps",y=0;for(;y<n;y++){if(t4(c)<=r){b="gradient";break}h.set(c);const A=f.length;for(let C=A-1;C>=0;C--){const B=m[C]*Tt(f[C],h);g[C]=B,Ta(h,d[C],-B)}if(A>0){const C=A-1,B=Tt(f[C],d[C])/Tt(d[C],d[C]);if(Number.isFinite(B)&&B>0)for(let F=0;F<i;F++)h[F]*=B}for(let C=0;C<A;C++){const B=m[C]*Tt(d[C],h);Ta(h,f[C],g[C]-B)}for(let C=0;C<i;C++)h[C]=-h[C];let N=Tt(c,h);if(!(N<0)){f.length=0,d.length=0,m.length=0;for(let C=0;C<i;C++)h[C]=-c[C];if(N=-Tt(c,c),!(N<0)){b="gradient";break}}const M=f.length===0?Math.min(1e10,1/Math.sqrt(Tt(c,c))):1,I=yield*jd(a,h,l,u,w,N,M,p,y,n);if(!I.ok){if(f.length>0){f.length=0,d.length=0,m.length=0,y--;continue}b="search";break}const x=new Float64Array(i),v=new Float64Array(i);for(let C=0;C<i;C++)x[C]=I.step*h[C],v[C]=u[C]-c[C];const S=w;a.set(l),c.set(u),w=I.value;const k=Tt(x,v);if(k>Zd*Math.sqrt(Tt(x,x)*Tt(v,v))&&(f.push(x),d.push(v),m.push(1/k),f.length>s&&(f.shift(),d.shift(),m.shift())),S-w<=o*Math.max(1,Math.abs(S),Math.abs(w))){y++,b="value";break}}return{point:a,value:w,iterations:y,evaluations:p.evaluations,stop:b}}function*jd(e,t,n,s,o,r,i,a,c,l){const u=e.length,h=p=>{for(let w=0;w<u;w++)n[w]=e[w]+p*t[w]};let f=0,d=o,m=i,g=Number.NaN;for(let p=0;p<y0;p++){h(m);const w=yield*Tn(n,s,a,c,l,o);if(g=m,!Number.isFinite(w)||w>o+A0*m*r||p>0&&w>=d)return yield*Ba(e,t,n,s,o,r,f,d,m,a,c,l);const y=Tt(s,t);if(Math.abs(y)<=-.9*r)return{ok:!0,step:m,value:w};if(y>=0)return yield*Ba(e,t,n,s,o,r,m,w,f,a,c,l);if(f=m,d=w,m*=2,!Number.isFinite(m))break}if(Number.isFinite(g)){h(g);const p=yield*Tn(n,s,a,c,l,o);if(Number.isFinite(p)&&p<o)return{ok:!0,step:g,value:p}}return{ok:!1,step:0,value:o}}function*Ba(e,t,n,s,o,r,i,a,c,l,u,h){const f=e.length;let d=i,m=a,g=c;for(let p=0;p<y0;p++){const w=(d+g)/2;if(!Number.isFinite(w)||w<=0)break;for(let N=0;N<f;N++)n[N]=e[N]+w*t[N];const b=yield*Tn(n,s,l,u,h,o);if(!Number.isFinite(b)||b>o+A0*w*r||b>=m){g=w;continue}const A=Tt(s,t);if(Math.abs(A)<=-.9*r)return{ok:!0,step:w,value:b};A*(g-d)>=0&&(g=d),d=w,m=b}if(d>0&&m<o){for(let w=0;w<f;w++)n[w]=e[w]+d*t[w];const p=yield*Tn(n,s,l,u,h,o);if(Number.isFinite(p))return{ok:!0,step:d,value:p}}return{ok:!1,step:0,value:o}}function*Tn(e,t,n,s,o,r){n.evaluations++;const i=yield{point:e,gradient:t,iteration:s,iterations:o,evaluations:n.evaluations,current:r};return Number.isNaN(i)?Number.POSITIVE_INFINITY:i}function Tt(e,t){let n=0;for(let s=0;s<e.length;s++)n+=e[s]*t[s];return n}function Ta(e,t,n){for(let s=0;s<e.length;s++)e[s]+=n*t[s]}function t4(e){let t=0;for(let n=0;n<e.length;n++){const s=Math.abs(e[n]);s>t&&(t=s)}return t}const M0=2220446049250313e-31,qo={tolerance:1e12*M0,gradient:1e-5,iterations:1e4},Oa={tolerance:10*M0,gradient:1},I0=10,e4=3,n4=2/3;function s4(e,t,n,s,o=null){const r=new Map;for(let d=0;d<t;d++){const m=r.get(n[d]);m===void 0?r.set(n[d],[d]):m.push(d)}let i=0;const a=[];for(const d of r.values())d.sort((m,g)=>s[m]-s[g]),a.push(Int32Array.from(d)),i=Math.max(i,d.length);const c=[],l=[],u=[],h=[],f=Math.floor(i*n4);for(let d=1;d<f;d++){let m=0,g=0,p=0;for(const y of a)for(let A=0;A+d<y.length;A++){const N=y[A],M=y[A+d],I=e[N<M?N*t+M:M*t+N];if(!Number.isFinite(I))continue;const x=o===null?1:o[N]*o[M];m++,g+=I/x,p+=1/x}if(m<2)continue;const w=g/m;let b=0;for(const y of a)for(let A=0;A+d<y.length;A++){const N=y[A],M=y[A+d],I=e[N<M?N*t+M:M*t+N];if(!Number.isFinite(I))continue;const x=I/(o===null?1:o[N]*o[M])-w;b+=x*x}c.push(d),l.push(w),u.push(b/(m-1)/(p/m)),h.push(m)}return{separation:Int32Array.from(c),mean:Float64Array.from(l),variance:Float64Array.from(u),pairs:Float64Array.from(h)}}function o4(e){const t=[],n=[],s=[];for(let l=0;l<e.mean.length;l++){const u=e.mean[l],h=e.variance[l];if(!(u>0)||!(h>u+3*Math.sqrt((u+2*u*u)/e.pairs[l])))continue;const f=u*u/(h-u);Number.isFinite(f)&&(t.push(Math.log(u)),n.push(f),s.push(e.pairs[l]))}if(t.length<e4)return null;const o=(l,u)=>{let h=0;for(let f=0;f<t.length;f++){const d=Math.exp(l+u*t[f])-n[f];h+=s[f]*d*d}return h};let r=0,i=0,a=o(r,i),c=.001;for(let l=0;l<500&&Number.isFinite(a);l++){let u=0,h=0,f=0,d=0,m=0;for(let p=0;p<t.length;p++){const w=Math.exp(r+i*t[p]),b=w-n[p],y=s[p];u+=y*w*w,h+=y*w*w*t[p],f+=y*w*w*t[p]*t[p],d+=y*w*b,m+=y*w*b*t[p]}let g=!1;for(;c<1e16;){const p=u*(1+c),w=f*(1+c),b=p*w-h*h;if(!(Math.abs(b)>0)){c*=10;continue}const y=-(w*d-h*m)/b,A=-(p*m-h*d)/b,N=o(r+y,i+A);if(Number.isFinite(N)&&N<a){r+=y,i+=A;const M=a-N;a=N,c=Math.max(c/10,1e-12),g=M>1e-15*a&&Math.abs(y)+Math.abs(A)>1e-15*(1+Math.abs(r)+Math.abs(i));break}c*=10}if(!g)break}return Number.isFinite(r)&&Number.isFinite(i)?{c0:r,c1:i}:null}function La(e,t){return Math.exp(e.c0+e.c1*Math.log(t))}function r4(e,t){const n=new Float64Array(e),s=new Float64Array(e),o=new Float64Array(e);for(let r=0;r<e;r++)n[r]=1-2*t(),s[r]=1-2*t(),o[r]=1-2*t();return{x:n,y:s,z:o}}function i4(e,t,n,s){const o=1/s;for(let r=0;r<t;r++)for(let i=r+1;i<t;i++){const a=e[r*t+i],c=Number.isFinite(a)&&a>0?a/(n===null?1:n[r]*n[i]):0;e[i*t+r]=c>0?s===3?Math.cbrt(c):Math.pow(c,o):0}}function a4(e){const t=k0(e),n=[0];for(let s=1;s<t;s++){const o=Math.round(e*Math.sqrt(s/t));o>n[n.length-1]&&o<e&&n.push(o)}return n.push(e),Int32Array.from(n)}function c4(e,t,n,s,o,r,i){const a=o;i.fill(0);let c=0;for(let l=Math.max(1,s);l<o;l++){const u=l*n,h=r[l],f=r[n+l],d=r[2*n+l];let m=0,g=0,p=0;for(let w=0;w<l;w++){const b=e[u+w];if(!(b>0))continue;const y=h-r[w],A=f-r[n+w],N=d-r[2*n+w],M=Math.sqrt(y*y+A*A+N*N),I=M*b-1;if(c+=2*I*I,!(M>0))continue;const x=4*I*b/M,v=x*y,S=x*A,k=x*N;m+=v,g+=S,p+=k,i[w]-=v,i[a+w]-=S,i[2*a+w]-=k}i[l]+=m,i[a+l]+=g,i[2*a+l]+=p}return c}let x0=0,v0=0;const _a=7,l4=16,u4=1e9;function h4(e,t,n){let s=t,o=1,r=0;for(;s<_a;)o*=s,r+=1/s,s+=1;let i=e+t,a=1,c=0;for(;i<_a;)a*=i,c+=1/i,i+=1;const l=s===t?n:Math.log(s),u=Math.log1p((i-s)/s),h=1/s,f=1/i,d=h*h,m=f*f,g=h*(1/12-d*(1/360-d*(1/1260-d*(1/1680-d*(1/1188-d*691/360360))))),p=f*(1/12-m*(1/360-m*(1/1260-m*(1/1680-m*(1/1188-m*691/360360))))),w=d*(1/12-d*(1/120-d*(1/252-d*(1/240-d*(1/132-d*691/32760))))),b=m*(1/12-m*(1/120-m*(1/252-m*(1/240-m*(1/132-m*691/32760))))),y=o===a?0:Math.log(o/a),A=o===1&&a===1?0:(i-s)*l-e*n;x0=(i-.5)*u-(o===1&&a===1?e:i-s)+p-g+y+A,v0=t*(u-.5*(f-h)-b+w-c+r)}function f4(e){const t=e.bias??null;return{n:e.n,alpha:e.alpha,bias:t,logBias:t===null?null:Float64Array.from(t,n=>Math.log(n)),dispersion:e.dispersion}}const d4=32,m4=64;function k0(e){return Math.max(1,Math.min(m4,Math.round(e/d4)))}function Da(e){const t=k0(e),n=[0];for(let s=1;s<t;s++){const o=Math.round(e*(1-Math.sqrt(1-s/t)));o>n[n.length-1]&&o<e&&n.push(o)}return n.push(e),Int32Array.from(n)}function g4(e,t,n,s,o,r,i){const{n:a,dispersion:c,alpha:l,bias:u,logBias:h}=e,f=l/2,d=l===3,m=c!==null,g=m?c.c0:0,p=m?c.c1:0,w=1-p,b=a-s;i.fill(0);let y=0;for(let A=s;A<o;A++){const N=A*a,M=r[A],I=r[a+A],x=r[2*a+A],v=u===null?1:u[A],S=h===null?0:h[A];let k=0,C=0,B=0;for(let F=A+1;F<a;F++){const $=t[N+F];if(!($>=0))continue;const _=M-r[F],E=I-r[a+F],O=x-r[2*a+F],L=_*_+E*E+O*O;if(!(L>0))return Number.POSITIVE_INFINITY;const D=-f*Math.log(L),q=d?1/(L*Math.sqrt(L)):Math.exp(D),X=(u===null?1:v*u[F])*q;let R;if(m){const G=w*D-g,Y=Math.exp(G),V=X/Y,J=Y>.001?Math.log(1+Y):Math.log1p(Y);if(y+=($+V)*J,R=w*(X-$)/(1+Y)+p*V*J,$>0){const Z=S+(h===null?0:h[F])+D,ut=Z-G;let Q=0,W=1;if($>1&&$<=l4&&$===Math.floor($)&&V<u4){let et=1,j=0,ot=0;for(;ot+4<=$;ot+=4){const at=V+ot,ht=at+1,ft=at+2,kt=at+3,pt=at*ht,Ft=ft*kt,gt=pt*Ft;j=j*gt+((at+ht)*Ft+(ft+kt)*pt)*et,et*=gt}for(;ot<$;ot++){const at=V+ot;j=j*at+et,et*=at}Q=Math.log(et)-$*ut,W=V*j/et}else $!==1&&(h4($,V,ut),Q=x0,W=v0);y-=Q+$*Z,R-=p*W}}else y+=X,R=X-$,$>0&&(y-=$*(S+(h===null?0:h[F])+D));const P=-l*R/L,z=P*_,U=P*E,K=P*O;k+=z,C+=U,B+=K,i[F-s]-=z,i[b+F-s]-=U,i[2*b+F-s]-=K}i[A-s]+=k,i[b+A-s]+=C,i[2*b+A-s]+=B}return y}function p4(e,t,n,s,o){for(let r=0;r<o;r++)e[s+r]+=t[r],e[n+s+r]+=t[o+r],e[2*n+s+r]+=t[2*o+r]}function S0(e){const t=e.x.length,n=new Float64Array(3*t);return n.set(e.x,0),n.set(e.y,t),n.set(e.z,2*t),n}function w4(e,t){return{x:Float64Array.from(e.subarray(0,t)),y:Float64Array.from(e.subarray(t,2*t)),z:Float64Array.from(e.subarray(2*t,3*t))}}function b4(e,t,n={}){const s=N0(S0(t),{iterations:n.iterations??qo.iterations,history:I0,tolerance:qo.tolerance,gradientTolerance:qo.gradient});return C0(s,e)}function A4(e,t,n={}){const s=N0(S0(t),{iterations:n.iterations??1e5,history:I0,tolerance:Oa.tolerance,gradientTolerance:Oa.gradient});return C0(s,e)}function*C0(e,t){const n=yield*e;return{coords:w4(n.point,t),value:n.value,iterations:n.iterations,evaluations:n.evaluations,stop:n.stop}}const y4=512*1024*1024,N4=256,Ra=.9;let F0=null,M4=1;function I4(e,t){F0=t>0?{spawn:e,helpers:Math.floor(t)}:null}function x4(e,t){const n=F0,s=a=>za(a)?0:a.BYTES_PER_ELEMENT,o=n===null||e<N4?0:Pa(Da(e),e,n.helpers,s(t)).length-2,r=[];for(let a=0;n!==null&&a<o;a++){let c;try{c=n.spawn()}catch{continue}const l={worker:c,alive:!0,ready:!1,owed:null};c.onmessage=u=>{const h=u.data;if(h.kind==="ready"){l.ready=!0;return}if(h.kind==="error"){An(l);return}const f=l.owed;f===null||h.id!==f.id||h.seq!==f.seq||(l.owed=null,f.resolve(h))},c.onerror=u=>{u.preventDefault(),An(l)},r.push(l)}const i=(a,c)=>{const l=c.counts,u=za(l),h=a==="mds",f=h?null:f4(c),d=h?a4(e):Da(e),m=d.length-1,g=v=>h?0:d[v],p=v=>h?d[v+1]:e-d[v],w=Array.from({length:m},(v,S)=>new Float64Array(3*p(S))),b=new Float64Array(m),y=M4++;let A=0;for(const v of r){const S=v.owed;v.owed=null,S?.resolve(null)}const N=r.filter(v=>v.alive),M=N.length===0?[0,m]:Pa(d,e,N.length,s(l),a),I=[];for(let v=1;v+1<M.length;v++){const S=N[v-1];try{const k=v4(y,a,c,d,M[v],M[v+1],u);S.worker.postMessage(k,u?[]:[k.values.buffer])}catch{An(S)}I.push({helper:S,from:M[v],to:M[v+1]})}const x=(v,S)=>{w[v].length===0&&(w[v]=new Float64Array(3*p(v))),b[v]=f===null?c4(l,null,e,d[v],d[v+1],S,w[v]):g4(f,l,null,d[v],d[v+1],S,w[v])};return{threads:1+I.filter(({helper:v})=>v.alive).length,async evaluate(v,S){A++;const k=I.map(({helper:F,from:$,to:_})=>!F.alive||!F.ready?Promise.resolve(null):new Promise(E=>{F.owed={id:y,seq:A,resolve:E};const O=[];for(let D=$;D<_;D++)w[D].length===0&&(w[D]=new Float64Array(3*p(D))),O.push(w[D]);const L={kind:"evaluate",id:y,seq:A,point:v,parts:O};try{F.worker.postMessage(L,O.map(D=>D.buffer))}catch{An(F)}}));for(let F=0;F<M[1];F++)x(F,v);const C=await Promise.all(k);I.forEach(({from:F,to:$},_)=>{const E=C[_];for(let O=F;O<$;O++){if(E===null){x(O,v);continue}b[O]=E.values[O-F],w[O]=E.parts[O-F]}}),S.fill(0);let B=0;for(let F=0;F<m;F++){const $=b[F];if(!h&&!Number.isFinite($))return Number.POSITIVE_INFINITY;B+=$,p4(S,w[F],e,g(F),p(F))}return h||Number.isFinite(B)?B:Number.POSITIVE_INFINITY}}};return{threads:1+r.length,mds:a=>i("mds",{counts:a,n:e,alpha:1,bias:null,dispersion:null}),likelihood:a=>i("likelihood",a),close(){for(const a of r)An(a)}}}function An(e){if(!e.alive)return;e.alive=!1,e.worker.onmessage=null,e.worker.onerror=null,e.worker.terminate();const t=e.owed;e.owed=null,t?.resolve(null)}function za(e){return typeof SharedArrayBuffer=="function"&&e.buffer instanceof SharedArrayBuffer}function Pa(e,t,n,s,o="likelihood"){const r=e.length-1;for(let i=Math.min(n+1,r);i>1;i--){const a=Ra+i-1,c=[0];for(let u=1;u<i;u++){const h=Math.round(r*(Ra+u-1)/a);h>c[c.length-1]&&h<r&&c.push(h)}c.push(r);let l=0;for(let u=1;u+1<c.length;u++)l+=$0(o,e[c[u]],e[c[u+1]],t)*s;if(c.length>2&&l<=y4)return c}return[0,r]}function $0(e,t,n,s){let o=0;for(let r=t;r<n;r++)o+=e==="mds"?r:s-1-r;return o}function v4(e,t,n,s,o,r,i){const{n:a,counts:c}=n,l=s[o],u=s[r];let h=c;if(!i){const f=new(c instanceof Float64Array?Float64Array:Float32Array)($0(t,l,u,a));let d=0;for(let m=l;m<u;m++){const g=t==="mds"?c.subarray(m*a,m*a+m):c.subarray(m*a+m+1,(m+1)*a);f.set(g,d),d+=g.length}h=f}return{kind:"setup",id:e,objective:t,n:a,alpha:n.alpha,bias:n.bias??null,dispersion:n.dispersion,edges:Array.from(s.subarray(o,r+1)),values:h,shared:i}}function k4(){return new Worker(new URL(""+new URL("likelihood.worker-CUgAXfAe.js",import.meta.url).href,import.meta.url),{type:"module"})}function ls(e,t){const{nrows:n,ncols:s,values:o}=e;if(!(t>=1)||n===0||s===0)return o;const r=Math.floor(t),i=new Float32Array(n*s),a=new Float64Array(s),c=new Int32Array(s),l=(u,h)=>{if(u<0||u>=n)return;const f=u*s;for(let d=0;d<s;d++){const m=o[f+d];Number.isFinite(m)&&(a[d]+=h*m,c[d]+=h)}};for(let u=0;u<=r&&u<n;u++)l(u,1);for(let u=0;u<n;u++){u>0&&(l(u-r-1,-1),l(u+r,1));let h=0,f=0;for(let m=0;m<=r&&m<s;m++)h+=a[m],f+=c[m];const d=u*s;for(let m=0;m<s;m++){if(m>0){const p=m-r-1;p>=0&&(h-=a[p],f-=c[p]);const w=m+r;w<s&&(h+=a[w],f+=c[w])}const g=o[d+m];if(Number.isNaN(g)){i[d+m]=Number.NaN;continue}i[d+m]=f>0?h/f:g}}return i}const S4=192,C4=16e6,qa=4e6,F4=.5;function Ie(e,t,n,s){const o=n+1,r=t-e+1,i=new Float64Array((r+1)*o);for(let a=0;a<r;a++){const c=a*o,l=(a+1)*o,u=(e+a)*n;let h=0;for(let f=0;f<n;f++)h+=s(u+f),i[l+f+1]=i[c+f+1]+h}return{table:i,stride:o,origin:e}}function yn(e,t,n,s,o){const{table:r,stride:i,origin:a}=e,c=(t-a)*i,l=(s-a+1)*i;return r[l+o+1]-r[c+o+1]-r[l+n]+r[c+n]}function $4(e,t,n,s){const{nrows:o,ncols:r,values:i}=e,a=Math.max(1,Math.floor(n.floor)),c={kind:"fixed",radius:a},l=()=>({values:ls(e,a),over:s===void 0?null:ls({...e,values:s},a)});if(o===0||r===0)return{values:i,over:s??null,applied:n,scope:null};if(o*r>C4)return{...l(),applied:c,scope:"adaptive smoothing not applied — block too large"};let u=0,h=0;const f=o*r;for(let v=0;v<f;v++){const S=t[v];Number.isFinite(S)&&S>0&&(u+=S,h++)}const d=h>0?u/h:0;if(!(d>0))return{...l(),applied:c,scope:null};const m=n.target*d,g=Math.min(S4,Math.max(o,r)),p=v=>Number.isFinite(i[v])&&(s===void 0||Number.isFinite(s[v])),w=Math.max(1,Math.floor(qa/(r+1))-2*g);if(Math.min(o,w+2*g+1)*(r+1)>qa*2)return{values:ls(e,a),applied:c,over:s===void 0?null:ls({...e,values:s},a),scope:"adaptive smoothing not applied — block too large"};const y=[];for(let v=0;v<o;v+=w){const S=Math.min(o-1,v+w-1);y.push([v,S,Math.max(0,v-g),Math.min(o-1,S+g)])}const A=(v,S,k,C,B,F)=>{const $=E=>{const O=S-E<C?C:S-E,L=k-E<0?0:k-E,D=S+E>B?B:S+E,q=k+E>=r?r-1:k+E;return yn(v,O,L,D,q)>=m};let _=F<a?a:F>g?g:F;if($(_))for(;_>a&&$(_-1);)_--;else for(;_<g&&!$(_);)_++;return _},N=(v,S,k,C,B,F,$)=>{const _=k-B<F?F:k-B,E=C-B<0?0:C-B,O=k+B>$?$:k+B,L=C+B>=r?r-1:C+B,D=yn(S,_,E,O,L);return D>0?yn(v,_,E,O,L)/D:Number.NaN},M=new Float32Array(o*r);for(const[v,S,k,C]of y){const B=Ie(k,C,r,E=>{const O=t[E];return Number.isFinite(O)&&O>0?O:0}),F=Ie(k,C,r,E=>p(E)?i[E]:0),$=Ie(k,C,r,E=>p(E)?1:0);let _=a;for(let E=v;E<=S;E++){const O=E*r;let L=_;for(let D=0;D<r;D++){const q=i[O+D];if(!p(O+D)){M[O+D]=q;continue}L=A(B,E,D,k,C,L),D===0&&(_=L);const X=E-L<k?k:E-L,R=D-L<0?0:D-L,P=E+L>C?C:E+L,z=D+L>=r?r-1:D+L,U=yn($,X,R,P,z)-1,K=U>0?(yn(F,X,R,P,z)-q)/U:0;M[O+D]=K>0?Math.min(q,K*(1+F4*U)):q}}}const I=new Float32Array(o*r),x=s===void 0?null:new Float32Array(o*r);for(const[v,S,k,C]of y){const B=Ie(k,C,r,O=>{const L=t[O];return Number.isFinite(L)&&L>0?L:0}),F=Ie(k,C,r,O=>p(O)?M[O]:0),$=s===void 0?null:Ie(k,C,r,O=>p(O)?s[O]:0),_=Ie(k,C,r,O=>p(O)?1:0);let E=a;for(let O=v;O<=S;O++){const L=O*r;let D=E;for(let q=0;q<r;q++){const X=i[L+q];if(Number.isNaN(X)){I[L+q]=Number.NaN,x!==null&&(x[L+q]=Number.NaN);continue}D=A(B,O,q,k,C,D),q===0&&(E=D);const R=D>>1<a?a:D>>1,P=D+(D>>1)>g?g:D+(D>>1);let z=0,U=0,K=0;for(const G of[R,D,P]){const Y=N(F,_,O,q,G,k,C);Number.isNaN(Y)||(z+=Y,U++,$!==null&&(K+=N($,_,O,q,G,k,C)))}I[L+q]=U>0?z/U:X,x!==null&&(x[L+q]=U>0?K/U:s?.[L+q]??Number.NaN)}}}return{values:I,over:x,applied:n,scope:null}}function E4(e){return`${e.contig}:${(e.start+1).toLocaleString("en-US")}-${e.end.toLocaleString("en-US")}`}function ei(e,t){return`${e}:${B4(t)}`}function B4(e){return(e+1).toLocaleString("en-US")}function E0(e){const t=T4(e);if(t===null)return e;const[n,s,o]=t;return s===null?e:o===null?`${n}:${(s+1).toLocaleString("en-US")}-`:E4({contig:n,start:s,end:o})}function T4(e){const t=e.indexOf(":");if(t<0)return[e,null,null];const n=e.slice(t+1).replace(/–|\.\./g,"-");try{return to(`${e.slice(0,t)}:${n}`,{reversed:!0})}catch{return null}}function H(e,t,n){const s=e[t];return typeof s=="number"&&Number.isFinite(s)?s:n}function lt(e,t,n){const s=e[t];return typeof s=="string"?s:n}function nt(e,t,n){const s=e[t];return typeof s=="boolean"?s:n}function O4(e,t){const n=e[t];return Array.isArray(n)?n.filter(s=>typeof s=="string"):typeof n=="string"&&n!==""?[n]:[]}function Ht(e,t){const n=e[t];return Array.isArray(n)?n.filter(s=>typeof s=="string"):typeof n=="string"&&n.trim()!==""?[n.trim()]:null}function _s(e,t){const n=e[t];return typeof n=="string"&&n!==""?n:null}function Ds(e,t,n){return{value:e?.[n],chosen:_s(t,n)!==null}}function B0(e,t){return e.value!==void 0?null:e.chosen?t.gone:t.unchosen}function Le(e,t,n){if(e.hasContig(t))return e.contigId(t);const s=e.names.find(t,...n===void 0?[]:[{from:n}]);return s===null||!e.hasContig(s.name)?-1:e.contigId(s.name)}function no(e,t,n){const s=new Set,o=[];for(const r of t){const i=Le(e,r,n);i<0?o.push(r):s.add(i)}return{ids:[...s].sort((r,i)=>r-i),missing:o}}function _e(e,t){const n=e.framing.circular;if(n===void 0||n.length===0)return!1;if(n.includes(t))return!0;const s=e.mc.hasContig(t)?e.mc.contigId(t):-1;return s<0?!1:n.some(o=>Le(e.mc,o)===s)}function T0(e,t){const n=e.framing.origins;if(n===void 0)return;const s=n[t];if(typeof s=="number"&&Number.isFinite(s))return s;const o=e.mc.hasContig(t)?e.mc.contigId(t):-1;if(!(o<0)){for(const[r,i]of Object.entries(n))if(typeof i=="number"&&Number.isFinite(i)&&Le(e.mc,r)===o)return i}}function en(){const e=new Error("cancelled");return e.name="AbortError",e}class us{constructor(t,n){this.rows=t,this.cols=n,this.sum=new Float64Array(t*n),this.count=new Float64Array(t*n)}sum;count;windows=0;add(t){let n=!1;for(let s=0;s<this.sum.length;s++){const o=t[s];Number.isFinite(o)&&(this.sum[s]+=o,this.count[s]+=1,n=!0)}n&&this.windows++}finish(){const t=new Float32Array(this.sum.length);for(let n=0;n<t.length;n++)t[n]=this.count[n]>0?this.sum[n]/this.count[n]:Number.NaN;return{values:t,n:this.windows}}}function O0(e,t,n,s,o){const r=new Float64Array(s*o);if(t===s&&n===o){for(let l=0;l<r.length;l++)r[l]=e[l];return r}const i=new Float64Array(s*o),a=s/t,c=o/n;for(let l=0;l<t;l++){const u=l*a,h=(l+1)*a;for(let f=0;f<n;f++){const d=e[l*n+f];if(!Number.isFinite(d))continue;const m=f*c,g=(f+1)*c;for(let p=Math.floor(u);p<Math.min(s,Math.ceil(h));p++){const w=Math.min(h,p+1)-Math.max(u,p);if(w>0)for(let b=Math.floor(m);b<Math.min(o,Math.ceil(g));b++){const y=Math.min(g,b+1)-Math.max(m,b);if(!(y>0))continue;const A=w*y;r[p*o+b]+=d*A,i[p*o+b]+=A}}}}for(let l=0;l<r.length;l++)r[l]=i[l]>0?r[l]/i[l]:Number.NaN;return r}function L0(e,t,n,s,o){const r=s.length-1,i=o.length-1,a=new Float64Array(r*i);for(let c=0;c<r;c++){const l=s[c],u=s[c+1],h=Math.max(0,Math.floor(l)),f=Math.min(t,Math.ceil(u));for(let d=0;d<i;d++){const m=o[d],g=o[d+1],p=Math.max(0,Math.floor(m)),w=Math.min(n,Math.ceil(g));let b=0,y=0;for(let A=h;A<f;A++){const N=Math.min(u,A+1)-Math.max(l,A);if(!(N>0))continue;const M=A*n;for(let I=p;I<w;I++){const x=e[M+I];if(!Number.isFinite(x))continue;const v=Math.min(g,I+1)-Math.max(m,I);if(!(v>0))continue;const S=N*v;b+=x*S,y+=S}}a[c*i+d]=y>0?b/y:Number.NaN}}return a}function L4(e,t,n){const s=n<t?Math.ceil(t/n):1,o=n*s,r=o>1?(t-1)/(o-1):1,i=new Int32Array(n),a=[];for(let f=0;f<n;f++){let d=Number.POSITIVE_INFINITY,m=-1;const g=[];for(let w=f*s;w<(f+1)*s;w++){const b=w*r;if(b>t-1)continue;const y=Math.floor(b),A=b-y;g.push([y,1-A]),y+1<t&&g.push([y+1,A]),d=Math.min(d,y),m=Math.max(m,y+1<t?y+1:y)}if(m<0){i[f]=0,a.push(new Float64Array(0));continue}const p=new Float64Array(m-d+1);for(const[w,b]of g)p[w-d]+=b;i[f]=d,a.push(p)}const c=new Float64Array(t*n),l=new Float64Array(t*n);for(let f=0;f<t;f++)for(let d=0;d<n;d++){const m=a[d],g=i[d];let p=0,w=0;for(let b=0;b<m.length;b++){const y=e[f*t+g+b];Number.isFinite(y)?p+=m[b]*y:w+=m[b]}c[f*n+d]=p,l[f*n+d]=w}const u=new Float64Array(n*n),h=1/(s*s);for(let f=0;f<n;f++){const d=a[f],m=i[f];for(let g=0;g<n;g++){let p=0,w=0;for(let b=0;b<d.length;b++)p+=d[b]*c[(m+b)*n+g],w+=d[b]*l[(m+b)*n+g];u[f*n+g]=w>0?Number.NaN:p*h}}return u}function _4(e,t,n,s){const r=.5*(2*n+1-1)*(t-e);return{from:Math.floor(Ua(e-r)/s),to:Math.ceil(Ua(t+r)/s)}}function Ua(e){const t=Math.floor(e),n=e-t;return n>.5?t+1:n<.5||t%2===0?t:t+1}function D4(e,t){const n=Math.max(3,Math.round(e)),s=Math.max(0,t),o=Math.floor((n-1)/2),r=Math.min(o,Math.round(n*s/(1+2*s)));return{side:n,flank:r,body:n-2*r}}function _0(e,t,n,s){const o=new Float64Array(e.side+1);for(let r=0;r<e.flank;r++)o[r]=t-s+s*r/e.flank;for(let r=0;r<=e.body;r++)o[e.flank+r]=t+(n-t)*r/e.body;for(let r=1;r<=e.flank;r++)o[e.flank+e.body+r]=n+s*r/e.flank;return o}function Ga(e,t){const{side:n,flank:s,body:o}=t;if(s===0||o===0)return Number.NaN;const r=Math.max(1,Math.round(o/4)),i=Math.min(s,r),a=(d,m,g,p)=>{let w=0,b=0;for(let y=Math.max(0,d);y<Math.min(n,m);y++)for(let A=Math.max(0,g);A<Math.min(n,p);A++){const N=e[y*n+A];Number.isFinite(N)&&(w+=N,b++)}return b===0?Number.NaN:w/b},c=a(s,s+r,s+o-r,s+o),l=[],u=a(s-i,s,s+o-r,s+o);Number.isFinite(u)&&l.push(u);const h=a(s,s+r,s+o,s+o+i);if(Number.isFinite(h)&&l.push(h),!Number.isFinite(c)||l.length===0)return Number.NaN;const f=l.reduce((d,m)=>d+m,0)/l.length;return f===0?Number.NaN:c/f}function yr(e,t,n){const s=Math.max(1,Math.floor(Math.min(t,n)/5)),o=Math.floor(t/2),r=Math.floor(n/2),i=Math.max(0,Math.floor(s/2)),a=(u,h,f,d)=>{let m=0,g=0;for(let p=Math.max(0,u);p<Math.min(t,h);p++)for(let w=Math.max(0,f);w<Math.min(n,d);w++){const b=e[p*n+w];Number.isFinite(b)&&(m+=b,g++)}return g===0?Number.NaN:m/g},c=a(o-i,o+i+1,r-i,r+i+1),l=a(t-s,t,0,s);return!Number.isFinite(c)||!Number.isFinite(l)||l===0?Number.NaN:c/l}function R4(e,t){const n=new Float32Array(e.length);for(let s=0;s<n.length;s++){const o=e[s],r=t[s];n[s]=Number.isFinite(o)&&Number.isFinite(r)&&r!==0?o/r:Number.NaN}return n}function z4(e,t,n){const s=[];for(let o=0;o<n.perPair;o++){const r=n.minShift+Math.floor(n.rng()*(n.maxShift-n.minShift+1)),i=n.rng()<.5?-1:1,a=e.row+i*r,c=e.col+i*r;a<t.from||c>=t.to||s.push({row:a,col:c,separation:e.separation})}return s}const P4=2e3;function so(e,t){const n=e.filter(i=>i.separation>0&&Number.isFinite(i.score)).map(i=>({separation:i.separation,score:i.score})).sort((i,a)=>i.separation-a.separation);if(n.length<2)return null;const s=Math.ceil(n.length/(t.cap??P4)),o=s<=1?n:n.filter((i,a)=>a%s===0),r=q4(n);return{kind:"curve",id:t.id,label:t.label,series:[{name:`${n.length.toLocaleString()} ${t.noun}`,x:Float64Array.from(o,i=>i.separation),y:Float64Array.from(o,i=>i.score),n:n.length,mark:"points"},{name:"median",x:r.x,y:r.y,n:n.length}],xLabel:"Separation between anchors (bp)",yLabel:t.yLabel,xScale:"log",yScale:"linear"}}function q4(e){const t=e[0].separation,n=e[e.length-1].separation,s=Math.max(3,Math.min(24,Math.round(Math.sqrt(e.length)))),o=n>t?Math.log(n/t)/s:0,r=Array.from({length:s},()=>[]),i=Array.from({length:s},()=>[]);for(const l of e){const u=o>0?Math.min(s-1,Math.floor(Math.log(l.separation/t)/o)):0;r[u].push(l.score),i[u].push(l.separation)}const a=[],c=[];for(let l=0;l<s;l++)r[l].length!==0&&(a.push(Ha(i[l])),c.push(Ha(r[l])));return{x:Float64Array.from(a),y:Float64Array.from(c)}}function Ha(e){const t=[...e].sort((s,o)=>s-o),n=t.length>>1;return t.length%2===1?t[n]:(t[n-1]+t[n])/2}function ni(e){let t=(Number.isFinite(e)?Math.trunc(e):0)>>>0;return function(){t=t+1831565813>>>0;let s=t;return s=Math.imul(s^s>>>15,s|1),s^=s+Math.imul(s^s>>>7,s|61),((s^s>>>14)>>>0)/4294967296}}function U4(e){const t=new Uint32Array(624);t[0]=(Number.isFinite(e)?Math.trunc(e):0)>>>0;for(let o=1;o<624;o++){const r=t[o-1]^t[o-1]>>>30;t[o]=Math.imul(1812433253,r)+o>>>0}let n=624;const s=()=>{if(n>=624){for(let r=0;r<624;r++){const i=t[r]&2147483648|t[(r+1)%624]&2147483647;t[r]=t[(r+397)%624]^i>>>1^(i&1?2567483615:0)}n=0}let o=t[n++];return o^=o>>>11,o^=o<<7&2636928640,o^=o<<15&4022730752,o^=o>>>18,o>>>0};return function(){const r=s()>>>5,i=s()>>>6;return(r*67108864+i)/9007199254740992}}const nn=[{name:"loops",blurb:"Focal contacts between two anchors — the classic dot off the diagonal.",minDist:2e4,maxDist:2e6,maxIterations:1,maxPercZero:10,maxPercUndetected:50,minSeparation:5e3,pearson:.3,resolution:2e3,onDiagonal:!1,sizes:[17]},{name:"loops_small",blurb:"The same, at a tighter 7×7 template, for short-range dots.",minDist:2e4,maxDist:2e6,maxIterations:1,maxPercZero:10,maxPercUndetected:50,minSeparation:5e3,pearson:.5,resolution:2e3,onDiagonal:!1,sizes:[7]},{name:"borders",blurb:"Domain boundaries: the corner where one self-interacting block ends and the next begins.",minDist:0,maxDist:0,maxIterations:1,maxPercZero:10,maxPercUndetected:75,minSeparation:5e3,pearson:.15,resolution:5e3,onDiagonal:!0,sizes:[17,17,17]},{name:"hairpins",blurb:"The fold-back signal of a hairpin, on the diagonal.",minDist:0,maxDist:0,maxIterations:1,maxPercZero:10,maxPercUndetected:75,minSeparation:5e3,pearson:.1,resolution:1e4,onDiagonal:!0,sizes:[15]},{name:"centromeres",blurb:"The large-scale depletion around a centromere.",minDist:0,maxDist:0,maxIterations:1,maxPercZero:30,maxPercUndetected:50,minSeparation:1e5,pearson:.5,resolution:2e3,onDiagonal:!0,sizes:[81]},{name:"stripes_left",blurb:"An architectural stripe running left from an anchor.",minDist:6e4,maxDist:2e6,maxIterations:1,maxPercZero:80,maxPercUndetected:50,minSeparation:4e4,pearson:.17,resolution:1e4,onDiagonal:!1,sizes:[31]},{name:"stripes_right",blurb:"An architectural stripe running right from an anchor.",minDist:6e4,maxDist:2e6,maxIterations:1,maxPercZero:80,maxPercUndetected:50,minSeparation:4e4,pearson:.17,resolution:1e4,onDiagonal:!1,sizes:[31]}];function G4(e){return e.hasWeights}const si={kind:"toggle",key:"balance",label:"use balancing weights",default:!1,derive:G4},Uo=q5.filter(U5);function Rs(e){const t=e.at;return t==="start"||t==="end"?t:"centre"}function H4(e){const t=typeof e.window=="number"?e.window:21;return Math.max(1,Math.min(200,Math.round(t)))}function Mt(e){return e.mode==="scaled"&&e.norm!=="chromosight"}function Q4(e){return e.norm==="chromosight"}const Nr=2;function K4(e){const t=typeof e.minDiag=="number"?e.minDiag:Nr;return Number.isFinite(t)?Math.max(0,Math.round(t)):Nr}const D0=5;function V4(e){const t=e.minDist;return typeof t=="number"&&Number.isFinite(t)?Math.max(0,Math.round(t)):D0}function Ve(e){const t=typeof e.flankFraction=="number"?e.flankFraction:.5;return Math.max(0,Math.min(4,t))}function oo(e){return Mt(e)&&te(e)}function R0(e){return oo(e)&&e.norm==="oe"}function te(e){return e.anchors!=="pairs"&&e.anchors!=="allpairs"&&e.anchors!=="consecutive"}function Y4(e){return!te(e)&&e.symmetric!==!1}function W4(e){const t=e.tracks;return Array.isArray(t)?t.length>0:typeof t=="string"&&t!==""}function Go(e){return e.anchors==="features"&&typeof e.rankBy=="string"&&e.rankBy!==""}function z0(e){return e.anchors==="pairs"}function Qa(e){return e.anchors==="allpairs"}function P0(e){return e.anchors==="consecutive"}function X4(e){if(te(e))return 1;const t=typeof e.byDistance=="number"?Math.round(e.byDistance):0;return t>1?Math.min(q0,t):1}const q0=6,Ka=2e5;nn.map(e=>({value:e.name,label:e.name.replace(/_/g," "),hint:e.blurb}));const J4={loops:[{size:17,values:[.85312823,.84999888,.86449323,.89848067,.89731004,.8828079,.8991951,.93574241,.98626893,.94680858,.92496206,.90751005,.91428048,.87331718,.89081318,.88887735,.89812284,.87427503,.84537754,.82985454,.81558,.89584271,.88080858,.91475572,.95848669,.96150597,.98247128,.90971439,.89248861,.85932324,.80995337,.88023035,.86361754,.8646012,.8692519,.88788988,.84916643,.85686,.86176338,.87028908,.91911272,.96605567,.96166305,.96432123,.92984099,.89885302,.85358974,.85747452,.86083162,.822195,.88605451,.88360881,.81610713,.86454241,.85453919,.88771835,.90972731,.99139015,1.0591303,1.0764743,1.0508512,.94905558,.91890882,.907508,.89806679,.84979212,.80942624,.88818904,.89273526,.86257584,.8398885,.90191445,.92520181,.9669612,1.0207693,1.1247707,1.1201985,1.110737,1.0138251,.97310288,.94194931,.8933119,.87546462,.89259011,.91885526,.8873561,.87646597,.87459739,.93436395,.96454701,.96674092,1.0503075,1.1603753,1.1935154,1.1222083,1.0359268,1.0131198,.97551707,.89427219,.89454472,.89683122,.90296185,.8897287,.9113657,.92834789,.9646179,1.0079169,1.030991,1.1441288,1.2773436,1.3594681,1.2540048,1.1579206,1.0552433,1.0266774,.97582784,.92060582,.9131044,.93442846,.92655103,.98995901,.95292056,1.0712673,1.1226021,1.1346177,1.294605,1.5249332,1.6538844,1.4644965,1.2367435,1.1479659,1.1129056,1.0387143,.97745634,.95099896,.95599996,1.000965,.99160949,.99041644,1.0976704,1.1502586,1.1870256,1.3883191,1.6925473,1.7306489,1.6152215,1.330617,1.2000051,1.0901384,1.0552782,.93290965,.93140245,.97157288,.92655103,.98995901,.95292056,1.0712673,1.1226021,1.1346177,1.294605,1.5249332,1.6538844,1.4644965,1.2367435,1.1479659,1.1129056,1.0387143,.97745634,.95099896,.95599996,.8897287,.9113657,.92834789,.9646179,1.0079169,1.030991,1.1441288,1.2773436,1.3594681,1.2540048,1.1579206,1.0552433,1.0266774,.97582784,.92060582,.9131044,.93442846,.8873561,.87646597,.87459739,.93436395,.96454701,.96674092,1.0503075,1.1603753,1.1935154,1.1222083,1.0359268,1.0131198,.97551707,.89427219,.89454472,.89683122,.90296185,.89273526,.86257584,.8398885,.90191445,.92520181,.9669612,1.0207693,1.1247707,1.1201985,1.110737,1.0138251,.97310288,.94194931,.8933119,.87546462,.89259011,.91885526,.88360881,.81610713,.86454241,.85453919,.88771835,.90972731,.99139015,1.0591303,1.0764743,1.0508512,.94905558,.91890882,.907508,.89806679,.84979212,.80942624,.88818904,.8692519,.88788988,.84916643,.85686,.86176338,.87028908,.91911272,.96605567,.96166305,.96432123,.92984099,.89885302,.85358974,.85747452,.86083162,.822195,.88605451,.87427503,.84537754,.82985454,.81558,.89584271,.88080858,.91475572,.95848669,.96150597,.98247128,.90971439,.89248861,.85932324,.80995337,.88023035,.86361754,.8646012,.85312823,.84999888,.86449323,.89848067,.89731004,.8828079,.8991951,.93574241,.98626893,.94680858,.92496206,.90751005,.91428048,.87331718,.89081318,.88887735,.89812284]}],loops_small:[{size:7,values:[.96674092,1.0503075,1.1603753,1.1935154,1.1222083,1.0359268,1.0131198,1.030991,1.1441288,1.2773436,1.3594681,1.2540048,1.1579206,1.0552433,1.1346177,1.294605,1.5249332,1.6538844,1.4644965,1.2367435,1.1479659,1.1870256,1.3883191,1.6925473,1.7306489,1.6152215,1.330617,1.2000051,1.1346177,1.294605,1.5249332,1.6538844,1.4644965,1.2367435,1.1479659,1.030991,1.1441288,1.2773436,1.3594681,1.2540048,1.1579206,1.0552433,.96674092,1.0503075,1.1603753,1.1935154,1.1222083,1.0359268,1.0131198]}],borders:[{size:17,values:[1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,.5,.5,.5,.5,.5,.5,.5,.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,.5,.5,.5,.5,.5,.5,.5,.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,.5,.5,.5,.5,.5,.5,.5,.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,.5,.5,.5,.5,.5,.5,.5,.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,.5,.5,.5,.5,.5,.5,.5,.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,.5,.5,.5,.5,.5,.5,.5,.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,.5,.5,.5,.5,.5,.5,.5,.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,.5,.5,.5,.5,.5,.5,.5,.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5]},{size:17,values:[1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5]},{size:17,values:[.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,.5,.5,.5,.5,.5,.5,.5,.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,.5,.5,.5,.5,.5,.5,.5,.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,.5,.5,.5,.5,.5,.5,.5,.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,.5,.5,.5,.5,.5,.5,.5,.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,.5,.5,.5,.5,.5,.5,.5,.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,.5,.5,.5,.5,.5,.5,.5,.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,.5,.5,.5,.5,.5,.5,.5,.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,.5,.5,.5,.5,.5,.5,.5,.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5]}],hairpins:[{size:15,values:[.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,1.75,1.75,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,1.75,1.75,1.75,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,1.75,1.75,1.75,1.75,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,1.75,1.75,1.75,1.75,1.75,.5,.5,.5,.5,.5,.5,.5,.5,.5,1.75,1.75,1.75,1.75,1.75,.5,.5,.5,.5,.5,.5,.5,.5,.5,1.75,1.75,1.75,1.75,1.75,.5,.5,.5,.5,.5,.5,.5,.5,.5,1.75,1.75,1.75,1.75,1.75,.5,.5,.5,.5,.5,.5,.5,.5,.5,1.75,1.75,1.75,1.75,1.75,.5,.5,.5,.5,.5,.5,.5,.5,.5,1.75,1.75,1.75,1.75,1.75,.5,.5,.5,.5,.5,.5,.5,.5,.5,1.75,1.75,1.75,1.75,1.75,.5,.5,.5,.5,.5,.5,.5,.5,.5,1.75,1.75,1.75,1.75,1.75,.5,.5,.5,.5,.5,.5,.5,.5,.5,1.75,1.75,1.75,1.75,1.75,.5,.5,.5,.5,.5,.5,.5,.5,.5,1.75,1.75,1.75,1.75,1.75,.5,.5,.5,.5,.5,.5,.5,.5,.5,1.75,1.75,1.75,1.75,1.75,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,1.75,1.75,1.75,1.75,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5]}],centromeres:[{size:81,values:[1.2281821,1.5665572,1.4762153,1.2397266,1.635678,1.5984158,1.1191438,1.559039,2.4802534,1.6591749,1.3981162,1.5282018,1.8675983,1.8368934,1.774249,1.3852965,1.8730091,1.6371324,1.586814,1.582305,1.1812525,1.6710035,1.6708221,1.3683022,1.5944455,1.5520679,1.6823268,1.4757592,1.6575846,1.5005167,1.9364187,.77497146,1.1736426,.7974988,1.3514356,1.2565539,1.0873172,.66686457,1.3130565,.55905722,.82774648,1.0764362,.84438987,.98804222,.9512087,1.060165,1.153551,1.6506198,.83026034,1.2553665,1.2039925,1.826186,1.5281806,1.9149942,1.8528276,1.385265,1.4134311,1.3303022,1.4539632,1.3152465,1.7132557,1.8423132,2.1014836,1.6568267,2.1174016,1.4646016,1.4565563,1.6645845,1.6118399,1.9165392,1.5504494,1.6615528,2.5067972,2.3427305,1.6964471,2.1217943,1.7786356,1.771775,2.1123204,1.7533281,1.6200988,1.1142446,1.1891177,1.7021685,1.639686,1.4593039,1.2757495,1.2551392,1.1521365,1.4428055,1.5304253,1.6840202,1.7142015,1.3704876,1.4896554,1.9912845,1.8217913,1.8117989,1.6721571,1.429595,1.5178622,1.5898692,1.9172806,1.5962391,1.6772917,1.4224527,1.4945697,2.2835728,1.466699,1.6035575,1.5943082,1.1777293,1.3931749,1.2491393,1.4212451,1.2184207,1.0032095,.92111424,1.0393002,.8825064,.79913533,1.0245933,1.0570704,.90549327,.8787752,.91563503,1.0011201,1.4246769,1.1501216,1.2888828,1.1041855,1.696173,1.1296678,1.5008347,1.6407639,1.4500274,1.515316,1.6536225,1.3547914,1.3714829,1.5561923,1.8723597,1.5399201,1.6435546,1.5183806,1.4932029,2.1514542,1.8814164,2.802379,2.1633295,1.6002091,1.6440576,2.1647862,1.830919,2.1789371,3.0088628,1.935386,1.5612024,1.5137108,1.4917002,1.8859538,2.0889227,1.2653756,1.2882204,1.436168,1.7437268,1.3614143,1.7534004,1.286542,1.6634342,1.3038817,1.627952,1.3141127,1.7861668,1.993894,2.0019478,1.2589192,1.5552105,1.3838447,1.7378047,1.5095603,1.9744936,1.483336,2.113924,1.7916639,2.2426275,1.6007411,1.2886184,1.4264287,1.2244309,1.7650874,1.4205113,.97194903,1.2512782,1.3192343,1.2608687,.93649722,1.0101217,.79693481,.66068132,.81619306,1.1270831,1.002784,1.004373,.7460736,.95066683,.99090385,.72778255,1.393795,1.3307624,1.0262551,1.1274217,1.0601048,1.8038424,1.4231768,1.4432051,1.2320833,1.4569727,2.0010981,1.5366687,1.6542766,1.5540459,1.3044308,1.7356108,1.5684255,2.2121276,1.7177643,1.2819309,1.6424472,1.875896,2.20556,1.7448882,1.235719,1.664337,1.908706,1.439897,1.6938442,1.6965743,1.8139262,1.2152072,2.0635421,1.7899035,1.9085503,1.4717358,1.8123437,1.3793539,1.508161,1.613869,1.3818616,1.2832351,1.3862234,1.3115369,1.3360966,1.560245,1.7229718,2.0436006,1.458418,1.3780453,1.7767131,1.507528,1.7311624,1.8606006,1.5818158,1.6381555,1.446764,1.600451,1.0989136,1.2589428,1.9540058,1.4195192,1.2222831,1.3252366,1.9032141,1.6887659,1.1926241,1.7335094,1.0989058,1.2302038,1.236687,.73329478,.84531655,.92779841,.88813244,.72984081,.87727784,1.130269,1.0297311,.82484452,.85460535,.94327788,1.2437314,1.1746982,1.4538097,1.2926531,1.2188052,1.5403496,1.4188448,1.7986682,1.5249981,1.7523987,1.9165484,1.8156769,1.6500994,1.2770293,1.7436501,2.088007,2.6148127,1.2488737,1.8443432,1.5969489,1.6326269,1.9893896,2.1291717,2.4289678,1.6119557,1.6056789,2.1228301,1.3976315,1.7689038,1.961196,1.7038397,2.5312481,2.0318371,2.1387169,1.0463385,1.1705493,1.3610896,1.2953676,1.0248097,1.7215619,1.2272949,1.723823,1.6528966,1.250391,1.1906423,1.561404,1.3797916,1.5852771,1.4151383,1.7869095,1.6341682,1.3648291,2.1641315,1.6603348,1.9343634,1.3871896,1.6664489,1.3139517,1.7663311,1.4575318,1.3906609,1.6209767,1.2610888,1.3671377,1.3220515,1.6884327,1.6723102,1.3172839,1.4343311,1.0677345,1.2205866,.99977567,1.1954819,.83141566,1.0253346,.82844489,.89418014,1.1647021,.90523327,.69101007,1.0894265,1.1501319,1.2677724,1.2699685,1.3907603,1.4418155,1.6205801,2.0170201,1.3784159,1.6021307,1.6869586,1.6336618,1.7067967,2.4468134,1.844659,1.9859419,1.6549902,1.2099298,1.6607599,1.9804752,1.7192682,1.8559679,1.7126817,1.8062766,1.8028094,2.1401791,1.683478,2.1551589,2.1193484,1.8661304,2.0174786,2.2214514,1.5978211,1.3854038,1.2656458,1.4859479,1.6040474,1.2464047,1.2730952,1.3796375,.96158352,1.5179224,1.8743632,1.4643206,1.6832089,1.5515785,1.6055125,1.7445488,1.862286,1.5491368,1.3624474,1.6119064,1.7074539,2.3156789,2.2985598,1.9235266,1.6576506,1.8274361,1.8219517,1.9124709,1.799844,1.4789926,1.2060526,1.2903157,1.6558607,1.6150645,1.6487009,1.1791305,1.1926907,1.4351337,1.2679291,1.0158968,.92592944,.75581679,1.1352978,.82153411,1.0007804,.92663681,1.0171561,1.1634271,1.0762344,1.2042936,1.3604298,1.3490448,1.1579155,1.2690319,1.7705364,1.7218791,2.2015845,1.4287451,1.5850552,2.0926797,1.5189462,2.3673894,1.7228599,1.8850001,1.4887195,1.7528845,1.9097913,1.5012268,1.5452881,1.8121949,1.9665804,1.8487535,1.4795083,2.0728987,1.8907705,2.2612062,2.7537088,2.0293423,2.5500304,1.9742728,1.8874839,2.007173,1.9113256,1.6338367,1.3350004,1.6093612,1.3257089,1.3066841,1.2581263,1.0365617,1.1421176,1.4151261,1.3789035,1.579346,1.611804,1.5175564,1.4924479,1.566885,1.9519575,1.8140691,1.985468,1.4742501,1.3554018,1.4682532,2.2356139,1.6621601,1.8262554,1.7778474,1.7725874,1.4244114,1.7300352,1.8371789,2.104406,1.3607506,1.6998209,1.1803949,1.4845493,1.087594,1.2450712,.81127339,1.1827991,.88222719,.90203792,.84604752,.85304682,.75612027,.99922702,1.2599842,.87687157,1.5081939,.98047087,1.2154685,1.3645392,1.0278316,1.3450874,1.7477078,2.3201213,1.6606412,1.7111159,2.4422814,1.5996084,1.6906186,1.6328091,1.6007609,1.8818963,1.5296549,1.9085197,1.8377088,1.8927013,2.4795272,1.9323645,1.9869591,1.7035839,1.919089,1.8148035,1.7302709,1.4815153,1.5841561,1.6922544,1.9853494,1.9113969,2.3635356,2.5781922,1.8684073,1.9273321,1.3888996,1.16525,1.3951398,1.1518959,1.5884502,1.1116862,1.5847148,1.5489369,1.7048899,1.8260978,1.5973448,1.4765868,2.2349854,1.714829,1.8828616,1.5839577,2.0294049,1.698403,1.8761481,1.8398768,2.1351538,2.3300878,1.9213591,1.7422279,2.0318438,1.9443019,1.8530272,1.5796115,1.8278486,1.5622321,1.6671014,1.8839342,1.6375991,1.4479651,1.3310351,1.0633794,1.4914018,.94964838,1.0430462,.95611353,.93278743,1.0904492,1.3084238,1.3913446,1.339618,1.3237439,1.473057,1.1172476,1.3599359,2.0216424,1.465996,1.4795925,1.7805525,1.8687939,1.7478032,2.1702265,1.7689065,2.5504444,1.808126,1.4951132,1.7454758,1.7351118,1.7278428,1.8810956,1.8253533,1.8974336,2.2131159,1.87621,2.1697527,1.9828608,2.3852555,1.9192996,2.039803,1.7642199,2.5191192,1.9039362,2.8849176,1.8894482,1.822512,2.0449776,1.9508479,1.3851861,1.5838111,1.3428709,1.3804065,1.3009546,1.4232557,1.5050843,1.4508404,1.5369379,1.4068913,1.8025772,1.4522079,1.8348206,2.4397466,1.8612876,2.1105862,1.5424866,1.7611775,1.829651,2.0527963,2.0009949,2.1535336,1.9658227,1.2884804,1.8677063,2.4132432,1.5548323,1.8566468,2.1679707,2.176818,2.0091718,1.7847282,1.5977346,1.5571175,1.6981142,1.0740788,1.4925847,1.160238,1.095743,1.3577438,1.2746748,.87894146,1.0822266,1.2110714,1.0400777,1.131795,1.1426784,1.1213092,1.6466643,1.5691323,1.8709124,1.6881095,1.3581907,1.8956749,1.5102003,2.0871289,1.7838157,1.5408709,2.1896332,1.7226656,1.6179185,2.8293784,1.7330855,1.957682,2.0346655,1.9221937,1.7223971,1.5972463,1.9361893,1.9319625,2.2871439,1.8569278,1.8148895,2.0745643,2.0657642,1.5723105,1.9182782,1.8038725,2.2353551,1.9939921,2.0206593,1.7988806,1.3982423,1.1747897,1.2994286,1.4016155,1.4093024,.96687983,1.3277113,1.3894779,1.6831804,1.6894799,1.7755053,1.8315352,2.1090953,1.8594914,1.7148616,1.5875902,1.5850218,1.6426416,1.5850378,1.7510098,1.8077193,1.7298094,1.8856004,1.7227693,2.209243,1.7519225,1.2271658,1.7335581,1.6458039,2.2460944,1.2994318,1.4802563,1.8950261,1.0364234,1.2515673,.66252274,1.2283045,.97173188,1.134374,1.09104,.97425805,1.2715438,1.4059462,1.2011455,.93735762,1.2652993,1.0615703,1.3271563,1.0748616,1.7672229,1.2732235,2.1903528,1.7783523,1.6515845,2.1073992,1.7080223,2.7413453,2.0114994,2.273893,1.6673248,1.9709255,1.6759152,1.8492865,1.6426177,1.8251729,1.6958557,1.7151299,1.8145959,2.0592947,2.0668286,1.5571022,2.4473328,2.1197413,1.4871664,2.2371904,2.6019438,1.6269761,2.5724959,2.171419,1.8867702,1.3011648,1.6058634,1.2794605,1.1385628,1.8482499,1.5360197,1.729164,1.3665132,1.658138,1.3848169,1.6089256,2.0301362,1.8384875,2.2911403,1.8820747,2.3757763,1.7114301,1.8261708,1.7040414,1.7842767,1.5048127,2.6808743,2.1360954,2.2111987,1.6227312,1.8569816,2.1859847,1.880047,2.0560867,1.6916072,1.7646106,1.7667278,1.7352207,1.4893796,1.2628871,2.348805,1.4622192,1.2501143,.95730636,1.021558,.8335584,1.0402648,1.216886,1.1699272,1.335345,1.2303404,1.6594952,1.566163,1.3729238,1.9305437,1.8977699,2.0418087,2.0898778,1.9965275,1.8061095,1.7717002,2.0137702,1.373344,1.9660341,2.3979575,1.8508722,1.7363805,1.4894547,2.1344598,2.058871,1.8372765,2.0875807,1.7086021,1.6427717,1.5847233,1.4610139,1.8679585,1.6271029,2.580351,2.5102036,2.2195124,2.0551551,1.6232778,2.0402827,2.0967432,1.9106916,1.5564293,2.0628769,1.1160338,1.7733892,1.6869719,1.6864402,1.8082074,1.8250731,1.8907896,1.4485662,1.7532252,1.9866841,1.7308821,1.8393791,2.0070016,1.6668975,2.3154035,2.5732324,1.8469527,2.481187,2.4683161,2.6018325,1.6662509,2.7406655,1.9282628,1.7686076,1.7964852,1.3553925,2.0367599,2.9438648,2.9661425,1.7069741,1.7378013,2.0120382,1.5178378,1.46907,1.9991083,1.301083,1.1298421,1.4151063,.81451338,1.2869426,1.5404901,1.3913848,1.3971248,1.18675,1.1296599,1.5678574,1.8027911,1.5745133,2.3497138,1.9799344,2.0048491,1.8559884,1.4185151,1.6679042,2.4730395,1.8462868,1.8456426,2.4281857,2.2020008,2.411478,2.2818341,1.8633196,1.8932787,2.2789318,1.944635,1.7871921,1.7499025,1.5618166,1.8835028,1.3829753,2.0401064,2.2925753,2.539569,2.6714908,1.6314713,2.5406506,1.7785248,2.1109043,1.3598737,1.6092047,1.3169494,2.4825281,1.6557949,2.0776093,2.2698974,2.0211097,2.2204884,1.7903639,1.8578211,1.8313128,1.9219363,2.0618585,2.6322246,2.3104224,2.3507939,1.5242865,2.5784967,1.632481,2.123599,2.1901284,2.2728947,1.8640019,2.6262222,2.2030846,1.9803192,2.3606338,2.1471387,2.0391922,1.9227836,1.8741521,1.8355501,1.5323782,1.9299325,1.354171,1.5001559,1.2631218,1.5976992,1.4785029,1.0034022,1.1290856,1.3022414,1.7698572,1.3905626,1.3731824,1.624604,1.8550657,1.9723663,2.0495023,2.2467138,1.9595726,2.1096505,1.8632877,2.0176779,2.5621678,2.4075573,1.8180857,2.6465756,1.9605823,2.2771967,2.1163069,1.9386753,1.7247073,2.0010282,1.8549208,1.5712025,1.921265,1.9280373,1.9338265,2.0879407,2.0506003,2.1127958,1.8003571,2.2878993,2.8865805,2.0052722,1.9004884,2.1782886,1.9820993,1.7875423,1.9661973,1.8724601,1.5974332,1.3361324,1.3912742,1.4647316,1.2579849,1.4850515,1.3075636,1.6482255,1.8531702,1.2797063,1.6027614,1.5516375,2.0038152,1.6038294,1.4639799,2.056078,1.7761721,1.3799626,1.9920462,2.2494585,1.9465089,1.8858784,2.357125,2.4795207,1.520614,2.1945038,1.8087004,2.3974498,2.6052038,2.5246852,1.6118728,2.1543642,1.4046371,1.8037037,1.4189373,1.4955668,1.4886637,1.109131,1.4181381,1.5512052,.98014297,1.5120601,1.2609024,1.3139663,1.4515051,1.6801646,1.3726141,1.8723222,1.5998268,2.3024936,2.1112624,2.5997268,2.1680394,2.1572821,1.7299494,2.747826,2.2331623,2.3375018,1.6993166,2.1272967,2.4238072,1.9369145,1.7835651,1.8600808,1.700759,2.0623853,1.7302959,1.6322612,2.1551828,1.7826019,2.2375474,2.1145817,2.3048437,1.9998658,1.7866584,1.3621175,1.6107703,1.8981502,1.8615933,1.5854527,1.7139005,1.4988466,1.7344541,1.2542622,2.0428705,1.5765679,1.5347404,2.3721283,1.8007967,1.45334,1.62937,2.1091977,1.6265674,2.0608064,2.0460502,1.9422756,1.7060555,1.8609178,1.9771171,2.2449454,2.4012673,2.1314498,1.6640012,2.1211634,2.1399437,2.2886097,2.4201544,2.0182026,1.7541029,2.1445411,1.7085882,1.8812964,1.8662716,1.6281113,2.0641524,1.7977273,1.7412378,1.186685,1.3722729,1.5900902,1.2592395,1.4960771,1.445406,1.3924462,1.5080094,1.4398482,1.7253682,1.5418885,2.0553236,1.8113401,2.1951563,1.8035779,2.2288797,2.1038527,1.8384735,2.4821069,2.3219637,1.8415634,2.1968535,1.9975513,2.2521239,1.5390849,1.7606167,2.2121964,2.1697868,1.9602222,1.9059157,2.3069714,1.9478026,1.8667029,1.4986946,2.5676031,1.666288,1.8131773,1.9887553,2.0867931,2.6275939,2.2669632,1.8108865,2.0223422,1.5946431,1.9891064,1.4660185,1.7499676,1.8063468,1.5866347,1.6483613,2.0853781,2.1786791,1.8920358,1.855485,2.2199609,2.0670894,1.5739795,1.6386028,2.0758797,1.7777706,2.5551276,2.1775251,1.8270255,2.2081997,2.2280135,1.7901753,2.0485942,2.1285759,1.7429559,2.1723832,1.9713098,2.4371206,3.1621103,1.8153116,2.0342798,1.6099036,2.5080099,2.0024782,1.4630344,1.3921135,1.6487539,1.150136,1.3265862,1.6617193,1.3560241,.9635006,1.5488801,1.717694,1.3589154,1.5738609,1.6866387,1.694184,2.0887199,1.7775249,1.8401456,2.1639192,2.2667839,2.2220107,2.3617477,2.2893369,1.911468,1.9301181,2.7194445,2.0549137,1.8989069,2.4435376,2.0833026,1.9042328,1.5199393,2.0827152,1.5646561,1.8515286,1.6402446,1.6788955,2.2600775,2.036186,2.3015187,2.5470493,1.9202701,2.0515152,1.9501422,1.9348411,1.4258656,1.5235425,1.2739153,1.1003172,1.5356543,1.7067769,1.4537106,1.9829513,1.4658087,1.3233433,1.6140262,1.8096622,1.8792628,1.9525516,1.7605103,1.777209,1.4840584,1.5020231,1.6423392,2.4509537,2.1417676,2.0904689,1.7093996,2.0649807,2.4487386,2.0149022,2.0265372,2.378787,2.0621637,1.8725509,1.9168712,1.9864029,2.1747451,2.3404017,1.9249811,2.1432794,1.5967608,1.427104,1.2752407,1.0821453,1.3172125,1.1161174,1.3744274,1.589317,1.2648489,1.9120596,1.7421771,1.8024597,1.6181186,2.082684,1.6464282,1.9906183,1.9398746,2.7860802,1.6440761,2.3002995,2.0444746,2.6238451,1.4796471,2.1687299,2.3942628,2.5492927,1.4822847,2.2432685,1.8026255,2.4541595,1.690559,1.6916939,1.7312464,1.9975241,1.8360942,1.9203694,1.7258471,1.6550545,1.9404048,1.5680278,2.3295684,1.9311543,1.7223234,2.1371627,2.209418,2.1406123,1.6520289,1.9557166,1.4806218,1.2748748,1.5528585,2.2967126,1.4485314,1.7751319,1.6767038,1.7065835,1.8669224,2.0928929,1.4316223,1.3591718,1.4569057,1.7022805,1.9991672,1.943906,1.9021255,2.1962119,2.0691005,2.3358303,2.3733606,2.2405565,2.3444749,1.9317766,2.2105853,2.4278401,2.2477531,2.1392544,2.0858019,1.919679,2.2372282,2.1790176,1.7989086,1.697079,2.2141439,1.2199302,1.6621833,1.5258387,1.6204141,1.7047009,1.1475579,1.4016421,1.4257828,1.7798818,1.7647437,1.6891118,2.1559824,2.6803148,1.7490262,1.8216787,2.1385188,3.1828935,2.1445446,2.1402807,2.4344089,2.0976946,2.3855514,2.4192591,1.9638012,3.4296965,2.2181325,1.9187192,1.9156968,2.0010099,2.5055484,1.8282979,2.1422122,1.9891253,2.5418293,1.8165416,2.1135667,2.2239178,2.1720424,2.281256,1.7872628,1.9292377,1.8726749,2.411853,1.4428875,1.8689599,2.0699108,1.4724327,1.1648855,1.7898585,1.6696796,1.5257173,1.7457196,1.55345,2.0584473,1.7119168,2.080426,2.1235502,2.0454498,1.9216182,1.836803,1.7591695,2.3250603,2.0341132,1.9347802,2.3621773,2.1165671,2.5321812,2.3021188,1.9192056,2.1888573,2.0055395,1.9438353,2.6722729,2.2038845,2.4788169,2.5334777,2.8191994,2.1659872,2.2552939,2.2188354,2.033484,1.7510865,1.6545647,1.3848707,2.2269795,1.8365123,1.896057,1.3111991,1.5356641,1.7464184,1.9756153,1.741086,1.8118577,2.6279301,2.9741027,2.8293177,2.1791196,3.3631801,2.4370861,2.8523719,2.5107038,1.9608018,1.8589908,3.0585591,1.9928571,1.7590304,2.2209178,1.7866462,2.0750702,1.6195619,2.4172739,2.4689555,2.2470296,1.8764299,2.0697265,2.3885711,2.2596874,1.4989627,1.8380605,1.7486996,1.8754964,1.841658,1.7227006,1.6077899,1.5281837,2.1918319,2.015833,1.755589,1.4220975,1.0626261,1.7837637,2.3005332,1.8938708,1.8940734,2.1810068,1.7514677,1.5933059,1.8906492,2.0655648,1.5660421,2.3578016,2.3465774,2.0251159,2.1988303,2.2061295,2.9528427,1.9351881,2.1850452,2.4060322,2.8939109,2.1030341,2.5618035,2.3749976,2.2276088,3.2061878,2.442885,2.7919725,3.4230152,1.9974057,1.5450538,2.9474237,3.3334954,2.2293925,1.7585186,1.8382322,1.6370776,1.3873455,1.2752018,1.7779439,1.9677266,2.1707617,1.8827576,1.9711621,2.2161963,2.2273938,2.5782575,2.5090481,3.5086867,3.0378798,3.1581745,2.5105761,2.8783539,1.9834471,2.444037,2.5608947,2.3061983,2.417654,2.3449189,2.0240786,1.8838864,2.3217362,2.3870858,2.6258438,2.6383181,2.3952262,2.741563,2.4590861,1.9852029,1.5471138,1.7465702,1.6394023,2.2998458,1.8832322,3.0936222,2.1275746,1.844841,2.5894287,1.7121675,1.0873471,.9597012,1.0680838,1.5501898,1.4853986,1.2590668,1.7669554,1.7058274,2.6452054,2.3665054,1.8311615,2.2413756,2.1054813,1.6662598,1.8722419,1.742039,2.0366717,2.0200095,2.377686,2.7743702,2.5203395,2.5154162,2.5742551,2.8723272,2.4960954,2.6179972,2.1023842,2.9382743,2.4420256,2.8464432,2.9420979,2.7317914,2.3346609,2.0609107,2.9819565,2.0090961,2.1298859,1.3841104,2.074271,1.731858,1.8439934,1.7475616,1.7886092,2.0780083,1.8436749,2.4918928,2.7544019,2.4839501,1.9692245,2.9576478,3.124483,2.378462,2.7013245,3.0343746,1.9940908,2.8425862,2.4075789,3.4888004,2.7485761,2.3640521,2.3280131,3.2404513,2.4185709,1.6037517,1.9763805,2.4490207,1.6783794,1.7069089,1.7816318,2.7421833,2.0077337,2.1641975,2.6074255,1.4520996,1.9971335,1.589016,1.5288566,2.2849562,1.916112,1.7852681,1.6932169,1.5644811,1.8461885,1.4740217,1.5353087,1.4435496,1.667283,1.4184303,1.5150715,1.5997691,2.4286995,2.2960023,1.4996206,2.2123563,2.571866,2.2840284,2.1849291,2.28903,1.8801851,2.4869987,1.5263324,2.4249944,2.6473655,2.2659979,2.6208782,1.6519463,1.7516078,2.6741596,2.7859941,2.5028466,2.0301106,1.8780133,2.5860232,1.9797314,2.9591883,2.2598864,2.2896379,1.8335645,1.8970387,1.3879799,1.6356119,1.5539179,2.0317942,1.9211543,2.0655289,2.0138479,2.3520605,2.4659114,2.3941466,2.3148675,2.6733819,2.508712,2.7401113,2.6391292,3.154632,3.0573719,2.6623727,2.3921411,1.9751008,2.1905461,2.3488614,2.2661984,2.0918858,2.7353344,2.4242662,2.3788889,2.1835833,1.835015,2.1033608,1.9246432,1.9446678,2.0967985,2.1031367,1.8713073,1.7439494,1.5330889,2.2915008,1.6671249,1.4994063,1.4706023,1.4989097,1.8731205,.84773652,1.0611147,1.3593184,1.3348391,1.5541516,2.0593954,1.7921462,2.4617001,1.4675667,1.5795162,2.1088454,1.865124,1.9729475,1.5304767,1.7607666,1.7153861,1.9726072,2.3555012,2.1485626,2.1297021,2.6277326,2.1135982,2.0915791,2.0864236,2.5632355,2.2148316,2.1824093,2.3124868,1.7737303,2.9306318,2.1743802,2.8885327,2.3316538,3.1168857,2.7765628,1.9287301,2.4258611,2.0465354,1.8802128,2.000909,2.3080008,2.0863801,1.9581656,2.4920112,1.8596017,2.5916332,2.8331679,3.0560084,2.877613,3.4440431,2.8817694,3.0966025,3.0537488,3.7618863,3.4335838,3.1653225,2.6342327,2.7377056,2.4026667,2.8696677,2.354813,2.3838334,2.6835464,1.8595803,1.4514915,2.2350414,1.9650879,2.7664911,1.9905527,1.2666729,1.9680398,1.5914933,1.8435444,1.8932994,1.7493175,1.6015962,1.6973333,1.7894987,1.7761613,1.7140721,1.8598816,1.2630901,1.2136591,1.1445975,2.04163,1.5336287,2.4661758,1.5143799,1.9747417,2.1871939,1.7786471,2.0404474,1.9412317,2.7339813,2.1882374,1.4554871,2.1971275,1.9671433,1.9489456,1.8099708,1.7965716,2.4031256,2.1912059,2.3972898,2.5408939,3.1349214,1.9814435,2.3620222,1.8185854,2.4183876,3.0843814,3.2342489,2.2808942,2.616829,2.3899506,2.4697832,2.3815759,1.9734426,2.1642553,2.1531487,2.1698642,2.0108869,1.7368162,2.0135599,2.5462553,2.5028847,2.5344925,2.4045236,2.9224721,2.3833379,3.2440807,2.4146364,2.9347184,2.4499489,2.6315466,2.5569228,3.0943671,2.4861166,3.1434749,3.0162861,2.3572044,2.172593,2.5004521,2.3911892,2.1301252,2.4301905,2.42793,2.2900369,2.0378527,1.8995397,2.3042441,2.3535152,1.9172648,1.4450711,2.1047915,1.5364107,2.5918446,1.9165941,2.0392799,1.5782427,1.6639782,1.5317094,1.643587,1.1175138,1.507933,1.5043575,1.6712869,1.5043591,1.5370548,1.7843644,1.9397588,2.0612905,2.0445956,1.9190101,2.0661538,2.2679805,1.9570542,1.6407511,1.7936704,2.020946,2.1045915,1.8976704,1.9395738,2.9360897,2.4612322,3.4064635,2.0272479,2.683631,2.305027,3.2658061,2.7684165,2.4735025,2.653852,2.4986542,2.3715001,2.6074464,3.311486,3.4469258,3.6014324,2.7895176,2.130839,2.8020695,2.0037273,1.8256267,2.1493285,2.7084837,2.3403278,2.6811487,2.3698593,2.8263404,2.9109505,3.8771676,2.7993633,2.2481354,3.2821948,2.978187,3.2099335,3.2599431,3.5726807,2.8264471,2.3365349,2.1403411,1.9695896,2.0970815,2.7356191,1.8963325,2.1022634,2.7445048,1.8721117,1.9801716,1.599216,2.3041738,1.8792593,1.4747684,2.1888333,2.4790958,2.1507264,1.863112,2.0666211,1.9978507,1.605062,1.4232421,1.2327922,1.1800311,1.4001844,1.0665592,1.6500993,1.6842617,2.7907296,1.5044087,1.3030804,2.0881718,2.1854146,1.7404497,2.2260219,1.7009805,1.8081162,2.1313642,1.7911023,2.2802655,1.8717105,2.3168036,2.9885858,2.4661196,1.9700915,2.4814966,2.8105738,2.9273212,2.7140949,3.360417,2.4397391,2.8983077,2.598895,2.7227676,3.5910973,2.8929164,2.708292,3.7042207,2.6721962,2.6271298,2.6236689,2.2994531,1.8467222,2.0523061,2.4897062,2.058532,2.7792926,3.729212,2.5832676,3.2394511,3.5441683,2.8361609,3.5454153,2.9619168,2.7300537,3.1952096,3.0142051,3.1137576,2.4495988,2.9029301,2.6832959,2.4333367,3.5439673,2.6470999,3.01574,2.5726837,1.9601275,2.0288181,1.8987811,2.1415597,1.6578427,1.5348636,1.7424476,1.6857441,1.8149128,1.7694327,1.8060193,2.124638,2.1436623,1.2085687,1.5681836,1.1676068,1.2068787,1.5134001,1.3699585,1.8168436,1.998418,1.4182461,1.3736205,1.0766686,2.3354895,1.7255246,1.5936624,2.0097943,1.1081989,1.7049025,1.8121871,1.6545462,1.9137091,1.7064872,1.6758733,1.6718639,1.732117,2.1372224,2.3591479,2.073107,1.8190087,2.7451348,2.5845399,2.6583272,2.8643913,3.132109,2.9711634,2.5742807,3.389419,3.3284998,3.5789808,2.7603816,3.1299594,2.2631898,2.8798282,2.2956784,2.9142621,1.7280724,4.1251128,2.2754297,2.7754543,3.6148421,2.865413,4.9767799,3.2202068,2.6132365,3.8040087,3.9119489,3.7616924,3.1551533,3.6539951,3.0018449,2.2459401,3.5033318,3.7510811,2.2952443,3.1656711,3.0389512,2.5131781,2.6598408,2.3247605,2.7127605,1.9406629,2.0774982,2.4853882,1.6424382,1.985548,2.2220769,1.4932767,1.7042456,1.9382821,1.6457722,2.1506498,1.6867559,1.9208491,1.2502171,1.7172333,1.5370917,1.28841,1.2931266,1.4535656,1.8788757,1.4579123,1.8008683,1.405515,2.0078792,1.6222105,1.4693958,1.8374059,1.6966655,2.0028594,2.0647571,1.9387442,2.4313784,1.6394709,2.2546965,1.5815291,2.5434306,2.4142652,2.3663972,2.6853704,3.7958601,2.5025552,3.0636508,2.8942133,3.0111135,3.1125328,3.7685764,3.0946191,4.0056906,3.9557224,3.3072263,2.6786686,3.0158187,3.0447841,2.8024415,2.9651038,2.2437694,2.3913493,2.7048347,2.8948834,2.577219,3.2360552,2.3990456,3.9684262,4.2343755,3.2581197,3.4478388,3.6952486,3.5913706,4.3569632,4.3904522,2.7765547,3.050732,2.4172828,3.014684,3.1966686,2.2420585,1.723845,2.5894907,2.4161666,3.0127936,2.2793604,2.1593108,2.4239686,1.9160064,2.2883748,1.8017213,2.2219406,1.6159705,1.385913,1.7556351,1.91337,1.6317844,1.3083323,2.1891356,1.4442812,1.6105022,1.5223145,1.2802658,1.2517934,1.2300549,1.1872779,1.8575006,1.5746817,1.7168809,1.689625,1.3059182,2.0373796,1.3761098,2.2864995,1.6095824,1.7707206,2.1673666,1.677239,1.6269298,1.371324,1.8948158,1.9223916,1.6284076,2.0164306,2.6435016,2.8603867,2.7866058,2.993722,2.7070987,3.0300792,3.0215461,3.5677586,3.7576367,4.2955538,4.7854177,4.7384028,3.5234824,3.5773201,3.5413396,3.6919731,3.5312558,2.5632352,3.3124276,3.8604908,3.5925841,3.045456,3.8919472,3.2971828,4.0063009,5.129414,3.610524,3.2450435,4.124865,3.5953797,3.7410963,3.6999848,2.9869691,3.207284,3.0566074,3.1103515,2.2645934,3.3397644,2.0847617,2.0706245,2.6997188,2.9521832,2.7454147,2.3295998,1.9382542,1.7664727,1.6725484,1.9799995,1.780614,1.7501393,1.8444428,1.8318856,1.4556676,1.6188775,1.4598914,1.3528814,1.5034556,1.5760472,1.1894987,1.3668214,1.0398148,1.3083914,1.8121303,1.3933668,1.6920908,1.4861731,1.3390595,1.6564047,1.5533882,1.4901351,2.0062735,2.0393728,1.85489,2.334753,1.8174102,1.8272169,2.1434544,1.9712624,2.0089582,2.1538483,2.4954261,2.076965,2.5231724,3.2008859,3.0180496,2.4303772,3.9350914,4.7936619,4.4851031,4.3864779,4.1873865,4.7117079,3.4873105,3.338599,3.675901,3.6625195,4.7887671,4.7546437,4.8259535,4.0145819,3.7967184,3.5047375,2.9537924,3.5322653,4.7327991,3.2101946,3.5855938,3.4134834,4.375955,3.8261034,3.8003719,4.3291194,3.5027538,3.545166,2.9412903,3.122977,2.7814963,3.4288057,2.5830213,2.4882665,2.0489115,1.9691644,2.2423129,2.6557065,2.0255453,1.9602668,2.2185618,1.9440829,1.6635506,1.7037979,1.5194411,1.6671169,1.839227,1.9920417,2.146604,1.4328095,1.3376046,1.0219204,1.2697091,1.1980333,1.382578,1.5333332,1.0467392,1.4762574,1.7664875,1.5067465,1.4901796,1.1320144,1.3850264,1.6275181,1.3635753,1.9243296,2.4010283,2.1901471,1.902966,1.6169846,1.9272755,2.2254085,1.4398554,1.7834952,1.8127481,3.1923923,2.2221505,2.395574,2.6084508,2.6251598,2.659356,3.8372583,4.7379796,3.1177434,4.7310622,4.0381666,4.4662097,4.6367739,4.4490116,4.7195519,4.47367,3.3099278,4.2069043,3.4734315,3.7296739,3.317292,3.3562506,3.8544498,3.3332616,3.6711749,4.2606824,4.2705276,5.3481276,4.5017101,3.9678005,2.8825461,4.0887869,3.8823254,3.6463755,2.8509382,2.156067,2.861794,2.5738251,2.1857869,2.1113936,2.0520813,2.5139221,2.4069496,2.0412984,1.8407547,2.0368029,1.7522393,2.5458021,1.7917612,1.5928436,1.8125855,1.92771,1.951082,2.1147993,1.4465088,1.1649836,1.0723909,1.3944544,1.2648929,1.0367455,1.2939168,1.1605523,1.9826791,1.3141531,1.4763536,1.2599568,1.5236662,1.0302163,1.8949487,.95641878,1.4530104,1.4983509,1.6488842,1.8724375,1.9411358,2.0049026,1.6101378,2.5154808,1.7858674,2.0337548,1.5183801,2.5665422,2.3885602,2.7843211,2.4750105,2.4880578,3.3454771,2.8206486,4.5317571,4.5777677,4.5535278,5.6760094,3.9250027,3.855238,3.6597696,3.6366056,5.1124704,5.4561128,4.5628447,4.3099262,5.1536931,4.1278648,4.1900256,4.6888478,5.6410042,4.6918477,4.9784349,4.5843779,3.9511516,4.4963325,3.8125403,3.4192813,4.2798454,3.5200599,3.3800548,2.2646504,2.7063071,3.3679913,2.3939184,2.1302555,2.7457276,1.9146363,2.9676366,2.0460593,2.1817708,2.0659544,1.9628077,2.4506123,1.2955903,1.5141425,1.5354109,1.5558811,1.9343412,1.1280586,2.400217,1.4582334,1.2080096,1.3639018,1.4379602,1.1678428,1.1708367,1.0142831,.88658781,1.2147708,.83171549,1.4243756,.99632948,1.3927401,1.2098711,1.3683022,1.5887478,1.8515444,1.4887349,1.6284766,1.522014,2.5169671,2.2193388,1.9842871,1.73436,1.5816048,1.8081841,2.5622246,2.5824511,2.2984295,3.6835077,2.9552147,2.9229448,3.1924919,3.8515199,4.1596597,4.4238811,4.0570362,4.9090571,4.2057573,4.1203513,5.0363886,4.9726336,5.8287791,5.0464087,5.0796467,4.8558959,4.7159103,5.2523269,4.1417234,4.0504064,5.2855977,4.8883579,4.6750632,5.4041387,4.4531092,4.8712571,3.439518,3.8418831,4.3325905,3.8839924,3.3020336,2.8541915,2.5439954,1.9339814,2.3899419,2.2366834,2.1343663,1.9626223,2.8720408,2.4892183,1.6462017,1.8279137,1.4910623,1.7841354,1.6902265,2.1710176,1.9406437,1.8164165,1.8629957,1.1772609,1.7022291,1.5563119,1.5121379,1.1611844,1.0784902,1.1945201,1.3401182,1.0227255,.91870703,.69902243,.93505169,1.249555,1.3187975,1.4017388,1.1471292,1.5620297,1.1136337,1.3724252,1.3694458,1.486476,2.2658051,1.9016314,1.9449517,1.6133091,1.5025035,1.5756496,1.293241,2.1770087,1.985043,1.5221771,2.5530317,2.6602267,3.1603516,3.2805304,3.2320571,3.8136338,3.598314,4.1158222,4.3517747,5.2726623,4.449591,4.6877331,5.0552796,7.6376431,5.7688743,5.3920622,4.405773,5.2093036,5.3988757,4.5037587,4.2939611,4.9778987,4.9934081,5.9954029,6.8449093,5.8874853,3.594219,4.9654507,3.7705154,3.0296754,4.0175864,4.1566879,2.5771871,1.6208774,2.3194752,2.056328,3.2823769,2.1624751,2.0730038,1.8853799,2.4766658,1.4783197,1.7966075,1.7353132,1.1804326,2.0887466,1.7911358,1.7116954,1.7731517,1.1881084,1.1465629,1.2985232,1.7883936,1.4758644,1.6311629,1.1856965,1.5503265,1.1902838,1.4680221,.93936759,2.1819741,1.0452443,1.081639,.98588941,1.6687542,1.8950473,1.6082503,2.0141212,1.5828979,2.030912,1.7103023,1.8375261,1.997867,1.9304289,1.4031479,2.0373666,1.4408079,2.3805595,1.8877224,2.6657172,1.7046202,2.4438278,2.6572743,3.7485709,2.998767,3.7272773,4.0771891,3.7416114,5.1544166,5.7962799,4.8729123,6.2250895,5.8529083,5.1170855,4.4838542,6.1740429,6.3446712,5.5777346,7.5883693,6.0084497,6.0037378,5.7222162,6.953672,5.3388184,6.601292,5.6382986,5.053867,4.9388514,4.9793267,4.2494004,3.9979679,3.5417235,3.4729861,3.6950257,3.1979481,2.8489374,1.8864165,1.6382342,1.9983393,1.7934712,1.8529009,2.0158432,1.9255699,1.6465309,2.236531,2.3884942,1.6635606,1.2697442,1.8588695,1.6962479,1.2797987,1.2873402,1.8430985,1.557673,1.5626404,1.4773328,1.3737529,1.6031159,1.3270658,1.6752371,.9631958,1.9450491,.75601909,1.2316898,1.0962134,1.6320112,.89303027,.92580677,1.2787514,1.5398601,1.3842735,1.461231,1.4276838,1.6124353,2.0299565,1.390276,1.474512,1.8441606,2.3019577,1.7606998,1.8035652,2.3621006,2.3613996,2.3748265,2.6947781,4.0646565,2.7655903,4.0733504,3.8280017,4.3554207,5.8668263,5.2206708,5.2916541,6.7909558,8.7678245,6.9966154,7.6896896,6.9543095,7.4726889,8.1369461,7.3814816,6.2353457,8.1974732,7.4708378,8.6562021,5.6600917,6.9976387,5.3921415,6.1871008,3.6147088,3.7446405,4.7130737,3.8077104,3.2230597,2.6746986,2.7167404,2.2441653,2.5090717,2.6670628,2.2686997,1.6568597,1.4912022,1.9954857,1.6354763,1.7884402,1.8194752,1.7213223,1.6089183,1.324807,2.0416705,1.6711146,1.6547874,.98304491,1.5041095,1.1154811,1.1771727,1.1181043,.67407854,.98273326,1.2763546,1.0858834,.83479053,.8620052,.71017311,1.1223944,1.0736545,.84949224,.86599775,1.2828718,1.363637,1.2262795,1.929887,1.1459183,1.8528945,1.7467838,1.8108344,1.7997451,1.624893,1.5158485,1.7510842,1.7344654,1.9420552,1.9386737,2.5776864,2.4866917,2.6319333,2.4862084,2.8631375,2.8682349,4.5699805,3.8722139,3.7367356,4.7107852,5.1701591,6.7694542,5.314395,8.1010528,7.5467849,8.9812344,8.5654799,9.852546,9.0323052,11.22444,9.2886849,7.6617184,7.3297105,6.0165246,8.2632385,6.8463011,5.2758195,5.7479378,4.98743,5.0808259,3.9582355,3.2255589,3.4592222,2.4468584,2.9930111,2.565188,2.2431314,2.0832688,1.960565,1.5968613,1.739569,1.6691954,1.3448581,1.6851168,1.6684179,1.5116516,1.3882162,1.3707462,1.3695522,1.1266699,1.034621,1.098882,1.1884045,1.2586189,.99404925,1.6888062,.92456151,1.1186296,.99775294,.75894276,1.3054393,1.0196258,.752827,1.3919657,1.1280418,.58824454,1.1757509,1.0221197,1.0810679,1.3107237,1.6469614,1.0362125,1.4353333,1.9097856,1.5179423,1.7408258,1.6059566,1.5684582,1.935901,1.6074322,2.1036397,1.8052208,2.2043551,2.2824924,2.4740781,2.6475944,4.0204946,3.5989155,3.4141997,4.7683135,5.0872981,5.7970513,6.3387116,6.9354658,7.9808606,8.4783167,9.9666705,9.5630293,13.79886,12.205793,9.6729567,9.0208349,9.2396908,7.5604439,7.6130466,7.8385888,7.4384517,5.3806669,4.3649046,4.6088013,4.2904471,3.6988634,2.6181711,2.1279866,2.7373655,1.9191327,1.968672,2.303219,1.4792483,1.8161898,1.6491994,1.5635515,1.6463416,1.2916201,1.4920273,1.4965931,.94890156,1.2993708,1.3406037,1.8027637,1.1332485,.9630116,.95008291,1.3539373,1.1455458,1.1240217,1.207962,.88299235,1.3345489,.71238207,.95464436,.84181078,1.003284,.96968842,.79723302,1.2081566,1.199488,1.0889573,1.0034902,1.1304194,1.7564535,.9792078,1.5730508,1.4769968,1.2461155,1.5100211,1.0510706,1.5399687,1.4020352,1.57174,1.6640445,2.3422653,2.1025939,1.9102712,2.4817942,2.4707303,2.6335492,4.3833438,3.6158162,5.4283955,4.5796948,6.2939373,5.2626466,6.4848017,9.0644175,9.6781411,10.655354,13.401246,15.841601,16.018899,15.061532,13.163563,12.95226,14.208965,10.353561,11.736353,8.0240178,6.753305,5.4493259,5.0176106,4.473543,3.7235483,4.0733146,2.5261747,2.6209576,3.3965924,3.6104341,2.1090472,2.181949,1.6180294,1.8530485,2.1704144,1.6559144,1.3141246,1.2461374,1.2925142,1.1735058,1.3577158,1.3047445,1.1086048,1.5023414,1.1048581,1.2830739,1.1096668,1.1543121,.62658817,1.0652159,.98337505,.85797858,1.0960474,.76142008,.59253495,.69869567,1.4661653,.83555817,1.049369,1.2534595,.68768647,1.667187,.83564363,1.1164699,1.299768,1.095813,1.2933011,1.3999265,1.4468438,1.8203555,1.9115788,1.4672908,1.424583,1.8521956,1.9130666,1.7467969,2.7951083,1.9198271,2.8092403,2.0822023,2.2546502,2.95525,3.1880403,4.0909676,5.3895008,3.8863863,6.7548222,6.1508506,8.3680729,8.0995096,10.264562,11.32088,15.950325,16.792941,18.016149,17.520703,14.619817,9.0611419,9.1329914,10.9519,7.9011934,6.0331496,5.449525,4.645258,3.2877972,4.9243239,2.9801781,2.3117919,2.1817439,1.9715737,2.0227721,2.6170184,1.9648643,1.4901347,1.4379608,1.7675474,.99850407,1.4476955,.94160819,1.6582148,1.2756171,1.577902,1.0083684,1.2363335,1.4494957,.88718345,1.1806623,1.0050259,.97200752,.91137065,1.1021581,1.0321627,1.0177052,.63500056,.99096126,.92871007,.7140504,.90375952,.90068937,.65678462,1.117746,.44322987,.55319402,.98422841,.77826291,1.1531126,.59637652,1.2721316,1.1957872,1.4538003,1.14404,.98870629,1.5346895,1.6328783,1.456585,1.8107548,1.9596894,1.7511843,1.9540626,1.9516638,2.0180559,2.1215899,2.5211975,3.1906617,2.6783724,3.9869191,3.5457247,4.4638758,5.6459235,7.3622279,7.7269484,9.8125826,14.056056,13.502428,19.224289,21.339681,20.591881,15.987653,12.405964,9.231152,9.2275523,6.6619596,8.4638326,4.4605672,4.972979,2.8499197,3.2814427,3.0298239,2.6593904,1.8402116,2.0653079,2.1060606,2.0288377,2.1224409,.9921361,1.6526505,1.5999173,1.4276278,1.522673,1.5710474,1.2072759,1.5018304,1.1805758,1.3615719,.85291633,1.4303543,1.2825711,1.1771729,1.2842883,1.3056324,1.2175879,1.6235643,.98095112,.83101638,.953051,1.1463265,1.1340093,.75389872,1.1027307,.69831981,.98737164,.9199895,.7870589,.75128035,1.0270138,.9255541,.8162165,1.0755713,.82168275,1.0214578,1.2003228,1.1731254,1.2484251,.88332533,1.4304415,1.8154059,2.0565234,1.4911914,1.9863533,2.3525368,2.0414625,1.8522317,2.6052663,2.6847397,4.2966719,4.1645265,5.9892872,5.1301221,4.4044322,6.3648372,7.6926131,10.59074,6.8364315,10.083302,14.496944,18.295495,20.431812,17.610725,12.80801,11.754487,10.495823,8.8910907,7.2010715,5.6948574,4.4253106,3.9593099,3.986018,3.9266684,3.201446,2.0318586,2.8195963,2.0914408,1.9675743,2.0150402,2.047683,1.609498,1.5973144,1.3605108,.89735547,1.3860323,1.0858624,1.2900101,1.036792,1.4210222,1.3647757,1.2881742,1.0526865,1.1306247,.80287775,1.0581702,.65657523,1.0056704,.73892003,.85235369,.92419687,.74123335,.65846467,.58406816,.63100953,.92732656,.95655309,.93780623,1.1505245,.70364515,1.1258869,1.3880789,1.0162793,1.7027043,1.248149,1.530804,1.2272664,1.1544653,1.189697,1.2755828,1.3939327,1.9191486,1.5387069,1.5223838,1.8827338,2.0462312,2.8354144,2.0829119,2.4557351,3.6146809,2.9670955,3.2483825,4.0327375,5.2639134,4.9489402,4.9398924,5.8499534,5.9303491,8.4705048,10.197802,9.937582,11.57343,14.76631,13.928308,9.9519783,13.821778,10.080608,8.9008063,6.7373678,7.7004234,6.5545356,6.6801225,4.7847453,3.7211702,3.8736077,2.6541578,3.0665873,2.5704872,2.5957144,2.1895976,2.6501393,1.9554863,1.7226996,1.5590126,1.6177919,1.6528067,1.2647088,1.1092626,1.1238858,1.0566087,1.0625932,1.2514495,1.1001806,1.0468272,1.1952926,.77777099,1.2968581,.98982556,1.001005,1.1289569,.94178757,.81464124,.98893969,1.3291995,.66320545,1.034357,.69678357,.87483459,1.2652661,.9149917,1.6137597,1.9295605,1.0311286,1.2243012,1.4347811,1.4514641,1.509402,1.154596,1.5117359,1.171381,1.1630478,1.6938533,2.2435947,2.3569135,2.2684442,2.4246132,2.0550869,2.5900396,2.3786289,2.2866906,2.8989143,3.1269506,4.5177257,4.4316397,4.4971275,4.614844,5.0727616,7.7225918,9.6205641,9.3677717,10.770792,10.014099,8.7015253,10.740593,12.137987,10.992495,10.286966,9.4481679,6.7812666,8.2731653,6.9293826,5.7153814,6.0839539,4.5474467,5.0328447,4.6284209,3.8476535,3.2069528,3.6248153,2.8829901,2.3845617,2.0370791,2.559851,1.662568,1.9144728,1.5038049,1.6529217,1.5317161,1.6462891,1.5771036,1.3141878,1.5135621,.95510022,1.1440472,1.0869848,1.4591477,.94117798,1.0826675,1.026368,1.1882169,1.0885895,1.0471571,1.1012322,1.3733882,1.0591351,.75160239,1.1715076,.92071129,1.3140545,.92131764,.90987126,1.2210068,1.410068,1.0018876,1.0601917,1.0989989,1.4892542,1.6886425,1.5619652,1.3237266,1.1883168,2.3249209,1.829506,2.5326082,1.639586,2.4307122,2.136748,2.2362998,2.4086673,2.883778,2.2271043,2.9022502,3.8726349,3.7269533,4.3961694,4.1255638,4.8740833,6.1311143,6.1019658,8.5724714,8.9902332,8.1730783,12.893334,8.7928745,10.56547,7.6053738,9.4203749,9.1894878,7.6670062,7.729865,7.0833352,6.1837555,5.9978587,5.0421057,5.1403509,3.7752856,3.841658,3.1466767,3.5637813,3.1790539,2.5468451,2.6650713,1.9014054,1.9008585,1.6198272,1.7246871,1.7469185,1.6976874,1.4211893,1.254067,1.8365036,1.8369018,1.3031921,1.1164167,1.4581682,1.4583367,1.1296686,1.3744254,1.2720617,.8183568,1.4691987,1.1139096,1.2698689,1.1946276,1.0948523,.86988455,.55540603,.97045695,1.3535963,1.6044879,1.1638904,1.0218608,1.315082,1.1703963,1.0864504,1.3172711,1.0992519,1.4566844,1.730561,1.4497887,1.7634137,2.2969008,1.7556347,1.4588144,3.0275041,2.257662,1.9793597,3.0099401,2.4989728,2.9703857,2.6732384,2.7774799,3.5553025,3.6454743,2.9703694,4.2111383,6.1584403,4.5072754,5.3874264,7.0173371,6.0255097,7.8525511,7.11173,7.368685,10.298407,8.8905586,7.4006148,7.4094261,7.0870743,7.0820625,6.7393905,6.5034219,6.30174,6.2798687,5.5967257,4.8584564,4.5929891,4.6427728,3.9629064,3.4110447,2.8441571,2.6372692,2.7859012,2.2659549,2.6669763,2.6650562,1.6029533,1.9121502,2.0735004,1.6085723,1.5280864,1.3044483,1.7499055,1.7340051,1.3213128,.89174521,1.4418085,1.8753594,1.1954195,1.5715681,.98230147,1.592071,1.0392763,1.1138062,1.013912,1.3123119,1.132181,.90150896,.64127881,1.2485226,1.2217093,.89739577,1.0688099,1.2452777,1.1061568,.89245341,1.4142161,1.2312145,1.6602783,2.2439835,2.0197548,3.0076526,1.7022754,1.6608604,1.4057535,2.1579805,2.1062732,2.3910521,3.6396713,2.7089965,2.7797915,2.7616356,2.9092233,3.3013477,4.711775,3.6147806,3.9829118,4.6137048,4.8233653,5.4457904,5.11974,6.5454554,8.2890849,7.5042849,7.9154763,9.1059387,6.027027,7.7309978,7.2754935,9.3251783,6.193075,9.4307111,5.6047658,5.4321268,5.7910801,4.8137791,4.8810626,5.0989429,4.3522941,4.5421536,4.2594707,3.4215506,2.9241442,2.8285679,2.885734,2.498858,1.7186351,2.0859601,2.2665258,1.7834567,1.833001,1.5191323,1.5871705,1.5056875,1.1870489,1.2785009,1.5839096,1.4281477,1.1756194,1.3619773,1.3710536,1.9489844,1.6666506,1.1265621,1.1469502,1.0816155,1.4094488,1.143862,1.0661688,1.4364715,.98920554,1.0122507,1.2802591,1.1626293,1.3623621,1.1979016,1.6706617,1.7768027,1.6947789,1.6282181,1.5165967,2.4508611,1.7073765,2.2612405,2.4158931,2.179953,1.5405181,2.1377916,1.9401465,2.3867058,2.653755,2.8791968,2.5198053,3.0758742,3.7987595,4.0462692,4.3443338,4.1832645,5.3445688,4.926408,4.1386385,5.2045773,7.3544532,5.3940835,5.8139209,6.2604339,5.3366505,6.0039899,5.5589914,5.7508845,7.9435707,5.381047,6.0506871,7.2419183,5.4021352,4.7425014,4.4924269,5.2325785,5.1584179,3.5698182,4.0839697,3.3529597,4.5567956,2.6281745,2.6462196,3.244384,3.0704994,1.8423863,1.7247907,2.1419393,2.0696601,1.9322215,1.3935289,1.701757,1.6357365,1.8881559,1.4191563,2.0608182,1.7005607,1.2262892,1.7879432,1.3092162,1.5826759,1.7790902,1.5890815,1.4621965,1.2637985,1.0361356,1.2777159,1.494658,1.0152783,1.3778887,2.0607769,1.4721226,1.327762,1.7413124,1.3418529,2.0153787,1.9088854,1.1922208,2.0748889,2.135076,2.1863029,2.4413956,2.1654914,2.152052,2.003447,3.2714111,2.6681573,3.8346589,2.8943436,3.4360009,3.6164964,3.3728418,3.0764882,4.0587227,4.7377244,4.7696073,3.7202944,6.1831724,4.6105104,6.5402813,6.5397218,5.3764513,5.2979768,5.9841891,4.916182,6.0157154,8.9873044,4.9937485,5.5021188,6.0384777,5.675832,3.8141607,5.1965377,5.6348883,5.6840188,5.4909133,4.1044368,5.3510056,4.5664877,3.1941371,3.2442297,4.7145166,2.7979688,4.0760644,3.6003988,2.5278743,2.3049942,2.1216812,1.8795905,2.514154,2.4823278,1.7484011,2.0632185,2.245399,2.0107559,1.7279313,1.9381344,1.6030511,1.4036084,1.3901809,1.4429003,1.3340299,1.7413163,1.4617838,.931749,.99622752,1.0968561,1.5857075,1.0591468,1.2529467,.93357442,1.7589628,1.9294929,1.9109628,1.4388009,2.469461,1.716117,1.319304,1.804743,1.9634992,1.4598129,2.3654507,2.0358845,2.2949711,2.1197086,1.7697538,2.5145803,3.0387518,2.8874326,2.8165808,3.1880517,3.5502187,3.2888024,2.8916388,3.3029054,3.5074532,4.0190297,4.3810369,5.0699843,5.5399616,4.0271416,5.1536515,5.4926697,5.3532598,3.836454,5.6830048,5.1862,5.610442,4.6911191,4.0825372,4.7418306,4.557493,4.1959809,4.328106,5.4963074,3.5958698,4.5573997,3.3458881,3.9779576,4.784597,3.8416837,3.4138559,3.7521005,3.340189,2.6383384,3.1617565,2.2464167,2.3202964,1.9866038,3.1380019,2.1841415,2.2639169,2.4981935,1.808173,2.1891225,1.7799002,1.4197194,1.0300785,2.2598547,2.1541742,1.3596296,1.5933393,1.5951283,1.8588909,1.3965805,1.6185889,1.3920673,1.1790391,1.2892085,1.4628188,1.7019949,1.5659381,1.2942994,1.1747315,1.4874732,1.2444547,1.8094872,1.9536032,1.7267763,2.6807163,1.7662428,1.9714902,2.2873568,2.2875033,2.5530436,2.8669608,2.2279737,2.5334926,3.464253,2.5541002,3.3645093,3.6095086,3.6605226,2.9647202,2.0583841,4.4272152,4.6831138,4.0722735,4.7274788,3.9042529,3.5296419,4.4661266,4.8742675,5.3134991,5.4719119,3.6912368,3.8577823,3.5858859,4.3742478,4.3774809,4.4483305,4.2608502,4.2577148,5.0515878,4.1577782,5.5145183,4.2636796,5.3898315,3.6217607,3.9597398,3.9234697,3.7695685,3.9990973,3.1366345,3.695261,3.0045131,3.5443615,3.0419259,2.1184506,2.6572979,2.491989,1.8965421,2.0777789,2.1289246,2.0297214,2.3356643,2.4140647,2.2226935,2.0494534,2.2208387,1.985285,2.5993448,1.6336565,1.6910867,2.5146588,1.38828,2.3560109,2.0339916,1.74432,1.2032908,.88855774,1.4248857,1.7072731,1.6166418,1.5251973,1.7566495,1.3472995,2.0343566,1.0499147,2.0559371,1.5724982,1.4017911,1.7671575,2.0533608,2.9289213,2.6180834,3.1129938,2.5329716,3.1764653,2.9147007,3.1261679,2.9155285,2.4475051,3.7113865,2.7866741,3.2128412,2.4920778,4.0971803,3.1038376,4.8636873,4.1163994,5.3549273,4.003646,2.9504411,4.9198775,5.0059228,3.756377,3.6725643,3.4951508,3.7608898,3.2098653,3.1241218,3.5929646,5.193655,3.8336057,4.1532421,3.7759529,5.1689857,5.0198565,4.6399284,4.2476448,4.3042535,3.943359,3.152322,4.4487803,3.5578826,2.7822488,2.4586348,3.9149059,2.7303518,2.701974,3.0352644,2.2323069,2.6129765,1.9426022,2.4412886,2.0830912,2.2901207,2.60352,1.9169948,1.7949664,2.5126233,2.138566,3.0234075,1.593032,1.4496833,1.7043515,2.3225084,1.8740255,1.5767574,1.5486101,1.0086569,.94526474,.98279077,2.3914427,1.3349233,1.662824,2.339585,1.7526785,1.5862078,1.8610395,2.3762897,2.0166726,1.6022628,3.1933354,2.8571055,2.5257729,2.2244317,2.1511868,2.124472,2.5147777,2.4212357,2.644408,3.0539043,2.2300806,3.1284882,3.1343258,3.6810976,3.7845642,3.7384032,4.0991779,3.9117012,4.2865827,4.0266169,2.9410464,4.1842732,3.1924183,4.3201712,2.1871915,2.7436362,3.3541433,2.8596771,4.1878851,2.6142636,2.9329908,4.1246693,3.6137715,3.8244525,3.7397349,3.9338839,3.1285973,3.6519795,4.5199139,3.697693,2.4859625,3.5974918,2.9213166,2.3955754,3.1453298,3.2863417,2.6117658,3.8960039,2.7854036,2.8472614,2.4324246,2.0804645,2.3453173,2.0402417,1.5093892,1.9235784,1.4262282,2.9496656,1.8228929,2.1659591,2.1881185,1.3273391,2.080789,2.1228795,2.190858,1.4611134,2.0319684,1.7938403,2.1299913,1.2124929,1.4141927,1.2035249,1.5306318,1.6586828,2.5056311,1.9386174,2.3676524,1.7895453,2.2681061,2.005874,2.2053365,2.5413435,3.1307276,2.8479943,1.9716775,2.3335658,2.9462844,3.6205107,3.2300032,3.4403449,3.3256801,2.4973198,2.0849346,2.9557563,3.0652164,3.366852,2.8278154,2.9864101,3.877302,6.0303115,3.2688723,3.5190745,2.639111,3.714445,3.5635734,3.24532,2.3533246,3.5833929,3.0448218,2.299633,3.4734401,3.7384744,2.2719605,3.9412303,3.6377095,3.2771119,4.3027501,3.8484619,2.4276811,3.0953322,2.8841077,4.1025334,4.2917576,2.6197757,3.3645138,2.4314183,1.8174194,2.8708788,2.403895,2.3589196,2.464658,2.4679907,2.3679985,2.4593946,2.4610779,2.2022625,2.2353027,2.5090053,2.0262569,2.0019615,1.4434697,1.4343788,1.5575397,1.4840818,1.8685464,1.6611002,2.3447595,2.2903757,1.1514776,1.5191833,1.0677715,2.1799466,1.7810843,1.2329185,1.3770509,1.2855529,1.6682111,1.5280775,2.4811806,2.2756383,1.9633624,2.7255051,2.0590435,2.1707241,1.9101258,2.1841365,2.6666939,1.6214889,2.6328187,2.7202091,2.8138242,2.8078783,2.8834629,2.5901096,2.3271893,3.1778248,3.9981471,3.9017082,4.0047739,4.0254713,2.4416343,4.6464583,4.2059929,2.9643663,3.451808,3.0252913,2.0898613,2.1381241,2.8784052,3.1247475,2.4667662,2.4751101,3.5039007,3.196976,3.9225509,4.6569945,3.6595093,2.4477783,2.7009495,2.5139625,2.7078665,3.2955959,3.758326,2.9206129,2.1607569,2.9329364,2.2193393,3.0691617,2.491672,2.9229015,2.2599894,2.6257638,2.307559,2.8692551,2.3554224,2.3762496,2.1136694,1.8869619,1.6637637,1.9777072,1.6481753,2.4218741,1.7282279,1.5245874,2.0205218,1.4359097,1.806009,2.1156843,1.4973111,2.0881653,1.5425025,1.1207337,1.2373331,1.357714,2.4873457,1.8521436,2.1994765,2.0450172,1.7376926,1.8371209,1.9261675,1.2650022,2.1753933,2.2675921,2.7752472,2.7339213,2.2504079,2.779555,2.3061332,3.0225079,2.2012532,2.8859209,2.8508381,2.3204393,2.7048592,2.2870153,3.1331141,3.4142485,2.6659103,3.1208968,3.0700521,3.1678036,3.3227998,3.3524141,4.2078986,3.2534622,3.0633676,3.0361458,2.4070208,3.127032,2.7343483,2.8085012,2.4785207,3.5431721,3.0041483,3.4155417,3.1883619,3.9387126,3.4372021,3.7709427,3.1696447,3.3070393,3.3017278,3.6088445,3.1380236,2.9559838,2.4681664,3.1302263,2.5382151,1.98369,2.7218488,2.4572655,2.747108,2.337239,2.0142952,2.3746822,2.2432434,1.8851716,1.8991448,1.8650244,1.9405107,2.0414038,1.8514746,1.9505811,1.9915473,2.2992275,1.6694529,1.5069999,1.1927527,1.8625175,1.6083106,2.3634707,1.1268652,1.319124,1.550456,2.167305,1.6624752,1.6797931,1.2817896,2.3547965,3.0417863,2.5599888,2.1678682,2.1882733,2.1737328,3.5933338,2.4979413,2.2777058,1.9875959,2.3851093,2.8959397,2.1435461,2.4614248,2.3759947,2.3082881,2.8803163,3.3675013,3.7249837,3.4582348,3.7308486,3.4623323,4.1891991,2.8846025,2.414989,3.8853606,2.8220506,2.7486158,2.5916212,2.3691058,2.1362572,2.9772587,2.9726772,2.5683964,2.5931004,3.3006919,2.8018009,3.2360739,2.9439428,3.0076574,2.530469,2.9164013,2.9640402,4.1879908,3.4459483,3.7470515,3.0953917,3.3743524,3.3379163,3.4338191,2.5970668,2.977305,2.4469682,2.2288582,2.5700798,2.2180937,2.5434766,2.0457856,1.8196689,2.4919511,1.8756653,1.9271162,1.6246183,2.1486223,1.7416291,1.6286651,2.7429568,1.8760217,1.511259,1.1401331,1.4559092,1.7956338,1.4025986,2.0769671,2.1885823,2.2859212,1.9724645,1.8490097,1.5428219,1.1609433,1.5581891,1.5500508,2.6324304,1.8824616,1.5216121,2.6384122,2.6320114,2.9699431,2.0938214,1.8331607,3.0998718,2.220329,1.8743962,1.5694134,2.7567912,2.2946468,1.9036605,2.6032552,3.3552207,3.0925421,3.7670158,3.3934116,2.8211113,2.7443922,4.0649083,3.0707547,3.4362311,2.5429864,2.2227649,1.7158489,1.8938501,2.5886793,1.9567585,1.2530184,2.8781564,2.619036,2.3123263,2.8100784,2.6468293,2.6494291,3.5312879,2.6083287,4.1647276,3.865765,3.0960333,3.8794466,2.5874003,2.6493856,3.5719867,3.068232,2.206468,2.7493425,3.2002151,2.0876092,3.1585995,2.0880792,2.215382,2.1695197,2.9012322,2.2149929,2.7186513,3.0939733,2.0884968,2.0590951,2.0554619,2.7188304,1.9278885,3.039938,1.58457,2.4995078,2.0631091,1.663603,2.7869127,2.1547038,1.6132883,2.0066623,2.3029744,2.0787818,2.2253451,1.974008,2.2741049,2.0685602,1.7166929,1.7817857,2.2880598,3.2131031,2.2890521,3.092812,2.5081707,2.8935188,1.9370422,1.8247684,2.7272384,2.3373358,2.7136271,2.7825329,2.6074275,2.3615403,2.6912467,2.6976287,2.2293496,2.3569712,2.7278514,3.0664779,2.8229872,2.719053,3.2213882,1.8734228,2.6986775,2.4309322,2.7534264,2.5020957,2.3092594,2.9419433,2.0284219,2.0139626,2.7918141,3.1072548,3.5224388,2.5032715,3.3557738,3.1200516,3.7449449,2.8579646,2.5135575,4.0654558,2.7758894,2.7385484,3.7570694,2.9749303,2.1311345,2.6206746,2.6021903,2.0907876,2.666256,3.8521962,2.3794581,2.236391,2.0307487,1.9296913,2.6010741,1.5414681,1.7869169,2.6423708,2.7537031,2.354426,2.7171518,2.1031868,1.8891381,2.1579672,1.7526763,1.765915,1.9714716,1.5582489,1.9353291,1.7436377,1.2530238,1.0888718,2.2274333,1.902162,1.355863,2.1658009,2.148127,2.0760883,1.1295943,1.9698673,1.8106116,1.8699013,2.0823057,3.2537903,2.1225036,1.7115783,2.0788841,1.6948263,2.3110374,1.5951523,2.1917969,1.7858164,3.0106739,2.7736664,2.795891,2.2123178,2.3454618,2.9382848,2.5790068,4.0439108,2.2954307,3.2748263,2.5102989,3.0343863,2.2159444,2.7667823,2.1310627,2.4683296,2.3778815,1.8691957,2.0187164,1.9531402,2.1187417,2.1417038,2.38315,2.4560892,2.0751409,3.1087945,3.1940879,2.7100041,4.4971099,2.8719994,3.32567,2.4097376,2.4691645,2.8138074,2.4125011,3.3513671,2.9930381,2.2608525,2.24499,2.1250562,2.14907,2.0867142,1.816637,2.2791981,2.5293934,1.9377913,2.5713846,1.7580076,1.8669798,1.3818829,1.9467103,1.9402774,2.3380654,2.126128,1.262681,2.0926249,2.065123,2.0378668,1.584575,1.8147973,2.2975679,1.8963987,2.1877793,1.983272,1.8889297,3.6371753,2.3034083,1.936248,1.6498452,2.6182049,2.2154592,1.8438022,2.4709299,3.1742946,2.3998794,2.3771231,2.4712226,2.7081758,2.5852713,2.7806426,2.8586778,2.1783928,2.1456366,2.6416863,3.5352878,3.0691915,2.3512015,2.7121063,2.6335059,2.6088299,2.047827,3.7866274,2.3482743,2.2572027,2.418581,2.1323501,1.8707078,2.223617,2.4131865,1.9750457,2.373449,1.9985017,2.1606145,2.4528057,2.0009498,2.2826453,2.5053277,2.5889298,3.1960144,3.1511066,2.9429558,3.4567229,3.5582477,2.4844828,2.8516518,2.7997466,3.3349205,2.2321127,2.1761108,2.775887,2.1191279,2.1790616,2.1947244,1.8397668,1.4147559,2.2351731,2.3289453,2.0471203,2.3383806,2.163754,2.3054297,1.6707258,1.8216285,2.141492,1.8454948,1.4615444,1.8668115,1.6872305,1.4182309,1.2303322,1.9315612,1.9794734,1.7804993,2.5807698,1.8059306,1.8106945,1.7813833,1.7388731,1.6585575,1.9516076,1.950433,2.0599881,1.7884966,2.4195636,1.8715973,1.9788956,1.6612426,3.5866308,2.0495437,2.3561091,2.4345525,2.9577589,2.417021,2.3799655,2.7413716,3.3145689,3.2001918,2.4823507,3.0948253,3.1081626,3.1602272,3.2060045,2.622409,3.1502396,3.2323287,2.0134792,1.5981567,1.7885223,2.2166163,1.5689586,2.3716627,2.0819625,2.5023223,2.3721338,2.3718219,2.3248316,3.0576187,1.9365711,1.8463423,2.7593669,2.8824033,2.6929363,2.8849758,3.8863788,2.7152039,2.3272339,2.4880438,3.8769269,2.9355076,2.0176006,2.1759968,2.5234584,2.4732865,1.8876527,2.0612722,3.2115867,1.8131362,2.067105,2.01859,2.7776353,1.4746675,2.0799367,2.5639535,2.7081865,2.4870038,1.6354672,1.9367316,1.8763661,1.4904414,2.1911521,1.6249459,2.1877548,1.6432794,2.2204326,1.9696207,1.5941823,3.0597752,1.7261852,2.0669377,1.7479766,2.4761598,2.4327564,2.470571,2.3243763,2.4789758,2.1070609,1.6037902,2.102305,2.5268225,2.3018746,3.0660983,2.842377,3.0182569,2.8726901,2.6994424,2.8727361,2.6473279,2.5408394,2.6348276,2.8801448,2.0109667,2.5323076,1.8405108,3.4196159,2.0566278,2.3069432,2.3313679,1.7029393,1.2637975,2.2086765,2.1535156,1.5799854,1.6167468,1.7377936,1.8818713,1.8119377,2.2111747,1.9933301,2.1719376,2.1168001,3.1539843,4.1993737,3.3211358,2.606165,2.2874983,2.3967551,2.0071401,3.0762291,2.6037495,2.8699929,2.3756243,3.2258159,2.1663393,2.4399133,2.5403759,1.5241727,1.8567909,1.931845,3.0275596,1.5034203,2.3306334,2.3996919,2.2702621,2.4128991,2.0222093,1.6029859,2.2740026,2.8779283,2.8762496,1.5014027,1.5387403,1.8107833,1.1940166,1.6507929,1.1761456,2.2553838,1.5948138,1.4650658,1.4880179,1.4954774,2.0680532,2.0343072,2.5279229,2.7426858,2.730995,1.8990979,3.0099374,2.3674646,2.5780924,3.7719697,2.3363335,2.7527954,3.5297958,2.4621901,2.8523478,2.1862074,2.2859444,1.8283227,2.8246476,2.1538208,2.5282616,2.3639611,2.8584154,3.035728,2.1502987,1.7586214,1.5324398,2.6522453,2.1273933,1.8182495,1.2456449,2.0389307,1.2750065,1.7770425,1.6752197,1.8005969,2.0548374,1.585869,1.7451527,2.1098167,2.5085751,2.3580887,2.5241841,1.673247,2.4727432,2.986818,2.5055412,3.1991836,3.4013885,2.9727617,2.3824126,2.3430509,1.5732426,1.8624314,2.2306958,1.7112334,2.0566642,1.6597689,1.7232449,1.9554877,1.5164974,2.5418695,2.210037,1.8992428,1.0094014,2.7929686,1.2927406,2.141303,2.3755399,2.065332,1.4599714,1.8311518,1.6016267,1.457112,1.6800138,2.0187107,1.641135,2.6571451,2.0596754,1.7927337,1.8028919,1.252789,1.6599207,2.2550712,1.8005523,2.0906642,2.2563348,1.996283,1.9015441,1.91457,2.4114571,2.5946939,2.8719431,1.7282237,2.645844,3.0876978,1.9352885,2.4522401,1.3768203,2.174308,1.6867061,2.5499766,1.9361878,1.9867352,1.5096237,2.9019509,1.7052017,1.7968846,1.8571559,.98205895,1.8945704,1.8772241,.93149389,1.5651287,1.6471251,2.2216318,1.9548921,1.7144566,1.4436749,2.641558,1.7938671,2.134576,1.6241978,1.9649049,2.1771009,2.1630554,2.8749226,2.5362045,2.8538626,2.2887464,3.2952252,3.0539202,2.1773857,2.0927042,1.7127113,1.7833065,2.1496275,1.6511817,1.8749838,1.6428218,1.756386,1.2394393,2.3597462,2.429746,1.2193716,1.9166708,1.4044111,1.287159,2.0685065,1.4489287,2.4784924,1.7301762,2.1593814,1.5436542,1.7410039,1.4314954,1.4187692,2.9365215,2.1047021,1.7489635,2.1125065,2.1230712,1.4434282,2.2630774,1.871933,1.8786935,2.4840349,1.900356,1.9092503,2.0880277,3.4982653,1.7298124,2.0173265,1.9657934,2.0405873,2.4244834,2.2430405,2.539473,2.4511349,1.8957479,2.5168316,2.5149379,2.3080183,2.5845515,2.161376,1.7017604,2.1439074,1.755998,1.6547605,1.1977078,1.6652113,.83425938,.8564632,1.448916,1.0679841,1.3870344,1.975974,.94501629,1.732736,2.204028,1.7214861,2.0645008,1.4572249,2.6291565,2.8051834,2.5093319,2.0076982,2.6362848,1.8332811,2.8915754,1.964082,1.6884376,3.1844177,2.429813,2.1846481,1.6742047,2.4565996,1.6583262,1.7268643,1.5515063,1.9243572,1.5200847,1.6981686,1.9390205,2.2257496,1.7811031,1.7333563,1.5109923,1.9164516,2.4995955,1.1549183,2.2375897,1.6063275,1.7672052,1.3882897,1.6162181,1.8914177,1.1926533,1.7818192,1.2782574,1.9514753,1.9777712,1.620942,1.8291532,1.436183,3.143729,2.4494535,3.0345127,2.0730229,2.3893869,2.5354082,2.7589102,1.8677414,1.7965585,2.3826539,2.0827881,2.4729081,2.9984808,2.6023855,2.5252197,2.4493793,3.3770014,1.9969213,2.0955974,1.4557975,2.5512676,1.6581994,1.1357497,1.4866726,1.5149368,1.6137232,1.3551421,1.9617397,1.5101058,1.245078,1.2801677,.98326431,1.4854022,1.6444093,1.4030566,2.8344494,1.3558252,2.2120555,1.4665224,2.3041848,2.4575899,2.0246388,2.6366668,2.7526793,2.8031448,2.5112882,2.95341,2.8416679,2.4655499,2.3549519,2.2037656,2.6332552,2.7088805,1.9556061,1.7628069,2.496695,2.5368465,1.9660005,1.5400431,1.8545345,2.0741437,2.2078247,2.4834239,2.0107254,1.9847618,1.7829467,1.9224809,1.3900617,1.4081977,1.3166354,2.0612257,2.4714891,2.2299161,1.7752641,2.9113395,1.7760014,1.9065337,1.877075,1.6253226,1.6215736,1.737427,2.8470114,2.2536916,2.3736558,3.5538697,2.0560542,2.2459497,1.3520696,2.4557831,1.8767046,2.0052332,2.7601783,2.8653436,2.5693832,1.8870747,1.8695047,1.9235118,2.7543425,1.5855319,1.7503964,1.70962,2.3982979,1.3092469,2.6025455,1.4514932,2.003394,1.1912446,1.4864406,1.6178744,1.2432954,.71780706,1.6311487,1.3251213,1.5186753,2.6719131,1.8267867,1.8932529,1.8999905,1.1975088,1.6728558,2.1505221,2.918871,2.5764408,2.5213528,1.9841386,1.8701658,2.4029567,1.9540922,1.9224031,1.8238828,2.0146119,2.4911758,2.2232743,1.6869953,2.1750618,2.2716943,2.4885497,2.6842515,1.9055483,1.5207456,1.3690668,1.7895467,1.7181907,1.9784921,1.8835985,1.6123766,1.6450049,1.7424415,1.924465,2.5283681,1.9732152,2.077388,1.9201045,2.4087999,1.7634124,1.9394359,1.8396952,1.8870543,1.545977,1.3854143,2.2271213,2.0093391,1.8950086,1.8971694,1.90527,1.7539787,2.1853306,2.3514298,1.8912982,1.8354199,2.1110042,1.9776326,1.7199595,2.0367148,3.0302039,1.5951848,2.4691822,2.4689356,1.783465,2.2264434,1.8496104,1.9873584,2.4685408,1.9803505,1.7647098,2.0724703,1.3907017,1.5061439,.87527431,1.1507149,1.4207834,1.161215,1.1276096,1.6295528,1.7039592,1.9533922,1.5765184,1.6365323,1.8224206,1.3617254,2.1259925,1.6089289,2.417532,1.8326854,2.6539303,1.5504236,1.3482398,1.599989,2.1521118,1.5506126,2.263368,1.6242548,1.8378209,2.5036583,1.7905984,1.8761239,1.9576806,1.5904046,1.9315544,1.3985896,1.7284463,1.7871083,1.9990943,1.485661,2.5088249,1.9004822,1.7111805,1.736225,1.863159,1.9888656,1.3675667,1.9419778,1.9942537,1.9139808,2.1866328,2.2363117,1.8449013,1.6049772,2.3908241,2.150671,1.9204851,2.5318571,1.8942279,1.4609321,2.3615657,1.6131291,2.0835277,2.3694488,2.6242605,2.2288634,2.6575403,1.9879634,2.3612383,2.0359086,2.3156425,2.1694274,2.2481842,1.7929961,2.0015238,2.3799189,2.2226938,2.6824985,2.0723558,1.7295132,1.917688,2.0318084,1.8304456,1.341896,1.1394123,1.4443099,1.2406437,1.358046,1.5085967,.71220018,1.2727869,1.2770429,1.5083049,1.5079532,1.52746,1.4858768,1.6471964,1.621834,2.3145163,1.9509016,2.59022,1.5537627,2.1164047,2.0122211,1.1992089,3.2747371,1.4655824,2.269247,1.5517391,2.0368914,1.6720741,1.9730631,2.5069554,1.9504472,3.0503912,1.7800241,2.0798124,1.7864842,1.8605557,1.5733882,1.8801561,1.4596532,2.3495454,2.2342816,1.8980225,1.6801397,1.88078,1.5957642,2.8473033,1.7837329,1.7317581,1.5112854,2.8881932,1.7365568,1.3717013,1.7612199,1.9791704,.99849991,2.111441,2.2690058,1.8688699,1.8303655,1.5165718,2.2048086,1.9537364,2.1761731,3.1172293,2.3634281,1.5390785,2.5514438,2.5130811,2.1814153,1.4866666,1.726888,2.9655165,2.2036744,2.6278128,1.8914937,1.4511832,2.276316,2.208094,1.4279578,1.6914182,1.6835757,1.3432019,1.5494011,1.1781422,1.6435976,1.4548737,.83002106,1.1242384,.80138381,1.2524261,1.2573104,1.1125233,1.222561,1.4427389,1.5224594,1.679095,1.7133606,1.4751725,1.4904459,1.7120079,1.5918958,2.0797286,1.760043,2.033961,2.002554,2.7433757,1.8496813,2.0370773,1.6484719,1.8527105,1.3834457,1.4280802,1.3116238,2.0553094,2.1343938,1.6718025,1.6052598,1.5400427,2.5514022,1.9997492,1.3181581,2.2490458,1.6542942,1.6974176,1.5192033,2.193872,2.6127009,1.9973062,1.7190944,2.1044035,1.88718,2.3962808,1.8163198,1.1807574,1.6488288,2.2167592,.95015681,1.983633,2.2545886,2.1887568,2.2714182,1.8477507,1.5397618,1.9202836,2.1454134,2.3367346,2.1882957,2.1669828,1.4450626,1.8792752,2.6775395,1.9746154,1.7695012,2.1809191,2.1748169,1.2347989,1.4377964,2.1792883,1.863154,1.5424336,2.2771547,1.3850572,1.2643787,1.0750502,1.2806733,1.3209787,1.2055279,.95970957,1.2125106,.55752152,1.1182064,1.0889919,1.3337615,1.7079026,2.1449927,1.7293258,1.8805774,1.4715356,2.0841567,2.1687361,1.3861364,2.3101635,1.669222,2.7429097,1.1347173,2.1970731,1.7891419,1.5806897,1.5980383,1.9914476,2.4306479,1.6054717,2.1487356,2.4385789,2.1924893,2.706522,1.9003238,1.8027308,1.9653792,1.4870187,1.8199841,1.7116251,1.8387781,2.27893,1.4707321,1.5012781,1.8599507,2.643843,1.3474685,2.7543888,2.3926587,2.0599629,1.6576112,2.0033419,1.8477658,1.1923152,1.8002779,1.6865568,2.7677061,1.6314038,1.6874064,2.0633386,3.2075171,1.4153052,1.2623564,1.7272425,2.0483456,1.5730251,2.9270368,2.1774514,2.2557849,2.2373435,1.598812,1.1924487,2.098487,3.1131986,2.1626021,1.8054886,1.5735135,1.4884572,1.9197685,1.2548668,1.3178826,1.6181616,1.1515121,.8338156,1.0758449,.91361492,1.0607173,1.2898844,1.3335163,1.2239475,.87093766,1.3692269,1.2267534,2.0857788,1.3617606,1.9036878,1.7620303,2.227032,1.7993649,1.8504501,1.7157396,1.6623763,1.8822909,2.0353768,1.9183549,2.7029566,3.6134577,1.4150624,2.4459061,2.4862385,1.5702534,1.476116,2.3538547,1.1836144,2.0005251,2.3942032,1.8320351,1.9329519,2.4778369,2.0718811,1.6222112,1.8641935,1.6004538,1.9784886,1.443055,1.8352299,1.8693099,1.3138299,2.4006107,2.4940645,1.6426751,1.6118467,2.2599108,2.0837132,3.0470947,1.5768486,.97634502,1.5913458,2.6594978,1.8186912,1.9169223,2.3253224,2.3986398,2.371961,3.4753605,1.6601488,1.6510313,1.5010407,1.8574498,1.8898645,2.1749959,1.5373309,1.6428408,1.2783029,1.9767043,2.2936493,1.6538095,1.0904218,1.5955439,1.769362,1.2383781,1.1166075,.99760077,1.5136025,1.200187,.97629321,.80033395,1.0981986,.81465839,.85448195,1.3240306,1.6190239,1.2139452,1.0310994,1.7279537,1.4246087,1.4693778,1.5267279,1.64361,1.5147679,2.2460687,1.3570052,1.27049,1.6587986,1.0527913,2.0414787,1.6194969,1.6225034,1.5957874,2.3949669,1.4328523,1.7800166,1.3525579,1.9948422,1.6434825,1.3649216,1.4754267,1.1953161,1.7543847,2.0327057,1.6531045,1.4528075,1.706656,1.5525566,2.0257176,1.9187212,1.4474351,1.5162162,1.2728672,1.8429029,1.9768607,1.6059284,1.5572298,1.7222487,1.4422234,2.3624042,1.8548196,1.0591202,1.7464634,1.3333837,1.4001926,2.0864621,2.135864,1.9152907,1.6185073,1.4474026,2.0131741,2.6180217,1.4586311,1.7698581,1.5642927,2.3051762,1.7145479,1.7145645,1.686396,1.4899991,1.2135307,1.4200605,1.5569857,1.7726416,1.4438518,.99346558,1.5999108,1.024861,1.0890406,.91933744,1.631669,.89991865,1.4717787,1.3357447,1.2887007,1.1523481,1.1371681,1.2714111,1.2332876,1.5540449,2.0911602,1.2036373,1.3793298,1.5489395,1.4627402,1.2310435,2.6069604,2.0952637,1.534267,2.7847317,1.8598911,1.5839004,1.8625952,1.8365601,1.6842834,1.3806975,1.7106167,1.8237703,1.342825,1.5512142,1.9411451,1.5953167,2.1147168,1.6496782,2.2741814,1.7366068,1.6470036,1.5167117,2.3126912,1.9166349,1.242891,1.9067896,2.0101604,1.2212879,1.4096559,1.2251759,1.5259432,1.4631568,1.8955268,1.4246145,1.5085565,1.9460133,1.7631678,3.2188826,2.742478,1.8447851,1.6068085,1.3747885,2.4756272,1.7552015,1.7832165,1.5593729,2.2621883,1.4796391,1.8490986,1.792128,2.0261348,2.1561015,1.2429311,1.3281518,2.5511558,1.738545,1.4227243,1.4659203,2.1570527,1.2574987,1.4903584,1.724805,1.0257101,1.1914645,2.1636661,1.2449466,1.5675588,.71819203,.91069681,1.112042,1.059094,1.4016226,1.22884,.90974121,1.5294787,.75044547,1.4624927,1.7430586,1.5516605,2.1101952,1.6960647,1.9273923,2.1971365,1.628087,1.9659735,2.4745676,1.22784,1.8180211,2.27114,1.7073591,1.843049,1.6394438,1.8169245,2.0020694,1.7913346,2.0081313,1.7451584,1.5667649,2.2579828,1.3410296,1.3549474,1.3722005,1.3446939,2.0278481,1.433244,1.739939,2.1418223,1.8681138,1.991315,1.5827643,3.2166968,2.3677273,2.684128,1.5974134,2.1795129,1.959992,1.9770005,1.3667313,1.7696264,1.3126062,1.6385399,2.209409,3.398844,1.4406392,3.0053185,2.4490745,2.0494008,1.8151632,2.5998114,1.5836965,2.5674557,1.9263718,2.2258983,1.7093807,1.721037,2.354009,1.8219143,2.0446016,1.6848209,1.6674662,1.8788552,1.5705137,1.3692913,1.0830433,1.6098257,1.1150221,1.4863497,1.2348629,1.3997423,1.0324226,.70651103,.81840928,1.0610551,1.3971293,1.7091173,1.2914554,1.3361535,1.7702115,1.5718114,1.2342786,1.9199208,1.7113216,1.7345578,1.3867797,1.7573394,1.3220703,2.4610131,2.052728,2.1391144,1.8627941,1.4298997,1.8283747,2.2112668,1.3295455,1.4537863,2.3316472,1.3983411,2.1515619,1.8163179,1.7619085,2.4582894,1.145609,2.0469658,1.8484048,1.3397731,1.7212729,2.0031649,2.1266671,1.7427529,1.0914236,2.2757567,1.5439375,1.8276355,1.6543269,1.3133395,1.850561,2.2805973,1.5998383,1.5990931,1.6748949,1.8292394,1.5952028,2.2015504,2.1979343,1.4296741,2.2587221,1.7946045,1.7349746,2.0361851,2.5008145,1.2286589,3.003856,1.4883894,1.4313113,1.9285079,1.4378405,1.8820868,1.675351,1.4690324,1.4573307,1.3491714,1.0891999,1.2978958,1.0351488,1.1114601,1.0504016,.48704082,.84641174,.56313555,.93137038,1.2709865,1.2326208,1.3559667,.94564396,1.0821413,1.1768049,1.3273345,1.347848,1.4514521,1.5827775,1.3774996,1.8692279,1.5825967,1.5205748,2.5684084,1.4142632,2.0504448,1.3072908,2.351949,1.9189055,1.5827215,1.4451363,1.9412271,1.7505884,1.6364608,1.3631338,1.8308923,1.7752157,1.2608082,1.3337804,1.6766684,1.1493091,1.6412264,1.7983686,1.0340087,1.6793624,2.0351679,1.075778,1.2246538,2.3885818,3.3433779,1.9020843,2.0337582,2.0749151,1.2772,1.2934398,1.4177308,1.2881155,2.1180323,1.4404893,1.897107,2.2287185,2.2918815,1.4420972,1.7806136,2.0255825,1.4946133,2.0114159,2.9045439,2.4749023,1.6911367,2.2445156,1.9403802,1.9760059,1.6861774,1.3147485,1.4126053,2.2710464,1.3255159,1.8275645,1.2031277,1.4333958,2.0027225,1.3958972,.78224003,.9202684,1.1732191,.76449677,.83786714,1.3011927,1.0298418,1.2801513,1.0735571,.82556287,.76321105,1.0365768,1.4775897,.9853043,1.7069666,2.1189453,1.2133785,1.9913764,1.4454624,2.2484218,1.5783358,1.0190089,1.9692241,1.5486198,2.2319056,1.6985272,1.752126,.9249996,.83413708,1.8578428,1.2145486,1.8112283,1.5251548,1.3013898,1.498576,1.5068849,1.6962861,.95696495,1.7806734,1.7453003,1.9514403,1.537452,1.7536699,1.6713509,1.7753993,1.0556818,1.457435,2.2829708,1.8036095,1.7325867,2.0988298,1.9278818,1.763997,1.4868074,1.911837,1.8669759,2.9070022,1.8368694,1.4938891,1.8296825,2.0860863,2.1724129,1.95032,1.3521269,2.2371862,1.8834968,1.5354294,1.762551,1.2585985,1.4943407,2.1265129,2.0581704,3.2141419,1.5431596,1.324378,2.0646874,1.7924824,1.1511113,1.8634772,1.1816361,1.0668434,1.6807702,1.2952483,1.4129145,1.5738213,.55114483,1.0613712,.63884596,.8950345,.86622096,1.0681119,.49981524,1.1443626,.97904212,1.2883847,1.217222,1.6035471,1.4790314,1.3611865,1.1487499,1.5347683,1.8983919,1.2954843,1.4859352,1.2857622,2.5702313,1.5159285,1.1574183,1.7673056,1.4261945,1.3591552,1.4242272,1.9970829,1.8756067,1.6559662,1.3306276,1.5611635,2.0419886,1.9570897,1.0631568,1.6665088,1.9753713,1.5479708,1.7180006,2.520308,1.727262,2.3928876,1.2889912,1.5577313,1.1297018,1.7681409,1.7456424,2.7558087,1.8975271,1.6579552,1.7083436,2.2418886,1.5759265,2.0886155,2.2414315,2.1080626,1.5099303,1.5151621,2.45117,2.2226929,1.4985808,2.0700989,1.6579781,2.0344462,1.5723177,1.3405269,1.8791903,1.6642831,.90567026,1.964873,2.2848443,1.1787853,1.1905507,.90776062,2.108124,1.0883983,1.5888046,.77732123,.83693763,.81551928,1.1710005,.86512618,1.2811787,.7820897,.9149315,.45850858,.88493222,.84057314,.92816296,1.1396381,1.2427449,1.0486576,2.1429215,1.0905735,1.6358539,1.2471114,1.3042478,1.2267781,2.5087277,1.1206302,2.7349566,2.4281831,1.6228703,1.3679348,1.3552291,1.1415301,1.6776165,1.7728657,1.8638664,1.3011915,2.403519,1.3827296,1.0209027,1.6168274,1.5757992,2.1462606,1.8757055,1.2016595,.98610813,1.6683705,1.8458875,1.5489873,1.5827084,1.3896466]}],stripes_left:[{size:31,values:[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]}],stripes_right:[{size:31,values:[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]}]};function U0(e){return(J4[e.name]??[]).map(n=>({size:n.size,values:Float64Array.from(n.values)}))}class G0{constructor(t,n,s){this.n=t,this.lo=n,this.hi=s,this.values=new Float64Array((s-n+1)*t)}values;index(t,n){const s=n-t;return s<this.lo||s>this.hi||t<0||t>=this.n?-1:(s-this.lo)*this.n+t}at(t,n){const s=this.index(t,n);return s<0?0:this.values[s]}set(t,n,s){const o=this.index(t,n);o>=0&&(this.values[o]=s)}}function H0(e,t,n){const s=new Uint8Array(n-t);for(let o=t;o<n;o++)s[o-t]=Number.isFinite(e[o])?1:0;return s}function Q0(e,t){const n=e.length,s=Va(e),o=new Float64Array(n);for(let c=0;c<n;c++)o[c]=Math.abs(e[c]-s);const r=Va(o),i=Math.max(1,s-r*t),a=new Uint8Array(n);for(let c=0;c<n;c++)a[c]=e[c]>=i?1:0;return a}function Va(e){const t=Array.from(e).filter(s=>Number.isFinite(s));if(t.length===0)return Number.NaN;t.sort((s,o)=>s-o);const n=t.length>>1;return t.length%2===1?t[n]:(t[n-1]+t[n])/2}function Z4(e,t,n,s){const o=new Float64Array(t),r=Math.min(t,s+1);for(let i=0;i<r;i++){let a=0,c=0;for(let l=0;l+i<t;l++){if(n[l]===0||n[l+i]===0)continue;const u=e.at(l,l+i);u>0&&(a+=u,c++)}o[i]=c===0?Number.NaN:a/c}return o}function j4(e,t,n,s,o=10){const r=Math.min(s,Math.max(0,t-1)),i=Z4(e,t,n,s),a=new G0(t,0,r);for(let c=0;c<=r;c++){const l=Number.isFinite(i[c])?i[c]:0,u=(c-a.lo)*t;for(let h=0;h+c<t;h++){const f=e.at(h,h+c);if(!Number.isFinite(f)||f===0)continue;const d=f/l;a.values[u+h]=d>=o?1:d}}return a}function t6(e,t,n,s,o,r){const i=e-t;if(i>=1&&i<=r)return!0;if(e>=0&&e<n&&t>=0&&t<n){const a=t-e;return a>=0&&a<=o&&(s[e]===0||s[t]===0)}return t<0?e<0:t>=n?e>=n-o-2:e<0?t<o+r:!1}function oi(e,t,n,s,o,r,i){const a=s.size,c=(a-1)/2,l=a*a,u=Math.trunc((1-i.missingTol)*l);let h=0,f=0,d=0,m=0,g=0,p=0,w=0,b=0;for(let k=0;k<a;k++){const C=o-c+k;for(let B=0;B<a;B++){const F=r-c+B,$=s.values[k*a+B];if(w+=$,b+=$*$,t6(C,F,t,n,i.maxDist,a)){p++,m+=$,g+=$*$;continue}const _=C>=0&&C<t&&F>=0&&F<t?e.at(C,F):0;h+=_,f+=_*_,d+=_*$}}const y=l-p;if(y<u||y===0)return{corr:0,present:y};const A=h/y,N=f/y,M=(w-m)/y,I=(b-g)/y,x=(N-A*A)*(I-M*M),v=x>0?Math.sqrt(x):0;if(!(Math.abs(v)>=1e-10))return{corr:0,present:y};const S=(d/y-A*M)/v;return{corr:Math.min(1,Math.max(-1,S)),present:y}}function e6(e,t){return new G0(e,0,Math.min(t,Math.max(0,e-1)))}function n6(e,t,n,s,o,r,i,a){const c=o.size,l=(c-1)/2,u=c*c,h=new Int32Array(n+1);for(let F=0;F<n;F++)h[F+1]=h[F]+(s[F]===0?1:0);const f=(F,$)=>{const _=Math.max(0,F),E=Math.min(n,$);return E<=_?!1:h[E]-h[_]>0};let d=0,m=0;for(let F=0;F<u;F++)d+=o.values[F],m+=o.values[F]*o.values[F];const g=d/u,w=m/u-g*g,b=Math.trunc((1-r.missingTol)*u),y=t.values,A=o.values,N=(F,$,_,E,O,L)=>{const D=u-E;if(D<b||D===0)return 0;const q=F/D,X=$/D,R=(d-O)/D,P=(m-L)/D,z=(X-q*q)*(P-R*R),U=z>0?Math.sqrt(z):0;return Math.abs(U)>=1e-10?Math.min(1,Math.max(-1,(_/D-q*R)/U)):0},M=new Float64Array(c+3),I=(F,$,_)=>{if($===0)return 0;const E=F/u,O=($/u-E*E)*w,L=O>0?Math.sqrt(O):0;return Math.abs(L)>=1e-10?Math.min(1,Math.max(-1,(_/u-E*g)/L)):0},x=t.hi-t.lo+1,v=Math.max(0,i-l),S=Math.min(n,a+l),k=new Float64Array(Math.max(0,S-v)*x);for(let F=0;F<x;F++)for(let $=v;$<S;$++)k[($-v)*x+F]=y[F*n+$];const C=Math.max(c-1,t.lo+c-1),B=Math.min(e.hi,t.hi-c+1)-3;for(let F=Math.max(0,i);F<Math.min(n,a);F++){const $=F-l,_=F+l+1,E=$>=0&&_<=n,O=E&&!f($,_);for(let L=0;L<=e.hi;L++){const D=F+L;if(D>=n)break;const q=D-l,X=D+l+1,R=E&&L>=C&&L<=B&&X+3<=n;if(R&&O&&!f(q,X+3)){let G=0,Y=0,V=0,J=0,Z=0,ut=0,Q=0,W=0,et=0,j=0,ot=0,at=0;for(let ft=0;ft<c;ft++){const kt=$+ft;let pt=(kt-v)*x+(q-kt)-t.lo,Ft=ft*c,gt=k[pt],yt=k[pt+1],Nt=k[pt+2];for(let oe=0;oe<c;oe++){const Kt=A[Ft],wt=gt;gt=yt,yt=Nt,Nt=k[pt+3],G+=wt,Y+=wt*wt,V+=wt*Kt,J+=gt,Z+=gt*gt,ut+=gt*Kt,Q+=yt,W+=yt*yt,et+=yt*Kt,j+=Nt,ot+=Nt*Nt,at+=Nt*Kt,pt++,Ft++}}const ht=L*n+F;e.values[ht]=I(G,Y,V),e.values[ht+n]=I(J,Z,ut),e.values[ht+2*n]=I(Q,W,et),e.values[ht+3*n]=I(j,ot,at),L+=3;continue}if(R&&L+3+c-1<=r.maxDist){for(let Lt=0;Lt<c+3;Lt++)M[Lt]=s[q+Lt]===0?1:0;const G=h[_]-h[$],Y=G*c+(c-G)*(h[X]-h[q]),V=G*c+(c-G)*(h[X+1]-h[q+1]),J=G*c+(c-G)*(h[X+2]-h[q+2]),Z=G*c+(c-G)*(h[X+3]-h[q+3]);let ut=0,Q=0,W=0,et=0,j=0,ot=0,at=0,ht=0,ft=0,kt=0,pt=0,Ft=0,gt=0,yt=0,Nt=0,oe=0,Kt=0,wt=0,Et=0,cn=0;for(let Lt=0;Lt<c;Lt++){const fe=$+Lt,de=s[fe]===0;let re=(fe-v)*x+(q-fe)-t.lo,mi=Lt*c,ho=k[re],fo=k[re+1],mo=k[re+2],ln=de?1:M[0],un=de?1:M[1],hn=de?1:M[2];for(let go=0;go<c;go++){const Vt=A[mi],qn=Vt*Vt,eu=ho;ho=fo,fo=mo,mo=k[re+3];const po=ln;ln=un,un=hn,hn=de?1:M[go+3];const Un=eu*(1-po),Gn=ho*(1-ln),Hn=fo*(1-un),Qn=mo*(1-hn);ut+=Un,Q+=Un*Un,W+=Un*Vt,et+=Vt*po,j+=qn*po,ot+=Gn,at+=Gn*Gn,ht+=Gn*Vt,ft+=Vt*ln,kt+=qn*ln,pt+=Hn,Ft+=Hn*Hn,gt+=Hn*Vt,yt+=Vt*un,Nt+=qn*un,oe+=Qn,Kt+=Qn*Qn,wt+=Qn*Vt,Et+=Vt*hn,cn+=qn*hn,re++,mi++}}const it=L*n+F;e.values[it]=N(ut,Q,W,Y,et,j),e.values[it+n]=N(ot,at,ht,V,ft,kt),e.values[it+2*n]=N(pt,Ft,gt,J,yt,Nt),e.values[it+3*n]=N(oe,Kt,wt,Z,Et,cn),L+=3;continue}if(!($>=0&&_<=n&&q>=0&&X<=n)){const G=oi(t,n,s,o,F,D,r);e.values[L*n+F]=G.corr;continue}if(q<=_-2||f($,_)||f(q,X)){e.values[L*n+F]=s6(t,n,s,o,$,q,r.maxDist,d,m,b);continue}let z=0,U=0,K=0;for(let G=0;G<c;G++){const Y=$+G,V=Math.max(0,t.lo-(q-Y)),J=Math.min(c,t.hi-(q-Y)+1);if(J<=V)continue;let Z=(q+V-Y-t.lo)*n+Y,ut=G*c+V;for(let Q=V;Q<J;Q++){const W=y[Z];z+=W,U+=W*W,K+=W*A[ut],Z+=n,ut++}}e.values[L*n+F]=I(z,U,K)}}}function s6(e,t,n,s,o,r,i,a,c,l){const u=s.size,h=s.values,f=e.values,{lo:d,hi:m}=e;let g=0,p=0,w=0,b=0,y=0,A=0;for(let F=0;F<u;F++){const $=o+F,_=n[$]===0;for(let E=0;E<u;E++){const O=r+E,L=h[F*u+E],D=O-$;if(D<0||(_||n[O]===0)&&D<=i){b++,y+=L,A+=L*L;continue}const q=D>=d&&D<=m?f[(D-d)*t+$]:0;g+=q,p+=q*q,w+=q*L}}const M=u*u-b;if(M<l||M===0)return 0;const I=g/M,x=p/M,v=(a-y)/M,S=(c-A)/M,k=(x-I*I)*(S-v*v),C=k>0?Math.sqrt(k):0;if(!(Math.abs(C)>=1e-10))return 0;const B=(w/M-I*v)/C;return Math.min(1,Math.max(-1,B))}function K0(e,t){if(!Number.isFinite(e)||t<=3)return Number.NaN;const n=Math.atanh(Math.min(1-1e-16,Math.max(-1+1e-16,e)));return 2*o6(-Math.abs(n*Math.sqrt(t-3)))}function o6(e){return .5*r6(-e/Math.SQRT2)}function r6(e){const t=Math.abs(e),n=t<2?1-i6(t):a6(t);return e>=0?n:2-n}function i6(e){let t=e,n=e;for(let s=1;s<200&&(t*=2*e*e/(2*s+1),n+=t,!(t<Math.abs(n)*1e-17));s++);return 2/Math.sqrt(Math.PI)*Math.exp(-e*e)*n}function a6(e){let n=1/e,s=1e300,o=n;for(let r=1;r<300;r++){const i=r/2;n=e+i*n,n===0&&(n=1e-300),s=e+i/s,s===0&&(s=1e-300),n=1/n;const a=s*n;if(o*=a,Math.abs(a-1)<1e-16)break}return Math.exp(-e*e)/Math.sqrt(Math.PI)*o}function V0(e){const t=e.length,n=new Float64Array(t).fill(Number.NaN),s=[];for(let i=0;i<t;i++)Number.isFinite(e[i])&&s.push(i);s.sort((i,a)=>e[a]-e[i]);const o=s.length;let r=Number.POSITIVE_INFINITY;for(let i=0;i<o;i++){const a=o/(o-i);r=Math.min(r,a*e[s[i]]),n[s[i]]=Math.min(1,r)}return n}function c6(e,t,n=2){const s=e.n,o=e.hi-e.lo+1,r=new Int32Array(o*s).fill(-1),i=u=>{let h=u;for(;r[h]!==h;)h=r[h];let f=u;for(;r[f]!==h;){const d=r[f];r[f]=h,f=d}return h},a=(u,h)=>{const f=i(u),d=i(h);f!==d&&(r[Math.max(f,d)]=Math.min(f,d))},c=(u,h)=>{const f=e.index(u,h);return f<0?-1:e.values[f]>=t?f:-1};for(let u=0;u<s;u++)for(let h=e.lo;h<=e.hi;h++){const f=u+h;if(f<0)continue;if(f>=s)break;const d=c(u,f);if(d<0)continue;r[d]=d;const m=c(u,f-1);m>=0&&a(d,m);const g=c(u-1,f);g>=0&&a(d,g)}const l=new Map;for(let u=0;u<s;u++)for(let h=e.lo;h<=e.hi;h++){const f=u+h;if(f<0)continue;if(f>=s)break;const d=c(u,f);if(d<0)continue;const m=i(d),g=e.values[d],p=l.get(m);if(p===void 0){l.set(m,{row:u,col:f,score:g,size:1});continue}p.size++,g>p.score&&(p.row=u,p.col=f,p.score=g)}return[...l.values()].filter(u=>u.size>=n)}function Y0(e,t,n,s,o,r){const i=Math.floor(s/2)+1,a=(s-1)/2;if(o-i+1+a<0||o+i+a>=t+2*a||r-i+1+a<0||r+i+a>=t+2*a)return null;const c=new Float64Array(s*s);for(let l=0;l<s;l++){const u=o-i+1+l,h=u<0||u>=t||n[u]===0;for(let f=0;f<s;f++){const d=r-i+1+f,m=h||d<0||d>=t||n[d]===0||u-d>=1&&u-d<=s;c[l*s+f]=m?Number.NaN:e.at(u,d)}}return c}function W0(e,t){let n=0,s=0;for(const a of e)Number.isFinite(a)?a===0&&s++:n++;const o=e.length,r=n/o,i=o-n===0?1:s/(o-n);return r<t.missingTol&&i<t.zeroTol}function ri(e,t){const n=new Float64Array(t*t),s=new Int32Array(t*t);for(const r of e)for(let i=0;i<n.length;i++){const a=r[i];Number.isFinite(a)&&(n[i]+=a,s[i]++)}const o=new Float64Array(t*t);for(let r=0;r<o.length;r++)o[r]=s[r]>0?n[r]/s[r]:Number.NaN;return o}function X0(e,t){const n=new Float64Array(t*t);for(let s=0;s<n.length;s++)n[s]=Number.isFinite(e[s])?e[s]:0;for(let s=0;s<t;s++)for(let o=0;o<t;o++)s!==o&&(n[s*t+o]+=Number.isFinite(e[o*t+s])?e[o*t+s]:0);return n}function J0(e,t){return j4(e.contacts,e.n,e.detectable,ii(e.n,t.maxDist,t.largestKernel))}function ii(e,t,n){return Math.min(t,e)+n}function l6(e,t,n,s,o){const{n:r,detectable:i}=e,a=[];let c=0;for(const l of c6(n,o.pearson)){const u=o.onDiagonal===!0?l.col:l.row,h=Y0(t,r,i,s.size,u,l.col);if(h===null||!W0(h,o)){c++;continue}const f=oi(t,r,i,s,u,l.col,o);a.push({pattern:{bin1:u,bin2:l.col,score:n.at(u,l.col),pvalue:K0(n.at(u,l.col),f.present),kernel:0,iteration:0},values:h})}return{matrix:t,corr:n,found:a,rejected:c}}function u6(e,t,n,s,o){const{n:r,detectable:i}=e,a=[];for(const c of o){const l=Y0(t,r,i,n.size,c.bin1,c.bin2),u=l!==null&&W0(l,s),h=u?oi(t,r,i,n,c.bin1,c.bin2,s):null;a.push({pattern:{bin1:c.bin1,bin2:c.bin2,score:h===null?Number.NaN:h.corr,pvalue:h===null?Number.NaN:K0(h.corr,h.present),kernel:0,iteration:0},values:u&&l!==null?l:new Float64Array(n.size*n.size).fill(Number.NaN)})}return a}function h6(e,t){const n=e.map((o,r)=>r);n.sort((o,r)=>{const i=e[r].score-e[o].score;return Number.isNaN(i)?Number.isNaN(e[o].score)?1:-1:i!==0?i:o-r});const s=new Set;for(const o of n){if(s.has(o))continue;const r=e[o];for(const i of n)i===o||s.has(i)||Math.abs(e[i].bin1-r.bin1)<t&&Math.abs(e[i].bin2-r.bin2)<t&&s.add(i)}return e.map((o,r)=>!s.has(r))}function f6(e,t){const n=h6(e,t.separationBins),s=e.filter((o,r)=>n[r]&&Math.abs(o.bin2-o.bin1)>=t.minDistBins&&Number.isFinite(o.pvalue));return{kept:s,qvalues:V0(s.map(o=>o.pvalue))}}const d6=0,m6=2,Z0={colormap:"seismic",centre:1,range:[d6,m6]},g6=16e6;function ro(e,t){let s=Math.max(256,Math.min(e,4096));for(;s>1&&s*(s+t)>g6;)s=Math.floor(s/2);return Math.max(1,s)}function p6(e,t){const n=ro(Math.max(0,t)*2,Math.max(0,t)*2);return Math.max(1,Math.ceil(Math.max(1,e)/n))}function ai(e,t){return e.rowExtent[0]<=t.from&&e.rowExtent[1]>=t.to&&e.colExtent[0]<=t.from&&e.colExtent[1]>=t.to}function Ct(e,t,n){const s=e.inputs.block;if(s===void 0||s.binsPerCell!==1||e.framing.balance!==n)return null;for(const o of t)if(!ai(s,o))return null;return s}function ci(e,t){if(e.rowExtent[0]===t.from&&e.rowExtent[1]===t.to&&e.colExtent[0]===t.from&&e.colExtent[1]===t.to)return e;const s=t.to-t.from,o=new Float32Array(s*s),r=t.from-e.rowExtent[0],i=t.from-e.colExtent[0];for(let a=0;a<s;a++){const c=(r+a)*e.ncols+i;o.set(e.values.subarray(c,c+s),a*s)}return{nrows:s,ncols:s,values:o,rowExtent:[t.from,t.to],colExtent:[t.from,t.to],binsPerCell:1}}async function io(e,t,n){if(n.block!==null)return ci(n.block,t);const s=await e.mc.matrix({rows:[t.from,t.to],cols:[t.from,t.to],balance:n.balance,value:n.value});return{nrows:s.nrows,ncols:s.ncols,values:s.values,rowExtent:[s.rowExtent[0],s.rowExtent[1]],colExtent:[s.colExtent[0],s.colExtent[1]],binsPerCell:1}}class w6{constructor(t,n,s=!1){this.n=t,this.hi=n,this.circular=s,this.values=new Float32Array((n+1)*t).fill(Number.NaN)}values;at(t,n){const s=n-t;if(s<0||s>this.hi||t<0||t>=this.n)return 0;const o=this.values[s*this.n+t];return Number.isNaN(o)?0:o}forward(t,n){if(n<0)return this.forward(t+n,-n);if(n>this.hi)return Number.NaN;let s=t;if(this.circular)s=(t%this.n+this.n)%this.n;else if(s<0||s+n>=this.n)return Number.NaN;return this.values[n*this.n+s]}}async function De(e,t,n){const s=t.to-t.from,o=n.circular===!0,r=Math.max(0,Math.min(n.hi,o?Math.floor(s/2):Math.max(0,s-1))),i=new w6(s,r,o),a=(h,f,d,m,g)=>{for(let p=d;p<m;p++){const w=(p-d)*f-g,b=Math.min(s-1,p+r);for(let y=p;y<=b;y++)i.values[(y-p)*s+p]=h[w+y]}},c=(h,f)=>{for(let d=0;d<r;d++)for(let m=s-r;m<s;m++){const g=d+s-m;g<1||g>r||(i.values[g*s+m]=h[d*f+(m-(s-r))])}};if(n.block!==null){const h=n.block,f=t.from-h.rowExtent[0],d=t.from-h.colExtent[0];for(let m=0;m<s;m++){const g=(f+m)*h.ncols+d,p=Math.min(s-1,m+r);for(let w=m;w<=p;w++)i.values[(w-m)*s+m]=h.values[g+w]}if(o&&r>0)for(let m=0;m<r;m++){const g=(f+m)*h.ncols+d;for(let p=s-r;p<s;p++){const w=m+s-p;w<1||w>r||(i.values[w*s+p]=h.values[g+p])}}return i}const l=ro(r,r),u=Math.max(1,Math.ceil(s/l));for(let h=0;h<u;h++){await n.onTile?.(h,u);const f=h*l,d=Math.min(s,f+l);if(d<=f)break;const m=Math.min(s,d+r),g=await e.mc.matrix({rows:[t.from+f,t.from+d],cols:[t.from+f,t.from+m],balance:n.balance,value:n.value});a(g.values,g.ncols,f,d,f)}if(o&&r>0){const h=await e.mc.matrix({rows:[t.from,t.from+r],cols:[t.to-r,t.to],balance:n.balance,value:n.value});c(h.values,h.ncols)}return await n.onTile?.(u,u),i}async function b6(e,t,n){const s=t.to-t.from,o=Math.max(0,Math.min(n.hi,s-1)),r=new Float64Array(o+1),i=new Float64Array(o+1),a=(u,h,f,d,m)=>{for(let g=d;g<m;g++){const p=f+g*h,w=Math.min(s-1,g+o);for(let b=g;b<=w;b++){const y=u[p+b];Number.isNaN(y)||(r[b-g]+=y,i[b-g]+=1)}}},c=n.block;if(c!==null&&ai(c,t)){const u=t.from-c.rowExtent[0],h=t.from-c.colExtent[0];a(c.values,c.ncols,u*c.ncols+h,0,s)}else{const u=ro(o,o),h=Math.max(1,Math.ceil(s/u));for(let f=0;f<h;f++){await n.onTile?.(f,h);const d=f*u,m=Math.min(s,d+u);if(m<=d)break;const g=await e.mc.matrix({rows:[t.from+d,t.from+m],cols:[t.from+d,t.from+Math.min(s,m+o)],balance:n.balance,value:n.value});a(g.values,g.ncols,-d*g.ncols-d,d,m)}await n.onTile?.(h,h)}const l=new Float64Array(o+1);for(let u=0;u<=o;u++)l[u]=i[u]>0?r[u]/i[u]:Number.NaN;return l}async function j0(e,t,n,s){const o=t.to-t.from,r=Math.max(0,n.halo),i=ro(r*2,r*2),a=p6(o,r);for(let c=0;c<a;c++){const l=c*i,u=Math.min(o,l+i);if(u<=l)break;const h=Math.max(0,l-r),f=Math.min(o,u+r),d=await io(e,{from:t.from+h,to:t.from+f},{block:n.block,balance:n.balance,value:n.value});await s({values:d.values,size:f-h,from:h,ownFrom:l,ownTo:u,index:c,total:a})}}async function t1(e){const t=e.mc,n=new Int32Array(t.nBins).fill(-1);for(let o=0;o<t.nContigs;o++){const[r,i]=t.contigExtent(o);for(let a=r;a<i;a++)n[a]=o}const s=new Float64Array(t.nBins);return e.progress(0,1,"counting coverage"),await t.scanPixels({onChunk:o=>{e.throwIfCancelled();for(let r=0;r<o.length;r++){const i=o.bin1[r],a=o.bin2[r];if(n[i]!==n[a])continue;const c=o.count[r];!Number.isFinite(c)||c===0||(s[i]+=1,a!==i&&(s[a]+=1))}},onProgress:(o,r)=>e.progress(o,r,"counting coverage")}),s}function ao(e){const t=[],n=e.hasGenomes?new Set(e.displayGenomes()):null;for(let s=0;s<e.nContigs;s++)n!==null&&!n.has(e.contigGenomeIds[s])||t.push(s);return t}function be(e,t){return t===null?{ids:ao(e),missing:[]}:no(e,t)}function he(e,t,n){if(t.length===0)return null;const s=`${t.slice(0,6).join(", ")}${t.length>6?", …":""}`;return n>0?`${t.length.toLocaleString()} ticked ${t.length===1?"sequence is":"sequences are"} not in this file and ${t.length===1?"is":"are"} left out: ${s}.`:`This file has no sequence called ${s}, under that spelling or any this viewer knows of. It has ${e.chromnames.slice(0,6).join(", ")}${e.chromnames.length>6?", …":""}.`}function Ut(e,t){const[n,s]=e.contigExtent(t);return s>n?{from:n,to:s}:null}function Qt(e,t){if(!t)return null;const n=`bins/${ae}`;if(!e.handle.has(n))throw new T("unsupported",`This file has no /${n} column, so there are no weights to balance with. Run \`cooler balance\` on it, or turn balancing off and read the raw counts.`,{column:ae});return e.handle.readNumeric(n,0,e.nBins)}function rt(e){return gu(e)}function A6(e){const t=Math.round(e);return t===0?"0":`${t<0?"-":"+"}${rt(Math.abs(t))}`}function On(e,t){if(!(e>0)||!(t>0))return[];const n=(e-1)/2*t;return[0,.25,.5,.75,1].map(s=>A6(-n+s*2*n))}function y6(e){const t=lt(e.params,"csPattern","loops");return nn.find(n=>n.name===t)??nn[0]}async function N6(e,t){const n=y6(e),s=U0(n)[0],o=nt(e.params,"balance",!1),r=e.framing.value===""?tt:e.framing.value,i=e.mc.binsize,a=t.reduce((v,S)=>Math.max(v,S.col-S.row),0),c=Math.max(Math.min(a,e.mc.nBins),1),l={maxDist:c,pearson:n.pearson,missingTol:n.maxPercUndetected/100,zeroTol:n.maxPercZero/100,onDiagonal:n.onDiagonal},u=t,h=new Map;for(const v of u){const S=h.get(v.bounds.from);S===void 0?h.set(v.bounds.from,[v]):S.push(v)}const f=Qt(e.mc,o);let d;if(f!==null)d=(v,S)=>H0(f,v,S);else{const v=await t1(e);d=(S,k)=>Q0(v.subarray(S,k),5)}const m=[...h.values()].map(v=>v[0].bounds),g=Ct(e,m,o),p=[],w=[];let b=0;for(const[,v]of h){await e.breathe(),e.progress(b,u.length,`${v.length.toLocaleString()} anchors`);const S=v[0].bounds,k=S.to-S.from;if(k<=s.size){b+=v.length;continue}const B={contacts:await De(e,S,{hi:ii(k,c,s.size),block:g,balance:o,value:r,onTile:async()=>{await e.breathe()}}),n:k,detectable:d(S.from,S.to)},F=J0(B,{maxDist:c,largestKernel:s.size}),$=u6(B,F,s,l,v.map(_=>({bin1:_.row-S.from,bin2:_.col-S.from})));for(let _=0;_<$.length;_++)p.push({anchor:v[_],score:$[_].pattern.score,pvalue:$[_].pattern.pvalue,qvalue:Number.NaN}),w.push($[_].values);b+=v.length}if(e.progress(u.length,u.length,"done"),e.throwIfCancelled(),w.length===0)return e.note("No anchor landed on a sequence long enough to score against this template."),[];const y=V0(p.map(v=>v.pvalue));for(let v=0;v<p.length;v++)p[v].qvalue=y[v];const A=p.filter(v=>Number.isFinite(v.score)).length;A<p.length&&e.note(`${(p.length-A).toLocaleString()} of ${p.length.toLocaleString()} anchors could not be scored: their window holds too many undetectable or empty pixels. They are in the table with no score, and they contribute nothing to the pileup.`);const N=s.size,M=ri(w,N),I=n.onDiagonal?X0(M,N):M,x=[M6(I,N,{preset:n,n:A,binsize:i}),I6(p,n)];if(!n.onDiagonal){const v=so(p.map(S=>({separation:(S.anchor.col-S.anchor.row)*i,score:S.score})),{id:"apa-chromosight-loop-spectrum",label:`Loop spectrum · ${n.name.replace(/_/g," ")}`,noun:"anchors",yLabel:"Pearson correlation with the template"});v!==null&&x.push(v)}return x.push(x6(p,i)),x}function M6(e,t,n){return{kind:"matrix",id:"apa-chromosight",label:`Pileup of ${n.n.toLocaleString()} ${n.preset.name.replace(/_/g," ")} · chromosight`,nrows:t,ncols:t,values:Float32Array.from(e),xLabel:"Distance from the centre",yLabel:"Distance from the centre",xTicks:On(t,n.binsize),yTicks:On(t,n.binsize),...Z0,stats:[{label:"windows",value:n.n,digits:0,hint:"Anchors that could be scored."},{label:"P2LL",value:yr(e,t,t),digits:2,hint:"Centre against the lower-left corner. Above 1 is enrichment."}]}}function I6(e,t){const n=e.map(u=>u.score).filter(u=>Number.isFinite(u)),s=n.length>0?Math.min(...n):0,o=n.length>0?Math.max(...n):1,r=Math.max(10,Math.min(50,Math.round(Math.sqrt(n.length)*2))),i=o>s?(o-s)/r:1,a=new Float64Array(r),c=new Float64Array(r);for(let u=0;u<r;u++)a[u]=s+i*(u+.5);for(const u of n)c[Math.min(r-1,Math.max(0,Math.floor((u-s)/i)))]++;return{kind:"curve",id:"apa-chromosight-spectrum",label:`Score spectrum · ${n.filter(u=>u>=t.pearson).length.toLocaleString()} of ${n.length.toLocaleString()} at or above ${t.pearson}`,series:[{name:"Anchors",x:a,y:c,n:n.length}],xLabel:"Pearson correlation with the template",yLabel:"Anchors",xScale:"linear",yScale:"linear"}}function x6(e,t){return{kind:"table",id:"apa-chromosight-anchors",label:"Per anchor",columns:[{key:"anchor",label:"Anchor",kind:"text"},{key:"separation",label:"Separation (bp)",kind:"number",digits:0},{key:"score",label:"Score",kind:"number",digits:4,hint:"Pearson r with the template."},{key:"pvalue",label:"p",kind:"number",digits:6},{key:"qvalue",label:"q",kind:"number",digits:6,hint:"Benjamini-Hochberg."}],rows:e.map(n=>({anchor:n.anchor.label,separation:(n.anchor.col-n.anchor.row)*t,score:n.score,pvalue:n.pvalue,qvalue:n.qvalue}))}}const Ya=512,v6=async e=>{const t=z0(e.params),n=Ds(e.inputs.pairs,e.params,"pairs"),s=Ds(e.inputs.features,e.params,"features"),o=t?n.value:void 0,r=t?void 0:s.value;if(o===void 0&&r===void 0)return e.note(B0(t?n:s,{unchosen:t?"No loop set was chosen. Load a BEDPE — a loop table, a set of anchor pairs — and pick it under Pairs.":"No feature set was chosen. Load a BED or a GTF and pick it under Features.",gone:`The ${t?"loop":"feature"} set chosen is no longer loaded. Re-open the file, or pick another.`})??""),[];const i=nt(e.params,"balance",!1),a=e.framing.value===""?tt:e.framing.value,c=Mt(e.params)?Math.max(11,Math.round(H(e.params,"grid",51))):H4(e.params)*2+1,l=lt(e.params,"norm","matched"),u=Qa(e.params),h=P0(e.params),f=t?F6(e,o):u?H6(e,r):h?G6(e,r):E6(e,r);if(f.length===0)return e.note(u?"No two features of this set are on one sequence within the separation range, so there is no pair to pile up. Widen the range, or check that the set and the file spell their sequences alike.":h?"No two features of this set are on one sequence this file has, so no feature has a next one to pair with. Check that the set and the file spell their sequences alike.":"No anchor of this set lands inside a sequence this file has, so there is nothing to pile up."),[];if(u&&e.note(`${f.length.toLocaleString()} pairs made from ${r.length.toLocaleString()} features.`),h&&e.note(`${f.length.toLocaleString()} pairs, each feature with the next along its sequence, made from ${r.length.toLocaleString()} features.`),Q4(e.params))return N6(e,f.map(z=>({row:z.pair.row,col:z.pair.col,bounds:z.bounds,label:z.label})));const d=(t||h)&&!Mt(e.params)?$6(e,f):f;if(d.length===0)return[];const m=X4(e.params),g=m>1?Q6(d,m):B6(e,d,te(e.params)?r:null),p=m>1?K6(g,z=>e.toBp(z)):null,w=D4(c,Ve(e.params)),b=Mt(e.params),y=oo(e.params),A=[],N=ni(Math.round(H(e.params,"seed",1))),M=l==="matched"?Math.max(1,Math.round(H(e.params,"controls",5))):0,I=Math.max(0,Math.round(H(e.params,"minShift",50))),x=Math.max(I,Math.round(H(e.params,"maxShift",500))),v=Math.max(0,H(e.params,"minShiftScale",1)),S=Math.max(v,H(e.params,"maxShiftScale",3));for(let z=0;z<g.length;z++)for(const U of g[z]){const K=Ja(e,U,c,w);if(K===null||(A.push({...K,label:U.label,separation:U.pair.separation,control:!1,group:z,bounds:U.bounds}),M===0))continue;const G=b?Math.max(1,L6(K.rect)):0,Y=b?Math.round(G*v):I,V=b?Math.max(Y,Math.round(G*S)):x;for(const J of z4(U.pair,U.bounds,{perPair:M,minShift:Y,maxShift:V,rng:N})){const Z=Ja(e,O6(U,J),c,w);Z!==null&&A.push({...Z,separation:J.separation,control:!0,group:z,bounds:U.bounds})}}if(A.length===0)return e.note("Every window ran off the end of its sequence. Use a narrower window."),[];const k=l==="oe"&&Mt(e.params)&&!te(e.params);k&&e.note("Observed / expected on a scaled grid is for a feature set on the diagonal, and these are pairs. The raw pileup is shown — use the distance-matched control, which is drawn at the same separations by construction.");const C=g.map(()=>new us(c,c)),B=g.map(()=>new us(c,c)),F=k6(e),$=g.map(()=>F.map(()=>new us(1,c))),_=g.map(()=>F.map(()=>new us(1,c))),E=Y4(e.params),O=[],L={minDiag:K4(e.params),mirrored:te(e.params)},D=Ct(e,A.map(R6),i),q=l==="oe"&&!k?await U6(e,A,{block:D,balance:i,value:a}):null,X=z6(A);let R=0;for(const z of X){await e.breathe(),e.progress(R,A.length,`${R.toLocaleString()} of ${A.length.toLocaleString()} windows`);const U=await P6(e,z.rect,{block:D,balance:i,value:a});for(const K of z.plans){R++;const G=q6(U,z.rect,K,c,w,{...L,expected:q?.get(K.bounds.from)});if(G!==null){if(K.control){B[K.group].add(G);continue}if(C[K.group].add(G),F.length>0){const Y=S6(K,w);for(let V=0;V<F.length;V++){const J=Wa(F[V].values,K.rect.rowFrom,K.rect.rowTo,c,Y),Z=Wa(F[V].values,K.rect.colFrom,K.rect.colTo,c,Y);$[K.group][V].add(J),_[K.group][V].add(Z),E&&($[K.group][V].add(Z),_[K.group][V].add(J))}}K.label!==void 0&&O.push({label:K.label,separation:K.separation*e.mc.binsize,score:y?Ga(G,w):yr(G,c,c)})}}}e.progress(A.length,A.length,"done"),e.throwIfCancelled();const P=[];for(let z=0;z<g.length;z++){const U=C[z].finish();if(U.n===0)continue;const K=p!==null?`APA · ${p[z]}`:g.length===1?"APA":`APA · group ${z+1}`;let G=U.values,Y,V=`${K} · raw`;if(l==="matched"){const Q=B[z].finish();Q.n>0?(G=R4(U.values,Q.values),Y=1,V=`${K} · over distance-matched control`):e.note("No control window could be placed, so the raw pileup is shown.")}else q!==null&&(Y=1,V=`${K} · observed / expected`);const J=te(e.params)?Rs(e.params)==="start"?"the feature’s start":Rs(e.params)==="end"?"the feature’s end":"the feature’s centre":"the centre",Z=Mt(e.params)?y?`Position along the feature (0 → 1), flanked by ${Ve(e.params)}×`:"Scaled position":`Distance from ${J}`,ut=Mt(e.params)?y?R0(e.params)?D6(Ve(e.params)):_6(w,Ve(e.params)):[]:On(c,e.mc.binsize);P.push({kind:"matrix",id:`apa-${z}`,label:V,nrows:c,ncols:c,values:G,xLabel:Z,yLabel:Z,xTicks:ut,yTicks:ut,colormap:Y===void 0?"default":pl,...Y===void 0?{}:{centre:Y},...(()=>{const Q=C6(F,$[z],_[z]);return Q===void 0?{}:{margins:Q}})(),stats:[{label:"windows",value:U.n,digits:0,hint:"Anchors that contributed."},y?{label:"in / out",value:Ga(G,w),digits:2,hint:"The domain’s own corner against the same block across its boundary. Above 1 means contact stops at the edge."}:{label:"P2LL",value:yr(G,c,c),digits:2,hint:"Centre against the lower-left corner. Above 1 is enrichment."}]})}if(O.length>0){if(!te(e.params)){const z=so(O,{id:"apa-loop-spectrum",label:"Loop spectrum",noun:Qa(e.params)?"pairs":"loops",yLabel:"P2LL — centre against the lower-left corner"});z!==null&&P.push(z)}O.sort((z,U)=>(Number.isNaN(U.score)?-1:U.score)-(Number.isNaN(z.score)?-1:z.score)),P.push({kind:"table",id:"apa-anchors",label:"Per anchor",columns:[{key:"anchor",label:"Anchor",kind:"text"},{key:"separation",label:"Separation (bp)",kind:"number",digits:0},{key:"score",label:y?"In / out":"P2LL",kind:"number",digits:3}],rows:O.map(z=>({anchor:z.label,separation:z.separation,score:z.score}))})}return P};function k6(e){const t=O4(e.params,"tracks"),n=[];for(const s of t){const o=e.inputs.vectors?.[`tracks:${s}`];o!==void 0&&n.push({name:e.inputs.labels?.[s]??s,values:o})}return n.length<t.length&&e.note(`${(t.length-n.length).toLocaleString()} of ${t.length.toLocaleString()} tracks could not be read and are not drawn beside the pileup. Re-open the file, or pick another under Track aggregates.`),n}function S6(e,t){if(e.body!==void 0)return _0(t,e.body.from-e.rect.rowFrom,e.body.to-e.rect.rowFrom,e.body.pad)}function Wa(e,t,n,s,o){const r=n-t;if(r<=0)return new Float64Array(s).fill(Number.NaN);const i=new Float64Array(r);for(let a=0;a<r;a++){const c=t+a;i[a]=c>=0&&c<e.length?e[c]:Number.NaN}return o===void 0?O0(i,1,r,1,s):L0(i,1,r,Float64Array.from([0,1]),o)}function C6(e,t,n){if(e.length===0)return;const s=[],o=[];for(let r=0;r<e.length;r++){const i=n[r].finish(),a=t[r].finish();i.n>0&&s.push({name:e[r].name,values:Float64Array.from(i.values)}),a.n>0&&o.push({name:e[r].name,values:Float64Array.from(a.values)})}if(!(s.length===0&&o.length===0))return{...s.length===0?{}:{top:s},...o.length===0?{}:{right:o}}}function F6(e,t){const n=[],s=Oe(t.contigs);for(let o=0;o<t.length;o++){const r=zs(e,t.contigs[t.contig1[o]],t.start1[o],t.end1[o],s),i=zs(e,t.contigs[t.contig2[o]],t.start2[o],t.end2[o],s);if(r===null||i===null||r.contig!==i.contig)continue;const a=Math.min(r.bin,i.bin),c=Math.max(r.bin,i.bin);n.push({pair:{row:a,col:c,separation:c-a},bounds:r.bounds,label:t.name[o]===""?ei(t.contigs[t.contig1[o]],t.start1[o]):t.name[o],rank:t.score[o],span:c-a,centreDistance:Math.abs((t.start2[o]+t.end2[o])/2-(t.start1[o]+t.end1[o])/2),rows:{from:a,to:a+1},cols:{from:c,to:c+1}})}return n}function $6(e,t){const n=V4(e.params),s=n*e.mc.binsize,o=t.filter(a=>(a.centreDistance??Number.POSITIVE_INFINITY)>=s),r=t.length-o.length;if(r===0)return o;const i=`nearer the diagonal than ${n.toLocaleString()} bins (${Ln(s)})`;return e.note(o.length===0?`Every loop of this set is ${i} so there is nothing to pile up. Set the nearest loop lower, or to 0 for every one.`:`${r.toLocaleString()} of ${t.length.toLocaleString()} loops are ${i} and are left out. Set the nearest loop to 0 to pile up every one.`),o}function e1(e,t,n){const s=e.start[t],o=e.end[t];if(n==="centre")return n1(s,o);const r=Math.max(s,o-1),i=e.strand[t]<0;return n==="start"?i?r:s:i?s:r}function n1(e,t){return Math.floor((e+t)/2)}function E6(e,t){const n=[],s=Oe(t.contigs),o=Rs(e.params);for(let r=0;r<t.length;r++){const i=zs(e,t.contigs[t.contig[r]],t.start[r],t.end[r],s,{point:e1(t,r,o)});i!==null&&n.push({pair:{row:i.bin,col:i.bin,separation:0},bounds:i.bounds,label:t.name[r]===""?ei(t.contigs[t.contig[r]],t.start[r]):t.name[r],rank:t.score[r],span:i.body.to-i.body.from,rows:i.body,cols:i.body,extent:{start:t.start[r],end:t.end[r]}})}return n}function zs(e,t,n,s,o,r={}){const i=Le(e.mc,t,o);if(i<0)return null;const[a,c]=e.mc.contigExtent(i),l=e.mc.coords.binIndex(i,r.point??n1(n,s));if(l<a||l>=c)return null;const u=Xa(e.mc.coords.binIndex(i,Math.max(0,Math.min(n,s))),a,c-1),h=Xa(e.mc.coords.binIndex(i,Math.max(0,Math.max(n,s)-1)),u,c-1);return{bin:l,body:{from:u,to:h+1},bounds:{from:a,to:c},contig:e.mc.chromnames[i]}}function Xa(e,t,n){return Math.min(n,Math.max(t,e))}function B6(e,t,n){const s=_s(e.params,"rankBy"),o=lt(e.params,"subset","all");if(s===null||o==="all"||n===null)return[t];const i=[...s==="score"?t:T6(t,n,s)].sort((u,h)=>Number.isNaN(u.rank)?Number.isNaN(h.rank)?0:1:Number.isNaN(h.rank)?-1:h.rank-u.rank);if(o==="top")return[i.slice(0,Math.max(1,Math.round(H(e.params,"count",500))))];const a=Math.max(2,Math.round(H(e.params,"groups",4))),c=Math.ceil(i.length/a),l=[];for(let u=0;u<a;u++){const h=i.slice(u*c,(u+1)*c);h.length>0&&l.push(h)}return l}function T6(e,t,n){return e.map((s,o)=>{for(const r of(t.detail[o]??"").split(G5)){const i=r.search(/[=:]/);if(i<0||r.slice(0,i).trim()!==n)continue;const a=Number.parseFloat(r.slice(i+1).trim());return{...s,rank:Number.isFinite(a)?a:Number.NaN}}return{...s,rank:Number.NaN}})}function Ja(e,t,n,s){const{from:o,to:r}=t.bounds;if(Mt(e.params)){if(t.span<=0)return null;if(R0(e.params)&&t.extent!==void 0){const u=_4(t.extent.start,t.extent.end,Ve(e.params),e.mc.binsize),h={rowFrom:o+u.from,rowTo:o+u.to,colFrom:o+u.from,colTo:o+u.to};return h.rowFrom<o||h.rowTo>r||h.rowTo<=h.rowFrom?null:{rect:h,zoomed:!0}}const c=s.flank===0?0:t.span*Ve(e.params),l={rowFrom:Math.floor(t.rows.from-c),rowTo:Math.ceil(t.rows.to+c),colFrom:Math.floor(t.cols.from-c),colTo:Math.ceil(t.cols.to+c)};return l.rowFrom<o||l.colTo>r||l.rowTo<=l.rowFrom||l.colTo<=l.colFrom?null:oo(e.params)?{rect:l,body:{from:t.rows.from,to:t.rows.to,pad:c}}:{rect:l}}const i=(n-1)/2,a={rowFrom:t.pair.row-i,rowTo:t.pair.row+i+1,colFrom:t.pair.col-i,colTo:t.pair.col+i+1};return a.rowFrom<o||a.colTo>r?null:{rect:a}}function O6(e,t){const n=t.row-e.pair.row;return{...e,pair:t,rows:{from:e.rows.from+n,to:e.rows.to+n},cols:{from:e.cols.from+n,to:e.cols.to+n}}}function L6(e){return Math.max(e.rowTo-e.rowFrom,e.colTo-e.colFrom)}function _6(e,t){if(e.flank===0)return["0","0.25","0.5","0.75","1"];const n=e.flank/e.side,s=(e.flank+e.body)/e.side,o=r=>r<=n?-t*(1-r/n):r>=s?1+t*(r-s)/(1-s):(r-n)/(s-n);return[0,.25,.5,.75,1].map(r=>String(Number(o(r).toFixed(2))))}function D6(e){return[0,.25,.5,.75,1].map(t=>String(Number((t*(1+2*e)-e).toFixed(2))))}function R6(e){return{from:Math.min(e.rect.rowFrom,e.rect.colFrom),to:Math.max(e.rect.rowTo,e.rect.colTo)}}function z6(e){const t=[...e].sort((s,o)=>s.rect.rowFrom-o.rect.rowFrom||s.rect.colFrom-o.rect.colFrom),n=[];for(const s of t){const o=n[n.length-1];if(o!==void 0){const r={rowFrom:Math.min(o.rect.rowFrom,s.rect.rowFrom),rowTo:Math.max(o.rect.rowTo,s.rect.rowTo),colFrom:Math.min(o.rect.colFrom,s.rect.colFrom),colTo:Math.max(o.rect.colTo,s.rect.colTo)};if(r.rowTo-r.rowFrom<=Ya&&r.colTo-r.colFrom<=Ya){o.rect=r,o.plans.push(s);continue}}n.push({rect:{...s.rect},plans:[s]})}return n}async function P6(e,t,n){if(n.block!==null){const o=n.block,r=t.rowTo-t.rowFrom,i=t.colTo-t.colFrom,a=new Float32Array(r*i);for(let c=0;c<r;c++){const l=(t.rowFrom-o.rowExtent[0]+c)*o.ncols+(t.colFrom-o.colExtent[0]);a.set(o.values.subarray(l,l+i),c*i)}return{values:a,nrows:r,ncols:i}}const s=await e.mc.matrix({rows:[t.rowFrom,t.rowTo],cols:[t.colFrom,t.colTo],balance:n.balance,value:n.value});return{values:s.values,nrows:s.nrows,ncols:s.ncols}}function q6(e,t,n,s,o,r){const i=n.rect,a=i.rowTo-i.rowFrom,c=i.colTo-i.colFrom;if(a<=0||c<=0)return null;const l=i.rowFrom-t.rowFrom,u=i.colFrom-t.colFrom;if(l<0||u<0||l+a>e.nrows||u+c>e.ncols)return null;const h=new Float64Array(a*c),{minDiag:f,mirrored:d,expected:m}=r;for(let p=0;p<a;p++){const w=(l+p)*e.ncols+u;for(let b=0;b<c;b++){const y=i.colFrom+b-(i.rowFrom+p);if((d?Math.abs(y):y)<f){h[p*c+b]=Number.NaN;continue}const A=e.values[w+b];if(m===void 0){h[p*c+b]=A;continue}const N=m[Math.abs(y)];h[p*c+b]=N>0?A/N:Number.NaN}}if(n.zoomed===!0)return L4(h,a,s);if(n.body===void 0)return O0(h,a,c,s,s);const g=_0(o,n.body.from-i.rowFrom,n.body.to-i.rowFrom,n.body.pad);return L0(h,a,c,g,g)}async function U6(e,t,n){const s=new Map;for(const i of t){const a=Math.max(Math.abs(i.rect.colTo-1-i.rect.rowFrom),Math.abs(i.rect.rowTo-1-i.rect.colFrom)),c=s.get(i.bounds.from);c===void 0?s.set(i.bounds.from,{bounds:i.bounds,hi:a}):a>c.hi&&(c.hi=a)}const o=new Map;let r=0;for(const[i,{bounds:a,hi:c}]of s)e.progress(r,s.size,`the expected, sequence ${r+1} of ${s.size}`),o.set(i,await b6(e,a,{hi:c,block:n.block,balance:n.balance,value:n.value,onTile:async()=>{await e.breathe(),e.throwIfCancelled()}})),r++;return o}function s1(e,t){const n=Oe(t.contigs),s=[],o=Rs(e.params);for(let r=0;r<t.length;r++){const i=e1(t,r,o),a=zs(e,t.contigs[t.contig[r]],t.start[r],t.end[r],n,{point:i});a!==null&&s.push({contig:a.contig,centre:i,exact:o==="centre"?(t.start[r]+t.end[r])/2:i,bin:a.bin,bounds:a.bounds,body:a.body,label:t.name[r]===""?ei(t.contigs[t.contig[r]],t.start[r]):t.name[r]})}return s.sort((r,i)=>r.contig===i.contig?r.centre-i.centre:r.contig<i.contig?-1:1),s}function G6(e,t){const n=s1(e,t),s=[];for(let o=0;o+1<n.length;o++){const r=n[o],i=n[o+1];if(i.contig!==r.contig)continue;const a=Math.min(r.bin,i.bin),c=Math.max(r.bin,i.bin);s.push({pair:{row:a,col:c,separation:c-a},bounds:r.bounds,label:`${r.label} × ${i.label}`,rank:Number.NaN,span:c-a,centreDistance:Math.abs(i.exact-r.exact),rows:{from:a,to:a+1},cols:{from:c,to:c+1}})}return s}function H6(e,t){const n=Math.max(0,Math.round(H(e.params,"minSep",2))),s=Math.max(n+1,Math.round(H(e.params,"maxSep",200)));e.note(`Pairs ${n.toLocaleString()} to ${s.toLocaleString()} bins apart, which is ${Ln(n*e.mc.binsize)} to ${Ln(s*e.mc.binsize)} at this file’s ${e.mc.binsize.toLocaleString()} bp resolution.`);const o=ni(Math.round(H(e.params,"seed",1))),r=s1(e,t),i=[];let a=0;const c=l=>{if(a++,i.length<Ka){i.push(l);return}const u=Math.floor(o()*a);u<Ka&&(i[u]=l)};for(let l=0;l<r.length;l++){const u=r[l];for(let h=l+1;h<r.length;h++){const f=r[h];if(f.contig!==u.contig)break;const d=Math.min(u.bin,f.bin),m=Math.max(u.bin,f.bin),g=m-d;if(g>s)break;g<n||d!==m&&c({pair:{row:d,col:m,separation:m-d},bounds:u.bounds,label:`${u.label} × ${f.label}`,rank:Number.NaN,span:m-d,rows:{from:d,to:d+1},cols:{from:m,to:m+1}})}}return a>i.length&&e.note(`${a.toLocaleString()} pairs are inside the separation range and ${i.length.toLocaleString()} were sampled, uniformly and with this run’s seed. Narrow the range for all of them.`),i}function Q6(e,t){let n=Number.POSITIVE_INFINITY,s=0;for(const a of e){const c=a.pair.separation;c<=0||(c<n&&(n=c),c>s&&(s=c))}if(!Number.isFinite(n)||s<=n)return[e];const o=Math.log(n),r=(Math.log(s)-o)/t,i=Array.from({length:t},()=>[]);for(const a of e){const c=a.pair.separation,l=c<=0?0:Math.min(t-1,Math.floor((Math.log(c)-o)/r));i[l].push(a)}return i.filter(a=>a.length>0)}function K6(e,t){return e.map(n=>{let s=Number.POSITIVE_INFINITY,o=0;for(const r of n){const i=r.pair.separation;i<s&&(s=i),i>o&&(o=i)}return`${Ln(t(Math.max(0,s)))}–${Ln(t(o))}`})}function Ln(e){return Number.isFinite(e)?e>=1e6?`${(e/1e6).toFixed(e>=1e7?0:1)} Mb`:e>=1e3?`${(e/1e3).toFixed(e>=1e4?0:1)} kb`:`${Math.round(e)} bp`:"?"}const St={matrixSize:2e3,varThreshold:.2,highSignThreshold:.5,maxLowSignThreshold:.4,minLowSignThreshold:0,decrementLowSignThreshold:.1,minBlockSize:60},xe=7;function V6(e){const t=new Float64Array(e);for(let n=0;n<e;n++)t[n]=n*e-n*(n-1)/2-n;return t}function Y6(e,t,n){const s=V6(t),o=(f,d)=>s[f]+d,{score:r,upVar:i,loVar:a,upSignMean:c,loSignMean:l}=X6(e,t,s),u=new Int32Array(r.length),h=[];for(const{varThreshold:f,signThreshold:d}of n){const m=g=>-c[g]<d||l[g]<d||!Number.isNaN(f)&&i[g]+a[g]>f?!1:r[g]>0;h.push(J6(t,s,m,u).map(g=>{let p=Number.NEGATIVE_INFINITY;for(let N=0;N<g.length;N+=2){const M=r[o(g[N],g[N+1])];M>p&&(p=M)}const w=[];for(let N=0;N<g.length;N+=2)r[o(g[N],g[N+1])]===p&&w.push([g[N],g[N+1]]);const[b,y]=w.length===1?w[0]:f8(w,g.length/2),A=o(b,y);return{i:b,j:y,score:r[A],uVarScore:i[A],lVarScore:a[A],upSign:-c[A],loSign:l[A]}}))}return h}function W6(e,t,n){const s=t*(t+1)/2,o=(d,m)=>n[d]+m,r=new Float64Array(s);for(let d=0;d<t;d++){let m=Math.min(t-(d+xe),d-xe);if(m=Math.min(m,t),m<xe)continue;const g=d*t;for(let p=0;p<=m-xe;p++){const w=e[g+d-xe-p],b=e[g+d+xe+p],y=(w-b)/(w+b);r[o(d,d+xe+p)]=Number.isNaN(y)?0:y}}const i=d=>d>0?1:d<0?-1:0,a=new Float64Array(s),c=new Int32Array(s),l=new Float64Array(s);for(let d=0;d<t;d++){const m=r[o(d,d)];a[o(d,d)]=m,c[o(d,d)]=i(m),l[o(d,d)]=m*m;for(let g=d-1;g>=0;g--){const p=r[o(g,d)],w=o(g+1,d);a[o(g,d)]=p+a[w],c[o(g,d)]=i(p)+c[w],l[o(g,d)]=p*p+l[w]}}const u=new Float64Array(s),h=new Int32Array(s),f=new Float64Array(s);for(let d=0;d<t;d++){const m=r[o(d,d)];u[o(d,d)]=m,h[o(d,d)]=i(m),f[o(d,d)]=m*m;for(let g=d+1;g<t;g++){const p=r[o(d,g)],w=o(d,g-1);u[o(d,g)]=p+u[w],h[o(d,g)]=i(p)+h[w],f[o(d,g)]=p*p+f[w]}}return{rSum:a,rSign:c,rSq:l,uSum:u,uSign:h,uSq:f}}function X6(e,t,n){const{rSum:s,rSign:o,rSq:r,uSum:i,uSign:a,uSq:c}=W6(e,t,n),l=t*(t+1)/2,u=(M,I)=>n[M]+I,h=new Float64Array(l),f=new Float64Array(l),d=new Float64Array(l),m=new Float64Array(l),g=new Float64Array(l);let p=0,w=0,b=0;for(let M=0;M<t;M++){let I=0,x=0,v=0,S=0,k=0,C=0,B=0,F=0;for(let $=M+1;$<t;$++){const _=Math.floor(($-M+1)/2),E=u(M,$),O=u(M+_,$);I=I+s[E]-s[O],x=x+o[E]-o[O],v=v+r[E]-r[O],S=S+($-M+1)-($-(M+_)+1);const L=Math.min(2*$-M,t-1),D=u($,L),q=u(M+_,$);F=F+(L-$+1)-($-(M+_)+1),k=k+i[D]-s[q],C=C+a[D]-o[q],B=B+c[D]-r[q];const X=S===0?1:S,R=F===0?1:F,P=I/X,z=x/X,U=k/R,K=C/R,G=v/X-P*P,Y=B/R-U*U,V=u(M,$),J=U-P,Z=K-z,ut=G+Y;h[V]=J,f[V]=G,d[V]=Y,m[V]=z,g[V]=K,p<J&&(p=J),w<Z&&(w=Z),b<ut&&(b=ut)}}const y=1/p,A=1/w,N=1/b;for(let M=0;M<t;M++)for(let I=M+1;I<t;I++){const x=u(M,I),v=g[x]-m[x],S=f[x]+d[x];h[x]=h[x]*y+v*A-S*N}return{score:h,upVar:f,loVar:d,upSignMean:m,loSignMean:g}}function J6(e,t,n,s){s.fill(0);const o=[],r=[];let i=1;for(let a=0;a<e;a++)for(let c=a+1;c<e;c++){const l=t[a]+c;if(s[l]!==0||!n(l))continue;const u=i++;s[l]=u;const h=[];for(r.push(a,c);r.length>0;){const f=r.pop(),d=r.pop();h.push(d,f);for(let m=-1;m<=1;m++){const g=d+m;if(!(g<0||g>=e))for(let p=-1;p<=1;p++){const w=f+p;if(w<=g||w>=e)continue;const b=t[g]+w;s[b]!==0||!n(b)||(s[b]=u,r.push(g,w))}}}o.push(h)}return o}function o1(e,t){const n=Math.trunc(t/2),s=[];if(n<=0)return s;for(let o=0;o<e;o+=n){let r=o;const i=Math.min(o+t,e);i===e&&e>n&&(r=i-t),s.push({limStart:o,limEnd:i,n:i-r+1})}return s}function Z6(e){const t=[];for(let n=e.maxLowSignThreshold;n>=e.minLowSignThreshold&&(t.push(n),e.decrementLowSignThreshold>0);n-=e.decrementLowSignThreshold);return t}async function j6(e,t,n,s=St,o){const r=Z6(s),i=[...r.map(g=>({varThreshold:Number.NaN,signThreshold:g})),{varThreshold:s.varThreshold,signThreshold:s.highSignThreshold}],a=o1(e,s.matrixSize),c=i.map(()=>[]);for(let g=0;g<a.length;g++){await o?.(g,a.length);const{limStart:p,n:w}=a[g],b=Math.min(p+w,e),y=await n(p,b),A=b-p,N=new Float32Array(w*w);for(let I=0;I<A;I++)N.set(y.subarray(I*A,(I+1)*A),I*w);const M=Y6(N,w,i);for(let I=0;I<i.length;I++)for(const x of M[I])c[I].push({...x,i:x.i+p,j:x.j+p})}await o?.(a.length,a.length);let l=[];for(let g=0;g<r.length&&(l=c[g],!(l.length>0));g++);const u=[...c[i.length-1]],f=t8(l,u).filter(g=>Math.abs(g.j-g.i)>s.minBlockSize);e8(u,f);const d=u.map(g=>({...g,i:g.i*t,j:g.j*t}));if(d.length===0)return[];let m=Za(d,5*t);return m=Za(m,10*t),m.sort((g,p)=>r1(p.uVarScore+p.lVarScore,g.uVarScore+g.lVarScore)),m}function t8(e,t){const n=new Set(t.map(Ho));return u8(e,s8,Ho,r8).filter(s=>!n.has(Ho(s)))}function e8(e,t){const n=new Set;for(const s of e)n.add(s.i),n.add(s.j);for(const s of t){let o=!1;for(let r=s.i;r<=s.j;r++)if(n.has(r)){o=!0;break}o||(e.push(s),n.add(s.i),n.add(s.j))}}class n8{minX;maxX;minY;maxY;scores=[];constructor(t){this.minX=t.i,this.maxX=t.i,this.minY=t.j,this.maxY=t.j,this.scores.push(t)}isNear(t,n){return(Math.abs(this.minX-t.i)<n||Math.abs(this.maxX-t.i)<n)&&(Math.abs(this.minY-t.j)<n||Math.abs(this.maxY-t.j)<n)}add(t){t.i<this.minX?this.minX=t.i:t.i>this.maxX&&(this.maxX=t.i),t.j<this.minY?this.minY=t.j:t.j>this.maxY&&(this.maxY=t.j),this.scores.push(t)}toHighScore(){const t=n=>{let s=0;for(const o of this.scores)s+=o[n];return s/this.scores.length};return{i:this.maxX,j:this.maxY,score:t("score"),uVarScore:t("uVarScore"),lVarScore:t("lVarScore"),upSign:t("upSign"),loSign:t("loSign")}}}function Za(e,t){const n=[];for(const s of e){const o=n.find(r=>r.isNear(s,t));o===void 0?n.push(new n8(s)):o.add(s)}return n.map(s=>s.toHighScore())}const Ps=new DataView(new ArrayBuffer(8));function r1(e,t){if(e<t)return-1;if(e>t)return 1;const n=r=>Number.isNaN(r)?0x7ff8000000000000n:(Ps.setFloat64(0,r),Ps.getBigInt64(0)),s=n(e),o=n(t);return s===o?0:s<o?-1:1}function s8(e){const t=e.score+e.uVarScore+e.lVarScore+e.upSign+e.loSign;return Math.imul(Math.imul(7,e.i+e.j|0),o8(Math.floor(t)))}function o8(e){return Number.isNaN(e)?0:e>=2147483647?2147483647:e<=-2147483648?-2147483648:Math.trunc(e)|0}function r8(e,t){return r1(e.uVarScore+e.lVarScore,t.uVarScore+t.lVarScore)}let i8=0;function Ho(e){const t=n=>Number.isNaN(n)?`NaN#${++i8}`:String(n===0?0:n);return[e.i,e.j,e.score,e.uVarScore,e.lVarScore,e.upSign,e.loSign].map(t).join(",")}const a8=8,ja=6,c8=64;class l8{constructor(t,n){this.compare=n,this.threshold=i1(t)}table=[];size=0;threshold;put(t,n,s){this.table.length===0&&this.resize();const o=this.table,i=o.length-1&t,a=o[i];if(a===null)o[i]=this.node(t,n,s,null);else{if(a.hash===t&&a.key===n)return;if(a.tree){if(!this.putTreeVal(a,t,n,s))return}else{let c=a;for(let l=0;;l++){if(c.next===null){c.next=this.node(t,n,s,null),l>=a8-1&&this.treeifyBin(t);break}const u=c.next;if(u.hash===t&&u.key===n)return;c=u}}}++this.size>this.threshold&&this.resize()}items(){const t=[];for(const n of this.table)for(let s=n;s!==null;s=s.next)t.push(s.item);return t}node(t,n,s,o){return{hash:t,key:n,item:s,next:o,prev:null,parent:null,left:null,right:null,red:!1,tree:!1}}resize(){const t=this.table,n=t.length;let s;n>0?(s=n*2,this.threshold=n>=16?this.threshold*2:Math.trunc(s*.75)):this.threshold>0?(s=this.threshold,this.threshold=Math.trunc(Math.fround(s*Math.fround(.75)))):(s=16,this.threshold=12);const o=new Array(s).fill(null);this.table=o;for(let r=0;r<n;r++){const i=t[r];if(i!==null)if(i.next===null)o[i.hash&s-1]=i;else if(i.tree)this.split(i,r,n);else{let a=null,c=null,l=null,u=null;for(let h=i;h!==null;){const f=h.next;(h.hash&n)===0?(c===null?a=h:c.next=h,c=h):(u===null?l=h:u.next=h,u=h),h=f}c!==null&&(c.next=null),u!==null&&(u.next=null),o[r]=a,o[r+n]=l}}}treeifyBin(t){const n=this.table;if(n.length<c8){this.resize();return}const s=n.length-1&t;let o=null;for(let i=n[s];i!==null;i=i.next)i.tree=!0,i.prev=o,o=i;const r=n[s];r!==null&&this.treeify(r)}treeify(t){let n=null;for(let s=t;s!==null;s=s.next){if(s.left=null,s.right=null,n===null){s.parent=null,s.red=!1,n=s;continue}for(let o=n;;){const r=this.direction(s.hash,s.item,o),i=o,a=r<=0?o.left:o.right;if(a===null){s.parent=i,r<=0?i.left=s:i.right=s,n=nc(n,s);break}o=a}}n!==null&&this.moveRootToFront(n)}direction(t,n,s){if(s.hash>t)return-1;if(s.hash<t)return 1;const o=this.compare===null?0:this.compare(n,s.item);return o===0?-1:o}putTreeVal(t,n,s,o){let r=t;for(;r.parent!==null;)r=r.parent;let i=!1;for(let a=r;;){let c;if(a.hash>n)c=-1;else if(a.hash<n)c=1;else{if(a.key===s)return!1;if(c=this.compare===null?0:this.compare(o,a.item),c===0){if(!i&&(i=!0,a.left!==null&&this.find(a.left,n,s,o)!==null||a.right!==null&&this.find(a.right,n,s,o)!==null))return!1;c=-1}}const l=a,u=c<=0?a.left:a.right;if(u===null){const h=l.next,f=this.node(n,s,o,h);return f.tree=!0,c<=0?l.left=f:l.right=f,l.next=f,f.parent=l,f.prev=l,h!==null&&(h.prev=f),this.moveRootToFront(nc(r,f)),!0}a=u}}find(t,n,s,o){for(let r=t;r!==null;){const i=r.left,a=r.right;if(r.hash>n)r=i;else if(r.hash<n)r=a;else{if(r.key===s)return r;if(i===null)r=a;else if(a===null)r=i;else{const c=this.compare===null?0:this.compare(o,r.item);if(c!==0)r=c<0?i:a;else{const l=this.find(a,n,s,o);if(l!==null)return l;r=i}}}}return null}moveRootToFront(t){const n=this.table,s=n.length-1&t.hash,o=n[s];if(t===o)return;n[s]=t;const r=t.prev,i=t.next;i!==null&&(i.prev=r),r!==null&&(r.next=i),o!==null&&(o.prev=t),t.next=o,t.prev=null}split(t,n,s){const o=this.table;let r=null,i=null,a=null,c=null,l=0,u=0;for(let f=t;f!==null;){const d=f.next;f.next=null,(f.hash&s)===0?(f.prev=i,i===null?r=f:i.next=f,i=f,l++):(f.prev=c,c===null?a=f:c.next=f,c=f,u++),f=d}const h=f=>{for(let d=f;d!==null;d=d.next)d.tree=!1,d.parent=d.left=d.right=d.prev=null;return f};r!==null&&(l<=ja?o[n]=h(r):(o[n]=r,a!==null&&this.treeify(r))),a!==null&&(u<=ja?o[n+s]=h(a):(o[n+s]=a,r!==null&&this.treeify(a)))}}function tc(e,t){const n=t.right;if(n===null)return e;const s=t.right=n.left;s!==null&&(s.parent=t);const o=n.parent=t.parent;return o===null?(e=n,n.red=!1):o.left===t?o.left=n:o.right=n,n.left=t,t.parent=n,e}function ec(e,t){const n=t.left;if(n===null)return e;const s=t.left=n.right;s!==null&&(s.parent=t);const o=n.parent=t.parent;return o===null?(e=n,n.red=!1):o.right===t?o.right=n:o.left=n,n.right=t,t.parent=n,e}function nc(e,t){for(t.red=!0;;){let n=t.parent;if(n===null)return t.red=!1,t;let s=n.parent;if(!n.red||s===null)return e;const o=s.left;if(n===o){const r=s.right;r!==null&&r.red?(r.red=!1,n.red=!1,s.red=!0,t=s):(t===n.right&&(t=n,e=tc(e,t),n=t.parent,s=n===null?null:n.parent),n!==null&&(n.red=!1,s!==null&&(s.red=!0,e=ec(e,s))))}else o!==null&&o.red?(o.red=!1,n.red=!1,s.red=!0,t=s):(t===n.left&&(t=n,e=ec(e,t),n=t.parent,s=n===null?null:n.parent),n!==null&&(n.red=!1,s!==null&&(s.red=!0,e=tc(e,s))))}}function u8(e,t,n,s=null){const o=new l8(Math.ceil(Math.max(e.length,12)/.75),s);for(const r of e){const i=t(r);o.put(i^i>>>16|0,n(r),r)}return o.items()}function h8(e,t){const n=i=>(Ps.setFloat64(0,i),Ps.getBigUint64(0)),s=BigInt.asUintN(64,n(e)^BigInt.asUintN(64,n(t)*31n)),o=Number(BigInt.asIntN(32,s)),r=Number(BigInt.asIntN(32,s>>32n));return o^r|0}function f8(e,t){const n=i1(Math.ceil(Math.max(t,12)/.75));let s=e[0],o=Number.POSITIVE_INFINITY;for(const r of e){const i=h8(r[0],r[1]),a=(i^i>>>16)&n-1;(a<o||a===o&&(r[0]<s[0]||r[0]===s[0]&&r[1]<s[1]))&&(s=r,o=a)}return s}function i1(e){let t=1;for(;t<e;)t*=2;return Math.min(t,2**30)}const a1=4e3;St.matrixSize,St.minBlockSize,St.varThreshold,St.highSignThreshold,St.maxLowSignThreshold,St.minLowSignThreshold,St.decrementLowSignThreshold;function d8(e){const t=typeof e.matrixSize=="number"?e.matrixSize:St.matrixSize;return Math.max(30,Math.min(a1,Math.round(t)))}const m8=async e=>{const t=e.mc,n=nt(e.params,"balance",!1),s=e.framing.value===""?tt:e.framing.value,o={matrixSize:d8(e.params),varThreshold:H(e.params,"varThreshold",St.varThreshold),highSignThreshold:H(e.params,"highSignThreshold",St.highSignThreshold),maxLowSignThreshold:H(e.params,"maxLowSignThreshold",St.maxLowSignThreshold),minLowSignThreshold:H(e.params,"minLowSignThreshold",St.minLowSignThreshold),decrementLowSignThreshold:H(e.params,"decrementLowSignThreshold",St.decrementLowSignThreshold),minBlockSize:Math.max(0,Math.round(H(e.params,"minBlockSize",St.minBlockSize)))},r=Ht(e.params,"sequences");if(r!==null&&r.length===0)return e.note("No sequence is ticked, so there is nothing to call domains on."),[];const{ids:i,missing:a}=be(t,r),c=t.chromsizes,l=[];for(const b of i){const y=Ut(t,b);if(y===null)continue;const A=t.chromnames[b];l.push({name:A,span:y,length:c.get(A)??(y.to-y.from)*t.binsize})}const u=he(t,a,l.length);if(u!==null&&e.note(u),l.length===0)return[];(t.binsize<5e3||t.binsize>25e3)&&e.note(`Arrowhead was tuned on maps at 5 and 10 kb, and Juicer picks one of the two when it is not told; this map is at ${rt(t.binsize)}, which is what it was run at. Its window of ${o.matrixSize.toLocaleString()} bins is ${rt(o.matrixSize*t.binsize)} here.`),n||e.note("Read without balancing weights, which is Juicer’s -k NONE. Juicer itself reads a normalised map unless told otherwise.");const h=Ct(e,l.map(b=>b.span),!1),f=Qt(t,n),d=l.reduce((b,y)=>b+o1(y.span.to-y.span.from,o.matrixSize).length,0);let m=0;const g=[];for(const b of l){await e.breathe();const y=b.span.to-b.span.from,A=m,N=await j6(y,t.binsize,async(M,I)=>{const x=b.span.from+M,v=await io(e,{from:x,to:b.span.from+I},{block:h,balance:!1,value:s});return f===null?v.values:g8(v.values,I-M,f,x)},o,async(M,I)=>{await e.breathe(),m=A+M,e.progress(m,d,`${b.name}, window ${Math.min(M+1,I)} of ${I}`)});for(const M of N)g.push({...M,sequence:b.name,start:M.i,end:Math.min(b.length,M.j)})}if(e.progress(d,d),g.length===0)return e.note("No domain was called. Arrowhead needs a deep map: Juicer warns that one with fewer than about 300 million contacts may give few domains."),[];const p=g.filter(b=>g.some(y=>y!==b&&y.sequence===b.sequence&&y.start<=b.start&&y.end>=b.end&&(y.start<b.start||y.end>b.end))).length,w=g.length;return e.note(`${w.toLocaleString()} ${w===1?"domain":"domains"} on ${new Set(g.map(b=>b.sequence)).size.toLocaleString()} of ${l.length.toLocaleString()} ${l.length===1?"sequence":"sequences"}`+(p===0?", none inside another.":`, ${p.toLocaleString()} of them inside another.`)),[p8(g),w8(g,t.binsize)]};function g8(e,t,n,s){const o=new Float32Array(t*t);for(let r=0;r<t;r++){const i=n[s+r];for(let a=0;a<t;a++){const c=e[r*t+a];o[r*t+a]=c===0?0:c*i*n[s+a]}}return o}function c1(e){const t=new Map;for(const n of e)t.has(n.sequence)||t.set(n.sequence,t.size);return[...e].sort((n,s)=>(t.get(n.sequence)??0)-(t.get(s.sequence)??0)||n.start-s.start||n.end-s.end)}function p8(e){const t=new se;let n=0;for(const s of c1(e))s.end<=s.start||t.add(s.sequence,s.start,s.end,{score:s.score,name:`domain_${++n}`,detail:`corner score ${s.score.toFixed(3)} · variance ${s.uVarScore.toFixed(3)} up, ${s.lVarScore.toFixed(3)} down · sign ${s.upSign.toFixed(3)} up, ${s.loSign.toFixed(3)} down`});return{kind:"intervals",id:"arrowhead-domains",label:"Arrowhead domains",payload:{kind:"features",data:t.finish()},noun:{one:"domain",many:"domains"},display:"overlay",look:"corner"}}function w8(e,t){return{kind:"table",id:"arrowhead-table",label:"Arrowhead domains, as Juicer writes them",columns:[{key:"sequence",label:"Sequence",kind:"text"},{key:"start",label:"Start",kind:"number",digits:0,coordinate:"start"},{key:"end",label:"End",kind:"number",digits:0,coordinate:"end"},{key:"width",label:"Width (bins)",kind:"number",digits:0},{key:"score",label:"Corner score",kind:"number",digits:6,hint:"The arrowhead difference and sign difference of the two triangles, less their variance."},{key:"uVarScore",label:"uVarScore",kind:"number",digits:6},{key:"lVarScore",label:"lVarScore",kind:"number",digits:6},{key:"upSign",label:"upSign",kind:"number",digits:6},{key:"loSign",label:"loSign",kind:"number",digits:6}],rows:c1(e).map(n=>({sequence:n.sequence,start:n.start,end:n.end,width:Math.round((n.j-n.i)/t),score:n.score,uVarScore:n.uVarScore,lVarScore:n.lVarScore,upSign:n.upSign,loSign:n.loSign}))}}class l1{constructor(t,n,s=null){this.nBins=t,this.contigOf=n,this.covered=s,this.cis=new Float64Array(t),this.trans=new Float64Array(t)}cis;trans;add(t,n,s){if(!Number.isFinite(s)||t<0||t>=this.nBins||n<0||n>=this.nBins)return;const o=this.contigOf[t]===this.contigOf[n]?this.cis:this.trans;o[t]+=s,t!==n&&(o[n]+=s)}addPixels(t,n,s,o,r){const{cis:i,trans:a,contigOf:c,nBins:l}=this;for(let u=0;u<o;u++){const h=t[u],f=n[u],d=r===null?s[u]:s[u]*r[h]*r[f];if(!Number.isFinite(d)||h<0||h>=l||f<0||f>=l)continue;const m=c[h]===c[f]?i:a;m[h]+=d,h!==f&&(m[f]+=d)}}addBlock(t){const{nrows:n,ncols:s,values:o}=t;for(let r=0;r<n;r++){const i=t.rowExtent[0]+r,a=r*s;for(let c=0;c<s;c++){const l=t.colExtent[0]+c;l<i||this.add(i,l,o[a+c])}}}finish(){const t=new Float64Array(this.nBins),n=new Float64Array(this.nBins),s=new Float64Array(this.nBins),o=new Float64Array(this.nBins);for(let r=0;r<this.nBins;r++){if(this.covered!==null&&!this.covered(r)){t[r]=Number.NaN,n[r]=Number.NaN,s[r]=Number.NaN,o[r]=Number.NaN;continue}const i=this.cis[r],a=this.trans[r];t[r]=i+a,n[r]=i,s[r]=a,o[r]=i+a>0?i/(i+a):Number.NaN}return{total:t,cis:n,trans:s,cisFraction:o}}}function Pn(e,t){const n=new Int32Array(t).fill(-1);for(let s=0;s<e.length;s++){const o=e[s],r=Math.min(t,o.startBin+o.bins);for(let i=o.startBin;i<r;i++)n[i]=s}return n}const b8=23e3,A8=2e6,y8=12e6,N8=async e=>{const t=e.mc;if(t.cells.length===0)return e.note("This file has no cells, so there is nothing to measure per cell."),[];const n=lt(e.params,"cells","all")!=="selected",s=t.activeCells;n&&t.selectCells(null);try{return await M8(e,s)}finally{n&&t.selectCells(s)}};async function M8(e,t){const n=e.mc,s=n.activeCells,o=nt(e.params,"balance",!1),r=Qt(n,o),i=[];let a=0;for(let A=0;A<n.nContigs;A++){const[N,M]=n.contigExtent(A);i.push({startBin:N,bins:Math.max(0,M-N)}),a=Math.max(a,(M-N)*n.binsize)}const c=Pn(i,n.nBins),l=Math.max(0,H(e.params,"nearFrom",b8)),u=Math.max(l+1,H(e.params,"nearTo",A8)),h=Math.max(u+1,H(e.params,"mitoticTo",y8));a<=u&&e.note(`The longest sequence here is ${Math.round(a).toLocaleString()} bp, which is inside the near band — so the mitotic and far bands are empty by construction and the phasing plot says nothing. Set the band edges to this assembly’s own scale.`);const f=s.map(()=>({contacts:0,nnz:0,cis:0,trans:0,short:0,near:0,mitotic:0,far:0}));e.progress(0,s.length,"reading cells"),await n.scanCells({onCell:(A,N)=>e.progress(A,s.length,N),onChunk:(A,N)=>{e.throwIfCancelled();const M=f[A];for(let I=0;I<N.length;I++){const x=N.bin1[I],v=N.bin2[I],S=N.count[I],k=r===null?S:S*r[x]*r[v];if(!Number.isFinite(k))continue;M.nnz++,M.contacts+=k;const C=c[x],B=c[v];if(C<0||B<0)continue;if(C!==B){M.trans+=k;continue}M.cis+=k;const F=Math.abs(v-x)*n.binsize;F<l?M.short+=k:F<u?M.near+=k:F<h?M.mitotic+=k:M.far+=k}return!0},onProgress:()=>e.throwIfCancelled()}),e.progress(s.length,s.length,"done"),e.throwIfCancelled();const d=new Set(t),m=(A,N)=>N>0?A/N:Number.NaN,g=s.map((A,N)=>{const M=f[N];return{cell:A,ticked:d.has(A)?"yes":"",contacts:M.contacts,nnz:M.nnz,cis:m(M.cis,M.contacts),trans:m(M.trans,M.contacts),short:m(M.short,M.contacts),near:m(M.near,M.contacts),mitotic:m(M.mitotic,M.contacts),far:m(M.far,M.contacts)}}),p=g.filter(A=>A.contacts===0).length;p>0&&e.note(`${p.toLocaleString()} cell${p===1?" has":"s have"} no contact at all, so ${p===1?"its":"their"} fractions are blank rather than zero.`);const w=[{kind:"table",id:"cellqc",label:`${s.length.toLocaleString()} cells`,columns:[{key:"cell",label:"Cell",kind:"text"},{key:"ticked",label:"Ticked",kind:"text",hint:"Whether this cell is in the set the map is drawn from."},{key:"contacts",label:"Contacts",kind:"number",digits:0},{key:"nnz",label:"Stored pixels",kind:"number",digits:0},{key:"cis",label:"Cis",kind:"number",digits:3},{key:"trans",label:"Trans",kind:"number",digits:3},{key:"short",label:`< ${Wt(l)}`,kind:"number",digits:3,hint:"Self-ligation and unligated ends, mostly."},{key:"near",label:`${Wt(l)}–${Wt(u)}`,kind:"number",digits:3,hint:"The near band. High in G1, low just after mitosis."},{key:"mitotic",label:`${Wt(u)}–${Wt(h)}`,kind:"number",digits:3,hint:"The mitotic band. The condensed chromosome’s signature."},{key:"far",label:`> ${Wt(h)}`,kind:"number",digits:3}],rows:g}],b=Float64Array.from(g,A=>A.near),y=Float64Array.from(g,A=>A.mitotic);return w.push({kind:"curve",id:"cellqc-phase",label:"Cell-cycle phasing",series:[{name:`${s.length.toLocaleString()} cells`,x:b,y,n:s.length,mark:"points"}],xLabel:`Contacts ${Wt(l)}–${Wt(u)} (near)`,yLabel:`Contacts ${Wt(u)}–${Wt(h)} (mitotic)`,xScale:"linear",yScale:"linear"}),w}function Wt(e){return e>=1e6?`${Number((e/1e6).toFixed(1))} Mb`:e>=1e3?`${Number((e/1e3).toFixed(1))} kb`:`${Math.round(e)} bp`}const I8=96e6;function x8(e,t,n){return(Math.min(t,e)+n+1)*e}function v8(e){const t=typeof e.pattern=="string"?e.pattern:"";return nn.find(n=>n.name===t)??nn[0]}function Qo(e,t,n){if(e.thresholds!=="custom")return n;const s=e[t];return typeof s=="number"&&Number.isFinite(s)?s:n}function ke(e){return e.thresholds==="custom"}function sc(e,t){return e===0?0:Math.max(1,Math.round(e/(t>0?t:1)))}function Ko(e,t,n,s){if(!ke(e))return sc(n,s);const o=e[t];return typeof o=="number"&&Number.isFinite(o)?Math.max(0,Math.round(o)):sc(n,s)}nn.map(e=>({value:e.name,label:e.name.replace(/_/g," "),hint:e.blurb}));const oc=64,k8=200,S8=async e=>{const t=v8(e.params),n=nt(e.params,"balance",!1),s=e.framing.value===""?tt:e.framing.value,o=e.mc.binsize,r=Ko(e.params,"maxDist",t.maxDist,o),i=Ko(e.params,"minDist",t.minDist,o),a=Ko(e.params,"minSeparation",t.minSeparation,o),c={maxDist:Math.max(r,1),pearson:Qo(e.params,"pearson",t.pearson),missingTol:Qo(e.params,"maxPercUndetected",t.maxPercUndetected)/100,zeroTol:Qo(e.params,"maxPercZero",t.maxPercZero)/100,onDiagonal:r===0},l=r===0;e.note(`A ${c.maxDist.toLocaleString()}-diagonal scan band reaches ${rt(c.maxDist*o)} from the diagonal at this file’s ${o.toLocaleString()} bp resolution.`);const u=Math.max(1,Math.round(H(e.params,"iterations",1))),h=U0(t),f=h.reduce((I,x)=>Math.max(I,x.size),0),{units:d,skipped:m}=C8(e,c.maxDist,f);if(m.length>0&&e.note(`${m.length} sequence${m.length===1?"":"s"} would need a band bigger than a scan can hold, and ${m.length===1?"was":"were"} skipped: ${F8(m)}. The band is the scan distance times the sequence — bring “furthest from the diagonal” down, or open the file at a coarser resolution.`),d.length===0)return e.note(m.length===0?"No sequence in this file is long enough for this template.":"Every sequence in this file would need a band bigger than a scan can hold. Bring “furthest from the diagonal” down, or open the file at a coarser resolution."),[];const g=Qt(e.mc,n);g===null&&e.note(`This file carries no balancing weights, so detectable bins are found from the coverage of the raw counts (${H(e.params,"nMads",5)} MADs below the median). The published method balances the file first and this app does not write to the file it opened, so the two do not start from the same matrix — the scores are comparable in kind, not to the digit.`);const p=Ct(e,d.map(I=>I.span),n),w=[];let b=0;const y=d.length*h.length*u;let A=0,N;if(g!==null)N=(I,x)=>H0(g,I,x);else{const I=H(e.params,"nMads",5),x=await t1(e);N=(v,S)=>Q0(x.subarray(v,S),I)}for(const I of d){await e.breathe();const x=I.span.to-I.span.from,v=ii(x,c.maxDist,f),k={contacts:await De(e,I.span,{hi:v,block:p,balance:n,value:s,onTile:async(B,F)=>{await e.breathe(),e.progress(A,y,F===1?`${I.name} · reading`:`${I.name} · reading ${B+1} of ${F}`)}}),n:x,detectable:N(I.span.from,I.span.to)},C=J0(k,{maxDist:c.maxDist,largestKernel:f});for(let B=0;B<h.length;B++){let F=h[B];for(let $=0;$<u&&(e.progress(A,y,`${I.name}${h.length>1?` · template ${B+1}`:""}${u>1?` · pass ${$+1}`:""}`),!(x<=F.size));$++){const _=e6(x,c.maxDist);for(let O=0;O<x;O+=oc)await e.breathe(),n6(_,C,x,k.detectable,F,c,O,O+oc);const E=l6(k,C,_,F,c);b+=E.rejected;for(const O of E.found)w.push({unit:I,bin1:I.span.from+O.pattern.bin1,bin2:I.span.from+O.pattern.bin2,score:O.pattern.score,pvalue:O.pattern.pvalue,qvalue:Number.NaN,kernel:B,iteration:$,values:O.values});if(A++,$+1<u){const O=ri(E.found.map(L=>L.values),F.size);if(E.found.length===0||O.some(L=>!Number.isFinite(L))){e.note(`Refinement stopped after pass ${$+1} on ${I.name}: the pileup of what it found has gaps in it, and a template with gaps cannot be correlated.`);break}F={size:F.size,values:O}}}}}if(e.progress(y,y,"done"),e.throwIfCancelled(),w.length===0)return e.note(`Nothing reached a score of ${c.pearson} on ${d.length.toLocaleString()} ${d.length===1?"sequence":"sequences"}${b>0?`; ${b.toLocaleString()} candidates were found and then dropped for sitting in missing data`:""}. Lower the score threshold, or check that the pattern suits this map.`),[];const M=$8(e,w,{separationBins:Math.max(1,a),minDistBins:l?0:i,maxQvalue:H(e.params,"maxQvalue",1)});return M.length===0?(e.note("Every call was suppressed by the separation, distance or q-value filters."),[]):(b>0&&e.note(`${b.toLocaleString()} candidate ${b===1?"pixel was":"pixels were"} dropped for sitting in too much missing or empty data.`),E8(e,t,M,{onDiagonal:l,size:h[0].size,sequences:d.length}))};function C8(e,t,n){const s=e.mc,o=s.chromsizes,r=e.params.sequences,i=Array.isArray(r)?new Set(r.filter(l=>typeof l=="string")):null,a=[],c=[];for(const l of ao(s)){const u=s.chromnames[l];if(i!==null&&!i.has(u))continue;const h=Ut(s,l);if(h!==null){if(x8(h.to-h.from,t,n)>I8){c.push(u);continue}a.push({name:u,cid:l,span:h,length:o.get(u)??(h.to-h.from)*s.binsize})}}return{units:a,skipped:c}}function F8(e){const t=e.slice(0,6).join(", ");return e.length>6?`${t} and ${e.length-6} more`:t}function $8(e,t,n){const{kept:s,qvalues:o}=f6(t,{separationBins:n.separationBins,minDistBins:n.minDistBins});for(let a=0;a<s.length;a++)s[a].qvalue=o[a];const r=t.length-s.length;if(r>0&&e.note(`${r.toLocaleString()} of ${t.length.toLocaleString()} calls were dropped as overlapping neighbours or as too close to the diagonal.`),n.maxQvalue>=1)return s;const i=s.filter(a=>a.qvalue<=n.maxQvalue);return e.note(`${i.length.toLocaleString()} of ${s.length.toLocaleString()} calls are at q ≤ ${n.maxQvalue}.`),i}function E8(e,t,n,s){const o=e.mc.binsize,r=t.name.replace(/_/g," "),i=[];i.push(s.onDiagonal?T8(n,r,o):B8(n,r,o));const a=ri(n.map(c=>c.values),s.size);if(i.push({kind:"matrix",id:"chromosight-pileup",label:`Pileup of ${n.length.toLocaleString()} ${r}`,nrows:s.size,ncols:s.size,values:Float32Array.from(s.onDiagonal?X0(a,s.size):a),xLabel:"Distance from the centre",yLabel:"Distance from the centre",xTicks:On(s.size,o),yTicks:On(s.size,o),...Z0,stats:[{label:"patterns",value:n.length,digits:0,hint:"Calls that entered the pileup."},{label:"median score",value:_8(n.map(c=>c.score)),digits:3,hint:"Pearson correlation with the template."}]}),i.push(O8(n,t)),!s.onDiagonal){const c=so(n.map(l=>({separation:Math.abs(l.bin2-l.bin1)*o,score:l.score})),{id:"chromosight-loop-spectrum",label:`Loop spectrum · ${r}`,noun:"calls",yLabel:"Pearson correlation with the template"});c!==null&&i.push(c)}return i.push(L8(n,o)),e.note(`${n.length.toLocaleString()} ${r} across ${s.sequences.toLocaleString()} ${s.sequences===1?"sequence":"sequences"}.`),i}function B8(e,t,n){const s=new Kl;for(const o of e){const r=(o.bin1-o.unit.span.from)*n,i=(o.bin2-o.unit.span.from)*n;s.add(o.unit.name,r,Math.min(o.unit.length,r+n),o.unit.name,i,Math.min(o.unit.length,i+n),{score:o.score,name:"",detail:u1(o)})}return{kind:"intervals",id:"chromosight-patterns",label:`Detected ${t}`,payload:{kind:"pairs",data:s.finish()},noun:{one:"pair",many:"pairs"},display:"overlay"}}function T8(e,t,n){const s=new se;for(const o of e){const r=(o.bin2-o.unit.span.from)*n;s.add(o.unit.name,r,Math.min(o.unit.length,r+n),{score:o.score,name:"",detail:u1(o)})}return{kind:"intervals",id:"chromosight-patterns",label:`Detected ${t}`,payload:{kind:"features",data:s.finish()},noun:{one:"interval",many:"intervals"},display:"overlay",look:"border"}}function u1(e){return`score ${e.score.toFixed(4)} · p ${rc(e.pvalue)} · q ${rc(e.qvalue)}${e.kernel>0?` · template ${e.kernel+1}`:""}`}function O8(e,t){const n=e.map(l=>l.score).filter(l=>Number.isFinite(l)),s=Math.min(...n),o=Math.max(...n),r=Math.max(10,Math.min(50,Math.round(Math.sqrt(n.length)*2))),i=o>s?(o-s)/r:1,a=new Float64Array(r);for(const l of n){const u=i>0?Math.min(r-1,Math.floor((l-s)/i)):0;a[u]++}const c=new Float64Array(r);for(let l=0;l<r;l++)c[l]=s+i*(l+.5);return{kind:"curve",id:"chromosight-spectrum",label:`Score spectrum · ${t.name.replace(/_/g," ")}`,series:[{name:"Calls",x:c,y:a,n:n.length}],xLabel:"Pearson correlation with the template",yLabel:"Calls",xScale:"linear",yScale:"linear"}}function L8(e,t){const n=[...e].sort((s,o)=>o.score-s.score).slice(0,k8).map(s=>({sequence:s.unit.name,start1:(s.bin1-s.unit.span.from)*t,start2:(s.bin2-s.unit.span.from)*t,separation:Math.abs(s.bin2-s.bin1)*t,score:s.score,pvalue:s.pvalue,qvalue:s.qvalue}));return{kind:"table",id:"chromosight-table",label:n.length<e.length?`Strongest ${n.length} of ${e.length}`:"Patterns",columns:[{key:"sequence",label:"Sequence",kind:"text"},{key:"start1",label:"Start 1",kind:"number",coordinate:"start"},{key:"start2",label:"Start 2",kind:"number",coordinate:"start"},{key:"separation",label:"Separation (bp)",kind:"number",digits:0},{key:"score",label:"Score",kind:"number",digits:4,hint:"Pearson r with the template."},{key:"pvalue",label:"p",kind:"number",digits:6},{key:"qvalue",label:"q",kind:"number",digits:6,hint:"Benjamini-Hochberg."}],rows:n}}function _8(e){const t=e.filter(s=>Number.isFinite(s)).sort((s,o)=>s-o);if(t.length===0)return Number.NaN;const n=t.length>>1;return t.length%2===1?t[n]:(t[n-1]+t[n])/2}function rc(e){return Number.isFinite(e)?e===0?"0":e<1e-4?e.toExponential(2):e.toFixed(6):"n/a"}const D8=8e6;class R8{constructor(t){this.nContigs=t,this.cis=new Float64Array(t),this.trans=new Float64Array(t)}cis;trans;transTotal=0;dropped=0;pairsOverflowed=!1;between=new Map;add(t,n,s){if(!Number.isFinite(s))return;if(t<0||n<0){this.dropped+=s;return}if(t===n){this.cis[t]+=s;return}this.trans[t]+=s,this.trans[n]+=s,this.transTotal+=s;const o=t<n?t:n,r=t<n?n:t,i=o*this.nContigs+r,a=this.between.get(i);a!==void 0?this.between.set(i,a+s):this.between.size<D8?this.between.set(i,s):this.pairsOverflowed=!0}pair(t,n){if(t===n)return this.cis[t];const s=t<n?t:n,o=t<n?n:t;return this.between.get(s*this.nContigs+o)??0}*pairs(){for(const[t,n]of this.between){const s=Math.floor(t/this.nContigs);yield[s,t-s*this.nContigs,n]}}}function z8(e,t){if(!(t>0))return()=>Number.NaN;let n=0;for(let r=0;r<e.length;r++)n+=e[r]*e[r];const s=4*t*t-n,o=s>0?4*t*t/s:1;return(r,i)=>o*e[r]*e[i]/(2*t)}const ic=512,P8=async e=>{const t=e.mc,n=nt(e.params,"balance",!1),s=Qt(t,n),o=[];for(let A=0;A<t.nContigs;A++){const[N,M]=t.contigExtent(A);o.push({startBin:N,bins:Math.max(0,M-N)})}const r=Pn(o,t.nBins),i=new R8(t.nContigs),a=Ht(e.params,"sequences");if(a!==null&&a.length===0)return e.note("No sequence is ticked, so there is nothing to report."),[];const c=e.inputs.block;if(c!==void 0&&c.binsPerCell===1&&e.framing.balance===n&&c.rowExtent[0]===0&&c.rowExtent[1]===t.nBins&&c.colExtent[0]===0&&c.colExtent[1]===t.nBins&&c!==void 0){e.progress(0,1,"summing the block on screen");for(let A=0;A<c.nrows;A++){e.throwIfCancelled();const N=A*c.ncols;for(let M=A;M<c.ncols;M++){const I=c.values[N+M];I!==0&&i.add(r[A],r[M],I)}}}else e.framing.value!==""&&e.framing.value!==tt&&e.note(`The map is drawn from the ${e.framing.value} column, but a whole-file pass reads ${tt}. These numbers are of the counts.`),e.progress(0,1,"reading pixels"),await t.scanPixels({onChunk:A=>{e.throwIfCancelled();for(let N=0;N<A.length;N++){const M=A.bin1[N],I=A.bin2[N],x=A.count[N];i.add(r[M],r[I],s===null?x:x*s[M]*s[I])}return!0},onProgress:(A,N)=>e.progress(A,N,"reading pixels")});e.throwIfCancelled(),i.pairsOverflowed&&e.note("This file has more sequence pairs with contact than the pair table holds, so the ranked list below is of the pairs seen first rather than of the strongest. The per-sequence rows are unaffected. Tick fewer sequences."),i.dropped>0&&e.note(`${Math.round(i.dropped).toLocaleString()} contacts have an end outside every sequence and are left out. A bin belonging to no contig means the bin table and the contig table disagree about this file.`);let u;if(a===null)u=o.map((A,N)=>N);else{const A=no(t,a),N=he(t,A.missing,A.ids.length);N!==null&&e.note(N),u=A.ids}if(u.length===0)return[];const h=u.map(A=>{const N=i.cis[A],M=i.trans[A],I=N+M;return{sequence:t.chromnames[A],bins:o[A].bins,cis:N,trans:M,total:I,fraction:I>0?N/I:Number.NaN,ratio:N>0?M/N:Number.NaN,perBin:o[A].bins>0?I/o[A].bins:Number.NaN}}),f=[{kind:"table",id:"cistrans",label:"Cis and trans per sequence",columns:[{key:"sequence",label:"Sequence",kind:"text"},{key:"bins",label:"Bins",kind:"number",digits:0},{key:"cis",label:"Cis",kind:"number",digits:0},{key:"trans",label:"Trans",kind:"number",digits:0},{key:"total",label:"Total",kind:"number",digits:0},{key:"fraction",label:"Cis fraction",kind:"number",digits:3,hint:"Of the contacts touching this sequence, how many stay on it."},{key:"ratio",label:"Trans / cis",kind:"number",digits:3},{key:"perBin",label:"Contacts per bin",kind:"number",digits:2}],rows:h},q8(h,lt(e.params,"bars","cis"))],d=lt(e.params,"normalise","enrichment"),m=Math.max(0,H(e.params,"minContacts",10)),g=Math.max(10,Math.round(H(e.params,"maxEdges",500))),p=z8(i.trans,i.transTotal),w=(A,N,M)=>{if(d==="raw")return M;if(d==="bins"){const x=o[A].bins*o[N].bins;return x>0?M/x:Number.NaN}const I=p(A,N);return I>0?M/I:Number.NaN},b=new Set(u),y=[];for(const[A,N,M]of i.pairs()){if(!b.has(A)||!b.has(N)||M<m)continue;const I=w(A,N,M);Number.isFinite(I)&&y.push({a:t.chromnames[A],b:t.chromnames[N],raw:M,value:I,expected:p(A,N)})}if(y.sort((A,N)=>N.value-A.value),y.length>0&&(f.push({kind:"table",id:"cistrans-pairs",label:d==="enrichment"?"Sequence pairs, against indifference":"Sequence pairs, ranked",columns:[{key:"a",label:"Sequence",kind:"text"},{key:"b",label:"Sequence",kind:"text"},{key:"raw",label:"Contacts",kind:"number",digits:0},...d==="enrichment"?[{key:"expected",label:"Expected",kind:"number",digits:1,hint:"From the two sequences’ own trans coverage, under indifference."}]:[],{key:"value",label:d==="raw"?"Contacts":d==="bins"?"Per bin pair":"Observed / expected",kind:"number",digits:d==="raw"?0:d==="bins"?5:2}],rows:y.slice(0,g)}),y.length>g&&e.note(`${y.length.toLocaleString()} sequence pairs pass the threshold and the strongest ${g.toLocaleString()} are listed. Raise “pairs to list” to see more.`)),nt(e.params,"matrix",!0))if(u.length>ic)e.note(`${u.length.toLocaleString()} sequences is past the ${ic.toLocaleString()} this draws a square for, so the ranked table above is the answer. Tick fewer sequences for the picture.`);else{const A=u.length,N=new Float32Array(A*A);for(let I=0;I<A;I++)for(let x=0;x<A;x++){const v=u[I],S=u[x];N[I*A+x]=v===S?Number.NaN:w(v,S,i.pair(v,S))}const M=A<=40?u.map(I=>t.chromnames[I]):[];f.push({kind:"matrix",id:"cistrans-matrix",label:d==="enrichment"?"Between sequences, against indifference":"Between sequences",nrows:A,ncols:A,values:N,xLabel:"Sequence",yLabel:"Sequence",xTicks:M,yTicks:M,colormap:d==="enrichment"?"RdBu_r":"default",...d==="enrichment"?{centre:1}:{}})}return f},ac="contacts";function q8(e,t){const n=(a,c)=>c>0?a/c:Number.NaN,s=Float64Array.from(e,(a,c)=>c),o=t==="both",r={name:"cis",x:s,y:Float64Array.from(e,a=>n(a.cis,a.total)),mark:"bars",...o?{stack:ac}:{}},i={name:"trans",x:s,y:Float64Array.from(e,a=>n(a.trans,a.total)),mark:"bars",...o?{stack:ac}:{}};return{kind:"curve",id:"cistrans-fractions",label:o?"Cis and trans fraction per sequence":t==="trans"?"Trans fraction per sequence":"Cis fraction per sequence",series:o?[r,i]:[t==="trans"?i:r],xLabel:"Sequence",yLabel:"Share of the sequence’s contacts (%)",xScale:"linear",yScale:"linear",yDomain:[0,1],xTicks:e.map(a=>a.sequence)}}function li(e){const{n:t,origin:n,circular:s}=e,o=new Int32Array(t),r=Math.floor(t/2);for(let i=0;i<t;i++){const a=i-n;o[i]=s?((a+r)%t+t)%t-r:a}return o}function ui(e){let t=0;for(let n=0;n<e.length;n++)t=Math.max(t,Math.abs(e[n]));return t}function h1(e,t,n,s,o){const r=li(s),i=t.length,a=n.length,c=ui(r),l=c+1,u=2*c+1,h=!(o>0),f=h?u:Math.max(1,Math.floor(o/4)),d=Math.ceil(u/f),m=h?d:Math.max(1,Math.ceil(o/f)),g=[new Float64Array(l*d),new Float64Array(l*d)],p=[new Float64Array(l*d),new Float64Array(l*d)],w=(A,N)=>{const M=r[t[A]],I=r[n[N]],x=Math.abs(M),v=Math.abs(I);return{kind:M>=0==I>=0?0:1,delta:Math.abs(x-v),slot:Math.floor((x+v)/f)}};for(let A=0;A<i;A++)for(let N=0;N<a;N++){const M=e[A*a+N];if(!Number.isFinite(M))continue;const{kind:I,delta:x,slot:v}=w(A,N),S=x*d+v;g[I][S]+=M,p[I][S]+=1}const b=[new Float64Array(l*d),new Float64Array(l*d)];for(let A=0;A<2;A++){const N=new Float64Array(d+1),M=new Float64Array(d+1);for(let I=0;I<l;I++){const x=I*d;for(let v=0;v<d;v++)N[v+1]=N[v]+g[A][x+v],M[v+1]=M[v]+p[A][x+v];for(let v=0;v<d;v++){const S=Math.max(0,v-m),k=Math.min(d,v+m+1),C=M[k]-M[S];b[A][x+v]=C>0?(N[k]-N[S])/C:Number.NaN}}}const y=new Float64Array(i*a);for(let A=0;A<i;A++)for(let N=0;N<a;N++){const{kind:M,delta:I,slot:x}=w(A,N);y[A*a+N]=b[M][I*d+x]}return y}function U8(e,t){const{n}=t,s=li(t),o=ui(s),r=()=>({maxOffset:o,sum:new Float64Array(o+1),count:new Float64Array(o+1),mean:new Float64Array(o+1)}),i=r(),a=r();for(let c=0;c<n;c++){const l=s[c],u=Math.abs(l);for(let h=0;h<n;h++){const f=e[c*n+h];if(!Number.isFinite(f))continue;const d=s[h],m=l>=0==d>=0?i:a,g=Math.abs(u-Math.abs(d));m.sum[g]+=f,m.count[g]+=1}}for(const c of[i,a])for(let l=0;l<=o;l++)c.mean[l]=c.count[l]>0?c.sum[l]/c.count[l]:Number.NaN;return{within:i,across:a}}function cc(e,t,n){const{n:s,circular:o}=t,r=li(t),i=Math.max(0,Math.round(n)),a=2*i+2,c=ui(r)+1,l=new Float64Array(c),u=new Float64Array(c),h=new Float64Array(s),f=new Float64Array(s);for(let g=0;g<s;g++){const p=r[g],w=Math.abs(p);for(let b=0;b<s;b++){const y=r[b];if(p>=0==y>=0)continue;const A=Math.abs(y);if(Math.abs(w-A)>i)continue;const N=Math.abs(g-b);if((o?Math.min(N,s-N):N)<a)continue;const M=e[g*s+b];if(!Number.isFinite(M))continue;const I=Math.min(c-1,Math.round((w+A)/2));l[I]+=M,u[I]+=1,h[g]+=M,f[g]+=1}}const d=new Float64Array(c);for(let g=0;g<c;g++)d[g]=u[g]>0?l[g]/u[g]:Number.NaN;const m=new Float64Array(s);for(let g=0;g<s;g++)m[g]=f[g]>0?h[g]/f[g]:Number.NaN;return{byDistance:d,byBin:m}}const G8=.02,H8=.2;function lc(e,t){let n=0;const s=e.length,o=new Float64Array(s);for(let m=0;m<s;m++){const g=e[m];o[m]=g>0?Math.log2(g):Number.NaN,Number.isFinite(o[m])&&n++}if(n===0)return{score:Number.NaN,reach:Number.NaN,peak:Number.NaN};const r=Math.max(1,Math.round(s*G8)),i=new Float64Array(s).fill(Number.NaN);let a=Number.NEGATIVE_INFINITY,c=-1;for(let m=0;m<s;m++){let g=0,p=0;for(let w=Math.max(0,m-r);w<=Math.min(s-1,m+r);w++)Number.isFinite(o[w])&&(g+=o[w],p++);p!==0&&(i[m]=g/p,i[m]>a&&(a=i[m],c=m))}let l=Number.NaN;if(a>0){l=c;for(let m=c+1;m<s;m++)if(Number.isFinite(i[m])){if(i[m]<a*H8)break;l=m}}const u=t===void 0?l:t,h=Number.isFinite(u)?Math.min(s-1,Math.max(0,Math.floor(u))):s-1;let f=0,d=0;for(let m=0;m<=h;m++)Number.isFinite(o[m])&&(f+=o[m],d++);return{score:d>0?f/d:Number.NaN,reach:l,peak:a}}function Q8(e,t,n,s={}){const o=s.iterations??512,r=s.tolerance??1e-12,i=[];if(t===0)return i;for(let a=0;a<n;a++){const c=f1(e,t,o,r,a);if(c===null)break;i.push(c),d1(e,t,c)}return i}function f1(e,t,n,s,o){const r=ni(24301+o),i=new Float64Array(t);for(let p=0;p<t;p++)i[p]=r()*2-1;if(!uc(i))return null;const a=Math.max(1,Math.min(t,n)),c=[i],l=new Float64Array(a),u=new Float64Array(a),h=new Float64Array(t);let f=0,d=null;for(let p=0;p<a;p++){m1(e,t,c[p],h);let w=0;for(let I=0;I<2;I++)for(let x=0;x<=p;x++){const v=c[x];let S=0;for(let k=0;k<t;k++)S+=v[k]*h[k];for(let k=0;k<t;k++)h[k]-=S*v[k];x===p&&(w+=S)}let b=0;for(let I=0;I<t;I++)b+=h[I]*h[I];if(b=Math.sqrt(b),!Number.isFinite(w)||!Number.isFinite(b))return null;l[p]=w,f=Math.max(f,Math.abs(w)+b+(p>0?u[p-1]:0));const y=p+1,N=!(b>f*1e-14)||y===a;if(N||y<=16||y%Math.ceil(y/8)===0){if(d=K8(l,u,y),d===null)return null;if(N||b*Math.abs(d.coefficients[y-1])<=s*Math.abs(d.value))break}u[p]=b;const M=new Float64Array(t);for(let I=0;I<t;I++)M[I]=h[I]/b;c.push(M)}if(d===null)return null;const m=new Float64Array(t),{coefficients:g}=d;for(let p=0;p<g.length;p++){const w=g[p],b=c[p];for(let y=0;y<t;y++)m[y]+=w*b[y]}return uc(m)?{value:d.value,vector:m}:null}function K8(e,t,n){const s=e.slice(0,n),o=new Float64Array(n);for(let u=0;u+1<n;u++)o[u]=t[u];const r=new Float64Array(n*n);for(let u=0;u<n;u++)r[u*n+u]=1;let i=0,a=0;const c=2**-52;for(let u=0;u<n;u++){a=Math.max(a,Math.abs(s[u])+Math.abs(o[u]));let h=u;for(;h<n-1&&!(Math.abs(o[h])<=c*a);)h++;if(h>u){let f=0;do{if(++f>64)return null;let d=s[u],m=(s[u+1]-d)/(2*o[u]),g=Math.hypot(m,1);m<0&&(g=-g),s[u]=o[u]/(m+g),s[u+1]=o[u]*(m+g);const p=s[u+1];let w=d-s[u];for(let x=u+2;x<n;x++)s[x]-=w;i+=w,m=s[h];let b=1,y=b,A=b;const N=o[u+1];let M=0,I=0;for(let x=h-1;x>=u;x--){A=y,y=b,I=M,d=b*o[x],w=b*m,g=Math.hypot(m,o[x]),o[x+1]=M*g,M=o[x]/g,b=m/g,m=b*s[x]-M*d,s[x+1]=w+M*(b*d+M*s[x]);const v=x*n,S=(x+1)*n;for(let k=0;k<n;k++)w=r[S+k],r[S+k]=M*r[v+k]+b*w,r[v+k]=b*r[v+k]-M*w}m=-M*I*A*N*o[u]/p,o[u]=M*m,s[u]=b*m}while(Math.abs(o[u])>c*a)}s[u]+=i,o[u]=0}let l=0;for(let u=1;u<n;u++)Math.abs(s[u])>Math.abs(s[l])&&(l=u);return Number.isFinite(s[l])?{value:s[l],coefficients:r.slice(l*n,l*n+n)}:null}function d1(e,t,n){const{value:s,vector:o}=n;for(let r=0;r<t;r++){const i=s*o[r];for(let a=0;a<t;a++)e[r*t+a]-=i*o[a]}}function m1(e,t,n,s){s.fill(0);let o=0;for(;o+3<t;o+=4){const r=o*t,i=r+t,a=i+t,c=a+t,l=n[o],u=n[o+1],h=n[o+2],f=n[o+3],d=e[r+o+1],m=e[r+o+2],g=e[r+o+3],p=e[i+o+2],w=e[i+o+3],b=e[a+o+3];let y=e[r+o]*l+d*u+m*h+g*f,A=d*l+e[i+o+1]*u+p*h+w*f,N=m*l+p*u+e[a+o+2]*h+b*f,M=g*l+w*u+b*h+e[c+o+3]*f;for(let I=o+4;I<t;I++){const x=e[r+I],v=e[i+I],S=e[a+I],k=e[c+I],C=n[I];y+=x*C,A+=v*C,N+=S*C,M+=k*C,s[I]+=x*l+v*u+S*h+k*f}s[o]+=y,s[o+1]+=A,s[o+2]+=N,s[o+3]+=M}for(;o<t;o++){const r=o*t,i=n[o];let a=e[r+o]*i;for(let c=o+1;c<t;c++){const l=e[r+c];a+=l*n[c],s[c]+=l*i}s[o]+=a}}function uc(e){let t=0;for(const s of e)t+=s*s;const n=Math.sqrt(t);if(!(n>0))return!1;for(let s=0;s<e.length;s++)e[s]/=n;return!0}function V8(e,t){if(t<2)return null;const n=new Float64Array(t);for(let u=0;u<t;u++){let h=0;for(let f=0;f<t;f++){const d=e[u*t+f];Number.isFinite(d)&&(h+=d)}n[u]=h}let s=0;for(const u of n)u>s&&(s=u);if(s*=2,!(s>0))return null;const o=new Float64Array(t*t);for(let u=0;u<t;u++)for(let h=0;h<t;h++){const f=Number.isFinite(e[u*t+h])?e[u*t+h]:0,d=u===h?n[u]-f:-f;o[u*t+h]=(u===h?s:0)-d}const r=new Float64Array(t).fill(1/Math.sqrt(t));let i=0;{const u=new Float64Array(t);m1(o,t,r,u);for(let h=0;h<t;h++)i+=r[h]*u[h]}d1(o,t,{value:i,vector:r});const a=f1(o,t,512,1e-9,0);if(a===null)return null;const c=new Int8Array(t);for(let u=0;u<t;u++)c[u]=a.vector[u]>=0?1:-1;let l=0;for(const u of c)u===1&&l++;return l===0||l===t?null:{side:c,value:s-a.value}}const qs=8192,Y8=.1;function hc(e,t){const{ids:n}=no(e.mc,t,Oe(t)),s=[];for(const l of n){const[u,h]=e.mc.contigExtent(l);h>u&&s.push(l)}let o=0;const r=[],i=[];for(let l=0;l<s.length;l++){const[u,h]=e.mc.contigExtent(s[l]);for(let f=u;f<h;f++)r.push(f),i.push(l);o+=h-u}const a=[],c=[];for(let l=0;l<r.length;l++){const u=a[a.length-1];if(u!==void 0&&r[l]===u.to){u.to++;continue}c.push(l),a.push({from:r[l],to:r[l]+1})}return{contigs:s,spans:a,spanAt:c,bin:Int32Array.from(r),group:Int32Array.from(i),n:o}}async function W8(e,t,n){const{spans:s,spanAt:o,n:r}=t,i=new Float32Array(r*r).fill(Number.NaN),a=Ct(e,s,n.balance),c=s.length*(s.length+1)/2;let l=0;const u=(h,f,d,m)=>{const g=s[d].to-s[d].from,p=s[m].to-s[m].from,w=o[d],b=o[m];for(let y=0;y<g;y++)for(let A=0;A<p;A++){const N=h[y*f+A];i[(w+y)*r+b+A]=N,i[(b+A)*r+w+y]=N}};for(let h=0;h<s.length;h++)for(let f=h;f<s.length;f++){if(e.throwIfCancelled(),e.progress(l,c,"reading"),l++,a!==null){const m=s[h].from-a.rowExtent[0],g=s[f].from-a.colExtent[0],p=s[h].to-s[h].from,w=s[f].to-s[f].from,b=new Float32Array(p*w);for(let y=0;y<p;y++){const A=(m+y)*a.ncols+g;b.set(a.values.subarray(A,A+w),y*w)}u(b,w,h,f);continue}const d=await e.mc.matrix({rows:[s[h].from,s[h].to],cols:[s[f].from,s[f].to],balance:n.balance,value:n.value});u(d.values,d.ncols,h,f)}return i}function X8(e,t,n){const{group:s,n:o}=t,r=t.contigs.length,i=[],a=[];for(let u=0;u<r;u++)i.push(s.indexOf(u)),a.push(s.lastIndexOf(u)+1);const c=i.map((u,h)=>{const f=a[h]-u,d=n[h],m=new Bn(d>0?Math.floor(f/2):Math.max(0,f-1),d);for(let g=u;g<a[h];g++)for(let p=u;p<a[h];p++)m.add(zn(Math.abs(p-g),d),e[g*o+p]);return m.finish()}),l=new Float64Array(r*r).fill(Number.NaN);for(let u=0;u<r;u++)for(let h=u+1;h<r;h++){let f=0,d=0;for(let g=i[u];g<a[u];g++)for(let p=i[h];p<a[h];p++){const w=e[g*o+p];Number.isFinite(w)&&(f+=w,d++)}const m=d===0?Number.NaN:f/d;l[u*r+h]=m,l[h*r+u]=m}return{group:s,cis:c,trans:l,contacts:"both",ignoreDiags:0,...n.some(u=>u>0)?{laps:n}:{}}}function J8(e,t){const n=new Int32Array(t.contigs.length);for(let s=0;s<t.contigs.length;s++){const o=t.contigs[s];if(!_e(e,e.mc.chromnames[o]))continue;const[r,i]=e.mc.contigExtent(o);n[s]=i-r}return n}function Z8(e,t,n,s,o,r){const i=Int32Array.from({length:t},(h,f)=>f),a=h1(e,i,i,s,o),c=n.length,l=s.circular?t:0,u=new Float64Array(c*c);for(let h=0;h<c;h++){const f=n[h];for(let d=0;d<c;d++){const m=n[d];if(zn(Math.abs(m-f),l)<r){u[h*c+d]=Number.NaN;continue}const g=e[f*t+m],p=a[f*t+m];u[h*c+d]=Number.isFinite(g)&&Number.isFinite(p)&&p!==0?g/p:Number.NaN}}return u}function j8(e,t,n,s){const o=t.contigs[0],r=e.mc.chromnames[o],i=T0(e,r),a=e.mc.binsize;i===void 0&&e.note(`${s}: the arms were read from the first base, the origin every sequence has until one is set — on the Chromosomes table, or by Origin and terminus.`);const c=i??0,l=Math.max(0,Math.min(t.n-1,Math.floor(c/a)));return e.note(`${s}: arms read from the origin at ${(c+1).toLocaleString()} (bin ${l.toLocaleString()}), ${n[0]>0?"round the circle":"along the line"}.`),{n:t.n,origin:l,circular:n[0]>0}}function tm(e){const t=e.contacts;return t==="trans"||t==="both"?t:"cis"}async function em(e,t,n){const{label:s}=n,o=nt(e.params,"balance",!0),r=e.framing.value===""?tt:e.framing.value,i=await W8(e,t,{balance:o,value:r});e.throwIfCancelled();const a=J8(e,t),c={...X8(i,t,a),contacts:n.contacts,ignoreDiags:n.ignoreDiags},l=m0(i,t.n,Y8);if(l.length<8)return e.note(`${s}: only ${l.length} bins carry enough data to correlate, which is too few for a component to mean anything.`),null;l.length<t.n&&e.note(`${s}: ${(t.n-l.length).toLocaleString()} of ${t.n.toLocaleString()} bins were left out for want of data. They are gaps in the track rather than zeros.`);const u=t.contigs.length===1?n.arms:void 0,h=u!==void 0?Z8(i,t.n,l,j8(e,t,a,s),u.window,n.ignoreDiags):Po(i,t.n,l,c);e.throwIfCancelled();let f=0;for(let M=0;M<h.length;M++)Number.isFinite(h[M])&&f++;if(f===0)return e.note(`${s}: nothing was left to correlate — ${c.ignoreDiags} ignored ${c.ignoreDiags===1?"diagonal covers":"diagonals cover"} the whole of a ${t.n.toLocaleString()}-bin square. Ignore fewer.`),null;const d=n.method==="cooltools";let m,g=l.length;if(d){if(e.progress(n.step.at,n.step.of,`decomposing ${s}`),u!==void 0)m=$a(h,l.length);else{const M=Po(i,t.n,l,Ea(c));m=$a(M,l.length,eo,M)}g=0;for(let M=0;M<m.length;M++)g+=m[M]*m[M]}else m=await Ud(h,l.length,async M=>{if(M%64!==0){e.throwIfCancelled();return}await e.breathe(),e.progress(n.step.at,n.step.of,`correlating ${s}`)});const p=Q8(m,l.length,n.wanted);if(p.length===0)return e.note(`${s}: the ${d?"matrix":"correlation matrix"} has no leading component; it may be too sparse.`),null;const w=d?p.map(({value:M})=>M*M/g):Kd(u!==void 0?h:Po(i,t.n,l,Ea(c),m),l.length,p.map(M=>M.vector)),b=rm(e,t,l);let y=null;for(let M=0;M<p.length&&M<n.tracks.length;M++){const{value:I,vector:x}=p[M];if(d){const k=Math.sqrt(Math.abs(I));for(let C=0;C<x.length;C++)x[C]*=k}let v=Number.NaN;if(b!==null){const k=Yd(x,b,H(e.params,"minR",.1));v=k.flipped?-k.r:k.r,M===0&&(y=k,e.note(k.confident?`${s}: E1 phased against ${k.signal} (r = ${dc(k.r)})${k.flipped?", sign flipped":""}.`:`${s}: phasing against ${k.signal} was ambiguous (r = ${dc(k.r)}), so E1’s sign is arbitrary and should not be compared with another run.`))}else M===0&&e.note("Nothing was chosen to phase against, so the sign of each component is arbitrary.");n.rows.push({sequence:s,component:`E${M+1}`,eigenvalue:I,...d?{}:{explained:I/g},contacts:w[M],r:v});const S=n.tracks[M];for(let k=0;k<l.length;k++)S[t.bin[l[k]]]=x[k]}if(om(e,n.segments,t,l,p[0].vector),!nt(e.params,"saddle",!0))return{saddle:null};const A=Math.max(5,Math.min(100,Math.round(H(e.params,"quantiles",50)))),N=Wd(h,l.length,p[0].vector,A);return{saddle:{kind:"matrix",id:`compartment-saddle-${s}`,label:`Saddle · ${s}`,nrows:A,ncols:A,values:N.values,xLabel:"E1 quantile",yLabel:"E1 quantile",xTicks:[],yTicks:[],colormap:pl,centre:1,stats:[{label:"AA",value:N.strength.aa,digits:2},{label:"BB",value:N.strength.bb,digits:2},{label:"AB",value:N.strength.ab,digits:2},...y===null||y.confident?[]:[{label:"phasing r",value:y.r,digits:2,hint:"Too weak to fix the sign, so AA and BB may be the wrong way round."}]]}}}const nm=async e=>{const n=Ht(e.params,"sequences")??ao(e.mc).map(w=>e.mc.chromnames[w]);if(n.length===0)return e.note("No sequence is ticked, so there is nothing to correlate."),[];const s=no(e.mc,n,Oe(n)).missing;if(s.length>0&&s.length<n.length){const w=s.slice(0,6).join(", ");e.note(`${s.length} of the ${n.length} ticked ${s.length===1?"sequence is":"sequences are"} not in this file and ${s.length===1?"was":"were"} left out: ${w}${s.length>6?", …":""}.`)}const o=hc(e,n);if(o.n===0)return e.note(`None of the ${n.length} ticked ${n.length===1?"sequence":"sequences"} is in this file, under that spelling or any this viewer knows of. It has ${e.mc.chromnames.slice(0,6).join(", ")}${e.mc.chromnames.length>6?", …":""}.`),[];const r=tm(e.params);if(r==="trans"&&o.contigs.length<2)throw new Error("A trans correlation is between sequences, so it needs at least two ticked. Tick more, or read this one’s cis contacts instead.");const i=r==="cis"?o.contigs.map(w=>hc(e,[e.mc.chromnames[w]])):[o],a=Math.max(0,Math.round(H(e.params,"ignoreDiags",2))),c=Math.max(1,Math.min(3,Math.round(H(e.params,"components",1)))),l=[];for(let w=0;w<c;w++)l.push(new Float32Array(e.mc.nBins).fill(Number.NaN));const u=new se,h=[],f=[],d=r==="cis"&&lt(e.params,"expected","distance")==="arms"?{window:Math.max(0,Math.round(H(e.params,"armWindow",50)))}:void 0;d!==void 0&&e.note(d.window===0?"Divided by the decay along both arms, over the whole of each chromosome.":`Divided by the decay along both arms, within ${d.window.toLocaleString()} bins (${(d.window*e.mc.binsize).toLocaleString()} bp) of each cell along them.`);const m=lt(e.params,"method","cooltools");if(m!=="correlation"&&m!=="cooltools")throw new Error(`There is no method “${m}”. It is cooltools or correlation (Lieberman-Aiden 2009).`);e.note(m==="cooltools"?`Observed/expected decomposed itself, as cooltools does: clipped at its ${eo}th percentile, one subtracted, every gap read as expected, and each eigenvector scaled by the square root of its eigenvalue.`:"The correlation of observed/expected’s rows decomposed, as Lieberman-Aiden et al. (2009) did it.");let g=0;for(let w=0;w<i.length;w++){await e.breathe();const b=i[w],y=fc(e,b);if(b.n>qs){const N=`${y} is ${b.n.toLocaleString()} bins, past the ${qs.toLocaleString()}-bin limit on a correlation matrix`;if(i.length===1)throw new Error(`Refusing to build it: ${N}. Untick some, or open the file coarser.`);e.note(`${N}, so it was skipped. Open the file at a coarser resolution to include it.`);continue}e.progress(w,i.length,`compartments on ${y}`);const A=await em(e,b,{contacts:r,ignoreDiags:a,wanted:c,tracks:l,segments:u,label:y,step:{at:w,of:i.length},...d===void 0?{}:{arms:d},method:m,rows:f});A!==null&&(g++,A.saddle!==null&&h.push(A.saddle))}if(g===0)return e.note("Nothing produced a component. See the notes above."),[];const p=`${fc(e,o)} · ${sm[r]}${d===void 0?"":" · arms"}${m==="correlation"?" · correlation":""}`;return h.push({kind:"table",id:"compartment-components",label:`Components · ${p}`,columns:[{key:"sequence",label:"sequence",kind:"text"},{key:"component",label:"component",kind:"text",hint:"By eigenvalue, largest first."},{key:"eigenvalue",label:"eigenvalue",kind:"number",digits:3},...m==="cooltools"?[]:[{key:"explained",label:"variance",kind:"number",digits:3,hint:"Share of the correlation matrix’s variance — how alike the rows are, not how much of the map: its eigenvalue over its rows."}],{key:"contacts",label:"of contacts",kind:"number",digits:3,hint:"How much of the contacts this component accounts for: the share of the map’s departure from expected — observed/expected, clipped, less one — that its checkerboard carries. The same measure under either method, and the components’ shares add up."},{key:"r",label:"r with phasing",kind:"number",digits:3,hint:"Pearson’s r with the phasing signal, as drawn: positive once the sign is fixed."}],rows:f}),h.unshift(...l.map((w,b)=>({kind:"profile",id:`compartment-e${b+1}`,label:`E${b+1} · ${p}`,values:w,extent:[0,e.mc.nBins],binsize:e.mc.binsize,unit:""})),{kind:"intervals",id:"compartment-ab",label:`A/B compartments · ${p}`,payload:{kind:"features",data:u.finish()},noun:{one:"segment",many:"segments"},lane:{under:{output:"compartment-e1"}},display:"lane",look:"corner"}),e.progress(i.length,i.length,"done"),h},sm={cis:"cis",trans:"trans",both:"cis + trans"};function fc(e,t){const n=t.contigs.length;if(n===1)return e.mc.chromnames[t.contigs[0]];const s=e.mc.chromnames.length;return n===s?"genome-wide":`${n.toLocaleString()} of ${s.toLocaleString()} sequences`}function dc(e){return Number.isFinite(e)?e.toFixed(3):"—"}function om(e,t,n,s,o){const r=e.mc.binsize;for(let i=0;i<n.contigs.length;i++){const a=n.contigs[i],c=e.mc.chromnames[a],[l]=e.mc.contigExtent(a),u=e.mc.chromsizes.get(c)??Number.POSITIVE_INFINITY,h=[],f=[];for(let d=0;d<s.length;d++)n.group[s[d]]===i&&(h.push(n.bin[s[d]]),f.push(o[d]));if(h.length!==0)for(const d of Xd(Int32Array.from(h),Float64Array.from(f)))t.add(c,(d.from-l)*r,Math.min(u,(d.to-l+1)*r),{name:d.side,score:d.mean,detail:`E1 mean ${d.mean.toFixed(3)} · ${d.bins} bins`})}}function rm(e,t,n){const s=Ds(e.inputs.features,e.params,"phaseFeatures").value;if(s!==void 0)return{name:"gene density",values:im(e,s,t,n)};const o=_s(e.params,"phaseTrack");if(o!==null){const c=e.inputs.vectors?.phaseTrack;if(c===void 0)e.note("The track chosen for phasing could not be read, so it was not used. Load it again, or choose another.");else{const l=new Float64Array(n.length);for(let u=0;u<n.length;u++){const h=t.bin[n[u]];l[u]=h>=0&&h<c.length?c[h]:Number.NaN}return{name:e.inputs.labels?.[o]??"the chosen track",values:l}}}if(_s(e.params,"phaseSequence")===null)return null;const i=e.inputs.vectors?.phaseSequence;if(i===void 0)return e.note("The sequence chosen for phasing could not be read, so the sign was left alone. Re-open the FASTA, or choose a gene model instead."),null;const a=new Float64Array(n.length);for(let c=0;c<n.length;c++){const l=t.bin[n[c]];a[c]=l>=0&&l<i.length?i[l]:Number.NaN}return{name:"GC content",values:a}}function im(e,t,n,s){const o=new Float64Array(e.mc.nBins);for(const i of n.contigs){const a=e.mc.chromnames[i],c=t.contigs.indexOf(a);if(!(c<0))for(let l=0;l<t.length;l++){if(t.contig[l]!==c)continue;const u=e.mc.coords.binIndex(i,t.start[l]),h=e.mc.coords.binIndex(i,Math.max(t.start[l],t.end[l]-1));for(let f=u;f<=h;f++)f>=0&&f<o.length&&(o[f]+=1)}}const r=new Float64Array(s.length);for(let i=0;i<s.length;i++)r[i]=o[n.bin[s[i]]];return r}function am(e,t){const n=Math.max(1,Math.trunc(e)),s=Math.max(0,Math.trunc(t));let o=0;for(let r=0;r<n;r++)for(let i=0;i<n;i++)r+i>=s&&o++;return o}function cm(e,t){const{size:n,values:s}=e,o=Math.max(1,Math.trunc(t.window)),r=Math.max(0,Math.trunc(t.ignoreDiags??0)),i=new Float64Array(n),a=new Float64Array(n);for(let c=0;c<n;c++){let l=0,u=0;for(let h=0;h<o&&c-h>=0;h++){const f=(c-h)*n;for(let d=Math.max(0,r-h);d<o&&c+d<n;d++){const m=s[f+c+d];Number.isNaN(m)||(l+=m,u++)}}i[c]=l,a[c]=u}return{sums:i,counts:a}}function lm(e,t){const n=e.n,s=Math.max(1,Math.trunc(t.window)),o=Math.max(0,Math.trunc(t.ignoreDiags??0)),r=new Float64Array(n),i=new Float64Array(n);for(let a=0;a<n;a++){let c=0,l=0;for(let u=0;u<s;u++)for(let h=Math.max(0,o-u);h<s;h++){const f=e.forward(a-u,u+h);Number.isFinite(f)&&(c+=f,l++)}r[a]=c,i[a]=l}return{sums:r,counts:i}}function um(e){const t=e.sums.length,n=new Float64Array(t).fill(Number.NaN),s=[];for(let i=0;i<t;i++)e.counts[i]>0&&(n[i]=e.sums[i]/e.counts[i],s.push(n[i]));const o=new Float64Array(t).fill(Number.NaN),r=hm(s);if(!(r>0))return o;for(let i=0;i<t;i++){const a=Math.log2(n[i]/r);Number.isFinite(a)&&(o[i]=a)}return o}function hm(e){if(e.length===0)return Number.NaN;const t=Float64Array.from(e).sort(),n=t.length>>1;return t.length%2===1?t[n]:(t[n-1]+t[n])/2}function fm(e){const t=e.length,n=[];for(let c=0;c<t;c++)Number.isNaN(e[c])||n.push(c);const s=[],o=[];for(let c=1;c<n.length-1;c++){const l=e[n[c-1]],u=e[n[c]],h=e[n[c+1]];l>u&&u<h&&s.push(n[c]),l<u&&u>h&&o.push(n[c])}const r=[],i=[];for(const c of o){const l=e[c];let u=-1;for(let m=c-1;m>=0;m--)if(e[m]>l){u=m;break}let h=-1;for(let m=c+1;m<t;m++)if(e[m]>l){h=m;break}const f=u>=0?l-Vo(e,u,c):Number.NaN,d=h>=0?l-Vo(e,c,h):Number.NaN;r.push(dm(f,d)),i.push(u<0&&h<0)}const a=i.indexOf(!0);if(a>=0){const c=o[a];let l=Number.POSITIVE_INFINITY;for(const u of s)l=Math.min(l,e[u]);r[a]=e[c]-(s.length>0?l:Vo(e,0,t))}return{positions:o,prominences:r}}function Vo(e,t,n){let s=Number.NaN;for(let o=t;o<n;o++){const r=e[o];Number.isNaN(r)||(Number.isNaN(s)||r<s)&&(s=r)}return s}function dm(e,t){return Number.isNaN(e)?t:Number.isNaN(t)?e:Math.min(e,t)}function mm(e,t,n){const s=[];for(let c=0;c<e.length;c++)t[c]>=n.minCells&&s.push(c);const o=s.map(c=>-e[c]),{positions:r,prominences:i}=fm(o),a=[];for(let c=0;c<r.length;c++){const l=i[c];if(!(l>=n.threshold))continue;const u=s[r[c]];a.push({bin:u,score:e[u],prominence:l})}return gm(a,n.spacing)}function gm(e,t){if(t<=1)return[...e].sort((o,r)=>o.bin-r.bin);const n=[...e].sort((o,r)=>r.prominence-o.prominence),s=[];for(const o of n)s.some(r=>Math.abs(r.bin-o.bin)<t)||s.push(o);return s.sort((o,r)=>o.bin-r.bin)}function pm(e,t){const n=[],s=e.length;for(let r=0;r<s;r++){const i=e[r];if(!Number.isFinite(i))continue;const a=wm(e,r),c=bm(e,r),l=a<0?Number.POSITIVE_INFINITY:e[a],u=c<0?Number.POSITIVE_INFINITY:e[c];if(i>l||i>u||i===l&&i===u||i===l)continue;const h=Math.min(mc(e,r,-1),mc(e,r,1));h>=t.prominence&&n.push({bin:r,score:i,prominence:h})}n.sort((r,i)=>i.prominence-r.prominence);const o=[];for(const r of n)o.some(i=>Math.abs(i.bin-r.bin)<t.spacing)||o.push(r);return o.sort((r,i)=>r.bin-i.bin),o}function g1(e){const t=[...e].sort((s,o)=>s.bin-o.bin),n=[];for(let s=1;s<t.length;s++){const o=t[s-1],r=t[s];r.bin<=o.bin||n.push({from:o.bin,to:r.bin,strength:Math.min(o.prominence,r.prominence)})}return n}function mc(e,t,n){const s=e[t];let o=s;for(let r=t+n;r>=0&&r<e.length;r+=n){const i=e[r];if(!Number.isFinite(i)||i<s)break;i>o&&(o=i)}return o-s}function wm(e,t){for(let n=t-1;n>=0;n--)if(Number.isFinite(e[n]))return n;return-1}function bm(e,t){for(let n=t+1;n<e.length;n++)if(Number.isFinite(e[n]))return n;return-1}function Am(e,t){const n=e.n,s=Math.max(1,Math.trunc(t.window)),o=Math.max(0,Math.trunc(t.ignoreDiags??0)),r=Math.max(1,o),i=new Float64Array(n).fill(Number.NaN);if(s<r)return i;const a=s-r+1,c=Math.max(1,Math.ceil(a*Math.max(0,Math.min(1,t.minValid))));for(let l=0;l<n;l++){let u=0,h=0,f=0,d=0;for(let w=r;w<=s;w++){const b=e.forward(l,w);Number.isFinite(b)&&(h+=b,d++);const y=e.forward(l-w,w);Number.isFinite(y)&&(u+=y,f++)}if(f<c||d<c)continue;const m=u+h;if(!(m>0))continue;const g=u-h,p=g*g/m;i[l]=h>=u?p:-p}return i}function ym(e){const t=e.length,n=new Float64Array(t).fill(Number.NaN),s=[];for(let i=0;i<t;i++){const a=e[i];Number.isFinite(a)&&s.push(Math.abs(a))}if(s.length===0)return n;s.sort((i,a)=>i-a);const o=s[s.length>>1];if(!(o>0))return n;let r=0;for(let i=0;i<t;i++){const a=e[i];Number.isFinite(a)&&(r+=a/o,n[i]=r)}return n}function Nm(e,t){const n=e.length,s=pm(ym(e),{prominence:t.prominence,spacing:t.spacing}),o=[],r=new Set;for(const i of s){const a=i.bin+1;a>=n||r.has(a)||(r.add(a),o.push({bin:a,score:e[a],prominence:i.prominence}))}return o}const Mm=50;function Im(e){const t=typeof e.window=="number"?e.window:Mm;return Math.max(1,Math.round(t))}function xm(e,t){const n=e.mc,s=n.chromsizes,{ids:o,missing:r}=be(n,t),i=[];for(const a of o){const c=Ut(n,a);if(c===null)continue;const l=n.chromnames[a];i.push({name:l,span:c,length:s.get(l)??(c.to-c.from)*n.binsize,circular:_e(e,l)})}return{units:i,missing:r}}const vm=async e=>{const t=nt(e.params,"balance",!1),n=e.framing.value===""?tt:e.framing.value,s=Math.max(0,Math.min(1,H(e.params,"minValid",.5))),o=Math.max(0,Math.round(H(e.params,"ignoreDiags",0))),r=Im(e.params),i=r*e.mc.binsize,a=nt(e.params,"call",!0),c=Math.max(0,H(e.params,"prominence",10)),l=Math.max(0,Math.round(H(e.params,"spacing",0)));e.note(`A ${r.toLocaleString()}-bin window is ${rt(i)} at this file’s ${e.mc.binsize.toLocaleString()} bp resolution.`),r<5&&e.note(`Each side of the statistic is then a sum over ${r} ${r===1?"cell":"cells"} per row. Widen the window, or open a finer rung.`);const u=Ht(e.params,"sequences");if(u!==null&&u.length===0)return e.note("No sequence is ticked, so there is nothing to score."),[];const{units:h,missing:f}=xm(e,u),d=he(e.mc,f,h.length);if(d!==null&&e.note(d),h.length===0)return[];const m=h.filter(I=>I.span.to-I.span.from>r*2);if(m.length===0)return e.note(`No sequence in this file is longer than twice a ${r.toLocaleString()}-bin window, so no bin on any of them has a whole window either side. Use a narrower window.`),[];const g=e.mc.nBins,p=new Float32Array(g).fill(Number.NaN),w=Ct(e,m.map(I=>I.span),t);let b=0;const y=[];for(let I=0;I<m.length;I++){const x=m[I];await e.breathe(),e.progress(I,m.length,x.name),x.circular&&b++;const v=await De(e,x.span,{hi:r,block:w,balance:t,value:n,circular:x.circular,onTile:async(k,C)=>{await e.breathe(),e.progress(I,m.length,C===1?x.name:`${x.name} · strip ${Math.min(k+1,C)} of ${C}`)}}),S=Am(v,{window:r,minValid:s,ignoreDiags:o});for(let k=0;k<S.length;k++)p[x.span.from+k]=S[k];a&&y.push({unit:x,borders:Nm(S,{prominence:c,spacing:l})})}e.progress(m.length,m.length,"done"),b>0&&e.note(`${b.toLocaleString()} of ${m.length.toLocaleString()} ${m.length===1?"sequence was":"sequences were"} read as circular, so the window wraps past the end and every bin is scored.`);const N=[{kind:"profile",id:`di-w${r}`,label:`DI · ${rt(i)}`,values:p,extent:[0,g],binsize:e.mc.binsize,unit:"signed χ²"}];if(!a)return N;const M=y.reduce((I,x)=>I+x.borders.length,0);if(M===0)return e.note(`No sign flip carried ${c} bins of steady bias on both sides, so no border was called. Lower the prominence, or widen the window.`),N;if(N.push(km(e,y)),e.note(`${M.toLocaleString()} borders called across ${y.length.toLocaleString()} sequences.`),nt(e.params,"domains",!0)){const I=Sm(e,y);I===null?e.note("No sequence had two borders, so no domain has both of its edges and none was made."):(N.push(I.output),e.note(`${I.count.toLocaleString()} domains between them, from each border to the next.`))}return N};function km(e,t){const n=new se;let s=0;for(const{unit:o,borders:r}of t)for(const i of r){const a=i.bin*e.mc.binsize,c=Number.isFinite(i.score)?`DI ${i.score.toFixed(1)}`:"DI not scored";n.add(o.name,a,Math.min(o.length,a+e.mc.binsize),{score:i.prominence,name:`border_${++s}`,detail:`${c} · prominence ${i.prominence.toFixed(1)} bins`})}return{kind:"intervals",id:"di-borders",label:"DI borders",payload:{kind:"features",data:n.finish()},noun:{one:"border",many:"borders"},display:"lane",look:"point"}}function Sm(e,t){const n=new se;let s=0;for(const{unit:o,borders:r}of t)for(const i of g1(r)){const a=i.from*e.mc.binsize,c=Math.min(o.length,i.to*e.mc.binsize);c<=a||(s++,n.add(o.name,a,c,{score:i.strength,name:`domain_${s}`,detail:`${i.to-i.from} bins · weaker border prominence ${i.strength.toFixed(1)} bins`}))}return s===0?null:{count:s,output:{kind:"intervals",id:"di-domains",label:"DI domains",payload:{kind:"features",data:n.finish()},noun:{one:"domain",many:"domains"},display:"overlay",look:"corner"}}}function p1(e,t=0){const n=Math.max(1,Math.trunc(e))-1,s=Math.max(0,Math.trunc(t));let o=0;for(let r=s;r<=n;r++)o+=r===0?1:2;return o}function Cm(e,t){const n=e.n,s=Math.max(1,Math.trunc(t.diagonals))-1,o=Math.max(0,Math.trunc(t.ignoreDiags??0)),r=new Float64Array(n).fill(Number.NaN);if(n===0)return r;const i=p1(s+1,o);if(i<=0)return r;const a=Math.max(1,Math.ceil(i*Math.max(0,Math.min(1,t.minValid)))),c=e.circular?0:s,l=e.circular?n-1:n-1-s;if(l<c)return r;for(let u=c;u<=l;u++){let h=0,f=0;for(let d=o;d<=s;d++){const m=e.forward(u,d);if(Number.isFinite(m)&&(h+=m,f++),d===0)continue;const g=e.forward(u,-d);Number.isFinite(g)&&(h+=g,f++)}f>=a&&(r[u]=h/i)}return r}const Fm=3;function w1(e){const t=typeof e.diagonals=="number"?e.diagonals:Fm;return Math.max(1,Math.round(t))}function $m(e){return w1(e)-1}function Em(e,t){const n=e.mc,{ids:s,missing:o}=be(n,t),r=[];for(const i of s){const a=Ut(n,i);if(a===null)continue;const c=n.chromnames[i];r.push({name:c,span:a,circular:_e(e,c)})}return{units:r,missing:o}}const Bm=async e=>{const t=nt(e.params,"balance",!1),n=e.framing.value===""?tt:e.framing.value,s=Math.max(0,Math.min(1,H(e.params,"minValid",.5))),o=Math.max(0,Math.round(H(e.params,"ignoreDiags",0))),r=w1(e.params),i=$m(e.params),a=i*e.mc.binsize,c=p1(r,o),l=o>0?"less the diagonals cut below":"the main diagonal included";if(e.note(`${r.toLocaleString()} ${r===1?"diagonal reaches":"diagonals reach"} ${rt(a)} either way at this file’s ${e.mc.binsize.toLocaleString()} bp resolution, and each score is the mean of ${c.toLocaleString()} ${c===1?"cell":"cells"} — the bin’s own line across them, ${l}.`),c===0)return e.note(`Ignoring ${o.toLocaleString()} diagonals leaves nothing of a ${r.toLocaleString()}-diagonal line, so there is no cell left to average. Count more diagonals, or ignore fewer.`),[];const u=Ht(e.params,"sequences");if(u!==null&&u.length===0)return e.note("No sequence is ticked, so there is nothing to score."),[];const{units:h,missing:f}=Em(e,u),d=he(e.mc,f,h.length);if(d!==null&&e.note(d),h.length===0)return[];const m=h.filter(N=>N.span.to-N.span.from>2*i);if(m.length===0)return e.note(`No sequence in this file is longer than the ${(2*i+1).toLocaleString()} bins this line spans, so no bin on any of them has a whole line through it. Use fewer diagonals.`),[];const g=e.mc.nBins,p=new Float32Array(g).fill(Number.NaN),w=Ct(e,m.map(N=>N.span),t);let b=0;for(let N=0;N<m.length;N++){const M=m[N];await e.breathe(),e.progress(N,m.length,M.name),M.circular&&b++;const I=await De(e,M.span,{hi:i,block:w,balance:t,value:n,circular:M.circular,onTile:async(v,S)=>{await e.breathe(),e.progress(N,m.length,S===1?M.name:`${M.name} · strip ${Math.min(v+1,S)} of ${S}`)}}),x=Cm(I,{diagonals:r,minValid:s,ignoreDiags:o});for(let v=0;v<x.length;v++)p[M.span.from+v]=x[v]}return e.progress(m.length,m.length,"done"),b>0&&e.note(`${b.toLocaleString()} of ${m.length.toLocaleString()} ${m.length===1?"sequence was":"sequences were"} read as circular, so the line wraps past the end and every bin is scored.`),b<m.length&&i>0&&e.note(`The first and last ${i.toLocaleString()} ${i===1?"bin":"bins"} of a linear sequence have no whole line through them and are left as gaps, rather than scored on the part of it that fits — which would fade both ends towards zero.`),[{kind:"profile",id:`hicscore-d${r}`,label:`HiC score · ${r} diag`,values:p,extent:[0,g],binsize:e.mc.binsize,unit:"contacts"}]},Tm=50,Us=new Float64Array(1),Rt=new Uint32Array(Us.buffer),b1=new Uint8Array(new Uint16Array([1]).buffer)[0]===1,gc=134217729;function Om(e,t){Us[0]=e;const n=b1?0:1,s=1-n;return t?(Rt[n]=Rt[n]+1>>>0,Rt[n]===0&&(Rt[s]=Rt[s]+1>>>0)):(Rt[n]===0&&(Rt[s]=Rt[s]-1>>>0),Rt[n]=Rt[n]-1>>>0),Us[0]}function Lm(e){return Us[0]=e,(Rt[b1?0:1]&1)===1}function Gs(e,t,n){const s=e*t;if(!Number.isFinite(s)||!Number.isFinite(n))return s+n;let o=gc*e;const r=o-(o-e),i=e-r;o=gc*t;const a=o-(o-t),c=t-a,l=r*a-s+r*c+i*a+i*c,u=n+s,h=u-n,f=n-(u-h)+(s-h);let d=f+l;const m=d-f,g=f-(d-m)+(l-m);return g!==0&&!Lm(d)&&(d=Om(d,g>0==d>0)),u+d}function Yo(e,t,n,s){return Gs(t,s,e*n)}function _m(e,t,n,s,o){return o===1?Gs(t,s,e*n):Gs(e,n,t*s)}function A1(e,t,n,s){return Gs(t,s,e*n)}function Mr(e,t){return e*e+t*t}class _n{n=0;ls0=0;ls1=0;ss=0;c0=0;c1=0;sqNorm=0;child=null;static of(t,n){const s=new _n;return s.n=1,s.ls0=t,s.ls1=n,s.c0=t,s.c1=n,s.ss=s.sqNorm=Yo(t,n,t,n),s}update(t){this.n+=t.n,this.ls0+=t.ls0,this.ls1+=t.ls1,this.ss+=t.ss,this.c0=this.ls0/this.n,this.c1=this.ls1/this.n,this.sqNorm=Yo(this.c0,this.c1,this.c0,this.c1)}merge(t,n){const s=this.ss+t.ss,o=this.ls0+t.ls0,r=this.ls1+t.ls1,i=this.n+t.n,a=1/i,c=a*o,l=a*r,u=Yo(c,l,c,l);return s/i-u<=n*n?(this.n=i,this.ls0=o,this.ls1=r,this.ss=s,this.c0=c,this.c1=l,this.sqNorm=u,!0):!1}}class Cn{constructor(t,n,s){this.threshold=t,this.branching=n,this.leaf=s}subclusters=[];centroids=[];sqNorms=[];prev=null;next=null;append(t){const n=this.subclusters.length;this.subclusters.push(t),this.centroids[2*n]=t.c0,this.centroids[2*n+1]=t.c1,this.sqNorms[n]=t.sqNorm}refresh(t){const n=this.subclusters[t];this.centroids[2*t]=n.c0,this.centroids[2*t+1]=n.c1,this.sqNorms[t]=n.sqNorm}replace(t,n,s){const o=this.subclusters.indexOf(t);this.subclusters[o]=n,this.refresh(o),this.append(s)}insert(t){const n=this.subclusters.length;if(n===0)return this.append(t),!1;let s=0,o=Number.POSITIVE_INFINITY;for(let i=0;i<n;i++){const a=_m(this.centroids[2*i],this.centroids[2*i+1],t.c0,t.c1,n)*-2+this.sqNorms[i];a<o&&(o=a,s=i)}const r=this.subclusters[s];if(r.child!==null){if(!r.child.insert(t))return r.update(t),this.refresh(s),!1;const[a,c]=y1(r.child,this.threshold,this.branching);return this.replace(r,a,c),this.subclusters.length>this.branching}return r.merge(t,this.threshold)?(this.refresh(s),!1):(this.append(t),this.subclusters.length>this.branching)}}function y1(e,t,n){const s=new _n,o=new _n,r=new Cn(t,n,e.leaf),i=new Cn(t,n,e.leaf);s.child=r,o.child=i,e.leaf&&(e.prev!==null&&(e.prev.next=r),r.prev=e.prev,r.next=i,i.prev=r,i.next=e.next,e.next!==null&&(e.next.prev=i));const a=e.subclusters.length,c=e.centroids,l=new Float64Array(a);for(let m=0;m<a;m++)l[m]=Mr(c[2*m],c[2*m+1]);const u=new Float64Array(a*a);let h=0;for(let m=0;m<a;m++)for(let g=0;g<a;g++){let p=0;m!==g&&(p=-2*A1(c[2*m],c[2*m+1],c[2*g],c[2*g+1])+l[m]+l[g],p>0||(p=0)),u[m*a+g]=p,p>u[h]&&(h=m*a+g)}const f=Math.floor(h/a),d=h%a;for(let m=0;m<a;m++){const g=e.subclusters[m];m===f||u[f*a+m]<u[d*a+m]?(r.append(g),s.update(g)):(i.append(g),o.update(g))}return[s,o]}function Dm(e,t,n=Tm){const s=e.length/2;let o=new Cn(t,n,!0);const r=new Cn(t,n,!0);r.next=o,o.prev=r;for(let f=0;f<s;f++){const d=_n.of(e[2*f],e[2*f+1]);if(o.insert(d)){const[m,g]=y1(o,t,n);o=new Cn(t,n,!1),o.append(m),o.append(g)}}const i=[];for(let f=r.next;f!==null;f=f.next)for(let d=0;d<f.subclusters.length;d++)i.push(f.centroids[2*d],f.centroids[2*d+1]);const a=Float64Array.from(i),c=a.length/2,l=new Float64Array(c);for(let f=0;f<c;f++)l[f]=Mr(a[2*f],a[2*f+1]);const u=new Float64Array(c),h=new Int32Array(s);for(let f=0;f<s;f++){const d=e[2*f],m=e[2*f+1],g=Mr(d,m);let p=Number.POSITIVE_INFINITY;for(let y=0;y<c;y++){const A=-2*(d*a[2*y]+m*a[2*y+1])+g+l[y];u[y]=A,A<p&&(p=A)}let w=0,b=Number.POSITIVE_INFINITY;for(let y=0;y<c;y++){if(u[y]>p+1e-12*(g+l[y]+Math.abs(p)))continue;let A=-2*A1(d,m,a[2*y],a[2*y+1])+g+l[y];A>0||(A=0),A=Math.sqrt(A),A<b&&(b=A,w=y)}h[f]=w}return{labels:h,centers:a}}const hi=["donut","vertical","horizontal","lowleft"];function N1(e){return e>28e3?null:e>=18e3?{w:3,p:1}:e>=8e3?{w:5,p:2}:e>=4e3?{w:7,p:4}:null}function Rm(e,t){const{w:n,p:s}=e,o=2*n+1,r=new Uint8Array(o*o);for(let i=0;i<o;i++)for(let a=0;a<o;a++){const c=i-n,l=a-n,u=-s<=l&&l<=s&&-s<=c&&c<=s;let h;switch(t){case"donut":h=u||l===0||c===0;break;case"vertical":h=-1>l||l>1||u;break;case"horizontal":h=-1>c||c>1||u;break;case"lowleft":{const f=-c,d=-l;h=d>=-s&&f<=s||d>=0||f<=0;break}}r[i*o+a]=h?0:1}return r}function zm(e,t){const{w:n}=e,s=2*n+1,o=Rm(e,t),r=[],i=[];for(let a=-n;a<=n;a++)for(let c=-n;c<=n;c++)o[(n-a)*s+(n-c)]!==0&&(r.push(a),i.push(c-a));return{dr:Int32Array.from(r),dd:Int32Array.from(i)}}const pc=40,wc=50,Pm=[1,1.2599210498948732,1.5874010519681996,2,2.5198420997897464,3.174802103936399,4.000000000000001,5.039684199579494,6.349604207872799,8.000000000000002,10.079368399158987,12.699208415745598,16.000000000000004,20.158736798317975,25.3984168314912,32.00000000000001,40.317473596635956,50.7968336629824,64.00000000000003,80.63494719327191,101.5936673259648,128.00000000000006,161.26989438654383,203.18733465192963,256.0000000000001,322.5397887730877,406.37466930385926,512.0000000000002,645.0795775461754,812.7493386077186,1024.0000000000007,1290.1591550923508,1625.4986772154373,2048.0000000000014,2580.318310184702,3250.997354430875,4096.000000000003,5160.636620369404,6501.99470886175,8192.000000000007,10321.273240738808,13003.989417723502,16384.000000000015,20642.54648147762,26007.978835447004,32768.00000000003,41285.09296295524,52015.95767089401,65536.00000000006,82570.1859259105],Wo=[[-.3333333333333333,.08333333333333333,-.014814814814814815,.0011574074074074073,.0003527336860670194,-.0001787551440329218,3919263178522438e-20,-21854485106799924e-22,-185406221071516e-20,8296711340953087e-22,-17665952736826078e-23,6707853543401498e-24,10261809784240309e-24,-4382036018453353e-24,914769958223679e-24,-2551419399494625e-26,-5830772132550426e-26,24361948020667415e-27,-50276692801141755e-28,11004392031956135e-29,33717632624009856e-29,-1392388722418162e-28,28534893807047445e-30,-5139111834242572e-31,-19752288294349442e-31],[-.001851851851851852,-.003472222222222222,.0026455026455026454,-.0009902263374485596,.00020576131687242798,-4018775720164609e-22,-18098550334489977e-21,764916091608111e-20,-16120900894563446e-22,4647127802807434e-24,1378633446915721e-22,-5752545603517705e-23,11951628599778148e-24,-17543241719747647e-27,-10091543710600413e-25,4162792991842583e-25,-856390702649298e-25,6067215101604758e-29,71624989648114856e-28,-2933186643771437e-27,5996696365683689e-28,-21671786527323313e-32,-4978339972369262e-29,20291628823713425e-30,-413125571381061e-29],[.004133597883597883,-.0026813271604938273,.0007716049382716049,20093878600823047e-22,-.00010736653226365161,52923448829120125e-21,-12760635188618728e-21,3423578734096138e-23,13721957309062932e-22,-6298992138380055e-22,14280614206064242e-23,-20477098421990866e-26,-1409252991086752e-23,6228974084922022e-24,-13670488396617112e-25,9428356159014678e-28,12872252400089318e-26,-55645956134363323e-27,1197593554636698e-26,-41689782251838634e-31,-10940640427884595e-28,4662239946390136e-28,-9905105763906907e-29,18931876768373515e-33,8859221872591127e-30],[.0006494341563786008,.00022947209362139917,-.0004691894943952557,.00026772063206283885,-7561801671883977e-20,-2396505113867297e-22,11082654115347302e-21,-56749528269915965e-22,14230900732435883e-22,-27861080291528143e-27,-16958404091930278e-23,8099464905388083e-23,-19111168485973655e-24,23928620439808118e-28,20620131815488797e-25,-9460496661855133e-25,21541049775774907e-26,-1388823336813903e-29,-21894761681963938e-27,9790998951171684e-27,-2178219188018096e-27,6208819573407901e-32,2126978363279737e-28,-9344688791517433e-29,2045367122678285e-29],[-.0008618882909167117,.0007840392217200666,-.0002990724803031902,-14638452578843418e-22,6641498215465122e-20,-3968365047179435e-20,11375726970678419e-21,2507497226237533e-25,-16954149536558305e-22,8907507532205309e-22,-2292934834000805e-22,2956794137544049e-26,28865829742708783e-24,-14189739437803219e-24,34463580499464896e-25,-23024517174528067e-29,-39409233028046403e-26,186023389685045e-24,-4356323005056618e-26,1278600101629623e-30,467927502665792e-26,-2149246470613483e-27,4908815614809652e-28,-633859148489156e-32,-5045332069080094e-29],[-.00033679855336635813,-6972813758365858e-20,.0002772753244959392,-.00019932570516188847,6797780477937208e-20,1419062920643967e-22,-13594048189768693e-21,8018470256334202e-21,-2291481176508095e-21,-3252473551298454e-25,34652846491085265e-23,-18447187191171344e-23,48240967037894184e-24,-17989466721743514e-30,-6306194500013523e-24,3162417628774568e-24,-7840924253697429e-25,5192679165254041e-30,9358944242306784e-26,-4513426216163278e-26,10799129993116828e-27,-3661886712685252e-32,-1210902069055155e-27,5680743584990564e-28,-13249659916340829e-29],[.0005313079364639922,-.0005921664373536939,.0002708782096718045,7902353232660328e-22,-8153969367561969e-20,561168275310625e-19,-18329116582843375e-21,-30796134506033047e-25,3465155368803609e-21,-20291327396058603e-22,5788792863149004e-22,2338630673826657e-28,-8828600746330484e-23,47435958880408125e-24,-12545415020710382e-24,8649648858010293e-29,16846058979264062e-25,-8575492823577594e-25,21598224929232125e-26,-7613230520476153e-31,-26639822008536144e-27,13065700536611057e-27,-31799163902367977e-28,4710976121367431e-33,36902800842763465e-29],[.00034436760689237765,5171790908260592e-20,-.00033493161081142234,.0002812695154763237,-.00010976582244684731,-12741009095484485e-23,27744451511563645e-21,-18263488805711332e-21,57876949497350525e-22,493875893393627e-24,-10595367014026043e-22,6166714376110408e-22,-17562973359060463e-23,-1297447328701544e-27,2695423606288966e-23,-14578352908731272e-24,3887645959386175e-24,-3881002251019412e-32,-5327994173877286e-25,27437977643314844e-26,-6995796092070568e-26,2589986387486848e-32,8856689099669639e-27,-4403168815871311e-27,10865561947091654e-28],[-.0006526239185953094,.0008394987206720873,-.000438297098541721,-6969091458420552e-22,.00016644846642067547,-.00012783517679769218,4629953263691304e-20,4557909867922708e-24,-10595271125805195e-21,6783342904865167e-21,-21075476666258803e-22,-17213731432817144e-27,3773587741611098e-22,-21867506700122867e-23,6220228804018927e-23,6597703826733e-28,-9590386497425686e-24,5213214492280807e-24,-13991589583935709e-25,5382058999060575e-31,19484714275467745e-26,-10127287556389682e-26,26077347197254926e-27,-5090418699993299e-33,-33721464474854593e-28],[-.0005967612901927463,-7204895416020011e-20,.0006782308837667328,-.0006401475260262758,.00027750107634328704,1819700838046515e-22,-8479507117068503e-20,6105192082501531e-20,-21073920183404862e-21,-8858589014125599e-25,45284535953805374e-22,-28427815022504407e-22,8708234177864641e-22,36886101871706966e-28,-1534469519070206e-22,8862466778790695e-23,-25184812301826817e-24,-10225912098215092e-30,3896947075815478e-24,-21267304792235634e-25,5737013552805138e-25,-1887749850169741e-34,-8093153869465787e-26,423827232834492e-25,-11002224534207727e-27],[.0013324454494800656,-.0019144384985654776,.0011089369134596636,99324041226423e-20,-.0005087450129309319,.00042735056665392886,-.000168588537679108,-81301893922785e-22,45284402370562144e-21,-3127053674781734e-20,1044986828530338e-20,48435226265680926e-27,-2148256587345626e-21,1329369701097492e-21,-4029569309210103e-22,-1756787766632329e-28,7014504316366825e-23,-4040787734999483e-23,11474026743371962e-24,39642746853563326e-34,-17804938269892715e-25,9748026254873165e-25,-26405338676507616e-26,5794875163403742e-33,3764774955354384e-26],[.001579727660730835,.00016251626278391583,-.0020633421035543276,.00213896861856891,-.0010108559391263003,-399127055299192e-21,.0003623502508476469,-.00028143901463712157,.00010449513336495887,212114184918303e-23,-25779417251947842e-21,17281818956040464e-21,-5641377387290428e-21,-11024320105776174e-27,11223224418895176e-22,-6869339637952674e-22,20653236975414888e-23,46714772409838506e-30,-35609886164949055e-24,20470855345905963e-24,-5809173863328336e-24,-1332821287582869e-31,9035460439133513e-25,-4959878251733084e-25,13481607129399748e-26],[-.004072512119514016,.00640336283380807,-.004041016108167662,-2183732802866233e-21,.002174044180125464,-.001970044051841889,.0008359546974796246,19445447567109655e-24,-.000257793871204217,.00019009987368139304,-6769649993743896e-20,-14440629666426571e-26,15712512518742267e-21,-10304008744776892e-21,3304517767401387e-21,7982976024232571e-28,-64097794149313e-20,38894624761300054e-23,-1161834764494887e-22,-2816808630596451e-30,19878012911297094e-24,-11407719956357511e-24,32355857064185554e-25,4175946829345594e-35,-5042311271810582e-25],[-.0059475779383993,-.0005401647678926045,.00879104135507679,-.009857631558785612,.005013469503102154,12807521786221875e-22,-.0020626019342754685,.0017109128573523059,-.000676953127141338,-6901154567656214e-24,.00018855128143995903,-.0001339521566349197,4626318303352804e-20,4003423061332135e-26,-10255652921494033e-21,6612086372797651e-21,-20913022027253007e-22,-20951775649603836e-29,3975602904199325e-22,-2395621197881589e-22,7118288338214586e-23,8925574873053455e-31,-12101547235064675e-24,6935061824833439e-24,-196614644538561e-23],[.01740202778752271,-.029527880945699123,.020045875571402798,70289515966903405e-22,-.012375421071343148,.011976293444235253,-.0054156038466518525,-6329089339641862e-23,.0018855118129005065,-.001473473274825001,.0005551581009770838,5240683441255066e-25,-.00014357913535784837,991812932249433e-19,-3346083474947831e-20,-3575583729109899e-27,71560851960630075e-22,-4551680262815553e-21,14236576649271474e-22,18803149082089665e-30,-2662340389892921e-22,15950642189595716e-23,-471875146738411e-22,-6510787295875518e-32,7979509102674624e-24],[.03024912416090589,.0024817436002649977,-.049939134373457025,.05991564300930787,-.03248320760162339,-5721296865210344e-21,.015085251778569354,-.013261324005088445,.0055515262632426145,3026318225703001e-23,-.0017229548406756724,.0012893570099929638,-.00046845138348319875,-1830259937893045e-25,.00011449739014822654,-7737856522124447e-20,25625836246985202e-21,10766165333192815e-28,-5324680928242262e-21,3349634863064464e-21,-10381253128684019e-22,-5608909920621128e-30,19150821930676592e-23,-11418365800203487e-23,33654425209171787e-24],[-.09905102088015905,.17954011706123485,-.12989606383463778,-31478872752284355e-21,.09051063527684813,-.0928288244111844,.04441211283987781,27779236316835886e-23,-.017229543805449696,.014182925050891573,-.005621416163374734,-239598509186381e-23,.0016029634366079909,-.0011606784674435774,.00041001337768153875,1836580075409066e-26,-958442565636559e-19,6364306233776471e-20,-2076250624489065e-20,-11806020912804483e-29,4213180823912065e-21,-2626224133701247e-21,8077062049493066e-22,6012591212363273e-31,-1472973737401884e-22],[-.19994542198219728,-.015056113040026424,.36470239469348487,-.4643519231173355,.26640934719197895,3403826602714719e-20,-.13784338709329624,.1276467178337056,-.056213828755200985,-1753150885483011e-22,.019235592956768112,-.015088821281095314,.005740185445135012,10622382710310225e-25,-.0015335082692563998,.0010819320643228215,-.0003737251019394566,-6617090972903199e-27,8426361738090962e-20,-5515070682748348e-20,1776953644834807e-20,3882792321020553e-29,-353513697488768e-20,2186583213004527e-21,-668128494476256e-21],[.7243860850402943,-1.3918010932653375,1.0654143352413967,.0001876173868950258,-.827055011761527,.8935243334782842,-.44971003995291337,-16107401567546651e-22,.1923559016527109,-.1659770216004261,.06888222268181433,13910091724608687e-24,-.02146911561508663,.016228980898865892,-.005979601617258426,-11287469112826745e-26,.0015167451119784856,-.00104786342935539,.0003553907288912642,8170432211180152e-28,-777730134424524e-19,5029141389700772e-20,-16035083867000518e-21,12469354315487606e-30,31369106244517616e-22],[1.6668949727276812,.1165462765994632,-3.3288393225018904,4.469232548286404,-2.6977693045875806,-.0002600667859891061,1.5389017615694538,-1.4937962361134611,.6888196463323315,13077482004552384e-22,-.2576296332559629,.21097676102125448,-.08371440835921988,-7792042888135476e-24,.0242679230648336,-.017813678334552312,.006397033038890006,4943080709048052e-26,-.0015554602758465635,.0010561196919903215,-.000352771844604729,9300233464502246e-29,7528585502655717e-20,-4818651556915635e-20,15227271505597605e-21],[-6.618829886137293,13.397985455142589,-10.789350606845145,-.0014352254537875018,9.23336945961898,-10.456552819547769,5.510552602903347,12024439690716742e-21,-2.5762961164755818,2.320744274538718,-1.0045728797216285,-10207833290021913e-23,.3397509217116947,-.2672051745075747,.10235252851562705,8432973048487163e-25,-.027998284958442594,.020066274144976814,-.007055436891508624,19402238183698188e-28,.0016562888105449611,-.0011082898580743682,.0003654545161310169,-5129003202697179e-26,-7634010369686903e-20],[-17.112706061976095,-1.1208044642899115,37.131966511885445,-52.29827102534896,33.058589696624615,.002479129897620022,-20.61089403411526,20.88672775145582,-10.045703956517752,-12238783449063012e-21,4.077013427422114,-3.473667358470195,1.4329352617312006,7135991441187971e-23,-.4479725715911561,.3411266608064446,-.12699786326594922,-2895367726908153e-25,.03312577627825986,-.023274087021036102,.008039999350364889,-1177805216235265e-24,-.001832162489107167,.0012108282933588664,-.0003947994124682252],[73.89033153567425,-156.80141270402274,132.2177542759164,.013692876877324546,-123.66496885920151,146.2068939106273,-80.36558772486535,-.00011259851148881298,40.77013219617994,-38.21034001327303,17.19522294277362,9351970795516835e-22,-6.271615990774704,5.116899907185264,-2.0319658112299095,-4950721558276154e-24,.596263972943326,-.44220765337238094,.16079998700166273,-24733786203223403e-24,-.040307574759979765,.02784905074709787,-.009475185899205422,6419922235909132e-21,.002125018077469946],[212.1683709838252,13.107863022633868,-496.9828593287175,731.215952669692,-482.1382172089085,-.028817248692894887,326.167203029471,-343.8934028008712,171.95193870816232,.00014038077378096157,-75.2594195897599,66.51969984520935,-28.447519748152462,-7613702615875392e-22,9.540223710530437,-7.517530111331138,2.894399756887196,-466121949995382e-21,-.8061514959879409,.5848300657063102,-.20845408972964957,.00014765818959305816,.05100043386375302,-.03306625214188366,.015109265210467774],[-989.5964309832236,2192.555536090523,-1928.3586782723355,-.15925738122215252,1956.9985945919857,-2407.2514765081555,1375.6149959336497,.0012920735237496668,-752.5941715948055,731.7166874220871,-341.37023466220063,-9985739026060805e-21,133.56313181291574,-112.76295161252794,46.31039609820446,-7923738713361476e-21,-14.510726927018647,11.111771248100563,-4.169081794527089,.0031008219800117806,1.1220095449981469,-.7605237992614992,.36262236505085255,.2216867741940747,.48683443692930506]];function M1(e,t){return Number.isNaN(e)||Number.isNaN(t)||t<0?Number.NaN:e<0?1:e===Number.POSITIVE_INFINITY?0:Math.min(1,Math.max(0,qm(Math.floor(e),t)))}function qm(e,t){return e<0||t<0?Number.NaN:t===0?0:A9(Math.floor(e)+1,t)}const sn=11102230246251565e-32,I1=.9189385332046728,Um=2556348e299,Gm=1.4142135623730951,Hm=.7071067811865476,Qm=500,fi=2e3,x1=1,Km=0,Vm=20,bc=200,Ym=.3,Wm=4.5,Xm=4503599627370496,hs=2220446049250313e-31;function Ae(e,t,n){let s=t[0];for(let o=1;o<=n;o++)s=s*e+t[o];return s}function Dn(e,t,n){let s=e+t[0];for(let o=1;o<n;o++)s=s*e+t[o];return s}function Jm(e,t,n,s,o){const r=Math.abs(e);let i,a,c;r>1?(i=-1,a=n,c=1/e):(i=1,a=0,c=e);let l=t[a];a+=i;for(let h=1;h<=n;h++)l=l*c+t[a],a+=i;a=r>1?o:0;let u=s[a];a+=i;for(let h=1;h<=o;h++)u=u*c+s[a],a+=i;return r>1?Math.pow(e,n-o)*l/u:l/u}const Zm=[.0008116141674705085,-.0005950619042843014,.0007936503404577169,-.002777777777300997,.08333333333333319],jm=[-1378.2515256912086,-38801.631513463784,-331612.9927388712,-1162370974927623e-9,-1.7217370082083966e6,-853555.6642457654],t9=[-351.81570143652345,-17064.210665188115,-220528.59055385445,-1.1393344436798252e6,-2.5325230717758294e6,-2.0188914143353277e6];function e9(e){const t=(e-.5)*Math.log(e)-e+I1;if(e>1e8)return t;let n=1/(e*e);return n=((.0007936507936507937*n-.002777777777777778)*n+.08333333333333333)/e,t+n}function di(e){if(!Number.isFinite(e))return e;if(e<=0)throw new RangeError(`lgam is ported for positive arguments only, not ${e}`);if(e<13){let s=1,o=0,r=e;for(;r>=3;)o-=1,r=e+o,s*=r;for(;r<2;)s/=r,o+=1,r=e+o;if(s<0&&(s=-s),r===2)return Math.log(s);o-=2;const i=e+o;return o=i*Ae(i,jm,5)/Dn(i,t9,6),Math.log(s)+o}if(e>Um)return Number.POSITIVE_INFINITY;if(e>=1e3)return e9(e);const t=(e-.5)*Math.log(e)-e+I1,n=1/(e*e);return t+Ae(n,Zm,4)/e}function n9(e){if(e===1)return 0;if(Math.abs(e)<=.5||Math.abs(e-1)<.5)throw new RangeError(`lgam1p is ported for x = 1 and x ≥ 1.5 only, not ${e}`);return di(e+1)}const Xo=6.02468004077673,s9=[.006061842346248907,.5098416655656676,19.519927882476175,449.9445569063168,6955.999602515376,75999.29304014542,601859.6171681099,3481712154980646e-9,14605578087685067e-9,4333888932467614e-8,863631312881386e-7,10379404311634454e-8,56906521913471565e-9],o9=[1,66,1925,32670,357423,2637558,13339535,45995730,105258076,150917976,120543840,39916800,0];function r9(e){return Jm(e,s9,12,o9,12)}const i9=[452700008624452e-19,.49854102823193375,6.578732594206104,29.911919328553072,60.94966798098779,57.11296359058554,20.039553499201283],a9=[15.062909083469192,83.04756596796722,221.76239823732857,309.09872225312057,216.42788614495947,60.11866049760384],c9=[.00012617719307481058,.030299440770744195,1],l9=[30019850513866446e-22,.002524483403496841,.22726554820815503,2];function u9(e){let t=1+e;return t<Hm||t>Gm?Math.log(t):(t=e*e,t=-.5*t+e*(t*Ae(e,i9,6)/Dn(e,a9,6)),e+t)}function Ir(e){if(Math.abs(e)<.5){let t=e,n=0;for(let s=2;s<Qm;s++){t*=-e;const o=t/s;if(n+=o,Math.abs(o)<sn*Math.abs(n))break}return n}return u9(e)-e}function h9(e){if(!Number.isFinite(e))return Number.isNaN(e)||e>0?e:-1;if(e<-.5||e>.5)return Math.exp(e)-1;const t=e*e;let n=e*Ae(t,c9,2);return n=n/(Ae(t,l9,3)-n),n+n}const f9=[2461969814735305e-25,.5641895648310689,7.463210564422699,48.63719709856814,196.5208329560771,526.4451949954773,934.5285271719576,1027.5518868951572,557.5353353693994],d9=[13.228195115474499,86.70721408859897,354.9377788878199,975.7085017432055,1823.9091668790973,2246.3376081871097,1656.6630919416134,557.5353408177277],m9=[.5641895835477551,1.275366707599781,5.019050422511805,6.160210979930536,7.4097426995044895,2.9788666537210022],g9=[2.2605286322011726,9.396035249380015,12.048953980809666,17.08144507475659,9.608968090632859,3.369076451000815],p9=[9.604973739870516,90.02601972038427,2232.005345946843,7003.325141128051,55592.30130103949],w9=[33.56171416475031,521.3579497801527,4594.323829709801,22629.000061389095,49267.39426086359];function v1(e){if(Number.isNaN(e))return Number.NaN;if(e<0)return-v1(-e);if(Math.abs(e)>1)return 1-k1(e);const t=e*e;return e*Ae(t,p9,4)/Dn(t,w9,5)}function k1(e){if(Number.isNaN(e))return Number.NaN;const t=e<0?-e:e;if(t<1)return 1-v1(e);let n=-e*e;if(n<-709.782712893384)return e<0?2:0;n=Math.exp(n);let s,o;t<8?(s=Ae(t,f9,8),o=Dn(t,d9,8)):(s=Ae(t,m9,5),o=Dn(t,g9,6));let r=n*s/o;return e<0&&(r=2-r),r!==0?r:e<0?2:0}function S1(e,t){if(Math.abs(e-t)>.4*Math.abs(e)){const o=e*Math.log(t)-t-di(e);return o<-709.782712893384?0:Math.exp(o)}const n=e+Xo-.5;let s=Math.sqrt(n/Math.exp(1))/r9(e);if(e<200&&t<200)s*=Math.exp(e-t)*Math.pow(t/n,e);else{const o=t-e-Xo+.5;s*=Math.exp(e*Ir(o/n)+t*(.5-Xo)/n)}return s}function b9(e,t){const n=S1(e,t);if(n===0)return 0;let s=1-e,o=t+s+1,r=0,i=1,a=t,c=t+1,l=o*t,u=c/l;for(let h=0;h<fi;h++){r+=1,s+=1,o+=2;const f=s*r,d=c*o-i*f,m=l*o-a*f;let g;if(m!==0){const p=d/m;g=Math.abs((u-p)/p),u=p}else g=1;if(i=c,c=d,a=l,l=m,Math.abs(d)>Xm&&(i*=hs,c*=hs,a*=hs,l*=hs),g<=sn)break}return u*n}function ys(e,t){const n=S1(e,t);if(n===0)return 0;let s=e,o=1,r=1;for(let i=0;i<fi&&(s+=1,o*=t/s,r+=o,!(o<=sn*r));i++);return r*n/e}function Ac(e,t){let n=1,s=0;for(let i=1;i<fi;i++){n*=-t/i;const a=n/(e+i);if(s+=a,Math.abs(a)<=sn*Math.abs(s))break}const o=Math.log(t);return-h9(e*o-n9(e))-Math.exp(e*o-di(e))*s}function C1(e,t,n){const s=Wo.length,o=Wo[0].length;let r=0;const i=t/e,a=(t-e)/e,c=new Float64Array(o);c[0]=1;let l=0,u=1,h=Number.POSITIVE_INFINITY;const f=n===x1?-1:1;let d;i>1?d=Math.sqrt(-2*Ir(a)):i<1?d=-Math.sqrt(-2*Ir(a)):d=0;let m=.5*k1(f*d*Math.sqrt(e/2));for(let g=0;g<s;g++){const p=Wo[g];let w=p[0];for(let A=1;A<o;A++){A>r&&(c[A]=d*c[A-1],r+=1);const N=p[A]*c[A];if(w+=N,Math.abs(N)<sn*Math.abs(w))break}const b=w*u,y=Math.abs(b);if(y>h||(l+=b,y<sn*Math.abs(l)))break;h=y,u/=e}return m+=f*Math.exp(-.5*e*d*d)*l/Math.sqrt(2*Math.PI*e),m}function F1(e,t){const n=Math.abs(t-e)/e;return e>Vm&&e<bc&&n<Ym||e>bc&&n<Wm/Math.sqrt(e)}function A9(e,t){return Number.isNaN(e)||Number.isNaN(t)||t<0||e<0?Number.NaN:e===0?t>0?1:Number.NaN:t===0?0:Number.isFinite(e)?Number.isFinite(t)?F1(e,t)?C1(e,t,x1):t>1&&t>e?1-y9(e,t):ys(e,t):1:Number.isFinite(t)?0:Number.NaN}function y9(e,t){return Number.isNaN(e)||Number.isNaN(t)||t<0||e<0?Number.NaN:e===0?t>0?0:Number.NaN:t===0?1:Number.isFinite(e)?Number.isFinite(t)?F1(e,t)?C1(e,t,Km):t>1.1?t<e?1-ys(e,t):b9(e,t):t<=.5?-.4/Math.log(t)<e?1-ys(e,t):Ac(e,t):t*1.1<e?1-ys(e,t):Ac(e,t):0:Number.isFinite(t)?1:Number.NaN}function N9(e){if(!Number.isInteger(e)||e<pc||e>wc)throw new RangeError(`cooltools takes ${pc} to ${wc} λ-chunks, not ${e}`);return Float64Array.from(Pm.slice(0,e))}function co(e){const t=o=>{const r=Math.round(o);return r-o===.5&&r%2!==0?r-1:r},n=(o,r)=>{if(!Number.isFinite(o)||o===0)return o;const i=Math.trunc(o),a=i===0?-Math.floor(Math.log10(Math.abs(o-i)))-1+r:r,c=Math.pow(10,Math.abs(a));return a>=0?t(o*c)/c:t(o/c)*c};let s=3;for(let o=3;o<20;o++){const r=Array.from(e,i=>n(i,o));if(new Set(r).size===e.length){s=o;break}}return Float64Array.from(e,o=>n(o,s))}function qe(e,t){const n=t.length;if(!(e>t[0]))return 0;$1[0]=e;let s=3*((M9[I9]>>>20&2047)-1023);for(s>n&&(s=n),s<1&&(s=1);s>0&&t[s-1]>=e;)s--;for(;s<n&&t[s]<e;)s++;return s}const $1=new Float64Array(1),M9=new Uint32Array($1.buffer),I9=new Uint8Array(new Uint16Array([1]).buffer)[0]===1?1:0,x9=2;function v9(e,t,n=x9){const{n:s,hi:o,values:r}=e,i=new Float64Array(o+1),a=new Float64Array(o+1),c=new Float64Array(o+1);for(let l=0;l<=o;l++){let u=0,h=0,f=0;for(let d=0;d+l<s;d++){const m=t[d],g=t[d+l];if(Number.isNaN(m)||Number.isNaN(g))continue;f++;const p=r[l*s+d];if(p===0||Number.isNaN(p))continue;const w=p*m*g-h,b=u+w;h=b-u-w,u=b}a[l]=f,c[l]=u,i[l]=l<n?Number.NaN:u/f}return{avg:i,nValid:a,sum:c}}const k9=256;async function S9(e,t,n,s,o,r,i){const{n:a,hi:c,values:l}=e,{w:u}=s,h=hi.map(E=>zm(s,E)),f=Math.min(o,a),d=new Int32Array(a+1);for(let E=0;E<a;E++)d[E+1]=d[E]+(Number.isNaN(t[E])?1:0);const m=E=>E-u>=0&&E+u<a&&d[E+u+1]-d[E-u]===0;let g=0,p=0;for(const E of h)for(const O of E.dd)g=Math.min(g,O),p=Math.max(p,O);const w=h.map(()=>new Float64Array(f)),b=new Uint8Array(f);for(let E=1;E<f;E++){if(E+g<0||E+p>c)continue;let O=Number.isFinite(n[E]);for(let L=0;O&&L<h.length;L++){const D=h[L].dd;let q=0;for(let X=0;X<D.length;X++){const R=n[E+D[X]];if(!Number.isFinite(R)){O=!1;break}q+=R}w[L][E]=q}b[E]=O?1:0}const y=2*u+1,A=c+1,N=new Float64Array(y*A),M=E=>{const O=E%y*A,L=t[E];for(let D=0;D<=c;D++){const q=E+D;N[O+D]=q<a?l[D*a+E]*(L*t[q]):Number.NaN}},I=new Int32Array(4);let x=0;h.forEach((E,O)=>I[O]=x+=E.dr.length);const v=new Int32Array(x),S=new Int32Array(x);h.forEach((E,O)=>{const L=O===0?0:I[O-1];v.set(E.dr,L),S.set(E.dd,L)});const k=new Int32Array(x),C=new Float64Array(4*f);for(let E=0;E<4;E++)C.set(w[E],E*f);const B=new Uint8Array(a);for(let E=0;E<a;E++)B[E]=m(E)?1:0;const F=new Int32Array(y),$=new Float64Array(16);let _=0;for(let E=0;E<Math.min(a,u);E++)M(E);for(let E=0;E<a;E++){if(E+u<a&&M(E+u),i!==void 0&&E%k9===0&&await i(E),B[E]===0)continue;for(let P=-u;P<=u;P++)F[P+u]=(E+P)%y*A;for(let P=0;P<x;P++)k[P]=F[v[P]+u]+S[P];const O=t[E],L=Math.min(f-1,a-1-u-E),D=Math.max(1,-g),q=Math.min(L,c-p),X=(P,z,U,K,G)=>{if(b[P]===0||B[E+P]===0||!Number.isFinite(z)||!Number.isFinite(U)||!Number.isFinite(K)||!Number.isFinite(G))return;const Y=n[P]/(O*t[E+P]);_++,r(E,E+P,l[P*a+E],Y*z,Y*U,Y*K,Y*G)};let R=D;for(;R+3<=q;R+=4){let P=0;for(let z=0;z<4;z++){const U=I[z];let K=0,G=0,Y=0,V=0;for(;P<U;P++){const Z=k[P]+R;K+=N[Z],G+=N[Z+1],Y+=N[Z+2],V+=N[Z+3]}const J=z*f+R;$[z]=K/C[J],$[4+z]=G/C[J+1],$[8+z]=Y/C[J+2],$[12+z]=V/C[J+3]}X(R,$[0],$[1],$[2],$[3]),X(R+1,$[4],$[5],$[6],$[7]),X(R+2,$[8],$[9],$[10],$[11]),X(R+3,$[12],$[13],$[14],$[15])}for(;R<=q;R++){let P=0;for(let z=0;z<4;z++){const U=I[z];let K=0;for(;P<U;P++)K+=N[k[P]+R];$[z]=K/C[z*f+R]}X(R,$[0],$[1],$[2],$[3])}}return _}const yc=16384;class C9{constructor(t){this.edges=t,this.chunks=t.length+1}chunks;dense=new Float64Array(0);denseRows=0;sparse=new Map;row(t){if(Number.isInteger(t)&&t>=0&&t<yc){if(t>=this.denseRows){const s=Math.min(yc,Math.max(t+1,this.denseRows*2,64)),o=new Float64Array(s*4*this.chunks);o.set(this.dense),this.dense=o,this.denseRows=s}return{cells:this.dense,at:t*4*this.chunks}}let n=this.sparse.get(t);return n===void 0&&(n=new Float64Array(4*this.chunks),this.sparse.set(t,n)),{cells:n,at:0}}add(t,n,s,o,r){const i=this.chunks;let a,c;if(Number.isInteger(t)&&t>=0&&t<this.denseRows)a=this.dense,c=t*4*i;else{const l=this.row(t);a=l.cells,c=l.at}a[c+n]++,a[c+i+s]++,a[c+2*i+o]++,a[c+3*i+r]++}table(){const t=[];for(let s=0;s<this.denseRows;s++){const o=s*4*this.chunks;let r=!1;for(let i=0;i<this.chunks&&!r;i++)r=this.dense[o+i]>0;r&&t.push(s)}for(const[s,o]of this.sparse){let r=!1;for(let i=0;i<this.chunks&&!r;i++)r=o[i]>0;r&&t.push(s)}t.sort((s,o)=>s-o);const n=hi.map(()=>new Float64Array(t.length*this.chunks));return t.forEach((s,o)=>{const{cells:r,at:i}=this.row(s);for(let a=0;a<4;a++)n[a].set(r.subarray(i+a*this.chunks,i+(a+1)*this.chunks),o*this.chunks)}),{counts:Float64Array.from(t),cells:n}}}function F9(e,t){const{counts:n,cells:s}=e.table(),o=e.chunks,r=n.length,i=co(e.edges);let a=0;for(let d=0;d<4;d++){let m=0;for(let g=0;g<r;g++)m+=s[d][g*o+o-1];a=Math.max(a,m)}const c=r===0?1:n[r-1]+1,l=new Map;n.forEach((d,m)=>l.set(d,m));const u=new Array(o).fill(null),h=d=>{let m=u[d];if(m===null){m=new Float64Array(r);const g=i[d];let p=!1;for(let w=0;w<r;w++)m[w]=p?0:M1(n[w],g),m[w]===0&&(p=!0);u[d]=m}return m};return{kernels:s.map(d=>{let m=-1;for(let b=0;b<o;b++)for(let y=0;y<r;y++)if(d[y*o+b]>0){m=b;break}const g=Math.max(0,Math.min(m+1,o-1)),p=new Float64Array(g),w=new Float64Array(r*g);for(let b=0;b<g;b++){const y=new Float64Array(r);let A=0;for(let S=r-1;S>=0;S--)A+=d[S*o+b],y[S]=A;const N=r===0?0:y[0],M=h(b);let I=Number.NEGATIVE_INFINITY,x=Number.NaN,v=Number.NaN;for(let S=0;S<r;S++){const k=N*M[S],C=t*y[S]-k;C>I&&(I=C),Number.isNaN(x)&&I>=0&&(x=n[S]);const B=k/y[S];!Number.isNaN(B)&&!(B>=v)&&(v=B);const F=Number.isNaN(B)?Number.NaN:v;w[S*g+b]=F>1?1:F}p[b]=Math.trunc(Number.isNaN(x)?c:x)}return{thresholds:p,qvalue(b,y){const A=l.get(b);return A===void 0||y>=g?Number.NaN:w[A*g+y]}}}),counts:n,overflow:a}}function $9(e,t){const n=co(e),s=new Float64Array(e.length+1).fill(Number.POSITIVE_INFINITY),o=t*(1+1e-9);for(let r=0;r<n.length;r++){let i=0;for(;M1(i,n[r])>o;)i++;s[r]=i}return s}function E9(e,t,n){const s=new Map;for(const i of e){let a=s.get(i.sequence);a===void 0&&s.set(i.sequence,a=[]),a.push(i)}const o=[],r=[];for(const[,i]of[...s].sort((a,c)=>a[0]-c[0])){const a=new Float64Array(i.length*2);i.forEach((f,d)=>{a[2*d]=f.bin1*t,a[2*d+1]=f.bin2*t});const{labels:c,centers:l}=Dm(a,n),u=new Map;for(const f of c)u.set(f,(u.get(f)??0)+1);const h=new Map;i.forEach((f,d)=>{const m=c[d],g={...f,cLabel:m,cSize:u.get(m)??0,cStart1:l[2*m],cStart2:l[2*m+1]};o.push(g);const p=h.get(m);(p===void 0||g.count>p.count)&&h.set(m,g)}),r.push(...[...h.values()].sort((f,d)=>f.cLabel-d.cLabel))}return{pixels:o,centroids:r}}const ve={vh:1.5,dAndLl:1.75,dOrLl:2,orphanFdr:.02};function B9(e){const[t,n,s,o]=e.laExp,[r,i,a,c]=e.qval,l=e.count;return l>ve.dAndLl*o&&l>ve.dAndLl*t&&l>ve.vh*n&&l>ve.vh*s&&(l>ve.dOrLl*o||l>ve.dOrLl*t)&&(e.cSize>1||c+r+i+a<=ve.orphanFdr)}class ge{values;length=0;constructor(t=1024){this.values=new Float64Array(t)}push(t){if(this.length===this.values.length){const n=new Float64Array(this.values.length*2);n.set(this.values),this.values=n}this.values[this.length++]=t}}class T9{constructor(t){this.settings=t,this.edges=N9(t.lambdaBins),this.histogram=new C9(this.edges),this.floors=$9(this.edges,t.fdr)}edges;histogram;floors;scored=0;sequence=new ge;row=new ge;col=new ge;count=new ge;la=[new ge,new ge,new ge,new ge];async addSequence(t,n,s,o,r){const{edges:i,floors:a,histogram:c}=this,l=await S9(n,s,o,this.settings.shape,this.settings.maxSeparation,(u,h,f,d,m,g,p)=>{const w=qe(d,i),b=qe(m,i),y=qe(g,i),A=qe(p,i);c.add(f,w,b,y,A);const N=Math.floor(f);N<a[w]||N<a[b]||N<a[y]||N<a[A]||(this.sequence.push(t),this.row.push(u),this.col.push(h),this.count.push(f),this.la[0].push(d),this.la[1].push(m),this.la[2].push(g),this.la[3].push(p))},r);return this.scored+=l,l}finish(t){const{kernels:n,overflow:s}=F9(this.histogram,this.settings.fdr),o=n.map(d=>d.thresholds),r={scored:this.scored,overflow:s,unlabelled:0,thresholds:o,enriched:[],clustered:[],calls:[]};if(s>0||this.scored===0)return r;const i=[],a=co(this.edges);let c=0;for(let d=0;d<this.count.length;d++){const m=this.count.values[d];let g=!0;for(let y=0;y<4&&g;y++){const A=n[y].thresholds[qe(this.la[y].values[d],this.edges)];g=A!==void 0&&m>=A}if(!g)continue;const p=[this.la[0].values[d],this.la[1].values[d],this.la[2].values[d],this.la[3].values[d]],w=[0,0,0,0];let b=!0;for(let y=0;y<4&&b;y++){const A=qe(p[y],a);b=A<n[y].thresholds.length,b&&(w[y]=n[y].qvalue(m,A))}if(!b){c++;continue}i.push({sequence:this.sequence.values[d],bin1:this.row.values[d],bin2:this.col.values[d],count:m,laExp:p,qval:w})}const l=this.settings.clusteringRadius;if(!(l>0)){const d=i.map(m=>({...m,cLabel:-1,cSize:1,cStart1:Number.NaN,cStart2:Number.NaN}));return{...r,unlabelled:c,enriched:i,clustered:d,calls:d}}const{pixels:u,centroids:h}=E9(i,t,l),f=this.settings.clusterFiltering?h.filter(B9):h;return{...r,unlabelled:c,enriched:i,clustered:u,calls:f}}}const O9=1e7,L9=2e4,_9=.1,D9=40,R9=96e6;function z9(e,t){return t>0?Math.trunc(e/t):0}function P9(e,t){const n=N1(t),s=(i,a)=>{const c=e[i];return typeof c=="number"&&Number.isFinite(c)?Math.round(c):a??null},o=s("outer",n?.w),r=s("inner",n?.p);return o===null||r===null?null:{w:o,p:r}}function q9(e,t){const n=e.maxSeparation;return typeof n=="number"&&Number.isFinite(n)?Math.max(1,Math.round(n)):Math.max(1,z9(O9,t))}function U9(e,t){const n=e.clusteringRadius;if(!(typeof n=="number"&&Number.isFinite(n)))return L9;const s=Math.max(0,n)*t,o=Math.round(s);return Math.abs(s-o)<1e-6?o:s}function E1(e,t,n){return Math.max(0,Math.min(t-1+2*n,e-1))}const B1=["donut","vertical","horizontal","lower-left"],G9=async e=>{const t=e.mc.binsize;if(!nt(e.params,"balance",!0))throw new T("unsupported","Loop calling needs a balanced map: its local expected is built from the balanced map and scaled back to raw counts for the Poisson test. Run it with the weights on.");const n=P9(e.params,t);if(n===null)throw new T("unsupported",`There are kernels for bins of 4 to 28 kb only, and this map’s are ${t.toLocaleString()} bp. Give the kernel’s two half-widths (--outer and --inner): w 7, p 4 is the kernel for 5 kb bins, and w 5, p 2 the one for 10 kb.`);if(n.p>=n.w)throw new T("unsupported","The inner half-width must be smaller than the outer one.");const s=q9(e.params,t),o=U9(e.params,t),r={shape:n,maxSeparation:s,lambdaBins:Math.round(H(e.params,"lambdaBins",D9)),fdr:H(e.params,"fdr",_9),clusteringRadius:o,clusterFiltering:nt(e.params,"filter",!0)},i=N1(t);e.note(i!==null&&i.w===n.w&&i.p===n.p?`Kernels w ${n.w}, p ${n.p}: the ones for ${rt(t)} bins.`:`Kernels w ${n.w}, p ${n.p}`+(i===null?`, given for ${rt(t)} bins, which have none of their own.`:`; the ones for ${rt(t)} bins are w ${i.w}, p ${i.p}.`)),e.note(`A ${s.toLocaleString()}-bin scan reaches ${rt(s*t)} from the diagonal at this file’s ${t.toLocaleString()} bp resolution, and the nearest pixel a kernel can be read around is ${(2*n.w+2).toLocaleString()} bins out.`);const{units:a,skipped:c}=H9(e,s,n.w);if(c.length>0&&e.note(`${c.length} sequence${c.length===1?"":"s"} would need a band bigger than a scan can hold, and ${c.length===1?"was":"were"} skipped: ${Q9(c)}. Bring “furthest from the diagonal” down, or open the file at a coarser resolution.`),a.length===0)return e.note("No sequence to scan."),[];const l=Qt(e.mc,!0),u=Ct(e,a.map(g=>g.span),!1),h=new T9(r);for(const[g,p]of a.entries()){await e.breathe();const w=p.span.to-p.span.from,b=await De(e,p.span,{hi:E1(w,s,n.w),block:u,balance:!1,value:tt,onTile:async(N,M)=>{await e.breathe();const I=M===1?`${p.name} · reading`:`${p.name} · reading ${N+1} of ${M}`;e.progress(g,a.length,I)}}),y=l.subarray(p.span.from,p.span.to),{avg:A}=v9(b,y);await h.addSequence(g,b,y,A,async N=>{await e.breathe(),e.progress(g+N/Math.max(1,w),a.length,`${p.name} · scoring`)})}e.progress(a.length,a.length,"thresholds"),e.throwIfCancelled();const f=h.finish(t);if(f.overflow>0){const g=h.edges;return e.note(`${f.overflow.toLocaleString()} ${f.overflow===1?"pixel has":"pixels have"} a local expected past the last λ-chunk’s edge, ${g[g.length-1].toFixed(0)} contacts, and loop calling stops there rather than test them. Raise the λ-chunks: 50 reach 82,570.`),[]}if(f.scored===0)return e.note("No pixel could be scored: every one either sits within the kernel’s reach of the diagonal, of a sequence’s end or of a bin the balancer dropped. Narrow the kernel, or open a finer resolution."),[];if(e.note(`${f.scored.toLocaleString()} pixels scored over ${a.length.toLocaleString()} ${a.length===1?"sequence":"sequences"}; ${f.enriched.length.toLocaleString()} reached all four thresholds at an FDR of ${r.fdr}.`),f.unlabelled>0&&e.note(`${f.unlabelled.toLocaleString()} of them ${f.unlabelled===1?"was":"were"} left out for sitting a hair past the rounded label of the last λ-chunk their kernel had.`),f.enriched.length===0)return e.note("Nothing passed: no loop is called."),Nc(h,f);if(o>0){const g=new Set(f.clustered.map(p=>`${p.sequence}:${p.cLabel}`)).size;e.note(`${g.toLocaleString()} ${g===1?"cluster":"clusters"} at a ${rt(o)} radius`+(r.clusterFiltering?`; ${f.calls.length.toLocaleString()} passed the enrichment filter.`:", each reported at its brightest pixel, unfiltered."))}else e.note("Unclustered: every enriched pixel is a call, and none is filtered.");const d=[];f.calls.length>0&&d.push(K9(a,f.calls,t)),d.push(Y9(a,f.calls,t));const m=so(f.calls.map(g=>({separation:(g.bin2-g.bin1)*t,score:g.count/g.laExp[0]})),{id:"loops-spectrum",label:"Loop spectrum",noun:"loops",yLabel:"Observed over the donut’s expected"});return m!==null&&d.push(m),d.push(...Nc(h,f)),d};function H9(e,t,n){const s=e.mc,o=e.params.sequences,r=Array.isArray(o)?new Set(o.filter(c=>typeof c=="string")):null,i=[],a=[];for(const c of ao(s)){const l=s.chromnames[c];if(r!==null&&!r.has(l))continue;const u=Ut(s,c);if(u===null)continue;const h=u.to-u.from;if((E1(h,t,n)+1)*h>R9){a.push(l);continue}i.push({name:l,span:u,length:s.chromsizes.get(l)??h*s.binsize})}return{units:i,skipped:a}}function Q9(e){const t=e.slice(0,6).join(", ");return e.length>6?`${t} and ${e.length-6} more`:t}function K9(e,t,n){const s=new Kl;for(const o of t){const r=e[o.sequence],i=o.bin1*n,a=o.bin2*n;s.add(r.name,i,Math.min(r.length,i+n),r.name,a,Math.min(r.length,a+n),{score:o.count/o.laExp[0],name:"",detail:V9(o)})}return{kind:"intervals",id:"loops-calls",label:"Called loops",payload:{kind:"pairs",data:s.finish()},noun:{one:"loop",many:"loops"},display:"overlay"}}function V9(e){return`observed ${e.count} · `+B1.map((t,n)=>`${t} ${e.laExp[n].toFixed(2)} (q ${W9(e.qval[n])})`).join(" · ")+(e.cLabel>=0?` · cluster of ${e.cSize}`:"")}function Y9(e,t,n){const s=t.map(i=>({sequence:e[i.sequence].name,start1:i.bin1*n,start2:i.bin2*n,separation:(i.bin2-i.bin1)*n,count:i.count,enrichment:i.count/i.laExp[0],donut:i.laExp[0],vertical:i.laExp[1],horizontal:i.laExp[2],lowleft:i.laExp[3],qDonut:i.qval[0],qVertical:i.qval[1],qHorizontal:i.qval[2],qLowleft:i.qval[3],cluster:i.cLabel>=0?i.cSize:1})),o=(i,a)=>({key:i,label:`λ ${a}`,kind:"number",digits:4,hint:`Locally adjusted expected, ${a} kernel, in raw counts.`}),r=(i,a)=>({key:i,label:`q ${a}`,kind:"number",digits:4,scientific:!0,hint:`Benjamini–Hochberg within its λ-chunk, ${a} kernel.`});return{kind:"table",id:"loops-table",label:`${t.length.toLocaleString()} ${t.length===1?"loop":"loops"}`,columns:[{key:"sequence",label:"Sequence",kind:"text"},{key:"start1",label:"Start 1",kind:"number",coordinate:"start"},{key:"start2",label:"Start 2",kind:"number",coordinate:"start"},{key:"separation",label:"Separation (bp)",kind:"number",digits:0},{key:"count",label:"Observed",kind:"number",digits:0,hint:"Raw contacts in the pixel."},{key:"enrichment",label:"Enrichment",kind:"number",digits:4,hint:"Observed over the donut’s locally adjusted expected."},o("donut","donut"),o("vertical","vertical"),o("horizontal","horizontal"),o("lowleft","lower-left"),r("qDonut","donut"),r("qVertical","vertical"),r("qHorizontal","horizontal"),r("qLowleft","lower-left"),{key:"cluster",label:"Cluster",kind:"number",digits:0,hint:"Enriched pixels in its cluster."}],rows:s}}function Nc(e,t){const{counts:n,cells:s}=e.histogram.table(),o=e.histogram.chunks,r=co(e.edges),i=[];hi.forEach((c,l)=>{const u=t.thresholds[l],h=[],f=[];for(let d=0;d<u.length;d++){let m=Number.NEGATIVE_INFINITY;for(let g=n.length-1;g>=0;g--)if(s[l][g*o+d]>0){m=n[g];break}m>=u[d]&&(h.push(r[d]),f.push(u[d]))}i.push({name:B1[l],x:Float64Array.from(h),y:Float64Array.from(f),mark:"points"})});const a=Math.max(1,...i.flatMap(c=>Array.from(c.x)));return i.push({name:"observed = λ",x:Float64Array.from([1,a]),y:Float64Array.from([1,a]),mark:"line"}),[{kind:"curve",id:"loops-thresholds",label:"Contacts needed, by λ-chunk",series:i,xLabel:"Locally adjusted expected (λ), raw counts",yLabel:"Contacts a pixel must reach",xScale:"log",yScale:"log"}]}function W9(e){return Number.isFinite(e)?e===0?"0":e<1e-4?e.toExponential(2):e.toFixed(4):"n/a"}function X9(e,t){const n=e.n,s=[...new Set(t.quantiles)].filter(h=>h>0&&h<1).sort((h,f)=>h-f),o=Math.max(0,Math.trunc(t.ignoreDiags??1)),r=Math.max(1,o),i=Math.max(r,Math.min(Math.trunc(t.maxDistance),e.hi)),a=s.map(h=>({quantile:h,distance:new Float64Array(n).fill(Number.NaN)}));if(s.length===0||n===0||i<r)return a;const c=i-r+1,l=Math.max(1,Math.ceil(c*Math.max(0,Math.min(1,t.minValid)))),u=new Float64Array(c+1);for(let h=0;h<n;h++){let f=0,d=0;for(let p=0;p<c;p++){const w=r+p,b=e.forward(h,w),y=e.forward(h,-w),A=Number.isFinite(b),N=Number.isFinite(y);(A||N)&&d++,f+=(A?b:0)+(N?y:0),u[p+1]=f}const m=u[c];if(d<l||!(m>0))continue;let g=1;for(let p=0;p<s.length;p++){const w=s[p]*m;for(;g<c&&u[g]<w;)g++;const b=u[g-1],y=u[g],A=y>b?(w-b)/(y-b):0;a[p].distance[h]=r+Math.max(0,g-2+A)}}return a}const Mc={quartiles:[.25,.5,.75],standard:[.1,.25,.5,.75,.9],deciles:[.1,.2,.3,.4,.5,.6,.7,.8,.9]};function J9(e){const t=typeof e.quantiles=="string"?e.quantiles:"standard";return Mc[t]??Mc.standard}function T1(e){const t=typeof e.reach=="number"?e.reach:0;return Math.max(0,Math.round(t))}function Z9(e,t,n=!1){const s=n?Math.floor(t/2):Math.max(1,t-1),o=T1(e);return o===0?s:Math.max(1,Math.min(s,o))}const j9="Scalogram";function t7(e){const t=[8,48,107],n=[198,219,239],s=[];for(let o=0;o<e;o++){const r=e===1?0:o/(e-1),i=(u,h)=>Math.round(u+(h-u)*r),[a,c,l]=[i(t[0],n[0]),i(t[1],n[1]),i(t[2],n[2])];s.push(`#${[a,c,l].map(u=>u.toString(16).padStart(2,"0")).join("")}`)}return s}function e7(e,t){const n=e.mc,{ids:s,missing:o}=be(n,t),r=[];for(const i of s){const a=Ut(n,i);if(a===null)continue;const c=n.chromnames[i];r.push({name:c,span:a,circular:_e(e,c)})}return{units:r,missing:o}}const n7=async e=>{const t=nt(e.params,"balance",!1),n=e.framing.value===""?tt:e.framing.value,s=Math.max(0,Math.min(1,H(e.params,"minValid",.5))),o=Math.max(0,Math.round(H(e.params,"ignoreDiags",1))),r=J9(e.params),i=T1(e.params);i>0&&e.note(`A ${i.toLocaleString()}-bin cut reaches ${rt(i*e.mc.binsize)} at this file’s ${e.mc.binsize.toLocaleString()} bp resolution, so every band is a quantile of what is within that.`);const a=Ht(e.params,"sequences");if(a!==null&&a.length===0)return e.note("No sequence is ticked, so there is nothing to measure."),[];const{units:c,missing:l}=e7(e,a),u=he(e.mc,l,c.length);if(u!==null&&e.note(u),c.length===0)return[];const h=c.filter(A=>A.span.to-A.span.from>=4),f=c.length-h.length;if(h.length===0)return e.note("No sequence in this file is long enough to have a distribution of separations."),[];f>0&&e.note(`${f.toLocaleString()} ${f===1?"sequence is":"sequences are"} shorter than four bins and were left out: there is no distribution of separations on them.`);const d=e.mc.nBins,m=r.map(()=>new Float32Array(d).fill(Number.NaN)),g=Ct(e,h.map(A=>A.span),t);let p=0;for(let A=0;A<h.length;A++){const N=h[A],M=N.span.to-N.span.from,I=Z9(e.params,M,N.circular);await e.breathe(),e.progress(A,h.length,N.name),N.circular&&p++;const x=await De(e,N.span,{hi:I,block:g,balance:t,value:n,circular:N.circular,onTile:async(S,k)=>{await e.breathe(),e.progress(A,h.length,k===1?N.name:`${N.name} · strip ${Math.min(S+1,k)} of ${k}`)}}),v=X9(x,{quantiles:r,maxDistance:I,ignoreDiags:o,minValid:s});for(let S=0;S<v.length;S++){const k=v[S].distance,C=m[S];for(let B=0;B<k.length;B++)C[N.span.from+B]=k[B]*e.mc.binsize}}e.progress(h.length,h.length,"done"),p>0&&e.note(`${p.toLocaleString()} of ${h.length.toLocaleString()} ${h.length===1?"sequence was":"sequences were"} read as circular, so a separation is the shorter of the two ways round and reaches at most half the sequence.`);const w=t7(r.length),b=i===0?"":` · within ${rt(i*e.mc.binsize)}`;return r.map((A,N)=>({kind:"profile",id:`scalogram-q${Math.round(A*100)}${i===0?"":`-${i}b`}`,label:`Scalogram ${Math.round(A*100)} %${b}`,values:m[N],extent:[0,d],binsize:e.mc.binsize,unit:"bp",colour:w[N],group:j9,mode:"ribbon",groupMode:"overlay",scale:"log2"}))};function s7(e,t){const n=[],s=Math.min(e.length,t.length);let o=0,r=0;for(let i=1;i<s;i++){const a=o7(e[i],t[i]);if(a===null)continue;const c=r7(a.x,a.y),l=(a.n+1)/12;n.push({k:i,n:a.n,r:c,weight:l}),o+=l*(Number.isFinite(c)?c:0),r+=l}return{scc:r>0?o/r:Number.NaN,strata:n}}function o7(e,t){const n=Math.min(e.length,t.length),s=c=>Number.isFinite(e[c])&&Number.isFinite(t[c])&&(e[c]!==0||t[c]!==0);let o=0;for(let c=0;c<n;c++)s(c)&&o++;if(o<2)return null;const r=new Float64Array(o),i=new Float64Array(o);let a=0;for(let c=0;c<n;c++)s(c)&&(r[a]=e[c],i[a]=t[c],a++);return{x:r,y:i,n:o}}function r7(e,t){const n=e.length;let s=0,o=0;for(let c=0;c<n;c++)s+=e[c],o+=t[c];s/=n,o/=n;let r=0,i=0,a=0;for(let c=0;c<n;c++){const l=e[c]-s,u=t[c]-o;r+=l*u,i+=l*l,a+=u*u}return r/Math.sqrt(i*a)}function i7(e,t,n,s){const o=e.size,r=s[0].length,i=o+1,a=new Float64Array(i*i),c=new Float64Array(i*i),l=new Float64Array(i*i);for(let f=0;f<o;f++){const d=f*o,m=f*i,g=(f+1)*i;for(let p=0;p<o;p++){const b=p-f>=1&&p-f<=n?e.values[d+p]:0,y=Number.isNaN(b);a[g+p+1]=(y?0:b)+a[g+p]+a[m+p+1]-a[m+p],c[g+p+1]=(y?1:0)+c[g+p]+c[m+p+1]-c[m+p],l[g+p+1]=(!y&&b!==0?1:0)+l[g+p]+l[m+p+1]-l[m+p]}}const u=(f,d,m,g,p)=>p[(m+1)*i+g+1]-p[f*i+g+1]-p[(m+1)*i+d]+p[f*i+d],h=f=>t+1+Math.min(Math.min(f,r-1-f),t);for(let f=e.ownFrom;f<e.ownTo;f++){const d=f-e.from;for(let m=1;m<s.length&&m<=n;m++){const g=d+m;if(g>=o)break;const p=Math.max(0,d-t),w=Math.min(o-1,d+t),b=Math.max(0,g-t),y=Math.min(o-1,g+t),A=h(f)*h(f+m)-u(p,b,w,y,c);if(!(A>0)){s[m][f]=Number.NaN;continue}s[m][f]=u(p,b,w,y,l)>0?u(p,b,w,y,a)/A:0}}}function a7(e,t){const n=[];for(let s=0;s<=t;s++)n.push(new Float64Array(e).fill(Number.NaN));return n}const Ic=500;function c7(e){const t=typeof e.maxDistance=="number"?e.maxDistance:Ic;return Math.max(1,Math.min(Ic,Math.round(t)))}const l7=async e=>{const t=e.inputs.peers??[];if(t.length===0)return e.note("No second map is open. Press + along the edge of the map to open a cell beside it, and drop another file in."),[];if(e.peer===void 0)throw new Error("Reproducibility reads a second file, and this venue cannot open one. Run it in the app, or with `chromosomix hicrep <file> --against <other>`.");const n=Ht(e.params,"sequences");if(n!==null&&n.length===0)return e.note("No sequence is ticked, so there is nothing to compare over."),[];const s=nt(e.params,"balance",!1),o=e.framing.value===""?tt:e.framing.value,r=Math.max(0,Math.round(H(e.params,"smooth",1))),i=c7(e.params),a=[{label:e.inputs.mapLabel??"This map",mc:e.mc,lead:!0}];try{for(let c=0;c<t.length;c++){e.progress(c,t.length,`opening ${t[c].label}`);const l=await e.peer(t[c]);if(l.mc.binsize!==e.mc.binsize){e.note(`${t[c].label} is at ${l.mc.binsize?.toLocaleString()??"?"} bp and this map is at ${e.mc.binsize.toLocaleString()} bp, so their strata are different separations and they are left out. Open them at one resolution.`),l.close();continue}a.push({label:t[c].label,mc:l.mc,lead:!1,close:l.close})}return a.length<2?(e.note("No map beside this one could be compared with it, so there is nothing to report."),[]):await u7(e,a,{balance:s,value:o,h:r,maxK:i})}finally{for(const c of a)c.close?.()}};async function u7(e,t,n){const{balance:s,value:o,h:r,maxK:i}=n,a=Ht(e.params,"sequences"),c=[],l=[];if(a===null)for(let x=0;x<e.mc.nContigs;x++)c.push(x);else for(const x of a){const v=Le(e.mc,x);v>=0?c.push(v):l.push(x)}const u=he(e.mc,l,c.length);if(u!==null&&e.note(u),c.length===0)return[];const h=t.length,f=new Float64Array(h*h),d=new Float64Array(h*h),m=[],g=[],p=[];for(let x=0;x<h*(h-1)/2;x++)g.push(new Float64Array(i+1)),p.push(new Float64Array(i+1));const w=Ct(e,c.map(x=>xr(e.mc,x)),s);for(let x=0;x<c.length;x++){await e.breathe();const v=c[x],S=e.mc.chromnames[v];e.progress(x,c.length,S);const k=xr(e.mc,v),C=k.to-k.from;if(C<3)continue;const B=[];let F=!1;for(const _ of t){const E=_.lead?k:f7(_.mc,S);if(E===null){F=!0;break}if(E.to-E.from!==C){e.note(`${S} is ${C.toLocaleString()} bins here and ${(E.to-E.from).toLocaleString()} in ${_.label}, so it is left out.`),F=!0;break}B.push(await h7(e,_,E,{balance:s,value:o,h:r,maxK:Math.min(i,C-1),bins:C,block:_.lead?w:null}))}if(F)continue;let $=0;for(let _=0;_<h;_++)for(let E=_+1;E<h;E++){const O=s7(B[_],B[E]),L=_*h+E;for(const D of O.strata)f[L]+=D.weight*(Number.isFinite(D.r)?D.r:0),d[L]+=D.weight,Number.isFinite(D.r)&&(g[$][D.k]+=D.weight*D.r,p[$][D.k]+=D.weight);m.push({sequence:S,a:t[_].label,b:t[E].label,scc:O.scc,strata:O.strata.length}),$++}}e.progress(c.length,c.length,"done"),e.throwIfCancelled();const b=new Float32Array(h*h);for(let x=0;x<h;x++){b[x*h+x]=1;for(let v=x+1;v<h;v++){const S=x*h+v,k=d[S]>0?f[S]/d[S]:Number.NaN;b[x*h+v]=k,b[v*h+x]=k}}const y=t.map(x=>x.label),A=[{kind:"matrix",id:"hicrep",label:"Stratum-adjusted correlation",nrows:h,ncols:h,values:b,xLabel:"",yLabel:"",xTicks:y,yTicks:y,colormap:"RdBu_r",range:[0,1],stats:[{label:"sequences",value:c.length,digits:0}],acrossMaps:!0}],N=[];for(let x=0;x<h;x++)for(let v=x+1;v<h;v++)N.push({a:y[x],b:y[v],scc:b[x*h+v]});A.push({kind:"table",id:"hicrep-pairs",label:"Pooled over every sequence",columns:[{key:"a",label:"Map",kind:"text"},{key:"b",label:"Map",kind:"text"},{key:"scc",label:"SCC",kind:"number",digits:4,hint:"Every stratum of every sequence, each weighted by the positions it compares."}],rows:N.sort((x,v)=>Number(v.scc)-Number(x.scc)),acrossMaps:!0}),m.length>0&&A.push({kind:"table",id:"hicrep-sequences",label:"Per sequence",columns:[{key:"sequence",label:"Sequence",kind:"text"},{key:"a",label:"Map",kind:"text"},{key:"b",label:"Map",kind:"text"},{key:"scc",label:"SCC",kind:"number",digits:4},{key:"strata",label:"Strata",kind:"number",digits:0}],rows:m,acrossMaps:!0});const M=[];let I=0;for(let x=0;x<h;x++)for(let v=x+1;v<h;v++){const S=new Float64Array(i+1),k=new Float64Array(i+1);for(let C=0;C<=i;C++)S[C]=e.toBp(C),k[C]=p[I][C]>0?g[I][C]/p[I][C]:Number.NaN;M.push({name:`${y[x]} vs ${y[v]}`,x:S,y:k}),I++}return M.length>0&&A.push({kind:"curve",id:"hicrep-strata",label:"Correlation by separation",series:M,xLabel:"Genomic separation (bp)",yLabel:"Pearson r on that stratum",xScale:"log",yScale:"linear",yDomain:[-.2,1],acrossMaps:!0}),A}async function h7(e,t,n,s){const o=a7(s.bins,s.maxK),r=s.maxK+s.h,i=t.lead?e:{...e,mc:t.mc};return await j0(i,n,{halo:r,block:s.block,balance:s.balance,value:s.value},a=>{e.throwIfCancelled(),i7(a,s.h,s.maxK,o)}),o}function xr(e,t){const[n,s]=e.contigExtent(t);return{from:n,to:s}}function f7(e,t){const n=Le(e,t);return n<0?null:xr(e,n)}function d7(e){const t=[];return e.total!==!1&&t.push("total"),e.cis===!0&&t.push("cis"),e.trans===!0&&t.push("trans"),e.fraction===!0&&t.push("fraction"),t}const m7={total:{label:"Coverage · total",unit:"contacts"},cis:{label:"Coverage · cis",unit:"contacts"},trans:{label:"Coverage · trans",unit:"contacts"},fraction:{label:"Cis fraction",unit:""}},g7=async e=>{const t=d7(e.params);if(t.length===0)return e.note("Every output was turned off, so there is nothing to compute."),[];const n=nt(e.params,"balance",!1),s=e.mc,o=[];for(let h=0;h<s.nContigs;h++){const[f,d]=s.contigExtent(h);o.push({startBin:f,bins:Math.max(0,d-f)})}const r=Qt(s,n),i=new l1(s.nBins,Pn(o,s.nBins),r===null?null:h=>Number.isFinite(r[h])),a=e.inputs.block;a!==void 0&&a.binsPerCell===1&&e.framing.balance===n&&a.rowExtent[0]===0&&a.rowExtent[1]===s.nBins&&a.colExtent[0]===0&&a.colExtent[1]===s.nBins&&a!==void 0?(e.progress(0,1,"summing the block on screen"),i.addBlock(a)):(e.framing.value!==""&&e.framing.value!==tt&&e.note(`The map is drawn from the ${e.framing.value} column, but a whole-file pass reads ${tt}. These marginals are of the counts.`),e.progress(0,1,"reading pixels"),await s.scanPixels({onChunk:h=>{e.throwIfCancelled(),i.addPixels(h.bin1,h.bin2,h.count,h.length,r)},onProgress:(h,f)=>e.progress(h,f,"reading pixels")})),e.throwIfCancelled();const l=i.finish(),u={total:l.total,cis:l.cis,trans:l.trans,fraction:l.cisFraction};return t.map(h=>{const f=m7[h];return{kind:"profile",id:`coverage-${h}`,label:f.label,values:Float32Array.from(u[h]),extent:[0,s.nBins],binsize:s.binsize,unit:f.unit}})},Jt=1,Zt=2,jt=4,zt=8,p7=Jt|Zt|jt|zt,w7=(()=>{const e=new Uint8Array(256),t={A:Jt,C:Zt,G:jt,T:zt,U:zt,R:Jt|jt,Y:Zt|zt,S:jt|Zt,W:Jt|zt,K:jt|zt,M:Jt|Zt,B:Zt|jt|zt,D:Jt|jt|zt,H:Jt|Zt|zt,V:Jt|Zt|jt,N:p7};for(const[n,s]of Object.entries(t))e[n.charCodeAt(0)]=s,e[n.toLowerCase().charCodeAt(0)]=s;return e})();(()=>{const e=new Uint8Array(16);for(let t=0;t<16;t++){let n=0;(t&Jt)!==0&&(n|=zt),(t&Zt)!==0&&(n|=jt),(t&jt)!==0&&(n|=Zt),(t&zt)!==0&&(n|=Jt),e[t]=n}return e})();function xc(e){return!(e instanceof Uint8Array)}const vc=200;function kc(e){const t=e.replace(/\s+/g,"");if(t==="")return{reason:"Type a sequence to search for."};if(t.length>vc)return{reason:`${t.length} bases is longer than this searches for (${vc}). It looks for short motifs, so a query that long is a job for an aligner.`};const n=new Uint8Array(t.length);for(let s=0;s<t.length;s++){const o=w7[t.charCodeAt(s)]??0;if(o===0)return{reason:`“${t[s]}” is not a IUPAC base. Use A C G T U and the ambiguity codes R Y S W K M B D H V N.`};n[s]=o}return n}const O1=[{name:"DpnII",site:"GATC",cut:0},{name:"HindIII",site:"AAGCTT",cut:1},{name:"NcoI",site:"CCATGG",cut:1},{name:"EcoRI",site:"GAATTC",cut:1},{name:"BglII",site:"AGATCT",cut:1},{name:"HinfI",site:"GANTC",cut:1},{name:"DdeI",site:"CTNAG",cut:1},{name:"MseI",site:"TTAA",cut:1},{name:"NlaIII",site:"CATG",cut:4},{name:"CviQI",site:"GTAC",cut:1},{name:"AluI",site:"AGCT",cut:2}],b7={MboI:"DpnII",Sau3AI:"DpnII",Csp6I:"CviQI"},Hs={Arima:["DpnII","HinfI"]},A7=(()=>{const e=new Map;for(const t of O1)e.set(t.name.toLowerCase(),t);for(const[t,n]of Object.entries(b7)){const s=e.get(n.toLowerCase());s!==void 0&&e.set(t.toLowerCase(),s)}return e})();function In(e){return A7.get(e.trim().toLowerCase())}function Sc(e){const t=e.trim().toLowerCase();return Object.keys(Hs).find(n=>n.toLowerCase()===t)}function y7(e){return`${e.site.slice(0,e.cut)}^${e.site.slice(e.cut)}`}function N7(e){return e.site.length-2*e.cut}const M7=1/256,Cc=255;function Fc(e){let t=1;for(const n of e){let s=0;for(let o=n;o!==0;o&=o-1)s++;t*=s/4}return t}function L1(e){const t=e.toUpperCase().replace(/U/g,"T"),n=t.indexOf("^"),s=t.replace("^","");if(n<0){const r=kc(s);return{refusal:xc(r)?`“${e}” is neither an enzyme this knows nor a site in IUPAC letters with a ^ where it cuts.`:`“${e}” needs a ^ where the enzyme cuts the top strand, as in ^GATC or G^ANTC.`}}if(s.includes("^"))return{refusal:`“${e}” has more than one ^, and a site is cut in one place.`};const o=kc(s);return xc(o)?{refusal:o.reason}:Fc(o)>M7?{refusal:`“${e}” would cut every ${Math.round(1/Fc(o))} bases or so; a site needs at least the specificity of a four-base cutter.`}:{name:`${t.slice(0,n)}^${t.slice(n+1)}`,site:s,cut:n}}function I7(e){const t=In(e);if(t!==void 0)return t;const n=L1(e);return"refusal"in n?void 0:n}function _1(e,t){const n=e.trim();if(n===""||n==="none")return[];if(n!=="custom"){const a=Sc(n);if(a!==void 0)return Hs[a].map(l=>In(l));const c=In(n);return c===void 0?{refusal:`“${n}” is not an enzyme or a kit this knows. Pick one, or type its site under custom.`}:[c]}const s=t.split(/[\s,;]+/).filter(a=>a!=="");if(s.length===0)return{refusal:"Type an enzyme or a site, such as DpnII or G^ANTC."};const o=[],r=new Set,i=a=>{const c=`${a.site}^${a.cut}`;r.has(c)||(r.add(c),o.push(a))};for(const a of s){const c=Sc(a);if(c!==void 0){for(const h of Hs[c])i(In(h));continue}const l=In(a);if(l!==void 0){i(l);continue}const u=L1(a);if("refusal"in u)return u;i(u)}return o.length>Cc?{refusal:`That is ${o.length} enzymes, and one digest takes ${Cc} at most.`}:o}function x7(e){return e.map(t=>t.name).join("+")}const v7=256;function $c(e,t){const n=new Int32Array(e.length+2);let s=0;for(let o=0;o<e.length;o++)s=Math.min(t,Math.max(s,e[o])),n[o+1]=s;return n[e.length+1]=t,n}function Ec(e,t){let n=0,s=e.length;for(;s>1;){const o=s>>1;n=e[n+o]<=t?n+o:n,s-=o}return Math.min(n,Math.max(0,e.length-2))}const Rn=["valid","re-ligation","dangling","self-circle","same-strand","trans"],Ye=["++","+-","-+","--"],k7=0,S7=1,C7=2,F7=3,$7=4,E7=5;function Bc(e,t,n,s){return e===n?t===s?$7:t===0?C7:F7:Math.abs(n-e)===1&&t===0&&s===1?S7:k7}const Jo=65536;function Tc(e){return e==="+"?0:e==="-"?1:-1}function B7(e){return e===0?0:33-Math.clz32(e-1)}function T7(e){if(e<=2)return String(e);const t=2**(e-1);return`${t/2+1}–${t}`}class O7{constructor(t,n,s=""){this.label=s;const o=[...n.keys()];let r=0;for(const[i,a]of t){let c=n.get(i);if(c===void 0){const u=Ss(o,i);c=u<0?void 0:n.get(o[u])}if(c===void 0||!(c>0)||this.index.has(i))continue;const l=$c(a.top,c);this.index.set(i,this.names.length),this.names.push(i),this.top.push(l),this.bottom.push($c(a.bottom,c)),this.firstFragment.push(r),r+=l.length-1}this.ends=new Uint32Array(r),this.plus=new Float64Array(this.edges.length),this.minus=new Float64Array(this.edges.length),this.distanceBin=new Uint8Array(Jo);for(let i=0,a=0;i<Jo;i++){for(;a+1<this.edges.length&&this.edges[a+1]<=i;)a++;this.distanceBin[i]=a}}names=[];top=[];bottom=[];firstFragment=[];index=new Map;ends;classCounts=new Float64Array(Rn.length*Ye.length);edges=e0();plus;minus;counted=0;unassigned=0;last1="";lastAt1=-1;last2="";lastAt2=-1;nearContig1=-1;nearAt1=0;nearContig2=-1;nearAt2=0;distanceBin;indexOf(t){const n=this.index.get(t);if(n!==void 0)return n;const s=Ss(this.names,t);return this.index.set(t,s),s}add(t,n,s,o,r,i,a){if(a)return;const c=Tc(s),l=Tc(i);if(c<0||l<0||t==="!"||o==="!"||n!==n||r!==r)return;let u;t===this.last1?u=this.lastAt1:(u=this.indexOf(t),this.last1=t,this.lastAt1=u);let h;if(o===this.last2?h=this.lastAt2:(h=this.indexOf(o),this.last2=o,this.lastAt2=h),u<0||h<0){this.unassigned++;return}this.counted++;const f=n-1,d=r-1,m=c===0?this.top[u]:this.bottom[u];let g=this.nearAt1;(u!==this.nearContig1||!(f>=m[g]&&f<m[g+1]))&&(g=Ec(m,f),this.nearContig1=u,this.nearAt1=g);const p=l===0?this.top[h]:this.bottom[h];let w=this.nearAt2;(h!==this.nearContig2||!(d>=p[w]&&d<p[w+1]))&&(w=Ec(p,d),this.nearContig2=h,this.nearAt2=w),this.ends[this.firstFragment[u]+g]++,this.ends[this.firstFragment[h]+w]++,this.distanceOf(u,g,f,c),this.distanceOf(h,w,d,l);let b,y;u!==h?(b=E7,y=c*2+l):d<f?(b=Bc(w,l,g,c),y=l*2+c):(b=Bc(g,c,w,l),y=c*2+l),this.classCounts[b*Ye.length+y]++}distanceOf(t,n,s,o){const r=o===0?this.top[t][n+1]-s:s-this.bottom[t][n]+1;let i=0;if(r>=0&&r<Jo)i=this.distanceBin[r];else if(r>0){const a=this.edges;let c=a.length;for(;c-i>1;){const l=i+c>>1;a[l]<=r?i=l:c=l}}o===0?this.plus[i]++:this.minus[i]++}result(){const t={},n={};Rn.forEach((r,i)=>{const a={};let c=0;Ye.forEach((l,u)=>{const h=this.classCounts[i*Ye.length+u];a[l]=h,c+=h}),t[r]=c,n[r]=a});const s=[];let o=0;for(let r=0;r<this.ends.length;r++){const i=this.ends[r];i>0&&o++;const a=B7(i);for(;s.length<=a;)s.push(0);s[a]++}return{enzymes:this.label,counted:this.counted,unassigned:this.unassigned,classes:t,byOrientation:n,distanceEdges:[...this.edges],distance:{"+":Array.from(this.plus),"-":Array.from(this.minus)},fragments:{total:this.ends.length,seen:o,binLabels:s.map((r,i)=>T7(i)),counts:s}}}}function L7(e){const t=e.outputs.find(i=>i.kind==="intervals"&&i.id.startsWith("restriction-")&&i.payload.kind==="features");if(t===void 0||t.payload.kind!=="features")return null;const n=t.payload.data,s=new Map,o=new Map,r=new Map;for(let i=0;i<n.contigs.length;i++){const a=n.contigAt[i]??0,c=n.contigAt[i+1]??a,l=n.start.slice(a,c),u=new Int32Array(l.length),h=new Uint8Array(l.length);for(let f=0;f<l.length;f++){const d=n.name[a+f]??"";let m=o.get(d);if(m===void 0){const g=I7(d);m=g===void 0?0:N7(g),o.set(d,m),r.set(d,Math.min(v7-1,r.size))}u[f]=l[f]+m,h[f]=r.get(d)??0}s.set(n.contigs[i],{top:l,bottom:u,enzyme:h})}return s}const Oc=["++","+−","−+","−−"],Zo=16;function _7(e,t={}){const n=Math.max(1.01,t.factor??1.1),s=e.edges;if(s.length<3)return null;const o=D7(s,1,n);if(o===null||o.length<3)return null;const r=o.map(d=>s[d]),i=o.length-1,a=[new Float64Array(i),new Float64Array(i),new Float64Array(i),new Float64Array(i)],c=[0,0,0,0];for(let d=0;d<i;d++)for(let m=o[d];m<o[d+1];m++)for(let g=0;g<4;g++){const p=e.counts[g][m];p>0&&(a[g][d]+=p,c[g]+=p)}const l=c[0]+c[1]+c[2]+c[3];if(l===0)return null;const u=[],h=[];for(let d=0;d<4;d++){if(c[d]===0)continue;const m=new Float64Array(i),g=new Float64Array(i);for(let p=0;p<i;p++)m[p]=Math.sqrt(r[p]*r[p+1]),g[p]=a[d][p]>0?a[d][p]/(l*(r[p+1]-r[p])):Number.NaN;u.push({name:Oc[d],x:m,y:g,n:c[d]}),h.push({name:Oc[d],...R7(a[d],r,l),n:c[d]})}if(h.length<2)return null;const f=z7(u);return{curves:h,convergence:f.at,spreadAt:f.spread}}function D7(e,t,n){const s=e.length-1;let o=0;for(;o<s&&e[o]<t;)o++;if(o>=s)return null;const r=[o];for(;o<s;){const i=e[o]*n;let a=o+1;for(;a<s&&Math.abs(Math.log(e[a+1]/i))<Math.abs(Math.log(e[a]/i));)a++;r.push(a),o=a}return r}function R7(e,t,n){let s=0,o=e.length-1;for(;s<=o&&!(e[s]>0);)s++;for(;o>=s&&!(e[o]>0);)o--;const r=[],i=[],a=(l,u,h)=>{const f=t[l],d=t[u+1];r.push(Math.sqrt(f*d)),i.push(h/(n*(d-f)))};let c=s;for(;c<=o;){if(e[c]>=Zo){a(c,c,e[c]),c++;continue}let l=c;for(;l<o&&e[l+1]<Zo;)l++;const u=[];let h=c,f=0;for(let d=c;d<=l;d++)f+=e[d],!(f<Zo)&&(u.push([h,d,f]),h=d+1,f=0);if(h<=l){const d=u[u.length-1];d!==void 0?(d[1]=l,d[2]+=f):f>0&&u.push([h,l,f])}for(const[d,m,g]of u)a(d,m,g);c=l+1}return{x:Float64Array.from(r),y:Float64Array.from(i)}}function z7(e){const t=e[0].x.length;let n=null,s=Number.NaN;for(let o=t-1;o>=0;o--){let r=Number.POSITIVE_INFINITY,i=0,a=0;for(const l of e){const u=l.y[o];!Number.isFinite(u)||u<=0||(a++,u<r&&(r=u),u>i&&(i=u))}if(a<e.length){if(n!==null)break;continue}const c=Math.log10(i/r);if(c>.1)break;n=e[0].x[o],s=c}return{at:n,spread:s}}[...O1.map(e=>({value:e.name,label:`${e.name} (${y7(e)})`})),...Object.entries(Hs).map(([e,t])=>({value:e,label:`${e} (${t.join(" + ")})`}))];function P7(e){const t=e.pairs;return typeof t=="string"?t.trim():""}function q7(e){const t=e.reference;return typeof t=="string"?t:""}function U7(e){const t=e.enzyme;return typeof t=="string"&&t!==""?t:"none"}function G7(e){const t=e.sites;return typeof t=="string"?t:""}function H7(e){const t=q7(e),n=U7(e);return t===""||n==="none"?null:{sequence:t,enzyme:n,sites:G7(e)}}const Q7=async e=>{const t=P7(e.params),n=e.inputs.mapSource,s=K7(e);let o=t===""?e.inputs.pairsStats??null:null,r=t===""?e.inputs.orientation??null:null,i=null,a=`as ${Dc(e.framing.uri)} was binned`;const c=t!==""||s!==null||o===null&&n!==void 0;if(c){const u=t!==""?t:Dc(e.framing.uri),h=t!==""?await D1(e,t,e.inputs.sources?.pairs):n!==void 0?await j7(e,n,u):null;if(h!==null)try{const f=await J7(e,u,h,s);f!==null&&(o===null&&f.stats!==null&&(a=`in one pass over ${u}`),o??=f.stats,r??=f.orientation,i=f.restriction)}finally{h.close()}}if(o===null&&r===null&&i===null)return t===""&&n===void 0&&!c&&e.note("This map was opened as a matrix, which keeps no records, so there is nothing to count. Name the .pairs it was binned from."),[];const l=[];if(r!==null){const u=R1(e,r,"pairsqc-orientation",{converge:!1});u!==null&&(l.push(u),o!==null&&e.note(tg(o)))}else o!==null&&e.note("The records carry no strand1/strand2 columns, so there is no P(s) by orientation: which way a read pointed is the whole of what that figure is about.");if(o!==null){o.scoped&&e.note("The import read part of the file, so these counts are of the part it read.");const u=eg(o);u!==null?l.push(u):e.note("The records carry no pair_type column, so pair types are not counted."),l.push(ng(o),sg(o)),e.note(`The pairtools statistics were counted ${a}: ${o.total.toLocaleString("en-US")} records, ${o.totalNodups.toLocaleString("en-US")} of them mapped on both sides and not duplicates.`)}if(i!==null&&(l.push(...og(e,i)),s!==null)){const u=await hg(e,s,i.enzymes);u!==null&&l.push(u)}return l};function K7(e){const t=H7(e.params),n=e.inputs.upstream?.restriction;if(t===null)return lt(e.params,"enzyme","none")!=="none"&&e.note("An enzyme was chosen with no reference to find its sites in, so there are no restriction statistics."),null;const s=_1(lt(t,"enzyme","none"),lt(t,"sites",""));if(!Array.isArray(s))throw new Error(s.refusal);if(n===void 0)return e.note("The restriction step did not run, so there are no restriction statistics."),null;const o=L7(n);return o===null?(e.note("The restriction step found no sequence to cut, so there are no restriction statistics."),null):o}function V7(e){const t=_1(lt(e.params,"enzyme","none"),lt(e.params,"sites",""));return Array.isArray(t)?x7(t):""}const Y7=43,W7=45,X7=33,Lc=68;function _c(e){return e===Y7?"+":e===W7?"-":"."}async function J7(e,t,n,s){e.progress(0,1,"reading records");const o=await js(n),r=s===null?null:Z7(e,s,o.contigs),i=o.contigs.map(f=>f.name),a=new TextDecoder,c=(f,d,m,g)=>d>=0?i[d]:g-m===1&&f[m]===X7?"!":a.decode(f.subarray(m,g)),l=r===null?void 0:(f,d,m,g,p,w,b,y,A,N,M,I,x)=>{const v=x-I===2&&f[I]===Lc&&f[I+1]===Lc;r.add(c(f,d,m,g),y<0?Number.NaN:y,_c(N),c(f,p,w,b),A<0?Number.NaN:A,_c(M),v)};let u=0;for(const f of o.contigs)u=Math.max(u,f.length);const h=await Jr(n,{binSize:Math.max(1,u),contigs:o.contigs,columns:o.columns,onProgress:(f,d)=>e.progress(f,d,"reading records"),cancelled:()=>e.cancelled(),...l===void 0?{}:{onRecord:l}});return e.throwIfCancelled(),h===null?null:(h.skippedUnknownContig>0&&r!==null&&e.note(`${h.skippedUnknownContig.toLocaleString("en-US")} records of ${t} name a sequence its header does not declare.`),{stats:h.stats??null,orientation:h.orientation??null,restriction:r===null?null:r.result()})}function Z7(e,t,n){const s=new Map;for(const a of n)s.set(a.name,a.length);const o=e.mc;for(const a of o.chromnames){const c=o.chromsizes.get(a);c!==void 0&&!s.has(a)&&s.set(a,c)}const r=[...s.keys()],i=[...t.keys()].filter(a=>!s.has(a)&&Ss(r,a)<0);return i.length>0&&e.note(`${i.length.toLocaleString("en-US")} of the reference's sequences (${i.slice(0,3).join(", ")}${i.length>3?", …":""}) are in neither the .pairs header nor the map, so their fragments are not counted.`),new O7(t,s,V7(e))}function Dc(e){return e.split("::")[0].split(/[?#]/)[0].split(/[\\/]/).filter(s=>s!=="").pop()??e}async function j7(e,t,n){if(e.openSource===void 0)return e.note(`Reading ${n} again needs a venue that can open a file it was handed, and this one cannot.`),null;try{return await e.openSource(t)}catch(s){return e.note(`Could not open ${n}: ${s instanceof Error?s.message:String(s)}`),null}}async function D1(e,t,n){const s=/^(https?|s3):\/\//i.test(t),o=n!==void 0&&e.openSource!==void 0?()=>e.openSource(n):s&&e.openBytes!==void 0?()=>e.openBytes(t):!s&&e.openFile!==void 0?()=>e.openFile(t):null;if(o===null)return e.note(s?`Reading ${t} needs a venue that can fetch an address, and this one reads local files only. Fetch it first and name the file, or run it in the app.`:`“${t}” is not an address this can fetch, and the file is no longer in hand — a dropped file is kept for the session it was dropped in. Drop it on the form again, or name it on a \`chromosomix\` command line, which reads it from disk.`),null;try{return await o()}catch(r){return e.note(`Could not open ${t}: ${r instanceof Error?r.message:String(r)}`),null}}function R1(e,t,n,s={}){const o=s.factor??Math.max(1.01,H(e.params,"factor",1.1)),r=_7(t,{factor:o});return r===null?(e.note("Fewer than two orientations have any cis record in them, so there is nothing to compare. A file whose strand columns are all `.` reads this way."),null):(s.converge!==!1&&e.note(r.convergence===null?"The four orientations never settle to within a tenth of each other, so no convergence point is reported. On a library that is otherwise fine this usually means the axis does not reach far enough — the sequences are short — rather than that the records are bad.":`The four orientations converge at about ${Math.round(r.convergence).toLocaleString()} bp, which is the shortest separation this library can be read at. Below it the counts are self-circles, dangling ends and undigested fragments rather than contacts.`),{kind:"curve",id:n,label:"P(s) by pair orientation",series:r.curves.map(i=>({name:i.name,x:i.x,y:i.y,n:i.n})),xLabel:"Genomic separation (bp)",yLabel:"Density, over the records in all four",xScale:"log",yScale:"log"})}function tg(e){const t=n0(e),n=s=>e.cis>0?`${(100*s/e.cis).toFixed(1)} %`:"—";return t.distance===null?"No orientation strays more than 5 % from the mean of the four at any separation, so pairtools reports no convergence distance: the four agree from the first base pair.":`The four orientations converge at ${t.distance.toLocaleString("en-US")} bp by pairtools' rule: past the last separation bin at which one of them (${t.strands??"?"}) strays more than 5 % from the mean of the four, with ${n(t.belowAll)} of the cis pairs below it. The furthest such bin sets it, so a sparse tail of chance strays can put it well past where the curves lie together.`}function lo(e){return Float64Array.from(e,(t,n)=>n)}function eg(e){if(e.pairTypes.length===0)return null;const t=e.pairTypes.map(([n])=>n);return{kind:"curve",id:"pairsqc-types",label:"Pair types",series:[{name:"records",x:lo(t),y:Float64Array.from(e.pairTypes,([,n])=>n),mark:"bars",n:e.total}],xLabel:"Pair type",yLabel:"Records",xScale:"linear",yScale:"linear",xTicks:t}}function ng(e){const t=["cis","trans",...e.cisAtLeast.map(([o])=>`cis_${o}kb+`)],n=[e.cis,e.trans,...e.cisAtLeast.map(([,o])=>o)],s=o=>e.totalNodups>0?o/e.totalNodups:Number.NaN;return{kind:"curve",id:"pairsqc-cis",label:"Cis and trans",series:[{name:"fraction",x:lo(t),y:Float64Array.from(n,s),mark:"bars",n:e.totalNodups}],xLabel:"Pairs",yLabel:"Share of the mapped pairs that are not duplicates",xScale:"linear",yScale:"linear",yDomain:[0,1],xTicks:t}}function sg(e){const t=$3(e).filter(([n])=>!/^(chrom_freq|dist_freq|chromsizes)\//.test(n)).map(([n,s])=>({statistic:n,value:E3(s)}));return{kind:"table",id:"pairsqc-stats",label:"pairtools stats",columns:[{key:"statistic",label:"Statistic",kind:"text"},{key:"value",label:"Value",kind:"text"}],rows:t}}function og(e,t){const n=t.enzymes===""?"the enzyme":t.enzymes;return e.note(`${t.counted.toLocaleString("en-US")} pairs were placed in ${n}'s ${t.fragments.total.toLocaleString("en-US")} fragments`+(t.unassigned>0?`; ${t.unassigned.toLocaleString("en-US")} more had an end on a sequence the reference has not got, and are not classified.`:".")),[rg(t,n),ig(t,n),ag(t,n),cg(t,n)]}function rg(e,t){const n=[...Rn],s=lo(n);return{kind:"curve",id:"pairsqc-classes",label:`Pair classes by orientation · ${t}`,series:Ye.map(o=>({name:o,x:s,y:Float64Array.from(Rn,r=>e.byOrientation[r][o]),mark:"bars",stack:"orientation"})),xLabel:"Class",yLabel:"Pairs",xScale:"linear",yScale:"linear",xTicks:n}}function ig(e,t){return{kind:"table",id:"pairsqc-classes-table",label:`Pair classes · ${t}`,columns:[{key:"class",label:"Class",kind:"text"},{key:"pairs",label:"Pairs",kind:"number"},{key:"share",label:"Share",kind:"number",digits:4},...Ye.map(n=>({key:n,label:n,kind:"number"}))],rows:Rn.map(n=>({class:n,pairs:e.classes[n],share:e.counted>0?e.classes[n]/e.counted:Number.NaN,...e.byOrientation[n]}))}}function ag(e,t){const n=e.distanceEdges,s=e.distance["+"],o=e.distance["-"];let r=n.length,i=-1;for(let l=1;l<n.length;l++)(s[l]>0||o[l]>0)&&(r=Math.min(r,l),i=l);const a=i<r?[]:Array.from({length:i-r+1},(l,u)=>r+u),c=Float64Array.from(a,l=>n[l]);return{kind:"curve",id:"pairsqc-distance",label:`Distance from a read to the next ${t} cut`,series:[{name:"+",x:c,y:Float64Array.from(a,l=>s[l])},{name:"-",x:c,y:Float64Array.from(a,l=>o[l])}],xLabel:"Distance to the cut (bp, bin start)",yLabel:"Read ends",xScale:"log",yScale:"linear"}}function cg(e,t){const{total:n,seen:s,binLabels:o,counts:r}=e.fragments;return{kind:"curve",id:"pairsqc-fragments",label:`Read ends per ${t} fragment · ${s.toLocaleString("en-US")} / ${n.toLocaleString("en-US")} fragments seen`,series:[{name:"fragments",x:lo(o),y:Float64Array.from(r),mark:"bars",n}],xLabel:"Read ends in the fragment",yLabel:"Fragments",xScale:"linear",yScale:"linear",xTicks:[...o]}}const lg=5,ug="five";async function hg(e,t,n){const s=e.mc,o=[];for(let w=0;w<s.nContigs;w++){const[b,y]=s.contigExtent(w);o.push({startBin:b,bins:Math.max(0,y-b)})}const r=[...t.keys()],i=new Float64Array(s.nBins),a=new Uint8Array(s.nBins);for(let w=0;w<s.nContigs;w++){const b=Ss(r,s.chromnames[w]);if(b<0)continue;const{startBin:y,bins:A}=o[w];a.fill(1,y,y+A);for(const N of t.get(r[b])?.top??[]){const M=y+Math.floor(N/s.binsize);M<y+A&&i[M]++}}e.progress(0,1,"reading pixels");const c=new l1(s.nBins,Pn(o,s.nBins));await s.scanPixels({onChunk:w=>{e.throwIfCancelled(),c.addPixels(w.bin1,w.bin2,w.count,w.length,null)},onProgress:(w,b)=>e.progress(w,b,"reading pixels")}),e.throwIfCancelled();const l=c.finish().total,u=[];let h=0;for(let w=0;w<s.nBins;w++)if(a[w]!==0){if(l[w]===0&&i[w]===0){h++;continue}u.push(w)}if(u.length===0)return e.note("No bin of the map lies on a sequence the reference has, so coverage is not set against sites."),null;u.sort((w,b)=>i[w]-i[b]||w-b);const f=Float64Array.from(u,w=>i[w]),d=Float64Array.from(u,w=>l[w]),m=new Float64Array(u.length);for(let w=0;w<u.length;){let b=w,y=0;for(;b<u.length&&f[b]===f[w];)y+=d[b++];m.fill(b-w>=lg?y/(b-w):Number.NaN,w,b),w=b}const g=i0(f,d),p=Number.isFinite(g)?g.toFixed(3):"undefined";return e.note(`Spearman ρ = ${p} between the ${n} sites in a bin and its coverage, over ${u.length.toLocaleString("en-US")} bins of ${s.binsize.toLocaleString("en-US")} bp`+(h===0?"":`; ${h.toLocaleString("en-US")} ${h===1?"bin":"bins"} with neither ${h===1?"was":"were"} left out`)+`. The mean is drawn where at least ${ug} bins share a number of sites.`),{kind:"curve",id:"pairsqc-sites",label:`Coverage against ${n} sites · ρ = ${p}`,series:[{name:"bins",x:f,y:d,mark:"points",n:u.length},{name:"mean",x:f,y:m}],xLabel:"Restriction sites in the bin",yLabel:"Contacts (coverage)",xScale:"linear",yScale:"linear"}}function jo(e,t,n=0){let s=0,o=0;for(const i of t){const a=i.to-i.from;s+=a,a>o&&(o=a)}const r=n>0&&t.length===1&&o===n;return{name:e,spans:t,bins:s,maxOffset:r?Math.floor(n/2):Math.max(0,o-1),lap:r?n:0}}function fg(e,t,n){const s=e.mc,o=(c,l)=>_e(e,s.chromnames[c])?l.to-l.from:0;if(t==="file"){const c=[];for(const l of be(s,null).ids){const u=Ut(s,l);u!==null&&c.push(u)}return{units:c.length===0?[]:[jo("All sequences",c)],missing:[]}}if(t==="genome"){if(!s.hasGenomes)throw new T("unsupported","This file is a plain cooler: it has no /genomes table, so there is nothing for a per-genome P(s) to be per. Use the contig scope, which is the same curve over the sequences this file does have.",{uri:s.uri});const c=[];for(const l of s.displayGenomes()){const[u,h]=s.genomeContigExtent(l),f=[];for(let d=u;d<h;d++){const m=Ut(s,d);m!==null&&f.push(m)}f.length>0&&c.push(jo(s.genomeNames[l],f))}return{units:c,missing:[]}}const{ids:r,missing:i}=be(s,n),a=[];for(const c of r){const l=Ut(s,c);l!==null&&a.push(jo(s.chromnames[c],[l],o(c,l)))}return{units:a,missing:i}}function dg(e,t,n){return e.map(s=>{const o=new Float64Array(s.maxOffset+1);for(const r of s.spans){const i=r.to-r.from,a=s.lap;let c;for(let l=0;l<=s.maxOffset&&l<i;l++){let u;if(a>0)if(n===null)u=2*l===a?a/2:a;else{u=0;for(let h=0;h<i;h++){const f=r.from+h,d=r.from+(h+l)%a;!Number.isNaN(n[f])&&!Number.isNaN(n[d])&&u++}l>0&&2*l===a&&(u/=2)}else n===null?u=i-l:(c??=mg(n,r.from,r.to,Math.min(s.maxOffset,i-1)),u=c[l]);o[l]+=t&&l>0?u*2:u}}return o})}function mg(e,t,n,s){const o=n-t,r=new Int32Array(o+1);for(let f=0;f<o;f++)r[f+1]=r[f]+(Number.isNaN(e[t+f])?1:0);const i=r[o],a=o-i<=i,c=new Int32Array(a?o-i:i);let l=0;for(let f=0;f<o;f++)Number.isNaN(e[t+f])!==a&&(c[l++]=f);const u=new Float64Array(s+1);for(let f=0;f<l;f++){u[0]++;for(let d=f+1;d<l;d++){const m=c[d]-c[f];if(m>s)break;u[m]++}}if(a)return u;const h=new Float64Array(s+1);for(let f=0;f<=s;f++)h[f]=o-f-r[o-f]-(i-r[f])+u[f];return h}function gg(e,t,n){const s=new Float64Array(t.length);for(let o=0;o<s.length;o++)s[o]=n[o]>0?t[o]/n[o]:Number.NaN;return{maxOffset:e,sum:t,count:n,mean:s}}async function pg(e,t,n){const s=e.mc,o=new Int32Array(s.nBins).fill(-1),r=[],i=[];for(let u=0;u<t.length;u++)for(const h of t[u].spans)o.fill(r.length,h.from,h.to),r.push(u),i.push(t[u].lap);const a=Qt(s,n);e.progress(0,1,"counting the pairs each separation could hold"),e.throwIfCancelled();const c=dg(t,s.storageMode==="square",a),l=t.map(u=>new Float64Array(u.maxOffset+1));return await s.scanPixels({onChunk:u=>{e.throwIfCancelled();for(let h=0;h<u.length;h++){const f=u.bin1[h],d=u.bin2[h],m=o[f];if(!(m>=0)||o[d]!==m)continue;const g=a===null?u.count[h]:u.count[h]*a[f]*a[d];if(Number.isNaN(g))continue;const p=d>f?d-f:f-d,w=i[m],b=w>0?Math.min(p,w-p):p;l[r[m]][b]+=g}},onProgress:(u,h)=>e.progress(u,h,"reading pixels")}),t.map((u,h)=>gg(u.maxOffset,l[h],c[h]))}async function wg(e,t,n,s){const o=Ct(e,t.flatMap(r=>r.spans),n);if(o!==null){const r=[];for(let i=0;i<t.length;i++){e.throwIfCancelled(),e.progress(i,t.length,t[i].name);const a=new Bn(t[i].maxOffset,t[i].lap);for(const c of t[i].spans)a.addBlock(ci(o,c));r.push(a.finish())}return{curves:r,mirrored:!0}}if(t.length===1&&t[0].spans.length===1){const r=t[0].spans[0];e.progress(0,1,t[0].name);const i=await e.mc.matrix({rows:[r.from,r.to],cols:[r.from,r.to],balance:n,value:s});e.throwIfCancelled();const a=new Bn(t[0].maxOffset,t[0].lap);return a.addBlock(i),{curves:[a.finish()],mirrored:!0}}return s!==tt&&e.note(`The whole-file pass reads the count column, so the ${s} column this map is drawn from was not used. Narrow the scope to one sequence to measure that column instead.`),{curves:await pg(e,t,n),mirrored:e.mc.storageMode==="square"}}const Rc=[4e3,1e6],bg=async e=>{const t=lt(e.params,"scope","contig"),n=Ht(e.params,"sequences"),s=H(e.params,"factor",1.1),o=H(e.params,"minSeparation",0),r=lt(e.params,"quantity","density")==="expected",i=nt(e.params,"derivative",!0),a=nt(e.params,"balance",!1);if(t==="contig"&&n!==null&&n.length===0)return e.note("No sequence is ticked, so there is nothing to measure."),[];const{units:c,missing:l}=fg(e,t,n),u=he(e.mc,l,c.length);if(u!==null&&e.note(u),c.length===0)return u===null&&e.note("No sequence in this file has any bins in it, so there is nothing to measure."),[];const h=c,{curves:f,mirrored:d}=await wg(e,h,a,e.framing.value);e.throwIfCancelled();const m=[],g=[];let p=0;for(let y=0;y<h.length;y++){const A=r?jr(f[y],e.mc.binsize,s,o):Ad(f[y],e.mc.binsize,s,o,d);if(A.x.length===0){p++;continue}let N=0;for(const M of A.n)N+=M;if(m.push({name:h[y].name,x:A.x,y:A.y,n:N}),i){const M=a0(A.x,A.y);M.x.length>0&&g.push({name:h[y].name,x:M.x,y:M.y,n:N})}}p>0&&e.note(`${p.toLocaleString()} ${p===1?"sequence":"sequences"} had no separation bin with any data in it at these settings, and so no line. A minimum separation longer than the sequence is the usual reason.`);const w=[{kind:"curve",id:"ps",label:"Contact probability P(s)",series:m,xLabel:"Genomic separation (bp)",yLabel:r?"Mean contact per bin pair":"Contact probability density (per bp)",xScale:"log",yScale:"log",xDomain:Rc}];g.length>0&&w.push({kind:"curve",id:"ps-slope",label:"Log-log slope of P(s)",series:g,xLabel:"Genomic separation (bp)",yLabel:"d log P / d log s",xScale:"log",yScale:"linear",xDomain:Rc});const b=await Ag(e);return b!==null&&w.push(b),w};async function Ag(e){if(!nt(e.params,"stranded",!1))return null;e.note("The four orientations have moved to Pairs QC, which draws them beside the rest of the library's statistics from the same records; this run drew them here because its parameters asked for them.");const t=lt(e.params,"pairsUri","").trim();let n=null;if(t===""){const s=e.inputs.orientation;if(s===void 0)return e.note("The four orientations are counted from the records, and this map has none to count: it was opened as a matrix rather than binned from a .pairs, or the file it was binned from carried no strand1/strand2 columns. Run Pairs QC and give it the .pairs."),null;n=s}else if(n=await yg(e,t,e.inputs.sources?.pairsUri),n===null)return null;return R1(e,n,"ps-orientation")}async function yg(e,t,n){const s=await D1(e,t,n);if(s===null)return null;try{return await Ng(e,t,s)}finally{s.close()}}async function Ng(e,t,n){e.progress(0,1,"reading records");const s=await js(n);if(s.columns.strand1<0||s.columns.strand2<0)return e.note(`${t} declares no strand1/strand2 columns, so its records cannot say which orientation a pair was in. \`pairtools\` writes them by default; a file trimmed of them cannot be read this way.`),null;let o=0;for(const a of s.contigs)o=Math.max(o,a.length);const r=await Jr(n,{binSize:Math.max(1,o),contigs:s.contigs,columns:s.columns,onProgress:(a,c)=>e.progress(a,c,"reading records"),cancelled:()=>e.cancelled()});if(e.throwIfCancelled(),r===null)return null;const i=r.orientation??null;return i===null?(e.note(`${t} carried strand columns but no cis record with a readable pair of strands.`),null):i}class Mg{constructor(t,n){this.nBins=t,this.band=n,this.sums=new Float64Array(t),this.seen=new Uint8Array(t)}sums;seen;reachable(t){return t<this.band.to}add(t,n,s){if(!Number.isFinite(s)||t<0||n<0||t>=this.nBins||n>=this.nBins)return;const o=t>=this.band.from&&t<this.band.to,r=n>=this.band.from&&n<this.band.to;o&&(this.sums[n]+=s,this.seen[n]=1),r&&t!==n&&(this.sums[t]+=s,this.seen[t]=1)}finish(t=null){const n=new Float32Array(this.nBins);for(let s=0;s<this.nBins;s++){if(t!==null&&!t(s)){n[s]=Number.NaN;continue}n[s]=this.seen[s]===1?this.sums[s]:0}return n}}function Ig(e,t,n="cols"){const{nrows:s,ncols:o,values:r}=e,i=n==="cols"?s:o,a=n==="cols"?o:s,c=new Float64Array(a),l=Math.max(0,Math.min(i,Math.trunc(t[0]))),u=Math.max(l,Math.min(i,Math.trunc(t[1])));for(let h=l;h<u;h++)for(let f=0;f<a;f++){const d=n==="cols"?r[h*o+f]:r[f*o+h];Number.isNaN(d)||(c[f]+=d)}return c}function xg(e,t){const n=e.mc,s=Zf(n,t),o={from:s.extent[0],to:Math.max(s.extent[0]+1,s.extent[1])};if(s.kind==="genome"){const[c,l]=n.genomeExtent(s.label);return{band:o,scope:{from:c,to:l},label:s.label}}const r=n.coords.contigOfBin(o.from),[i,a]=n.contigExtent(r);return{band:o,scope:{from:i,to:a},label:n.chromnames[r]}}const vg=async e=>{const t=lt(e.params,"anchor","").trim();if(t==="")return e.note("No anchor was chosen. Type a locus, or a whole sequence by name, or press “pick on map” and drag along either axis."),[];const n=nt(e.params,"balance",!1),s=e.mc.hasGenomes,o=!s&&nt(e.params,"cis",!0),r=s?"none":lt(e.params,"normalise","none"),i=r==="ps"||r==="ps+coverage",a=r==="coverage"||r==="ps+coverage",c=e.framing.value===""?tt:e.framing.value,l=xg(e,t),u=o?await z1(e,l,n,c,i,a):await Sg(e,l,n,i,a),h=nt(e.params,"excludeAnchor",!1);return h&&(kg(u,l.band),u.every(m=>Number.isNaN(m))&&e.note(`The anchor covers everything this profile reads, so leaving it out leaves nothing${o?" — turn off “cis only”, or name a narrower anchor.":"."}`)),[{kind:"profile",id:`fourc-${l.band.from}-${l.band.to}`,label:`4C · ${E0(t)}${o||s?"":" (with trans)"}${i?" · O/E":""}${a?" · per coverage":""}${h?" · anchor excluded":""}`,values:u,extent:[0,e.mc.nBins],binsize:e.mc.binsize,unit:i?"":"contacts"}]};function kg(e,t){e.fill(Number.NaN,Math.max(0,t.from-1),Math.min(e.length,t.to+1))}async function Sg(e,t,n,s,o){const r=s?await z1(e,t,n,tt,!0):null,i=o?new Float64Array(e.mc.nBins):null,a=await Eg(e,t,n,i);let c=r===null?a:Fg(e,a,t);if(r!==null)for(let l=t.scope.from;l<Math.min(t.scope.to,c.length);l++)c[l]=r[l];return i!==null&&(c=P1(e,c,i)),c}function Cg(e,t,n){const s=new Bn(Math.max(0,t.nrows-1));s.addBlock({nrows:t.nrows,ncols:t.ncols,values:t.values,rowExtent:[t.rowExtent[0],t.rowExtent[1]],colExtent:[t.colExtent[0],t.colExtent[1]]});const o=s.finish(),r=(n[0]+n[1]-1)/2,i=new Float64Array(e.length);for(let a=0;a<e.length;a++){const c=ti(o,Math.abs(a-r));i[a]=Number.isFinite(e[a])&&Number.isFinite(c)&&c!==0?e[a]/c:Number.NaN}return i}function Fg(e,t,n){let s=0,o=0;for(let a=0;a<t.length;a++){if(a>=n.scope.from&&a<n.scope.to)continue;const c=t[a];Number.isFinite(c)&&(s+=c,o++)}if(o===0)return e.note("Nothing was reached outside this sequence, so there is no trans expectation to divide by."),t;const r=s/o,i=new Float32Array(t.length);for(let a=0;a<t.length;a++){const c=a>=n.scope.from&&a<n.scope.to,l=t[a];i[a]=c||!Number.isFinite(l)?l:r===0?Number.NaN:l/r}return i}async function z1(e,t,n,s,o=!1,r=!1){e.progress(0,1,`reading ${t.label}`);const i=e.framing.value===""?tt:e.framing.value,a=s===i?Ct(e,[t.scope],n):null,c=await io(e,t.scope,{block:a,balance:n,value:s});e.throwIfCancelled();const l=[Math.max(0,t.band.from-t.scope.from),Math.min(t.scope.to-t.scope.from,t.band.to-t.scope.from)],u=Ig({nrows:c.nrows,ncols:c.ncols,values:c.values,rowExtent:[c.rowExtent[0],c.rowExtent[1]],colExtent:[c.colExtent[0],c.colExtent[1]]},l,"cols"),h=o?Cg(u,c,l):u,f=new Float32Array(e.mc.nBins).fill(Number.NaN);for(let m=0;m<h.length;m++)f[t.scope.from+m]=h[m];if(!r)return f;const d=new Float64Array(e.mc.nBins);for(let m=0;m<c.nrows;m++){const g=m*c.ncols;for(let p=0;p<c.ncols;p++){const w=c.values[g+p];Number.isFinite(w)&&(d[t.scope.from+p]+=w)}}return P1(e,f,d)}function P1(e,t,n){const s=$g(n);if(s===null)return e.note("No bin carried any coverage, so there was nothing to correct for and the profile is as it was counted."),t;const o=new Float32Array(t.length).fill(Number.NaN);let r=0;for(let i=0;i<t.length;i++){const a=t[i];if(!Number.isFinite(a))continue;const c=s[i];if(!(c>0)){r++;continue}o[i]=a/c}return r>0&&e.note(`${r.toLocaleString()} ${r===1?"bin has":"bins have"} a value but no coverage to divide it by, so they are gaps rather than uncorrected numbers.`),o}function $g(e){let t=0,n=0;for(let r=0;r<e.length;r++){const i=e[r];!Number.isFinite(i)||i<=0||(t+=i,n++)}if(n===0)return null;const s=t/n,o=new Float64Array(e.length);for(let r=0;r<e.length;r++){const i=e[r];o[r]=Number.isFinite(i)&&i>0?i/s:0}return o}async function Eg(e,t,n,s=null){const o=e.mc,r=Qt(o,n);e.framing.value!==""&&e.framing.value!==tt&&e.note(`The map is drawn from the ${e.framing.value} column, but a whole-file pass reads ${tt}. This profile is of the counts.`);const i=new Mg(o.nBins,t.band);let a=!1;return await o.scanPixels({onChunk:c=>{e.throwIfCancelled();for(let l=0;l<c.length;l++){const u=c.bin1[l],h=i.reachable(u);if(!h&&s===null)return a=!0,!1;const f=c.bin2[l],d=c.count[l],m=r===null?d:d*r[u]*r[f];s!==null&&Number.isFinite(m)&&(s[u]+=m,f!==u&&(s[f]+=m)),h&&i.add(u,f,m)}return!0},onProgress:(c,l)=>{a||e.progress(c,l,"reading pixels")}}),e.throwIfCancelled(),e.progress(1,1,"done"),i.finish(r===null?null:c=>Number.isFinite(r[c]))}function Bg(e){const t=typeof e.window=="number"?e.window:10;return Math.max(1,Math.round(t))}function Tg(e){const t=typeof e.scales=="number"?e.scales:1;return Math.max(1,Math.min(4,Math.trunc(t)))}function Og(e){const t=Bg(e),n=[];for(let s=0;s<Tg(e);s++)n.push(t*2**s);return n}function Lg(e,t){const n=e.mc,s=n.chromsizes,{ids:o,missing:r}=be(n,t),i=[];for(const a of o){const c=Ut(n,a);if(c===null)continue;const l=n.chromnames[a];i.push({name:l,span:c,length:s.get(l)??(c.to-c.from)*n.binsize,circular:_e(e,l)})}return{units:i,missing:r}}const _g="Insulation",Dg=async e=>{const t=nt(e.params,"balance",!1),n=e.framing.value===""?tt:e.framing.value,s=Math.max(0,Math.min(1,H(e.params,"minValid",.5))),o=Math.max(0,Math.round(H(e.params,"ignoreDiags",2))),r=Og(e.params),i=r[r.length-1],a=r.filter(x=>am(x,o)<=0);if(a.length===r.length)throw new Error(`Ignoring ${o} diagonals leaves nothing in a diamond ${r.length===1?`${i} bins`:`up to ${i} bins`} wide: it spans separations 0 to ${2*i-2}, so every cell is inside the cut. Ignore fewer, or use a wider window.`);a.length>0&&e.note(`Ignoring ${o} diagonals empties the ${a.join(", ")} bin ${a.length===1?"window":"windows"} — a diamond of ${a[0]} bins spans only separations 0 to ${2*a[0]-2} — so ${a.length===1?"that scale is":"those scales are"} all gaps.`);const c=Ht(e.params,"sequences");if(c!==null&&c.length===0)return e.note("No sequence is ticked, so there is nothing to score."),[];const{units:l,missing:u}=Lg(e,c),h=he(e.mc,u,l.length);if(h!==null&&e.note(h),l.length===0)return[];const f=e.mc.nBins,d=r.map(()=>new Float32Array(f).fill(Number.NaN)),m=Ct(e,l.map(x=>x.span),t),g=[],p=nt(e.params,"call",!0),w=H(e.params,"prominence",.2),b=Math.max(1,Math.round(H(e.params,"spacing",0))),y=(x,v,S)=>{for(let k=0;k<r.length;k++){const C=um(S[k]);for(let B=0;B<v;B++)d[k][x.span.from+B]=C[B];p&&k===0&&g.push({unit:x,score:C,counts:S[k].counts})}};for(let x=0;x<l.length;x++){const v=l[x],S=v.span.to-v.span.from,k=r.map(()=>({sums:new Float64Array(S),counts:new Float64Array(S)}));if(v.circular){await e.breathe(),e.progress(x,l.length,v.name);const C=await De(e,v.span,{hi:2*i,block:m,balance:t,value:n,circular:!0,onTile:async(B,F)=>{await e.breathe(),e.progress(x,l.length,F===1?v.name:`${v.name} · strip ${Math.min(B+1,F)} of ${F}`)}});for(let B=0;B<r.length;B++)k[B]=lm(C,{window:r[B],ignoreDiags:o});y(v,S,k);continue}await j0(e,v.span,{halo:i,block:m,balance:t,value:n},async C=>{await e.breathe(),e.progress(x,l.length,C.total===1?v.name:`${v.name} · part ${C.index+1} of ${C.total}`);for(let B=0;B<r.length;B++){const F=cm({size:C.size,values:C.values},{window:r[B],ignoreDiags:o});for(let $=C.ownFrom;$<C.ownTo;$++)k[B].sums[$]=F.sums[$-C.from],k[B].counts[$]=F.counts[$-C.from]}}),y(v,S,k)}e.progress(l.length,l.length,"done");let A=0;for(const x of g)for(const v of x.counts)A=Math.max(A,v);const N=g.map(({unit:x,score:v,counts:S})=>({unit:x,boundaries:mm(v,S,{minCells:A*s,threshold:w,spacing:b}),window:r[0]})),M=l.filter(x=>x.circular).length;M>0&&e.note(`${M.toLocaleString()} of ${l.length.toLocaleString()} ${l.length===1?"sequence was":"sequences were"} read as circular, so the diamond wraps past the end and every bin is scored — including the ones at the junction, which a linear reading leaves as gaps.`);const I=r.map((x,v)=>Rg(e,d[v],x,r.length>1)).reverse();if(p){const x=N.reduce((v,S)=>v+S.boundaries.length,0);if(x===0)e.note(`No minimum was prominent enough to call at a threshold of ${w}. Lower it, or widen the window.`);else if(I.push(zg(e,N)),e.note(`${x.toLocaleString()} boundaries called across ${N.length} sequences.`),nt(e.params,"domains",!0)){const v=Pg(e,N);v===null?e.note("No sequence had two calls, so no domain has both of its edges and none was made."):(I.push(v.output),e.note(`${v.count.toLocaleString()} domains between them, from each call to the next.`))}}return I};function Rg(e,t,n,s){const o=n*e.mc.binsize;return{kind:"profile",id:`insulation-w${n}`,label:`Insulation · ${rt(o)}`,values:t,extent:[0,e.mc.nBins],binsize:e.mc.binsize,unit:"log2 ratio",...s?{group:_g,mode:"heatmap",groupMode:"overlay"}:{}}}function zg(e,t){const n=new se;let s=0;for(const{unit:o,boundaries:r}of t)for(const i of r){const a=i.bin*e.mc.binsize;n.add(o.name,a,Math.min(o.length,a+e.mc.binsize),{score:i.prominence,name:`boundary_${++s}`,detail:`insulation ${i.score.toFixed(3)} · prominence ${i.prominence.toFixed(3)}`})}return{kind:"intervals",id:"insulation-boundaries",label:"Insulation boundaries",payload:{kind:"features",data:n.finish()},noun:{one:"boundary",many:"boundaries"},display:"lane",look:"point"}}function Pg(e,t){const n=new se;let s=0;for(const{unit:o,boundaries:r}of t)for(const i of g1(r)){const a=i.from*e.mc.binsize,c=Math.min(o.length,i.to*e.mc.binsize);c<=a||(s++,n.add(o.name,a,c,{score:i.strength,name:`domain_${s}`,detail:`${i.to-i.from} bins · weaker boundary prominence ${i.strength.toFixed(3)}`}))}return s===0?null:{count:s,output:{kind:"intervals",id:"insulation-domains",label:"Insulation domains",payload:{kind:"features",data:n.finish()},noun:{one:"domain",many:"domains"},display:"overlay",look:"corner"}}}async function qg(e,t,n,s){const o={from:t,to:n},r=Ct(e,[o],s);if(r!==null)return ci(r,o).values;const i=e.framing.value===""?tt:e.framing.value;return(await e.mc.matrix({rows:[t,n],cols:[t,n],balance:s,value:i})).values}function Ug(e,t,n){const s=new Bn(n>0?Math.floor(t/2):Math.max(0,t-1),n);for(let o=0;o<t;o++)for(let r=0;r<t;r++)s.add(zn(Math.abs(r-o),n),e[o*t+r]);return s.finish()}function Gg(e,t,n,s){const o=new Float64Array(t*t);for(let r=0;r<t;r++)for(let i=0;i<t;i++){const a=e[r*t+i],c=ti(s,zn(Math.abs(i-r),n));o[r*t+i]=Number.isFinite(a)&&Number.isFinite(c)&&c>0?a/c:Number.NaN}return o}function Hg(e,t,n,s){const o=Int32Array.from({length:t},(i,a)=>a),r=h1(e,o,o,n,s);for(let i=0;i<r.length;i++){const a=e[i],c=r[i];r[i]=Number.isFinite(a)&&c>0?a/c:Number.NaN}return r}const Qg=1.1;function zc(e,t,n){const s=new Float64Array(t.length),o=new Float64Array(t.length);for(let r=0;r<t.length;r++)s[r]=r*n,o[r]=t[r]>0?Math.log2(t[r]):Number.NaN;return{name:e,x:s,y:o}}function tr(e,t,n){const s=jr(t,n,Qg,1);if(s.x.length===0)return null;let o=0;for(const r of s.n)o+=r;return{name:e,x:s.x,y:s.y,n:o}}function Kg(...e){let t=Number.POSITIVE_INFINITY,n=Number.NEGATIVE_INFINITY;for(const r of e)for(const i of r)for(const a of i.y)Number.isFinite(a)&&(t=Math.min(t,a),n=Math.max(n,a));if(!(t<=n))return;const s=Math.floor(t),o=Math.ceil(n);return s<o?[s,o]:[s-1,o+1]}const Vg=async e=>{const{ids:t,missing:n}=be(e.mc,Ht(e.params,"sequences")),s=he(e.mc,n,t.length);if(s!==null&&e.note(s),t.length===0)return[];const o=nt(e.params,"balance",!0),r=Math.max(0,Math.min(50,Math.round(H(e.params,"band",2)))),i=Math.max(0,Math.round(H(e.params,"armWindow",50))),a=e.mc.binsize,c=new Float32Array(e.mc.nBins).fill(Number.NaN),l=[],u=[],h=[],f=[];for(let g=0;g<t.length;g++){await e.breathe();const p=t[g],w=e.mc.chromnames[p],[b,y]=e.mc.contigExtent(p),A=y-b;if(e.progress(g,t.length,w),A<8){e.note(`${w}: ${A} bins is too few to have arms at this resolution; left out.`);continue}if(A>qs){e.note(`${w}: ${A.toLocaleString()} bins is past the ${qs.toLocaleString()}-bin limit on a square read whole; left out. Open the file at a coarser resolution.`);continue}const N=_e(e,w),M=T0(e,w),I=Math.max(0,Math.min(A-1,Math.floor((M??0)/a)));M===void 0&&e.note(`${w}: the arms were read from the first base, the origin every sequence has until one is set — on the Chromosomes table, or by Origin and terminus.`);const x={n:A,origin:I,circular:N},v=await qg(e,b,y,o);e.throwIfCancelled();const S=N?A:0,k=Ug(v,A,S),{byDistance:C,byBin:B}=cc(Gg(v,A,S,k),x,r),F=lc(C);for(let L=0;L<A;L++)c[b+L]=B[L]>0?Math.log2(B[L]):Number.NaN;l.push(zc(w,C,a)),await e.breathe(),e.throwIfCancelled();const $=cc(Hg(v,A,x,i),x,r).byDistance,_=lc($,F.reach);u.push(zc(w,$,a));const{within:E,across:O}=U8(v,x);for(const L of[tr(`${w} · along the diagonal`,k,a),tr(`${w} · within an arm`,E,a),tr(`${w} · across the arms`,O,a)])L!==null&&h.push(L);f.push({sequence:w,topology:N?"circular":"linear",origin:(M??0)+1,enrichment:2**F.score,detrended:2**_.score,reach:Number.isFinite(F.reach)?F.reach*a:Number.NaN,armLength:(N?Math.floor(A/2):Math.max(I,A-1-I))*a}),N||e.note(`${w} was read as linear, so an origin near either end leaves it one arm. A bacterial chromosome is usually circular — tick it on the Chromosomes table.`)}if(e.progress(t.length,t.length,"done"),f.length===0)return[];e.note(`Cells within ${r} ${r===1?"bin":"bins"} (${(r*a).toLocaleString()} bp) of the secondary diagonal, over each sequence’s own distance decay.`),e.note("Enrichment after detrend is the same cells over the decay along both arms"+(i===0?", over the whole of it":`, within ${i.toLocaleString()} ${i===1?"bin":"bins"} (${(i*a).toLocaleString()} bp) of each cell along them`)+" — the expectation the map’s arms and Compartments divide by, which takes the secondary diagonal in — read over the same stretch as the enrichment. It is near 1 where that works: it is a control on the detrending, not a second measure of the alignment."),e.note("The decay curves are the two expectations: along the diagonal, the one the enrichment is read over; within an arm and across the arms, the decay along both arms averaged over the whole of them, which the window follows along the arms. Log-binned as P(s)’s expected curve is, from one bin away from each diagonal.");const d=Kg(l,u);return[{kind:"profile",id:"interarm-enrichment",label:`Inter-arm enrichment · ±${r} ${r===1?"bin":"bins"}`,values:c,extent:[0,e.mc.nBins],binsize:a,unit:"log2"},{kind:"curve",id:"interarm-curve",label:"Inter-arm alignment by distance from the origin",series:l,xLabel:"Distance from the origin (bp)",yLabel:"Secondary-diagonal enrichment (log2 O/E)",xScale:"linear",yScale:"linear",...d===void 0?{}:{yDomain:d}},{kind:"curve",id:"interarm-detrended-curve",label:"Inter-arm alignment after detrend, by distance from the origin",series:u,xLabel:"Distance from the origin (bp)",yLabel:"Enrichment over the decay along both arms (log2 O/E)",xScale:"linear",yScale:"linear",...d===void 0?{}:{yDomain:d}},{kind:"curve",id:"interarm-decay",label:"Contact decay along the diagonal and along both arms",series:h,xLabel:"Separation from the main or the secondary diagonal (bp)",yLabel:"Mean contact per bin pair",xScale:"log",yScale:"log"},{kind:"table",id:"interarm-table",label:"Inter-arm alignment per sequence",columns:[{key:"sequence",label:"sequence",kind:"text"},{key:"topology",label:"read as",kind:"text"},{key:"origin",label:"origin",kind:"number",hint:"The origin the arms were read from, 1-based."},{key:"enrichment",label:"enrichment",kind:"number",digits:3,hint:"How many times as often mirror images touch as expected, from the origin out to the reach — how strong the alignment is where it holds — or over the whole arm where nothing reaches: 1 is no alignment, 2 twice as often. Two to the mean log2, so enrichment and depletion weigh alike."},{key:"detrended",label:"enrichment after detrend",kind:"number",digits:3,hint:"The same cells over the decay along both arms, as the map’s arms and Compartments divide by, read over the same stretch as the enrichment: 1 is no alignment left over — a control on that detrending."},{key:"reach",label:"reach",kind:"number",hint:"How far from the origin, in bp, the enrichment holds before it falls four-fifths of the way from its peak back to 1, on a log scale — where the alignment ends."},{key:"armLength",label:"arm",kind:"number",hint:"Length of the longer arm, in bp."}],rows:f}]};class Yg{constructor(t,n,s){this.nBins=t,this.contigOf=n,this.binsize=s,this.ends=new Float64Array(t),this.thresholdBp=$e.map(o=>o*1e3)}ends;contacts=0;cis=0;trans=0;diagonal=0;pixels=0;cisAtLeast=new Float64Array($e.length);thresholdBp;addPixels(t,n,s,o){for(let r=0;r<o;r++){const i=s[r];if(!(i>0))continue;const a=t[r],c=n[r];if(a<0||a>=this.nBins||c<0||c>=this.nBins)continue;this.pixels++,this.contacts+=i,this.ends[a]+=i,this.ends[c]+=i;const l=this.contigOf[a];if(l<0||l!==this.contigOf[c]){this.trans+=i;continue}this.cis+=i,a===c&&(this.diagonal+=i);const u=Math.abs(c-a)*this.binsize;for(let h=0;h<this.thresholdBp.length;h++)u>=this.thresholdBp[h]&&(this.cisAtLeast[h]+=i)}}finish(){return{contacts:this.contacts,cis:this.cis,trans:this.trans,diagonal:this.diagonal,cisAtLeast:$e.map((t,n)=>[t,this.cisAtLeast[n]]),pixels:this.pixels,ends:this.ends}}}function Wg(e,t,n){const s=new Set;if(!(e>0))return[];const o=Math.max(e,n/10);for(let r=e;r<=o;r*=2)s.add(r);for(const r of t)r>=e&&r%e===0&&s.add(r);return[...s].sort((r,i)=>i-r)}function Xg(e,t,n,s){const o=[];for(const r of s){const i=r/n;if(!Number.isInteger(i)||i<1)continue;let a=0;for(const u of t)a+=Math.ceil(u.bins/i);const c=new Float64Array(a);let l=0;for(const u of t)for(let h=0;h<u.bins;h+=i){let f=0;const d=Math.min(u.bins,h+i);for(let m=h;m<d;m++)f+=e[u.startBin+m];c[l++]=f}o.push(Jg(r,c))}return o}function Jg(e,t){const n=t.length;if(n===0)return{binsize:e,bins:0,mean:Number.NaN,median:Number.NaN,reached80:Number.NaN,empty:Number.NaN,belowTenthMedian:Number.NaN};const s=Float64Array.from(t).sort();let o=0,r=0;for(const l of s)o+=l,l===0&&r++;const i=n%2===1?s[(n-1)/2]:(s[n/2-1]+s[n/2])/2,a=i/10;let c=0;for(const l of s)if(l<a)c++;else break;return{binsize:e,bins:n,mean:o/n,median:i,reached80:s[Math.floor(.2*n)],empty:r/n,belowTenthMedian:c/n}}function q1(e){return e.entryGroup!=="resolutions"?[]:e.entries.filter(t=>/^\d+$/.test(t)).map(Number).filter(t=>t>0)}function Zg(e){const t=[];for(let n=0;n<e.nContigs;n++){const[s,o]=e.contigExtent(n);t.push({startBin:s,bins:Math.max(0,o-s)})}return t}async function jg(e){const t={mc:e.mc,close:()=>{}};if(lt(e.params,"base","finest")!=="finest")return t;const n=q1(e.mc);if(n.length===0)return t;const s=Math.min(...n);if(s>=e.mc.binsize)return t;const o=e.inputs.self;if(o===void 0||e.peer===void 0)return e.note(`The file’s finest resolution is ${rt(s)}, and it could not be opened beside this map here, so the ladder starts at this map’s ${rt(e.mc.binsize)}.`),t;const{path:r}=Pt(o.uri);return e.progress(0,1,`opening the ${rt(s)} resolution`),await e.peer({...o,label:`${o.label} at ${rt(s)}`,uri:Gu(r,`/resolutions/${s}`)})}const tp=async e=>{const t=e.inputs.upstream??{},n=[],s=await jg(e);let o,r,i;try{const l=s.mc;i=l.binsize;const u=Zg(l),h=new Yg(l.nBins,Pn(u,l.nBins),l.binsize);e.progress(0,1,`reading pixels at ${rt(l.binsize)}`),await l.scanPixels({onChunk:m=>{e.throwIfCancelled(),h.addPixels(m.bin1,m.bin2,m.count,m.length)},onProgress:(m,g)=>e.progress(m,g,`reading pixels at ${rt(l.binsize)}`)}),e.throwIfCancelled(),o=h.finish();let f=0;for(const m of u)f=Math.max(f,m.bins*l.binsize);const d=Wg(l.binsize,q1(l),f);r=Xg(o.ends,u,l.binsize,d)}finally{s.close()}e.note(`The library and the resolutions are counted at ${rt(i)}`+(i===e.mc.binsize?", this map’s resolution":", the file’s finest")+", from the raw counts. A contact is counted at both its ends, as Juicer’s `calculate_map_resolution.sh` counts it, so a coarser bin holds exactly the sum of its finer ones."),i>1e3&&e.note(`Cis contacts at least 1, 2 and 4 kb apart are of bins at least that far apart, and at ${rt(i)} a bin’s neighbour is already ${rt(i)} away.`),n.push(ep(o,e.inputs.pairsStats)),n.push(np(r));const a=sp(r);a!==null&&n.push(a),n.push(...Pc(t.expected,{ps:"report-ps","ps-slope":"report-ps-slope"}));const c=t.coverage?.outputs.find(l=>l.kind==="profile"&&l.id==="coverage-total");if(c!==void 0){n.push(rp(c));const l=ip(c);l!==null&&(n.push(l.curve),l.empty>0&&e.note(`${l.empty.toLocaleString()} of ${l.bins.toLocaleString()} bins at ${rt(c.binsize)} have no contact at all and are left out of the spread of coverage, whose axis is logarithmic.`))}return n.push(...Pc(t.cistrans,{cistrans:"report-cistrans","cistrans-fractions":"report-cistrans-fractions"})),n};function Pc(e,t){if(e===void 0)return[];const n=[];for(const[s,o]of Object.entries(t)){const r=e.outputs.find(i=>i.id===s);r!==void 0&&r.kind!=="profile"&&r.kind!=="intervals"&&n.push({...r,id:o})}return n}function ep(e,t){const n=[],s=(r,i)=>i>0?r/i:Number.NaN,o=(r,i,a,c)=>{n.push({group:r,measure:i,value:a,share:c===null?Number.NaN:s(a,c)})};if(t!==void 0){o("Records","pairs",t.total,null),o("Records","mapped on both sides",t.totalMapped,t.total),o("Records","mapped on one side",t.totalSingleSidedMapped,t.total),o("Records","unmapped",t.totalUnmapped,t.total),o("Records","duplicates",t.totalDups,t.totalMapped),o("Records","unique",t.totalNodups,t.totalMapped),o("Records","cis",t.cis,t.totalNodups),o("Records","trans",t.trans,t.totalNodups);for(const[r,i]of t.cisAtLeast)o("Records",`cis ≥ ${r} kb`,i,t.totalNodups)}o("Map","contacts",e.contacts,null),o("Map","cis",e.cis,e.contacts),o("Map","trans",e.trans,e.contacts),o("Map","within one bin",e.diagonal,e.contacts);for(const[r,i]of e.cisAtLeast)o("Map",`cis, bins ≥ ${r} kb apart`,i,e.contacts);return o("Map","pixels with a count",e.pixels,null),{kind:"table",id:"report-summary",label:"The library",columns:[{key:"group",label:"Counted from",kind:"text"},{key:"measure",label:"Measure",kind:"text"},{key:"value",label:"Value",kind:"number",digits:0},{key:"share",label:"Share",kind:"number",digits:4,hint:"Of every contact; duplicates of the mapped pairs, and cis and trans of the unique ones."}],rows:n}}function np(e){return{kind:"table",id:"report-resolutions",label:"Resolutions, coarsest to finest",columns:[{key:"binsize",label:"Bin size (bp)",kind:"number",digits:0},{key:"bins",label:"Bins",kind:"number",digits:0},{key:"mean",label:"Mean contacts per bin",kind:"number",digits:1},{key:"median",label:"Median",kind:"number",digits:1},{key:"reached80",label:"Reached by 80 % of bins",kind:"number",digits:0,hint:"The largest count at least four bins in five hold: Rao et al.’s criterion, as a number."},{key:"empty",label:"Empty bins",kind:"number",digits:4,hint:"As a fraction of the bins."},{key:"belowTenthMedian",label:"Below a tenth of the median",kind:"number",digits:4,hint:"Bins with fewer contacts than a tenth of the median, the empty ones included, as a fraction."}],rows:e.map(t=>({...t}))}}function sp(e){const t=[...e].filter(o=>o.bins>0).sort((o,r)=>o.binsize-r.binsize);if(t.length===0)return null;const n=Float64Array.from(t,o=>o.binsize),s=o=>o>0?o:Number.NaN;return{kind:"curve",id:"report-resolution-curve",label:"Contacts per bin, by bin size",series:[{name:"median",x:n,y:Float64Array.from(t,o=>s(o.median)),mark:"points"},{name:"reached by 80 % of bins",x:n,y:Float64Array.from(t,o=>s(o.reached80)),mark:"points"}],xLabel:"Bin size (bp)",yLabel:"Contacts per bin (both ends)",xScale:"log",yScale:"log"}}const op=3e3;function rp(e){const t=e.values.length,n=Math.max(1,Math.ceil(t/op)),s=[],o=[];for(let r=0;r<t;r+=n){let i=0,a=0;const c=Math.min(t,r+n);for(let l=r;l<c;l++){const u=e.values[l];Number.isFinite(u)&&(i+=u,a++)}s.push((e.extent[0]+(r+c)/2)*e.binsize/1e6),o.push(a>0?i/a:Number.NaN)}return{kind:"curve",id:"report-coverage",label:n===1?"Coverage along the genome":`Coverage along the genome, a mean of every ${n} bins`,series:[{name:"coverage",x:Float64Array.from(s),y:Float64Array.from(o)}],xLabel:"Position, sequences end to end (Mb)",yLabel:`Contacts per ${rt(e.binsize)} bin`,xScale:"linear",yScale:"linear"}}function ip(e){let t=Number.POSITIVE_INFINITY,n=0,s=0,o=0;for(const u of e.values)if(Number.isFinite(u)){if(o++,u<=0){s++;continue}t=Math.min(t,u),n=Math.max(n,u)}if(!(n>0))return null;const r=40,i=Math.log10(t),a=Math.max(1e-9,(Math.log10(n)-i)/r),c=new Float64Array(r);for(const u of e.values)u>0&&c[Math.min(r-1,Math.floor((Math.log10(u)-i)/a))]++;const l=Float64Array.from({length:r},(u,h)=>10**(i+(h+.5)*a));return{empty:s,bins:o,curve:{kind:"curve",id:"report-coverage-spread",label:"Spread of coverage",series:[{name:"bins",x:l,y:c,mark:"bars"}],xLabel:`Contacts per ${rt(e.binsize)} bin`,yLabel:"Bins",xScale:"log",yScale:"linear"}}}const ap="shared";function cp(e,t){const n=new se;for(const o of e)n.add(o.contig,o.start,o.end,{xyz:[o.x,o.y,o.z],hap:0});const s={width:t,haplotypes:[ap]};return n.finish(null,s)}function U1(e,t,n){const s=n.length;for(let o=0;o<s;o++){const r=n[o]*t,i=o*s;for(let a=0;a<s;a++)e[i+a]=e[r+n[a]]}}function lp(e,t,n){const s=-1/n;for(let o=0;o<t;o++){for(let r=o+1;r<t;r++){const i=e[o*t+r],a=e[r*t+o],c=Number.isFinite(i)&&i>0,l=Number.isFinite(a)&&a>0,u=!c&&!l?Number.NaN:Math.pow(c&&l?(i+a)/2:c?i:a,s);e[o*t+r]=u,e[r*t+o]=u}e[o*t+o]=0}}function up(e,t,n,s){const o=e;let r=0;for(let m=0;m<t;m++)r=Math.max(r,s[m]);const i=new Float64Array(r+1),a=new Float64Array(r+1);let c=0,l=0;for(let m=0;m<t;m++)for(let g=m+1;g<t;g++){const p=o[m*t+g];if(!Number.isNaN(p))if(n[m]===n[g]){const w=Math.abs(s[m]-s[g]);i[w]+=p,a[w]+=1}else c+=p,l+=1}const u=new Float64Array(r+1).fill(Number.NaN);let h=0;for(let m=0;m<=r;m++)a[m]!==0&&(u[m]=i[m]/a[m],u[m]>h&&(h=u[m]));const f=new Float64Array(r+1);for(let m=0;m<=r;m++){if(!Number.isNaN(u[m])){f[m]=u[m];continue}let g=Number.NaN;for(let p=1;p<=r&&Number.isNaN(g);p++){const w=m-p,b=m+p;w>=0&&!Number.isNaN(u[w])?g=u[w]:b<=r&&!Number.isNaN(u[b])&&(g=u[b])}f[m]=Number.isNaN(g)?1:g}const d=l>0?c/l:h>0?h:1;for(let m=0;m<t;m++)for(let g=m+1;g<t;g++){if(!Number.isNaN(o[m*t+g]))continue;const p=n[m]===n[g]?f[Math.abs(s[m]-s[g])]:d;e[m*t+g]=p,e[g*t+m]=p}}function*hp(e,t,n=80){const s=new Float64Array(t);let o=0;for(let l=0;l<t;l++){let u=0;const h=l*t;for(let f=0;f<t;f++){const d=e[h+f];u+=d*d}s[l]=u/t,o+=u}o/=t*t;const r=(l,u)=>fp(e,t,s,o,l,u),i=[Ns(t,1),Ns(t,2),Ns(t,3)];qc(i);const a=[new Float64Array(t),new Float64Array(t),new Float64Array(t)];for(let l=0;l<n;l++){for(let u=0;u<3;u++)r(i[u],a[u]);for(let u=0;u<3;u++)i[u].set(a[u]);qc(i),yield{sweep:l+1,sweeps:n}}const c=i.map((l,u)=>{r(l,a[u]);let h=0;for(let m=0;m<t;m++)h+=l[m]*a[u][m];const f=Math.sqrt(Math.max(0,h)),d=new Float64Array(t);for(let m=0;m<t;m++)d[m]=l[m]*f;return d});return{x:c[0],y:c[1],z:c[2]}}function fp(e,t,n,s,o,r){let i=0,a=0;for(let l=0;l<t;l++)i+=o[l],a+=n[l]*o[l];const c=s*i-a;for(let l=0;l<t;l++){const u=l*t;let h=0;for(let f=0;f<t;f++){const d=e[u+f];h+=d*d*o[f]}r[l]=-.5*(h-n[l]*i+c)}}function Ns(e,t){const n=new Float64Array(e);let s=2463534242+t*7919;for(let o=0;o<e;o++)s^=s<<13,s^=s>>>17,s^=s<<5,n[o]=(s>>>0)/4294967296*2-1;return n}function qc(e){const t=e[0].length;for(let n=0;n<e.length;n++){const s=e[n];for(let r=0;r<n;r++){const i=e[r];let a=0;for(let c=0;c<t;c++)a+=i[c]*s[c];for(let c=0;c<t;c++)s[c]-=a*i[c]}let o=0;for(let r=0;r<t;r++)o+=s[r]*s[r];if(o=Math.sqrt(o),o<1e-12){s.set(Ns(t,11+n)),n--;continue}for(let r=0;r<t;r++)s[r]/=o}}function dp(e,t,n,s=null,o){const r=o??vr(e,t,n,s),{x:i,y:a,z:c}=n;for(let l=0;l<t;l++){let u=0,h=0,f=0,d=0;const m=l*t,g=i[l],p=a[l],w=c[l];for(let b=0;b<t;b++){if(b===l)continue;const y=e[m+b];if(Number.isNaN(y))continue;const A=s===null?1:s[m+b];if(!(A>0))continue;const N=g-i[b],M=p-a[b],I=w-c[b],x=Math.sqrt(N*N+M*M+I*I),v=x>1e-12?y/x:0;u+=A*(i[b]+v*N),h+=A*(a[b]+v*M),f+=A*(c[b]+v*I),d+=A}d<=0||(i[l]=u/d,a[l]=h/d,c[l]=f/d)}return r}function vr(e,t,n,s=null){const{x:o,y:r,z:i}=n;let a=0,c=0;for(let l=0;l<t;l++)for(let u=l+1;u<t;u++){const h=e[l*t+u];if(Number.isNaN(h))continue;const f=s===null?1:s[l*t+u];if(!(f>0))continue;const d=o[l]-o[u],m=r[l]-r[u],g=i[l]-i[u],p=h-Math.sqrt(d*d+m*m+g*g);a+=f*p*p,c+=f*h*h}return c>0?a/c:0}function mp(e){const{x:t,y:n,z:s}=e,o=t.length;if(o===0)return;let r=0,i=0,a=0;for(let u=0;u<o;u++)r+=t[u],i+=n[u],a+=s[u];r/=o,i/=o,a/=o;let c=0;for(let u=0;u<o;u++)t[u]-=r,n[u]-=i,s[u]-=a,c+=t[u]*t[u]+n[u]*n[u]+s[u]*s[u];const l=Math.sqrt(c/o);if(!(l<=0))for(let u=0;u<o;u++)t[u]/=l,n[u]/=l,s[u]/=l}function G1(e,t){const n=e.length;if(n<3)return Number.NaN;const s=Uc(e),o=Uc(t);let r=0,i=0;for(let u=0;u<n;u++)r+=s[u],i+=o[u];r/=n,i/=n;let a=0,c=0,l=0;for(let u=0;u<n;u++){const h=s[u]-r,f=o[u]-i;a+=h*f,c+=h*h,l+=f*f}return c>0&&l>0?a/Math.sqrt(c*l):Number.NaN}function Uc(e){const t=e.length,n=Array.from({length:t},(r,i)=>i).sort((r,i)=>e[r]-e[i]),s=new Float64Array(t);let o=0;for(;o<t;){let r=o;for(;r+1<t&&e[n[r+1]]===e[n[o]];)r++;const i=(o+r)/2+1;for(let a=o;a<=r;a++)s[n[a]]=i;o=r+1}return s}function Qs(e,t,n){const s=e.contigs.indexOf(t);if(s<0)return[];const o=e.model,r=[];for(let i=e.contigAt[s];i<e.contigAt[s+1];i++)o!==void 0&&n>=0&&o.hap[i]!==n||r.push(i);return r}function gp(e,t,n,s,o){const r=new Float64Array(o),i=new Float64Array(o),a=new Float64Array(o),c=new Int32Array(o);for(const u of n){if(!Number.isFinite(t.x[u])||!Number.isFinite(t.y[u])||!Number.isFinite(t.z[u]))continue;const h=Math.floor((e.start[u]+e.end[u])/2/s);h<0||h>=o||(r[h]+=t.x[u],i[h]+=t.y[u],a[h]+=t.z[u],c[h]++)}const l=new Uint8Array(o);for(let u=0;u<o;u++)c[u]!==0&&(r[u]/=c[u],i[u]/=c[u],a[u]/=c[u],l[u]=1);return{n:o,x:r,y:i,z:a,present:l,beads:c}}function pp(e){const t=e.n,n=new Float32Array(t*t).fill(Number.NaN);for(let s=0;s<t;s++)if(e.present[s]!==0){n[s*t+s]=0;for(let o=s+1;o<t;o++){if(e.present[o]===0)continue;const r=Math.hypot(e.x[o]-e.x[s],e.y[o]-e.y[s],e.z[o]-e.z[s]);n[s*t+o]=r,n[o*t+s]=r}}return n}function wp(e,t){const n=new Float32Array(e.length).fill(Number.NaN);for(let s=0;s<e.length;s++){const o=e[s];Number.isNaN(o)||(n[s]=o>0?1/o**t:Number.NaN)}return n}function H1(e,t){const n=new Float64Array(t),s=new Float64Array(t);for(let r=0;r<t;r++)for(let i=r+1;i<t;i++){const a=e[r*t+i];Number.isNaN(a)||(n[i-r]+=a,s[i-r]++)}const o=new Float64Array(t).fill(Number.NaN);for(let r=1;r<t;r++)s[r]>0&&(o[r]=n[r]/s[r]);return o}function bp(e,t){return H1(e,t)}function Ap(e,t,n,s=2){const o=[],r=[];for(let i=0;i<n;i++)for(let a=i+s;a<n;a++){const c=e[i*n+a],l=t[i*n+a];Number.isNaN(c)||c<=0||!Number.isFinite(l)||(o.push(1/c),r.push(l))}return{rho:o.length<3?Number.NaN:i0(o,r),pairs:o.length}}function kr(e,t){let n=0,s=0,o=0,r=0;for(const i of t)!Number.isFinite(e.x[i])||!Number.isFinite(e.y[i])||!Number.isFinite(e.z[i])||(n+=e.x[i],s+=e.y[i],o+=e.z[i],r++);return r===0?null:[n/r,s/r,o/r]}function Gc(e,t,n){const s=new Float64Array(t.length).fill(Number.NaN);return t.forEach((o,r)=>{Number.isFinite(e.x[o])&&(s[r]=Math.hypot(e.x[o]-n[0],e.y[o]-n[1],e.z[o]-n[2]))}),s}function yp(e,t,n){const s=new Float64Array(t.length).fill(Number.NaN),o=Math.max(1,Math.floor(n));for(let r=0;r<t.length;r++){const i=t[r];if(!Number.isFinite(e.x[i]))continue;let a=0,c=0;for(let l=-o;l<=o;l++){if(l===0)continue;const u=t[r+l];u===void 0||!Number.isFinite(e.x[u])||(a+=Math.hypot(e.x[u]-e.x[i],e.y[u]-e.y[i],e.z[u]-e.z[i]),c++)}c>0&&(s[r]=a/c)}return s}function er(e,t,n,s,o,r,i){const a=new Float64Array(r),c=new Int32Array(r);t.forEach((l,u)=>{const h=n[u];if(!Number.isFinite(h))return;const f=Math.floor((e.start[l]+e.end[l])/2/s);f<0||f>=r||(a[f]+=h,c[f]++)});for(let l=0;l<r;l++)c[l]>0&&o+l<i.length&&(i[o+l]=a[l]/c[l])}const nr=100,Np={gradient:"the gradient: a maximum of the likelihood",value:"pastis’ tolerance: a step bought less than a double can hold",steps:"the step budget — raise it for a closer fit",search:"a line search that found nowhere better to go"},Hc={unchosen:"No structure was chosen. Load a g3d or bed3d and pick it under Structure.",gone:"The structure that was chosen is no longer in the list."};function Q1(e,t){const n=[];for(const s of t.contigs){const o=e.mc.chromnames.indexOf(s);if(o<0)continue;const[r,i]=e.mc.contigExtent(o);i>r&&n.push({name:s,cid:o,span:{from:r,to:i}})}return n}function K1(e){return e.haplotypes.length<=1?[{index:-1,label:""}]:e.haplotypes.map((t,n)=>({index:n,label:t}))}function V1(e){const t=Ds(e.inputs.features,e.params,"structure"),n=B0(t,Hc);if(n!==null||t.value===void 0)return e.note(n??Hc.unchosen),null;const s=t.value;if(s.model===void 0)return e.note("The chosen row is not a structure: it has no coordinates to read."),null;const o=typeof e.params.structure=="string"?e.params.structure:"";return{set:s,model:s.model,label:e.inputs.labels?.[o]??"structure"}}const Mp=async e=>{const t=V1(e);if(t===null)return[];const{set:n,model:s,label:o}=t,r=Q1(e,n);if(r.length===0)return e.note("The model and the map share no sequence name. Check the names in the Tracks card."),[];const i=nt(e.params,"balance",!1),a=e.framing.value===""?tt:e.framing.value,c=H(e.params,"alpha",3),l=(Yr(lt(e.params,"region",""))[0]??"").split(":")[0].trim(),u=r.find(w=>w.name===l)??r[0],h=K1(s),f=e.mc.binsize,d=[],m=[];let g=0;for(const w of r){e.throwIfCancelled(),e.progress(g++,r.length,w.name);const b=w.span.to-w.span.from,y=await io(e,w.span,{block:Ct(e,[w.span],i),balance:i,value:a});for(const A of h){const N=Qs(n,w.name,A.index);if(N.length===0)continue;const M=gp(n,s,N,f,b),I=pp(M),x=Ap(I,y.values,b);if(m.push({sequence:w.name,haplotype:A.label,bins:Array.from(M.present).filter(k=>k===1).length,pairs:x.pairs,rho:x.rho}),w!==u)continue;const v=A.label===""?"":` · ${A.label}`,S=Array.from({length:b},(k,C)=>rt(C*f));d.push(Qc(`structure-distance-${A.index}`,`Model distances · ${w.name}${v}`,b,I,S,"viridis"),Qc(`structure-predicted-${A.index}`,`Predicted contacts 1/d^${c} · ${w.name}${v}`,b,wp(I,c),S,"fall")),d.push(...Ip(w.name,v,A.index,I,y.values,b,f))}}e.progress(r.length,r.length);const p={kind:"table",id:"structure-agreement",label:`Model versus map · ${o}`,columns:[{key:"sequence",label:"Sequence",kind:"text"},...h.length>1?[{key:"haplotype",label:"Haplotype",kind:"text"}]:[],{key:"bins",label:"Bins modelled",kind:"number",digits:0},{key:"pairs",label:"Pairs compared",kind:"number",digits:0},{key:"rho",label:"Spearman ρ (1/d vs contacts)",kind:"number",digits:3,hint:"Rank correlation between the inverse model distance and the observed contacts, over pairs at least two bins apart. 1 is a model that orders every pair as the map does."}],rows:m};return d.push(p),d};function Qc(e,t,n,s,o,r){return{kind:"matrix",id:e,label:t,nrows:n,ncols:n,values:s,xLabel:"Position",yLabel:"Position",xTicks:o,yTicks:o,colormap:r}}function Ip(e,t,n,s,o,r,i){const a=H1(s,r),c=bp(o,r),l=(d,m)=>{const g={maxOffset:r-1,sum:new Float64Array(r),count:new Float64Array(r)};for(let w=1;w<r;w++)Number.isNaN(d[w])||(g.sum[w]=d[w],g.count[w]=1);const p=jr(g,i,1.25,1);return p.x.length<2?null:{name:m,x:p.x,y:p.y,n:r}},u=[],h=l(a,`${e}${t}`);if(h!==null){const d=a0(h.x,h.y);u.push({kind:"curve",id:`structure-dvs-${n}`,label:`Model distance by separation · ${e}${t}`,series:[h],xLabel:"Genomic separation (bp)",yLabel:"Mean distance (model units)",xScale:"log",yScale:"log"}),d.x.length>0&&u.push({kind:"curve",id:`structure-dvs-slope-${n}`,label:`Log-log slope of the model’s distance · ${e}${t}`,series:[{name:`${e}${t}`,x:d.x,y:d.y}],xLabel:"Genomic separation (bp)",yLabel:"d log d / d log s",xScale:"log",yScale:"linear"})}const f=l(c,`${e} · map`);return f!==null&&u.push({kind:"curve",id:`structure-ps-${n}`,label:`The map’s P(s) over the same bins · ${e}`,series:[f],xLabel:"Genomic separation (bp)",yLabel:"Mean contact per bin pair",xScale:"log",yScale:"log"}),u}const xp=async e=>{const t=V1(e);if(t===null)return[];const{set:n,model:s,label:o}=t,r=Q1(e,n);if(r.length===0)return e.note("The model and the map share no sequence name. Check the names in the Tracks card."),[];const i=e.mc.binsize,a=e.mc.nBins,c=H(e.params,"window",5),l=lt(e.params,"locus","").trim(),u=K1(s),h=[];for(const f of u){const d=f.label===""?"":` · ${f.label}`,m=r.flatMap(N=>Qs(n,N.name,f.index)),g=kr(s,m),p=new Float32Array(a).fill(Number.NaN),w=new Float32Array(a).fill(Number.NaN),b=new Float32Array(a).fill(Number.NaN),y=l===""?null:vp(n,s,l,f.index);for(const N of r){e.throwIfCancelled();const M=Qs(n,N.name,f.index);if(M.length===0)continue;const I=N.span.to-N.span.from;g!==null&&er(n,M,Gc(s,M,g),i,N.span.from,I,p),er(n,M,yp(s,M,c),i,N.span.from,I,w),y!==null&&er(n,M,Gc(s,M,y),i,N.span.from,I,b)}const A=(N,M,I,x)=>({kind:"profile",id:`${N}-${f.index}`,label:`${M}${d} · ${o}`,values:I,extent:[0,a],binsize:i,unit:"model units",colour:x,group:"structure"});g!==null&&h.push(A("structure-radial","Radial position",p,"#1f5fa9")),h.push(A("structure-compaction",`Local compaction (±${Math.round(c)} beads)`,w,"#0f8a5f")),y!==null&&h.push(A("structure-distance",`Distance to the centre of ${Sr(l)}`,b,"#c0392b"))}return l!==""&&!h.some(f=>f.id.startsWith("structure-distance"))&&e.note(`No bead of the model lies at ${Sr(l)}, so the distance track was not made.`),h};function Sr(e){return Yr(e).map(E0).join(", ")}function vp(e,t,n,s){const[o,r]=n.split(":"),i=o?.trim()??"",a=Qs(e,i,s);if(a.length===0)return null;const c=(r??"").replace(/,/g,"").split(/[-–]/),l=Number(c[0]),u=c.length>1?Number(c[1]):l;if(!Number.isFinite(l))return kr(t,a);const h=Math.min(l,u),f=Math.max(l,u),d=a.filter(m=>e.end[m]>h&&e.start[m]<f);return kr(t,d.length>0?d:a.filter(m=>e.start[m]<=h&&e.end[m]>h))}function Y1(e){const t=[];for(let n=0;n<e.mc.nContigs;n++){const[s,o]=e.mc.contigExtent(n);o>s&&t.push({cid:n,from:s,to:o})}return t}async function kp(e,t,n,s,o){const r=e.inputs.block,i=r!==void 0&&r.binsPerCell===1&&r.rowExtent[0]===r.colExtent[0]&&r.rowExtent[1]===r.colExtent[1]&&r.nrows===r.ncols&&e.framing.balance===n,c=t===null?Y1(e).map(w=>({from:w.from,to:w.to})):t;if(c.length===0)return{refusal:"The file has no sequence to model."};if(i&&r!==void 0&&c.every(w=>ai(r,w))){const w=[],b=[],y=[];for(let M=0;M<r.nrows;M++){const I=r.rowExtent[0]+M;c.some(x=>I<x.to&&I+1>x.from)&&(w.push(M),b.push(I),y.push(I+1))}const A=w.length,N=Ms(A);if(N===null)return{refusal:Cr(A,o)};for(let M=0;M<A;M++){const I=w[M]*r.ncols;for(let x=0;x<A;x++)N[M*A+x]=r.values[I+w[x]]}return{cells:{n:A,binFrom:Int32Array.from(b),binTo:Int32Array.from(y),values:N},how:"the block as drawn, one bin to a bead"}}const l=[],u=[],h=[];for(const w of c){const b=[];for(let y=w.from;y<w.to;y++)b.push(l.length),l.push(y),u.push(y+1);h.push(b)}const f=l.length;if(f===0)return{refusal:"The framing holds no bins to model."};const d=Ms(f),m=d===null?null:Ms(f);if(d===null||m===null)return{refusal:Cr(f,o)};const g=c.length*(c.length+1)/2;let p=0;for(let w=0;w<c.length;w++)for(let b=w;b<c.length;b++){await e.breathe(),e.progress(p++,g+2,`reading ${p} of ${g} blocks`);const y=c[w],A=c[b],N=await e.mc.matrix({rows:[y.from,y.to],cols:[A.from,A.to],balance:n,value:s}),M=h[w],I=h[b];for(let x=0;x<N.nrows;x++){const v=M[x];if(v===void 0)continue;const S=x*N.ncols;for(let k=0;k<N.ncols;k++){const C=N.values[S+k];if(!Number.isFinite(C))continue;const B=I[k];B!==void 0&&(d[v*f+B]+=C,m[v*f+B]+=1,v!==B&&(d[B*f+v]+=C,m[B*f+v]+=1))}}}for(let w=0;w<d.length;w++)d[w]=m[w]>0?d[w]/m[w]:Number.NaN;return{cells:{n:f,binFrom:Int32Array.from(l),binTo:Int32Array.from(u),values:d},how:"read from the file, one bin to a bead"}}function Ms(e){try{const t=globalThis.crossOriginIsolated;if(typeof SharedArrayBuffer=="function"&&t!==!1)try{return new Float32Array(new SharedArrayBuffer(4*e*e))}catch{}return new Float32Array(e*e)}catch{return null}}function Cr(e,t){const n=t*4*e*e/1e9;return`${e.toLocaleString()} beads needs about ${n.toFixed(1)} GB ${t===1?"for its matrix":"of matrices"}, and this browser would not give it. Select a coarser resolution, or name fewer sequences.`}function Sp(e,t){if(t==="")return null;const n=[];for(const s of Yr(t)){const o=Cp(e,s);if(o===void 0)return{missing:to(s)[0]};o!==null&&n.push(o)}return n.length===0?null:n}function Cp(e,t){if(t==="")return null;const[n,s,o]=to(t),r=Le(e.mc,n);if(r<0)return;const i=Ut(e.mc,r);if(i===null)return;if(s===null)return i;const a=e.mc.binsize,c=Math.max(i.from,i.from+Math.floor(s/a)),l=o===null?i.to:Math.min(i.to,i.from+Math.ceil(o/a));return l>c?{from:c,to:l}:void 0}const Fp=async e=>{const t=nt(e.params,"balance",!1),n=e.framing.value===""?tt:e.framing.value,s=Math.max(.5,H(e.params,"alpha",3)),o=Math.max(0,Math.round(H(e.params,"iterations",150))),r=lt(e.params,"weights","contacts"),i=r==="sqrt"||r==="equal"?r:"contacts",a=lt(e.params,"method","nb")!=="shrec3d",c=Math.max(0,Math.round(H(e.params,"seed",0))),l=lt(e.params,"region","").trim(),u=Sp(e,l);if(u!==null&&!Array.isArray(u))return e.note(`This file has no sequence called ${u.missing}, under that spelling or any this viewer knows of. It has ${e.mc.chromnames.slice(0,6).join(", ")}${e.mc.chromnames.length>6?", …":""}.`),[];const f=await kp(e,u,a?!1:t,n,a?1:2);if("refusal"in f)return e.note(f.refusal),[];const{cells:d,how:m}=f;e.framing.normalise!==null&&e.note(`The view is drawn ${e.framing.normalise}-normalised and the model is solved on the counts underneath: no read can ask for that normalisation.`),a&&t&&e.note("The view is drawn balanced and pastis-NB is solved on the raw counts underneath, with the file’s weights folded into each pair’s expected count instead (μ = bᵢ·bⱼ·d^(−α), bᵢ = 1/wᵢ scaled to a mean of 1, as pastis scales them). A balanced value is a count times two weights, and overdispersion is a statement about a count: handed one, the negative binomial would find none and become a Poisson. The coverage is still corrected — it is corrected in the mean rather than in the data (§25.6).");const g=Y1(e),p=e.mc.binsize,w=new Int32Array(d.n).fill(-1),b=new Int32Array(d.n),y=[],A=new Map;for(let R=0;R<d.n;R++){const P=d.binFrom[R],z=g.find(J=>J.from<=P&&P<J.to);if(z===void 0)continue;const U=e.mc.chromnames[z.cid],K=e.mc.chromsizes.get(U)??(z.to-z.from)*p,G=(P-z.from)*p,Y=Math.min(K,(Math.min(z.to,d.binTo[R])-z.from)*p);w[R]=z.cid;const V=A.get(z.cid)??0;b[R]=V,A.set(z.cid,V+1),y.push({contig:U,start:G,end:Math.max(G+1,Y),x:0,y:0,z:0})}const N=a?$p(e,d):null;if(N!==null){for(let R=0;R<d.n;R++)if(!Number.isFinite(N[R]))for(let P=0;P<d.n;P++)d.values[R*d.n+P]=Number.NaN,d.values[P*d.n+R]=Number.NaN}const M=Int32Array.from({length:d.n},(R,P)=>P).filter(R=>w[R]>=0);if(M.length<4)return e.note("Fewer than four cells of the block lie on a sequence of this file; nothing to model."),[];const I=M.length,x=new Int32Array(d.n).fill(-1);for(let R=0;R<M.length;R++)x[M[R]]=R;e.progress(0,1,a?"the counts":"contacts to distances"),await e.breathe();const v=Dp(d.values,d.n,M),S=I-v.length;if(v.length<4)return e.note("Fewer than four beads have any counted pair; nothing to model."),[];const k=v.length,C=Int32Array.from(v,R=>w[R]),B=Int32Array.from(v,R=>b[R]);let F,$,_;if(a){const R=Ep(d.values,d.n,v),P=N===null?null:Bp(Float64Array.from(v,Y=>N[Y])),z=s4(R,k,C,B,P),U=o4(z),K=Tp(R,k);i4(R,k,P,s);const G=x4(k,R);try{const Y=nr+o,V=G.mds(R),J=V.threads===1?"":` · ${V.threads} threads`,Z=b4(k,r4(k,U4(c)));let ut=Number.POSITIVE_INFINITY,Q=Z.next();for(;Q.done!==!0;){await e.breathe();const{point:j,gradient:ot,iteration:at,current:ht}=Q.value,ft=await V.evaluate(j,ot);ut=Math.min(ut,ht,ft),e.progress(Math.min(nr-1,at),Y,`MDS start · step ${at}${J} · stress ${ut.toPrecision(3)}`),Q=Z.next(ft)}const W=Q.value;let et=null;if(F=W.coords,o>0){const j=G.likelihood({counts:R,n:k,bias:P,dispersion:U,alpha:s}),ot=j.threads===1?"":` · ${j.threads} threads`,at=A4(k,W.coords,{iterations:o});let ht=Number.POSITIVE_INFINITY,ft=at.next();for(;ft.done!==!0;){await e.breathe();const{point:kt,gradient:pt,iteration:Ft,current:gt}=ft.value,yt=await j.evaluate(kt,pt);ht=Math.min(ht,gt,yt);const Nt=Math.min(o,Ft);e.progress(nr+Nt,Y,`pastis-NB · step ${Nt} of at most ${o}${ot} · −log L ${ht.toExponential(3)}`),ft=at.next(yt)}et=ft.value,F=et.coords}e.progress(Y,Y),_=zp(R,k,F,P),$=Op(K,s,U,z,P!==null,c,W,et,o)}finally{G.close()}}else{const R=Lp(d.values,d.n,v,i);if(R===null)return _p(e,k);U1(d.values,d.n,v);const P=d.values.subarray(0,k*k);lp(P,k,s);const z=80,U=z+o+1;e.progress(0,U,"classical scaling"),await e.breathe(),up(P,k,C,B);const K=hp(P,k,z);let G=K.next();for(;G.done!==!0;)await e.breathe(),e.progress(G.value.sweep,U,`classical scaling · sweep ${G.value.sweep} of ${G.value.sweeps}`),G=K.next();F=G.value;let Y=vr(P,k,F,R),V=0;for(;V<o;V++){await e.breathe();const J=Y;dp(P,k,F,R,J);const Z=vr(P,k,F,R);if(Y=Z,e.progress(z+V+1,U,`refining · step ${V+1} of ${o} · stress ${Z.toPrecision(3)}`),J-Z<1e-6*J){V++;break}}e.progress(U,U),_=Rp(P,k,F,R),$=[{what:"Distance law",value:`d = c^(−1/${s})`},{what:"Pairs weighted by",value:i==="contacts"?"the contact count":i==="sqrt"?"the square root of the contact count":"nothing: every counted pair alike"},{what:"Refinement steps",value:`${V.toLocaleString()}`},{what:"Weighted stress",value:Y.toPrecision(3)}]}mp(F);const E=[];for(let R=0;R<k;R++){const P=y[x[v[R]]];E.push({...P,x:F.x[R],y:F.y[R],z:F.z[R]})}const O=cp(E,p),L=new Set(E.map(R=>R.contig)),D=u===null?L.size===1?[...L][0]:`${L.size} sequences`:Sr(l);S>0&&e.note(`${S.toLocaleString()} of ${I.toLocaleString()} cells had no counted pair at all — filtered bins, gaps — and are not in the model.`);const q={kind:"intervals",id:"reconstructed-model",label:`3D model · ${D}`,payload:{kind:"features",data:O},entryKind:"structure",noun:{one:"bead",many:"beads"}},X={kind:"table",id:"reconstructed-fit",label:`Model from the map · ${D}`,columns:[{key:"what",label:"Quantity",kind:"text"},{key:"value",label:"Value",kind:"text"}],rows:[{what:"Method",value:a?"pastis-NB · negative binomial likelihood":"ShRec3D · MDS with SMACOF"},{what:"Contacts",value:m},{what:"Beads",value:`${k.toLocaleString()} of ${d.n.toLocaleString()} cells, ${rt(p)} each`},{what:"Sequences",value:`${L.size.toLocaleString()}`},{what:"Pairs counted",value:`${_.pairs.toLocaleString()} of ${(k*(k-1)/2).toLocaleString()}`},...$,{what:"Spearman ρ (1/d vs contacts)",value:Number.isNaN(_.rho)?"—":_.rho.toFixed(3)}]};return[q,X]};function $p(e,t){if(t.n===0)return null;let n=t.binFrom[0],s=t.binTo[0];for(let i=1;i<t.n;i++)t.binFrom[i]<n&&(n=t.binFrom[i]),t.binTo[i]>s&&(s=t.binTo[i]);if(!(s>n))return null;let o;try{const i=e.mc.binColumn(ae,n,s);if(typeof i[0]=="string")return null;o=i}catch{return null}const r=new Float64Array(t.n);for(let i=0;i<t.n;i++){let a=0,c=0;for(let l=t.binFrom[i];l<t.binTo[i];l++){const u=o[l-n];!Number.isFinite(u)||!(u>0)||(a+=1/u,c++)}r[i]=c>0?a/c:Number.NaN}return r}function Ep(e,t,n){U1(e,t,n);const s=n.length,o=e.subarray(0,s*s);for(let r=0;r<s;r++){o[r*s+r]=0;for(let i=r+1;i<s;i++){const a=o[r*s+i],c=o[i*s+r],l=Number.isFinite(a),u=Number.isFinite(c),h=!l&&!u?Number.NaN:l&&u?(a+c)/2:l?a:c,f=Number.isNaN(h)||h>0?h:0;o[r*s+i]=f,o[i*s+r]=f}}return o}function Bp(e){let t=0;for(const s of e)t+=s;const n=t/e.length;if(n>0)for(let s=0;s<e.length;s++)e[s]/=n;return e}function Tp(e,t){let n=0,s=0;for(let o=0;o<t;o++)for(let r=o+1;r<t;r++){const i=e[o*t+r];Number.isFinite(i)&&(n++,i===0&&s++)}return{observed:n,zeros:s}}function sr(e){return e>=100?e.toFixed(0):e>=10?e.toFixed(1):e.toPrecision(3)}function Op(e,t,n,s,o,r,i,a,c){const l=s.mean.length-1,u=[{what:"Mean of a pair",value:o?`μ = bᵢ · bⱼ · d^(−${t})`:`μ = d^(−${t})`},{what:"Counts",value:o?"raw, with the file’s balancing weights folded into the mean as bᵢ = 1/wᵢ, scaled to a mean of 1":"raw: this file carries no balancing weights"},{what:"Zeros used as evidence",value:`${e.zeros.toLocaleString()} of the ${e.observed.toLocaleString()} pairs the map could see`},{what:"Overdispersion (r)",value:n===null?"none: these counts are Poisson, so this is pastis-PM2":`r = ${sr(Math.exp(n.c0))} · λ^${n.c1.toFixed(3)}, λ the mean, fitted over ${s.mean.length.toLocaleString()} separations: ${sr(La(n,s.mean[0]))} near the diagonal, ${sr(La(n,s.mean[l]))} at ${s.separation[l].toLocaleString()} bins — a smaller r is more overdispersed`},{what:"MDS start",value:`pastis’ MDS from random positions, seed ${r}: ${i.iterations.toLocaleString()} steps, ${i.evaluations.toLocaleString()} evaluations`}];return a===null||u.push({what:"Likelihood steps",value:`${a.iterations.toLocaleString()} of at most ${c.toLocaleString()}, over ${a.evaluations.toLocaleString()} evaluations`},{what:"Stopped on",value:Np[a.stop]},{what:"−log L (less Σ log c!, as pastis gives it)",value:a.value.toExponential(4)}),u}function Lp(e,t,n,s){const o=n.length,r=Ms(o);if(r===null)return null;for(let i=0;i<o;i++){const a=n[i]*t;for(let c=i+1;c<o;c++){const l=e[a+n[c]],u=e[n[c]*t+n[i]],h=Number.isFinite(l)&&l>0,f=Number.isFinite(u)&&u>0;if(!h&&!f)continue;const d=h&&f?(l+u)/2:h?l:u,m=s==="contacts"?d:s==="sqrt"?Math.sqrt(d):1;r[i*o+c]=m,r[c*o+i]=m}}return r}function _p(e,t){return e.note(Cr(t,2)),[]}function Dp(e,t,n){const s=[];for(const o of n){let r=!1;for(const i of n){if(i===o)continue;const a=e[o*t+i],c=e[i*t+o];if(Number.isFinite(a)&&a>0||Number.isFinite(c)&&c>0){r=!0;break}}r&&s.push(o)}return Int32Array.from(s)}function Rp(e,t,n,s){let o=0;for(let l=0;l<t;l++)for(let u=l+1;u<t;u++)s[l*t+u]>0&&o++;const r=Math.max(1,Math.ceil(o/2e5)),i=[],a=[];let c=0;for(let l=0;l<t;l++)for(let u=l+1;u<t;u++){const h=e[l*t+u];s[l*t+u]>0&&c++%r===0&&(i.push(W1(n,l,u)),a.push(-h))}return{rho:G1(i,a),pairs:o}}function zp(e,t,n,s){let o=0;for(let l=0;l<t;l++)for(let u=l+1;u<t;u++)e[l*t+u]>0&&o++;const r=Math.max(1,Math.ceil(o/2e5)),i=[],a=[];let c=0;for(let l=0;l<t;l++)for(let u=l+1;u<t;u++){const h=e[l*t+u];h>0&&c++%r===0&&(i.push(W1(n,l,u)),a.push(s===null?h:h/(s[l]*s[u])))}return{rho:G1(i,a),pairs:o}}function W1(e,t,n){const s=Math.hypot(e.x[t]-e.x[n],e.y[t]-e.y[n],e.z[t]-e.z[n]);return s>0?1/s:Number.POSITIVE_INFINITY}class Pp{constructor(t,n={}){this.nGenomes=t,this.intra=new Float64Array(t),this.inter=new Float64Array(t),this.pairs=n.pairs===!0?new Float64Array(t*t):null}intra;inter;pairs;add(t,n,s){Number.isFinite(s)&&(t<0||n<0||(t===n?this.intra[t]+=s:(this.inter[t]+=s,this.inter[n]+=s),this.pairs!==null&&(this.pairs[t*this.nGenomes+n]+=s,t!==n&&(this.pairs[n*this.nGenomes+t]+=s))))}}class qp{constructor(t,n,s=32){this.nContigs=t,this.genomeOfContig=n,this.maxPartners=s,this.own=new Float64Array(t),this.other=new Float64Array(t),this.partners=new Array(t).fill(null)}own;other;partners;capped=0;add(t,n,s){if(!Number.isFinite(s)||t<0||n<0||t>=this.nContigs||n>=this.nContigs)return;const o=this.genomeOfContig[t],r=this.genomeOfContig[n];if(!(o<0||r<0)){if(o===r){this.own[t]+=s,t!==n&&(this.own[n]+=s);return}this.other[t]+=s,this.other[n]+=s,this.note(t,r,s),this.note(n,o,s)}}note(t,n,s){let o=this.partners[t];o===null&&(o=new Map,this.partners[t]=o);const r=o.get(n);if(r!==void 0){o.set(n,r+s);return}if(o.size>=this.maxPartners){this.capped++;return}o.set(n,s)}topPartner(t){const n=this.partners[t];if(n===null)return null;let s=-1,o=0;for(const[r,i]of n)i>o&&(s=r,o=i);return s<0?null:{genome:s,value:o}}get droppedPartners(){return this.capped}}function Up(e,t,n){const s=new Int32Array(t).fill(-1);for(const o of e){if(o.genome<0||n!==null&&!n.has(o.genome))continue;const r=Math.min(t,o.startBin+o.bins);for(let i=o.startBin;i<r;i++)s[i]=o.genome}return s}function Gp(e,t){const n=new Int32Array(t).fill(-1);for(let s=0;s<e.length;s++){const o=Math.min(t,e[s].startBin+e[s].bins);for(let r=e[s].startBin;r<o;r++)n[r]=s}return n}function uo(e){const t=e.mc,n=new Set(t.displayGenomes()),s=t.chromsizes,o=[],r=new Map;for(let a=0;a<t.nContigs;a++){const[c,l]=t.contigExtent(a),u=t.contigGenomeIds[a],h=t.chromnames[a];if(o.push({name:h,startBin:c,bins:Math.max(0,l-c),genome:u,length:s.get(h)??(l-c)*t.binsize}),!n.has(u))continue;const f=r.get(u);f===void 0?r.set(u,{bins:l-c,contigs:1}):(f.bins+=l-c,f.contigs++)}const i=[...r.entries()].map(([a,c])=>({id:a,name:t.genomeNames[a],bins:c.bins,contigs:c.contigs})).sort((a,c)=>a.id-c.id);return{contigs:o,genomes:i,allowed:n}}function X1(e){e.framing.value!==""&&e.framing.value!==tt&&e.note(`The map is drawn from the ${e.framing.value} column, but a whole-file pass reads ${tt}. These numbers are of the counts.`)}const Hp=async e=>{const t=uo(e);if(t.genomes.length===0)return e.note("This file has no displayable genome, so there is nothing to report per MAG (§7.4)."),[];const n=nt(e.params,"balance",!1),s=Qt(e.mc,n);X1(e);const o=Up(t.contigs,e.mc.nBins,t.allowed),r=new Pp(e.mc.nGenomes,{pairs:nt(e.params,"network",!0)}),i=new Uint8Array(e.mc.nBins);e.progress(0,1,"reading pixels"),await e.mc.scanPixels({onChunk:u=>{e.throwIfCancelled();for(let h=0;h<u.length;h++){const f=u.bin1[h],d=u.bin2[h],m=u.count[h],g=s===null?m:m*s[f]*s[d];Number.isFinite(g)&&(i[f]=1,i[d]=1,r.add(o[f],o[d],g))}return!0},onProgress:(u,h)=>e.progress(u,h,"reading pixels")}),e.throwIfCancelled();const a=new Int32Array(e.mc.nGenomes);for(let u=0;u<e.mc.nBins;u++)i[u]===1&&o[u]>=0&&a[o[u]]++;const c=t.genomes.map(u=>{const h=r.intra[u.id],f=r.inter[u.id],d=h+f;return{genome:u.name,contigs:u.contigs,bins:u.bins,covered:a[u.id],intra:h,inter:f,fraction:d>0?h/d:Number.NaN,perBin:u.bins>0?d/u.bins:Number.NaN}}),l=[{kind:"table",id:"magqc",label:"MAG contact QC",columns:[{key:"genome",label:"MAG",kind:"text"},{key:"contigs",label:"Contigs",kind:"number",digits:0},{key:"bins",label:"Bins",kind:"number",digits:0},{key:"covered",label:"Bins with contact",kind:"number",digits:0},{key:"intra",label:"Intra",kind:"number",digits:0},{key:"inter",label:"Inter",kind:"number",digits:0},{key:"fraction",label:"Intra fraction",kind:"number",digits:3},{key:"perBin",label:"Contacts per bin",kind:"number",digits:2}],rows:[...c].sort((u,h)=>(h.fraction||0)-(u.fraction||0))}];return r.pairs!==null&&l.push(Qp(r.pairs,t,e.mc.nGenomes)),l};function Qp(e,t,n){const s=t.genomes.map(i=>i.id),o=s.length,r=new Float32Array(o*o);for(let i=0;i<o;i++)for(let a=0;a<o;a++)r[i*o+a]=e[s[i]*n+s[a]];return{kind:"matrix",id:"magqc-network",label:"Inter-MAG contacts",nrows:o,ncols:o,values:r,xLabel:"MAG",yLabel:"MAG",xTicks:o<=40?t.genomes.map(i=>i.name):[],yTicks:o<=40?t.genomes.map(i=>i.name):[],colormap:"default"}}const Kp=async e=>{const n=e.inputs.upstream?.magqc?.outputs.find(d=>d.kind==="matrix"&&d.id==="magqc-network");if(n===void 0)throw new Error('The MAG QC step produced no inter-MAG matrix. Turn its "inter-MAG matrix" option on.');const s=n.xTicks.length===n.nrows?n.xTicks:null,o=n.nrows,r=lt(e.params,"normalise","coverage"),i=H(e.params,"minWeight",0),a=Math.max(10,Math.round(H(e.params,"maxEdges",500))),c=new Float64Array(o);for(let d=0;d<o;d++)c[d]=n.values[d*o+d];let l=null;if(r==="bins"){const d=uo(e);if(d.genomes.length!==o)throw new Error(`The MAG QC step's matrix has ${o.toLocaleString()} MAGs and this file has ${d.genomes.length.toLocaleString()}, so its rows cannot be matched to their bin counts. Run MAG contact QC again.`);l=Float64Array.from(d.genomes,m=>m.bins)}const u=(d,m,g)=>{if(r==="coverage")return c[d]>0&&c[m]>0?g/Math.sqrt(c[d]*c[m]):Number.NaN;if(r==="bins"&&l!==null){const p=l[d]*l[m];return p>0?g/p:Number.NaN}return g},h=[];for(let d=0;d<o;d++)for(let m=d+1;m<o;m++){const g=n.values[d*o+m];if(!Number.isFinite(g)||g<=0)continue;const p=u(d,m,g);!Number.isFinite(p)||p<i||h.push({a:s===null?`MAG ${d}`:s[d],b:s===null?`MAG ${m}`:s[m],weight:p,raw:g})}return h.sort((d,m)=>m.weight-d.weight),h.length>a&&e.note(`${(h.length-a).toLocaleString()} weaker edges are not listed.`),[{kind:"table",id:"magnetwork",label:"Inter-MAG edges",columns:[{key:"a",label:"genome1",kind:"text"},{key:"b",label:"genome2",kind:"text"},{key:"raw",label:"contacts",kind:"number",digits:0},{key:"weight",label:"weight",kind:"number",digits:6}],rows:h.slice(0,a)}]},Vp=async e=>{const t=uo(e);if(t.genomes.length===0)return[];const n=nt(e.params,"balance",!1),s=Qt(e.mc,n);X1(e);const o=Gp(t.contigs,e.mc.nBins),r=Int32Array.from(t.contigs.map(m=>t.allowed.has(m.genome)?m.genome:-1)),i=new qp(t.contigs.length,r);e.progress(0,1,"reading pixels"),await e.mc.scanPixels({onChunk:m=>{e.throwIfCancelled();for(let g=0;g<m.length;g++){const p=m.bin1[g],w=m.bin2[g],b=m.count[g],y=s===null?b:b*s[p]*s[w];i.add(o[p],o[w],y)}return!0},onProgress:(m,g)=>e.progress(m,g,"reading pixels")}),e.throwIfCancelled(),i.droppedPartners>0&&e.note(`${i.droppedPartners.toLocaleString()} partner observations were past the per-contig cap, so a few "top partner" calls are from the partners seen first.`);const a=H(e.params,"minContacts",10),c=H(e.params,"flagAbove",.5),l=[],u=new se,h=new Float32Array(e.mc.nBins).fill(Number.NaN);for(let m=0;m<t.contigs.length;m++){const g=t.contigs[m];if(r[m]<0)continue;const p=i.own[m],w=i.other[m],b=p+w,y=b>0?w/b:Number.NaN,A=i.topPartner(m),N=b>=a;if(N&&Number.isFinite(y))for(let M=g.startBin;M<g.startBin+g.bins;M++)h[M]=1-y;l.push({contig:g.name,genome:e.mc.genomeNames[r[m]],contacts:b,outside:N?y:Number.NaN,partner:A===null?"":e.mc.genomeNames[A.genome],flagged:N&&y>c?"yes":""}),N&&y>c&&u.add(g.name,0,g.length,{score:y,name:A===null?"":e.mc.genomeNames[A.genome],detail:`${(y*100).toFixed(1)}% of contact leaves ${e.mc.genomeNames[r[m]]}`})}const f=l.filter(m=>m.flagged==="yes").length;e.note(`${f.toLocaleString()} of ${l.length.toLocaleString()} contigs send more than ${(c*100).toFixed(0)}% of their contact outside their own MAG.`);const d=[{kind:"table",id:"magpurity",label:"Contig purity",columns:[{key:"contig",label:"Contig",kind:"text"},{key:"genome",label:"Assigned to",kind:"text"},{key:"contacts",label:"Contacts",kind:"number",digits:0},{key:"outside",label:"Outside fraction",kind:"number",digits:3},{key:"partner",label:"Top partner",kind:"text"},{key:"flagged",label:"Flagged",kind:"text"}],rows:l.sort((m,g)=>Number(g.outside||0)-Number(m.outside||0))},{kind:"profile",id:"magpurity-track",label:"Contig purity",values:h,extent:[0,e.mc.nBins],binsize:e.mc.binsize,unit:""}];return f>0&&d.push({kind:"intervals",id:"magpurity-flagged",label:"Contigs to look at",payload:{kind:"features",data:u.finish()},noun:{one:"contig",many:"contigs"},display:"overlay",look:"corner"}),d},Yp=async e=>{const t=uo(e),n=nt(e.params,"balance",!1),s=e.framing.value===""?tt:e.framing.value,o=Math.max(2,Math.round(H(e.params,"minContigs",4))),r=H(e.params,"flagBelow",.1),i=t.genomes.filter(h=>h.contigs>=o);if(i.length===0)return e.note(`No MAG has ${o} or more contigs, so none can be split into two blocks.`),[];const a=[],c=new se;for(let h=0;h<i.length;h++){await e.breathe(),e.progress(h,i.length,i[h].name);const f=i[h],d=e.mc.genomeExtent(f.id),m=await e.mc.matrix({rows:[d[0],d[1]],cols:[d[0],d[1]],balance:n,value:s}),g=e.mc.aggregate(m,"contig"),p=g.nrows;if(p<o)continue;const w=new Float64Array(p*p);for(let k=0;k<p;k++)for(let C=0;C<p;C++){const B=g.values[k*p+C];w[k*p+C]=Number.isFinite(B)?B:0}const b=V8(w,p);if(b===null)continue;let y=0,A=0;for(let k=0;k<p;k++)for(let C=k+1;C<p;C++){const B=w[k*p+C];b.side[k]===b.side[C]?y+=B:A+=B}const N=y>0?A/y:Number.NaN,M=e.mc.chromnames,I=[];for(let k=0;k<t.contigs.length;k++)t.contigs[k].genome===f.id&&I.push(k);const x=I.filter((k,C)=>b.side[C]===1).map(k=>M[k]),v=I.filter((k,C)=>b.side[C]===-1).map(k=>M[k]),S=Number.isFinite(N)&&N<r;if(a.push({genome:f.name,contigs:f.contigs,ratio:N,sideA:x.length,sideB:v.length,flagged:S?"yes":""}),S)for(const[k,C]of[["A",x],["B",v]])for(const B of C){const F=t.contigs.find($=>$.name===B);F!==void 0&&c.add(B,0,F.length,{name:`${f.name} · side ${k}`,score:N,detail:`cross-block ratio ${N.toFixed(4)}`})}}e.progress(i.length,i.length,"done");const l=a.filter(h=>h.flagged==="yes").length;e.note(`${l.toLocaleString()} of ${a.length.toLocaleString()} MAGs split into two blocks with less than ${r} of their internal contact crossing.`);const u=[{kind:"table",id:"magsplit",label:"Split MAGs",columns:[{key:"genome",label:"MAG",kind:"text"},{key:"contigs",label:"Contigs",kind:"number",digits:0},{key:"ratio",label:"Cross-block ratio",kind:"number",digits:4},{key:"sideA",label:"Side A",kind:"number",digits:0},{key:"sideB",label:"Side B",kind:"number",digits:0},{key:"flagged",label:"Flagged",kind:"text"}],rows:a.sort((h,f)=>Number(h.ratio||1)-Number(f.ratio||1))}];return l>0&&u.push({kind:"intervals",id:"magsplit-flagged",label:"MAGs that look like two things",payload:{kind:"features",data:c.finish()},noun:{one:"MAG",many:"MAGs"},display:"overlay",look:"corner"}),u},Wp={expected:bg,pairsqc:Q7,mapreport:tp,coverage:g7,cistrans:P8,cellqc:N8,insulation:Dg,di:vm,hicscore:Bm,arrowhead:m8,scalogram:n7,fourc:vg,apa:v6,loops:G9,chromosight:S8,compartments:nm,interarm:Vg,hicrep:l7,structuremap:Mp,spatialtracks:xp,reconstruct3d:Fp,magqc:Hp,magnetwork:Kp,magpurity:Vp,magsplit:Yp};function Xp(e){const t=[],n=e;if(n===null||typeof n!="object")return t;const s=r=>{if(r===void 0)return;const i=r.buffer;i instanceof ArrayBuffer&&!t.includes(i)&&t.push(i)},o=e;if(o.values instanceof Float32Array)return s(o.values),o.over instanceof Float32Array&&s(o.over),t;if(!Array.isArray(n.outputs))return t;for(const r of n.outputs)switch(r.kind){case"profile":s(r.values);break;case"matrix":if(s(r.values),r.streaks!==void 0){const i=r.streaks;for(const a of[i.x0,i.x1,i.y0,i.y1,i.strand,i.anchors])s(a)}break;case"curve":for(const i of r.series)s(i.x),s(i.y),s(i.low),s(i.high);break;case"intervals":{const i=r.payload.data;for(const a of Object.values(i))ArrayBuffer.isView(a)&&s(a);break}}return t}const J1=self,Z1=Math.max(0,Math.min(7,(typeof navigator>"u"?1:navigator.hardwareConcurrency||1)-1));_d(Jd,Z1);I4(k4,Z1);let Be=null;const $t=new Set;function Jp(){if(Be===null)throw new Error("no file is open in the analysis worker");return Be}function ye(e,t=[]){J1.postMessage(e,t)}async function Zp(e){Be?.mc.close(),Be=null,$t.clear();const t=await o0(e.uri,e.source,{report:(o,r,i)=>ye({type:"progress",id:e.id,done:o,total:r,note:i}),cancelled:()=>$t.has(e.id),...e.binning===void 0?{}:{binning:e.binning}}),n=t.source;let s=t.mc;if(e.edit!==void 0){const o=bt.withLens(s,e.edit);if(o.refusal!==null)throw s.close(),new T("unsupported",o.refusal);s=o.map}if(e.cells!==void 0&&s.cells.length>0&&s.selectCells(e.cells),e.resample!==void 0&&await tu(s,e.resample,e.id),e.coarsen!==void 0)try{s=j1(s,e.coarsen)}catch(o){throw s.close(),o}return Be={mc:s,source:n},{nBins:s.nBins,binsize:s.binsize}}function j1(e,t){if(t===e.binsize)return e;const n=t/e.binsize;if(!Number.isInteger(n)||n<1)throw new T("unsupported",`This map's bins are ${e.binsize.toLocaleString("en-US")} bp, and ${t.toLocaleString("en-US")} bp is not a whole number of them, so it is not measured at that resolution.`);const s=bt.coarsened(e,n);if(s.refusal!==null)throw new T("unsupported",s.refusal);return s.map}async function tu(e,t,n){await e.resample({contacts:t.contacts,...t.seed===void 0?{}:{seed:t.seed},...t.alpha===void 0?{}:{alpha:t.alpha}},{...t.census===void 0?{}:{census:t.census},onProgress:(s,o)=>ye({type:"progress",id:n,done:s,total:o,note:"counting contacts"}),cancelled:()=>$t.has(n)})}const jp=0,tw=[h5,f5].filter(e=>e!=="").join(" ");async function ew(e){const t=Wp[e.task];if(t===void 0)throw new Error(`no analysis runner is registered for the task ${e.task}`);const{mc:n}=Jp(),s=e.id,o=[],r=performance.now(),i={mc:n,params:e.params,framing:e.framing,inputs:e.inputs,cancelled:()=>$t.has(s),throwIfCancelled:()=>{if($t.has(s))throw en()},breathe:async()=>{if(await vt(),$t.has(s))throw en()},progress:(c,l,u)=>ye({type:"progress",id:s,done:c,total:l,note:u}),note:c=>{o.push(c)},toBp:c=>c*n.binsize,toBins:c=>Math.max(1,Math.round(c/n.binsize)),openBytes:async c=>await pr({kind:"remote",url:c}),openSource:async c=>await pr(c),peer:async c=>{const l=await o0(c.uri,c.source,{report:(h,f,d)=>ye({type:"progress",id:s,done:h,total:f,note:d}),cancelled:()=>$t.has(s),detached:!0,...c.binning===void 0?{}:{binning:c.binning}});if(c.cells!==void 0&&l.mc.cells.length>0&&l.mc.selectCells(c.cells),c.resample!==void 0)try{await tu(l.mc,c.resample,s)}catch(h){throw l.mc.close(),l.source.close(),h}let u=l.mc;if(c.coarsen!==void 0)try{u=j1(l.mc,c.coarsen)}catch{u=l.mc}return{mc:u,close:()=>{u.close(),l.source.close()}}}},a=await t(i);return i.throwIfCancelled(),{outputs:a,provenance:{task:e.task,taskVersion:jp,params:e.params,framing:e.framing,app:tw,createdAt:new Date().toISOString()},notes:o,ms:performance.now()-r}}async function nw(e){const t=e.id,n=e.n,s=await Vd(e.values,e.n,async o=>{if(o%sw===0){if(await vt(),$t.has(t))throw en();ye({type:"progress",id:t,done:o,total:n,note:"correlating"})}});if($t.has(t))throw en();return s}const sw=64;function ow(e){if($t.has(e.id))throw en();const t=$4({nrows:e.nrows,ncols:e.ncols,values:e.values},e.counts,e.spec,e.over??void 0);if($t.has(e.id))throw en();return{values:t.values,over:t.over,applied:t.applied,scope:t.scope}}async function rw(e){switch(e.kind){case"open":return Zp(e);case"run":return ew(e);case"correlate":return nw(e);case"smooth":return ow(e);case"cancel":return $t.add(e.target),null;case"close":return Be?.mc.close(),Be=null,$t.clear(),En(),null}}async function iw(e){try{const t=await rw(e);ye({type:"result",id:e.id,kind:e.kind,payload:t},Xp(t))}catch(t){ye({type:"error",id:e.id,error:d5(t)})}finally{$t.delete(e.id)}}J1.onmessage=e=>{iw(e.data)};const aw=iu();setInterval(()=>ye({type:"busy",fraction:aw.busy()}),ru);