Skip to content

Commit

Permalink
taxon distance
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhouyang Lian committed Nov 4, 2020
1 parent 7c3b37a commit 10b9d9d
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/utils/calcTaxonDistance.js
@@ -0,0 +1,39 @@
static calcTaxonomicDistance(taxonDistance, targetSpecies, measuredSpecies) {
let distance = null;

targetSpecies = targetSpecies.toLowerCase();
measuredSpecies = measuredSpecies.toLowerCase();
taxonDistance = Object.assign({}, taxonDistance);
for (const key in taxonDistance) {
taxonDistance[key.toLowerCase()] = taxonDistance[key];
}

if (
targetSpecies === measuredSpecies ||
measuredSpecies.startsWith(targetSpecies)
) {
distance = 0;
} else if (
targetSpecies + "_canon_ancestors" in taxonDistance &&
measuredSpecies + "_canon_ancestors" in taxonDistance
) {
const toAncestors = taxonDistance[targetSpecies + "_canon_ancestors"];
const fromAncestors = taxonDistance[measuredSpecies + "_canon_ancestors"];
toAncestors.push(targetSpecies);
fromAncestors.push(measuredSpecies);
distance = 0;
for (
let iLineage = 0;
iLineage < Math.min(toAncestors.length, fromAncestors.length);
iLineage++
) {
if (toAncestors[iLineage] !== fromAncestors[iLineage]) {
distance = toAncestors.length - iLineage;
break;
}
}
toAncestors.pop();
fromAncestors.pop();
} else {
distance = null;
}

0 comments on commit 10b9d9d

Please sign in to comment.