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

% Last Modified by GUIDE v2.5 15-May-2011 22:00:47

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

% Choose default command line output for DataTable
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);



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


% --- Outputs from this function are returned to the command line.
function varargout = DataTable_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 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');
guidata(hObject, handles);
handles.SelectFilesTable=hObject;
set(handles.SelectFilesTable, 'Data',handles.Files);


% --- Executes on button press in ShowFile.
function ShowFile_Callback(hObject, eventdata, handles)
% hObject    handle to ShowFile (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%%Error - if file was not selected
if isfield(handles,'cell')==0
     h = msgbox('Please select a file. For more information see our manual page at http://www.tau.ac.il/~nshomron/GenomeGems/Manual.html','Missing Information','error')
end
f=handles.cell;
% INSTANTIATE THE CONTROL
Spreadsheet1=handles.activex1;

% FIND THE ACTIVE SHEET
ActSheet = get(Spreadsheet1,'ActiveSheet');
SheetName=strcat('SA', handles.Files{f,1},handles.Files{f,2},handles.Files{f,3} );
set(ActSheet,'Name',SheetName)

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

%Get the original input file for later display
inputFileNames = (handles.Files{f,4});
%Create single-chromosome file for display
GenerateChrFile(handles.Files{f,2},handles.Files{f,4},handles.Files{f,1});
%Get the single-chromosome file (created earlier) for display
tail='.txt';
head='SA';
chrz = handles.Files{f,2};
SamNum=handles.Files{f,1};
SaNumStr=num2str(SamNum);
filename=strcat(head,SaNumStr,chrz,tail);
fid = fopen(filename);
DispZ =textscan(fid, '%s %f %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s');
fclose(fid);
for i=1:length(DispZ{1,1}) 
   for j=1:length(DispZ)
    XLfmt = nn2an(ActCellRow+i-1,ActCellColumn+j-1);
    Select(Range(ActSheet,XLfmt));
    ActCell = get(Spreadsheet1,'ActiveCell');
    set(ActCell,'Value',DispZ{1,j}(i));
   end
end
a=hObject;

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)];


% --------------------------------------------------------------------
function activex1_Initialize(hObject, eventdata, handles)
% hObject    handle to activex1 (see GCBO)
% eventdata  structure with parameters passed to COM event listener
% handles    structure with handles and user data (see GUIDATA)


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


% --- Executes during object creation, after setting all properties.
function popupmenu1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu1 (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 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)
% Choose default command line output for DataTable
index=eventdata.Indices;
handles.cell = index(1);
% Update handles structure
guidata(hObject, handles);


% --------------------------------------------------------------------
function activex1_SheetActivate(hObject, eventdata, handles)
% hObject    handle to activex1 (see GCBO)
% eventdata  structure with parameters passed to COM event listener
% handles    structure with handles and user data (see GUIDATA)
handles.worksheet=handles.activex1;
% FIND THE ACTIVE SHEET
handles.activesheet = get(handles.worksheet,'ActiveSheet');
guidata(hObject,handles);


% --- Executes when entered data in editable cell(s) in SelectFilesTable.
function SelectFilesTable_CellEditCallback(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) edited
%	PreviousData: previous data for the cell(s) edited
%	EditData: string(s) entered by the user
%	NewData: EditData or its converted form set on the Data property. Empty if Data was not changed
%	Error: error string when failed to convert EditData to appropriate value for Data
% handles    structure with handles and user data (see GUIDATA)


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

hactive=handles.activex1;
% INSTANTIATE THE CONTROL
Spreadsheet1=hactive;
ActSheet = get(Spreadsheet1,'ActiveSheet');
% CLEAR CONTENTS
ActSheet.Cells.ClearContents;
Select(Range(ActSheet,'A1'));


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

% Hint: place code in OpeningFcn to populate axes1


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