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

% Last Modified by GUIDE v2.5 22-May-2011 21:52:36

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

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

% Update handles structure
guidata(hObject, handles);

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


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

if handles.GlobalCells~=0
% INSTANTIATE THE CONTROL
f=handles.cells;
Spreadsheet1 = handles.activex1;

% FIND THE ACTIVE SHEET
ActSheet = get(Spreadsheet1,'ActiveSheet');
% CLEAR CONTENTS
ActSheet.Cells.ClearContents;

% FIND THE ACTIVE CELL, ROW AND COLUMN
ActCell = get(Spreadsheet1,'ActiveCell');
Select(Range(ActSheet,'A1'));
ActCellRow = get(ActCell,'Row');
ActCellColumn = get(ActCell,'Column');

for i=1:length(f)
    sanum(i)=str2num(handles.Files{f(i),1});
    path{i}=handles.Files{f(i),4};
end
TotalMatrix=SampCompare(sanum,handles.Files{f(1),2},handles.Files{f(1),3},path);


for i=1:length(TotalMatrix(:,1)) 
   for j=1:length(TotalMatrix(1,:))
    XLfmt = nn2an(ActCellRow+i-1,ActCellColumn+j-1);
    Select(Range(ActSheet,XLfmt));
    ActCell = get(Spreadsheet1,'ActiveCell');
    set(ActCell,'Value',TotalMatrix(i,j));
   end
end

Select(Range(ActSheet,'A1'));
handles.GlobalCells=0;
guidata(hObject, handles);
hGenomeGems=getappdata(0,'hGenomeGems');
setappdata(hGenomeGems,'GlobalCells',handles.GlobalCells);
% Get default command line output from handles structure
varargout{1} = handles.output;
end


% --- Executes on button press in ShowCommonSNPsButton.
function ShowCommonSNPsButton_Callback(hObject, eventdata, handles)
% hObject    handle to ShowCommonSNPsButton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
f=handles.cells;
% INSTANTIATE THE CONTROL
Spreadsheet1=handles.activex1;

% FIND THE ACTIVE SHEET
ActSheet = get(Spreadsheet1,'ActiveSheet');
% CLEAR CONTENTS
ActSheet.Cells.ClearContents;

% FIND THE ACTIVE CELL, ROW AND COLUMN
ActCell = get(Spreadsheet1,'ActiveCell');
Select(Range(ActSheet,'A1'));
ActCellRow = get(ActCell,'Row');
ActCellColumn = get(ActCell,'Column');

for i=1:length(f)
    sanum(i)=str2num(handles.Files{f(i),1});
    path{i}=handles.Files{f(i),4};
end
TotalMatrix=SampCompare(sanum,handles.Files{f(1),2},handles.Files{f(1),3},path);


for i=1:length(TotalMatrix(:,1)) 
   for j=1:length(TotalMatrix(1,:))
    XLfmt = nn2an(ActCellRow+i-1,ActCellColumn+j-1);
    Select(Range(ActSheet,XLfmt));
    ActCell = get(Spreadsheet1,'ActiveCell');
    set(ActCell,'Value',TotalMatrix(i,j));
   end
end

Select(Range(ActSheet,'A1'));

function cr = nn2an(r,c)
% Thanks Brett Shoelson

t = [floor((c - 1)/26) + 64 rem(c - 1, 26) + 65];
if(t(1)<65), t(1) = []; end
cr = [char(t) num2str(r)];

% --- 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
hGenomeGems=getappdata(0,'hGenomeGems');
handles.Files=getappdata(hGenomeGems,'Files');
handles.GlobalCells=getappdata(hGenomeGems,'GlobalCells');
guidata(hObject, handles);
handles.SelectFilesTable=hObject;
set(handles.SelectFilesTable, 'Data',handles.Files);
if handles.GlobalCells ~=0
    SelectFilesTable_CellSelectionCallback(hObject,eventdata,handles);
end

% --- 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)

if handles.GlobalCells~=0
    eventdata.Indices=handles.GlobalCells;
end
index=eventdata.Indices;
handles.cells = index;
guidata(hObject, handles);

% --- 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('SNPViewTitle.jpg');
axis image

% Hint: place code in OpeningFcn to populate axes2

% --- Executes on button press in GoToSampleCompare.
function GoToSampleCompare_Callback(hObject, eventdata, handles)
% hObject    handle to GoToSampleCompare (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 BackToMain.
function BackToMain_Callback(hObject, eventdata, handles)
% hObject    handle to BackToMain (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
GenomeGems
