<commit_before># -----------------------------------------------------------------------------
# Copyright (c) 2014--, The Qiita Development Team.
#
# Distributed under the terms of the BSD 3-clause License.
#
# The full license is in the file LICENSE, distributed with this software.
# -----------------------------------------------------------------------------

from dateutil.parser import parse
from os import listdir
from os.path import join
from functools import partial
from future import standard_library
with standard_library.hooks():
    from configparser import ConfigParser

import pandas as pd

from .study import Study, StudyPerson
from .user import User
from .util import get_filetypes, get_filepath_types, get_data_types
from .data import RawData, PreprocessedData, ProcessedData
from .metadata_template import SampleTemplate, PrepTemplate


def load_study_from_cmd(owner, title, info):
    r"""Adds a study to the database

    Parameters
    ----------
    owner : str
        The email address of the owner of the study_abstract
    title : str
        The title of the study_abstract
    info : file-like object
        File-like object containing study information

    """
    # Parse the configuration file
    config = ConfigParser()
    config.readfp(info)

    optional = dict(config.items('optional'))
    get_optional = lambda name: optional.get(name, None)
    get_required = partial(config.get, 'required')
    required_fields = ['timeseries_type_id', 'mixs_compliant',
                       'portal_type_id', 'reprocess', 'study_alias',
                       'study_description', 'study_abstract',
                       'metadata_complete']
    optional_fields = ['funding', 'most_recent_contact', 'spatial_series',
                       'number_samples_collected', 'number_samples_promised',
                       'vamps_id', 'study_id']
    infodict = {}
    for value in required_fields:
        infodict[value] = get_required(value)

    for value in optional_fields:
        optvalue = get_optional(value)
        if optvalue is not None:
            infodict[value] = optvalue

    emp_person_name_email = get_optional('emp_person_name')
    if emp_person_name_email is not None:
        emp_name, emp_email, emp_affiliation = emp_person_name_email.split(',')
        infodict['emp_person_id'] = StudyPerson.create(emp_name.strip(),
                                                       emp_email.strip(),
                                                       emp_affiliation.strip())
    lab_name_email = get_optional('lab_person')
    if lab_name_email is not None:
        lab_name, lab_email, lab_affiliation = lab_name_email.split(',')
        infodict['lab_person_id'] = StudyPerson.create(lab_name.strip(),
                                                       lab_email.strip(),
                                                       lab_affiliation.strip())
    pi_name_email = get_required('principal_investigator')
    pi_name, pi_email, pi_affiliation = pi_name_email.split(',')
    infodict['principal_investigator_id'] = StudyPerson.create(
        pi_name.strip(), pi_email.strip(), pi_affiliation.strip())
    # this will eventually change to using the Experimental Factory Ontolgoy
    # names
    efo_ids = get_required('efo_ids')
    efo_ids = [x.strip() for x in efo_ids.split(',')]

    return Study.create(User(owner), title, efo_ids, infodict)


def load_preprocessed_data_from_cmd(study_id, params_table, filedir,
                                    filepathtype, params_id,
                                    submitted_to_insdc, raw_data_id):
    r"""Adds preprocessed data to the database

    Parameters
    ----------
    study_id : int
        The study id to which the preprocessed data belongs
    filedir : str
        Directory path of the preprocessed data
    filepathtype: str
        The filepath_type of the preprecessed data
    params_table_name : str
        The name of the table which contains the parameters of the
        preprocessing
    params_id : int
        The id of parameters int the params_table
    submitted_to_insdc : bool
        Has the data been submitted to insdc
    raw_data_id : int
        Raw data id associated with data
    """
    fp_types_dict = get_filepath_types()
    fp_type = fp_types_dict[filepathtype]
    filepaths = [(join(filedir, fp), fp_type) for fp in listdir(filedir)]
    raw_data = None if raw_data_id is None else RawData(raw_data_id)
    return PreprocessedData.create(Study(study_id), params_table, params_id,
                                   filepaths, raw_data=raw_data,
                                   submitted_to_insdc=submitted_to_insdc)


def load_sample_template_from_cmd(sample_temp_path, study_id):
    r"""Adds a sample template to the database

    Parameters
    ----------
    sample_temp_path : str
        Path to the sample template file
    study_id : int
        The study id to which the sample template belongs
    """
    sample_temp = pd.DataFrame.from_csv(sample_temp_path, sep='\t',
                                        infer_datetime_format=True)
    return SampleTemplate.create(sample_temp, Study(study_id))


