1 | #include "GDE_extglob.h" |
---|
2 | #include <ctime> |
---|
3 | #include <algorithm> |
---|
4 | |
---|
5 | /* |
---|
6 | Copyright (c) 1989-1990, University of Illinois board of trustees. All |
---|
7 | rights reserved. Written by Steven Smith at the Center for Prokaryote Genome |
---|
8 | Analysis. Design and implementation guidance by Dr. Gary Olsen and Dr. |
---|
9 | Carl Woese. |
---|
10 | |
---|
11 | Copyright (c) 1990,1991,1992 Steven Smith at the Harvard Genome Laboratory. |
---|
12 | all rights reserved. |
---|
13 | |
---|
14 | Copyright (c) 1993, Steven Smith, all rights reserved. |
---|
15 | |
---|
16 | */ |
---|
17 | |
---|
18 | static int CheckType(char *seq, int len) { |
---|
19 | /* CheckType: Check base composition to see if the sequence |
---|
20 | * appears to be an amino acid sequence. If it is, pass back |
---|
21 | * TRUE, else FALSE. |
---|
22 | */ |
---|
23 | int j, count1 = 0, count2 = 0; |
---|
24 | |
---|
25 | for (j=0; j<len; j++) |
---|
26 | if (((seq[j]|32) < 'z') && ((seq[j]|32) > 'a')) |
---|
27 | { |
---|
28 | count1++; |
---|
29 | if (strchr("ACGTUNacgtun", seq[j]) == NULL) |
---|
30 | count2++; |
---|
31 | } |
---|
32 | |
---|
33 | return ((count2 > count1/4) ? TRUE : FALSE); |
---|
34 | } |
---|
35 | |
---|
36 | // ARB |
---|
37 | struct ARB_TIME { |
---|
38 | int yy; |
---|
39 | int mm; |
---|
40 | int dd; |
---|
41 | int hr; |
---|
42 | int mn; |
---|
43 | int sc; |
---|
44 | }; |
---|
45 | |
---|
46 | static void AsciiTime(void *b, char *asciitime) |
---|
47 | { |
---|
48 | ARB_TIME *a=(ARB_TIME*)b; |
---|
49 | int j; |
---|
50 | char temp[GBUFSIZ]; |
---|
51 | |
---|
52 | a->dd = 0; |
---|
53 | a->yy = 0; |
---|
54 | a->mm = 0; |
---|
55 | sscanf(asciitime, "%d%5c%d", &(a->dd), temp, &(a->yy)); |
---|
56 | temp[5] = '\0'; |
---|
57 | for (j=0; j<12; j++) |
---|
58 | if (strcmp(temp, GDEmonth[j]) == 0) |
---|
59 | a->mm = j+1; |
---|
60 | if (a->dd <0 || a->dd > 31 || a->yy < 0 || a->mm > 11) |
---|
61 | SetTime(a); |
---|
62 | return; |
---|
63 | } |
---|
64 | // ENDARB |
---|
65 | |
---|
66 | GB_ERROR ReadGen(char *filename, NA_Alignment *dataset) { |
---|
67 | GB_ERROR error = NULL; |
---|
68 | FILE *file = fopen(filename, "r"); |
---|
69 | if (!file) { |
---|
70 | error = GB_IO_error("reading", filename); |
---|
71 | } |
---|
72 | else { |
---|
73 | int done = FALSE; |
---|
74 | size_t len = 0; |
---|
75 | int IS_REALLY_AA = FALSE; |
---|
76 | char in_line[GBUFSIZ], c; |
---|
77 | char *buffer = 0, *gencomments = NULL, fields[8][GBUFSIZ]; |
---|
78 | size_t buflen = 0; |
---|
79 | int genclen = 0, curelem = 0, n = 0; |
---|
80 | int start_col = -1; |
---|
81 | |
---|
82 | NA_Sequence *this_elem = 0; |
---|
83 | |
---|
84 | for (; fgets(in_line, GBUFSIZ, file) != 0;) |
---|
85 | { |
---|
86 | if (in_line[strlen(in_line)-1] == '\n') |
---|
87 | in_line[strlen(in_line)-1] = '\0'; |
---|
88 | if (Find(in_line, "LOCUS")) |
---|
89 | { |
---|
90 | curelem = dataset->numelements++; |
---|
91 | if (curelem == 0) |
---|
92 | { |
---|
93 | dataset->element=(NA_Sequence*) Calloc(5, sizeof(NA_Sequence)); |
---|
94 | dataset->maxnumelements = 5; |
---|
95 | } |
---|
96 | else if (curelem==dataset->maxnumelements) |
---|
97 | { |
---|
98 | (dataset->maxnumelements) *= 2; |
---|
99 | dataset->element = (NA_Sequence*) Realloc((char*)dataset->element, dataset->maxnumelements * sizeof(NA_Sequence)); |
---|
100 | } |
---|
101 | this_elem = &(dataset->element[curelem]); |
---|
102 | n = sscanf(in_line, "%s %s %s %s %s %s %s %s", |
---|
103 | fields[0], fields[1], fields[2], fields[3], fields[4], |
---|
104 | fields[5], fields[6], fields[7]); |
---|
105 | if (IS_REALLY_AA) |
---|
106 | { |
---|
107 | InitNASeq(this_elem, PROTEIN); |
---|
108 | } |
---|
109 | else if (Find(in_line, "DNA")) |
---|
110 | { |
---|
111 | InitNASeq(this_elem, DNA); |
---|
112 | } |
---|
113 | else if (Find(in_line, "RNA")) |
---|
114 | { |
---|
115 | InitNASeq(this_elem, RNA); |
---|
116 | } |
---|
117 | else if (Find(in_line, "MASK")) |
---|
118 | { |
---|
119 | InitNASeq(this_elem, MASK); |
---|
120 | } |
---|
121 | else if (Find(in_line, "TEXT")) |
---|
122 | { |
---|
123 | InitNASeq(this_elem, TEXT); |
---|
124 | } |
---|
125 | else if (Find(in_line, "PROT")) |
---|
126 | { |
---|
127 | InitNASeq(this_elem, PROTEIN); |
---|
128 | } |
---|
129 | else |
---|
130 | InitNASeq(this_elem, DNA); |
---|
131 | |
---|
132 | strncpy_terminate(this_elem->short_name, fields[1], SIZE_SHORT_NAME); |
---|
133 | AsciiTime(&(this_elem->t_stamp.origin), fields[n-1]); |
---|
134 | this_elem->attr = DEFAULT_X_ATTR; |
---|
135 | |
---|
136 | if (Find(in_line, "Circular")) |
---|
137 | this_elem->attr |= IS_CIRCULAR; |
---|
138 | |
---|
139 | gencomments = NULL; |
---|
140 | genclen = 0; |
---|
141 | } |
---|
142 | else if (Find(in_line, "DEFINITION")) |
---|
143 | strncpy_terminate(this_elem->description, in_line+12, SIZE_DESCRIPTION); |
---|
144 | |
---|
145 | else if (Find(in_line, "AUTHOR")) |
---|
146 | strncpy_terminate(this_elem->authority, in_line+12, SIZE_AUTHORITY); |
---|
147 | |
---|
148 | else if (Find(in_line, " ORGANISM")) |
---|
149 | strncpy_terminate(this_elem->seq_name, in_line+12, SIZE_SEQ_NAME); |
---|
150 | |
---|
151 | else if (Find(in_line, "ACCESSION")) |
---|
152 | strncpy_terminate(this_elem->id, in_line+12, SIZE_ID); |
---|
153 | |
---|
154 | else if (Find(in_line, "ORIGIN")) |
---|
155 | { |
---|
156 | done = FALSE; |
---|
157 | len = 0; |
---|
158 | for (; done == FALSE && fgets(in_line, GBUFSIZ, file) != 0;) |
---|
159 | { |
---|
160 | if (in_line[0] != '/') |
---|
161 | { |
---|
162 | if (buflen == 0) |
---|
163 | { |
---|
164 | buflen = GBUFSIZ; |
---|
165 | buffer = Calloc(sizeof(char), buflen); |
---|
166 | } |
---|
167 | |
---|
168 | else if (len+strlen(in_line) >= buflen) |
---|
169 | { |
---|
170 | buflen += GBUFSIZ; |
---|
171 | buffer = Realloc((char*)buffer, sizeof(char)*buflen); |
---|
172 | for (size_t j=buflen-GBUFSIZ ; j<buflen; j++) buffer[j] = '\0'; |
---|
173 | } |
---|
174 | // Search for the fist column of data (whitespace-number-whitespace)data |
---|
175 | if (start_col == -1) |
---|
176 | { |
---|
177 | for (start_col=0; in_line[start_col] == ' ' || in_line[start_col] == '\t'; start_col++) ; |
---|
178 | for (start_col++; strchr("1234567890", in_line[start_col]) != NULL; start_col++) ; |
---|
179 | for (start_col++; in_line[start_col] == ' ' || in_line[start_col] == '\t'; start_col++) ; |
---|
180 | } |
---|
181 | for (int j=start_col; (c = in_line[j]) != '\0'; j++) |
---|
182 | { |
---|
183 | if ((c != '\n') && ((j-start_col + 1) % 11 != 0)) { |
---|
184 | buffer[len++] = c; |
---|
185 | } |
---|
186 | } |
---|
187 | } |
---|
188 | else |
---|
189 | { |
---|
190 | AppendNA((NA_Base*)buffer, len, &(dataset-> |
---|
191 | element[curelem])); |
---|
192 | for (size_t j=0; j<len; j++) buffer[j] = '\0'; |
---|
193 | len = 0; |
---|
194 | done = TRUE; |
---|
195 | dataset->element[curelem].comments = gencomments; |
---|
196 | dataset->element[curelem].comments_len= genclen - 1; |
---|
197 | dataset->element[curelem].comments_maxlen = genclen; |
---|
198 | |
---|
199 | gencomments = NULL; |
---|
200 | genclen = 0; |
---|
201 | } |
---|
202 | } |
---|
203 | /* Test if sequence should be converted by the translation table |
---|
204 | * If it looks like a protein... |
---|
205 | */ |
---|
206 | if (dataset->element[curelem].rmatrix && |
---|
207 | IS_REALLY_AA == FALSE) |
---|
208 | { |
---|
209 | IS_REALLY_AA = CheckType((char*)dataset->element[curelem]. |
---|
210 | sequence, dataset->element[curelem].seqlen); |
---|
211 | |
---|
212 | if (IS_REALLY_AA == FALSE) |
---|
213 | Ascii2NA((char*)dataset->element[curelem].sequence, |
---|
214 | dataset->element[curelem].seqlen, |
---|
215 | dataset->element[curelem].rmatrix); |
---|
216 | else { |
---|
217 | // Force the sequence to be AA |
---|
218 | dataset->element[curelem].elementtype = PROTEIN; |
---|
219 | dataset->element[curelem].rmatrix = NULL; |
---|
220 | dataset->element[curelem].tmatrix = NULL; |
---|
221 | dataset->element[curelem].col_lut = Default_PROColor_LKUP; |
---|
222 | } |
---|
223 | } |
---|
224 | } |
---|
225 | else if (Find(in_line, "ZZZZZ")) |
---|
226 | { |
---|
227 | Cfree(gencomments); |
---|
228 | genclen = 0; |
---|
229 | } |
---|
230 | else |
---|
231 | { |
---|
232 | if (gencomments == NULL) |
---|
233 | { |
---|
234 | gencomments = strdup(in_line); |
---|
235 | genclen = strlen(gencomments)+1; |
---|
236 | } |
---|
237 | else |
---|
238 | { |
---|
239 | genclen += strlen(in_line)+1; |
---|
240 | gencomments = Realloc((char*)gencomments, genclen * sizeof(char)); |
---|
241 | strncat(gencomments, in_line, GBUFSIZ); |
---|
242 | strncat(gencomments, "\n", GBUFSIZ); |
---|
243 | } |
---|
244 | } |
---|
245 | } |
---|
246 | Cfree(buffer); |
---|
247 | fclose(file); |
---|
248 | } |
---|
249 | for (size_t j=0; j<dataset->numelements; j++) { |
---|
250 | dataset->maxlen = std::max(dataset->maxlen, |
---|
251 | dataset->element[j].seqlen+dataset->element[j].offset); |
---|
252 | } |
---|
253 | return error; |
---|
254 | } |
---|
255 | |
---|
256 | int WriteGen(NA_Alignment *aln, char *filename, int method) |
---|
257 | { |
---|
258 | int i; |
---|
259 | size_t j; |
---|
260 | int k; |
---|
261 | FILE *file; |
---|
262 | NA_Sequence *this_elem; |
---|
263 | if (aln == NULL) return (1); |
---|
264 | |
---|
265 | file = fopen(filename, "w"); |
---|
266 | if (file == NULL) |
---|
267 | { |
---|
268 | Warning("Cannot open file for output"); |
---|
269 | return (1); |
---|
270 | } |
---|
271 | |
---|
272 | for (j=0; j<aln->numelements; j++) |
---|
273 | { |
---|
274 | this_elem = &(aln->element[j]); |
---|
275 | if (method == ALL) |
---|
276 | { |
---|
277 | fprintf(file, |
---|
278 | "LOCUS %10s%8d bp %4s %10s %2d%5s%4d\n", |
---|
279 | this_elem->short_name, this_elem->seqlen+this_elem->offset, |
---|
280 | (this_elem->elementtype == DNA) ? "DNA" : |
---|
281 | (this_elem->elementtype == RNA) ? "RNA" : |
---|
282 | (this_elem->elementtype == MASK) ? "MASK" : |
---|
283 | (this_elem->elementtype == PROTEIN) ? "PROT" : "TEXT", |
---|
284 | this_elem->attr & IS_CIRCULAR ? "Circular" : "", |
---|
285 | this_elem->t_stamp.origin.dd, |
---|
286 | GDEmonth[this_elem->t_stamp.origin.mm-1], |
---|
287 | (this_elem->t_stamp.origin.yy>1900) ? this_elem->t_stamp.origin.yy : |
---|
288 | this_elem->t_stamp.origin.yy+1900); |
---|
289 | |
---|
290 | if (this_elem->description[0]) |
---|
291 | fprintf(file, "DEFINITION %s\n", this_elem->description); |
---|
292 | if (this_elem->seq_name[0]) |
---|
293 | fprintf(file, " ORGANISM %s\n", this_elem->seq_name); |
---|
294 | if (this_elem->id[0]) |
---|
295 | fprintf(file, " ACCESSION %s\n", this_elem->id); |
---|
296 | if (this_elem->authority[0]) |
---|
297 | fprintf(file, " AUTHORS %s\n", this_elem->authority); |
---|
298 | if (this_elem->comments) |
---|
299 | fprintf(file, "%s\n", this_elem->comments); |
---|
300 | fprintf(file, "ORIGIN"); |
---|
301 | if (this_elem->tmatrix) |
---|
302 | { |
---|
303 | for (i=0, k=0; k<this_elem->seqlen+this_elem->offset; k++) |
---|
304 | { |
---|
305 | if (i%60 == 0) |
---|
306 | fprintf(file, "\n%9d", i+1); |
---|
307 | if (i%10 == 0) |
---|
308 | fprintf(file, " "); |
---|
309 | fprintf(file, "%c", this_elem->tmatrix |
---|
310 | [getelem(this_elem, k)]); |
---|
311 | i++; |
---|
312 | } |
---|
313 | } |
---|
314 | else |
---|
315 | { |
---|
316 | for (i=0, k=0; k<this_elem->seqlen+this_elem->offset; k++) |
---|
317 | { |
---|
318 | if (i%60 == 0) |
---|
319 | fprintf(file, "\n%9d", i+1); |
---|
320 | if (i%10 == 0) |
---|
321 | fprintf(file, " "); |
---|
322 | fprintf(file, "%c", getelem(this_elem, k)); |
---|
323 | i++; |
---|
324 | } |
---|
325 | } |
---|
326 | fprintf(file, "\n//\n"); |
---|
327 | } |
---|
328 | } |
---|
329 | fclose(file); |
---|
330 | return (0); |
---|
331 | } |
---|
332 | |
---|
333 | |
---|
334 | void SetTime(void *b) |
---|
335 | { |
---|
336 | ARB_TIME *a=(ARB_TIME*)b; |
---|
337 | struct tm *tim; |
---|
338 | time_t clock; |
---|
339 | |
---|
340 | clock = time(0); |
---|
341 | tim = localtime(&clock); |
---|
342 | |
---|
343 | a->yy = tim->tm_year; |
---|
344 | a->mm = tim->tm_mon+1; |
---|
345 | a->dd = tim->tm_mday; |
---|
346 | a->hr = tim->tm_hour; |
---|
347 | a->mn = tim->tm_min; |
---|
348 | a->sc = tim->tm_sec; |
---|
349 | return; |
---|
350 | } |
---|
351 | |
---|