# You might have to set the path to run this notebook directly from ipython notebook
import sys

my_path_to_modules = "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/"
sys.path.append(my_path_to_modules)

"""
Explanation: .. _getting_start:
You can code your own scripts using pergola as a Python library. Here we summarize some examples of how you can use it on your scripts.
<div class="alert alert-block alert-info"><b>Tip:</b> This page is available as a <a href="http://jupyter.org/">Jupyter Notebook</a> on <code>/pergola/doc/notebooks</code> under <a href="https://github.com/cbcrg/pergola">pergola</a> GitHub repository. Should you want, you can interactively execute the code using Jupyter.</div>

Input data
The two basics data inputs pergola uses is a file with longitudinal recordings (sequence of temporal events) in the form of a CSV or xlsx file and a mapping file containing the correspondence between the fields in this previous file and the pergola ontology.
Sequence of temporal events
Pergola can process any sequence of temporal events contained in a character-separated file as in the example below:  
Animal  StartT  EndT    Behavior    Value
1   137 156 eat 0.06
1   168 192 drink   0.02
1   250 281 eat 0.07
1   311 333 eat 0.08
1   457 482 drink   0.02
1   569 601 drink   0.03
<div class="alert alert-block alert-info"><b>Note:</b> This example loads a sequence of eating and drinking events from a experiment where mice were used to study feeding behavior.</div>

Mapping file
Pergola needs that you set the equivalences between the fields of the input data and a controled vocabular defined by Pergola ontology. The format of the mapping file is the external mapping file format from the Gene Ontology Consortium, you can see an example below:
! Mapping of behavioural fields into genome browser fields
!
behavioural_file:Animal &gt; pergola:track
behavioural_file:StartT &gt; pergola:chromStart
behavioural_file:EndT &gt; pergola:chromEnd
behavioural_file:Behavior &gt; pergola:dataTypes
behavioural_file:Value &gt; pergola:dataValue
End of explanation
"""


from pergola import mapping
# load mapping file 
mapping_info = mapping.MappingInfo("../../sample_data/feeding_behavior/b2p.txt")

"""
Explanation: MappingInfo objects
Mappings between the input data and pergola ontology are loaded in MappingInfo objects:
End of explanation
"""


mapping_info.write()

"""
Explanation: To view the mappings MappingInfo objects provide the :func:pergola.mapping.Mapping.write method
End of explanation
"""


from pergola import parsers
from pergola import intervals


# load the data into an IntData object that will store the sequence of events
int_data = intervals.IntData("../../sample_data/feeding_behavior/feeding_behavior_HF_mice.csv", map_dict=mapping_info.correspondence)


"""
Explanation: MappingInfo objects are needed to load data into IntData objects as it will be explained in the lines below.
IntData objects
IntData objects load all the intervals of a file:
End of explanation
"""


#Displays first 10 tuples of data list
int_data.data[:10]

"""
Explanation: Intervals when loaded are stored in a list of tuples that can be accessed by data attribute:
End of explanation
"""


int_data.data_types

"""
Explanation: IntData objects also provide some other attributes like the set of different tracks (term for IDs in pergola ontology) contained in the data:
End of explanation
"""


int_data.min

"""
Explanation: The minimun value present in the data:
End of explanation
"""


int_data.max

"""
Explanation: The maximun value:
End of explanation
"""


int_data.tracks

"""
Explanation: The set of different tracks present in the data (term for different IDs in pergola ontology). In this case the different IDs for each mice:
End of explanation
"""


mapping_info.write()

mapping_info.correspondence['EndT']

"""
Explanation: And finally the dataTypes (term for different types of data in pergola ontology) that can be used to encode for example different behaviours:
End of explanation
"""


int_data_read = int_data.read(relative_coord=True)


int_data_read.list_tracks

int_data_read.range_values

dict_bed = int_data_read.convert(mode='bed')

#dict_bed = data_read.convert(mode='bed')
for key in dict_bed:
    print "key.......: ",key#del
    bedSingle = dict_bed [key]
    print "::::::::::::::",bedSingle.data_types

bed_12_food_sc = dict_bed[('2', 'food_sc')]

bed_12_food_sc.range_values

type(bed_12_food_sc)

bed_12_food_sc.data    

# Code to print the data inside a bed object (generator object)
#for row in bed_12_food_sc.data:
#    print row

dict_bedGraph = int_data_read.convert(mode='bedGraph')

