From 3a0daf11baf93abc579d9882c063dcd9ae0982b1 Mon Sep 17 00:00:00 2001 From: VinzentRisch Date: Tue, 20 Aug 2024 13:36:22 +0200 Subject: [PATCH] lint and comments --- rescript/bv_brc.py | 11 ++++++++--- rescript/tests/test_bv_brc.py | 17 ++++++++++------- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/rescript/bv_brc.py b/rescript/bv_brc.py index 37fa422..d1860ff 100644 --- a/rescript/bv_brc.py +++ b/rescript/bv_brc.py @@ -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 @@ -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) @@ -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) @@ -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))}))" diff --git a/rescript/tests/test_bv_brc.py b/rescript/tests/test_bv_brc.py index 9edfd5c..48d07e6 100644 --- a/rescript/tests/test_bv_brc.py +++ b/rescript/tests/test_bv_brc.py @@ -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 @@ -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" @@ -349,7 +349,8 @@ 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" ) @@ -357,7 +358,8 @@ def test_fetch_metadata_bv_brc(self, mock_download_data, mock_read_csv, mock_met 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) @@ -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)" @@ -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( @@ -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" )