home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / biology / gsrc208a.zip / REACTION.C < prev    next >
C/C++ Source or Header  |  1992-12-15  |  5KB  |  204 lines

  1. #include "copyleft.h"
  2.  
  3. /*
  4.     GEPASI - a simulator of metabolic pathways and other dynamical systems
  5.     Copyright (C) 1989, 1992  Pedro Mendes
  6. */
  7.  
  8. /*************************************/
  9. /*                                   */
  10. /*       reaction scheme parser      */
  11. /*                                   */
  12. /*           QuickC/WIN 1.0          */
  13. /*                                   */
  14. /*   (include here compilers that    */
  15. /*   compiled GEPASI successfully)   */
  16. /*                                   */
  17. /*************************************/
  18.  
  19.  
  20. /*
  21.  
  22.   Reaction scheme:
  23.  
  24.    <side> <connector> <side>
  25.  
  26.   Side:
  27.  
  28.    <element> [ [ '+' <element> ]  '+' <element> ]
  29.  
  30.   Element:
  31.  
  32.    [ <number> '*' ] <identifier>
  33.  
  34.   Connector:
  35.  
  36.    "->" | '='
  37.  
  38.   Identifier:
  39.  
  40.    string with up to NAME_L ASCII characters 33-126 except '*', '+', '=' or '$'
  41.  
  42. */
  43.  
  44. #include <windows.h>
  45. #include <stdio.h>
  46. #include <string.h>
  47. #include "globals.h"
  48. #include "gep1.h"
  49.  
  50. int element( char *ptrN, int s, int r, int *tm,
  51.              char (huge *mn)[NAME_L], int huge *stoi,
  52.              int (huge *rs)[MAX_STEP][MAX_MOL], int *im  );
  53. int side( char *ptrR, int s, int r, int *tm,
  54.           char (huge *mn)[NAME_L], int huge *stoi,
  55.           int(huge *rs)[MAX_STEP][MAX_MOL], int *im  );
  56. int reaction( char *ptrReac, int r, int *tm,
  57.               char (huge *mn)[NAME_L], int huge *stoi,
  58.               int (huge *rs)[MAX_STEP][MAX_MOL], int *im,
  59.               unsigned char *rvr );
  60.  
  61. #pragma alloc_text( CODE7, element, side, reaction )
  62.  
  63. int element( char *ptrN, int s, int r, int *tm,
  64.              char (huge *mn)[NAME_L], int huge *stoi,
  65.              int (huge *rs)[MAX_STEP][MAX_MOL], int *im  )
  66. {
  67.  char *ptrS, *ptrIni, *ptrB;
  68.  int i, j, k, st, fl, intm;
  69.  
  70.  /* get the soicheiometry coefficient or assign it to 1 if none    */
  71.  if( strchr( ptrN, '*' ) != NULL )
  72.  {
  73.   sscanf( ptrN, "%d*", &st);
  74.   ptrB = strchr(ptrN, '*' )+1;
  75.  }
  76.  else
  77.  {
  78.   st = 1;
  79.   ptrB = ptrN;
  80.  }
  81.  
  82.  /* if the soicheiometry coefficient is 0 signal error and ret.    */
  83.  if( !st ) return -1;
  84.  /* or if the rest of the string is empty do the same            */
  85.  if( *ptrB=='\0' ) return -1;
  86.  /* if there are more '*' in the string do the same again!        */
  87.  if( strchr( ptrB, '*' ) != NULL ) return -1;
  88.  
  89.  /* check if metabolite has been flagged with a '$'                */
  90.  if( ( *ptrB == '$' ) )
  91.  {
  92.   /* if so point past of it ...                                    */
  93.   ptrB++;
  94.   /* and signal that it is a external metabolite                */
  95.   intm = 0;
  96.  }
  97.  /* if not, this is a internal metabolite                        */
  98.  else intm = 1;
  99.  
  100.  /* if metabolite name is longer than max. allowed, truncate it    */
  101.  if( lstrlen( ptrB ) > NAME_L - 1 )
  102.  {
  103.   ptrB[NAME_L-1] = '\0';
  104.   fl = 1;
  105.  }
  106.  else fl =0;
  107.  
  108.  /* is this metabolite already registered?                        */
  109.  for( i=0; i<*tm; i++)
  110.   if( ! lstrcmp( &mn[i][0], ptrB ) ) break;
  111.  /* if not, do it and increase the number of total metabolites    */
  112.  if( i==*tm )
  113.  {
  114.   /* first check if we reached the maximum MAX_MET                */
  115.   if(*tm == MAX_MET) return -2;
  116.   lstrcpy( &mn[i][0], ptrB );
  117.   (*tm)++;
  118.  }
  119.  /* if this has been marked as external ignore other claims!    */
  120.  if ( ! intm ) im[i] = intm;
  121.  /* store the stoicheiometry coefficient                        */
  122.  stoi[i*MAX_MET + r] +=  s*st;
  123.  /* look for the first empty position of rs[r]                    */
  124.  for(j=0; (j<MAX_MOL) && ((*rs)[r][j]!=0) ; j++);
  125.  /* now assign it and the following to the relevant metabolite    */
  126.  for(k=st; k>0; k--, j++)
  127.   (*rs)[r][j] = s*(i+1);
  128.  /* if string has been truncated, overwrite the '\0' w/ anything*/
  129.  if( fl ) ptrB[NAME_L-1] = '~';
  130.  return 0;
  131. }
  132.  
  133. int side( char *ptrR, int s, int r, int *tm,
  134.           char (huge *mn)[NAME_L], int huge *stoi,
  135.           int(huge *rs)[MAX_STEP][MAX_MOL], int *im  )
  136. {
  137.  int i,len,fl;
  138.  char *ptrIni, *ptrId;
  139.  
  140.  len = strlen( ptrR );
  141.  ptrIni = ptrR;
  142.  if( *ptrIni == '+' ) return -1;
  143.  for (i=0;;i++)
  144.  {
  145.   ptrId = strtok( ptrR, "+" );
  146.   if( ptrId == NULL ) break;
  147.   if( *ptrId == '\0' ) return -1;
  148.   if( fl = element( ptrId, s, r, tm, mn, stoi, rs, im ) ) return fl;
  149.   do ptrR++; while (*ptrR != '\0');
  150.   ptrR++;
  151.   if( (int)( ptrR - ptrIni ) > len ) break;
  152.   if( ( *ptrR == '\0' ) ) return -1;
  153.  }
  154.  return 0;
  155. }
  156.  
  157. int reaction( char *ptrReac, int r, int *tm,
  158.               char (huge *mn)[NAME_L], int huge *stoi,
  159.               int (huge *rs)[MAX_STEP][MAX_MOL], int *im,
  160.               unsigned char *rvr )
  161. {
  162.  int i,len, fl;
  163.  char *ptrEle, *ptr;
  164.  
  165.  
  166.  for(ptr=ptrReac; *ptr!='\0'; ptr++)                    /* get rid of whitespace    */
  167.   if( (*ptr==' ') || (*ptr=='\t') )
  168.   {
  169.    lstrcpy( ptr, ptr+1 );
  170.    ptr--;
  171.   }
  172.  
  173.  len = strlen( ptrReac );
  174.  ptrEle = ptrReac;
  175.  for (i=0;i<len;i++,ptrEle++)                            /*find the separator        */
  176.  {
  177.   if( *ptrEle == '=' )                                    /* that can be a '='        */
  178.   {
  179.    *ptrEle = '\0';
  180.    ptrEle++;
  181.    rvr[r] = 1;
  182.    break;
  183.   }
  184.   if( ( *ptrEle == '-' ) && ( *(ptrEle+1) == '>') )        /* or a "->"                */
  185.   {
  186.    *ptrEle = '\0';
  187.    ptrEle += 2;
  188.    rvr[r] = 0;
  189.    break;
  190.   }
  191.  }
  192.  if( i==len ) return -1;
  193.  if( strchr( ptrEle, '=' ) != NULL ) return -1;
  194.  for ( i=0, ptr=ptrEle; i<len; i++,ptr++ )
  195.   if( ( *ptr == '-' ) && ( *(ptr+1) == '>') ) return -1;
  196.  for( i=0; i<MAX_MET; i++ )
  197.   stoi[i*MAX_MET + r] = 0;                                /* reset this column        */
  198.  for( i=0; i<MAX_MOL; i++ )
  199.   (*rs)[r][i] = 0;
  200.  if( fl = side( ptrReac, -1, r, tm, mn, stoi, rs, im) ) return fl;
  201.  if ( fl = side( ptrEle, 1, r, tm, mn, stoi, rs, im) ) return fl;
  202.  return 0;
  203. }
  204.