def load_prep_template_from_cmd(sample_temp_path, study_id):
    r"""Adds a prep template to the database

    Parameters
    ----------
    prep_temp_path : str
        Path to the sample template file<commit_msg>TST: fixed failing test<commit_after># -----------------------------------------------------------------------------
# Copyright (c) 2014--, The Qiita Development Team.
#
# Distributed under the terms of the BSD 3-clause License.
#
# The full license is in the file LICENSE, distributed with this software.
# -----------------------------------------------------------------------------

from dateutil.parser import parse
from os import listdir
from os.path import join
from functools import partial
from future import standard_library
with standard_library.hooks():
    from configparser import ConfigParser

import pandas as pd

from .study import Study, StudyPerson
from .user import User
from .util import get_filetypes, get_filepath_types, get_data_types
from .data import RawData, PreprocessedData, ProcessedData
from .metadata_template import SampleTemplate, PrepTemplate


def load_study_from_cmd(owner, title, info):
    r"""Adds a study to the database

    Parameters
    ----------
    owner : str
        The email address of the owner of the study_abstract
    title : str
        The title of the study_abstract
    info : file-like object
        File-like object containing study information

    """
    # Parse the configuration file
    config = ConfigParser()
    config.readfp(info)

    optional = dict(config.items('optional'))
    get_optional = lambda name: optional.get(name, None)
    get_required = partial(config.get, 'required')
    required_fields = ['timeseries_type_id', 'mixs_compliant',
                       'portal_type_id', 'reprocess', 'study_alias',
                       'study_description', 'study_abstract',
                       'metadata_complete']
    optional_fields = ['funding', 'most_recent_contact', 'spatial_series',
                       'number_samples_collected', 'number_samples_promised',
                       'vamps_id', 'study_id']
    infodict = {}
    for value in required_fields:
        infodict[value] = get_required(value)

    for value in optional_fields:
        optvalue = get_optional(value)
        if optvalue is not None:
            infodict[value] = optvalue

    emp_person_name_email = get_optional('emp_person_name')
    if emp_person_name_email is not None:
        emp_name, emp_email, emp_affiliation = emp_person_name_email.split(',')
        infodict['emp_person_id'] = StudyPerson.create(emp_name.strip(),
                                                       emp_email.strip(),
                                                       emp_affiliation.strip())
    lab_name_email = get_optional('lab_person')
    if lab_name_email is not None:
        lab_name, lab_email, lab_affiliation = lab_name_email.split(',')
        infodict['lab_person_id'] = StudyPerson.create(lab_name.strip(),
                                                       lab_email.strip(),
                                                       lab_affiliation.strip())
    pi_name_email = get_required('principal_investigator')
    pi_name, pi_email, pi_affiliation = pi_name_email.split(',')
    infodict['principal_investigator_id'] = StudyPerson.create(
        pi_name.strip(), pi_email.strip(), pi_affiliation.strip())
    # this will eventually change to using the Experimental Factory Ontolgoy
    # names
    efo_ids = get_required('efo_ids')
    efo_ids = [x.strip() for x in efo_ids.split(',')]

    return Study.create(User(owner), title, efo_ids, infodict)


def load_preprocessed_data_from_cmd(study_id, params_table, filedir,
                                    filepathtype, params_id,
                                    submitted_to_insdc_status, raw_data_id):
    r"""Adds preprocessed data to the database

    Parameters
    ----------
    study_id : int
        The study id to which the preprocessed data belongs
    filedir : str
        Directory path of the preprocessed data
    filepathtype: str
        The filepath_type of the preprecessed data
    params_table_name : str
        The name of the table which contains the parameters of the
        preprocessing
    params_id : int
        The id of parameters int the params_table
    submitted_to_insdc : str
        INSDC submission status
    raw_data_id : int
        Raw data id associated with data
    """
    fp_types_dict = get_filepath_types()
    fp_type = fp_types_dict[filepathtype]
    filepaths = [(join(filedir, fp), fp_type) for fp in listdir(filedir)]
    raw_data = None if raw_data_id is None else RawData(raw_data_id)
    return PreprocessedData.create(
        Study(study_id), params_table, params_id, filepaths, raw_data=raw_data,
        submitted_to_insdc_status=submitted_to_insdc_status)


def load_sample_template_from_cmd(sample_temp_path, study_id):
    r"""Adds a sample template to the database

    Parameters
    ----------
    sample_temp_path : str
        Path to the sample template file
    study_id : int
        The study id to which the sample template belongs
    """
    sample_temp = pd.DataFrame.from_csv(sample_temp_path, sep='\t',
                                        infer_datetime_format=True)
    return SampleTemplate.create(sample_temp, Study(study_id))


def load_prep_template_from_cmd(sample_temp_path, study_id):
    r"""Adds a prep template to the database

    Parameters
    ----------
    prep_temp_path : str
        Path to the sample template file
{'fraction_non_alphanumeric': 0.1043874326082915, 'fraction_numerical': 0.0009295408068414204, 'mean_word_length': 2.293235384144475, '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': 26, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2056790', 'n_tokens_mistral': 2960, 'n_tokens_neox': 2862, 'n_words': 811}