Skip to content
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

fix tabular file header detection #264

Merged
merged 2 commits into from
Jul 11, 2024
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
12 changes: 7 additions & 5 deletions src/nplinker/metabolomics/gnps/gnps_format.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from __future__ import annotations
import re
import zipfile
from enum import Enum
from enum import unique
from os import PathLike
from pathlib import Path
import httpx
from bs4 import BeautifulSoup
from nplinker.utils import get_headers


GNPS_TASK_URL = "https://gnps.ucsd.edu/ProteoSAFe/status.jsp?task={}"
Expand Down Expand Up @@ -118,11 +118,13 @@ def gnps_format_from_file_mapping(file: str | PathLike) -> GNPSFormat:
Returns:
GNPS format identified in the file.
"""
headers = get_headers(file)
if "AllFiles" in headers:
with open(file, "r") as f:
header = f.readline().strip()

if re.search(r"\bAllFiles\b", header):
return GNPSFormat.SNETS
if "UniqueFileSources" in headers:
if re.search(r"\bUniqueFileSources\b", header):
return GNPSFormat.SNETSV2
if "row ID" in headers:
if re.search(r"\b{}\b".format(re.escape("row ID")), header):
return GNPSFormat.FBMN
return GNPSFormat.Unknown
33 changes: 0 additions & 33 deletions src/nplinker/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,39 +59,6 @@ def wrapper_check_disk_space(*args, **kwargs):
#


def find_delimiter(file: str | PathLike) -> str:
"""Detect the delimiter for the given tabular file.

Args:
file: Path to tabular file.

Returns:
Detected delimiter character.

Examples:
>>> delim = find_delimiter("~/table.csv")
"""
sniffer = csv.Sniffer()
with open(file, mode="rt", encoding="utf-8") as fp:
delimiter = sniffer.sniff(fp.read(5000)).delimiter
return delimiter


def get_headers(file: str | PathLike) -> list[str]:
"""Read headers from the given tabular file.

Args:
file: Path to the file to read the header from.

Returns:
A list of column names from the header.
"""
with open(file) as f:
headers = f.readline().strip()
dl = find_delimiter(file)
return headers.split(dl)


def is_file_format(file: str | PathLike, format: str = "tsv") -> bool:
"""Check if the file is in the given format.

Expand Down
11 changes: 0 additions & 11 deletions tests/unit/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,6 @@
from tempfile import mkdtemp
import pytest
from nplinker import utils
from nplinker.utils import find_delimiter
from . import GNPS_DATA_DIR


@pytest.mark.parametrize(
"filename, expected",
[[GNPS_DATA_DIR / "nodes.tsv", "\t"], [GNPS_DATA_DIR / "nodes_mwe.csv", ","]],
)
def test_find_delimiter(filename, expected):
actual = find_delimiter(filename)
assert actual == expected


BGC_GBK_URL = "https://mibig.secondarymetabolites.org/repository/BGC0000001/BGC0000001.gbk"
Expand Down
Loading