Skip to content

Commit

Permalink
Add error message when NCBI server fails
Browse files Browse the repository at this point in the history
  • Loading branch information
lauraluebbert authored Nov 14, 2023
1 parent f100dc3 commit 1b3720a
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions gget/gget_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,36 +244,55 @@ def info(
## Web scrape NCBI website for gene ID, synonyms and description
soup = BeautifulSoup(html.text, "html.parser")

# Check for error message in NCBI return
if (
soup.find("li", class_="error icon") is not None
and "An error has occured"
in soup.find("li", class_="error icon").text.strip()
):
error_message = soup.find(
"li", class_="error icon"
).text.strip()
if verbose:
logging.warning(
f"The NCBI server request for Ensembl ID '{ens_id}' returned the following error:\n{error_message}"
)

ncbi_gene_id = np.nan
ncbi_description = np.nan
ncbi_synonyms = None
continue

# Check if NCBI gene ID is available
try:
ncbi_gene_id = soup.find("input", {"id": "gene-id-value"}).get(
"value"
)
except:
except AttributeError:
ncbi_gene_id = np.nan

# Check if NCBI description is available
try:
ncbi_description = (
soup.find("div", class_="section", id="summaryDiv")
.find("dt", text="Summary")
.find("dt", string="Summary")
.find_next_sibling("dd")
.text
)
except:
except AttributeError:
ncbi_description = np.nan

# Check if NCBI synonyms are available
try:
ncbi_synonyms = (
soup.find("div", class_="section", id="summaryDiv")
.find("dt", text="Also known as")
.find("dt", string="Also known as")
.find_next_sibling("dd")
.text
)
# Split NCBI synonyms
ncbi_synonyms = ncbi_synonyms.split("; ")
except:
except AttributeError:
ncbi_synonyms = None

except Exception as e:
Expand All @@ -285,7 +304,6 @@ def info(
ncbi_gene_id = np.nan
ncbi_description = np.nan
ncbi_synonyms = None

continue

# Save NCBI info to data frame
Expand Down

0 comments on commit 1b3720a

Please sign in to comment.