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/BUG/MAINT: support comments/blank lines and better validation in "per-sample sequence" formats #136

Merged
merged 1 commit into from
Jul 24, 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
2 changes: 2 additions & 0 deletions q2_types/per_sample_sequences/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from ._format import (CasavaOneEightSingleLanePerSampleDirFmt, FastqGzFormat,
YamlFormat, FastqManifestFormat,
FastqAbsolutePathManifestFormat,
SingleLanePerSampleSingleEndFastqDirFmt,
SingleLanePerSamplePairedEndFastqDirFmt,
SingleEndFastqManifestPhred33,
Expand All @@ -21,6 +22,7 @@

__all__ = ['CasavaOneEightSingleLanePerSampleDirFmt', 'FastqGzFormat',
'YamlFormat', 'FastqManifestFormat',
'FastqAbsolutePathManifestFormat',
'SingleLanePerSampleSingleEndFastqDirFmt',
'SingleLanePerSamplePairedEndFastqDirFmt', 'SequencesWithQuality',
'PairedEndSequencesWithQuality', 'PerSampleDNAIterators',
Expand Down
101 changes: 63 additions & 38 deletions q2_types/per_sample_sequences/_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,74 @@
from ..plugin_setup import plugin


class FastqManifestFormat(model.TextFileFormat):
class _FastqManifestBase(model.TextFileFormat):
Copy link
Contributor

Choose a reason for hiding this comment

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

Sweet, I like this game plan.

"""
Mapping of sample identifiers to filepaths and read direction.
Base class for mapping of sample identifiers to filepaths and read
direction.

"""
EXPECTED_HEADER = None

def sniff(self):
with self.open() as fh:
header = fh.readline()
return header.strip() == 'sample-id,filename,direction'
data_lines = 0
header = None
while data_lines < 10:
line = fh.readline()

if line == '':
# EOF
break
elif line.lstrip(' ') == '\n':
# Blank line
continue
elif line.startswith('#'):
# Comment line
continue

cells = line.rstrip('\n').split(',')
if header is None:
if cells != self.EXPECTED_HEADER:
return False
header = cells
else:
if len(cells) != len(header):
return False
data_lines += 1

return header is not None and data_lines > 0


class FastqManifestFormat(_FastqManifestBase):
"""
Mapping of sample identifiers to relative filepaths and read direction.

"""
EXPECTED_HEADER = ['sample-id', 'filename', 'direction']


class FastqAbsolutePathManifestFormat(_FastqManifestBase):
"""
Mapping of sample identifiers to absolute filepaths and read direction.

"""
EXPECTED_HEADER = ['sample-id', 'absolute-filepath', 'direction']


class SingleEndFastqManifestPhred33(FastqAbsolutePathManifestFormat):
pass


class SingleEndFastqManifestPhred64(FastqAbsolutePathManifestFormat):
pass


class PairedEndFastqManifestPhred33(FastqAbsolutePathManifestFormat):
pass


class PairedEndFastqManifestPhred64(FastqAbsolutePathManifestFormat):
pass


class YamlFormat(model.TextFileFormat):
Expand Down Expand Up @@ -64,40 +123,6 @@ def sniff(self):
return False


class FastqAbsolutePathManifestFormat(model.TextFileFormat):
"""
Mapping of sample identifiers to filepaths and read direction.

"""
def sniff(self):
expected_header = 'sample-id,absolute-filepath,direction'
with self.open() as fh:
for line in fh:
line = line.strip()
if line and not line.startswith('#'):
# the first non-blank, non-comment line should be
# the header
return line == expected_header
# never found the header
return False


class SingleEndFastqManifestPhred33(FastqAbsolutePathManifestFormat):
pass


class SingleEndFastqManifestPhred64(FastqAbsolutePathManifestFormat):
pass


class PairedEndFastqManifestPhred33(FastqAbsolutePathManifestFormat):
pass


class PairedEndFastqManifestPhred64(FastqAbsolutePathManifestFormat):
pass


class CasavaOneEightSingleLanePerSampleDirFmt(model.DirectoryFormat):
sequences = model.FileCollection(
r'.+_.+_L[0-9][0-9][0-9]_R[12]_001\.fastq\.gz',
Expand Down
Loading