forked from stevage/opentrees-data
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3a-updateTaxoCount.js
executable file
·34 lines (30 loc) · 1.3 KB
/
3a-updateTaxoCount.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
#!/usr/bin/env node --max-old-space-size=8192
const fs = require('fs');
const ndjson = require('ndjson');
const taxoCount = { class: {}, subclass: {}, family: {}, genus: {}, species: {} };
function countSpecies({properties:tree}) {
if (tree.genus && tree.species) {
taxoCount.species[tree.genus + ' ' + tree.species] = 1 + (taxoCount.species[tree.genus + ' ' + tree.species] || 0)
}
if (tree.family) {
taxoCount.class [tree.class] = 1 + (taxoCount.class[tree.class] || 0);
taxoCount.subclass [tree.subclass] = 1 + (taxoCount.subclass[tree.subclass] || 0);
taxoCount.family[tree.family] = 1 + (taxoCount.family[tree.family] || 0);
}
}
fs.createReadStream(`tmp/allout.json`)
.pipe(ndjson.parse())
.on('data', countSpecies)
.on('end', () => {
fs.writeFile('taxoCount.json', JSON.stringify(taxoCount), () => 'taxoCount.json written')
for (let type of ['class','subclass','family','species']) {
console.log(`*** ${type} ***`);
console.log(
Object.keys(taxoCount[type])
.sort((a, b) => taxoCount[type][b] - taxoCount[type][a])
.slice(0, 20)
.map(k => k + ': ' + taxoCount[type][k])
.join('\n')
);
}
});