Content-Type: text/html ../example/scoreawt.c
   1: #ifndef REAL 
   2: #define REAL double 
   3: #endif 
   4: #include <stdio.h> 
   5: #include <string.h> 
   6: #include <stdlib.h> 
   7: #include <math.h> 
   8: #include <sys/types.h> 
   9: #include <malloc.h> 
  10: #include "cx.h" 
  11: #include "cx_molecule.h" 
  12: #include "cx_message.h" 
  13: #include "utils_cx.h" 
  14: #include "dockpublic.h"  
  15: #include "ErrorFunction.h" 
  16: #include "ErrorFunctionUtils.h" 
  17: #include "vec.h" 
  18: /*  Define the internal structure for the score function structure.
  19:  *  This structure is created in UserSetupSite and returned to DockIt as
  19:  *  a (void *).  It gets treated in DockIt as an opaque object pointer and
  19:  *  is passed back in all the calls to functions in this file.
  19:  */
  20: 
  21: struct _Atom {
  22:   REAL awt;
  23: };
  24: 
  25: struct _ScoreInfo {
  26:   vec atomvec;
  27: };
  28: 
  29: /** Process ligand after it is read in.
  30:   This function is called once per ligand after the ligand structure is
  30:   read in.  It should process the ligand in any way needed to set up
  30:   the appropriate structures and parameters for the scoring function.
  30: 
  30:   \param nsiteatoms    Number of site atoms within the site cutoff 
  30:   \param nligandatoms  Number of ligand atoms.   
  30:   \param S             Pointer to internal scoring structure
  30:   \return              0 if success, 1 if error. 
  30: */
  31: int UserProcessLigand (int nsiteatoms ,int nligandatoms ,void *S )
  32: {
  33:   struct _ScoreInfo * Score = (struct _ScoreInfo *)S;
  34:   return 0;
  35: }
  36: 
  37: /** Do any necessary cleanup after ligand is finished 
  38:   This function is called once per ligand after all docking is
  38:   completed for that ligand.  It can be used to release structures and
  38:   clean up on a ligand by ligand basis.
  38: 
  38:   \param S  Pointer to internal scoring structure
  38: */
  39: void UserLigandCleanup (void *S )
  40: {
  41:   struct _ScoreInfo * Score = (struct _ScoreInfo *)S;
  42:   int i;
  43:   for (i=0;i<VECLEN(Score->atomvec);i++)
  44:     free(VECDATA(Score->atomvec)[i]);
  45:   freevec(Score->atomvec);
  46: }
  47: 
  48: /** Set up site description. 
  49:   This function is called once before any site atoms are read in.  It can
  49:   be used to set up an internal structure for use in the scoring function.
  49:   A pointer to this structure is returned from this function and is passed
  49:   to the other functions in this API as an opaque object pointer (void *) 
  49:   by DockIt.  This structure can be used to store any site and ligand 
  49:   information which is needed for the scoring function.  It is released
  49:   by UserCleanup as DockIt terminates.  Options specific for the scoring
  49:   function can be picked up from the cxoptions object.
  49: 
  49:   \param cxoptions      CEX object with options for program
  49:   \param cxsite         CEX molecule object for site
  49: */
  50: /* 
  51: */
  52: void * UserSetupSite (cx_Object cxoptions ,cx_Object cxsite )
  53: {
  54:   struct _ScoreInfo * Score;
  55:   char *func = "UserSetupSite";
  56:   /* Here is where to allocate _Score structure */
  57:   Score = malloc(sizeof(*Score));
  58:   if (Score == NULL) {
  59:     /* Example of how to do error messages in CEX */
  60:     cx_error_save("Cannot make ScoreInfo structure", CX_ERR_ERROR, func);
  61:     ErrorSpew();
  62:     exit(1);
  63:   }
  64:   /* Initialize site structures in Score */
  65: 
  66:   /* and return Score */
  67:   return Score;
  68: }
  69: 
  70: /** Process each site atom. 
  71:   This routine is called once per site atom.  It can be used to process
  71:   each atom (i.e. to assign atom types) and to store any required atomic
  71:   information in the S struct.
  71: 
  71:   \param atom   CEX object for atom
  71:   \param cxsite CEX object for site
  71:   \param S      Pointer to internal scoring function structure
  71:   \return 1 on error (atom cannot be processed and should be ignored) or 0 on success
  71: */
  72: int UserSiteAtomType (cx_Object atom ,cx_Object cxsite, void *S )
  73: {
  74:   struct _ScoreInfo * Score = (struct _ScoreInfo *)S;
  75:   /* Get per atom information from atom CEX object */
  76:   return 0;
  77: }
  78: 
  79: /** Prepare for ligand input 
  80:   This function is called once per ligand before the molecule object is
  80:   interpreted.  It can by used to set up any necessary strucutures or
  80:   check for parameters, etc.
  80: 
  80:   \param cxmol  CEX molecule object
  80:   \param S  Pointer to internal scoring function structure
  80:   \return 0 if success, 1 if fatal error, 2 if skip this ligand.
  80: */
  81: int UserSetupLigand (cx_Object cxmol ,void *S )
  82: {
  83:   struct _ScoreInfo * Score = (struct _ScoreInfo *)S;
  84:   /* Allocate the ligand atoms vector */
  85:   Score->atomvec = makevec(200);
  86:   if (Score->atomvec)
  87:     return 0;
  88:   else
  89:     return 1;
  90: }
  91: 
  92: /** Process each ligand atom. 
  93:   This routine is called once per ligand atom.  It can be used to process
  93:   each atom (i.e. to assign atom types) and to store any required atomic
  93:   information in the S struct.
  93: 
  93:   \param atom   CEX object for atom
  93:   \param cxsite CEX object for site
  93:   \param S      Pointer to internal scoring function structure
  93:   \return 1 on error (atom cannot be processed and should be ignored) or 0 on success
  93: */
  94: /* 
  95: */
  96: int UserLigandAtomType (cx_Object atom ,cx_Object cxligand, void *S )
  97: {
  98:   struct _ScoreInfo * Score = (struct _ScoreInfo *)S;
  99:   double awt;
 100:   char *awtype;
 101:   struct _Atom *atomrec;
 102:   awtype = atuplename(cxligand, "awtype");
 103:   awt = cx_rprop(atom,awtype);
 104:   atomrec = malloc(sizeof(*atomrec));
 105:   if (!atom) return 1;
 106:   atomrec->awt = awt;
 107:   /* pushvec takes care of allocating more memory if necessary */
 108:   pushvec(Score->atomvec,atomrec);
 109:   return 0;
 110: }
 111: 
 112: /** User defined scoring function 
 113:   This function is called to evaluate the user defined score for a given
 113:   ligand conformation.  
 113: 
 113:   \param D      Public interface to Dock molecular information. See dockpublic.h
 113:   \param S      Pointer to scoring function stucture
 113:   \param nv     Number of degrees of freedom
 113:   \param xyzw   Coordinates
 113:   \param g      Gradients (returned)
 113:   \param weight Factor by which to weight this function
 113:   \return Calculated score
 113: */
 114: /* 
 115: */
 116: REAL energy( void *D, void *S, int nv, REAL *xyzw, REAL *g, REAL weight,
 117: 	     int rescore)
 118: {
 119:   struct _ScoreInfo * Score = (struct _ScoreInfo *)S;
 120:   int nligand_atoms, ncons;
 121:   int last;
 122:   int i,j,k;
 123:   double xi,yi,zi,xj,yj,zj;
 124:   double dist2;
 125:   double err;
 126: 
 127:   nligand_atoms = nv/4;
 128:   /* For each ligand atom */
 129:   err = 0.0;
 130:   for (i = 0;i < nligand_atoms; i++) {
 131:     /* get the ligand atom positions */
 132:     xi = X(i);
 133:     yi = Y(i);
 134:     zi = Z(i);
 135:     /* if !rescoring, then use the nearest neighbors list */
 136:     /* Using near neighbors list is optional if you don't want it */
 137:     if (!rescore)
 138:       last = Nneighbors(D,i);
 139:     else
 140:       last = NSiteAtoms(D);
 141:     
 142:     for (k = 0; k < last; k++) {
 143:       if (!rescore)
 144: 	j = Neighbor(D,i,k);
 145:       else
 146: 	j = k;
 147:       /* if this atom pair is not involved in a constraint, then score it */
 148:       if (ncons == 0 || !inConstraint(D, i, j)) {
 149: 	/* get the site atom coordinates */
 150: 	xj =Sitex(D,j);
 151: 	yj =Sitey(D,j);
 152: 	zj =Sitez(D,j);
 153: 	dist2 = (xi-xj)*(xi-xj) + (yi-yj)*(yi-yj) + (zi-zj)*(zi-zj);
 154: 	if (dist2 >= 6.25 && dist2 <= 16) {
 155: 	  err += ((struct _Atom *)VECDATA(Score->atomvec)[i])->awt;
 156: 	  break;
 157: 	}
 158:       }
 159:     }
 160:     GX(i) = 0.0;
 161:     GY(i) = 0.0;
 162:     GZ(i) = 0.0;
 163:   }
 164:   return weight*err;
 165: }
 166: 
 167: 
 168: /** Clean up all internal scoring function structures and release memory 
 169:   This function is called once as DockIt is exiting to clean up memory
 169:   allocated in S.
 169: 
 169:   \param S      Pointer to internal scoring function structure
 169: */
 170: void UserCleanup (void *S )
 171: {
 172:   struct _ScoreInfo * Score = (struct _ScoreInfo *)S;
 173:   /* Free any heap based structures allocated in S and then free S */
 174:   free(S);
 175: }
 176: