source: tags/arb_5.2/ARB_GDE/GDE_HGLfile.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: 26.7 KB
Line 
1#include <sys/time.h>
2#include <stdio.h>
3#include <stdlib.h>
4// #include <malloc.h>
5#include <string.h>
6#include <time.h>
7
8//#include <xview/xview.h>
9//#include <xview/panel.h>
10
11#include <arbdb.h>
12#include <aw_root.hxx>
13#include <aw_device.hxx>
14#include <aw_window.hxx>
15#include <awt.hxx>
16
17#include "gde.hxx"
18#include "GDE_menu.h"
19#include "GDE_def.h"
20#include "GDE_extglob.h"
21
22/*
23  Copyright (c) 1990,1991,1992 Steven Smith at the Harvard Genome Laboratory.
24  All rights reserved.
25*/
26
27static void AdjustGroups(NA_Alignment *aln)
28{
29    size_t i;
30    size_t j;
31    int c,done=FALSE;
32
33#ifdef HGL
34    return;
35#else
36    for(c=0;c<200 && !done;c++)
37    {
38        for(j=1;j<=aln->numgroups;j++)
39        {
40            done = FALSE;
41            for(i=0;i<aln->numelements;i++)
42            {
43                if(aln->element[i].groupid == j)
44                {
45                    if(aln->element[i].groupf!=NULL ||
46                       aln->element[i].groupb!=NULL)
47                        done = TRUE;
48                    else
49                        aln->element[i].groupid = 0;
50                }
51            }
52            if(done == FALSE)
53            {
54                for(i=0;i<aln->numelements;i++)
55                    if(aln->element[i].groupid == aln->numgroups)
56                        aln->element[i].groupid = j;
57                aln->numgroups--;
58            }
59        }
60        if(aln->numgroups == 0)
61            done = TRUE;
62    }
63    return;
64#endif
65}
66
67static void StripSpecial(char *string)
68{
69    int i,j,len;
70    i=0;
71
72    len = strlen(string);
73    for(j=0;j<len;j++)
74    {
75        if(string[j] == '"')
76            string[j] = '`';
77        else if(string[j] == '{')
78            string[j] = '(';
79        else if(string[j] == '}')
80            string[j] = ')';
81    }
82    //return;
83}
84
85
86static void RemoveQuotes(char *string)
87{
88    int i, j, len;
89
90    len = strlen(string);
91    for(j=0;j<len;j++) {
92        if(string[j] == '"') string[j] = ' ';
93    }
94
95    for(j=0;string[j]==' ' && j<(int)strlen(string);j++) ;
96
97    len = strlen(string);
98    for(i=0;i<len - j;i++) string[i] = string[i+j];
99
100    for(j=strlen(string)-1; j>=0 && (string[j]=='\n'||string[j]==' '); j--) {
101        string[j] = '\0';
102    }
103
104    //return;
105}
106
107/*
108 *       OverWrite(), overwrite all non-default data from a sequence entry
109 *       onto any entry with the same ID or short name.
110 */
111static int OverWrite(NA_Sequence *thiss,NA_Alignment *aln)
112{
113    size_t j;
114    int indx = -1;
115    NA_Sequence *that;
116    for(j=0;j<aln->numelements;j++)
117    {
118        if(Find2(thiss->id,aln->element[j].id) != -1)
119            if(Find2(aln->element[j].id,thiss->id) != -1)
120                indx = j;
121    }
122    if(indx == -1)
123        for(j=0;j<aln->numelements;j++)
124        {
125            if(Find2(thiss->short_name,aln->element[j].short_name)!= -1)
126                if(Find2(aln->element[j].short_name,thiss->short_name)!= -1)
127                    indx = j;
128        }
129    if(indx != -1)
130    {
131        that = &(aln->element[indx]);
132        if(thiss->seq_name[0])
133            strcpy(that->seq_name,thiss->seq_name);
134        if(thiss->barcode[0])
135            strcpy(that->barcode,thiss->barcode);
136        if(thiss->contig[0])
137            strcpy(that->contig,thiss->contig);
138        if(thiss->membrane[0])
139            strcpy(that->membrane,thiss->membrane);
140        if(thiss->authority[0])
141            strcpy(that->authority,thiss->authority);
142        if(thiss->short_name[0])
143            strcpy(that->short_name,thiss->short_name);
144        if(thiss->description[0])
145            strcpy(that->description,thiss->description);
146        if(thiss->sequence)
147        {
148            free((char*)that->sequence);
149            that->sequence = thiss->sequence;
150            that->seqlen = thiss->seqlen;
151            that->seqmaxlen = thiss->seqmaxlen;
152        }
153        if(thiss->baggage)
154        {
155            that->baggage_len += thiss->baggage_len;
156            that->baggage_maxlen += thiss->baggage_maxlen;
157            if(that->baggage)
158                that->baggage =
159                    Realloc(that->baggage,that->baggage_maxlen*sizeof(char));
160            else
161                that->baggage = Calloc(that->baggage_maxlen,sizeof(char));
162            strncat(that->baggage,thiss->baggage,that->baggage_maxlen);
163        }
164        if(thiss->comments)
165        {
166            that->comments_len += thiss->comments_len;
167            that->comments_maxlen += thiss->comments_maxlen;
168            if(that->comments)
169                that->comments =
170                    Realloc(that->comments,that->comments_maxlen*sizeof(char));
171            else
172                that->comments = Calloc(that->comments_maxlen,sizeof(char));
173            strncat(that->comments,thiss->comments,that->comments_maxlen);
174        }
175        if(thiss->cmask)
176        {
177            free((char*)that->cmask);
178            that->cmask = thiss->cmask;
179        }
180        if(thiss->offset != that->offset)
181            that->offset = thiss->offset;
182        if(thiss->attr != 0)
183            that->attr = thiss->attr;
184        if(thiss->groupid != 0)
185        {
186            that->groupid = thiss->groupid;
187        }
188        that->groupb = NULL;
189        that->groupf = NULL;
190    }
191
192    return(indx);
193}
194
195void ReadGDE(char *filename,NA_Alignment *dataset,int type)
196{
197    int          done               = FALSE;
198    size_t       len                = 0, j=0;
199    int          success,count,temp = 0;
200    char         in_line[GBUFSIZ],c;
201    char        *buffer,*line;
202    size_t       buflen             = GBUFSIZ;
203    int          genclen            = 0,curelem = 0;
204    NA_Sequence *this_elem          = NULL,temp_elem;
205    FILE        *file;
206
207    type=0;count=0;c=0;
208
209    ErrorOut5(0!=(file = fopen(filename,"r")),"No such file");
210
211    for(;fgets(in_line,GBUFSIZ,file) != 0;)
212    {
213        for(line = in_line;line[0]==' ' || line[0] == '\t';line++) ;
214
215        if(Find2(line,"{")==0)
216        {
217            this_elem = &temp_elem;
218            InitNASeq(this_elem,DNA);
219            this_elem->offset = -(dataset->rel_offset);
220        }
221        else if(Find2(line,"type")==0)
222        {
223            if(Find(line,"DNA"))
224            {
225                this_elem->elementtype = DNA;
226                this_elem->tmatrix = Default_DNA_Trans;
227                this_elem->rmatrix = Default_NA_RTrans;
228            }
229            else if(Find(line,"RNA"))
230            {
231                this_elem->elementtype = RNA;
232                this_elem->tmatrix = Default_RNA_Trans;
233                this_elem->rmatrix = Default_NA_RTrans;
234            }
235            else if(Find(line,"MASK"))
236            {
237                this_elem->elementtype = MASK;
238                this_elem->rmatrix = NULL;
239                this_elem->tmatrix = NULL;
240                this_elem->col_lut = NULL;
241            }
242            else if(Find(line,"TEXT"))
243            {
244                this_elem->elementtype = TEXT;
245                this_elem->rmatrix = NULL;
246                this_elem->tmatrix = NULL;
247                this_elem->col_lut = NULL;
248            }
249            else if(Find(line,"PROT"))
250            {
251                this_elem->elementtype = PROTEIN;
252                this_elem->rmatrix = NULL;
253                this_elem->tmatrix = NULL;
254                this_elem->col_lut = Default_PROColor_LKUP;
255            }
256            /*
257              this_elem->attr = DEFAULT_X_ATTR;
258            */
259        }
260        else if(Find2(line,"circular")==0)
261        {
262            sscanf(line,"%*s %d",&temp);
263            if(temp == 1)
264            {
265                this_elem->attr |= IS_CIRCULAR;
266            }
267            else
268            {
269                this_elem->attr &= ~IS_CIRCULAR;
270            }
271        }
272        else if(Find2(line,"orig_direction")==0)
273        {
274            sscanf(line,"%*s %d",&temp);
275            if(temp == 1)
276            {
277                this_elem->attr |= IS_ORIG_5_TO_3;
278                this_elem->attr &= ~IS_ORIG_3_TO_5;
279            }
280            else
281            {
282                this_elem->attr |= IS_ORIG_3_TO_5;
283                this_elem->attr &= ~IS_ORIG_5_TO_3;
284            }
285        }
286        else if(Find2(line,"direction")==0)
287        {
288            sscanf(line,"%*s %d",&temp);
289            if(temp == 1)
290            {
291                this_elem->attr |= IS_5_TO_3;
292                this_elem->attr &= ~IS_3_TO_5;
293            }
294            else
295            {
296                this_elem->attr |= IS_3_TO_5;
297                this_elem->attr &= ~IS_5_TO_3;
298            }
299        }
300        else if(Find2(line,"orig_strand")==0)
301        {
302            sscanf(line,"%*s %d",&temp);
303            if(temp == 1)
304            {
305                this_elem->attr |= IS_ORIG_PRIMARY;
306                this_elem->attr &= ~IS_ORIG_SECONDARY;
307            }
308            else
309            {
310                this_elem->attr |= IS_ORIG_SECONDARY;
311                this_elem->attr &= ~IS_ORIG_PRIMARY;
312            }
313        }
314        else if(Find2(line,"strandedness")==0)
315        {
316            sscanf(line,"%*s %d",&temp);
317            if(temp == 1)
318            {
319                this_elem->attr |= IS_PRIMARY;
320                this_elem->attr &= ~IS_SECONDARY;
321            }
322            else
323            {
324                this_elem->attr |= IS_SECONDARY;
325                this_elem->attr &= ~IS_PRIMARY;
326            }
327        }
328        else if(Find2(line,"creator")==0)
329        {
330            sscanf(line,"%*s %[^\n]",this_elem->authority);
331            RemoveQuotes(this_elem->authority);
332        }
333        else if(Find2(line,"longname")==0)
334        {
335            sscanf(line,"%*s %[^\n]",this_elem->seq_name);
336            RemoveQuotes(this_elem->seq_name);
337        }
338        else if(Find2(line,"descrip")==0)
339        {
340            sscanf(line,"%*s %[^\n]",this_elem->description);
341            RemoveQuotes(this_elem->description);
342        }
343        else if(Find2(line,"name")==0)
344        {
345            sscanf(line,"%*s %[^\n]",this_elem->short_name);
346            RemoveQuotes(this_elem->short_name);
347        }
348        else if(Find2(line,"group-ID")==0)
349        {
350            sscanf(line,"%*s %zu",&(this_elem->groupid));
351            dataset->numgroups =
352                MAX(this_elem->groupid, dataset->numgroups);
353        }
354        else if(Find2(line,"sequence-ID")==0)
355        {
356            sscanf(line,"%*s %[^\n]",this_elem->id);
357            RemoveQuotes(this_elem->id);
358        }
359        else if(Find2(line,"barcode")==0)
360        {
361            sscanf(line,"%*s %[^\n]",this_elem->barcode);
362            RemoveQuotes(this_elem->barcode);
363        }
364        else if(Find2(line,"membrane")==0)
365        {
366            sscanf(line,"%*s %[^\n]",this_elem->membrane);
367            RemoveQuotes(this_elem->membrane);
368        }
369        else if(Find2(line,"contig")==0)
370        {
371            sscanf(line,"%*s %[^\n]",this_elem->contig);
372            RemoveQuotes(this_elem->contig);
373        }
374        else if(Find2(line,"creation-date")==0)
375        {
376            sscanf(line,"%*s %2d%*c%2d%*c%2d%*c%2d%*c%2d%*c%2d\n",
377                   &(this_elem->t_stamp.origin.mm),
378                   &(this_elem->t_stamp.origin.dd),
379                   &(this_elem->t_stamp.origin.yy),
380                   &(this_elem->t_stamp.origin.hr),
381                   &(this_elem->t_stamp.origin.mn),
382                   &(this_elem->t_stamp.origin.sc));
383        }
384        else if(Find2(line,"offset")==0)
385        {
386            sscanf(line,"%*s %d",&(this_elem->offset));
387            this_elem->offset -= dataset->rel_offset;
388        }
389        else if(Find2(line,"comments")==0)
390        {
391            if(this_elem->comments_maxlen ==0)
392                buflen = 2048;
393            else
394                buflen = this_elem->comments_maxlen;
395
396            done = FALSE;
397            len = this_elem->comments_len;
398
399            for(;line[0] != '"';line++)
400                if(line[0] == '\0')
401                    ErrorOut5(0,"Error in input file");
402            line++;
403            buffer = Calloc(buflen,sizeof(char));
404            for(;!done;)
405            {
406                for(j=0;j<strlen(line);j++)
407                {
408                    if(len+strlen(line) >= buflen)
409                    {
410                        buflen *=2;
411                        buffer = Realloc(buffer,
412                                         buflen*sizeof(char));
413                    }
414                    if(line[j] == '"') done = TRUE;
415
416                    else
417                        buffer[len++] = line[j];
418                }
419                /*
420                 *       Check pad with null
421                 */
422                buffer[len] = '\0';
423                if(!done)
424                {
425                    if(fgets(in_line,GBUFSIZ,file) == 0)
426                        done = TRUE;
427                    line = in_line;
428                }
429            }
430            this_elem->comments = buffer;
431            this_elem->comments_len = strlen(buffer);
432            this_elem->comments_maxlen = buflen;
433            RemoveQuotes(this_elem->comments);
434        }
435        else if(Find2(line,"sequence")==0)
436        {
437            buflen = GBUFSIZ;
438            done = FALSE;
439            len = 0;
440
441            buffer = Calloc(buflen,sizeof(char));
442            for(;line[0] != '"';line++)
443                if(line[0] == '\0')
444                    ErrorOut5(0,"Error in input file");
445
446            line++;
447            for(;!done;)
448            {
449                for(j=0;j<strlen(line);j++)
450                {
451                    if(len+strlen(line) >= buflen)
452                    {
453                        buflen *=2;
454                        buffer = Realloc(buffer,
455                                         buflen*sizeof(char));
456                    }
457                    if(line[j] == '"') done = TRUE;
458
459                    else
460                    {
461                        /*
462                         *                      If not text, ignore spaces...
463                         */
464                        if(this_elem->elementtype !=TEXT)
465                        {
466                            if(line[j]!=' ' && line[j] !=
467                               '\t' && line[j] != '\n')
468                                buffer[len++] = line[j];
469                        }
470                        else
471                            if(line[j] != '\t' && line[j] != '\n')
472                                buffer[len++] = line[j];
473                    }
474                }
475                if(!done)
476                {
477                    if(fgets(in_line,GBUFSIZ,file) == 0)
478                        done = TRUE;
479                    line = in_line;
480                }
481            }
482            if(this_elem->rmatrix) {
483                for(j=0;j<len;j++) {
484                    buffer[j]=this_elem->rmatrix[(unsigned char)buffer[j]];
485                }
486            }
487            this_elem->sequence =(NA_Base*)buffer;
488            this_elem->seqlen = len;
489            this_elem->seqmaxlen = buflen;
490        }
491
492        else if (Find2(line,"}")==0)
493        {
494            if(this_elem->id[0] == '\0')
495                strncpy(this_elem->id,uniqueID(),79);
496            if(this_elem->short_name[0] == '\0')
497                strncpy(this_elem->short_name,this_elem->id,79);
498            if(this_elem->seqlen == 0)
499                this_elem->protect=
500                    PROT_BASE_CHANGES+
501                    PROT_GREY_SPACE+
502                    PROT_WHITE_SPACE+
503                    PROT_TRANSLATION;
504            genclen = 0;
505            /*
506             *              Make a new sequence entry...
507             */
508
509            success = -1;
510            if(OVERWRITE)
511                success = OverWrite(this_elem,dataset);
512
513            if(success == -1)
514            {
515                curelem = dataset->numelements++;
516                if(curelem == 0)
517                {
518                    dataset->element=(NA_Sequence*)
519                        Calloc(5,sizeof(NA_Sequence));
520                    dataset->maxnumelements = 5;
521                }
522                else if (curelem==dataset->maxnumelements)
523                {
524                    (dataset->maxnumelements) *= 2;
525                    dataset->element =(NA_Sequence*)
526                        Realloc((char*)dataset->element,
527                                dataset->maxnumelements * sizeof(NA_Sequence));
528                }
529                dataset->element[curelem] = *this_elem;
530            }
531        }
532        else if(this_elem != NULL)
533        {
534            if (this_elem->baggage == NULL)
535            {
536                this_elem->baggage = strdup(line);
537                this_elem->baggage_maxlen =
538                    this_elem->baggage_len =
539                    strlen(this_elem->baggage)+1;
540            }
541            else
542            {
543                this_elem->baggage_len += strlen(line)+1;
544                this_elem->baggage = Realloc(
545                                             this_elem->baggage,this_elem->baggage_len *
546                                             sizeof(char));
547                this_elem->baggage_maxlen =
548                    this_elem->baggage_len;
549
550                strncat(this_elem->baggage,line,GBUFSIZ);
551            }
552        }
553    }
554
555    fclose(file);
556    NormalizeOffset(dataset);
557    Regroup(dataset);
558    AdjustGroups(dataset);
559    return;
560}
561
562
563int WriteGDE(NA_Alignment *aln,char *filename,int method,int maskable)
564{
565    int i;
566    size_t j;
567    int k,mask = -1;
568    FILE *file;
569    NA_Sequence *this_elem;
570    //extern char month[12][6];
571
572    if(aln == NULL)
573        return(1);
574    //ARB
575    //if(aln->na_ddata == NULL)
576    //      return(1);
577    //ARB END
578    file = fopen(filename,"w");
579    if(file == NULL)
580    {
581        Warning("Cannot open file for output");
582        return(1);
583    }
584
585    if(maskable && method != SELECT_REGION)
586        for(j=0;j<aln->numelements;j++)
587            if(aln->element[j].elementtype == MASK &&
588               aln->element[j].selected)
589                mask = j;
590
591    for(j=0;j<aln->numelements;j++)
592    {
593        if((aln->element[j].selected && (int)j!=mask && method!=SELECT_REGION)
594           || (method == ALL)
595           || (aln->element[j].subselected && method == SELECT_REGION))
596        {
597            this_elem = &(aln->element[j]);
598            /*SeqNorm(this_elem);*/
599            fprintf(file,"{\n");
600            if(this_elem->short_name[0])
601                fprintf(file,"name      \"%s\"\n",this_elem->short_name);
602            switch(this_elem->elementtype)
603            {
604                case DNA:
605                    fprintf(file,"type              \"DNA\"\n");
606                    break;
607                case RNA:
608                    fprintf(file,"type              \"RNA\"\n");
609                    break;
610                case PROTEIN:
611                    fprintf(file,"type              \"PROTEIN\"\n");
612                    break;
613                case MASK:
614                    fprintf(file,"type              \"MASK\"\n");
615                    break;
616                case TEXT:
617                    fprintf(file,"type              \"TEXT\"\n");
618                    break;
619            }
620            if(this_elem->seq_name[0])
621                fprintf(file,"longname  %s\n",this_elem->seq_name);
622
623            if(this_elem->id[0])
624                fprintf(file,"sequence-ID       \"%s\"\n",this_elem->id);
625            RemoveQuotes(this_elem->barcode);
626            RemoveQuotes(this_elem->contig);
627
628            if(this_elem->barcode[0])
629                fprintf(file,"barcode           \"%s\"\n",this_elem->barcode);
630            if(this_elem->membrane[0])
631                fprintf(file,"membrane          \"%s\"\n",this_elem->membrane);
632            if(this_elem->contig[0])
633                fprintf(file,"contig            \"%s\"\n",this_elem->contig);
634            if(this_elem->description[0])
635                fprintf(file,"descrip           \"%s\"\n",this_elem->description);
636            if(this_elem->authority[0])
637                fprintf(file,"creator           \"%s\"\n",this_elem->authority);
638            if(this_elem->groupid)
639                fprintf(file,"group-ID          %zu\n", this_elem->groupid);
640            if(this_elem->offset+aln->rel_offset && method!=SELECT_REGION)
641                fprintf(file,"offset            %d\n",this_elem->offset+aln->rel_offset);
642            if(method == SELECT_REGION)
643            {
644                /*
645                 *       If selecting a region, the offset should be moved to the first
646                 *       non-'0' space in the mask.
647                 */
648                for(k=this_elem->offset;k<aln->selection_mask_len && aln->selection_mask[k] == '0';k++) ;
649                fprintf(file,"offset        %d\n", aln->rel_offset+k);
650            }
651            if(this_elem->t_stamp.origin.mm != 0)
652                fprintf(file,
653                        "creation-date      %2d/%2d/%2d %2d:%2d:%2d\n",
654                        this_elem->t_stamp.origin.mm,
655                        this_elem->t_stamp.origin.dd,
656                        (this_elem->t_stamp.origin.yy)>1900?
657                        (this_elem->t_stamp.origin.yy-1900):
658                        (this_elem->t_stamp.origin.yy),
659                        this_elem->t_stamp.origin.hr,
660                        this_elem->t_stamp.origin.mn,
661                        this_elem->t_stamp.origin.sc);
662            if((this_elem->attr & IS_ORIG_5_TO_3) &&
663               ((this_elem->attr & IS_ORIG_3_TO_5) == 0))
664                fprintf(file,"orig_direction    1\n");
665
666            if((this_elem->attr & IS_CIRCULAR) )
667                fprintf(file,"circular  1\n");
668
669            if((this_elem->attr & IS_5_TO_3) &&
670               ((this_elem->attr & IS_3_TO_5) == 0))
671                fprintf(file,"direction 1\n");
672
673            if((this_elem->attr & IS_ORIG_3_TO_5) &&
674               ((this_elem->attr & IS_ORIG_5_TO_3) == 0))
675                fprintf(file,"orig_direction    -1\n");
676
677            if((this_elem->attr & IS_3_TO_5) &&
678               ((this_elem->attr & IS_5_TO_3) == 0))
679                fprintf(file,"direction -1\n");
680
681            if((this_elem->attr & IS_ORIG_PRIMARY) &&
682               ((this_elem->attr & IS_ORIG_SECONDARY) == 0))
683                fprintf(file,"orig_strand       1\n");
684
685            if((this_elem->attr & IS_PRIMARY) &&
686               ((this_elem->attr & IS_SECONDARY) == 0))
687                fprintf(file,"strandedness      1\n");
688
689            if(((this_elem->attr & IS_ORIG_PRIMARY) == 0) &&
690               (this_elem->attr & IS_ORIG_SECONDARY))
691                fprintf(file,"orig_strand       2\n");
692
693            if(((this_elem->attr & IS_PRIMARY) == 0) &&
694               (this_elem->attr & IS_SECONDARY))
695                fprintf(file,"strandedness      2\n");
696
697            if(this_elem->comments != NULL)
698            {
699                StripSpecial(this_elem->comments);
700                fprintf(file,"comments  \"%s\"\n",this_elem->comments);
701            }
702            if(this_elem->baggage != NULL)
703            {
704                if(this_elem->
705                   baggage[strlen(this_elem->baggage)-1] == '\n')
706                    fprintf(file,"%s",this_elem->baggage);
707                else
708                    fprintf(file,"%s\n",this_elem->baggage);
709            }
710            fprintf(file,"sequence  \"");
711            if(this_elem->tmatrix)
712            {
713                if(mask == -1)
714                {
715                    for(k=this_elem->offset;k<this_elem->seqlen+this_elem->offset;k++)
716                    {
717                        if(k%60 == 0)
718                            putc('\n',file);
719                        if(method == SELECT_REGION)
720                        {
721                            if(aln->selection_mask[k] == '1')
722                                putc(this_elem->tmatrix[getelem(this_elem,k)],
723                                     file);
724                        }
725                        else
726                            putc(this_elem->tmatrix[getelem(this_elem,k)],
727                                 file);
728                    }
729                }
730                else
731                {
732                    for(i=0,k=this_elem->offset;k<this_elem->seqlen+this_elem->offset;k++)
733                        if(aln->element[mask].seqlen+this_elem->offset>k)
734                            if((char)getelem(&(aln->element[mask]),k) != '0'
735                               && ((char)getelem(&(aln->element[mask]),k) != '-'))
736                            {
737                                if(i%60 == 0)
738                                    putc('\n',file);
739                                putc(this_elem->tmatrix[getelem(this_elem,k)],
740                                     file);
741                                i++;
742                            }
743                }
744                fprintf(file,"\"\n");
745            }
746            else
747            {
748                if(mask == -1)
749                {
750                    for(k=this_elem->offset;k<this_elem->seqlen+this_elem->offset;k++)
751                    {
752                        if(k%60 == 0)
753                            putc('\n',file);
754                        if(method == SELECT_REGION)
755                        {
756                            if(aln->selection_mask[k] == '1')
757                                putc(getelem(this_elem,k),file);
758                        }
759                        else
760                            putc(getelem(this_elem,k),file);
761                    }
762                }
763                else
764                {
765                    for(i=0,k=this_elem->offset;k<this_elem->seqlen+this_elem->offset;k++)
766                        if(((aln->element[mask].seqlen)+(aln->element[mask].
767                                                         offset)) > k)
768                            if((char)getelem(&(aln->element[mask]),k) == '1')
769                            {
770                                if(i%60 == 0)
771                                    putc('\n',file);
772                                putc(getelem(this_elem,k),file);
773                                i++;
774                            }
775                }
776                fprintf(file,"\"\n");
777            }
778            fprintf(file,"}\n");
779        }
780    }
781    fclose(file);
782    return(0);
783}
784
785
786
787/*
788 *       Normalize seq (remove leading indels in the sequence;
789 */
790void SeqNorm(NA_Sequence *seq)
791{
792    int len,j,shift_width,trailer;
793    char *sequence;
794    len = seq->seqlen;
795
796    sequence =(char*)seq->sequence;
797
798    if(len == 0) return;
799
800    if(seq->tmatrix) {
801        for(shift_width=0; (shift_width<len) && ((sequence[shift_width]&15) == '\0'); shift_width++) ;
802    }
803    else {
804        for(shift_width=0; (shift_width<len) && (sequence[shift_width] == '-'); shift_width++) ;
805    }
806
807    for(j=0;j<len-shift_width;j++) {
808        sequence[j] = sequence[j+shift_width];
809    }
810
811    seq->seqlen -= shift_width;
812    seq->offset += shift_width;
813    for (trailer=seq->seqlen-1;
814         (sequence[trailer] =='-' || sequence[trailer] == '\0') && trailer>=0;
815         trailer--)
816    {
817        sequence[trailer] = '\0';
818    }
819    seq->seqlen = trailer+1;
820    return;
821}
822
823/* ALWAYS COPY the result from uniqueID() to a char[32],
824 * (strlen(hostname)+1+10).  Memory is lost when the function
825 * is finished.
826 */
827
828char *uniqueID()
829{
830    static char vname[32];
831    time_t *tp;
832    static int cnt = 0;
833
834    tp = (time_t *)Calloc(1, sizeof(time_t));
835
836    time(tp);
837    sprintf(vname, "host:%d:%ld", cnt, *tp);
838    cnt++;
839    Cfree((char*)tp);
840    return(vname);
841}
842
Note: See TracBrowser for help on using the repository browser.