home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / biology / gsrc208a.zip / IOSIM.C < prev    next >
C/C++ Source or Header  |  1993-07-14  |  15KB  |  560 lines

  1. #include "copyleft.h"
  2.  
  3. /*
  4.     GEPASI - a simulator of metabolic pathways and other dynamical systems
  5.     Copyright (C) 1989, 1992, 1993  Pedro Mendes
  6. */
  7.  
  8. /*************************************/
  9. /*                                   */
  10. /*         GWSIM - Simulation        */
  11. /*        MS-WINDOWS front end       */
  12. /*                                   */
  13. /*        Simulation files I/O       */
  14. /*                                   */
  15. /*          QuickC/WIN 1.0           */
  16. /*                                   */
  17. /*   (include here compilers that    */
  18. /*   compiled GWSIM successfully)    */
  19. /*                                   */
  20. /*************************************/
  21.  
  22.  
  23. #include <windows.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <io.h>
  28. #include <sys\types.h>
  29. #include <sys\stat.h>
  30. #include "commdlg.h"
  31. #include "globals.h"
  32. #include "iotop.h"
  33. #include "iosim.h"
  34. #include "gep2.h"
  35. #include "simgvar.h"
  36. #include "gaussw.h"
  37. #include "strtbl.h"
  38.  
  39. #pragma alloc_text( CODE13, TopIn, SimToBuf, WriteSim, BufToSim, ReadSim, setdb )
  40.  
  41. /*
  42.     set up several database variables
  43. */
  44. int setdb( int swapnames )
  45. {
  46.  /* set up the strings describing each step                            */
  47.  step_string( );
  48.  
  49.  /* reduce the stoicheiometry matrix                                */
  50.  if( reduce( swapnames ) != 0 )
  51.   return -1;
  52.  
  53.  /* if there are no steps with internal metabolites signal and stop    */
  54.  if( (!nsteps) || (!nmetab) )
  55.   return IDS_ERR_BAD_EMPTY;
  56.  /* setup the parameters structure                                    */
  57.  if( SetParams() == -1 )
  58.   return IDS_ERR_NOEXEC;
  59.  
  60.  /* setup the outpel structure                                        */
  61.  if( SetOutpEl() == -1 )
  62.   return IDS_ERR_NOEXEC;
  63.  
  64.  return 0;
  65. }
  66.  
  67. /*
  68.     read topology file with validity check
  69. */
  70.  
  71. int TopIn( LPSTR tname )
  72. {
  73.  unsigned char  nmod;
  74.  BOOL            badtop;
  75.  int            i, j, nRc;
  76.  
  77.  if( !(nRc = ReadTop( tname ) ) )
  78.  {
  79.   badtop = FALSE;
  80.   if( (nsteps<=0) || (nsteps>MAX_STEP) ||
  81.       (totmet<=0) || (totmet>MAX_MET)    )
  82.    badtop = TRUE;
  83.   /* check if topology is complete                            */
  84.   for( i=0; i<nsteps; i++ )
  85.   {
  86.    if( kinetu[i]==NOT ) badtop = TRUE;
  87.    if( ktype[kinetu[i]].nmodf > 0 )
  88.    {
  89.     /* count the number of assigned modfs for each reaction    */
  90.     for( j=0, nmod=0; j<totmet; j++ )
  91.      if( (*loop)[i][j] != 0 ) nmod++;
  92.     if( ktype[kinetu[i]].nmodf != nmod ) badtop = TRUE;
  93.    }
  94.   }
  95.  
  96.   /* if topology is incomplete, signal and return                */
  97.   if( badtop )
  98.   {
  99.    tname[0] = '\0';
  100.    return IDS_ERR_BAD_TOP;
  101.   }
  102.  
  103.   nRc = setdb( 1 ); /* 1 means swap metabolite and step names    */
  104.  
  105.   /* exit and signal no errors                                */
  106.   return nRc;
  107.  }
  108.  else return IDS_ERR_LOAD;
  109. }
  110.  
  111. /*
  112.     write simulation parameters to file
  113. */
  114.  
  115. void SimToBuf( LPSTR Buff, LPSTR FName )
  116. {
  117.  int i, j;
  118.  char *p;
  119.  char auxstr[260];
  120.  
  121.  /* write the kinetic constants, one line per step                        */
  122.  for( i=0; i<nsteps; i++ )
  123.  {
  124.   for( j=0; j<(int)ktype[kinetu[i]].nconst; j++ )
  125.   {
  126.    lstrcat( Buff, (LPSTR) " " );
  127.    gcvt( *(params[i]+j), 20, auxstr );
  128.    lstrcat( Buff, (LPSTR) auxstr );
  129.   }
  130.   lstrcat( Buff, (LPSTR) "\n" );
  131.  }
  132.  
  133.  /* write the concentrations, one per line                                */
  134.  for( i=0; i<totmet; i++ )
  135.  {
  136.   gcvt( xu[i], 20, auxstr );
  137.   lstrcat( Buff, (LPSTR) auxstr );
  138.   lstrcat( Buff, (LPSTR) "\n" );
  139.  }
  140.  
  141.  /* write the units in one  line                                        */
  142.  wsprintf( (LPSTR) auxstr, "%s %s\n", (LPSTR) options.concu, (LPSTR) options.timeu );
  143.  lstrcat( Buff, (LPSTR) auxstr );
  144.  /* write log, dyn and ss options in one line                            */
  145.  sprintf( auxstr, "%d %d %lu %.20le %.20le %.20le %.20le %d %d %d\n",
  146.           options.debug, options.dyn, options.pfo, options.endtime,
  147.           options.reltol, options.abstol, options.hrcz, options.adams,
  148.           options.bdf, options.ss);
  149.  lstrcat( Buff, (LPSTR) auxstr );
  150.  /* write txt options in one line                                        */
  151.  sprintf( auxstr, "%d %d %d %d %d %d %d\n", options.txt,
  152.           options.structan, options.staban, options.stdela,
  153.           options.nonela, options.stdcc, options.noncc );
  154.  lstrcat( Buff, (LPSTR) auxstr );
  155.  /* write the dat options in one line                                    */
  156.  sprintf( auxstr, "%d %d %d %d %d %d %d %d %d\n", options.dat, options.datsep,
  157.           options.datwidth, options.dattit, options.datmca, options.datss,
  158.           totsel, options.append, options.quotes );
  159.  lstrcat( Buff, (LPSTR) auxstr );
  160.  /* write the dat file title                                            */
  161.  sprintf( auxstr, "%s\n", DatName );
  162.  lstrcat( Buff, (LPSTR) auxstr );
  163.  /* write each output item in one line (index and title)                */
  164.  for( i=0; i<totsel; i++ )
  165.   for( j=0; j<noutpel; j++ )
  166.    if( outpel[j].idx == (unsigned int) (i+1) )
  167.    {
  168.     wsprintf( (LPSTR) auxstr, "%d %s\n", j, outpel[j].title );
  169.     lstrcat( Buff, (LPSTR) auxstr );
  170.    }
  171.  /* write the scan options in one line                                    */
  172.  sprintf( auxstr, "%d %lu %d %d %d\n", options.scan, options.scandens,
  173.           options.scanlog, totscan, nlinks );
  174.  lstrcat( Buff, (LPSTR) auxstr );
  175.  /* write each scan item in one line                                      */
  176.  for( i=0; i<totscan; i++ )
  177.   for( j=0; j<nscanpar; j++ )
  178.    if( spar[j].idx == (i+1) )
  179.    {
  180.     sprintf( auxstr, "%d %.20le %.20le %lu %d\n", j, spar[j].low,
  181.              spar[j].high, spar[j].dens, spar[j].log );
  182.     lstrcat( Buff, (LPSTR) auxstr );
  183.    }
  184.  /* write each linked item in one line                                    */
  185.  for( i=0; i<nlinks; i++ )
  186.   for( j=0; j<nscanpar; j++ )
  187.    if( spar[j].lidx == i )
  188.    {
  189.     sprintf( auxstr, "%d %d %d %20le\n", j, spar[j].linkedto,
  190.              spar[j].operation, spar[j].factor );
  191.     lstrcat( Buff, (LPSTR) auxstr );
  192.    }
  193. }
  194.  
  195.  
  196. int WriteSim( LPSTR FName )
  197. {
  198.  HCURSOR hSaveCursor;
  199.  GLOBALHANDLE hBuff;
  200.  LPSTR Buff;
  201.  int i, j, ch1;
  202.  WORD bufsize;
  203.  OFSTRUCT OfStruct;
  204.  
  205.  /* load the wait cursor                                                */
  206.  hSaveCursor = SetCursor(hHourGlass);
  207.  
  208.  /*bufsize = (WORD) (3*nsteps + (8*nsteps + 6 + NAME_L)*totmet);        */
  209.  bufsize = (WORD) 10240;
  210.  hBuff = GlobalAlloc( GMEM_MOVEABLE | GMEM_ZEROINIT , bufsize );
  211.  Buff = GlobalLock( hBuff );
  212.  /* write the information on the buffer                                    */
  213.  TopToBuf( Buff );
  214.  if( (ch1 = OpenFile( FName, &OfStruct, OF_CREATE | OF_WRITE )) != -1 )
  215.  {
  216.   /* write the buffer to the file and close it                            */
  217.   _lwrite( ch1, (LPSTR) Buff, (WORD) lstrlen( Buff ) );
  218.   ch1 = _lclose( ch1 );
  219.  }
  220.  if( ch1 == -1 )
  221.  {
  222.   GlobalUnlock( hBuff );
  223.   GlobalFree( hBuff );
  224.   SetCursor(hSaveCursor);
  225.   return IDS_ERR_SAVE;
  226.  }
  227.  
  228.  /* set the flags, meaning 'kinetic type not saved' */
  229.  for( i=0; i<nsteps; i++ ) kfl[i] = 1;
  230.  /* check if model contains user-defined kinetic types                    */
  231.  if( nudf>0 )
  232.  {
  233.   for( i=0; i<nsteps; i++ )
  234.    if( (kinetu[i] > MAX_TYP-1) && (kfl[i]) )
  235.    {
  236.     /* write the tree function to the buffer                                */
  237.     TreeToBuf( kinetu[i]-MAX_TYP, (LPSTR) Buff );
  238.     /* reopen the file and write this tree to it                            */
  239.     if( (ch1 = OpenFile( (LPSTR) NULL, &OfStruct, OF_REOPEN | OF_WRITE )) != -1 )
  240.     {
  241.      /* write the buffer to the file and close it                            */
  242.      _lseek( ch1, (LONG) 0, 2 );
  243.      _lwrite( ch1, (LPSTR) Buff, (WORD) lstrlen( Buff ) );
  244.      ch1 = _lclose( ch1 );
  245.     }
  246.     if( ch1 == -1 )
  247.     {
  248.      GlobalUnlock( hBuff );
  249.      GlobalFree( hBuff );
  250.      SetCursor(hSaveCursor);
  251.      return IDS_ERR_SAVE;
  252.     }
  253.     /* mark all other reactions with this type */
  254.     for( j=i; j<nsteps; j++ ) if(kinetu[j]==kinetu[i]) kfl[j] = 0;
  255.    }
  256.  }
  257.  
  258.  
  259.  Buff[0] = '\0';
  260.  SimToBuf( Buff, FName );
  261.  /* append buffer to  the file                                            */
  262.  if( (ch1 = OpenFile( (LPSTR) NULL, &OfStruct, OF_REOPEN | OF_WRITE )) != -1 )
  263.  {
  264.   /* write the buffer to the file and close it                            */
  265.   _lseek( ch1, (LONG) 0, 2 );
  266.   _lwrite( ch1, (LPSTR) Buff, (WORD) lstrlen( Buff ) );
  267.    ch1 = _lclose( ch1 );
  268.  }
  269.  if( ch1 == -1 )
  270.  {
  271.   GlobalUnlock( hBuff );
  272.   GlobalFree( hBuff );
  273.   SetCursor(hSaveCursor);
  274.   return IDS_ERR_SAVE;
  275.  }
  276.  
  277.  /* release the memory block                                            */
  278.  GlobalUnlock( hBuff );
  279.  GlobalFree( hBuff );
  280.  SetCursor(hSaveCursor);
  281.  return 0;
  282. }
  283.  
  284.  
  285. /*
  286.     read buffer with simulation parameters and store them
  287. */
  288.  
  289. int BufToSim( char *Buff )
  290. {
  291.  int i, j, log, lkt, op;
  292.  unsigned long dens;
  293.  unsigned u_dummy;
  294.  double faux, low, high, fac;
  295.  char structfile[128], auxstr[128];
  296.  int nRc;
  297.  struct stat fst;
  298.  
  299.   /* read the kinetic constants from the buffer, one line per step    */
  300.   for(i=0;i<nsteps;i++)
  301.   {
  302.    Buff = strchr( Buff, '\n' );
  303.    if( Buff != NULL ) Buff++;
  304.    else return IDS_ERR_BAD_KPARAM;
  305.    for( j=0; j<(int)ktype[kinetu[i]].nconst; j++ )
  306.    {
  307.     Buff = strchr( Buff, ' ' );
  308.     for( ;*Buff == ' '; Buff++);
  309.     if ( (Buff == NULL) || (sscanf( Buff,"%le", &faux ) < 1) )
  310.      return IDS_ERR_BAD_KPARAM;
  311.     *(params[i]+j) = faux;
  312.    }
  313.   }
  314.  
  315.   /* read the concentrations, one per line                            */
  316.   for(i=0;i<totmet;i++)
  317.   {
  318.    Buff = strchr( Buff, '\n' );
  319.    if( Buff != NULL ) Buff++;
  320.    else return IDS_ERR_BAD_CONCENT;
  321.    if ( (Buff == NULL) || (sscanf( Buff,"%le", &faux ) < 1) )
  322.     return IDS_ERR_BAD_CONCENT;
  323.    xu[i] = faux;
  324.   }
  325.   /* read the units in one line                                            */
  326.   Buff = strchr( Buff, '\n' );
  327.   if( Buff != NULL )
  328.    Buff++;
  329.   if ( (Buff == NULL) || (sscanf( Buff,"%s %s", &options.concu, &options.timeu ) < 2) )
  330.    return IDS_ERR_BAD_UNITS;
  331.   /* read log, dyn and ss options in one line                            */
  332.   Buff = strchr( Buff, '\n' );
  333.   if( Buff != NULL )
  334.    Buff++;
  335.   if ( (Buff == NULL) || (sscanf( Buff,"%d %d %ld %le %le %le %le %d %d %d",
  336.           &options.debug, &options.dyn, &options.pfo, &options.endtime,
  337.           &options.reltol, &options.abstol, &options.hrcz, &options.adams,
  338.           &options.bdf, &options.ss ) < 10 ) )
  339.    return IDS_ERR_BAD_OPTIONS;
  340.   /* read txt options in one line                                        */
  341.   Buff = strchr( Buff, '\n' );
  342.   if( Buff != NULL )
  343.    Buff++;
  344.   if ( (Buff == NULL) || (sscanf( Buff,"%d %d %d %d %d %d %d",
  345.           &options.txt, &options.structan, &options.staban,
  346.           &options.stdela, &options.nonela, &options.stdcc,
  347.           &options.noncc ) < 7 ) )
  348.    return IDS_ERR_BAD_OPTIONS;
  349.   /* read the dat options in one line                                    */
  350.   Buff = strchr( Buff, '\n' );
  351.   if( Buff != NULL )
  352.    Buff++;
  353.   if ( (Buff == NULL) || (sscanf( Buff,"%d %d %d %d %d %d %d %d %d",
  354.         &options.dat, &options.datsep, &options.datwidth,
  355.         &options.dattit, &options.datmca, &options.datss,
  356.         &totsel, &options.append, &options.quotes ) < 9 ) )
  357.    return IDS_ERR_BAD_OPTIONS;
  358.   /* read the dat filename in one line                                    */
  359.   Buff = strchr( Buff, '\n' );
  360.   if( Buff != NULL )
  361.    Buff++;
  362.   if ( (Buff == NULL) || (sscanf( Buff,"%s", DatName ) != 1 ) )
  363.    return IDS_ERR_BAD_OPTIONS;
  364.  
  365.   /* read each output item in one line (index only)                        */
  366.   for( i=0; i<totsel; i++ )
  367.   {
  368.    Buff = strchr( Buff, '\n' );
  369.    if( Buff != NULL )
  370.     Buff++;
  371.    if ( (Buff == NULL) || (sscanf( Buff,"%d", &j ) < 1 ) )
  372.     return IDS_ERR_BAD_OPTIONS;
  373.    if( ver_no < (float) 2.05 ) outpel[j+totmet].idx = i+1;
  374.    else outpel[j].idx = i+1;
  375.   }
  376.  
  377.  /* read the scan options in one line                                    */
  378.   Buff = strchr( Buff, '\n' );
  379.   if( Buff != NULL )
  380.    Buff++;
  381.   if ( (Buff == NULL) || (sscanf( Buff, "%d %lu %d %d %d", &options.scan,
  382.                           &options.scandens, &options.scanlog,
  383.                           &totscan, &nlinks ) < 5 ) )
  384.    return IDS_ERR_BAD_OPTIONS;
  385.  
  386.  
  387.  /* read each scan item in one line                 */
  388.   for( i=0; i<totscan; i++ )
  389.   {
  390.    Buff = strchr( Buff, '\n' );
  391.    if( Buff != NULL )
  392.     Buff++;
  393.    if ( (Buff == NULL) || (sscanf( Buff,"%d %le %le %lu %d", &j,
  394.                                    &low, &high, &dens, &log ) < 5 ) )
  395.     return IDS_ERR_BAD_OPTIONS;
  396.    spar[j].idx  = i+1;
  397.    spar[j].low  = low;
  398.    spar[j].high = high;
  399.    spar[j].dens = dens;
  400.    spar[j].log  = log;
  401.   }
  402.  
  403.  /* read each linked item in one line                 */
  404.   for( i=0; i<nlinks; i++ )
  405.   {
  406.    Buff = strchr( Buff, '\n' );
  407.    if( Buff != NULL )
  408.     Buff++;
  409.    if ( (Buff == NULL) || (sscanf( Buff,"%d %d %d %le", &j, &lkt, &op,
  410.                                    &fac ) < 4 ) )
  411.     return IDS_ERR_BAD_OPTIONS;
  412.    spar[j].lidx  = i;
  413.    spar[j].linkedto  = lkt;
  414.    spar[j].operation = op;
  415.    spar[j].factor = fac;
  416.   }
  417.  return 0;
  418. }
  419.  
  420.  
  421. /*
  422.     read simulation parameters from file
  423. */
  424.  
  425. int ReadSim( LPSTR FName )
  426. {
  427.  HCURSOR hSaveCursor;
  428.  HANDLE hBuff;
  429.  unsigned int bufsize;
  430.  char *Buff;
  431.  int i, j, k, fl, ty, ch1, nRc;
  432.  OFSTRUCT OfStruct;
  433.  struct stat fst;
  434.  WORD sizetr;
  435.  
  436.  /* display the wait cursor                                            */
  437.  hSaveCursor = SetCursor(hHourGlass);
  438.  
  439.  /* open the file                                                    */
  440.  if( (ch1 = OpenFile( FName, &OfStruct, OF_READ )) != -1 )
  441.  {
  442.   /* get the file statistics                                        */
  443.   fstat( ch1, &fst );
  444.  
  445.   /* set the buffer to the size of the file plus one                */
  446.   bufsize = (unsigned int) fst.st_size;
  447.  
  448.   /* allocate space for the read buffer and lock it                    */
  449.   hBuff = LocalAlloc( GMEM_MOVEABLE | GMEM_ZEROINIT, (WORD) (bufsize+1) );
  450.   if( hBuff == NULL )
  451.   {
  452.    SetCursor(hSaveCursor);
  453.    return IDS_ERR_LOAD;
  454.   }
  455.   Buff = (char *) LocalLock( hBuff );
  456.  
  457.   /*read the file and close it                                        */
  458.   if( read( ch1, Buff, bufsize ) == -1 )
  459.   {
  460.    LocalUnlock( hBuff );
  461.    LocalFree( hBuff );
  462.    SetCursor(hSaveCursor);
  463.    return IDS_ERR_LOAD;
  464.   }
  465.   if( close( ch1 ) == -1 )
  466.   {
  467.    LocalUnlock( hBuff );
  468.    LocalFree( hBuff );
  469.    SetCursor(hSaveCursor);
  470.    return IDS_ERR_LOAD;
  471.   }
  472.  
  473.   /* set the number of user-def functions to zero                    */
  474.   nudf = 0;
  475.   nrateq = MAX_TYP;
  476.   sizetr = 4;
  477.   GlobalUnlock( hTreeStr );
  478.   GlobalFree( hTreeStr );
  479.   hTreeStr = GlobalAlloc( GMEM_ZEROINIT, sizetr );
  480.   if( hTreeStr == NULL )
  481.   {
  482.    LocalUnlock( hBuff );
  483.    LocalFree( hBuff );
  484.    SetCursor(hSaveCursor);
  485.    return IDS_ERR_NOEXEC;
  486.   }
  487.   treestr = GlobalLock( hTreeStr );
  488.   treeptr = treestr;
  489.  
  490.   /* read the topology section                                         */
  491.   if( nRc = BufToTop( Buff, &Buff ) )
  492.   {
  493.    LocalUnlock( hBuff );
  494.    LocalFree( hBuff );
  495.    SetCursor(hSaveCursor);
  496.    return nRc;
  497.   }
  498.  
  499.   for( i=0; i<nsteps; i++ ) kfl[i] = 1;
  500.  
  501.   /* check if model contains user-defined kinetic types                */
  502.   /* if there are any, check if they have to be read                */
  503.   for( i=0; i<nsteps; i++ )
  504.    if( (kinetu[i] >= MAX_TYP) && (kfl[i]) )
  505.    {
  506.     /* look for a form-feed                                            */
  507.     Buff = strchr( Buff, '\f' );
  508.     Buff = BufToTree( ++Buff );
  509.     for( j=0, ty=-1; j<nudf; j++ )
  510.      if( lstrcmp( (LPSTR) tree[j].descr, (LPSTR) tr.descr ) == 0 )
  511.      {
  512.       ty = j;
  513.       break;
  514.      }
  515.     if( ty == -1 )
  516.     {
  517.      if( (nRc=addtr( i )) !=0 )
  518.      {
  519.       LocalUnlock( hBuff );
  520.       LocalFree( hBuff );
  521.       SetCursor(hSaveCursor);
  522.       return nRc;
  523.      }
  524.      fl = 1;
  525.     }
  526.     else
  527.      for( j=i,k=kinetu[i]; j<nsteps; j++ )
  528.       if( (kinetu[j]==k) && (kfl[j]) )
  529.       {
  530.        kinetu[j] = MAX_TYP+ty;
  531.        kfl[j] = 0;
  532.       }
  533.    }
  534.  
  535.   /* setup the relevant databases                                    */
  536.   if( nRc = setdb( 0 ) )
  537.   {
  538.    LocalUnlock( hBuff );
  539.    LocalFree( hBuff );
  540.    SetCursor(hSaveCursor);
  541.    return nRc;
  542.   }
  543.  
  544.  
  545.   /* read the simulation section                                         */
  546.   nRc = BufToSim( Buff );
  547.  
  548.   LocalUnlock( hBuff );
  549.   LocalFree( hBuff );
  550.   SetCursor(hSaveCursor);
  551.   return nRc;
  552.  }
  553.  else
  554.  {
  555.   SetCursor(hSaveCursor);
  556.   return IDS_ERR_LOAD;
  557.  }
  558. }
  559.  
  560.