-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy path3b-addSpeciesCounts.js
executable file
·39 lines (31 loc) · 1.14 KB
/
3b-addSpeciesCounts.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
#!/usr/bin/env node --max-old-space-size=8192
const fs = require('fs');
const ndjson = require('ndjson');
const taxoCount = require('./taxoCount.json');
const perf = require('execution-time')();
const child_process = require('child_process');
function addSpeciesCount(tree) {
if (tree.genus && tree.species) {
tree.species_count = taxoCount.species[tree.genus + ' ' + tree.species];
}
}
async function process() {
const out = fs.createWriteStream('tmp/allout-species.json').on('error', console.error);
console.log('Adding species counts.');
perf.start('process');
fs.createReadStream(`tmp/allout.json`)
.pipe(ndjson.parse())
.on('data', tree => {
addSpeciesCount(tree.properties);
out.write(JSON.stringify(tree) + '\n');
})
.on('error', e => {
console.error(e)
})
.on('end', () => {
console.log(`Added species counts in ${perf.stop('process').words}, renaming files.`);
child_process.execSync('mv tmp/allout.json tmp/allout-without-species.json');
child_process.execSync('mv tmp/allout-species.json tmp/allout.json');
})
}
process();