00001
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
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
00061 while ( !count && fgets( line, sizeof( line ), fp ) ){
00062 char * pline = line;
00063 while ( (pline = skip( pline )) ){
00064
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
00075 while ( fgets( line, sizeof( line ), fp ) ){
00076 char * pline = skip( line );
00077
00078 char leftChar = (char)*pline-'A';
00079
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
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
00110 while ( !count && fgets( line, sizeof( line ), fp ) ){
00111 char * pline = line;
00112 while ( (pline = skip( pline )) ){
00113
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
00124 while ( fgets( line, sizeof( line ), fp ) ){
00125 char * pline = skip( line );
00126
00127 char leftChar = (char)*pline-'A';
00128
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
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
00163 return 256.0/threshold;
00164 }
00165