# Path: q2_types/feature_table/_format.py
# class BIOMV100Format(model.TextFileFormat):
#     top_level_keys = {
#         'id', 'format', 'format_url', 'type', 'generated_by',
#         'date', 'rows', 'columns', 'matrix_type', 'matrix_element_type',
#         'shape', 'data', 'comment'
#     }
# 
#     def sniff(self):
#         # Can't self.open(mode='rb'), so we defer to the backing pathlib object
#         with self.path.open(mode='rb') as fh:
#             try:
#                 parser = ijson.parse(fh)
#                 for prefix, event, value in parser:
#                     if (prefix, event) == ('', 'map_key'):
#                         # `format_url` seems pretty unique to BIOM 1.0.
#                         if value == 'format_url':
#                             return True
#                         elif value not in self.top_level_keys:
#                             return False
#             except (ijson.JSONError, UnicodeDecodeError):
#                 pass
#             return False
# 
# class BIOMV210Format(model.BinaryFileFormat):
#     # minimum requirements as described by
#     # http://biom-format.org/documentation/format_versions/biom-2.1.html
#     groups = {'sample',
#               'sample/matrix',
#               'sample/metadata',
#               'sample/group-metadata',
#               'observation',
#               'observation/matrix',
#               'observation/metadata',
#               'observation/group-metadata'}
#     datasets = {'sample/ids',
#                 'sample/matrix/data',
#                 'sample/matrix/indptr',
#                 'sample/matrix/indices',
#                 'observation/ids',
#                 'observation/matrix/data',
#                 'observation/matrix/indptr',
#                 'observation/matrix/indices'}
#     attrs = {'id',
#              'type',
#              'format-url',
#              'format-version',
#              'generated-by',
#              'creation-date',
#              'shape',
#              'nnz'}
# 
#     def open(self):
#         return h5py.File(str(self), mode=self._mode)
# 
#     def sniff(self):
#         try:
#             # Always sniff in read-only mode. If opened in 'w' mode,
#             # h5py will truncate the file, effectively destroying
#             # the table's contents
#             with h5py.File(str(self), mode='r') as fh:
#                 for grp in self.groups:
#                     if grp not in fh:
#                         return False
#                 for ds in self.datasets:
#                     if ds not in fh:
#                         return False
#                 for attr in self.attrs:
#                     if attr not in fh.attrs:
#                         return False
#                 return True
#         except Exception:
#             return False
# 
# Path: q2_types/feature_table/_transformer.py
# def _parse_biom_table_v100(ff):
#     with ff.open() as fh:
#         table = biom.Table.from_json(json.load(fh))
#         return _drop_axis_metadata(table)
# 
# def _parse_biom_table_v210(ff):
#     with ff.open() as fh:
#         table = biom.Table.from_hdf5(fh)
#         return _drop_axis_metadata(table)
# 
# def _table_to_dataframe(table: biom.Table) -> pd.DataFrame:
#     array = table.matrix_data.toarray().T
#     sample_ids = table.ids(axis='sample')
#     feature_ids = table.ids(axis='observation')
#     return pd.DataFrame(array, index=sample_ids, columns=feature_ids)
# 
# def _table_to_metadata(table: biom.Table) -> qiime2.Metadata:
#     table = _table_to_dataframe(table)
#     table.index.name = 'id'
#     return qiime2.Metadata(table)


import unittest
import biom
import pandas as pd
import qiime2
from pandas.testing import assert_frame_equal
from q2_types.feature_table import BIOMV100Format, BIOMV210Format
from qiime2.plugin.testing import TestPluginBase
from q2_types.feature_table._transformer import (_parse_biom_table_v100,
                                                 _parse_biom_table_v210,
                                                 _table_to_dataframe,
                                                 _table_to_metadata)

    def test_to_pandas_data_frame_to_biom_table(self):
        filepath = self.get_data_path('feature-table_v100.biom')
        transformer1 = self.get_transformer(BIOMV100Format, pd.DataFrame)
        input = BIOMV100Format(filepath, mode='r')
        df = transformer1(input)

        transformer2 = self.get_transformer(pd.DataFrame, biom.Table)
        obs = transformer2(df)
        self.assertIsInstance(obs, biom.Table)

    def test_biom_table_to_metadata(self):
        filepath = self.get_data_path('feature-table_v100.biom')
        transformer = self.get_transformer(biom.Table, qiime2.Metadata)
        input = biom.load_table(filepath)

        obs = transformer(input)

        self.assertIsInstance(obs, qiime2.Metadata)

    def test_biom_v100_format_to_metadata(self):
        input, obs = self.transform_format(BIOMV100Format, qiime2.Metadata,
                                           filename='feature-table_v100.biom')

        table = _parse_biom_table_v100(input)
        df = _table_to_metadata(table)

        self.assertEqual(df, obs)

    def test_biom_v210_format_to_metadata(self):

