Skip to content

Commit

Permalink
add some tests, fix some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
danvk committed Apr 28, 2015
1 parent 95a96d9 commit 13f41d3
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 32 deletions.
6 changes: 3 additions & 3 deletions src/SamRead.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ class SamRead {

// Go ahead and parse a few fields immediately.
var jv = this._getJDataView();
this.refID = jv.getUint32(0);
this.refID = jv.getInt32(0);
this.ref = ref;
this.pos = jv.getUint32(4);
this.l_seq = jv.getUint32(16);
this.pos = jv.getInt32(4);
this.l_seq = jv.getInt32(16);
}

toString(): string {
Expand Down
2 changes: 1 addition & 1 deletion src/TwoBitDataSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ var createTwoBitDataSource = function(remoteSource: TwoBit): TwoBitSource {
function getRangeAsString(range: GenomeRange): string {
if (!range) return '';
return _.range(range.start, range.stop + 1)
.map(x => getBasePair(range.contig, x))
.map(x => getBasePair(range.contig, x) || '.')
.join('');
}

Expand Down
8 changes: 7 additions & 1 deletion src/bam.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,13 @@ class Bam {
var slice = function(u8: Uint8Array) {
return u8.buffer.slice(u8.byteOffset, u8.byteOffset + u8.byteLength - 1);
};
o.alignments = o.alignments.map(x => new SamRead(slice(x.contents), vo, ''));
o.alignments = o.alignments.map(x => {
var r = new SamRead(slice(x.contents), vo, '');
if (r.refID != -1) {
r.ref = o.header.references[r.refID].name;
}
return r;
});
return o;
});
}
Expand Down
40 changes: 15 additions & 25 deletions test/SamRead-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,18 @@
'use strict';

import type * as Q from 'q';
import type * as SamRead from '../src/SamRead';

var expect = require('chai').expect;

var jBinary = require('jbinary');

var RemoteFile = require('../src/RemoteFile'),
utils = require('../src/utils'),
bamTypes = require('../src/formats/bamTypes'),
SamRead = require('../src/SamRead'),
VirtualOffset = require('../src/VirtualOffset'),
Bam = require('../src/bam'),
ContigInterval = require('../src/ContigInterval');

