Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .eslintignore

This file was deleted.

28 changes: 0 additions & 28 deletions .eslintrc.json

This file was deleted.

3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Don't include Claude config.
.claude

# Don't save any JPhyloRef files into GitHub.
/test/jphyloref-*.jar

Expand Down
95 changes: 57 additions & 38 deletions bin/phyx2owl.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node

const fs = require('fs');
const path = require('path');
const fs = require('node:fs');
const path = require('node:path');
const phyx = require('..');

/*
Expand All @@ -10,21 +10,29 @@ const phyx = require('..');

// Read command line arguments.
const argv = require('yargs')
.usage("$0 [files or directories to convert into OWL ontologies]")
.describe('max-internal-specifiers', 'The maximum number of internal specifiers (phylorefs with more than this number will be ignored)')
.usage('$0 [files or directories to convert into OWL ontologies]')
.describe(
'max-internal-specifiers',
'The maximum number of internal specifiers (phylorefs with more than this number will be ignored)',
)
.default('max-internal-specifiers', 8)
.describe('max-external-specifiers', 'The maximum number of external specifiers (phylorefs with more than this number will be ignored)')
.describe(
'max-external-specifiers',
'The maximum number of external specifiers (phylorefs with more than this number will be ignored)',
)
.default('max-external-specifiers', 8)
.describe('base-iri', 'The base IRI to use for the input files')
.help()
.alias('h', 'help')
.argv
.alias('h', 'help').argv;

/*
* Get a list of all files in a directory. We will recurse into directories and choose
* files that meet the criteria in the function `check(filename) => boolean`.
*/
function getFilesInDir(filePath, check = (filename => filename.toLowerCase().endsWith(".json"))) {
function getFilesInDir(
filePath,
check = filename => filename.toLowerCase().endsWith('.json'),
) {
// console.debug(`Processing file: ${filePath}`)
if (!fs.existsSync(filePath)) return [];

Expand All @@ -41,7 +49,8 @@ function getFilesInDir(filePath, check = (filename => filename.toLowerCase().end
} else if (lsync.isDirectory()) {
// If `path` is a directory, recurse into every file in that directory.
const files = fs.readdirSync(filePath);
return files.map(file => getFilesInDir(path.join(filePath, file), check))
return files
.map(file => getFilesInDir(path.join(filePath, file), check))
.reduce((acc, curr) => acc.concat(curr), []);
} else {
// console.debug(`${filePath} is neither a file nor a directory; skipping.`);
Expand All @@ -53,7 +62,7 @@ function getFilesInDir(filePath, check = (filename => filename.toLowerCase().end
// At this point, we convert directories into lists of files.
const filenames = argv._;
if (filenames.length === 0) {
console.error("No input files provided.");
console.error('No input files provided.');
process.exit(1);
}

Expand All @@ -63,7 +72,9 @@ const files = filenames
// console.debug(`Files to process: ${files.join(", ")}`);

if (files.length === 0) {
console.error(`Input files do not exist or consist of directories that do not contain JSON files: ${filenames.join(', ')}`);
console.error(
`Input files do not exist or consist of directories that do not contain JSON files: ${filenames.join(', ')}`,
);
process.exit(1);
}

Expand All @@ -73,20 +84,20 @@ if (files.length === 0) {
* filename: either by replacing '.json' with '.owl', or by concatenating
* '.owl' at the end.
*/
function convertFileToOWL(filename, argOutputFilename = "") {
function convertFileToOWL(filename, argOutputFilename = '') {
// console.debug(`Starting with ${filename}.`);
let outputFilename;
if (argOutputFilename != "") {
if (argOutputFilename !== '') {
outputFilename = argOutputFilename;
} else if (filename.toLowerCase().endsWith(".json")) {
outputFilename = filename.substring(0, filename.length - 5) + ".owl";
} else if (filename.toLowerCase().endsWith('.json')) {
outputFilename = `${filename.substring(0, filename.length - 5)}.owl`;
} else {
outputFilename = filename + ".owl";
outputFilename = `${filename}.owl`;
}

try {
// Parse the input file into JSON.
let phyxContent = JSON.parse(fs.readFileSync(filename));
const phyxContent = JSON.parse(fs.readFileSync(filename));

// Remove any phylorefs that have too many specifiers.
const phylorefCount = (phyxContent.phylorefs || []).length;
Expand All @@ -95,10 +106,14 @@ function convertFileToOWL(filename, argOutputFilename = "") {
const internalSpecifiersCount = wrappedPhyloref.internalSpecifiers.length;
const externalSpecifiersCount = wrappedPhyloref.externalSpecifiers.length;
if (internalSpecifiersCount > argv.maxInternalSpecifiers) {
console.warn(`Phyloreference ${wrappedPhyloref.label} was skipped, since it has ${internalSpecifiersCount} internal specifiers.`);
console.warn(
`Phyloreference ${wrappedPhyloref.label} was skipped, since it has ${internalSpecifiersCount} internal specifiers.`,
);
return false;
} else if (externalSpecifiersCount > argv.maxExternalSpecifiers) {
console.warn(`Phyloreference ${wrappedPhyloref.label} was skipped, since it has ${externalSpecifiersCount} external specifiers.`);
console.warn(
`Phyloreference ${wrappedPhyloref.label} was skipped, since it has ${externalSpecifiersCount} external specifiers.`,
);
return false;
}
return true;
Expand All @@ -107,41 +122,45 @@ function convertFileToOWL(filename, argOutputFilename = "") {

// Convert the Phyx file into JSON-LD.
const wrappedPhyx = new phyx.PhyxWrapper(phyxContent);
wrappedPhyx.toRDF(argv.baseIri, path.dirname(filename))
wrappedPhyx
.toRDF(argv.baseIri, path.dirname(filename))
.then(nquads => {
fs.writeFileSync(
outputFilename,
nquads
);
fs.writeFileSync(outputFilename, nquads);
})
.catch(err => {
throw err;
});

// Report on whether any phyloreferences were converted.
if (filteredPhylorefs.length == 0) {
console.warn(`No phyloreferences in ${filename} were converted to ${outputFilename}, as they were all filtered out.`);
return false;
if (filteredPhylorefs.length === 0) {
console.warn(
`No phyloreferences in ${filename} were converted to ${outputFilename}, as they were all filtered out.`,
);
return false;
} else if (phylorefCount > filteredPhylorefs.length) {
console.warn(`Only ${filteredPhylorefs.length} out of ${phylorefCount} were converted from ${filename} to ${outputFilename}.`);
return true;
console.warn(
`Only ${filteredPhylorefs.length} out of ${phylorefCount} were converted from ${filename} to ${outputFilename}.`,
);
return true;
} else {
console.info(`Converted ${filename} to ${outputFilename}.`);
return true;
console.info(`Converted ${filename} to ${outputFilename}.`);
return true;
}

return true;
} catch(e) {
console.error(`Could not convert ${filename} to ${outputFilename}: ${e} at ${e.stack}`);
console.error(``)
} catch (e) {
console.error(
`Could not convert ${filename} to ${outputFilename}: ${e} at ${e.stack}`,
);
console.error(``);
}
return false;
}

// Count and report all the successes in converting files to OWL.
const successes = files.map(file => convertFileToOWL(file));
if(successes.every(x => x)) {
if (successes.every(x => x)) {
console.log(`${successes.length} files converted successfully.`);
} else {
console.log(`Errors occurred; ${successes.filter(x => x).length} files converted successfully, ${successes.filter(x => !x).length} files failed.`);
console.log(
`Errors occurred; ${successes.filter(x => x).length} files converted successfully, ${successes.filter(x => !x).length} files failed.`,
);
}
Loading