Find SIFT features matching between two video - arrays

I extracted SIFT features from two videos to make a matching.
I need to compare each of the second video features with those stored in an array of features of the first video. I'm having trouble set the code so that when I have the correspondence I can get the frame in which the feature is present. How can I do? Can anyone show me a code example?
This is my code:
obj = VideoReader('video2.avi');
lastFrame = read(obj, inf);
numFrames = obj.NumberOfFrames;
%estrazione frame e sift
for k = 1 : 3 % numFrames / 5
disp(['Processing frame #', num2str(k)]);
this_frame = read(obj, k * 5); % leggi solo un fotogramma ogni 5 per velocizzarle la cosa
this_frame = imresize(this_frame, 0.5); % rimpiccioliamolo per questioni di efficienza!
I = single(rgb2gray(this_frame)) ;
[f,d] = vl_sift(I); % estrazione feature
features{k} = f; % salviamo le feautre e i relativi descrittori in delle celle
descriptors{k} = d;
end
save('feature_input', 'features');
save('descrittori_input', 'descriptors');
%%% un esempio di come ripescare i dati...
pippo = load('feature_input');
newfeat = pippo.features;
pippo = load('descrittori_input');
newdesc = pippo.descriptors;
for k = 1 : 3
disp(['Le feature del fotogramma', num2str(k), ' sono: ']);
f = cell2mat( newfeat(k) );
f(:, 1:10) % ne mostriamo solo un pezzetto... le posizioni delle prime 10 features
end
obj2 = VideoReader('video2u.avi');
lastFrame = read(obj2, inf);
numFrames = obj2.NumberOfFrames;
%estrazione frame e sift video2
for k2 = 1 : 3 % numFrames / 5
disp(['Processing frame #', num2str(k2)]);
this_frame2 = read(obj2, k2 * 5); % leggi solo un fotogramma ogni 5 per velocizzarle la cosa
this_frame2 = imresize(this_frame2, 0.5); % rimpiccioliamolo per questioni di efficienza!
K = single(rgb2gray(this_frame2)) ;
[f2,d2] = vl_sift(K); % estrazione feature
features2{k2} = f2; % salviamo le feautre e i relativi descrittori in delle celle
descriptors2{k2} = d2;
end
save('feature2_input', 'features2');
save('descrittori2_input', 'descriptors2');
%%% un esempio di come ripescare i dati...
pippo2 = load('feature2_input');
newfeat2 = pippo2.features2;
pippo2 = load('descrittori2_input');
newdesc2 = pippo2.descriptors2;
for k2 = 1 : 3
disp(['Le feature del fotogramma', num2str(k2), ' sono: ']);
f2 = cell2mat( newfeat2(k2) );
f2(:, 1:10) % ne mostriamo solo un pezzetto... le posizioni delle prime 10 features
end
[matches, scores] = vl_ubcmatch(d, d2, 1.5) ;
% sift points plot
subplot(1,2,1);
imshow(uint8(I));
hold on;
plot(f(1,matches(1,:)),f(2,matches(1,:)),'b*');
subplot(1,2,2);
imshow(uint8(K));
hold on;
plot(f2(1,matches(2,:)),f2(2,matches(2,:)),'r*');
figure;
%-------------
% RANSAC
X1 = f(1:2,matches(1,:)) ; X1(3,:) = 1 ;
X2 = f2(1:2,matches(2,:)) ; X2(3,:) = 1 ;
numMatches = size(matches,2) ;
for t = 1:100
% estimate homograpyh
subset = vl_colsubset(1:numMatches, 4) ;
A = [] ;
for i = subset
A = cat(1, A, kron(X1(:,i)', vl_hat(X2(:,i)))) ;
end
[U,S,V] = svd(A) ;
H{t} = reshape(V(:,9),3,3) ;
% score homography
X2_ = H{t} * X1 ;
du = X2_(1,:)./X2_(3,:) - X2(1,:)./X2(3,:) ;
dv = X2_(2,:)./X2_(3,:) - X2(2,:)./X2(3,:) ;
ok{t} = (du.*du + dv.*dv) < 6*6 ;
score(t) = sum(ok{t}) ;
end
[score, best] = max(score) ;
H = H{best};
ok = ok{best};
% sift feature matching
dh1 = max(size(K,1)-size(I,1),0) ;
dh2 = max(size(I,1)-size(K,1),0) ;
subplot(2,1,1) ;
imagesc([padarray(I,dh1,'post') padarray(K,dh2,'post')]) ;
colormap (gray);
o = size(I,2) ;
line([f(1,matches(1,:));f2(1,matches(2,:))+o], ...
[f(2,matches(1,:));f2(2,matches(2,:))]) ;
axis image off ;
subplot(2,1,2) ;
imagesc([padarray(I,dh1,'post') padarray(K,dh2,'post')]) ;
colormap (gray);
o = size(I,2) ;
line([f(1,matches(1,ok));f2(1,matches(2,ok))+o], ...
[f(2,matches(1,ok));f2(2,matches(2,ok))]) ;
title(sprintf('%d (%.2f%%) inliner matches out of %d', ...
sum(ok), ...
100*sum(ok)/numMatches, ...
numMatches)) ;
axis image off ;
drawnow ;
end

You are matching using [matches, scores] = vl_ubcmatch(d, d2, 1.5) ;. d contains only the descriptors of the latest frame. You should do something like:
for nFrames=1:3
[matches{nFrames}, scores{nFrames}] = vl_ubcmatch(descriptors{nFrames}, descriptors2{nFrames}, 1.5);
end
From here, you should be able to get the matches between the frames.

Related

Why does this code has an error of incompatible array size?

*[I found that the error occurred at line 57, z2 = w12.a1 + b12; with incompatible array size,
May I know how to fix it? Basically the data is from the simulation on MATLAB Simulink, so the size of the array changes as we increase the inputs from the original code which only have 1 input type][1]
%% load training data (use training_data.mat attached file)
% initialize number of nodes, weights, biases, errors and gradients.
%epochs and mini-batch size
clc
data = [out.dutycycle out.voltage out.current]
sample=1000;
labels = data(1:sample,1);
y = labels'; %output vector
images = data(1:sample,2:3);
images(:,1) = images(:,1)/400;
images(:,2) = images(:,2)/100;
images = images'; %Input vectors
hn1 = 80; %Number of neurons in the first hidden layer
hn2 = 60; %Number of neurons in the second hidden layer
%Initializing weights and biases
w12 = randn(hn1,1000).*sqrt(2/1000);
w23 = randn(hn2,hn1)*sqrt(2/hn1);
w34 = randn(1,hn2)*sqrt(2/hn2);
b12 = randn(hn1,1);
b23 = randn(hn2,1);
b34 = randn(1,1);
%learning rate
eta = 0.0058;
%Initializing errors and gradients
error4 = zeros(1,1);
error3 = zeros(hn2,1);
error2 = zeros(hn1,1);
errortot4 = zeros(1,1);
errortot3 = zeros(hn2,1);
errortot2 = zeros(hn1,1);
grad4 = zeros(1,1);
grad3 = zeros(hn2,1);
grad2 = zeros(hn1,1);
epochs = 50;
m = 10; %Minibatch size
%% Training phase
for k = 1:epochs %Outer epoch loop
batches = 1;
for j = 1:sample/m
error4 = zeros(1,1);
error3 = zeros(hn2,1);
error2 = zeros(hn1,1);
errortot4 = zeros(1,1);
errortot3 = zeros(hn2,1);
errortot2 = zeros(hn1,1);
grad4 = zeros(1,1);
grad3 = zeros(hn2,1);
grad2 = zeros(hn1,1);
for i = batches:batches+m-1 %Loop over each minibatch
%Feed forward
a1 = images(:,i);
z2 = w12.*a1 + b12;
a2 = elu(z2);
z3 = w23*a2 + b23;
a3 = elu(z3);
z4 = w34*a3 + b34;
a4 = elu(z4); %Output vector
%backpropagation
error4 = (a4-y(:,i)).*elup(z4);
error3 = (w34'*error4).*elup(z3);
error2 = (w23'*error3).*elup(z2);
errortot4 = errortot4 + error4;
errortot3 = errortot3 + error3;
errortot2 = errortot2 + error2;
grad4 = grad4 + error4*a3';
grad3 = grad3 + error3*a2';
grad2 = grad2 + error2*a1';
end
%Gradient descent
w34 = w34 - eta/m*grad4;
w23 = w23 - eta/m*grad3;
w12 = w12 - eta/m*grad2;
b34 = b34 - eta/m*errortot4;
b23 = b23 - eta/m*errortot3;
b12 = b12 - eta/m*errortot2;
batches = batches + m;
end
fprintf('Epochs:');
disp(k) %Track number of epochs
[images,y] = shuffle(images,y); %Shuffles order of the images for next epoch
end
disp('Training done!')
%note : create folder for this experiment and save the parameters.
% don't forget to keep the folder in matlab path!
save('wfour.mat','w34');
save('wthree.mat','w23');
save('wtwo.mat','w12');
save('bfour.mat','b34');
save('bthree.mat','b23');
save('btwo.mat','b12');
%% Testing phase
% load testing data with labels ... (use testing_data.mat attached file)
testsample = 100;
labels = test(1:testsample,1);
y = labels';
images = test(1:testsample,2:3);
images(:,1) = images(:,1)/400;
images(:,2) = images(:,2)/100;%copy
images = images';
we34 = matfile('wfour.mat');
w4 = we34.w34;
we23 = matfile('wthree.mat');
w3 = we23.w23;
we12 = matfile('wtwo.mat');
w2 = we12.w12;
bi34 = matfile('bfour.mat');
b4 = bi34.b34;
bi23 = matfile('bthree.mat');
b3 = bi23.b23;
bi12 = matfile('btwo.mat');
b2 = bi12.b12;
success = 0;
n = testsample;
for i = 1:n
out2 = elu(w2*images(:,i)+b2);
out3 = elu(w3*out2+b3);
out = elu(w4*out3+b4);
big = 0;
num = 0;
for k = 1:10
if out(k) > big
num = k-1;
big = out(k);
end
end
if labels(i) == num
success = success + 1;
end
end
fprintf('Accuracy: ');
fprintf('%f',success/n*100);
disp(' %');
%% ELU activation function
function fr = elu(x)
f = zeros(length(x),1);
for i = 1:length(x)
if x(i)>=0
f(i) = x(i);
else
f(i) = 0.2*(exp(x(i))-1);
end
end
fr = f;
end
%% derivative of the ELU activation function
function fr = elup(x)
f = zeros(length(x),1);
for i = 1:length(x)
if x(i)>=0
f(i) = 1;
else
f(i) = 0.2*exp(x(i));
end
end
fr = f;
end
%% SHuffle function
function [B,v] = shuffle(A,y)
cols = size(A,2);
P = randperm(cols);
B = A(:,P);
v = y(:,P);
end
Your help is much appreciated, thank you
MATLAB Error :
Arrays have incompatible sizes for this operation.
Error in ANN_PV_Array (line 57)
z2 = w12.*a1 + b12;

Writing For loop for summation of B in Matlab

the summation to be written in a for loop is here
TCPC = csvread ( 'tcpc.csv' ) ;
TCPC ( : , 3) = TCPC ( : , 3 ) * (100000); %unit conversion
TCPC ( : , 5) = TCPC ( : , 5 ) * (1e-6); %unit conversion
%------------------------------------------------------------------------
% Within the outer loop we calculate Bii's Psat for all components
for i = 1 : n
W(i,i) = TCPC (SP(i),1) ;
TC(i,i) = TCPC (SP(i),2);
PC(i,i) = TCPC (SP(i),3);
ZC(i,i) = TCPC (SP(i),4);
VC(i,i) = TCPC (SP(i),5);
TR(i,i) = T/TC(i,i);
Bi(i,i) = (R*TC(i,i)/PC(i,i))*((.083-(.422/(TR(i,i)^(1.6))))+(W(i,i)*(.139-
((.0172/(TR(i,i)^4.2))))));
phisat(i) = exp((Bi(i,i)*P)/(R*T)) ;
for j = i+1 : n % Within the inner loop we calculate Bij's (mixed coefficients)
W(i,j) = (TCPC (SP(i),1) + TCPC (SP(j),1)) / 2 ;
TC(i,j) = sqrt((TCPC (SP(i),2) * (TCPC (SP(j),2)))) ;
ZC(i,j) = ((TCPC (SP(i),4) * (TCPC (SP(j),4))))/2 ;
VC(i,j) = (((TCPC (SP(i),5)^(1/3) * (TCPC (SP(j),5)^(1/3))))/2)^(3) ;
PC(i,j) = ((ZC(i,j) * R * (TC(i,j)))/VC(i,j)) ;
Bi(i,j) = ((R*TC(i,j))/PC(i,j))*((.083-(.422/(TR(i,i)^(1.6))))+(W(i,j)*(.139-
((.0172/(TR(i,i)^4.2))))));
Bi(j,i)=Bi(i,j); % as Bij=Bji we write the inner loop for j = i+1 : n and assigned
the values to rest
end
end
for i = 1 : n
B(i,i) = ((X(i))^2)*Bi(i,i);
a = sum(B(i,i));
for j = i+1 : n
B(i,j) = X(i)*X(j)*Bi(i,j);
B(j,i) = B(i,j);
b = sum(B(i,j)); c = sum(B(j,I));
end
end
Bfinal = a+b+c
%------------------------------------------------------------------------
% Having calculated all the second virial coefficients,
% we calculate the fugacity coefficients for all the components of the mixture
% use phi bar 1-3 and phi 1-3
for i = 1:n
for j = i+1 : n
phi(i,j) = exp((((-Bfinal+2*X(i)Bi(i) + 2X(j)))P)/(RT));
end
end

Argument types problem ('float', 'const int') in array

I've been trying my first codes in pine script. The question is this. I have created few array.new_float to use as buffers in the 'for' statement. The thing is that I need to do some math over the data. Now, once the 'for' is done, an error pops: 'Cannot call 'operator -' with argument 'expr0' = 'High'.An argument of 'float[]' type was used but a 'const int' is expected'.
Please, if anyone knows what am I doing wrong, I will thank you.
Edit: I will leave the script of what I'm trying to do here
//#version=5
// Indicator name
indicator("DAF_Swing_Index", shorttitle= 'DAF_SwInd', overlay=false)
// Input
T = input.int(30000, title = 'Ratio de escala', minval = 1000, maxval = 150000)
Shift = input.int(0, title = 'Desplazamiento horizontal', minval = 0, maxval = 100)
// Array
SWINGINDEX = array.new_float(200)
Open = array.new_float(200)
Open1 = array.new_float(200)
Close = array.new_float(200)
Close1 = array.new_float(200)
High = array.new_float(200)
Low = array.new_float(200)
// Other variable
var float SwingIndex = 0
var int StartBars = 1
Prev_calculated = bar_index
Rates_total = bar_index + 1
var float SH1 = 0
var float SI = 0
var float R = 0
// Initial bar verification
if Rates_total < StartBars
SwingIndex := 0
Primero = 1
if Prev_calculated > Rates_total or Prev_calculated <= 0
Primero := 1
else
Primero := Prev_calculated-1
// Main process
for bar = Primero to Rates_total
array.push(Open, high[bar])
array.push(Open1, open[bar-1])
array.push(Close, close[bar])
array.push(Close1, close[bar-1])
array.push(High, high[bar])
array.push(Low, low[bar])
K = math.max(math.abs(High - Close1), math.abs(Low - Close1))
TR = math.max(math.max(math.abs(High-Close1), math.abs(Low-Close1)), math.abs(High-Low))
ER = 0.0
if Close1 > High
ER := math.abs(High - Close1)
if Close1 < Low
ER := math.abs(Low - Close1)
SH1 := math.abs(Close1 - Open1)
R := TR - 0.5 * ER + 0.25 * SH1
SI := 0.0
if R != 0
SI := 50 * ((Close - Close1) + 0.5 * (Close - Open1)) * (K / T) / R
SwingIndex := SI
// ploting result
plot(SwingIndex, title = 'Swing Index', style = plot.style_line, color = color.rgb(193, 255, 51, 10))
So, what the error message tells you is, your are passing an array, where it expects a const value.
Like here:
K = math.max(math.abs(High - Close1), math.abs(Low - Close1))
All those variables (High, Close1, Low) are arrays. It simply can not subtract one array from another. You can however, subtract one element from another element.
So for that line, I believe you want something like this:
K = math.max(math.abs(array.get(High, bar) - array.get(Close1, bar)), math.abs(array.get(Low, bar) - array.get(Close1, bar)))
With array.get(), you can get value the of the element at the specified index.
You should fix this in all other occurences.

Error solving a system of differential equations with one third order differential equation in Matlab. Dimensions of array are not consistent

J1 = 18.041;
J2 = 1.99;
J3 = 6.58;
J4 = 1.46;
J5 = 7.50;
J6 = 15.46;
J7 = 4.21;
J8 = 7.68;
J9 = 24.84;
J10 = 8.58;
J11 = 6.57;
J12 = 4.20;
J13 = 4.63;
J14 = 0.7648;
J15 = 0.3607;
J16 = 68.48;
Js1 = 145.09;
Js2 = 128.58;
Js3 = 138.841;
Js4 = 145.09;
Jcar = 1.7916;
Jhollow = 1.478;
r1 = 45;
r2 = 46;
r3 = 38;
r4 = 15;
r5 = 37;
r6 = 68;
r7 = 29;
r8 = 37;
r9 = 60;
r10 = 33;
r11 = 29;
r12 = 27;
r13 = 52;
r14 = 19;
r15 = 16;
r16 = 57;
k0 = 241.32;
w2 = 1000;
kcar = 0.356;
khollow = 6.091;
D = 4867.91;
%syms h3(t) x3(t) theta2(t) theta1(t) x2(t) h4(t) h5(t) h1(t) h2(t) x1(t)
%ode1 = diff(h3)- (J4*r9*(diff(theta2,2)))/r4 == (theta2*khollow*r9)/r4 - kcar*theta1;
%ode2 = diff(x3,t) - (h3/J8)*r8 + w2*r3 == 0;
%ode3 = diff(x2,t) - (h2/J10)*r10 + w2*r5 == 0;
%ode4 = diff(theta2,t)*(r4- 1/(k0*r4)) - (diff(theta2,t,3)/(k0*r4)) - h3*r9/(J9 + Js1) + w2*r4 == 0;
%ode5 = diff(h1,t) - D*r16*(diff(x1)) == k0*x1*r16 - D*r16*((h4*r14)/(Js2 + J1 + (Jcar/3)));
%ode6 = diff(x1,t) - (h4)*r14/(Js2 + J1 + (Jcar/3)) + h1*r16/J16 == 0;
%ode7 = diff(h5,t) - x3*k0*r8 == 0;
%ode8 = diff(h2,t) - x2*k0*r10 == 0;
%ode9 = diff(h4,t) - kcar*theta1 - (2*h4*D)/(Js2 + J1 + (Jcar/3)) + k0*x1*r14 == 0;
%ode10 = diff(theta1,t) - h3/(J9 + Js1) + (h4)/(Js2 + J1 + (Jcar/3)) == 0;
%odes = [ode1; ode2; ode3; ode4; ode5; ode6; ode7; ode8; ode9; ode10];
%S = dsolve(odes);
f = #(t,x) [(J4*r9*(diff(x(4),2)))/r4 + (x(4)*khollow*r9)/r4 - kcar*x(10);
(x(1)*r8)/(J8) - w2*r3;
(x(8)/J10)*r10 - w2*r5;
(x(1)*r9)/(J9 + Js1)*r4 + (diff(x(4)) + diff(x(4),3))/(k0*r4*r4) - w2;
k0*x(6)*r16 - D*r16*(((x(9))*r14)/(Js2 + J1 + (Jcar/3)) - diff(x(6)));
(x(9)*r14)/(Js2 + J1 + (Jcar/3)) - (x(5)*r16)/J16;
x(2)*k0*r8;
x(3)*k0*r10;
kcar*x(10) - x(9)*2*D/(Js1 + J1 + (Jcar/3)) -k0*x(6)*r14;
x(1)/(J9 + Js1) - x(9)/(Js2 + J1 + (Jcar/3))];
[t,xa] = ode45(f,[0 1.5], [0 2 2 2 2 3 1 9 9 1])
Array dimensions are not consistent matlab output error. The commented out code runs for a long time and has not outputted results after running it for 15-20 minutes. The differentials equations are solved using ODE 45. Would really appreciate some help on this. The differential equations come from analyzing a dual clutch manual transmission using the bond graph technique.
Before putting a function like f into ode, try it. It is already broken and you get the same error when calling f(t,x). Further debugging it, evaluate the lines of f one by one:
>> (J4*r9*(diff(x(4),2)))/r4 + (x(4)*khollow*r9)/r4 - kcar*x(10)
ans =
[]
>> (x(1)*r8)/(J8) - w2*r3
ans =
-38000
The first row holds 0 elements, the second 1 element. I did not continue but this is already a problem. Each row has to have the same number of elements, probably 1 in your case.

Matlab : How to highlight GLYCINE residues in my Ramachandran plot? [duplicate]

This question already has an answer here:
MATLAB : What is the mistake in my Ramachandran plot?
(1 answer)
Closed 8 years ago.
I am trying matlab to plot ramachandran plot, without using built in command. I have succeeded too. Now I wanted to spot the GLYCINEs alone in the scatter array. Any ideas how to do this? (link to 1UBQ.pdb file : http://www.rcsb.org/pdb/download/downloadFile.do?fileFormat=pdb&compression=NO&structureId=1UBQ)
% Program to plot Ramanchandran plot of Ubiquitin
close all; clear ; clc; % close all figure windows, clear variables, clear screen
pdb1 ='/home/devanandt/Documents/VMD/1UBQ.pdb';
p=pdbread(pdb1); % read pdb file corresponding to ubiquitin protein
atom={p.Model.Atom.AtomName};
n_i=find(strcmp(atom,'N')); % Find indices of atoms
ca_i=find(strcmp(atom,'CA'));
c_i=find(strcmp(atom,'C'));
X = [p.Model.Atom.X];
Y = [p.Model.Atom.Y];
Z = [p.Model.Atom.Z];
X_n = X(n_i(2:end)); % X Y Z coordinates of atoms
Y_n = Y(n_i(2:end));
Z_n = Z(n_i(2:end));
X_ca = X(ca_i(2:end));
Y_ca = Y(ca_i(2:end));
Z_ca = Z(ca_i(2:end));
X_c = X(c_i(2:end));
Y_c = Y(c_i(2:end));
Z_c = Z(c_i(2:end));
X_c_ = X(c_i(1:end-1)); % the n-1 th C (C of cabonyl)
Y_c_ = Y(c_i(1:end-1));
Z_c_ = Z(c_i(1:end-1));
V_c_ = [X_c_' Y_c_' Z_c_'];
V_n = [X_n' Y_n' Z_n'];
V_ca = [X_ca' Y_ca' Z_ca'];
V_c = [X_c' Y_c' Z_c'];
V_ab = V_n - V_c_;
V_bc = V_ca - V_n;
V_cd = V_c - V_ca;
phi=0;
for k=1:numel(X_c)
n1=cross(V_ab(k,:),V_bc(k,:))/norm(cross(V_ab(k,:),V_bc(k,:)));
n2=cross(V_bc(k,:),V_cd(k,:))/norm(cross(V_bc(k,:),V_cd(k,:)));
x=dot(n1,n2);
m1=cross(n1,(V_bc(k,:)/norm(V_bc(k,:))));
y=dot(m1,n2);
phi=cat(2,phi,-atan2d(y,x));
end
phi=phi(1,2:end);
X_n_ = X(n_i(2:end)); % (n+1) nitrogens
Y_n_ = Y(n_i(2:end));
Z_n_ = Z(n_i(2:end));
X_ca = X(ca_i(1:end-1));
Y_ca = Y(ca_i(1:end-1));
Z_ca = Z(ca_i(1:end-1));
X_n = X(n_i(1:end-1));
Y_n = Y(n_i(1:end-1));
Z_n = Z(n_i(1:end-1));
X_c = X(c_i(1:end-1));
Y_c = Y(c_i(1:end-1));
Z_c = Z(c_i(1:end-1));
V_n_ = [X_n_' Y_n_' Z_n_'];
V_n = [X_n' Y_n' Z_n'];
V_ca = [X_ca' Y_ca' Z_ca'];
V_c = [X_c' Y_c' Z_c'];
V_ab = V_ca - V_n;
V_bc = V_c - V_ca;
V_cd = V_n_ - V_c;
psi=0;
for k=1:numel(X_c)
n1=cross(V_ab(k,:),V_bc(k,:))/norm(cross(V_ab(k,:),V_bc(k,:)));
n2=cross(V_bc(k,:),V_cd(k,:))/norm(cross(V_bc(k,:),V_cd(k,:)));
x=dot(n1,n2);
m1=cross(n1,(V_bc(k,:)/norm(V_bc(k,:))));
y=dot(m1,n2);
psi=cat(2,psi,-atan2d(y,x));
end
psi=psi(1,2:end);
scatter(phi,psi)
box on
axis([-180 180 -180 180])
title('Ramachandran Plot for Ubiquitn Protein','FontSize',16)
xlabel('\Phi^o','FontSize',20)
ylabel('\Psi^o','FontSize',20)
grid
The output is :
EDIT : Is my plot correct? Biopython: How to avoid particular amino acid sequences from a protein so as to plot Ramachandran plot? has an answer which has slightly different plot.
The modified code is as below :
% Program to plot Ramanchandran plot of Ubiquitin with no glycines
close all; clear ; clc; % close all figure windows, clear variables, clear screen
pdb1 ='/home/devanandt/Documents/VMD/1UBQ.pdb';
p=pdbread(pdb1); % read pdb file corresponding to ubiquitin protein
atom={p.Model.Atom.AtomName};
n_i=find(strcmp(atom,'N')); % Find indices of atoms
ca_i=find(strcmp(atom,'CA'));
c_i=find(strcmp(atom,'C'));
X = [p.Model.Atom.X];
Y = [p.Model.Atom.Y];
Z = [p.Model.Atom.Z];
X_n = X(n_i(2:end)); % X Y Z coordinates of atoms
Y_n = Y(n_i(2:end));
Z_n = Z(n_i(2:end));
X_ca = X(ca_i(2:end));
Y_ca = Y(ca_i(2:end));
Z_ca = Z(ca_i(2:end));
X_c = X(c_i(2:end));
Y_c = Y(c_i(2:end));
Z_c = Z(c_i(2:end));
X_c_ = X(c_i(1:end-1)); % the n-1 th C (C of cabonyl)
Y_c_ = Y(c_i(1:end-1));
Z_c_ = Z(c_i(1:end-1));
V_c_ = [X_c_' Y_c_' Z_c_'];
V_n = [X_n' Y_n' Z_n'];
V_ca = [X_ca' Y_ca' Z_ca'];
V_c = [X_c' Y_c' Z_c'];
V_ab = V_n - V_c_;
V_bc = V_ca - V_n;
V_cd = V_c - V_ca;
phi=0;
for k=1:numel(X_c)
n1=cross(V_ab(k,:),V_bc(k,:))/norm(cross(V_ab(k,:),V_bc(k,:)));
n2=cross(V_bc(k,:),V_cd(k,:))/norm(cross(V_bc(k,:),V_cd(k,:)));
x=dot(n1,n2);
m1=cross(n1,(V_bc(k,:)/norm(V_bc(k,:))));
y=dot(m1,n2);
phi=cat(2,phi,-atan2d(y,x));
end
phi=phi(1,2:end);
X_n_ = X(n_i(2:end)); % (n+1) nitrogens
Y_n_ = Y(n_i(2:end));
Z_n_ = Z(n_i(2:end));
X_ca = X(ca_i(1:end-1));
Y_ca = Y(ca_i(1:end-1));
Z_ca = Z(ca_i(1:end-1));
X_n = X(n_i(1:end-1));
Y_n = Y(n_i(1:end-1));
Z_n = Z(n_i(1:end-1));
X_c = X(c_i(1:end-1));
Y_c = Y(c_i(1:end-1));
Z_c = Z(c_i(1:end-1));
V_n_ = [X_n_' Y_n_' Z_n_'];
V_n = [X_n' Y_n' Z_n'];
V_ca = [X_ca' Y_ca' Z_ca'];
V_c = [X_c' Y_c' Z_c'];
V_ab = V_ca - V_n;
V_bc = V_c - V_ca;
V_cd = V_n_ - V_c;
psi=0;
for k=1:numel(X_c)
n1=cross(V_ab(k,:),V_bc(k,:))/norm(cross(V_ab(k,:),V_bc(k,:)));
n2=cross(V_bc(k,:),V_cd(k,:))/norm(cross(V_bc(k,:),V_cd(k,:)));
x=dot(n1,n2);
m1=cross(n1,(V_bc(k,:)/norm(V_bc(k,:))));
y=dot(m1,n2);
psi=cat(2,psi,-atan2d(y,x));
end
psi=psi(1,2:end);
res=strsplit(p.Sequence.ResidueNames,' ');
angle =[phi;psi];
angle(:,find(strcmp(res,'GLY'))-1)=[];
scatter(angle(1,:),angle(2,:))
box on
axis([-180 180 -180 180])
title('Ramachandran Plot for Ubiquitn Protein','FontSize',16)
xlabel('\Phi^o','FontSize',20)
ylabel('\Psi^o','FontSize',20)
grid
which gives output (with no GLY) as below :
I would change this code block to use logical indexing
res=strsplit(p.Sequence.ResidueNames,' ');
angle =[phi;psi];
angle(:,find(strcmp(res,'GLY'))-1)=[];
Instead:
residues = strsplit(p.Sequency.ResidueNames,' ');
glycine = ismember(residues,'GLY');
angle = [phi;psi];
angleNoGLY= angle(:,~glycine);
Doing it this way, if you wanted to highlight glycine (or any other residue) you can easily call it out:
angleGLY = angle(:,glycine);
plot(angleNoGLY(1,:),angleNoGLY(2,:),'ob')
line(angleGLY(1,:),angleGLY(2,:),'Marker','o','Color','r','LineStyle','none')

Resources