cuser_form54.c 7.5 KB

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