# Path: rxncon/input/sbtab/sbtab.py
# def sbtab_data_from_file(filename: str, separator: str = '\t', definitions: Optional[SBtabData] = None) -> SBtabData:
#     sbtab_input = []
# 
#     with open(filename) as f:
#         for row in f:
#             if row.startswith('%'):
#                 continue
#             else:
#                 sbtab_input.append(row.split(separator))
# 
#     if definitions:
#         return ValidatedSBtabData(sbtab_input, definitions)
#     else:
#         return SBtabData(sbtab_input)
# 
# class ValidatedSBtabData(SBtabData):
#     def __init__(self, input: List[List[str]], definition: SBtabData) -> None:
#         super().__init__(input)
# 
#         self._definition = definition
#         self._field_postprocessors = {}  # type: Dict[str, Callable[[str], Union[str, float, bool, int]]]
#         self._construct_field_postprocessors()
#         self._entry_class.field_postprocessors = {_field_name_from_column_name(col): func  # type: ignore
#                                                   for col, func in self._field_postprocessors.items()}  # type: ignore
#         self._postprocess_entries()
# 
#     def _construct_field_postprocessors(self) -> None:
#         if hasattr(self._definition.entries[0], 'ComponentName'):
#             type_definitions = {def_entry.ComponentName: def_entry.Format  # type: ignore
#                                 for def_entry in self._definition.entries
#                                 if def_entry.IsPartOf == self.table_type}  # type: ignore
#         elif hasattr(self._definition.entries[0], 'Component'):
#             type_definitions = {def_entry.Component: def_entry.Format      # type: ignore
#                                 for def_entry in self._definition.entries
#                                 if def_entry.IsPartOf == self.table_type}  # type: ignore
#         else:
#             raise AssertionError('Could not parse the definition file')
# 
#         for column in self._column_names:
#             self._field_postprocessors[column] = _field_postprocessor_for_type_string(type_definitions[column])
# 
#     def _postprocess_entries(self) -> None:
#         for entry in self.entries:
#             entry.postprocess()
# 
# class SBtabData:
#     def __init__(self, input: List[List[str]]) -> None:
#         self.version = None  # type: Optional[str]
#         self.entries = []  # type: List[EntryBase]
#         self.document_name = None  # type: Optional[str]
#         self.table_type = None  # type: Optional[str]
#         self.table_name = None  # type: Optional[str]
# 
#         self._input = input
#         self._column_names = []  # type: List[str]
# 
#         self._parse_header()
#         self._parse_column_names()
#         self._construct_entry_class()
#         self._parse_entries()
# 
#     def _parse_header(self) -> None:
#         REGEX_VERSION_A = 'SBtabVersion (\'|\").+?(\'|\")'
#         REGEX_VERSION_B = 'SBtabVersion=(\'|\").+?(\'|\")'
#         REGEX_DOCUMENT = 'Document=(\'|\").+?(\'|\")'
#         REGEX_TABLE_TYPE = 'TableType=(\'|\").+?(\'|\")'
#         REGEX_TABLE_NAME = 'TableName=(\'|\").+?(\'|\")'
# 
#         for col in self._input[0][1:]:
#             assert not col.strip('\n')
# 
#         header = self._input[0][0]
# 
#         assert header.startswith('!!SBtab')
# 
#         match = re.search(REGEX_VERSION_A, header)
#         if match:
#             setattr(self, 'version', _unquote(match.group(0).split(' ')[1]))
# 
#         for regex, attr in zip([REGEX_VERSION_B, REGEX_DOCUMENT, REGEX_TABLE_TYPE, REGEX_TABLE_NAME],
#                                ['version', 'document_name', 'table_type', 'table_name']):
#             match = re.search(regex, header)
#             if match:
#                 setattr(self, attr, _header_value(match.group(0)))
# 
#     def _parse_column_names(self) -> None:
#         columns = self._input[1]
#         assert len(columns) > 0
# 
#         self._column_names = [_cleaned_column_name(name) for name in columns]
# 
#     def _construct_entry_class(self) -> None:
#         assert self.table_name
#         assert self._column_names
#         self._entry_class = type(_class_name_from_table_name(self.table_name), (EntryBase,),
#                                  {'field_names': [_field_name_from_column_name(col) for col in self._column_names]})
# 
#     def _parse_entries(self) -> None:
#         for row in self._input[2:]:
#             entry = self._entry_class()
# 
#             for i, column_value in enumerate(row):
#                 setattr(entry, _field_name_from_column_name(self._column_names[i]), column_value.strip())
# 
#             self.entries.append(entry)


import os
import math
import pytest
from rxncon.input.sbtab.sbtab import sbtab_data_from_file, ValidatedSBtabData, SBtabData



TIGER_CONTINGENCY_FILENAME = 'Tiger_et_al_TableS1_SBtab_ContingencyID.tsv'
TIGER_CONTINGENCY_PATH     = os.path.join(os.path.dirname(__file__), TIGER_CONTINGENCY_FILENAME)

DEFINITIONS_FILENAME = 'definitions.tsv'
DEFINITIONS_PATH     = os.path.join(os.path.dirname(__file__), DEFINITIONS_FILENAME)

RXNCON_DEFINITIONS_FILENAME = 'rxncon_Definition.tsv'
RXNCON_DEFINITIONS_PATH     = os.path.join(os.path.dirname(__file__), RXNCON_DEFINITIONS_FILENAME)


def test_sbtab_definition_from_file() -> None:

