Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions gtfparse/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from .attribute_parsing import expand_attribute_strings
from .create_missing_features import create_missing_features
from .parsing_error import ParsingError
from .write_gtf import write_gtf
from .read_gtf import (
read_gtf,
parse_gtf,
Expand Down
23 changes: 23 additions & 0 deletions gtfparse/write_gtf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import polars
from pathlib import Path
import typing as t


COMMONS_COL = ['seqname', 'source', 'feature', 'start', 'end', 'score', 'strand', 'frame']


def write_gtf(df: polars.DataFrame, export_path: str | Path, headers: t.List[str] = None):
headers = headers or []
with open(export_path, 'w') as f:
for header in headers:
f.write(f"{header}\n")
for row in df.iter_rows(named=True):
f.write(f"{commons_cols(row)}\t{custom_fields(row)}\n")


def commons_cols(row) -> str :
return "\t".join([str(row[field] or '.') for field in COMMONS_COL])


def custom_fields(row) -> str:
return "; ".join([f'{field} "{row[field]}"' for field in row.keys() if (field not in COMMONS_COL) and (row[field])])
11 changes: 11 additions & 0 deletions tests/test_write_gtf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from gtfparse import read_gtf, write_gtf
from .data import data_path
from polars import DataFrame

REFSEQ_GTF_PATH = data_path("refseq.ucsc.small.gtf")


def test_write_gtf(tmp_path):
gtf_dict = read_gtf(REFSEQ_GTF_PATH)
write_gtf(gtf_dict, tmp_path/"dummy_gtf.gtf")
assert isinstance(read_gtf(str(tmp_path/"dummy_gtf.gtf")), DataFrame)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check for equality of this gtf and the original one too ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it better now ?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup 👍