function varargout = CO(varargin)
% CO M-file for CO.fig
%      CO, by itself, creates a new CO or raises the existing
%      singleton*.
%
%      H = CO returns the handle to a new CO or the handle to
%      the existing singleton*.
%
%      CO('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in CO.M with the given input arguments.
%
%      CO('Property','Value',...) creates a new CO or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before CO_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to CO_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help CO

% Last Modified by GUIDE v2.5 02-Jan-2013 21:02:12

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @CO_OpeningFcn, ...
                   'gui_OutputFcn',  @CO_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before CO is made visible.
function CO_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to CO (see VARARGIN)

path(path,genpath(pwd));

% DEFAULT PARAMETERS
handles.K = 3;
handles.min_rep = 5;
handles.LIMIT = Inf;
handles.save_mode = 0;


% Choose default command line output for CO
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes CO wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = CO_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;
%varargout{1} = handles;


% --------------------------------------------------------------------
function File_Callback(hObject, eventdata, handles)
% hObject    handle to File (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[filename, pathname] = uigetfile('*.fasta', 'Select Fasta File');
% If "Cancel" is selected then return
if isequal([filename,pathname],[0,0])
    return
% Otherwise construct the fullfilename to load the file.
else
    handles.FASTA = fullfile(pathname,filename);
    [handles.seq handles.info ] = read_fasta(handles.FASTA);
    guidata(hObject,handles)
end


function K_val_Callback(hObject, eventdata, handles)
% hObject    handle to K_val (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of K_val as text
%        str2double(get(hObject,'String')) returns contents of K_val as a double
handles.K = str2double(get(hObject,'String'));
guidata(hObject,handles);


% --- Executes during object creation, after setting all properties.
function K_val_CreateFcn(hObject, eventdata, handles)
% hObject    handle to K_val (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


function rep_val_Callback(hObject, eventdata, handles)
% hObject    handle to rep_val (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of rep_val as text
%        str2double(get(hObject,'String')) returns contents of rep_val as a double
handles.min_rep = str2double(get(hObject,'String'));
guidata(hObject,handles);

% --- Executes during object creation, after setting all properties.
function rep_val_CreateFcn(hObject, eventdata, handles)
% hObject    handle to rep_val (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


function Limit_val_Callback(hObject, eventdata, handles)
% hObject    handle to Limit_val (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of Limit_val as text
%        str2double(get(hObject,'String')) returns contents of Limit_val as a double

handles.LIMIT = str2double(get(hObject,'String'));
guidata(hObject,handles);

% --- Executes during object creation, after setting all properties.
function Limit_val_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Limit_val (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in RUN_ALGO.
function RUN_ALGO_Callback(hObject, eventdata, handles)
% hObject    handle to RUN_ALGO (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

[OUT RES] = Main(handles.seq, handles.info, handles.min_rep, handles.LIMIT);

% Print Results to TEXT file
printOUT(OUT, 'OUT.txt');

axes(handles.Intervals) % Select the proper axes
% PLOTs
[a b] = hist(RES.all_intervals,unique(RES.all_intervals));
loglog(b, a, 'ko');
% clean zeros
tmp = find(a==0); a(tmp) = []; b(tmp) = [];
%for the ranked distribution:
[a_ord period_id] = sort(a,'descend');
hold; 
loglog(b, a_ord, 'r.');
xlabel('Intervals|Ranks');
ylabel('# of occurences');
%RES.Proteomic_Periods = round(b(period_id(1:2)));
RES.Proteomic_Periods = round(b(period_id));
RES.Proteomic_Periods_probability = a_ord./sum(a_ord);


resStr = get(handles.KeyRes,'String');
resStr1 = {['# of proteins:      ', '   ', num2str(RES.Nco + RES.Nno)]};
resStr2 = {['# of CO proteins:   ', '   ', num2str(RES.Nco)]};
resStr3 = {['CO fraction [%]:    ', '   ', num2str(100*RES.Nco/(RES.Nco+RES.Nno))]};
resStr4 = {['# of DFTs:          ', '   ', num2str(length(RES.CO_DFTs))]};
resStr5 = {['2 Most Leading Proteomic Periods:    ', '   ', num2str(RES.Proteomic_Periods(1:2))]};
resStr = [resStr1; resStr2; resStr3; resStr4; resStr5];
set(handles.KeyRes,'String',resStr);

% Save Results as MAT file
if handles.save_mode == 1
    RES.OUT = OUT;
    save 'RES.mat' RES
end


% --- Executes during object creation, after setting all properties.
function Intervals_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Intervals (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: place code in OpeningFcn to populate Intervals


% --- Executes on selection change in KeyRes.
function KeyRes_Callback(hObject, eventdata, handles)
% hObject    handle to KeyRes (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns KeyRes contents as cell array
%        contents{get(hObject,'Value')} returns selected item from KeyRes


% --- Executes during object creation, after setting all properties.
function KeyRes_CreateFcn(hObject, eventdata, handles)
% hObject    handle to KeyRes (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: listbox controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in radio1.
function radio1_Callback(hObject, eventdata, handles)
% hObject    handle to radio1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of radio1
handles.save_mode = 1;
guidata(hObject,handles);



