/tags/R2004-11-16/octave-forge/extra/pdb/write_pdb.m

# · Objective C · 329 lines · 292 code · 37 blank · 0 comment · 43 complexity · 2c2f85dc50e0746b21f234a2de9c7368 MD5 · raw file

  1. function write_pdb(p, fname, varargin)
  2. #function write_pdb(p, fname[, quick])
  3. #
  4. # Writes a pdb struct p (see read_pdb.m) to a pdb-file fname.
  5. #
  6. # If the third parameter quick is given, write only ATOM and HETATM lines.
  7. ## Created: 3.8.2001
  8. ## Author: Teemu Ikonen <tpikonen@pcu.helsinki.fi>
  9. if(!is_struct(p)) # || !struct_contains(p, "acoord"))
  10. error("p must be a pdb struct");
  11. endif
  12. [f, err] = fopen(fname, "w");
  13. if(f < 0)
  14. error("Could not open output file: %s", err);
  15. endif
  16. quick = false;
  17. if(nargin() > 2)
  18. quick = true;
  19. endif
  20. buf = blanks(80);
  21. if(!quick)
  22. # Print the Title Section
  23. if( struct_contains(p, "classification") || struct_contains(p, "idcode") )
  24. if struct_contains(p, "classification")
  25. classification = p.classification;
  26. else
  27. classification = "Unknown";
  28. endif
  29. if(struct_contains(p, "idcode"))
  30. idcode = p.idcode;
  31. else
  32. idcode = "N/A ";
  33. endif
  34. buf(1:6) = sprintf("HEADER");
  35. buf(11:50) = sprintf("%-40s", classification);
  36. buf(63:66) = sprintf("%4s", idcode);
  37. buf(80) = sprintf("\n");
  38. fprintf(f, "%s", buf);
  39. endif
  40. # Print the Primary Structure Section
  41. numres = 0;
  42. if(struct_contains(p, "seqres"))
  43. numres = size(p.seqres, 1);
  44. j = 1;
  45. i = 1;
  46. while(j <= numres),
  47. buf = blanks(80);
  48. buf(1:6) = sprintf("SEQRES");
  49. buf(9:10) = sprintf("%2d", i);
  50. buf(14:17) = sprintf("%4d", numres);
  51. k = 20;
  52. while(k <= 68 && j <= numres)
  53. buf(k:(k+2)) = sprintf("%s", p.seqres(j, :));
  54. k = k + 4;
  55. j++;
  56. endwhile
  57. buf(80) = sprintf("\n");
  58. fprintf(f, "%s", buf);
  59. i++;
  60. endwhile
  61. endif
  62. # Print the Heterogen Section
  63. # -------------
  64. # Print the Secondary Structure Section
  65. # -------------
  66. # Print the Connectivity Annotation Section
  67. # -------------
  68. # Print the Miscellaneous Features Section
  69. # -------------
  70. # Print the Crystallographic and Coordinate Transformation Section
  71. if( struct_contains(p, "cellsize") || struct_contains(p, "cellangl") \
  72. || struct_contains(p, "sgroup") || struct_contains(p, "z") )
  73. if(struct_contains(p, "cellsize"))
  74. cellsize = p.cellsize;
  75. else
  76. cellsize = zeros(3,1);
  77. endif
  78. if(struct_contains(p, "cellangl"))
  79. cellangl = p.cellangl;
  80. else
  81. cellangl = zeros(3,1);
  82. endif
  83. if(struct_contains(p, "sgroup"))
  84. sgroup = p.sgroup;
  85. else
  86. sgroup = "N/A";
  87. endif
  88. if(struct_contains(p, "z"))
  89. z = p.z;
  90. else
  91. z = 0;
  92. endif
  93. buf = blanks(80);
  94. buf(1:6) = "CRYST1";
  95. buf(7:54) = sprintf(" %8.3f %8.3f %8.3f %6.2f %6.2f %6.2f", \
  96. cellsize, cellangl);
  97. buf(56:66) = sprintf("%-11s", sgroup);
  98. buf(67:70) = sprintf("%4d", z);
  99. buf(80) = "\n";
  100. fprintf(f, "%s", buf);
  101. end
  102. #buf = blanks(80);
  103. #buf(1:55) = "ORIGX1 1.000000 0.000000 0.000000 0.000000";
  104. #buf(80) = "\n";
  105. #fprintf(f, "%s", buf);
  106. #buf = blanks(80);
  107. #buf(1:55) = "ORIGX2 0.000000 1.000000 0.000000 0.000000";
  108. #buf(80) = "\n";
  109. #fprintf(f, "%s", buf);
  110. #buf = blanks(80);
  111. #buf(1:55) = "ORIGX3 0.000000 0.000000 1.000000 0.000000";
  112. #buf(80) = "\n";
  113. #fprintf(f, "%s", buf);
  114. if( struct_contains(p, "scalem") || struct_contains(p, "scalev") )
  115. if(struct_contains(p, "scalem"))
  116. scalem = p.scalem;
  117. else
  118. scalem = eye(3);
  119. endif
  120. if(struct_contains(p, "scalev"))
  121. scalev = p.scalev;
  122. else
  123. scalev = zeros(3,1);
  124. endif
  125. buf = blanks(80);
  126. buf(1:6) = "SCALE1";
  127. buf(11:40) = sprintf(" %9.6f %9.6f %9.6f", scalem(1,:));
  128. buf(46:55) = sprintf("%10.5f", scalev(1));
  129. buf(80) = "\n";
  130. fprintf(f, "%s", buf);
  131. buf = blanks(80);
  132. buf(1:6) = "SCALE2";
  133. buf(11:40) = sprintf(" %9.6f %9.6f %9.6f", scalem(2,:));
  134. buf(46:55) = sprintf("%10.5f", scalev(2));
  135. buf(80) = "\n";
  136. fprintf(f, "%s", buf);
  137. buf = blanks(80);
  138. buf(1:6) = "SCALE3";
  139. buf(11:40) = sprintf(" %9.6f %9.6f %9.6f", scalem(3,:));
  140. buf(46:55) = sprintf("%10.5f", scalev(3));
  141. buf(80) = "\n";
  142. fprintf(f, "%s", buf);
  143. endif
  144. endif # if(!quick)
  145. serialn = 0;
  146. if(struct_contains(p, "acoord"))
  147. natoms = size(p.acoord, 1);
  148. if(struct_contains(p, "atomname"))
  149. atomname = toupper(p.atomname);
  150. else
  151. for i = 1:natoms,
  152. atomname(i, :) = " ";
  153. endfor
  154. endif
  155. if(struct_contains(p, "aresname"))
  156. aresname = toupper(p.aresname);
  157. else
  158. for i = 1:natoms,
  159. aresname(i, :) = " ";
  160. endfor
  161. endif
  162. if(struct_contains(p, "aresseq"))
  163. aresseq = p.aresseq;
  164. else
  165. aresseq = ones(natoms, 1);
  166. endif
  167. if(struct_contains(p, "aoccupancy"))
  168. aoccupancy = p.aoccupancy;
  169. else
  170. aoccupancy = ones(natoms, 1);
  171. endif
  172. if(struct_contains(p, "atempfactor"))
  173. atempfactor = p.atempfactor;
  174. else
  175. atempfactor = zeros(natoms, 1);
  176. endif
  177. i = 1;
  178. segid = toascii("C")-1;
  179. first = 1;
  180. while(i <= natoms)
  181. if (aresseq(i) == 1)
  182. if(first)
  183. segid = segid + 1;
  184. first = 0;
  185. endif
  186. else
  187. first = 1;
  188. endif
  189. serialn++;
  190. # buf = blanks(80);
  191. # buf(1:6) = "ATOM ";
  192. # buf(7:11) = sprintf("%5d", serialn);
  193. # buf(13:16) = sprintf("%4s", atomname(i,:));
  194. # buf(18:20) = aresname(i,:);
  195. # buf(23:26) = sprintf("%4d", aresseq(i));
  196. # buf(31:54) = sprintf("%8.3f%8.3f%8.3f", p.acoord(i,:));
  197. # buf(55:60) = sprintf(" %5.2f", aoccupancy(i,:));
  198. # buf(61:66) = sprintf("%6.2f", atempfactor(i,:));
  199. # buf(73:76) = sprintf("%c ", segid);
  200. # buf(74) = "\n";
  201. # fprintf(f, "%s", buf(1:74));
  202. fprintf(f, "ATOM ");
  203. fprintf(f, "%5d", serialn);
  204. fprintf(f, " %-4s", atomname(i,:));
  205. fprintf(f, " %3s", aresname(i,:));
  206. fprintf(f, " %4d", aresseq(i));
  207. fprintf(f, " %8.3f%8.3f%8.3f", p.acoord(i,:));
  208. fprintf(f, " %5.2f", aoccupancy(i,:));
  209. fprintf(f, "%6.2f", atempfactor(i,:));
  210. fprintf(f, " %-4c", segid);
  211. fprintf(f, "\n");
  212. i++;
  213. endwhile
  214. serialn++;
  215. buf = blanks(80);
  216. buf(1:6) = "TER ";
  217. buf(7:11) = sprintf("%5d", serialn);
  218. buf(18:20) = aresname(natoms, :);
  219. buf(23:26) = sprintf("%4d", aresseq(natoms));
  220. buf(80) = "\n";
  221. fprintf(f, "%s", buf);
  222. endif
  223. if(struct_contains(p,"hetcoord"))
  224. nhet = size(p.hetcoord, 1);
  225. if(struct_contains(p, "hetname"))
  226. hetname = toupper(p.hetname);
  227. else
  228. for i = 1:nhet
  229. hetname(i, :) = " ";
  230. endfor
  231. endif
  232. if(struct_contains(p, "hetresname"))
  233. hetresname = toupper(p.hetresname);
  234. else
  235. for i = 1:nhet,
  236. hetresname(i, :) = " ";
  237. endfor
  238. endif
  239. if(struct_contains(p, "hetresseq"))
  240. hetresseq = p.hetresseq;
  241. else
  242. hetresseq = ones(nhet, 1);
  243. endif
  244. if(struct_contains(p, "hetoccupancy"))
  245. hetoccupancy = p.hetoccupancy;
  246. else
  247. hetoccupancy = ones(nhet, 1);
  248. endif
  249. if(struct_contains(p, "hettempfactor"))
  250. hettempfactor = p.hettempfactor;
  251. else
  252. hettempfactor = zeros(nhet, 1);
  253. endif
  254. i = 1;
  255. while(i <= nhet)
  256. serialn++;
  257. # buf = blanks(80);
  258. # buf(1:6) = "HETATM";
  259. # buf(7:11) = sprintf("%5d", serialn);
  260. # buf(13:16) = sprintf("%s", hetname(i,:));
  261. # buf(18:20) = hetresname(i, :);
  262. # buf(23:26) = sprintf("%4d", hetresseq(i));
  263. # buf(31:54) = sprintf(" %7.3f %7.3f %7.3f", p.hetcoord(i,:));
  264. # buf(55:60) = sprintf(" %5.2f", hetoccupancy(i,:));
  265. # buf(61:66) = sprintf("%6.2f", hettempfactor(i,:));
  266. # buf(80) = "\n";
  267. # fprintf(f, "%s", buf);
  268. fprintf(f, "HETATM");
  269. fprintf(f, "%5d", serialn);
  270. fprintf(f, " %-4s", hetname(i,:));
  271. fprintf(f, " %3s", hetresname(i,:));
  272. fprintf(f, " %4d", hetresseq(i));
  273. fprintf(f, " %8.3f%8.3f%8.3f", p.hetcoord(i,:));
  274. fprintf(f, " %5.2f", hetoccupancy(i,:));
  275. fprintf(f, "%6.2f", hettempfactor(i,:));
  276. # fprintf(f, " %-4c", segid);
  277. fprintf(f, "\n");
  278. i++;
  279. endwhile
  280. endif
  281. #buf = blanks(80);
  282. #buf(1:6) = "MASTER";
  283. #buf(11:70) = sprintf(" %4d %4d %4d %4d %4d %4d %4d %4d %4d %4d %4d %4d",
  284. # 0, 0, 0, 0, 0, 0, 0, 6, natoms+nhetatoms, 1, 0, numres);
  285. #buf(80) = "\n";
  286. #fprintf(f, "%s", buf);
  287. buf = blanks(80);
  288. buf(1:6) = "END ";
  289. buf(80) = "\n";
  290. fprintf(f, "%s", buf);
  291. fclose(f);
  292. endfunction