for key in dict_bedGraph:
    print "key.......: ",key#del
    bedGraphSingle = dict_bedGraph [key]
    print "::::::::::::::",bedGraphSingle.data_types

bedG_8_food_sc = dict_bedGraph[('8', 'food_sc')]

"""
Explanation: Data conversion:
GenomicContainer is a generic class from which three subclasses derive:
Track objects
Data can be loaded into a Track objects by read function. This function allows to convert the intervals to relative values using the first time point as 0:
End of explanation
"""


bedG_8_food_sc.data

# Code to print the data inside a bed object (generator object)
#for row in bedG_8_food_sc:
#    print row

type(int_data_read)

type(int_data_read.data)

int_data_read.range_values

int_data_read.list_tracks

int_data_read.data[-10]

int_data_read.data_types

#data_read.convert(mode=write_format, tracks=sel_tracks, tracks_merge=tracks2merge, 
#                                 data_types=data_types_list, dataTypes_actions=dataTypes_act, 
#                                 window=window_size) 

mapping.write_chr (int_data_read)

# Generate a cytoband file and a bed file with phases
mapping.write_cytoband(end = int_data.max - int_data.min, delta=43200, start_phase="dark", lab_bed=False)

#data_read = intData.read(relative_coord=True, multiply_t=1)
data_read = int_data.read(relative_coord=True)

#for i in data_read.data:
#        print i

data_type_col = {'food_sc': 'orange', 'food_fat':'blue'}

bed_str = data_read.convert(mode="bed", data_types=["food_sc", "food_fat"], dataTypes_actions="all", 
                            color_restrictions=data_type_col)

for key in bed_str:
    bedSingle = bed_str[key]
    bedSingle.save_track()

"""
Explanation: Track object
End of explanation
"""


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

bedGraph_str = data_read.convert(mode="bedGraph", window=1800, data_types=["food_sc", "food_fat"], dataTypes_actions="all", color_restrictions=data_type_col_bedGraph)

for key in bedGraph_str:
    bedGraph_single = bedGraph_str[key]
    bedGraph_single.save_track()

"""
Explanation: Output data
pergola allows the conversion to several genomic formats, here we summarize some commands and operations as an example of pergola capabilities:
Bed file
track type=bed name="1_eat" description="1 eat" visibility=2 itemRgb="On" priority=20
chr1    137.0   156.0   ""  0.06    +   137.0   156.0   51,254,51
chr1    250.0   281.0   ""  0.07    +   250.0   281.0   0,254,0
chr1    311.0   333.0   ""  0.08    +   311.0   333.0   25,115,25
track type=bed name="1_eat" description="1 eat" visibility=2 itemRgb="On" priority=20
chr1    0   19  ""  0.06    +   0   19  51,254,51
chr1    113 144 ""  0.07    +   113 144 0,254,0
chr1    174 196 ""  0.08    +   174 196 25,115,25
End of explanation
"""


## Bed file showing the files (recordings)
# reading correspondence file
mapping_file_data = mapping.MappingInfo("../../sample_data/feeding_behavior/f2g.txt")

mapping_file_data.write()

# Reading file info
files_data = intervals.IntData("../../sample_data/feeding_behavior/files.csv", map_dict=mapping_file_data.correspondence)
data_file_read = files_data.read(relative_coord=True)

bed_file = data_file_read.convert(mode="bed", dataTypes_actions="all", tracks_merge=files_data.tracks)


for key in bed_file:
    bed_file_single = bed_file[key]
    bed_file_single.save_track(name_file = "files_data")

# Reading phase info
phase_data = intervals.IntData("../../sample_data/feeding_behavior/phases_exp.csv", map_dict=mapping_file_data.correspondence)
data_phase_read = phase_data.read(relative_coord=True)

bed_file = data_phase_read.convert(mode="bed", dataTypes_actions="all", tracks_merge=phase_data.tracks)

for key in bed_file:
    bed_file_single = bed_file[key]
    bed_file_single.save_track(name_file = "phase_exp")

"""
Explanation: bedGraph files
track type=bedGraph name="1_eat" description="1_eat" visibility=full color=0,254,0 altColor=25,115,25 priority=20
chr1    0   30  0.06
chr1    30  60  0
chr1    60  90  0
chr1    90  120 0.0158064516129
chr1    120 150 0.0541935483871
chr1    150 180 0.0218181818182
chr1    180 210 0.0581818181818
chr1    210 240 0
End of explanation
"""


