- #include "fileIO.h"
- int dumpComplexListToFile( const char *fn, complexList *data )
- {
- int a;
- FILE *file;
- file = fopen( fn, "w" );
- if ( file == NULL ) {
- printf( "Error: Could not open filen" );
- return 1;
- } else {
- fprintf( file, "%.11f", data->values[0][0] );
- for ( a = 1; a < data->length; a++ )
- {
- fprintf( file, ", %.11f", data->values[a][0] );
- }
- fprintf( file, "n" );
- fprintf( file, "%.11f", data->values[0][1] );
- for ( a = 1; a < data->length; a++ )
- {
- fprintf( file, ", %.11f", data->values[a][1] );
- }
- fprintf( file, "n" );
- fclose( file );
- return 0;
- }
- }
- int dumpSimpleListToFile( const char *fn, simpleList *data )
- {
- int a;
- FILE *file;
- file = fopen( fn, "w" );
- if ( file == NULL ) {
- printf( "Error: Could not open filen" );
- return 1;
- } else {
- fprintf( file, "%.15f", data->values[0] );
- for ( a = 1; a < data->length; a++ )
- {
- fprintf( file, ", %.15f", data->values[a] );
- }
- fclose( file );
- return 0;
- }
- }
- int dumpVectorToFile( const char *fn, float *data, int signalLength )
- {
- int a;
- FILE *file;
- file = fopen( fn, "w" );
- if ( file == NULL ) {
- printf( "Error: Could not open filen" );
- return 1;
- } else {
- fprintf( file, "%f", data[0] );
- for ( a = 1; a < signalLength; a++ )
- {
- fprintf( file, ", %f", data[a] );
- }
- fclose( file );
- return 0;
- }
- }
- int dumpSimpleMatrixToFile( const char *fn, simpleMatrix *data )
- {
- int a, b;
- FILE *file;
- file = fopen( fn, "w" );
- if ( file == NULL ) {
- printf( "Error: Could not open filen" );
- return 1;
- } else {
- for ( b = 0 ; b < data->dimY; b++ )
- {
- fprintf( file, "%.33f", data->values[0+b*data->dimX] );
- for ( a = 1; a < data->dimX; a++ )
- {
- fprintf( file, ", %.33f", data->values[a+b*data->dimX] );
- }
- fprintf( file, "n" );
- }
- fclose( file );
- return 0;
- }
- }
- int dumpComplexMatrixToFile( const char *fn, complexMatrix *data )
- {
- int a, b;
- FILE *file;
- file = fopen( fn, "w" );
- if ( file == NULL ) {
- printf( "Error: Could not open filen" );
- return 1;
- } else {
- for ( b = 0 ; b < data->dimY; b++ )
- {
- fprintf( file, "%f", data->values[0+b*data->dimX][0] );
- for ( a = 1; a < data->dimX; a++ )
- {
- fprintf( file, ", %f", data->values[a+b*data->dimX][0] );
- }
- fprintf( file, "n" );
- }
- for ( b = 0 ; b < data->dimY; b++ )
- {
- fprintf( file, "%f", data->values[0+b*data->dimX][1] );
- for ( a = 1; a < data->dimX; a++ )
- {
- fprintf( file, ", %f", data->values[a+b*data->dimX][1] );
- }
- fprintf( file, "n" );
- }
- fclose( file );
- return 0;
- }
- }
- mrFloat readDouble( FILE *handle )
- {
- double val;
- fread( &val, sizeof( double ), 1, handle );
- return ( mrFloat ) val;
- }
- void writeDouble( double value, FILE *handle )
- {
- fwrite( &value, sizeof( double ), 1, handle );
- }
- signed int readInt( FILE *handle )
- {
- signed int val;
- fread( &val, sizeof( int ), 1, handle );
- return ( signed int ) val;
- }
- void writeInt( signed int value, FILE *handle )
- {
- fwrite( &value, sizeof( signed int ), 1, handle );
- }
- // Read list from file (note that this list must later be freed)
- simpleList *readList( FILE *handle )
- {
- int length;
- simpleList *returnList;
- length = fgetc( handle );
- returnList = createList( length );
- for ( int i = 0; i < length; i++ )
- {
- returnList->values[ i ] = readDouble( handle );
- }
- return returnList;
- }
- // Write list to file
- void writeList( simpleList *list, FILE *handle )
- {
- fputc( list->length, handle );
- for ( int i = 0; i < list->length; i++ )
- {
- writeDouble( (double) list->values[i], handle );
- }
- }
- // Read complex list from file (note that this list must later be freed)
- complexList *readComplexList( FILE *handle )
- {
- int length;
- complexList *returnList;
- length = readInt( handle );
- returnList = createComplexList( length );
- for ( int i = 0; i < length; i++ )
- {
- returnList->values[ i ][0] = readDouble( handle );
- returnList->values[ i ][1] = readDouble( handle );
- }
- return returnList;
- }
- // Write complex list to file
- void writeComplexList( complexList *list, FILE *handle )
- {
- writeInt( list->length, handle );
- for ( int i = 0; i < list->length; i++ )
- {
- writeDouble( (double) list->values[i][0], handle );
- writeDouble( (double) list->values[i][1], handle );
- }
- }
- // Write a string to file, prefixed by its length
- void writeString( char *pString, FILE *handle )
- {
- writeInt( (int) strlen( pString ) + 1, handle );
- fputs( pString, handle );
- }
- // Read a string from file, prefixed by its length
- char *readString( char *pString, FILE *handle )
- {
- int nSize = readInt( handle );
- pString = (char *) realloc( pString, nSize );
- fgets( pString, nSize, handle );
- return pString;
- }
- // Read a single global model parameter from file
- void readModParam( modelParameter *param, FILE *handle )
- {
- param->locked = (int) getc( handle );
- param->value = readDouble( handle );
- param->min = readDouble( handle );
- param->max = readDouble( handle );
- }
- // Write a single global model parameter to file
- void writeModParam( modelParameter *param, FILE *handle )
- {
- putc( (char) param->locked, handle );
- writeDouble( (double) param->value, handle );
- writeDouble( (double) param->min, handle );
- writeDouble( (double) param->max, handle );
- }
- // Read a soft constraint from file
- void readSoftConstraint( softConstraint *sc, FILE *handle )
- {
- sc->type = fgetc( handle );
- sc->min = readDouble( handle );
- sc->max = readDouble( handle );
- }
- // Write a soft constraint to a file
- void writeSoftConstraint( softConstraint *sc, FILE *handle )
- {
- fputc( sc->type, handle );
- writeDouble( (double) sc->min, handle );
- writeDouble( (double) sc->max, handle );
- }
- // Read a single peak parameter from file
- void readPeakParam( peakParameter *param, FILE *handle )
- {
- param->locked = fgetc( handle );
- param->type = readInt( handle );
- param->target = readInt( handle );
- param->value = readDouble( handle );
- param->priorTarget[0] = readInt( handle );
- param->priorTarget[1] = readInt( handle );
- param->priorValue[0] = readDouble( handle );
- param->priorValue[1] = readDouble( handle );
- param->priorValue[2] = readDouble( handle );
- readSoftConstraint( ¶m->sc, handle );
- }
- // Write a single peak parameter to file
- void writePeakParam( peakParameter *param, FILE *handle )
- {
- putc( (unsigned char) param->locked, handle );
- writeInt( (int) param->type, handle );
- writeInt( (int) param->target, handle );
- writeDouble( (double) param->value, handle );
- writeInt( (int) param->priorTarget[0], handle );
- writeInt( (int) param->priorTarget[1], handle );
- writeDouble( (double) param->priorValue[0], handle );
- writeDouble( (double) param->priorValue[1], handle );
- writeDouble( (double) param->priorValue[2], handle );
- writeSoftConstraint( ¶m->sc, handle );
- }
- // Read a baseline from file
- void readBaseline( modelBaseline *baseline, FILE *handle )
- {
- baseline->locked = fgetc( handle );
- baseline->type = readInt( handle );
- baseline->penalty = readDouble( handle );
- // Create & fill the new one
- baseline->params = readComplexList( handle );
- }
- // Write a baseline to the file
- void writeBaseline( modelBaseline *baseline, FILE *handle )
- {
- fputc( (char) baseline->locked, handle );
- writeInt( (int) baseline->type, handle );
- writeDouble( (double) baseline->penalty, handle );
- writeComplexList( baseline->params, handle );
- }
- // Read a prior knowledge variable to file
- void readPriorVar( priorVar *priorVar, FILE *handle )
- {
- priorVar->name = readString( NULL, handle );
- priorVar->locked = getc( handle );
- priorVar->value = readDouble( handle );
- readSoftConstraint( &priorVar->sc, handle );
- }
- // Write a prior knowledge variable to file
- void writePriorVar( priorVar *priorVar, FILE *handle )
- {
- writeString( priorVar->name, handle );
- putc( priorVar->locked, handle );
- writeDouble( (double) priorVar->value, handle );
- writeSoftConstraint( &priorVar->sc, handle );
- }
- // Read a peak from a file
- void readPeak( modelPeak *peak, FILE *handle )
- {
- peak->name = readString( NULL, handle );
- peak->referencePeakID = readInt( handle );
- peak->databasePeakID = readInt( handle );
- for ( int a = 0; a < 5; a++ )
- {
- readPeakParam( &peak->parameters[a], handle );
- }
- }
- // Write a peak to the file
- void writePeak( modelPeak *peak, FILE *handle )
- {
- writeString( peak->name, handle );
- writeInt( peak->referencePeakID, handle );
- writeInt( peak->databasePeakID, handle );
- for ( int a = 0; a < 5; a++ )
- {
- writePeakParam( &peak->parameters[a], handle );
- }
- }
- // Read a reference peak from file
- void readRefPeak( referencePeak *refPeak, FILE *handle )
- {
- refPeak->name = readString( NULL, handle );
- refPeak->locked = fgetc( handle );
- refPeak->type = readInt( handle );
- refPeak->params = readList( handle );
- refPeak->params2 = readList( handle );
- refPeak->sc = ( softConstraint * ) malloc( refPeak->params->length * sizeof( softConstraint ) );
- for ( int a = 0; a < refPeak->params->length; a++ )
- {
- readSoftConstraint( &refPeak->sc[a], handle );
- fprintf( stderr, "m %f ", refPeak->sc[a].min );
- fprintf( stderr, "x %f ", refPeak->sc[a].max );
- fprintf( stderr, "t %d ", refPeak->sc[a].type );
- }
- }
- // Write a reference peak to the file
- void writeRefPeak( referencePeak *refPeak, FILE *handle )
- {
- writeString( refPeak->name, handle );
- fputc( refPeak->locked, handle );
- writeInt( refPeak->type, handle );
- writeList( refPeak->params, handle );
- writeList( refPeak->params2, handle );
- for ( int a = 0; a < refPeak->params->length; a++ )
- writeSoftConstraint( &refPeak->sc[a], handle );
- }
- // Write a reference peak to the file
- void readDatabasePeak( databasePeak *databasePeak, FILE *handle )
- {
- databasePeak->name = readString( NULL, handle );
- databasePeak->type = readInt( handle );
- databasePeak->signal = readComplexList( handle );
- }
- // Write a reference peak to the file
- void writeDatabasePeak( databasePeak *databasePeak, FILE *handle )
- {
- writeString( databasePeak->name, handle );
- writeInt( databasePeak->type, handle );
- writeComplexList( databasePeak->signal, handle );
- }
- // Write a model to file
- int writeModel( mrModel *model, const char *fname )
- {
- FILE *file;
- file = fopen( fname, "wb" );
- if ( file != NULL ) {
- fprintf( file, "MDL" );
- fputc( 1, file );
- writeDouble( (double) model->dT, file );
- fputc( 2, file );
- writeModParam( &model->t0, file );
- fputc( 3, file );
- writeModParam( &model->globalPhase, file );
- fputc( 4, file );
- writeDouble( (double) model->ref, file );
- fputc( 10, file );
- writeInt( (int) model->nPeaks, file );
- fputc( 11, file );
- writeInt( (int) model->nPriorVars, file );
- fputc( 12, file );
- writeInt( (int) model->nReferencePeaks, file );
- fputc( 13, file );
- writeInt( (int) model->nDatabasePeaks, file );
- fputc( 20, file );
- writeInt( (int) model->nPenalty, file );
- fputc( 30, file );
- writeBaseline( &model->baseline, file );
- // Denote end of global section
- fputc( 254, file );
- // Start dumping peaks
- for ( int a = 0; a < model->nPeaks; a++ )
- writePeak( &model->peak[ a ], file );
- // Start dumping reference peaks
- for ( int a = 0; a < model->nReferencePeaks; a++ )
- writeRefPeak( &model->referencePeaks[ a ], file );
- // Start dumping database peaks
- for ( int a = 0; a < model->nDatabasePeaks; a++ )
- writeDatabasePeak( &model->databasePeaks[ a ], file );
- // Start dumping prior vars
- for ( int a = 0; a < model->nPriorVars; a++ )
- writePriorVar( &model->priorVars[ a ], file );
- fclose( file );
- } else {
- fprintf( stdout, "FATAL ERROR: Cannot write file" );
- }
- return 1;
- }
- //mrModel *model;
- //
- //model = initModel( );
- //model = initBaseline( model, inputModel->baseline.type, inputModel->baseline.params->length, inputModel->baseline.penalty );
- //for ( int a = 0; a < inputModel->baseline.params->length; a++ ) {
- //model->baseline.params->values[a][0] = inputModel->baseline.params->values[a][0];
- //model->baseline.params->values[a][1] = inputModel->baseline.params->values[a][1];
- //}
- //
- //model->dT = inputModel->dT;
- //model->globalPhase = inputModel->globalPhase;
- //model->ref = inputModel->ref;
- //model->t0 = inputModel->t0;
- //
- //model->nPenalty = inputModel->nPenalty;
- //model->nPeaks = inputModel->nPeaks;
- //model->nPriorVars = inputModel->nPriorVars;
- //model->nDatabasePeaks = inputModel->nDatabasePeaks;
- //model->nReferencePeaks = inputModel->nReferencePeaks;
- // Read a model from file
- mrModel *readModel( const char *fname )
- {
- FILE *file;
- file = fopen( fname, "rb" );
- mrModel *model = initModel( );
- if ( file != NULL ) {
- char header[3];
- fread ( header, 1, 3, file );
- if ( strncmp( header, "MDL", 3 ) != 0 )
- {
- fprintf( stdout, " Not a valid file!" );
- return NULL;
- }
- unsigned char type = getc( file );
- #ifdef MAXOUT
- fprintf( stdout, "Reading global parametersn" );
- #endif
- while( type != 254 )
- {
- switch( type )
- {
- case 1:
- model->dT = readDouble( file );
- break;
- case 2:
- readModParam( &model->t0, file );
- break;
- case 3:
- readModParam( &model->globalPhase, file );
- break;
- case 4:
- model->ref = readDouble( file );
- break;
- case 10:
- model->nPeaks = readInt( file );
- // Allocate some memory for the peaks
- model->peak = (modelPeak *) realloc( model->peak, ( model->nPeaks ) * sizeof( modelPeak ) );
- break;
- case 11:
- model->nPriorVars = readInt(file );
- // Allocate some memory for the prior variables
- model->priorVars = (priorVar *) realloc( model->priorVars, ( model->nPriorVars ) * sizeof( priorVar ) );
- break;
- case 12:
- model->nReferencePeaks = readInt(file );
- // Allocate some memory for the reference peaks
- model->referencePeaks = (referencePeak *) realloc( model->referencePeaks, ( model->nReferencePeaks ) * sizeof( referencePeak ) );
- break;
- case 13:
- model->nDatabasePeaks = readInt(file );
- // Allocate some memory for the database peaks
- model->databasePeaks = (databasePeak *) realloc( model->databasePeaks, ( model->nDatabasePeaks ) * sizeof( databasePeak ) );
- break;
- case 20:
- model->nPenalty = readInt(file );
- break;
- case 30:
- readBaseline( &model->baseline, file );
- break;
- }
- type = fgetc( file );
- }
- #ifdef MAXOUT
- fprintf( stdout, "Reading peaksn" );
- #endif
- // Start reading peaks
- for ( int a = 0; a < model->nPeaks; a++ )
- readPeak( &model->peak[ a ], file );
- #ifdef MAXOUT
- fprintf( stdout, "Reading reference peaksn" );
- #endif
- // Start reading reference peaks
- for ( int a = 0; a < model->nReferencePeaks; a++ )
- readRefPeak( &model->referencePeaks[ a ], file );
- #ifdef MAXOUT
- fprintf( stdout, "Reading database peaksn" );
- #endif
- // Start reading database peaks
- for ( int a = 0; a < model->nDatabasePeaks; a++ )
- readDatabasePeak( &model->databasePeaks[ a ], file );
- #ifdef MAXOUT
- fprintf( stdout, "Reading prior knowledge variablesn" );
- #endif
- // Start reading prior vars
- for ( int a = 0; a < model->nPriorVars; a++ )
- readPriorVar( &model->priorVars[ a ], file );
- fclose( file );
- } else {
- fprintf( stdout, "FATAL ERROR: Cannot read/find file" );
- }
- fprintf( stderr, "badness" );
- return model;
- }
Undefined
By: Guest | Date: Jan 14 2010 16:02 | Format: None | Expires: never | Size: 16.1 KB | Hits: 1069
Latest pastes
1 hours ago
11 hours ago
1 days ago
2 days ago
2 days ago