function nma_microarray_main()
%function nma_microarray_main()
%
% This function download Chen Xin et all, Liver data used in the
% 2002 paper from NCBI Geo database and process it to generate
% the liver_tumor.txt and liver_normal.txt files to be used
% by other routines for the PCA and ICA processing.
%
% THis file is part of my Math 501 project, for spring 2007, CSUF
%

% geodata = geosoftread('GSM79940_normal.txt');
% geodata
% geodata = 
%                  Scope: 'SAMPLE'
%              Accession: 'GSM79940'
%                 Header: [1x1 struct]
%     ColumnDescriptions: {53x1 cell}
%            ColumnNames: {53x1 cell}
%                   Data: [24192x53 double]
% 
% geodata.ColumnNames
% ans = 
%     'ID_REF'
%     'CH1I_MEAN'
%     'CH2I_MEAN'
%     'CH1B_MEDIAN'
%     'CH2B_MEDIAN'
%     'CH1D_MEAN'
%     'CH2D_MEAN'
%     'CH1I_MEDIAN'
%     'CH2I_MEDIAN'
%     'CH1B_MEAN'
%     'CH2B_MEAN'
%     'CH1D_MEDIAN'
%     'CH2D_MEDIAN'
%     'CH1_PER_SAT'
%     'CH2_PER_SAT'
%     'CH1I_SD'
%     'CH2I_SD'
%     'CH1B_SD'
%     'CH2B_SD'
%     'PERGTBCH1I_1SD'
%     'PERGTBCH2I_1SD'
%     'PERGTBCH1I_2SD'
%     'PERGTBCH2I_2SD'
%     'SUM_MEAN'
%     'SUM_MEDIAN'
%     'RAT1_MEAN'
%     'RAT2_MEAN'
%     'RAT2_MEDIAN'
%     'PIX_RAT2_MEAN'
%     'PIX_RAT2_MEDIAN'
%     'RAT2_SD'
%     'TOT_SPIX'
%     'TOT_BPIX'
%     'REGR'
%     'CORR'
%     'DIAMETER'
%     'X_COORD'
%     'Y_COORD'
%     'TOP'
%     'BOT'
%     'LEFT'
%     'RIGHT'
%     'FLAG'
%     'CH2IN_MEAN'
%     'CH2BN_MEDIAN'
%     'CH2DN_MEAN'
%     'RAT2N_MEAN'
%     'CH2IN_MEDIAN'
%     'CH2DN_MEDIAN'
%     'RAT1N_MEAN'
%     'RAT2N_MEDIAN'
%     'VALUE'
%     'LOG_RAT2N_MEDIAN'
%     
       
% change history
% nabbasi 040407  started.
% nabbasi 050607  changed the content of liver_tumor.txt and
%                 liver_normal.txt to also include the gene name as 
%                 first column. I did not want to lose the connection.
%

DEBUG=1;
DOWNLOAD=0; %only do once as needed

if DOWNLOAD
   downloadMicroarrayData();
end

generateSimpleFilesFromDownloadedData();

end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% FIXME make modular
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function generateSimpleFilesFromDownloadedData()
     generateOneFile('GSM_names_tumor_liver.txt','tumor');
     generateOneFile('GSM_names_normal_liver.txt','normal');
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% FIXME make modular
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function generateOneFile(fname,type)
     
ID=nma_readFile(fname);

numSamples=size(ID,2);
sampleName=ID{1};
fileName=sprintf('%s_%s.txt',sampleName,type);
geodata = geosoftread(fileName);    
numGenes=size(geodata.Data,1);

A=zeros(numGenes,numSamples);
A(:,1)=geodata.

numSamples=5;

k=0;
for n=1:numSamples
     sampleName=ID{n};
     fileName=sprintf('%s_%s.txt',sampleName,type);

     geodata = geosoftread(fileName);     
     fprintf('....loaded sample [%d] to file %s\n',n,fileName); 

     if size(geodata.Data,2) ~=53
        fprintf('.... skipping file %s, bad number of columns\n',fileName);
     else
        if size(geodata.Data,1) ~= numGenes
            fprintf('.... skipping file %s, bad number of rows\n',fileName);         
        else
            k=k+1;        
            A(:,k)=geodata.Data(1:numGenes,52);                 
        end
     end     
end

B=size(k,numSamples);
B=A(:,1:k);

fname=sprintf('liver_%s.txt',type);

save(fname,'-ascii','B');

end
%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function downloadMicroarrayData()         

fname='GSM_names_tumor_liver.txt';
gsmTumorLiverID=nma_readFile(fname);

fprintf('Read %d GSM sample names from %s\n',size(gsmTumorLiverID,2),fname); 

fname='GSM_names_normal_liver.txt';
gsmNormalLiverID=nma_readFile(fname);
if err==-1
    return;
end
fprintf('Read %d GSM sample names from %s\n',size(gsmNormalLiverID,2),fname); 

downloadDataFromGEO(gsmTumorLiverID,'tumor');
downloadDataFromGEO(gsmNormalLiverID,'normal');
end


%%%%%%%%%%%%%%%%%%%%
%
%
%
%%%%%%%%%%%%%%%%%%%%%%
function downloadDataFromGEO(gsmID,type)

numSamples=size(gsmID,2);

for n=1:numSamples
     sampleName=gsmID{n};
     fileName=sprintf('%s_%s.txt',sampleName,type);
     getgeodata(sampleName,'ToFile',fileName);
     fprintf('....downloaded sample [%d] to file %s\n',n,fileName); 
end

end