describe('SamRead', function() {

function getSamArray(url): Q.Promise<SamRead[]> {
var zero = new VirtualOffset(0, 0);
var file = new RemoteFile(url);
return file.getAll().then(gzipBuffer => {
var buf = utils.inflateGzip(gzipBuffer);
var jb = new jBinary(buf, bamTypes.TYPE_SET);
jb.read('BamHeader'); // skip past the header to get to alignments.
return jb.read(['array', {
block_size: 'int32',
contents: ['blob', 'block_size']
}]).map(block => new SamRead(block.contents, zero, 'ref'));
});
return new Bam(new RemoteFile(url)).readAll().then(d => d.alignments);
}

var testReads = getSamArray('/test/data/test_input_1_a.bam');
Expand All @@ -46,10 +32,10 @@ describe('SamRead', function() {
var read = reads[0];
expect(read.getName()).to.equal('r000');
expect(read.refID).to.equal(0);
expect(read.ref).to.equal('ref');
expect(read.ref).to.equal('insert');
expect(read.pos).to.equal(49); // 0-based
expect(read.l_seq).to.equal(10);
expect(read.toString()).to.equal('ref:50-59');
expect(read.toString()).to.equal('insert:50-59');
expect(read.getCigarOps()).to.deep.equal([{op: 'M', length: 10}]);

// This one has a more interesting Cigar string
Expand All @@ -73,7 +59,7 @@ describe('SamRead', function() {
var r000 = reads[0].getFull();
expect(r000.read_name).to.equal('r000');
expect(r000.FLAG).to.equal(99);
expect(r000.refID).to.equal(0);
expect(reads[0].ref).to.equal('insert');
// .. POS
expect(r000.MAPQ).to.equal(30);
expect(reads[0].getCigarString()).to.equal('10M');
Expand All @@ -88,19 +74,23 @@ describe('SamRead', function() {
expect(aux).to.have.length(2);
expect(aux[0]).to.contain({tag: 'RG', value: 'cow'});
expect(aux[1]).to.contain({tag: 'PG', value: 'bull'});

expect(reads).to.have.length(15);
expect(reads[14].refID).to.equal(-1); // unmapped read
expect(reads[14].ref).to.equal('');
});
});

it('should find record intersections', function() {
return testReads.then(reads => {
var read = reads[0];
// toString() produces a 1-based result, but ContigInterval is 0-based.
expect(read.toString()).to.equal('ref:50-59');
expect(read.intersects(new ContigInterval('ref', 40, 49))).to.be.true;
expect(read.intersects(new ContigInterval('ref', 40, 48))).to.be.false;
expect(read.toString()).to.equal('insert:50-59');
expect(read.intersects(new ContigInterval('insert', 40, 49))).to.be.true;
expect(read.intersects(new ContigInterval('insert', 40, 48))).to.be.false;
expect(read.intersects(new ContigInterval('0', 40, 55))).to.be.false;
expect(read.intersects(new ContigInterval('ref', 58, 60))).to.be.true;
expect(read.intersects(new ContigInterval('ref', 59, 60))).to.be.false;
expect(read.intersects(new ContigInterval('insert', 58, 60))).to.be.true;
expect(read.intersects(new ContigInterval('insert', 59, 60))).to.be.false;
});
});
});
2 changes: 2 additions & 0 deletions test/TwoBitDataSource-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ describe('TwoBitDataSource', function() {
'chr22:3': null,
'chr22:4': null
});
expect(source.getRangeAsString(range)).to.equal('....');

source.on('newdata', () => {
expect(source.getRange(range)).to.deep.equal({
Expand All @@ -41,6 +42,7 @@ describe('TwoBitDataSource', function() {
'chr22:3': 'C',
'chr22:4': 'A'
});
expect(source.getRangeAsString(range)).to.equal('NTCA');
done();
});
source.rangeChanged(range);
Expand Down
31 changes: 29 additions & 2 deletions test/pileuputils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ var expect = require('chai').expect;

var _ = require('underscore');

var {pileup, addToPileup} = require('../src/pileuputils'),
Interval = require('../src/Interval');
var {pileup, addToPileup, getDifferingBasePairs} = require('../src/pileuputils'),
Interval = require('../src/Interval'),
Bam = require('../src/bam'),
RemoteFile = require('../src/RemoteFile');

describe('pileuputils', function() {
// This checks that pileup's guarantee is met.
Expand Down Expand Up @@ -84,4 +86,29 @@ describe('pileuputils', function() {
checkGuarantee(reads, rows);
expect(rows).to.deep.equal([0,1,2,0,2]);
});

function getSamArray(url) {
return new Bam(new RemoteFile(url)).readAll().then(d => d.alignments);
}

it('should find differences between ref and read', function() {
return getSamArray('/test/data/test_input_1_a.bam').then(reads => {
var read = reads[0];
expect(read.pos).to.equal(49);
expect(read.getCigarString()).to.equal('10M');
// 0123456789
expect(read.getSequence()).to.equal('ATTTAGCTAC');
var ref = 'TTTTAGCGAC';

expect(getDifferingBasePairs(read, ref)).to.deep.equal([
{pos: 49+0, basePair: 'A'},
{pos: 49+7, basePair: 'T'}
]);

// More complex CIGAR strings are not supported yet.
var read3 = reads[3];
expect(read3.getCigarString()).to.equal('1S2I6M1P1I1P1I4M2I');
expect(getDifferingBasePairs(read3, ref)).to.deep.equal([]);
});
});
});

0 comments on commit 13f41d3

Please sign in to comment.