-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathparse.test.ts
320 lines (293 loc) · 8.86 KB
/
parse.test.ts
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import fs from 'fs'
import gff from '../src'
import {
formatFeature,
GFF3Feature,
GFF3Directive,
GFF3Comment,
GFF3Sequence,
} from '../src/util'
interface ReadAllResults {
features: GFF3Feature[]
comments: GFF3Comment[]
directives: GFF3Directive[]
sequences: GFF3Sequence[]
all: (GFF3Feature | GFF3Comment | GFF3Directive | GFF3Sequence)[]
}
function readAll(
filename: string,
args: Record<string, unknown> = {},
): Promise<ReadAllResults> {
return new Promise((resolve, reject) => {
const stuff: ReadAllResults = {
features: [],
comments: [],
directives: [],
sequences: [],
all: [],
}
// $p->max_lookback(1)
fs.createReadStream(require.resolve(filename))
.pipe(
gff.parseStream({
parseFeatures: true,
parseDirectives: true,
parseComments: true,
parseSequences: true,
bufferSize: 10,
...args,
}),
)
.on('data', (d) => {
stuff.all.push(d)
if (d.directive) {
stuff.directives.push(d)
} else if (d.comment) {
stuff.comments.push(d)
} else if (d.sequence) {
stuff.sequences.push(d)
} else {
stuff.features.push(d)
}
})
.on('end', () => {
resolve(stuff)
})
.on('error', reject)
})
}
describe('GFF3 parser', () => {
it('can parse gff3_with_syncs.gff3', async () => {
const stuff = await readAll('./data/gff3_with_syncs.gff3')
const referenceResult = JSON.parse(
fs.readFileSync(
require.resolve('./data/gff3_with_syncs.result.json'),
'utf8',
),
)
expect(stuff.all).toEqual(referenceResult)
})
;[
[1010, 'messy_protein_domains.gff3'],
[4, 'gff3_with_syncs.gff3'],
[51, 'au9_scaffold_subset.gff3'],
[14, 'tomato_chr4_head.gff3'],
[5, 'directives.gff3'],
[5, 'hybrid1.gff3'],
[3, 'hybrid2.gff3'],
[6, 'knownGene.gff3'],
[6, 'knownGene2.gff3'],
[16, 'tomato_test.gff3'],
[3, 'spec_eden.gff3'],
[1, 'spec_match.gff3'],
[8, 'quantitative.gff3'],
].forEach(([count, filename]) => {
it(`can cursorily parse ${filename}`, async () => {
const stuff = await readAll(`./data/${filename}`)
// $p->max_lookback(10);
expect(stuff.all.length).toEqual(count)
})
})
it('supports children before parents, and Derives_from', async () => {
const stuff = await readAll('./data/knownGene_out_of_order.gff3')
// $p->max_lookback(2);
const expectedOutput = JSON.parse(
fs.readFileSync(
require.resolve('./data/knownGene_out_of_order.result.json'),
'utf8',
),
)
expect(stuff.all).toEqual(expectedOutput)
})
it('can parse the EDEN gene from the gff3 spec', async () => {
const stuff = await readAll('./data/spec_eden.gff3')
expect(stuff.all[2]).toHaveLength(1)
const [eden] = stuff.all[2] as GFF3Feature
expect(eden.child_features).toHaveLength(4)
expect(eden.child_features[0][0].type).toEqual('TF_binding_site')
// all the rest are mRNAs
const mrnas = eden.child_features.slice(1, 4)
expect(mrnas.filter((m) => m.length === 1)).toHaveLength(3)
const mrnaLines = mrnas.map((m) => {
expect(m).toHaveLength(1)
return m[0]
})
mrnaLines.forEach((m) => {
expect(m.type).toEqual('mRNA')
})
// check that all the mRNAs share the last exon
const lastExon = mrnaLines[2].child_features[3]
expect(mrnaLines[0].child_features).toContain(lastExon)
expect(mrnaLines[1].child_features).toContain(lastExon)
expect(mrnaLines[2].child_features).toContain(lastExon)
expect(mrnaLines[0].child_features).toHaveLength(5)
expect(mrnaLines[1].child_features).toHaveLength(4)
expect(mrnaLines[2].child_features).toHaveLength(6)
const referenceResult = JSON.parse(
fs.readFileSync(require.resolve('./data/spec_eden.result.json'), 'utf8'),
)
expect(stuff.all).toEqual(referenceResult)
})
it('can parse an excerpt of the refGene gff3', async () => {
const stuff = await readAll('./data/refGene_excerpt.gff3')
expect(true).toBeTruthy()
expect(stuff.all).toHaveLength(2)
})
it('can parse an excerpt of the TAIR10 gff3', async () => {
const stuff = await readAll('./data/tair10.gff3')
expect(stuff.all).toHaveLength(3)
})
it('can parse chr1 TAIR10 gff3', async () => {
const stuff = await readAll('./data/tair10_chr1.gff', {
disableDerivesFromReferences: true,
})
expect(stuff.all).toHaveLength(17697)
})
// check that some files throw a parse error
;['mm9_sample_ensembl.gff3', 'Saccharomyces_cerevisiae_EF3_e64.gff3'].forEach(
(errorFile) => {
it(`throws an error when parsing ${errorFile}`, async () => {
await expect(readAll(`./data/${errorFile}`)).rejects.toMatch(
/inconsistent types/,
)
})
},
)
it('can parse a string synchronously', () => {
const gff3 = fs
.readFileSync(require.resolve('./data/spec_eden.gff3'))
.toString('utf8')
const result = gff.parseStringSync(gff3, {
parseFeatures: true,
parseDirectives: true,
parseComments: true,
})
expect(result).toHaveLength(3)
const referenceResult = JSON.parse(
fs.readFileSync(require.resolve('./data/spec_eden.result.json'), 'utf8'),
)
expect(result).toEqual(referenceResult)
})
it('can parse some whitespace', () => {
const gff3 = `
SL2.40%25ch01 IT%25AG eugene g%25e;ne 80999140 81004317 . + . multivalue = val1,val2, val3;testing = blah
`
const result = gff.parseStringSync(gff3, {
parseFeatures: true,
parseDirectives: true,
parseComments: true,
})
expect(result).toHaveLength(1)
const referenceResult = [
[
{
seq_id: 'SL2.40%ch01',
source: 'IT%AG eugene',
type: 'g%e;ne',
start: 80999140,
end: 81004317,
score: null,
strand: '+',
phase: null,
attributes: {
multivalue: ['val1', 'val2', 'val3'],
testing: ['blah'],
},
child_features: [],
derived_features: [],
},
],
]
expect(result).toEqual(referenceResult)
})
it('can parse another string synchronously', () => {
const gff3 = `
SL2.40%25ch01 IT%25AG eugene g%25e;ne 80999140 81004317 . + . Alias=Solyc01g098840;ID=gene:Solyc01g098840.2;Name=Solyc01g098840.2;from_BOGAS=1;length=5178
`
const result = gff.parseStringSync(gff3, {
parseFeatures: true,
parseDirectives: true,
parseComments: true,
})
expect(result).toHaveLength(1)
const referenceResult = [
[
{
seq_id: 'SL2.40%ch01',
source: 'IT%AG eugene',
type: 'g%e;ne',
start: 80999140,
end: 81004317,
score: null,
strand: '+',
phase: null,
attributes: {
Alias: ['Solyc01g098840'],
ID: ['gene:Solyc01g098840.2'],
Name: ['Solyc01g098840.2'],
from_BOGAS: ['1'],
length: ['5178'],
},
child_features: [],
derived_features: [],
},
],
]
expect(result).toEqual(referenceResult)
expect(`\n${formatFeature(referenceResult[0])}`).toEqual(gff3)
})
;(
[
[
'hybrid1.gff3',
[
{
id: 'A00469',
sequence: 'GATTACAGATTACA',
},
{
id: 'zonker',
sequence:
'AAAAAACTAGCATGATCGATCGATCGATCGATATTAGCATGCATGCATGATGATGATAGCTATGATCGATCCCCCCCAAAAAACTAGCATGATCGATCGATCGATCGATATTAGCATGCATGCATGATGATGATAGCTATGATCGATCCCCCCC',
},
{
id: 'zeebo',
description: 'this is a test description',
sequence:
'AAAAACTAGTAGCTAGCTAGCTGATCATAGATCGATGCATGGCATACTGACTGATCGACCCCCC',
},
],
],
[
'hybrid2.gff3',
[
{
id: 'A00469',
sequence: 'GATTACAWATTACABATTACAGATTACA',
},
],
],
] as const
).forEach(([filename, expectedOutput]) => {
it(`can parse FASTA sections in hybrid ${filename} file`, async () => {
const stuff = await readAll(`./data/${filename}`)
expect(stuff.sequences).toEqual(expectedOutput)
})
})
it('can be written to directly', async () => {
const items: GFF3Feature[] = await new Promise((resolve, reject) => {
const i: GFF3Feature[] = []
const stream = gff
.parseStream()
.on('data', (d) => i.push(d))
.on('end', () => resolve(i))
.on('error', reject)
stream.write(
`SL2.40ch00 ITAG_eugene gene 16437 18189 . + . Alias=Solyc00g005000;ID=gene:Solyc00g005000.2;Name=Solyc00g005000.2;from_BOGAS=1;length=1753\n`,
)
stream.end()
})
expect(items).toHaveLength(1)
expect(items[0][0].seq_id).toEqual('SL2.40ch00')
})
})