swps3
fasta.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 "fasta.h"
31 #include "swps3.h"
32 #include "debug.h"
33 #include <stdlib.h>
34 #ifdef HAVE_MALLOC_H
35 #include <malloc.h>
36 #endif /* HAVE_MALLOC_H */
37 #include <string.h>
38 #include <errno.h>
39 
40 EXPORT FastaLib * swps3_openLib( char * filename ){
41  FastaLib * lib = NULL;
42  FILE * fp;
43  int len;
44  if ( (fp = fopen( filename, "r" )) ){
45 #ifdef HAVE_MALLOC_H
46  lib = memalign( 16, sizeof( FastaLib ) );
47 #else
48  lib = malloc( sizeof( FastaLib ) );
49 #endif /* HAVE_MALLOC_H */
50  lib->fp = fp;
51  lib->name = "";
52  }
53  else{
54  error("Fasta: %s\n",strerror(errno));
55  }
56  swps3_readNextSequence( lib, &len );
57  if (len)
58  rewind( lib->fp );
59  return lib;
60 }
61 EXPORT char * swps3_readNextSequence( FastaLib * lib, int * len ){
62  char * pres = lib->readBuffer;
63  if ( feof( lib->fp ) )
64  return NULL;
65  if( (*pres = fgetc(lib->fp)) != '>' ) {
66  warning("Missing comment line, trying to continue anyway\n");
67  lib->name = "";
68  lib->data = pres++;
69  goto readseq;
70  } else {
71  fgets( pres, sizeof(lib->readBuffer)-(pres-lib->readBuffer), lib->fp );
72  lib->name = pres;
73  for (;*pres && *pres!='\n';pres++);
74  *pres++ = '\0';
75  while ((long)pres&0xf) *pres++ = '\0';
76  lib->data = pres;
77  }
78  while ( (*pres = fgetc( lib->fp) ) != '>' ){
79 readseq:
80  if( fgets( pres+1, sizeof(lib->readBuffer)-(pres+1-lib->readBuffer), lib->fp ) == 0 ) goto finish;
81  for (;*pres && *pres!='\n';pres++)
82  if ('A'>*pres || *pres>'Z'){
83  error("Invalid character in input sequence '%c'\n", *pres);
84  }
85  }
86  *pres = '\0';
87  ungetc( '>', lib->fp );
88 finish:
89  if (len)
90  *len = pres - lib->data;
91  swps3_translateSequence(lib->data,pres - lib->data,NULL);
92  return lib->data;
93 }
95  return lib->name;
96 }
98  fclose( lib->fp );
99  free( lib );
100 }
101 EXPORT void swps3_translateSequence(char *sequence, int seqLen, char table[256]) {
102  int i;
103  for(i=0;i<seqLen && sequence[i]!='\n' && sequence[i]!='\0';++i) {
104  if(table) sequence[i] = table[(int)sequence[i]];
105  else sequence[i] -= 'A';
106 
107  if(sequence[i] < 0 || sequence[i] >= MATRIX_DIM) error("Invalid character in input sequence at position %d\n",i);
108  }
109 }