Skip to content

Commit

Permalink
Update results exporters
Browse files Browse the repository at this point in the history
  • Loading branch information
jlumpe committed Dec 1, 2024
1 parent ca4ead2 commit 292d0c4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 24 deletions.
18 changes: 7 additions & 11 deletions src/gambit/results.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from gambit.util.io import FilePath, maybe_open
import gambit.util.json as gjson
from gambit.query import QueryResults, QueryResultItem, QueryInput
from gambit.query import QueryResults, QueryResultItem
from gambit.db import ReferenceGenomeSet, Taxon, AnnotatedGenome, Genome


Expand Down Expand Up @@ -84,8 +84,9 @@ class CSVResultsExporter(AbstractResultsExporter):
"""
format_opts: dict[str, Any]

# Pairs of column name and QueryResultItem attribute
COLUMNS = [
('query', 'input.label'),
('query', 'label'),
('predicted.name', 'report_taxon.name'),
('predicted.rank', 'report_taxon.rank'),
('predicted.ncbi_id', 'report_taxon.ncbi_id'),
Expand Down Expand Up @@ -140,20 +141,15 @@ def _results_to_json(self, results: QueryResults):
@to_json.register(QueryResultItem)
def _item_to_json(self, item: QueryResultItem):
return dict(
query=item.input,
query=dict(
name=item.label,
path=item.file,
),
predicted_taxon=item.report_taxon,
next_taxon=item.classifier_result.next_taxon,
closest_genomes=item.closest_genomes,
)

@to_json.register(QueryInput)
def _input_to_json(self, input: QueryInput):
return dict(
name=input.label,
path=None if input.file is None else input.file.path,
format=None if input.file is None else input.file.format,
)

@to_json.register(ReferenceGenomeSet)
def _genomeset_to_json(self, gset: ReferenceGenomeSet):
return _todict(gset, ['id', 'key', 'version', 'name', 'description'])
Expand Down
13 changes: 5 additions & 8 deletions tests/results.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,20 +201,17 @@ def check_json_results(file: TextIO,

# Compare data['query'] <-> item.input
query = item_data['query']
assert query['name'] == item.input.label
assert query['name'] == item.label

if item.input.file is None:
if item.file is None:
assert query['path'] is None
assert query['format'] is None

else:
assert query['format'] == item.input.file.format

# Check path matches exactly if strict mode, otherwise just file name
if strict:
assert query['path'] == str(item.input.file.path)
assert query['path'] == str(item.file)
else:
assert Path(query['path']).name == item.input.file.path.name
assert Path(query['path']).name == item.file.name

# Predicted/next taxon
cmp_taxon_json(item_data['predicted_taxon'], item.report_taxon)
Expand Down Expand Up @@ -269,7 +266,7 @@ def check_csv_results(file: TextIO, results: QueryResults, strict: bool = False)
assert len(rows) == len(results.items)

for item, row in zip(results.items, rows):
assert row['query'] == item.input.label
assert row['query'] == item.label

cmp_csv_taxon(row, item.report_taxon, 'predicted')
cmp_csv_taxon(row, item.classifier_result.next_taxon, 'next')
Expand Down
17 changes: 12 additions & 5 deletions tests/test_results.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
"""Test the gambit.results module.
Each ResultsExporter subclass is tested by exporting a fake QueryResults instance to a string buffer,
parsing the exported results and checking the against the original using the functions in the
.results tests helper module.
"""

from io import StringIO

import pytest

from gambit.query import QueryResults, QueryResultItem, QueryInput, QueryParams
from gambit.query import QueryResults, QueryResultItem, QueryParams
from gambit.classify import ClassifierResult, GenomeMatch
from gambit.db import ReferenceGenomeSet, Genome
from gambit.sigs import SignaturesMeta
from gambit.seq import SequenceFile
from gambit.results import JSONResultsExporter, CSVResultsExporter, ResultsArchiveReader, ResultsArchiveWriter
from .results import check_json_results, check_csv_results

Expand Down Expand Up @@ -77,14 +83,15 @@ def results(session):
for i, cr in enumerate(classifier_results):
predicted = cr.predicted_taxon
items.append(QueryResultItem(
input=QueryInput(f'query-{i}', SequenceFile(f'query-{i}.fasta', 'fasta')),
f'query-{i}',
classifier_result=cr,
file=f'query-{i}.fasta',
report_taxon=None if predicted is None else predicted.parent if i % 4 == 0 else predicted,
closest_genomes=[cr.closest_match],
))

# Set one input file to None
items[-1].input.file = None
# Set one file to None
items[-1].file = None

return QueryResults(
items=items,
Expand Down

0 comments on commit 292d0c4

Please sign in to comment.