fasta.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 "fasta.h"
00031 #include "swps3.h"
00032 #include "debug.h"
00033 #include <stdlib.h>
00034 #ifdef HAVE_MALLOC_H
00035 #include <malloc.h>
00036 #endif /* HAVE_MALLOC_H */
00037 #include <string.h>
00038 #include <errno.h>
00039 
00040 EXPORT FastaLib * swps3_openLib( char * filename ){
00041         FastaLib * lib = NULL;
00042         FILE * fp;
00043         int len;
00044         if ( (fp = fopen( filename, "r" )) ){
00045 #ifdef HAVE_MALLOC_H
00046                 lib = memalign( 16, sizeof( FastaLib ) );
00047 #else
00048                 lib = malloc( sizeof( FastaLib ) );
00049 #endif /* HAVE_MALLOC_H */
00050                 lib->fp = fp;
00051                 lib->name = "";
00052         }
00053         else{
00054                 error("Fasta: %s\n",strerror(errno));
00055         }
00056         swps3_readNextSequence( lib, &len );
00057         if (len)
00058                 rewind( lib->fp );
00059         return lib;
00060 }
00061 EXPORT char * swps3_readNextSequence( FastaLib * lib, int * len ){
00062         char * pres = lib->readBuffer;
00063         if ( feof( lib->fp ) )
00064                 return NULL;
00065         if( (*pres = fgetc(lib->fp)) != '>' ) {
00066            warning("Missing comment line, trying to continue anyway\n");
00067            lib->name = "";
00068            lib->data = pres++;
00069            goto readseq;
00070         } else {
00071            fgets( pres, sizeof(lib->readBuffer)-(pres-lib->readBuffer), lib->fp );
00072            lib->name = pres;
00073            for (;*pres && *pres!='\n';pres++);
00074            *pres++ = '\0';
00075            while ((long)pres&0xf) *pres++ = '\0';
00076            lib->data = pres;
00077         }
00078         while ( (*pres = fgetc( lib->fp) ) != '>' ){
00079 readseq:
00080                 if( fgets( pres+1, sizeof(lib->readBuffer)-(pres+1-lib->readBuffer), lib->fp ) == 0 ) goto finish;
00081                 for (;*pres && *pres!='\n';pres++)
00082                         if ('A'>*pres || *pres>'Z'){
00083                                 error("Invalid character in input sequence '%c'\n", *pres);
00084                         }
00085         }
00086         *pres = '\0';
00087         ungetc( '>', lib->fp );
00088 finish:
00089         if (len)
00090                 *len = pres - lib->data;
00091         swps3_translateSequence(lib->data,pres - lib->data,NULL);
00092         return lib->data;
00093 }
00094 EXPORT char * swps3_getSequenceName( FastaLib * lib ){
00095         return lib->name;
00096 }
00097 EXPORT void swps3_closeLib( FastaLib * lib ){
00098         free( lib );
00099 }
00100 EXPORT void swps3_translateSequence(char *sequence, int seqLen, char table[256]) {
00101    int i;
00102    for(i=0;i<seqLen && sequence[i]!='\n' && sequence[i]!='\0';++i) {
00103       if(table) sequence[i] = table[(int)sequence[i]];
00104       else sequence[i] -= 'A';
00105 
00106       if(sequence[i] < 0 || sequence[i] >= MATRIX_DIM) error("Invalid character in input sequence at position %d\n",i);
00107    }
00108 }

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