Skip to content

Commit

Permalink
Less mutation for gff3 parser
Browse files Browse the repository at this point in the history
  • Loading branch information
cmdcolin committed Sep 17, 2024
1 parent ac4de67 commit e9fcddd
Show file tree
Hide file tree
Showing 4 changed files with 642 additions and 629 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,73 +3,61 @@
exports[`adapter can fetch features from volvox.gff3 test getfeatures on gff plain text adapter 1`] = `
[
{
"attributes": undefined,
"child_features": undefined,
"data": undefined,
"derived_features": [],
"end": 6079,
"name": "ctgB",
"phase": undefined,
"refName": "ctgB",
"score": undefined,
"seq_id": undefined,
"source": "example",
"start": 0,
"strand": undefined,
"subfeatures": [],
"type": "contig",
"uniqueId": "test-ctgB-0",
},
{
"attributes": undefined,
"child_features": undefined,
"data": undefined,
"derived_features": [],
"end": 1984,
"name": "f07",
"note": "This is an example",
"phase": undefined,
"refName": "ctgB",
"score": undefined,
"seq_id": undefined,
"source": "example",
"start": 1658,
"strand": 1,
"subfeatures": [],
"type": "remark",
"uniqueId": "test-ctgB-1",
},
{
"attributes": undefined,
"child_features": undefined,
"data": undefined,
"derived_features": [],
"end": 6130,
"name": "f06",
"note": "This is another example",
"phase": undefined,
"refName": "ctgB",
"score": undefined,
"seq_id": undefined,
"source": "example",
"start": 3013,
"strand": 1,
"subfeatures": [],
"type": "remark",
"uniqueId": "test-ctgB-2",
},
{
"attributes": undefined,
"child_features": undefined,
"data": undefined,
"derived_features": [],
"end": 5968,
"name": "f05",
"note": "ああ、この機能は、世界中を旅しています!",
"phase": undefined,
"refName": "ctgB",
"score": undefined,
"seq_id": undefined,
"source": "example",
"start": 4714,
"strand": -1,
"subfeatures": [],
"type": "remark",
"uniqueId": "test-ctgB-3",
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,77 +3,61 @@
exports[`adapter can fetch features from volvox.gff3 test getfeatures on gff plain text adapter 1`] = `
[
{
"_linehash": "794328",
"attributes": undefined,
"child_features": undefined,
"data": undefined,
"derived_features": [],
"end": 6079,
"name": "ctgB",
"phase": undefined,
"refName": "ctgB",
"score": undefined,
"seq_id": undefined,
"source": "example",
"start": 0,
"strand": undefined,
"subfeatures": [],
"type": "contig",
"uniqueId": "test-offset-794328",
},
{
"_linehash": "794371",
"attributes": undefined,
"child_features": undefined,
"data": undefined,
"derived_features": [],
"end": 1984,
"name": "f07",
"note": "This is an example",
"phase": undefined,
"refName": "ctgB",
"score": undefined,
"seq_id": undefined,
"source": "example",
"start": 1658,
"strand": 1,
"subfeatures": [],
"type": "remark",
"uniqueId": "test-offset-794371",
},
{
"_linehash": "794440",
"attributes": undefined,
"child_features": undefined,
"data": undefined,
"derived_features": [],
"end": 6130,
"name": "f06",
"note": "This is another example",
"phase": undefined,
"refName": "ctgB",
"score": undefined,
"seq_id": undefined,
"source": "example",
"start": 3013,
"strand": 1,
"subfeatures": [],
"type": "remark",
"uniqueId": "test-offset-794440",
},
{
"_linehash": "794514",
"attributes": undefined,
"child_features": undefined,
"data": undefined,
"derived_features": [],
"end": 5968,
"name": "f05",
"note": "ああ、この機能は、世界中を旅しています!",
"phase": undefined,
"refName": "ctgB",
"score": undefined,
"seq_id": undefined,
"source": "example",
"start": 4714,
"strand": -1,
"subfeatures": [],
"type": "remark",
"uniqueId": "test-offset-794514",
},
Expand Down
86 changes: 53 additions & 33 deletions plugins/gff3/src/featureData.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,41 @@
import { GFF3FeatureLineWithRefs } from 'gff-nostream'

export function featureData(data: GFF3FeatureLineWithRefs) {
const f: Record<string, unknown> = { ...data }
;(f.start as number) -= 1 // convert to interbase
if (data.strand === '+') {
f.strand = 1
} else if (data.strand === '-') {
f.strand = -1
} else if (data.strand === '.') {
f.strand = 0
} else {
f.strand = undefined
}
f.phase = data.phase === null ? undefined : Number(data.phase)
f.refName = data.seq_id
if (data.score === null) {
f.score = undefined
interface GFF3Feature {
start: number
end: number
strand?: number
type: string | null
source: string | null
refName: string
derived_features: unknown[] | null
phase?: number
score?: number
subfeatures: GFF3Feature[] | undefined
[key: string]: unknown
}

export function featureData(data: GFF3FeatureLineWithRefs): GFF3Feature {
const {
end,
start,
child_features,
derived_features,
attributes,
type,
source,
phase,
seq_id,
score,
strand,
} = data

let strand2: number | undefined
if (strand === '+') {
strand2 = 1
} else if (strand === '-') {
strand2 = -1
} else if (strand === '.') {
strand2 = 0
}

const defaultFields = new Set([
Expand All @@ -28,37 +48,37 @@ export function featureData(data: GFF3FeatureLineWithRefs) {
'phase',
'strand',
])
const dataAttributes = data.attributes || {}
const dataAttributes = attributes || {}
const resultAttributes = {} as Record<string, unknown>
for (const a of Object.keys(dataAttributes)) {
let b = a.toLowerCase()
if (defaultFields.has(b)) {
// add "suffix" to tag name if it already exists
// reproduces behavior of NCList
b += '2'
}
if (dataAttributes[a]) {
if (dataAttributes[a] && a !== '_lineHash') {
let attr: string | string[] | undefined = dataAttributes[a]
if (Array.isArray(attr) && attr.length === 1) {
;[attr] = attr
}
f[b] = attr
resultAttributes[b] = attr
}
}
f.refName = f.seq_id

// the SimpleFeature constructor takes care of recursively inflating
// subfeatures
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (data.child_features && data.child_features.length > 0) {
f.subfeatures = data.child_features.flatMap(childLocs =>
return {
...resultAttributes,
start: start! - 1,
end: end!,
strand: strand2,
type,
source,
refName: seq_id!,
derived_features,
phase: phase === null ? undefined : Number(phase),
score: score === null ? undefined : score,
subfeatures: child_features?.flatMap(childLocs =>

Check failure on line 80 in plugins/gff3/src/featureData.ts

View workflow job for this annotation

GitHub Actions / Lint, typecheck, test

Unnecessary optional chain on a non-nullish value
childLocs.map(childLoc => featureData(childLoc)),
)
),
}

f.child_features = undefined
f.data = undefined
// delete f.derived_features
f.attributes = undefined
f.seq_id = undefined
return f
}
Loading

0 comments on commit e9fcddd

Please sign in to comment.