cuser_form.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. // comile
  2. // gcc -shared -Wall -fPIC `/cluster/user/armin/bin/modeller9.10/bin/mod9.10 --cflags --libs` `pkg-config --cflags glib-2.0` -I/usr/include/python2.4 cuser_form52.c cuser_form52_wrap.c -o _cuser_form52.so -lm
  3. #include <glib.h>
  4. #include "modeller.h"
  5. #include "cuser_form.h"
  6. #include <stdio.h>
  7. #include <math.h>
  8. #include <stdlib.h>
  9. /* RT at 297.15K, in kcal/mol */
  10. //static const float RT = 0.5900991;
  11. static const float RT = 6.0;
  12. /* Decode parameters from Modeller parameter array */
  13. inline static void get_param(const float pcsr[6], float *w, float *mean, float *stdev, float *w1, float *mean1, float *stdev1)
  14. {
  15. *w = pcsr[0]+1E-5;
  16. *mean = pcsr[1];
  17. *stdev = pcsr[2];
  18. *w1 = pcsr[3]+1E-5;
  19. *mean1 = pcsr[4];
  20. *stdev1 = pcsr[5];
  21. //check if sum w+w1 equals 1
  22. float sum = *w + *w1;
  23. *w /= sum;
  24. *w1 /= sum;
  25. }
  26. float get_p(float stdev, float x, float delta)
  27. {
  28. return exp(-0.5 * delta * delta / (stdev*stdev))/x;
  29. }
  30. ///////////////////////
  31. /* Evaluate the form */
  32. ///////////////////////
  33. static int myform_eval(void *data, const float *feat, const int *iftyp,
  34. const int *modal, int n_feat, const float *pcsr,
  35. int n_pcsr, gboolean deriv, float *fderv, float *val)
  36. {
  37. float w, mean, stdev, w1, mean1, stdev1;
  38. get_param(pcsr, &w, &mean, &stdev, &w1, &mean1, &stdev1);
  39. if (feat[0]<=0) {fprintf (stderr, "Error in cuser_form.c, myform_eval: negative argument of log!\n");return 1;}
  40. float logdist = log(feat[0]);
  41. float delta = logdist-mean;
  42. float delta1 = logdist-mean1;
  43. float arg = delta*delta/(2.*stdev*stdev);//printf("*x: %8.4f\n",arg);
  44. float arg1= delta1*delta1/(2.*stdev1*stdev1);//printf("*y: %8.4f\n",arg1);
  45. if (arg < arg1) {
  46. *val = RT * (log(feat[0]) + arg - log(w/stdev + w1/stdev1 * exp(arg-arg1)));
  47. }
  48. else{
  49. *val = RT * (log(feat[0]) + arg1 - log(w1/stdev1 + w/stdev * exp(arg1-arg)));
  50. }
  51. if (deriv) {
  52. if (arg < arg1) {
  53. fderv[0] = RT * (1./feat[0] * (1.+ ( w/(stdev*stdev*stdev)*delta + (w1)/(stdev1*stdev1*stdev1) * delta1 * exp(arg-arg1) ) / (w/stdev + (w1)/stdev1 * exp(arg-arg1)) ) );
  54. }
  55. else{
  56. fderv[0] = RT * (1./feat[0] * (1.+ ( w/(stdev*stdev*stdev)*delta*exp(arg1-arg) + (w1)/(stdev1*stdev1*stdev1) * delta1) / (w/stdev * exp(arg1-arg) + (w1)/stdev1) ) );
  57. }
  58. }
  59. return 0;
  60. }
  61. // 1 ///////////////////////////////////
  62. /* Evaluate the minimum violation*/
  63. ///////////////////////////////////
  64. static int myform_vmin(void *data, const float *feat, const int *iftyp,
  65. const int *modal, int n_feat, const float *pcsr,
  66. int n_pcsr, float *val)
  67. {
  68. float w, mean, stdev, w1, mean1, stdev1;
  69. get_param(pcsr, &w, &mean, &stdev, &w1, &mean1, &stdev1);
  70. if (feat[0]<=0) {fprintf (stderr, "Error in cuser_form.c, negative argument of log!\n");return 1;}
  71. //search nearest minimum
  72. float x = exp(mean-stdev*stdev);
  73. float x1 = exp(mean1-stdev1*stdev1);
  74. if (abs(x-feat[0]) <= abs(x1-feat[0])) {*val = feat[0]-x;}
  75. else{*val=feat[0]-x1;}
  76. return 0;
  77. }
  78. // 2 /////////////////////////////////
  79. /* Evaluate the heavy violation*/
  80. /////////////////////////////////
  81. static int myform_vheavy(void *data, const float *feat, const int *iftyp,
  82. const int *modal, int n_feat, const float *pcsr,
  83. int n_pcsr, float *val)
  84. {
  85. float w, mean, stdev, w1, mean1, stdev1;
  86. get_param(pcsr, &w, &mean, &stdev, &w1, &mean1, &stdev1);
  87. if (feat[0]<=0) {fprintf (stderr, "Error in cuser_form.c, negative argument of log!\n");return 1;}
  88. //search global minimum
  89. float x = exp(mean-stdev*stdev);
  90. float x1 = exp(mean1-stdev1*stdev1);
  91. float max = w*get_p(stdev,x,x-mean) + w1*get_p(stdev1,x,x-mean1);
  92. float max1 = w*get_p(stdev,x1,x1-mean) + w1*get_p(stdev1,x1,x1-mean1);
  93. if (max>=max1){*val = feat[0]-x;}
  94. else {*val = feat[0]-x1;}
  95. return 0;
  96. }
  97. // 3 ////////////////////////////////////////////
  98. /* Evaluate the relative minimum violation*/
  99. ////////////////////////////////////////////
  100. static int myform_rvmin(void *data, const float *feat, const int *iftyp,
  101. const int *modal, int n_feat, const float *pcsr,
  102. int n_pcsr, float *val)
  103. {
  104. float w, mean, stdev, w1, mean1, stdev1;
  105. get_param(pcsr, &w, &mean, &stdev, &w1, &mean1, &stdev1);
  106. if (feat[0]<=0) {fprintf (stderr, "Error in cuser_form.c, negative argument of log!\n");return 1;}
  107. float logdist = log(feat[0]);
  108. //search nearest minimum & according stdev
  109. float x = exp(mean-stdev*stdev);
  110. float x1 = exp(mean1-stdev1*stdev1);
  111. if (abs(x-feat[0]) <= abs(x1-feat[0])) {*val = (logdist-mean)/stdev;}
  112. else{*val = (logdist-mean1)/stdev1;}
  113. return 0;
  114. }
  115. // 4 //////////////////////////////////////////
  116. /* Evaluate the relative heavy violation*/
  117. //////////////////////////////////////////
  118. static int myform_rvheavy(void *data, const float *feat, const int *iftyp,
  119. const int *modal, int n_feat, const float *pcsr,
  120. int n_pcsr, float *val)
  121. {
  122. float w, mean, stdev, w1, mean1, stdev1;
  123. get_param(pcsr, &w, &mean, &stdev, &w1, &mean1, &stdev1);
  124. if (feat[0]<=0) {fprintf (stderr, "Error in cuser_form.c, negative argument of log!\n");return 1;}
  125. float logdist = log(feat[0]);
  126. //search global minimum & according stdev
  127. float x = exp(mean-stdev*stdev);
  128. float x1 = exp(mean1-stdev1*stdev1);
  129. float max = w*get_p(stdev,x,x-mean) + w1*get_p(stdev1,x,x-mean1);
  130. float max1 = w*get_p(stdev,x1,x1-mean) + w1*get_p(stdev1,x1,x1-mean1);
  131. if (max>=max1){*val = (logdist-mean)/stdev;}
  132. else{*val = (logdist-mean1)/stdev1;}
  133. return 0;
  134. }
  135. //////////////////////////////////////
  136. /* Evaluate the minimum feature mean*/
  137. //////////////////////////////////////
  138. static int myform_minmean(void *data, const float *feat, const int *iftyp,
  139. const int *modal, int n_feat, const float *pcsr,
  140. int n_pcsr, float *val)
  141. {
  142. float w, mean, stdev, w1, mean1, stdev1;
  143. get_param(pcsr, &w, &mean, &stdev, &w1, &mean1, &stdev1);
  144. if (feat[0]<=0) {fprintf (stderr, "Error in cuser_form.c, negative argument of log!\n");return 1;}
  145. //search nearest minimum
  146. float x = exp(mean-stdev*stdev);
  147. float x1 = exp(mean1-stdev1*stdev1);
  148. if (abs(x-feat[0]) <= abs(x1-feat[0])) {*val = x;}
  149. else{*val=x1;}
  150. return 0;
  151. }
  152. ////////////////////////////////////
  153. /* Evaluate the heavy feature mean*/
  154. ////////////////////////////////////
  155. static int myform_heavymean(void *data, const float *feat, const int *iftyp,
  156. const int *modal, int n_feat, const float *pcsr,
  157. int n_pcsr, float *val)
  158. {
  159. float w, mean, stdev, w1, mean1, stdev1;
  160. get_param(pcsr, &w, &mean, &stdev, &w1, &mean1, &stdev1);
  161. if (feat[0]<=0) {fprintf (stderr, "Error in cuser_form.c, negative argument of log!\n");return 1;}
  162. //search global minimum
  163. float x = exp(mean-stdev*stdev);
  164. float x1 = exp(mean1-stdev1*stdev1);
  165. float max = w*get_p(stdev,x,x-mean) + w1*get_p(stdev1,x,x-mean1);
  166. float max1 = w*get_p(stdev,x1,x1-mean) + w1*get_p(stdev1,x1,x1-mean1);
  167. if (max>=max1){*val = x;}
  168. else {*val = x1;}
  169. return 0;
  170. }
  171. ////////////////////////////////////////////////////
  172. /* Create the new form, and return its identifier */
  173. ////////////////////////////////////////////////////
  174. int myform_create(void)
  175. {
  176. return mod_user_form_new(myform_eval, NULL, myform_vmin, NULL, myform_vheavy,
  177. NULL, myform_rvmin, NULL, myform_rvheavy, NULL,
  178. myform_minmean, NULL, myform_heavymean, NULL);
  179. }
  180. /*
  181. int myform_create(void)
  182. {
  183. return mod_user_form_new(myform_eval, NULL, myform_vheavy, NULL, myform_vheavy,
  184. NULL, myform_rvheavy, NULL, myform_rvheavy, NULL,
  185. myform_heavymean, NULL, myform_heavymean, NULL);
  186. }
  187. */