Sunday, 12 April 2020

Encode and Decode

Hi All

function pushbutton17_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton17 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[filename pathname]=uigetfile({'*.txt'},'file selctor');
fullpathname=strcat(pathname,filename);
text=fileread(fullpathname);
ASCII = double(text)
A=dec2bin(ASCII,8)

B= logical(A - 48)

 m=size(B,1)
 n=size(B,2)

p='A'
q='C'
r='G'
S= 'T'

k=1
 x=.4;
h=waitbar(x,'Process start');
for i=1:m
for j=1:2:n
if (B(i,j)==0 && B(i,j+1)==1)
sc(k)=p
elseif (B(i,j)==1 && B(i,j+1)==0)
sc(k)=q
elseif (B(i,j)==0 && B(i,j+1)==0)
sc(k)=r
else
sc(k)=S
end
k=k+1
end
end
%p53nt = fastaread('p53nt.txt')
L = get(handles.text50,'String');
j=1
for i=1:4:length(sc)
k=strfind(L,sc(i:i+3))

code(j)=k(randi([1 length(k)]));
j=j+1;
end


fileID = fopen('Ciphertext.txt','w');
for (i=1:length(code))
fprintf(fileID,'%d ',code(i))
end
rawdata1=load(['Ciphertext.txt']);
fclose(fileID)
pause(3)
waitbar(x+.6, h, 'Completed');

% --- Executes on button press in pushbutton18.
function pushbutton18_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton18 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
x=.4;
h=waitbar(x,'Process start');
fileID = fopen('Ciphertext.txt','r');
fileID2 = fopen('Plaintext.txt','w');

a=fscanf(fileID,'%d');
L = get(handles.text50,'String');
seq=L
%p53nt = fastaread('p53nt.txt')
%seq=p53nt.Sequence;
for i=1:1:length(a)
    seq4='';
    %Generate 4 DNA charaters for a number in file
    if(seq(a(i))=='A')
        seq4=strcat(seq4,'01');
    elseif(seq(a(i))=='G')
        seq4=strcat(seq4,'00');
    elseif(seq(a(i))=='C')
        seq4=strcat(seq4,'10');
    elseif(seq(a(i))=='T')
        seq4=strcat(seq4,'11');
    end
       
    if(seq(a(i)+1)=='A')
        seq4=strcat(seq4,'01');
    elseif(seq(a(i)+1)=='G')
        seq4=strcat(seq4,'00');
    elseif(seq(a(i)+1)=='C')
        seq4=strcat(seq4,'10');
    elseif(seq(a(i)+1)=='T')
        seq4=strcat(seq4,'11');
    end
   
    if(seq(a(i)+2)=='A')
        seq4=strcat(seq4,'01');
    elseif(seq(a(i)+2)=='G')
        seq4=strcat(seq4,'00');
    elseif(seq(a(i)+2)=='C')
        seq4=strcat(seq4,'10');
    elseif(seq(a(i)+2)=='T')
        seq4=strcat(seq4,'11');
    end
   
    if(seq(a(i)+3)=='A')
        seq4=strcat(seq4,'01');
    elseif(seq(a(i)+3)=='G')
        seq4=strcat(seq4,'00');
    elseif(seq(a(i)+3)=='C')
        seq4=strcat(seq4,'10');
    elseif(seq(a(i)+3)=='T')
        seq4=strcat(seq4,'11');
    end   
%     seq4=strcat(seq4,seq(a(i)));
%     seq4=strcat(seq4,seq(a(i)+1));
%     seq4=strcat(seq4,seq(a(i)+2));
%     seq4=strcat(seq4,seq(a(i)+3));
    disp(seq4);
    d1=bin2dec(seq4);
    c1=char(d1);
    fprintf(fileID2,'%s ',c1);
end
fclose(fileID);
fclose(fileID2);
pause(3)
waitbar(x+.6, h, 'Completed');