-
Notifications
You must be signed in to change notification settings - Fork 0
/
blastQueue.js
68 lines (64 loc) · 2.92 KB
/
blastQueue.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
const spawn = require('child_process').spawn;
const config = require('./config');
const async = require('async');
const fs = require('fs');
const {getFastaFromRequest} = require('./util.js')
/*
This is version of the blastQueue that have been used since the seq id tool was launched.
It uses child_process.spawn without opening shells.
It needs to write the query sequence to a fasta file which is passed to blastn.
When the query completes, the file is removed.
The query sequence files are written to BLAST_SEQ_PATH , and the process needs to have RW access to this directory.
*/
const blastQueue = async.queue(function(options, callback) {
// const fasta = _.isArray(options.seq) ? : '>' + options.req_id + '\n' + options.seq
fs.writeFile(config.BLAST_SEQ_PATH + options.filename, getFastaFromRequest(options.seq, options.resultArray), 'utf-8', function(e) {
if (e) {
callback(e, null);
} else {
// blastn -db /Users/thomas/unite -query /Users/thomas/blast/seq/test.fasta -outfmt "6 qseqid sseqid pident length evalue bitscore qseq sseq" -max_target_seqs 2
let params = ['-query', config.BLAST_SEQ_PATH + options.filename,
'-db', config.BLAST_DATABASE_PATH + config.DATABASE_NAME[options.marker],
'-outfmt', '6 sseqid pident length evalue bitscore qseq sseq qstart qend sstart send qcovs qseqid', // 6,
'-max_target_seqs', !isNaN(parseInt(options.max_target_seqs)) && parseInt(options.max_target_seqs) <= config.LIMIT_MAX_TARGET_SEQS ? options.max_target_seqs : config.MAX_TARGET_SEQS,
'-num_threads', config.NUM_THREADS,
'-qcov_hsp_perc', config.MINIMUM_QUERY_COVER,
'-max_hsps', 1
];
if(options.perc_identity){
params = [...params, '-perc_identity', options.perc_identity]
}
let pcs = spawn('blastn',
params,
{stdio: [0, 'pipe', 0]});
let string = '';
pcs.on('error',
function(e) {
callback(e, null);
console.log(e);
});
if (pcs.stdout) {
pcs.stdout.on('data', function(chunk) {
let part = chunk.toString();
string += part;
});
pcs.stdout.on('end', function() {
fs.unlink(config.BLAST_SEQ_PATH + options.filename, function(e1) {
if (e1) {
console.log('Failed to remove seq file: ' + options.filename);
}
});
callback(null, string);
pcs.stdout.destroy();
});
}
if (pcs.stderr) {
pcs.stderr.destroy();
}
if (pcs.stdin) {
pcs.stdin.destroy();
}
}
});
}, config.NUM_CONCURRENT_PROCESSES);
module.exports = blastQueue;