Skip to content

Commit 6698212

Browse files
committed
Handle missingness
1 parent 5ebda0f commit 6698212

File tree

4 files changed

+67
-33
lines changed

4 files changed

+67
-33
lines changed

src/parse.ts

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { parseGenotypesOnly } from './parseGenotypesOnly.ts'
12
import { parseMetaString } from './parseMetaString.ts'
23
import vcfReserved from './vcfReserved.ts'
34

@@ -120,38 +121,6 @@ export default class VCFParser {
120121
return genotypes
121122
}
122123

123-
private parseGenotypesOnly(format: string, prerest: string) {
124-
const rest = prerest.split('\t')
125-
const genotypes = {} as Record<string, string>
126-
let i = 0
127-
const formatSplit = format.split(':')
128-
if (formatSplit.length === 1) {
129-
for (const sample of this.samples) {
130-
genotypes[sample] = rest[i++]!
131-
}
132-
} else {
133-
const gtIndex = formatSplit.indexOf('GT')
134-
if (gtIndex === 0) {
135-
for (const sample of this.samples) {
136-
const val = rest[i++]!
137-
const idx = val.indexOf(':')
138-
if (idx !== -1) {
139-
genotypes[sample] = val.slice(0, idx)
140-
} else {
141-
console.warn('unknown')
142-
}
143-
}
144-
} else {
145-
for (const sample of this.samples) {
146-
const val = rest[i++]!.split(':')
147-
genotypes[sample] = val[gtIndex]!
148-
}
149-
}
150-
}
151-
152-
return genotypes
153-
}
154-
155124
/**
156125
* Parse a VCF metadata line (i.e. a line that starts with "##") and add its
157126
* properties to the object.
@@ -338,7 +307,7 @@ export default class VCFParser {
338307
QUAL: qual,
339308
FORMAT: format,
340309
SAMPLES: () => this.parseSamples(fields[8] ?? '', rest),
341-
GENOTYPES: () => this.parseGenotypesOnly(fields[8] ?? '', rest),
310+
GENOTYPES: () => parseGenotypesOnly(fields[8] ?? '', rest, this.samples),
342311
}
343312
}
344313
}

src/parseGenotypesOnly.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
export function parseGenotypesOnly(
2+
format: string,
3+
prerest: string,
4+
samples: string[],
5+
) {
6+
const rest = prerest.split('\t')
7+
const genotypes = {} as Record<string, string>
8+
let i = 0
9+
const formatSplit = format.split(':')
10+
if (formatSplit.length === 1) {
11+
for (const sample of samples) {
12+
genotypes[sample] = rest[i++]!
13+
}
14+
} else {
15+
const gtIndex = formatSplit.indexOf('GT')
16+
if (gtIndex === 0) {
17+
for (const sample of samples) {
18+
const val = rest[i++]!
19+
const idx = val.indexOf(':')
20+
genotypes[sample] = idx !== -1 ? val.slice(0, idx) : val
21+
}
22+
} else {
23+
for (const sample of samples) {
24+
const val = rest[i++]!.split(':')
25+
genotypes[sample] = val[gtIndex]!
26+
}
27+
}
28+
}
29+
30+
return genotypes
31+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2+
3+
exports[`parse genotypes 1`] = `
4+
{
5+
"h1": "./.",
6+
"h2": "./.",
7+
}
8+
`;
9+
10+
exports[`parse genotypes 2`] = `
11+
{
12+
"h1": "./.",
13+
"h2": "./.",
14+
}
15+
`;
16+
17+
exports[`parse genotypes 3`] = `
18+
{
19+
"h1": "./.",
20+
"h2": "./.",
21+
}
22+
`;

test/parseGenotypesOnly.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { test, expect } from 'vitest'
2+
import { parseGenotypesOnly } from '../src/parseGenotypesOnly'
3+
4+
test('parse genotypes', () => {
5+
expect(parseGenotypesOnly('GT', './.\t./.', ['h1', 'h2'])).toMatchSnapshot()
6+
expect(
7+
parseGenotypesOnly('GT:RT', './.:1\t./.', ['h1', 'h2']),
8+
).toMatchSnapshot()
9+
expect(
10+
parseGenotypesOnly('RT:GT', '1:./.\t2:./.', ['h1', 'h2']),
11+
).toMatchSnapshot()
12+
})

0 commit comments

Comments
 (0)