import{aW as d,aN as b,aR as q,a9 as l,u as x,G as E}from"./index-Bm2SqMk3.js";import{B as F}from"./index-6rN3ss-L.js";import{Q as L}from"./QuickLRU-BERSBA88.js";import{r as k}from"./rxjs-DT7GVwt0.js";function w(r,e){return r.offset+r.lineBytes*Math.floor(e/r.lineLength)+e%r.lineLength}async function N(r,e){const t=await r.readFile(e);if(!(t!=null&&t.length))throw new Error("No data read from FASTA index (FAI) file");let a=0,n;const i=t.toString("utf8").split(/\r?\n/).filter(s=>/\S/.test(s)).map(s=>s.split("	")).filter(s=>s[0]!=="").map(s=>((!n||n.name!==s[0])&&(n={name:s[0],id:a},a+=1),{id:n.id,name:s[0],length:+s[1],start:0,end:+s[1],offset:+s[2],lineLength:+s[3],lineBytes:+s[4]}));return{name:Object.fromEntries(i.map(s=>[s.name,s])),id:Object.fromEntries(i.map(s=>[s.id,s]))}}class O{constructor({fasta:e,fai:t,path:a,faiPath:n}){if(e)this.fasta=e;else if(a)this.fasta=new d(a);else throw new Error("Need to pass filehandle for fasta or path to localfile");if(t)this.fai=t;else if(n)this.fai=new d(n);else if(a)this.fai=new d(`${a}.fai`);else throw new Error("Need to pass filehandle for  or path to localfile")}async _getIndexes(e){return this.indexes||(this.indexes=N(this.fai,e)),this.indexes}async getSequenceNames(e){return Object.keys((await this._getIndexes(e)).name)}async getSequenceSizes(e){const t={},a=await this._getIndexes(e);for(const n of Object.values(a.id))t[n.name]=n.length;return t}async getSequenceSize(e,t){var a;return(a=(await this._getIndexes(t)).name[e])===null||a===void 0?void 0:a.length}async hasReferenceSequence(e,t){return!!(await this._getIndexes(t)).name[e]}async getResiduesById(e,t,a,n){const i=(await this._getIndexes(n)).id[e];if(i)return this._fetchFromIndexEntry(i,t,a,n)}async getResiduesByName(e,t,a,n){const i=(await this._getIndexes(n)).name[e];if(i)return this._fetchFromIndexEntry(i,t,a,n)}async getSequence(e,t,a,n){return this.getResiduesByName(e,t,a,n)}async _fetchFromIndexEntry(e,t=0,a,n){let i=a;if(t<0)throw new TypeError("regionStart cannot be less than 0");if((i===void 0||i>e.length)&&(i=e.length),t>=i)return"";const s=w(e,t),o=w(e,i)-s,u=b.Buffer.allocUnsafe(o);return await this.fasta.read(u,0,o,s,n),u.toString("utf8").replace(/\s+/g,"")}}class j extends F.BaseSequenceAdapter{constructor(){super(...arguments),this.seqCache=new q({cache:new L({maxSize:200}),fill:async e=>{const{refName:t,start:a,end:n,fasta:i}=e;return i.getSequence(t,a,n)}})}async getRefNames(e){const{fasta:t}=await this.setup();return t.getSequenceNames()}async getRegions(e){const{fasta:t}=await this.setup(),a=await t.getSequenceSizes();return Object.keys(a).map(n=>({refName:n,start:0,end:a[n]}))}async setupPre(){const e=this.getConf("fastaLocation"),t=this.getConf("faiLocation");return{fasta:new O({fasta:l.openLocation(e,this.pluginManager),fai:l.openLocation(t,this.pluginManager)})}}async getHeader(){const e=this.getConf("metadataLocation");return e.uri===""||e.uri==="/path/to/fa.metadata.yaml"?null:l.openLocation(e,this.pluginManager).readFile("utf8")}async setup(){return this.setupP||(this.setupP=this.setupPre().catch(e=>{throw this.setupP=void 0,e})),this.setupP}getFeatures(e,t){const{statusCallback:a=()=>{},stopToken:n}=t||{},{refName:i,start:s,end:o}=e;return k.ObservableCreate(async u=>{await x.updateStatus2("Downloading sequence",a,n,async()=>{const{fasta:h}=await this.setup(),_=await h.getSequenceSize(i),g=Math.min(_,o),p=[],f=128e3,m=s-s%f,I=o+(f-o%f);for(let c=m;c<I;c+=f){const S={refName:i,start:c,end:c+f};E.checkStopToken(n),p.push(await this.seqCache.get(JSON.stringify(S),{...S,fasta:h}))}const y=p.filter(c=>!!c).join("").slice(s-m).slice(0,o-s);y&&u.next(new x.SimpleFeature({id:`${i}-${s}-${g}`,data:{refName:i,start:s,end:g,seq:y}}))}),u.complete()})}freeResources(){}}const R=Object.freeze(Object.defineProperty({__proto__:null,default:j},Symbol.toStringTag,{value:"Module"}));export{O as I,j as a,R as b};
