Skip to content

Commit

Permalink
fix mypy errors
Browse files Browse the repository at this point in the history
  • Loading branch information
CunliangGeng committed Apr 12, 2024
1 parent ce2c24d commit 0c90632
Show file tree
Hide file tree
Showing 9 changed files with 18 additions and 21 deletions.
6 changes: 3 additions & 3 deletions src/nplinker/genomics/antismash/antismash_downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ def download_and_extract_antismash_data(
raise e


def _check_roots(download_root: PathLike, extract_root: PathLike):
def _check_roots(download_root: Path, extract_root: Path):
if download_root == extract_root:
raise ValueError("Identical path of download directory and extract directory")


def _check_extract_path(extract_path: PathLike):
def _check_extract_path(extract_path: Path):
# check if extract_path is empty
if any(os.scandir(extract_path)):
if any(extract_path.iterdir()):
raise ValueError(f'Nonempty directory: "{extract_path}"')
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ def _resolve_genbank_accession(genbank_id: str) -> str:
# run a search using the GenBank accession ID
try:
dl_element = _ncbi_genbank_search(genbank_id)
if dl_element is None:
if dl_element is None or isinstance(dl_element, NavigableString):
raise Exception("Unknown HTML format")

refseq_idx = -1
Expand Down
2 changes: 1 addition & 1 deletion src/nplinker/genomics/gcf.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def __str__(self) -> str:
def __repr__(self) -> str:
return str(self)

def __eq__(self, other: GCF) -> bool:
def __eq__(self, other) -> bool:
if isinstance(other, GCF):
return self.gcf_id == other.gcf_id and self.bgcs == other.bgcs
return NotImplemented
Expand Down
6 changes: 3 additions & 3 deletions src/nplinker/genomics/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ def generate_mappings_genome_id_bgc_id(

# sort mappings by genome_id and construct json data
genome_bgc_mappings = dict(sorted(genome_bgc_mappings.items()))
json_data = [{"genome_ID": k, "BGC_ID": v} for k, v in genome_bgc_mappings.items()]
json_data = {"mappings": json_data, "version": "1.0"}
json_data_mappings = [{"genome_ID": k, "BGC_ID": v} for k, v in genome_bgc_mappings.items()]
json_data = {"mappings": json_data_mappings, "version": "1.0"}

# validate json data
validate(instance=json_data, schema=GENOME_BGC_MAPPINGS_SCHEMA)
Expand Down Expand Up @@ -210,7 +210,7 @@ def extract_mappings_strain_id_original_genome_id(
PODP platform. For example, for project MSV000079284, its json file is
https://pairedomicsdata.bioinformatics.nl/api/projects/4b29ddc3-26d0-40d7-80c5-44fb6631dbf9.4.
"""
mappings_dict = {}
mappings_dict: dict[str, set[str]] = {}
with open(podp_project_json_file, "r") as f:
json_data = json.load(f)

Expand Down
6 changes: 1 addition & 5 deletions src/nplinker/loader.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
from importlib.resources import files
from deprecated import deprecated
from nplinker import globals
from nplinker.config import config
Expand Down Expand Up @@ -26,11 +27,6 @@
from nplinker.strain.utils import load_user_strains


try:
from importlib.resources import files
except ImportError:
from importlib_resources import files

logger = LogConfig.getLogger(__name__)

NPLINKER_APP_DATA_DIR = files("nplinker").joinpath("data")
Expand Down
2 changes: 1 addition & 1 deletion src/nplinker/metabolomics/gnps/gnps_file_mapping_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def mapping_reversed(self) -> dict[str, set[str]]:
Returns:
Mapping from file name to all spectra ids that occur in this file.
"""
mapping_reversed = {}
mapping_reversed: dict[str, set[str]] = {}
for spectrum_id, ms_filenames in self._mapping.items():
for filename in ms_filenames:
if filename in mapping_reversed:
Expand Down
11 changes: 6 additions & 5 deletions src/nplinker/metabolomics/gnps/gnps_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from pathlib import Path
import httpx
from bs4 import BeautifulSoup
from bs4 import Tag
from nplinker.utils import get_headers


Expand Down Expand Up @@ -46,10 +45,12 @@ def gnps_format_from_task_id(task_id: str) -> GNPSFormat:
"""
task_html = httpx.get(GNPS_TASK_URL.format(task_id))
soup = BeautifulSoup(task_html.text, features="html.parser")
tags = soup.find_all("th")
workflow_tag: Tag = list(filter(lambda x: x.contents == ["Workflow"], tags))[0]
workflow_format_tag: Tag = workflow_tag.parent.contents[3]
workflow_format = workflow_format_tag.contents[0].strip()
try:
# find the td tag that follows the th tag containing 'Workflow'
workflow_tag = soup.find("th", string="Workflow").find_next_sibling("td") # type: ignore
workflow_format = workflow_tag.contents[0].strip() # type: ignore
except AttributeError:
return GNPSFormat.Unknown

if workflow_format == GNPSFormat.FBMN.value:
return GNPSFormat.FBMN
Expand Down
2 changes: 1 addition & 1 deletion src/nplinker/metabolomics/gnps/gnps_spectrum_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def _load(self):
)
self._spectra.append(spectrum)

def _get_precursor_charge(self, charges: list) -> int:
def _get_precursor_charge(self, charges: list[int]) -> int:
"""Get the precursor charge from the charge list.
Args:
Expand Down
2 changes: 1 addition & 1 deletion src/nplinker/metabolomics/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def extract_mappings_strain_id_ms_filename(
PODP platform. For example, for project MSV000079284, its json file is
https://pairedomicsdata.bioinformatics.nl/api/projects/4b29ddc3-26d0-40d7-80c5-44fb6631dbf9.4.
"""
mappings_dict = {}
mappings_dict: dict[str, set[str]] = {}
with open(podp_project_json_file, "r") as f:
json_data = json.load(f)

Expand Down

0 comments on commit 0c90632

Please sign in to comment.