Skip to content

Commit

Permalink
Merge b60f0c2 into 6735805
Browse files Browse the repository at this point in the history
  • Loading branch information
David-Rod committed Jul 29, 2019
2 parents 6735805 + b60f0c2 commit fe15dbb
Showing 1 changed file with 28 additions and 9 deletions.
37 changes: 28 additions & 9 deletions q2_types/feature_data/_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,34 +100,53 @@ class TSVTaxonomyFormat(model.TextFileFormat):
"""
HEADER = ['Feature ID', 'Taxon']

def sniff(self):
def _check_n_records(self, n=None):
with self.open() as fh:
data_lines = 0
header = None
while data_lines < 10:
line = fh.readline()

file_ = enumerate(fh) if n is None else zip(range(n), fh)

for i, line in file_:
i = i + 1

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

cells = line.rstrip('\n').split('\t')
cells = line.strip('\n').split('\t')

if header is None:
if cells[:2] != self.HEADER:
return False
raise ValidationError("['Feature ID' and 'Taxon'] "
"must be the first two header "
"values to be a valid axonomy "
"file.\n\nThe first two header "
"values provided are: {}."
.format(cells[:2]))
header = cells
else:
if len(cells) != len(header):
return False
raise ValidationError('Number of headers are not the '
'same as number of columns in '
'the file. \nHeader values: '
'{} \nColumn values: {} '
.format(header, cells[:], i))

data_lines += 1

return header is not None and data_lines > 0
if data_lines == 0:
raise ValidationError("No feature records found in manifest, "
"only observed comments, blank lines, "
"and/or a header row.")

def _validate_(self, level):
self._check_n_records(n={'min': 10, 'max': None}[level])


TSVTaxonomyDirectoryFormat = model.SingleFileDirectoryFormat(
Expand Down

0 comments on commit fe15dbb

Please sign in to comment.