Skip to content

Commit

Permalink
Allow disabling derives from references (#57)
Browse files Browse the repository at this point in the history
* Use postversion instead of postpublish

* Updates
  • Loading branch information
cmdcolin committed Oct 19, 2022
1 parent 6cfa300 commit 1bf5ab9
Show file tree
Hide file tree
Showing 6 changed files with 157,740 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ dist
*.log
.vscode/
esm
.eslintcache
5 changes: 5 additions & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ export type {

/** Parser options */
export interface ParseOptions {
/** Whether to resolve references to derives from features */
disableDerivesFromReferences?: boolean
/** Text encoding of the input GFF3. default 'utf8' */
encoding?: BufferEncoding
/** Whether to parse features, default true */
Expand Down Expand Up @@ -63,6 +65,7 @@ function _processParseOptions(options: ParseOptions): ParseOptionsProcessed {
parseSequences: true,
parseComments: false,
bufferSize: 1000,
disableDerivesFromReferences: false,
...options,
}

Expand Down Expand Up @@ -98,6 +101,7 @@ class GFFTransform extends Transform {
sequenceCallback: options.parseSequences ? push : undefined,
errorCallback: (err) => this.emit('error', err),
bufferSize: options.bufferSize,
disableDerivesFromReferences: options.disableDerivesFromReferences,
})
}

Expand Down Expand Up @@ -462,6 +466,7 @@ export function parseStringSync(
directiveCallback: options.parseDirectives ? push : undefined,
commentCallback: options.parseComments ? push : undefined,
sequenceCallback: options.parseSequences ? push : undefined,
disableDerivesFromReferences: options.disableDerivesFromReferences || false,
bufferSize: Infinity,
errorCallback: (err) => {
throw err
Expand Down
10 changes: 8 additions & 2 deletions src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ interface ParserArgs {
directiveCallback?(directive: GFF3.GFF3Directive): void
sequenceCallback?(sequence: GFF3.GFF3Sequence): void
bufferSize?: number
disableDerivesFromReferences?: boolean
}

interface References {
Expand All @@ -56,6 +57,7 @@ export default class Parser {
endCallback: () => void
commentCallback: (comment: GFF3.GFF3Comment) => void
errorCallback: (error: string) => void
disableDerivesFromReferences: boolean
directiveCallback: (directive: GFF3.GFF3Directive) => void
sequenceCallback: (sequence: GFF3.GFF3Sequence) => void
bufferSize: number
Expand Down Expand Up @@ -94,6 +96,8 @@ export default class Parser {
this.errorCallback = args.errorCallback || nullFunc
this.directiveCallback = args.directiveCallback || nullFunc
this.sequenceCallback = args.sequenceCallback || nullFunc
this.disableDerivesFromReferences =
args.disableDerivesFromReferences || false

// number of lines to buffer
this.bufferSize = args.bufferSize === undefined ? 1000 : args.bufferSize
Expand Down Expand Up @@ -221,7 +225,7 @@ export default class Parser {
// problem. die with a parse error
if (Array.from(Object.values(this._underConstructionOrphans)).length) {
throw new Error(
`some features reference other features that do not exist in the file (or in the same '###' scope). ${JSON.stringify(
`some features reference other features that do not exist in the file (or in the same '###' scope). ${Object.keys(
this._underConstructionOrphans,
)}`,
)
Expand All @@ -241,7 +245,9 @@ export default class Parser {
// NOTE: a feature is an arrayref of one or more feature lines.
const ids = featureLine.attributes?.ID || []
const parents = featureLine.attributes?.Parent || []
const derives = featureLine.attributes?.Derives_from || []
const derives = this.disableDerivesFromReferences
? []
: featureLine.attributes?.Derives_from || []

if (!ids.length && !parents.length && !derives.length) {
// if it has no IDs and does not refer to anything, we can just
Expand Down
4 changes: 2 additions & 2 deletions test/data/tair10.gff3
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
1 TAIR10 chromosome 1 30427671 . . . ID=Chr1;Name=Chr1
1 TAIR10 chromosome 1 30427671 . . . ID=Chr1;Name=Chr1
1 TAIR10 gene 3631 5899 . + . ID=AT1G01010;Note=protein_coding_gene;Name=AT1G01010
1 TAIR10 mRNA 3631 5899 . + . ID=AT1G01010.1;Parent=AT1G01010;Name=AT1G01010.1;Index=1
1 TAIR10 protein 3760 5630 . + . ID=AT1G01010.1-Protein;Name=AT1G01010.1;Derives_from=AT1G01010.1
Expand All @@ -21,4 +21,4 @@
1 TAIR10 protein 6915 8666 . - . ID=AT1G01020.1-Protein;Name=AT1G01020.1;Derives_from=AT1G01020.1
1 TAIR10 five_prime_UTR 8667 8737 . - . Parent=AT1G01020.1
1 TAIR10 CDS 8571 8666 . - 0 Parent=AT1G01020.1,AT1G01020.1-Protein;
1 TAIR10 exon 8571 8737 . - . Parent=AT1G01020.1
1 TAIR10 exon 8571 8737 . - . Parent=AT1G01020.1
Loading

0 comments on commit 1bf5ab9

Please sign in to comment.