-
Notifications
You must be signed in to change notification settings - Fork 63
/
pileuputils-test.js
195 lines (174 loc) · 6.39 KB
/
pileuputils-test.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/* @flow */
'use strict';
import type SamRead from '../../main/data/SamRead';
import {expect} from 'chai';
import _ from 'underscore';
import {pileup, addToPileup, getOpInfo} from '../../main/viz/pileuputils';
import Interval from '../../main/Interval';
import ContigInterval from '../../main/ContigInterval';
import Bam from '../../main/data/bam';
import RemoteFile from '../../main/RemoteFile';
describe('pileuputils', function() {
// This checks that pileup's guarantee is met.
function checkGuarantee(reads: Interval[], rows: number[]) {
var readsByRow = _.groupBy(reads, (read, i) => rows[i]);
_.each(readsByRow, reads => {
// No pair of reads in the same row should intersect.
for (var i = 0; i < reads.length - 1; i++) {
for (var j = i + 1; j < reads.length; j++) {
expect(reads[i].intersects(reads[j])).to.be.false;
}
}
});
}
it('should check the guarantee', function(done) {
var reads = [
new Interval(0, 9),
new Interval(5, 14),
new Interval(10, 19),
];
checkGuarantee(reads, [0, 1, 2]); // ok
checkGuarantee(reads, [0, 1, 0]); // ok
expect(() => checkGuarantee(reads, [0, 0, 0])).to.throw(); // not ok
done();
});
it('should pile up a collection of reads', function(done) {
var reads = [
new Interval(0, 9),
new Interval(5, 14),
new Interval(10, 19),
new Interval(15, 24)
];
var rows = pileup(reads);
checkGuarantee(reads, rows);
expect(rows).to.deep.equal([0,1,0,1]);
done();
});
it('should pile up a deep collection of reads', function(done) {
var reads = [
new Interval(0, 9),
new Interval(1, 10),
new Interval(2, 11),
new Interval(3, 12),
new Interval(4, 13)
];
var rows = pileup(reads);
checkGuarantee(reads, rows);
expect(rows).to.deep.equal([0,1,2,3,4]);
done();
});
it('should pile up around a long read', function(done) {
var reads = [
new Interval(1, 9),
new Interval(0, 100),
new Interval(5, 14),
new Interval(10, 19),
new Interval(15, 24)
];
var rows = pileup(reads);
checkGuarantee(reads, rows);
expect(rows).to.deep.equal([0,1,2,0,2]);
done();
});
it('should build a pileup progressively', function(done) {
var reads = [
new Interval(1, 9),
new Interval(0, 100),
new Interval(5, 14),
new Interval(10, 19),
new Interval(15, 24)
];
var pileup = [];
var rows = reads.map(read => addToPileup(read, pileup));
checkGuarantee(reads, rows);
expect(rows).to.deep.equal([0,1,2,0,2]);
done();
});
var ref = // chr17:7513000-7513500
"CCTTTTGGGTTCTTCCCTTAGCTCCTGCTCAAGTGTCCTCCCCACTCCCACAACCACTAATATTTTATCCA" +
"TTCCCTCTTCTTTTCCCTGTAATCCCAACACTTGGAGGCCGAGGTCGGTAGATCAGCTGAGGCCAGGAGTT" +
"CGAGACCAGTCTGGCCAATATGGCAAAACCCCATTGCTACTATATATATATGTATACATATACATATATAT" +
"ACACATACATATATATGTATATATACATGTATATGTATATATATACATGTATATGTATACATATATATACA" +
"TGTATATGTATACATATATATATACATGTATATGTATACATATATATATACATGTATATGTATACATGTAT" +
"GTATATATATACACACACACACACACACATATATATAAATTAGCCAGGCGTGGTGGCACATGGCTGTAACC" +
"TCAGCTATTCAGGGTGGCTGAGATATGAGAATCACTTGAAGCCAGGAGGCAGAGGCTGCAGGGTCGTCTGG" +
"ATTT";
it('should split reads into ops', function(): any {
var bamFile = new RemoteFile('/test-data/synth3.normal.17.7500000-7515000.bam'),
bamIndexFile = new RemoteFile('/test-data/synth3.normal.17.7500000-7515000.bam.bai'),
bam = new Bam(bamFile, bamIndexFile);
var range = new ContigInterval('chr17', 7513106, 7513400);
var fakeReferenceSource = {
getRangeAsString: function({contig,start,stop}) {
expect(contig).to.equal('17');
expect(start).to.be.within(7513000, 7513500);
expect(stop).to.be.within(7513000, 7513500);
return ref.slice(start - 7513000, stop - 7513000 + 1);
}
};
var unknownReferenceSource = {
getRangeAsString: function({start, stop}) {
return _.range(start, stop + 1).map(x => '.').join('');
}
};
return bam.getFeaturesInRange(range).then(reads => {
var findRead = function(startPos): SamRead {
var r = null;
for (var i = 0; i < reads.length; i++) {
if (reads[i].pos == startPos) {
expect(r).to.be.null; // duplicate read
r = reads[i];
}
}
if (r) return r;
throw `Unable to find read starting at position ${startPos}`;
};
var simpleMismatch = findRead(7513223 - 1),
deleteRead = findRead(7513329 - 1),
insertRead = findRead(7513205 - 1),
softClipRead = findRead(7513109 - 1);
expect(simpleMismatch.getCigarString()).to.equal('101M');
expect(deleteRead.getCigarString()).to.equal('37M4D64M');
expect(insertRead.getCigarString()).to.equal('73M20I8M');
expect(softClipRead.getCigarString()).to.equal('66S35M');
expect(getOpInfo(simpleMismatch, fakeReferenceSource)).to.deep.equal({
ops: [
{ op: 'M', length: 101, pos: 7513222, arrow: 'R' }
],
mismatches: [{pos: 7513272, basePair: 'G', quality: 1}]
});
expect(getOpInfo(deleteRead, fakeReferenceSource)).to.deep.equal({
ops: [
{ op: 'M', length: 37, pos: 7513328 + 0, arrow: null },
{ op: 'D', length: 4, pos: 7513328 + 37, arrow: null },
{ op: 'M', length: 64, pos: 7513328 + 41, arrow: 'R' }
],
mismatches: []
});
expect(getOpInfo(insertRead, fakeReferenceSource)).to.deep.equal({
ops: [
{ op: 'M', length: 73, pos: 7513204 + 0, arrow: 'L' },
{ op: 'I', length: 20, pos: 7513204 + 73, arrow: null },
{ op: 'M', length: 8, pos: 7513204 + 73, arrow: null }
],
mismatches: []
});
expect(getOpInfo(softClipRead, fakeReferenceSource)).to.deep.equal({
ops: [
{ op: 'S', length: 66, pos: 7513108 + 0, arrow: null },
{ op: 'M', length: 35, pos: 7513108 + 0, arrow: 'L' }
],
mismatches: [
{ pos: 7513109, basePair: 'G', quality: 2 },
{ pos: 7513112, basePair: 'C', quality: 2 }
]
});
expect(getOpInfo(simpleMismatch, unknownReferenceSource)).to.deep.equal({
ops: [
{ op: 'M', length: 101, pos: 7513222, arrow: 'R' }
],
mismatches: [] // no mismatches against unknown reference data
});
});
});
});