Skip to content

Commit

Permalink
feat!: use filecollection (#35)
Browse files Browse the repository at this point in the history
* fix: use fileCollection

* chore: update dependencies
  • Loading branch information
jobo322 committed Oct 3, 2022
1 parent 6dba813 commit 0c30f15
Show file tree
Hide file tree
Showing 8 changed files with 909 additions and 1,012 deletions.
1,853 changes: 872 additions & 981 deletions package-lock.json

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,27 +42,27 @@
},
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-jest": "^28.1.3",
"babel-jest": "^29.1.2",
"babel-plugin-remove-comments": "^2.0.0",
"babel-plugin-transform-es2015-modules-commonjs": "^6.26.2",
"babel-preset-babili": "^0.1.4",
"cheminfo-tools": "^1.23.3",
"codecov": "^3.8.2",
"eslint": "^8.22.0",
"eslint": "^8.24.0",
"eslint-config-cheminfo": "^8.0.2",
"esm": "^3.2.25",
"jest": "^28.1.3",
"nmredata-data-test": "^0.3.0",
"jest": "^29.1.2",
"nmredata-data-test": "0.4.0",
"prettier": "^2.7.1",
"rollup": "^2.78.0"
"rollup": "^2.79.1"
},
"dependencies": {
"filelist-utils": "^0.7.1",
"filelist-utils": "^0.11.0",
"jszip": "^3.10.1",
"openchemlib": "^8.0.0",
"openchemlib": "^8.0.1",
"openchemlib-utils": "^2.0.0"
},
"volta": {
"node": "16.16.0"
}
}
}
17 changes: 9 additions & 8 deletions src/NmrRecord.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,20 @@ export class NmrRecord {
if (!(nmrRecord instanceof Object)) {
throw new Error('Cannot be called directly');
}
let { sdfData, files } = nmrRecord;
this.files = files;
let { sdfData, fileCollection } = nmrRecord;
this.fileCollection = fileCollection;
this.sdfData = sdfData;
this.activeElement = 0;
this.nbSamples = sdfData.length;
}

static async fromFileList(files) {
static async fromFileCollection(fileCollection) {
const files = fileCollection.files;
if (!Array.isArray(files) || files.length < 1) {
throw new Error('should be at least 1 file');
}
const sdfData = await getSDF(files);
return new NmrRecord({ sdfData, files });
const sdfData = await getSDF(fileCollection);
return new NmrRecord({ sdfData, fileCollection });
}

getMol(i = this.activeElement) {
Expand Down Expand Up @@ -72,7 +73,7 @@ export class NmrRecord {
toJSON(i = this.activeElement) {
let index = this.checkIndex(i);
return nmrRecordToJSON({
files: this.files,
fileCollection: this.fileCollection,
sdf: this.sdfData[index],
});
}
Expand Down Expand Up @@ -115,7 +116,7 @@ export class NmrRecord {
*/
// there is an error here, we should be able to export any index in sdf, add options use activeElement.
export const nmrRecordToJSON = (options = {}) => {
let { sdf, molecule, files } = options;
let { sdf, molecule, fileCollection } = options;
let sdfFile = checkSdf(sdf);
molecule = !molecule
? OCLMolecule.fromMolfile(sdfFile.molecules[0].molfile)
Expand All @@ -128,7 +129,7 @@ export const nmrRecordToJSON = (options = {}) => {
return nmredataToJSON(nmredata, {
molecule,
root: sdfFile.root,
files,
fileCollection,
});
};

Expand Down
2 changes: 1 addition & 1 deletion src/converter/__tests__/nmredataToJSON.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('NMReData to nmrium', () => {
expect(experiment).toBe('zg30');

expect(
source.file.files.map((file) => file.webkitRelativePath),
source.file.fileCollection.files.map((file) => file.relativePath),
).toStrictEqual([
'AN-menthol/10/uxnmr.par',
'AN-menthol/10/prosol_History',
Expand Down
15 changes: 8 additions & 7 deletions src/converter/util/toJSON/getBrukerFiles.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { FileCollection } from 'filelist-utils';

export async function getBrukerFiles(tag, options) {
let { files, root } = options;
let { fileCollection, root } = options;

let locationLine = tag.data.find((e) => e.value.spectrum_location);

Expand All @@ -14,17 +16,16 @@ export async function getBrukerFiles(tag, options) {
let toCheck2 = pathSpectrum.replace(/.*\/[0-9]+\/pdata\/([0-9]+)\/.*/, '$1');

let brukerFolder = [];
for (let file of files) {
for (let file of fileCollection) {
if (
toCheck !==
file.webkitRelativePath.replace(/([.*/]*\w+\/[0-9]+\/).*/, '$1')
toCheck !== file.relativePath.replace(/([.*/]*\w+\/[0-9]+\/).*/, '$1')
) {
continue;
}
if (file.webkitRelativePath.match('pdata')) {
if (file.relativePath.match('pdata')) {
if (
toCheck2 !==
file.webkitRelativePath.replace(/.*\/[0-9]+\/pdata\/([0-9]+)\/.*/, '$1')
file.relativePath.replace(/.*\/[0-9]+\/pdata\/([0-9]+)\/.*/, '$1')
) {
continue;
}
Expand All @@ -34,6 +35,6 @@ export async function getBrukerFiles(tag, options) {
return {
name: `${pathSpectrum}`,
type: 'brukerFiles',
files: brukerFolder,
fileCollection: new FileCollection(brukerFolder),
};
}
10 changes: 7 additions & 3 deletions src/converter/util/toJSON/getJcamp.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { FileCollection } from 'filelist-utils';

export async function getJcamp(tag, options) {
let { files, root } = options;
let { fileCollection, root } = options;
let locationLine = tag.data.find((e) => e.value.jcamp_location);

if (!locationLine) {
Expand All @@ -9,14 +11,16 @@ export async function getJcamp(tag, options) {

let relativePath = locationLine.value.jcamp_location;
let pathJcamp = root + relativePath.replace(/file:/s, '');
const jcampFile = files.find((file) => file.webkitRelativePath === pathJcamp);
const jcampFile = fileCollection.files.find(
(file) => file.relativePath === pathJcamp,
);
if (!jcampFile) {
new Error(`There is not jcamp with path: ${pathJcamp}`);
return;
}
return {
name: pathJcamp,
type: 'jcamp',
files: [jcampFile],
fileCollection: new FileCollection([jcampFile]),
};
}
6 changes: 3 additions & 3 deletions src/reader/readNmrRecord.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fileListFromZip } from 'filelist-utils';
import { fileCollectionFromZip } from 'filelist-utils';

import { NmrRecord } from '../NmrRecord';

Expand All @@ -8,6 +8,6 @@ import { NmrRecord } from '../NmrRecord';
* @return {} An Object with two properties zip and sdfFiles.
*/
export async function readNmrRecord(zipFile) {
const files = await fileListFromZip(zipFile);
return NmrRecord.fromFileList(files);
const fileCollection = await fileCollectionFromZip(zipFile);
return NmrRecord.fromFileCollection(fileCollection);
}
2 changes: 1 addition & 1 deletion src/util/getSDF.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { parseSDF } from '../parser/parseSDF';
export async function getSDF(files) {
let result = [];
for (const file of files) {
const pathFile = file.webkitRelativePath.split('/');
const pathFile = file.relativePath.split('/');
if (/^[^.].+sdf$/.exec(file.name)) {
const filename = file.name.replace(/\.sdf/, '');
const root = pathFile.slice(0, pathFile.length - 1).join('/');
Expand Down

0 comments on commit 0c30f15

Please sign in to comment.