# Path: q2_types/feature_table/_format.py
# class BIOMV100Format(model.TextFileFormat):
#     top_level_keys = {
#         'id', 'format', 'format_url', 'type', 'generated_by',
#         'date', 'rows', 'columns', 'matrix_type', 'matrix_element_type',
#         'shape', 'data', 'comment'
#     }
# 
#     def sniff(self):
#         # Can't self.open(mode='rb'), so we defer to the backing pathlib object
#         with self.path.open(mode='rb') as fh:
#             try:
#                 parser = ijson.parse(fh)
#                 for prefix, event, value in parser:
#                     if (prefix, event) == ('', 'map_key'):
#                         # `format_url` seems pretty unique to BIOM 1.0.
#                         if value == 'format_url':
#                             return True
#                         elif value not in self.top_level_keys:
#                             return False
#             except (ijson.JSONError, UnicodeDecodeError):
#                 pass
#             return False
# 
# class BIOMV210Format(model.BinaryFileFormat):
#     # minimum requirements as described by
#     # http://biom-format.org/documentation/format_versions/biom-2.1.html
#     groups = {'sample',
#               'sample/matrix',
#               'sample/metadata',
#               'sample/group-metadata',
#               'observation',
#               'observation/matrix',
#               'observation/metadata',
#               'observation/group-metadata'}
#     datasets = {'sample/ids',
#                 'sample/matrix/data',
#                 'sample/matrix/indptr',
#                 'sample/matrix/indices',
#                 'observation/ids',
#                 'observation/matrix/data',
#                 'observation/matrix/indptr',
#                 'observation/matrix/indices'}
#     attrs = {'id',
#              'type',
#              'format-url',
#              'format-version',
#              'generated-by',
#              'creation-date',
#              'shape',
#              'nnz'}
# 
#     def open(self):
#         return h5py.File(str(self), mode=self._mode)
# 
#     def sniff(self):
#         try:
#             # Always sniff in read-only mode. If opened in 'w' mode,
#             # h5py will truncate the file, effectively destroying
#             # the table's contents
#             with h5py.File(str(self), mode='r') as fh:
#                 for grp in self.groups:
#                     if grp not in fh:
#                         return False
#                 for ds in self.datasets:
#                     if ds not in fh:
#                         return False
#                 for attr in self.attrs:
#                     if attr not in fh.attrs:
#                         return False
#                 return True
#         except Exception:
#             return False
# 
# Path: q2_types/feature_table/_transformer.py
# def _parse_biom_table_v100(ff):
#     with ff.open() as fh:
#         table = biom.Table.from_json(json.load(fh))
#         return _drop_axis_metadata(table)
# 
# def _parse_biom_table_v210(ff):
#     with ff.open() as fh:
#         table = biom.Table.from_hdf5(fh)
#         return _drop_axis_metadata(table)
# 
# def _table_to_dataframe(table: biom.Table) -> pd.DataFrame:
#     array = table.matrix_data.toarray().T
#     sample_ids = table.ids(axis='sample')
#     feature_ids = table.ids(axis='observation')
#     return pd.DataFrame(array, index=sample_ids, columns=feature_ids)
# 
# def _table_to_metadata(table: biom.Table) -> qiime2.Metadata:
#     table = _table_to_dataframe(table)
#     table.index.name = 'id'
#     return qiime2.Metadata(table)


# Path: q2_types/feature_table/tests/test_transformer.py
import unittest
import biom
import pandas as pd
import qiime2
from pandas.testing import assert_frame_equal
from q2_types.feature_table import BIOMV100Format, BIOMV210Format
from qiime2.plugin.testing import TestPluginBase
from q2_types.feature_table._transformer import (_parse_biom_table_v100,
                                                 _parse_biom_table_v210,
                                                 _table_to_dataframe,
                                                 _table_to_metadata)


    def test_to_pandas_data_frame_to_biom_table(self):
        filepath = self.get_data_path('feature-table_v100.biom')
        transformer1 = self.get_transformer(BIOMV100Format, pd.DataFrame)
        input = BIOMV100Format(filepath, mode='r')
        df = transformer1(input)

        transformer2 = self.get_transformer(pd.DataFrame, biom.Table)
        obs = transformer2(df)
        self.assertIsInstance(obs, biom.Table)

    def test_biom_table_to_metadata(self):
        filepath = self.get_data_path('feature-table_v100.biom')
        transformer = self.get_transformer(biom.Table, qiime2.Metadata)
        input = biom.load_table(filepath)

        obs = transformer(input)

        self.assertIsInstance(obs, qiime2.Metadata)

    def test_biom_v100_format_to_metadata(self):
        input, obs = self.transform_format(BIOMV100Format, qiime2.Metadata,
                                           filename='feature-table_v100.biom')

        table = _parse_biom_table_v100(input)
        df = _table_to_metadata(table)

        self.assertEqual(df, obs)

    def test_biom_v210_format_to_metadata(self):

        input, obs = self.transform_format(BIOMV210Format, qiime2.Metadata,