%%%Main GUI
function varargout = GenomeGems(varargin)
% GENOMEGEMS M-file for GenomeGems.fig
%      GENOMEGEMS, by itself, creates a new GENOMEGEMS or raises the existing
%      singleton*.
%
%      H = GENOMEGEMS returns the handle to a new GENOMEGEMS or the handle to
%      the existing singleton*.
%
%      GENOMEGEMS('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in GENOMEGEMS.M with the given input arguments.
%
%      GENOMEGEMS('Property','Value',...) creates a new GENOMEGEMS or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before GenomeGems_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to GenomeGems_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 GenomeGems

% Last Modified by GUIDE v2.5 26-May-2011 11:34:14

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @GenomeGems_OpeningFcn, ...
                   'gui_OutputFcn',  @GenomeGems_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 GenomeGems is made visible.
function GenomeGems_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 GenomeGems (see VARARGIN)

% Choose default command line output for GenomeGems
handles.output = hObject;
setappdata(0,'hGenomeGems',gcf);
% UIWAIT makes GenomeGems wait for user response (see UIRESUME)
% uiwait(handles.figure1);
handles.Files={0,'chr0','NorCA','Loc'};
handles.GlobalCells={0,'chr0','NorCA','Loc'};
% Update handles structure
guidata(hObject, handles);

axes1_CreateFcn;


% --- Executes during object creation, after setting all properties.
function axes1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to axes1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
imshow('Title_Banner.jpg')
axis image

% Hint: place code in OpeningFcn to populate axes1

% --- Outputs from this function are returned to the command line.
function varargout = GenomeGems_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;


% --- Executes on button press in GeneratePgSNPButton.
function GeneratePgSNPButton_Callback(hObject, eventdata, handles)
% hObject    handle to GeneratePgSNPButton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%get the current list of file names from the listbox
PgSNP


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


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


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


% --- Executes on button press in AddFilesButton.
function AddFilesButton_Callback(hObject, eventdata, handles)
% hObject    handle to AddFilesButton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[input_file,pathname] = uigetfile( ...
       {'*.txt', 'Text (*.txt)'; ...
        '*.*', 'All Files (*.*)'}, ...
        'Select files', ... 
        'MultiSelect', 'on');
    %if file selection is cancelled, pathname should be zero
%and nothing should happen
if pathname == 0
    return
end
 
%gets the current data file names inside the listbox
inputFileNames = get(handles.inputFiles_listbox,'String');
 
%if they only select one file, then the data will not be a cell
%if more than one file selected at once,
%then the data is stored inside a cell
if iscell(input_file) == 0
 
    %add the most recent data file selected to the cell containing
    %all the data file names
    inputFileNames{end+1} = fullfile(pathname,input_file);
 
%else, data will be in cell format
else
    %stores full file path into inputFileNames
    for n = 1:length(input_file)
        %notice the use of {}, because we are dealing with a cell here!
        inputFileNames{end+1} = fullfile(pathname,input_file{n});
    end
end
 
%updates the gui to display all filenames in the listbox
set(handles.inputFiles_listbox,'String',inputFileNames);
 
%make sure first file is always selected so it doesn't go out of range
%the GUI will break if this value is out of range
set(handles.inputFiles_listbox,'Value',1);
 
% Update handles structure
guidata(hObject, handles)

% --- Executes on button press in DeleteFileButton.
function DeleteFileButton_Callback(hObject, eventdata, handles)
% hObject    handle to DeleteFileButton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%get the current list of file names from the listbox
inputFileNames = get(handles.inputFiles_listbox,'String');
 
%get the values for the selected file names
option = get(handles.inputFiles_listbox,'Value');
 
%is there is nothing to delete, nothing happens
if (isempty(option) == 1 || option(1) == 0 || isempty(inputFileNames))
    return
end
 
%erases the contents of highlighted item in data array
inputFileNames(option) = [];
 
%updates the gui, erasing the selected item from the listbox
set(handles.inputFiles_listbox,'String',inputFileNames);
 
%moves the highlighted item to an appropiate value or else will get error
if option(end) > length(inputFileNames)
    set(handles.inputFiles_listbox,'Value',length(inputFileNames));
end
 
% Update handles structure
guidata(hObject, handles);

% --- Executes on selection change in inputFiles_listbox.
function inputFiles_listbox_Callback(hObject, eventdata, handles)
% hObject    handle to inputFiles_listbox (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 inputFiles_listbox contents as cell array
%        contents{get(hObject,'Value')} returns selected item from inputFiles_listbox


% --- Executes during object creation, after setting all properties.
function inputFiles_listbox_CreateFcn(hObject, eventdata, handles)
% hObject    handle to inputFiles_listbox (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



function SaNum_Callback(hObject, eventdata, handles)
% hObject    handle to SaNum (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.SaNum=get(hObject,'String');  
guidata(hObject,handles) % Save the handles structure after adding data
% Hints: get(hObject,'String') returns contents of SaNum as text
%        str2double(get(hObject,'String')) returns contents of SaNum as a double


% --- Executes during object creation, after setting all properties.
function SaNum_CreateFcn(hObject, eventdata, handles)
% hObject    handle to SaNum (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 selection change in chromosomename.
function chromosomename_Callback(hObject, eventdata, handles)
% hObject    handle to chromosomename (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 chromosomename contents as cell array
%        contents{get(hObject,'Value')} returns selected item from chromosomename
val = get(hObject,'Value');

switch val
case 2 % User selected chr1
handles.chr = 'chr1';
case 3 % User selected chr2
handles.chr = 'chr2';
case 4 % User selected chr3
handles.chr= 'chr3';
case 5 % User selected chr4
handles.chr = 'chr4';
case 6 % User selected chr5
handles.chr = 'chr5';
case 7 % User selected chr6
handles.chr = 'chr6';
case 8 % User selected chr7
handles.chr = 'chr7';
case 9 % User selected chr8
handles.chr = 'chr8';
case 10 % User selected chr9
handles.chr = 'chr9';
case 11 % User selected chr10
handles.chr = 'chr10';
case 12 % User selected chr11
handles.chr = 'chr11';
case 13 % User selected chr12
handles.chr = 'chr12';
case 14 % User selected chr13
handles.chr = 'chr13';
case 15 % User selected chr14
handles.chr = 'chr14';
case 16 % User selected chr15
handles.chr = 'chr15';
case 17 % User selected chr16
handles.chr = 'chr16';
case 18 % User selected chr17
handles.chr = 'chr17';
case 19 % User selected chr18
handles.chr = 'chr18';
case 20 % User selected chr19
handles.chr = 'chr19';
case 21 % User selected chr20
handles.chr = 'chr20';
case 22 % User selected chr21
handles.chr = 'chr21';
case 23 % User selected chr22
handles.chr = 'chr22';
case 24 % User selected chr23
handles.chr = 'chrX';
end
guidata(hObject,handles) % Save the handles structure after adding data

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

% Hint: popupmenu 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 AddDataButton.
function AddDataButton_Callback(hObject, eventdata, handles)
% hObject    handle to AddDataButton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
hactive=handles.SelectFilesTable;
%%Error Message - Missing Information - Chromosome Number
if isfield(handles,'chr')==0
     h = msgbox('Please make sure you have entered all of the relevant infomation. For more information see our manual page at http://www.tau.ac.il/~nshomron/GenomeGems/Manual.html','Missing Information','error')
end
z = handles.chr; % Load data from handles structure
%get the current list of file names from the listbox
inputFileNames = get(handles.inputFiles_listbox,'String');
input_file=inputFileNames{length(inputFileNames)};
SamNum=handles.SaNum;
%%Error Message - Missing Information - Novel Or Associated
if isfield(handles,'NovelOrAssociated')==0
     h = msgbox('Please make sure you have entered all of the relevant infomation. For more information see our manual page at http://www.tau.ac.il/~nshomron/GenomeGems/Manual.html','Missing Information','error')
end
NorCA=handles.NovelOrAssociated;
NewFile={SamNum,z,NorCA,input_file};
Files=handles.Files;
NumOfFiles=size(Files);
if Files{1,1}==0;
    NextFile=NumOfFiles(1);
else
    NextFile=NumOfFiles(1)+1;
end
Files{NextFile,1}=SamNum;
%%Error Message - Missing Information - Sample Number
type=class(SamNum)
if strcmp(type,'double')==1
     h = msgbox('Please make sure you have entered all of the relevant infomation. For more information see our manual page at http://www.tau.ac.il/~nshomron/GenomeGems/Manual.html','Missing Information','error')
end
if isempty(SamNum)==1
     h = msgbox('Please make sure you have entered all of the relevant infomation. For more information see our manual page at http://www.tau.ac.il/~nshomron/GenomeGems/Manual.html','Missing Information','error')
end
Files{NextFile,2}=z;
Files{NextFile,3}=NorCA;
Files{NextFile,4}=input_file;
handles.Files=Files;
set(handles.SelectFilesTable, 'Data',handles.Files);
guidata(hObject,handles);

%%Save File List as Global List
hGenomeGems=getappdata(0,'hGenomeGems')
setappdata(hGenomeGems,'Files',handles.Files);



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


% --- Executes when selected cell(s) is changed in SelectFilesTable.
function SelectFilesTable_CellSelectionCallback(hObject, eventdata, handles)
% hObject    handle to SelectFilesTable (see GCBO)
% eventdata  structure with the following fields (see UITABLE)
%	Indices: row and column indices of the cell(s) currently selecteds
% handles    structure with handles and user data (see GUIDATA)
index=eventdata.Indices;
handles.cell = index(1);
guidata(hObject, handles);


% --- Executes on selection change in NovelOrCa.
function NovelOrCa_Callback(hObject, eventdata, handles)
% hObject    handle to NovelOrCa (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 NovelOrCa contents as cell array
%        contents{get(hObject,'Value')} returns selected item from NovelOrCa
val = get(hObject,'Value');
switch val
case 2 % User selected Novel
handles.NovelOrAssociated = 'Novel';
case 3 % User selected Clinically Associated
handles.NovelOrAssociated = 'Clinically Associated';
end
guidata(hObject,handles) % Save the handles structure after adding data

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

% Hint: popupmenu 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 during object creation, after setting all properties.
function axes2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to axes2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
imshow('DataTableGem.jpg');

% Hint: place code in OpeningFcn to populate axes2


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

% Hint: place code in OpeningFcn to populate axes3


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


% Hint: place code in OpeningFcn to populate axes4


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

% Hint: place code in OpeningFcn to populate axes5

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


% --- Executes during object creation, after setting all properties.
function axes6_CreateFcn(hObject, eventdata, handles)
% hObject    handle to axes6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
imshow('AddInfoGem.jpg')
axis image


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