-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstreamMsa.js
69 lines (59 loc) · 1.9 KB
/
streamMsa.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
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
var ncbi = require('bionode-ncbi');
var es = require('event-stream');
var filter = require('through2-filter');
var concat = require('concat-stream');
var tool = require('tool-stream');
var cp = require('child_process');
var ndjson = require('ndjson');
// Only supports one level deep property
// i.e. car['wheels'] and not car['wheels.tire']
// for that, do car.wheels['tire']
function propMatchRegex(obj, prop, regex) {
return obj[prop].match(regex);
}
function getProteinSeqs(req, res, next) {
var opts = req.opts;
// var species = [];
var rMSA = cp.spawn('/Users/jmazz/r/js-bioinformatics-exercise/msa.r');
var stream = ncbi.search('protein', opts.query);
opts.filters.forEach(function (f) {
stream = stream.pipe(filter.obj(f));
});
if (opts.uniqueSpecies) {
// This will actually belong to scope of function
var species=[];
stream = stream
.pipe(filter.obj(function (obj) {
var specieName = obj.title.substring(obj.title.indexOf('[') + 1, obj.title.length-1);
specieName = specieName.split(' ').slice(0,1).join(' ');
if (species.indexOf(specieName) >= 0) {
return false;
} else {
species.push(specieName);
return true;
}
}));
}
stream
.pipe(tool.extractProperty('gi'))
.pipe(ncbi.fetch('protein'))
.pipe(es.through(function (obj) {
this.emit('data', JSON.stringify(obj) + '\n');
}))
.pipe(rMSA.stdin);
var seqs=[];
rMSA.stdout
.pipe(ndjson.parse())
.on('data', function(data) {
seqs.push(data);
})
.on('end', function() {
res.send({
seqs: seqs
});
});
}
module.exports = {
getProteinSeqs: getProteinSeqs,
propMatchRegex: propMatchRegex
};