# Path: pergola/mapping.py
# class MappingInfo():
#     def __init__(self, path, **kwargs):
#     def _correspondence_from_config(self, path):
#     def _tab_config(self, file_tab):
#     def _mapping_config(self, file_map):
#     def write(self, indent=0):
# def check_path(path):
# def write_chr(self, mode="w", path_w=None, min_c=None, max_c=None):
# def write_chr_sizes(self, mode="w", path_w=None, file_n=None, min_c=None, max_c=None):
# def write_cytoband(end, start=0, delta=43200, start_phase="light", mode="w", path_w=None, lab_bed=True, track_line=True):
# def write_period_seq (end, start=0, delta=43200, tag="day", mode="w", path_w=None, name_file="period_seq", lab_bed=True, track_line=True):
# 
# Path: pergola/intervals.py
# class IntData(object):
#     def __init__(self, path, map_dict, header=True, **kwargs):
#     def _check_delimiter (self, path, delimiter):
#     def _reader_data(self):
#     def _pandas_df_reader(self, df):
#     def _set_fields_b(self, fields=None):
#     def _set_fields_g (self, map_dict):
#     def _simple_read(self):
#     def get_field_items(self, data, field="data_types", default=None):
#     def read(self, fields=None, relative_coord=False, intervals=False, int_step=None, fields2rel=None, multiply_t=None,
#              **kwargs):
#     def _min_max(self, list_data, f_start="start", f_end="end"):
#     def _time2rel_time(self, i_fields):
#     def _multiply_values(self, i_fields, factor=1):
#     def _create_int(self, start_int, int_step=None):
#     def _create_int_add_integ(self, start_int, integer=1):
#     def __del__(self):
# def is_number(var):
# def num(s):


import pybedtools
from os import path, getcwd
from pergola import mapping
from pergola import intervals
data_type_col_m = {'food_sc': 'orange', 'food_fat_food_sc':'blue'}

bed_str = data_read.convert(mode="bed", data_types=["food_sc", "food_fat"], data_types_actions="all", color_restrictions=data_type_col)
bed_str_out = data_read.convert(mode="bed", data_types=["food_sc", "food_fat"], data_types_actions="all", color_restrictions=data_type_col)
mapping_bed = mapping.MappingInfo(out_dir + "/pybed2perg.txt")

# Experimentals phases
# Habituation phase (before high-fat food introduction)
# Development phase (after high-fat food introduction)
mapping_file_data_exp_ph = mapping.MappingInfo(base_dir + "/sample_data/feeding_behavior/f2g.txt")
exp_ph_data = intervals.IntData(base_dir + "/sample_data/feeding_behavior/phases_exp.csv", map_dict=mapping_file_data_exp_ph.correspondence)
data_exp_ph_tr = exp_ph_data.read(relative_coord=True)  
exp_ph_bed = data_exp_ph_tr.convert(mode="bed", dataTypes_actions="all", tracks_merge=exp_ph_data.tracks)


hab_bed = exp_ph_bed [('1_2', 'Habituation phase')].create_pybedtools()
dev_bed = exp_ph_bed [('1_2', 'Development phase')].create_pybedtools()

# For each track merge feeding acts that are separated by less than 120 seconds (thus generating feeding bouts track)
# Calculates the complement of feeding bouts (intermeal intervals) and intersect them with day and experimental phases
# Dumping results into bed files
for tr, bed in bed_str.iteritems():
    
    bed_BedTools = bed.create_pybedtools()
    
    merged_tr_n = 'merged_' + '_'.join(tr)
    merged_tr_f = merged_tr_n + '.bed'

    bed_BedTools.merge(d=120, stream=True, c=(4,5,6,9), o=("distinct","sum","distinct","collapse")).saveas(merged_tr_f)
    

