Skip to content

Commit

Permalink
add 2nd transformer, simplify usage example factories
Browse files Browse the repository at this point in the history
  • Loading branch information
gregcaporaso committed Apr 12, 2024
1 parent 790c735 commit 93a3098
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
14 changes: 4 additions & 10 deletions q2_dwq2/_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,17 @@

import qiime2

from q2_dwq2 import SingleRecordDNAFASTAFormat


def seq1_factory():
seq = skbio.DNA("AACCGGTTGGCCAA", metadata={"id": "seq1"})
return _create_seq_artifact(seq)
return qiime2.Artifact.import_data(
"SingleDNASequence", seq, view_type=skbio.DNA)


def seq2_factory():
seq = skbio.DNA("AACCGCTGGCGAA", metadata={"id": "seq2"})
return _create_seq_artifact(seq)


def _create_seq_artifact(seq: skbio.DNA):
ff = SingleRecordDNAFASTAFormat()
seq.write(str(ff.path))
return qiime2.Artifact.import_data("SingleDNASequence", ff)
return qiime2.Artifact.import_data(
"SingleDNASequence", seq, view_type=skbio.DNA)


def nw_align_example_1(use):
Expand Down
7 changes: 7 additions & 0 deletions q2_dwq2/_transformers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,10 @@ def _1(ff: SingleRecordDNAFASTAFormat) -> DNA:
# by default, DNA.read will read the first sequence in the file
with ff.open() as fh:
return DNA.read(fh)


@plugin.register_transformer
def _2(seq: DNA) -> SingleRecordDNAFASTAFormat:
ff = SingleRecordDNAFASTAFormat()
seq.write(str(ff.path))
return ff
19 changes: 19 additions & 0 deletions q2_dwq2/tests/test_transformers.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,22 @@ def test_single_record_fasta_to_DNA_simple2(self):
metadata={'id': 'example-sequence-2', 'description': ''})

self.assertEqual(observed, expected)

def test_DNA_to_single_record_fasta_simple1(self):
in_ = DNA('ACCGGTGGAACCGGTAACACCCAC',
metadata={'id': 'example-sequence-1', 'description': ''})
tx = self.get_transformer(DNA, SingleRecordDNAFASTAFormat)

observed = tx(in_)
# confirm "round-trip" of DNA -> SingleRecordDNAFASTAFormat -> DNA
# results in an observed sequence that is the same as the starting
# sequence
self.assertEqual(observed.view(DNA), in_)

def test_DNA_to_single_record_fasta_simple2(self):
in_ = DNA('ACCGGTAACCGGTTAACACCCAC',
metadata={'id': 'example-sequence-2', 'description': ''})
tx = self.get_transformer(DNA, SingleRecordDNAFASTAFormat)

observed = tx(in_)
self.assertEqual(observed.view(DNA), in_)

0 comments on commit 93a3098

Please sign in to comment.