1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
|
#include "filebuffer.h"
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
#ifdef CHAOS__FLAG
char* alphabet = "ATCGNPCMHDEKRQSILVFYWX*";
#else
char* alphabet = "ATCGN-.";
#endif
FileBuffer FileOpen (const char *path){
FileBuffer buf;
FILE *data = fopen (path, "r");
if (!data) return NULL;
buf = (FileBuffer) malloc (sizeof (struct FileBufferImplementation));
if (!buf) return NULL;
buf->filename = (char*) path;
buf->head = NULL;
buf->tail = NULL;
buf->startpos = 0; //100000000;
buf->endpos = 100000000; //0;
//buf->pos = BUFFER_SIZE;
//buf->len = BUFFER_SIZE;
buf->data = data;
return buf;
}
void FileUpdate (FileBuffer buf){
if (buf->head >= buf->tail){
buf->tail = buf->buffer + fread (buf->buffer, sizeof(char), BUFFER_SIZE, buf->data);
buf->head = buf->buffer;
}
}
int FileEOF (FileBuffer buf){
FileUpdate (buf);
return buf->head >= buf->tail && feof (buf->data);
}
void FileGetS (char *buffer, int length, FileBuffer buf){
int a;
for (a = 0; a < length && !FileEOF (buf); a++){
buffer[a] = FilePeekC (buf);
buf->head++;
if (a + 1 < length && buffer[a] == '\n'){
buffer[a + 1] = '\0';
break;
}
}
}
char *FileGetLine (FileBuffer buf){
int a = 0, length = 1;
char *buffer = (char *) malloc (1 * sizeof(char));
assert (buffer);
while (!FileEOF (buf)){
buffer[a] = FilePeekC (buf);
buf->head++;
if (buffer[a] == '\n'){
buffer[a] = '\0';
break;
}
a++;
if (a == length){
buffer = (char *) realloc (buffer, (length *= 2) * sizeof(char));
assert (buffer);
}
}
return buffer;
}
void FilePopC (FileBuffer buf){
buf->head++;
}
char FilePeekC (FileBuffer buf){
FileUpdate (buf);
return *(buf->head);
// return buf->buffer[buf->pos];
}
void FileClose (FileBuffer buf){
fclose (buf->data);
free (buf);
}
seq* FileRead (FileBuffer buf, int start, int finish, int version){
char* res = (char*) malloc(sizeof(char));
int ressize = 1, numread = 0, i, numNs = 0;
char *tempname, temp[256], currchar, *curr, *resend;
seq* myseq = (seq*) malloc(sizeof(seq));
if (FileEOF(buf))
return 0;
if (start == 1 && finish == 0) {
start = buf->startpos;
finish = buf->endpos;
if (start == 0)
start = 1;
}
tempname = FileGetLine (buf);
if (tempname[0] != '>') {
fprintf(stderr, "File is not in FASTA format!!\n");
exit(1);
}
myseq->name = (char*) malloc((strlen(tempname))*sizeof(char));
strcpy(myseq->name, tempname+1);
if (strchr(myseq->name, '\n'))
*(char *)(strchr(myseq->name, '\n')) = 0;
free (tempname);
for (i = 0; i < 256; i++){
temp[i] = (strchr (alphabet, toupper ((char) i)) != 0) ?
toupper((char) i) : 'N';
}
FileUpdate (buf);
curr = res;
resend = res + ressize;
if (version == VER_ORDER || version == VER_MLAGAN){
ressize = 2;
numread = 1;
if (version == VER_ORDER)
res[0] = 0;
else
res[0] = 'N';
curr++;
}
while (buf->head < buf->tail || !feof (buf->data)){
while (buf->head < buf->tail){
currchar = *(buf->head);
if (currchar == '>') goto outer;
if (currchar != ' ' && currchar != '\n' && currchar != '\r' &&
currchar != '\t' && currchar != '\t' && currchar != '\v') {
if (currchar == 'N') numNs++;
*curr++ = temp[(int) currchar];
if (curr >= resend) {
numread = curr - res;
res = (char *) realloc (res, sizeof(char) * (ressize *= 2));
curr = res + numread;
resend = res + ressize;
}
}
buf->head++;
}
buf->tail = buf->buffer + fread (buf->buffer, sizeof(char), BUFFER_SIZE, buf->data);
buf->head = buf->buffer;
}
outer:
numread = curr - res;
res[numread]=0;
myseq->rptr = res;
if (version == VER_FCHAOS){
if (start > 0) {
res[finish] = 0;
res = &res[start-1];
numread = finish-start+1;
}
myseq->numlets = numread;
}
else if (version == VER_ORDER){
if (start > 0){
res = &res[start-1];
res[0] = 0;
res[finish-start+2] = 0;
numread = finish-start+2;
}
myseq->numlets = numread-1;
}
else if (version == VER_MLAGAN){
if (start > 0 || finish > 0) {
res[finish] = 0;
res = &res[start-1];
numread = finish-start+1;
}
myseq->numlets = numread;
myseq->leftbound = start;
myseq->rightbound = finish;
}
myseq->numsiglets = numread - numNs;
myseq->lets = res;
return myseq;
}
|