/tags/R2004-07-07/octave-forge/extra/pdb/write_pdb.m

# · Objective C · 295 lines · 266 code · 29 blank · 0 comment · 40 complexity · a787c48930cee06165692e16aea96b27 MD5 · raw file

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