Skip to content

Bug in GTF handling [patch offered] #90

@ggilestro

Description

@ggilestro

Hi, the code below is problematic for two reasons:

name_attr = self.properties.get("name_attr", "auto")
if name_attr == "auto":
gene_name = df['attribute'].str.extract(".*gene_name (.*?) ").iloc[:, 0].str.strip('\";')
if gene_name.hasnans:
gene_id = df['attribute'].str.extract(".*gene_id (.*?) ").iloc[:, 0].str.strip('\";')
gene_name.fillna(gene_id, inplace=True)
if gene_name.hasnans:
pos_str = df['seqname'].astype(str) + ":" +\
df['start'].astype(str) + "-" +\
df['end'].astype(str)
gene_name.fillna(pos_str, inplace=True)
df['feature_name'] = gene_name
else:
df['feature_name'] = df['attribute'].str.extract(f".*{name_attr} (.*?) ").iloc[:, 0].str.strip('\";')
return df

  1. it does not do a sanity check for NaN values when the name_attr is not set to auto. This means that any NaN will be passed as a label to DnaFeaturesViewer and the code will crash because it tries to split a float.
  2. The regex will no work if name_attr is the last of the list.

The code can be fixed doing a sanity check for NaN out outside of the if..else and adjusting the regular expression pattern, in the following way:

        name_attr = self.properties.get("name_attr", "auto")
        if name_attr == "auto":
            gene_name = df['attribute'].str.extract(".*gene_name (.*?) ").iloc[:, 0].str.strip('\";')
            if gene_name.hasnans:
                gene_id = df['attribute'].str.extract(".*gene_id (.*?) ").iloc[:, 0].str.strip('\";')
                gene_name.fillna(gene_id, inplace=True)
        else:
            gene_name = df['attribute'].str.extract(f".*{name_attr} (.*?)(?:[ ;])").iloc[:, 0].str.strip('\";')

        if gene_name.hasnans:
            pos_str = df['seqname'].astype(str) + ":" +\
                      df['start'].astype(str) + "-" +\
                      df['end'].astype(str)
            gene_name.fillna(pos_str, inplace=True)

        df['feature_name'] = gene_name
        return df

Hope this helps.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions