Skip to content

Commit

Permalink
Fix conversion of AnnotationFeature to GFF3Feature (for GFF3 export) (#…
Browse files Browse the repository at this point in the history
…487)

* Start adding test and possibly some fixes for #481

Conversion to GFF3Feature includes source and score and sets ID and
Parent attributes. However, export to GFF from UI fails.

* Fix imports

* Test expanded

* Process CDSs, tests extended

* Temporarily skip test

* Skip a few more tests temporarily

* Fix checks on feature import

---------

Co-authored-by: Garrett Stevens <[email protected]>
  • Loading branch information
dariober and garrettjstevens authored Dec 16, 2024
1 parent 0936c87 commit 1f5ddb4
Show file tree
Hide file tree
Showing 12 changed files with 560 additions and 115 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
RefSeqDocument,
} from '@apollo-annotation/schemas'
import {
makeGFF3Feature,
annotationFeatureToGFF3,
splitStringIntoChunks,
} from '@apollo-annotation/shared'
import gff from '@gmod/gff'
Expand Down Expand Up @@ -179,7 +179,7 @@ export class ExportService {
const refSeqNames = Object.fromEntries(
refSeqs.map((refSeq) => [refSeq._id, refSeq.name]),
)
const gff3Feature = makeGFF3Feature(
const gff3Feature = annotationFeatureToGFF3(
flattened as unknown as AnnotationFeatureSnapshot,
undefined,
refSeqNames,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ import { FeaturesService } from './features.service'
useFactory: (connection: Connection, checksService: ChecksService) => {
FeatureSchema.plugin(idValidator, { connection })
const runChecksOnDocument = async (doc: FeatureDocument) => {
await checksService.checkFeatures([doc])
await checksService.checkFeatures([doc], false)
}
const runChecksOnDocuments = async (docs: FeatureDocument[]) => {
await checksService.checkFeatures(docs)
await checksService.checkFeatures(docs, false)
}
FeatureSchema.post('save', runChecksOnDocument)
FeatureSchema.post('updateOne', runChecksOnDocument)
Expand Down
170 changes: 170 additions & 0 deletions packages/apollo-shared/src/GFF3/annotationFeatureToGFF3.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
/* eslint-disable prefer-destructuring */
/* eslint-disable @typescript-eslint/no-floating-promises */
import { describe, it } from 'node:test'
import { assert } from 'chai'
import { readAnnotationFeatureSnapshot } from './gff3ToAnnotationFeature.test'
import { annotationFeatureToGFF3 } from './annotationFeatureToGFF3'
import { AnnotationFeatureSnapshot } from '@apollo-annotation/mst'

describe('annotationFeatureToGFF3', () => {
it('Test mandatory columns', () => {
const annotationFeature = readAnnotationFeatureSnapshot(
'test_data/gene.json',
)
const [gff3Feature] = annotationFeatureToGFF3(annotationFeature)

assert.deepEqual(gff3Feature.seq_id, 'chr1')
assert.deepEqual(gff3Feature.type, 'gene')
assert.deepEqual(gff3Feature.start, 1000)
assert.deepEqual(gff3Feature.end, 9000)
assert.deepEqual(gff3Feature.strand, '+')
assert.deepEqual(gff3Feature.score, 123)
assert.deepEqual(gff3Feature.source, 'test_data')
})
it('Feature with no children and no gff_id has no ID attribute', () => {
const annotationFeature = JSON.parse(`{
"_id": "66d70e4ccc30b55b65e5f619",
"refSeq": "chr1",
"type": "gene",
"min": 999,
"max": 9000,
"strand": 1,
"attributes": {}
}`) as AnnotationFeatureSnapshot
const [gff3Feature] = annotationFeatureToGFF3(annotationFeature)
assert.isUndefined(gff3Feature.attributes?.ID)
})
it('Feature with children and no gff_id has internal _id as ID', () => {
const annotationFeature = JSON.parse(`{
"_id": "66d70e4ccc30b55b65e5f619",
"refSeq": "chr1",
"type": "gene",
"min": 999,
"max": 9000,
"strand": 1,
"attributes": {},
"children": {
"66d70e4ccc30b55b65e5f618": {
"_id": "66d70e4ccc30b55b65e5f618",
"refSeq": "chr1",
"type": "gene_segment",
"min": 1049,
"max": 9000,
"strand": 1,
"attributes": {}
}
}
}`) as AnnotationFeatureSnapshot
const [gff3Feature] = annotationFeatureToGFF3(annotationFeature)
assert.deepEqual(gff3Feature.attributes?.ID, ['66d70e4ccc30b55b65e5f619'])
})
it('Convert multiple scores', () => {
const annotationFeature = JSON.parse(`{
"_id": "66d70e4ccc30b55b65e5f619",
"refSeq": "chr1",
"type": "gene",
"min": 999,
"max": 9000,
"strand": 1,
"attributes": {
"gff_id": ["gene10001"],
"gff_score": ["123", "345"]
}
}`) as AnnotationFeatureSnapshot
const [gff3Feature] = annotationFeatureToGFF3(annotationFeature)
assert.deepEqual(gff3Feature.score, 123)
})
it('Convert invalid score', () => {
const annotationFeature = JSON.parse(`{
"_id": "66d70e4ccc30b55b65e5f619",
"refSeq": "chr1",
"type": "gene",
"min": 999,
"max": 9000,
"strand": 1,
"attributes": {
"gff_id": ["gene10001"],
"gff_score": ["xyz"]
}
}`) as AnnotationFeatureSnapshot
const [gff3Feature] = annotationFeatureToGFF3(annotationFeature)
assert.deepEqual(gff3Feature.score, null)
})
it('Convert one gene test attributes', () => {
const annotationFeature = readAnnotationFeatureSnapshot(
'test_data/gene.json',
)
const [gff3Feature] = annotationFeatureToGFF3(annotationFeature)

assert.deepEqual(gff3Feature.attributes?.Name, ['EDEN'])
assert.deepEqual(gff3Feature.attributes?.testid, ['t001', 't003'])
assert.deepEqual(gff3Feature.attributes?.ID, ['gene10001'])
assert.deepEqual(gff3Feature.attributes?.Ontology_term, [
'GO1234',
'GO4567',
'SO1234',
])
assert.deepEqual(gff3Feature.attributes?.Alias, ['myalias'])
assert.deepEqual(gff3Feature.attributes?.Target, ['mytarget'])
assert.deepEqual(gff3Feature.attributes?.Gap, ['mygap'])
assert.deepEqual(gff3Feature.attributes?.Derives_from, ['myderives'])
assert.deepEqual(gff3Feature.attributes?.Note, ['mynote'])
assert.deepEqual(gff3Feature.attributes?.Dbxref, ['mydbxref'])
assert.deepEqual(gff3Feature.attributes?.Is_circular, ['true'])
})
it('Convert one gene test children', () => {
const annotationFeature = readAnnotationFeatureSnapshot(
'test_data/gene.json',
)
const [gff3Feature] = annotationFeatureToGFF3(annotationFeature)
const [children] = gff3Feature.child_features
const [mrna] = children
assert.deepEqual(mrna.type, 'mRNA')
assert.deepEqual(mrna.attributes?.Parent, ['gene10001'])

const [cds] = mrna.child_features[2]
assert.deepEqual(cds.type, 'CDS')
assert.deepEqual(cds.attributes?.ID, ['cds10001'])
assert.deepEqual(cds.attributes?.Parent, ['mRNA10001'])
})
it('Convert CDSs', () => {
const annotationFeature = readAnnotationFeatureSnapshot(
'test_data/gene.json',
)
const [gff3Feature] = annotationFeatureToGFF3(annotationFeature)
const [children] = gff3Feature.child_features
const [mrna] = children
const cds10001 = mrna.child_features.filter((child) => {
const id = child[0].attributes?.ID
return id !== undefined && id[0] === 'cds10001'
})
assert.deepEqual(cds10001.length, 2)

const cds1_1 = cds10001[0][0]
assert.deepEqual(cds1_1.attributes?.ID, ['cds10001'])
assert.deepEqual(cds1_1.start, 1201)
assert.deepEqual(cds1_1.end, 1500)
assert.deepEqual(cds1_1.phase, '0')

const cds1_2 = cds10001[1][0]
assert.deepEqual(cds1_2.attributes?.ID, ['cds10001'])
assert.deepEqual(cds1_2.start, 5000)
assert.deepEqual(cds1_2.end, 5100)
assert.deepEqual(cds1_2.phase, '0')

assert.deepEqual(cds1_1.child_features[0][0].attributes?.ID, [
'cds_region10001',
])
assert.deepEqual(cds1_1.child_features[0][0].start, 1351)
assert.deepEqual(cds1_1.child_features[0][0].end, 1400)
assert.deepEqual(cds1_1.child_features[0][0].phase, null)

const cds10004 = mrna.child_features.filter((child) => {
const id = child[0].attributes?.ID
return id !== undefined && id[0] === 'cds10004'
})
assert.deepEqual(cds10004.length, 2)
const cds4_1 = cds10004[0][0]
assert.deepEqual(cds4_1.attributes?.ID, ['cds10004'])
})
})
Loading

0 comments on commit 1f5ddb4

Please sign in to comment.