Skip to content

Commit

Permalink
lint and comments
Browse files Browse the repository at this point in the history
  • Loading branch information
VinzentRisch committed Aug 20, 2024
1 parent 450eb61 commit 3a0daf1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
11 changes: 8 additions & 3 deletions rescript/bv_brc.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
import qiime2
import pandas as pd
import requests
from q2_types.feature_data import MixedCaseDNAFASTAFormat, ProteinFASTAFormat, TSVTaxonomyDirectoryFormat
from q2_types.feature_data import (MixedCaseDNAFASTAFormat, ProteinFASTAFormat,
TSVTaxonomyDirectoryFormat)
from q2_types.genome_data import GenomeSequencesDirectoryFormat

from rescript.ncbi import _allowed_ranks, _default_ranks
Expand Down Expand Up @@ -46,17 +47,19 @@ def fetch_genomes_bv_brc(

def fetch_metadata_bv_brc(data_type: str, rql_query: str) -> qiime2.Metadata:

# Get requests response
# Download data
response = download_data(
url=f"https://www.bv-brc.org/api/{data_type}/?{rql_query}&http_accept=text/tsv",
data_type=data_type
)

# Convert data to data frame
tsv_data = StringIO(response.text)
metadata = pd.read_csv(tsv_data, sep='\t')
metadata.index.name = "id"
metadata.index = metadata.index.astype(str)

# Return as qiime2 metadata
return qiime2.Metadata(metadata)


Expand Down Expand Up @@ -249,11 +252,13 @@ def error_handling(response, data_type):
f"Allowed fields for data type {data_type}: \n{data_fields[data_type]}"
)

# Handling any other errors that start with "A Database Error Occured:"
else:
raise ValueError(
f"Error code {response_dict['code']}: {response_dict['msg']}."
)

# Handling any other error codes
else:
raise ValueError(response.text)

Expand All @@ -269,7 +274,7 @@ def id_list_handling(rql_query: str, ids: list, parameter_name: str, data_field:
raise ValueError("At least one of the parameters rql_query and "
f"{parameter_name} has to be given.")

# construct the RQL queries
# Construct the RQL queries
elif ids:
rql_query = f"in({data_field},({','.join(map(str, ids))}))"

Expand Down
17 changes: 10 additions & 7 deletions rescript/tests/test_bv_brc.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
# The full license is in the file LICENSE, distributed with this software.
# ----------------------------------------------------------------------------
import unittest
from typing import Any
from unittest.mock import Mock, patch, mock_open, MagicMock

import pandas as pd
Expand Down Expand Up @@ -324,7 +323,8 @@ class TestFetchMetadataBVBR(TestPluginBase):
@patch('rescript.bv_brc.qiime2.Metadata')
@patch('rescript.bv_brc.pd.read_csv')
@patch('rescript.bv_brc.download_data')
def test_fetch_metadata_bv_brc(self, mock_download_data, mock_read_csv, mock_metadata):
def test_fetch_metadata_bv_brc(self, mock_download_data,
mock_read_csv, mock_metadata):
# Mock the download_data response
mock_response = MagicMock()
mock_response.text = "id\tcolumn1\tcolumn2\n1\tdata1\tdata2\n2\tdata3\tdata4"
Expand All @@ -349,15 +349,17 @@ def test_fetch_metadata_bv_brc(self, mock_download_data, mock_read_csv, mock_met

# Assertions
mock_download_data.assert_called_once_with(
url="https://www.bv-brc.org/api/genome/?genome_id=in(1,2)&http_accept=text/tsv",
url="https://www.bv-brc.org/api/genome/"
"?genome_id=in(1,2)&http_accept=text/tsv",
data_type="genome"
)

mock_read_csv.assert_called_once()
args, kwargs = mock_read_csv.call_args
self.assertEqual(kwargs['sep'], '\t')

self.assertEqual(args[0].getvalue(), "id\tcolumn1\tcolumn2\n1\tdata1\tdata2\n2\tdata3\tdata4")
self.assertEqual(args[0].getvalue(),
"id\tcolumn1\tcolumn2\n1\tdata1\tdata2\n2\tdata3\tdata4")

mock_metadata.assert_called_once_with(mock_df)
self.assertEqual(result, mock_metadata_instance)
Expand All @@ -372,7 +374,8 @@ class TestFetchTaxonomyBVBR(TestPluginBase):
@patch('rescript.bv_brc.pd.read_csv')
@patch('rescript.bv_brc.id_list_handling')
def test_fetch_taxonomy_bv_brc(
self, mock_id_list_handling, mock_read_csv, mock_download_data, mock_transform_taxonomy_df, mock_to_csv
self, mock_id_list_handling, mock_read_csv, mock_download_data,
mock_transform_taxonomy_df, mock_to_csv
):
# Mock the id_list_handling function
mock_id_list_handling.return_value = "taxon_id=in(taxon1,taxon2)"
Expand All @@ -382,7 +385,6 @@ def test_fetch_taxonomy_bv_brc(
mock_response.text = "id\trank1\trank2\n1\tdata1\tdata2\n2\tdata3\tdata4"
mock_download_data.return_value = mock_response


# Prepare mocks for file output
with patch('builtins.open', unittest.mock.mock_open()) as mock_file:
directory = fetch_taxonomy_bv_brc(
Expand All @@ -400,7 +402,8 @@ def test_fetch_taxonomy_bv_brc(
)

mock_download_data.assert_called_once_with(
url="https://www.bv-brc.org/api/taxonomy/?taxon_id=in(taxon1,taxon2)&http_accept=text/tsv",
url="https://www.bv-brc.org/api/taxonomy/?taxon_id=in(taxon1,taxon2)"
"&http_accept=text/tsv",
data_type="taxonomy"
)

Expand Down

0 comments on commit 3a0daf1

Please sign in to comment.