# Path: pergola/mapping.py
# class MappingInfo():
#     def __init__(self, path, **kwargs):
#     def _correspondence_from_config(self, path):
#     def _tab_config(self, file_tab):
#     def _mapping_config(self, file_map):
#     def write(self, indent=0):
# def check_path(path):
# def write_chr(self, mode="w", path_w=None, min_c=None, max_c=None):
# def write_chr_sizes(self, mode="w", path_w=None, file_n=None, min_c=None, max_c=None):
# def write_cytoband(end, start=0, delta=43200, start_phase="light", mode="w", path_w=None, lab_bed=True, track_line=True):
# def write_period_seq (end, start=0, delta=43200, tag="day", mode="w", path_w=None, name_file="period_seq", lab_bed=True, track_line=True):
# 
# Path: pergola/intervals.py
# class IntData(object):
#     def __init__(self, path, map_dict, header=True, **kwargs):
#     def _check_delimiter (self, path, delimiter):
#     def _reader_data(self):
#     def _pandas_df_reader(self, df):
#     def _set_fields_b(self, fields=None):
#     def _set_fields_g (self, map_dict):
#     def _simple_read(self):
#     def get_field_items(self, data, field="data_types", default=None):
#     def read(self, fields=None, relative_coord=False, intervals=False, int_step=None, fields2rel=None, multiply_t=None,
#              **kwargs):
#     def _min_max(self, list_data, f_start="start", f_end="end"):
#     def _time2rel_time(self, i_fields):
#     def _multiply_values(self, i_fields, factor=1):
#     def _create_int(self, start_int, int_step=None):
#     def _create_int_add_integ(self, start_int, integer=1):
#     def __del__(self):
# def is_number(var):
# def num(s):


# Path: examples/intermeal_extraction.py
import pybedtools
from os import path, getcwd
from pergola import mapping
from pergola import intervals

data_type_col_m = {'food_sc': 'orange', 'food_fat_food_sc':'blue'}

bed_str = data_read.convert(mode="bed", data_types=["food_sc", "food_fat"], data_types_actions="all", color_restrictions=data_type_col)
bed_str_out = data_read.convert(mode="bed", data_types=["food_sc", "food_fat"], data_types_actions="all", color_restrictions=data_type_col)
mapping_bed = mapping.MappingInfo(out_dir + "/pybed2perg.txt")

# Experimentals phases
# Habituation phase (before high-fat food introduction)
# Development phase (after high-fat food introduction)
mapping_file_data_exp_ph = mapping.MappingInfo(base_dir + "/sample_data/feeding_behavior/f2g.txt")
exp_ph_data = intervals.IntData(base_dir + "/sample_data/feeding_behavior/phases_exp.csv", map_dict=mapping_file_data_exp_ph.correspondence)
data_exp_ph_tr = exp_ph_data.read(relative_coord=True)  
exp_ph_bed = data_exp_ph_tr.convert(mode="bed", dataTypes_actions="all", tracks_merge=exp_ph_data.tracks)


hab_bed = exp_ph_bed [('1_2', 'Habituation phase')].create_pybedtools()
dev_bed = exp_ph_bed [('1_2', 'Development phase')].create_pybedtools()

# For each track merge feeding acts that are separated by less than 120 seconds (thus generating feeding bouts track)
# Calculates the complement of feeding bouts (intermeal intervals) and intersect them with day and experimental phases
# Dumping results into bed files
for tr, bed in bed_str.iteritems():
    
    bed_BedTools = bed.create_pybedtools()
    
    merged_tr_n = 'merged_' + '_'.join(tr)
    merged_tr_f = merged_tr_n + '.bed'

    bed_BedTools.merge(d=120, stream=True, c=(4,5,6,9), o=("distinct","sum","distinct","collapse")).saveas(merged_tr_f)
    

    pybed_intdata = intervals.IntData(merged_tr_f, map_dict=mapping_bed.correspondence, header=False, fields_names=['chrm', 'start', 'end', 'nature', 'value', 'strain', 'color'])