-
Notifications
You must be signed in to change notification settings - Fork 1
/
calcTaxonDistance.js
39 lines (37 loc) · 1.25 KB
/
calcTaxonDistance.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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;
}