cuser_form52.c 7.2 KB

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