Public paste
Undefined
By: Guest | Date: Jan 14 2010 16:02 | Format: None | Expires: never | Size: 16.1 KB | Hits: 977

  1. #include "fileIO.h"
  2.  
  3. int             dumpComplexListToFile( const char *fn, complexList *data )
  4. {
  5.         int a;
  6.         FILE *file;
  7.        
  8.         file = fopen( fn, "w" );
  9.         if ( file == NULL ) {
  10.                 printf( "Error: Could not open filen" );
  11.                 return 1;
  12.         } else {
  13.                 fprintf( file, "%.11f", data->values[0][0] );
  14.                 for ( a = 1; a < data->length; a++ )
  15.                 {
  16.                         fprintf( file, ", %.11f", data->values[a][0] );
  17.                 }
  18.                 fprintf( file, "n" );
  19.                 fprintf( file, "%.11f", data->values[0][1] );
  20.                 for ( a = 1; a < data->length; a++ )
  21.                 {
  22.                         fprintf( file, ", %.11f", data->values[a][1] );
  23.                 }
  24.                 fprintf( file, "n" );
  25.                 fclose( file );
  26.                 return 0;
  27.         }
  28. }
  29.  
  30. int             dumpSimpleListToFile( const char *fn, simpleList *data )
  31. {
  32.         int a;
  33.         FILE *file;
  34.        
  35.         file = fopen( fn, "w" );
  36.         if ( file == NULL ) {
  37.                 printf( "Error: Could not open filen" );
  38.                 return 1;
  39.         } else {
  40.                 fprintf( file, "%.15f", data->values[0] );
  41.                 for ( a = 1; a < data->length; a++ )
  42.                 {
  43.                         fprintf( file, ", %.15f", data->values[a] );
  44.                 }
  45.                 fclose( file );
  46.                 return 0;
  47.         }
  48. }
  49.  
  50. int             dumpVectorToFile( const char *fn, float *data, int signalLength )
  51. {
  52.         int a;
  53.         FILE *file;
  54.        
  55.         file = fopen( fn, "w" );
  56.         if ( file == NULL ) {
  57.                 printf( "Error: Could not open filen" );
  58.                 return 1;
  59.         } else {
  60.                 fprintf( file, "%f", data[0] );
  61.                 for ( a = 1; a < signalLength; a++ )
  62.                 {
  63.                         fprintf( file, ", %f", data[a] );
  64.                 }
  65.                 fclose( file );
  66.                 return 0;
  67.         }
  68. }
  69.  
  70. int             dumpSimpleMatrixToFile( const char *fn, simpleMatrix *data )
  71. {
  72.         int a, b;
  73.         FILE *file;
  74.        
  75.         file = fopen( fn, "w" );
  76.         if ( file == NULL ) {
  77.                 printf( "Error: Could not open filen" );
  78.                 return 1;
  79.         } else {
  80.                 for ( b = 0 ; b < data->dimY; b++ )
  81.                 {
  82.                         fprintf( file, "%.33f", data->values[0+b*data->dimX] );
  83.                         for ( a = 1; a < data->dimX; a++ )
  84.                         {
  85.                                 fprintf( file, ", %.33f", data->values[a+b*data->dimX] );
  86.                         }
  87.                         fprintf( file, "n" );
  88.                 }
  89.                 fclose( file );
  90.                 return 0;
  91.         }
  92. }
  93.  
  94. int             dumpComplexMatrixToFile( const char *fn, complexMatrix *data )
  95. {
  96.         int a, b;
  97.         FILE *file;
  98.        
  99.         file = fopen( fn, "w" );
  100.         if ( file == NULL ) {
  101.                 printf( "Error: Could not open filen" );
  102.                 return 1;
  103.         } else {
  104.                 for ( b = 0 ; b < data->dimY; b++ )
  105.                 {
  106.                         fprintf( file, "%f", data->values[0+b*data->dimX][0] );
  107.                         for ( a = 1; a < data->dimX; a++ )
  108.                         {
  109.                                 fprintf( file, ", %f", data->values[a+b*data->dimX][0] );
  110.                         }
  111.                         fprintf( file, "n" );
  112.                 }
  113.                 for ( b = 0 ; b < data->dimY; b++ )
  114.                 {
  115.                         fprintf( file, "%f", data->values[0+b*data->dimX][1] );
  116.                         for ( a = 1; a < data->dimX; a++ )
  117.                         {
  118.                                 fprintf( file, ", %f", data->values[a+b*data->dimX][1] );
  119.                         }
  120.                         fprintf( file, "n" );
  121.                 }
  122.                 fclose( file );
  123.                 return 0;
  124.         }
  125. }
  126.  
  127.         mrFloat                 readDouble( FILE *handle )
  128.         {
  129.                 double val;
  130.                 fread( &val, sizeof( double ), 1, handle );
  131.                 return ( mrFloat ) val;
  132.         }
  133.  
  134.         void                    writeDouble( double value, FILE *handle )
  135.         {
  136.                 fwrite( &value, sizeof( double ), 1, handle ); 
  137.         }
  138.        
  139.         signed int                      readInt( FILE *handle )
  140.         {
  141.                 signed int val;
  142.                 fread( &val, sizeof( int ), 1, handle );
  143.                 return ( signed int ) val;
  144.         }
  145.  
  146.         void                    writeInt( signed int value, FILE *handle )
  147.         {
  148.                 fwrite( &value, sizeof( signed int ), 1, handle );     
  149.         }
  150.  
  151.         // Read list from file (note that this list must later be freed)
  152.         simpleList              *readList( FILE *handle )
  153.         {
  154.                 int length;
  155.                
  156.                 simpleList *returnList;
  157.                
  158.                 length = fgetc( handle );
  159.                
  160.                 returnList = createList( length );
  161.                
  162.                 for ( int i = 0; i < length; i++ )
  163.                 {
  164.                         returnList->values[ i ] = readDouble( handle );
  165.                 }
  166.                
  167.                 return returnList;
  168.         }
  169.  
  170.         // Write list to file
  171.         void                    writeList( simpleList *list, FILE *handle )
  172.         {
  173.                 fputc( list->length, handle );
  174.                
  175.                 for ( int i = 0; i < list->length; i++ )
  176.                 {
  177.                         writeDouble( (double) list->values[i], handle );
  178.                 }
  179.         }
  180.  
  181.         // Read complex list from file (note that this list must later be freed)
  182.         complexList             *readComplexList( FILE *handle )
  183.         {
  184.                 int length;
  185.                
  186.                 complexList *returnList;
  187.                
  188.                 length = readInt( handle );
  189.                
  190.                 returnList = createComplexList( length );
  191.                
  192.                 for ( int i = 0; i < length; i++ )
  193.                 {
  194.                         returnList->values[ i ][0] = readDouble( handle );
  195.                         returnList->values[ i ][1] = readDouble( handle );
  196.                 }
  197.                
  198.                 return returnList;
  199.         }
  200.  
  201.         // Write complex list to file
  202.         void                    writeComplexList( complexList *list, FILE *handle )
  203.         {      
  204.                 writeInt( list->length, handle );
  205.                
  206.                 for ( int i = 0; i < list->length; i++ )
  207.                 {
  208.                         writeDouble( (double) list->values[i][0], handle );
  209.                         writeDouble( (double) list->values[i][1], handle );
  210.                 }
  211.         }
  212.        
  213.         // Write a string to file, prefixed by its length
  214.         void                    writeString( char *pString, FILE *handle )
  215.         {
  216.                 writeInt( (int) strlen( pString ) + 1, handle );
  217.                 fputs( pString, handle );      
  218.         }
  219.        
  220.         // Read a string from file, prefixed by its length
  221.         char                    *readString( char *pString, FILE *handle )
  222.         {
  223.                 int nSize = readInt( handle );
  224.                 pString = (char *) realloc( pString, nSize );
  225.                 fgets( pString, nSize, handle );       
  226.                 return pString;
  227.         }
  228.  
  229.         // Read a single global model parameter from file
  230.         void                    readModParam( modelParameter *param, FILE *handle )
  231.         {
  232.                 param->locked = (int) getc( handle );
  233.                 param->value = readDouble( handle );
  234.                 param->min = readDouble( handle );             
  235.                 param->max = readDouble( handle );
  236.         }
  237.  
  238.         // Write a single global model parameter to file
  239.         void                    writeModParam( modelParameter *param, FILE *handle )
  240.         {
  241.                 putc( (char) param->locked, handle );
  242.                 writeDouble( (double) param->value, handle );
  243.                 writeDouble( (double) param->min, handle );
  244.                 writeDouble( (double) param->max, handle );
  245.         }
  246.        
  247.         // Read a soft constraint from file
  248.         void                    readSoftConstraint( softConstraint *sc, FILE *handle )
  249.         {
  250.                 sc->type = fgetc( handle );
  251.                 sc->min  = readDouble( handle );
  252.                 sc->max  = readDouble( handle );
  253.         }
  254.  
  255.         // Write a soft constraint to a file
  256.         void                    writeSoftConstraint( softConstraint *sc, FILE *handle )
  257.         {
  258.                 fputc( sc->type, handle );
  259.                 writeDouble( (double) sc->min, handle );
  260.                 writeDouble( (double) sc->max, handle );
  261.         }
  262.        
  263.         // Read a single peak parameter from file
  264.         void                    readPeakParam( peakParameter *param, FILE *handle )
  265.         {
  266.                 param->locked                           = fgetc( handle );
  267.                 param->type                             = readInt( handle );
  268.                 param->target                           = readInt( handle );
  269.                 param->value                            = readDouble( handle );
  270.                 param->priorTarget[0]           = readInt( handle );
  271.                 param->priorTarget[1]           = readInt( handle );
  272.                 param->priorValue[0]            = readDouble( handle );
  273.                 param->priorValue[1]            = readDouble( handle );
  274.                 param->priorValue[2]            = readDouble( handle );
  275.                 readSoftConstraint( &param->sc, handle );
  276.         }      
  277.                
  278.         // Write a single peak parameter to file
  279.         void                    writePeakParam( peakParameter *param, FILE *handle )
  280.         {
  281.                 putc( (unsigned char) param->locked, handle );
  282.                 writeInt( (int) param->type, handle );
  283.                 writeInt( (int) param->target, handle );
  284.                 writeDouble( (double) param->value, handle );
  285.                 writeInt( (int) param->priorTarget[0], handle );
  286.                 writeInt( (int) param->priorTarget[1], handle );
  287.                 writeDouble( (double) param->priorValue[0], handle );
  288.                 writeDouble( (double) param->priorValue[1], handle );
  289.                 writeDouble( (double) param->priorValue[2], handle );
  290.                 writeSoftConstraint( &param->sc, handle );
  291.         }
  292.        
  293.         // Read a baseline from file
  294.         void            readBaseline( modelBaseline *baseline, FILE *handle )
  295.         {
  296.                 baseline->locked = fgetc( handle );
  297.                 baseline->type = readInt( handle );
  298.                 baseline->penalty = readDouble( handle );
  299.                 // Create & fill the new one
  300.                 baseline->params = readComplexList( handle );
  301.         }
  302.        
  303.         // Write a baseline to the file
  304.         void            writeBaseline( modelBaseline *baseline, FILE *handle )
  305.         {
  306.                 fputc( (char) baseline->locked, handle );
  307.                 writeInt( (int) baseline->type, handle );
  308.                 writeDouble( (double) baseline->penalty, handle );
  309.                 writeComplexList( baseline->params, handle );
  310.         }
  311.        
  312.         // Read a prior knowledge variable to file
  313.         void            readPriorVar( priorVar *priorVar, FILE *handle )
  314.         {
  315.                 priorVar->name          = readString( NULL, handle );
  316.                 priorVar->locked        = getc( handle );
  317.                 priorVar->value         = readDouble( handle );
  318.                 readSoftConstraint( &priorVar->sc, handle );
  319.         }      
  320.        
  321.         // Write a prior knowledge variable to file
  322.         void            writePriorVar( priorVar *priorVar, FILE *handle )
  323.         {
  324.                 writeString( priorVar->name, handle );
  325.                 putc( priorVar->locked, handle );
  326.                 writeDouble( (double) priorVar->value, handle );
  327.                 writeSoftConstraint( &priorVar->sc, handle );
  328.         }
  329.  
  330.         // Read a peak from a file
  331.         void            readPeak( modelPeak *peak, FILE *handle )
  332.         {
  333.                 peak->name                                      = readString( NULL, handle );
  334.                 peak->referencePeakID           = readInt( handle );
  335.                 peak->databasePeakID            = readInt( handle );
  336.                
  337.                 for ( int a = 0; a < 5; a++ )
  338.                 {
  339.                         readPeakParam( &peak->parameters[a], handle );
  340.                 }
  341.         }      
  342.        
  343.         // Write a peak to the file
  344.         void            writePeak( modelPeak *peak, FILE *handle )
  345.         {
  346.                 writeString( peak->name, handle );
  347.                 writeInt( peak->referencePeakID, handle );
  348.                 writeInt( peak->databasePeakID, handle );
  349.                
  350.                 for ( int a = 0; a < 5; a++ )
  351.                 {
  352.                         writePeakParam( &peak->parameters[a], handle );
  353.                 }
  354.         }
  355.        
  356.         // Read a reference peak from file
  357.         void            readRefPeak( referencePeak *refPeak, FILE *handle )
  358.         {
  359.                 refPeak->name                   = readString( NULL, handle );
  360.                 refPeak->locked                 = fgetc( handle );
  361.                 refPeak->type                   = readInt( handle );
  362.                 refPeak->params                 = readList( handle );
  363.                 refPeak->params2                = readList( handle );
  364.                 refPeak->sc                     = ( softConstraint * ) malloc( refPeak->params->length * sizeof( softConstraint ) );
  365.  
  366.                 for ( int a = 0; a < refPeak->params->length; a++ )
  367.                 {
  368.                         readSoftConstraint( &refPeak->sc[a], handle );
  369.                         fprintf( stderr, "m %f ", refPeak->sc[a].min );
  370.                         fprintf( stderr, "x %f ", refPeak->sc[a].max );
  371.                         fprintf( stderr, "t %d ", refPeak->sc[a].type );
  372.                 }
  373.         }      
  374.        
  375.         // Write a reference peak to the file
  376.         void            writeRefPeak( referencePeak *refPeak, FILE *handle )
  377.         {
  378.                 writeString( refPeak->name, handle );
  379.                 fputc( refPeak->locked, handle );
  380.                 writeInt( refPeak->type, handle );
  381.                 writeList( refPeak->params, handle );
  382.                 writeList( refPeak->params2, handle );
  383.                 for ( int a = 0; a < refPeak->params->length; a++ )
  384.                         writeSoftConstraint( &refPeak->sc[a], handle );
  385.         }
  386.        
  387.         // Write a reference peak to the file
  388.         void            readDatabasePeak( databasePeak *databasePeak, FILE *handle )
  389.         {
  390.                 databasePeak->name              = readString( NULL, handle );
  391.                 databasePeak->type              = readInt( handle );
  392.                 databasePeak->signal    = readComplexList( handle );
  393.         }      
  394.        
  395.         // Write a reference peak to the file
  396.         void            writeDatabasePeak( databasePeak *databasePeak, FILE *handle )
  397.         {
  398.                 writeString( databasePeak->name, handle );
  399.                 writeInt( databasePeak->type, handle );
  400.                 writeComplexList( databasePeak->signal, handle );
  401.         }      
  402.  
  403.         // Write a model to file
  404.         int                     writeModel( mrModel *model, const char *fname )
  405.         {
  406.                 FILE    *file;
  407.                 file = fopen( fname, "wb" );
  408.                
  409.                 if ( file != NULL ) {
  410.                         fprintf( file, "MDL" );
  411.                        
  412.                         fputc( 1, file );
  413.                         writeDouble( (double) model->dT, file );
  414.                        
  415.                         fputc( 2, file );
  416.                         writeModParam( &model->t0, file );
  417.                        
  418.                         fputc( 3, file );
  419.                         writeModParam( &model->globalPhase, file );
  420.                        
  421.                         fputc( 4, file );
  422.                         writeDouble( (double) model->ref, file );
  423.  
  424.                         fputc( 10, file );
  425.                         writeInt( (int) model->nPeaks, file );
  426.                        
  427.                         fputc( 11, file );
  428.                         writeInt( (int) model->nPriorVars, file );
  429.                        
  430.                         fputc( 12, file );
  431.                         writeInt( (int) model->nReferencePeaks, file );
  432.                        
  433.                         fputc( 13, file );
  434.                         writeInt( (int) model->nDatabasePeaks, file );
  435.                        
  436.                         fputc( 20, file );
  437.                         writeInt( (int) model->nPenalty, file );
  438.                        
  439.                         fputc( 30, file );
  440.                         writeBaseline( &model->baseline, file );
  441.                        
  442.                         // Denote end of global section
  443.                         fputc( 254, file );
  444.                        
  445.                         // Start dumping peaks
  446.                         for ( int a = 0; a < model->nPeaks; a++ )
  447.                                 writePeak( &model->peak[ a ], file );
  448.                        
  449.                         // Start dumping reference peaks
  450.                         for ( int a = 0; a < model->nReferencePeaks; a++ )
  451.                                 writeRefPeak( &model->referencePeaks[ a ], file );
  452.                        
  453.                         // Start dumping database peaks
  454.                         for ( int a = 0; a < model->nDatabasePeaks; a++ )
  455.                                 writeDatabasePeak( &model->databasePeaks[ a ], file );
  456.                        
  457.                         // Start dumping prior vars
  458.                         for ( int a = 0; a < model->nPriorVars; a++ )
  459.                                 writePriorVar( &model->priorVars[ a ], file );
  460.                        
  461.                         fclose( file );                
  462.                        
  463.                 } else {
  464.                         fprintf( stdout, "FATAL ERROR: Cannot write file"  );
  465.                 }
  466.                  
  467.                 return 1;
  468.         }
  469.        
  470.        
  471.                 //mrModel *model;
  472.                 //
  473.                 //model         = initModel( );
  474.                 //model         = initBaseline( model, inputModel->baseline.type, inputModel->baseline.params->length, inputModel->baseline.penalty );
  475.                 //for ( int a = 0; a < inputModel->baseline.params->length; a++ ) {
  476.                         //model->baseline.params->values[a][0] = inputModel->baseline.params->values[a][0];
  477.                         //model->baseline.params->values[a][1] = inputModel->baseline.params->values[a][1];
  478.                 //}
  479.                 //
  480.                 //model->dT                             = inputModel->dT;
  481.                 //model->globalPhase            = inputModel->globalPhase;
  482.                 //model->ref                            = inputModel->ref;
  483.                 //model->t0                             = inputModel->t0;
  484.                 //
  485.                 //model->nPenalty                       = inputModel->nPenalty;
  486.                 //model->nPeaks                 = inputModel->nPeaks;
  487.                 //model->nPriorVars             = inputModel->nPriorVars;
  488.                 //model->nDatabasePeaks = inputModel->nDatabasePeaks;
  489.                 //model->nReferencePeaks        = inputModel->nReferencePeaks; 
  490.        
  491.        
  492.         // Read a model from file
  493.         mrModel                 *readModel( const char *fname )
  494.         {
  495.                 FILE    *file;
  496.                 file = fopen( fname, "rb" );
  497.                
  498.                 mrModel *model = initModel( );
  499.                
  500.                 if ( file != NULL ) {
  501.                        
  502.                         char header[3];
  503.                         fread ( header, 1, 3, file );
  504.                        
  505.                         if ( strncmp( header, "MDL", 3 ) != 0 )
  506.                         {
  507.                                 fprintf( stdout, " Not a valid file!"  );
  508.                                 return NULL;
  509.                         }
  510.                        
  511.                         unsigned char type = getc( file );
  512.                        
  513.                         #ifdef MAXOUT
  514.                                 fprintf( stdout, "Reading global parametersn" );
  515.                         #endif
  516.                         while( type != 254 )
  517.                         {
  518.                                 switch( type )
  519.                                 {
  520.                                         case 1:
  521.                                                 model->dT = readDouble( file );
  522.                                         break;
  523.                                         case 2:
  524.                                                 readModParam( &model->t0, file );
  525.                                         break;
  526.                                         case 3:
  527.                                                 readModParam( &model->globalPhase, file );
  528.                                         break;
  529.                                         case 4:
  530.                                                 model->ref = readDouble( file );                                       
  531.                                         break;
  532.                                         case 10:
  533.                                                 model->nPeaks = readInt( file );
  534.                                                
  535.                                                 // Allocate some memory for the peaks
  536.                                                 model->peak     = (modelPeak *) realloc( model->peak, ( model->nPeaks ) * sizeof( modelPeak ) );
  537.                                                
  538.                                         break;
  539.                                         case 11:
  540.                                                 model->nPriorVars = readInt(file );
  541.                                                
  542.                                                 // Allocate some memory for the prior variables
  543.                                                 model->priorVars        = (priorVar *) realloc( model->priorVars, ( model->nPriorVars ) * sizeof( priorVar ) );
  544.                                                                                                
  545.                                         break;
  546.                                         case 12:
  547.                                                 model->nReferencePeaks = readInt(file );
  548.                                                
  549.                                                 // Allocate some memory for the reference peaks
  550.                                                 model->referencePeaks   = (referencePeak *) realloc( model->referencePeaks, ( model->nReferencePeaks ) * sizeof( referencePeak ) );
  551.                                         break;
  552.                                         case 13:
  553.                                                 model->nDatabasePeaks = readInt(file );
  554.                                                
  555.                                                 // Allocate some memory for the database peaks
  556.                                                 model->databasePeaks    = (databasePeak *) realloc( model->databasePeaks, ( model->nDatabasePeaks ) * sizeof( databasePeak ) );
  557.                                         break;
  558.                                         case 20:
  559.                                                 model->nPenalty = readInt(file );
  560.                                         break;
  561.                                         case 30:
  562.                                                 readBaseline( &model->baseline, file );
  563.                                         break;
  564.                                 }
  565.                                 type = fgetc( file );
  566.                                
  567.                         }
  568.                        
  569.                         #ifdef MAXOUT
  570.                                 fprintf( stdout, "Reading peaksn" );
  571.                         #endif
  572.  
  573.                         // Start reading peaks
  574.                         for ( int a = 0; a < model->nPeaks; a++ )
  575.                                 readPeak( &model->peak[ a ], file );   
  576.                        
  577.                         #ifdef MAXOUT
  578.                                 fprintf( stdout, "Reading reference peaksn" );
  579.                         #endif
  580.  
  581.                         // Start reading reference peaks
  582.                         for ( int a = 0; a < model->nReferencePeaks; a++ )
  583.                                 readRefPeak( &model->referencePeaks[ a ], file );
  584.  
  585.                         #ifdef MAXOUT
  586.                                 fprintf( stdout, "Reading database peaksn" );
  587.                         #endif
  588.                        
  589.                         // Start reading database peaks
  590.                         for ( int a = 0; a < model->nDatabasePeaks; a++ )
  591.                                 readDatabasePeak( &model->databasePeaks[ a ], file );
  592.                        
  593.                         #ifdef MAXOUT
  594.                                 fprintf( stdout, "Reading prior knowledge variablesn" );
  595.                         #endif
  596.                        
  597.                         // Start reading prior vars
  598.                         for ( int a = 0; a < model->nPriorVars; a++ )
  599.                                 readPriorVar( &model->priorVars[ a ], file );
  600.                        
  601.                         fclose( file );                
  602.                        
  603.                 } else {
  604.                         fprintf( stdout, "FATAL ERROR: Cannot read/find file"  );
  605.                 }
  606.                 fprintf( stderr, "badness" );
  607.                 return model;
  608.         }