Skip to content

Commit

Permalink
First stab at normalize() static methods for all wrappers.
Browse files Browse the repository at this point in the history
  • Loading branch information
gaurav committed Jan 17, 2024
1 parent 002f472 commit e2b9c64
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 2 deletions.
21 changes: 20 additions & 1 deletion src/wrappers/CitationWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Based on BibJSON (http://okfnlabs.org/bibjson/).
*/

const { has, isEmpty } = require('lodash');
const { has, isEmpty, cloneDeep } = require('lodash');

Check failure on line 6 in src/wrappers/CitationWrapper.js

View workflow job for this annotation

GitHub Actions / build (18.x)

'cloneDeep' is assigned a value but never used

Check failure on line 6 in src/wrappers/CitationWrapper.js

View workflow job for this annotation

GitHub Actions / build (20.x)

'cloneDeep' is assigned a value but never used

Check failure on line 6 in src/wrappers/CitationWrapper.js

View workflow job for this annotation

GitHub Actions / build (20.x)

'cloneDeep' is assigned a value but never used

class CitationWrapper {
/**
Expand All @@ -13,6 +13,25 @@ class CitationWrapper {
this.citation = citation;
}

/**
* Return a normalized form of a citation.
*
* I'm not really sure how to normalize a citation, but the main thing we can do is delete any key
* that is equivalent to ''. We could interconvert between `name` and `firstname/lastname/middlename`,

Check failure on line 20 in src/wrappers/CitationWrapper.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Line 20 exceeds the maximum line length of 100

Check failure on line 20 in src/wrappers/CitationWrapper.js

View workflow job for this annotation

GitHub Actions / build (20.x)

Line 20 exceeds the maximum line length of 100

Check failure on line 20 in src/wrappers/CitationWrapper.js

View workflow job for this annotation

GitHub Actions / build (20.x)

Line 20 exceeds the maximum line length of 100
* but that's not really equivalent, is it?
*/
static normalize(citation) {
const normalizedCitation = {};
Object.keys(citation).forEach((key) => {
// As long as citation[key] has a reasonable value, we copy it into the normalized citation.
if (citation[key]) {
normalizedCitation[key] = citation[key];
}
});
return normalizedCitation;
}


/**
* Helper method to return a single name for a given agent entry.
* The algorithm we use is:
Expand Down
20 changes: 19 additions & 1 deletion src/wrappers/PhylogenyWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
* PhylogenyWrapper
*/

const { has } = require('lodash');
const { has,

Check failure on line 5 in src/wrappers/PhylogenyWrapper.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Expected a line break after this opening brace

Check failure on line 5 in src/wrappers/PhylogenyWrapper.js

View workflow job for this annotation

GitHub Actions / build (20.x)

Expected a line break after this opening brace

Check failure on line 5 in src/wrappers/PhylogenyWrapper.js

View workflow job for this annotation

GitHub Actions / build (20.x)

Expected a line break after this opening brace
cloneDeep

Check failure on line 6 in src/wrappers/PhylogenyWrapper.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Missing trailing comma

Check failure on line 6 in src/wrappers/PhylogenyWrapper.js

View workflow job for this annotation

GitHub Actions / build (20.x)

Missing trailing comma

Check failure on line 6 in src/wrappers/PhylogenyWrapper.js

View workflow job for this annotation

GitHub Actions / build (20.x)

Missing trailing comma
} = require('lodash');

/** Used to parse Newick strings. */
const newickJs = require('newick-js');
Expand Down Expand Up @@ -34,6 +36,22 @@ class PhylogenyWrapper {
this.defaultNomenCode = defaultNomenCode;
}

/**
* Return a normalized form of the phylogeny.
*/
static normalize(phylogeny) {
const normalizedPhylogeny = cloneDeep(phylogeny);

// We could normalize the Newick string, but that doesn't seem very nice.

// Normalize the source if there is one.
if ('source' in phylogeny) {
normalizedPhylogeny.source = CitationWrapper.normalize(phylogeny.source || {});
}

return normalizedPhylogeny;
}

static getErrorsInNewickString(newick) {
// Given a Newick string, return a list of errors found in parsing this
// string. The errors are returned as a list of objects, each of which
Expand Down
14 changes: 14 additions & 0 deletions src/wrappers/PhylorefWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,20 @@ class PhylorefWrapper {
return this.phyloref.internalSpecifiers;
}

/**
* Normalize a phyloreference.
*
* @param phyloref
*/
static normalize(phyloref) {
const normalizedPhyloref = cloneDeep(phyloref);

normalizedPhyloref.internalSpecifiers = (phyloref.internalSpecifiers || []).map(TaxonomicUnitWrapper.normalize);

Check failure on line 44 in src/wrappers/PhylorefWrapper.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Line 44 exceeds the maximum line length of 100

Check failure on line 44 in src/wrappers/PhylorefWrapper.js

View workflow job for this annotation

GitHub Actions / build (20.x)

Line 44 exceeds the maximum line length of 100

Check failure on line 44 in src/wrappers/PhylorefWrapper.js

View workflow job for this annotation

GitHub Actions / build (20.x)

Line 44 exceeds the maximum line length of 100
normalizedPhyloref.externalSpecifiers = (phyloref.internalSpecifiers || []).map(TaxonomicUnitWrapper.normalize);

Check failure on line 45 in src/wrappers/PhylorefWrapper.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Line 45 exceeds the maximum line length of 100

Check failure on line 45 in src/wrappers/PhylorefWrapper.js

View workflow job for this annotation

GitHub Actions / build (20.x)

Line 45 exceeds the maximum line length of 100

Check failure on line 45 in src/wrappers/PhylorefWrapper.js

View workflow job for this annotation

GitHub Actions / build (20.x)

Line 45 exceeds the maximum line length of 100

return normalizedPhyloref;
}

/** Return the external specifiers of this phyloref (if any). */
get externalSpecifiers() {
if (!has(this.phyloref, 'externalSpecifiers')) {
Expand Down
24 changes: 24 additions & 0 deletions src/wrappers/PhyxWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,30 @@ class PhyxWrapper {
return owlterms.UNKNOWN_CODE;
}

/**
* Return a provided Phyx document as a normalized JSON document. We ignore most keys -- including
* keys we don't know -- but any key that can be wrapped by one of the other Wrappers in this package

Check failure on line 56 in src/wrappers/PhyxWrapper.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Line 56 exceeds the maximum line length of 100

Check failure on line 56 in src/wrappers/PhyxWrapper.js

View workflow job for this annotation

GitHub Actions / build (20.x)

Line 56 exceeds the maximum line length of 100

Check failure on line 56 in src/wrappers/PhyxWrapper.js

View workflow job for this annotation

GitHub Actions / build (20.x)

Line 56 exceeds the maximum line length of 100
* will be wrapped and normalized before being returned.
*
* Normalization is mostly needed for TaxonomicUnitWrappers and its subclasses (TaxonConceptWrapper,

Check failure on line 59 in src/wrappers/PhyxWrapper.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Line 59 exceeds the maximum line length of 100

Check failure on line 59 in src/wrappers/PhyxWrapper.js

View workflow job for this annotation

GitHub Actions / build (20.x)

Line 59 exceeds the maximum line length of 100

Check failure on line 59 in src/wrappers/PhyxWrapper.js

View workflow job for this annotation

GitHub Actions / build (20.x)

Line 59 exceeds the maximum line length of 100
* TaxonNameWrapper), since these can be represented in several essentially identical ways. But if we

Check failure on line 60 in src/wrappers/PhyxWrapper.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Line 60 exceeds the maximum line length of 100

Check failure on line 60 in src/wrappers/PhyxWrapper.js

View workflow job for this annotation

GitHub Actions / build (20.x)

Line 60 exceeds the maximum line length of 100

Check failure on line 60 in src/wrappers/PhyxWrapper.js

View workflow job for this annotation

GitHub Actions / build (20.x)

Line 60 exceeds the maximum line length of 100
* implement it at every level, we can implement comparison code in Klados easily.
*
* Two Phyx documents should -- upon being normalized -- be comparable with each other with
* lodash.deepEqual().
*/
static normalize(phyxDocument) {
const normalizedDocument = cloneDeep(phyxDocument);

normalizedDocument.phylorefs = (phyxDocument.phylorefs || []).map(PhylorefWrapper.normalize);
normalizedDocument.phylogenies = (phyxDocument.phylogenies || []).map(PhylogenyWrapper.normalize);

Check failure on line 70 in src/wrappers/PhyxWrapper.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Line 70 exceeds the maximum line length of 100

Check failure on line 70 in src/wrappers/PhyxWrapper.js

View workflow job for this annotation

GitHub Actions / build (20.x)

Line 70 exceeds the maximum line length of 100

Check failure on line 70 in src/wrappers/PhyxWrapper.js

View workflow job for this annotation

GitHub Actions / build (20.x)

Line 70 exceeds the maximum line length of 100
if ('source' in phyxDocument) {
normalizedDocument.source = CitationWrapper.normalize(phyxDocument.source);
}

return normalizedDocument;
}

/**
* Generate an executable ontology from this Phyx document. The document is mostly in JSON-LD
* already, except for three important things:
Expand Down

0 comments on commit e2b9c64

Please sign in to comment.