<commit_before>from .. import get_python_manager, get_gateway, none, Some, \
    get_remote_manager, get_mode, _get_source_table
from .loaders import MetaLoaderFile, RegLoaderFile, Materializations
from .DataStructures.RegField import RegField
from .DataStructures.MetaField import MetaField
from .DataStructures.Aggregates import Aggregate
import shutil
import os
from .DataStructures import reg_fixed_fileds
from .import GDataframe
from .loaders import MemoryLoader, Loader
from ..FileManagment.TempFileManager import get_unique_identifier, get_new_dataset_tmp_folder


class GMQLDataset:
    """
    The main abstraction of the library.
    A GMQLDataset represents a genomic dataset in the GMQL standard and it is divided
    in region data and meta data. The function that can be applied to a GMQLDataset
    affect one of these two features or both.
    """

    def __init__(self, parser=None, index=None, location="local", path_or_name=None,
                 local_sources=None, remote_sources=None):
        self.parser = parser
        self.index = index
        self.location = location
        self.path_or_name = path_or_name
        self.pmg = get_python_manager()
        self.opmng = self.pmg.getOperatorManager()

        # provenance
        if isinstance(local_sources, list):
            self._local_sources = local_sources
        else:
            self._local_sources = []
        if isinstance(remote_sources, list):
            self._remote_sources = remote_sources
        else:
            self._remote_sources = []

        # get the schema of the dataset
        schemaJava = self.pmg.getVariableSchemaNames(self.index)
        self.schema = []
        for field in schemaJava:
            self.schema.append(field)
        self.schema.extend(reg_fixed_fileds)

        # setting the schema as properties of the dataset
        for field in self.schema:
            self.__setattr__(field, self.RegField(field))
        # add also left and right
        self.left = self.RegField("left")
        self.right = self.RegField("right")

    def __getitem__(self, item):
        if isinstance(item, str):
            return self.MetaField(item)
        elif isinstance(item, MetaField):
            return self.meta_select(predicate=item)
        else:
            raise ValueError("Input must be a string or a MetaField. {} was found".format(type(item)))

    def __show_info(self):
        print("GMQLDataset")
        print("\tParser:\t{}".format(self.parser.get_parser_name()))
        print("\tIndex:\t{}".format(self.index))

    def get_metadata(self):
        if self.path_or_name is None:
            raise ValueError("You cannot explore the metadata of an intermediate query."
                             "You can get metadata only after a load_from_local or load_from_remote")
        if self.location == 'local':
            return self.__get_metadata_local()
        elif self.location == 'remote':
            return self.__get_metadata_remote()

    def __get_metadata_local(self):
        meta = MetaLoaderFile.load_meta_from_path(self.path_or_name)
        return GDataframe.GDataframe(meta=meta)

    def __get_metadata_remote(self):
        pass

    def get_reg_attributes(self):
        """
        Returns the region fields of the dataset
        
        :return: a list of field names
        """
        return self.schema

    def MetaField(self, name):
        """
        Creates an instance of a metadata field of the dataset. It can be used in building expressions
        or conditions for projection or selection.
        Notice that this function is equivalent to call::
            
            dataset[name]
        
        :param name: the name of the metadata that is considered
        :return: a MetaField instance
        """
        return MetaField(name=name, index=self.index)

    def RegField(self, name):
        """
        Creates an instance of a region field of the dataset. It can be used in building expressions
        or conditions for projection or selection.<commit_msg>changed 'get_metadata' to return directly a dataframe<commit_after>from .. import get_python_manager, get_gateway, none, Some, \
    get_remote_manager, get_mode, _get_source_table
from .loaders import MetaLoaderFile, RegLoaderFile, Materializations
from .DataStructures.RegField import RegField
from .DataStructures.MetaField import MetaField
from .DataStructures.Aggregates import Aggregate
import shutil
import os
from .DataStructures import reg_fixed_fileds
from .import GDataframe
from .loaders import MemoryLoader, Loader
from ..FileManagment.TempFileManager import get_unique_identifier, get_new_dataset_tmp_folder


class GMQLDataset:
    """
    The main abstraction of the library.
    A GMQLDataset represents a genomic dataset in the GMQL standard and it is divided
    in region data and meta data. The function that can be applied to a GMQLDataset
    affect one of these two features or both.
    """

    def __init__(self, parser=None, index=None, location="local", path_or_name=None,
                 local_sources=None, remote_sources=None):
        self.parser = parser
        self.index = index
        self.location = location
        self.path_or_name = path_or_name
        self.pmg = get_python_manager()
        self.opmng = self.pmg.getOperatorManager()

        # provenance
        if isinstance(local_sources, list):
            self._local_sources = local_sources
        else:
            self._local_sources = []
        if isinstance(remote_sources, list):
            self._remote_sources = remote_sources
        else:
            self._remote_sources = []

        # get the schema of the dataset
        schemaJava = self.pmg.getVariableSchemaNames(self.index)
        self.schema = []
        for field in schemaJava:
            self.schema.append(field)
        self.schema.extend(reg_fixed_fileds)

        # setting the schema as properties of the dataset
        for field in self.schema:
            self.__setattr__(field, self.RegField(field))
        # add also left and right
        self.left = self.RegField("left")
        self.right = self.RegField("right")

    def __getitem__(self, item):
        if isinstance(item, str):
            return self.MetaField(item)
        elif isinstance(item, MetaField):
            return self.meta_select(predicate=item)
        else:
            raise ValueError("Input must be a string or a MetaField. {} was found".format(type(item)))

    def __show_info(self):
        print("GMQLDataset")
        print("\tParser:\t{}".format(self.parser.get_parser_name()))
        print("\tIndex:\t{}".format(self.index))

    def get_metadata(self):
        if self.path_or_name is None:
            raise ValueError("You cannot explore the metadata of an intermediate query."
                             "You can get metadata only after a load_from_local or load_from_remote")
        if self.location == 'local':
            return self.__get_metadata_local()
        elif self.location == 'remote':
            return self.__get_metadata_remote()

    def __get_metadata_local(self):
        meta = MetaLoaderFile.load_meta_from_path(self.path_or_name)
        return meta

    def __get_metadata_remote(self):
        pass

    def get_reg_attributes(self):
        """
        Returns the region fields of the dataset
        
        :return: a list of field names
        """
        return self.schema

    def MetaField(self, name):
        """
        Creates an instance of a metadata field of the dataset. It can be used in building expressions
        or conditions for projection or selection.
        Notice that this function is equivalent to call::
            
            dataset[name]
        
        :param name: the name of the metadata that is considered
        :return: a MetaField instance
        """
        return MetaField(name=name, index=self.index)

    def RegField(self, name):
        """
        Creates an instance of a region field of the dataset. It can be used in building expressions
        or conditions for projection or selection.
{'fraction_non_alphanumeric': 0.07419275326596007, 'fraction_numerical': 0.0, 'mean_word_length': 2.531331592689295, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '92488', 'n_tokens_mistral': 2138, 'n_tokens_neox': 2125, 'n_words': 679}