jcampconverter 9.4.2 → 9.5.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.
- package/jcampconverter.cjs +6 -6
- package/jcampconverter.d.ts +26 -0
- package/jcampconverter.mjs +6 -6
- package/package.json +6 -5
package/jcampconverter.cjs
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var be=Object.create;var P=Object.defineProperty;var xe=Object.getOwnPropertyDescriptor;var Ne=Object.getOwnPropertyNames;var Te=Object.getPrototypeOf,Ae=Object.prototype.hasOwnProperty;var Se=(e,i)=>{for(var s in i)P(e,s,{get:i[s],enumerable:!0})},ae=(e,i,s,a)=>{if(i&&typeof i=="object"||typeof i=="function")for(let n of Ne(i))!Ae.call(e,n)&&n!==s&&P(e,n,{get:()=>i[n],enumerable:!(a=xe(i,n))||a.enumerable});return e};var ye=(e,i,s)=>(s=e!=null?be(Te(e)):{},ae(i||!e||!e.__esModule?P(s,"default",{value:e,enumerable:!0}):s,e)),Xe=e=>ae(P({},"__esModule",{value:!0}),e);var we={};Se(we,{convert:()=>ce,createTree:()=>ne,parseJcampCS:()=>he});module.exports=Xe(we);var re=require("ensure-string");var D=/\s*\$\$.*/;function F(e){return e.replace(D,"")}function ne(e,i={}){e=(0,re.ensureString)(e);let{flatten:s=!1}=i;if(typeof e!="string")throw new TypeError("the JCAMP should be a string");let a=e.split(/\r?\n/),n=[],t=[],r=[],l,o=0,p=e.includes("## ");for(let c=0;c<a.length;c++){let d=a[c],u=p?d.replace(/ /g,""):d;if(u.substring(0,9)==="##NTUPLES"&&o++,u.substring(0,7)==="##TITLE"){let f=[u.substring(8).trim()];for(let m=c+1;m<a.length&&!a[m].startsWith("##");m++)f.push(a[m].trim());t.push({title:f.join(`
|
|
2
2
|
`),jcamp:`${d}
|
|
3
|
-
`,children:[]}),l=t[t.length-1],
|
|
4
|
-
`;let
|
|
5
|
-
`;let n=r.match(/^##(.*?)=(.+)/);if(n){let m=n[1].replace(/[ _-]/g,"").toUpperCase();m==="DATATYPE"&&(l.dataType=n[2].trim()),m==="DATACLASS"&&(l.dataClass=n[2].trim())}}r.substring(0,5)==="##END"&&p>0&&p--}return s?(o.forEach(c=>{c.children=void 0}),o):f}var oe=require("dynamic-typing"),pe=require("ensure-string");var D=["TIC",".RIC","SCANNUMBER"];function ae(e){let i=e.spectra,s=i.length,a={times:new Array(s),series:{ms:{dimension:2,data:new Array(s)}}},o=[];for(let t=0;t<D.length;t++){let f=U(D[t]);i[0][f]&&(o.push(f),a.series[f]={dimension:1,data:new Array(s)})}for(let t=0;t<s;t++){let f=i[t];a.times[t]=f.pageValue;for(let l=0;l<o.length;l++)a.series[o[l]].data[t]=Number(f[o[l]]);f.data&&(a.series.ms.data[t]=[f.data.x,f.data.y])}e.chromatogram=a}function re(e){return D.indexOf(e)!==-1}function U(e){return e.toLowerCase().replace(/[^a-z0-9]/g,"")}function Y(e){let i=[];for(let s=0;s<e.length;s++)i.push(Number(e[s]));return i}function V(e,i){let s=e.yFactor,a=e.deltaX;e.isXYdata=!0;let o={x:[],y:[]};e.data=o;let t=e.firstX,f=e.firstY,l=!1,p,u=0;for(;u<i.length;u++)if(p=i.charCodeAt(u),p===13||p===10)l=!0;else if(l)break;let c=!0,d=!1,r=!1,n=0,m=!1,h=!1,g=0,T=0,A=!1,y=!1,N=!1,S=0;for(;u<=i.length;u++)if(u===i.length?p=13:p=i.charCodeAt(u),h)(p===13||p===10)&&(c=!0,h=!1);else if(p<=57&&p>=48)y=!0,S>0?g+=(p-48)/Math.pow(10,S++):(g*=10,g+=p-48);else if(p===44||p===46)y=!0,S++;else{if(y){if(c)c=!1,r&&(N=!0);else if(N)N=!1;else{d?(n=A?0-g:g,r=!0,d=!1):m||(T=A?0-g:g);let O=m?g-1:1;for(let F=0;F<O;F++)r?f+=n:f=T,o.x.push(t),o.y.push(f*s),t+=a}A=!1,g=0,S=0,y=!1,m=!1}if(p<74&&p>63)y=!0,r=!1,g=p-64;else if(p>96&&p<106)y=!0,r=!1,g=p-96,A=!0;else if(p===115)y=!0,m=!0,g=9;else if(p>82&&p<91)y=!0,m=!0,g=p-82;else if(p>73&&p<83)y=!0,d=!0,g=p-73;else if(p>105&&p<115)y=!0,d=!0,g=p-105,A=!0;else if(p===36&&i.charCodeAt(u+1)===36)y=!0,h=!0;else if(p===37)y=!0,d=!0,g=0,A=!1;else if(p===45){let O=i.charCodeAt(u+1);(O>=48&&O<=57||O===44||O===46)&&(y=!0,c||(r=!1),A=!0)}else(p===13||p===10)&&(c=!0,h=!1)}}var fe=/\$\$.*/,ne=/[,\t ]+/;function w(e,i,s){if(e.isPeaktable=!0,!e.variables||Object.keys(e.variables)===2?Xe(e,i,s):ye(e,i,s),e.variables)for(let a in e.variables)e.variables[a].data=e.data[a]}function Xe(e,i,s){let a={x:[],y:[]};e.data=a;let o=i.split(/,? *,?[;\r\n]+ */);for(let t=1;t<o.length;t++){let f=o[t].trim().replace(fe,"").split(ne);if(f.length%2===0)for(let l=0;l<f.length;l=l+2)a.x.push(Number(f[l])*e.xFactor),a.y.push(Number(f[l+1])*e.yFactor);else s.logs.push(`Format error: ${f}`)}}function ye(e,i,s){let a={},o=Object.keys(e.variables),t=o.length;o.forEach(l=>a[l]=[]),e.data=a;let f=i.split(/,? *,?[;\r\n]+ */);for(let l=1;l<f.length;l++){let p=f[l].trim().replace(fe,"").split(ne);if(p.length%t===0)for(let u=0;u<p.length;u++)a[o[u%t]].push(Number(p[u]));else s.logs.push(`Format error: ${p}`)}}function I(e,i){e.isXYAdata=!0;let s={};e.data=s;let a=i.split(/\r?\n/),o=a[0].replace(/^.*?([A-Z]+).*$/,"$1").split("").map(t=>t.toLowerCase());for(let t=1;t<a.length;t++){let f=a[t].replace(/^\((.*)\)$/,"$1").split(/ *, */);for(let l=0;l<o.length;l++){let p=f[l];switch(o[l]){case"x":case"y":case"w":p=Number.parseFloat(p);break;case"a":p=p.replace(/^<(.*)>$/,"$1");break;case"m":break;default:continue}s[o[l]]||(s[o[l]]=[]),s[o[l]].push(p)}}}var H=require("dynamic-typing");var E=Te(require("ml-array-median"));function k(e){let i=e[0].data.y[0],s=i,a=e.length,o=e[0].data.x.length,t=new Array(a);for(let r=0;r<a;r++){t[r]=e[r].data.y;for(let n=0;n<o;n++){let m=t[r][n];m<i&&(i=m),m>s&&(s=m)}}let f=e[0].data.x[0],l=e[0].data.x[e[0].data.x.length-1],p=e[0].pageValue,u=e[a-1].pageValue;if(f>l)for(let r of t)r.reverse();p>u&&t.reverse();let c=[];for(let r=0;r<t.length;r++){let n=Float64Array.from(t[r]);for(let m=0;m<n.length;m++)n[m]<0&&(n[m]=-n[m]);c.push((0,E.default)(n))}let d=(0,E.default)(c);return{z:t,minX:Math.min(f,l),maxX:Math.max(f,l),minY:Math.min(p,u),maxY:Math.max(p,u),minZ:i,maxZ:s,noise:d}}function Z(e,i){let s=e.noise,a=e.z,o,t,f,l,p,u,c,d,r=a.length,n=a[0].length,m,h,g,T,A=e.minX,N=(e.maxX-A)/(n-1),S=e.minY,F=(e.maxY-S)/(r-1),me=e.minZ,z=e.maxZ,W=i.nbContourLevels*2,J=new Array(W),x;for(let M=0;M<W;M++){let $={};J[M]=$;let ce=M%2,Q=(z-i.noiseMultiplier*s)*Math.exp((M>>1)-i.nbContourLevels);ce===0?x=Q+i.noiseMultiplier*s:x=0-Q-i.noiseMultiplier*s;let b=[];if($.zValue=x,$.lines=b,!(x<=me||x>=z))for(let v=0;v<r-1;v++){let ee=a[v],te=a[v+1];for(let X=0;X<n-1;X++)o=ee[X],t=ee[X+1],f=te[X],l=te[X+1],p=o>x,u=t>x,c=f>x,d=l>x,p!==u&&p!==c&&(m=X+(x-o)/(t-o),h=v,g=X,T=v+(x-o)/(f-o),b.push(m*N+A),b.push(h*F+S),b.push(g*N+A),b.push(T*F+S)),d!==u&&d!==c&&(m=X+1,h=v+1-(x-l)/(t-l),g=X+1-(x-l)/(f-l),T=v+1,b.push(m*N+A),b.push(h*F+S),b.push(g*N+A),b.push(T*F+S)),u!==c&&(m=(X+1-(x-t)/(f-t))*N+A,h=(v+(x-t)/(f-t))*F+S,u!==p&&(g=X+1-(x-t)/(o-t),T=v,b.push(m),b.push(h),b.push(g*N+A),b.push(T*F+S)),c!==p&&(g=X,T=v+1-(x-f)/(o-f),b.push(m),b.push(h),b.push(g*N+A),b.push(T*F+S)),u!==d&&(g=X+1,T=v+(x-t)/(l-t),b.push(m),b.push(h),b.push(g*N+A),b.push(T*F+S)),c!==d&&(g=X+(x-f)/(l-f),T=v+1,b.push(m),b.push(h),b.push(g*N+A),b.push(T*F+S)))}}return{minX:e.minX,maxX:e.maxX,minY:e.minY,maxY:e.maxY,segments:J}}function j(e,i){let s=k(e.spectra);i.noContour||(e.contourLines=Z(s,i),delete s.z),e.minMax=s}var B=require("gyromagnetic-ratio");function q(e,i){for(let s of e){let a=0,o=0;for(let t of s.spectra){if(s.ntuples&&s.ntuples.symbol?(!a&&t.observeFrequency&&(a=t.observeFrequency),!o&&t.shiftOffsetVal&&(o=t.shiftOffsetVal)):(a=t.observeFrequency,o=t.shiftOffsetVal),a&&t.xUnits&&t.xUnits.toUpperCase().includes("HZ")){t.xUnits="PPM",t.xFactor=t.xFactor/a,t.firstX=t.firstX/a,t.lastX=t.lastX/a,t.deltaX=t.deltaX/a;for(let f=0;f<t.data.x.length;f++)t.data.x[f]/=a}if(o&&t.xUnits.toLowerCase().includes("ppm")){let f=t.firstX-o;t.firstX=t.firstX-f,t.lastX=t.lastX-f;for(let l=0;l<t.data.x.length;l++)t.data.x[l]-=f}if(s.ntuples&&s.ntuples.nucleus&&s.ntuples.symbol)for(let f=0;f<s.ntuples.nucleus.length;f++){let l=s.ntuples.symbol[f],p=s.ntuples.nucleus[f];if(l.match(/^[F|T]/)&&!p){if(l.match(/[F|T]1/))if(s.tmp.$NUC2)s.ntuples.nucleus[f]=s.tmp.$NUC2;else{let u=s.ntuples.symbol.indexOf(l.replace(/^([F|T]).*/,"$12"));u&&s.ntuples.nucleus[u]&&(s.ntuples.nucleus[f]=s.ntuples.nucleus[u])}l.match(/[F|T]2/)&&(s.ntuples.nucleus[f]=s.tmp.$NUC1)}l.match(/[F|T]2/)&&(s.yType=s.ntuples.nucleus[0])}if(a&&s.ntuples&&s.ntuples.symbol&&s.ntuples.nucleus){let f="",l=s.ntuples.symbol.indexOf(t.pageSymbol);if(s.ntuples.units&&s.ntuples.units[l]&&(f=s.ntuples.units[l]),f!=="PPM"){if(l!==0){let r="Not sure about this ntuples format";if(i)i.warn(r);else throw Error(r)}let{nucleus:p}=s.ntuples,u=(0,B.getGyromagneticRatio)(p[0]),c=(0,B.getGyromagneticRatio)(p[1]);if(!u||!c){let r=`Problem with determination of gyromagnetic ratio for ${p.join("-")}`;if(i)i.error(r);else throw Error(r)}let d=u/c*a;t.pageValue/=d}}}}}function L(e,i,s={}){s.logger?.trace(i),e.profiling&&e.profiling.push({action:i,time:Date.now()-s.start})}function G(e){let i=e.spectra[0].data;e.chromatogram={times:i.x.slice(),series:{intensity:{dimension:1,data:i.y.slice()}}}}function K(e,i,s){q(e,s.logger),Ne(e,s);for(let a of e){if(Object.keys(a.ntuples).length>0){let o=[],t=Object.keys(a.ntuples);for(let f=0;f<t.length;f++){let l=t[f],p=a.ntuples[l];for(let u=0;u<p.length;u++)o[u]||(o[u]={}),o[u][l]=p[u]}a.ntuples=o}a.twoD&&s.wantXY&&(j(a,s),L(i,"Finished countour plot calculation",s),s.keepSpectra||delete a.spectra),s.chromatogram&&(a.spectra.length>1?ae(a):G(a),L(i,"Finished chromatogram calculation",s)),delete a.tmp}}function Ne(e,i){for(let s of e)for(let a in s.meta){let o=s.meta[a];if(typeof o=="string"){if(o[0]==="{"){if(o[o.length-1]==="}"){let t=o.slice(1,-1).split(/[,; ]+/).filter(f=>f);for(let f=0;f<t.length;f++)s.meta[a+String(f)]=i.dynamicTyping?(0,H.parseString)(t[f]):t[f]}}else if(o[0]==="("){let t=o.split(/\r?\n/),f=/^\((?<from>\d+)\.\.(?<to>\d+)\)$/;if(f.test(t[0])){let[l,p]=t[0].match(f).slice(1).map(Number),u=t.slice(1).join(" ").split(/[,; ]+/).filter(c=>c);for(let c=l;c<=p;c++)s.meta[a+String(c)]=i.dynamicTyping?(0,H.parseString)(u[c-l]):u[c-l]}}}}}function _(e,i,s){let a=-1,o=-1,t="",f="";if(s.indexOf("++")>0)t=s.replace(/.*\(([a-zA-Z0-9]+)\+\+.*/,"$1"),f=s.replace(/.*\.\.([a-zA-Z0-9]+).*/,"$1");else{s=s.replace(/[^a-zA-Z]/g,""),t=s.charAt(0),f=s.charAt(1),i.variables={};for(let l of s){let p=l.toLowerCase(),u=e.ntuples.symbol.indexOf(l);if(u===-1)throw Error(`Symbol undefined: ${l}`);i.variables[p]={};for(let c in e.ntuples)e.ntuples[c][u]&&(i.variables[p][c.replace(/^var/,"")]=e.ntuples[c][u])}}a=e.ntuples.symbol.indexOf(t),o=e.ntuples.symbol.indexOf(f),a===-1&&(a=0),o===-1&&(o=0),e.ntuples.first&&(e.ntuples.first.length>a&&(i.firstX=e.ntuples.first[a]),e.ntuples.first.length>o&&(i.firstY=e.ntuples.first[o])),e.ntuples.last&&(e.ntuples.last.length>a&&(i.lastX=e.ntuples.last[a]),e.ntuples.last.length>o&&(i.lastY=e.ntuples.last[o])),e.ntuples.vardim&&e.ntuples.vardim.length>a&&(i.nbPoints=e.ntuples.vardim[a]),e.ntuples.factor&&(e.ntuples.factor.length>a&&(i.xFactor=e.ntuples.factor[a]),e.ntuples.factor.length>o&&(i.yFactor=e.ntuples.factor[o])),e.ntuples.units&&(e.ntuples.units.length>a&&(e.ntuples.varname&&e.ntuples.varname[a]?i.xUnits=`${e.ntuples.varname[a]} [${e.ntuples.units[a]}]`:i.xUnits=e.ntuples.units[a]),e.ntuples.units.length>o&&(e.ntuples.varname&&e.ntuples.varname[o]?i.yUnits=`${e.ntuples.varname[o]} [${e.ntuples.units[o]}]`:i.yUnits=e.ntuples.units[o]))}function R(e){e.xFactor||(e.xFactor=1),e.yFactor||(e.yFactor=1)}var C=/[ \t]*,[ \t]*/,ve={keepRecordsRegExp:/^$/,canonicDataLabels:!0,canonicMetadataLabels:!1,dynamicTyping:!0,withoutXY:!1,noTrimRegExp:/^$/,chromatogram:!1,keepSpectra:!1,noContour:!1,nbContourLevels:7,noiseMultiplier:5,profiling:!1};function ue(e,i={}){e=(0,pe.ensureString)(e),i={...ve,...i},i.logger?.debug("Starting jcamp conversion"),i.wantXY=!i.withoutXY,i.start=Date.now();let s=[],a={profiling:i.profiling?[]:!1,logs:[],entries:[]},o={children:[]},t=o,f=[],l={};if(typeof e!="string")throw new TypeError("the JCAMP should be a string");L(a,"Before split to LDRS",i);let p=e.replace(/[\r\n]+##/g,`
|
|
3
|
+
`,children:[]}),l=t[t.length-1],n.push(l)}else if(u.substring(0,5)==="##END"&&o===0){l.jcamp+=`${d}
|
|
4
|
+
`;let f=t.pop();t.length!==0?(l=t[t.length-1],l.children.push(f)):(l=void 0,r.push(f))}else if(l&&l.jcamp){l.jcamp+=`${d}
|
|
5
|
+
`;let f=u.match(/^##(.*?)=(.+)/);if(f){let m=f[1].replace(/[ _-]/g,"").toUpperCase();m==="DATATYPE"?l.dataType=f[2].trim():m==="DATACLASS"?l.dataClass=f[2].trim():m==="JCAMPDX"?l.jcampDX=F(f[2].trim()):m==="JCAMPCS"&&(l.jcampCS=F(f[2].trim()))}}u.substring(0,5)==="##END"&&o>0&&o--}return s?(n.forEach(c=>{c.children=void 0}),n):r}var pe=require("dynamic-typing"),me=require("ensure-string");var U=["TIC",".RIC","SCANNUMBER"];function oe(e){let i=e.spectra,s=i.length,a={times:new Array(s),series:{ms:{dimension:2,data:new Array(s)}}},n=[];for(let t=0;t<U.length;t++){let r=V(U[t]);i[0][r]&&(n.push(r),a.series[r]={dimension:1,data:new Array(s)})}for(let t=0;t<s;t++){let r=i[t];a.times[t]=r.pageValue;for(let l=0;l<n.length;l++)a.series[n[l]].data[t]=Number(r[n[l]]);r.data&&(a.series.ms.data[t]=[r.data.x,r.data.y])}e.chromatogram=a}function fe(e){return U.indexOf(e)!==-1}function V(e){return e.toLowerCase().replace(/[^a-z0-9]/g,"")}function Y(e){let i=[];for(let s=0;s<e.length;s++)i.push(Number(e[s]));return i}function B(e,i){let s=e.yFactor,a=e.deltaX;e.isXYdata=!0;let n={x:[],y:[]};e.data=n;let t=e.firstX,r=e.firstY,l=!1,o,p=0;for(;p<i.length;p++)if(o=i.charCodeAt(p),o===13||o===10)l=!0;else if(l)break;let c=!0,d=!1,u=!1,f=0,m=!1,h=!1,g=0,T=0,N=!1,y=!1,X=!1,A=0;for(;p<=i.length;p++)if(p===i.length?o=13:o=i.charCodeAt(p),h)(o===13||o===10)&&(c=!0,h=!1);else if(o<=57&&o>=48)y=!0,A>0?g+=(o-48)/Math.pow(10,A++):(g*=10,g+=o-48);else if(o===44||o===46)y=!0,A++;else{if(y){if(c)c=!1,u&&(X=!0);else if(X)X=!1;else{d?(f=N?0-g:g,u=!0,d=!1):m||(T=N?0-g:g);let O=m?g-1:1;for(let v=0;v<O;v++)u?r+=f:r=T,n.x.push(t),n.y.push(r*s),t+=a}N=!1,g=0,A=0,y=!1,m=!1}if(o<74&&o>63)y=!0,u=!1,g=o-64;else if(o>96&&o<106)y=!0,u=!1,g=o-96,N=!0;else if(o===115)y=!0,m=!0,g=9;else if(o>82&&o<91)y=!0,m=!0,g=o-82;else if(o>73&&o<83)y=!0,d=!0,g=o-73;else if(o>105&&o<115)y=!0,d=!0,g=o-105,N=!0;else if(o===36&&i.charCodeAt(p+1)===36)y=!0,h=!0;else if(o===37)y=!0,d=!0,g=0,N=!1;else if(o===45){let O=i.charCodeAt(p+1);(O>=48&&O<=57||O===44||O===46)&&(y=!0,c||(u=!1),N=!0)}else(o===13||o===10)&&(c=!0,h=!1)}}var ue=/[,\t ]+/;function w(e,i,s){if(e.isPeaktable=!0,!e.variables||Object.keys(e.variables)===2?Ce(e,i,s):ve(e,i,s),e.variables)for(let a in e.variables)e.variables[a].data=e.data[a]}function Ce(e,i,s){let a={x:[],y:[]};e.data=a;let n=i.split(/,? *,?[;\r\n]+ */);for(let t=1;t<n.length;t++){let r=n[t].trim().replace(D,"").split(ue);if(r.length%2===0)for(let l=0;l<r.length;l=l+2)a.x.push(Number(r[l])*e.xFactor),a.y.push(Number(r[l+1])*e.yFactor);else s.logs.push(`Format error: ${r}`)}}function ve(e,i,s){let a={},n=Object.keys(e.variables),t=n.length;n.forEach(l=>a[l]=[]),e.data=a;let r=i.split(/,? *,?[;\r\n]+ */);for(let l=1;l<r.length;l++){let o=r[l].trim().replace(D,"").split(ue);if(o.length%t===0)for(let p=0;p<o.length;p++)a[n[p%t]].push(Number(o[p]));else s.logs.push(`Format error: ${o}`)}}function k(e,i){e.isXYAdata=!0;let s={};e.data=s;let a=i.split(/\r?\n/),n=a[0].replace(/^.*?([A-Z]+).*$/,"$1").split("").map(t=>t.toLowerCase());for(let t=1;t<a.length;t++){let r=a[t].replace(/^\((.*)\)$/,"$1").split(/ *, */);for(let l=0;l<n.length;l++){let o=r[l];switch(n[l]){case"x":case"y":case"w":o=Number.parseFloat(o);break;case"a":o=o.replace(/^<(.*)>$/,"$1");break;case"m":break;default:continue}s[n[l]]||(s[n[l]]=[]),s[n[l]].push(o)}}}var Q=require("dynamic-typing");var j=require("gyromagnetic-ratio"),Z=ye(require("ml-array-median"));function J(e){let{spectra:i}=e,s=i[0].data.y[0],a=s,n=i.length,t=i[0].data.x.length,r=new Array(n);for(let f=0;f<n;f++){r[f]=i[f].data.y;for(let m=0;m<t;m++){let h=r[f][m];h<s&&(s=h),h>a&&(a=h)}}let l=i[0].data.x[0],o=i[0].data.x[i[0].data.x.length-1],{firstY:p,lastY:c}=Me(e);if(l>o)for(let f of r)f.reverse();p>c&&r.reverse();let d=[];for(let f=0;f<r.length;f++){let m=Float64Array.from(r[f]);for(let h=0;h<m.length;h++)m[h]<0&&(m[h]=-m[h]);d.push((0,Z.default)(m))}let u=(0,Z.default)(d);return{z:r,minX:Math.min(l,o),maxX:Math.max(l,o),minY:Math.min(p,c),maxY:Math.max(p,c),minZ:s,maxZ:a,noise:u}}function Me(e){let{spectra:i,ntuples:s,info:a}=e;if(s)for(let n=0;n<s.length;n++){let{symbol:t,nucleus:r}=s[n];if(t.match(/[F|T]1/)){let l=a[".OBSERVEFREQUENCY"],{nucleus:o}=s.find(m=>m.symbol.match(/[F|T]2/));if([l,o,r].some(m=>!m))break;let p=(0,j.getGyromagneticRatio)(o),c=(0,j.getGyromagneticRatio)(r),{first:d,last:u}=s[n],f=l*c/p;return{firstY:d/f,lastY:u/f}}}return{firstY:i[0].pageValue,lastY:i[i.length-1].pageValue}}function G(e,i){let s=e.noise,a=e.z,n,t,r,l,o,p,c,d,u=a.length,f=a[0].length,m,h,g,T,N=e.minX,X=(e.maxX-N)/(f-1),A=e.minY,v=(e.maxY-A)/(u-1),de=e.minZ,W=e.maxZ,ee=i.nbContourLevels*2,te=new Array(ee),x;for(let R=0;R<ee;R++){let I={};te[R]=I;let ge=R%2,le=(W-i.noiseMultiplier*s)*Math.exp((R>>1)-i.nbContourLevels);ge===0?x=le+i.noiseMultiplier*s:x=0-le-i.noiseMultiplier*s;let b=[];if(I.zValue=x,I.lines=b,!(x<=de||x>=W))for(let C=0;C<u-1;C++){let se=a[C],ie=a[C+1];for(let S=0;S<f-1;S++)n=se[S],t=se[S+1],r=ie[S],l=ie[S+1],o=n>x,p=t>x,c=r>x,d=l>x,o!==p&&o!==c&&(m=S+(x-n)/(t-n),h=C,g=S,T=C+(x-n)/(r-n),b.push(m*X+N),b.push(h*v+A),b.push(g*X+N),b.push(T*v+A)),d!==p&&d!==c&&(m=S+1,h=C+1-(x-l)/(t-l),g=S+1-(x-l)/(r-l),T=C+1,b.push(m*X+N),b.push(h*v+A),b.push(g*X+N),b.push(T*v+A)),p!==c&&(m=(S+1-(x-t)/(r-t))*X+N,h=(C+(x-t)/(r-t))*v+A,p!==o&&(g=S+1-(x-t)/(n-t),T=C,b.push(m),b.push(h),b.push(g*X+N),b.push(T*v+A)),c!==o&&(g=S,T=C+1-(x-r)/(n-r),b.push(m),b.push(h),b.push(g*X+N),b.push(T*v+A)),p!==d&&(g=S+1,T=C+(x-t)/(l-t),b.push(m),b.push(h),b.push(g*X+N),b.push(T*v+A)),c!==d&&(g=S+(x-r)/(l-r),T=C+1,b.push(m),b.push(h),b.push(g*X+N),b.push(T*v+A)))}}return{minX:e.minX,maxX:e.maxX,minY:e.minY,maxY:e.maxY,segments:te}}function H(e,i){let s=J(e);i.noContour||(e.contourLines=G(s,i),delete s.z),e.minMax=s}var $=require("gyromagnetic-ratio");function q(e,i){for(let s of e){let a=0,n=0;for(let t of s.spectra){if(s.ntuples&&s.ntuples.symbol?(!a&&t.observeFrequency&&(a=t.observeFrequency),!n&&t.shiftOffsetVal&&(n=t.shiftOffsetVal)):(a=t.observeFrequency,n=t.shiftOffsetVal),a&&t.xUnits&&t.xUnits.toUpperCase().includes("HZ")){t.xUnits="PPM",t.xFactor=t.xFactor/a,t.firstX=t.firstX/a,t.lastX=t.lastX/a,t.deltaX=t.deltaX/a;for(let r=0;r<t.data.x.length;r++)t.data.x[r]/=a}if(n&&t.xUnits.toLowerCase().includes("ppm")){let r=t.firstX-n;t.firstX=t.firstX-r,t.lastX=t.lastX-r;for(let l=0;l<t.data.x.length;l++)t.data.x[l]-=r}if(s.ntuples&&s.ntuples.nucleus&&s.ntuples.symbol)for(let r=0;r<s.ntuples.nucleus.length;r++){let l=s.ntuples.symbol[r],o=s.ntuples.nucleus[r];if(l.match(/^[F|T]/)&&!o){if(l.match(/[F|T]1/))if(s.tmp.$NUC2)s.ntuples.nucleus[r]=s.tmp.$NUC2;else{let p=s.ntuples.symbol.indexOf(l.replace(/^([F|T]).*/,"$12"));p&&s.ntuples.nucleus[p]&&(s.ntuples.nucleus[r]=s.ntuples.nucleus[p])}l.match(/[F|T]2/)&&(s.ntuples.nucleus[r]=s.tmp.$NUC1)}l.match(/[F|T]2/)&&(s.yType=s.ntuples.nucleus[0],(0,$.getGyromagneticRatio)(s.xType)||(s.xType=s.ntuples.nucleus[1]))}if(a&&s.ntuples&&s.ntuples.symbol&&s.ntuples.nucleus){let r="",l=s.ntuples.symbol.indexOf(t.pageSymbol);if(s.ntuples.units&&s.ntuples.units[l]&&(r=s.ntuples.units[l]),r!=="PPM"){if(l!==0){let u="Not sure about this ntuples format";if(i)i.warn(u);else throw Error(u)}let{nucleus:o}=s.ntuples,p=(0,$.getGyromagneticRatio)(o[0]),c=(0,$.getGyromagneticRatio)(o[1]);if(!p||!c){let u=`Problem with determination of gyromagnetic ratio for ${o.join("-")}`;if(i)i.error(u);else throw Error(u)}let d=p/c*a;t.pageValue/=d}}}}}function L(e,i,s={}){s.logger?.trace(i),e.profiling&&e.profiling.push({action:i,time:Date.now()-s.start})}function K(e){let i=e.spectra[0].data;e.chromatogram={times:i.x.slice(),series:{intensity:{dimension:1,data:i.y.slice()}}}}function _(e,i,s){q(e,s.logger),Fe(e,s);for(let a of e){if(Object.keys(a.ntuples).length>0){let n=[],t=Object.keys(a.ntuples);for(let r=0;r<t.length;r++){let l=t[r],o=a.ntuples[l];for(let p=0;p<o.length;p++)n[p]||(n[p]={}),n[p][l]=o[p]}a.ntuples=n}a.twoD&&s.wantXY&&(H(a,s),L(i,"Finished countour plot calculation",s),s.keepSpectra||delete a.spectra),s.chromatogram&&(a.spectra.length>1?oe(a):K(a),L(i,"Finished chromatogram calculation",s)),delete a.tmp}}function Fe(e,i){for(let s of e)for(let a in s.meta){let n=s.meta[a];if(typeof n=="string"){if(n[0]==="{"){if(n[n.length-1]==="}"){let t=n.slice(1,-1).split(/[,; ]+/).filter(r=>r);for(let r=0;r<t.length;r++)s.meta[a+String(r)]=i.dynamicTyping?(0,Q.parseString)(t[r]):t[r]}}else if(n[0]==="("){let t=n.split(/\r?\n/),r=/^\((?<from>\d+)\.\.(?<to>\d+)\)$/;if(r.test(t[0])){let[l,o]=t[0].match(r).slice(1).map(Number),p=t.slice(1).join(" ").split(/[,; ]+/).filter(c=>c);for(let c=l;c<=o;c++)s.meta[a+String(c)]=i.dynamicTyping?(0,Q.parseString)(p[c-l]):p[c-l]}}}}}function z(e,i,s){let a=-1,n=-1,t="",r="";if(s.indexOf("++")>0)t=s.replace(/.*\(([a-zA-Z0-9]+)\+\+.*/,"$1"),r=s.replace(/.*\.\.([a-zA-Z0-9]+).*/,"$1");else{s=s.replace(/[^a-zA-Z]/g,""),t=s.charAt(0),r=s.charAt(1),i.variables={};for(let l of s){let o=l.toLowerCase(),p=e.ntuples.symbol.indexOf(l);if(p===-1)throw Error(`Symbol undefined: ${l}`);i.variables[o]={};for(let c in e.ntuples)e.ntuples[c][p]&&(i.variables[o][c.replace(/^var/,"")]=e.ntuples[c][p])}}a=e.ntuples.symbol.indexOf(t),n=e.ntuples.symbol.indexOf(r),a===-1&&(a=0),n===-1&&(n=0),e.ntuples.first&&(e.ntuples.first.length>a&&(i.firstX=e.ntuples.first[a]),e.ntuples.first.length>n&&(i.firstY=e.ntuples.first[n])),e.ntuples.last&&(e.ntuples.last.length>a&&(i.lastX=e.ntuples.last[a]),e.ntuples.last.length>n&&(i.lastY=e.ntuples.last[n])),e.ntuples.vardim&&e.ntuples.vardim.length>a&&(i.nbPoints=e.ntuples.vardim[a]),e.ntuples.factor&&(e.ntuples.factor.length>a&&(i.xFactor=e.ntuples.factor[a]),e.ntuples.factor.length>n&&(i.yFactor=e.ntuples.factor[n])),e.ntuples.units&&(e.ntuples.units.length>a&&(e.ntuples.varname&&e.ntuples.varname[a]?i.xUnits=`${e.ntuples.varname[a]} [${e.ntuples.units[a]}]`:i.xUnits=e.ntuples.units[a]),e.ntuples.units.length>n&&(e.ntuples.varname&&e.ntuples.varname[n]?i.yUnits=`${e.ntuples.varname[n]} [${e.ntuples.units[n]}]`:i.yUnits=e.ntuples.units[n]))}function E(e){e.xFactor||(e.xFactor=1),e.yFactor||(e.yFactor=1)}var M=/[ \t]*,[ \t]*/,Le={keepRecordsRegExp:/^$/,canonicDataLabels:!0,canonicMetadataLabels:!1,dynamicTyping:!0,withoutXY:!1,noTrimRegExp:/^$/,chromatogram:!1,keepSpectra:!1,noContour:!1,nbContourLevels:7,noiseMultiplier:5,profiling:!1};function ce(e,i={}){e=(0,me.ensureString)(e),i={...Le,...i},i.logger?.debug("Starting jcamp conversion"),i.wantXY=!i.withoutXY,i.start=Date.now();let s=[],a={profiling:i.profiling?[]:!1,logs:[],entries:[]},n={children:[]},t=n,r=[],l={};if(typeof e!="string")throw new TypeError("the JCAMP should be a string");L(a,"Before split to LDRS",i);let o=e.replace(/[\r\n]+##/g,`
|
|
6
6
|
##`).split(`
|
|
7
|
-
##`);L(a,"Split to LDRS",i),
|
|
8
|
-
`);if(m===-1&&(m=
|
|
7
|
+
##`);L(a,"Split to LDRS",i),o[0]&&(o[0]=o[0].replace(/^[\r\n ]*##/,""));for(let p of o){let c=p.indexOf("="),d=c>0?p.substring(0,c):p,u=c>0?d.match(i.noTrimRegExp)?p.substring(c+1):p.substring(c+1).trim():"",f=d.replace(/[_ -]/g,"").toUpperCase();if(f==="DATATABLE"){let m=u.indexOf(`
|
|
8
|
+
`);if(m===-1&&(m=u.indexOf("\r")),m>0){let h=u.substring(0,m).split(/[ ,;\t]+/);z(t,l,h[0]),l.datatable=h[0],h[1]&&h[1].indexOf("PEAKS")>-1?f="PEAKTABLE":h[1]&&(h[1].indexOf("XYDATA")||h[0].indexOf("++")>0)&&(f="XYDATA",l.nbPoints&&(l.deltaX=(l.lastX-l.firstX)/(l.nbPoints-1)))}}if(f==="XYDATA"){i.wantXY&&(E(l),u.match(/.*\+\+.*/)?(l.nbPoints&&(l.deltaX=(l.lastX-l.firstX)/(l.nbPoints-1)),B(l,u,a)):w(l,u,a),t.spectra.push(l),l={});continue}else if(f==="PEAKTABLE"){i.wantXY&&(E(l),w(l,u,a),t.spectra.push(l),l={});continue}if(f==="PEAKASSIGNMENTS"){i.wantXY&&(u.match(/.*([^A-Z]*).*/)&&k(l,u),t.spectra.push(l),l={});continue}if(f==="TITLE"){let m=t;m.children||(m.children=[]),t={spectra:[],ntuples:{},info:{},meta:{},tmp:{}},m.children.push(t),r.push(m),s.push(t),t.title=u}else f==="DATATYPE"?(t.dataType=u,u.match(/(^nd|\snd\s)/i)&&(t.twoD=!0)):f==="NTUPLES"?u.match(/(^nd|\snd\s)/i)&&(t.twoD=!0):f==="DATACLASS"?t.dataClass=u:f==="JCAMPDX"?t.jcampDX=F(u):f==="JCAMPCS"?t.jcampCS=F(u):f==="XUNITS"?l.xUnits=u:f==="YUNITS"?l.yUnits=u:f==="FIRSTX"?l.firstX=Number(u):f==="LASTX"?l.lastX=Number(u):f==="FIRSTY"?l.firstY=Number(u):f==="LASTY"?l.lastY=Number(u):f==="NPOINTS"?l.nbPoints=Number(u):f==="XFACTOR"?l.xFactor=Number(u):f==="YFACTOR"?l.yFactor=Number(u):f==="MAXX"?l.maxX=Number(u):f==="MINX"?l.minX=Number(u):f==="MAXY"?l.maxY=Number(u):f==="MINY"?l.minY=Number(u):f==="DELTAX"?l.deltaX=Number(u):f===".OBSERVEFREQUENCY"||f==="$SFO1"?l.observeFrequency||(l.observeFrequency=Number(u)):f===".OBSERVENUCLEUS"?l.xType||(t.xType=u.replace(/[^a-zA-Z0-9]/g,"")):f==="$OFFSET"?(t.shiftOffsetNum=0,l.shiftOffsetVal||(l.shiftOffsetVal=Number(u))):f==="$REFERENCEPOINT"||(f==="VARNAME"?t.ntuples.varname=u.split(M):f==="SYMBOL"?t.ntuples.symbol=u.split(M):f==="VARTYPE"?t.ntuples.vartype=u.split(M):f==="VARFORM"?t.ntuples.varform=u.split(M):f==="VARDIM"?t.ntuples.vardim=Y(u.split(M)):f==="UNITS"?t.ntuples.units=u.split(M):f==="FACTOR"?t.ntuples.factor=Y(u.split(M)):f==="FIRST"?t.ntuples.first=Y(u.split(M)):f==="LAST"?t.ntuples.last=Y(u.split(M)):f==="MIN"?t.ntuples.min=Y(u.split(M)):f==="MAX"?t.ntuples.max=Y(u.split(M)):f===".NUCLEUS"?t.ntuples&&(t.ntuples.nucleus=u.split(M).map(m=>m.replace(/[^a-zA-Z0-9]/g,""))):f==="PAGE"?(l.page=u.trim(),l.pageValue=Number(u.replace(/^.*=/,"")),l.pageSymbol=l.page.replace(/[=].*/,"")):f==="RETENTIONTIME"?l.pageValue=Number(u):fe(f)?l[V(f)]=u:f==="SAMPLEDESCRIPTION"?l.sampleDescription=u:f.startsWith("$NUC")?!t.tmp[f]&&!u.includes("off")&&(t.tmp[f]=u.replace(/[<>]/g,"")):f==="END"&&(t=r.pop()));if(t&&t.info&&t.meta&&f.match(i.keepRecordsRegExp)){let m,h;d.startsWith("$")?(h=i.canonicMetadataLabels?f.substring(1):d.substring(1),m=t.meta):(h=i.canonicDataLabels?f:d,m=t.info),i.dynamicTyping&&(u=(0,pe.parseString)(u)),m[h]?(Array.isArray(m[h])||(m[h]=[m[h]]),m[h].push(u)):m[h]=u}}if(L(a,"Finished parsing",i),_(s,a,i),L(a,"Total time",i),a.entries=n.children,a.flatten=s,i.logger){i.logger.debug("Finished jcamp conversion");for(let p of a.flatten)i.logger.debug(`${p.dataType} - ${p.title}`)}return a}function he(e,i={}){let{logger:s,OCL:a}=i;if(!e.info.ATOMLIST||!e.info.BONDLIST){s?.warn("No ATOMLIST or BONDLIST in the JCAMP-CS entry");return}De(e,s);let n=Oe(e.info.ATOMLIST),t=Re(e.info.BONDLIST),r=Pe(e.info.CHARGE,s),l=Ye(a,n,t,r,s);return{molecule:l,molfile:l.toMolfile()}}function Ye(e,i,s,a,n){let t={S:e.Molecule.cBondTypeSingle,D:e.Molecule.cBondTypeDouble,T:e.Molecule.cBondTypeTriple,Q:e.Molecule.cBondTypeQuadruple},r={},l=new e.Molecule(i.length,s.length);for(let o of i){let p=e.Molecule.getAtomicNoFromLabel(o.element);p||(n?.error(`Atomic number of ${o.element} could not be determined`),p=e.Molecule.getAtomicNoFromLabel("X")),r[o.atomNumber]=l.addAtom(p),l.setAtomMapNo(r[o.atomNumber],o.atomNumber)}for(let o of s){if(r[o.atomNumber1]===void 0){n?.error(`A bond goes to atomNumber ${o.atomNumber1} that does not exists`);continue}if(r[o.atomNumber2]===void 0){n?.error(`A bond goes to atomNumber ${o.atomNumber2} that does not exists`);continue}let p=l.addBond(r[o.atomNumber1],r[o.atomNumber2]);l.setBondType(p,t[o.bondType])}for(let o of a){if(r[o.atomNumber]===void 0){n?.error(`A charge goes to atomNumber ${o.atomNumber1} that does not exists`);continue}l.setAtomCharge(r[o.atomNumber],o.value)}return l}function Oe(e){let i=e.split(/\r?\n/).map(a=>F(a)).filter(a=>a),s=[];for(let a of i){let[n,t,r]=a.trim().split(/\s+/);s.push({atomNumber:Number(n),element:t,nbImplicitHydrogens:r?.length>0?Number(r):void 0})}return s}function Re(e){let i=e.split(/\r?\n/).map(a=>F(a)).filter(a=>a),s=[];for(let a of i){let[n,t,r]=a.trim().split(/\s+/);s.push({atomNumber1:Number(n),atomNumber2:Number(t),bondType:r})}return s}function Pe(e,i){if(!e)return[];let s=e.split(/\r?\n/).map(n=>F(n)).filter(n=>n),a=[];for(let n of s){let[t,r,l]=n.trim().split(/\s+/);l!==void 0&&i?.warn("Charge on multiple atom is not supported"),a.push({atomNumber:Number(r),value:Number(t)})}return a}function De(e,i){let s=["STEREOPAIR","STEREOCENTER","RADICAL","STEREOMOLECULE"];for(let a of s)e.info[a]&&i?.warn(`JCAMP-CS parser do not support: ${a}`)}
|
package/jcampconverter.d.ts
CHANGED
|
@@ -66,6 +66,8 @@ import { Logger } from 'cheminfo-types';
|
|
|
66
66
|
* @property {string} [title]
|
|
67
67
|
* @property {string} [dataType]
|
|
68
68
|
* @property {string} [dataClass]
|
|
69
|
+
* @property {string?} [jcampDX]
|
|
70
|
+
* @property {string?} [jcampCS]
|
|
69
71
|
* @property {boolean} [twoD]
|
|
70
72
|
* @property {MinMax} [minMax]
|
|
71
73
|
*/
|
|
@@ -186,6 +188,8 @@ declare type Entry = {
|
|
|
186
188
|
title?: string | undefined;
|
|
187
189
|
dataType?: string | undefined;
|
|
188
190
|
dataClass?: string | undefined;
|
|
191
|
+
jcampDX?: string | null | undefined;
|
|
192
|
+
jcampCS?: string | null | undefined;
|
|
189
193
|
twoD?: boolean | undefined;
|
|
190
194
|
minMax?: MinMax | undefined;
|
|
191
195
|
};
|
|
@@ -216,6 +220,28 @@ declare type Ntuples = {
|
|
|
216
220
|
nucleus?: string[] | undefined;
|
|
217
221
|
};
|
|
218
222
|
|
|
223
|
+
/**
|
|
224
|
+
*
|
|
225
|
+
* @typedef {object} ParsedCS@typedef {object} ParsedCS
|
|
226
|
+
* @property {string} molfile
|
|
227
|
+
* @property {import("openchemlib").OCL} molecule
|
|
228
|
+
*/
|
|
229
|
+
/**
|
|
230
|
+
*
|
|
231
|
+
* @param {import("./convert.js").Entry} entry
|
|
232
|
+
* @param {object} [options={}]
|
|
233
|
+
* @param {import("fifo-logger").Logger} [options.logger]
|
|
234
|
+
* @param {import("openchemlib").OCL} [options.OCL]
|
|
235
|
+
* @returns ParsedCS
|
|
236
|
+
*/
|
|
237
|
+
export declare function parseJcampCS(entry: Entry, options?: {
|
|
238
|
+
logger?: any;
|
|
239
|
+
OCL?: any;
|
|
240
|
+
} | undefined): {
|
|
241
|
+
molecule: any;
|
|
242
|
+
molfile: any;
|
|
243
|
+
} | undefined;
|
|
244
|
+
|
|
219
245
|
declare type Spectrum = Record<string, any>;
|
|
220
246
|
|
|
221
247
|
declare type Tree = {
|
package/jcampconverter.mjs
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import{ensureString as fe}from"ensure-string";function
|
|
1
|
+
import{ensureString as fe}from"ensure-string";var P=/\s*\$\$.*/;function F(e){return e.replace(P,"")}function ue(e,a={}){e=fe(e);let{flatten:s=!1}=a;if(typeof e!="string")throw new TypeError("the JCAMP should be a string");let i=e.split(/\r?\n/),o=[],t=[],r=[],l,n=0,p=e.includes("## ");for(let c=0;c<i.length;c++){let d=i[c],u=p?d.replace(/ /g,""):d;if(u.substring(0,9)==="##NTUPLES"&&n++,u.substring(0,7)==="##TITLE"){let f=[u.substring(8).trim()];for(let m=c+1;m<i.length&&!i[m].startsWith("##");m++)f.push(i[m].trim());t.push({title:f.join(`
|
|
2
2
|
`),jcamp:`${d}
|
|
3
|
-
`,children:[]}),l=
|
|
4
|
-
`;let
|
|
5
|
-
`;let n=r.match(/^##(.*?)=(.+)/);if(n){let m=n[1].replace(/[ _-]/g,"").toUpperCase();m==="DATATYPE"&&(l.dataType=n[2].trim()),m==="DATACLASS"&&(l.dataClass=n[2].trim())}}r.substring(0,5)==="##END"&&o>0&&o--}return s?(p.forEach(c=>{c.children=void 0}),p):f}import{parseString as me}from"dynamic-typing";import{ensureString as ce}from"ensure-string";var $=["TIC",".RIC","SCANNUMBER"];function J(t){let i=t.spectra,s=i.length,a={times:new Array(s),series:{ms:{dimension:2,data:new Array(s)}}},p=[];for(let e=0;e<$.length;e++){let f=D($[e]);i[0][f]&&(p.push(f),a.series[f]={dimension:1,data:new Array(s)})}for(let e=0;e<s;e++){let f=i[e];a.times[e]=f.pageValue;for(let l=0;l<p.length;l++)a.series[p[l]].data[e]=Number(f[p[l]]);f.data&&(a.series.ms.data[e]=[f.data.x,f.data.y])}t.chromatogram=a}function Q(t){return $.indexOf(t)!==-1}function D(t){return t.toLowerCase().replace(/[^a-z0-9]/g,"")}function Y(t){let i=[];for(let s=0;s<t.length;s++)i.push(Number(t[s]));return i}function U(t,i){let s=t.yFactor,a=t.deltaX;t.isXYdata=!0;let p={x:[],y:[]};t.data=p;let e=t.firstX,f=t.firstY,l=!1,o,u=0;for(;u<i.length;u++)if(o=i.charCodeAt(u),o===13||o===10)l=!0;else if(l)break;let c=!0,d=!1,r=!1,n=0,m=!1,h=!1,g=0,T=0,A=!1,y=!1,N=!1,S=0;for(;u<=i.length;u++)if(u===i.length?o=13:o=i.charCodeAt(u),h)(o===13||o===10)&&(c=!0,h=!1);else if(o<=57&&o>=48)y=!0,S>0?g+=(o-48)/Math.pow(10,S++):(g*=10,g+=o-48);else if(o===44||o===46)y=!0,S++;else{if(y){if(c)c=!1,r&&(N=!0);else if(N)N=!1;else{d?(n=A?0-g:g,r=!0,d=!1):m||(T=A?0-g:g);let O=m?g-1:1;for(let F=0;F<O;F++)r?f+=n:f=T,p.x.push(e),p.y.push(f*s),e+=a}A=!1,g=0,S=0,y=!1,m=!1}if(o<74&&o>63)y=!0,r=!1,g=o-64;else if(o>96&&o<106)y=!0,r=!1,g=o-96,A=!0;else if(o===115)y=!0,m=!0,g=9;else if(o>82&&o<91)y=!0,m=!0,g=o-82;else if(o>73&&o<83)y=!0,d=!0,g=o-73;else if(o>105&&o<115)y=!0,d=!0,g=o-105,A=!0;else if(o===36&&i.charCodeAt(u+1)===36)y=!0,h=!0;else if(o===37)y=!0,d=!0,g=0,A=!1;else if(o===45){let O=i.charCodeAt(u+1);(O>=48&&O<=57||O===44||O===46)&&(y=!0,c||(r=!1),A=!0)}else(o===13||o===10)&&(c=!0,h=!1)}}var ee=/\$\$.*/,te=/[,\t ]+/;function P(t,i,s){if(t.isPeaktable=!0,!t.variables||Object.keys(t.variables)===2?oe(t,i,s):pe(t,i,s),t.variables)for(let a in t.variables)t.variables[a].data=t.data[a]}function oe(t,i,s){let a={x:[],y:[]};t.data=a;let p=i.split(/,? *,?[;\r\n]+ */);for(let e=1;e<p.length;e++){let f=p[e].trim().replace(ee,"").split(te);if(f.length%2===0)for(let l=0;l<f.length;l=l+2)a.x.push(Number(f[l])*t.xFactor),a.y.push(Number(f[l+1])*t.yFactor);else s.logs.push(`Format error: ${f}`)}}function pe(t,i,s){let a={},p=Object.keys(t.variables),e=p.length;p.forEach(l=>a[l]=[]),t.data=a;let f=i.split(/,? *,?[;\r\n]+ */);for(let l=1;l<f.length;l++){let o=f[l].trim().replace(ee,"").split(te);if(o.length%e===0)for(let u=0;u<o.length;u++)a[p[u%e]].push(Number(o[u]));else s.logs.push(`Format error: ${o}`)}}function V(t,i){t.isXYAdata=!0;let s={};t.data=s;let a=i.split(/\r?\n/),p=a[0].replace(/^.*?([A-Z]+).*$/,"$1").split("").map(e=>e.toLowerCase());for(let e=1;e<a.length;e++){let f=a[e].replace(/^\((.*)\)$/,"$1").split(/ *, */);for(let l=0;l<p.length;l++){let o=f[l];switch(p[l]){case"x":case"y":case"w":o=Number.parseFloat(o);break;case"a":o=o.replace(/^<(.*)>$/,"$1");break;case"m":break;default:continue}s[p[l]]||(s[p[l]]=[]),s[p[l]].push(o)}}}import{parseString as se}from"dynamic-typing";import le from"ml-array-median";function I(t){let i=t[0].data.y[0],s=i,a=t.length,p=t[0].data.x.length,e=new Array(a);for(let r=0;r<a;r++){e[r]=t[r].data.y;for(let n=0;n<p;n++){let m=e[r][n];m<i&&(i=m),m>s&&(s=m)}}let f=t[0].data.x[0],l=t[0].data.x[t[0].data.x.length-1],o=t[0].pageValue,u=t[a-1].pageValue;if(f>l)for(let r of e)r.reverse();o>u&&e.reverse();let c=[];for(let r=0;r<e.length;r++){let n=Float64Array.from(e[r]);for(let m=0;m<n.length;m++)n[m]<0&&(n[m]=-n[m]);c.push(le(n))}let d=le(c);return{z:e,minX:Math.min(f,l),maxX:Math.max(f,l),minY:Math.min(o,u),maxY:Math.max(o,u),minZ:i,maxZ:s,noise:d}}function E(t,i){let s=t.noise,a=t.z,p,e,f,l,o,u,c,d,r=a.length,n=a[0].length,m,h,g,T,A=t.minX,N=(t.maxX-A)/(n-1),S=t.minY,F=(t.maxY-S)/(r-1),ae=t.minZ,G=t.maxZ,H=i.nbContourLevels*2,K=new Array(H),x;for(let M=0;M<H;M++){let R={};K[M]=R;let re=M%2,_=(G-i.noiseMultiplier*s)*Math.exp((M>>1)-i.nbContourLevels);re===0?x=_+i.noiseMultiplier*s:x=0-_-i.noiseMultiplier*s;let b=[];if(R.zValue=x,R.lines=b,!(x<=ae||x>=G))for(let v=0;v<r-1;v++){let z=a[v],W=a[v+1];for(let X=0;X<n-1;X++)p=z[X],e=z[X+1],f=W[X],l=W[X+1],o=p>x,u=e>x,c=f>x,d=l>x,o!==u&&o!==c&&(m=X+(x-p)/(e-p),h=v,g=X,T=v+(x-p)/(f-p),b.push(m*N+A),b.push(h*F+S),b.push(g*N+A),b.push(T*F+S)),d!==u&&d!==c&&(m=X+1,h=v+1-(x-l)/(e-l),g=X+1-(x-l)/(f-l),T=v+1,b.push(m*N+A),b.push(h*F+S),b.push(g*N+A),b.push(T*F+S)),u!==c&&(m=(X+1-(x-e)/(f-e))*N+A,h=(v+(x-e)/(f-e))*F+S,u!==o&&(g=X+1-(x-e)/(p-e),T=v,b.push(m),b.push(h),b.push(g*N+A),b.push(T*F+S)),c!==o&&(g=X,T=v+1-(x-f)/(p-f),b.push(m),b.push(h),b.push(g*N+A),b.push(T*F+S)),u!==d&&(g=X+1,T=v+(x-e)/(l-e),b.push(m),b.push(h),b.push(g*N+A),b.push(T*F+S)),c!==d&&(g=X+(x-f)/(l-f),T=v+1,b.push(m),b.push(h),b.push(g*N+A),b.push(T*F+S)))}}return{minX:t.minX,maxX:t.maxX,minY:t.minY,maxY:t.maxY,segments:K}}function k(t,i){let s=I(t.spectra);i.noContour||(t.contourLines=E(s,i),delete s.z),t.minMax=s}import{getGyromagneticRatio as ie}from"gyromagnetic-ratio";function Z(t,i){for(let s of t){let a=0,p=0;for(let e of s.spectra){if(s.ntuples&&s.ntuples.symbol?(!a&&e.observeFrequency&&(a=e.observeFrequency),!p&&e.shiftOffsetVal&&(p=e.shiftOffsetVal)):(a=e.observeFrequency,p=e.shiftOffsetVal),a&&e.xUnits&&e.xUnits.toUpperCase().includes("HZ")){e.xUnits="PPM",e.xFactor=e.xFactor/a,e.firstX=e.firstX/a,e.lastX=e.lastX/a,e.deltaX=e.deltaX/a;for(let f=0;f<e.data.x.length;f++)e.data.x[f]/=a}if(p&&e.xUnits.toLowerCase().includes("ppm")){let f=e.firstX-p;e.firstX=e.firstX-f,e.lastX=e.lastX-f;for(let l=0;l<e.data.x.length;l++)e.data.x[l]-=f}if(s.ntuples&&s.ntuples.nucleus&&s.ntuples.symbol)for(let f=0;f<s.ntuples.nucleus.length;f++){let l=s.ntuples.symbol[f],o=s.ntuples.nucleus[f];if(l.match(/^[F|T]/)&&!o){if(l.match(/[F|T]1/))if(s.tmp.$NUC2)s.ntuples.nucleus[f]=s.tmp.$NUC2;else{let u=s.ntuples.symbol.indexOf(l.replace(/^([F|T]).*/,"$12"));u&&s.ntuples.nucleus[u]&&(s.ntuples.nucleus[f]=s.ntuples.nucleus[u])}l.match(/[F|T]2/)&&(s.ntuples.nucleus[f]=s.tmp.$NUC1)}l.match(/[F|T]2/)&&(s.yType=s.ntuples.nucleus[0])}if(a&&s.ntuples&&s.ntuples.symbol&&s.ntuples.nucleus){let f="",l=s.ntuples.symbol.indexOf(e.pageSymbol);if(s.ntuples.units&&s.ntuples.units[l]&&(f=s.ntuples.units[l]),f!=="PPM"){if(l!==0){let r="Not sure about this ntuples format";if(i)i.warn(r);else throw Error(r)}let{nucleus:o}=s.ntuples,u=ie(o[0]),c=ie(o[1]);if(!u||!c){let r=`Problem with determination of gyromagnetic ratio for ${o.join("-")}`;if(i)i.error(r);else throw Error(r)}let d=u/c*a;e.pageValue/=d}}}}}function L(t,i,s={}){s.logger?.trace(i),t.profiling&&t.profiling.push({action:i,time:Date.now()-s.start})}function j(t){let i=t.spectra[0].data;t.chromatogram={times:i.x.slice(),series:{intensity:{dimension:1,data:i.y.slice()}}}}function B(t,i,s){Z(t,s.logger),ue(t,s);for(let a of t){if(Object.keys(a.ntuples).length>0){let p=[],e=Object.keys(a.ntuples);for(let f=0;f<e.length;f++){let l=e[f],o=a.ntuples[l];for(let u=0;u<o.length;u++)p[u]||(p[u]={}),p[u][l]=o[u]}a.ntuples=p}a.twoD&&s.wantXY&&(k(a,s),L(i,"Finished countour plot calculation",s),s.keepSpectra||delete a.spectra),s.chromatogram&&(a.spectra.length>1?J(a):j(a),L(i,"Finished chromatogram calculation",s)),delete a.tmp}}function ue(t,i){for(let s of t)for(let a in s.meta){let p=s.meta[a];if(typeof p=="string"){if(p[0]==="{"){if(p[p.length-1]==="}"){let e=p.slice(1,-1).split(/[,; ]+/).filter(f=>f);for(let f=0;f<e.length;f++)s.meta[a+String(f)]=i.dynamicTyping?se(e[f]):e[f]}}else if(p[0]==="("){let e=p.split(/\r?\n/),f=/^\((?<from>\d+)\.\.(?<to>\d+)\)$/;if(f.test(e[0])){let[l,o]=e[0].match(f).slice(1).map(Number),u=e.slice(1).join(" ").split(/[,; ]+/).filter(c=>c);for(let c=l;c<=o;c++)s.meta[a+String(c)]=i.dynamicTyping?se(u[c-l]):u[c-l]}}}}}function q(t,i,s){let a=-1,p=-1,e="",f="";if(s.indexOf("++")>0)e=s.replace(/.*\(([a-zA-Z0-9]+)\+\+.*/,"$1"),f=s.replace(/.*\.\.([a-zA-Z0-9]+).*/,"$1");else{s=s.replace(/[^a-zA-Z]/g,""),e=s.charAt(0),f=s.charAt(1),i.variables={};for(let l of s){let o=l.toLowerCase(),u=t.ntuples.symbol.indexOf(l);if(u===-1)throw Error(`Symbol undefined: ${l}`);i.variables[o]={};for(let c in t.ntuples)t.ntuples[c][u]&&(i.variables[o][c.replace(/^var/,"")]=t.ntuples[c][u])}}a=t.ntuples.symbol.indexOf(e),p=t.ntuples.symbol.indexOf(f),a===-1&&(a=0),p===-1&&(p=0),t.ntuples.first&&(t.ntuples.first.length>a&&(i.firstX=t.ntuples.first[a]),t.ntuples.first.length>p&&(i.firstY=t.ntuples.first[p])),t.ntuples.last&&(t.ntuples.last.length>a&&(i.lastX=t.ntuples.last[a]),t.ntuples.last.length>p&&(i.lastY=t.ntuples.last[p])),t.ntuples.vardim&&t.ntuples.vardim.length>a&&(i.nbPoints=t.ntuples.vardim[a]),t.ntuples.factor&&(t.ntuples.factor.length>a&&(i.xFactor=t.ntuples.factor[a]),t.ntuples.factor.length>p&&(i.yFactor=t.ntuples.factor[p])),t.ntuples.units&&(t.ntuples.units.length>a&&(t.ntuples.varname&&t.ntuples.varname[a]?i.xUnits=`${t.ntuples.varname[a]} [${t.ntuples.units[a]}]`:i.xUnits=t.ntuples.units[a]),t.ntuples.units.length>p&&(t.ntuples.varname&&t.ntuples.varname[p]?i.yUnits=`${t.ntuples.varname[p]} [${t.ntuples.units[p]}]`:i.yUnits=t.ntuples.units[p]))}function w(t){t.xFactor||(t.xFactor=1),t.yFactor||(t.yFactor=1)}var C=/[ \t]*,[ \t]*/,he={keepRecordsRegExp:/^$/,canonicDataLabels:!0,canonicMetadataLabels:!1,dynamicTyping:!0,withoutXY:!1,noTrimRegExp:/^$/,chromatogram:!1,keepSpectra:!1,noContour:!1,nbContourLevels:7,noiseMultiplier:5,profiling:!1};function ge(t,i={}){t=ce(t),i={...he,...i},i.logger?.debug("Starting jcamp conversion"),i.wantXY=!i.withoutXY,i.start=Date.now();let s=[],a={profiling:i.profiling?[]:!1,logs:[],entries:[]},p={children:[]},e=p,f=[],l={};if(typeof t!="string")throw new TypeError("the JCAMP should be a string");L(a,"Before split to LDRS",i);let o=t.replace(/[\r\n]+##/g,`
|
|
3
|
+
`,children:[]}),l=t[t.length-1],o.push(l)}else if(u.substring(0,5)==="##END"&&n===0){l.jcamp+=`${d}
|
|
4
|
+
`;let f=t.pop();t.length!==0?(l=t[t.length-1],l.children.push(f)):(l=void 0,r.push(f))}else if(l&&l.jcamp){l.jcamp+=`${d}
|
|
5
|
+
`;let f=u.match(/^##(.*?)=(.+)/);if(f){let m=f[1].replace(/[ _-]/g,"").toUpperCase();m==="DATATYPE"?l.dataType=f[2].trim():m==="DATACLASS"?l.dataClass=f[2].trim():m==="JCAMPDX"?l.jcampDX=F(f[2].trim()):m==="JCAMPCS"&&(l.jcampCS=F(f[2].trim()))}}u.substring(0,5)==="##END"&&n>0&&n--}return s?(o.forEach(c=>{c.children=void 0}),o):r}import{parseString as de}from"dynamic-typing";import{ensureString as ge}from"ensure-string";var E=["TIC",".RIC","SCANNUMBER"];function te(e){let a=e.spectra,s=a.length,i={times:new Array(s),series:{ms:{dimension:2,data:new Array(s)}}},o=[];for(let t=0;t<E.length;t++){let r=I(E[t]);a[0][r]&&(o.push(r),i.series[r]={dimension:1,data:new Array(s)})}for(let t=0;t<s;t++){let r=a[t];i.times[t]=r.pageValue;for(let l=0;l<o.length;l++)i.series[o[l]].data[t]=Number(r[o[l]]);r.data&&(i.series.ms.data[t]=[r.data.x,r.data.y])}e.chromatogram=i}function le(e){return E.indexOf(e)!==-1}function I(e){return e.toLowerCase().replace(/[^a-z0-9]/g,"")}function Y(e){let a=[];for(let s=0;s<e.length;s++)a.push(Number(e[s]));return a}function U(e,a){let s=e.yFactor,i=e.deltaX;e.isXYdata=!0;let o={x:[],y:[]};e.data=o;let t=e.firstX,r=e.firstY,l=!1,n,p=0;for(;p<a.length;p++)if(n=a.charCodeAt(p),n===13||n===10)l=!0;else if(l)break;let c=!0,d=!1,u=!1,f=0,m=!1,h=!1,g=0,T=0,N=!1,y=!1,X=!1,A=0;for(;p<=a.length;p++)if(p===a.length?n=13:n=a.charCodeAt(p),h)(n===13||n===10)&&(c=!0,h=!1);else if(n<=57&&n>=48)y=!0,A>0?g+=(n-48)/Math.pow(10,A++):(g*=10,g+=n-48);else if(n===44||n===46)y=!0,A++;else{if(y){if(c)c=!1,u&&(X=!0);else if(X)X=!1;else{d?(f=N?0-g:g,u=!0,d=!1):m||(T=N?0-g:g);let O=m?g-1:1;for(let v=0;v<O;v++)u?r+=f:r=T,o.x.push(t),o.y.push(r*s),t+=i}N=!1,g=0,A=0,y=!1,m=!1}if(n<74&&n>63)y=!0,u=!1,g=n-64;else if(n>96&&n<106)y=!0,u=!1,g=n-96,N=!0;else if(n===115)y=!0,m=!0,g=9;else if(n>82&&n<91)y=!0,m=!0,g=n-82;else if(n>73&&n<83)y=!0,d=!0,g=n-73;else if(n>105&&n<115)y=!0,d=!0,g=n-105,N=!0;else if(n===36&&a.charCodeAt(p+1)===36)y=!0,h=!0;else if(n===37)y=!0,d=!0,g=0,N=!1;else if(n===45){let O=a.charCodeAt(p+1);(O>=48&&O<=57||O===44||O===46)&&(y=!0,c||(u=!1),N=!0)}else(n===13||n===10)&&(c=!0,h=!1)}}var se=/[,\t ]+/;function D(e,a,s){if(e.isPeaktable=!0,!e.variables||Object.keys(e.variables)===2?pe(e,a,s):me(e,a,s),e.variables)for(let i in e.variables)e.variables[i].data=e.data[i]}function pe(e,a,s){let i={x:[],y:[]};e.data=i;let o=a.split(/,? *,?[;\r\n]+ */);for(let t=1;t<o.length;t++){let r=o[t].trim().replace(P,"").split(se);if(r.length%2===0)for(let l=0;l<r.length;l=l+2)i.x.push(Number(r[l])*e.xFactor),i.y.push(Number(r[l+1])*e.yFactor);else s.logs.push(`Format error: ${r}`)}}function me(e,a,s){let i={},o=Object.keys(e.variables),t=o.length;o.forEach(l=>i[l]=[]),e.data=i;let r=a.split(/,? *,?[;\r\n]+ */);for(let l=1;l<r.length;l++){let n=r[l].trim().replace(P,"").split(se);if(n.length%t===0)for(let p=0;p<n.length;p++)i[o[p%t]].push(Number(n[p]));else s.logs.push(`Format error: ${n}`)}}function V(e,a){e.isXYAdata=!0;let s={};e.data=s;let i=a.split(/\r?\n/),o=i[0].replace(/^.*?([A-Z]+).*$/,"$1").split("").map(t=>t.toLowerCase());for(let t=1;t<i.length;t++){let r=i[t].replace(/^\((.*)\)$/,"$1").split(/ *, */);for(let l=0;l<o.length;l++){let n=r[l];switch(o[l]){case"x":case"y":case"w":n=Number.parseFloat(n);break;case"a":n=n.replace(/^<(.*)>$/,"$1");break;case"m":break;default:continue}s[o[l]]||(s[o[l]]=[]),s[o[l]].push(n)}}}import{parseString as re}from"dynamic-typing";import{getGyromagneticRatio as ie}from"gyromagnetic-ratio";import ae from"ml-array-median";function B(e){let{spectra:a}=e,s=a[0].data.y[0],i=s,o=a.length,t=a[0].data.x.length,r=new Array(o);for(let f=0;f<o;f++){r[f]=a[f].data.y;for(let m=0;m<t;m++){let h=r[f][m];h<s&&(s=h),h>i&&(i=h)}}let l=a[0].data.x[0],n=a[0].data.x[a[0].data.x.length-1],{firstY:p,lastY:c}=ce(e);if(l>n)for(let f of r)f.reverse();p>c&&r.reverse();let d=[];for(let f=0;f<r.length;f++){let m=Float64Array.from(r[f]);for(let h=0;h<m.length;h++)m[h]<0&&(m[h]=-m[h]);d.push(ae(m))}let u=ae(d);return{z:r,minX:Math.min(l,n),maxX:Math.max(l,n),minY:Math.min(p,c),maxY:Math.max(p,c),minZ:s,maxZ:i,noise:u}}function ce(e){let{spectra:a,ntuples:s,info:i}=e;if(s)for(let o=0;o<s.length;o++){let{symbol:t,nucleus:r}=s[o];if(t.match(/[F|T]1/)){let l=i[".OBSERVEFREQUENCY"],{nucleus:n}=s.find(m=>m.symbol.match(/[F|T]2/));if([l,n,r].some(m=>!m))break;let p=ie(n),c=ie(r),{first:d,last:u}=s[o],f=l*c/p;return{firstY:d/f,lastY:u/f}}}return{firstY:a[0].pageValue,lastY:a[a.length-1].pageValue}}function k(e,a){let s=e.noise,i=e.z,o,t,r,l,n,p,c,d,u=i.length,f=i[0].length,m,h,g,T,N=e.minX,X=(e.maxX-N)/(f-1),A=e.minY,v=(e.maxY-A)/(u-1),ne=e.minZ,K=e.maxZ,Q=a.nbContourLevels*2,_=new Array(Q),x;for(let R=0;R<Q;R++){let $={};_[R]=$;let oe=R%2,z=(K-a.noiseMultiplier*s)*Math.exp((R>>1)-a.nbContourLevels);oe===0?x=z+a.noiseMultiplier*s:x=0-z-a.noiseMultiplier*s;let b=[];if($.zValue=x,$.lines=b,!(x<=ne||x>=K))for(let C=0;C<u-1;C++){let W=i[C],ee=i[C+1];for(let S=0;S<f-1;S++)o=W[S],t=W[S+1],r=ee[S],l=ee[S+1],n=o>x,p=t>x,c=r>x,d=l>x,n!==p&&n!==c&&(m=S+(x-o)/(t-o),h=C,g=S,T=C+(x-o)/(r-o),b.push(m*X+N),b.push(h*v+A),b.push(g*X+N),b.push(T*v+A)),d!==p&&d!==c&&(m=S+1,h=C+1-(x-l)/(t-l),g=S+1-(x-l)/(r-l),T=C+1,b.push(m*X+N),b.push(h*v+A),b.push(g*X+N),b.push(T*v+A)),p!==c&&(m=(S+1-(x-t)/(r-t))*X+N,h=(C+(x-t)/(r-t))*v+A,p!==n&&(g=S+1-(x-t)/(o-t),T=C,b.push(m),b.push(h),b.push(g*X+N),b.push(T*v+A)),c!==n&&(g=S,T=C+1-(x-r)/(o-r),b.push(m),b.push(h),b.push(g*X+N),b.push(T*v+A)),p!==d&&(g=S+1,T=C+(x-t)/(l-t),b.push(m),b.push(h),b.push(g*X+N),b.push(T*v+A)),c!==d&&(g=S+(x-r)/(l-r),T=C+1,b.push(m),b.push(h),b.push(g*X+N),b.push(T*v+A)))}}return{minX:e.minX,maxX:e.maxX,minY:e.minY,maxY:e.maxY,segments:_}}function j(e,a){let s=B(e);a.noContour||(e.contourLines=k(s,a),delete s.z),e.minMax=s}import{getGyromagneticRatio as Z}from"gyromagnetic-ratio";function J(e,a){for(let s of e){let i=0,o=0;for(let t of s.spectra){if(s.ntuples&&s.ntuples.symbol?(!i&&t.observeFrequency&&(i=t.observeFrequency),!o&&t.shiftOffsetVal&&(o=t.shiftOffsetVal)):(i=t.observeFrequency,o=t.shiftOffsetVal),i&&t.xUnits&&t.xUnits.toUpperCase().includes("HZ")){t.xUnits="PPM",t.xFactor=t.xFactor/i,t.firstX=t.firstX/i,t.lastX=t.lastX/i,t.deltaX=t.deltaX/i;for(let r=0;r<t.data.x.length;r++)t.data.x[r]/=i}if(o&&t.xUnits.toLowerCase().includes("ppm")){let r=t.firstX-o;t.firstX=t.firstX-r,t.lastX=t.lastX-r;for(let l=0;l<t.data.x.length;l++)t.data.x[l]-=r}if(s.ntuples&&s.ntuples.nucleus&&s.ntuples.symbol)for(let r=0;r<s.ntuples.nucleus.length;r++){let l=s.ntuples.symbol[r],n=s.ntuples.nucleus[r];if(l.match(/^[F|T]/)&&!n){if(l.match(/[F|T]1/))if(s.tmp.$NUC2)s.ntuples.nucleus[r]=s.tmp.$NUC2;else{let p=s.ntuples.symbol.indexOf(l.replace(/^([F|T]).*/,"$12"));p&&s.ntuples.nucleus[p]&&(s.ntuples.nucleus[r]=s.ntuples.nucleus[p])}l.match(/[F|T]2/)&&(s.ntuples.nucleus[r]=s.tmp.$NUC1)}l.match(/[F|T]2/)&&(s.yType=s.ntuples.nucleus[0],Z(s.xType)||(s.xType=s.ntuples.nucleus[1]))}if(i&&s.ntuples&&s.ntuples.symbol&&s.ntuples.nucleus){let r="",l=s.ntuples.symbol.indexOf(t.pageSymbol);if(s.ntuples.units&&s.ntuples.units[l]&&(r=s.ntuples.units[l]),r!=="PPM"){if(l!==0){let u="Not sure about this ntuples format";if(a)a.warn(u);else throw Error(u)}let{nucleus:n}=s.ntuples,p=Z(n[0]),c=Z(n[1]);if(!p||!c){let u=`Problem with determination of gyromagnetic ratio for ${n.join("-")}`;if(a)a.error(u);else throw Error(u)}let d=p/c*i;t.pageValue/=d}}}}}function L(e,a,s={}){s.logger?.trace(a),e.profiling&&e.profiling.push({action:a,time:Date.now()-s.start})}function G(e){let a=e.spectra[0].data;e.chromatogram={times:a.x.slice(),series:{intensity:{dimension:1,data:a.y.slice()}}}}function H(e,a,s){J(e,s.logger),he(e,s);for(let i of e){if(Object.keys(i.ntuples).length>0){let o=[],t=Object.keys(i.ntuples);for(let r=0;r<t.length;r++){let l=t[r],n=i.ntuples[l];for(let p=0;p<n.length;p++)o[p]||(o[p]={}),o[p][l]=n[p]}i.ntuples=o}i.twoD&&s.wantXY&&(j(i,s),L(a,"Finished countour plot calculation",s),s.keepSpectra||delete i.spectra),s.chromatogram&&(i.spectra.length>1?te(i):G(i),L(a,"Finished chromatogram calculation",s)),delete i.tmp}}function he(e,a){for(let s of e)for(let i in s.meta){let o=s.meta[i];if(typeof o=="string"){if(o[0]==="{"){if(o[o.length-1]==="}"){let t=o.slice(1,-1).split(/[,; ]+/).filter(r=>r);for(let r=0;r<t.length;r++)s.meta[i+String(r)]=a.dynamicTyping?re(t[r]):t[r]}}else if(o[0]==="("){let t=o.split(/\r?\n/),r=/^\((?<from>\d+)\.\.(?<to>\d+)\)$/;if(r.test(t[0])){let[l,n]=t[0].match(r).slice(1).map(Number),p=t.slice(1).join(" ").split(/[,; ]+/).filter(c=>c);for(let c=l;c<=n;c++)s.meta[i+String(c)]=a.dynamicTyping?re(p[c-l]):p[c-l]}}}}}function q(e,a,s){let i=-1,o=-1,t="",r="";if(s.indexOf("++")>0)t=s.replace(/.*\(([a-zA-Z0-9]+)\+\+.*/,"$1"),r=s.replace(/.*\.\.([a-zA-Z0-9]+).*/,"$1");else{s=s.replace(/[^a-zA-Z]/g,""),t=s.charAt(0),r=s.charAt(1),a.variables={};for(let l of s){let n=l.toLowerCase(),p=e.ntuples.symbol.indexOf(l);if(p===-1)throw Error(`Symbol undefined: ${l}`);a.variables[n]={};for(let c in e.ntuples)e.ntuples[c][p]&&(a.variables[n][c.replace(/^var/,"")]=e.ntuples[c][p])}}i=e.ntuples.symbol.indexOf(t),o=e.ntuples.symbol.indexOf(r),i===-1&&(i=0),o===-1&&(o=0),e.ntuples.first&&(e.ntuples.first.length>i&&(a.firstX=e.ntuples.first[i]),e.ntuples.first.length>o&&(a.firstY=e.ntuples.first[o])),e.ntuples.last&&(e.ntuples.last.length>i&&(a.lastX=e.ntuples.last[i]),e.ntuples.last.length>o&&(a.lastY=e.ntuples.last[o])),e.ntuples.vardim&&e.ntuples.vardim.length>i&&(a.nbPoints=e.ntuples.vardim[i]),e.ntuples.factor&&(e.ntuples.factor.length>i&&(a.xFactor=e.ntuples.factor[i]),e.ntuples.factor.length>o&&(a.yFactor=e.ntuples.factor[o])),e.ntuples.units&&(e.ntuples.units.length>i&&(e.ntuples.varname&&e.ntuples.varname[i]?a.xUnits=`${e.ntuples.varname[i]} [${e.ntuples.units[i]}]`:a.xUnits=e.ntuples.units[i]),e.ntuples.units.length>o&&(e.ntuples.varname&&e.ntuples.varname[o]?a.yUnits=`${e.ntuples.varname[o]} [${e.ntuples.units[o]}]`:a.yUnits=e.ntuples.units[o]))}function w(e){e.xFactor||(e.xFactor=1),e.yFactor||(e.yFactor=1)}var M=/[ \t]*,[ \t]*/,be={keepRecordsRegExp:/^$/,canonicDataLabels:!0,canonicMetadataLabels:!1,dynamicTyping:!0,withoutXY:!1,noTrimRegExp:/^$/,chromatogram:!1,keepSpectra:!1,noContour:!1,nbContourLevels:7,noiseMultiplier:5,profiling:!1};function xe(e,a={}){e=ge(e),a={...be,...a},a.logger?.debug("Starting jcamp conversion"),a.wantXY=!a.withoutXY,a.start=Date.now();let s=[],i={profiling:a.profiling?[]:!1,logs:[],entries:[]},o={children:[]},t=o,r=[],l={};if(typeof e!="string")throw new TypeError("the JCAMP should be a string");L(i,"Before split to LDRS",a);let n=e.replace(/[\r\n]+##/g,`
|
|
6
6
|
##`).split(`
|
|
7
|
-
##`);L(
|
|
8
|
-
`);if(m===-1&&(m=
|
|
7
|
+
##`);L(i,"Split to LDRS",a),n[0]&&(n[0]=n[0].replace(/^[\r\n ]*##/,""));for(let p of n){let c=p.indexOf("="),d=c>0?p.substring(0,c):p,u=c>0?d.match(a.noTrimRegExp)?p.substring(c+1):p.substring(c+1).trim():"",f=d.replace(/[_ -]/g,"").toUpperCase();if(f==="DATATABLE"){let m=u.indexOf(`
|
|
8
|
+
`);if(m===-1&&(m=u.indexOf("\r")),m>0){let h=u.substring(0,m).split(/[ ,;\t]+/);q(t,l,h[0]),l.datatable=h[0],h[1]&&h[1].indexOf("PEAKS")>-1?f="PEAKTABLE":h[1]&&(h[1].indexOf("XYDATA")||h[0].indexOf("++")>0)&&(f="XYDATA",l.nbPoints&&(l.deltaX=(l.lastX-l.firstX)/(l.nbPoints-1)))}}if(f==="XYDATA"){a.wantXY&&(w(l),u.match(/.*\+\+.*/)?(l.nbPoints&&(l.deltaX=(l.lastX-l.firstX)/(l.nbPoints-1)),U(l,u,i)):D(l,u,i),t.spectra.push(l),l={});continue}else if(f==="PEAKTABLE"){a.wantXY&&(w(l),D(l,u,i),t.spectra.push(l),l={});continue}if(f==="PEAKASSIGNMENTS"){a.wantXY&&(u.match(/.*([^A-Z]*).*/)&&V(l,u),t.spectra.push(l),l={});continue}if(f==="TITLE"){let m=t;m.children||(m.children=[]),t={spectra:[],ntuples:{},info:{},meta:{},tmp:{}},m.children.push(t),r.push(m),s.push(t),t.title=u}else f==="DATATYPE"?(t.dataType=u,u.match(/(^nd|\snd\s)/i)&&(t.twoD=!0)):f==="NTUPLES"?u.match(/(^nd|\snd\s)/i)&&(t.twoD=!0):f==="DATACLASS"?t.dataClass=u:f==="JCAMPDX"?t.jcampDX=F(u):f==="JCAMPCS"?t.jcampCS=F(u):f==="XUNITS"?l.xUnits=u:f==="YUNITS"?l.yUnits=u:f==="FIRSTX"?l.firstX=Number(u):f==="LASTX"?l.lastX=Number(u):f==="FIRSTY"?l.firstY=Number(u):f==="LASTY"?l.lastY=Number(u):f==="NPOINTS"?l.nbPoints=Number(u):f==="XFACTOR"?l.xFactor=Number(u):f==="YFACTOR"?l.yFactor=Number(u):f==="MAXX"?l.maxX=Number(u):f==="MINX"?l.minX=Number(u):f==="MAXY"?l.maxY=Number(u):f==="MINY"?l.minY=Number(u):f==="DELTAX"?l.deltaX=Number(u):f===".OBSERVEFREQUENCY"||f==="$SFO1"?l.observeFrequency||(l.observeFrequency=Number(u)):f===".OBSERVENUCLEUS"?l.xType||(t.xType=u.replace(/[^a-zA-Z0-9]/g,"")):f==="$OFFSET"?(t.shiftOffsetNum=0,l.shiftOffsetVal||(l.shiftOffsetVal=Number(u))):f==="$REFERENCEPOINT"||(f==="VARNAME"?t.ntuples.varname=u.split(M):f==="SYMBOL"?t.ntuples.symbol=u.split(M):f==="VARTYPE"?t.ntuples.vartype=u.split(M):f==="VARFORM"?t.ntuples.varform=u.split(M):f==="VARDIM"?t.ntuples.vardim=Y(u.split(M)):f==="UNITS"?t.ntuples.units=u.split(M):f==="FACTOR"?t.ntuples.factor=Y(u.split(M)):f==="FIRST"?t.ntuples.first=Y(u.split(M)):f==="LAST"?t.ntuples.last=Y(u.split(M)):f==="MIN"?t.ntuples.min=Y(u.split(M)):f==="MAX"?t.ntuples.max=Y(u.split(M)):f===".NUCLEUS"?t.ntuples&&(t.ntuples.nucleus=u.split(M).map(m=>m.replace(/[^a-zA-Z0-9]/g,""))):f==="PAGE"?(l.page=u.trim(),l.pageValue=Number(u.replace(/^.*=/,"")),l.pageSymbol=l.page.replace(/[=].*/,"")):f==="RETENTIONTIME"?l.pageValue=Number(u):le(f)?l[I(f)]=u:f==="SAMPLEDESCRIPTION"?l.sampleDescription=u:f.startsWith("$NUC")?!t.tmp[f]&&!u.includes("off")&&(t.tmp[f]=u.replace(/[<>]/g,"")):f==="END"&&(t=r.pop()));if(t&&t.info&&t.meta&&f.match(a.keepRecordsRegExp)){let m,h;d.startsWith("$")?(h=a.canonicMetadataLabels?f.substring(1):d.substring(1),m=t.meta):(h=a.canonicDataLabels?f:d,m=t.info),a.dynamicTyping&&(u=de(u)),m[h]?(Array.isArray(m[h])||(m[h]=[m[h]]),m[h].push(u)):m[h]=u}}if(L(i,"Finished parsing",a),H(s,i,a),L(i,"Total time",a),i.entries=o.children,i.flatten=s,a.logger){a.logger.debug("Finished jcamp conversion");for(let p of i.flatten)a.logger.debug(`${p.dataType} - ${p.title}`)}return i}function Ne(e,a={}){let{logger:s,OCL:i}=a;if(!e.info.ATOMLIST||!e.info.BONDLIST){s?.warn("No ATOMLIST or BONDLIST in the JCAMP-CS entry");return}Xe(e,s);let o=Ae(e.info.ATOMLIST),t=Se(e.info.BONDLIST),r=ye(e.info.CHARGE,s),l=Te(i,o,t,r,s);return{molecule:l,molfile:l.toMolfile()}}function Te(e,a,s,i,o){let t={S:e.Molecule.cBondTypeSingle,D:e.Molecule.cBondTypeDouble,T:e.Molecule.cBondTypeTriple,Q:e.Molecule.cBondTypeQuadruple},r={},l=new e.Molecule(a.length,s.length);for(let n of a){let p=e.Molecule.getAtomicNoFromLabel(n.element);p||(o?.error(`Atomic number of ${n.element} could not be determined`),p=e.Molecule.getAtomicNoFromLabel("X")),r[n.atomNumber]=l.addAtom(p),l.setAtomMapNo(r[n.atomNumber],n.atomNumber)}for(let n of s){if(r[n.atomNumber1]===void 0){o?.error(`A bond goes to atomNumber ${n.atomNumber1} that does not exists`);continue}if(r[n.atomNumber2]===void 0){o?.error(`A bond goes to atomNumber ${n.atomNumber2} that does not exists`);continue}let p=l.addBond(r[n.atomNumber1],r[n.atomNumber2]);l.setBondType(p,t[n.bondType])}for(let n of i){if(r[n.atomNumber]===void 0){o?.error(`A charge goes to atomNumber ${n.atomNumber1} that does not exists`);continue}l.setAtomCharge(r[n.atomNumber],n.value)}return l}function Ae(e){let a=e.split(/\r?\n/).map(i=>F(i)).filter(i=>i),s=[];for(let i of a){let[o,t,r]=i.trim().split(/\s+/);s.push({atomNumber:Number(o),element:t,nbImplicitHydrogens:r?.length>0?Number(r):void 0})}return s}function Se(e){let a=e.split(/\r?\n/).map(i=>F(i)).filter(i=>i),s=[];for(let i of a){let[o,t,r]=i.trim().split(/\s+/);s.push({atomNumber1:Number(o),atomNumber2:Number(t),bondType:r})}return s}function ye(e,a){if(!e)return[];let s=e.split(/\r?\n/).map(o=>F(o)).filter(o=>o),i=[];for(let o of s){let[t,r,l]=o.trim().split(/\s+/);l!==void 0&&a?.warn("Charge on multiple atom is not supported"),i.push({atomNumber:Number(r),value:Number(t)})}return i}function Xe(e,a){let s=["STEREOPAIR","STEREOCENTER","RADICAL","STEREOMOLECULE"];for(let i of s)e.info[i]&&a?.warn(`JCAMP-CS parser do not support: ${i}`)}export{xe as convert,ue as createTree,Ne as parseJcampCS};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "jcampconverter",
|
|
3
|
-
"version": "9.
|
|
3
|
+
"version": "9.5.0",
|
|
4
4
|
"description": "Parse and convert JCAMP data",
|
|
5
5
|
"types": "jcampconverter.d.ts",
|
|
6
6
|
"main": "jcampconverter.cjs",
|
|
@@ -51,23 +51,24 @@
|
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
53
53
|
"@microsoft/api-extractor": "^7.38.3",
|
|
54
|
-
"@types/jest": "^29.5.
|
|
54
|
+
"@types/jest": "^29.5.10",
|
|
55
55
|
"@types/pino": "^7.0.5",
|
|
56
56
|
"@vitest/coverage-v8": "^0.34.6",
|
|
57
57
|
"benchmark": "^2.1.4",
|
|
58
58
|
"c8": "^8.0.1",
|
|
59
59
|
"cheminfo-build": "^1.2.0",
|
|
60
|
-
"esbuild": "^0.19.
|
|
61
|
-
"eslint": "^8.
|
|
60
|
+
"esbuild": "^0.19.7",
|
|
61
|
+
"eslint": "^8.54.0",
|
|
62
62
|
"eslint-config-cheminfo-typescript": "^12.0.4",
|
|
63
63
|
"esm": "^3.2.25",
|
|
64
64
|
"fifo-logger": "^0.6.1",
|
|
65
65
|
"jest-matcher-deep-close-to": "^3.0.2",
|
|
66
66
|
"jscpd": "^3.5.10",
|
|
67
67
|
"ml-spectra-processing": "^12.6.0",
|
|
68
|
+
"openchemlib": "^8.7.2",
|
|
68
69
|
"prettier": "^3.1.0",
|
|
69
70
|
"rimraf": "^5.0.5",
|
|
70
|
-
"typescript": "^5.
|
|
71
|
+
"typescript": "^5.3.2",
|
|
71
72
|
"vitest": "^0.34.6"
|
|
72
73
|
},
|
|
73
74
|
"dependencies": {
|