source: tags/arb_5.2/ARB_GDE/GDE_Genbank.cxx

Last change on this file was 5695, checked in by westram, 15 years ago
  • added functions to handle temporary files
    • GB_unique_filename (used pid of current process. before in most cases $ARB_PID was used, but that is only useful for arb_panic and arb_clean functionality. For normal tempfiles pid is the better choice - otherwise two processes of one ARB instance could generate the same tempfile name)
    • GB_create_tempfile
    • GB_fopen_tempfile
    • GB_is_privatefile (function for testing whether tempfiles have correct permissions - especially those tempfiles pre-generated by GB_create_tempfile and overwritten later by a shell script)
  • added GB_read_fp (similar to GB_read_file, but works on open filepointer)
  • added GB_append_suffix
  • added GB_remove_on_exit (removes files at program exit)
  • rewrote all functions creating temporary files. Files are opened in private mode (user only read/write) now and are properly removed after usage or when program exits.
  • bug-fixes:
    • GB_size_of_file(NULL)
    • GB_split_full_path (returned name as suffix if no suffix was present. Now returns NULL)
    • awt_get_selected_fullname returned /print.ps (instead of using cwd)
  • replaced String() by strdup() in GDE
  • fixed several calls to GB_unlink()
  • removed GB_getuid / GB_getpid
  • tweaked arb_panic (panicfile is private now! modified output and added cmd killall)
  • help-search now ignores comment lines in helpfiles
  • rewrote exporter (cause it was impossible to correctly handle tmp-files). Automatically appends suffix to outputfilename.
  • improved error handling in several functions
  • file selection boxes refresh automatically if directory modtime changes
  • When exporting frequencies from arb_phylo, dont write 3 files to tmp (of which 2 were always empty) - write only the one with content. I dont know what the file is good for, but since it is removed on exit now, it doesnt do any harm.
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 15.3 KB
Line 
1#include <sys/time.h>
2#include <stdio.h>
3// #include <malloc.h>
4#include <string.h>
5#include <time.h>
6
7//#include <xview/xview.h>
8//#include <xview/panel.h>
9
10#include <arbdb.h>
11#include <aw_root.hxx>
12#include <aw_device.hxx>
13#include <aw_window.hxx>
14#include <awt.hxx>
15
16#include "gde.hxx"
17#include "GDE_menu.h"
18#include "GDE_def.h"
19#include "GDE_extglob.h"
20
21/*
22  Copyright (c) 1989-1990, University of Illinois board of trustees.  All
23  rights reserved.  Written by Steven Smith at the Center for Prokaryote Genome
24  Analysis.  Design and implementation guidance by Dr. Gary Olsen and Dr.
25  Carl Woese.
26
27  Copyright (c) 1990,1991,1992 Steven Smith at the Harvard Genome Laboratory.
28  all rights reserved.
29
30  Copyright (c) 1993, Steven Smith, all rights reserved.
31
32*/
33
34/*
35 *   CheckType:  Check base composition to see if the sequence
36 *   appears to be an amino acid sequence.  If it is, pass back
37 *   TRUE, else FALSE.
38 */
39static int CheckType(char *seq,int len)
40{
41
42    int j,count1 = 0,count2 = 0;
43
44    for(j=0;j<len;j++)
45        if(((seq[j]|32) < 'z') && ((seq[j]|32) > 'a'))
46        {
47            count1++;
48            if(strchr("ACGTUNacgtun",seq[j]) == NULL)
49                count2++;
50        }
51
52    return( (count2 > count1/4)?TRUE:FALSE);
53}
54
55// ARB
56typedef struct ARB_TIME_STRUCT
57{
58    int yy;
59    int mm;
60    int dd;
61    int hr;
62    int mn;
63    int sc;
64} ARB_TIME;
65
66static void AsciiTime(void *b,char *asciitime)
67{
68    ARB_TIME *a=(ARB_TIME*)b;
69    int j;
70    char temp[GBUFSIZ];
71    //extern char month[12][6];
72
73    a->dd = 0;
74    a->yy = 0;
75    a->mm = 0;
76    sscanf(asciitime,"%d%5c%d",&(a->dd),temp,&(a->yy));
77    temp[5] = '\0';
78    for(j=0;j<12;j++)
79        if(strcmp(temp,GDEmonth[j]) == 0)
80            a->mm = j+1;
81    if(a->dd <0 || a->dd > 31 || a->yy < 0 || a->mm > 11)
82        SetTime(a);
83    return;
84}
85// ENDARB
86
87
88
89void ReadGen(char *filename,NA_Alignment *dataset,int type)
90{
91    int     done               = FALSE;
92    size_t  len                = 0;
93    size_t  j                  = 0;
94    int     count,IS_REALLY_AA = FALSE;
95    char    in_line[GBUFSIZ],c;
96    char   *buffer             = 0,*gencomments = NULL,fields[8][GBUFSIZ];
97    size_t  buflen             = 0;
98    int     genclen            = 0,curelem = 0,n = 0;
99    int     start_col          = -1;
100
101    type=0;count=0;
102
103    NA_Sequence *this_elem =0;
104    FILE *file;
105
106    ErrorOut5(0 != (file = fopen(filename,"r")),"No such file");
107
108    for(;fgets(in_line,GBUFSIZ,file) != 0;)
109    {
110        if(in_line[strlen(in_line)-1] == '\n')
111            in_line[strlen(in_line)-1] = '\0';
112        if(Find(in_line,"LOCUS"))
113        {
114            curelem = dataset->numelements++;
115            if(curelem == 0)
116            {
117                dataset->element=(NA_Sequence*)
118                    Calloc(5,sizeof(NA_Sequence));
119                dataset->maxnumelements = 5;
120            }
121            else if (curelem==dataset->maxnumelements)
122            {
123                (dataset->maxnumelements) *= 2;
124                dataset->element =(NA_Sequence*)
125                    Realloc((char*)dataset->element,
126                            dataset->maxnumelements * sizeof(NA_Sequence));
127            }
128            this_elem = &(dataset->element[curelem]);
129            n = sscanf(in_line,"%s %s %s %s %s %s %s %s",
130                       fields[0],fields[1],fields[2],fields[3],fields[4],
131                       fields[5],fields[6],fields[7]);
132            if(IS_REALLY_AA)
133            {
134                InitNASeq(this_elem,PROTEIN);
135            }
136            else if(Find(in_line,"DNA"))
137            {
138                InitNASeq(this_elem,DNA);
139            }
140            else if(Find(in_line,"RNA"))
141            {
142                InitNASeq(this_elem,RNA);
143            }
144            else if(Find(in_line,"MASK"))
145            {
146                InitNASeq(this_elem,MASK);
147            }
148            else if(Find(in_line,"TEXT"))
149            {
150                InitNASeq(this_elem,TEXT);
151            }
152            else if(Find(in_line,"PROT"))
153            {
154                InitNASeq(this_elem,PROTEIN);
155            }
156            else
157                InitNASeq(this_elem,DNA);
158
159            strncpy(this_elem->short_name,fields[1],31);
160            AsciiTime(&(this_elem->t_stamp.origin),fields[n-1]);
161            this_elem->attr = DEFAULT_X_ATTR;
162
163            if( Find(in_line, "Circular") )
164                this_elem->attr |= IS_CIRCULAR;
165
166            gencomments = NULL;
167            genclen = 0;
168        }
169        else if(Find(in_line,"DEFINITION"))
170            strncpy(this_elem->description,&(in_line[12]),79);
171
172        else if(Find(in_line,"AUTHOR"))
173            strncpy(this_elem->authority,&(in_line[12]),79);
174
175        else if(Find(in_line,"  ORGANISM"))
176            strncpy(this_elem->seq_name,&(in_line[12]),79);
177
178        else if(Find(in_line,"ACCESSION"))
179            strncpy(this_elem->id,&(in_line[12]),79);
180
181        else if(Find(in_line,"ORIGIN"))
182        {
183            done = FALSE;
184            len = 0;
185            for(;done == FALSE && fgets(in_line,GBUFSIZ,file) != 0;)
186            {
187                if(in_line[0] != '/')
188                {
189                    if(buflen == 0)
190                    {
191                        buflen = GBUFSIZ;
192                        buffer = Calloc(sizeof(char) ,
193                                        buflen);
194                    }
195
196                    else if (len+strlen(in_line) >= buflen)
197                    {
198                        buflen += GBUFSIZ;
199                        buffer = Realloc((char*)buffer,
200                                         sizeof(char)*buflen);
201                        for(j=buflen-GBUFSIZ
202                                ;j<buflen;j++)
203                            buffer[j] = '\0';
204                    }
205                    /*
206                     *  Search for the fist column of data (whitespace-number-whitespace)data
207                     */
208                    if(start_col == -1)
209                    {
210                        for(start_col=0; in_line[start_col] == ' ' || in_line[start_col] == '\t';start_col++) ;
211                        for(start_col++;strchr("1234567890", in_line[start_col]) != NULL;start_col++) ;
212                        for(start_col++; in_line[start_col] == ' ' || in_line[start_col] == '\t';start_col++) ;
213                    }
214                    for(j=start_col;(c = in_line[j]) != '\0';j++)
215                    {
216                        if ((c != '\n') && ((j-start_col + 1) % 11 !=0)) {
217                            buffer[len++] = c;
218                        }
219                    }
220                }
221                else
222                {
223                    AppendNA((NA_Base*)buffer,len,&(dataset->
224                                                    element[curelem]));
225                    for(j=0;j<len;j++)
226                        buffer[j] = '\0';
227                    len = 0;
228                    done = TRUE;
229                    dataset->element[curelem].comments
230                        = gencomments;
231                    dataset->element[curelem].comments_len=
232                        genclen - 1;
233                    dataset->element[curelem].
234                        comments_maxlen = genclen;
235
236                    gencomments = NULL;
237                    genclen = 0;
238                }
239            }
240            /*
241             *      Test if sequence should be converted by the translation table
242             *      If it looks like a protein...
243             */
244            if(dataset->element[curelem].rmatrix &&
245               IS_REALLY_AA == FALSE)
246            {
247                IS_REALLY_AA = CheckType((char*)dataset->element[curelem].
248                                         sequence,dataset->element[curelem].seqlen);
249
250                if(IS_REALLY_AA == FALSE)
251                    Ascii2NA((char*)dataset->element[curelem].sequence,
252                             dataset->element[curelem].seqlen,
253                             dataset->element[curelem].rmatrix);
254                else
255                    /*
256                     *      Force the sequence to be AA
257                     */
258                {
259                    dataset->element[curelem].elementtype = PROTEIN;
260                    dataset->element[curelem].rmatrix = NULL;
261                    dataset->element[curelem].tmatrix = NULL;
262                    dataset->element[curelem].col_lut =
263                        Default_PROColor_LKUP;
264                }
265            }
266        }
267        else if (Find(in_line,"ZZZZZ"))
268        {
269            Cfree(gencomments);
270            genclen = 0;
271        }
272        else
273        {
274            if (gencomments == NULL)
275            {
276                gencomments = strdup(in_line);
277                genclen = strlen(gencomments)+1;
278            }
279            else
280            {
281                genclen += strlen(in_line)+1;
282                gencomments = Realloc((char*)gencomments,genclen *
283                                      sizeof(char));
284                strncat(gencomments,in_line,GBUFSIZ);
285                strncat(gencomments,"\n",GBUFSIZ);
286            }
287        }
288    }
289    Cfree(buffer);
290    fclose(file);
291    for(j=0;j<dataset->numelements;j++)
292        dataset->maxlen = MAX(dataset->maxlen,
293                              dataset->element[j].seqlen+dataset->element[j].offset);
294    //return;
295}
296
297int WriteGen(NA_Alignment *aln,char *filename,int method,int maskable)
298{
299    int i;
300    size_t j;
301    int k,mask = -1;
302    FILE *file;
303    //extern char month[12][6];
304    NA_Sequence *this_elem;
305    char c;
306    if(aln == NULL)
307        return(1);
308    // ARB
309    //if(aln->na_ddata == NULL)
310    //  return(1);
311
312    file = fopen(filename,"w");
313    if(file == NULL)
314    {
315        Warning("Cannot open file for output");
316        return(1);
317    }
318
319    if(maskable && method != SELECT_REGION)
320        for(j=0;j<aln->numelements;j++)
321            if(aln->element[j].elementtype == MASK &&
322               aln->element[j].selected)
323                mask = j;
324
325    for(j=0;j<aln->numelements;j++)
326    {
327        this_elem = &(aln->element[j]);
328        if ((aln->element[j].selected && (int)j!=mask && method != SELECT_REGION)
329            ||(aln->element[j].subselected && method == SELECT_REGION)
330            || (method == ALL))
331        {
332            fprintf(file,
333                    "LOCUS       %10s%8d bp    %4s  %10s   %2d%5s%4d\n",
334                    this_elem->short_name,this_elem->seqlen+this_elem->offset,
335                    (this_elem->elementtype == DNA) ? "DNA":
336                    (this_elem->elementtype ==RNA)?"RNA":
337                    (this_elem->elementtype == MASK)?"MASK":
338                    (this_elem->elementtype == PROTEIN)?"PROT":"TEXT",
339                    this_elem->attr & IS_CIRCULAR?"Circular":"",
340                    this_elem->t_stamp.origin.dd,
341                    GDEmonth[this_elem->t_stamp.origin.mm-1],
342                    (this_elem->t_stamp.origin.yy>1900)?this_elem->t_stamp.origin.yy:
343                    this_elem->t_stamp.origin.yy+1900);
344
345            if(this_elem->description[0])
346                fprintf(file,"DEFINITION  %s\n",this_elem->description);
347            if(this_elem->seq_name[0])
348                fprintf(file,"  ORGANISM  %s\n",this_elem->seq_name);
349            if(this_elem->id[0])
350                fprintf(file," ACCESSION  %s\n",this_elem->id);
351            if(this_elem->authority[0])
352                fprintf(file,"   AUTHORS  %s\n",this_elem->authority);
353            if(this_elem->comments)
354                fprintf(file,"%s\n",this_elem->comments);
355            fprintf(file,"ORIGIN");
356            if(this_elem->tmatrix)
357            {
358                if(mask == -1)
359                {
360                    for(i=0,k=0;k<this_elem->seqlen+this_elem->offset;k++)
361                    {
362                        if(method == SELECT_REGION)
363                        {
364                            if(aln->selection_mask[k] == '1')
365                            {
366                                if(i%60 == 0)
367                                    fprintf(file,"\n%9d",i+1);
368                                if(i%10 == 0)
369                                    fprintf(file," ");
370                                fprintf(file,"%c",this_elem->tmatrix
371                                        [getelem(this_elem,k)]);
372                                i++;
373                            }
374                        }
375                        else
376                        {
377                            if(i%60 == 0)
378                                fprintf(file,"\n%9d",i+1);
379                            if(i%10 == 0)
380                                fprintf(file," ");
381                            fprintf(file,"%c",this_elem->tmatrix
382                                    [getelem(this_elem,k)]);
383                            i++;
384                        }
385                    }
386                }
387                else
388                {
389                    for(k=0;k<this_elem->seqlen+this_elem->offset;k++)
390                    {
391                        c =(char)getelem(&(aln->element[mask]),k);
392                        if(c != '0' && c!= '-')
393                        {
394                            if(k%60 == 0)
395                                fprintf(file,"\n%9d",k+1);
396                            if(k%10 == 0)
397                                fprintf(file," ");
398                            fprintf(file,"%c",this_elem->tmatrix
399                                    [getelem(this_elem,k)]);
400                        }
401                    }
402                }
403            }
404            else
405            {
406                if(mask == -1)
407                {
408                    for(i=0,k=0;k<this_elem->seqlen+this_elem->offset;k++)
409                    {
410                        if(method == SELECT_REGION)
411                        {
412                            if(aln->selection_mask[k] == '1')
413                            {
414                                if(i%60 == 0)
415                                    fprintf(file,"\n%9d",i+1);
416                                if(i%10 == 0)
417                                    fprintf(file," ");
418                                fprintf(file,"%c", getelem(this_elem,k));
419                                i++;
420                            }
421                        }
422                        else
423                        {
424                            if(i%60 == 0)
425                                fprintf(file,"\n%9d",i+1);
426                            if(i%10 == 0)
427                                fprintf(file," ");
428                            fprintf(file,"%c",getelem(this_elem,k));
429                            i++;
430                        }
431                    }
432                }
433                else
434                {
435                    for(k=0;k<this_elem->seqlen+this_elem->offset;k++)
436                    {
437                        c =(char)getelem(&(aln->element[mask]),k);
438                        if(c != '0' && c!= '-')
439                        {
440                            if(k%60 == 0)
441                                fprintf(file,"\n%9d",k+1);
442                            if(k%10 == 0)
443                                fprintf(file," ");
444                            fprintf(file,"%c",getelem(this_elem,k));
445                        }
446                    }
447                }
448            }
449            fprintf(file,"\n//\n");
450        }
451    }
452    fclose(file);
453    return(0);
454}
455
456
457void SetTime(void *b)
458{
459    ARB_TIME *a=(ARB_TIME*)b;
460    struct tm *tim; //*localtime();
461    time_t clock;
462
463    clock = time(0);
464    tim = localtime(&clock);
465
466    a->yy = tim->tm_year;
467    a->mm = tim->tm_mon+1;
468    a->dd = tim->tm_mday;
469    a->hr = tim->tm_hour;
470    a->mn = tim->tm_min;
471    a->sc = tim->tm_sec;
472    return;
473}
474
Note: See TracBrowser for help on using the repository browser.