root/trunk/ARB_GDE/GDE_Genbank.cxx

Revision 8254, 14.8 KB (checked in by westram, 6 months ago)
  • removed unused(-but-set) variables/params
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1#include "GDE_extglob.h"
2#include <ctime>
3
4/*
5  Copyright (c) 1989-1990, University of Illinois board of trustees.  All
6  rights reserved.  Written by Steven Smith at the Center for Prokaryote Genome
7  Analysis.  Design and implementation guidance by Dr. Gary Olsen and Dr.
8  Carl Woese.
9
10  Copyright (c) 1990,1991,1992 Steven Smith at the Harvard Genome Laboratory.
11  all rights reserved.
12
13  Copyright (c) 1993, Steven Smith, all rights reserved.
14
15*/
16
17static int CheckType(char *seq, int len) {
18    /*   CheckType:  Check base composition to see if the sequence
19     *   appears to be an amino acid sequence.  If it is, pass back
20     *   TRUE, else FALSE.
21     */
22    int j, count1 = 0, count2 = 0;
23
24    for (j=0; j<len; j++)
25        if (((seq[j]|32) < 'z') && ((seq[j]|32) > 'a'))
26        {
27            count1++;
28            if (strchr("ACGTUNacgtun", seq[j]) == NULL)
29                count2++;
30        }
31
32    return ((count2 > count1/4) ? TRUE : FALSE);
33}
34
35// ARB
36struct ARB_TIME {
37    int yy;
38    int mm;
39    int dd;
40    int hr;
41    int mn;
42    int sc;
43};
44
45static void AsciiTime(void *b, char *asciitime)
46{
47    ARB_TIME *a=(ARB_TIME*)b;
48    int j;
49    char temp[GBUFSIZ];
50
51    a->dd = 0;
52    a->yy = 0;
53    a->mm = 0;
54    sscanf(asciitime, "%d%5c%d", &(a->dd), temp, &(a->yy));
55    temp[5] = '\0';
56    for (j=0; j<12; j++)
57        if (strcmp(temp, GDEmonth[j]) == 0)
58            a->mm = j+1;
59    if (a->dd <0 || a->dd > 31 || a->yy < 0 || a->mm > 11)
60        SetTime(a);
61    return;
62}
63// ENDARB
64
65void ReadGen(char *filename, NA_Alignment *dataset) {
66    int     done               = FALSE;
67    size_t  len                = 0;
68    size_t  j                  = 0;
69    int     IS_REALLY_AA = FALSE;
70    char    in_line[GBUFSIZ], c;
71    char   *buffer             = 0, *gencomments = NULL, fields[8][GBUFSIZ];
72    size_t  buflen             = 0;
73    int     genclen            = 0, curelem = 0, n = 0;
74    int     start_col          = -1;
75
76    NA_Sequence *this_elem = 0;
77    FILE *file;
78
79    ErrorOut5(0 != (file = fopen(filename, "r")), "No such file");
80
81    for (; fgets(in_line, GBUFSIZ, file) != 0;)
82    {
83        if (in_line[strlen(in_line)-1] == '\n')
84            in_line[strlen(in_line)-1] = '\0';
85        if (Find(in_line, "LOCUS"))
86        {
87            curelem = dataset->numelements++;
88            if (curelem == 0)
89            {
90                dataset->element=(NA_Sequence*)
91                    Calloc(5, sizeof(NA_Sequence));
92                dataset->maxnumelements = 5;
93            }
94            else if (curelem==dataset->maxnumelements)
95            {
96                (dataset->maxnumelements) *= 2;
97                dataset->element = (NA_Sequence*)
98                    Realloc((char*)dataset->element,
99                            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 (j=buflen-GBUFSIZ
173                                ; j<buflen; j++)
174                            buffer[j] = '\0';
175                    }
176                    // Search for the fist column of data (whitespace-number-whitespace)data
177                    if (start_col == -1)
178                    {
179                        for (start_col=0; in_line[start_col] == ' ' || in_line[start_col] == '\t'; start_col++) ;
180                        for (start_col++; strchr("1234567890", in_line[start_col]) != NULL; start_col++) ;
181                        for (start_col++; in_line[start_col] == ' ' || in_line[start_col] == '\t'; start_col++) ;
182                    }
183                    for (j=start_col; (c = in_line[j]) != '\0'; j++)
184                    {
185                        if ((c != '\n') && ((j-start_col + 1) % 11 != 0)) {
186                            buffer[len++] = c;
187                        }
188                    }
189                }
190                else
191                {
192                    AppendNA((NA_Base*)buffer, len, &(dataset->
193                                                    element[curelem]));
194                    for (j=0; j<len; j++)
195                        buffer[j] = '\0';
196                    len = 0;
197                    done = TRUE;
198                    dataset->element[curelem].comments
199                        = gencomments;
200                    dataset->element[curelem].comments_len=
201                        genclen - 1;
202                    dataset->element[curelem].
203                        comments_maxlen = genclen;
204
205                    gencomments = NULL;
206                    genclen = 0;
207                }
208            }
209            /* Test if sequence should be converted by the translation table
210             * If it looks like a protein...
211             */
212            if (dataset->element[curelem].rmatrix &&
213               IS_REALLY_AA == FALSE)
214            {
215                IS_REALLY_AA = CheckType((char*)dataset->element[curelem].
216                                         sequence, dataset->element[curelem].seqlen);
217
218                if (IS_REALLY_AA == FALSE)
219                    Ascii2NA((char*)dataset->element[curelem].sequence,
220                             dataset->element[curelem].seqlen,
221                             dataset->element[curelem].rmatrix);
222                else {
223                    // Force the sequence to be AA
224                    dataset->element[curelem].elementtype = PROTEIN;
225                    dataset->element[curelem].rmatrix = NULL;
226                    dataset->element[curelem].tmatrix = NULL;
227                    dataset->element[curelem].col_lut = Default_PROColor_LKUP;
228                }
229            }
230        }
231        else if (Find(in_line, "ZZZZZ"))
232        {
233            Cfree(gencomments);
234            genclen = 0;
235        }
236        else
237        {
238            if (gencomments == NULL)
239            {
240                gencomments = strdup(in_line);
241                genclen = strlen(gencomments)+1;
242            }
243            else
244            {
245                genclen += strlen(in_line)+1;
246                gencomments = Realloc((char*)gencomments, genclen * sizeof(char));
247                strncat(gencomments, in_line, GBUFSIZ);
248                strncat(gencomments, "\n", GBUFSIZ);
249            }
250        }
251    }
252    Cfree(buffer);
253    fclose(file);
254    for (j=0; j<dataset->numelements; j++)
255        dataset->maxlen = MAX(dataset->maxlen,
256                              dataset->element[j].seqlen+dataset->element[j].offset);
257}
258
259int WriteGen(NA_Alignment *aln, char *filename, int method, int maskable)
260{
261    int i;
262    size_t j;
263    int k, mask = -1;
264    FILE *file;
265    NA_Sequence *this_elem;
266    char c;
267    if (aln == NULL) return (1);
268
269    file = fopen(filename, "w");
270    if (file == NULL)
271    {
272        Warning("Cannot open file for output");
273        return (1);
274    }
275
276    if (maskable && method != SELECT_REGION)
277        for (j=0; j<aln->numelements; j++)
278            if (aln->element[j].elementtype == MASK &&
279               aln->element[j].selected)
280                mask = j;
281
282    for (j=0; j<aln->numelements; j++)
283    {
284        this_elem = &(aln->element[j]);
285        if ((aln->element[j].selected && (int)j!=mask && method != SELECT_REGION)
286            ||(aln->element[j].subselected && method == SELECT_REGION)
287            || (method == ALL))
288        {
289            fprintf(file,
290                    "LOCUS       %10s%8d bp    %4s  %10s   %2d%5s%4d\n",
291                    this_elem->short_name, this_elem->seqlen+this_elem->offset,
292                    (this_elem->elementtype == DNA) ? "DNA" :
293                    (this_elem->elementtype == RNA) ? "RNA" :
294                    (this_elem->elementtype == MASK) ? "MASK" :
295                    (this_elem->elementtype == PROTEIN) ? "PROT" : "TEXT",
296                    this_elem->attr & IS_CIRCULAR ? "Circular" : "",
297                    this_elem->t_stamp.origin.dd,
298                    GDEmonth[this_elem->t_stamp.origin.mm-1],
299                    (this_elem->t_stamp.origin.yy>1900) ? this_elem->t_stamp.origin.yy :
300                    this_elem->t_stamp.origin.yy+1900);
301
302            if (this_elem->description[0])
303                fprintf(file, "DEFINITION  %s\n", this_elem->description);
304            if (this_elem->seq_name[0])
305                fprintf(file, "  ORGANISM  %s\n", this_elem->seq_name);
306            if (this_elem->id[0])
307                fprintf(file, " ACCESSION  %s\n", this_elem->id);
308            if (this_elem->authority[0])
309                fprintf(file, "   AUTHORS  %s\n", this_elem->authority);
310            if (this_elem->comments)
311                fprintf(file, "%s\n", this_elem->comments);
312            fprintf(file, "ORIGIN");
313            if (this_elem->tmatrix)
314            {
315                if (mask == -1)
316                {
317                    for (i=0, k=0; k<this_elem->seqlen+this_elem->offset; k++)
318                    {
319                        if (method == SELECT_REGION)
320                        {
321                            if (aln->selection_mask[k] == '1')
322                            {
323                                if (i%60 == 0)
324                                    fprintf(file, "\n%9d", i+1);
325                                if (i%10 == 0)
326                                    fprintf(file, " ");
327                                fprintf(file, "%c", this_elem->tmatrix
328                                        [getelem(this_elem, k)]);
329                                i++;
330                            }
331                        }
332                        else
333                        {
334                            if (i%60 == 0)
335                                fprintf(file, "\n%9d", i+1);
336                            if (i%10 == 0)
337                                fprintf(file, " ");
338                            fprintf(file, "%c", this_elem->tmatrix
339                                    [getelem(this_elem, k)]);
340                            i++;
341                        }
342                    }
343                }
344                else
345                {
346                    for (k=0; k<this_elem->seqlen+this_elem->offset; k++)
347                    {
348                        c = (char)getelem(&(aln->element[mask]), k);
349                        if (c != '0' && c != '-')
350                        {
351                            if (k%60 == 0)
352                                fprintf(file, "\n%9d", k+1);
353                            if (k%10 == 0)
354                                fprintf(file, " ");
355                            fprintf(file, "%c", this_elem->tmatrix
356                                    [getelem(this_elem, k)]);
357                        }
358                    }
359                }
360            }
361            else
362            {
363                if (mask == -1)
364                {
365                    for (i=0, k=0; k<this_elem->seqlen+this_elem->offset; k++)
366                    {
367                        if (method == SELECT_REGION)
368                        {
369                            if (aln->selection_mask[k] == '1')
370                            {
371                                if (i%60 == 0)
372                                    fprintf(file, "\n%9d", i+1);
373                                if (i%10 == 0)
374                                    fprintf(file, " ");
375                                fprintf(file, "%c", getelem(this_elem, k));
376                                i++;
377                            }
378                        }
379                        else
380                        {
381                            if (i%60 == 0)
382                                fprintf(file, "\n%9d", i+1);
383                            if (i%10 == 0)
384                                fprintf(file, " ");
385                            fprintf(file, "%c", getelem(this_elem, k));
386                            i++;
387                        }
388                    }
389                }
390                else
391                {
392                    for (k=0; k<this_elem->seqlen+this_elem->offset; k++)
393                    {
394                        c = (char)getelem(&(aln->element[mask]), k);
395                        if (c != '0' && c != '-')
396                        {
397                            if (k%60 == 0)
398                                fprintf(file, "\n%9d", k+1);
399                            if (k%10 == 0)
400                                fprintf(file, " ");
401                            fprintf(file, "%c", getelem(this_elem, k));
402                        }
403                    }
404                }
405            }
406            fprintf(file, "\n//\n");
407        }
408    }
409    fclose(file);
410    return (0);
411}
412
413
414void SetTime(void *b)
415{
416    ARB_TIME *a=(ARB_TIME*)b;
417    struct tm *tim;
418    time_t clock;
419
420    clock = time(0);
421    tim = localtime(&clock);
422
423    a->yy = tim->tm_year;
424    a->mm = tim->tm_mon+1;
425    a->dd = tim->tm_mday;
426    a->hr = tim->tm_hour;
427    a->mn = tim->tm_min;
428    a->sc = tim->tm_sec;
429    return;
430}
Note: See TracBrowser for help on using the browser.