jcampconverter 10.0.1 → 10.0.2
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 +3 -3
- package/jcampconverter.d.ts +8 -8
- package/jcampconverter.mjs +3 -3
- package/package.json +1 -1
package/jcampconverter.cjs
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var de=Object.create;var $=Object.defineProperty;var be=Object.getOwnPropertyDescriptor;var ge=Object.getOwnPropertyNames;var he=Object.getPrototypeOf,xe=Object.prototype.hasOwnProperty;var ye=(e,f)=>{for(var t in f)$(e,t,{get:f[t],enumerable:!0})},se=(e,f,t,o)=>{if(f&&typeof f=="object"||typeof f=="function")for(let a of ge(f))!xe.call(e,a)&&a!==t&&$(e,a,{get:()=>f[a],enumerable:!(o=be(f,a))||o.enumerable});return e};var Ae=(e,f,t)=>(t=e!=null?de(he(e)):{},se(f||!e||!e.__esModule?$(t,"default",{value:e,enumerable:!0}):t,e)),Ce=e=>se($({},"__esModule",{value:!0}),e);var $e={};ye($e,{convert:()=>Me,createTree:()=>Ne,parseJcampCS:()=>Oe});module.exports=Ce($e);var oe=require("ensure-string");var P=/\s*\$\$.*/;function O(e){return e.replace(P,"")}function Ne(e,f={}){e=(0,oe.ensureString)(e);let{flatten:t=!1}=f,o=e.split(/\r?\n/),a=[],i=[],n=[],m={title:"",jcamp:"",children:[]},r=0,p=e.includes("## ");for(let u=0;u<o.length;u++){let b=o[u],h=p?b.replaceAll(" ",""):b;if(h.startsWith("##NTUPLES")&&r++,h.startsWith("##TITLE")){let s=[h.slice(8).trim()];for(let l=u+1;l<o.length&&!o[l].startsWith("##");l++)s.push(o[l].trim());i.push({title:s.join(`
|
|
2
2
|
`),jcamp:`${b}
|
|
3
3
|
`,children:[]}),m=i.at(-1),a.push(m)}else if(h.startsWith("##END")&&r===0&&m){m.jcamp+=`${b}
|
|
4
4
|
`;let s=i.pop();if(!s)throw new Error("Not finished");i.length>0?(m=i.at(-1),m.children?.push(s)):(m=void 0,n.push(s))}else if(m?.jcamp){m.jcamp+=`${b}
|
|
5
|
-
`;let s=h.match(/^##(.*?)=(.+)/);if(s){let l=s[1].replaceAll(/[ _-]/g,"").toUpperCase();l==="DATATYPE"?m.dataType=s[2].trim():l==="DATACLASS"?m.dataClass=s[2].trim():l==="JCAMPDX"?m.jcampDX=O(s[2].trim()):l==="JCAMPCS"&&(m.jcampCS=O(s[2].trim()))}}h.startsWith("##END")&&r>0&&r--}if(t){for(let u of a)u.children=void 0;return a}else return n}var ce=require("dynamic-typing"),pe=require("ensure-string");var ae=["TIC",".RIC","SCANNUMBER"];function le(e){let f=e.spectra,t=f.length,o={times:new Array(t),series:{ms:{dimension:2,data:new Array(t)}}},a=[];for(let i of ae){let n=B(i);f[0][n]&&(a.push(n),o.series[n]={dimension:1,data:new Array(t)})}for(let i=0;i<t;i++){let n=f[i];o.times[i]=n.pageValue;for(let m of a)o.series[m].data[i]=Number(n[m]);n.data&&(o.series.ms.data[i]=[n.data.x,n.data.y])}e.chromatogram=o}function fe(e){return ae.includes(e)}function B(e){return e.toLowerCase().replaceAll(/[^a-z0-9]/g,"")}function E(e){return e.map(Number)}function j(e,f,t){let{logger:o}=t;o&&(e.xFactor||o.info("The xFactor is not defined, it will be set to 1"),e.yFactor||o.info("The yFactor is not defined, it will be set to 1"));let a=e.yFactor??1,i=e.xFactor??1,n=e.deltaX??1,m=Math.abs(n*.1),r=.01;e.isXYdata=!0;let p={x:[],y:[]};e.data=p;let u=e.firstX||0,b=e.firstY||0,h=!1,s,l=0;for(;l<f.length;l++)if(s=f.codePointAt(l),s===13||s===10)h=!0;else if(h)break;let c=0,d=!0,A=!1,y=!1,k=0,S=!1,N=!1,g=0,X=0,M=!1,C=!1,Y=!1,R=0;for(;l<=f.length;l++)if(l===f.length?s=13:s=f.codePointAt(l),N)(s===13||s===10)&&(d=!0,N=!1,c++);else if(s<=57&&s>=48)C=!0,R>0?g+=(s-48)/10**R++:(g*=10,g+=s-48);else if(s===44||s===46)C=!0,R++;else{if(C){if(M&&(g=g*-1,M=!1),d)g*=i,o&&(y?Math.abs(u-n-g)>m&&(Math.abs(u-g)<m?o.trace(`Data line ${c}: After a DIFFERENCE the next line should repeat the last value and this does not seem to be the case: Current value: ${g} - Expected: ${u}.`):Math.abs(u-n-g)>10*m?o.trace(`Data line ${c}: The difference between the first value ${g} and the expected first x value ${u} based on increment after a DIFFERENCE is too high`):o.trace(`Data line ${c}: The difference between the first value ${g} and the expected first x value ${u} based on increment after a DIFFERENCE is too high`)):Math.abs(u-g)>m&&o.trace(`Data line ${c}: The difference between the first value ${g} and the first x value ${u} is too high`)),d=!1,y&&(Y=!0);else if(Y)Y=!1,o&&Math.abs(b-g)>r&&o.trace(`Data line ${c}: After a duplicate the next line should repeat the same value ${g} !== ${b}`),b=g;else{A?(k=M?0-g:g,y=!0,A=!1):S||(X=M?0-g:g);let x=S?g-1:1;for(let F=0;F<x;F++)y?b+=k:b=X,p.x.push(u),p.y.push(b*a),u+=n}M=!1,g=0,R=0,C=!1,S=!1}if(s<74&&s>63)C=!0,y=!1,g=s-64;else if(s>96&&s<106)C=!0,y=!1,g=s-96,M=!0;else if(s===115)C=!0,S=!0,g=9;else if(s>82&&s<91)C=!0,S=!0,g=s-82;else if(s>73&&s<83)C=!0,A=!0,g=s-73;else if(s>105&&s<115)C=!0,A=!0,g=s-105,M=!0;else if(s===36&&f.codePointAt(l+1)===36)C=!0,N=!0;else if(s===37)C=!0,A=!0,g=0,M=!1;else if(s===45){let x=f.codePointAt(l+1);(x!==void 0&&x>=48&&x<=57||x===44||x===46)&&(C=!0,d||(y=!1),M=!0)}else(s===13||s===10)&&(d=!0,N=!1,c++)}o&&y&&o.warn("The last value is a difference, it should be repeated on the next line")}var me=/[,\t ]+/;function w(e,f,t){if(e.isPeaktable=!0,!e.variables||Object.keys(e.variables).length===2?ve(e,f,t):Xe(e,f,t),e.variables)for(let o in e.variables)e.variables[o].data=e.data?.[o]}function ve(e,f,t){let{logger:o}=t,a={x:[],y:[]};e.data=a;let i=f.split(/,? *,?[;\r\n]+ */);for(let n=1;n<i.length;n++){let m=i[n].trim().replace(P,"").split(me);if(m.length%2===0)for(let r=0;r<m.length;r=r+2)e.xFactor!==void 0&&e.yFactor!==void 0&&(a.x.push(Number(m[r])*e.xFactor),a.y.push(Number(m[r+1])*e.yFactor));else o?.warn(`Format error: ${m.toString()}`)}}function Xe(e,f,t){let{logger:o}=t,a={},i=Object.keys(e.variables),n=i.length;for(let r of i)a[r]=[];e.data=a;let m=f.split(/,? *,?[;\r\n]+ */);for(let r=1;r<m.length;r++){let p=m[r].trim().replace(P,"").split(me);if(p.length%n===0)for(let u=0;u<p.length;u++)a[i[u%n]].push(Number(p[u]));else o?.warn(`Wrong number of columns: ${p.toString()}`)}}function Z(e,f){e.isXYAdata=!0;let t={};e.data=t;let o=f.split(/\r?\n/),a=o[0].replace(/^.*?([A-Z]+).*$/,"$1").split("").map(i=>i.toLowerCase());for(let i=1;i<o.length;i++){let n=o[i].replace(/^\((.*)\)$/,"$1").split(/ *, */);for(let m=0;m<a.length;m++){let r=n[m];switch(a[m]){case"x":case"y":case"w":r=Number.parseFloat(r);break;case"a":r=r.replace(/^<(.*)>$/,"$1");break;case"m":break;default:continue}t[a[m]]||(t[a[m]]=[]),t[a[m]].push(r)}}}var Q=require("dynamic-typing");var G=require("gyromagnetic-ratio"),W=Ne(require("ml-array-median"));function ue(e){return Array.isArray(e)?e[0]:e}function H(e){let{spectra:f}=e,t=f[0].data.y[0],o=t,a=f.length,i=f[0].data.x.length||0,n=new Array(a);for(let s=0;s<a;s++){n[s]=f[s].data.y;for(let l=0;l<i;l++){let c=n[s][l];c<t&&(t=c),c>o&&(o=c)}}let m=f[0].data.x[0],r=f[0].data.x.at(-1),{firstY:p,lastY:u}=Le(e);if(m>r)for(let s of n)s.reverse();p>u&&n.reverse();let b=[];for(let s of n){let l=Float64Array.from(s);for(let c=0;c<l.length;c++)l[c]<0&&(l[c]=-l[c]);b.push((0,W.default)(l))}let h=(0,W.default)(b);return{z:n,minX:Math.min(m,r),maxX:Math.max(m,r),minY:Math.min(p,u),maxY:Math.max(p,u),minZ:t,maxZ:o,noise:h}}function Le(e){let{spectra:f,ntuples:t,info:o}=e;if(t)for(let a of t){let{symbol:i,nucleus:n,units:m}=a;if(i.match(/[F|T]1/)&&m?.toUpperCase().match("HZ")){let r=ue(o[".OBSERVEFREQUENCY"]),{nucleus:p}=t.find(c=>c.symbol.match(/[F|T]2/));if([r,p,n].some(c=>!c))break;let u=(0,G.getGyromagneticRatio)(p),b=(0,G.getGyromagneticRatio)(n),{first:h,last:s}=a,l=b!==null&&u!==null?r*b/u:r;return{firstY:h/l,lastY:s/l}}}return{firstY:f[0].pageValue,lastY:f.at(-1).pageValue}}function J(e,f){let t=e.noise,o=e.z,a,i,n,m,r,p,u,b,h=o.length,s=o[0].length,l,c,d,A,y=e.minX,S=(e.maxX-y)/(s-1),N=e.minY,X=(e.maxY-N)/(h-1),M=e.minZ,C=e.maxZ,Y=f.nbContourLevels*2,R=new Array(Y),x;for(let F=0;F<Y;F++){let V={};R[F]=V;let ge=F%2,te=(C-f.noiseMultiplier*t)*Math.exp((F>>1)-f.nbContourLevels);ge===0?x=te+f.noiseMultiplier*t:x=0-te-f.noiseMultiplier*t;let D=[];if(V.zValue=x,V.lines=D,!(x<=M||x>=C))for(let v=0;v<h-1;v++){let ne=o[v],re=o[v+1];for(let T=0;T<s-1;T++)a=ne[T],i=ne[T+1],n=re[T],m=re[T+1],r=a>x,p=i>x,u=n>x,b=m>x,r!==p&&r!==u&&(l=T+(x-a)/(i-a),c=v,d=T,A=v+(x-a)/(n-a),D.push(l*S+y,c*X+N,d*S+y,A*X+N)),b!==p&&b!==u&&(l=T+1,c=v+1-(x-m)/(i-m),d=T+1-(x-m)/(n-m),A=v+1,D.push(l*S+y,c*X+N,d*S+y,A*X+N)),p!==u&&(l=(T+1-(x-i)/(n-i))*S+y,c=(v+(x-i)/(n-i))*X+N,p!==r&&(d=T+1-(x-i)/(a-i),A=v,D.push(l,c,d*S+y,A*X+N)),u!==r&&(d=T,A=v+1-(x-n)/(a-n),D.push(l,c,d*S+y,A*X+N)),p!==b&&(d=T+1,A=v+(x-i)/(m-i),D.push(l,c,d*S+y,A*X+N)),u!==b&&(d=T+(x-n)/(m-n),A=v+1,D.push(l,c,d*S+y,A*X+N)))}}return{minX:e.minX,maxX:e.maxX,minY:e.minY,maxY:e.maxY,segments:R}}function q(e,f){let t=H(e);f.noContour||(e.contourLines=J(t,f),delete t.z),e.minMax=t}var I=require("gyromagnetic-ratio");function z(e,f){for(let t of e){let o=0,a=0;for(let i of t.spectra){if(t.ntuples?.symbol?(!o&&i.observeFrequency&&(o=i.observeFrequency),!a&&i.shiftOffsetVal&&(a=i.shiftOffsetVal)):(o=i.observeFrequency,a=i.shiftOffsetVal),o&&i.xUnits?.toUpperCase().includes("HZ")&&(i.xUnits="PPM",i.xFactor=i.xFactor!==void 0?i.xFactor/o:void 0,i.firstX=i.firstX!==void 0?i.firstX/o:void 0,i.lastX=i.lastX!==void 0?i.lastX/o:void 0,i.deltaX=i.deltaX!==void 0?i.deltaX/o:void 0,i.data))for(let n=0;n<i.data.x.length;n++)i.data.x[n]/=o;if(a&&i.xUnits.toLowerCase().includes("ppm")&&i.firstX!==void 0&&i.lastX!==void 0){let n=i.firstX-a;if(i.firstX=i.firstX-n,i.lastX=i.lastX-n,i.data)for(let m=0;m<i.data.x.length;m++)i.data.x[m]-=n}if(t.ntuples?.nucleus&&t.ntuples.symbol)for(let n=0;n<t.ntuples.nucleus.length;n++){let m=t.ntuples.symbol[n],r=t.ntuples.nucleus[n];if(m.match(/^[F|T]/)&&!r){if(m.match(/[F|T]1/))if(t.tmp.$NUC2&&typeof t.tmp.$NUC2=="string")t.ntuples.nucleus[n]=t.tmp.$NUC2;else{let p=t.ntuples.symbol.indexOf(m.replace(/^([F|T]).*/,"$12"));p&&t.ntuples.nucleus[p]&&(t.ntuples.nucleus[n]=t.ntuples.nucleus[p])}m.match(/[F|T]2/)&&typeof t.tmp.$NUC1=="string"&&(t.ntuples.nucleus[n]=t.tmp.$NUC1)}m.match(/[F|T]2/)&&(t.yType=t.ntuples.nucleus[0],t.xType&&!(0,I.getGyromagneticRatio)(t.xType)&&(t.xType=t.ntuples.nucleus[1]))}if(o&&t.ntuples?.symbol&&t.ntuples.nucleus){let n="",m=t.ntuples.symbol.indexOf(i.pageSymbol);t.ntuples.units?.[m]&&(n=t.ntuples.units[m]);let{nucleus:r}=t.ntuples;if(!n.toLowerCase().match(/(ppm|seconds)/)&&r.length>1){if(m!==0){let h="Not sure about this ntuples format";if(f){f.warn(h);continue}else throw new Error(h)}let{nucleus:p}=t.ntuples,u=(0,I.getGyromagneticRatio)(p[0]),b=(0,I.getGyromagneticRatio)(p[1]);if(!u||!b){let h=`Problem with determination of gyromagnetic ratio for ${p.join("-")}`;if(f)f.error(h);else throw new Error(h)}else{let h=u/b*o;i.pageValue/=h}}}}}}function K(e){let f=e.spectra[0].data;e.chromatogram={times:f.x.slice(),series:{intensity:{dimension:1,data:f.y.slice()}}}}function _(e,f,t){z(e,t.logger),Me(e,t);for(let o of e){if(Object.keys(o.ntuples).length>0){let a=[],i=Object.keys(o.ntuples);for(let n of i){let m=o.ntuples[n];if(m)for(let r=0;r<m.length;r++)a[r]||(a[r]={}),a[r][n]=m[r]}o.ntuples=a}o.twoD&&t.wantXY&&(q(o,t),t.logger?.trace({profiling:!0},"Finished countour plot calculation"),t.keepSpectra||delete o.spectra),t.chromatogram&&(o.spectra.length>1?le(o):K(o),t.logger?.trace({profiling:!0},"Finished chromatogram calculation")),delete o.tmp}}function Me(e,f){for(let t of e)for(let o in t.meta){let a=t.meta[o];if(typeof a=="string"){if(a.startsWith("{")){if(!a.includes(":")&&a.endsWith("}")){let i=a.slice(1,-1).split(/[,; ]+/).filter(Boolean);for(let n=0;n<i.length;n++)t.meta[o+String(n)]=f.dynamicTyping?(0,Q.parseString)(i[n]):i[n]}}else if(a.startsWith("(")){let i=a.split(/\r?\n/),n=/^\((?<from>\d+)\.\.(?<to>\d+)\).*$/;if(n.test(i[0])){let[m,r]=i[0].match(n)?.slice(1).map(Number)??[],p=i.slice(1).join(" ").split(/[,; ]+/).filter(Boolean);for(let u=m;u<=r;u++)f.dynamicTyping&&typeof p[u-m]=="string"?t.meta[o+String(u)]=(0,Q.parseString)(p[u-m]):t.meta[o+String(u)]=p[u-m]}}}}}function ee(e,f,t){let o=-1,a=-1,i="",n="";if(t.indexOf("++")>0)i=t.replace(/.*\(([a-zA-Z0-9]+)\+\+.*/,"$1"),n=t.replace(/.*\.\.([a-zA-Z0-9]+).*/,"$1");else{t=t.replaceAll(/[^a-zA-Z%]/g,""),i=t.charAt(0),n=t.charAt(1),f.variables={};for(let m of t){let r=m.toLowerCase(),p=e.ntuples.symbol?.indexOf(m)||0;if(p===-1)throw new Error(`Symbol undefined: ${m}`);f.variables[r]={};for(let u in e.ntuples)e.ntuples[u]?.[p]&&(f.variables[r][u.replace(/^var/,"")]=e.ntuples[u]?.[p])}}o=e.ntuples.symbol?.indexOf(i)??-1,a=e.ntuples.symbol?.indexOf(n)??-1,o===-1&&(o=0),a===-1&&(a=0),e.ntuples.first&&(e.ntuples.first.length>o&&(f.firstX=e.ntuples.first[o]),e.ntuples.first.length>a&&(f.firstY=e.ntuples.first[a])),e.ntuples.last&&(e.ntuples.last.length>o&&(f.lastX=e.ntuples.last[o]),e.ntuples.last.length>a&&(f.lastY=e.ntuples.last[a])),e.ntuples.vardim&&e.ntuples.vardim.length>o&&(f.nbPoints=e.ntuples.vardim[o]),e.ntuples.factor&&(e.ntuples.factor.length>o&&(f.xFactor=e.ntuples.factor[o]),e.ntuples.factor.length>a&&(f.yFactor=e.ntuples.factor[a])),e.ntuples.units&&(e.ntuples.units.length>o&&(e.ntuples.varname?.[o]?f.xUnits=`${e.ntuples.varname[o]} [${e.ntuples.units[o]}]`:f.xUnits=e.ntuples.units[o]),e.ntuples.units.length>a&&(e.ntuples.varname?.[a]?f.yUnits=`${e.ntuples.varname[a]} [${e.ntuples.units[a]}]`:f.yUnits=e.ntuples.units[a]))}function U(e){e.xFactor||(e.xFactor=1),e.yFactor||(e.yFactor=1)}var L=/[ \t]*,[ \t]*/,Oe={removeComments:!1,keepRecordsRegExp:/^$/,canonicDataLabels:!0,canonicMetadataLabels:!1,dynamicTyping:!0,withoutXY:!1,noTrimRegExp:/^$/,chromatogram:!1,keepSpectra:!1,noContour:!1,nbContourLevels:7,noiseMultiplier:5};function de(e,f={}){e=(0,pe.ensureString)(e);let t={...Oe,...f};t.logger?.debug("Starting jcamp conversion"),t.wantXY=!t.withoutXY,t.start=Date.now();let o=[],a={entries:[],flatten:[]},i={children:[],spectra:[],ntuples:{},info:{},meta:{},tmp:{}},n=i,m=[],r={data:{}};t.logger?.trace({profiling:!0},"Before split to LDRS");let p=e.replaceAll(/[\r\n]+##/g,`
|
|
5
|
+
`;let s=h.match(/^##(.*?)=(.+)/);if(s){let l=s[1].replaceAll(/[ _-]/g,"").toUpperCase();l==="DATATYPE"?m.dataType=s[2].trim():l==="DATACLASS"?m.dataClass=s[2].trim():l==="JCAMPDX"?m.jcampDX=O(s[2].trim()):l==="JCAMPCS"&&(m.jcampCS=O(s[2].trim()))}}h.startsWith("##END")&&r>0&&r--}if(t){for(let u of a)u.children=void 0;return a}else return n}var ue=require("dynamic-typing"),ce=require("ensure-string");var ie=["TIC",".RIC","SCANNUMBER"];function ae(e){let f=e.spectra,t=f.length,o={times:new Array(t),series:{ms:{dimension:2,data:new Array(t)}}},a=[];for(let i of ie){let n=B(i);f[0][n]&&(a.push(n),o.series[n]={dimension:1,data:new Array(t)})}for(let i=0;i<t;i++){let n=f[i];o.times[i]=n.pageValue;for(let m of a)o.series[m].data[i]=Number(n[m]);n.data&&(o.series.ms.data[i]=[n.data.x,n.data.y])}e.chromatogram=o}function le(e){return ie.includes(e)}function B(e){return e.toLowerCase().replaceAll(/[^a-z0-9]/g,"")}function E(e){return e.map(Number)}function j(e,f,t){let{logger:o}=t;o&&(e.xFactor||o.info("The xFactor is not defined, it will be set to 1"),e.yFactor||o.info("The yFactor is not defined, it will be set to 1"));let a=e.yFactor??1,i=e.xFactor??1,n=e.deltaX??1,m=Math.abs(n*.1),r=.01;e.isXYdata=!0;let p={x:[],y:[]};e.data=p;let u=e.firstX||0,b=e.firstY||0,h=!1,s,l=0;for(;l<f.length;l++)if(s=f.codePointAt(l),s===13||s===10)h=!0;else if(h)break;let c=0,d=!0,A=!1,y=!1,k=0,N=!1,S=!1,g=0,X=0,M=!1,C=!1,Y=!1,R=0;for(;l<=f.length;l++)if(l===f.length?s=13:s=f.codePointAt(l),S)(s===13||s===10)&&(d=!0,S=!1,c++);else if(s<=57&&s>=48)C=!0,R>0?g+=(s-48)/10**R++:(g*=10,g+=s-48);else if(s===44||s===46)C=!0,R++;else{if(C){if(M&&(g=g*-1,M=!1),d)g*=i,o&&(y?Math.abs(u-n-g)>m&&(Math.abs(u-g)<m?o.trace(`Data line ${c}: After a DIFFERENCE the next line should repeat the last value and this does not seem to be the case: Current value: ${g} - Expected: ${u}.`):Math.abs(u-n-g)>10*m?o.trace(`Data line ${c}: The difference between the first value ${g} and the expected first x value ${u} based on increment after a DIFFERENCE is too high`):o.trace(`Data line ${c}: The difference between the first value ${g} and the expected first x value ${u} based on increment after a DIFFERENCE is too high`)):Math.abs(u-g)>m&&o.trace(`Data line ${c}: The difference between the first value ${g} and the first x value ${u} is too high`)),d=!1,y&&(Y=!0);else if(Y)Y=!1,o&&Math.abs(b-g)>r&&o.trace(`Data line ${c}: After a duplicate the next line should repeat the same value ${g} !== ${b}`),b=g;else{A?(k=M?0-g:g,y=!0,A=!1):N||(X=M?0-g:g);let x=N?g-1:1;for(let F=0;F<x;F++)y?b+=k:b=X,p.x.push(u),p.y.push(b*a),u+=n}M=!1,g=0,R=0,C=!1,N=!1}if(s<74&&s>63)C=!0,y=!1,g=s-64;else if(s>96&&s<106)C=!0,y=!1,g=s-96,M=!0;else if(s===115)C=!0,N=!0,g=9;else if(s>82&&s<91)C=!0,N=!0,g=s-82;else if(s>73&&s<83)C=!0,A=!0,g=s-73;else if(s>105&&s<115)C=!0,A=!0,g=s-105,M=!0;else if(s===36&&f.codePointAt(l+1)===36)C=!0,S=!0;else if(s===37)C=!0,A=!0,g=0,M=!1;else if(s===45){let x=f.codePointAt(l+1);(x!==void 0&&x>=48&&x<=57||x===44||x===46)&&(C=!0,d||(y=!1),M=!0)}else(s===13||s===10)&&(d=!0,S=!1,c++)}o&&y&&o.warn("The last value is a difference, it should be repeated on the next line")}var fe=/[,\t ]+/;function w(e,f,t){if(e.isPeaktable=!0,!e.variables||Object.keys(e.variables).length===2?Se(e,f,t):Te(e,f,t),e.variables)for(let o in e.variables)e.variables[o].data=e.data?.[o]}function Se(e,f,t){let{logger:o}=t,a={x:[],y:[]};e.data=a;let i=f.split(/,? *,?[;\r\n]+ */);for(let n=1;n<i.length;n++){let m=i[n].trim().replace(P,"").split(fe);if(m.length%2===0)for(let r=0;r<m.length;r=r+2)e.xFactor!==void 0&&e.yFactor!==void 0&&(a.x.push(Number(m[r])*e.xFactor),a.y.push(Number(m[r+1])*e.yFactor));else o?.warn(`Format error: ${m.toString()}`)}}function Te(e,f,t){let{logger:o}=t,a={},i=Object.keys(e.variables),n=i.length;for(let r of i)a[r]=[];e.data=a;let m=f.split(/,? *,?[;\r\n]+ */);for(let r=1;r<m.length;r++){let p=m[r].trim().replace(P,"").split(fe);if(p.length%n===0)for(let u=0;u<p.length;u++)a[i[u%n]].push(Number(p[u]));else o?.warn(`Wrong number of columns: ${p.toString()}`)}}function Z(e,f){e.isXYAdata=!0;let t={};e.data=t;let o=f.split(/\r?\n/),a=o[0].replace(/^.*?([A-Z]+).*$/,"$1").split("").map(i=>i.toLowerCase());for(let i=1;i<o.length;i++){let n=o[i].replace(/^\((.*)\)$/,"$1").split(/ *, */);for(let m=0;m<a.length;m++){let r=n[m];switch(a[m]){case"x":case"y":case"w":r=Number.parseFloat(r);break;case"a":r=r.replace(/^<(.*)>$/,"$1");break;case"m":break;default:continue}t[a[m]]||(t[a[m]]=[]),t[a[m]].push(r)}}}var Q=require("dynamic-typing");var G=require("gyromagnetic-ratio"),W=Ae(require("ml-array-median"));function me(e){return Array.isArray(e)?e[0]:e}function H(e){let{spectra:f}=e,t=f[0].data.y[0],o=t,a=f.length,i=f[0].data.x.length||0,n=new Array(a);for(let s=0;s<a;s++){n[s]=f[s].data.y;for(let l=0;l<i;l++){let c=n[s][l];c<t&&(t=c),c>o&&(o=c)}}let m=f[0].data.x[0],r=f[0].data.x.at(-1),{firstY:p,lastY:u}=ve(e);if(m>r)for(let s of n)s.reverse();p>u&&n.reverse();let b=[];for(let s of n){let l=Float64Array.from(s);for(let c=0;c<l.length;c++)l[c]<0&&(l[c]=-l[c]);b.push((0,W.default)(l))}let h=(0,W.default)(b);return{z:n,minX:Math.min(m,r),maxX:Math.max(m,r),minY:Math.min(p,u),maxY:Math.max(p,u),minZ:t,maxZ:o,noise:h}}function ve(e){let{spectra:f,ntuples:t,info:o}=e;if(t)for(let a of t){let{symbol:i,nucleus:n,units:m}=a;if(i.match(/[F|T]1/)&&m?.toUpperCase().match("HZ")){let r=me(o[".OBSERVEFREQUENCY"]),{nucleus:p}=t.find(c=>c.symbol.match(/[F|T]2/));if([r,p,n].some(c=>!c))break;let u=(0,G.getGyromagneticRatio)(p),b=(0,G.getGyromagneticRatio)(n),{first:h,last:s}=a,l=b!==null&&u!==null?r*b/u:r;return{firstY:h/l,lastY:s/l}}}return{firstY:f[0].pageValue,lastY:f.at(-1).pageValue}}function J(e,f){let t=e.noise,o=e.z,a,i,n,m,r,p,u,b,h=o.length,s=o[0].length,l,c,d,A,y=e.minX,N=(e.maxX-y)/(s-1),S=e.minY,X=(e.maxY-S)/(h-1),M=e.minZ,C=e.maxZ,Y=f.nbContourLevels*2,R=new Array(Y),x;for(let F=0;F<Y;F++){let V={};R[F]=V;let pe=F%2,te=(C-f.noiseMultiplier*t)*Math.exp((F>>1)-f.nbContourLevels);pe===0?x=te+f.noiseMultiplier*t:x=0-te-f.noiseMultiplier*t;let D=[];if(V.zValue=x,V.lines=D,!(x<=M||x>=C))for(let v=0;v<h-1;v++){let ne=o[v],re=o[v+1];for(let T=0;T<s-1;T++)a=ne[T],i=ne[T+1],n=re[T],m=re[T+1],r=a>x,p=i>x,u=n>x,b=m>x,r!==p&&r!==u&&(l=T+(x-a)/(i-a),c=v,d=T,A=v+(x-a)/(n-a),D.push(l*N+y,c*X+S,d*N+y,A*X+S)),b!==p&&b!==u&&(l=T+1,c=v+1-(x-m)/(i-m),d=T+1-(x-m)/(n-m),A=v+1,D.push(l*N+y,c*X+S,d*N+y,A*X+S)),p!==u&&(l=(T+1-(x-i)/(n-i))*N+y,c=(v+(x-i)/(n-i))*X+S,p!==r&&(d=T+1-(x-i)/(a-i),A=v,D.push(l,c,d*N+y,A*X+S)),u!==r&&(d=T,A=v+1-(x-n)/(a-n),D.push(l,c,d*N+y,A*X+S)),p!==b&&(d=T+1,A=v+(x-i)/(m-i),D.push(l,c,d*N+y,A*X+S)),u!==b&&(d=T+(x-n)/(m-n),A=v+1,D.push(l,c,d*N+y,A*X+S)))}}return{minX:e.minX,maxX:e.maxX,minY:e.minY,maxY:e.maxY,segments:R}}function q(e,f){let t=H(e);f.noContour||(e.contourLines=J(t,f),delete t.z),e.minMax=t}var I=require("gyromagnetic-ratio");function z(e,f){for(let t of e){let o=0,a=0;for(let i of t.spectra){if(t.ntuples?.symbol?(!o&&i.observeFrequency&&(o=i.observeFrequency),!a&&i.shiftOffsetVal&&(a=i.shiftOffsetVal)):(o=i.observeFrequency,a=i.shiftOffsetVal),o&&i.xUnits?.toUpperCase().includes("HZ")&&(i.xUnits="PPM",i.xFactor=i.xFactor!==void 0?i.xFactor/o:void 0,i.firstX=i.firstX!==void 0?i.firstX/o:void 0,i.lastX=i.lastX!==void 0?i.lastX/o:void 0,i.deltaX=i.deltaX!==void 0?i.deltaX/o:void 0,i.data))for(let n=0;n<i.data.x.length;n++)i.data.x[n]/=o;if(a&&i.xUnits.toLowerCase().includes("ppm")&&i.firstX!==void 0&&i.lastX!==void 0){let n=i.firstX-a;if(i.firstX=i.firstX-n,i.lastX=i.lastX-n,i.data)for(let m=0;m<i.data.x.length;m++)i.data.x[m]-=n}if(t.ntuples?.nucleus&&t.ntuples.symbol)for(let n=0;n<t.ntuples.nucleus.length;n++){let m=t.ntuples.symbol[n],r=t.ntuples.nucleus[n];if(m.match(/^[F|T]/)&&!r){if(m.match(/[F|T]1/))if(t.tmp.$NUC2&&typeof t.tmp.$NUC2=="string")t.ntuples.nucleus[n]=t.tmp.$NUC2;else{let p=t.ntuples.symbol.indexOf(m.replace(/^([F|T]).*/,"$12"));p&&t.ntuples.nucleus[p]&&(t.ntuples.nucleus[n]=t.ntuples.nucleus[p])}m.match(/[F|T]2/)&&typeof t.tmp.$NUC1=="string"&&(t.ntuples.nucleus[n]=t.tmp.$NUC1)}m.match(/[F|T]2/)&&(t.yType=t.ntuples.nucleus[0],t.xType&&!(0,I.getGyromagneticRatio)(t.xType)&&(t.xType=t.ntuples.nucleus[1]))}if(o&&t.ntuples?.symbol&&t.ntuples.nucleus){let n="",m=t.ntuples.symbol.indexOf(i.pageSymbol);t.ntuples.units?.[m]&&(n=t.ntuples.units[m]);let{nucleus:r}=t.ntuples;if(!n.toLowerCase().match(/(ppm|seconds)/)&&r.length>1){if(m!==0){let h="Not sure about this ntuples format";if(f){f.warn(h);continue}else throw new Error(h)}let{nucleus:p}=t.ntuples,u=(0,I.getGyromagneticRatio)(p[0]),b=(0,I.getGyromagneticRatio)(p[1]);if(!u||!b){let h=`Problem with determination of gyromagnetic ratio for ${p.join("-")}`;if(f)f.error(h);else throw new Error(h)}else{let h=u/b*o;i.pageValue/=h}}}}}}function K(e){let f=e.spectra[0].data;e.chromatogram={times:f.x.slice(),series:{intensity:{dimension:1,data:f.y.slice()}}}}function _(e,f,t){z(e,t.logger),Xe(e,t);for(let o of e){if(Object.keys(o.ntuples).length>0){let a=[],i=Object.keys(o.ntuples);for(let n of i){let m=o.ntuples[n];if(m)for(let r=0;r<m.length;r++)a[r]||(a[r]={}),a[r][n]=m[r]}o.ntuples=a}o.twoD&&t.wantXY&&(q(o,t),t.logger?.trace({profiling:!0},"Finished countour plot calculation"),t.keepSpectra||delete o.spectra),t.chromatogram&&(o.spectra.length>1?ae(o):K(o),t.logger?.trace({profiling:!0},"Finished chromatogram calculation")),delete o.tmp}}function Xe(e,f){for(let t of e)for(let o in t.meta){let a=t.meta[o];if(typeof a=="string"){if(a.startsWith("{")){if(!a.includes(":")&&a.endsWith("}")){let i=a.slice(1,-1).split(/[,; ]+/).filter(Boolean);for(let n=0;n<i.length;n++)t.meta[o+String(n)]=f.dynamicTyping?(0,Q.parseString)(i[n]):i[n]}}else if(a.startsWith("(")){let i=a.split(/\r?\n/),n=/^\((?<from>\d+)\.\.(?<to>\d+)\).*$/;if(n.test(i[0])){let[m,r]=i[0].match(n)?.slice(1).map(Number)??[],p=i.slice(1).join(" ").split(/[,; ]+/).filter(Boolean);for(let u=m;u<=r;u++)f.dynamicTyping&&typeof p[u-m]=="string"?t.meta[o+String(u)]=(0,Q.parseString)(p[u-m]):t.meta[o+String(u)]=p[u-m]}}}}}function ee(e,f,t){let o=-1,a=-1,i="",n="";if(t.indexOf("++")>0)i=t.replace(/.*\(([a-zA-Z0-9]+)\+\+.*/,"$1"),n=t.replace(/.*\.\.([a-zA-Z0-9]+).*/,"$1");else{t=t.replaceAll(/[^a-zA-Z%]/g,""),i=t.charAt(0),n=t.charAt(1),f.variables={};for(let m of t){let r=m.toLowerCase(),p=e.ntuples.symbol?.indexOf(m)||0;if(p===-1)throw new Error(`Symbol undefined: ${m}`);f.variables[r]={};for(let u in e.ntuples)e.ntuples[u]?.[p]&&(f.variables[r][u.replace(/^var/,"")]=e.ntuples[u]?.[p])}}o=e.ntuples.symbol?.indexOf(i)??-1,a=e.ntuples.symbol?.indexOf(n)??-1,o===-1&&(o=0),a===-1&&(a=0),e.ntuples.first&&(e.ntuples.first.length>o&&(f.firstX=e.ntuples.first[o]),e.ntuples.first.length>a&&(f.firstY=e.ntuples.first[a])),e.ntuples.last&&(e.ntuples.last.length>o&&(f.lastX=e.ntuples.last[o]),e.ntuples.last.length>a&&(f.lastY=e.ntuples.last[a])),e.ntuples.vardim&&e.ntuples.vardim.length>o&&(f.nbPoints=e.ntuples.vardim[o]),e.ntuples.factor&&(e.ntuples.factor.length>o&&(f.xFactor=e.ntuples.factor[o]),e.ntuples.factor.length>a&&(f.yFactor=e.ntuples.factor[a])),e.ntuples.units&&(e.ntuples.units.length>o&&(e.ntuples.varname?.[o]?f.xUnits=`${e.ntuples.varname[o]} [${e.ntuples.units[o]}]`:f.xUnits=e.ntuples.units[o]),e.ntuples.units.length>a&&(e.ntuples.varname?.[a]?f.yUnits=`${e.ntuples.varname[a]} [${e.ntuples.units[a]}]`:f.yUnits=e.ntuples.units[a]))}function U(e){e.xFactor||(e.xFactor=1),e.yFactor||(e.yFactor=1)}var L=/[ \t]*,[ \t]*/,Le={removeComments:!1,keepRecordsRegExp:/^$/,canonicDataLabels:!0,canonicMetadataLabels:!1,dynamicTyping:!0,withoutXY:!1,noTrimRegExp:/^$/,chromatogram:!1,keepSpectra:!1,noContour:!1,nbContourLevels:7,noiseMultiplier:5};function Me(e,f={}){e=(0,ce.ensureString)(e);let t={...Le,...f};t.logger?.debug("Starting jcamp conversion"),t.wantXY=!t.withoutXY,t.start=Date.now();let o=[],a={entries:[],flatten:[]},i={children:[],spectra:[],ntuples:{},info:{},meta:{},tmp:{}},n=i,m=[],r={data:{}};t.logger?.trace({profiling:!0},"Before split to LDRS");let p=e.replaceAll(/[\r\n]+##/g,`
|
|
6
6
|
##`).split(`
|
|
7
7
|
##`);t.logger?.trace({profiling:!0},"Split to LDRS"),p[0]&&(p[0]=p[0].replace(/^[\r\n ]*##/,""));for(let u of p){let b=u.indexOf("="),h=b>0?u.slice(0,Math.max(0,b)):u,s=b>0?h.match(t.noTrimRegExp)?u.slice(Math.max(0,b+1)):u.slice(Math.max(0,b+1)).trim():"",l=h.replaceAll(/[_ -]/g,"").toUpperCase();if(l==="DATATABLE"){let c=s.indexOf(`
|
|
8
8
|
`);if(c===-1&&(c=s.indexOf("\r")),c>0){let d=s.slice(0,Math.max(0,c)).split(/[ ,;\t]+/);ee(n,r,d[0]),r.datatable=d[0],d[1]?.includes("PEAKS")?l="PEAKTABLE":d[1]&&(d[1].indexOf("XYDATA")||d[0].indexOf("++")>0)&&(l="XYDATA",r.nbPoints&&r.lastX!==void 0&&r.firstX!==void 0&&(r.deltaX=(r.lastX-r.firstX)/(r.nbPoints-1)))}}if(l==="XYDATA"){t.wantXY&&(U(r),s.match(/.*\+\+.*/)?(r.nbPoints&&r.lastX!==void 0&&r.firstX!==void 0&&(r.deltaX=(r.lastX-r.firstX)/(r.nbPoints-1)),j(r,s,t)):w(r,s,t),n?.spectra.push(r),r={data:{}});continue}else if(l==="PEAKTABLE"){t.wantXY&&(U(r),w(r,s,t),n?.spectra.push(r),r={data:{}});continue}if(l==="PEAKASSIGNMENTS"){t.wantXY&&(s.match(/.*[^A-Z]*.*/)&&Z(r,s),n?.spectra.push(r),r={data:{}});continue}if(t.removeComments&&(s=s.split(/\r?\n/).map(c=>c.replace(/ *\$\$.*$/,"")).join(`
|
|
9
|
-
`)),l==="TITLE"){let c=n;c.children||(c.children=[]),n={spectra:[],ntuples:{},info:{},meta:{},tmp:{}},c.children.push(n),m.push(c),o.push(n),n.title=s}else l==="DATATYPE"?(n.dataType=s,s.match(/^nd|\snd\s/i)&&(n.twoD=!0)):l==="NTUPLES"?s.match(/^nd|\snd\s/i)&&(n.twoD=!0):l==="DATACLASS"?n.dataClass=s:l==="JCAMPDX"?n.jcampDX=O(s):l==="JCAMPCS"?n.jcampCS=O(s):l==="XUNITS"?r.xUnits=s:l==="YUNITS"?r.yUnits=s:l==="FIRSTX"?r.firstX=Number(s):l==="LASTX"?r.lastX=Number(s):l==="FIRSTY"?r.firstY=Number(s):l==="LASTY"?r.lastY=Number(s):l==="NPOINTS"?r.nbPoints=Number(s):l==="XFACTOR"?r.xFactor=Number(s):l==="YFACTOR"?r.yFactor=Number(s):l==="MAXX"?r.maxX=Number(s):l==="MINX"?r.minX=Number(s):l==="MAXY"?r.maxY=Number(s):l==="MINY"?r.minY=Number(s):l==="DELTAX"?r.deltaX=Number(s):l===".OBSERVEFREQUENCY"||l==="$SFO1"?r.observeFrequency||(r.observeFrequency=Number(s)):l===".OBSERVENUCLEUS"?r.xType||(n.xType=s.replaceAll(/[^a-zA-Z0-9]/g,"")):l==="$OFFSET"?(n.shiftOffsetNum=0,r.shiftOffsetVal||(r.shiftOffsetVal=Number(s))):l==="$REFERENCEPOINT"||(l==="VARNAME"?n.ntuples.varname=s.split(L):l==="SYMBOL"?n.ntuples.symbol=s.split(L):l==="VARTYPE"?n.ntuples.vartype=s.split(L):l==="VARFORM"?n.ntuples.varform=s.split(L):l==="VARDIM"?n.ntuples.vardim=E(s.split(L)):l==="UNITS"?n.ntuples.units=s.split(L):l==="FACTOR"?n.ntuples.factor=E(s.split(L)):l==="FIRST"?n.ntuples.first=E(s.split(L)):l==="LAST"?n.ntuples.last=E(s.split(L)):l==="MIN"?n.ntuples.min=E(s.split(L)):l==="MAX"?n.ntuples.max=E(s.split(L)):l===".NUCLEUS"?n.ntuples&&(n.ntuples.nucleus=s.split(L).map(c=>c.replaceAll(/[^a-zA-Z0-9]/g,""))):l==="PAGE"?(r.page=s.trim(),r.pageValue=Number(s.replace(/^.*=/,"")),r.pageSymbol=r.page.replace(/[=].*/,"")):l==="RETENTIONTIME"?r.pageValue=Number(s):
|
|
9
|
+
`)),l==="TITLE"){let c=n;c.children||(c.children=[]),n={spectra:[],ntuples:{},info:{},meta:{},tmp:{}},c.children.push(n),m.push(c),o.push(n),n.title=s}else l==="DATATYPE"?(n.dataType=s,s.match(/^nd|\snd\s/i)&&(n.twoD=!0)):l==="NTUPLES"?s.match(/^nd|\snd\s/i)&&(n.twoD=!0):l==="DATACLASS"?n.dataClass=s:l==="JCAMPDX"?n.jcampDX=O(s):l==="JCAMPCS"?n.jcampCS=O(s):l==="XUNITS"?r.xUnits=s:l==="YUNITS"?r.yUnits=s:l==="FIRSTX"?r.firstX=Number(s):l==="LASTX"?r.lastX=Number(s):l==="FIRSTY"?r.firstY=Number(s):l==="LASTY"?r.lastY=Number(s):l==="NPOINTS"?r.nbPoints=Number(s):l==="XFACTOR"?r.xFactor=Number(s):l==="YFACTOR"?r.yFactor=Number(s):l==="MAXX"?r.maxX=Number(s):l==="MINX"?r.minX=Number(s):l==="MAXY"?r.maxY=Number(s):l==="MINY"?r.minY=Number(s):l==="DELTAX"?r.deltaX=Number(s):l===".OBSERVEFREQUENCY"||l==="$SFO1"?r.observeFrequency||(r.observeFrequency=Number(s)):l===".OBSERVENUCLEUS"?r.xType||(n.xType=s.replaceAll(/[^a-zA-Z0-9]/g,"")):l==="$OFFSET"?(n.shiftOffsetNum=0,r.shiftOffsetVal||(r.shiftOffsetVal=Number(s))):l==="$REFERENCEPOINT"||(l==="VARNAME"?n.ntuples.varname=s.split(L):l==="SYMBOL"?n.ntuples.symbol=s.split(L):l==="VARTYPE"?n.ntuples.vartype=s.split(L):l==="VARFORM"?n.ntuples.varform=s.split(L):l==="VARDIM"?n.ntuples.vardim=E(s.split(L)):l==="UNITS"?n.ntuples.units=s.split(L):l==="FACTOR"?n.ntuples.factor=E(s.split(L)):l==="FIRST"?n.ntuples.first=E(s.split(L)):l==="LAST"?n.ntuples.last=E(s.split(L)):l==="MIN"?n.ntuples.min=E(s.split(L)):l==="MAX"?n.ntuples.max=E(s.split(L)):l===".NUCLEUS"?n.ntuples&&(n.ntuples.nucleus=s.split(L).map(c=>c.replaceAll(/[^a-zA-Z0-9]/g,""))):l==="PAGE"?(r.page=s.trim(),r.pageValue=Number(s.replace(/^.*=/,"")),r.pageSymbol=r.page.replace(/[=].*/,"")):l==="RETENTIONTIME"?r.pageValue=Number(s):le(l)?r[B(l)]=s:l==="SAMPLEDESCRIPTION"?r.sampleDescription=s:l.startsWith("$NUC")?!n.tmp[l]&&!s.includes("off")&&(n.tmp[l]=s.replaceAll(/[<>]/g,"")):l==="END"&&(n=m.pop()));if(n?.info&&n.meta&&l.match(t.keepRecordsRegExp)){let c,d;h.startsWith("$")?(d=t.canonicMetadataLabels?l.slice(1):h.slice(1),c=n.meta):(d=t.canonicDataLabels?l:h,c=n.info),t.dynamicTyping&&(s=(0,ue.parseString)(s)),c[d]?(Array.isArray(c[d])||(c[d]=[c[d]]),c[d].push(s)):c[d]=s}}if(t.logger?.trace({profiling:!0},"Finished parsing"),_(o,a,t),t.logger?.trace({profiling:!0},"Total time"),a.entries=i.children||[],a.flatten=o,t.logger){t.logger.debug("Finished jcamp conversion");for(let u of a.flatten)t.logger.debug(`${u.dataType} - ${u.title}`)}return a}function Oe(e,f){let{logger:t,OCL:o}=f;if(!e.info.ATOMLIST||!e.info.BONDLIST){t?.warn("No ATOMLIST or BONDLIST in the JCAMP-CS entry");return}Ye(e,t);let a=Ee(e.info.ATOMLIST),i=Re(e.info.BONDLIST),n=De(e.info.CHARGE,t),m=Fe(o,a,i,n,t);return{molecule:m,molfile:m.toMolfile()}}function Fe(e,f,t,o,a){let i={S:e.Molecule.cBondTypeSingle,D:e.Molecule.cBondTypeDouble,T:e.Molecule.cBondTypeTriple,Q:e.Molecule.cBondTypeQuadruple},n={},m=new e.Molecule(f.length,t.length);for(let r of f){let p=e.Molecule.getAtomicNoFromLabel(r.element);p||(a?.error(`Atomic number of ${r.element} could not be determined`),p=e.Molecule.getAtomicNoFromLabel("X")),n[r.atomNumber]=m.addAtom(p),m.setAtomMapNo(n[r.atomNumber],r.atomNumber,!1)}for(let r of t){if(n[r.atomNumber1]===void 0){a?.error(`A bond goes to atomNumber ${r.atomNumber1} that does not exists`);continue}if(n[r.atomNumber2]===void 0){a?.error(`A bond goes to atomNumber ${r.atomNumber2} that does not exists`);continue}let p=m.addBond(n[r.atomNumber1],n[r.atomNumber2]);m.setBondType(p,i[r.bondType])}for(let r of o){if(n[r.atomNumber]===void 0){a?.error(`A charge goes to atomNumber ${r.atomNumber} that does not exists`);continue}m.setAtomCharge(n[r.atomNumber],r.value)}return m}function Ee(e){let f=e.split(/\r?\n/).map(o=>O(o)).filter(Boolean),t=[];for(let o of f){let[a,i,n]=o.trim().split(/\s+/);t.push({atomNumber:Number(a),element:i,nbImplicitHydrogens:n?.length>0?Number(n):void 0})}return t}function Re(e){let f=e.split(/\r?\n/).map(o=>O(o)).filter(Boolean),t=[];for(let o of f){let[a,i,n]=o.trim().split(/\s+/);t.push({atomNumber1:Number(a),atomNumber2:Number(i),bondType:n})}return t}function De(e,f){if(!e)return[];let t=e.split(/\r?\n/).map(a=>O(a)).filter(Boolean),o=[];for(let a of t){let[i,n,m]=a.trim().split(/\s+/);m!==void 0&&f?.warn("Charge on multiple atom is not supported"),o.push({atomNumber:Number(n),value:Number(i)})}return o}function Ye(e,f){let t=["STEREOPAIR","STEREOCENTER","RADICAL","STEREOMOLECULE"];for(let o of t)e.info[o]&&f?.warn(`JCAMP-CS parser do not support: ${o}`)}
|
package/jcampconverter.d.ts
CHANGED
|
@@ -41,7 +41,7 @@ export declare function convert(jcamp: string | ArrayBuffer | Uint8Array, option
|
|
|
41
41
|
/**
|
|
42
42
|
* Conversion options.
|
|
43
43
|
*/
|
|
44
|
-
declare interface ConvertOptions extends Partial<Add2DOptions> {
|
|
44
|
+
export declare interface ConvertOptions extends Partial<Add2DOptions> {
|
|
45
45
|
/**
|
|
46
46
|
* If it is true, anywhere out of XYDATA, PEAKTABLE n PEAKASSIGNMENTS the comments are removed
|
|
47
47
|
* @default false
|
|
@@ -93,7 +93,7 @@ declare interface ConvertOptions extends Partial<Add2DOptions> {
|
|
|
93
93
|
fastParse?: boolean;
|
|
94
94
|
}
|
|
95
95
|
|
|
96
|
-
declare interface ConvertResult {
|
|
96
|
+
export declare interface ConvertResult {
|
|
97
97
|
entries: Entry[];
|
|
98
98
|
flatten: Entry[];
|
|
99
99
|
}
|
|
@@ -106,11 +106,11 @@ declare interface ConvertResult {
|
|
|
106
106
|
*/
|
|
107
107
|
export declare function createTree(jcamp: string | ArrayBuffer | Uint8Array, options?: CreateTreeOptions): Stack[];
|
|
108
108
|
|
|
109
|
-
declare interface CreateTreeOptions {
|
|
109
|
+
export declare interface CreateTreeOptions {
|
|
110
110
|
flatten?: boolean;
|
|
111
111
|
}
|
|
112
112
|
|
|
113
|
-
declare interface Entry {
|
|
113
|
+
export declare interface Entry {
|
|
114
114
|
spectra: Spectrum[];
|
|
115
115
|
ntuples: Ntuples;
|
|
116
116
|
meta: Record<string, string | string[] | number | number[] | boolean | boolean[] | undefined>;
|
|
@@ -145,7 +145,7 @@ declare interface GenerateContourLinesOptions {
|
|
|
145
145
|
noiseMultiplier: number;
|
|
146
146
|
}
|
|
147
147
|
|
|
148
|
-
declare interface MinMax {
|
|
148
|
+
export declare interface MinMax {
|
|
149
149
|
z: number[][];
|
|
150
150
|
minX: number;
|
|
151
151
|
maxX: number;
|
|
@@ -157,7 +157,7 @@ declare interface MinMax {
|
|
|
157
157
|
noice?: number;
|
|
158
158
|
}
|
|
159
159
|
|
|
160
|
-
declare interface Ntuples {
|
|
160
|
+
export declare interface Ntuples {
|
|
161
161
|
varname?: string[];
|
|
162
162
|
symbol?: string[];
|
|
163
163
|
vartype?: string[];
|
|
@@ -188,12 +188,12 @@ declare interface ParsedCS {
|
|
|
188
188
|
*/
|
|
189
189
|
export declare function parseJcampCS(entry: Entry, options: ParseJcampCSOptions): ParsedCS | undefined;
|
|
190
190
|
|
|
191
|
-
declare interface ParseJcampCSOptions {
|
|
191
|
+
export declare interface ParseJcampCSOptions {
|
|
192
192
|
OCL: OCL;
|
|
193
193
|
logger?: Logger;
|
|
194
194
|
}
|
|
195
195
|
|
|
196
|
-
declare interface Spectrum {
|
|
196
|
+
export declare interface Spectrum {
|
|
197
197
|
[key: string]: any;
|
|
198
198
|
data: Record<string, number[]>;
|
|
199
199
|
firstX?: number;
|
package/jcampconverter.mjs
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import{ensureString as fe}from"ensure-string";var $=/\s*\$\$.*/;function O(e){return e.replace($,"")}function
|
|
1
|
+
import{ensureString as fe}from"ensure-string";var $=/\s*\$\$.*/;function O(e){return e.replace($,"")}function ve(e,m={}){e=fe(e);let{flatten:n=!1}=m,o=e.split(/\r?\n/),f=[],i=[],t=[],l={title:"",jcamp:"",children:[]},r=0,p=e.includes("## ");for(let u=0;u<o.length;u++){let b=o[u],h=p?b.replaceAll(" ",""):b;if(h.startsWith("##NTUPLES")&&r++,h.startsWith("##TITLE")){let s=[h.slice(8).trim()];for(let a=u+1;a<o.length&&!o[a].startsWith("##");a++)s.push(o[a].trim());i.push({title:s.join(`
|
|
2
2
|
`),jcamp:`${b}
|
|
3
3
|
`,children:[]}),l=i.at(-1),f.push(l)}else if(h.startsWith("##END")&&r===0&&l){l.jcamp+=`${b}
|
|
4
4
|
`;let s=i.pop();if(!s)throw new Error("Not finished");i.length>0?(l=i.at(-1),l.children?.push(s)):(l=void 0,t.push(s))}else if(l?.jcamp){l.jcamp+=`${b}
|
|
5
|
-
`;let s=h.match(/^##(.*?)=(.+)/);if(s){let a=s[1].replaceAll(/[ _-]/g,"").toUpperCase();a==="DATATYPE"?l.dataType=s[2].trim():a==="DATACLASS"?l.dataClass=s[2].trim():a==="JCAMPDX"?l.jcampDX=O(s[2].trim()):a==="JCAMPCS"&&(l.jcampCS=O(s[2].trim()))}}h.startsWith("##END")&&r>0&&r--}if(n){for(let u of f)u.children=void 0;return f}else return t}import{parseString as be}from"dynamic-typing";import{ensureString as ge}from"ensure-string";var ee=["TIC",".RIC","SCANNUMBER"];function te(e){let m=e.spectra,n=m.length,o={times:new Array(n),series:{ms:{dimension:2,data:new Array(n)}}},f=[];for(let i of ee){let t=k(i);m[0][t]&&(f.push(t),o.series[t]={dimension:1,data:new Array(n)})}for(let i=0;i<n;i++){let t=m[i];o.times[i]=t.pageValue;for(let l of f)o.series[l].data[i]=Number(t[l]);t.data&&(o.series.ms.data[i]=[t.data.x,t.data.y])}e.chromatogram=o}function ne(e){return ee.includes(e)}function k(e){return e.toLowerCase().replaceAll(/[^a-z0-9]/g,"")}function E(e){return e.map(Number)}function V(e,m,n){let{logger:o}=n;o&&(e.xFactor||o.info("The xFactor is not defined, it will be set to 1"),e.yFactor||o.info("The yFactor is not defined, it will be set to 1"));let f=e.yFactor??1,i=e.xFactor??1,t=e.deltaX??1,l=Math.abs(t*.1),r=.01;e.isXYdata=!0;let p={x:[],y:[]};e.data=p;let u=e.firstX||0,b=e.firstY||0,h=!1,s,a=0;for(;a<m.length;a++)if(s=m.codePointAt(a),s===13||s===10)h=!0;else if(h)break;let c=0,d=!0,A=!1,y=!1,I=0,S=!1,N=!1,g=0,X=0,M=!1,C=!1,Y=!1,R=0;for(;a<=m.length;a++)if(a===m.length?s=13:s=m.codePointAt(a),N)(s===13||s===10)&&(d=!0,N=!1,c++);else if(s<=57&&s>=48)C=!0,R>0?g+=(s-48)/10**R++:(g*=10,g+=s-48);else if(s===44||s===46)C=!0,R++;else{if(C){if(M&&(g=g*-1,M=!1),d)g*=i,o&&(y?Math.abs(u-t-g)>l&&(Math.abs(u-g)<l?o.trace(`Data line ${c}: After a DIFFERENCE the next line should repeat the last value and this does not seem to be the case: Current value: ${g} - Expected: ${u}.`):Math.abs(u-t-g)>10*l?o.trace(`Data line ${c}: The difference between the first value ${g} and the expected first x value ${u} based on increment after a DIFFERENCE is too high`):o.trace(`Data line ${c}: The difference between the first value ${g} and the expected first x value ${u} based on increment after a DIFFERENCE is too high`)):Math.abs(u-g)>l&&o.trace(`Data line ${c}: The difference between the first value ${g} and the first x value ${u} is too high`)),d=!1,y&&(Y=!0);else if(Y)Y=!1,o&&Math.abs(b-g)>r&&o.trace(`Data line ${c}: After a duplicate the next line should repeat the same value ${g} !== ${b}`),b=g;else{A?(I=M?0-g:g,y=!0,A=!1):S||(X=M?0-g:g);let x=S?g-1:1;for(let F=0;F<x;F++)y?b+=I:b=X,p.x.push(u),p.y.push(b*f),u+=t}M=!1,g=0,R=0,C=!1,S=!1}if(s<74&&s>63)C=!0,y=!1,g=s-64;else if(s>96&&s<106)C=!0,y=!1,g=s-96,M=!0;else if(s===115)C=!0,S=!0,g=9;else if(s>82&&s<91)C=!0,S=!0,g=s-82;else if(s>73&&s<83)C=!0,A=!0,g=s-73;else if(s>105&&s<115)C=!0,A=!0,g=s-105,M=!0;else if(s===36&&m.codePointAt(a+1)===36)C=!0,N=!0;else if(s===37)C=!0,A=!0,g=0,M=!1;else if(s===45){let x=m.codePointAt(a+1);(x!==void 0&&x>=48&&x<=57||x===44||x===46)&&(C=!0,d||(y=!1),M=!0)}else(s===13||s===10)&&(d=!0,N=!1,c++)}o&&y&&o.warn("The last value is a difference, it should be repeated on the next line")}var re=/[,\t ]+/;function P(e,m,n){if(e.isPeaktable=!0,!e.variables||Object.keys(e.variables).length===2?ue(e,m,n):ce(e,m,n),e.variables)for(let o in e.variables)e.variables[o].data=e.data?.[o]}function ue(e,m,n){let{logger:o}=n,f={x:[],y:[]};e.data=f;let i=m.split(/,? *,?[;\r\n]+ */);for(let t=1;t<i.length;t++){let l=i[t].trim().replace($,"").split(re);if(l.length%2===0)for(let r=0;r<l.length;r=r+2)e.xFactor!==void 0&&e.yFactor!==void 0&&(f.x.push(Number(l[r])*e.xFactor),f.y.push(Number(l[r+1])*e.yFactor));else o?.warn(`Format error: ${l.toString()}`)}}function ce(e,m,n){let{logger:o}=n,f={},i=Object.keys(e.variables),t=i.length;for(let r of i)f[r]=[];e.data=f;let l=m.split(/,? *,?[;\r\n]+ */);for(let r=1;r<l.length;r++){let p=l[r].trim().replace($,"").split(re);if(p.length%t===0)for(let u=0;u<p.length;u++)f[i[u%t]].push(Number(p[u]));else o?.warn(`Wrong number of columns: ${p.toString()}`)}}function B(e,m){e.isXYAdata=!0;let n={};e.data=n;let o=m.split(/\r?\n/),f=o[0].replace(/^.*?([A-Z]+).*$/,"$1").split("").map(i=>i.toLowerCase());for(let i=1;i<o.length;i++){let t=o[i].replace(/^\((.*)\)$/,"$1").split(/ *, */);for(let l=0;l<f.length;l++){let r=t[l];switch(f[l]){case"x":case"y":case"w":r=Number.parseFloat(r);break;case"a":r=r.replace(/^<(.*)>$/,"$1");break;case"m":break;default:continue}n[f[l]]||(n[f[l]]=[]),n[f[l]].push(r)}}}import{parseString as ae}from"dynamic-typing";import{getGyromagneticRatio as oe}from"gyromagnetic-ratio";import ie from"ml-array-median";function se(e){return Array.isArray(e)?e[0]:e}function j(e){let{spectra:m}=e,n=m[0].data.y[0],o=n,f=m.length,i=m[0].data.x.length||0,t=new Array(f);for(let s=0;s<f;s++){t[s]=m[s].data.y;for(let a=0;a<i;a++){let c=t[s][a];c<n&&(n=c),c>o&&(o=c)}}let l=m[0].data.x[0],r=m[0].data.x.at(-1),{firstY:p,lastY:u}=pe(e);if(l>r)for(let s of t)s.reverse();p>u&&t.reverse();let b=[];for(let s of t){let a=Float64Array.from(s);for(let c=0;c<a.length;c++)a[c]<0&&(a[c]=-a[c]);b.push(ie(a))}let h=ie(b);return{z:t,minX:Math.min(l,r),maxX:Math.max(l,r),minY:Math.min(p,u),maxY:Math.max(p,u),minZ:n,maxZ:o,noise:h}}function pe(e){let{spectra:m,ntuples:n,info:o}=e;if(n)for(let f of n){let{symbol:i,nucleus:t,units:l}=f;if(i.match(/[F|T]1/)&&l?.toUpperCase().match("HZ")){let r=se(o[".OBSERVEFREQUENCY"]),{nucleus:p}=n.find(c=>c.symbol.match(/[F|T]2/));if([r,p,t].some(c=>!c))break;let u=oe(p),b=oe(t),{first:h,last:s}=f,a=b!==null&&u!==null?r*b/u:r;return{firstY:h/a,lastY:s/a}}}return{firstY:m[0].pageValue,lastY:m.at(-1).pageValue}}function Z(e,m){let n=e.noise,o=e.z,f,i,t,l,r,p,u,b,h=o.length,s=o[0].length,a,c,d,A,y=e.minX,S=(e.maxX-y)/(s-1),N=e.minY,X=(e.maxY-N)/(h-1),M=e.minZ,C=e.maxZ,Y=m.nbContourLevels*2,R=new Array(Y),x;for(let F=0;F<Y;F++){let U={};R[F]=U;let le=F%2,K=(C-m.noiseMultiplier*n)*Math.exp((F>>1)-m.nbContourLevels);le===0?x=K+m.noiseMultiplier*n:x=0-K-m.noiseMultiplier*n;let D=[];if(U.zValue=x,U.lines=D,!(x<=M||x>=C))for(let v=0;v<h-1;v++){let Q=o[v],_=o[v+1];for(let T=0;T<s-1;T++)f=Q[T],i=Q[T+1],t=_[T],l=_[T+1],r=f>x,p=i>x,u=t>x,b=l>x,r!==p&&r!==u&&(a=T+(x-f)/(i-f),c=v,d=T,A=v+(x-f)/(t-f),D.push(a*S+y,c*X+N,d*S+y,A*X+N)),b!==p&&b!==u&&(a=T+1,c=v+1-(x-l)/(i-l),d=T+1-(x-l)/(t-l),A=v+1,D.push(a*S+y,c*X+N,d*S+y,A*X+N)),p!==u&&(a=(T+1-(x-i)/(t-i))*S+y,c=(v+(x-i)/(t-i))*X+N,p!==r&&(d=T+1-(x-i)/(f-i),A=v,D.push(a,c,d*S+y,A*X+N)),u!==r&&(d=T,A=v+1-(x-t)/(f-t),D.push(a,c,d*S+y,A*X+N)),p!==b&&(d=T+1,A=v+(x-i)/(l-i),D.push(a,c,d*S+y,A*X+N)),u!==b&&(d=T+(x-t)/(l-t),A=v+1,D.push(a,c,d*S+y,A*X+N)))}}return{minX:e.minX,maxX:e.maxX,minY:e.minY,maxY:e.maxY,segments:R}}function G(e,m){let n=j(e);m.noContour||(e.contourLines=Z(n,m),delete n.z),e.minMax=n}import{getGyromagneticRatio as W}from"gyromagnetic-ratio";function H(e,m){for(let n of e){let o=0,f=0;for(let i of n.spectra){if(n.ntuples?.symbol?(!o&&i.observeFrequency&&(o=i.observeFrequency),!f&&i.shiftOffsetVal&&(f=i.shiftOffsetVal)):(o=i.observeFrequency,f=i.shiftOffsetVal),o&&i.xUnits?.toUpperCase().includes("HZ")&&(i.xUnits="PPM",i.xFactor=i.xFactor!==void 0?i.xFactor/o:void 0,i.firstX=i.firstX!==void 0?i.firstX/o:void 0,i.lastX=i.lastX!==void 0?i.lastX/o:void 0,i.deltaX=i.deltaX!==void 0?i.deltaX/o:void 0,i.data))for(let t=0;t<i.data.x.length;t++)i.data.x[t]/=o;if(f&&i.xUnits.toLowerCase().includes("ppm")&&i.firstX!==void 0&&i.lastX!==void 0){let t=i.firstX-f;if(i.firstX=i.firstX-t,i.lastX=i.lastX-t,i.data)for(let l=0;l<i.data.x.length;l++)i.data.x[l]-=t}if(n.ntuples?.nucleus&&n.ntuples.symbol)for(let t=0;t<n.ntuples.nucleus.length;t++){let l=n.ntuples.symbol[t],r=n.ntuples.nucleus[t];if(l.match(/^[F|T]/)&&!r){if(l.match(/[F|T]1/))if(n.tmp.$NUC2&&typeof n.tmp.$NUC2=="string")n.ntuples.nucleus[t]=n.tmp.$NUC2;else{let p=n.ntuples.symbol.indexOf(l.replace(/^([F|T]).*/,"$12"));p&&n.ntuples.nucleus[p]&&(n.ntuples.nucleus[t]=n.ntuples.nucleus[p])}l.match(/[F|T]2/)&&typeof n.tmp.$NUC1=="string"&&(n.ntuples.nucleus[t]=n.tmp.$NUC1)}l.match(/[F|T]2/)&&(n.yType=n.ntuples.nucleus[0],n.xType&&!W(n.xType)&&(n.xType=n.ntuples.nucleus[1]))}if(o&&n.ntuples?.symbol&&n.ntuples.nucleus){let t="",l=n.ntuples.symbol.indexOf(i.pageSymbol);n.ntuples.units?.[l]&&(t=n.ntuples.units[l]);let{nucleus:r}=n.ntuples;if(!t.toLowerCase().match(/(ppm|seconds)/)&&r.length>1){if(l!==0){let h="Not sure about this ntuples format";if(m){m.warn(h);continue}else throw new Error(h)}let{nucleus:p}=n.ntuples,u=W(p[0]),b=W(p[1]);if(!u||!b){let h=`Problem with determination of gyromagnetic ratio for ${p.join("-")}`;if(m)m.error(h);else throw new Error(h)}else{let h=u/b*o;i.pageValue/=h}}}}}}function J(e){let m=e.spectra[0].data;e.chromatogram={times:m.x.slice(),series:{intensity:{dimension:1,data:m.y.slice()}}}}function q(e,m,n){H(e,n.logger),de(e,n);for(let o of e){if(Object.keys(o.ntuples).length>0){let f=[],i=Object.keys(o.ntuples);for(let t of i){let l=o.ntuples[t];if(l)for(let r=0;r<l.length;r++)f[r]||(f[r]={}),f[r][t]=l[r]}o.ntuples=f}o.twoD&&n.wantXY&&(G(o,n),n.logger?.trace({profiling:!0},"Finished countour plot calculation"),n.keepSpectra||delete o.spectra),n.chromatogram&&(o.spectra.length>1?te(o):J(o),n.logger?.trace({profiling:!0},"Finished chromatogram calculation")),delete o.tmp}}function de(e,m){for(let n of e)for(let o in n.meta){let f=n.meta[o];if(typeof f=="string"){if(f.startsWith("{")){if(!f.includes(":")&&f.endsWith("}")){let i=f.slice(1,-1).split(/[,; ]+/).filter(Boolean);for(let t=0;t<i.length;t++)n.meta[o+String(t)]=m.dynamicTyping?ae(i[t]):i[t]}}else if(f.startsWith("(")){let i=f.split(/\r?\n/),t=/^\((?<from>\d+)\.\.(?<to>\d+)\).*$/;if(t.test(i[0])){let[l,r]=i[0].match(t)?.slice(1).map(Number)??[],p=i.slice(1).join(" ").split(/[,; ]+/).filter(Boolean);for(let u=l;u<=r;u++)m.dynamicTyping&&typeof p[u-l]=="string"?n.meta[o+String(u)]=ae(p[u-l]):n.meta[o+String(u)]=p[u-l]}}}}}function z(e,m,n){let o=-1,f=-1,i="",t="";if(n.indexOf("++")>0)i=n.replace(/.*\(([a-zA-Z0-9]+)\+\+.*/,"$1"),t=n.replace(/.*\.\.([a-zA-Z0-9]+).*/,"$1");else{n=n.replaceAll(/[^a-zA-Z%]/g,""),i=n.charAt(0),t=n.charAt(1),m.variables={};for(let l of n){let r=l.toLowerCase(),p=e.ntuples.symbol?.indexOf(l)||0;if(p===-1)throw new Error(`Symbol undefined: ${l}`);m.variables[r]={};for(let u in e.ntuples)e.ntuples[u]?.[p]&&(m.variables[r][u.replace(/^var/,"")]=e.ntuples[u]?.[p])}}o=e.ntuples.symbol?.indexOf(i)??-1,f=e.ntuples.symbol?.indexOf(t)??-1,o===-1&&(o=0),f===-1&&(f=0),e.ntuples.first&&(e.ntuples.first.length>o&&(m.firstX=e.ntuples.first[o]),e.ntuples.first.length>f&&(m.firstY=e.ntuples.first[f])),e.ntuples.last&&(e.ntuples.last.length>o&&(m.lastX=e.ntuples.last[o]),e.ntuples.last.length>f&&(m.lastY=e.ntuples.last[f])),e.ntuples.vardim&&e.ntuples.vardim.length>o&&(m.nbPoints=e.ntuples.vardim[o]),e.ntuples.factor&&(e.ntuples.factor.length>o&&(m.xFactor=e.ntuples.factor[o]),e.ntuples.factor.length>f&&(m.yFactor=e.ntuples.factor[f])),e.ntuples.units&&(e.ntuples.units.length>o&&(e.ntuples.varname?.[o]?m.xUnits=`${e.ntuples.varname[o]} [${e.ntuples.units[o]}]`:m.xUnits=e.ntuples.units[o]),e.ntuples.units.length>f&&(e.ntuples.varname?.[f]?m.yUnits=`${e.ntuples.varname[f]} [${e.ntuples.units[f]}]`:m.yUnits=e.ntuples.units[f]))}function w(e){e.xFactor||(e.xFactor=1),e.yFactor||(e.yFactor=1)}var L=/[ \t]*,[ \t]*/,he={removeComments:!1,keepRecordsRegExp:/^$/,canonicDataLabels:!0,canonicMetadataLabels:!1,dynamicTyping:!0,withoutXY:!1,noTrimRegExp:/^$/,chromatogram:!1,keepSpectra:!1,noContour:!1,nbContourLevels:7,noiseMultiplier:5};function xe(e,m={}){e=ge(e);let n={...he,...m};n.logger?.debug("Starting jcamp conversion"),n.wantXY=!n.withoutXY,n.start=Date.now();let o=[],f={entries:[],flatten:[]},i={children:[],spectra:[],ntuples:{},info:{},meta:{},tmp:{}},t=i,l=[],r={data:{}};n.logger?.trace({profiling:!0},"Before split to LDRS");let p=e.replaceAll(/[\r\n]+##/g,`
|
|
5
|
+
`;let s=h.match(/^##(.*?)=(.+)/);if(s){let a=s[1].replaceAll(/[ _-]/g,"").toUpperCase();a==="DATATYPE"?l.dataType=s[2].trim():a==="DATACLASS"?l.dataClass=s[2].trim():a==="JCAMPDX"?l.jcampDX=O(s[2].trim()):a==="JCAMPCS"&&(l.jcampCS=O(s[2].trim()))}}h.startsWith("##END")&&r>0&&r--}if(n){for(let u of f)u.children=void 0;return f}else return t}import{parseString as de}from"dynamic-typing";import{ensureString as be}from"ensure-string";var ee=["TIC",".RIC","SCANNUMBER"];function te(e){let m=e.spectra,n=m.length,o={times:new Array(n),series:{ms:{dimension:2,data:new Array(n)}}},f=[];for(let i of ee){let t=k(i);m[0][t]&&(f.push(t),o.series[t]={dimension:1,data:new Array(n)})}for(let i=0;i<n;i++){let t=m[i];o.times[i]=t.pageValue;for(let l of f)o.series[l].data[i]=Number(t[l]);t.data&&(o.series.ms.data[i]=[t.data.x,t.data.y])}e.chromatogram=o}function ne(e){return ee.includes(e)}function k(e){return e.toLowerCase().replaceAll(/[^a-z0-9]/g,"")}function E(e){return e.map(Number)}function V(e,m,n){let{logger:o}=n;o&&(e.xFactor||o.info("The xFactor is not defined, it will be set to 1"),e.yFactor||o.info("The yFactor is not defined, it will be set to 1"));let f=e.yFactor??1,i=e.xFactor??1,t=e.deltaX??1,l=Math.abs(t*.1),r=.01;e.isXYdata=!0;let p={x:[],y:[]};e.data=p;let u=e.firstX||0,b=e.firstY||0,h=!1,s,a=0;for(;a<m.length;a++)if(s=m.codePointAt(a),s===13||s===10)h=!0;else if(h)break;let c=0,d=!0,A=!1,y=!1,I=0,N=!1,S=!1,g=0,X=0,M=!1,C=!1,Y=!1,R=0;for(;a<=m.length;a++)if(a===m.length?s=13:s=m.codePointAt(a),S)(s===13||s===10)&&(d=!0,S=!1,c++);else if(s<=57&&s>=48)C=!0,R>0?g+=(s-48)/10**R++:(g*=10,g+=s-48);else if(s===44||s===46)C=!0,R++;else{if(C){if(M&&(g=g*-1,M=!1),d)g*=i,o&&(y?Math.abs(u-t-g)>l&&(Math.abs(u-g)<l?o.trace(`Data line ${c}: After a DIFFERENCE the next line should repeat the last value and this does not seem to be the case: Current value: ${g} - Expected: ${u}.`):Math.abs(u-t-g)>10*l?o.trace(`Data line ${c}: The difference between the first value ${g} and the expected first x value ${u} based on increment after a DIFFERENCE is too high`):o.trace(`Data line ${c}: The difference between the first value ${g} and the expected first x value ${u} based on increment after a DIFFERENCE is too high`)):Math.abs(u-g)>l&&o.trace(`Data line ${c}: The difference between the first value ${g} and the first x value ${u} is too high`)),d=!1,y&&(Y=!0);else if(Y)Y=!1,o&&Math.abs(b-g)>r&&o.trace(`Data line ${c}: After a duplicate the next line should repeat the same value ${g} !== ${b}`),b=g;else{A?(I=M?0-g:g,y=!0,A=!1):N||(X=M?0-g:g);let x=N?g-1:1;for(let F=0;F<x;F++)y?b+=I:b=X,p.x.push(u),p.y.push(b*f),u+=t}M=!1,g=0,R=0,C=!1,N=!1}if(s<74&&s>63)C=!0,y=!1,g=s-64;else if(s>96&&s<106)C=!0,y=!1,g=s-96,M=!0;else if(s===115)C=!0,N=!0,g=9;else if(s>82&&s<91)C=!0,N=!0,g=s-82;else if(s>73&&s<83)C=!0,A=!0,g=s-73;else if(s>105&&s<115)C=!0,A=!0,g=s-105,M=!0;else if(s===36&&m.codePointAt(a+1)===36)C=!0,S=!0;else if(s===37)C=!0,A=!0,g=0,M=!1;else if(s===45){let x=m.codePointAt(a+1);(x!==void 0&&x>=48&&x<=57||x===44||x===46)&&(C=!0,d||(y=!1),M=!0)}else(s===13||s===10)&&(d=!0,S=!1,c++)}o&&y&&o.warn("The last value is a difference, it should be repeated on the next line")}var re=/[,\t ]+/;function P(e,m,n){if(e.isPeaktable=!0,!e.variables||Object.keys(e.variables).length===2?me(e,m,n):ue(e,m,n),e.variables)for(let o in e.variables)e.variables[o].data=e.data?.[o]}function me(e,m,n){let{logger:o}=n,f={x:[],y:[]};e.data=f;let i=m.split(/,? *,?[;\r\n]+ */);for(let t=1;t<i.length;t++){let l=i[t].trim().replace($,"").split(re);if(l.length%2===0)for(let r=0;r<l.length;r=r+2)e.xFactor!==void 0&&e.yFactor!==void 0&&(f.x.push(Number(l[r])*e.xFactor),f.y.push(Number(l[r+1])*e.yFactor));else o?.warn(`Format error: ${l.toString()}`)}}function ue(e,m,n){let{logger:o}=n,f={},i=Object.keys(e.variables),t=i.length;for(let r of i)f[r]=[];e.data=f;let l=m.split(/,? *,?[;\r\n]+ */);for(let r=1;r<l.length;r++){let p=l[r].trim().replace($,"").split(re);if(p.length%t===0)for(let u=0;u<p.length;u++)f[i[u%t]].push(Number(p[u]));else o?.warn(`Wrong number of columns: ${p.toString()}`)}}function B(e,m){e.isXYAdata=!0;let n={};e.data=n;let o=m.split(/\r?\n/),f=o[0].replace(/^.*?([A-Z]+).*$/,"$1").split("").map(i=>i.toLowerCase());for(let i=1;i<o.length;i++){let t=o[i].replace(/^\((.*)\)$/,"$1").split(/ *, */);for(let l=0;l<f.length;l++){let r=t[l];switch(f[l]){case"x":case"y":case"w":r=Number.parseFloat(r);break;case"a":r=r.replace(/^<(.*)>$/,"$1");break;case"m":break;default:continue}n[f[l]]||(n[f[l]]=[]),n[f[l]].push(r)}}}import{parseString as ae}from"dynamic-typing";import{getGyromagneticRatio as oe}from"gyromagnetic-ratio";import ie from"ml-array-median";function se(e){return Array.isArray(e)?e[0]:e}function j(e){let{spectra:m}=e,n=m[0].data.y[0],o=n,f=m.length,i=m[0].data.x.length||0,t=new Array(f);for(let s=0;s<f;s++){t[s]=m[s].data.y;for(let a=0;a<i;a++){let c=t[s][a];c<n&&(n=c),c>o&&(o=c)}}let l=m[0].data.x[0],r=m[0].data.x.at(-1),{firstY:p,lastY:u}=ce(e);if(l>r)for(let s of t)s.reverse();p>u&&t.reverse();let b=[];for(let s of t){let a=Float64Array.from(s);for(let c=0;c<a.length;c++)a[c]<0&&(a[c]=-a[c]);b.push(ie(a))}let h=ie(b);return{z:t,minX:Math.min(l,r),maxX:Math.max(l,r),minY:Math.min(p,u),maxY:Math.max(p,u),minZ:n,maxZ:o,noise:h}}function ce(e){let{spectra:m,ntuples:n,info:o}=e;if(n)for(let f of n){let{symbol:i,nucleus:t,units:l}=f;if(i.match(/[F|T]1/)&&l?.toUpperCase().match("HZ")){let r=se(o[".OBSERVEFREQUENCY"]),{nucleus:p}=n.find(c=>c.symbol.match(/[F|T]2/));if([r,p,t].some(c=>!c))break;let u=oe(p),b=oe(t),{first:h,last:s}=f,a=b!==null&&u!==null?r*b/u:r;return{firstY:h/a,lastY:s/a}}}return{firstY:m[0].pageValue,lastY:m.at(-1).pageValue}}function Z(e,m){let n=e.noise,o=e.z,f,i,t,l,r,p,u,b,h=o.length,s=o[0].length,a,c,d,A,y=e.minX,N=(e.maxX-y)/(s-1),S=e.minY,X=(e.maxY-S)/(h-1),M=e.minZ,C=e.maxZ,Y=m.nbContourLevels*2,R=new Array(Y),x;for(let F=0;F<Y;F++){let U={};R[F]=U;let le=F%2,K=(C-m.noiseMultiplier*n)*Math.exp((F>>1)-m.nbContourLevels);le===0?x=K+m.noiseMultiplier*n:x=0-K-m.noiseMultiplier*n;let D=[];if(U.zValue=x,U.lines=D,!(x<=M||x>=C))for(let v=0;v<h-1;v++){let Q=o[v],_=o[v+1];for(let T=0;T<s-1;T++)f=Q[T],i=Q[T+1],t=_[T],l=_[T+1],r=f>x,p=i>x,u=t>x,b=l>x,r!==p&&r!==u&&(a=T+(x-f)/(i-f),c=v,d=T,A=v+(x-f)/(t-f),D.push(a*N+y,c*X+S,d*N+y,A*X+S)),b!==p&&b!==u&&(a=T+1,c=v+1-(x-l)/(i-l),d=T+1-(x-l)/(t-l),A=v+1,D.push(a*N+y,c*X+S,d*N+y,A*X+S)),p!==u&&(a=(T+1-(x-i)/(t-i))*N+y,c=(v+(x-i)/(t-i))*X+S,p!==r&&(d=T+1-(x-i)/(f-i),A=v,D.push(a,c,d*N+y,A*X+S)),u!==r&&(d=T,A=v+1-(x-t)/(f-t),D.push(a,c,d*N+y,A*X+S)),p!==b&&(d=T+1,A=v+(x-i)/(l-i),D.push(a,c,d*N+y,A*X+S)),u!==b&&(d=T+(x-t)/(l-t),A=v+1,D.push(a,c,d*N+y,A*X+S)))}}return{minX:e.minX,maxX:e.maxX,minY:e.minY,maxY:e.maxY,segments:R}}function G(e,m){let n=j(e);m.noContour||(e.contourLines=Z(n,m),delete n.z),e.minMax=n}import{getGyromagneticRatio as W}from"gyromagnetic-ratio";function H(e,m){for(let n of e){let o=0,f=0;for(let i of n.spectra){if(n.ntuples?.symbol?(!o&&i.observeFrequency&&(o=i.observeFrequency),!f&&i.shiftOffsetVal&&(f=i.shiftOffsetVal)):(o=i.observeFrequency,f=i.shiftOffsetVal),o&&i.xUnits?.toUpperCase().includes("HZ")&&(i.xUnits="PPM",i.xFactor=i.xFactor!==void 0?i.xFactor/o:void 0,i.firstX=i.firstX!==void 0?i.firstX/o:void 0,i.lastX=i.lastX!==void 0?i.lastX/o:void 0,i.deltaX=i.deltaX!==void 0?i.deltaX/o:void 0,i.data))for(let t=0;t<i.data.x.length;t++)i.data.x[t]/=o;if(f&&i.xUnits.toLowerCase().includes("ppm")&&i.firstX!==void 0&&i.lastX!==void 0){let t=i.firstX-f;if(i.firstX=i.firstX-t,i.lastX=i.lastX-t,i.data)for(let l=0;l<i.data.x.length;l++)i.data.x[l]-=t}if(n.ntuples?.nucleus&&n.ntuples.symbol)for(let t=0;t<n.ntuples.nucleus.length;t++){let l=n.ntuples.symbol[t],r=n.ntuples.nucleus[t];if(l.match(/^[F|T]/)&&!r){if(l.match(/[F|T]1/))if(n.tmp.$NUC2&&typeof n.tmp.$NUC2=="string")n.ntuples.nucleus[t]=n.tmp.$NUC2;else{let p=n.ntuples.symbol.indexOf(l.replace(/^([F|T]).*/,"$12"));p&&n.ntuples.nucleus[p]&&(n.ntuples.nucleus[t]=n.ntuples.nucleus[p])}l.match(/[F|T]2/)&&typeof n.tmp.$NUC1=="string"&&(n.ntuples.nucleus[t]=n.tmp.$NUC1)}l.match(/[F|T]2/)&&(n.yType=n.ntuples.nucleus[0],n.xType&&!W(n.xType)&&(n.xType=n.ntuples.nucleus[1]))}if(o&&n.ntuples?.symbol&&n.ntuples.nucleus){let t="",l=n.ntuples.symbol.indexOf(i.pageSymbol);n.ntuples.units?.[l]&&(t=n.ntuples.units[l]);let{nucleus:r}=n.ntuples;if(!t.toLowerCase().match(/(ppm|seconds)/)&&r.length>1){if(l!==0){let h="Not sure about this ntuples format";if(m){m.warn(h);continue}else throw new Error(h)}let{nucleus:p}=n.ntuples,u=W(p[0]),b=W(p[1]);if(!u||!b){let h=`Problem with determination of gyromagnetic ratio for ${p.join("-")}`;if(m)m.error(h);else throw new Error(h)}else{let h=u/b*o;i.pageValue/=h}}}}}}function J(e){let m=e.spectra[0].data;e.chromatogram={times:m.x.slice(),series:{intensity:{dimension:1,data:m.y.slice()}}}}function q(e,m,n){H(e,n.logger),pe(e,n);for(let o of e){if(Object.keys(o.ntuples).length>0){let f=[],i=Object.keys(o.ntuples);for(let t of i){let l=o.ntuples[t];if(l)for(let r=0;r<l.length;r++)f[r]||(f[r]={}),f[r][t]=l[r]}o.ntuples=f}o.twoD&&n.wantXY&&(G(o,n),n.logger?.trace({profiling:!0},"Finished countour plot calculation"),n.keepSpectra||delete o.spectra),n.chromatogram&&(o.spectra.length>1?te(o):J(o),n.logger?.trace({profiling:!0},"Finished chromatogram calculation")),delete o.tmp}}function pe(e,m){for(let n of e)for(let o in n.meta){let f=n.meta[o];if(typeof f=="string"){if(f.startsWith("{")){if(!f.includes(":")&&f.endsWith("}")){let i=f.slice(1,-1).split(/[,; ]+/).filter(Boolean);for(let t=0;t<i.length;t++)n.meta[o+String(t)]=m.dynamicTyping?ae(i[t]):i[t]}}else if(f.startsWith("(")){let i=f.split(/\r?\n/),t=/^\((?<from>\d+)\.\.(?<to>\d+)\).*$/;if(t.test(i[0])){let[l,r]=i[0].match(t)?.slice(1).map(Number)??[],p=i.slice(1).join(" ").split(/[,; ]+/).filter(Boolean);for(let u=l;u<=r;u++)m.dynamicTyping&&typeof p[u-l]=="string"?n.meta[o+String(u)]=ae(p[u-l]):n.meta[o+String(u)]=p[u-l]}}}}}function z(e,m,n){let o=-1,f=-1,i="",t="";if(n.indexOf("++")>0)i=n.replace(/.*\(([a-zA-Z0-9]+)\+\+.*/,"$1"),t=n.replace(/.*\.\.([a-zA-Z0-9]+).*/,"$1");else{n=n.replaceAll(/[^a-zA-Z%]/g,""),i=n.charAt(0),t=n.charAt(1),m.variables={};for(let l of n){let r=l.toLowerCase(),p=e.ntuples.symbol?.indexOf(l)||0;if(p===-1)throw new Error(`Symbol undefined: ${l}`);m.variables[r]={};for(let u in e.ntuples)e.ntuples[u]?.[p]&&(m.variables[r][u.replace(/^var/,"")]=e.ntuples[u]?.[p])}}o=e.ntuples.symbol?.indexOf(i)??-1,f=e.ntuples.symbol?.indexOf(t)??-1,o===-1&&(o=0),f===-1&&(f=0),e.ntuples.first&&(e.ntuples.first.length>o&&(m.firstX=e.ntuples.first[o]),e.ntuples.first.length>f&&(m.firstY=e.ntuples.first[f])),e.ntuples.last&&(e.ntuples.last.length>o&&(m.lastX=e.ntuples.last[o]),e.ntuples.last.length>f&&(m.lastY=e.ntuples.last[f])),e.ntuples.vardim&&e.ntuples.vardim.length>o&&(m.nbPoints=e.ntuples.vardim[o]),e.ntuples.factor&&(e.ntuples.factor.length>o&&(m.xFactor=e.ntuples.factor[o]),e.ntuples.factor.length>f&&(m.yFactor=e.ntuples.factor[f])),e.ntuples.units&&(e.ntuples.units.length>o&&(e.ntuples.varname?.[o]?m.xUnits=`${e.ntuples.varname[o]} [${e.ntuples.units[o]}]`:m.xUnits=e.ntuples.units[o]),e.ntuples.units.length>f&&(e.ntuples.varname?.[f]?m.yUnits=`${e.ntuples.varname[f]} [${e.ntuples.units[f]}]`:m.yUnits=e.ntuples.units[f]))}function w(e){e.xFactor||(e.xFactor=1),e.yFactor||(e.yFactor=1)}var L=/[ \t]*,[ \t]*/,ge={removeComments:!1,keepRecordsRegExp:/^$/,canonicDataLabels:!0,canonicMetadataLabels:!1,dynamicTyping:!0,withoutXY:!1,noTrimRegExp:/^$/,chromatogram:!1,keepSpectra:!1,noContour:!1,nbContourLevels:7,noiseMultiplier:5};function pt(e,m={}){e=be(e);let n={...ge,...m};n.logger?.debug("Starting jcamp conversion"),n.wantXY=!n.withoutXY,n.start=Date.now();let o=[],f={entries:[],flatten:[]},i={children:[],spectra:[],ntuples:{},info:{},meta:{},tmp:{}},t=i,l=[],r={data:{}};n.logger?.trace({profiling:!0},"Before split to LDRS");let p=e.replaceAll(/[\r\n]+##/g,`
|
|
6
6
|
##`).split(`
|
|
7
7
|
##`);n.logger?.trace({profiling:!0},"Split to LDRS"),p[0]&&(p[0]=p[0].replace(/^[\r\n ]*##/,""));for(let u of p){let b=u.indexOf("="),h=b>0?u.slice(0,Math.max(0,b)):u,s=b>0?h.match(n.noTrimRegExp)?u.slice(Math.max(0,b+1)):u.slice(Math.max(0,b+1)).trim():"",a=h.replaceAll(/[_ -]/g,"").toUpperCase();if(a==="DATATABLE"){let c=s.indexOf(`
|
|
8
8
|
`);if(c===-1&&(c=s.indexOf("\r")),c>0){let d=s.slice(0,Math.max(0,c)).split(/[ ,;\t]+/);z(t,r,d[0]),r.datatable=d[0],d[1]?.includes("PEAKS")?a="PEAKTABLE":d[1]&&(d[1].indexOf("XYDATA")||d[0].indexOf("++")>0)&&(a="XYDATA",r.nbPoints&&r.lastX!==void 0&&r.firstX!==void 0&&(r.deltaX=(r.lastX-r.firstX)/(r.nbPoints-1)))}}if(a==="XYDATA"){n.wantXY&&(w(r),s.match(/.*\+\+.*/)?(r.nbPoints&&r.lastX!==void 0&&r.firstX!==void 0&&(r.deltaX=(r.lastX-r.firstX)/(r.nbPoints-1)),V(r,s,n)):P(r,s,n),t?.spectra.push(r),r={data:{}});continue}else if(a==="PEAKTABLE"){n.wantXY&&(w(r),P(r,s,n),t?.spectra.push(r),r={data:{}});continue}if(a==="PEAKASSIGNMENTS"){n.wantXY&&(s.match(/.*[^A-Z]*.*/)&&B(r,s),t?.spectra.push(r),r={data:{}});continue}if(n.removeComments&&(s=s.split(/\r?\n/).map(c=>c.replace(/ *\$\$.*$/,"")).join(`
|
|
9
|
-
`)),a==="TITLE"){let c=t;c.children||(c.children=[]),t={spectra:[],ntuples:{},info:{},meta:{},tmp:{}},c.children.push(t),l.push(c),o.push(t),t.title=s}else a==="DATATYPE"?(t.dataType=s,s.match(/^nd|\snd\s/i)&&(t.twoD=!0)):a==="NTUPLES"?s.match(/^nd|\snd\s/i)&&(t.twoD=!0):a==="DATACLASS"?t.dataClass=s:a==="JCAMPDX"?t.jcampDX=O(s):a==="JCAMPCS"?t.jcampCS=O(s):a==="XUNITS"?r.xUnits=s:a==="YUNITS"?r.yUnits=s:a==="FIRSTX"?r.firstX=Number(s):a==="LASTX"?r.lastX=Number(s):a==="FIRSTY"?r.firstY=Number(s):a==="LASTY"?r.lastY=Number(s):a==="NPOINTS"?r.nbPoints=Number(s):a==="XFACTOR"?r.xFactor=Number(s):a==="YFACTOR"?r.yFactor=Number(s):a==="MAXX"?r.maxX=Number(s):a==="MINX"?r.minX=Number(s):a==="MAXY"?r.maxY=Number(s):a==="MINY"?r.minY=Number(s):a==="DELTAX"?r.deltaX=Number(s):a===".OBSERVEFREQUENCY"||a==="$SFO1"?r.observeFrequency||(r.observeFrequency=Number(s)):a===".OBSERVENUCLEUS"?r.xType||(t.xType=s.replaceAll(/[^a-zA-Z0-9]/g,"")):a==="$OFFSET"?(t.shiftOffsetNum=0,r.shiftOffsetVal||(r.shiftOffsetVal=Number(s))):a==="$REFERENCEPOINT"||(a==="VARNAME"?t.ntuples.varname=s.split(L):a==="SYMBOL"?t.ntuples.symbol=s.split(L):a==="VARTYPE"?t.ntuples.vartype=s.split(L):a==="VARFORM"?t.ntuples.varform=s.split(L):a==="VARDIM"?t.ntuples.vardim=E(s.split(L)):a==="UNITS"?t.ntuples.units=s.split(L):a==="FACTOR"?t.ntuples.factor=E(s.split(L)):a==="FIRST"?t.ntuples.first=E(s.split(L)):a==="LAST"?t.ntuples.last=E(s.split(L)):a==="MIN"?t.ntuples.min=E(s.split(L)):a==="MAX"?t.ntuples.max=E(s.split(L)):a===".NUCLEUS"?t.ntuples&&(t.ntuples.nucleus=s.split(L).map(c=>c.replaceAll(/[^a-zA-Z0-9]/g,""))):a==="PAGE"?(r.page=s.trim(),r.pageValue=Number(s.replace(/^.*=/,"")),r.pageSymbol=r.page.replace(/[=].*/,"")):a==="RETENTIONTIME"?r.pageValue=Number(s):ne(a)?r[k(a)]=s:a==="SAMPLEDESCRIPTION"?r.sampleDescription=s:a.startsWith("$NUC")?!t.tmp[a]&&!s.includes("off")&&(t.tmp[a]=s.replaceAll(/[<>]/g,"")):a==="END"&&(t=l.pop()));if(t?.info&&t.meta&&a.match(n.keepRecordsRegExp)){let c,d;h.startsWith("$")?(d=n.canonicMetadataLabels?a.slice(1):h.slice(1),c=t.meta):(d=n.canonicDataLabels?a:h,c=t.info),n.dynamicTyping&&(s=
|
|
9
|
+
`)),a==="TITLE"){let c=t;c.children||(c.children=[]),t={spectra:[],ntuples:{},info:{},meta:{},tmp:{}},c.children.push(t),l.push(c),o.push(t),t.title=s}else a==="DATATYPE"?(t.dataType=s,s.match(/^nd|\snd\s/i)&&(t.twoD=!0)):a==="NTUPLES"?s.match(/^nd|\snd\s/i)&&(t.twoD=!0):a==="DATACLASS"?t.dataClass=s:a==="JCAMPDX"?t.jcampDX=O(s):a==="JCAMPCS"?t.jcampCS=O(s):a==="XUNITS"?r.xUnits=s:a==="YUNITS"?r.yUnits=s:a==="FIRSTX"?r.firstX=Number(s):a==="LASTX"?r.lastX=Number(s):a==="FIRSTY"?r.firstY=Number(s):a==="LASTY"?r.lastY=Number(s):a==="NPOINTS"?r.nbPoints=Number(s):a==="XFACTOR"?r.xFactor=Number(s):a==="YFACTOR"?r.yFactor=Number(s):a==="MAXX"?r.maxX=Number(s):a==="MINX"?r.minX=Number(s):a==="MAXY"?r.maxY=Number(s):a==="MINY"?r.minY=Number(s):a==="DELTAX"?r.deltaX=Number(s):a===".OBSERVEFREQUENCY"||a==="$SFO1"?r.observeFrequency||(r.observeFrequency=Number(s)):a===".OBSERVENUCLEUS"?r.xType||(t.xType=s.replaceAll(/[^a-zA-Z0-9]/g,"")):a==="$OFFSET"?(t.shiftOffsetNum=0,r.shiftOffsetVal||(r.shiftOffsetVal=Number(s))):a==="$REFERENCEPOINT"||(a==="VARNAME"?t.ntuples.varname=s.split(L):a==="SYMBOL"?t.ntuples.symbol=s.split(L):a==="VARTYPE"?t.ntuples.vartype=s.split(L):a==="VARFORM"?t.ntuples.varform=s.split(L):a==="VARDIM"?t.ntuples.vardim=E(s.split(L)):a==="UNITS"?t.ntuples.units=s.split(L):a==="FACTOR"?t.ntuples.factor=E(s.split(L)):a==="FIRST"?t.ntuples.first=E(s.split(L)):a==="LAST"?t.ntuples.last=E(s.split(L)):a==="MIN"?t.ntuples.min=E(s.split(L)):a==="MAX"?t.ntuples.max=E(s.split(L)):a===".NUCLEUS"?t.ntuples&&(t.ntuples.nucleus=s.split(L).map(c=>c.replaceAll(/[^a-zA-Z0-9]/g,""))):a==="PAGE"?(r.page=s.trim(),r.pageValue=Number(s.replace(/^.*=/,"")),r.pageSymbol=r.page.replace(/[=].*/,"")):a==="RETENTIONTIME"?r.pageValue=Number(s):ne(a)?r[k(a)]=s:a==="SAMPLEDESCRIPTION"?r.sampleDescription=s:a.startsWith("$NUC")?!t.tmp[a]&&!s.includes("off")&&(t.tmp[a]=s.replaceAll(/[<>]/g,"")):a==="END"&&(t=l.pop()));if(t?.info&&t.meta&&a.match(n.keepRecordsRegExp)){let c,d;h.startsWith("$")?(d=n.canonicMetadataLabels?a.slice(1):h.slice(1),c=t.meta):(d=n.canonicDataLabels?a:h,c=t.info),n.dynamicTyping&&(s=de(s)),c[d]?(Array.isArray(c[d])||(c[d]=[c[d]]),c[d].push(s)):c[d]=s}}if(n.logger?.trace({profiling:!0},"Finished parsing"),q(o,f,n),n.logger?.trace({profiling:!0},"Total time"),f.entries=i.children||[],f.flatten=o,n.logger){n.logger.debug("Finished jcamp conversion");for(let u of f.flatten)n.logger.debug(`${u.dataType} - ${u.title}`)}return f}function gt(e,m){let{logger:n,OCL:o}=m;if(!e.info.ATOMLIST||!e.info.BONDLIST){n?.warn("No ATOMLIST or BONDLIST in the JCAMP-CS entry");return}Ce(e,n);let f=xe(e.info.ATOMLIST),i=ye(e.info.BONDLIST),t=Ae(e.info.CHARGE,n),l=he(o,f,i,t,n);return{molecule:l,molfile:l.toMolfile()}}function he(e,m,n,o,f){let i={S:e.Molecule.cBondTypeSingle,D:e.Molecule.cBondTypeDouble,T:e.Molecule.cBondTypeTriple,Q:e.Molecule.cBondTypeQuadruple},t={},l=new e.Molecule(m.length,n.length);for(let r of m){let p=e.Molecule.getAtomicNoFromLabel(r.element);p||(f?.error(`Atomic number of ${r.element} could not be determined`),p=e.Molecule.getAtomicNoFromLabel("X")),t[r.atomNumber]=l.addAtom(p),l.setAtomMapNo(t[r.atomNumber],r.atomNumber,!1)}for(let r of n){if(t[r.atomNumber1]===void 0){f?.error(`A bond goes to atomNumber ${r.atomNumber1} that does not exists`);continue}if(t[r.atomNumber2]===void 0){f?.error(`A bond goes to atomNumber ${r.atomNumber2} that does not exists`);continue}let p=l.addBond(t[r.atomNumber1],t[r.atomNumber2]);l.setBondType(p,i[r.bondType])}for(let r of o){if(t[r.atomNumber]===void 0){f?.error(`A charge goes to atomNumber ${r.atomNumber} that does not exists`);continue}l.setAtomCharge(t[r.atomNumber],r.value)}return l}function xe(e){let m=e.split(/\r?\n/).map(o=>O(o)).filter(Boolean),n=[];for(let o of m){let[f,i,t]=o.trim().split(/\s+/);n.push({atomNumber:Number(f),element:i,nbImplicitHydrogens:t?.length>0?Number(t):void 0})}return n}function ye(e){let m=e.split(/\r?\n/).map(o=>O(o)).filter(Boolean),n=[];for(let o of m){let[f,i,t]=o.trim().split(/\s+/);n.push({atomNumber1:Number(f),atomNumber2:Number(i),bondType:t})}return n}function Ae(e,m){if(!e)return[];let n=e.split(/\r?\n/).map(f=>O(f)).filter(Boolean),o=[];for(let f of n){let[i,t,l]=f.trim().split(/\s+/);l!==void 0&&m?.warn("Charge on multiple atom is not supported"),o.push({atomNumber:Number(t),value:Number(i)})}return o}function Ce(e,m){let n=["STEREOPAIR","STEREOCENTER","RADICAL","STEREOMOLECULE"];for(let o of n)e.info[o]&&m?.warn(`JCAMP-CS parser do not support: ${o}`)}export{pt as convert,ve as createTree,gt as parseJcampCS};
|