matrix.c

Go to the documentation of this file.
00001 
00005 /*
00006  * Copyright (c) 2007-2008 ETH Zürich, Institute of Computational Science
00007  *
00008  * Permission is hereby granted, free of charge, to any person
00009  * obtaining a copy of this software and associated documentation
00010  * files (the "Software"), to deal in the Software without
00011  * restriction, including without limitation the rights to use,
00012  * copy, modify, merge, publish, distribute, sublicense, and/or sell
00013  * copies of the Software, and to permit persons to whom the
00014  * Software is furnished to do so, subject to the following
00015  * conditions:
00016  *
00017  * The above copyright notice and this permission notice shall be
00018  * included in all copies or substantial portions of the Software.
00019  *
00020  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00021  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
00022  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00023  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
00024  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
00025  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00026  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00027  * OTHER DEALINGS IN THE SOFTWARE.
00028  */
00029 
00030 #include "swps3.h"
00031 #include "matrix.h"
00032 #include <stdio.h>
00033 #include <stdlib.h>
00034 #include <ctype.h>
00035 #include <string.h>
00036 #include "debug.h"
00037 #include <sys/types.h>
00038 #include <math.h>
00039 
00040 static char * skip( char * line ){
00041         while (isspace( * line )) line ++;
00042         return line;
00043 }
00044 
00045 static int8_t sbmatrix[MATRIX_DIM][MATRIX_DIM] __ALIGNED__;
00046 static double dmatrix[MATRIX_DIM][MATRIX_DIM] __ALIGNED__;
00047 
00048 EXPORT DMatrix swps3_readDMatrix( char * filename ){
00049         FILE * fp;
00050         int i;
00051         char line[ 1024 ];
00052         char topChar[ 64 ];
00053         int count=0;
00054         memset( dmatrix, 0, sizeof(dmatrix) );
00055         fp = fopen( filename, "r" );
00056         if (!fp){
00057                 perror("Matrix - Error opening file:" );
00058                 exit( 1 );
00059         }
00060         /* parse the first line containing the caption ( "A R N ..." )*/
00061         while ( !count && fgets( line, sizeof( line ), fp ) ){
00062                 char * pline = line;
00063                 while ( (pline = skip( pline )) ){
00064                         /* Skip the comment and empty lines */
00065                         if ( *pline=='#' || !*pline )
00066                                 break;
00067                         if ( *pline<'A' || *pline>'Z'){
00068                                 error("Matrix: Invalid matrix desciption (Character expected in the caption line)" );
00069                         }
00070                         topChar[ count++ ] = (char)*pline-'A';
00071                         pline++;
00072                 }
00073         }
00074         /* Parse the left charactes and values */
00075         while ( fgets( line, sizeof( line ), fp ) ){
00076                 char * pline = skip( line );
00077                 /* Read the first character on a line */
00078                 char leftChar = (char)*pline-'A';
00079                 /* Skip the comment and empty lines */
00080                 if ( *pline=='#' || !*pline ) continue;
00081                 if ( *pline<'A' || *pline>'Z'){
00082                         error("Matrix: Invalid matrix desciption (Character expected at the beginning of the line)" );
00083                 }
00084                 /* Read in the values of the matrix */
00085                 for( i=0; i<count; i++ ){
00086                         pline = skip( pline+1 );
00087                         if (!isdigit( *pline ) && *pline!='-' && *pline!='.'){
00088                                 error("Matrix: Excepted a matrix value got '%c'", *pline );
00089                         }
00090                         dmatrix[ (int)leftChar ][ (int)topChar[ i ] ] = strtod( pline, &pline );
00091                 }
00092         }
00093         fclose( fp );
00094         return (DMatrix)dmatrix;
00095 }
00096 
00097 EXPORT SBMatrix swps3_readSBMatrix( char * filename ){
00098         FILE * fp;
00099         int i;
00100         char line[ 1024 ];
00101         char topChar[ 64 ];
00102         int count=0;
00103         memset( sbmatrix, 0, sizeof(sbmatrix) );
00104         fp = fopen( filename, "r" );
00105         if (!fp){
00106                 perror("Matrix - Error opening file:" );
00107                 exit( 1 );
00108         }
00109         /* parse the first line containing the caption ( "A R N ..." )*/
00110         while ( !count && fgets( line, sizeof( line ), fp ) ){
00111                 char * pline = line;
00112                 while ( (pline = skip( pline )) ){
00113                         /* Skip the comment and empty lines */
00114                         if ( *pline=='#' || !*pline )
00115                                 break;
00116                         if ( *pline<'A' || *pline>'Z'){
00117                                 error("Matrix: Invalid matrix desciption (Character expected in the caption line)" );
00118                         }
00119                         topChar[ count++ ] = (char)*pline-'A';
00120                         pline++;
00121                 }
00122         }
00123         /* Parse the left charactes and values */
00124         while ( fgets( line, sizeof( line ), fp ) ){
00125                 char * pline = skip( line );
00126                 /* Read the first character on a line */
00127                 char leftChar = (char)*pline-'A';
00128                 /* Skip the comment and empty lines */
00129                 if ( *pline=='#' || !*pline ) continue;
00130                 if ( *pline<'A' || *pline>'Z'){
00131                         error("Matrix: Invalid matrix desciption (Character expected at the beginning of the line)" );
00132                 }
00133                 /* Read in the values of the matrix */
00134                 for( i=0; i<count; i++ ){
00135                         pline = skip( pline+1 );
00136                         if (!isdigit( *pline ) && *pline!='-' && *pline!='.'){
00137                                 error("Matrix: Excepted a matrix value got '%c'", *pline );
00138                         }
00139                         sbmatrix[ (int)leftChar ][ (int)topChar[ i ] ] = strtol( pline, &pline, 10 );
00140                 }
00141         }
00142         fclose( fp );
00143         return (SBMatrix)sbmatrix;
00144 }
00145 
00146 EXPORT SBMatrix swps3_convertMatrixD2B( double factor ) {
00147         int i,j;
00148         for (i=0; i<MATRIX_DIM; i++)  {
00149                 for(j=0; j<MATRIX_DIM; j++) {
00150                         double val = dmatrix[i][j]*factor;
00151                         if ((int8_t)val < val)
00152                                 dmatrix[i][j] = (int8_t)val + 1;
00153                         else
00154                                 dmatrix[i][j] = (int8_t)val;
00155                 }
00156         }
00157         return (SBMatrix)sbmatrix;
00158 }
00159 
00160 EXPORT double swps3_factorFromThreshold( double threshold, double singleGapCost ) {
00161         (void)singleGapCost;
00162         /* return 256.0/(threshold+(max-min)+1.0);  FIXME is this right??? */
00163         return 256.0/threshold;
00164 }
00165 

Generated on Thu Jun 5 12:44:37 2008 for swps3 by  doxygen 1.5.4