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