gtfparse 3.0.4__tar.gz → 3.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {gtfparse-3.0.4/gtfparse.egg-info → gtfparse-3.1.0}/PKG-INFO +47 -30
- gtfparse-3.1.0/README.md +85 -0
- {gtfparse-3.0.4 → gtfparse-3.1.0}/benchmarks/compare_releases.py +2 -2
- {gtfparse-3.0.4 → gtfparse-3.1.0}/benchmarks/optimization.md +2 -3
- {gtfparse-3.0.4 → gtfparse-3.1.0}/gtfparse/__init__.py +1 -1
- {gtfparse-3.0.4 → gtfparse-3.1.0}/gtfparse/_read_csv.py +2 -14
- gtfparse-3.1.0/gtfparse/columns.py +77 -0
- {gtfparse-3.0.4 → gtfparse-3.1.0}/gtfparse/create_missing_features.py +5 -4
- {gtfparse-3.0.4 → gtfparse-3.1.0}/gtfparse/parsing_error.py +6 -1
- {gtfparse-3.0.4 → gtfparse-3.1.0}/gtfparse/read_gtf.py +44 -46
- {gtfparse-3.0.4 → gtfparse-3.1.0}/gtfparse/write_gtf.py +4 -23
- {gtfparse-3.0.4 → gtfparse-3.1.0/gtfparse.egg-info}/PKG-INFO +47 -30
- {gtfparse-3.0.4 → gtfparse-3.1.0}/gtfparse.egg-info/SOURCES.txt +2 -0
- {gtfparse-3.0.4 → gtfparse-3.1.0}/gtfparse.egg-info/requires.txt +1 -1
- {gtfparse-3.0.4 → gtfparse-3.1.0}/pyproject.toml +5 -5
- {gtfparse-3.0.4 → gtfparse-3.1.0}/requirements.txt +1 -1
- {gtfparse-3.0.4 → gtfparse-3.1.0}/tests/test_attribute_optimization.py +2 -3
- gtfparse-3.1.0/tests/test_columns.py +25 -0
- {gtfparse-3.0.4 → gtfparse-3.1.0}/tests/test_create_missing_features.py +57 -0
- {gtfparse-3.0.4 → gtfparse-3.1.0}/tests/test_gencode_gtf.py +1 -3
- gtfparse-3.0.4/README.md +0 -68
- {gtfparse-3.0.4 → gtfparse-3.1.0}/LICENSE +0 -0
- {gtfparse-3.0.4 → gtfparse-3.1.0}/MANIFEST.in +0 -0
- {gtfparse-3.0.4 → gtfparse-3.1.0}/benchmarks/README.md +0 -0
- {gtfparse-3.0.4 → gtfparse-3.1.0}/benchmarks/compare_readers.py +0 -0
- {gtfparse-3.0.4 → gtfparse-3.1.0}/benchmarks/optimization-results.json +0 -0
- {gtfparse-3.0.4 → gtfparse-3.1.0}/benchmarks/results.json +0 -0
- {gtfparse-3.0.4 → gtfparse-3.1.0}/gtfparse/_polars.py +0 -0
- {gtfparse-3.0.4 → gtfparse-3.1.0}/gtfparse/attribute_parsing.py +0 -0
- {gtfparse-3.0.4 → gtfparse-3.1.0}/gtfparse.egg-info/dependency_links.txt +0 -0
- {gtfparse-3.0.4 → gtfparse-3.1.0}/gtfparse.egg-info/top_level.txt +0 -0
- {gtfparse-3.0.4 → gtfparse-3.1.0}/lint.sh +0 -0
- {gtfparse-3.0.4 → gtfparse-3.1.0}/setup.cfg +0 -0
- {gtfparse-3.0.4 → gtfparse-3.1.0}/test.sh +0 -0
- {gtfparse-3.0.4 → gtfparse-3.1.0}/tests/__init__.py +0 -0
- {gtfparse-3.0.4 → gtfparse-3.1.0}/tests/data/B16.stringtie.head.gtf +0 -0
- {gtfparse-3.0.4 → gtfparse-3.1.0}/tests/data/ensembl_grch37.head.gtf +0 -0
- {gtfparse-3.0.4 → gtfparse-3.1.0}/tests/data/ensembl_grch37.head.gtf.gz +0 -0
- {gtfparse-3.0.4 → gtfparse-3.1.0}/tests/data/gencode.head.gtf +0 -0
- {gtfparse-3.0.4 → gtfparse-3.1.0}/tests/data/gencode.real.head.gtf +0 -0
- {gtfparse-3.0.4 → gtfparse-3.1.0}/tests/data/refseq.ucsc.small.gtf +0 -0
- {gtfparse-3.0.4 → gtfparse-3.1.0}/tests/data.py +0 -0
- {gtfparse-3.0.4 → gtfparse-3.1.0}/tests/test_attribute_values.py +0 -0
- {gtfparse-3.0.4 → gtfparse-3.1.0}/tests/test_ensembl_gtf.py +0 -0
- {gtfparse-3.0.4 → gtfparse-3.1.0}/tests/test_expand_attribute_column_false.py +0 -0
- {gtfparse-3.0.4 → gtfparse-3.1.0}/tests/test_expand_attributes.py +0 -0
- {gtfparse-3.0.4 → gtfparse-3.1.0}/tests/test_multiple_values_for_tag_attribute.py +0 -0
- {gtfparse-3.0.4 → gtfparse-3.1.0}/tests/test_parse_gtf_lines.py +0 -0
- {gtfparse-3.0.4 → gtfparse-3.1.0}/tests/test_polars_compatibility.py +0 -0
- {gtfparse-3.0.4 → gtfparse-3.1.0}/tests/test_progress.py +0 -0
- {gtfparse-3.0.4 → gtfparse-3.1.0}/tests/test_read_stringtie_gtf.py +0 -0
- {gtfparse-3.0.4 → gtfparse-3.1.0}/tests/test_reader.py +0 -0
- {gtfparse-3.0.4 → gtfparse-3.1.0}/tests/test_refseq_gtf.py +0 -0
- {gtfparse-3.0.4 → gtfparse-3.1.0}/tests/test_write_gtf.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: gtfparse
|
|
3
|
-
Version: 3.0
|
|
3
|
+
Version: 3.1.0
|
|
4
4
|
Summary: Parsing library for extracting data frames of genomic features from GTF files
|
|
5
5
|
Author-email: Alex Rubinsteyn <alex.rubinsteyn@unc.edu>
|
|
6
6
|
License-Expression: Apache-2.0
|
|
@@ -12,16 +12,16 @@ Classifier: Operating System :: OS Independent
|
|
|
12
12
|
Classifier: Intended Audience :: Science/Research
|
|
13
13
|
Classifier: Programming Language :: Python
|
|
14
14
|
Classifier: Programming Language :: Python :: 3
|
|
15
|
-
Classifier: Programming Language :: Python :: 3.9
|
|
16
|
-
Classifier: Programming Language :: Python :: 3.10
|
|
17
15
|
Classifier: Programming Language :: Python :: 3.11
|
|
18
16
|
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
19
19
|
Classifier: Topic :: Scientific/Engineering :: Bio-Informatics
|
|
20
|
-
Requires-Python: >=3.
|
|
20
|
+
Requires-Python: >=3.11
|
|
21
21
|
Description-Content-Type: text/markdown
|
|
22
22
|
License-File: LICENSE
|
|
23
23
|
Requires-Dist: pyarrow>=18.0.0
|
|
24
|
-
Requires-Dist: pandas
|
|
24
|
+
Requires-Dist: pandas<4,>=3
|
|
25
25
|
Provides-Extra: polars
|
|
26
26
|
Requires-Dist: polars>=0.20.31; extra == "polars"
|
|
27
27
|
Provides-Extra: dev
|
|
@@ -31,17 +31,18 @@ Requires-Dist: ruff; extra == "dev"
|
|
|
31
31
|
Requires-Dist: coveralls; extra == "dev"
|
|
32
32
|
Dynamic: license-file
|
|
33
33
|
|
|
34
|
+
# gtfparse
|
|
35
|
+
|
|
34
36
|
[](https://github.com/openvax/gtfparse/actions/workflows/tests.yml)
|
|
35
37
|
[](https://coveralls.io/github/openvax/gtfparse?branch=master)
|
|
36
|
-
|
|
37
|
-
<img src="https://img.shields.io/pypi/v/gtfparse.svg?maxAge=1000" alt="PyPI" />
|
|
38
|
-
</a>
|
|
38
|
+
[](https://pypi.org/project/gtfparse/)
|
|
39
39
|
|
|
40
|
-
gtfparse
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
40
|
+
gtfparse reads GTF (Gene Transfer Format) annotation files, such as those from
|
|
41
|
+
Ensembl and GENCODE, into pandas DataFrames: one row per feature, with
|
|
42
|
+
attributes such as gene and transcript IDs expanded into columns. It can also
|
|
43
|
+
write a table back to GTF. In the [OpenVax](https://github.com/openvax) tools,
|
|
44
|
+
[PyEnsembl](https://github.com/openvax/pyensembl) uses it to read Ensembl and
|
|
45
|
+
custom annotations before indexing them.
|
|
45
46
|
|
|
46
47
|
## Install
|
|
47
48
|
|
|
@@ -49,13 +50,14 @@ into columns.
|
|
|
49
50
|
python -m pip install gtfparse
|
|
50
51
|
```
|
|
51
52
|
|
|
52
|
-
Python 3.
|
|
53
|
-
`gtfparse[polars]`.
|
|
53
|
+
Python 3.11 or later is required. gtfparse depends on pandas and PyArrow; for
|
|
54
|
+
optional Polars output, install `gtfparse[polars]`.
|
|
54
55
|
|
|
55
|
-
##
|
|
56
|
+
## Quick start
|
|
56
57
|
|
|
57
|
-
|
|
58
|
-
|
|
58
|
+
<a id="read-an-annotation-and-find-genes"></a><a id="example-usage"></a><a id="parsing-all-rows-of-a-gtf-file-into-a-pandas-dataframe"></a>This
|
|
59
|
+
small artificial GTF runs without a download. Replace the stream with your
|
|
60
|
+
annotation filename when working with a real dataset.
|
|
59
61
|
|
|
60
62
|
```python
|
|
61
63
|
from io import StringIO
|
|
@@ -80,22 +82,37 @@ exon 1 101 150 g1
|
|
|
80
82
|
|
|
81
83
|
`feature` identifies the row type. `seqname` is the chromosome or contig name;
|
|
82
84
|
it is not the gene ID. The ninth GTF field supplies attributes such as
|
|
83
|
-
`gene_id`, `gene_name` and `transcript_id`.
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
<a id="version-3-migration"></a>
|
|
87
|
-
<a id="reporting-parsing-progress"></a>
|
|
88
|
-
<a id="example-usage"></a>
|
|
89
|
-
<a id="parsing-all-rows-of-a-gtf-file-into-a-pandas-dataframe"></a>
|
|
90
|
-
<a id="getting-gene-fpkm-values-from-a-stringtie-gtf-file"></a>
|
|
85
|
+
`gene_id`, `gene_name` and `transcript_id`. Coordinates stay one-based and
|
|
86
|
+
inclusive, as in the file.
|
|
91
87
|
|
|
92
88
|
## Documentation
|
|
93
89
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
-
|
|
90
|
+
The guides are Markdown files in
|
|
91
|
+
[`docs/`](https://github.com/openvax/gtfparse/tree/master/docs). In reading order:
|
|
92
|
+
|
|
93
|
+
1. <a id="getting-gene-fpkm-values-from-a-stringtie-gtf-file"></a>[Filter and write annotations](https://github.com/openvax/gtfparse/blob/master/docs/guides/annotations.md):
|
|
94
|
+
select chromosomes and features, write GTF and read numeric attributes.
|
|
95
|
+
2. <a id="reporting-parsing-progress"></a>[Report parsing progress](https://github.com/openvax/gtfparse/blob/master/docs/guides/progress.md)
|
|
96
|
+
with your own progress bar.
|
|
97
|
+
3. <a id="version-3-migration"></a>[Version 3 migration](https://github.com/openvax/gtfparse/blob/master/docs/guides/migration.md):
|
|
98
|
+
pandas by default, and optional Polars output.
|
|
99
|
+
4. [API reference](https://github.com/openvax/gtfparse/blob/master/docs/reference.md)
|
|
100
|
+
for every public function.
|
|
98
101
|
|
|
99
102
|
To build the documentation locally, run
|
|
100
103
|
`python -m pip install -r requirements-docs.txt` and `./docs.sh`.
|
|
101
104
|
`python scripts/check_docs_examples.py` checks the printed example output.
|
|
105
|
+
|
|
106
|
+
## Development
|
|
107
|
+
|
|
108
|
+
```sh
|
|
109
|
+
python -m pip install -e ".[dev]"
|
|
110
|
+
./lint.sh # ruff check and ruff format --check
|
|
111
|
+
./test.sh # pytest with coverage
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
`./format.sh` applies ruff formatting.
|
|
115
|
+
|
|
116
|
+
## License
|
|
117
|
+
|
|
118
|
+
Apache 2.0; see [LICENSE](LICENSE).
|
gtfparse-3.1.0/README.md
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
# gtfparse
|
|
2
|
+
|
|
3
|
+
[](https://github.com/openvax/gtfparse/actions/workflows/tests.yml)
|
|
4
|
+
[](https://coveralls.io/github/openvax/gtfparse?branch=master)
|
|
5
|
+
[](https://pypi.org/project/gtfparse/)
|
|
6
|
+
|
|
7
|
+
gtfparse reads GTF (Gene Transfer Format) annotation files, such as those from
|
|
8
|
+
Ensembl and GENCODE, into pandas DataFrames: one row per feature, with
|
|
9
|
+
attributes such as gene and transcript IDs expanded into columns. It can also
|
|
10
|
+
write a table back to GTF. In the [OpenVax](https://github.com/openvax) tools,
|
|
11
|
+
[PyEnsembl](https://github.com/openvax/pyensembl) uses it to read Ensembl and
|
|
12
|
+
custom annotations before indexing them.
|
|
13
|
+
|
|
14
|
+
## Install
|
|
15
|
+
|
|
16
|
+
```sh
|
|
17
|
+
python -m pip install gtfparse
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Python 3.11 or later is required. gtfparse depends on pandas and PyArrow; for
|
|
21
|
+
optional Polars output, install `gtfparse[polars]`.
|
|
22
|
+
|
|
23
|
+
## Quick start
|
|
24
|
+
|
|
25
|
+
<a id="read-an-annotation-and-find-genes"></a><a id="example-usage"></a><a id="parsing-all-rows-of-a-gtf-file-into-a-pandas-dataframe"></a>This
|
|
26
|
+
small artificial GTF runs without a download. Replace the stream with your
|
|
27
|
+
annotation filename when working with a real dataset.
|
|
28
|
+
|
|
29
|
+
```python
|
|
30
|
+
from io import StringIO
|
|
31
|
+
from gtfparse import read_gtf
|
|
32
|
+
|
|
33
|
+
annotation = StringIO(
|
|
34
|
+
'1\texample\tgene\t101\t200\t.\t+\t.\tgene_id "g1"; gene_name "EXAMPLE";\n'
|
|
35
|
+
'1\texample\texon\t101\t150\t.\t+\t.\tgene_id "g1"; transcript_id "t1";\n'
|
|
36
|
+
)
|
|
37
|
+
features = read_gtf(annotation)
|
|
38
|
+
genes = features.loc[features["feature"] == "gene"]
|
|
39
|
+
print(genes["gene_name"].tolist())
|
|
40
|
+
for row in features.itertuples():
|
|
41
|
+
print(row.feature, row.seqname, row.start, row.end, row.gene_id)
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
```text
|
|
45
|
+
['EXAMPLE']
|
|
46
|
+
gene 1 101 200 g1
|
|
47
|
+
exon 1 101 150 g1
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
`feature` identifies the row type. `seqname` is the chromosome or contig name;
|
|
51
|
+
it is not the gene ID. The ninth GTF field supplies attributes such as
|
|
52
|
+
`gene_id`, `gene_name` and `transcript_id`. Coordinates stay one-based and
|
|
53
|
+
inclusive, as in the file.
|
|
54
|
+
|
|
55
|
+
## Documentation
|
|
56
|
+
|
|
57
|
+
The guides are Markdown files in
|
|
58
|
+
[`docs/`](https://github.com/openvax/gtfparse/tree/master/docs). In reading order:
|
|
59
|
+
|
|
60
|
+
1. <a id="getting-gene-fpkm-values-from-a-stringtie-gtf-file"></a>[Filter and write annotations](https://github.com/openvax/gtfparse/blob/master/docs/guides/annotations.md):
|
|
61
|
+
select chromosomes and features, write GTF and read numeric attributes.
|
|
62
|
+
2. <a id="reporting-parsing-progress"></a>[Report parsing progress](https://github.com/openvax/gtfparse/blob/master/docs/guides/progress.md)
|
|
63
|
+
with your own progress bar.
|
|
64
|
+
3. <a id="version-3-migration"></a>[Version 3 migration](https://github.com/openvax/gtfparse/blob/master/docs/guides/migration.md):
|
|
65
|
+
pandas by default, and optional Polars output.
|
|
66
|
+
4. [API reference](https://github.com/openvax/gtfparse/blob/master/docs/reference.md)
|
|
67
|
+
for every public function.
|
|
68
|
+
|
|
69
|
+
To build the documentation locally, run
|
|
70
|
+
`python -m pip install -r requirements-docs.txt` and `./docs.sh`.
|
|
71
|
+
`python scripts/check_docs_examples.py` checks the printed example output.
|
|
72
|
+
|
|
73
|
+
## Development
|
|
74
|
+
|
|
75
|
+
```sh
|
|
76
|
+
python -m pip install -e ".[dev]"
|
|
77
|
+
./lint.sh # ruff check and ruff format --check
|
|
78
|
+
./test.sh # pytest with coverage
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
`./format.sh` applies ruff formatting.
|
|
82
|
+
|
|
83
|
+
## License
|
|
84
|
+
|
|
85
|
+
Apache 2.0; see [LICENSE](LICENSE).
|
|
@@ -11,7 +11,7 @@ import os
|
|
|
11
11
|
import statistics
|
|
12
12
|
import subprocess
|
|
13
13
|
import sys
|
|
14
|
-
from datetime import
|
|
14
|
+
from datetime import UTC, datetime
|
|
15
15
|
from pathlib import Path
|
|
16
16
|
|
|
17
17
|
|
|
@@ -120,7 +120,7 @@ def main():
|
|
|
120
120
|
)
|
|
121
121
|
result = {
|
|
122
122
|
"description": "Complete production readers; imports/checksums outside wall time; sequential fresh processes",
|
|
123
|
-
"date": datetime.now(
|
|
123
|
+
"date": datetime.now(UTC).isoformat(),
|
|
124
124
|
"threads": args.threads,
|
|
125
125
|
"inputs": {
|
|
126
126
|
path.name: {
|
|
@@ -143,12 +143,11 @@ repeated/empty values, pre-split rows, missing-value sentinels and column
|
|
|
143
143
|
selection. Additional tests check configured Python/Arrow string storage, batch
|
|
144
144
|
boundaries, late-discovered columns, missing batches, fixed-field attribute
|
|
145
145
|
collisions, first-seen order and global progress counts. The batch regression
|
|
146
|
-
|
|
147
|
-
exercised there; default object-string behavior has separate coverage. Existing tests cover streams, null fields, exact
|
|
146
|
+
selects each pandas string storage backend so both are exercised there; default object-string behavior has separate coverage. Existing tests cover streams, null fields, exact
|
|
148
147
|
integer coordinates, comments/BOM, aliases, converters, progress/cancellation,
|
|
149
148
|
optional Polars, dictionary output and writer round trips.
|
|
150
149
|
|
|
151
150
|
The verification sweep compares 71 full API/dtype combinations on each pandas
|
|
152
151
|
version against 3.0.0, including all five real fixtures and configured string
|
|
153
|
-
storage. CI exercises Python 3.
|
|
152
|
+
storage. CI now exercises Python 3.11–3.14, pandas 3 (minimum and current) and Arrow, the base
|
|
154
153
|
installation without Polars, optional Polars versions and packaged source tests.
|
|
@@ -9,21 +9,9 @@ import pyarrow as pa
|
|
|
9
9
|
import pyarrow.compute as pc
|
|
10
10
|
import pyarrow.csv as pacsv
|
|
11
11
|
|
|
12
|
+
from .columns import CATEGORICAL_COLUMNS, MISSING_VALUE, REQUIRED_COLUMNS
|
|
12
13
|
from .parsing_error import ParsingError
|
|
13
14
|
|
|
14
|
-
REQUIRED_COLUMNS = [
|
|
15
|
-
"seqname",
|
|
16
|
-
"source",
|
|
17
|
-
"feature",
|
|
18
|
-
"start",
|
|
19
|
-
"end",
|
|
20
|
-
"score",
|
|
21
|
-
"strand",
|
|
22
|
-
"frame",
|
|
23
|
-
"attribute",
|
|
24
|
-
]
|
|
25
|
-
CATEGORICAL_COLUMNS = ("seqname", "source", "feature", "strand")
|
|
26
|
-
|
|
27
15
|
|
|
28
16
|
class _PrefixedReader:
|
|
29
17
|
def __init__(self, stream, prefix):
|
|
@@ -124,7 +112,7 @@ def read_fixed_columns(source, features=None):
|
|
|
124
112
|
read_options=pacsv.ReadOptions(column_names=REQUIRED_COLUMNS),
|
|
125
113
|
parse_options=pacsv.ParseOptions(delimiter="\t", quote_char=False),
|
|
126
114
|
convert_options=pacsv.ConvertOptions(
|
|
127
|
-
column_types=types, null_values=[
|
|
115
|
+
column_types=types, null_values=[MISSING_VALUE, ""], strings_can_be_null=True
|
|
128
116
|
),
|
|
129
117
|
)
|
|
130
118
|
except pa.ArrowInvalid as error:
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
2
|
+
# you may not use this file except in compliance with the License.
|
|
3
|
+
# You may obtain a copy of the License at
|
|
4
|
+
#
|
|
5
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
+
#
|
|
7
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
8
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
9
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
10
|
+
# See the License for the specific language governing permissions and
|
|
11
|
+
# limitations under the License.
|
|
12
|
+
|
|
13
|
+
"""
|
|
14
|
+
The nine tab-separated columns of a GTF file, shared by the reader and writer.
|
|
15
|
+
|
|
16
|
+
seqname - name of the chromosome or scaffold; chromosome names
|
|
17
|
+
without a 'chr' in Ensembl (but sometimes with a 'chr'
|
|
18
|
+
elsewhere)
|
|
19
|
+
source - name of the program that generated this feature, or
|
|
20
|
+
the data source (database or project name)
|
|
21
|
+
feature - feature type name.
|
|
22
|
+
Features currently in Ensembl GTFs:
|
|
23
|
+
gene
|
|
24
|
+
transcript
|
|
25
|
+
exon
|
|
26
|
+
CDS
|
|
27
|
+
Selenocysteine
|
|
28
|
+
start_codon
|
|
29
|
+
stop_codon
|
|
30
|
+
UTR
|
|
31
|
+
Older Ensembl releases may be missing some of these features.
|
|
32
|
+
start - start position of the feature, with sequence numbering
|
|
33
|
+
starting at 1.
|
|
34
|
+
end - end position of the feature, with sequence numbering
|
|
35
|
+
starting at 1.
|
|
36
|
+
score - a floating point value indicating the score of a feature
|
|
37
|
+
strand - defined as + (forward) or - (reverse).
|
|
38
|
+
frame - one of '0', '1' or '2'. Frame indicates the number of base pairs
|
|
39
|
+
before you encounter a full codon. '0' indicates the feature
|
|
40
|
+
begins with a whole codon. '1' indicates there is an extra
|
|
41
|
+
base (the 3rd base of the prior codon) at the start of this feature.
|
|
42
|
+
'2' indicates there are two extra bases (2nd and 3rd base of the
|
|
43
|
+
prior exon) before the first codon. All values are given with
|
|
44
|
+
relation to the 5' end.
|
|
45
|
+
attribute - a semicolon-separated list of tag-value pairs (separated by a space),
|
|
46
|
+
providing additional information about each feature. A key can be
|
|
47
|
+
repeated multiple times.
|
|
48
|
+
|
|
49
|
+
(from ftp://ftp.ensembl.org/pub/release-75/gtf/homo_sapiens/README)
|
|
50
|
+
"""
|
|
51
|
+
|
|
52
|
+
# The eight tab-separated columns that precede the attribute field of a GTF
|
|
53
|
+
# line, in the order they must be written. Any other column in a DataFrame is
|
|
54
|
+
# treated as an expanded attribute (see read_gtf's expand_attribute_column).
|
|
55
|
+
GTF_FIXED_COLUMNS = [
|
|
56
|
+
"seqname",
|
|
57
|
+
"source",
|
|
58
|
+
"feature",
|
|
59
|
+
"start",
|
|
60
|
+
"end",
|
|
61
|
+
"score",
|
|
62
|
+
"strand",
|
|
63
|
+
"frame",
|
|
64
|
+
]
|
|
65
|
+
|
|
66
|
+
# Name of the ninth, raw attribute column, which read_gtf keeps unexpanded
|
|
67
|
+
# when called with expand_attribute_column=False.
|
|
68
|
+
RAW_ATTRIBUTE_COLUMN = "attribute"
|
|
69
|
+
|
|
70
|
+
# All nine GTF columns in file order, as parse_gtf returns them.
|
|
71
|
+
REQUIRED_COLUMNS = [*GTF_FIXED_COLUMNS, RAW_ATTRIBUTE_COLUMN]
|
|
72
|
+
|
|
73
|
+
# Fixed columns with few distinct values, which the reader stores as categoricals.
|
|
74
|
+
CATEGORICAL_COLUMNS = ("seqname", "source", "feature", "strand")
|
|
75
|
+
|
|
76
|
+
# GTF uses a single dot to denote a missing value in the fixed columns.
|
|
77
|
+
MISSING_VALUE = "."
|
|
@@ -64,10 +64,11 @@ def create_missing_features(dataframe, unique_keys={}, extra_columns={}, missing
|
|
|
64
64
|
continue
|
|
65
65
|
logger.info("Creating rows for missing feature '%s'", feature_name)
|
|
66
66
|
|
|
67
|
-
# don't include rows where the groupby key was missing
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
67
|
+
# don't include rows where the groupby key was missing; the mask keeps
|
|
68
|
+
# the frame's own index, so filtered or re-sorted frames work (#104)
|
|
69
|
+
keys = dataframe[groupby_key]
|
|
70
|
+
has_key = keys.notna() & (keys != "")
|
|
71
|
+
row_groups = dataframe[has_key].groupby(groupby_key)
|
|
71
72
|
|
|
72
73
|
# Each group corresponds to a unique feature entry for which the
|
|
73
74
|
# other columns may or may not be uniquely defined. Start off by
|
|
@@ -12,4 +12,9 @@
|
|
|
12
12
|
|
|
13
13
|
|
|
14
14
|
class ParsingError(Exception):
|
|
15
|
-
|
|
15
|
+
"""Raised when GTF input is empty or malformed.
|
|
16
|
+
|
|
17
|
+
For example, a non-comment line without exactly nine tab-separated fields,
|
|
18
|
+
or a fixed column (such as ``start``) whose value can't be converted to the
|
|
19
|
+
column's type.
|
|
20
|
+
"""
|
|
@@ -17,9 +17,9 @@ import pandas as pd
|
|
|
17
17
|
import pyarrow as pa
|
|
18
18
|
|
|
19
19
|
from ._polars import require_polars
|
|
20
|
-
from ._read_csv import REQUIRED_COLUMNS as REQUIRED_COLUMNS
|
|
21
20
|
from ._read_csv import read_fixed_columns
|
|
22
21
|
from .attribute_parsing import ATTRIBUTE_PATTERN, expand_attribute_strings
|
|
22
|
+
from .columns import REQUIRED_COLUMNS as REQUIRED_COLUMNS
|
|
23
23
|
|
|
24
24
|
logger = logging.getLogger(__name__)
|
|
25
25
|
|
|
@@ -45,46 +45,6 @@ INTEGER_VERSION_COLUMNS = (
|
|
|
45
45
|
)
|
|
46
46
|
|
|
47
47
|
|
|
48
|
-
"""
|
|
49
|
-
Columns of a GTF file:
|
|
50
|
-
|
|
51
|
-
seqname - name of the chromosome or scaffold; chromosome names
|
|
52
|
-
without a 'chr' in Ensembl (but sometimes with a 'chr'
|
|
53
|
-
elsewhere)
|
|
54
|
-
source - name of the program that generated this feature, or
|
|
55
|
-
the data source (database or project name)
|
|
56
|
-
feature - feature type name.
|
|
57
|
-
Features currently in Ensembl GTFs:
|
|
58
|
-
gene
|
|
59
|
-
transcript
|
|
60
|
-
exon
|
|
61
|
-
CDS
|
|
62
|
-
Selenocysteine
|
|
63
|
-
start_codon
|
|
64
|
-
stop_codon
|
|
65
|
-
UTR
|
|
66
|
-
Older Ensembl releases may be missing some of these features.
|
|
67
|
-
start - start position of the feature, with sequence numbering
|
|
68
|
-
starting at 1.
|
|
69
|
-
end - end position of the feature, with sequence numbering
|
|
70
|
-
starting at 1.
|
|
71
|
-
score - a floating point value indiciating the score of a feature
|
|
72
|
-
strand - defined as + (forward) or - (reverse).
|
|
73
|
-
frame - one of '0', '1' or '2'. Frame indicates the number of base pairs
|
|
74
|
-
before you encounter a full codon. '0' indicates the feature
|
|
75
|
-
begins with a whole codon. '1' indicates there is an extra
|
|
76
|
-
base (the 3rd base of the prior codon) at the start of this feature.
|
|
77
|
-
'2' indicates there are two extra bases (2nd and 3rd base of the
|
|
78
|
-
prior exon) before the first codon. All values are given with
|
|
79
|
-
relation to the 5' end.
|
|
80
|
-
attribute - a semicolon-separated list of tag-value pairs (separated by a space),
|
|
81
|
-
providing additional information about each feature. A key can be
|
|
82
|
-
repeated multiple times.
|
|
83
|
-
|
|
84
|
-
(from ftp://ftp.ensembl.org/pub/release-75/gtf/homo_sapiens/README)
|
|
85
|
-
"""
|
|
86
|
-
|
|
87
|
-
|
|
88
48
|
def parse_with_polars_lazy(
|
|
89
49
|
filepath_or_buffer, split_attributes=True, features=None, fix_quotes_columns=None
|
|
90
50
|
):
|
|
@@ -109,10 +69,34 @@ def parse_gtf(
|
|
|
109
69
|
):
|
|
110
70
|
"""Read the fixed GTF columns, preserving raw attribute text.
|
|
111
71
|
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
72
|
+
Parameters
|
|
73
|
+
----------
|
|
74
|
+
filepath_or_buffer : str, path-like, or binary/text buffer
|
|
75
|
+
GTF file (may be gzip compressed) or an open stream such as StringIO.
|
|
76
|
+
|
|
77
|
+
split_attributes : bool
|
|
78
|
+
If True, add an ``attribute_split`` list column with the quoted
|
|
79
|
+
key/value pairs of each row's attribute field kept intact.
|
|
80
|
+
|
|
81
|
+
features : set of str or None
|
|
82
|
+
Keep only rows whose ``feature`` is in this set.
|
|
83
|
+
|
|
84
|
+
fix_quotes_columns : list of str or None
|
|
85
|
+
Legacy semicolon cleanup for these columns; disabled by default.
|
|
86
|
+
|
|
87
|
+
progress_callback : callable, optional
|
|
88
|
+
Follows read_gtf's callback contract and emits only the ``read`` stage.
|
|
89
|
+
|
|
90
|
+
Returns
|
|
91
|
+
-------
|
|
92
|
+
pandas.DataFrame
|
|
93
|
+
One row per feature with the nine columns in ``REQUIRED_COLUMNS``,
|
|
94
|
+
plus ``attribute_split`` when requested.
|
|
95
|
+
|
|
96
|
+
Raises
|
|
97
|
+
------
|
|
98
|
+
ParsingError
|
|
99
|
+
If the input is empty or a line is malformed.
|
|
116
100
|
"""
|
|
117
101
|
if progress_callback is not None:
|
|
118
102
|
progress_callback("read", 0, None)
|
|
@@ -137,7 +121,21 @@ def parse_gtf(
|
|
|
137
121
|
|
|
138
122
|
|
|
139
123
|
def parse_gtf_pandas(*args, **kwargs):
|
|
140
|
-
"""Like parse_gtf,
|
|
124
|
+
"""Like parse_gtf, also reporting a ``convert`` progress stage.
|
|
125
|
+
|
|
126
|
+
Kept for compatibility from when parse_gtf returned Polars; parse_gtf now
|
|
127
|
+
returns pandas itself, so the conversion stage does no work.
|
|
128
|
+
|
|
129
|
+
Parameters
|
|
130
|
+
----------
|
|
131
|
+
*args, **kwargs
|
|
132
|
+
Passed to parse_gtf unchanged, including ``progress_callback``.
|
|
133
|
+
|
|
134
|
+
Returns
|
|
135
|
+
-------
|
|
136
|
+
pandas.DataFrame
|
|
137
|
+
The same frame parse_gtf returns.
|
|
138
|
+
"""
|
|
141
139
|
df = parse_gtf(*args, **kwargs)
|
|
142
140
|
progress_callback = kwargs.get("progress_callback")
|
|
143
141
|
if progress_callback is not None:
|
|
@@ -16,36 +16,17 @@ import sys
|
|
|
16
16
|
from collections.abc import Iterable
|
|
17
17
|
from itertools import repeat
|
|
18
18
|
from pathlib import Path
|
|
19
|
-
from typing import TYPE_CHECKING,
|
|
19
|
+
from typing import TYPE_CHECKING, Union
|
|
20
20
|
|
|
21
21
|
import pandas as pd
|
|
22
22
|
|
|
23
|
+
from .columns import GTF_FIXED_COLUMNS, MISSING_VALUE, RAW_ATTRIBUTE_COLUMN
|
|
24
|
+
|
|
23
25
|
if TYPE_CHECKING:
|
|
24
26
|
import polars
|
|
25
27
|
|
|
26
28
|
logger = logging.getLogger(__name__)
|
|
27
29
|
|
|
28
|
-
# The eight tab-separated columns that precede the attribute field of a GTF
|
|
29
|
-
# line, in the order they must be written. Any other column in a DataFrame is
|
|
30
|
-
# treated as an expanded attribute (see read_gtf's expand_attribute_column).
|
|
31
|
-
GTF_FIXED_COLUMNS = [
|
|
32
|
-
"seqname",
|
|
33
|
-
"source",
|
|
34
|
-
"feature",
|
|
35
|
-
"start",
|
|
36
|
-
"end",
|
|
37
|
-
"score",
|
|
38
|
-
"strand",
|
|
39
|
-
"frame",
|
|
40
|
-
]
|
|
41
|
-
|
|
42
|
-
# GTF uses a single dot to denote a missing value in the fixed columns.
|
|
43
|
-
MISSING_VALUE = "."
|
|
44
|
-
|
|
45
|
-
# Name of the raw, unexpanded attribute column produced by
|
|
46
|
-
# read_gtf(expand_attribute_column=False).
|
|
47
|
-
RAW_ATTRIBUTE_COLUMN = "attribute"
|
|
48
|
-
|
|
49
30
|
|
|
50
31
|
def _lines(df):
|
|
51
32
|
"""Format bounded batches, preserving each column's string representation."""
|
|
@@ -74,7 +55,7 @@ def _lines(df):
|
|
|
74
55
|
def write_gtf(
|
|
75
56
|
df: Union[pd.DataFrame, "polars.DataFrame"],
|
|
76
57
|
path: Union[str, Path],
|
|
77
|
-
header_lines:
|
|
58
|
+
header_lines: Iterable[str] | None = None,
|
|
78
59
|
) -> None:
|
|
79
60
|
"""
|
|
80
61
|
Write a DataFrame of genomic features back out to a GTF file.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: gtfparse
|
|
3
|
-
Version: 3.0
|
|
3
|
+
Version: 3.1.0
|
|
4
4
|
Summary: Parsing library for extracting data frames of genomic features from GTF files
|
|
5
5
|
Author-email: Alex Rubinsteyn <alex.rubinsteyn@unc.edu>
|
|
6
6
|
License-Expression: Apache-2.0
|
|
@@ -12,16 +12,16 @@ Classifier: Operating System :: OS Independent
|
|
|
12
12
|
Classifier: Intended Audience :: Science/Research
|
|
13
13
|
Classifier: Programming Language :: Python
|
|
14
14
|
Classifier: Programming Language :: Python :: 3
|
|
15
|
-
Classifier: Programming Language :: Python :: 3.9
|
|
16
|
-
Classifier: Programming Language :: Python :: 3.10
|
|
17
15
|
Classifier: Programming Language :: Python :: 3.11
|
|
18
16
|
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
19
19
|
Classifier: Topic :: Scientific/Engineering :: Bio-Informatics
|
|
20
|
-
Requires-Python: >=3.
|
|
20
|
+
Requires-Python: >=3.11
|
|
21
21
|
Description-Content-Type: text/markdown
|
|
22
22
|
License-File: LICENSE
|
|
23
23
|
Requires-Dist: pyarrow>=18.0.0
|
|
24
|
-
Requires-Dist: pandas
|
|
24
|
+
Requires-Dist: pandas<4,>=3
|
|
25
25
|
Provides-Extra: polars
|
|
26
26
|
Requires-Dist: polars>=0.20.31; extra == "polars"
|
|
27
27
|
Provides-Extra: dev
|
|
@@ -31,17 +31,18 @@ Requires-Dist: ruff; extra == "dev"
|
|
|
31
31
|
Requires-Dist: coveralls; extra == "dev"
|
|
32
32
|
Dynamic: license-file
|
|
33
33
|
|
|
34
|
+
# gtfparse
|
|
35
|
+
|
|
34
36
|
[](https://github.com/openvax/gtfparse/actions/workflows/tests.yml)
|
|
35
37
|
[](https://coveralls.io/github/openvax/gtfparse?branch=master)
|
|
36
|
-
|
|
37
|
-
<img src="https://img.shields.io/pypi/v/gtfparse.svg?maxAge=1000" alt="PyPI" />
|
|
38
|
-
</a>
|
|
38
|
+
[](https://pypi.org/project/gtfparse/)
|
|
39
39
|
|
|
40
|
-
gtfparse
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
40
|
+
gtfparse reads GTF (Gene Transfer Format) annotation files, such as those from
|
|
41
|
+
Ensembl and GENCODE, into pandas DataFrames: one row per feature, with
|
|
42
|
+
attributes such as gene and transcript IDs expanded into columns. It can also
|
|
43
|
+
write a table back to GTF. In the [OpenVax](https://github.com/openvax) tools,
|
|
44
|
+
[PyEnsembl](https://github.com/openvax/pyensembl) uses it to read Ensembl and
|
|
45
|
+
custom annotations before indexing them.
|
|
45
46
|
|
|
46
47
|
## Install
|
|
47
48
|
|
|
@@ -49,13 +50,14 @@ into columns.
|
|
|
49
50
|
python -m pip install gtfparse
|
|
50
51
|
```
|
|
51
52
|
|
|
52
|
-
Python 3.
|
|
53
|
-
`gtfparse[polars]`.
|
|
53
|
+
Python 3.11 or later is required. gtfparse depends on pandas and PyArrow; for
|
|
54
|
+
optional Polars output, install `gtfparse[polars]`.
|
|
54
55
|
|
|
55
|
-
##
|
|
56
|
+
## Quick start
|
|
56
57
|
|
|
57
|
-
|
|
58
|
-
|
|
58
|
+
<a id="read-an-annotation-and-find-genes"></a><a id="example-usage"></a><a id="parsing-all-rows-of-a-gtf-file-into-a-pandas-dataframe"></a>This
|
|
59
|
+
small artificial GTF runs without a download. Replace the stream with your
|
|
60
|
+
annotation filename when working with a real dataset.
|
|
59
61
|
|
|
60
62
|
```python
|
|
61
63
|
from io import StringIO
|
|
@@ -80,22 +82,37 @@ exon 1 101 150 g1
|
|
|
80
82
|
|
|
81
83
|
`feature` identifies the row type. `seqname` is the chromosome or contig name;
|
|
82
84
|
it is not the gene ID. The ninth GTF field supplies attributes such as
|
|
83
|
-
`gene_id`, `gene_name` and `transcript_id`.
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
<a id="version-3-migration"></a>
|
|
87
|
-
<a id="reporting-parsing-progress"></a>
|
|
88
|
-
<a id="example-usage"></a>
|
|
89
|
-
<a id="parsing-all-rows-of-a-gtf-file-into-a-pandas-dataframe"></a>
|
|
90
|
-
<a id="getting-gene-fpkm-values-from-a-stringtie-gtf-file"></a>
|
|
85
|
+
`gene_id`, `gene_name` and `transcript_id`. Coordinates stay one-based and
|
|
86
|
+
inclusive, as in the file.
|
|
91
87
|
|
|
92
88
|
## Documentation
|
|
93
89
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
-
|
|
90
|
+
The guides are Markdown files in
|
|
91
|
+
[`docs/`](https://github.com/openvax/gtfparse/tree/master/docs). In reading order:
|
|
92
|
+
|
|
93
|
+
1. <a id="getting-gene-fpkm-values-from-a-stringtie-gtf-file"></a>[Filter and write annotations](https://github.com/openvax/gtfparse/blob/master/docs/guides/annotations.md):
|
|
94
|
+
select chromosomes and features, write GTF and read numeric attributes.
|
|
95
|
+
2. <a id="reporting-parsing-progress"></a>[Report parsing progress](https://github.com/openvax/gtfparse/blob/master/docs/guides/progress.md)
|
|
96
|
+
with your own progress bar.
|
|
97
|
+
3. <a id="version-3-migration"></a>[Version 3 migration](https://github.com/openvax/gtfparse/blob/master/docs/guides/migration.md):
|
|
98
|
+
pandas by default, and optional Polars output.
|
|
99
|
+
4. [API reference](https://github.com/openvax/gtfparse/blob/master/docs/reference.md)
|
|
100
|
+
for every public function.
|
|
98
101
|
|
|
99
102
|
To build the documentation locally, run
|
|
100
103
|
`python -m pip install -r requirements-docs.txt` and `./docs.sh`.
|
|
101
104
|
`python scripts/check_docs_examples.py` checks the printed example output.
|
|
105
|
+
|
|
106
|
+
## Development
|
|
107
|
+
|
|
108
|
+
```sh
|
|
109
|
+
python -m pip install -e ".[dev]"
|
|
110
|
+
./lint.sh # ruff check and ruff format --check
|
|
111
|
+
./test.sh # pytest with coverage
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
`./format.sh` applies ruff formatting.
|
|
115
|
+
|
|
116
|
+
## License
|
|
117
|
+
|
|
118
|
+
Apache 2.0; see [LICENSE](LICENSE).
|
|
@@ -15,6 +15,7 @@ gtfparse/__init__.py
|
|
|
15
15
|
gtfparse/_polars.py
|
|
16
16
|
gtfparse/_read_csv.py
|
|
17
17
|
gtfparse/attribute_parsing.py
|
|
18
|
+
gtfparse/columns.py
|
|
18
19
|
gtfparse/create_missing_features.py
|
|
19
20
|
gtfparse/parsing_error.py
|
|
20
21
|
gtfparse/read_gtf.py
|
|
@@ -28,6 +29,7 @@ tests/__init__.py
|
|
|
28
29
|
tests/data.py
|
|
29
30
|
tests/test_attribute_optimization.py
|
|
30
31
|
tests/test_attribute_values.py
|
|
32
|
+
tests/test_columns.py
|
|
31
33
|
tests/test_create_missing_features.py
|
|
32
34
|
tests/test_ensembl_gtf.py
|
|
33
35
|
tests/test_expand_attribute_column_false.py
|
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "gtfparse"
|
|
7
|
-
requires-python = ">=3.
|
|
7
|
+
requires-python = ">=3.11"
|
|
8
8
|
authors = [ {name="Alex Rubinsteyn", email="alex.rubinsteyn@unc.edu" } ]
|
|
9
9
|
description = "Parsing library for extracting data frames of genomic features from GTF files"
|
|
10
10
|
license = "Apache-2.0"
|
|
@@ -16,10 +16,10 @@ classifiers = [
|
|
|
16
16
|
"Intended Audience :: Science/Research",
|
|
17
17
|
"Programming Language :: Python",
|
|
18
18
|
"Programming Language :: Python :: 3",
|
|
19
|
-
"Programming Language :: Python :: 3.9",
|
|
20
|
-
"Programming Language :: Python :: 3.10",
|
|
21
19
|
"Programming Language :: Python :: 3.11",
|
|
22
20
|
"Programming Language :: Python :: 3.12",
|
|
21
|
+
"Programming Language :: Python :: 3.13",
|
|
22
|
+
"Programming Language :: Python :: 3.14",
|
|
23
23
|
"Topic :: Scientific/Engineering :: Bio-Informatics",
|
|
24
24
|
]
|
|
25
25
|
readme = "README.md"
|
|
@@ -46,7 +46,7 @@ dependencies = {file = ["requirements.txt"]}
|
|
|
46
46
|
packages = ["gtfparse"]
|
|
47
47
|
|
|
48
48
|
[tool.ruff]
|
|
49
|
-
target-version = "
|
|
49
|
+
target-version = "py311"
|
|
50
50
|
line-length = 100
|
|
51
51
|
src = ["gtfparse", "tests"]
|
|
52
52
|
exclude = [
|
|
@@ -78,7 +78,7 @@ ignore = [
|
|
|
78
78
|
"B008", # do not perform function calls in argument defaults
|
|
79
79
|
"B905", # zip() without explicit strict
|
|
80
80
|
"SIM108", # use ternary operator instead of if-else
|
|
81
|
-
"UP007", #
|
|
81
|
+
"UP007", # retain the established Union annotation style
|
|
82
82
|
"UP031", # %-format strings
|
|
83
83
|
]
|
|
84
84
|
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
pyarrow>=18.0.0
|
|
2
|
-
pandas>=
|
|
2
|
+
pandas>=3,<4
|
|
@@ -112,9 +112,8 @@ def test_batched_expansion_preserves_late_columns_gaps_collisions_and_progress(
|
|
|
112
112
|
attrs += ['gene_id "G3"; start "42"; late "last";'] * 3
|
|
113
113
|
text = "".join("1\ttest\tgene\t1\t100\t.\t+\t.\t%s\n" % a for a in attrs)
|
|
114
114
|
events = []
|
|
115
|
-
# pandas
|
|
116
|
-
|
|
117
|
-
with pd.option_context("mode.string_storage", storage, "future.infer_string", True):
|
|
115
|
+
# pandas 3 infers strings, so string_storage selects the backend.
|
|
116
|
+
with pd.option_context("mode.string_storage", storage):
|
|
118
117
|
frame = read_gtf(StringIO(text), progress_callback=lambda *event: events.append(event))
|
|
119
118
|
assert list(frame)[-4:] == ["gene_id", "tag", "note", "late"]
|
|
120
119
|
assert list(frame).index("start") == 3
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import importlib
|
|
2
|
+
|
|
3
|
+
import gtfparse
|
|
4
|
+
from gtfparse import columns
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def test_required_columns_are_fixed_columns_then_attribute():
|
|
8
|
+
assert [*columns.GTF_FIXED_COLUMNS, "attribute"] == columns.REQUIRED_COLUMNS
|
|
9
|
+
assert columns.RAW_ATTRIBUTE_COLUMN == "attribute"
|
|
10
|
+
assert set(columns.CATEGORICAL_COLUMNS) <= set(columns.GTF_FIXED_COLUMNS)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def test_column_constants_remain_importable_from_their_old_modules():
|
|
14
|
+
# gtfparse.read_gtf and gtfparse.write_gtf are functions on the package,
|
|
15
|
+
# so look the modules up by name.
|
|
16
|
+
read_module = importlib.import_module("gtfparse.read_gtf")
|
|
17
|
+
write_module = importlib.import_module("gtfparse.write_gtf")
|
|
18
|
+
reader = importlib.import_module("gtfparse._read_csv")
|
|
19
|
+
assert gtfparse.REQUIRED_COLUMNS is columns.REQUIRED_COLUMNS
|
|
20
|
+
assert read_module.REQUIRED_COLUMNS is columns.REQUIRED_COLUMNS
|
|
21
|
+
assert reader.REQUIRED_COLUMNS is columns.REQUIRED_COLUMNS
|
|
22
|
+
assert reader.CATEGORICAL_COLUMNS is columns.CATEGORICAL_COLUMNS
|
|
23
|
+
assert write_module.GTF_FIXED_COLUMNS is columns.GTF_FIXED_COLUMNS
|
|
24
|
+
assert write_module.MISSING_VALUE == columns.MISSING_VALUE == "."
|
|
25
|
+
assert write_module.RAW_ATTRIBUTE_COLUMN == columns.RAW_ATTRIBUTE_COLUMN
|
|
@@ -82,3 +82,60 @@ def test_create_missing_features():
|
|
|
82
82
|
},
|
|
83
83
|
)
|
|
84
84
|
_check_expanded_dataframe(df_extra_features)
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
# A gene row without a transcript_id, then exons of two transcripts. The gene
|
|
88
|
+
# row's empty transcript_id must never become a synthesized transcript.
|
|
89
|
+
GENE_AND_EXONS_GTF = "".join(
|
|
90
|
+
"1\tsrc\t%s\t%d\t%d\t.\t+\t.\t%s\n" % row
|
|
91
|
+
for row in [
|
|
92
|
+
("gene", 100, 500, 'gene_id "G1";'),
|
|
93
|
+
("exon", 100, 200, 'gene_id "G1"; transcript_id "T1";'),
|
|
94
|
+
("exon", 300, 500, 'gene_id "G1"; transcript_id "T1";'),
|
|
95
|
+
("exon", 150, 250, 'gene_id "G1"; transcript_id "T2";'),
|
|
96
|
+
]
|
|
97
|
+
)
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def synthesized_transcript_spans(df):
|
|
101
|
+
"""Return sorted (transcript_id, start, end) tuples of df's transcript rows.
|
|
102
|
+
|
|
103
|
+
Parameters
|
|
104
|
+
----------
|
|
105
|
+
df : pandas.DataFrame
|
|
106
|
+
Output of create_missing_features.
|
|
107
|
+
|
|
108
|
+
Returns
|
|
109
|
+
-------
|
|
110
|
+
list of tuple
|
|
111
|
+
One (transcript_id, start, end) tuple per transcript row, sorted.
|
|
112
|
+
"""
|
|
113
|
+
transcripts = df[df["feature"] == "transcript"]
|
|
114
|
+
return sorted(zip(transcripts["transcript_id"], transcripts["start"], transcripts["end"]))
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def test_create_missing_features_on_filtered_frame():
|
|
118
|
+
# Filtering leaves gaps in the index, which a positional row mask can't align to.
|
|
119
|
+
df = parse_gtf_and_expand_attributes(StringIO(GENE_AND_EXONS_GTF))
|
|
120
|
+
exons = df[df["feature"] == "exon"]
|
|
121
|
+
assert list(exons.index) == [1, 2, 3]
|
|
122
|
+
result = create_missing_features(exons, unique_keys={"transcript": "transcript_id"})
|
|
123
|
+
assert synthesized_transcript_spans(result) == [("T1", 100, 500), ("T2", 150, 250)]
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def test_create_missing_features_on_reverse_sorted_frame():
|
|
127
|
+
# Reversing keeps every index label, so a positional mask would silently
|
|
128
|
+
# select the wrong rows: the gene row instead of T2's exon.
|
|
129
|
+
df = parse_gtf_and_expand_attributes(StringIO(GENE_AND_EXONS_GTF))
|
|
130
|
+
reversed_df = df.iloc[::-1]
|
|
131
|
+
assert list(reversed_df.index) == [3, 2, 1, 0]
|
|
132
|
+
result = create_missing_features(reversed_df, unique_keys={"transcript": "transcript_id"})
|
|
133
|
+
assert synthesized_transcript_spans(result) == [("T1", 100, 500), ("T2", 150, 250)]
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
def test_create_missing_features_skips_null_keys():
|
|
137
|
+
df = parse_gtf_and_expand_attributes(StringIO(GENE_AND_EXONS_GTF))
|
|
138
|
+
df.loc[df["transcript_id"] == "", "transcript_id"] = None
|
|
139
|
+
assert df["transcript_id"].isna().sum() == 1
|
|
140
|
+
result = create_missing_features(df, unique_keys={"transcript": "transcript_id"})
|
|
141
|
+
assert synthesized_transcript_spans(result) == [("T1", 100, 500), ("T2", 150, 250)]
|
|
@@ -134,9 +134,7 @@ def test_cast_version_columns_false_keeps_strings():
|
|
|
134
134
|
import pandas as pd
|
|
135
135
|
|
|
136
136
|
df = read_gtf(GENCODE_GTF_PATH, cast_version_columns=False, result_type="pandas")
|
|
137
|
-
# column is still string-typed when opted out
|
|
138
|
-
# object dtype or pandas' newer StringDtype, depending on the installed
|
|
139
|
-
# pandas version.
|
|
137
|
+
# column is still string-typed when opted out
|
|
140
138
|
assert pd.api.types.is_string_dtype(df["gene_version"])
|
|
141
139
|
gene_rows = df[df["feature"] == "gene"].reset_index(drop=True)
|
|
142
140
|
assert gene_rows.loc[0, "gene_version"] == "5"
|
gtfparse-3.0.4/README.md
DELETED
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
[](https://github.com/openvax/gtfparse/actions/workflows/tests.yml)
|
|
2
|
-
[](https://coveralls.io/github/openvax/gtfparse?branch=master)
|
|
3
|
-
<a href="https://pypi.python.org/pypi/gtfparse/">
|
|
4
|
-
<img src="https://img.shields.io/pypi/v/gtfparse.svg?maxAge=1000" alt="PyPI" />
|
|
5
|
-
</a>
|
|
6
|
-
|
|
7
|
-
gtfparse
|
|
8
|
-
========
|
|
9
|
-
Read GTF (gene transfer format) annotations into a pandas DataFrame, with one
|
|
10
|
-
row per feature and attribute values such as gene and transcript IDs expanded
|
|
11
|
-
into columns.
|
|
12
|
-
|
|
13
|
-
## Install
|
|
14
|
-
|
|
15
|
-
```sh
|
|
16
|
-
python -m pip install gtfparse
|
|
17
|
-
```
|
|
18
|
-
|
|
19
|
-
Python 3.9 or later is required. For optional Polars output, install
|
|
20
|
-
`gtfparse[polars]`.
|
|
21
|
-
|
|
22
|
-
## Read an annotation and find genes
|
|
23
|
-
|
|
24
|
-
This small artificial GTF runs without a download. Replace the stream with
|
|
25
|
-
your annotation filename when working with a real dataset.
|
|
26
|
-
|
|
27
|
-
```python
|
|
28
|
-
from io import StringIO
|
|
29
|
-
from gtfparse import read_gtf
|
|
30
|
-
|
|
31
|
-
annotation = StringIO(
|
|
32
|
-
'1\texample\tgene\t101\t200\t.\t+\t.\tgene_id "g1"; gene_name "EXAMPLE";\n'
|
|
33
|
-
'1\texample\texon\t101\t150\t.\t+\t.\tgene_id "g1"; transcript_id "t1";\n'
|
|
34
|
-
)
|
|
35
|
-
features = read_gtf(annotation)
|
|
36
|
-
genes = features.loc[features["feature"] == "gene"]
|
|
37
|
-
print(genes["gene_name"].tolist())
|
|
38
|
-
for row in features.itertuples():
|
|
39
|
-
print(row.feature, row.seqname, row.start, row.end, row.gene_id)
|
|
40
|
-
```
|
|
41
|
-
|
|
42
|
-
```text
|
|
43
|
-
['EXAMPLE']
|
|
44
|
-
gene 1 101 200 g1
|
|
45
|
-
exon 1 101 150 g1
|
|
46
|
-
```
|
|
47
|
-
|
|
48
|
-
`feature` identifies the row type. `seqname` is the chromosome or contig name;
|
|
49
|
-
it is not the gene ID. The ninth GTF field supplies attributes such as
|
|
50
|
-
`gene_id`, `gene_name` and `transcript_id`.
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
<a id="version-3-migration"></a>
|
|
54
|
-
<a id="reporting-parsing-progress"></a>
|
|
55
|
-
<a id="example-usage"></a>
|
|
56
|
-
<a id="parsing-all-rows-of-a-gtf-file-into-a-pandas-dataframe"></a>
|
|
57
|
-
<a id="getting-gene-fpkm-values-from-a-stringtie-gtf-file"></a>
|
|
58
|
-
|
|
59
|
-
## Documentation
|
|
60
|
-
|
|
61
|
-
- [Filter features, select chromosomes, write GTF and read numeric attributes](docs/guides/annotations.md)
|
|
62
|
-
- [Progress callbacks](docs/guides/progress.md)
|
|
63
|
-
- [Version 3 migration and optional Polars output](docs/guides/migration.md)
|
|
64
|
-
- [Complete API reference](docs/reference.md)
|
|
65
|
-
|
|
66
|
-
To build the documentation locally, run
|
|
67
|
-
`python -m pip install -r requirements-docs.txt` and `./docs.sh`.
|
|
68
|
-
`python scripts/check_docs_examples.py` checks the printed example output.
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|