msa-parsers 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/gff/gffToInterPro.d.ts +20 -0
- package/dist/gff/gffToInterPro.js +83 -0
- package/dist/gff/gffToInterPro.js.map +1 -0
- package/dist/gff/gffToInterPro.test.d.ts +1 -0
- package/dist/gff/gffToInterPro.test.js +181 -0
- package/dist/gff/gffToInterPro.test.js.map +1 -0
- package/dist/gff/index.d.ts +3 -0
- package/dist/gff/index.js +4 -0
- package/dist/gff/index.js.map +1 -0
- package/dist/gff/interProToGFF.d.ts +9 -0
- package/dist/gff/interProToGFF.js +48 -0
- package/dist/gff/interProToGFF.js.map +1 -0
- package/dist/gff/interProToGFF.test.d.ts +1 -0
- package/dist/gff/interProToGFF.test.js +189 -0
- package/dist/gff/interProToGFF.test.js.map +1 -0
- package/dist/gff/parseGFF.d.ts +2 -0
- package/dist/gff/parseGFF.js +41 -0
- package/dist/gff/parseGFF.js.map +1 -0
- package/dist/gff/parseGFF.test.d.ts +1 -0
- package/dist/gff/parseGFF.test.js +92 -0
- package/dist/gff/parseGFF.test.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +9 -0
- package/dist/index.js.map +1 -0
- package/dist/msa/A3mMSA.d.ts +33 -0
- package/dist/msa/A3mMSA.js +280 -0
- package/dist/msa/A3mMSA.js.map +1 -0
- package/dist/msa/A3mMSA.test.d.ts +1 -0
- package/dist/msa/A3mMSA.test.js +155 -0
- package/dist/msa/A3mMSA.test.js.map +1 -0
- package/dist/msa/ClustalMSA.d.ts +30 -0
- package/dist/msa/ClustalMSA.js +53 -0
- package/dist/msa/ClustalMSA.js.map +1 -0
- package/dist/msa/EmfMSA.d.ts +27 -0
- package/dist/msa/EmfMSA.js +53 -0
- package/dist/msa/EmfMSA.js.map +1 -0
- package/dist/msa/FastaMSA.d.ts +19 -0
- package/dist/msa/FastaMSA.js +69 -0
- package/dist/msa/FastaMSA.js.map +1 -0
- package/dist/msa/StockholmMSA.d.ts +54 -0
- package/dist/msa/StockholmMSA.js +113 -0
- package/dist/msa/StockholmMSA.js.map +1 -0
- package/dist/msa/index.d.ts +18 -0
- package/dist/msa/index.js +34 -0
- package/dist/msa/index.js.map +1 -0
- package/dist/msa/index.test.d.ts +1 -0
- package/dist/msa/index.test.js +60 -0
- package/dist/msa/index.test.js.map +1 -0
- package/dist/msa/parseNewick.d.ts +60 -0
- package/dist/msa/parseNewick.js +95 -0
- package/dist/msa/parseNewick.js.map +1 -0
- package/dist/msa/stockholmParser.d.ts +22 -0
- package/dist/msa/stockholmParser.js +141 -0
- package/dist/msa/stockholmParser.js.map +1 -0
- package/dist/msa/stockholmParser.test.d.ts +1 -0
- package/dist/msa/stockholmParser.test.js +111 -0
- package/dist/msa/stockholmParser.test.js.map +1 -0
- package/dist/types.d.ts +66 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/util.d.ts +2 -0
- package/dist/util.js +10 -0
- package/dist/util.js.map +1 -0
- package/package.json +25 -0
- package/src/gff/gffToInterPro.test.ts +202 -0
- package/src/gff/gffToInterPro.ts +113 -0
- package/src/gff/index.ts +3 -0
- package/src/gff/interProToGFF.test.ts +206 -0
- package/src/gff/interProToGFF.ts +59 -0
- package/src/gff/parseGFF.test.ts +106 -0
- package/src/gff/parseGFF.ts +46 -0
- package/src/index.ts +29 -0
- package/src/msa/A3mMSA.test.ts +192 -0
- package/src/msa/A3mMSA.ts +320 -0
- package/src/msa/ClustalMSA.ts +67 -0
- package/src/msa/EmfMSA.ts +67 -0
- package/src/msa/FastaMSA.ts +82 -0
- package/src/msa/StockholmMSA.ts +141 -0
- package/src/msa/index.test.ts +74 -0
- package/src/msa/index.ts +44 -0
- package/src/msa/parseNewick.ts +94 -0
- package/src/msa/stockholmParser.test.ts +123 -0
- package/src/msa/stockholmParser.ts +157 -0
- package/src/types.ts +68 -0
- package/src/util.ts +19 -0
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import type { NodeWithIds } from '../types'
|
|
2
|
+
|
|
3
|
+
export default class FastaMSA {
|
|
4
|
+
private MSA: { seqdata: Record<string, string> }
|
|
5
|
+
|
|
6
|
+
constructor(text: string) {
|
|
7
|
+
const seqdata: Record<string, string> = {}
|
|
8
|
+
for (const entry of text.split('>')) {
|
|
9
|
+
if (!/\S/.test(entry)) {
|
|
10
|
+
continue
|
|
11
|
+
}
|
|
12
|
+
const newlineIdx = entry.indexOf('\n')
|
|
13
|
+
if (newlineIdx === -1) {
|
|
14
|
+
continue
|
|
15
|
+
}
|
|
16
|
+
const defLine = entry.slice(0, newlineIdx)
|
|
17
|
+
const spaceIdx = defLine.indexOf(' ')
|
|
18
|
+
const id = spaceIdx === -1 ? defLine : defLine.slice(0, spaceIdx)
|
|
19
|
+
if (id) {
|
|
20
|
+
seqdata[id] = entry.slice(newlineIdx + 1).replaceAll(/\s/g, '')
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
this.MSA = { seqdata }
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
getMSA() {
|
|
27
|
+
return this.MSA
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
getRowData() {
|
|
31
|
+
return undefined
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
getNames() {
|
|
35
|
+
return Object.keys(this.MSA.seqdata)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
getRow(name: string) {
|
|
39
|
+
return this.MSA.seqdata[name] || ''
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
getWidth() {
|
|
43
|
+
const name = Object.keys(this.MSA.seqdata)[0]!
|
|
44
|
+
return this.getRow(name).length
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
getStructures() {
|
|
48
|
+
return {}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
get alignmentNames() {
|
|
52
|
+
return []
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
getHeader() {
|
|
56
|
+
return {}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
getTree(): NodeWithIds {
|
|
60
|
+
return {
|
|
61
|
+
id: 'root',
|
|
62
|
+
name: 'root',
|
|
63
|
+
noTree: true,
|
|
64
|
+
children: this.getNames().map(name => ({
|
|
65
|
+
id: name,
|
|
66
|
+
children: [],
|
|
67
|
+
name,
|
|
68
|
+
})),
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
get seqConsensus() {
|
|
73
|
+
return undefined
|
|
74
|
+
}
|
|
75
|
+
get secondaryStructureConsensus() {
|
|
76
|
+
return undefined
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
get tracks() {
|
|
80
|
+
return []
|
|
81
|
+
}
|
|
82
|
+
}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import parseNewick from './parseNewick'
|
|
2
|
+
import { parseAll } from './stockholmParser'
|
|
3
|
+
import { generateNodeIds } from '../util'
|
|
4
|
+
|
|
5
|
+
import type { NodeWithIds } from '../types'
|
|
6
|
+
import type { StockholmData } from './stockholmParser'
|
|
7
|
+
|
|
8
|
+
export default class StockholmMSA {
|
|
9
|
+
private data: StockholmData[]
|
|
10
|
+
private MSA: StockholmData
|
|
11
|
+
|
|
12
|
+
constructor(text: string, currentAlignment: number) {
|
|
13
|
+
const res = parseAll(text)
|
|
14
|
+
this.data = res
|
|
15
|
+
this.MSA = res[currentAlignment]!
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
getMSA() {
|
|
19
|
+
return this.MSA
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
getRow(name: string) {
|
|
23
|
+
return this.MSA.seqdata[name] || ''
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
getWidth() {
|
|
27
|
+
const name = Object.keys(this.MSA.seqdata)[0]!
|
|
28
|
+
return this.getRow(name).length
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
get alignmentNames() {
|
|
32
|
+
return this.data.map((aln, idx) => aln.gf.DE?.[0] ?? `Alignment ${idx + 1}`)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
getHeader() {
|
|
36
|
+
const acEntries = this.MSA.gs.AC ?? {}
|
|
37
|
+
const drEntries = this.MSA.gs.DR ?? {}
|
|
38
|
+
return {
|
|
39
|
+
General: this.MSA.gf,
|
|
40
|
+
Accessions: Object.fromEntries(
|
|
41
|
+
Object.entries(acEntries).map(([k, v]) => [k, v[0]]),
|
|
42
|
+
),
|
|
43
|
+
Dbxref: Object.fromEntries(
|
|
44
|
+
Object.entries(drEntries).map(([k, v]) => [k, v.join('; ')]),
|
|
45
|
+
),
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
getRowData(rowName: string) {
|
|
50
|
+
return {
|
|
51
|
+
name: rowName,
|
|
52
|
+
accession: this.MSA.gs.AC?.[rowName]?.[0],
|
|
53
|
+
dbxref: this.MSA.gs.DR?.[rowName]?.join('; '),
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
getNames() {
|
|
58
|
+
return Object.keys(this.MSA.seqdata)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
getSeqCoords() {}
|
|
62
|
+
|
|
63
|
+
getStructures() {
|
|
64
|
+
const pdbRegex = /PDB; +(\S+) +(\S); ([0-9]+)-([0-9]+)/
|
|
65
|
+
const drEntries = this.MSA.gs.DR ?? {}
|
|
66
|
+
const args = Object.entries(drEntries)
|
|
67
|
+
.flatMap(([id, drList]) =>
|
|
68
|
+
drList.map(dr => {
|
|
69
|
+
const match = pdbRegex.exec(dr)
|
|
70
|
+
return match ? { id, match } : null
|
|
71
|
+
}),
|
|
72
|
+
)
|
|
73
|
+
.filter((item): item is { id: string; match: RegExpExecArray } => !!item)
|
|
74
|
+
.map(({ id, match }) => ({
|
|
75
|
+
id,
|
|
76
|
+
pdb: match[1]!.toLowerCase(),
|
|
77
|
+
chain: match[2]!,
|
|
78
|
+
startPos: +match[3]!,
|
|
79
|
+
endPos: +match[4]!,
|
|
80
|
+
}))
|
|
81
|
+
|
|
82
|
+
const ret = {} as Record<
|
|
83
|
+
string,
|
|
84
|
+
{ pdb: string; chain: string; startPos: number; endPos: number }[]
|
|
85
|
+
>
|
|
86
|
+
for (const entry of args) {
|
|
87
|
+
const { id, ...rest } = entry
|
|
88
|
+
if (!ret[id]) {
|
|
89
|
+
ret[id] = []
|
|
90
|
+
}
|
|
91
|
+
ret[id].push(rest)
|
|
92
|
+
}
|
|
93
|
+
return ret
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
getTree(): NodeWithIds {
|
|
97
|
+
const tree = this.MSA.gf.NH?.[0]
|
|
98
|
+
return tree
|
|
99
|
+
? generateNodeIds(parseNewick(tree))
|
|
100
|
+
: {
|
|
101
|
+
id: 'root',
|
|
102
|
+
name: 'root',
|
|
103
|
+
noTree: true,
|
|
104
|
+
children: this.getNames().map(name => ({
|
|
105
|
+
id: name,
|
|
106
|
+
children: [],
|
|
107
|
+
name,
|
|
108
|
+
})),
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
get seqConsensus() {
|
|
113
|
+
return this.MSA.gc.seq_cons
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
get secondaryStructureConsensus() {
|
|
117
|
+
return this.MSA.gc.SS_cons
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
get tracks() {
|
|
121
|
+
return [
|
|
122
|
+
{
|
|
123
|
+
id: 'seqConsensus',
|
|
124
|
+
name: 'Sequence consensus',
|
|
125
|
+
data: this.seqConsensus,
|
|
126
|
+
customColorScheme: {},
|
|
127
|
+
},
|
|
128
|
+
{
|
|
129
|
+
id: 'secondaryStruct',
|
|
130
|
+
name: 'Secondary-structure',
|
|
131
|
+
data: this.secondaryStructureConsensus,
|
|
132
|
+
customColorScheme: {
|
|
133
|
+
'>': 'pink',
|
|
134
|
+
'<': 'lightblue',
|
|
135
|
+
},
|
|
136
|
+
},
|
|
137
|
+
]
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export { sniff as stockholmSniff } from './stockholmParser'
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { describe, expect, test } from 'vitest'
|
|
2
|
+
|
|
3
|
+
import A3mMSA from './A3mMSA'
|
|
4
|
+
import ClustalMSA from './ClustalMSA'
|
|
5
|
+
import FastaMSA from './FastaMSA'
|
|
6
|
+
import { getUngappedSequence, parseMSA } from './index'
|
|
7
|
+
|
|
8
|
+
describe('parseMSA', () => {
|
|
9
|
+
test('parses FASTA format', () => {
|
|
10
|
+
const fasta = `>seq1
|
|
11
|
+
ACDEF
|
|
12
|
+
>seq2
|
|
13
|
+
GHIKL`
|
|
14
|
+
const msa = parseMSA(fasta)
|
|
15
|
+
|
|
16
|
+
expect(msa).toBeInstanceOf(FastaMSA)
|
|
17
|
+
expect(msa.getNames()).toEqual(['seq1', 'seq2'])
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
test('parses A3M format', () => {
|
|
21
|
+
const a3m = `>seq1
|
|
22
|
+
ACDEFghiKLMNPQ
|
|
23
|
+
>seq2
|
|
24
|
+
ACDEF---KLMNPQ`
|
|
25
|
+
const msa = parseMSA(a3m)
|
|
26
|
+
|
|
27
|
+
expect(msa).toBeInstanceOf(A3mMSA)
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
test('parses Clustal format', () => {
|
|
31
|
+
const clustal = `CLUSTAL W (1.83) multiple sequence alignment
|
|
32
|
+
|
|
33
|
+
seq1 ACDEFGHIKL
|
|
34
|
+
seq2 ACDEFGHIKL
|
|
35
|
+
**********`
|
|
36
|
+
const msa = parseMSA(clustal)
|
|
37
|
+
|
|
38
|
+
expect(msa).toBeInstanceOf(ClustalMSA)
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
test('defaults to Clustal for unknown format', () => {
|
|
42
|
+
const unknown = `some unknown format
|
|
43
|
+
that doesn't match anything`
|
|
44
|
+
const msa = parseMSA(unknown)
|
|
45
|
+
|
|
46
|
+
expect(msa).toBeInstanceOf(ClustalMSA)
|
|
47
|
+
})
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
describe('getUngappedSequence', () => {
|
|
51
|
+
test('removes dashes', () => {
|
|
52
|
+
expect(getUngappedSequence('AC--DEF')).toBe('ACDEF')
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
test('removes dots', () => {
|
|
56
|
+
expect(getUngappedSequence('AC..DEF')).toBe('ACDEF')
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
test('removes mixed gaps', () => {
|
|
60
|
+
expect(getUngappedSequence('A-C.D--E..F')).toBe('ACDEF')
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
test('handles empty string', () => {
|
|
64
|
+
expect(getUngappedSequence('')).toBe('')
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
test('handles string with only gaps', () => {
|
|
68
|
+
expect(getUngappedSequence('---...')).toBe('')
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
test('handles string with no gaps', () => {
|
|
72
|
+
expect(getUngappedSequence('ACDEF')).toBe('ACDEF')
|
|
73
|
+
})
|
|
74
|
+
})
|
package/src/msa/index.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import A3mMSA from './A3mMSA'
|
|
2
|
+
import ClustalMSA from './ClustalMSA'
|
|
3
|
+
import EmfMSA from './EmfMSA'
|
|
4
|
+
import FastaMSA from './FastaMSA'
|
|
5
|
+
import StockholmMSA, { stockholmSniff } from './StockholmMSA'
|
|
6
|
+
|
|
7
|
+
export { parseEmfTree } from 'emf-js'
|
|
8
|
+
export { default as parseNewick } from './parseNewick'
|
|
9
|
+
|
|
10
|
+
export type MSAParserType =
|
|
11
|
+
| StockholmMSA
|
|
12
|
+
| A3mMSA
|
|
13
|
+
| FastaMSA
|
|
14
|
+
| EmfMSA
|
|
15
|
+
| ClustalMSA
|
|
16
|
+
|
|
17
|
+
export function parseMSA(text: string, currentAlignment = 0): MSAParserType {
|
|
18
|
+
if (stockholmSniff(text)) {
|
|
19
|
+
return new StockholmMSA(text, currentAlignment)
|
|
20
|
+
}
|
|
21
|
+
if (A3mMSA.sniff(text)) {
|
|
22
|
+
return new A3mMSA(text)
|
|
23
|
+
}
|
|
24
|
+
if (text.startsWith('>')) {
|
|
25
|
+
return new FastaMSA(text)
|
|
26
|
+
}
|
|
27
|
+
if (text.startsWith('SEQ')) {
|
|
28
|
+
return new EmfMSA(text)
|
|
29
|
+
}
|
|
30
|
+
return new ClustalMSA(text)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Get ungapped sequence from an aligned row
|
|
35
|
+
*/
|
|
36
|
+
export function getUngappedSequence(alignedSeq: string): string {
|
|
37
|
+
return alignedSeq.replaceAll(/[.-]/g, '')
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export { default as A3mMSA } from './A3mMSA'
|
|
41
|
+
export { default as ClustalMSA } from './ClustalMSA'
|
|
42
|
+
export { default as EmfMSA } from './EmfMSA'
|
|
43
|
+
export { default as FastaMSA } from './FastaMSA'
|
|
44
|
+
export { default as StockholmMSA, stockholmSniff } from './StockholmMSA'
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Newick format parser in JavaScript.
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) Jason Davies 2010.
|
|
5
|
+
*
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
* furnished to do so, subject to the following conditions:
|
|
12
|
+
*
|
|
13
|
+
* The above copyright notice and this permission notice shall be included in
|
|
14
|
+
* all copies or substantial portions of the Software.
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
22
|
+
* THE SOFTWARE.
|
|
23
|
+
*
|
|
24
|
+
* Example tree (from http://en.wikipedia.org/wiki/Newick_format):
|
|
25
|
+
*
|
|
26
|
+
* +--0.1--A
|
|
27
|
+
* F-----0.2-----B +-------0.3----C
|
|
28
|
+
* +------------------0.5-----E
|
|
29
|
+
* +---------0.4------D
|
|
30
|
+
*
|
|
31
|
+
* Newick format:
|
|
32
|
+
* (A:0.1,B:0.2,(C:0.3,D:0.4)E:0.5)F;
|
|
33
|
+
*
|
|
34
|
+
* Converted to JSON:
|
|
35
|
+
* {
|
|
36
|
+
* name: "F",
|
|
37
|
+
* children: [
|
|
38
|
+
* {name: "A", length: 0.1},
|
|
39
|
+
* {name: "B", length: 0.2},
|
|
40
|
+
* {
|
|
41
|
+
* name: "E",
|
|
42
|
+
* length: 0.5,
|
|
43
|
+
* children: [
|
|
44
|
+
* {name: "C", length: 0.3},
|
|
45
|
+
* {name: "D", length: 0.4}
|
|
46
|
+
* ]
|
|
47
|
+
* }
|
|
48
|
+
* ]
|
|
49
|
+
* }
|
|
50
|
+
*
|
|
51
|
+
* Converted to JSON, but with no names or lengths:
|
|
52
|
+
* {
|
|
53
|
+
* children: [
|
|
54
|
+
* {}, {}, {
|
|
55
|
+
* children: [{}, {}]
|
|
56
|
+
* }
|
|
57
|
+
* ]
|
|
58
|
+
* }
|
|
59
|
+
*/
|
|
60
|
+
export default function parse(s: string) {
|
|
61
|
+
const ancestors = []
|
|
62
|
+
|
|
63
|
+
let tree = {} as Record<string, any>
|
|
64
|
+
const tokens = s.split(/\s*(;|\(|\)|,|:)\s*/)
|
|
65
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
66
|
+
const token = tokens[i]!
|
|
67
|
+
const subtree = {}
|
|
68
|
+
switch (token) {
|
|
69
|
+
case '(': // new children
|
|
70
|
+
tree.children = [subtree]
|
|
71
|
+
ancestors.push(tree)
|
|
72
|
+
tree = subtree
|
|
73
|
+
break
|
|
74
|
+
case ',': // another branch
|
|
75
|
+
ancestors.at(-1)?.children.push(subtree)
|
|
76
|
+
tree = subtree
|
|
77
|
+
break
|
|
78
|
+
case ')': // optional name next
|
|
79
|
+
tree = ancestors.pop()!
|
|
80
|
+
break
|
|
81
|
+
case ':': // optional length next
|
|
82
|
+
break
|
|
83
|
+
default: {
|
|
84
|
+
const x = tokens[i - 1]!
|
|
85
|
+
if (x === ')' || x === '(' || x === ',') {
|
|
86
|
+
tree.name = token
|
|
87
|
+
} else if (x === ':') {
|
|
88
|
+
tree.length = Number.parseFloat(token)
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return tree
|
|
94
|
+
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { describe, expect, test } from 'vitest'
|
|
2
|
+
|
|
3
|
+
import { parse, parseAll, sniff } from './stockholmParser'
|
|
4
|
+
|
|
5
|
+
describe('stockholmParser', () => {
|
|
6
|
+
describe('sniff', () => {
|
|
7
|
+
test('returns true for Stockholm format', () => {
|
|
8
|
+
expect(sniff('# STOCKHOLM 1.0\n')).toBe(true)
|
|
9
|
+
})
|
|
10
|
+
|
|
11
|
+
test('returns false for non-Stockholm format', () => {
|
|
12
|
+
expect(sniff('>seq1\nACDEF')).toBe(false)
|
|
13
|
+
expect(sniff('CLUSTAL W')).toBe(false)
|
|
14
|
+
})
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
describe('parseAll', () => {
|
|
18
|
+
test('parses simple Stockholm file', () => {
|
|
19
|
+
const stockholm = `# STOCKHOLM 1.0
|
|
20
|
+
seq1 ACDEFGHIKL
|
|
21
|
+
seq2 ACDEFGHIKL
|
|
22
|
+
//`
|
|
23
|
+
const result = parseAll(stockholm)
|
|
24
|
+
expect(result).toHaveLength(1)
|
|
25
|
+
expect(result[0]?.seqdata).toEqual({
|
|
26
|
+
seq1: 'ACDEFGHIKL',
|
|
27
|
+
seq2: 'ACDEFGHIKL',
|
|
28
|
+
})
|
|
29
|
+
expect(result[0]?.seqname).toEqual(['seq1', 'seq2'])
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
test('parses GF lines', () => {
|
|
33
|
+
const stockholm = `# STOCKHOLM 1.0
|
|
34
|
+
#=GF DE Description of the alignment
|
|
35
|
+
#=GF NH (A:0.1,B:0.2);
|
|
36
|
+
seq1 ACDEF
|
|
37
|
+
//`
|
|
38
|
+
const result = parseAll(stockholm)
|
|
39
|
+
expect(result[0]?.gf.DE).toEqual(['Description of the alignment'])
|
|
40
|
+
expect(result[0]?.gf.NH).toEqual(['(A:0.1,B:0.2);'])
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
test('parses GC lines', () => {
|
|
44
|
+
const stockholm = `# STOCKHOLM 1.0
|
|
45
|
+
seq1 ACDEF
|
|
46
|
+
#=GC seq_cons *****
|
|
47
|
+
#=GC SS_cons <<>>>
|
|
48
|
+
//`
|
|
49
|
+
const result = parseAll(stockholm)
|
|
50
|
+
expect(result[0]?.gc.seq_cons).toBe('*****')
|
|
51
|
+
expect(result[0]?.gc.SS_cons).toBe('<<>>>')
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
test('parses GS lines', () => {
|
|
55
|
+
const stockholm = `# STOCKHOLM 1.0
|
|
56
|
+
#=GS seq1 AC P12345
|
|
57
|
+
#=GS seq1 DR PDB; 1ABC A; 1-100
|
|
58
|
+
seq1 ACDEF
|
|
59
|
+
//`
|
|
60
|
+
const result = parseAll(stockholm)
|
|
61
|
+
expect(result[0]?.gs.AC?.seq1).toEqual(['P12345'])
|
|
62
|
+
expect(result[0]?.gs.DR?.seq1).toEqual(['PDB; 1ABC A; 1-100'])
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
test('parses multiple alignments', () => {
|
|
66
|
+
const stockholm = `# STOCKHOLM 1.0
|
|
67
|
+
seq1 ACDEF
|
|
68
|
+
//
|
|
69
|
+
# STOCKHOLM 1.0
|
|
70
|
+
seq2 GHIKL
|
|
71
|
+
//`
|
|
72
|
+
const result = parseAll(stockholm)
|
|
73
|
+
expect(result).toHaveLength(2)
|
|
74
|
+
expect(result[0]?.seqdata.seq1).toBe('ACDEF')
|
|
75
|
+
expect(result[1]?.seqdata.seq2).toBe('GHIKL')
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
test('concatenates interleaved sequence blocks', () => {
|
|
79
|
+
const stockholm = `# STOCKHOLM 1.0
|
|
80
|
+
seq1 ACDEF
|
|
81
|
+
seq2 GHIKL
|
|
82
|
+
|
|
83
|
+
seq1 MNOPQ
|
|
84
|
+
seq2 RSTUV
|
|
85
|
+
//`
|
|
86
|
+
const result = parseAll(stockholm)
|
|
87
|
+
expect(result[0]?.seqdata.seq1).toBe('ACDEFMNOPQ')
|
|
88
|
+
expect(result[0]?.seqdata.seq2).toBe('GHIKLRSTUV')
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
test('handles file without trailing //', () => {
|
|
92
|
+
const stockholm = `# STOCKHOLM 1.0
|
|
93
|
+
seq1 ACDEF`
|
|
94
|
+
const result = parseAll(stockholm)
|
|
95
|
+
expect(result).toHaveLength(1)
|
|
96
|
+
expect(result[0]?.seqdata.seq1).toBe('ACDEF')
|
|
97
|
+
})
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
describe('parse', () => {
|
|
101
|
+
test('returns single alignment', () => {
|
|
102
|
+
const stockholm = `# STOCKHOLM 1.0
|
|
103
|
+
seq1 ACDEF
|
|
104
|
+
//`
|
|
105
|
+
const result = parse(stockholm)
|
|
106
|
+
expect(result.seqdata.seq1).toBe('ACDEF')
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
test('throws on empty input', () => {
|
|
110
|
+
expect(() => parse('')).toThrow('No alignments found')
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
test('throws on multiple alignments', () => {
|
|
114
|
+
const stockholm = `# STOCKHOLM 1.0
|
|
115
|
+
seq1 ACDEF
|
|
116
|
+
//
|
|
117
|
+
# STOCKHOLM 1.0
|
|
118
|
+
seq2 GHIKL
|
|
119
|
+
//`
|
|
120
|
+
expect(() => parse(stockholm)).toThrow('More than one alignment found')
|
|
121
|
+
})
|
|
122
|
+
})
|
|
123
|
+
})
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Stockholm format parser
|
|
3
|
+
* Based on stockholm-js by Ian Holmes
|
|
4
|
+
* Rewritten in TypeScript
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
export interface StockholmData {
|
|
8
|
+
gf: Record<string, string[]>
|
|
9
|
+
gc: Record<string, string>
|
|
10
|
+
gs: Record<string, Record<string, string[]>>
|
|
11
|
+
gr: Record<string, Record<string, string>>
|
|
12
|
+
seqname: string[]
|
|
13
|
+
seqdata: Record<string, string>
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function createStockholm(): StockholmData {
|
|
17
|
+
return {
|
|
18
|
+
gf: {},
|
|
19
|
+
gc: {},
|
|
20
|
+
gs: {},
|
|
21
|
+
gr: {},
|
|
22
|
+
seqname: [],
|
|
23
|
+
seqdata: {},
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const formatStartRegex = /^# STOCKHOLM 1.0/
|
|
28
|
+
const formatEndRegex = /^\/\/\s*$/
|
|
29
|
+
const gfRegex = /^#=GF\s+(\S+)\s+(.*?)\s*$/
|
|
30
|
+
const gcRegex = /^#=GC\s+(\S+)\s+(.*?)\s*$/
|
|
31
|
+
const gsRegex = /^#=GS\s+(\S+)\s+(\S+)\s+(.*?)\s*$/
|
|
32
|
+
const grRegex = /^#=GR\s+(\S+)\s+(\S+)\s+(.*?)\s*$/
|
|
33
|
+
const lineRegex = /^\s*(\S+)\s+(\S+)\s*$/
|
|
34
|
+
const nonwhiteRegex = /\S/
|
|
35
|
+
|
|
36
|
+
export function sniff(text: string): boolean {
|
|
37
|
+
return formatStartRegex.test(text)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function parseAll(
|
|
41
|
+
text: string,
|
|
42
|
+
opts?: { strict?: boolean; quiet?: boolean },
|
|
43
|
+
): StockholmData[] {
|
|
44
|
+
const options = opts ?? {}
|
|
45
|
+
const db: StockholmData[] = []
|
|
46
|
+
let stock: StockholmData | null = null
|
|
47
|
+
|
|
48
|
+
const lines = text.split('\n')
|
|
49
|
+
for (const line of lines) {
|
|
50
|
+
let match: RegExpExecArray | null
|
|
51
|
+
|
|
52
|
+
if (formatStartRegex.test(line)) {
|
|
53
|
+
stock = createStockholm()
|
|
54
|
+
} else if (formatEndRegex.test(line)) {
|
|
55
|
+
if (stock) {
|
|
56
|
+
db.push(stock)
|
|
57
|
+
}
|
|
58
|
+
stock = null
|
|
59
|
+
} else if ((match = gfRegex.exec(line))) {
|
|
60
|
+
if (!stock) {
|
|
61
|
+
if (options.strict) {
|
|
62
|
+
throw new Error('No format header: # STOCKHOLM 1.0')
|
|
63
|
+
}
|
|
64
|
+
stock = createStockholm()
|
|
65
|
+
}
|
|
66
|
+
const tag = match[1]!
|
|
67
|
+
if (!stock.gf[tag]) {
|
|
68
|
+
stock.gf[tag] = []
|
|
69
|
+
}
|
|
70
|
+
stock.gf[tag].push(match[2]!)
|
|
71
|
+
} else if ((match = gcRegex.exec(line))) {
|
|
72
|
+
if (!stock) {
|
|
73
|
+
if (options.strict) {
|
|
74
|
+
throw new Error('No format header: # STOCKHOLM 1.0')
|
|
75
|
+
}
|
|
76
|
+
stock = createStockholm()
|
|
77
|
+
}
|
|
78
|
+
const tag = match[1]!
|
|
79
|
+
if (!stock.gc[tag]) {
|
|
80
|
+
stock.gc[tag] = ''
|
|
81
|
+
}
|
|
82
|
+
stock.gc[tag] += match[2]!
|
|
83
|
+
} else if ((match = gsRegex.exec(line))) {
|
|
84
|
+
if (!stock) {
|
|
85
|
+
if (options.strict) {
|
|
86
|
+
throw new Error('No format header: # STOCKHOLM 1.0')
|
|
87
|
+
}
|
|
88
|
+
stock = createStockholm()
|
|
89
|
+
}
|
|
90
|
+
const seqname = match[1]!
|
|
91
|
+
const tag = match[2]!
|
|
92
|
+
const value = match[3]!
|
|
93
|
+
if (!stock.gs[tag]) {
|
|
94
|
+
stock.gs[tag] = {}
|
|
95
|
+
}
|
|
96
|
+
if (!stock.gs[tag][seqname]) {
|
|
97
|
+
stock.gs[tag][seqname] = []
|
|
98
|
+
}
|
|
99
|
+
stock.gs[tag][seqname].push(value)
|
|
100
|
+
} else if ((match = grRegex.exec(line))) {
|
|
101
|
+
if (!stock) {
|
|
102
|
+
if (options.strict) {
|
|
103
|
+
throw new Error('No format header: # STOCKHOLM 1.0')
|
|
104
|
+
}
|
|
105
|
+
stock = createStockholm()
|
|
106
|
+
}
|
|
107
|
+
const seqname = match[1]!
|
|
108
|
+
const tag = match[2]!
|
|
109
|
+
const value = match[3]!
|
|
110
|
+
if (!stock.gr[tag]) {
|
|
111
|
+
stock.gr[tag] = {}
|
|
112
|
+
}
|
|
113
|
+
if (!stock.gr[tag][seqname]) {
|
|
114
|
+
stock.gr[tag][seqname] = ''
|
|
115
|
+
}
|
|
116
|
+
stock.gr[tag][seqname] += value
|
|
117
|
+
} else if ((match = lineRegex.exec(line))) {
|
|
118
|
+
if (!stock) {
|
|
119
|
+
if (options.strict) {
|
|
120
|
+
throw new Error('No format header: # STOCKHOLM 1.0')
|
|
121
|
+
}
|
|
122
|
+
stock = createStockholm()
|
|
123
|
+
}
|
|
124
|
+
const seqname = match[1]!
|
|
125
|
+
const seqdata = match[2]!
|
|
126
|
+
if (!stock.seqdata[seqname]) {
|
|
127
|
+
stock.seqdata[seqname] = ''
|
|
128
|
+
stock.seqname.push(seqname)
|
|
129
|
+
}
|
|
130
|
+
stock.seqdata[seqname] += seqdata
|
|
131
|
+
} else if (nonwhiteRegex.test(line)) {
|
|
132
|
+
if (options.strict) {
|
|
133
|
+
throw new Error('Malformed line')
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
if (stock) {
|
|
139
|
+
db.push(stock)
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
return db
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export function parse(
|
|
146
|
+
text: string,
|
|
147
|
+
opts?: { strict?: boolean; quiet?: boolean },
|
|
148
|
+
): StockholmData {
|
|
149
|
+
const db = parseAll(text, opts)
|
|
150
|
+
if (db.length === 0) {
|
|
151
|
+
throw new Error('No alignments found')
|
|
152
|
+
}
|
|
153
|
+
if (db.length > 1) {
|
|
154
|
+
throw new Error('More than one alignment found')
|
|
155
|
+
}
|
|
156
|
+
return db[0]!
|
|
157
|
+
}
|