%%% This function creates a new file from the source file, which contains
%%% the data of the specified chromosome
function []=GenerateChrFile(ChrNum,MyData,SaNum)

fid = fopen(MyData);
C =textscan(fid, '%s %f %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s');
fclose(fid);
j=1;
%Create a new cell array
for i=1:length(C{1,1})
    if  (strcmp(C{1,1}(i), ChrNum)==1)
        C1{1,1}(j)=C{1,1}(i);       %Chromosome number
        C1{1,2}(j)=C{1,2}(i);       %position
        C1{1,3}(j)=C{1,3}(i);       %Consensus
        C1{1,4}(j)=C{1,4}(i);       %SNP
        C1{1,5}(j)=C{1,5}(i);       %SNP score
        C1{1,6}(j)=C{1,6}(i);       %Reads SNP
        C1{1,7}(j)=C{1,7}(i);       %Reads Consensus
        C1{1,8}(j)=C{1,8}(i);
        C1{1,9}(j)=C{1,9}(i);
        C1{1,10}(j)=C{1,10}(i);
        C1{1,11}(j)=C{1,11}(i);
        C1{1,12}(j)=C{1,12}(i);
        C1{1,13}(j)=C{1,13}(i);
        C1{1,14}(j)=C{1,14}(i);
        C1{1,15}(j)=C{1,15}(i);
        C1{1,16}(j)=C{1,16}(i);
        C1{1,17}(j)=C{1,17}(i);
        j=j+1;
    end
end
    
% create a new text file: "SA*chr**.txt" that contains only the specified
% chromosome data.
tail='.txt';
head='SA';
SaNumStr=num2str(SaNum);
filename =strcat(head,SaNumStr,ChrNum,tail);
fid2 = fopen(filename,'w+');

for i=1:length(C1{1,1})
    chrname=char(C1{1,1}(i));
    position(i)=C1{1,2}(i);
    consensus=char(C1{1,3}(i));
    snp=char(C1{1,4}(i));
    score=char(C1{1,5}(i));
    readssnp=char(C1{1,6}(i));
    readsconsensus=char(C1{1,7}(i));
    change=char(C1{1,8}(i));
    genename=char(C1{1,9}(i));
    novelORknown=char(C1{1,10}(i));
    codingORnoncoding=char(C1{1,11}(i));
    if ((strcmp( C1{1,6}(i), '0') ==1)) &&  ((strcmp(C1{1,7}(i), '0') ==1))
     PercentOfMutantReads='NA(%Of_mutant_reads)';
    else
       d= C1{1,6}(i); %Reads for SNP
      e=C1{1,7}(i);  %Reads for the consensus
       read1=d{1}(3);
      read2=e{1}(3); 
      %calculate the percentage of mutant reads
        if (strcmp( read1, '0')==1 ) && (strcmp(read2, '0') ==0)
         PercentOfMutantReads='0(%Of_mutant_reads)';
        else
          if (strcmp( read1, '0')==0 ) && (strcmp(read2, '0') ==1)
         PercentOfMutantReads='100(%Of_mutant_reads)';
          else
      if length(d{1})>3
      for m=4:(length(d{1})) 
          read1=strcat(read1,d{1}(m));
      end
      end
      if length(e{1})>3
      for m=4:(length(e{1}))
          read2=strcat(read2,e{1}(m));
      end
      end
        read1=str2num(read1); read2=str2num(read2);

sum=(read1+read2);
calc=(read1*100)/sum;
calc=num2str(calc);
PercentOfMutantReads=strcat(calc, '(%Of_mutant_reads)');
       end
        end
    end 
    changeinCDS=char(C1{1,12}(i));
    changeinAA=char(C1{1,13}(i));
    synonemousORnot=char(C1{1,14}(i));
    refcodonAA=char(C1{1,15}(i));
    mutcodonAA=char(C1{1,16}(i));
    changecoservedORnot=char(C1{1,17}(i));
    fprintf(fid2,'%s %d %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s \n',chrname, position(i),  consensus, snp, score, readssnp, readsconsensus,change, genename,novelORknown, PercentOfMutantReads, codingORnoncoding, changeinCDS, changeinAA, synonemousORnot, refcodonAA,  mutcodonAA, changecoservedORnot);
end
   
