From e13d02db713c9eabdff3587a7ae3d3d25ed54b7c Mon Sep 17 00:00:00 2001 From: adewyer Date: Tue, 18 Jan 2022 17:27:12 -0800 Subject: [PATCH 1/7] Updates for setting file name based on file contents instead of name --- src/parsers/poscar.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/parsers/poscar.js b/src/parsers/poscar.js index 17e0244e..c0b3c563 100644 --- a/src/parsers/poscar.js +++ b/src/parsers/poscar.js @@ -63,8 +63,17 @@ export function atomsCount(poscarFileContent) { return atomsLine.map((x) => parseInt(x, 10)).reduce((a, b) => a + b); } +export function materialNameFromFileContents(name, fileContent) { + const materialNameFromFile = fileContent.split(/\r?\n/)[0]; + if (name === materialNameFromFile || !materialNameFromFile) { + return name; + } + return materialNameFromFile; +} + export default { toPoscar, atomicConstraintsCharFromBool, atomsCount, + materialNameFromFileContents, }; From 9e46b96c1eb87c567a16188925af1f1acfa7ff98 Mon Sep 17 00:00:00 2001 From: adewyer Date: Tue, 18 Jan 2022 17:34:05 -0800 Subject: [PATCH 2/7] added test for new poscar feature that returns material name based on first line of poscar file --- tests/enums.js | 1 + tests/fixtures/POSCAR.poscar | 3 +++ tests/parsers/poscar.js | 9 +++++++-- 3 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 tests/fixtures/POSCAR.poscar diff --git a/tests/enums.js b/tests/enums.js index 1b876f27..78740ead 100644 --- a/tests/enums.js +++ b/tests/enums.js @@ -31,3 +31,4 @@ export const SiPWSCFInput = readFile(path.join(FIXTURES_DIR, "Si-pwscf.in")); export const Zr1H23Zr1H1 = readJSONFile(path.join(FIXTURES_DIR, "Zr1H23Zr1H1.json")); export const Zr1H23Zr1H1Poscar = readFile(path.join(FIXTURES_DIR, "Zr1H23Zr1H1.poscar")); export const H2O = readFile(path.join(FIXTURES_DIR, "H2O.poscar")); +export const GenericPoscar = readFile(path.join(FIXTURES_DIR, "POSCAR.poscar")); diff --git a/tests/fixtures/POSCAR.poscar b/tests/fixtures/POSCAR.poscar new file mode 100644 index 00000000..30e9f354 --- /dev/null +++ b/tests/fixtures/POSCAR.poscar @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:937fe0ff44defdce92042e6261e79bc2d425c87b33d2b412f20883e39384833f +size 302 diff --git a/tests/parsers/poscar.js b/tests/parsers/poscar.js index c4642fb1..eb466531 100644 --- a/tests/parsers/poscar.js +++ b/tests/parsers/poscar.js @@ -1,8 +1,8 @@ import { expect } from "chai"; import { Material } from "../../src/material"; -import { atomsCount } from "../../src/parsers/poscar"; -import { H2O, Na4Cl4, Na4Cl4Poscar, Zr1H23Zr1H1, Zr1H23Zr1H1Poscar } from "../enums"; +import { atomsCount, materialNameFromFileContents } from "../../src/parsers/poscar"; +import { GenericPoscar, H2O, Na4Cl4, Na4Cl4Poscar, Zr1H23Zr1H1, Zr1H23Zr1H1Poscar } from "../enums"; describe("Parsers.POSCAR", () => { it("should return a valid poscar", () => { @@ -18,4 +18,9 @@ describe("Parsers.POSCAR", () => { it("should return the number of atoms for a molecule in a poscar file", () => { expect(atomsCount(H2O)).to.be.equal(3); }); + it("should return the material name based on the poscar file contents", () => { + const name = "POSCAR.poscar"; + const fileContents = GenericPoscar; + expect(materialNameFromFileContents(name, fileContents)).to.be.equal("H2S"); + }); }); From b42852d97a24a1c56e23b1f4fadb801c3f23ac2e Mon Sep 17 00:00:00 2001 From: adewyer Date: Tue, 18 Jan 2022 17:47:39 -0800 Subject: [PATCH 3/7] update cicd.yml file to fix broken tests --- .github/workflows/cicd.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/workflows/cicd.yml b/.github/workflows/cicd.yml index e3e6c484..90d3b3be 100644 --- a/.github/workflows/cicd.yml +++ b/.github/workflows/cicd.yml @@ -18,6 +18,8 @@ jobs: steps: - name: Checkout this repository uses: actions/checkout@v2 + with: + lfs: true - name: Checkout actions repository uses: actions/checkout@v2 @@ -40,7 +42,8 @@ jobs: steps: - name: Checkout this repository uses: actions/checkout@v2 - + with: + lfs: true - name: Checkout actions repository uses: actions/checkout@v2 with: From d27aa0174f1702353bbc0bd06da389a62a6f6c06 Mon Sep 17 00:00:00 2001 From: adewyer Date: Fri, 21 Jan 2022 13:37:38 -0800 Subject: [PATCH 4/7] added function to get xyz file name from contents --- src/parsers/poscar.js | 7 +++++++ src/parsers/xyz.js | 16 ++++++++++++++++ tests/enums.js | 1 + tests/fixtures/Structure.xyz | 3 +++ tests/parsers/xyz.js | 10 +++++++++- 5 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 tests/fixtures/Structure.xyz diff --git a/src/parsers/poscar.js b/src/parsers/poscar.js index c0b3c563..07142222 100644 --- a/src/parsers/poscar.js +++ b/src/parsers/poscar.js @@ -63,6 +63,13 @@ export function atomsCount(poscarFileContent) { return atomsLine.map((x) => parseInt(x, 10)).reduce((a, b) => a + b); } +/** + * Function returns the string on the first line of POSCAR file content. Generally the first line contains text + * containing the name of the structure. + * @param {String} name + * @param {String} fileContent + * @returns {String} + */ export function materialNameFromFileContents(name, fileContent) { const materialNameFromFile = fileContent.split(/\r?\n/)[0]; if (name === materialNameFromFile || !materialNameFromFile) { diff --git a/src/parsers/xyz.js b/src/parsers/xyz.js index 20da6e54..767d80f7 100644 --- a/src/parsers/xyz.js +++ b/src/parsers/xyz.js @@ -140,10 +140,26 @@ function fromMaterial(materialOrConfig, fractional = false) { return fromBasis(basis, "%11.6f"); } +/** + * Function returns the string on the second line of XYZ file content. Generally the second line contains text + * containing the name of the structure. + * @param {String} name + * @param {String} fileContent + * @returns {String} + */ +export function materialNameFromFileContents(name, fileContent) { + const materialNameFromFile = fileContent.split(/\r?\n/)[1]; + if (name === materialNameFromFile || !materialNameFromFile) { + return name; + } + return materialNameFromFile; +} + export default { validate, fromMaterial, toBasisConfig, fromBasis, CombinatorialBasis, + materialNameFromFileContents, }; diff --git a/tests/enums.js b/tests/enums.js index 78740ead..8e0517f5 100644 --- a/tests/enums.js +++ b/tests/enums.js @@ -32,3 +32,4 @@ export const Zr1H23Zr1H1 = readJSONFile(path.join(FIXTURES_DIR, "Zr1H23Zr1H1.jso export const Zr1H23Zr1H1Poscar = readFile(path.join(FIXTURES_DIR, "Zr1H23Zr1H1.poscar")); export const H2O = readFile(path.join(FIXTURES_DIR, "H2O.poscar")); export const GenericPoscar = readFile(path.join(FIXTURES_DIR, "POSCAR.poscar")); +export const GenericXYZ = readFile(path.join(FIXTURES_DIR, "Structure.xyz")); diff --git a/tests/fixtures/Structure.xyz b/tests/fixtures/Structure.xyz new file mode 100644 index 00000000..90afc92c --- /dev/null +++ b/tests/fixtures/Structure.xyz @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:476366d715b7f4149bbfc56bd65a65bd2bcdfde9a2bc6b546322b6ad89204855 +size 195 diff --git a/tests/parsers/xyz.js b/tests/parsers/xyz.js index 6d685627..8e0da41e 100644 --- a/tests/parsers/xyz.js +++ b/tests/parsers/xyz.js @@ -1,5 +1,8 @@ +import { expect } from "chai"; + import parsers from "../../src/parsers/parsers"; -import { Si } from "../enums"; +import { materialNameFromFileContents } from "../../src/parsers/xyz"; +import { GenericXYZ, Si } from "../enums"; import { assertDeepAlmostEqual } from "../utils"; describe("Parsers:XYZ", () => { @@ -11,4 +14,9 @@ describe("Parsers:XYZ", () => { "units", ]); }); + it("should return the material name based on the xyz file contents", () => { + const name = "test.xyz"; + const fileContents = GenericXYZ; + expect(materialNameFromFileContents(name, fileContents)).to.be.equal("Methane"); + }); }); From efc4f7c9cf8078c0fd1f5819f012cdf16b11c79d Mon Sep 17 00:00:00 2001 From: adewyer Date: Tue, 25 Jan 2022 14:19:56 -0800 Subject: [PATCH 5/7] updated materialNameFromFileContent function --> getNameFromContents --- src/parsers/poscar.js | 15 +++++++-------- src/parsers/utils.js | 7 +++++++ src/parsers/xyz.js | 15 +++++++-------- tests/parsers/poscar.js | 5 ++--- tests/parsers/xyz.js | 5 ++--- 5 files changed, 25 insertions(+), 22 deletions(-) create mode 100644 src/parsers/utils.js diff --git a/src/parsers/poscar.js b/src/parsers/poscar.js index 07142222..c615374e 100644 --- a/src/parsers/poscar.js +++ b/src/parsers/poscar.js @@ -4,6 +4,7 @@ import { ConstrainedBasis } from "../basis/constrained_basis"; import { ATOMIC_COORD_UNITS } from "../constants"; import { Lattice } from "../lattice/lattice"; import math from "../math"; +import { getLineFromContent } from "./utils"; const _print = (x, printFormat = "%14.9f") => s.sprintf(printFormat, math.precise(x)); const _latticeVectorsToString = (vectors) => @@ -66,21 +67,19 @@ export function atomsCount(poscarFileContent) { /** * Function returns the string on the first line of POSCAR file content. Generally the first line contains text * containing the name of the structure. - * @param {String} name * @param {String} fileContent + * @param {String} failoverName * @returns {String} */ -export function materialNameFromFileContents(name, fileContent) { - const materialNameFromFile = fileContent.split(/\r?\n/)[0]; - if (name === materialNameFromFile || !materialNameFromFile) { - return name; - } - return materialNameFromFile; +export function getNameFromContents(fileContent, failoverName = "material") { + const nameFromContent = getLineFromContent(fileContent, 0); + if (nameFromContent) return nameFromContent; + return failoverName; } export default { toPoscar, atomicConstraintsCharFromBool, atomsCount, - materialNameFromFileContents, + getNameFromContents, }; diff --git a/src/parsers/utils.js b/src/parsers/utils.js new file mode 100644 index 00000000..8b1db558 --- /dev/null +++ b/src/parsers/utils.js @@ -0,0 +1,7 @@ +export function getLineFromContent(fileContent, lineNumber) { + return fileContent.split(/\r?\n/)[lineNumber]; +} + +export default { + getLineFromContent, +}; diff --git a/src/parsers/xyz.js b/src/parsers/xyz.js index 767d80f7..202780a2 100644 --- a/src/parsers/xyz.js +++ b/src/parsers/xyz.js @@ -6,6 +6,7 @@ import { ConstrainedBasis } from "../basis/constrained_basis"; import { Lattice } from "../lattice/lattice"; import math from "../math"; import { InvalidLineError } from "./errors"; +import { getLineFromContent } from "./utils"; import { CombinatorialBasis } from "./xyz_combinatorial_basis"; // Regular expression for an XYZ line with atomic constraints, eg. Si 0.000000 0.500000 0.446678 1 1 1` @@ -143,16 +144,14 @@ function fromMaterial(materialOrConfig, fractional = false) { /** * Function returns the string on the second line of XYZ file content. Generally the second line contains text * containing the name of the structure. - * @param {String} name * @param {String} fileContent + * @param {String} failoverName * @returns {String} */ -export function materialNameFromFileContents(name, fileContent) { - const materialNameFromFile = fileContent.split(/\r?\n/)[1]; - if (name === materialNameFromFile || !materialNameFromFile) { - return name; - } - return materialNameFromFile; +export function getNameFromContents(fileContent, failoverName = "material") { + const nameFromContent = getLineFromContent(fileContent, 1); + if (nameFromContent) return nameFromContent; + return failoverName; } export default { @@ -161,5 +160,5 @@ export default { toBasisConfig, fromBasis, CombinatorialBasis, - materialNameFromFileContents, + getNameFromContents, }; diff --git a/tests/parsers/poscar.js b/tests/parsers/poscar.js index eb466531..ed378c9a 100644 --- a/tests/parsers/poscar.js +++ b/tests/parsers/poscar.js @@ -1,7 +1,7 @@ import { expect } from "chai"; import { Material } from "../../src/material"; -import { atomsCount, materialNameFromFileContents } from "../../src/parsers/poscar"; +import { atomsCount, getNameFromContents } from "../../src/parsers/poscar"; import { GenericPoscar, H2O, Na4Cl4, Na4Cl4Poscar, Zr1H23Zr1H1, Zr1H23Zr1H1Poscar } from "../enums"; describe("Parsers.POSCAR", () => { @@ -19,8 +19,7 @@ describe("Parsers.POSCAR", () => { expect(atomsCount(H2O)).to.be.equal(3); }); it("should return the material name based on the poscar file contents", () => { - const name = "POSCAR.poscar"; const fileContents = GenericPoscar; - expect(materialNameFromFileContents(name, fileContents)).to.be.equal("H2S"); + expect(getNameFromContents(fileContents)).to.be.equal("H2S"); }); }); diff --git a/tests/parsers/xyz.js b/tests/parsers/xyz.js index 8e0da41e..7da227b3 100644 --- a/tests/parsers/xyz.js +++ b/tests/parsers/xyz.js @@ -1,7 +1,7 @@ import { expect } from "chai"; import parsers from "../../src/parsers/parsers"; -import { materialNameFromFileContents } from "../../src/parsers/xyz"; +import { getNameFromContents } from "../../src/parsers/xyz"; import { GenericXYZ, Si } from "../enums"; import { assertDeepAlmostEqual } from "../utils"; @@ -15,8 +15,7 @@ describe("Parsers:XYZ", () => { ]); }); it("should return the material name based on the xyz file contents", () => { - const name = "test.xyz"; const fileContents = GenericXYZ; - expect(materialNameFromFileContents(name, fileContents)).to.be.equal("Methane"); + expect(getNameFromContents(fileContents)).to.be.equal("Methane"); }); }); From 64be7d5465513c19940baf7e0ecb55d0c60dbc3d Mon Sep 17 00:00:00 2001 From: adewyer Date: Tue, 25 Jan 2022 17:27:30 -0800 Subject: [PATCH 6/7] removed double export in parsers/utils.js, and updated function to returning number of atoms line to trim whitespace. --- src/parsers/utils.js | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/parsers/utils.js b/src/parsers/utils.js index 8b1db558..7103121e 100644 --- a/src/parsers/utils.js +++ b/src/parsers/utils.js @@ -1,7 +1,4 @@ export function getLineFromContent(fileContent, lineNumber) { - return fileContent.split(/\r?\n/)[lineNumber]; + const lineString = fileContent.split(/\r?\n/)[lineNumber]; + return lineString.trim(); } - -export default { - getLineFromContent, -}; From 8064efaedbf66e07f19a0636a371eef8f3f2b59e Mon Sep 17 00:00:00 2001 From: adewyer Date: Tue, 25 Jan 2022 17:40:03 -0800 Subject: [PATCH 7/7] adjusted logic for getting file name from contents --- src/parsers/parsers.js | 22 ++++++++++++++++++++++ src/parsers/poscar.js | 15 --------------- src/parsers/xyz.js | 15 --------------- tests/parsers/poscar.js | 6 +++--- tests/parsers/xyz.js | 6 ++---- 5 files changed, 27 insertions(+), 37 deletions(-) diff --git a/src/parsers/parsers.js b/src/parsers/parsers.js index 2e9924c3..eaa164dc 100644 --- a/src/parsers/parsers.js +++ b/src/parsers/parsers.js @@ -1,11 +1,33 @@ import cif from "./cif"; import espresso from "./espresso"; import poscar from "./poscar"; +import { getLineFromContent } from "./utils"; import xyz from "./xyz"; +/** + * Function returns the of the file that should contain file contents based on the fileExtension value + * @param {String} fileContent + * @param {String} fileExtension + * @param {String} failoverName + * @returns {String} + */ +export function getNameFromContents(fileContent, fileExtension, failoverName = "material") { + let lineNumber; + if (fileExtension === "poscar") { + lineNumber = 0; + } + if (fileExtension === "xyz") { + lineNumber = 1; + } + const nameFromContent = getLineFromContent(fileContent, lineNumber); + if (nameFromContent) return nameFromContent; + return failoverName; +} + export default { xyz, poscar, cif, espresso, + getNameFromContents, }; diff --git a/src/parsers/poscar.js b/src/parsers/poscar.js index c615374e..17e0244e 100644 --- a/src/parsers/poscar.js +++ b/src/parsers/poscar.js @@ -4,7 +4,6 @@ import { ConstrainedBasis } from "../basis/constrained_basis"; import { ATOMIC_COORD_UNITS } from "../constants"; import { Lattice } from "../lattice/lattice"; import math from "../math"; -import { getLineFromContent } from "./utils"; const _print = (x, printFormat = "%14.9f") => s.sprintf(printFormat, math.precise(x)); const _latticeVectorsToString = (vectors) => @@ -64,22 +63,8 @@ export function atomsCount(poscarFileContent) { return atomsLine.map((x) => parseInt(x, 10)).reduce((a, b) => a + b); } -/** - * Function returns the string on the first line of POSCAR file content. Generally the first line contains text - * containing the name of the structure. - * @param {String} fileContent - * @param {String} failoverName - * @returns {String} - */ -export function getNameFromContents(fileContent, failoverName = "material") { - const nameFromContent = getLineFromContent(fileContent, 0); - if (nameFromContent) return nameFromContent; - return failoverName; -} - export default { toPoscar, atomicConstraintsCharFromBool, atomsCount, - getNameFromContents, }; diff --git a/src/parsers/xyz.js b/src/parsers/xyz.js index 202780a2..20da6e54 100644 --- a/src/parsers/xyz.js +++ b/src/parsers/xyz.js @@ -6,7 +6,6 @@ import { ConstrainedBasis } from "../basis/constrained_basis"; import { Lattice } from "../lattice/lattice"; import math from "../math"; import { InvalidLineError } from "./errors"; -import { getLineFromContent } from "./utils"; import { CombinatorialBasis } from "./xyz_combinatorial_basis"; // Regular expression for an XYZ line with atomic constraints, eg. Si 0.000000 0.500000 0.446678 1 1 1` @@ -141,24 +140,10 @@ function fromMaterial(materialOrConfig, fractional = false) { return fromBasis(basis, "%11.6f"); } -/** - * Function returns the string on the second line of XYZ file content. Generally the second line contains text - * containing the name of the structure. - * @param {String} fileContent - * @param {String} failoverName - * @returns {String} - */ -export function getNameFromContents(fileContent, failoverName = "material") { - const nameFromContent = getLineFromContent(fileContent, 1); - if (nameFromContent) return nameFromContent; - return failoverName; -} - export default { validate, fromMaterial, toBasisConfig, fromBasis, CombinatorialBasis, - getNameFromContents, }; diff --git a/tests/parsers/poscar.js b/tests/parsers/poscar.js index ed378c9a..47c29ad8 100644 --- a/tests/parsers/poscar.js +++ b/tests/parsers/poscar.js @@ -1,7 +1,8 @@ import { expect } from "chai"; import { Material } from "../../src/material"; -import { atomsCount, getNameFromContents } from "../../src/parsers/poscar"; +import { getNameFromContents } from "../../src/parsers/parsers"; +import { atomsCount } from "../../src/parsers/poscar"; import { GenericPoscar, H2O, Na4Cl4, Na4Cl4Poscar, Zr1H23Zr1H1, Zr1H23Zr1H1Poscar } from "../enums"; describe("Parsers.POSCAR", () => { @@ -19,7 +20,6 @@ describe("Parsers.POSCAR", () => { expect(atomsCount(H2O)).to.be.equal(3); }); it("should return the material name based on the poscar file contents", () => { - const fileContents = GenericPoscar; - expect(getNameFromContents(fileContents)).to.be.equal("H2S"); + expect(getNameFromContents(GenericPoscar, "poscar")).to.be.equal("H2S"); }); }); diff --git a/tests/parsers/xyz.js b/tests/parsers/xyz.js index 7da227b3..b63f614c 100644 --- a/tests/parsers/xyz.js +++ b/tests/parsers/xyz.js @@ -1,7 +1,6 @@ import { expect } from "chai"; -import parsers from "../../src/parsers/parsers"; -import { getNameFromContents } from "../../src/parsers/xyz"; +import parsers, { getNameFromContents } from "../../src/parsers/parsers"; import { GenericXYZ, Si } from "../enums"; import { assertDeepAlmostEqual } from "../utils"; @@ -15,7 +14,6 @@ describe("Parsers:XYZ", () => { ]); }); it("should return the material name based on the xyz file contents", () => { - const fileContents = GenericXYZ; - expect(getNameFromContents(fileContents)).to.be.equal("Methane"); + expect(getNameFromContents(GenericXYZ, "xyz")).to.be.equal("Methane"); }); });