-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcountMembersByTaxonomy.js
More file actions
executable file
·91 lines (79 loc) · 2.19 KB
/
countMembersByTaxonomy.js
File metadata and controls
executable file
·91 lines (79 loc) · 2.19 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env node
var through2 = require('through2');
var byline = require('byline');
var fs = require('fs');
var argv = require('minimist')(process.argv.slice(2));
var url = argv.swagger || 'https://data.gramene.org/vitis1/swagger';
var idFile = argv.ids;
var taxa;
global.gramene = {defaultServer: url};
var gramene = require('gramene-search-client').client.grameneClient;
let GrameneTrees = require('gramene-trees-client');
var reader = byline(fs.createReadStream(idFile));
var treeFetcher = through2.obj(function(id, enc, done) {
var that = this;
id = id.toString();
gramene.then(function(client) {
client['Data access']['genetrees']({idList:[id],rows:-1}).then(function(res) {
that.push(res.obj);
done();
})
})
});
function walkTo(node, test, process) {
if (test(node)) {
process(node);
}
else {
if (node.hasChildren()) {
node.children.forEach(function(childNode) {
walkTo(childNode, test, process)
})
}
}
}
var checkTree = function checkTree() {
var results = [];
var cols = ['tree','taxa'].concat(taxa);
results.push(cols.join("\t"));
var transform = function(tree, enc, done) {
let genetree = GrameneTrees.genetree.tree(tree);
let tally = {};
taxa.forEach(tid => tally[tid]=0);
walkTo(
genetree,
node => (!node.children || node.children.length === 0 || node.model.node_type === "gene_split") && tally.hasOwnProperty(node.model.taxon_id),
node => tally[node.model.taxon_id]++
);
let row = [];
row.push(genetree._id);
row.push(0); // number of taxa present
for(let i=0;i<taxa.length;i++) {
let tid = taxa[i];
row.push(tally[tid]);
if (tally[tid] > 0) {
row[1]++;
}
}
if (row[1] > 0) {
results.push(row.join("\t"));
}
done();
};
var flush = function(done) {
this.push(results.join("\n"));
done();
}
return through2.obj(transform, flush);
};
gramene.then(function(client) {
client['Data access']['maps']({rows:-1,fl:'taxon_id,left_index'}).then(res => {
let maps = res.obj;
maps.sort((a, b) => a.left_index < b.left_index);
taxa = maps.map(m => m.taxon_id);
reader
.pipe(treeFetcher)
.pipe(checkTree())
.pipe(process.stdout);
})
});