I/O — DataIterator & GFFWriter¶
DataIterator¶
Factory function that returns a streaming iterator over GFF3/GTF input
(file path, URL, raw string with from_string=True, or an iterable of
Feature objects).
gffbase.iterators.DataIterator ¶
DataIterator(data, checklines: int = 10, transform=None, force_dialect_check: bool = False, from_string: bool = False, **kwargs) -> _DataIterator
Legacy factory. Returns an iterator yielding Feature.
Dispatches on the input, the way the subclasses below have always
described and the way gffutils.DataIterator behaves:
from_string=True--datais the GFF text itself.- a URL -- fetched to a temporary file first (
_UrlIterator). - any other path-like -- read from disk, gzipped or not (
_FileIterator). - an iterable of
Feature/ParsedFeature-- yielded straight back (_FeatureIterator), so a generator can be piped intocreate_dborFeatureDB.updatewithout being written to a file first.
The dispatch was missing: every input went to _DataIterator, which
hands whatever it gets to parse_gff(path). So a URL was opened as a
filename and an in-memory feature list raised, while the subclasses that
exist to handle both sat unreachable and their docstrings described a
behaviour the factory did not have.
Source code in python/gffbase/iterators.py
GFFWriter¶
gffbase.gffwriter.GFFWriter ¶
Write Feature records back to a GFF/GTF file.
Source code in python/gffbase/gffwriter.py
write_rec ¶
Write one record, followed by a newline.
Parameters:
-
rec(Feature | str) –A
Feature, or a pre-formatted GFF line as a string. A trailing newline on a string is not doubled.
Source code in python/gffbase/gffwriter.py
write_recs ¶
Write many records, in the order given.
Parameters:
-
recs(Iterable) –An iterable of
Featureobjects or GFF line strings.
write_gene_recs ¶
Write a gene and its ENTIRE subtree, sorted by start.
Parameters:
-
db(FeatureDB) –The
FeatureDBto read from. -
gene_id(str | Feature) –The gene, as an id or a
Feature.
Source code in python/gffbase/gffwriter.py
write_mRNA_children ¶
Write a transcript and its DIRECT children, sorted by start.
Parameters:
-
db(FeatureDB) –The
FeatureDBto read from. -
mrna_id(str | Feature) –The transcript, as an id or a
Feature.
Source code in python/gffbase/gffwriter.py
write_exon_children ¶
Write an exon and its direct children, sorted by start.
Parameters:
-
db(FeatureDB) –The
FeatureDBto read from. -
exon_id(str | Feature) –The exon, as an id or a
Feature.
Source code in python/gffbase/gffwriter.py
close ¶
Flush, and close only a handle this writer opened.
A stream the caller passed in belongs to the caller. Closing it -- as
this used to, and as gffutils still does -- means
GFFWriter(sys.stdout) shuts stdout down for the whole process, so
anything written afterwards raises ValueError: I/O operation on closed
file. It is flushed instead, which is the part that actually matters
for the output being complete.
Source code in python/gffbase/gffwriter.py
export_sqlite¶
Serialize a GFFBase DuckDB connection back into a legacy
gffutils-compatible SQLite database.
gffbase.sqlite_export.export_sqlite ¶
Write a legacy SQLite .db from the given DuckDB connection.
The result is openable by real gffutils. Because gffutils has no way to
represent a discontinuous feature, one is flattened back into the N
features gffutils itself would have made -- see _legacy_ids. The grouping
is not lost: duplicates records (logical_id, legacy_id) for every
segment past the first, which is both what that table means and how a
re-import can rediscover it.
Returns the absolute path on success.
Source code in python/gffbase/sqlite_export.py
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 | |
Low-level parser¶
gffbase.parser.parse_gff ¶
parse_gff(path: str, *, checklines: int = 10, force_dialect_check: bool = False, force_gff: bool = False, strict: bool = True, validation: str = 'ncbi', engine: str | None = 'auto') -> _Iterator
Parse a GFF3/GTF file (plain text or .gz).
Returns an iterator of ParsedFeature plus .dialect(),
.directives() and .warnings accessors.
Parameters¶
validation : {"ncbi", "gffutils"}
Which rule set to apply. "ncbi" (default here) is the full GFF3
specification. "gffutils" is the compatibility profile used by
create_db: every rule still runs, but a violation annotates the
record instead of rejecting it, because real annotation files break
the spec routinely and gffutils reads them anyway.
strict : bool
What a rejection does. True (default) raises GFFFormatError on
the first offending line; False skips it and records it in
iterator.warnings. Under validation="gffutils" nothing is
rejected, so this only affects lines that cannot be parsed at all.
Source code in python/gffbase/parser.py
gffbase.parser.parse_bytes ¶
parse_bytes(data: bytes, *, checklines: int = 10, force_dialect_check: bool = False, force_gff: bool = False, strict: bool = True, validation: str = 'ncbi', engine: str | None = 'auto') -> _Iterator