<commit_before>from .storers import FrameToGMQL
import pandas as pd
from .loaders import Loader
from .DataStructures import reg_fixed_fileds, \
    strand_aliases, stop_aliases, start_aliases, chr_aliases, id_sample_aliases
import numpy as np

chr_types = [object, str]
start_types = [int, np.int, np.int8, np.int16, np.int32, np.int64]
stop_types = start_types
strand_types = [object, str]
id_sample_types = [object, str] + start_types

default_id_sample = "sample"

class GDataframe:
    """ Class holding the result of a materialization of a GMQLDataset.
    It is composed by two data structures:
    
        - A table with the *region* data
        - A table with the *metadata* corresponding to the regions
    """
    def __init__(self, regs, meta):
        if not isinstance(regs, pd.DataFrame):
            raise TypeError("regs: expected pandas Dataframe, got {}".format(type(regs)))
        if not isinstance(meta, pd.DataFrame):
            raise TypeError("meta: expected pandas Dataframe, got {}".format(type(meta)))
        self.regs = regs
        self.meta = meta

    def to_genomic_space(self):
        """ Translates the GDataframe to the Genomic Space data structure
        """
        raise NotImplementedError("Not yet implemented")

    def to_dataset_files(self, local_path=None, remote_path=None):
        """ Save the GDataframe to a local or remote location

        :param local_path: a local path to the folder in which the data must be saved
        :param remote_path: a remote dataset name that wants to be used for these data
        :return: None
        """
        return FrameToGMQL.to_dataset_files(self, path_local=local_path, path_remote=remote_path)

    def to_GMQLDataset(self, local_path=None, remote_path=None):
        """ Converts the GDataframe in a GMQLDataset for later local or remote computation

        :return: a GMQLDataset
        """
        self.to_dataset_files(local_path, remote_path)
        if local_path is not None:
            return Loader.load_from_path(local_path=local_path)
        elif remote_path is not None:
            raise NotImplementedError()
        else:
            raise ValueError("You must specify at least one of local_path and remote_path")


def from_pandas(regs, meta=None, chr_name=None, start_name=None, stop_name=None,
                strand_name=None, sample_name=None):
    """ Creates a GDataframe from a pandas dataframe of region and a pandas dataframe of metadata

    :param regs: a pandas Dataframe of regions that is coherent with the GMQL data model
    :param meta: (optional) a pandas Dataframe of metadata that is coherent with the regions
    :param chr_name: (optional) which column of :attr:`~.regs` is the chromosome
    :param start_name: (optional) which column of :attr:`~.regs` is the start
    :param stop_name: (optional) which column of :attr:`~.regs` is the stop
    :param strand_name: (optional) which column of :attr:`~.regs` is the strand
    :param sample_name: (optional) which column of :attr:`~.regs` represents the sample name
           of that region. If nothing is provided, all the region will be put in a single sample.
    :return: a GDataframe
    """
    regs = check_regs(regs, chr_name, start_name, stop_name, strand_name, sample_name)<commit_msg>added temporary file management in to_GMQLDataset<commit_after>from .storers import FrameToGMQL
import pandas as pd
from .loaders import Loader
from .DataStructures import reg_fixed_fileds, \
    strand_aliases, stop_aliases, start_aliases, chr_aliases, id_sample_aliases
import numpy as np
from ..FileManagment import TempFileManager

chr_types = [object, str]
start_types = [int, np.int, np.int8, np.int16, np.int32, np.int64]
stop_types = start_types
strand_types = [object, str]
id_sample_types = [object, str] + start_types

default_id_sample = "sample"


class GDataframe:
    """ Class holding the result of a materialization of a GMQLDataset.
    It is composed by two data structures:
    
        - A table with the *region* data
        - A table with the *metadata* corresponding to the regions
    """
    def __init__(self, regs, meta):
        if not isinstance(regs, pd.DataFrame):
            raise TypeError("regs: expected pandas Dataframe, got {}".format(type(regs)))
        if not isinstance(meta, pd.DataFrame):
            raise TypeError("meta: expected pandas Dataframe, got {}".format(type(meta)))
        self.regs = regs
        self.meta = meta

    def to_genomic_space(self):
        """ Translates the GDataframe to the Genomic Space data structure
        """
        raise NotImplementedError("Not yet implemented")

    def to_dataset_files(self, local_path=None, remote_path=None):
        """ Save the GDataframe to a local or remote location

        :param local_path: a local path to the folder in which the data must be saved
        :param remote_path: a remote dataset name that wants to be used for these data
        :return: None
        """
        return FrameToGMQL.to_dataset_files(self, path_local=local_path, path_remote=remote_path)

    def to_GMQLDataset(self, local_path=None, remote_path=None):
        """ Converts the GDataframe in a GMQLDataset for later local or remote computation

        :return: a GMQLDataset
        """
        local = None
        remote = None
        if (local_path is None) and (remote_path is None):
            # get a temporary path
            local = TempFileManager.get_new_dataset_tmp_folder()
        if local_path is not None:
            local = local_path
        if remote_path is not None:
            remote = remote_path
        self.to_dataset_files(local, remote)

        if local is not None:
            return Loader.load_from_path(local_path=local)
        elif remote is not None:
            raise NotImplementedError("The remote loading is not implemented yet!")


def from_pandas(regs, meta=None, chr_name=None, start_name=None, stop_name=None,
                strand_name=None, sample_name=None):
    """ Creates a GDataframe from a pandas dataframe of region and a pandas dataframe of metadata

    :param regs: a pandas Dataframe of regions that is coherent with the GMQL data model
    :param meta: (optional) a pandas Dataframe of metadata that is coherent with the regions
    :param chr_name: (optional) which column of :attr:`~.regs` is the chromosome
    :param start_name: (optional) which column of :attr:`~.regs` is the start
    :param stop_name: (optional) which column of :attr:`~.regs` is the stop
    :param strand_name: (optional) which column of :attr:`~.regs` is the strand
    :param sample_name: (optional) which column of :attr:`~.regs` represents the sample name
           of that region. If nothing is provided, all the region will be put in a single sample.
    :return: a GDataframe
    """
    regs = check_regs(regs, chr_name, start_name, stop_name, strand_name, sample_name)
{'fraction_non_alphanumeric': 0.07845975680370584, 'fraction_numerical': 0.002026635784597568, 'mean_word_length': 3.251692307692308, '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': '1329948', 'n_tokens_mistral': 2010, 'n_tokens_neox': 1944, 'n_words': 734}