# Importing pergola modules used
import sys
# We need to set the path to run this notebook directly from ipython notebook
my_path_to_modules = "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/"
sys.path.append(my_path_to_modules)
from pergola import jaaba_parsers

input_jaaba_file="../../sample_data/jaaba_example/scores_chase.mat"


"""
Explanation: .. contents::    :depth: 3
Using Pergola with JAABA data
The Janelia Automatic Animal Behavior Annotator (JAABA)* is a machine learning-based system that enables researchers to automatically anotate animal behaviors. 
Pergola can import score files generated by JAABA in matlab format as show in this notebook:
*Kabra, M., Robie, A. A., Rivera-Alba, M., Branson, S., & Branson, K. (2013). JAABA: interactive machine learning for automatic annotation of animal behavior. Nat Meth, 10(1), 64–67. doi:10.1038/nmeth.2281
Path to matlab file containing scores of Drosophila chasing behavior generated using JAABA (sample_data folder):
End of explanation
"""


path_csv="../../test"
jaaba_parsers.jaaba_scores_to_csv(input_file=input_jaaba_file, path_w=path_csv, norm=True, data_type="chase")

"""
Explanation: Data can be easily export to a csv file.
Note that we can normalize the scores using "norm" flag so they move within a range of -1 to 1.
End of explanation
"""


map_file_jaaba = "../../sample_data/jaaba_example/jaaba2pergola.txt"
int_data_jaaba = jaaba_parsers.jaaba_scores_to_intData(input_file=input_jaaba_file, map_jaaba = map_file_jaaba, norm=True, data_type="chase")

"""
Explanation: We can also load the data into an IntData object.
For this we can use the file we have generated above but the function jaaba_scores_to_intData performs the process without this intermediate steps. We need to set the equivalences between the jaaba scores fields and the pergola ontology using a file that contains the mappings.
This function also allows for the normalization of the scores using "norm" flag and for the annotation of the behavior using data_type parameter.
End of explanation
"""


print int_data_jaaba.data_types
print int_data_jaaba.min
print int_data_jaaba.max
print int_data_jaaba.tracks
print int_data_jaaba.data[:10]

"""
Explanation: IntData objects
IntData, contains all the intervals of a file and some interesting related attributes like the dataTypes it contains, min and max values, the tracks (in this case individuals) and the data itself:
End of explanation
"""


#reading the data with your desired options
read_data_jaaba = int_data_jaaba.read(relative_coord=True, fields2rel=None, multiply_t=1)
#You can set several functions when tranforming the data
#data_types select the data_types you want to transform
#dataTypes_actions in the case of having multiple dataTypes you can join them in the same track ("all") or keep them separated.
#data_type_col sets the colors to display by the genome browser when bed file is loaded for each dataTypes
data_type_col={'chase': 'blue'}
#range_color sets the range of data you want to be the maximun and lower maximun intensity
range_color=[-1, 1]

bed_jaaba = read_data_jaaba.convert(mode="bed", data_types=["chase"], dataTypes_actions="all", 
                                 color_restrictions=data_type_col, range_color=range_color)

for key in bed_jaaba:
    bedSingle = bed_jaaba[key]
    bedSingle.save_track(path="/Users/jespinosa/git/pergola/test")

"""
Explanation: Basic data outputs for visualization or analysis
IntData to bed
Data can be transformed to bed files. Bed files contain just the raw intervals and values without any further manipulation of the data:
End of explanation
"""


