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

ENH: adds transformer #161

Merged
merged 2 commits into from
Nov 22, 2017
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
34 changes: 33 additions & 1 deletion q2_types/per_sample_sequences/_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# The full license is in the file LICENSE, distributed with this software.
# ----------------------------------------------------------------------------

import re
import os
import gzip
import shutil
Expand All @@ -24,7 +25,8 @@
CasavaOneEightSingleLanePerSampleDirFmt,
CasavaOneEightLanelessPerSampleDirFmt,
SingleEndFastqManifestPhred33, SingleEndFastqManifestPhred64,
PairedEndFastqManifestPhred33, PairedEndFastqManifestPhred64)
PairedEndFastqManifestPhred33, PairedEndFastqManifestPhred64,
QIIME1DemuxDirFmt)


def _single_lane_per_sample_fastq_helper(dirfmt, output_cls, parse_lane=True):
Expand Down Expand Up @@ -342,3 +344,33 @@ def _9(fmt: PairedEndFastqManifestPhred64) \
warnings.warn(_phred64_warning)
return _fastq_manifest_helper(fmt, _write_phred64_to_phred33,
single_end=False)


@plugin.register_transformer
def _12(dirfmt: SingleLanePerSampleSingleEndFastqDirFmt) \
-> QIIME1DemuxDirFmt:
with dirfmt.manifest.view(FastqManifestFormat).open() as fh:
input_manifest = _parse_and_validate_manifest(fh, single_end=True,
absolute=False)

result = QIIME1DemuxDirFmt()
fp = str(result.path / 'seqs.fna')
with open(fp, 'w') as fh:
i = 0
for r in input_manifest.iterrows():
sample_id = r[1]['sample-id']
filename = r[1]['filename']
if re.search("\s", sample_id) is not None:
raise ValueError(
"Whitespace was found in the ID for sample %s. Sample "
"IDs with whitespace are incompatible with FASTA."
% sample_id)
fq_reader = skbio.io.read('%s/%s' % (str(dirfmt), filename),
format='fastq', constructor=skbio.DNA,
phred_offset=33, verify=False)
for seq in fq_reader:
seq.metadata['id'] = '%s_%d' % (sample_id, i)
seq.write(fh)
i += 1

return result
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Quantitative Insights Into Kneecaps and Armpits
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gross.


sample-id,filename,direction
Human-Kneecap,Human-Kneecap_S1_L001_R1_001.fastq.gz,forward
Human-Armpit,Human-Armpit_S2_L001_R1_001.fastq.gz,forward
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Quantitative Insights Into Kneecaps and Armpits

sample-id,filename,direction
# space in sample-id
Human-Kne ecap,Human-Kneecap_S1_L001_R1_001.fastq.gz,forward
Human-Armpit,Human-Armpit_S2_L001_R1_001.fastq.gz,forward
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Quantitative Insights Into Kneecaps and Armpits

sample-id,filename,direction
Human-Kneecap,Human-Kneecap_S1_L001_R1_001.fastq.gz,forward
# tab in sample-id
Human-Armp it,Human-Armpit_S2_L001_R1_001.fastq.gz,forward
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

59 changes: 58 additions & 1 deletion q2_types/per_sample_sequences/tests/test_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
SingleEndFastqManifestPhred33,
SingleEndFastqManifestPhred64,
PairedEndFastqManifestPhred33,
PairedEndFastqManifestPhred64)
PairedEndFastqManifestPhred64,
QIIME1DemuxDirFmt)
from q2_types.per_sample_sequences._transformer import (
_validate_header,
_validate_single_end_fastq_manifest_directions,
Expand Down Expand Up @@ -56,6 +57,62 @@ def test_slpspefdf_to_slpssefdf(self):
for act, exp in zip(obs, expected):
self.assertEqual(act, exp)

def test_slpssefdf_to_qiime1demuxdf(self):
filenames = ('single-end-two-sample-data1/MANIFEST',
'metadata.yml',
'Human-Kneecap_S1_L001_R1_001.fastq.gz',
'Human-Armpit_S2_L001_R1_001.fastq.gz')
input, observed = self.transform_format(
SingleLanePerSampleSingleEndFastqDirFmt,
QIIME1DemuxDirFmt, filenames=filenames
)
expected1 = list(skbio.io.read(
'%s/Human-Kneecap_S1_L001_R1_001.fastq.gz' % str(input),
format='fastq', constructor=skbio.DNA
))
expected2 = list(skbio.io.read(
'%s/Human-Armpit_S2_L001_R1_001.fastq.gz' % str(input),
format='fastq', constructor=skbio.DNA
))
expected = \
list(zip(expected1, ['Human-Kneecap'] * len(expected1))) + \
list(zip(expected2, ['Human-Armpit'] * len(expected2)))
observed = skbio.io.read(
'%s/seqs.fna' % str(observed),
format='fasta', constructor=skbio.DNA
)
observed = list(observed)

self.assertEqual(len(observed), len(expected))

for i, obs in enumerate(observed):
# identifiers are as expected
self.assertEqual(obs.metadata['id'],
'%s_%d' % (expected[i][1], i))
# sequences are as expected
self.assertEqual(str(obs), str(expected[i][0]))

def test_slpssefdf_to_qiime1demuxdf_bad_sample_ids(self):
filenames = ('single-end-two-sample-data2/MANIFEST',
'metadata.yml',
'Human-Kneecap_S1_L001_R1_001.fastq.gz',
'Human-Armpit_S2_L001_R1_001.fastq.gz')
with self.assertRaisesRegex(ValueError,
expected_regex='space.*Human-K'):
self.transform_format(
SingleLanePerSampleSingleEndFastqDirFmt,
QIIME1DemuxDirFmt, filenames=filenames)

filenames = ('single-end-two-sample-data3/MANIFEST',
'metadata.yml',
'Human-Kneecap_S1_L001_R1_001.fastq.gz',
'Human-Armpit_S2_L001_R1_001.fastq.gz')
with self.assertRaisesRegex(ValueError,
expected_regex='space.*Human-A'):
self.transform_format(
SingleLanePerSampleSingleEndFastqDirFmt,
QIIME1DemuxDirFmt, filenames=filenames)

def test_casava_one_eight_single_lane_per_sample_dirfmt_to_slpssefdf(self):
filenames = ('Human-Kneecap_S1_L001_R1_001.fastq.gz',)
input, obs = self.transform_format(
Expand Down
16 changes: 10 additions & 6 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,16 @@
'q2_types.feature_data.tests': ['data/*', 'data/taxonomy/*'],
'q2_types.feature_table.tests': ['data/*'],
'q2_types.ordination.tests': ['data/*'],
'q2_types.per_sample_sequences.tests': ['data/*',
'data/paired_end_data/*',
'data/single_end_data/*',
'data/absolute_manifests/*',
'data/relative_manifests/*',
'data/qiime1-demux-format/*'],
'q2_types.per_sample_sequences.tests':
['data/*',
'data/paired_end_data/*',
'data/single_end_data/*',
'data/absolute_manifests/*',
'data/relative_manifests/*',
'data/qiime1-demux-format/*',
'data/single-end-two-sample-data1/*',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

'data/single-end-two-sample-data2/*',
'data/single-end-two-sample-data3/*'],
'q2_types.sample_data.tests': ['data/*'],
'q2_types.tree.tests': ['data/*']
},
Expand Down