# Path: rxncon/input/sbtab/sbtab.py
# def sbtab_data_from_file(filename: str, separator: str = '\t', definitions: Optional[SBtabData] = None) -> SBtabData:
#     sbtab_input = []
# 
#     with open(filename) as f:
#         for row in f:
#             if row.startswith('%'):
#                 continue
#             else:
#                 sbtab_input.append(row.split(separator))
# 
#     if definitions:
#         return ValidatedSBtabData(sbtab_input, definitions)
#     else:
#         return SBtabData(sbtab_input)
# 
# class ValidatedSBtabData(SBtabData):
#     def __init__(self, input: List[List[str]], definition: SBtabData) -> None:
#         super().__init__(input)
# 
#         self._definition = definition
#         self._field_postprocessors = {}  # type: Dict[str, Callable[[str], Union[str, float, bool, int]]]
#         self._construct_field_postprocessors()
#         self._entry_class.field_postprocessors = {_field_name_from_column_name(col): func  # type: ignore
#                                                   for col, func in self._field_postprocessors.items()}  # type: ignore
#         self._postprocess_entries()
# 
#     def _construct_field_postprocessors(self) -> None:
#         if hasattr(self._definition.entries[0], 'ComponentName'):
#             type_definitions = {def_entry.ComponentName: def_entry.Format  # type: ignore
#                                 for def_entry in self._definition.entries
#                                 if def_entry.IsPartOf == self.table_type}  # type: ignore
#         elif hasattr(self._definition.entries[0], 'Component'):
#             type_definitions = {def_entry.Component: def_entry.Format      # type: ignore
#                                 for def_entry in self._definition.entries
#                                 if def_entry.IsPartOf == self.table_type}  # type: ignore
#         else:
#             raise AssertionError('Could not parse the definition file')
# 
#         for column in self._column_names:
#             self._field_postprocessors[column] = _field_postprocessor_for_type_string(type_definitions[column])
# 
#     def _postprocess_entries(self) -> None:
#         for entry in self.entries:
#             entry.postprocess()
# 
# class SBtabData:
#     def __init__(self, input: List[List[str]]) -> None:
#         self.version = None  # type: Optional[str]
#         self.entries = []  # type: List[EntryBase]
#         self.document_name = None  # type: Optional[str]
#         self.table_type = None  # type: Optional[str]
#         self.table_name = None  # type: Optional[str]
# 
#         self._input = input
#         self._column_names = []  # type: List[str]
# 
#         self._parse_header()
#         self._parse_column_names()
#         self._construct_entry_class()
#         self._parse_entries()
# 
#     def _parse_header(self) -> None:
#         REGEX_VERSION_A = 'SBtabVersion (\'|\").+?(\'|\")'
#         REGEX_VERSION_B = 'SBtabVersion=(\'|\").+?(\'|\")'
#         REGEX_DOCUMENT = 'Document=(\'|\").+?(\'|\")'
#         REGEX_TABLE_TYPE = 'TableType=(\'|\").+?(\'|\")'
#         REGEX_TABLE_NAME = 'TableName=(\'|\").+?(\'|\")'
# 
#         for col in self._input[0][1:]:
#             assert not col.strip('\n')
# 
#         header = self._input[0][0]
# 
#         assert header.startswith('!!SBtab')
# 
#         match = re.search(REGEX_VERSION_A, header)
#         if match:
#             setattr(self, 'version', _unquote(match.group(0).split(' ')[1]))
# 
#         for regex, attr in zip([REGEX_VERSION_B, REGEX_DOCUMENT, REGEX_TABLE_TYPE, REGEX_TABLE_NAME],
#                                ['version', 'document_name', 'table_type', 'table_name']):
#             match = re.search(regex, header)
#             if match:
#                 setattr(self, attr, _header_value(match.group(0)))
# 
#     def _parse_column_names(self) -> None:
#         columns = self._input[1]
#         assert len(columns) > 0
# 
#         self._column_names = [_cleaned_column_name(name) for name in columns]
# 
#     def _construct_entry_class(self) -> None:
#         assert self.table_name
#         assert self._column_names
#         self._entry_class = type(_class_name_from_table_name(self.table_name), (EntryBase,),
#                                  {'field_names': [_field_name_from_column_name(col) for col in self._column_names]})
# 
#     def _parse_entries(self) -> None:
#         for row in self._input[2:]:
#             entry = self._entry_class()
# 
#             for i, column_value in enumerate(row):
#                 setattr(entry, _field_name_from_column_name(self._column_names[i]), column_value.strip())
# 
#             self.entries.append(entry)


# Path: rxncon/test/input/sbtab/test_sbtab.py
import os
import math
import pytest
from rxncon.input.sbtab.sbtab import sbtab_data_from_file, ValidatedSBtabData, SBtabData




TIGER_CONTINGENCY_FILENAME = 'Tiger_et_al_TableS1_SBtab_ContingencyID.tsv'
TIGER_CONTINGENCY_PATH     = os.path.join(os.path.dirname(__file__), TIGER_CONTINGENCY_FILENAME)

DEFINITIONS_FILENAME = 'definitions.tsv'
DEFINITIONS_PATH     = os.path.join(os.path.dirname(__file__), DEFINITIONS_FILENAME)

RXNCON_DEFINITIONS_FILENAME = 'rxncon_Definition.tsv'
RXNCON_DEFINITIONS_PATH     = os.path.join(os.path.dirname(__file__), RXNCON_DEFINITIONS_FILENAME)


def test_sbtab_definition_from_file() -> None:

    sbtab = sbtab_data_from_file(DEFINITIONS_PATH)