Skip to content

Commit

Permalink
Merge pull request #470 from akmorrow13/fix-ga4gh-alignments
Browse files Browse the repository at this point in the history
Updated GA4GHAlignment from v0.5.1 to v0.6.0a10
  • Loading branch information
akmorrow13 authored Sep 27, 2017
2 parents 3395026 + 847fc33 commit 78e2171
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 183 deletions.
11 changes: 6 additions & 5 deletions src/main/GA4GHAlignment.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import type {CigarOp, MateProperties, Strand} from './Alignment';
import ContigInterval from './ContigInterval';
import SamRead from './data/SamRead';

// See https://github.com/ga4gh/schemas/blob/v0.5.1/src/main/resources/avro/common.avdl
// See https://github.com/ga4gh/ga4gh-schemas/blob/master/src/main/proto/ga4gh/reads.proto
// Data can be queried at http://1kg.ga4gh.org/reads/search
var OP_MAP = {
ALIGNMENT_MATCH: 'M',
INSERT: 'I',
Expand All @@ -36,7 +37,7 @@ class GA4GHAlignment /* implements Alignment */ {
_interval: ContigInterval<string>;

// alignment follows org.ga4gh.GAReadAlignment
// https://github.com/ga4gh/schemas/blob/v0.5.1/src/main/resources/avro/reads.avdl
// https://github.com/ga4gh/ga4gh-schemas/blob/master/src/main/proto/ga4gh/reads.proto
constructor(alignment: Object) {
this.alignment = alignment;
this.pos = parseInt(alignment.alignment.position.position);
Expand All @@ -56,7 +57,7 @@ class GA4GHAlignment /* implements Alignment */ {
}

getStrand(): Strand {
return this.alignment.alignment.position.reverseStrand ? '-' : '+';
return this.alignment.alignment.position.strand == 'NEG_STRAND' ? '-' : '+';
}

getQualityScores(): number[] {
Expand All @@ -83,8 +84,8 @@ class GA4GHAlignment /* implements Alignment */ {
var next = this.alignment.nextMatePosition;
return next && {
ref: next.referenceName,
pos: next.position,
strand: next.reverseStrand ? '-' : '+'
pos: parseInt(next.position),
strand: next.strand == 'NEG_STRAND' ? '-' : '+'
};
}

Expand Down
78 changes: 42 additions & 36 deletions src/test/GA4GHAlignment-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,63 +11,69 @@ describe('GA4GHAlignment', function() {
var sampleAlignments = [];

before(function() {
return new RemoteFile('/test-data/alignments.ga4gh.chr17.1-250.json').getAllString().then(data => {
return new RemoteFile('/test-data/alignments.ga4gh.1.10000-11000.json').getAllString().then(data => {
sampleAlignments = JSON.parse(data).alignments;
});
});

it('should read the sample alignments', function() {
expect(sampleAlignments).to.have.length(14);
expect(sampleAlignments).to.have.length(100);
});

it('should provide basic accessors', function() {
var a = new GA4GHAlignment(sampleAlignments[0]);
expect(a.name).to.equal('r000');
expect(a.getSequence()).to.equal('ATTTAGCTAC');
expect(a.getQualityScores()).to.deep.equal([32,32,32,32,32,32,32,32,32,32]);
expect(a.name).to.equal('ERR181329.21587964');
expect(a.getSequence()).to.equal('ATAACCCTAACCATAACCCTAACCCTAACCCTAACCCTAACCCTAACCCTAACCCTAACCCTAACCCTAACCCTAACCCTAACCCTAACCCTAACCCTAA');
expect(a.getQualityScores()).to.deep.equal([2, 36, 36, 37, 37, 38, 37, 39, 37, 38, 38, 38, 3, 38, 38, 39, 38, 39, 39, 39, 38, 39, 39, 38, 40, 40, 38, 39, 39, 39, 39, 40, 38, 40, 39, 40, 38, 38, 38, 39, 38, 40, 37, 40, 38, 39, 39, 40, 40, 40, 39, 32, 39, 35, 39, 36, 38, 40, 39, 39, 39, 36, 39, 37, 39, 40, 39, 31, 39, 35, 36, 39, 37, 32, 40, 41, 38, 38, 37, 32, 39, 38, 38, 39, 33, 36, 25, 37, 38, 19, 35, 13, 37, 31, 35, 33, 34, 8, 33, 18]);
expect(a.getStrand()).to.equal('+');
expect(a.getInterval().toString()).to.equal('chr17:4-13'); // 0-based
expect(a.getInterval().toString()).to.equal('1:9999-10098'); // 0-based
expect(a.cigarOps).to.deep.equal([
{op: 'M', length: 10}
{op: 'M', length: 100}
]);
expect(a.getMateProperties()).to.deep.equal({
ref: 'chr17',
pos: 79,
strand: '+'
ref: '1',
pos: 10007,
strand: '-'
});
});

it('should match SamRead', function() {
var bam = new Bam(new RemoteFile('/test-data/chr17.1-250.bam'));
return bam.readAll().then(({alignments: samReads}) => {
// This is a workaround. See https://github.com/ga4gh/server/issues/488
samReads.splice(-1, 1);
var json = new RemoteFile('/test-data/alignments.ga4gh.chr17.1-250.json');

expect(sampleAlignments.length).to.equal(samReads.length);
for (var i = 0; i < sampleAlignments.length; i++) {
var ga4gh = new GA4GHAlignment(sampleAlignments[i]),
bam = samReads[i];
expect(ga4gh.getSequence()).to.equal(bam.getSequence());
var interval = ga4gh.getInterval();
expect(interval.start()).to.equal(bam.pos);
json.getAllString().then(data => {
var matchingBamAlignments = JSON.parse(data).alignments;

// See https://github.com/ga4gh/server/issues/491
// expect(ga4gh.getStrand()).to.equal(bam.getStrand());
// For the if statement, see https://github.com/ga4gh/server/issues/492
var quality = ga4gh.getQualityScores();
if (quality.length) {
expect(quality).to.deep.equal(bam.getQualityScores());
}
expect(ga4gh.cigarOps).to.deep.equal(bam.cigarOps);
// After ga4gh#491, change this to a .deep.equal on getMateProperties()
var ga4ghMate = ga4gh.getMateProperties(),
bamMate = bam.getMateProperties();
expect(!!ga4ghMate).to.equal(!!bamMate);
if (ga4ghMate && bamMate) {
expect(ga4ghMate.ref).to.equal(bamMate.ref);
expect(ga4ghMate.pos).to.equal(bamMate.pos);
return bam.readAll().then(({alignments: samReads}) => {
// This is a workaround. See https://github.com/ga4gh/server/issues/488
samReads.splice(-1, 1);

expect(matchingBamAlignments.length).to.equal(samReads.length);
for (var i = 0; i < matchingBamAlignments.length; i++) {
var ga4gh = new GA4GHAlignment(matchingBamAlignments[i]),
bam = samReads[i];
expect(ga4gh.getSequence()).to.equal(bam.getSequence());
var interval = ga4gh.getInterval();
expect(interval.start()).to.equal(bam.pos);

// See https://github.com/ga4gh/server/issues/491
// expect(ga4gh.getStrand()).to.equal(bam.getStrand());
// For the if statement, see https://github.com/ga4gh/server/issues/492
var quality = ga4gh.getQualityScores();
if (quality.length) {
expect(quality).to.deep.equal(bam.getQualityScores());
}
expect(ga4gh.cigarOps).to.deep.equal(bam.cigarOps);
// After ga4gh#491, change this to a .deep.equal on getMateProperties()
var ga4ghMate = ga4gh.getMateProperties(),
bamMate = bam.getMateProperties();
expect(!!ga4ghMate).to.equal(!!bamMate);
if (ga4ghMate && bamMate) {
expect(ga4ghMate.ref).to.equal(bamMate.ref);
expect(ga4ghMate.pos).to.equal(bamMate.pos);
}
}
}
});
});
});
});
12 changes: 6 additions & 6 deletions src/test/sources/GA4GHAlignmentSource-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('GA4GHAlignmentSource', function() {
var server: any = null, response;

before(function () {
return new RemoteFile('/test-data/alignments.ga4gh.chr17.1-250.json').getAllString().then(data => {
return new RemoteFile('/test-data/alignments.ga4gh.1.10000-11000.json').getAllString().then(data => {
response = data;
server = sinon.fakeServer.create(); // _after_ we do a real XHR!
});
Expand All @@ -24,16 +24,16 @@ describe('GA4GHAlignmentSource', function() {
});

it('should fetch alignments from a server', function(done) {
server.respondWith('POST', '/v0.5.1/reads/search',
server.respondWith('POST', '/v0.6.0a10/reads/search',
[200, { "Content-Type": "application/json" }, response]);

var source = GA4GHAlignmentSource.create({
endpoint: '/v0.5.1',
endpoint: '/v0.6.0a10',
readGroupId: 'some-group-set:some-read-group',
forcedReferenceId: null
});

var requestInterval = new ContigInterval('chr17', 10, 20);
var requestInterval = new ContigInterval('1', 10000, 10007);
expect(source.getAlignmentsInRange(requestInterval))
.to.deep.equal([]);

Expand All @@ -42,11 +42,11 @@ describe('GA4GHAlignmentSource', function() {
source.on('networkdone', e => { progress.push('done'); });
source.on('newdata', () => {
var reads = source.getAlignmentsInRange(requestInterval);
expect(reads).to.have.length(1);
expect(reads).to.have.length(16);
done();
});

source.rangeChanged({contig: 'chr17', start: 1, stop: 30});
source.rangeChanged({contig: '1', start: 10000, stop: 10007});
server.respond();
});

Expand Down
11 changes: 9 additions & 2 deletions test-data/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,16 @@ corresponding SAM files for them in the same repo.
This was hand-edited from the SAM equivalent of `test_input_1_a.bam` to have
reads in chr17:1-250. It was then converted back to BAM/BAI using `samtools view`.

The JSON variant was formed by loading `chr17.1-250.bam` into v0.5.1 of the [GA4GH demo server][ga4gh] and querying for all the reads via:
The JSON variant was formed by loading `chr17.1-250.bam` into 0.6.0a10 of the [GA4GH demo server][ga4gh] and querying for all the reads via:

curl --data '{"readGroupIds":["pileup.js:chr17.1-250"]}' --header 'Content-Type: application/json' http://localhost:8000/v0.5.1/reads/search > alginments.ga4gh.chr17.1-250.json
curl --data '{"readGroupIds":["pileup.js:chr17.1-250"]}' --header 'Content-Type: application/json' http://localhost:8000/0.6.0a10/reads/search > alignments.ga4gh.chr17.1-250.json


#### alignments.ga4gh.1.10000-11000.json

alignments.ga4gh.1.10000-11000.json was generated from the 0.6.0a10 GA4GH server http://1kgenomes.ga4gh.org/reads/search with the following
parameters:
{"readGroupIds": ["WyIxa2dlbm9tZXMiLCJyZ3MiLCJIRzAzMjcwIiwiRVJSMTgxMzI5Il0"], "referenceId": "WyJOQ0JJMzciLCIxIl0"}

#### index_test.bam

Expand Down
1 change: 1 addition & 0 deletions test-data/alignments.ga4gh.1.10000-11000.json

Large diffs are not rendered by default.

Loading

0 comments on commit 78e2171

Please sign in to comment.