    clc;
    clear;
    close all;
      
load ALL_AML_data.txt;             %%%%%%%%%%%%%dataset1
 v=ALL_AML_data;
%  load BrainTumor.txt;
% v=BrainTumor(:,1:34);
%  load Medulloblastoma_data.txt;  %%%%%%%%%%%%%%%%%%%dataset2
%  v=Medulloblastoma_data;
%  load
%  'Dataset_A_multiple_tumor_samples.txt';%%%%%%%%%%%%%%dataset3                                                          
%  v=Dataset_A_multiple_tumor_samples;
 mix=v';
[n,p]=size(mix);   %%n is the number of samples,and p is the number of genes
Snumber=n;      
 z=6;                     %%%%%%%%%%%%%%%% z is the number of ICs
genenumber=1000;            %%%%%%%%%%the number of selected genes 
sindex=zeros(10,genenumber);
for c=1:10   %%%%%%%%%% c is the loop number of select genes using fastica   
[icas] = fastica(mix,'numOfIC', z);
   
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   if z==1
   gl=abs(icas);
   else
   gl=max(abs(icas));
   end                 %%gl is the maximum one of the absolute
                       %%score on each component Sj
   [sortedgene,index]=sort(gl); %%the p genes are sorted in increasing
                                 %%order according to the maximum absolute 
                                 %%scores and for each gene the rank r(l)
                                 %%is computed
 
   rankg=index;
  
   mm=[1 10 13 16 19 24 28 33 38 42 ...
       48 55 60 65 70 80 90 100 120 280 ...
       300 350 380 400 450 500 600 700 800  900 ...
       1000 1200 1400  1500 2000  2500 2800 3000 3500 5000];
   selectgene=zeros(40,5000);
   for i=1:40
       m=mm(i);
       for j=1:m
       selectgene(i,j)=rankg(p-m+j);
       end
   end
   
   for i=31 
     pp=mm(i);    %%%%%the number of selected genes,the mm(i)should equal to genenumber   
      selectindex=zeros(1,pp);
      for j=1:pp
          selectindex(j)=selectgene(i,j);
      end
      selectindex=sort(selectindex);
      selectedmatrix=zeros(Snumber,pp);
      for ii=1:Snumber
      for jj=1:pp
          selectedmatrix(ii,jj)=mix(ii,selectindex(jj));
      end
      end
   end
   genenumber=mm(i);
    for cc=1:genenumber
        sindex(c,cc)=selectindex(cc);
   end
    

end    
    
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

nn=c*genenumber;  %%
b=zeros(nn,2);
for kk=1:nn
    counter=0;
  for i1=1:c
      for j1=1:genenumber
          if sindex(i1,j1)~=0 
           m1=sindex(i1,j1);
           break;
          end
      end
 end
           for i2=1:c
             for j2=1:genenumber
              if sindex(i2,j2)==m1
                  counter=counter+1;
                 sindex(i2,j2)=0;
              end
             end
           end
         b(kk,1)=m1;
         b(kk,2)=counter;
end
m2=0;
for i3=1:nn
         if b(i3,2)~=0
          m2=m2+1;
       
         end
end
for i4=1:m2
    for j4=1:2
    c1(i4,j4)=b(i4,j4);
    end
end
d=sortrows(c1,-2);
msgn=zeros(1,genenumber);
for i=1:genenumber
    msgn(i)=d(i,1);
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

  p1=genenumber;
   selectindex1=zeros(1,p1);
      for j5=1:p1
          selectindex1(j5)=msgn(j5);
      end
      selectindex1=sort(selectindex1);
      selectedmatrix1=zeros(Snumber,p1);
      for i6=1:Snumber
      for j6=1:p1
          selectedmatrix1(i6,j6)=mix(i6,selectindex1(j6));
      end
      end
      selmatrix1=selectedmatrix1';
      save Selected_data selmatrix1; %%%selmatrix1 is the data matrix of selected genes
     
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

