bionode-fasta
Advanced tools
Comparing version 0.4.1 to 0.4.2
{ | ||
"name": "bionode-fasta", | ||
"description": "Streamable FASTA parser", | ||
"version": "0.4.1", | ||
"version": "0.4.2", | ||
"homepage": "http://bionode.io", | ||
@@ -23,4 +23,7 @@ "repository": { | ||
"devDependencies": { | ||
"async": "~0.9.0", | ||
"beefy": "^2.1.0", | ||
"browserify": "^5.9.3", | ||
"browserify": "git+https://github.com/bmpvieira/node-browserify#3.46.1/http-browserify-1.6.0-pull-53-61-62", | ||
"browserify-fs": "~1.0.0", | ||
"request": "~2.40.0", | ||
"contributor": "~0.1.22", | ||
@@ -49,4 +52,8 @@ "coveralls": "~2.11.1", | ||
}, | ||
"browser": { | ||
"fs": "browserify-fs" | ||
}, | ||
"scripts": { | ||
"test": "node test/bionode-fasta.js | tap-spec", | ||
"test-browser": "browserify test/bionode-fasta.js | testling -x 'open -a \"Google Chrome\"' | tap-spec", | ||
"coverage": "istanbul cover test/bionode-fasta.js --report lcovonly -- | tap-spec && rm -rf ./coverage", | ||
@@ -53,0 +60,0 @@ "coveralls": "istanbul cover test/bionode-fasta.js --report lcovonly -- | tap-spec && cat ./coverage/lcov.info | ./node_modules/.bin/coveralls && rm -rf ./coverage", |
var fs = require('fs') | ||
var test = require('tape') | ||
var request = require('request') | ||
var async = require('async') | ||
var fasta = require('../') | ||
var data = require('./data') | ||
var zlib = require('zlib') | ||
var through = require('through2') | ||
var buffer = require('buffer') | ||
var file = fs.readFileSync('./test/test.fasta') | ||
// Node.js fs is implemented in the browser by browserify-fs using leveldb. | ||
// So we need to fetch the file and save it locally (server) or in leveldb (client) with fs. | ||
// Because Cross-Origin Resource Sharing (CORS) isn't enabled on GitHub, we need | ||
// to use a proxy to access the file. | ||
test('Read a fasta file and pipe content to parser.', function (t) { | ||
t.plan(3) | ||
var msg | ||
var pushFunc | ||
async.eachSeries(['test.fasta', 'test.fasta.gz'], download, startTests) | ||
msg = 'should return a Buffer for each sequence' | ||
testPipeParser(msg, fasta(), true) | ||
msg = 'should return an Object for each sequence' | ||
testPipeParser(msg, fasta({ objectMode: true })) | ||
msg = 'should return an Object for each sequence (shortcut version)' | ||
testPipeParser(msg, fasta.obj()) | ||
function testPipeParser(msg, parser, jsparse) { | ||
var result = [] | ||
parser | ||
.on('data', pushResult) | ||
.on('end', function() { t.deepEqual(result, data.fasta, msg) }) | ||
function pushResult(data) { | ||
if (jsparse) { data = JSON.parse(data.toString()) } | ||
result.push(data) | ||
} | ||
parser.write(file) | ||
parser.end() | ||
function download(file, callback) { | ||
var proxy = 'http://cors.inb.io/' | ||
var rooturl = 'https://raw.githubusercontent.com/bionode/bionode-fasta/master/test/' | ||
fs.mkdir('test', gotDir) | ||
function gotDir() { | ||
var get = request(proxy + rooturl + file, {encoding: null}) | ||
var write = fs.createWriteStream('test/' + file) | ||
get.pipe(write) | ||
write.on('close', callback) | ||
} | ||
}) | ||
} | ||
testFilename('./test/test.fasta') | ||
testFilename('./test/test.fasta.gz') | ||
testFilename('./test/test.fasta', true) | ||
testFilename('./test/test.fasta.gz', true) | ||
function testFilename(file, includePath) { | ||
var solution = JSON.parse(JSON.stringify(data.fasta)) | ||
var extramsg = '' | ||
if (includePath) { | ||
extramsg = ' (add path to results)' | ||
solution.forEach(addPath) | ||
function addPath(obj, i) { | ||
obj.path = file | ||
solution[i] = obj | ||
} | ||
} | ||
if ('gz' === file.split('.').pop()) { | ||
extramsg += ' (read gzipped file)' | ||
} | ||
test('Use parser to read file by passing filename' + extramsg, function (t) { | ||
function startTests() { | ||
test('Read a fasta file and pipe content to parser.', function (t) { | ||
t.plan(3) | ||
var msg | ||
var parser | ||
var pushFunc | ||
msg = 'should return a Buffer for each sequence' | ||
parser = includePath ? fasta({ includePath: true }, file) : fasta(file) | ||
testFilenamePipe(msg, parser, true) | ||
testPipeParser(msg, fasta(), true) | ||
msg = 'should return an Object for each sequence' | ||
var options = { objectMode: true } | ||
if (includePath) { options.includePath = true } | ||
parser = fasta(options, file) | ||
testFilenamePipe(msg, parser) | ||
testPipeParser(msg, fasta({ objectMode: true })) | ||
msg = 'should return an Object for each sequence (shortcut version)' | ||
parser = includePath ? fasta.obj({ includePath: true }, file) : fasta.obj(file) | ||
testFilenamePipe(msg, parser) | ||
testPipeParser(msg, fasta.obj()) | ||
function testFilenamePipe(msg, parser, jsparse) { | ||
function testPipeParser(msg, parser, jsparse) { | ||
var result = [] | ||
parser | ||
fs.createReadStream('test/test.fasta') | ||
.pipe(parser) | ||
.on('data', pushResult) | ||
.on('end', function() { t.deepEqual(result, solution, msg) }) | ||
.on('end', function() { t.deepEqual(result, data.fasta, msg) }) | ||
function pushResult(data) { | ||
@@ -97,53 +62,111 @@ if (jsparse) { data = JSON.parse(data.toString()) } | ||
test('Use parser to read file by passing filename and get results with callback' + extramsg, function (t) { | ||
t.plan(6) | ||
testFilename('test/test.fasta') | ||
testFilename('test/test.fasta.gz') | ||
testFilename('test/test.fasta', true) | ||
testFilename('test/test.fasta.gz', true) | ||
function testFilename(file, includePath) { | ||
var solution = JSON.parse(JSON.stringify(data.fasta)) | ||
var extramsg = '' | ||
if (includePath) { | ||
fasta({ includePath: true }, file, callback1) | ||
extramsg = ' (add path to results)' | ||
solution.forEach(addPath) | ||
function addPath(obj, i) { | ||
obj.path = file | ||
solution[i] = obj | ||
} | ||
} | ||
else { | ||
fasta(file, callback1) | ||
if ('gz' === file.split('.').pop()) { | ||
extramsg += ' (read gzipped file)' | ||
} | ||
function callback1(err, result) { | ||
t.error(err, 'callback error should be null') | ||
var objects = [] | ||
result.toString().split('\n').forEach(pushObject) | ||
function pushObject(obj) { | ||
if (obj !== '') { | ||
objects.push(JSON.parse(obj)) | ||
test('Use parser to read file by passing filename' + extramsg, function (t) { | ||
t.plan(3) | ||
var msg | ||
var parser | ||
var pushFunc | ||
msg = 'should return a Buffer for each sequence' | ||
parser = includePath ? fasta({ includePath: true }, file) : fasta(file) | ||
testFilenamePipe(msg, parser, true) | ||
msg = 'should return an Object for each sequence' | ||
var options = { objectMode: true } | ||
if (includePath) { options.includePath = true } | ||
parser = fasta(options, file) | ||
testFilenamePipe(msg, parser) | ||
msg = 'should return an Object for each sequence (shortcut version)' | ||
parser = includePath ? fasta.obj({ includePath: true }, file) : fasta.obj(file) | ||
testFilenamePipe(msg, parser) | ||
function testFilenamePipe(msg, parser, jsparse) { | ||
var result = [] | ||
parser | ||
.on('data', pushResult) | ||
.on('end', function() { t.deepEqual(result, solution, msg) }) | ||
function pushResult(data) { | ||
if (jsparse) { data = JSON.parse(data.toString()) } | ||
result.push(data) | ||
} | ||
} | ||
var msg = 'should return a Buffer with all sequences objects separated by newline' | ||
t.deepEqual(objects, solution, msg) | ||
} | ||
}) | ||
var options = { objectMode: true } | ||
if (includePath) { options.includePath = true } | ||
fasta(options, file, function(err, result) { | ||
t.error(err, 'callback error should be null') | ||
var msg = 'should return an array of Objects' | ||
t.deepEqual(result, solution, msg) | ||
test('Use parser to read file by passing filename and get results with callback' + extramsg, function (t) { | ||
t.plan(6) | ||
if (includePath) { | ||
fasta({ includePath: true }, file, callback1) | ||
} | ||
else { | ||
fasta(file, callback1) | ||
} | ||
function callback1(err, result) { | ||
t.error(err, 'callback error should be null') | ||
var objects = [] | ||
result.toString().split('\n').forEach(pushObject) | ||
function pushObject(obj) { | ||
if (obj !== '') { | ||
objects.push(JSON.parse(obj)) | ||
} | ||
} | ||
var msg = 'should return a Buffer with all sequences objects separated by newline' | ||
t.deepEqual(objects, solution, msg) | ||
} | ||
var options = { objectMode: true } | ||
if (includePath) { options.includePath = true } | ||
fasta(options, file, function(err, result) { | ||
t.error(err, 'callback error should be null') | ||
var msg = 'should return an array of Objects' | ||
t.deepEqual(result, solution, msg) | ||
}) | ||
if (includePath) { | ||
fasta.obj({ includePath: true }, file, callback2) | ||
} | ||
else { | ||
fasta.obj(file, callback2) | ||
} | ||
function callback2(err, result) { | ||
t.error(err, 'callback error should be null') | ||
var msg = 'should return an array of Objects' | ||
t.deepEqual(result, solution, msg) | ||
} | ||
}) | ||
} | ||
if (includePath) { | ||
fasta.obj({ includePath: true }, file, callback2) | ||
} | ||
else { | ||
fasta.obj(file, callback2) | ||
} | ||
function callback2(err, result) { | ||
t.error(err, 'callback error should be null') | ||
var msg = 'should return an array of Objects' | ||
t.deepEqual(result, solution, msg) | ||
} | ||
test('Errors should be caught', function(t) { | ||
t.plan(1) | ||
var msg = 'should return a ENOENT error for non-existing path' | ||
fasta('./nosuchfile.fasta') | ||
.on('error', function(error) { t.equal(error.code, 'ENOENT', msg) }) | ||
}) | ||
} | ||
test('Errors should be caught', function(t) { | ||
t.plan(1) | ||
var msg = 'should return a ENOENT error for non-existing path' | ||
fasta('./nosuchfile.fasta') | ||
.on('error', function(error) { t.equal(error.code, 'ENOENT', msg) }) | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
614163
12
27
1841