| 1 | /* #include <malloc.h> */ |
|---|
| 2 | #include <stdio.h> |
|---|
| 3 | #include <stdlib.h> |
|---|
| 4 | #include <string.h> |
|---|
| 5 | |
|---|
| 6 | #define TRUE 1 |
|---|
| 7 | #define FALSE 0 |
|---|
| 8 | #define MAX(a,b) ((a)>(b)?(a):(b)) |
|---|
| 9 | #define MIN(a,b) ((a)<(b)?(a):(b)) |
|---|
| 10 | |
|---|
| 11 | struct data_format |
|---|
| 12 | { |
|---|
| 13 | int length; |
|---|
| 14 | char *nuc; |
|---|
| 15 | int offset; |
|---|
| 16 | char name[64]; |
|---|
| 17 | char type; |
|---|
| 18 | }; |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | void ErrorOut(code,string) |
|---|
| 22 | int code; |
|---|
| 23 | char *string; |
|---|
| 24 | { |
|---|
| 25 | if (code == 0) |
|---|
| 26 | { |
|---|
| 27 | fprintf(stderr,"Error:%s\n",string); |
|---|
| 28 | exit(1); |
|---|
| 29 | } |
|---|
| 30 | } |
|---|
| 31 | |
|---|
| 32 | char *Calloc2(count,size) |
|---|
| 33 | int count,size; |
|---|
| 34 | { |
|---|
| 35 | char *temp; |
|---|
| 36 | |
|---|
| 37 | temp = (char*)calloc(count,size); |
|---|
| 38 | if(temp == NULL) |
|---|
| 39 | { |
|---|
| 40 | fprintf(stdout,"Error in Calloc\n"); |
|---|
| 41 | exit(-1); |
|---|
| 42 | } |
|---|
| 43 | else |
|---|
| 44 | return(temp); |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | char *Realloc(block,size) |
|---|
| 48 | char *block; |
|---|
| 49 | int size; |
|---|
| 50 | { |
|---|
| 51 | char *temp; |
|---|
| 52 | temp =(char*)realloc(block,size); |
|---|
| 53 | if(temp == NULL) |
|---|
| 54 | { |
|---|
| 55 | fprintf(stdout,"Error in Calloc\n"); |
|---|
| 56 | exit(-1); |
|---|
| 57 | } |
|---|
| 58 | else |
|---|
| 59 | return(temp); |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | int ReadFlat(file,align,maxseqs) |
|---|
| 65 | FILE *file; |
|---|
| 66 | struct data_format align[]; |
|---|
| 67 | int maxseqs; |
|---|
| 68 | { |
|---|
| 69 | int j,len=0, count=-1,offset; |
|---|
| 70 | unsigned maxlen = 1024; |
|---|
| 71 | char Inline[1025]; |
|---|
| 72 | |
|---|
| 73 | if(file == NULL) |
|---|
| 74 | ErrorOut(0, "Cannot open data file"); |
|---|
| 75 | |
|---|
| 76 | for(;fgets(Inline,1024,file) != NULL;) |
|---|
| 77 | { |
|---|
| 78 | Inline[strlen(Inline)-1] = '\0'; |
|---|
| 79 | switch(Inline[0]) |
|---|
| 80 | { |
|---|
| 81 | case '>': |
|---|
| 82 | case '#': |
|---|
| 83 | case '%': |
|---|
| 84 | case '"': |
|---|
| 85 | case '@': |
|---|
| 86 | offset = 0; |
|---|
| 87 | { |
|---|
| 88 | int InlineLen=strlen(Inline); |
|---|
| 89 | for(j=0;j<InlineLen;j++) |
|---|
| 90 | { |
|---|
| 91 | if(Inline[j] == '(') |
|---|
| 92 | { |
|---|
| 93 | sscanf((char*) |
|---|
| 94 | (Inline+j+1),"%d", |
|---|
| 95 | &offset); |
|---|
| 96 | Inline[j] = '\0'; |
|---|
| 97 | } |
|---|
| 98 | } |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | if(count != -1) |
|---|
| 102 | { |
|---|
| 103 | align[count].length = len; |
|---|
| 104 | align[count].nuc[len] = '\0'; |
|---|
| 105 | maxlen = len; |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | count++; |
|---|
| 109 | if(count > maxseqs) |
|---|
| 110 | ErrorOut(0, "Sorry, alignment is too large"); |
|---|
| 111 | |
|---|
| 112 | align[count].nuc = Calloc2(maxlen,sizeof(char)); |
|---|
| 113 | align[count].type = Inline[0]; |
|---|
| 114 | align[count].offset = offset; |
|---|
| 115 | if( align[count].nuc == NULL) |
|---|
| 116 | ErrorOut(0, "Calloc problem"); |
|---|
| 117 | |
|---|
| 118 | sscanf((char*)(Inline+1),"%s", |
|---|
| 119 | align[count].name); |
|---|
| 120 | len = 0; |
|---|
| 121 | break; |
|---|
| 122 | default: |
|---|
| 123 | if(len+strlen(Inline) > maxlen) |
|---|
| 124 | { |
|---|
| 125 | maxlen = (maxlen+strlen(Inline))*2; |
|---|
| 126 | align[count].nuc = |
|---|
| 127 | Realloc(align[count].nuc, maxlen); |
|---|
| 128 | } |
|---|
| 129 | { |
|---|
| 130 | int InlineLen=strlen(Inline); |
|---|
| 131 | for(j=0;j<InlineLen;j++) |
|---|
| 132 | align[count].nuc[j+len] = Inline[j]; |
|---|
| 133 | } |
|---|
| 134 | len += strlen(Inline); |
|---|
| 135 | break; |
|---|
| 136 | } |
|---|
| 137 | } |
|---|
| 138 | if(count == -1) exit(1); |
|---|
| 139 | |
|---|
| 140 | align[count].length = len; |
|---|
| 141 | align[count].nuc[len] = '\0'; |
|---|
| 142 | return(++count); |
|---|
| 143 | } |
|---|