Skip to content

Commit 28aeb59

Browse files
committed
Minor speed up
1 parent 9c084e2 commit 28aeb59

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed

src/parse.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,13 @@ export default class VCFParser {
9898
const r = this.getMetadata('FORMAT', key, 'Type')
9999
return r === 'Integer' || r === 'Float'
100100
})
101-
for (let i = 0; i < this.samples.length; i++) {
101+
const len = this.samples.length
102+
for (let i = 0; i < len; i++) {
102103
const sample = this.samples[i]!
103104
genotypes[sample] = {}
104105
const columns = rest[i]!.split(':')
105-
for (let j = 0; j < columns.length; j++) {
106+
const len2 = columns.length
107+
for (let j = 0; j < len2; j++) {
106108
const val = columns[j]!
107109
genotypes[sample][formatKeys[j]!] = val
108110
.split(',')

src/parseGenotypesOnly.ts

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,28 @@ export function parseGenotypesOnly(
55
) {
66
const rest = prerest.split('\t')
77
const genotypes = {} as Record<string, string>
8-
let i = 0
8+
let len = samples.length
99
if (format.includes('GT')) {
10-
const formatSplit = format.split(':')
11-
if (formatSplit.length === 1) {
12-
for (const sample of samples) {
13-
genotypes[sample] = rest[i++]!
10+
if (format === 'GT') {
11+
let len = samples.length
12+
for (let i = 0; i < len; i++) {
13+
genotypes[samples[i]!] = rest[i]!
1414
}
1515
} else {
16-
const gtIndex = formatSplit.indexOf('GT')
17-
if (gtIndex === 0) {
18-
for (const sample of samples) {
19-
const val = rest[i++]!
20-
const idx = val.indexOf(':')
21-
genotypes[sample] = idx !== -1 ? val.slice(0, idx) : val
16+
if (format.startsWith('GT')) {
17+
for (let i = 0; i < len; i++) {
18+
const idx = rest[i]!.indexOf(':')
19+
genotypes[samples[i]!] =
20+
idx !== -1 ? rest[i]!.slice(0, idx) : rest[i]!
2221
}
2322
} else {
24-
for (const sample of samples) {
25-
const val = rest[i++]!.split(':')
26-
genotypes[sample] = val[gtIndex]!
23+
// according to vcf spec, GT should be first, so shouldn't even get
24+
// here, but just added to beware
25+
const formatSplit = format.split(':')
26+
const gtIndex = formatSplit.indexOf('GT')
27+
for (let i = 0; i < len; i++) {
28+
const val = rest[i]!.split(':')
29+
genotypes[samples[i]!] = val[gtIndex]!
2730
}
2831
}
2932
}

0 commit comments

Comments
 (0)