cokelaer / bioservices

Access to Biological Web Services from Python.
http://bioservices.readthedocs.io
Other
280 stars 61 forks source link

Annotation_from_goid sets initial page wrongly #186

Closed Hrovatin closed 3 years ago

Hrovatin commented 3 years ago

I wanted to retrieve genes for human GO:0033577, e.g. image However, in python I get image This is odd as the retrieved page is 2/1 which does not make sense. Thus I changed the code of the function to start at page 0+1 instead of 1+1 (see below) and now it works. (Note that this function uses go as input rather than being object-specific function as this was easier to fix for me).

def Annotation_from_goid(go, goId, max_number_of_pages=25, **kargs):
    """Returns a DataFrame containing annotation on a given GO identifier

    :param str protein: a GO identifier
    :return: all outputs are stored into a Pandas.DataFrame data structure.

    All parameters from :math:`Annotation` are also valid except **format** that
    is set to **tsv**  and cols that is made of all possible column names.

    """
    data =go.Annotation(goId=goId, **kargs)
    number_of_pages = data['pageInfo']['total']
    if number_of_pages > max_number_of_pages:
        print("As of 23d Oct 2017, the QuickGO API limits the number of pages to 25")
        number_of_pages = max_number_of_pages

    # unfortunately, the new API requires to call the service for each page.
    results = []
    for i in range(0, number_of_pages):
        print("fetching page %s / %s " % (i+1, number_of_pages))
        data = go.Annotation(goId=goId, page=i+1, **kargs)
        if data not in [400, '400']:
            results.extend(data['results'])
    try:
        import pandas as pd
        return pd.DataFrame(results)
    except:
        go.logging.warning(
            "Cannot return a DataFrame. Returns the list. If you want the dataframe, install pandas library")
        return results

image

cokelaer commented 3 years ago

Thanks for reporting this issue and solution. It is now incorporated in the code and will be available in the next release