swps3
matrix.c
Go to the documentation of this file.
1 
5 /*
6  * Copyright (c) 2007-2008 ETH Zürich, Institute of Computational Science
7  *
8  * Permission is hereby granted, free of charge, to any person
9  * obtaining a copy of this software and associated documentation
10  * files (the "Software"), to deal in the Software without
11  * restriction, including without limitation the rights to use,
12  * copy, modify, merge, publish, distribute, sublicense, and/or sell
13  * copies of the Software, and to permit persons to whom the
14  * Software is furnished to do so, subject to the following
15  * conditions:
16  *
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
22  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
24  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
25  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
27  * OTHER DEALINGS IN THE SOFTWARE.
28  */
29 
30 #include "swps3.h"
31 #include "matrix.h"
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <ctype.h>
35 #include <string.h>
36 #include "debug.h"
37 #include <sys/types.h>
38 #include <math.h>
39 
40 static char * skip( char * line ){
41  while (isspace( * line )) line ++;
42  return line;
43 }
44 
45 static int8_t sbmatrix[MATRIX_DIM][MATRIX_DIM] __ALIGNED__;
46 static double dmatrix[MATRIX_DIM][MATRIX_DIM] __ALIGNED__;
47 
48 EXPORT DMatrix swps3_readDMatrix( char * filename ){
49  FILE * fp;
50  int i;
51  char line[ 1024 ];
52  char topChar[ 64 ];
53  int count=0;
54  memset( dmatrix, 0, sizeof(dmatrix) );
55  fp = fopen( filename, "r" );
56  if (!fp){
57  perror("Matrix - Error opening file:" );
58  exit( 1 );
59  }
60  /* parse the first line containing the caption ( "A R N ..." )*/
61  while ( !count && fgets( line, sizeof( line ), fp ) ){
62  char * pline = line;
63  while ( (pline = skip( pline )) ){
64  /* Skip the comment and empty lines */
65  if ( *pline=='#' || !*pline )
66  break;
67  if ( *pline<'A' || *pline>'Z'){
68  error("Matrix: Invalid matrix desciption (Character expected in the caption line)" );
69  }
70  topChar[ count++ ] = (char)*pline-'A';
71  pline++;
72  }
73  }
74  /* Parse the left charactes and values */
75  while ( fgets( line, sizeof( line ), fp ) ){
76  char * pline = skip( line );
77  /* Read the first character on a line */
78  char leftChar = (char)*pline-'A';
79  /* Skip the comment and empty lines */
80  if ( *pline=='#' || !*pline ) continue;
81  if ( *pline<'A' || *pline>'Z'){
82  error("Matrix: Invalid matrix desciption (Character expected at the beginning of the line)" );
83  }
84  /* Read in the values of the matrix */
85  for( i=0; i<count; i++ ){
86  pline = skip( pline+1 );
87  if (!isdigit( *pline ) && *pline!='-' && *pline!='.'){
88  error("Matrix: Excepted a matrix value got '%c'", *pline );
89  }
90  dmatrix[ (int)leftChar ][ (int)topChar[ i ] ] = strtod( pline, &pline );
91  }
92  }
93  fclose( fp );
94  return (DMatrix)dmatrix;
95 }
96 
97 EXPORT SBMatrix swps3_readSBMatrix( char * filename ){
98  FILE * fp;
99  int i;
100  char line[ 1024 ];
101  char topChar[ 64 ];
102  int count=0;
103  memset( sbmatrix, 0, sizeof(sbmatrix) );
104  fp = fopen( filename, "r" );
105  if (!fp){
106  perror("Matrix - Error opening file:" );
107  exit( 1 );
108  }
109  /* parse the first line containing the caption ( "A R N ..." )*/
110  while ( !count && fgets( line, sizeof( line ), fp ) ){
111  char * pline = line;
112  while ( (pline = skip( pline )) ){
113  /* Skip the comment and empty lines */
114  if ( *pline=='#' || !*pline )
115  break;
116  if ( *pline<'A' || *pline>'Z'){
117  error("Matrix: Invalid matrix desciption (Character expected in the caption line)" );
118  }
119  topChar[ count++ ] = (char)*pline-'A';
120  pline++;
121  }
122  }
123  /* Parse the left charactes and values */
124  while ( fgets( line, sizeof( line ), fp ) ){
125  char * pline = skip( line );
126  /* Read the first character on a line */
127  char leftChar = (char)*pline-'A';
128  /* Skip the comment and empty lines */
129  if ( *pline=='#' || !*pline ) continue;
130  if ( *pline<'A' || *pline>'Z'){
131  error("Matrix: Invalid matrix desciption (Character expected at the beginning of the line)" );
132  }
133  /* Read in the values of the matrix */
134  for( i=0; i<count; i++ ){
135  pline = skip( pline+1 );
136  if (!isdigit( *pline ) && *pline!='-' && *pline!='.'){
137  error("Matrix: Excepted a matrix value got '%c'", *pline );
138  }
139  sbmatrix[ (int)leftChar ][ (int)topChar[ i ] ] = strtol( pline, &pline, 10 );
140  }
141  }
142  fclose( fp );
143  return (SBMatrix)sbmatrix;
144 }
145 
147  int i,j;
148  for (i=0; i<MATRIX_DIM; i++) {
149  for(j=0; j<MATRIX_DIM; j++) {
150  double val = dmatrix[i][j]*factor;
151  if ((int8_t)val < val)
152  dmatrix[i][j] = (int8_t)val + 1;
153  else
154  dmatrix[i][j] = (int8_t)val;
155  }
156  }
157  return (SBMatrix)sbmatrix;
158 }
159 
160 EXPORT double swps3_factorFromThreshold( double threshold, double singleGapCost ) {
161  (void)singleGapCost;
162  /* return 256.0/(threshold+(max-min)+1.0); FIXME is this right??? */
163  return 256.0/threshold;
164 }
165