8000 fixed empty attribute bug in read_gtf when duplicate_attr=True by fairliereese · Pull Request #297 · pyranges/pyranges · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

fixed empty attribute bug in read_gtf when duplicate_attr=True #297

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 9 additions & 0 deletions pyranges/methods/join.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,20 @@ def _both_indexes(scdf, ocdf, how=False):
def null_types(h):
h2 = h.copy()
for n, d in zip(h, h.dtypes):

if n in ["Chromosome", "Strand"]:
continue

d = str(d)

# for bools, turn into a category so we can add an
# "unknown" null value ("-1")
if "bool" in d:
h2[n] = h2[n].astype("category")
d = "category"

# use numerical -1 for numbers
# or string "-1" for strings, objects, and cats
if "int" in d or "float" in d:
null = -1
elif "string" in d or d == "object":
Expand Down
18 changes: 5 additions & 13 deletions pyranges/readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,9 @@ def read_gtf_full(f, as_df=False, nrows=None, skiprows=0, duplicate_attr=False):
else:
return df

def parse_kv_fields(l):
# rstrip: allows for GFF not having a last ";", or having final spaces
return [kv.replace('""', '"NA"').replace('"', '').split(None, 1) for kv in l.rstrip('; ').split("; ")]

def to_rows(anno):
rowdicts = []
Expand All @@ -380,16 +383,7 @@ def to_rows(anno):
)

for l in anno:
rowdicts.append(
{
k: v
for k, v in [
kv.replace('""', '"NA"').replace('"', "").split(None, 1)
# rstrip: allows for GFF not having a last ";", or having final spaces
for kv in l.rstrip("; ").split("; ")
]
}
)
rowdicts.append({k: v for k, v in parse_kv_fields(l)})

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

Expand All @@ -400,9 +394,7 @@ def to_rows_keep_duplicates(anno):
rowdict = {}

# rstrip: allows for GFF not having a last ";", or having final spaces
for k, v in (
kv.replace('"', "").split(None, 1) for kv in l.rstrip("; ").split("; ")
):
for k, v in tuple(parse_kv_fields(l)):
if k not in rowdict:
rowdict[k] = v
elif k in rowdict and isinstance(rowdict[k], list):
Expand Down
0