function write_atom_itp(atom,Box_dim,filename,varargin)
format long
nAtoms=size(atom,2);
if regexp(filename,'.itp') ~= false
filename = filename;
else
filename = strcat(filename,'.itp');
end
if nargin > 3
short_r=cell2mat(varargin(1));
long_r=cell2mat(varargin(2));
else
short_r=1.25;
long_r=1.25;
end
if nargin>5
ffname=varargin(3)
if nargin>6
watermodel=varargin(4)
else
disp('Unknown watermodel, will try SPC/E')
watermodel='SPC/E'
end
if strncmpi(ffname,'clayff',5)
clayff_param(sort(unique([atom.type])),watermodel);
if ~isfield(atom,'charge')
atom = charge_atom(atom,Box_dim,'clayff',watermodel,'adjust');
end
Total_charge=sum([atom.charge])
nrexcl=1;
explicit_bonds = 0;
explicit_angles = 0;
elseif strcmpi(ffname,'interface')
interface_param(sort(unique([atom.type])),watermodel);
atom = charge_atom(atom,Box_dim,'interface',watermodel,'adjust');
Total_charge
nrexcl=2;
explicit_bonds = 1;
explicit_angles = 1;
elseif strcmpi(ffname,'interface15')
atom = mass_atom(atom);
interface15_param(sort(unique([atom.type])),watermodel);
atom = charge_atom(atom,Box_dim,'interface15',watermodel,'adjust');
Total_charge
nrexcl=2;
explicit_bonds = 1;
explicit_angles = 1;
elseif strcmpi(ffname,'interface_car')
atom = mass_atom(atom);
nrexcl=2;
explicit_bonds = 0;
explicit_angles = 0;
end
else
disp('Unknown forcefield, will try clayff and SPC/E')
watermodel='spc/e';
ffname='clayff';
clayff_param(sort(unique([atom.type])),watermodel);
atom = charge_atom(atom,Box_dim,'clayff','spc/e');
Total_charge
nrexcl=1;
explicit_bonds = 0;
explicit_angles = 0;
end
atom = bond_angle_atom(atom,Box_dim,short_r,long_r);
assignin('caller','Bond_index',Bond_index);
assignin('caller','Angle_index',Angle_index);
assignin('caller','nBonds',nBonds);
assignin('caller','nAngles',nAngles);
assignin('caller','atom',atom);
file_title = 'Gromacs awesome itp file';
molecule_name = filename;
Atom_label = unique([atom.type]);
ind_allh = find(~cellfun(@isempty,regexpi([atom.type],'h')));
ind_H=find(strncmp([atom.type],{'H'},1));
ind_O=find(strncmp([atom.type],{'O'},1));
ind_Oh=intersect(ind_O,ind_allh);
ind_Al=find(strcmp([atom.type],'Al'));
ind_Mgo=find(strncmp([atom.type],{'Mgo'},3));
ind_Oct=sort([ind_Al ind_Mgo]);
fid = fopen(molecule_name, 'wt');
fprintf(fid, '%s % s\r\n',';',file_title);
fprintf(fid, '%s % s\r\n',';','File written by MHolmboe (michael.holmboe@umu.se)');
fprintf(fid, '\r\n');
fprintf(fid, '%s\r\n','[ moleculetype ]');
fprintf(fid, '%s % s\r\n',';','molname nrexcl');
fprintf(fid, '%s %d\r\n',strrep(molecule_name,'.itp',''),nrexcl);
fprintf(fid, '\r\n');
fprintf(fid, '%s\r\n','[ atoms ]');
fprintf(fid, '%s\r\n','; id attype resnr resname atname cgnr charge mass');
Atom_label_ID=ones(size(atom,2),1);
for i = 1:nAtoms
if sum(ismember(Atom_label,[atom(i).type])) > 0
Atom_label_ID(i,1)=find(ismember(Atom_label,[atom(i).type])==1);
end
if isfield(atom,'mass')
Atoms_data(i,:) = {i, char([atom(i).fftype]),[atom(i).molid],molecule_name(1:3),char([atom(i).type]),i, [atom(i).charge],[atom(i).mass]};
else exist('Masses','var');
Atoms_data(i,:) = {i, char([atom(i).type]),[atom(i).molid],molecule_name(1:3),char([atom(i).type]),i, [atom(i).charge], Masses(Atom_label_ID(i,1))};
end
fprintf(fid, '%-4i%6s%8i%8s%8s%8i\t%8.5f\t%8.6f\r\n', Atoms_data{i,:});
end
fprintf(fid, '\r\n');
fprintf(fid, '[ bonds ] \r\n');
fprintf(fid, '%s\r\n','; i j type');
count_b = 1;
bondtype=1;
while count_b <= nBonds
if explicit_bonds == 1
if sum(ismember(Bond_index(count_b,1:2),ind_H))>0
r=0.09290;
kb=414216;
else
r=Bond_index(count_b,3)/10*1.05;
kb=359824;
end
Bond_order(count_b,:)= {Bond_index(count_b,1), Bond_index(count_b,2), bondtype, r, kb, ';',strtrim(char([atom(Bond_index(count_b,1)).type])), strtrim(char([atom(Bond_index(count_b,2)).type]))};
fprintf(fid, '%-5i\t%-5i\t%-5i\t%-8.4f\t%-8.4f\t%s\t%s-%s\r\n', Bond_order{count_b,:});
count_b = count_b + 1;
else
Bond_order(count_b,:)= {Bond_index(count_b,1), Bond_index(count_b,2), bondtype, ';', Bond_index(count_b,3)/10, strtrim(char([atom(Bond_index(count_b,1)).type])), strtrim(char([atom(Bond_index(count_b,2)).type]))};
fprintf(fid, '%-5i %-5i %-5i %s %-8.4f %s-%s \r\n', Bond_order{count_b,:});
count_b = count_b + 1;
end
end
if numel(Bond_order)>0
assignin('caller','Bond_order',Bond_order);
disp('These atom types has bonds')
unique(Bond_order(:,end-1:end))
end
fprintf(fid, '\r\n');
fprintf(fid, '\r\n');
fprintf(fid, '[ angles ] \r\n');
fprintf(fid, '%s\r\n','; i j k type');
if strncmpi(ffname,'clayff',5)
disp('Removing angles with Al')
[Al_row,Al_col]=ind2sub(size(Angle_index),find(ismember(Angle_index,ind_Al)));
Angle_index(Al_row,:)=[];
end
count_a = 1;
angletype=1; Angle_order={};
Angle_index=sortrows(Angle_index);
while count_a <= length(Angle_index)
if explicit_angles == 1
if sum(ismember(Angle_index(count_a,1:3),ind_H))==1
if sum(ismember(Angle_index(count_a,1:3),ind_Mgo))>0
adeg=110;
ktheta=50.208;
elseif sum(ismember(Angle_index(count_a,1:3),ind_Al))>0
adeg=110;
ktheta=125.52;
else
adeg=110;
ktheta=251.04;
end
elseif sum(ismember(Angle_index(count_a,1:3),ind_H))==2
adeg=109.47;
ktheta=383;
else
adeg=Angle_index(count_a,4);
ktheta=1422.56;
end
Angle_order(count_a,:)= {Angle_index(count_a,1), Angle_index(count_a,2), Angle_index(count_a,3), angletype, adeg, ktheta, ';', strtrim(char([atom(Angle_index(count_a,1)).type])), strtrim(char([atom(Angle_index(count_a,2)).type])), strtrim(char([atom(Angle_index(count_a,3)).type]))};
fprintf(fid, '%-5i %-5i %-5i %-5i %-8.4f %-8.4f %s %s-%s-%s\r\n', Angle_order{count_a,:});
count_a = count_a + 1;
else
Angle_order(count_a,:)= {Angle_index(count_a,1), Angle_index(count_a,2), Angle_index(count_a,3), angletype, ';', Angle_index(count_a,4), strtrim(char([atom(Angle_index(count_a,1)).type])), strtrim(char([atom(Angle_index(count_a,2)).type])), strtrim(char([atom(Angle_index(count_a,3)).type]))};
fprintf(fid, '%-5i %-5i %-5i %-5i %s %-8.4f %s-%s-%s\r\n', Angle_order{count_a,:});
count_a = count_a + 1;
end
end
fprintf(fid, '\r\n');
fprintf(fid, '\r\n');
if numel(Angle_order)>0
assignin('caller','Angle_order',Angle_order);
disp('These atom types has angles')
unique(Angle_order(:,end-2:end))
end
if exist('Total_charge','var')
disp('Total charge for the .itp file was')
Total_charge
end
if strncmpi(ffname,'clayff',5)
fprintf(fid, '#ifdef POSRES_Clayff \r\n');
fprintf(fid, '[ position_restraints ] \r\n');
fprintf(fid, '%s\r\n','; atom type fx fy fz');
for i = 1:nAtoms
pos_res(i,:) = {num2str(i), '1', '1000', '1000', '1000'};
fprintf(fid, '%6s\t%6s\t%6s\t%6s\t%6s%\r\n', pos_res{i,:});
fprintf(fid, '\r\n');
end
fprintf(fid, '#endif \r\n');
else
fprintf(fid, '#ifdef POSRES_interface \r\n');
fprintf(fid, '[ position_restraints ] \r\n');
fprintf(fid, '%s\r\n','; atom type fx fy fz');
for i = 1:nAtoms
pos_res(i,:) = {num2str(i), '1', '1000', '1000', '1000'};
fprintf(fid, '%6s\t%6s\t%6s\t%6s\t%6s%\r\n', pos_res{i,:});
fprintf(fid, '\r\n');
end
fprintf(fid, '#endif \r\n');
end
fprintf(fid, '\r\n');
fprintf(fid, '\r\n');
fprintf(fid, '#ifdef POSRES \r\n');
fprintf(fid, '[ position_restraints ] \r\n');
fprintf(fid, '%s\r\n','; atom type fx fy fz');
for i = 1:nAtoms
pos_res(i,:) = {num2str(i), '1', '1000', '1000', '1000'};
fprintf(fid, '%6s\t%6s\t%6s\t%6s\t%6s%\r\n', pos_res{i,:});
fprintf(fid, '\r\n');
end
fprintf(fid, '#endif \r\n');
fprintf(fid, '\r\n');
fprintf(fid, '\r\n');
fprintf(fid, '#ifdef POSRES_noH \r\n');
fprintf(fid, '[ position_restraints ] \r\n');
fprintf(fid, '%s\r\n','; atom type fx fy fz');
for i = 1:nAtoms;
if strncmpi([atom(i).type],'H',1)==0
pos_res(i,:) = {num2str(i), '1', '1000', '1000', '1000'};
fprintf(fid, '%6s\t%6s\t%6s\t%6s\t%6s%\r\n', pos_res{i,:});
fprintf(fid, '\r\n');
end
end
fprintf(fid, '#endif \r\n');
fprintf(fid, '\r\n');
fprintf(fid, '\r\n');
fprintf(fid, '#ifdef POSRES_XYZ \r\n');
fprintf(fid, '[ position_restraints ] \r\n');
fprintf(fid, '%s\r\n','; atom type fx fy fz');
for i = 1:nAtoms
if ismember(i,ind_Oct)
pos_res(i,:) = {num2str(i), '1', '1000', '1000', '1000'};
fprintf(fid, '%6s\t%6s\t%6s\t%6s\t%6s%\r\n', pos_res{i,:});
fprintf(fid, '\r\n');
end
end
fprintf(fid, '#endif \r\n');
fprintf(fid, '\r\n');
fprintf(fid, '\r\n');
fprintf(fid, '#ifdef POSRES_XY_MMT_1 \r\n');
fprintf(fid, '[ position_restraints ] \r\n');
fprintf(fid, '%s\r\n','; atom type fx fy fz');
for i = 1:nAtoms
if ismember(i,ind_Oct)
if strcmp([atom(i).type],'Al') > 0 || strcmp([atom(i).type],'Mgo') > 0
pos_res(i,:) = {num2str(i), '1', '1000', '1000','0'};
fprintf(fid, '%6s\t%6s\t%6s\t%6s\t%6s%\r\n', pos_res{i,:});
fprintf(fid, '\r\n');
end
end
end
fprintf(fid, '#endif \r\n');
fprintf(fid, '\r\n');
fprintf(fid, '\r\n');
fprintf(fid, '#ifdef POSRES_Y_10 \r\n');
fprintf(fid, '[ position_restraints ] \r\n');
fprintf(fid, '%s\r\n','; atom type fx fy fz');
for i = 1:nAtoms
if ismember(i,ind_Oct)
pos_res(i,:) = {num2str(i), '1', '1000', '10', '1000'};
fprintf(fid, '%6s\t%6s\t%6s\t%6s\t%6s%\r\n', pos_res{i,:});
fprintf(fid, '\r\n');
end
end
fprintf(fid, '#endif \r\n');
fprintf(fid, '\r\n');
fprintf(fid, '\r\n');
fprintf(fid, '#ifdef POSRES_Y_100 \r\n');
fprintf(fid, '[ position_restraints ] \r\n');
fprintf(fid, '%s\r\n','; atom type fx fy fz');
for i = 1:nAtoms
if ismember(i,ind_Oct)
pos_res(i,:) = {num2str(i), '1', '1000', '100', '1000'};
fprintf(fid, '%6s\t%6s\t%6s\t%6s\t%6s%\r\n', pos_res{i,:});
fprintf(fid, '\r\n');
end
end
fprintf(fid, '#endif \r\n');
fclose(fid);
[atom(strcmp([atom.type],{'Ow'})).type]=deal({'OW'});
[atom(strcmp([atom.type],{'Hw'})).type]=deal({'HW'});
atom_itp=atom;
assignin('caller','atom_itp',atom_itp);