Skip to content

Commit 74c78d2

Browse files
authored
GTF parse fix (#155)
* GTF parse fix * GTF parse: fix tests also * GTF parse: fix tests also
1 parent 1fae5c8 commit 74c78d2

4 files changed

Lines changed: 21 additions & 14 deletions

File tree

CHANGELOG.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# 0.0.85 (17.09.20)
2+
- fix error when parsing gtf-files with whitespace in value-tags
3+
14
# 0.0.84 (18.08.20)
25
- add option to report overlap in join
36

pyranges/readers.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ def read_gtf_full(f, as_df=False, nrows=None, skiprows=0, duplicate_attr=False):
313313

314314
names = "Chromosome Source Feature Start End Score Strand Frame Attribute".split(
315315
)
316-
# names = "Chromosome Start End Score Strand Source Feature Frame Attribute".split()
316+
317317
df_iter = pd.read_csv(
318318
f,
319319
sep="\t",
@@ -352,8 +352,14 @@ def to_rows(anno):
352352
raise Exception("Invalid attribute string: {l}. If the file is in GFF3 format, use pr.read_gff3 instead.".format(l=l))
353353

354354
for l in anno:
355-
l = l.replace('"', '').replace(";", "").split()
356-
rowdicts.append({k: v for k, v in zip(*([iter(l)] * 2))})
355+
rowdicts.append({k: v
356+
for k, v in [kv.replace('"', '').split(None, 1)
357+
# l[:-1] removes final ";" cheaply
358+
for kv in l[:-1].split("; ")]})
359+
360+
# for l in anno:
361+
# l = l.replace('"', '').replace(";", "").split()
362+
# rowdicts.append({k: v for k, v in zip(*([iter(l)] * 2))})
357363

358364
return pd.DataFrame.from_dict(rowdicts).set_index(anno.index)
359365

@@ -362,8 +368,10 @@ def to_rows_keep_duplicates(anno):
362368
rowdicts = []
363369
for l in anno:
364370
rowdict = {}
365-
l = l.replace('"', '').replace(";", "").split()
366-
for k, v in zip(*([iter(l)] * 2)):
371+
372+
# l[:-1] removes final ";" cheaply
373+
for k, v in (kv.replace('"', '').split(None, 1) for kv in l[:-1].split("; ")):
374+
367375
if k not in rowdict:
368376
rowdict[k] = v
369377
elif k in rowdict and isinstance(rowdict[k], list):
@@ -442,8 +450,6 @@ def read_gtf_restricted(f,
442450
def to_rows_gff3(anno):
443451
rowdicts = []
444452

445-
# anno = anno.str.replace(";$", "")
446-
447453
for l in list(anno):
448454
l = (it.split("=") for it in l.split(";"))
449455
rowdicts.append({k: v for k, v in l})

pyranges/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.0.84"
1+
__version__ = "0.0.85"

tests/test_io.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
import pyranges as pr
22

3-
# def test_read_bam():
4-
5-
# pr.read_bam("tests/test_data/test_sorted.bam")
6-
73

84
def test_read_gtf():
95

106
gr = pr.read_gtf("tests/test_data/ensembl.gtf", full=True)
11-
assert len(gr.columns) == 28
7+
8+
assert len(gr.columns) == 26
129

1310
df = gr.df
1411
transcript = df.iloc[1]
@@ -19,7 +16,8 @@ def test_read_gtf():
1916

2017
gr = pr.read_gtf("tests/test_data/ensembl.gtf",
2118
full=True, duplicate_attr=True)
22-
assert len(gr.columns) == 28
19+
print(gr.columns)
20+
assert len(gr.columns) == 26
2321

2422
df = gr.df
2523
transcript = df.iloc[1]

0 commit comments

Comments
 (0)