# Path: q2_types/plugin_setup.py

# 
# Path: q2_types/sample_data/_format.py
# class AlphaDiversityFormat(model.TextFileFormat):
#     def _validate_(self, level):
#         with self.open() as fh:
#             header, records_seen, is_min = None, 0, level == 'min'
#             fh_ = csv.reader(fh, delimiter='\t')
#             file_ = enumerate(fh_, 1) if is_min else zip(range(1, 11), fh_)
#             for i, cells in file_:
#                 if header is None:
#                     if len(cells) < 2:
#                         raise ValidationError(
#                             'Found header on line %d with the following '
#                             'columns: %s (length: %d), expected at least 2 '
#                             'columns.' % (i, cells, len(cells)))
#                     else:
#                         header = cells
#                 else:
#                     if len(cells) != len(header):
#                         raise ValidationError(
#                             'Line %d has %s cells (%s), expected %s.'
#                             % (i, len(cells), cells, len(header)))
# 
#                     records_seen += 1
# 
#             # The first non-comment and non-blank row observed will always be
#             # the header row, and since we have no requirement on the field
#             # names (because they are dynamically defined), so no need to check
#             # for the presence (or validity) of a header row at this point.
# 
#             if records_seen == 0:
#                 raise ValidationError('No records found in file, only '
#                                       'observed comments, blank lines, and/or '
#                                       'a header row.')


import pandas as pd
import numpy as np
import qiime2
from ..plugin_setup import plugin
from . import AlphaDiversityFormat
    # of any columns or the index.
    df = pd.read_csv(fh, sep='\t', header=0, dtype=object)
    df.set_index(df.columns[0], drop=True, append=False, inplace=True)
    df.index.name = None
    # casting of columns adapted from SO post:
    # https://stackoverflow.com/a/36814203/3424666
    cols = df.columns
    df[cols] = df[cols].apply(pd.to_numeric, errors='ignore')
    return df


@plugin.register_transformer
def _1(data: pd.Series) -> AlphaDiversityFormat:
    ff = AlphaDiversityFormat()
    with ff.open() as fh:
        data.to_csv(fh, sep='\t', header=True)
    return ff


@plugin.register_transformer
def _2(ff: AlphaDiversityFormat) -> pd.Series:
    with ff.open() as fh:
        df = _read_alpha_diversity(fh)
        series = df.iloc[:, 0]
        if not np.issubdtype(series, np.number):
            raise ValueError('Non-numeric values detected in alpha diversity '
                             'estimates.')
        return series



# Path: q2_types/plugin_setup.py

# 
# Path: q2_types/sample_data/_format.py
# class AlphaDiversityFormat(model.TextFileFormat):
#     def _validate_(self, level):
#         with self.open() as fh:
#             header, records_seen, is_min = None, 0, level == 'min'
#             fh_ = csv.reader(fh, delimiter='\t')
#             file_ = enumerate(fh_, 1) if is_min else zip(range(1, 11), fh_)
#             for i, cells in file_:
#                 if header is None:
#                     if len(cells) < 2:
#                         raise ValidationError(
#                             'Found header on line %d with the following '
#                             'columns: %s (length: %d), expected at least 2 '
#                             'columns.' % (i, cells, len(cells)))
#                     else:
#                         header = cells
#                 else:
#                     if len(cells) != len(header):
#                         raise ValidationError(
#                             'Line %d has %s cells (%s), expected %s.'
#                             % (i, len(cells), cells, len(header)))
# 
#                     records_seen += 1
# 
#             # The first non-comment and non-blank row observed will always be
#             # the header row, and since we have no requirement on the field
#             # names (because they are dynamically defined), so no need to check
#             # for the presence (or validity) of a header row at this point.
# 
#             if records_seen == 0:
#                 raise ValidationError('No records found in file, only '
#                                       'observed comments, blank lines, and/or '
#                                       'a header row.')


# Path: q2_types/sample_data/_transformer.py
import pandas as pd
import numpy as np
import qiime2
from ..plugin_setup import plugin
from . import AlphaDiversityFormat

    # of any columns or the index.
    df = pd.read_csv(fh, sep='\t', header=0, dtype=object)
    df.set_index(df.columns[0], drop=True, append=False, inplace=True)
    df.index.name = None
    # casting of columns adapted from SO post:
    # https://stackoverflow.com/a/36814203/3424666
    cols = df.columns
    df[cols] = df[cols].apply(pd.to_numeric, errors='ignore')
    return df


@plugin.register_transformer
def _1(data: pd.Series) -> AlphaDiversityFormat:
    ff = AlphaDiversityFormat()
    with ff.open() as fh:
        data.to_csv(fh, sep='\t', header=True)
    return ff


@plugin.register_transformer
def _2(ff: AlphaDiversityFormat) -> pd.Series:
    with ff.open() as fh:
        df = _read_alpha_diversity(fh)
        series = df.iloc[:, 0]
        if not np.issubdtype(series, np.number):
            raise ValueError('Non-numeric values detected in alpha diversity '
                             'estimates.')
        return series



@plugin.register_transformer